mm: replace access_process_vm() write parameter with gup_flags
[cascardo/linux.git] / drivers / pci / host / pcie-xilinx.c
1 /*
2  * PCIe host controller driver for Xilinx AXI PCIe Bridge
3  *
4  * Copyright (c) 2012 - 2014 Xilinx, Inc.
5  *
6  * Based on the Tegra PCIe driver
7  *
8  * Bits taken from Synopsys Designware Host controller driver and
9  * ARM PCI Host generic driver.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 2 of the License, or
14  * (at your option) any later version.
15  */
16
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/msi.h>
23 #include <linux/of_address.h>
24 #include <linux/of_pci.h>
25 #include <linux/of_platform.h>
26 #include <linux/of_irq.h>
27 #include <linux/pci.h>
28 #include <linux/platform_device.h>
29
30 /* Register definitions */
31 #define XILINX_PCIE_REG_BIR             0x00000130
32 #define XILINX_PCIE_REG_IDR             0x00000138
33 #define XILINX_PCIE_REG_IMR             0x0000013c
34 #define XILINX_PCIE_REG_PSCR            0x00000144
35 #define XILINX_PCIE_REG_RPSC            0x00000148
36 #define XILINX_PCIE_REG_MSIBASE1        0x0000014c
37 #define XILINX_PCIE_REG_MSIBASE2        0x00000150
38 #define XILINX_PCIE_REG_RPEFR           0x00000154
39 #define XILINX_PCIE_REG_RPIFR1          0x00000158
40 #define XILINX_PCIE_REG_RPIFR2          0x0000015c
41
42 /* Interrupt registers definitions */
43 #define XILINX_PCIE_INTR_LINK_DOWN      BIT(0)
44 #define XILINX_PCIE_INTR_ECRC_ERR       BIT(1)
45 #define XILINX_PCIE_INTR_STR_ERR        BIT(2)
46 #define XILINX_PCIE_INTR_HOT_RESET      BIT(3)
47 #define XILINX_PCIE_INTR_CFG_TIMEOUT    BIT(8)
48 #define XILINX_PCIE_INTR_CORRECTABLE    BIT(9)
49 #define XILINX_PCIE_INTR_NONFATAL       BIT(10)
50 #define XILINX_PCIE_INTR_FATAL          BIT(11)
51 #define XILINX_PCIE_INTR_INTX           BIT(16)
52 #define XILINX_PCIE_INTR_MSI            BIT(17)
53 #define XILINX_PCIE_INTR_SLV_UNSUPP     BIT(20)
54 #define XILINX_PCIE_INTR_SLV_UNEXP      BIT(21)
55 #define XILINX_PCIE_INTR_SLV_COMPL      BIT(22)
56 #define XILINX_PCIE_INTR_SLV_ERRP       BIT(23)
57 #define XILINX_PCIE_INTR_SLV_CMPABT     BIT(24)
58 #define XILINX_PCIE_INTR_SLV_ILLBUR     BIT(25)
59 #define XILINX_PCIE_INTR_MST_DECERR     BIT(26)
60 #define XILINX_PCIE_INTR_MST_SLVERR     BIT(27)
61 #define XILINX_PCIE_INTR_MST_ERRP       BIT(28)
62 #define XILINX_PCIE_IMR_ALL_MASK        0x1FF30FED
63 #define XILINX_PCIE_IDR_ALL_MASK        0xFFFFFFFF
64
65 /* Root Port Error FIFO Read Register definitions */
66 #define XILINX_PCIE_RPEFR_ERR_VALID     BIT(18)
67 #define XILINX_PCIE_RPEFR_REQ_ID        GENMASK(15, 0)
68 #define XILINX_PCIE_RPEFR_ALL_MASK      0xFFFFFFFF
69
70 /* Root Port Interrupt FIFO Read Register 1 definitions */
71 #define XILINX_PCIE_RPIFR1_INTR_VALID   BIT(31)
72 #define XILINX_PCIE_RPIFR1_MSI_INTR     BIT(30)
73 #define XILINX_PCIE_RPIFR1_INTR_MASK    GENMASK(28, 27)
74 #define XILINX_PCIE_RPIFR1_ALL_MASK     0xFFFFFFFF
75 #define XILINX_PCIE_RPIFR1_INTR_SHIFT   27
76
77 /* Bridge Info Register definitions */
78 #define XILINX_PCIE_BIR_ECAM_SZ_MASK    GENMASK(18, 16)
79 #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT   16
80
81 /* Root Port Interrupt FIFO Read Register 2 definitions */
82 #define XILINX_PCIE_RPIFR2_MSG_DATA     GENMASK(15, 0)
83
84 /* Root Port Status/control Register definitions */
85 #define XILINX_PCIE_REG_RPSC_BEN        BIT(0)
86
87 /* Phy Status/Control Register definitions */
88 #define XILINX_PCIE_REG_PSCR_LNKUP      BIT(11)
89
90 /* ECAM definitions */
91 #define ECAM_BUS_NUM_SHIFT              20
92 #define ECAM_DEV_NUM_SHIFT              12
93
94 /* Number of MSI IRQs */
95 #define XILINX_NUM_MSI_IRQS             128
96
97 /**
98  * struct xilinx_pcie_port - PCIe port information
99  * @reg_base: IO Mapped Register Base
100  * @irq: Interrupt number
101  * @msi_pages: MSI pages
102  * @root_busno: Root Bus number
103  * @dev: Device pointer
104  * @msi_domain: MSI IRQ domain pointer
105  * @leg_domain: Legacy IRQ domain pointer
106  * @resources: Bus Resources
107  */
108 struct xilinx_pcie_port {
109         void __iomem *reg_base;
110         u32 irq;
111         unsigned long msi_pages;
112         u8 root_busno;
113         struct device *dev;
114         struct irq_domain *msi_domain;
115         struct irq_domain *leg_domain;
116         struct list_head resources;
117 };
118
119 static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
120
121 static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
122 {
123         return readl(port->reg_base + reg);
124 }
125
126 static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
127 {
128         writel(val, port->reg_base + reg);
129 }
130
131 static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
132 {
133         return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
134                 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
135 }
136
137 /**
138  * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
139  * @port: PCIe port information
140  */
141 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
142 {
143         unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
144
145         if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
146                 dev_dbg(port->dev, "Requester ID %lu\n",
147                         val & XILINX_PCIE_RPEFR_REQ_ID);
148                 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
149                            XILINX_PCIE_REG_RPEFR);
150         }
151 }
152
153 /**
154  * xilinx_pcie_valid_device - Check if a valid device is present on bus
155  * @bus: PCI Bus structure
156  * @devfn: device/function
157  *
158  * Return: 'true' on success and 'false' if invalid device is found
159  */
160 static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
161 {
162         struct xilinx_pcie_port *port = bus->sysdata;
163
164         /* Check if link is up when trying to access downstream ports */
165         if (bus->number != port->root_busno)
166                 if (!xilinx_pcie_link_is_up(port))
167                         return false;
168
169         /* Only one device down on each root port */
170         if (bus->number == port->root_busno && devfn > 0)
171                 return false;
172
173         return true;
174 }
175
176 /**
177  * xilinx_pcie_map_bus - Get configuration base
178  * @bus: PCI Bus structure
179  * @devfn: Device/function
180  * @where: Offset from base
181  *
182  * Return: Base address of the configuration space needed to be
183  *         accessed.
184  */
185 static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
186                                          unsigned int devfn, int where)
187 {
188         struct xilinx_pcie_port *port = bus->sysdata;
189         int relbus;
190
191         if (!xilinx_pcie_valid_device(bus, devfn))
192                 return NULL;
193
194         relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
195                  (devfn << ECAM_DEV_NUM_SHIFT);
196
197         return port->reg_base + relbus + where;
198 }
199
200 /* PCIe operations */
201 static struct pci_ops xilinx_pcie_ops = {
202         .map_bus = xilinx_pcie_map_bus,
203         .read   = pci_generic_config_read,
204         .write  = pci_generic_config_write,
205 };
206
207 /* MSI functions */
208
209 /**
210  * xilinx_pcie_destroy_msi - Free MSI number
211  * @irq: IRQ to be freed
212  */
213 static void xilinx_pcie_destroy_msi(unsigned int irq)
214 {
215         struct msi_desc *msi;
216         struct xilinx_pcie_port *port;
217         struct irq_data *d = irq_get_irq_data(irq);
218         irq_hw_number_t hwirq = irqd_to_hwirq(d);
219
220         if (!test_bit(hwirq, msi_irq_in_use)) {
221                 msi = irq_get_msi_desc(irq);
222                 port = msi_desc_to_pci_sysdata(msi);
223                 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
224         } else {
225                 clear_bit(hwirq, msi_irq_in_use);
226         }
227 }
228
229 /**
230  * xilinx_pcie_assign_msi - Allocate MSI number
231  * @port: PCIe port structure
232  *
233  * Return: A valid IRQ on success and error value on failure.
234  */
235 static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
236 {
237         int pos;
238
239         pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
240         if (pos < XILINX_NUM_MSI_IRQS)
241                 set_bit(pos, msi_irq_in_use);
242         else
243                 return -ENOSPC;
244
245         return pos;
246 }
247
248 /**
249  * xilinx_msi_teardown_irq - Destroy the MSI
250  * @chip: MSI Chip descriptor
251  * @irq: MSI IRQ to destroy
252  */
253 static void xilinx_msi_teardown_irq(struct msi_controller *chip,
254                                     unsigned int irq)
255 {
256         xilinx_pcie_destroy_msi(irq);
257         irq_dispose_mapping(irq);
258 }
259
260 /**
261  * xilinx_pcie_msi_setup_irq - Setup MSI request
262  * @chip: MSI chip pointer
263  * @pdev: PCIe device pointer
264  * @desc: MSI descriptor pointer
265  *
266  * Return: '0' on success and error value on failure
267  */
268 static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
269                                      struct pci_dev *pdev,
270                                      struct msi_desc *desc)
271 {
272         struct xilinx_pcie_port *port = pdev->bus->sysdata;
273         unsigned int irq;
274         int hwirq;
275         struct msi_msg msg;
276         phys_addr_t msg_addr;
277
278         hwirq = xilinx_pcie_assign_msi(port);
279         if (hwirq < 0)
280                 return hwirq;
281
282         irq = irq_create_mapping(port->msi_domain, hwirq);
283         if (!irq)
284                 return -EINVAL;
285
286         irq_set_msi_desc(irq, desc);
287
288         msg_addr = virt_to_phys((void *)port->msi_pages);
289
290         msg.address_hi = 0;
291         msg.address_lo = msg_addr;
292         msg.data = irq;
293
294         pci_write_msi_msg(irq, &msg);
295
296         return 0;
297 }
298
299 /* MSI Chip Descriptor */
300 static struct msi_controller xilinx_pcie_msi_chip = {
301         .setup_irq = xilinx_pcie_msi_setup_irq,
302         .teardown_irq = xilinx_msi_teardown_irq,
303 };
304
305 /* HW Interrupt Chip Descriptor */
306 static struct irq_chip xilinx_msi_irq_chip = {
307         .name = "Xilinx PCIe MSI",
308         .irq_enable = pci_msi_unmask_irq,
309         .irq_disable = pci_msi_mask_irq,
310         .irq_mask = pci_msi_mask_irq,
311         .irq_unmask = pci_msi_unmask_irq,
312 };
313
314 /**
315  * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
316  * @domain: IRQ domain
317  * @irq: Virtual IRQ number
318  * @hwirq: HW interrupt number
319  *
320  * Return: Always returns 0.
321  */
322 static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
323                                irq_hw_number_t hwirq)
324 {
325         irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
326         irq_set_chip_data(irq, domain->host_data);
327
328         return 0;
329 }
330
331 /* IRQ Domain operations */
332 static const struct irq_domain_ops msi_domain_ops = {
333         .map = xilinx_pcie_msi_map,
334 };
335
336 /**
337  * xilinx_pcie_enable_msi - Enable MSI support
338  * @port: PCIe port information
339  */
340 static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
341 {
342         phys_addr_t msg_addr;
343
344         port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
345         msg_addr = virt_to_phys((void *)port->msi_pages);
346         pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
347         pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
348 }
349
350 /* INTx Functions */
351
352 /**
353  * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
354  * @domain: IRQ domain
355  * @irq: Virtual IRQ number
356  * @hwirq: HW interrupt number
357  *
358  * Return: Always returns 0.
359  */
360 static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
361                                 irq_hw_number_t hwirq)
362 {
363         irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
364         irq_set_chip_data(irq, domain->host_data);
365
366         return 0;
367 }
368
369 /* INTx IRQ Domain operations */
370 static const struct irq_domain_ops intx_domain_ops = {
371         .map = xilinx_pcie_intx_map,
372 };
373
374 /* PCIe HW Functions */
375
376 /**
377  * xilinx_pcie_intr_handler - Interrupt Service Handler
378  * @irq: IRQ number
379  * @data: PCIe port information
380  *
381  * Return: IRQ_HANDLED on success and IRQ_NONE on failure
382  */
383 static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
384 {
385         struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
386         u32 val, mask, status, msi_data;
387
388         /* Read interrupt decode and mask registers */
389         val = pcie_read(port, XILINX_PCIE_REG_IDR);
390         mask = pcie_read(port, XILINX_PCIE_REG_IMR);
391
392         status = val & mask;
393         if (!status)
394                 return IRQ_NONE;
395
396         if (status & XILINX_PCIE_INTR_LINK_DOWN)
397                 dev_warn(port->dev, "Link Down\n");
398
399         if (status & XILINX_PCIE_INTR_ECRC_ERR)
400                 dev_warn(port->dev, "ECRC failed\n");
401
402         if (status & XILINX_PCIE_INTR_STR_ERR)
403                 dev_warn(port->dev, "Streaming error\n");
404
405         if (status & XILINX_PCIE_INTR_HOT_RESET)
406                 dev_info(port->dev, "Hot reset\n");
407
408         if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
409                 dev_warn(port->dev, "ECAM access timeout\n");
410
411         if (status & XILINX_PCIE_INTR_CORRECTABLE) {
412                 dev_warn(port->dev, "Correctable error message\n");
413                 xilinx_pcie_clear_err_interrupts(port);
414         }
415
416         if (status & XILINX_PCIE_INTR_NONFATAL) {
417                 dev_warn(port->dev, "Non fatal error message\n");
418                 xilinx_pcie_clear_err_interrupts(port);
419         }
420
421         if (status & XILINX_PCIE_INTR_FATAL) {
422                 dev_warn(port->dev, "Fatal error message\n");
423                 xilinx_pcie_clear_err_interrupts(port);
424         }
425
426         if (status & XILINX_PCIE_INTR_INTX) {
427                 /* INTx interrupt received */
428                 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
429
430                 /* Check whether interrupt valid */
431                 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
432                         dev_warn(port->dev, "RP Intr FIFO1 read error\n");
433                         goto error;
434                 }
435
436                 if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
437                         /* Clear interrupt FIFO register 1 */
438                         pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
439                                    XILINX_PCIE_REG_RPIFR1);
440
441                         /* Handle INTx Interrupt */
442                         val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
443                                 XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
444                         generic_handle_irq(irq_find_mapping(port->leg_domain,
445                                                             val));
446                 }
447         }
448
449         if (status & XILINX_PCIE_INTR_MSI) {
450                 /* MSI Interrupt */
451                 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
452
453                 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
454                         dev_warn(port->dev, "RP Intr FIFO1 read error\n");
455                         goto error;
456                 }
457
458                 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
459                         msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
460                                    XILINX_PCIE_RPIFR2_MSG_DATA;
461
462                         /* Clear interrupt FIFO register 1 */
463                         pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
464                                    XILINX_PCIE_REG_RPIFR1);
465
466                         if (IS_ENABLED(CONFIG_PCI_MSI)) {
467                                 /* Handle MSI Interrupt */
468                                 generic_handle_irq(msi_data);
469                         }
470                 }
471         }
472
473         if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
474                 dev_warn(port->dev, "Slave unsupported request\n");
475
476         if (status & XILINX_PCIE_INTR_SLV_UNEXP)
477                 dev_warn(port->dev, "Slave unexpected completion\n");
478
479         if (status & XILINX_PCIE_INTR_SLV_COMPL)
480                 dev_warn(port->dev, "Slave completion timeout\n");
481
482         if (status & XILINX_PCIE_INTR_SLV_ERRP)
483                 dev_warn(port->dev, "Slave Error Poison\n");
484
485         if (status & XILINX_PCIE_INTR_SLV_CMPABT)
486                 dev_warn(port->dev, "Slave Completer Abort\n");
487
488         if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
489                 dev_warn(port->dev, "Slave Illegal Burst\n");
490
491         if (status & XILINX_PCIE_INTR_MST_DECERR)
492                 dev_warn(port->dev, "Master decode error\n");
493
494         if (status & XILINX_PCIE_INTR_MST_SLVERR)
495                 dev_warn(port->dev, "Master slave error\n");
496
497         if (status & XILINX_PCIE_INTR_MST_ERRP)
498                 dev_warn(port->dev, "Master error poison\n");
499
500 error:
501         /* Clear the Interrupt Decode register */
502         pcie_write(port, status, XILINX_PCIE_REG_IDR);
503
504         return IRQ_HANDLED;
505 }
506
507 /**
508  * xilinx_pcie_init_irq_domain - Initialize IRQ domain
509  * @port: PCIe port information
510  *
511  * Return: '0' on success and error value on failure
512  */
513 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
514 {
515         struct device *dev = port->dev;
516         struct device_node *node = dev->of_node;
517         struct device_node *pcie_intc_node;
518
519         /* Setup INTx */
520         pcie_intc_node = of_get_next_child(node, NULL);
521         if (!pcie_intc_node) {
522                 dev_err(dev, "No PCIe Intc node found\n");
523                 return -ENODEV;
524         }
525
526         port->leg_domain = irq_domain_add_linear(pcie_intc_node, 4,
527                                                  &intx_domain_ops,
528                                                  port);
529         if (!port->leg_domain) {
530                 dev_err(dev, "Failed to get a INTx IRQ domain\n");
531                 return -ENODEV;
532         }
533
534         /* Setup MSI */
535         if (IS_ENABLED(CONFIG_PCI_MSI)) {
536                 port->msi_domain = irq_domain_add_linear(node,
537                                                          XILINX_NUM_MSI_IRQS,
538                                                          &msi_domain_ops,
539                                                          &xilinx_pcie_msi_chip);
540                 if (!port->msi_domain) {
541                         dev_err(dev, "Failed to get a MSI IRQ domain\n");
542                         return -ENODEV;
543                 }
544
545                 xilinx_pcie_enable_msi(port);
546         }
547
548         return 0;
549 }
550
551 /**
552  * xilinx_pcie_init_port - Initialize hardware
553  * @port: PCIe port information
554  */
555 static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
556 {
557         if (xilinx_pcie_link_is_up(port))
558                 dev_info(port->dev, "PCIe Link is UP\n");
559         else
560                 dev_info(port->dev, "PCIe Link is DOWN\n");
561
562         /* Disable all interrupts */
563         pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
564                    XILINX_PCIE_REG_IMR);
565
566         /* Clear pending interrupts */
567         pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
568                          XILINX_PCIE_IMR_ALL_MASK,
569                    XILINX_PCIE_REG_IDR);
570
571         /* Enable all interrupts */
572         pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
573
574         /* Enable the Bridge enable bit */
575         pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
576                          XILINX_PCIE_REG_RPSC_BEN,
577                    XILINX_PCIE_REG_RPSC);
578 }
579
580 /**
581  * xilinx_pcie_parse_dt - Parse Device tree
582  * @port: PCIe port information
583  *
584  * Return: '0' on success and error value on failure
585  */
586 static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
587 {
588         struct device *dev = port->dev;
589         struct device_node *node = dev->of_node;
590         struct resource regs;
591         const char *type;
592         int err;
593
594         type = of_get_property(node, "device_type", NULL);
595         if (!type || strcmp(type, "pci")) {
596                 dev_err(dev, "invalid \"device_type\" %s\n", type);
597                 return -EINVAL;
598         }
599
600         err = of_address_to_resource(node, 0, &regs);
601         if (err) {
602                 dev_err(dev, "missing \"reg\" property\n");
603                 return err;
604         }
605
606         port->reg_base = devm_ioremap_resource(dev, &regs);
607         if (IS_ERR(port->reg_base))
608                 return PTR_ERR(port->reg_base);
609
610         port->irq = irq_of_parse_and_map(node, 0);
611         err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
612                                IRQF_SHARED | IRQF_NO_THREAD,
613                                "xilinx-pcie", port);
614         if (err) {
615                 dev_err(dev, "unable to request irq %d\n", port->irq);
616                 return err;
617         }
618
619         return 0;
620 }
621
622 /**
623  * xilinx_pcie_probe - Probe function
624  * @pdev: Platform device pointer
625  *
626  * Return: '0' on success and error value on failure
627  */
628 static int xilinx_pcie_probe(struct platform_device *pdev)
629 {
630         struct xilinx_pcie_port *port;
631         struct device *dev = &pdev->dev;
632         struct pci_bus *bus;
633         int err;
634         resource_size_t iobase = 0;
635         LIST_HEAD(res);
636
637         if (!dev->of_node)
638                 return -ENODEV;
639
640         port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
641         if (!port)
642                 return -ENOMEM;
643
644         port->dev = dev;
645
646         err = xilinx_pcie_parse_dt(port);
647         if (err) {
648                 dev_err(dev, "Parsing DT failed\n");
649                 return err;
650         }
651
652         xilinx_pcie_init_port(port);
653
654         err = xilinx_pcie_init_irq_domain(port);
655         if (err) {
656                 dev_err(dev, "Failed creating IRQ Domain\n");
657                 return err;
658         }
659
660         err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
661                                                &iobase);
662         if (err) {
663                 dev_err(dev, "Getting bridge resources failed\n");
664                 return err;
665         }
666
667         err = devm_request_pci_bus_resources(dev, &res);
668         if (err)
669                 goto error;
670
671         bus = pci_create_root_bus(&pdev->dev, 0,
672                                   &xilinx_pcie_ops, port, &res);
673         if (!bus) {
674                 err = -ENOMEM;
675                 goto error;
676         }
677
678 #ifdef CONFIG_PCI_MSI
679         xilinx_pcie_msi_chip.dev = port->dev;
680         bus->msi = &xilinx_pcie_msi_chip;
681 #endif
682         pci_scan_child_bus(bus);
683         pci_assign_unassigned_bus_resources(bus);
684 #ifndef CONFIG_MICROBLAZE
685         pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
686 #endif
687         pci_bus_add_devices(bus);
688         platform_set_drvdata(pdev, port);
689
690         return 0;
691
692 error:
693         pci_free_resource_list(&res);
694         return err;
695 }
696
697 static struct of_device_id xilinx_pcie_of_match[] = {
698         { .compatible = "xlnx,axi-pcie-host-1.00.a", },
699         {}
700 };
701
702 static struct platform_driver xilinx_pcie_driver = {
703         .driver = {
704                 .name = "xilinx-pcie",
705                 .of_match_table = xilinx_pcie_of_match,
706                 .suppress_bind_attrs = true,
707         },
708         .probe = xilinx_pcie_probe,
709 };
710 builtin_platform_driver(xilinx_pcie_driver);