1 From c503e92983f2e18c1e18e21b15d5bedd6d0be8ac Mon Sep 17 00:00:00 2001
2 From: Biwen Li <biwen.li@nxp.com>
3 Date: Tue, 30 Oct 2018 18:26:56 +0800
4 Subject: [PATCH 33/40] pcie: support layerscape
5 This is an integrated patch of pcie for layerscape
7 Signed-off-by: Bao Xiaowei <xiaowei.bao@nxp.com>
8 Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
9 Signed-off-by: Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
10 Signed-off-by: Minghuan Lian <Minghuan.Lian@nxp.com>
11 Signed-off-by: Po Liu <po.liu@nxp.com>
12 Signed-off-by: Zhang Ying-22455 <ying.zhang22455@nxp.com>
13 Signed-off-by: Biwen Li <biwen.li@nxp.com>
15 .../bindings/pci/layerscape-pci.txt | 14 +-
16 arch/arm/kernel/bios32.c | 43 ++++++
17 arch/arm64/kernel/pci.c | 43 ++++++
18 drivers/misc/pci_endpoint_test.c | 20 +--
19 drivers/pci/dwc/Kconfig | 8 ++
20 drivers/pci/dwc/pci-layerscape.c | 132 +++++++++++++++++-
21 drivers/pci/endpoint/functions/pci-epf-test.c | 2 +-
22 drivers/pci/endpoint/pci-epf-core.c | 8 +-
23 drivers/pci/pcie/portdrv_core.c | 29 ++++
24 drivers/pci/quirks.c | 15 ++
25 include/linux/pci.h | 1 +
26 11 files changed, 292 insertions(+), 23 deletions(-)
28 --- a/Documentation/devicetree/bindings/pci/layerscape-pci.txt
29 +++ b/Documentation/devicetree/bindings/pci/layerscape-pci.txt
30 @@ -18,11 +18,16 @@ Required properties:
35 - reg: base addresses and lengths of the PCIe controller register blocks.
36 - interrupts: A list of interrupt outputs of the controller. Must contain an
37 entry for each entry in the interrupt-names property.
38 -- interrupt-names: Must include the following entries:
39 - "intr": The interrupt that is asserted for controller interrupts
40 +- interrupt-names: It could include the following entries:
41 + "aer": Asserted for aer interrupt when chip support the aer interrupt with
42 + none MSI/MSI-X/INTx mode,but there is interrupt line for aer.
43 + "pme": Asserted for pme interrupt when chip support the pme interrupt with
44 + none MSI/MSI-X/INTx mode,but there is interrupt line for pme.
46 - fsl,pcie-scfg: Must include two entries.
47 The first entry must be a link to the SCFG device node
48 The second entry must be '0' or '1' based on physical PCIe controller index.
49 @@ -38,8 +43,9 @@ Example:
50 reg = <0x00 0x03400000 0x0 0x00010000 /* controller registers */
51 0x40 0x00000000 0x0 0x00002000>; /* configuration space */
52 reg-names = "regs", "config";
53 - interrupts = <GIC_SPI 177 IRQ_TYPE_LEVEL_HIGH>; /* controller interrupt */
54 - interrupt-names = "intr";
55 + interrupts = <GIC_SPI 176 IRQ_TYPE_LEVEL_HIGH>, /* aer interrupt */
56 + <GIC_SPI 177 IRQ_TYPE_LEVEL_HIGH>; /* pme interrupt */
57 + interrupt-names = "aer", "pme";
58 fsl,pcie-scfg = <&scfg 0>;
61 --- a/arch/arm/kernel/bios32.c
62 +++ b/arch/arm/kernel/bios32.c
64 #include <linux/slab.h>
65 #include <linux/init.h>
67 +#include <linux/of_irq.h>
68 +#include <linux/pcieport_if.h>
70 #include <asm/mach-types.h>
71 #include <asm/mach/map.h>
72 @@ -65,6 +67,47 @@ void pcibios_report_status(u_int status_
76 + * Check device tree if the service interrupts are there
78 +int pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask)
81 + struct device_node *np = NULL;
83 + if (dev->bus->dev.of_node)
84 + np = dev->bus->dev.of_node;
89 + if (!IS_ENABLED(CONFIG_OF_IRQ))
92 + /* If root port doesn't support MSI/MSI-X/INTx in RC mode,
93 + * request irq for aer
95 + if (mask & PCIE_PORT_SERVICE_AER) {
96 + ret = of_irq_get_byname(np, "aer");
98 + irqs[PCIE_PORT_SERVICE_AER_SHIFT] = ret;
103 + if (mask & PCIE_PORT_SERVICE_PME) {
104 + ret = of_irq_get_byname(np, "pme");
106 + irqs[PCIE_PORT_SERVICE_PME_SHIFT] = ret;
111 + /* TODO: add more service interrupts if there it is in the device tree*/
117 * We don't use this to fix the device, but initialisation of it.
118 * It's not the correct use for this, but it works.
119 * Note that the arbiter/ISA bridge appears to be buggy, specifically in
120 --- a/arch/arm64/kernel/pci.c
121 +++ b/arch/arm64/kernel/pci.c
123 #include <linux/mm.h>
124 #include <linux/of_pci.h>
125 #include <linux/of_platform.h>
126 +#include <linux/of_irq.h>
127 +#include <linux/pcieport_if.h>
128 #include <linux/pci.h>
129 #include <linux/pci-acpi.h>
130 #include <linux/pci-ecam.h>
131 @@ -36,6 +38,47 @@ int pcibios_alloc_irq(struct pci_dev *de
135 + * Check device tree if the service interrupts are there
137 +int pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask)
139 + int ret, count = 0;
140 + struct device_node *np = NULL;
142 + if (dev->bus->dev.of_node)
143 + np = dev->bus->dev.of_node;
148 + if (!IS_ENABLED(CONFIG_OF_IRQ))
151 + /* If root port doesn't support MSI/MSI-X/INTx in RC mode,
152 + * request irq for aer
154 + if (mask & PCIE_PORT_SERVICE_AER) {
155 + ret = of_irq_get_byname(np, "aer");
157 + irqs[PCIE_PORT_SERVICE_AER_SHIFT] = ret;
162 + if (mask & PCIE_PORT_SERVICE_PME) {
163 + ret = of_irq_get_byname(np, "pme");
165 + irqs[PCIE_PORT_SERVICE_PME_SHIFT] = ret;
170 + /* TODO: add more service interrupts if there it is in the device tree*/
176 * raw_pci_read/write - Platform-specific PCI config space access.
178 int raw_pci_read(unsigned int domain, unsigned int bus,
179 --- a/drivers/misc/pci_endpoint_test.c
180 +++ b/drivers/misc/pci_endpoint_test.c
181 @@ -97,6 +97,8 @@ struct pci_endpoint_test {
182 struct miscdevice miscdev;
183 enum pci_barno test_reg_bar;
189 struct pci_endpoint_test_data {
190 @@ -454,9 +456,7 @@ static int pci_endpoint_test_probe(struc
199 struct device *dev = &pdev->dev;
200 @@ -501,19 +501,19 @@ static int pci_endpoint_test_probe(struc
201 pci_set_master(pdev);
204 - irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
206 + test->irq_num = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
207 + if (test->irq_num < 0)
208 dev_err(dev, "failed to get MSI interrupts\n");
211 - err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
212 + err = request_irq(pdev->irq, pci_endpoint_test_irqhandler,
213 IRQF_SHARED, DRV_MODULE_NAME, test);
215 dev_err(dev, "failed to request IRQ %d\n", pdev->irq);
216 goto err_disable_msi;
219 - for (i = 1; i < irq; i++) {
220 + for (i = 1; i < test->irq_num; i++) {
221 err = devm_request_irq(dev, pdev->irq + i,
222 pci_endpoint_test_irqhandler,
223 IRQF_SHARED, DRV_MODULE_NAME, test);
224 @@ -548,10 +548,10 @@ static int pci_endpoint_test_probe(struc
228 - snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
229 + snprintf(test->name, sizeof(test->name), DRV_MODULE_NAME ".%d", id);
230 misc_device = &test->miscdev;
231 misc_device->minor = MISC_DYNAMIC_MINOR;
232 - misc_device->name = name;
233 + misc_device->name = test->name;
234 misc_device->fops = &pci_endpoint_test_fops,
236 err = misc_register(misc_device);
237 @@ -584,6 +584,7 @@ err_disable_pdev:
238 static void pci_endpoint_test_remove(struct pci_dev *pdev)
243 struct pci_endpoint_test *test = pci_get_drvdata(pdev);
244 struct miscdevice *misc_device = &test->miscdev;
245 @@ -599,6 +600,8 @@ static void pci_endpoint_test_remove(str
247 pci_iounmap(pdev, test->bar[bar]);
249 + for (i = 0; i < test->irq_num; i++)
250 + free_irq(pdev->irq + i, test);
251 pci_disable_msi(pdev);
252 pci_release_regions(pdev);
253 pci_disable_device(pdev);
254 @@ -607,6 +610,7 @@ static void pci_endpoint_test_remove(str
255 static const struct pci_device_id pci_endpoint_test_tbl[] = {
256 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
257 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
258 + { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID) },
261 MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
262 --- a/drivers/pci/dwc/Kconfig
263 +++ b/drivers/pci/dwc/Kconfig
264 @@ -111,6 +111,14 @@ config PCI_LAYERSCAPE
266 Say Y here if you want PCIe controller support on Layerscape SoCs.
268 +config PCI_LAYERSCAPE_EP
269 + bool "PCI layerscape Endpoint Mode"
270 + depends on PCI_ENDPOINT
273 + Enables support for the PCIe controller in the layerscape SoC to work in
277 depends on OF && ARM64
278 bool "HiSilicon Hip05 and Hip06 SoCs PCIe controllers"
279 --- a/drivers/pci/dwc/pci-layerscape.c
280 +++ b/drivers/pci/dwc/pci-layerscape.c
283 /* PEX Internal Configuration Registers */
284 #define PCIE_STRFMR1 0x71c /* Symbol Timer & Filter Mask Register1 */
285 +#define PCIE_ABSERR 0x8d0 /* Bridge Slave Error Response Register */
286 +#define PCIE_ABSERR_SETTING 0x9401 /* Forward error of non-posted request */
288 +#define PCIE_DBI2_BASE 0x1000 /* DBI2 base address*/
289 +#define PCIE_MSI_MSG_DATA_OFF 0x5c /* MSI Data register address*/
290 +#define PCIE_MSI_OB_SIZE 4096
291 +#define PCIE_MSI_ADDR_OFFSET (1024 * 1024)
292 #define PCIE_IATU_NUM 6
293 +#define PCIE_EP_ADDR_SPACE_SIZE 0x100000000
295 struct ls_pcie_drvdata {
297 @@ -44,12 +51,20 @@ struct ls_pcie_drvdata {
298 const struct dw_pcie_ops *dw_pcie_ops;
302 + dma_addr_t msi_phys_addr;
303 + void __iomem *msi_virt_addr;
312 const struct ls_pcie_drvdata *drvdata;
314 + struct ls_pcie_ep *pcie_ep;
317 #define to_ls_pcie(x) dev_get_drvdata((x)->dev)
318 @@ -124,6 +139,14 @@ static int ls_pcie_link_up(struct dw_pci
322 +/* Forward error response of outbound non-posted requests */
323 +static void ls_pcie_fix_error_response(struct ls_pcie *pcie)
325 + struct dw_pcie *pci = pcie->pci;
327 + iowrite32(PCIE_ABSERR_SETTING, pci->dbi_base + PCIE_ABSERR);
330 static int ls_pcie_host_init(struct pcie_port *pp)
332 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
333 @@ -135,6 +158,7 @@ static int ls_pcie_host_init(struct pcie
334 * dw_pcie_setup_rc() will reconfigure the outbound windows.
336 ls_pcie_disable_outbound_atus(pcie);
337 + ls_pcie_fix_error_response(pcie);
339 dw_pcie_dbi_ro_wr_en(pci);
340 ls_pcie_clear_multifunction(pcie);
341 @@ -253,6 +277,7 @@ static struct ls_pcie_drvdata ls2088_drv
344 static const struct of_device_id ls_pcie_of_match[] = {
345 + { .compatible = "fsl,ls1012a-pcie", .data = &ls1046_drvdata },
346 { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
347 { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
348 { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
349 @@ -263,6 +288,99 @@ static const struct of_device_id ls_pcie
353 +static void ls_pcie_raise_msi_irq(struct ls_pcie_ep *pcie_ep)
355 + iowrite32(pcie_ep->msi_msg_data, pcie_ep->msi_virt_addr);
358 +static int ls_pcie_raise_irq(struct dw_pcie_ep *ep,
359 + enum pci_epc_irq_type type, u8 interrupt_num)
361 + struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
362 + struct ls_pcie *pcie = to_ls_pcie(pci);
363 + struct ls_pcie_ep *pcie_ep = pcie->pcie_ep;
366 + /* get the msi message address and msi message data */
367 + pcie_ep->msi_msg_addr = ioread32(pci->dbi_base + MSI_MESSAGE_ADDR_L32) |
368 + (((u64)ioread32(pci->dbi_base + MSI_MESSAGE_ADDR_U32)) << 32);
369 + pcie_ep->msi_msg_data = ioread16(pci->dbi_base + PCIE_MSI_MSG_DATA_OFF);
371 + /* request and config the outband window for msi */
372 + free_win = find_first_zero_bit(&ep->ob_window_map,
373 + sizeof(ep->ob_window_map));
374 + if (free_win >= ep->num_ob_windows) {
375 + dev_err(pci->dev, "no free outbound window\n");
379 + dw_pcie_prog_outbound_atu(pci, free_win, PCIE_ATU_TYPE_MEM,
380 + pcie_ep->msi_phys_addr,
381 + pcie_ep->msi_msg_addr,
384 + set_bit(free_win, &ep->ob_window_map);
386 + /* generate the msi interrupt */
387 + ls_pcie_raise_msi_irq(pcie_ep);
389 + /* release the outband window of msi */
390 + dw_pcie_disable_atu(pci, free_win, DW_PCIE_REGION_OUTBOUND);
391 + clear_bit(free_win, &ep->ob_window_map);
396 +static struct dw_pcie_ep_ops pcie_ep_ops = {
397 + .raise_irq = ls_pcie_raise_irq,
400 +static int __init ls_add_pcie_ep(struct ls_pcie *pcie,
401 + struct platform_device *pdev)
403 + struct dw_pcie *pci = pcie->pci;
404 + struct device *dev = pci->dev;
405 + struct dw_pcie_ep *ep;
406 + struct ls_pcie_ep *pcie_ep;
407 + struct resource *cfg_res;
411 + ep->ops = &pcie_ep_ops;
413 + pcie_ep = devm_kzalloc(dev, sizeof(*pcie_ep), GFP_KERNEL);
417 + pcie->pcie_ep = pcie_ep;
419 + cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
421 + ep->phys_base = cfg_res->start;
422 + ep->addr_size = PCIE_EP_ADDR_SPACE_SIZE;
424 + dev_err(dev, "missing *config* space\n");
428 + pcie_ep->msi_phys_addr = ep->phys_base + PCIE_MSI_ADDR_OFFSET;
430 + pcie_ep->msi_virt_addr = ioremap(pcie_ep->msi_phys_addr,
432 + if (!pcie_ep->msi_virt_addr) {
433 + dev_err(dev, "failed to map MSI outbound region\n");
437 + ret = dw_pcie_ep_init(ep);
439 + dev_err(dev, "failed to initialize endpoint\n");
446 static int __init ls_add_pcie_port(struct ls_pcie *pcie)
448 struct dw_pcie *pci = pcie->pci;
449 @@ -309,18 +427,18 @@ static int __init ls_pcie_probe(struct p
450 if (IS_ERR(pci->dbi_base))
451 return PTR_ERR(pci->dbi_base);
453 - pcie->lut = pci->dbi_base + pcie->drvdata->lut_offset;
454 + pci->dbi_base2 = pci->dbi_base + PCIE_DBI2_BASE;
456 - if (!ls_pcie_is_bridge(pcie))
458 + pcie->lut = pci->dbi_base + pcie->drvdata->lut_offset;
460 platform_set_drvdata(pdev, pcie);
462 - ret = ls_add_pcie_port(pcie);
465 + if (!ls_pcie_is_bridge(pcie))
466 + ret = ls_add_pcie_ep(pcie, pdev);
468 + ret = ls_add_pcie_port(pcie);
474 static struct platform_driver ls_pcie_driver = {
475 --- a/drivers/pci/endpoint/functions/pci-epf-test.c
476 +++ b/drivers/pci/endpoint/functions/pci-epf-test.c
477 @@ -471,7 +471,7 @@ static int pci_epf_test_probe(struct pci
478 const struct pci_epf_device_id *match;
479 struct pci_epf_test_data *data;
480 enum pci_barno test_reg_bar = BAR_0;
481 - bool linkup_notifier = true;
482 + bool linkup_notifier = false;
484 match = pci_epf_match_device(pci_epf_test_ids, epf);
485 data = (struct pci_epf_test_data *)match->driver_data;
486 --- a/drivers/pci/endpoint/pci-epf-core.c
487 +++ b/drivers/pci/endpoint/pci-epf-core.c
488 @@ -104,8 +104,8 @@ void pci_epf_free_space(struct pci_epf *
492 - dma_free_coherent(dev, epf->bar[bar].size, addr,
493 - epf->bar[bar].phys_addr);
494 + free_pages((unsigned long)addr,
495 + get_order(epf->bar[bar].size));
497 epf->bar[bar].phys_addr = 0;
498 epf->bar[bar].size = 0;
499 @@ -129,7 +129,9 @@ void *pci_epf_alloc_space(struct pci_epf
501 size = roundup_pow_of_two(size);
503 - space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
504 + space = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
506 + phys_addr = virt_to_phys(space);
508 dev_err(dev, "failed to allocate mem space\n");
510 --- a/drivers/pci/pcie/portdrv_core.c
511 +++ b/drivers/pci/pcie/portdrv_core.c
512 @@ -45,6 +45,20 @@ static void release_pcie_device(struct d
516 + * pcibios_check_service_irqs - check irqs in the device tree
517 + * @dev: PCI Express port to handle
518 + * @irqs: Array of irqs to populate
519 + * @mask: Bitmask of port capabilities returned by get_port_device_capability()
521 + * Return value: 0 means no service irqs in the device tree
524 +int __weak pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask)
530 * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
532 * @dev: PCI Express port to handle
533 @@ -185,10 +199,25 @@ out_free_irqs:
534 static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
539 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
542 + /* Check if some platforms owns independent irq pins for AER/PME etc.
543 + * Some platforms may own independent AER/PME interrupts and set
544 + * them in the device tree file.
546 + ret = pcibios_check_service_irqs(dev, irqs, mask);
550 + for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
551 + if (irqs[i] == -1 && i != PCIE_PORT_SERVICE_VC_SHIFT)
557 * If we support PME or hotplug, but we can't use MSI/MSI-X for
558 * them, we have to fall back to INTx or other interrupts, e.g., a
559 --- a/drivers/pci/quirks.c
560 +++ b/drivers/pci/quirks.c
561 @@ -3376,6 +3376,13 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_A
562 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATHEROS, 0x003c, quirk_no_bus_reset);
563 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATHEROS, 0x0033, quirk_no_bus_reset);
566 + * NXP (Freescale Vendor ID) LS1088 chips do not behave correctly after
567 + * bus reset. Link state of device does not comes UP and so config space
568 + * never accessible again.
570 +DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, 0x80c0, quirk_no_bus_reset);
572 static void quirk_no_pm_reset(struct pci_dev *dev)
575 @@ -4857,3 +4864,11 @@ static void quirk_no_ats(struct pci_dev
576 /* AMD Stoney platform GPU */
577 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x98e4, quirk_no_ats);
578 #endif /* CONFIG_PCI_ATS */
580 +/* Freescale PCIe doesn't support MSI in RC mode */
581 +static void quirk_fsl_no_msi(struct pci_dev *pdev)
583 + if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT)
586 +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, quirk_fsl_no_msi);
587 --- a/include/linux/pci.h
588 +++ b/include/linux/pci.h
589 @@ -1944,6 +1944,7 @@ void pcibios_release_device(struct pci_d
590 void pcibios_penalize_isa_irq(int irq, int active);
591 int pcibios_alloc_irq(struct pci_dev *dev);
592 void pcibios_free_irq(struct pci_dev *dev);
593 +int pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask);
595 #ifdef CONFIG_HIBERNATE_CALLBACKS
596 extern struct dev_pm_ops pcibios_pm_ops;