1 From 9e6e0a53b29190dbd86a39304b59c3028f5b36c2 Mon Sep 17 00:00:00 2001
2 From: Yangbo Lu <yangbo.lu@nxp.com>
3 Date: Mon, 25 Sep 2017 11:04:10 +0800
4 Subject: [PATCH] pci: support layerscape
6 This is a integrated patch for layerscape pcie support.
8 Signed-off-by: Po Liu <po.liu@nxp.com>
9 Signed-off-by: Liu Gang <Gang.Liu@nxp.com>
10 Signed-off-by: Minghuan Lian <Minghuan.Lian@freescale.com>
11 Signed-off-by: hongbo.wang <hongbo.wang@nxp.com>
12 Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
13 Signed-off-by: Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
14 Signed-off-by: Mingkai Hu <mingkai.hu@nxp.com>
15 Signed-off-by: Christoph Hellwig <hch@lst.de>
16 Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
18 drivers/irqchip/irq-ls-scfg-msi.c | 257 +++++++--
19 drivers/pci/host/Makefile | 2 +-
20 drivers/pci/host/pci-layerscape-ep-debugfs.c | 758 +++++++++++++++++++++++++++
21 drivers/pci/host/pci-layerscape-ep.c | 309 +++++++++++
22 drivers/pci/host/pci-layerscape-ep.h | 115 ++++
23 drivers/pci/host/pci-layerscape.c | 38 +-
24 drivers/pci/host/pcie-designware.c | 6 +
25 drivers/pci/host/pcie-designware.h | 1 +
26 drivers/pci/pcie/portdrv_core.c | 181 +++----
27 include/linux/pci.h | 1 +
28 10 files changed, 1520 insertions(+), 148 deletions(-)
29 create mode 100644 drivers/pci/host/pci-layerscape-ep-debugfs.c
30 create mode 100644 drivers/pci/host/pci-layerscape-ep.c
31 create mode 100644 drivers/pci/host/pci-layerscape-ep.h
33 --- a/drivers/irqchip/irq-ls-scfg-msi.c
34 +++ b/drivers/irqchip/irq-ls-scfg-msi.c
36 #include <linux/irq.h>
37 #include <linux/irqchip/chained_irq.h>
38 #include <linux/irqdomain.h>
39 +#include <linux/of_irq.h>
40 #include <linux/of_pci.h>
41 #include <linux/of_platform.h>
42 #include <linux/spinlock.h>
44 -#define MSI_MAX_IRQS 32
45 -#define MSI_IBS_SHIFT 3
47 +#define MSI_IRQS_PER_MSIR 32
48 +#define MSI_MSIR_OFFSET 4
50 +#define MSI_LS1043V1_1_IRQS_PER_MSIR 8
51 +#define MSI_LS1043V1_1_MSIR_OFFSET 0x10
53 +struct ls_scfg_msi_cfg {
54 + u32 ibs_shift; /* Shift of interrupt bit select */
55 + u32 msir_irqs; /* The irq number per MSIR */
56 + u32 msir_base; /* The base address of MSIR */
59 +struct ls_scfg_msir {
60 + struct ls_scfg_msi *msi_data;
62 + unsigned int gic_irq;
63 + unsigned int bit_start;
64 + unsigned int bit_end;
65 + unsigned int srs; /* Shared interrupt register select */
71 @@ -32,8 +51,11 @@ struct ls_scfg_msi {
72 struct irq_domain *msi_domain;
74 phys_addr_t msiir_addr;
76 - DECLARE_BITMAP(used, MSI_MAX_IRQS);
77 + struct ls_scfg_msi_cfg *cfg;
79 + struct ls_scfg_msir *msir;
81 + unsigned long *used;
84 static struct irq_chip ls_scfg_msi_irq_chip = {
85 @@ -49,19 +71,56 @@ static struct msi_domain_info ls_scfg_ms
86 .chip = &ls_scfg_msi_irq_chip,
89 +static int msi_affinity_flag = 1;
91 +static int __init early_parse_ls_scfg_msi(char *p)
93 + if (p && strncmp(p, "no-affinity", 11) == 0)
94 + msi_affinity_flag = 0;
96 + msi_affinity_flag = 1;
100 +early_param("lsmsi", early_parse_ls_scfg_msi);
102 static void ls_scfg_msi_compose_msg(struct irq_data *data, struct msi_msg *msg)
104 struct ls_scfg_msi *msi_data = irq_data_get_irq_chip_data(data);
106 msg->address_hi = upper_32_bits(msi_data->msiir_addr);
107 msg->address_lo = lower_32_bits(msi_data->msiir_addr);
108 - msg->data = data->hwirq << MSI_IBS_SHIFT;
109 + msg->data = data->hwirq;
111 + if (msi_affinity_flag)
112 + msg->data |= cpumask_first(data->common->affinity);
115 static int ls_scfg_msi_set_affinity(struct irq_data *irq_data,
116 const struct cpumask *mask, bool force)
119 + struct ls_scfg_msi *msi_data = irq_data_get_irq_chip_data(irq_data);
122 + if (!msi_affinity_flag)
126 + cpu = cpumask_any_and(mask, cpu_online_mask);
128 + cpu = cpumask_first(mask);
130 + if (cpu >= msi_data->msir_num)
133 + if (msi_data->msir[cpu].gic_irq <= 0) {
134 + pr_warn("cannot bind the irq to cpu%d\n", cpu);
138 + cpumask_copy(irq_data->common->affinity, mask);
140 + return IRQ_SET_MASK_OK;
143 static struct irq_chip ls_scfg_msi_parent_chip = {
144 @@ -81,8 +140,8 @@ static int ls_scfg_msi_domain_irq_alloc(
145 WARN_ON(nr_irqs != 1);
147 spin_lock(&msi_data->lock);
148 - pos = find_first_zero_bit(msi_data->used, MSI_MAX_IRQS);
149 - if (pos < MSI_MAX_IRQS)
150 + pos = find_first_zero_bit(msi_data->used, msi_data->irqs_num);
151 + if (pos < msi_data->irqs_num)
152 __set_bit(pos, msi_data->used);
155 @@ -106,7 +165,7 @@ static void ls_scfg_msi_domain_irq_free(
159 - if (pos < 0 || pos >= MSI_MAX_IRQS) {
160 + if (pos < 0 || pos >= msi_data->irqs_num) {
161 pr_err("failed to teardown msi. Invalid hwirq %d\n", pos);
164 @@ -123,15 +182,22 @@ static const struct irq_domain_ops ls_sc
166 static void ls_scfg_msi_irq_handler(struct irq_desc *desc)
168 - struct ls_scfg_msi *msi_data = irq_desc_get_handler_data(desc);
169 + struct ls_scfg_msir *msir = irq_desc_get_handler_data(desc);
170 + struct ls_scfg_msi *msi_data = msir->msi_data;
173 + int pos, size, virq, hwirq;
175 chained_irq_enter(irq_desc_get_chip(desc), desc);
177 - val = ioread32be(msi_data->regs + MSIR);
178 - for_each_set_bit(pos, &val, MSI_MAX_IRQS) {
179 - virq = irq_find_mapping(msi_data->parent, (31 - pos));
180 + val = ioread32be(msir->reg);
182 + pos = msir->bit_start;
183 + size = msir->bit_end + 1;
185 + for_each_set_bit_from(pos, &val, size) {
186 + hwirq = ((msir->bit_end - pos) << msi_data->cfg->ibs_shift) |
188 + virq = irq_find_mapping(msi_data->parent, hwirq);
190 generic_handle_irq(virq);
192 @@ -143,7 +209,7 @@ static int ls_scfg_msi_domains_init(stru
194 /* Initialize MSI domain parent */
195 msi_data->parent = irq_domain_add_linear(NULL,
197 + msi_data->irqs_num,
198 &ls_scfg_msi_domain_ops,
200 if (!msi_data->parent) {
201 @@ -164,16 +230,118 @@ static int ls_scfg_msi_domains_init(stru
205 +static int ls_scfg_msi_setup_hwirq(struct ls_scfg_msi *msi_data, int index)
207 + struct ls_scfg_msir *msir;
208 + int virq, i, hwirq;
210 + virq = platform_get_irq(msi_data->pdev, index);
214 + msir = &msi_data->msir[index];
215 + msir->index = index;
216 + msir->msi_data = msi_data;
217 + msir->gic_irq = virq;
218 + msir->reg = msi_data->regs + msi_data->cfg->msir_base + 4 * index;
220 + if (msi_data->cfg->msir_irqs == MSI_LS1043V1_1_IRQS_PER_MSIR) {
221 + msir->bit_start = 32 - ((msir->index + 1) *
222 + MSI_LS1043V1_1_IRQS_PER_MSIR);
223 + msir->bit_end = msir->bit_start +
224 + MSI_LS1043V1_1_IRQS_PER_MSIR - 1;
226 + msir->bit_start = 0;
227 + msir->bit_end = msi_data->cfg->msir_irqs - 1;
230 + irq_set_chained_handler_and_data(msir->gic_irq,
231 + ls_scfg_msi_irq_handler,
234 + if (msi_affinity_flag) {
235 + /* Associate MSIR interrupt to the cpu */
236 + irq_set_affinity(msir->gic_irq, get_cpu_mask(index));
237 + msir->srs = 0; /* This value is determined by the CPU */
241 + /* Release the hwirqs corresponding to this MSIR */
242 + if (!msi_affinity_flag || msir->index == 0) {
243 + for (i = 0; i < msi_data->cfg->msir_irqs; i++) {
244 + hwirq = i << msi_data->cfg->ibs_shift | msir->index;
245 + bitmap_clear(msi_data->used, hwirq, 1);
252 +static int ls_scfg_msi_teardown_hwirq(struct ls_scfg_msir *msir)
254 + struct ls_scfg_msi *msi_data = msir->msi_data;
257 + if (msir->gic_irq > 0)
258 + irq_set_chained_handler_and_data(msir->gic_irq, NULL, NULL);
260 + for (i = 0; i < msi_data->cfg->msir_irqs; i++) {
261 + hwirq = i << msi_data->cfg->ibs_shift | msir->index;
262 + bitmap_set(msi_data->used, hwirq, 1);
268 +static struct ls_scfg_msi_cfg ls1021_msi_cfg = {
270 + .msir_irqs = MSI_IRQS_PER_MSIR,
271 + .msir_base = MSI_MSIR_OFFSET,
274 +static struct ls_scfg_msi_cfg ls1046_msi_cfg = {
276 + .msir_irqs = MSI_IRQS_PER_MSIR,
277 + .msir_base = MSI_MSIR_OFFSET,
280 +static struct ls_scfg_msi_cfg ls1043_v1_1_msi_cfg = {
282 + .msir_irqs = MSI_LS1043V1_1_IRQS_PER_MSIR,
283 + .msir_base = MSI_LS1043V1_1_MSIR_OFFSET,
286 +static const struct of_device_id ls_scfg_msi_id[] = {
287 + /* The following two misspelled compatibles are obsolete */
288 + { .compatible = "fsl,1s1021a-msi", .data = &ls1021_msi_cfg},
289 + { .compatible = "fsl,1s1043a-msi", .data = &ls1021_msi_cfg},
291 + { .compatible = "fsl,ls1012a-msi", .data = &ls1021_msi_cfg },
292 + { .compatible = "fsl,ls1021a-msi", .data = &ls1021_msi_cfg },
293 + { .compatible = "fsl,ls1043a-msi", .data = &ls1021_msi_cfg },
294 + { .compatible = "fsl,ls1043a-v1.1-msi", .data = &ls1043_v1_1_msi_cfg },
295 + { .compatible = "fsl,ls1046a-msi", .data = &ls1046_msi_cfg },
298 +MODULE_DEVICE_TABLE(of, ls_scfg_msi_id);
300 static int ls_scfg_msi_probe(struct platform_device *pdev)
302 + const struct of_device_id *match;
303 struct ls_scfg_msi *msi_data;
304 struct resource *res;
308 + match = of_match_device(ls_scfg_msi_id, &pdev->dev);
312 msi_data = devm_kzalloc(&pdev->dev, sizeof(*msi_data), GFP_KERNEL);
316 + msi_data->cfg = (struct ls_scfg_msi_cfg *) match->data;
318 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
319 msi_data->regs = devm_ioremap_resource(&pdev->dev, res);
320 if (IS_ERR(msi_data->regs)) {
321 @@ -182,23 +350,48 @@ static int ls_scfg_msi_probe(struct plat
323 msi_data->msiir_addr = res->start;
325 - msi_data->irq = platform_get_irq(pdev, 0);
326 - if (msi_data->irq <= 0) {
327 - dev_err(&pdev->dev, "failed to get MSI irq\n");
331 msi_data->pdev = pdev;
332 spin_lock_init(&msi_data->lock);
334 + msi_data->irqs_num = MSI_IRQS_PER_MSIR *
335 + (1 << msi_data->cfg->ibs_shift);
336 + msi_data->used = devm_kcalloc(&pdev->dev,
337 + BITS_TO_LONGS(msi_data->irqs_num),
338 + sizeof(*msi_data->used),
340 + if (!msi_data->used)
343 + * Reserve all the hwirqs
344 + * The available hwirqs will be released in ls1_msi_setup_hwirq()
346 + bitmap_set(msi_data->used, 0, msi_data->irqs_num);
348 + msi_data->msir_num = of_irq_count(pdev->dev.of_node);
350 + if (msi_affinity_flag) {
353 + cpu_num = num_possible_cpus();
354 + if (msi_data->msir_num >= cpu_num)
355 + msi_data->msir_num = cpu_num;
357 + msi_affinity_flag = 0;
360 + msi_data->msir = devm_kcalloc(&pdev->dev, msi_data->msir_num,
361 + sizeof(*msi_data->msir),
363 + if (!msi_data->msir)
366 + for (i = 0; i < msi_data->msir_num; i++)
367 + ls_scfg_msi_setup_hwirq(msi_data, i);
369 ret = ls_scfg_msi_domains_init(msi_data);
373 - irq_set_chained_handler_and_data(msi_data->irq,
374 - ls_scfg_msi_irq_handler,
377 platform_set_drvdata(pdev, msi_data);
380 @@ -207,8 +400,10 @@ static int ls_scfg_msi_probe(struct plat
381 static int ls_scfg_msi_remove(struct platform_device *pdev)
383 struct ls_scfg_msi *msi_data = platform_get_drvdata(pdev);
386 - irq_set_chained_handler_and_data(msi_data->irq, NULL, NULL);
387 + for (i = 0; i < msi_data->msir_num; i++)
388 + ls_scfg_msi_teardown_hwirq(&msi_data->msir[i]);
390 irq_domain_remove(msi_data->msi_domain);
391 irq_domain_remove(msi_data->parent);
392 @@ -218,12 +413,6 @@ static int ls_scfg_msi_remove(struct pla
396 -static const struct of_device_id ls_scfg_msi_id[] = {
397 - { .compatible = "fsl,1s1021a-msi", },
398 - { .compatible = "fsl,1s1043a-msi", },
402 static struct platform_driver ls_scfg_msi_driver = {
404 .name = "ls-scfg-msi",
405 --- a/drivers/pci/host/Makefile
406 +++ b/drivers/pci/host/Makefile
407 @@ -17,7 +17,7 @@ obj-$(CONFIG_PCIE_XILINX) += pcie-xilinx
408 obj-$(CONFIG_PCIE_XILINX_NWL) += pcie-xilinx-nwl.o
409 obj-$(CONFIG_PCI_XGENE) += pci-xgene.o
410 obj-$(CONFIG_PCI_XGENE_MSI) += pci-xgene-msi.o
411 -obj-$(CONFIG_PCI_LAYERSCAPE) += pci-layerscape.o
412 +obj-$(CONFIG_PCI_LAYERSCAPE) += pci-layerscape.o pci-layerscape-ep.o pci-layerscape-ep-debugfs.o
413 obj-$(CONFIG_PCI_VERSATILE) += pci-versatile.o
414 obj-$(CONFIG_PCIE_IPROC) += pcie-iproc.o
415 obj-$(CONFIG_PCIE_IPROC_MSI) += pcie-iproc-msi.o
417 +++ b/drivers/pci/host/pci-layerscape-ep-debugfs.c
420 + * PCIe Endpoint driver for Freescale Layerscape SoCs
422 + * Copyright (C) 2015 Freescale Semiconductor.
424 + * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
426 + * This program is free software; you can redistribute it and/or modify
427 + * it under the terms of the GNU General Public License version 2 as
428 + * published by the Free Software Foundation.
431 +#include <linux/kernel.h>
432 +#include <linux/module.h>
433 +#include <linux/debugfs.h>
434 +#include <linux/time.h>
435 +#include <linux/uaccess.h>
436 +#include <linux/kthread.h>
437 +#include <linux/slab.h>
438 +#include <linux/dmaengine.h>
439 +#include <linux/dma-mapping.h>
440 +#include <linux/freezer.h>
442 +#include <linux/completion.h>
444 +#include "pci-layerscape-ep.h"
446 +#define PCIE_ATU_INDEX3 (0x3 << 0)
447 +#define PCIE_ATU_INDEX2 (0x2 << 0)
448 +#define PCIE_ATU_INDEX1 (0x1 << 0)
449 +#define PCIE_ATU_INDEX0 (0x0 << 0)
451 +#define PCIE_BAR0_SIZE (4 * 1024) /* 4K */
452 +#define PCIE_BAR1_SIZE (8 * 1024) /* 8K for MSIX */
453 +#define PCIE_BAR2_SIZE (4 * 1024) /* 4K */
454 +#define PCIE_BAR4_SIZE (1 * 1024 * 1024) /* 1M */
455 +#define PCIE_MSI_OB_SIZE (4 * 1024) /* 4K */
457 +#define PCIE_MSI_MSG_ADDR_OFF 0x54
458 +#define PCIE_MSI_MSG_DATA_OFF 0x5c
476 + struct ls_ep_dev *ep;
481 + dma_addr_t cfg_addr;
482 + dma_addr_t buf_addr;
483 + dma_addr_t out_addr;
484 + dma_addr_t bus_addr;
485 + dma_addr_t msi_addr;
488 + struct task_struct *thread;
490 + struct completion done;
494 + enum test_dirt dirt;
495 + enum test_type type;
496 + enum test_status status;
497 + u64 result; /* Mbps */
501 +static int ls_pcie_ep_trigger_msi(struct ls_ep_test *test)
506 + iowrite32(test->msi_msg_data, test->msi);
511 +static int ls_pcie_ep_test_try_run(struct ls_ep_test *test)
515 + spin_lock(&test->lock);
516 + if (test->status == TEST_IDLE) {
517 + test->status = TEST_BUSY;
521 + spin_unlock(&test->lock);
526 +static void ls_pcie_ep_test_done(struct ls_ep_test *test)
528 + spin_lock(&test->lock);
529 + test->status = TEST_IDLE;
530 + spin_unlock(&test->lock);
533 +static void ls_pcie_ep_test_dma_cb(void *arg)
535 + struct ls_ep_test *test = arg;
537 + complete(&test->done);
540 +static int ls_pcie_ep_test_dma(struct ls_ep_test *test)
542 + dma_cap_mask_t mask;
543 + struct dma_chan *chan;
544 + struct dma_device *dma_dev;
545 + dma_addr_t src, dst;
546 + enum dma_data_direction direction;
547 + enum dma_ctrl_flags dma_flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
548 + struct timespec start, end, period;
551 + dma_cap_zero(mask);
552 + dma_cap_set(DMA_MEMCPY, mask);
554 + chan = dma_request_channel(mask, NULL, test);
556 + pr_err("failed to request dma channel\n");
560 + memset(test->buf, test->data, test->len);
562 + if (test->dirt == TEST_DIRT_WRITE) {
563 + src = test->buf_addr;
564 + dst = test->out_addr;
565 + direction = DMA_TO_DEVICE;
567 + src = test->out_addr;
568 + dst = test->buf_addr;
569 + direction = DMA_FROM_DEVICE;
572 + dma_dev = chan->device;
573 + dma_flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
575 + dma_sync_single_for_device(&test->ep->dev, test->buf_addr,
576 + test->len, direction);
580 + getrawmonotonic(&start);
581 + while (!kthread_should_stop() && (i < test->loop)) {
582 + struct dma_async_tx_descriptor *dma_desc;
583 + dma_cookie_t dma_cookie = {0};
587 + init_completion(&test->done);
589 + dma_desc = dma_dev->device_prep_dma_memcpy(chan,
594 + pr_err("DMA desc constr failed...\n");
598 + dma_desc->callback = ls_pcie_ep_test_dma_cb;
599 + dma_desc->callback_param = test;
600 + dma_cookie = dmaengine_submit(dma_desc);
602 + if (dma_submit_error(dma_cookie)) {
603 + pr_err("DMA submit error....\n");
607 + /* Trigger the transaction */
608 + dma_async_issue_pending(chan);
610 + tmo = wait_for_completion_timeout(&test->done,
611 + msecs_to_jiffies(5 * test->len));
613 + pr_err("Self-test copy timed out, disabling\n");
617 + status = dma_async_is_tx_complete(chan, dma_cookie,
619 + if (status != DMA_COMPLETE) {
620 + pr_err("got completion callback, but status is %s\n",
621 + status == DMA_ERROR ? "error" : "in progress");
628 + getrawmonotonic(&end);
629 + period = timespec_sub(end, start);
630 + test->result = test->len * 8ULL * i * 1000;
631 + do_div(test->result, period.tv_sec * 1000 * 1000 * 1000 + period.tv_nsec);
632 + dma_release_channel(chan);
637 + dma_release_channel(chan);
642 +static int ls_pcie_ep_test_cpy(struct ls_ep_test *test)
645 + struct timespec start, end, period;
648 + memset(test->buf, test->data, test->len);
650 + if (test->dirt == TEST_DIRT_WRITE) {
658 + getrawmonotonic(&start);
659 + while (!kthread_should_stop() && i < test->loop) {
660 + memcpy(dst, src, test->len);
663 + getrawmonotonic(&end);
665 + period = timespec_sub(end, start);
666 + test->result = test->len * 8ULL * i * 1000;
667 + do_div(test->result, period.tv_sec * 1000 * 1000 * 1000 + period.tv_nsec);
672 +int ls_pcie_ep_test_thread(void *arg)
676 + struct ls_ep_test *test = arg;
678 + if (test->type == TEST_TYPE_DMA)
679 + ret = ls_pcie_ep_test_dma(test);
681 + ret = ls_pcie_ep_test_cpy(test);
684 + pr_err("\n%s \ttest failed\n",
688 + pr_err("\n%s \tthroughput:%lluMbps\n",
689 + test->cmd, test->result);
691 + ls_pcie_ep_test_done(test);
693 + ls_pcie_ep_trigger_msi(test);
698 +static int ls_pcie_ep_free_test(struct ls_ep_dev *ep)
700 + struct ls_ep_test *test = ep->driver_data;
705 + if (test->status == TEST_BUSY) {
706 + kthread_stop(test->thread);
708 + "test is running please wait and run again\n");
713 + free_pages((unsigned long)test->buf,
714 + get_order(PCIE_BAR4_SIZE));
717 + free_pages((unsigned long)test->cfg,
718 + get_order(PCIE_BAR2_SIZE));
721 + iounmap(test->out);
724 + ep->driver_data = NULL;
729 +static int ls_pcie_ep_init_test(struct ls_ep_dev *ep, u64 bus_addr)
731 + struct ls_pcie *pcie = ep->pcie;
732 + struct ls_ep_test *test = ep->driver_data;
737 + "Please use 'free' to remove the exiting test\n");
741 + test = kzalloc(sizeof(*test), GFP_KERNEL);
744 + ep->driver_data = test;
746 + spin_lock_init(&test->lock);
747 + test->status = TEST_IDLE;
749 + test->buf = dma_alloc_coherent(pcie->dev, get_order(PCIE_BAR4_SIZE),
753 + dev_info(&ep->dev, "failed to get mem for bar4\n");
758 + test->cfg = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
759 + get_order(PCIE_BAR2_SIZE));
761 + dev_info(&ep->dev, "failed to get mem for bar4\n");
765 + test->cfg_addr = virt_to_phys(test->cfg);
767 + test->out_addr = pcie->out_base;
768 + test->out = ioremap(test->out_addr, PCIE_BAR4_SIZE);
770 + dev_info(&ep->dev, "failed to map out\n");
775 + test->bus_addr = bus_addr;
777 + test->msi_addr = test->out_addr + PCIE_BAR4_SIZE;
778 + test->msi = ioremap(test->msi_addr, PCIE_MSI_OB_SIZE);
780 + dev_info(&ep->dev, "failed to map MSI outbound region\n");
782 + test->msi_msg_addr = ioread32(pcie->dbi + PCIE_MSI_MSG_ADDR_OFF) |
783 + (((u64)ioread32(pcie->dbi + PCIE_MSI_MSG_ADDR_OFF + 4)) << 32);
784 + test->msi_msg_data = ioread16(pcie->dbi + PCIE_MSI_MSG_DATA_OFF);
786 + ls_pcie_ep_dev_cfg_enable(ep);
788 + /* outbound iATU for memory */
789 + ls_pcie_iatu_outbound_set(pcie, 0, PCIE_ATU_TYPE_MEM,
790 + test->out_addr, bus_addr, PCIE_BAR4_SIZE);
791 + /* outbound iATU for MSI */
792 + ls_pcie_iatu_outbound_set(pcie, 1, PCIE_ATU_TYPE_MEM,
793 + test->msi_addr, test->msi_msg_addr,
796 + /* ATU 0 : INBOUND : map BAR0 */
797 + ls_pcie_iatu_inbound_set(pcie, 0, 0, test->cfg_addr);
798 + /* ATU 2 : INBOUND : map BAR2 */
799 + ls_pcie_iatu_inbound_set(pcie, 2, 2, test->cfg_addr);
800 + /* ATU 3 : INBOUND : map BAR4 */
801 + ls_pcie_iatu_inbound_set(pcie, 3, 4, test->buf_addr);
806 + ls_pcie_ep_free_test(ep);
810 +static int ls_pcie_ep_start_test(struct ls_ep_dev *ep, char *cmd)
812 + struct ls_ep_test *test = ep->driver_data;
813 + enum test_type type;
814 + enum test_dirt dirt;
815 + u32 cnt, len, loop;
820 + if (strncmp(cmd, "dma", 3) == 0)
821 + type = TEST_TYPE_DMA;
823 + type = TEST_TYPE_MEMCPY;
825 + cnt = sscanf(&cmd[4], "%1s %u %u %x", dirt_str, &len, &loop, &data);
827 + dev_info(&ep->dev, "format error %s", cmd);
828 + dev_info(&ep->dev, "dma/cpy <r/w> <packet_size> <loop> <data>\n");
832 + if (strncmp(dirt_str, "r", 1) == 0)
833 + dirt = TEST_DIRT_READ;
835 + dirt = TEST_DIRT_WRITE;
837 + if (len > PCIE_BAR4_SIZE) {
838 + dev_err(&ep->dev, "max len is %d", PCIE_BAR4_SIZE);
843 + dev_err(&ep->dev, "Please first run init command\n");
847 + if (ls_pcie_ep_test_try_run(test)) {
848 + dev_err(&ep->dev, "There is already a test running\n");
855 + test->data = (char)data;
857 + strcpy(test->cmd, cmd);
858 + test->thread = kthread_run(ls_pcie_ep_test_thread, test,
860 + if (IS_ERR(test->thread)) {
861 + dev_err(&ep->dev, "fork failed for pcie ep test\n");
862 + ls_pcie_ep_test_done(test);
863 + ret = PTR_ERR(test->thread);
871 + * ls_pcie_reg_ops_read - read for regs data
872 + * @filp: the opened file
873 + * @buffer: where to write the data for the user to read
874 + * @count: the size of the user's buffer
875 + * @ppos: file position offset
877 +static ssize_t ls_pcie_ep_dbg_regs_read(struct file *filp, char __user *buffer,
878 + size_t count, loff_t *ppos)
880 + struct ls_ep_dev *ep = filp->private_data;
881 + struct ls_pcie *pcie = ep->pcie;
883 + int desc = 0, i, len;
885 + buf = kmalloc(4 * 1024, GFP_KERNEL);
889 + ls_pcie_ep_dev_cfg_enable(ep);
891 + desc += sprintf(buf + desc, "%s", "reg info:");
892 + for (i = 0; i < 0x200; i += 4) {
894 + desc += sprintf(buf + desc, "\n%08x:", i);
895 + desc += sprintf(buf + desc, " %08x", readl(pcie->dbi + i));
898 + desc += sprintf(buf + desc, "\n%s", "outbound iATU info:\n");
899 + for (i = 0; i < 6; i++) {
900 + writel(PCIE_ATU_REGION_OUTBOUND | i,
901 + pcie->dbi + PCIE_ATU_VIEWPORT);
902 + desc += sprintf(buf + desc, "iATU%d", i);
903 + desc += sprintf(buf + desc, "\tLOWER PHYS 0x%08x\n",
904 + readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
905 + desc += sprintf(buf + desc, "\tUPPER PHYS 0x%08x\n",
906 + readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
907 + desc += sprintf(buf + desc, "\tLOWER BUS 0x%08x\n",
908 + readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
909 + desc += sprintf(buf + desc, "\tUPPER BUS 0x%08x\n",
910 + readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
911 + desc += sprintf(buf + desc, "\tLIMIT 0x%08x\n",
912 + readl(pcie->dbi + PCIE_ATU_LIMIT));
913 + desc += sprintf(buf + desc, "\tCR1 0x%08x\n",
914 + readl(pcie->dbi + PCIE_ATU_CR1));
915 + desc += sprintf(buf + desc, "\tCR2 0x%08x\n",
916 + readl(pcie->dbi + PCIE_ATU_CR2));
919 + desc += sprintf(buf + desc, "\n%s", "inbound iATU info:\n");
920 + for (i = 0; i < 6; i++) {
921 + writel(PCIE_ATU_REGION_INBOUND | i,
922 + pcie->dbi + PCIE_ATU_VIEWPORT);
923 + desc += sprintf(buf + desc, "iATU%d", i);
924 + desc += sprintf(buf + desc, "\tLOWER BUS 0x%08x\n",
925 + readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
926 + desc += sprintf(buf + desc, "\tUPPER BUSs 0x%08x\n",
927 + readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
928 + desc += sprintf(buf + desc, "\tLOWER PHYS 0x%08x\n",
929 + readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
930 + desc += sprintf(buf + desc, "\tUPPER PHYS 0x%08x\n",
931 + readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
932 + desc += sprintf(buf + desc, "\tLIMIT 0x%08x\n",
933 + readl(pcie->dbi + PCIE_ATU_LIMIT));
934 + desc += sprintf(buf + desc, "\tCR1 0x%08x\n",
935 + readl(pcie->dbi + PCIE_ATU_CR1));
936 + desc += sprintf(buf + desc, "\tCR2 0x%08x\n",
937 + readl(pcie->dbi + PCIE_ATU_CR2));
940 + len = simple_read_from_buffer(buffer, count, ppos, buf, desc);
947 + * ls_pcie_ep_dbg_regs_write - write into regs datum
948 + * @filp: the opened file
949 + * @buffer: where to find the user's data
950 + * @count: the length of the user's data
951 + * @ppos: file position offset
953 +static ssize_t ls_pcie_ep_dbg_regs_write(struct file *filp,
954 + const char __user *buffer,
955 + size_t count, loff_t *ppos)
957 + struct ls_ep_dev *ep = filp->private_data;
958 + struct ls_pcie *pcie = ep->pcie;
961 + if (count >= sizeof(buf))
964 + memset(buf, 0, sizeof(buf));
966 + if (copy_from_user(buf, buffer, count))
969 + ls_pcie_ep_dev_cfg_enable(ep);
971 + if (strncmp(buf, "reg", 3) == 0) {
975 + cnt = sscanf(&buf[3], "%x %x", ®, &value);
977 + writel(value, pcie->dbi + reg);
978 + value = readl(pcie->dbi + reg);
979 + dev_info(&ep->dev, "reg 0x%08x: 0x%08x\n",
982 + dev_info(&ep->dev, "reg <reg> <value>\n");
984 + } else if (strncmp(buf, "atu", 3) == 0) {
986 + dev_info(&ep->dev, " Not support atu command\n");
988 + dev_info(&ep->dev, "Unknown command %s\n", buf);
989 + dev_info(&ep->dev, "Available commands:\n");
990 + dev_info(&ep->dev, " reg <reg> <value>\n");
996 +static const struct file_operations ls_pcie_ep_dbg_regs_fops = {
997 + .owner = THIS_MODULE,
998 + .open = simple_open,
999 + .read = ls_pcie_ep_dbg_regs_read,
1000 + .write = ls_pcie_ep_dbg_regs_write,
1003 +static ssize_t ls_pcie_ep_dbg_test_read(struct file *filp,
1004 + char __user *buffer,
1005 + size_t count, loff_t *ppos)
1007 + struct ls_ep_dev *ep = filp->private_data;
1008 + struct ls_ep_test *test = ep->driver_data;
1010 + int desc = 0, len;
1013 + dev_info(&ep->dev, " there is NO test\n");
1017 + if (test->status != TEST_IDLE) {
1018 + dev_info(&ep->dev, "test %s is running\n", test->cmd);
1022 + desc = sprintf(buf, "MSI ADDR:0x%llx MSI DATA:0x%x\n",
1023 + test->msi_msg_addr, test->msi_msg_data);
1025 + desc += sprintf(buf + desc, "%s throughput:%lluMbps\n",
1026 + test->cmd, test->result);
1028 + len = simple_read_from_buffer(buffer, count, ppos,
1034 +static ssize_t ls_pcie_ep_dbg_test_write(struct file *filp,
1035 + const char __user *buffer,
1036 + size_t count, loff_t *ppos)
1038 + struct ls_ep_dev *ep = filp->private_data;
1041 + if (count >= sizeof(buf))
1044 + memset(buf, 0, sizeof(buf));
1046 + if (copy_from_user(buf, buffer, count))
1049 + if (strncmp(buf, "init", 4) == 0) {
1053 + while (buf[i] == ' ')
1056 + if (kstrtou64(&buf[i], 0, &bus_addr))
1057 + dev_info(&ep->dev, "command: init <bus_addr>\n");
1059 + if (ls_pcie_ep_init_test(ep, bus_addr))
1060 + dev_info(&ep->dev, "failed to init test\n");
1062 + } else if (strncmp(buf, "free", 4) == 0)
1063 + ls_pcie_ep_free_test(ep);
1064 + else if (strncmp(buf, "dma", 3) == 0 ||
1065 + strncmp(buf, "cpy", 3) == 0)
1066 + ls_pcie_ep_start_test(ep, buf);
1068 + dev_info(&ep->dev, "Unknown command: %s\n", buf);
1069 + dev_info(&ep->dev, "Available commands:\n");
1070 + dev_info(&ep->dev, "\tinit <bus_addr>\n");
1071 + dev_info(&ep->dev, "\t<dma/cpy> <r/w> <packet_size> <loop>\n");
1072 + dev_info(&ep->dev, "\tfree\n");
1078 +static const struct file_operations ls_pcie_ep_dbg_test_fops = {
1079 + .owner = THIS_MODULE,
1080 + .open = simple_open,
1081 + .read = ls_pcie_ep_dbg_test_read,
1082 + .write = ls_pcie_ep_dbg_test_write,
1085 +static ssize_t ls_pcie_ep_dbg_dump_read(struct file *filp,
1086 + char __user *buffer,
1087 + size_t count, loff_t *ppos)
1089 + struct ls_ep_dev *ep = filp->private_data;
1090 + struct ls_ep_test *test = ep->driver_data;
1092 + int desc = 0, i, len;
1094 + buf = kmalloc(4 * 1024, GFP_KERNEL);
1099 + dev_info(&ep->dev, " there is NO test\n");
1104 + desc += sprintf(buf + desc, "%s", "dump info:");
1105 + for (i = 0; i < 256; i += 4) {
1107 + desc += sprintf(buf + desc, "\n%08x:", i);
1108 + desc += sprintf(buf + desc, " %08x", readl(test->buf + i));
1111 + desc += sprintf(buf + desc, "\n");
1112 + len = simple_read_from_buffer(buffer, count, ppos, buf, desc);
1119 +static const struct file_operations ls_pcie_ep_dbg_dump_fops = {
1120 + .owner = THIS_MODULE,
1121 + .open = simple_open,
1122 + .read = ls_pcie_ep_dbg_dump_read,
1125 +static int ls_pcie_ep_dev_dbgfs_init(struct ls_ep_dev *ep)
1127 + struct ls_pcie *pcie = ep->pcie;
1128 + struct dentry *pfile;
1130 + ls_pcie_ep_dev_cfg_enable(ep);
1132 + ep->dir = debugfs_create_dir(dev_name(&ep->dev), pcie->dir);
1136 + pfile = debugfs_create_file("regs", 0600, ep->dir, ep,
1137 + &ls_pcie_ep_dbg_regs_fops);
1139 + dev_info(&ep->dev, "debugfs regs for failed\n");
1141 + pfile = debugfs_create_file("test", 0600, ep->dir, ep,
1142 + &ls_pcie_ep_dbg_test_fops);
1144 + dev_info(&ep->dev, "debugfs test for failed\n");
1146 + pfile = debugfs_create_file("dump", 0600, ep->dir, ep,
1147 + &ls_pcie_ep_dbg_dump_fops);
1149 + dev_info(&ep->dev, "debugfs dump for failed\n");
1154 +int ls_pcie_ep_dbgfs_init(struct ls_pcie *pcie)
1156 + struct ls_ep_dev *ep;
1158 + pcie->dir = debugfs_create_dir(dev_name(pcie->dev), NULL);
1162 + list_for_each_entry(ep, &pcie->ep_list, node)
1163 + ls_pcie_ep_dev_dbgfs_init(ep);
1168 +int ls_pcie_ep_dbgfs_remove(struct ls_pcie *pcie)
1170 + debugfs_remove_recursive(pcie->dir);
1174 +MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
1175 +MODULE_DESCRIPTION("Freescale Layerscape PCIe EP controller driver");
1176 +MODULE_LICENSE("GPL v2");
1178 +++ b/drivers/pci/host/pci-layerscape-ep.c
1181 + * PCIe Endpoint driver for Freescale Layerscape SoCs
1183 + * Copyright (C) 2015 Freescale Semiconductor.
1185 + * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
1187 + * This program is free software; you can redistribute it and/or modify
1188 + * it under the terms of the GNU General Public License version 2 as
1189 + * published by the Free Software Foundation.
1192 +#include <linux/kernel.h>
1193 +#include <linux/delay.h>
1194 +#include <linux/interrupt.h>
1195 +#include <linux/module.h>
1196 +#include <linux/of_pci.h>
1197 +#include <linux/of_platform.h>
1198 +#include <linux/of_irq.h>
1199 +#include <linux/of_address.h>
1200 +#include <linux/pci.h>
1201 +#include <linux/platform_device.h>
1202 +#include <linux/resource.h>
1203 +#include <linux/debugfs.h>
1204 +#include <linux/time.h>
1205 +#include <linux/uaccess.h>
1207 +#include "pci-layerscape-ep.h"
1210 +ls_pci_ep_find(struct ls_pcie *pcie, int dev_id)
1212 + struct ls_ep_dev *ep;
1214 + list_for_each_entry(ep, &pcie->ep_list, node) {
1215 + if (ep->dev_id == dev_id)
1222 +static void ls_pcie_try_cfg2(struct ls_pcie *pcie, int pf, int vf)
1225 + writel(PCIE_LCTRL0_VAL(pf, vf),
1226 + pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
1229 +static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
1231 + u32 header_type = 0;
1233 + header_type = readl(pcie->dbi + (PCI_HEADER_TYPE & ~0x3));
1234 + header_type = (header_type >> 16) & 0x7f;
1236 + return header_type == PCI_HEADER_TYPE_BRIDGE;
1239 +void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
1240 + u64 cpu_addr, u64 pci_addr, u32 size)
1242 + writel(PCIE_ATU_REGION_OUTBOUND | idx,
1243 + pcie->dbi + PCIE_ATU_VIEWPORT);
1244 + writel(lower_32_bits(cpu_addr),
1245 + pcie->dbi + PCIE_ATU_LOWER_BASE);
1246 + writel(upper_32_bits(cpu_addr),
1247 + pcie->dbi + PCIE_ATU_UPPER_BASE);
1248 + writel(lower_32_bits(cpu_addr + size - 1),
1249 + pcie->dbi + PCIE_ATU_LIMIT);
1250 + writel(lower_32_bits(pci_addr),
1251 + pcie->dbi + PCIE_ATU_LOWER_TARGET);
1252 + writel(upper_32_bits(pci_addr),
1253 + pcie->dbi + PCIE_ATU_UPPER_TARGET);
1254 + writel(type, pcie->dbi + PCIE_ATU_CR1);
1255 + writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
1258 +/* Use bar match mode and MEM type as default */
1259 +void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
1260 + int bar, u64 phys)
1262 + writel(PCIE_ATU_REGION_INBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
1263 + writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_TARGET);
1264 + writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
1265 + writel(PCIE_ATU_TYPE_MEM, pcie->dbi + PCIE_ATU_CR1);
1266 + writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
1267 + PCIE_ATU_BAR_NUM(bar), pcie->dbi + PCIE_ATU_CR2);
1270 +void ls_pcie_ep_dev_cfg_enable(struct ls_ep_dev *ep)
1272 + ls_pcie_try_cfg2(ep->pcie, ep->pf_idx, ep->vf_idx);
1275 +void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
1277 + if (size < 4 * 1024)
1282 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
1285 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
1288 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
1289 + writel(0, bar_base + PCI_BASE_ADDRESS_3);
1292 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
1293 + writel(0, bar_base + PCI_BASE_ADDRESS_5);
1300 +void ls_pcie_ep_dev_setup_bar(struct ls_ep_dev *ep, int bar, u32 size)
1302 + struct ls_pcie *pcie = ep->pcie;
1305 + if (size < 4 * 1024)
1309 + bar_base = pcie->dbi;
1311 + bar_base = pcie->dbi + PCIE_NO_SRIOV_BAR_BASE;
1313 + ls_pcie_ep_dev_cfg_enable(ep);
1314 + ls_pcie_ep_setup_bar(bar_base, bar, size);
1317 +static int ls_pcie_ep_dev_init(struct ls_pcie *pcie, int pf_idx, int vf_idx)
1319 + struct ls_ep_dev *ep;
1321 + ep = devm_kzalloc(pcie->dev, sizeof(*ep), GFP_KERNEL);
1326 + ep->pf_idx = pf_idx;
1327 + ep->vf_idx = vf_idx;
1329 + ep->dev_id = pf_idx + 4 + 4 * (vf_idx - 1);
1331 + ep->dev_id = pf_idx;
1334 + dev_set_name(&ep->dev, "pf%d-vf%d",
1338 + dev_set_name(&ep->dev, "pf%d",
1341 + list_add_tail(&ep->node, &pcie->ep_list);
1346 +static int ls_pcie_ep_init(struct ls_pcie *pcie)
1351 + sriov_header = readl(pcie->dbi + PCIE_SRIOV_POS);
1353 + if (PCI_EXT_CAP_ID(sriov_header) == PCI_EXT_CAP_ID_SRIOV) {
1354 + pcie->sriov = PCIE_SRIOV_POS;
1363 + for (i = 0; i < pf; i++) {
1364 + for (j = 0; j <= vf; j++)
1365 + ls_pcie_ep_dev_init(pcie, i, j);
1371 +static struct ls_pcie_ep_drvdata ls1043_drvdata = {
1372 + .lut_offset = 0x10000,
1373 + .ltssm_shift = 24,
1377 +static struct ls_pcie_ep_drvdata ls1046_drvdata = {
1378 + .lut_offset = 0x80000,
1379 + .ltssm_shift = 24,
1380 + .lut_dbg = 0x407fc,
1383 +static struct ls_pcie_ep_drvdata ls2080_drvdata = {
1384 + .lut_offset = 0x80000,
1389 +static const struct of_device_id ls_pcie_ep_of_match[] = {
1390 + { .compatible = "fsl,ls1021a-pcie", },
1391 + { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
1392 + { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
1393 + { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
1394 + { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
1397 +MODULE_DEVICE_TABLE(of, ls_pcie_ep_of_match);
1399 +static int ls_pcie_ep_probe(struct platform_device *pdev)
1401 + struct ls_pcie *pcie;
1402 + struct resource *dbi_base, *cfg_res;
1403 + const struct of_device_id *match;
1406 + match = of_match_device(ls_pcie_ep_of_match, &pdev->dev);
1410 + pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
1414 + pcie->dev = &pdev->dev;
1415 + INIT_LIST_HEAD(&pcie->ep_list);
1417 + dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1418 + pcie->dbi = devm_ioremap_resource(&pdev->dev, dbi_base);
1419 + if (IS_ERR(pcie->dbi)) {
1420 + dev_err(&pdev->dev, "missing *regs* space\n");
1421 + return PTR_ERR(pcie->dbi);
1424 + pcie->drvdata = match->data;
1425 + pcie->lut = pcie->dbi + pcie->drvdata->lut_offset;
1427 + if (ls_pcie_is_bridge(pcie))
1430 + dev_info(pcie->dev, "in EP mode\n");
1432 + cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
1434 + pcie->out_base = cfg_res->start;
1436 + dev_err(&pdev->dev, "missing *config* space\n");
1440 + ret = ls_pcie_ep_init(pcie);
1444 + ls_pcie_ep_dbgfs_init(pcie);
1446 + platform_set_drvdata(pdev, pcie);
1451 +static int ls_pcie_ep_dev_remove(struct ls_ep_dev *ep)
1453 + list_del(&ep->node);
1458 +static int ls_pcie_ep_remove(struct platform_device *pdev)
1460 + struct ls_pcie *pcie = platform_get_drvdata(pdev);
1461 + struct ls_ep_dev *ep, *tmp;
1466 + ls_pcie_ep_dbgfs_remove(pcie);
1468 + list_for_each_entry_safe(ep, tmp, &pcie->ep_list, node)
1469 + ls_pcie_ep_dev_remove(ep);
1474 +static struct platform_driver ls_pcie_ep_driver = {
1476 + .name = "ls-pcie-ep",
1477 + .owner = THIS_MODULE,
1478 + .of_match_table = ls_pcie_ep_of_match,
1480 + .probe = ls_pcie_ep_probe,
1481 + .remove = ls_pcie_ep_remove,
1484 +module_platform_driver(ls_pcie_ep_driver);
1486 +MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
1487 +MODULE_DESCRIPTION("Freescale Layerscape PCIe EP driver");
1488 +MODULE_LICENSE("GPL v2");
1490 +++ b/drivers/pci/host/pci-layerscape-ep.h
1493 + * PCIe Endpoint driver for Freescale Layerscape SoCs
1495 + * Copyright (C) 2015 Freescale Semiconductor.
1497 + * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
1499 + * This program is free software; you can redistribute it and/or modify
1500 + * it under the terms of the GNU General Public License version 2 as
1501 + * published by the Free Software Foundation.
1505 +#ifndef _PCIE_LAYERSCAPE_EP_H
1506 +#define _PCIE_LAYERSCAPE_EP_H
1508 +#include <linux/device.h>
1510 +/* Synopsis specific PCIE configuration registers */
1511 +#define PCIE_ATU_VIEWPORT 0x900
1512 +#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
1513 +#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
1514 +#define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
1515 +#define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
1516 +#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
1517 +#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
1518 +#define PCIE_ATU_CR1 0x904
1519 +#define PCIE_ATU_TYPE_MEM (0x0 << 0)
1520 +#define PCIE_ATU_TYPE_IO (0x2 << 0)
1521 +#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
1522 +#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
1523 +#define PCIE_ATU_CR2 0x908
1524 +#define PCIE_ATU_ENABLE (0x1 << 31)
1525 +#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
1526 +#define PCIE_ATU_LOWER_BASE 0x90C
1527 +#define PCIE_ATU_UPPER_BASE 0x910
1528 +#define PCIE_ATU_LIMIT 0x914
1529 +#define PCIE_ATU_LOWER_TARGET 0x918
1530 +#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
1531 +#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
1532 +#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
1533 +#define PCIE_ATU_UPPER_TARGET 0x91C
1535 +/* PEX internal configuration registers */
1536 +#define PCIE_DBI_RO_WR_EN 0x8bc /* DBI Read-Only Write Enable Register */
1538 +/* PEX LUT registers */
1539 +#define PCIE_LUT_BASE 0x80000
1540 +#define PCIE_LUT_DBG 0x7FC /* PEX LUT Debug register */
1542 +#define PCIE_LUT_LCTRL0 0x7F8
1544 +#define PCIE_ATU_BAR_NUM(bar) ((bar) << 8)
1545 +#define PCIE_LCTRL0_CFG2_ENABLE (1 << 31)
1546 +#define PCIE_LCTRL0_VF(vf) ((vf) << 22)
1547 +#define PCIE_LCTRL0_PF(pf) ((pf) << 16)
1548 +#define PCIE_LCTRL0_VF_ACTIVE (1 << 21)
1549 +#define PCIE_LCTRL0_VAL(pf, vf) (PCIE_LCTRL0_PF(pf) | \
1550 + PCIE_LCTRL0_VF(vf) | \
1551 + ((vf) == 0 ? 0 : PCIE_LCTRL0_VF_ACTIVE) | \
1552 + PCIE_LCTRL0_CFG2_ENABLE)
1554 +#define PCIE_NO_SRIOV_BAR_BASE 0x1000
1556 +#define PCIE_SRIOV_POS 0x178
1557 +#define PCIE_PF_NUM 2
1558 +#define PCIE_VF_NUM 64
1560 +struct ls_pcie_ep_drvdata {
1567 + struct list_head ep_list;
1568 + struct device *dev;
1569 + struct dentry *dir;
1570 + const struct ls_pcie_ep_drvdata *drvdata;
1571 + void __iomem *dbi;
1572 + void __iomem *lut;
1573 + phys_addr_t out_base;
1579 + struct list_head node;
1580 + struct ls_pcie *pcie;
1581 + struct device dev;
1582 + struct dentry *dir;
1586 + void *driver_data;
1589 +struct ls_ep_dev *ls_pci_ep_find(struct ls_pcie *pcie, int dev_id);
1591 +void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
1592 + u64 cpu_addr, u64 pci_addr, u32 size);
1594 +/* Use bar match mode and MEM type as default */
1595 +void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
1596 + int bar, u64 phys);
1598 +void ls_pcie_ep_dev_setup_bar(struct ls_ep_dev *ep, int bar, u32 size);
1601 +void ls_pcie_ep_dev_cfg_enable(struct ls_ep_dev *ep);
1603 +int ls_pcie_ep_dbgfs_init(struct ls_pcie *pcie);
1604 +int ls_pcie_ep_dbgfs_remove(struct ls_pcie *pcie);
1606 +#endif /* _PCIE_LAYERSCAPE_EP_H */
1607 --- a/drivers/pci/host/pci-layerscape.c
1608 +++ b/drivers/pci/host/pci-layerscape.c
1610 #define PCIE_STRFMR1 0x71c /* Symbol Timer & Filter Mask Register1 */
1611 #define PCIE_DBI_RO_WR_EN 0x8bc /* DBI Read-Only Write Enable Register */
1613 -/* PEX LUT registers */
1614 -#define PCIE_LUT_DBG 0x7FC /* PEX LUT Debug Register */
1615 +#define PCIE_IATU_NUM 6
1617 +static void ls_pcie_host_init(struct pcie_port *pp);
1619 struct ls_pcie_drvdata {
1623 struct pcie_host_ops *ops;
1626 @@ -86,6 +88,14 @@ static void ls_pcie_drop_msg_tlp(struct
1627 iowrite32(val, pcie->pp.dbi_base + PCIE_STRFMR1);
1630 +static void ls_pcie_disable_outbound_atus(struct ls_pcie *pcie)
1634 + for (i = 0; i < PCIE_IATU_NUM; i++)
1635 + dw_pcie_disable_outbound_atu(&pcie->pp, i);
1638 static int ls1021_pcie_link_up(struct pcie_port *pp)
1641 @@ -134,7 +144,7 @@ static int ls_pcie_link_up(struct pcie_p
1642 struct ls_pcie *pcie = to_ls_pcie(pp);
1645 - state = (ioread32(pcie->lut + PCIE_LUT_DBG) >>
1646 + state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
1647 pcie->drvdata->ltssm_shift) &
1650 @@ -153,6 +163,9 @@ static void ls_pcie_host_init(struct pci
1651 ls_pcie_clear_multifunction(pcie);
1652 ls_pcie_drop_msg_tlp(pcie);
1653 iowrite32(0, pcie->pp.dbi_base + PCIE_DBI_RO_WR_EN);
1655 + ls_pcie_disable_outbound_atus(pcie);
1656 + dw_pcie_setup_rc(pp);
1659 static int ls_pcie_msi_host_init(struct pcie_port *pp,
1660 @@ -196,20 +209,39 @@ static struct ls_pcie_drvdata ls1021_drv
1661 static struct ls_pcie_drvdata ls1043_drvdata = {
1662 .lut_offset = 0x10000,
1665 + .ops = &ls_pcie_host_ops,
1668 +static struct ls_pcie_drvdata ls1046_drvdata = {
1669 + .lut_offset = 0x80000,
1670 + .ltssm_shift = 24,
1671 + .lut_dbg = 0x407fc,
1672 .ops = &ls_pcie_host_ops,
1675 static struct ls_pcie_drvdata ls2080_drvdata = {
1676 .lut_offset = 0x80000,
1679 + .ops = &ls_pcie_host_ops,
1682 +static struct ls_pcie_drvdata ls2088_drvdata = {
1683 + .lut_offset = 0x80000,
1685 + .lut_dbg = 0x407fc,
1686 .ops = &ls_pcie_host_ops,
1689 static const struct of_device_id ls_pcie_of_match[] = {
1690 + { .compatible = "fsl,ls1012a-pcie", .data = &ls1046_drvdata },
1691 { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
1692 { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
1693 + { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
1694 { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
1695 { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
1696 + { .compatible = "fsl,ls2088a-pcie", .data = &ls2088_drvdata },
1700 --- a/drivers/pci/host/pcie-designware.c
1701 +++ b/drivers/pci/host/pcie-designware.c
1702 @@ -478,6 +478,12 @@ int dw_pcie_wait_for_link(struct pcie_po
1706 +void dw_pcie_disable_outbound_atu(struct pcie_port *pp, int index)
1708 + dw_pcie_writel_rc(pp, PCIE_ATU_VIEWPORT, PCIE_ATU_REGION_OUTBOUND | index);
1709 + dw_pcie_writel_rc(pp, PCIE_ATU_CR2, 0);
1712 int dw_pcie_link_up(struct pcie_port *pp)
1715 --- a/drivers/pci/host/pcie-designware.h
1716 +++ b/drivers/pci/host/pcie-designware.h
1717 @@ -82,5 +82,6 @@ int dw_pcie_wait_for_link(struct pcie_po
1718 int dw_pcie_link_up(struct pcie_port *pp);
1719 void dw_pcie_setup_rc(struct pcie_port *pp);
1720 int dw_pcie_host_init(struct pcie_port *pp);
1721 +void dw_pcie_disable_outbound_atu(struct pcie_port *pp, int index);
1723 #endif /* _PCIE_DESIGNWARE_H */
1724 --- a/drivers/pci/pcie/portdrv_core.c
1725 +++ b/drivers/pci/pcie/portdrv_core.c
1726 @@ -44,52 +44,30 @@ static void release_pcie_device(struct d
1730 - * pcie_port_msix_add_entry - add entry to given array of MSI-X entries
1731 - * @entries: Array of MSI-X entries
1732 - * @new_entry: Index of the entry to add to the array
1733 - * @nr_entries: Number of entries already in the array
1734 + * pcibios_check_service_irqs - check irqs in the device tree
1735 + * @dev: PCI Express port to handle
1736 + * @irqs: Array of irqs to populate
1737 + * @mask: Bitmask of port capabilities returned by get_port_device_capability()
1739 + * Return value: 0 means no service irqs in the device tree
1741 - * Return value: Position of the added entry in the array
1743 -static int pcie_port_msix_add_entry(
1744 - struct msix_entry *entries, int new_entry, int nr_entries)
1745 +int __weak pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask)
1749 - for (j = 0; j < nr_entries; j++)
1750 - if (entries[j].entry == new_entry)
1753 - entries[j].entry = new_entry;
1759 * pcie_port_enable_msix - try to set up MSI-X as interrupt mode for given port
1760 * @dev: PCI Express port to handle
1761 - * @vectors: Array of interrupt vectors to populate
1762 + * @irqs: Array of interrupt vectors to populate
1763 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
1765 * Return value: 0 on success, error code on failure
1767 -static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
1768 +static int pcie_port_enable_msix(struct pci_dev *dev, int *irqs, int mask)
1770 - struct msix_entry *msix_entries;
1771 - int idx[PCIE_PORT_DEVICE_MAXSERVICES];
1772 - int nr_entries, status, pos, i, nvec;
1776 - nr_entries = pci_msix_vec_count(dev);
1777 - if (nr_entries < 0)
1778 - return nr_entries;
1779 - BUG_ON(!nr_entries);
1780 - if (nr_entries > PCIE_PORT_MAX_MSIX_ENTRIES)
1781 - nr_entries = PCIE_PORT_MAX_MSIX_ENTRIES;
1783 - msix_entries = kzalloc(sizeof(*msix_entries) * nr_entries, GFP_KERNEL);
1784 - if (!msix_entries)
1786 + int nr_entries, entry, nvec = 0;
1789 * Allocate as many entries as the port wants, so that we can check
1790 @@ -97,20 +75,13 @@ static int pcie_port_enable_msix(struct
1791 * equal to the number of entries this port actually uses, we'll happily
1792 * go through without any tricks.
1794 - for (i = 0; i < nr_entries; i++)
1795 - msix_entries[i].entry = i;
1797 - status = pci_enable_msix_exact(dev, msix_entries, nr_entries);
1801 - for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1805 + nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSIX_ENTRIES,
1807 + if (nr_entries < 0)
1808 + return nr_entries;
1810 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
1815 * The code below follows the PCI Express Base Specification 2.0
1816 @@ -125,18 +96,16 @@ static int pcie_port_enable_msix(struct
1817 pcie_capability_read_word(dev, PCI_EXP_FLAGS, ®16);
1818 entry = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
1819 if (entry >= nr_entries)
1821 + goto out_free_irqs;
1823 - i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
1826 + irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, entry);
1827 + irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, entry);
1829 - idx[PCIE_PORT_SERVICE_PME_SHIFT] = i;
1830 - idx[PCIE_PORT_SERVICE_HP_SHIFT] = i;
1831 + nvec = max(nvec, entry + 1);
1834 if (mask & PCIE_PORT_SERVICE_AER) {
1839 * The code below follows Section 7.10.10 of the PCI Express
1840 @@ -151,13 +120,11 @@ static int pcie_port_enable_msix(struct
1841 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, ®32);
1842 entry = reg32 >> 27;
1843 if (entry >= nr_entries)
1845 + goto out_free_irqs;
1847 - i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
1850 + irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, entry);
1852 - idx[PCIE_PORT_SERVICE_AER_SHIFT] = i;
1853 + nvec = max(nvec, entry + 1);
1857 @@ -165,41 +132,54 @@ static int pcie_port_enable_msix(struct
1858 * what we have. Otherwise, the port has some extra entries not for the
1859 * services we know and we need to work around that.
1861 - if (nvec == nr_entries) {
1864 + if (nvec != nr_entries) {
1865 /* Drop the temporary MSI-X setup */
1866 - pci_disable_msix(dev);
1867 + pci_free_irq_vectors(dev);
1869 /* Now allocate the MSI-X vectors for real */
1870 - status = pci_enable_msix_exact(dev, msix_entries, nvec);
1873 + nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
1875 + if (nr_entries < 0)
1876 + return nr_entries;
1879 - for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1880 - vectors[i] = idx[i] >= 0 ? msix_entries[idx[i]].vector : -1;
1883 - kfree(msix_entries);
1888 - pci_disable_msix(dev);
1891 + pci_free_irq_vectors(dev);
1896 - * init_service_irqs - initialize irqs for PCI Express port services
1897 + * pcie_init_service_irqs - initialize irqs for PCI Express port services
1898 * @dev: PCI Express port to handle
1899 * @irqs: Array of irqs to populate
1900 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
1902 * Return value: Interrupt mode associated with the port
1904 -static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
1905 +static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
1908 + unsigned flags = PCI_IRQ_LEGACY | PCI_IRQ_MSI;
1912 + for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1915 + /* Check if some platforms owns independent irq pins for AER/PME etc.
1916 + * Some platforms may own independent AER/PME interrupts and set
1917 + * them in the device tree file.
1919 + ret = pcibios_check_service_irqs(dev, irqs, mask);
1923 + for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1924 + if (irqs[i] == -1 && i != PCIE_PORT_SERVICE_VC_SHIFT)
1930 * If MSI cannot be used for PCIe PME or hotplug, we have to use
1931 @@ -207,41 +187,25 @@ static int init_service_irqs(struct pci_
1933 if (((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) ||
1934 ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())) {
1938 + flags &= ~PCI_IRQ_MSI;
1940 + /* Try to use MSI-X if supported */
1941 + if (!pcie_port_enable_msix(dev, irqs, mask))
1945 - /* Try to use MSI-X if supported */
1946 - if (!pcie_port_enable_msix(dev, irqs, mask))
1950 - * We're not going to use MSI-X, so try MSI and fall back to INTx.
1951 - * If neither MSI/MSI-X nor INTx available, try other interrupt. On
1952 - * some platforms, root port doesn't support MSI/MSI-X/INTx in RC mode.
1954 - if (!pci_enable_msi(dev) || dev->irq)
1956 + ret = pci_alloc_irq_vectors(dev, 1, 1, flags);
1961 - for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1963 - irqs[PCIE_PORT_SERVICE_VC_SHIFT] = -1;
1964 + for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
1965 + if (i != PCIE_PORT_SERVICE_VC_SHIFT)
1966 + irqs[i] = pci_irq_vector(dev, 0);
1974 -static void cleanup_service_irqs(struct pci_dev *dev)
1976 - if (dev->msix_enabled)
1977 - pci_disable_msix(dev);
1978 - else if (dev->msi_enabled)
1979 - pci_disable_msi(dev);
1983 * get_port_device_capability - discover capabilities of a PCI Express port
1984 * @dev: PCI Express port to examine
1985 @@ -378,7 +342,7 @@ int pcie_port_device_register(struct pci
1986 * that can be used in the absence of irqs. Allow them to determine
1987 * if that is to be used.
1989 - status = init_service_irqs(dev, irqs, capabilities);
1990 + status = pcie_init_service_irqs(dev, irqs, capabilities);
1992 capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
1994 @@ -401,7 +365,7 @@ int pcie_port_device_register(struct pci
1998 - cleanup_service_irqs(dev);
1999 + pci_free_irq_vectors(dev);
2001 pci_disable_device(dev);
2003 @@ -469,7 +433,7 @@ static int remove_iter(struct device *de
2004 void pcie_port_device_remove(struct pci_dev *dev)
2006 device_for_each_child(&dev->dev, NULL, remove_iter);
2007 - cleanup_service_irqs(dev);
2008 + pci_free_irq_vectors(dev);
2009 pci_disable_device(dev);
2012 @@ -499,7 +463,6 @@ static int pcie_port_probe_service(struc
2016 - dev_printk(KERN_DEBUG, dev, "service driver %s loaded\n", driver->name);
2020 @@ -524,8 +487,6 @@ static int pcie_port_remove_service(stru
2021 pciedev = to_pcie_device(dev);
2022 driver = to_service_driver(dev->driver);
2023 if (driver && driver->remove) {
2024 - dev_printk(KERN_DEBUG, dev, "unloading service driver %s\n",
2026 driver->remove(pciedev);
2029 --- a/include/linux/pci.h
2030 +++ b/include/linux/pci.h
2031 @@ -1823,6 +1823,7 @@ void pcibios_release_device(struct pci_d
2032 void pcibios_penalize_isa_irq(int irq, int active);
2033 int pcibios_alloc_irq(struct pci_dev *dev);
2034 void pcibios_free_irq(struct pci_dev *dev);
2035 +int pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask);
2037 #ifdef CONFIG_HIBERNATE_CALLBACKS
2038 extern struct dev_pm_ops pcibios_pm_ops;