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