1 From c4813da334b0c31e9c55eea015f1e898e84ff45b 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 | 256 +++++++--
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 | 37 +-
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, 1518 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,117 @@ 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,ls1021a-msi", .data = &ls1021_msi_cfg },
292 + { .compatible = "fsl,ls1043a-msi", .data = &ls1021_msi_cfg },
293 + { .compatible = "fsl,ls1043a-v1.1-msi", .data = &ls1043_v1_1_msi_cfg },
294 + { .compatible = "fsl,ls1046a-msi", .data = &ls1046_msi_cfg },
297 +MODULE_DEVICE_TABLE(of, ls_scfg_msi_id);
299 static int ls_scfg_msi_probe(struct platform_device *pdev)
301 + const struct of_device_id *match;
302 struct ls_scfg_msi *msi_data;
303 struct resource *res;
307 + match = of_match_device(ls_scfg_msi_id, &pdev->dev);
311 msi_data = devm_kzalloc(&pdev->dev, sizeof(*msi_data), GFP_KERNEL);
315 + msi_data->cfg = (struct ls_scfg_msi_cfg *) match->data;
317 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
318 msi_data->regs = devm_ioremap_resource(&pdev->dev, res);
319 if (IS_ERR(msi_data->regs)) {
320 @@ -182,23 +349,48 @@ static int ls_scfg_msi_probe(struct plat
322 msi_data->msiir_addr = res->start;
324 - msi_data->irq = platform_get_irq(pdev, 0);
325 - if (msi_data->irq <= 0) {
326 - dev_err(&pdev->dev, "failed to get MSI irq\n");
330 msi_data->pdev = pdev;
331 spin_lock_init(&msi_data->lock);
333 + msi_data->irqs_num = MSI_IRQS_PER_MSIR *
334 + (1 << msi_data->cfg->ibs_shift);
335 + msi_data->used = devm_kcalloc(&pdev->dev,
336 + BITS_TO_LONGS(msi_data->irqs_num),
337 + sizeof(*msi_data->used),
339 + if (!msi_data->used)
342 + * Reserve all the hwirqs
343 + * The available hwirqs will be released in ls1_msi_setup_hwirq()
345 + bitmap_set(msi_data->used, 0, msi_data->irqs_num);
347 + msi_data->msir_num = of_irq_count(pdev->dev.of_node);
349 + if (msi_affinity_flag) {
352 + cpu_num = num_possible_cpus();
353 + if (msi_data->msir_num >= cpu_num)
354 + msi_data->msir_num = cpu_num;
356 + msi_affinity_flag = 0;
359 + msi_data->msir = devm_kcalloc(&pdev->dev, msi_data->msir_num,
360 + sizeof(*msi_data->msir),
362 + if (!msi_data->msir)
365 + for (i = 0; i < msi_data->msir_num; i++)
366 + ls_scfg_msi_setup_hwirq(msi_data, i);
368 ret = ls_scfg_msi_domains_init(msi_data);
372 - irq_set_chained_handler_and_data(msi_data->irq,
373 - ls_scfg_msi_irq_handler,
376 platform_set_drvdata(pdev, msi_data);
379 @@ -207,8 +399,10 @@ static int ls_scfg_msi_probe(struct plat
380 static int ls_scfg_msi_remove(struct platform_device *pdev)
382 struct ls_scfg_msi *msi_data = platform_get_drvdata(pdev);
385 - irq_set_chained_handler_and_data(msi_data->irq, NULL, NULL);
386 + for (i = 0; i < msi_data->msir_num; i++)
387 + ls_scfg_msi_teardown_hwirq(&msi_data->msir[i]);
389 irq_domain_remove(msi_data->msi_domain);
390 irq_domain_remove(msi_data->parent);
391 @@ -218,12 +412,6 @@ static int ls_scfg_msi_remove(struct pla
395 -static const struct of_device_id ls_scfg_msi_id[] = {
396 - { .compatible = "fsl,1s1021a-msi", },
397 - { .compatible = "fsl,1s1043a-msi", },
401 static struct platform_driver ls_scfg_msi_driver = {
403 .name = "ls-scfg-msi",
404 --- a/drivers/pci/host/Makefile
405 +++ b/drivers/pci/host/Makefile
406 @@ -17,7 +17,7 @@ obj-$(CONFIG_PCIE_XILINX) += pcie-xilinx
407 obj-$(CONFIG_PCIE_XILINX_NWL) += pcie-xilinx-nwl.o
408 obj-$(CONFIG_PCI_XGENE) += pci-xgene.o
409 obj-$(CONFIG_PCI_XGENE_MSI) += pci-xgene-msi.o
410 -obj-$(CONFIG_PCI_LAYERSCAPE) += pci-layerscape.o
411 +obj-$(CONFIG_PCI_LAYERSCAPE) += pci-layerscape.o pci-layerscape-ep.o pci-layerscape-ep-debugfs.o
412 obj-$(CONFIG_PCI_VERSATILE) += pci-versatile.o
413 obj-$(CONFIG_PCIE_IPROC) += pcie-iproc.o
414 obj-$(CONFIG_PCIE_IPROC_MSI) += pcie-iproc-msi.o
416 +++ b/drivers/pci/host/pci-layerscape-ep-debugfs.c
419 + * PCIe Endpoint driver for Freescale Layerscape SoCs
421 + * Copyright (C) 2015 Freescale Semiconductor.
423 + * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
425 + * This program is free software; you can redistribute it and/or modify
426 + * it under the terms of the GNU General Public License version 2 as
427 + * published by the Free Software Foundation.
430 +#include <linux/kernel.h>
431 +#include <linux/module.h>
432 +#include <linux/debugfs.h>
433 +#include <linux/time.h>
434 +#include <linux/uaccess.h>
435 +#include <linux/kthread.h>
436 +#include <linux/slab.h>
437 +#include <linux/dmaengine.h>
438 +#include <linux/dma-mapping.h>
439 +#include <linux/freezer.h>
441 +#include <linux/completion.h>
443 +#include "pci-layerscape-ep.h"
445 +#define PCIE_ATU_INDEX3 (0x3 << 0)
446 +#define PCIE_ATU_INDEX2 (0x2 << 0)
447 +#define PCIE_ATU_INDEX1 (0x1 << 0)
448 +#define PCIE_ATU_INDEX0 (0x0 << 0)
450 +#define PCIE_BAR0_SIZE (4 * 1024) /* 4K */
451 +#define PCIE_BAR1_SIZE (8 * 1024) /* 8K for MSIX */
452 +#define PCIE_BAR2_SIZE (4 * 1024) /* 4K */
453 +#define PCIE_BAR4_SIZE (1 * 1024 * 1024) /* 1M */
454 +#define PCIE_MSI_OB_SIZE (4 * 1024) /* 4K */
456 +#define PCIE_MSI_MSG_ADDR_OFF 0x54
457 +#define PCIE_MSI_MSG_DATA_OFF 0x5c
475 + struct ls_ep_dev *ep;
480 + dma_addr_t cfg_addr;
481 + dma_addr_t buf_addr;
482 + dma_addr_t out_addr;
483 + dma_addr_t bus_addr;
484 + dma_addr_t msi_addr;
487 + struct task_struct *thread;
489 + struct completion done;
493 + enum test_dirt dirt;
494 + enum test_type type;
495 + enum test_status status;
496 + u64 result; /* Mbps */
500 +static int ls_pcie_ep_trigger_msi(struct ls_ep_test *test)
505 + iowrite32(test->msi_msg_data, test->msi);
510 +static int ls_pcie_ep_test_try_run(struct ls_ep_test *test)
514 + spin_lock(&test->lock);
515 + if (test->status == TEST_IDLE) {
516 + test->status = TEST_BUSY;
520 + spin_unlock(&test->lock);
525 +static void ls_pcie_ep_test_done(struct ls_ep_test *test)
527 + spin_lock(&test->lock);
528 + test->status = TEST_IDLE;
529 + spin_unlock(&test->lock);
532 +static void ls_pcie_ep_test_dma_cb(void *arg)
534 + struct ls_ep_test *test = arg;
536 + complete(&test->done);
539 +static int ls_pcie_ep_test_dma(struct ls_ep_test *test)
541 + dma_cap_mask_t mask;
542 + struct dma_chan *chan;
543 + struct dma_device *dma_dev;
544 + dma_addr_t src, dst;
545 + enum dma_data_direction direction;
546 + enum dma_ctrl_flags dma_flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
547 + struct timespec start, end, period;
550 + dma_cap_zero(mask);
551 + dma_cap_set(DMA_MEMCPY, mask);
553 + chan = dma_request_channel(mask, NULL, test);
555 + pr_err("failed to request dma channel\n");
559 + memset(test->buf, test->data, test->len);
561 + if (test->dirt == TEST_DIRT_WRITE) {
562 + src = test->buf_addr;
563 + dst = test->out_addr;
564 + direction = DMA_TO_DEVICE;
566 + src = test->out_addr;
567 + dst = test->buf_addr;
568 + direction = DMA_FROM_DEVICE;
571 + dma_dev = chan->device;
572 + dma_flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
574 + dma_sync_single_for_device(&test->ep->dev, test->buf_addr,
575 + test->len, direction);
579 + getrawmonotonic(&start);
580 + while (!kthread_should_stop() && (i < test->loop)) {
581 + struct dma_async_tx_descriptor *dma_desc;
582 + dma_cookie_t dma_cookie = {0};
586 + init_completion(&test->done);
588 + dma_desc = dma_dev->device_prep_dma_memcpy(chan,
593 + pr_err("DMA desc constr failed...\n");
597 + dma_desc->callback = ls_pcie_ep_test_dma_cb;
598 + dma_desc->callback_param = test;
599 + dma_cookie = dmaengine_submit(dma_desc);
601 + if (dma_submit_error(dma_cookie)) {
602 + pr_err("DMA submit error....\n");
606 + /* Trigger the transaction */
607 + dma_async_issue_pending(chan);
609 + tmo = wait_for_completion_timeout(&test->done,
610 + msecs_to_jiffies(5 * test->len));
612 + pr_err("Self-test copy timed out, disabling\n");
616 + status = dma_async_is_tx_complete(chan, dma_cookie,
618 + if (status != DMA_COMPLETE) {
619 + pr_err("got completion callback, but status is %s\n",
620 + status == DMA_ERROR ? "error" : "in progress");
627 + getrawmonotonic(&end);
628 + period = timespec_sub(end, start);
629 + test->result = test->len * 8ULL * i * 1000;
630 + do_div(test->result, period.tv_sec * 1000 * 1000 * 1000 + period.tv_nsec);
631 + dma_release_channel(chan);
636 + dma_release_channel(chan);
641 +static int ls_pcie_ep_test_cpy(struct ls_ep_test *test)
644 + struct timespec start, end, period;
647 + memset(test->buf, test->data, test->len);
649 + if (test->dirt == TEST_DIRT_WRITE) {
657 + getrawmonotonic(&start);
658 + while (!kthread_should_stop() && i < test->loop) {
659 + memcpy(dst, src, test->len);
662 + getrawmonotonic(&end);
664 + period = timespec_sub(end, start);
665 + test->result = test->len * 8ULL * i * 1000;
666 + do_div(test->result, period.tv_sec * 1000 * 1000 * 1000 + period.tv_nsec);
671 +int ls_pcie_ep_test_thread(void *arg)
675 + struct ls_ep_test *test = arg;
677 + if (test->type == TEST_TYPE_DMA)
678 + ret = ls_pcie_ep_test_dma(test);
680 + ret = ls_pcie_ep_test_cpy(test);
683 + pr_err("\n%s \ttest failed\n",
687 + pr_err("\n%s \tthroughput:%lluMbps\n",
688 + test->cmd, test->result);
690 + ls_pcie_ep_test_done(test);
692 + ls_pcie_ep_trigger_msi(test);
697 +static int ls_pcie_ep_free_test(struct ls_ep_dev *ep)
699 + struct ls_ep_test *test = ep->driver_data;
704 + if (test->status == TEST_BUSY) {
705 + kthread_stop(test->thread);
707 + "test is running please wait and run again\n");
712 + free_pages((unsigned long)test->buf,
713 + get_order(PCIE_BAR4_SIZE));
716 + free_pages((unsigned long)test->cfg,
717 + get_order(PCIE_BAR2_SIZE));
720 + iounmap(test->out);
723 + ep->driver_data = NULL;
728 +static int ls_pcie_ep_init_test(struct ls_ep_dev *ep, u64 bus_addr)
730 + struct ls_pcie *pcie = ep->pcie;
731 + struct ls_ep_test *test = ep->driver_data;
736 + "Please use 'free' to remove the exiting test\n");
740 + test = kzalloc(sizeof(*test), GFP_KERNEL);
743 + ep->driver_data = test;
745 + spin_lock_init(&test->lock);
746 + test->status = TEST_IDLE;
748 + test->buf = dma_alloc_coherent(pcie->dev, get_order(PCIE_BAR4_SIZE),
752 + dev_info(&ep->dev, "failed to get mem for bar4\n");
757 + test->cfg = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
758 + get_order(PCIE_BAR2_SIZE));
760 + dev_info(&ep->dev, "failed to get mem for bar4\n");
764 + test->cfg_addr = virt_to_phys(test->cfg);
766 + test->out_addr = pcie->out_base;
767 + test->out = ioremap(test->out_addr, PCIE_BAR4_SIZE);
769 + dev_info(&ep->dev, "failed to map out\n");
774 + test->bus_addr = bus_addr;
776 + test->msi_addr = test->out_addr + PCIE_BAR4_SIZE;
777 + test->msi = ioremap(test->msi_addr, PCIE_MSI_OB_SIZE);
779 + dev_info(&ep->dev, "failed to map MSI outbound region\n");
781 + test->msi_msg_addr = ioread32(pcie->dbi + PCIE_MSI_MSG_ADDR_OFF) |
782 + (((u64)ioread32(pcie->dbi + PCIE_MSI_MSG_ADDR_OFF + 4)) << 32);
783 + test->msi_msg_data = ioread16(pcie->dbi + PCIE_MSI_MSG_DATA_OFF);
785 + ls_pcie_ep_dev_cfg_enable(ep);
787 + /* outbound iATU for memory */
788 + ls_pcie_iatu_outbound_set(pcie, 0, PCIE_ATU_TYPE_MEM,
789 + test->out_addr, bus_addr, PCIE_BAR4_SIZE);
790 + /* outbound iATU for MSI */
791 + ls_pcie_iatu_outbound_set(pcie, 1, PCIE_ATU_TYPE_MEM,
792 + test->msi_addr, test->msi_msg_addr,
795 + /* ATU 0 : INBOUND : map BAR0 */
796 + ls_pcie_iatu_inbound_set(pcie, 0, 0, test->cfg_addr);
797 + /* ATU 2 : INBOUND : map BAR2 */
798 + ls_pcie_iatu_inbound_set(pcie, 2, 2, test->cfg_addr);
799 + /* ATU 3 : INBOUND : map BAR4 */
800 + ls_pcie_iatu_inbound_set(pcie, 3, 4, test->buf_addr);
805 + ls_pcie_ep_free_test(ep);
809 +static int ls_pcie_ep_start_test(struct ls_ep_dev *ep, char *cmd)
811 + struct ls_ep_test *test = ep->driver_data;
812 + enum test_type type;
813 + enum test_dirt dirt;
814 + u32 cnt, len, loop;
819 + if (strncmp(cmd, "dma", 3) == 0)
820 + type = TEST_TYPE_DMA;
822 + type = TEST_TYPE_MEMCPY;
824 + cnt = sscanf(&cmd[4], "%1s %u %u %x", dirt_str, &len, &loop, &data);
826 + dev_info(&ep->dev, "format error %s", cmd);
827 + dev_info(&ep->dev, "dma/cpy <r/w> <packet_size> <loop> <data>\n");
831 + if (strncmp(dirt_str, "r", 1) == 0)
832 + dirt = TEST_DIRT_READ;
834 + dirt = TEST_DIRT_WRITE;
836 + if (len > PCIE_BAR4_SIZE) {
837 + dev_err(&ep->dev, "max len is %d", PCIE_BAR4_SIZE);
842 + dev_err(&ep->dev, "Please first run init command\n");
846 + if (ls_pcie_ep_test_try_run(test)) {
847 + dev_err(&ep->dev, "There is already a test running\n");
854 + test->data = (char)data;
856 + strcpy(test->cmd, cmd);
857 + test->thread = kthread_run(ls_pcie_ep_test_thread, test,
859 + if (IS_ERR(test->thread)) {
860 + dev_err(&ep->dev, "fork failed for pcie ep test\n");
861 + ls_pcie_ep_test_done(test);
862 + ret = PTR_ERR(test->thread);
870 + * ls_pcie_reg_ops_read - read for regs data
871 + * @filp: the opened file
872 + * @buffer: where to write the data for the user to read
873 + * @count: the size of the user's buffer
874 + * @ppos: file position offset
876 +static ssize_t ls_pcie_ep_dbg_regs_read(struct file *filp, char __user *buffer,
877 + size_t count, loff_t *ppos)
879 + struct ls_ep_dev *ep = filp->private_data;
880 + struct ls_pcie *pcie = ep->pcie;
882 + int desc = 0, i, len;
884 + buf = kmalloc(4 * 1024, GFP_KERNEL);
888 + ls_pcie_ep_dev_cfg_enable(ep);
890 + desc += sprintf(buf + desc, "%s", "reg info:");
891 + for (i = 0; i < 0x200; i += 4) {
893 + desc += sprintf(buf + desc, "\n%08x:", i);
894 + desc += sprintf(buf + desc, " %08x", readl(pcie->dbi + i));
897 + desc += sprintf(buf + desc, "\n%s", "outbound iATU info:\n");
898 + for (i = 0; i < 6; i++) {
899 + writel(PCIE_ATU_REGION_OUTBOUND | i,
900 + pcie->dbi + PCIE_ATU_VIEWPORT);
901 + desc += sprintf(buf + desc, "iATU%d", i);
902 + desc += sprintf(buf + desc, "\tLOWER PHYS 0x%08x\n",
903 + readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
904 + desc += sprintf(buf + desc, "\tUPPER PHYS 0x%08x\n",
905 + readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
906 + desc += sprintf(buf + desc, "\tLOWER BUS 0x%08x\n",
907 + readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
908 + desc += sprintf(buf + desc, "\tUPPER BUS 0x%08x\n",
909 + readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
910 + desc += sprintf(buf + desc, "\tLIMIT 0x%08x\n",
911 + readl(pcie->dbi + PCIE_ATU_LIMIT));
912 + desc += sprintf(buf + desc, "\tCR1 0x%08x\n",
913 + readl(pcie->dbi + PCIE_ATU_CR1));
914 + desc += sprintf(buf + desc, "\tCR2 0x%08x\n",
915 + readl(pcie->dbi + PCIE_ATU_CR2));
918 + desc += sprintf(buf + desc, "\n%s", "inbound iATU info:\n");
919 + for (i = 0; i < 6; i++) {
920 + writel(PCIE_ATU_REGION_INBOUND | i,
921 + pcie->dbi + PCIE_ATU_VIEWPORT);
922 + desc += sprintf(buf + desc, "iATU%d", i);
923 + desc += sprintf(buf + desc, "\tLOWER BUS 0x%08x\n",
924 + readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
925 + desc += sprintf(buf + desc, "\tUPPER BUSs 0x%08x\n",
926 + readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
927 + desc += sprintf(buf + desc, "\tLOWER PHYS 0x%08x\n",
928 + readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
929 + desc += sprintf(buf + desc, "\tUPPER PHYS 0x%08x\n",
930 + readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
931 + desc += sprintf(buf + desc, "\tLIMIT 0x%08x\n",
932 + readl(pcie->dbi + PCIE_ATU_LIMIT));
933 + desc += sprintf(buf + desc, "\tCR1 0x%08x\n",
934 + readl(pcie->dbi + PCIE_ATU_CR1));
935 + desc += sprintf(buf + desc, "\tCR2 0x%08x\n",
936 + readl(pcie->dbi + PCIE_ATU_CR2));
939 + len = simple_read_from_buffer(buffer, count, ppos, buf, desc);
946 + * ls_pcie_ep_dbg_regs_write - write into regs datum
947 + * @filp: the opened file
948 + * @buffer: where to find the user's data
949 + * @count: the length of the user's data
950 + * @ppos: file position offset
952 +static ssize_t ls_pcie_ep_dbg_regs_write(struct file *filp,
953 + const char __user *buffer,
954 + size_t count, loff_t *ppos)
956 + struct ls_ep_dev *ep = filp->private_data;
957 + struct ls_pcie *pcie = ep->pcie;
960 + if (count >= sizeof(buf))
963 + memset(buf, 0, sizeof(buf));
965 + if (copy_from_user(buf, buffer, count))
968 + ls_pcie_ep_dev_cfg_enable(ep);
970 + if (strncmp(buf, "reg", 3) == 0) {
974 + cnt = sscanf(&buf[3], "%x %x", ®, &value);
976 + writel(value, pcie->dbi + reg);
977 + value = readl(pcie->dbi + reg);
978 + dev_info(&ep->dev, "reg 0x%08x: 0x%08x\n",
981 + dev_info(&ep->dev, "reg <reg> <value>\n");
983 + } else if (strncmp(buf, "atu", 3) == 0) {
985 + dev_info(&ep->dev, " Not support atu command\n");
987 + dev_info(&ep->dev, "Unknown command %s\n", buf);
988 + dev_info(&ep->dev, "Available commands:\n");
989 + dev_info(&ep->dev, " reg <reg> <value>\n");
995 +static const struct file_operations ls_pcie_ep_dbg_regs_fops = {
996 + .owner = THIS_MODULE,
997 + .open = simple_open,
998 + .read = ls_pcie_ep_dbg_regs_read,
999 + .write = ls_pcie_ep_dbg_regs_write,
1002 +static ssize_t ls_pcie_ep_dbg_test_read(struct file *filp,
1003 + char __user *buffer,
1004 + size_t count, loff_t *ppos)
1006 + struct ls_ep_dev *ep = filp->private_data;
1007 + struct ls_ep_test *test = ep->driver_data;
1009 + int desc = 0, len;
1012 + dev_info(&ep->dev, " there is NO test\n");
1016 + if (test->status != TEST_IDLE) {
1017 + dev_info(&ep->dev, "test %s is running\n", test->cmd);
1021 + desc = sprintf(buf, "MSI ADDR:0x%llx MSI DATA:0x%x\n",
1022 + test->msi_msg_addr, test->msi_msg_data);
1024 + desc += sprintf(buf + desc, "%s throughput:%lluMbps\n",
1025 + test->cmd, test->result);
1027 + len = simple_read_from_buffer(buffer, count, ppos,
1033 +static ssize_t ls_pcie_ep_dbg_test_write(struct file *filp,
1034 + const char __user *buffer,
1035 + size_t count, loff_t *ppos)
1037 + struct ls_ep_dev *ep = filp->private_data;
1040 + if (count >= sizeof(buf))
1043 + memset(buf, 0, sizeof(buf));
1045 + if (copy_from_user(buf, buffer, count))
1048 + if (strncmp(buf, "init", 4) == 0) {
1052 + while (buf[i] == ' ')
1055 + if (kstrtou64(&buf[i], 0, &bus_addr))
1056 + dev_info(&ep->dev, "command: init <bus_addr>\n");
1058 + if (ls_pcie_ep_init_test(ep, bus_addr))
1059 + dev_info(&ep->dev, "failed to init test\n");
1061 + } else if (strncmp(buf, "free", 4) == 0)
1062 + ls_pcie_ep_free_test(ep);
1063 + else if (strncmp(buf, "dma", 3) == 0 ||
1064 + strncmp(buf, "cpy", 3) == 0)
1065 + ls_pcie_ep_start_test(ep, buf);
1067 + dev_info(&ep->dev, "Unknown command: %s\n", buf);
1068 + dev_info(&ep->dev, "Available commands:\n");
1069 + dev_info(&ep->dev, "\tinit <bus_addr>\n");
1070 + dev_info(&ep->dev, "\t<dma/cpy> <r/w> <packet_size> <loop>\n");
1071 + dev_info(&ep->dev, "\tfree\n");
1077 +static const struct file_operations ls_pcie_ep_dbg_test_fops = {
1078 + .owner = THIS_MODULE,
1079 + .open = simple_open,
1080 + .read = ls_pcie_ep_dbg_test_read,
1081 + .write = ls_pcie_ep_dbg_test_write,
1084 +static ssize_t ls_pcie_ep_dbg_dump_read(struct file *filp,
1085 + char __user *buffer,
1086 + size_t count, loff_t *ppos)
1088 + struct ls_ep_dev *ep = filp->private_data;
1089 + struct ls_ep_test *test = ep->driver_data;
1091 + int desc = 0, i, len;
1093 + buf = kmalloc(4 * 1024, GFP_KERNEL);
1098 + dev_info(&ep->dev, " there is NO test\n");
1103 + desc += sprintf(buf + desc, "%s", "dump info:");
1104 + for (i = 0; i < 256; i += 4) {
1106 + desc += sprintf(buf + desc, "\n%08x:", i);
1107 + desc += sprintf(buf + desc, " %08x", readl(test->buf + i));
1110 + desc += sprintf(buf + desc, "\n");
1111 + len = simple_read_from_buffer(buffer, count, ppos, buf, desc);
1118 +static const struct file_operations ls_pcie_ep_dbg_dump_fops = {
1119 + .owner = THIS_MODULE,
1120 + .open = simple_open,
1121 + .read = ls_pcie_ep_dbg_dump_read,
1124 +static int ls_pcie_ep_dev_dbgfs_init(struct ls_ep_dev *ep)
1126 + struct ls_pcie *pcie = ep->pcie;
1127 + struct dentry *pfile;
1129 + ls_pcie_ep_dev_cfg_enable(ep);
1131 + ep->dir = debugfs_create_dir(dev_name(&ep->dev), pcie->dir);
1135 + pfile = debugfs_create_file("regs", 0600, ep->dir, ep,
1136 + &ls_pcie_ep_dbg_regs_fops);
1138 + dev_info(&ep->dev, "debugfs regs for failed\n");
1140 + pfile = debugfs_create_file("test", 0600, ep->dir, ep,
1141 + &ls_pcie_ep_dbg_test_fops);
1143 + dev_info(&ep->dev, "debugfs test for failed\n");
1145 + pfile = debugfs_create_file("dump", 0600, ep->dir, ep,
1146 + &ls_pcie_ep_dbg_dump_fops);
1148 + dev_info(&ep->dev, "debugfs dump for failed\n");
1153 +int ls_pcie_ep_dbgfs_init(struct ls_pcie *pcie)
1155 + struct ls_ep_dev *ep;
1157 + pcie->dir = debugfs_create_dir(dev_name(pcie->dev), NULL);
1161 + list_for_each_entry(ep, &pcie->ep_list, node)
1162 + ls_pcie_ep_dev_dbgfs_init(ep);
1167 +int ls_pcie_ep_dbgfs_remove(struct ls_pcie *pcie)
1169 + debugfs_remove_recursive(pcie->dir);
1173 +MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
1174 +MODULE_DESCRIPTION("Freescale Layerscape PCIe EP controller driver");
1175 +MODULE_LICENSE("GPL v2");
1177 +++ b/drivers/pci/host/pci-layerscape-ep.c
1180 + * PCIe Endpoint driver for Freescale Layerscape SoCs
1182 + * Copyright (C) 2015 Freescale Semiconductor.
1184 + * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
1186 + * This program is free software; you can redistribute it and/or modify
1187 + * it under the terms of the GNU General Public License version 2 as
1188 + * published by the Free Software Foundation.
1191 +#include <linux/kernel.h>
1192 +#include <linux/delay.h>
1193 +#include <linux/interrupt.h>
1194 +#include <linux/module.h>
1195 +#include <linux/of_pci.h>
1196 +#include <linux/of_platform.h>
1197 +#include <linux/of_irq.h>
1198 +#include <linux/of_address.h>
1199 +#include <linux/pci.h>
1200 +#include <linux/platform_device.h>
1201 +#include <linux/resource.h>
1202 +#include <linux/debugfs.h>
1203 +#include <linux/time.h>
1204 +#include <linux/uaccess.h>
1206 +#include "pci-layerscape-ep.h"
1209 +ls_pci_ep_find(struct ls_pcie *pcie, int dev_id)
1211 + struct ls_ep_dev *ep;
1213 + list_for_each_entry(ep, &pcie->ep_list, node) {
1214 + if (ep->dev_id == dev_id)
1221 +static void ls_pcie_try_cfg2(struct ls_pcie *pcie, int pf, int vf)
1224 + writel(PCIE_LCTRL0_VAL(pf, vf),
1225 + pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
1228 +static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
1230 + u32 header_type = 0;
1232 + header_type = readl(pcie->dbi + (PCI_HEADER_TYPE & ~0x3));
1233 + header_type = (header_type >> 16) & 0x7f;
1235 + return header_type == PCI_HEADER_TYPE_BRIDGE;
1238 +void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
1239 + u64 cpu_addr, u64 pci_addr, u32 size)
1241 + writel(PCIE_ATU_REGION_OUTBOUND | idx,
1242 + pcie->dbi + PCIE_ATU_VIEWPORT);
1243 + writel(lower_32_bits(cpu_addr),
1244 + pcie->dbi + PCIE_ATU_LOWER_BASE);
1245 + writel(upper_32_bits(cpu_addr),
1246 + pcie->dbi + PCIE_ATU_UPPER_BASE);
1247 + writel(lower_32_bits(cpu_addr + size - 1),
1248 + pcie->dbi + PCIE_ATU_LIMIT);
1249 + writel(lower_32_bits(pci_addr),
1250 + pcie->dbi + PCIE_ATU_LOWER_TARGET);
1251 + writel(upper_32_bits(pci_addr),
1252 + pcie->dbi + PCIE_ATU_UPPER_TARGET);
1253 + writel(type, pcie->dbi + PCIE_ATU_CR1);
1254 + writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
1257 +/* Use bar match mode and MEM type as default */
1258 +void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
1259 + int bar, u64 phys)
1261 + writel(PCIE_ATU_REGION_INBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
1262 + writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_TARGET);
1263 + writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
1264 + writel(PCIE_ATU_TYPE_MEM, pcie->dbi + PCIE_ATU_CR1);
1265 + writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
1266 + PCIE_ATU_BAR_NUM(bar), pcie->dbi + PCIE_ATU_CR2);
1269 +void ls_pcie_ep_dev_cfg_enable(struct ls_ep_dev *ep)
1271 + ls_pcie_try_cfg2(ep->pcie, ep->pf_idx, ep->vf_idx);
1274 +void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
1276 + if (size < 4 * 1024)
1281 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
1284 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
1287 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
1288 + writel(0, bar_base + PCI_BASE_ADDRESS_3);
1291 + writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
1292 + writel(0, bar_base + PCI_BASE_ADDRESS_5);
1299 +void ls_pcie_ep_dev_setup_bar(struct ls_ep_dev *ep, int bar, u32 size)
1301 + struct ls_pcie *pcie = ep->pcie;
1304 + if (size < 4 * 1024)
1308 + bar_base = pcie->dbi;
1310 + bar_base = pcie->dbi + PCIE_NO_SRIOV_BAR_BASE;
1312 + ls_pcie_ep_dev_cfg_enable(ep);
1313 + ls_pcie_ep_setup_bar(bar_base, bar, size);
1316 +static int ls_pcie_ep_dev_init(struct ls_pcie *pcie, int pf_idx, int vf_idx)
1318 + struct ls_ep_dev *ep;
1320 + ep = devm_kzalloc(pcie->dev, sizeof(*ep), GFP_KERNEL);
1325 + ep->pf_idx = pf_idx;
1326 + ep->vf_idx = vf_idx;
1328 + ep->dev_id = pf_idx + 4 + 4 * (vf_idx - 1);
1330 + ep->dev_id = pf_idx;
1333 + dev_set_name(&ep->dev, "pf%d-vf%d",
1337 + dev_set_name(&ep->dev, "pf%d",
1340 + list_add_tail(&ep->node, &pcie->ep_list);
1345 +static int ls_pcie_ep_init(struct ls_pcie *pcie)
1350 + sriov_header = readl(pcie->dbi + PCIE_SRIOV_POS);
1352 + if (PCI_EXT_CAP_ID(sriov_header) == PCI_EXT_CAP_ID_SRIOV) {
1353 + pcie->sriov = PCIE_SRIOV_POS;
1362 + for (i = 0; i < pf; i++) {
1363 + for (j = 0; j <= vf; j++)
1364 + ls_pcie_ep_dev_init(pcie, i, j);
1370 +static struct ls_pcie_ep_drvdata ls1043_drvdata = {
1371 + .lut_offset = 0x10000,
1372 + .ltssm_shift = 24,
1376 +static struct ls_pcie_ep_drvdata ls1046_drvdata = {
1377 + .lut_offset = 0x80000,
1378 + .ltssm_shift = 24,
1379 + .lut_dbg = 0x407fc,
1382 +static struct ls_pcie_ep_drvdata ls2080_drvdata = {
1383 + .lut_offset = 0x80000,
1388 +static const struct of_device_id ls_pcie_ep_of_match[] = {
1389 + { .compatible = "fsl,ls1021a-pcie", },
1390 + { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
1391 + { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
1392 + { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
1393 + { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
1396 +MODULE_DEVICE_TABLE(of, ls_pcie_ep_of_match);
1398 +static int ls_pcie_ep_probe(struct platform_device *pdev)
1400 + struct ls_pcie *pcie;
1401 + struct resource *dbi_base, *cfg_res;
1402 + const struct of_device_id *match;
1405 + match = of_match_device(ls_pcie_ep_of_match, &pdev->dev);
1409 + pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
1413 + pcie->dev = &pdev->dev;
1414 + INIT_LIST_HEAD(&pcie->ep_list);
1416 + dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1417 + pcie->dbi = devm_ioremap_resource(&pdev->dev, dbi_base);
1418 + if (IS_ERR(pcie->dbi)) {
1419 + dev_err(&pdev->dev, "missing *regs* space\n");
1420 + return PTR_ERR(pcie->dbi);
1423 + pcie->drvdata = match->data;
1424 + pcie->lut = pcie->dbi + pcie->drvdata->lut_offset;
1426 + if (ls_pcie_is_bridge(pcie))
1429 + dev_info(pcie->dev, "in EP mode\n");
1431 + cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
1433 + pcie->out_base = cfg_res->start;
1435 + dev_err(&pdev->dev, "missing *config* space\n");
1439 + ret = ls_pcie_ep_init(pcie);
1443 + ls_pcie_ep_dbgfs_init(pcie);
1445 + platform_set_drvdata(pdev, pcie);
1450 +static int ls_pcie_ep_dev_remove(struct ls_ep_dev *ep)
1452 + list_del(&ep->node);
1457 +static int ls_pcie_ep_remove(struct platform_device *pdev)
1459 + struct ls_pcie *pcie = platform_get_drvdata(pdev);
1460 + struct ls_ep_dev *ep, *tmp;
1465 + ls_pcie_ep_dbgfs_remove(pcie);
1467 + list_for_each_entry_safe(ep, tmp, &pcie->ep_list, node)
1468 + ls_pcie_ep_dev_remove(ep);
1473 +static struct platform_driver ls_pcie_ep_driver = {
1475 + .name = "ls-pcie-ep",
1476 + .owner = THIS_MODULE,
1477 + .of_match_table = ls_pcie_ep_of_match,
1479 + .probe = ls_pcie_ep_probe,
1480 + .remove = ls_pcie_ep_remove,
1483 +module_platform_driver(ls_pcie_ep_driver);
1485 +MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
1486 +MODULE_DESCRIPTION("Freescale Layerscape PCIe EP driver");
1487 +MODULE_LICENSE("GPL v2");
1489 +++ b/drivers/pci/host/pci-layerscape-ep.h
1492 + * PCIe Endpoint driver for Freescale Layerscape SoCs
1494 + * Copyright (C) 2015 Freescale Semiconductor.
1496 + * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
1498 + * This program is free software; you can redistribute it and/or modify
1499 + * it under the terms of the GNU General Public License version 2 as
1500 + * published by the Free Software Foundation.
1504 +#ifndef _PCIE_LAYERSCAPE_EP_H
1505 +#define _PCIE_LAYERSCAPE_EP_H
1507 +#include <linux/device.h>
1509 +/* Synopsis specific PCIE configuration registers */
1510 +#define PCIE_ATU_VIEWPORT 0x900
1511 +#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
1512 +#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
1513 +#define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
1514 +#define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
1515 +#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
1516 +#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
1517 +#define PCIE_ATU_CR1 0x904
1518 +#define PCIE_ATU_TYPE_MEM (0x0 << 0)
1519 +#define PCIE_ATU_TYPE_IO (0x2 << 0)
1520 +#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
1521 +#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
1522 +#define PCIE_ATU_CR2 0x908
1523 +#define PCIE_ATU_ENABLE (0x1 << 31)
1524 +#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
1525 +#define PCIE_ATU_LOWER_BASE 0x90C
1526 +#define PCIE_ATU_UPPER_BASE 0x910
1527 +#define PCIE_ATU_LIMIT 0x914
1528 +#define PCIE_ATU_LOWER_TARGET 0x918
1529 +#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
1530 +#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
1531 +#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
1532 +#define PCIE_ATU_UPPER_TARGET 0x91C
1534 +/* PEX internal configuration registers */
1535 +#define PCIE_DBI_RO_WR_EN 0x8bc /* DBI Read-Only Write Enable Register */
1537 +/* PEX LUT registers */
1538 +#define PCIE_LUT_BASE 0x80000
1539 +#define PCIE_LUT_DBG 0x7FC /* PEX LUT Debug register */
1541 +#define PCIE_LUT_LCTRL0 0x7F8
1543 +#define PCIE_ATU_BAR_NUM(bar) ((bar) << 8)
1544 +#define PCIE_LCTRL0_CFG2_ENABLE (1 << 31)
1545 +#define PCIE_LCTRL0_VF(vf) ((vf) << 22)
1546 +#define PCIE_LCTRL0_PF(pf) ((pf) << 16)
1547 +#define PCIE_LCTRL0_VF_ACTIVE (1 << 21)
1548 +#define PCIE_LCTRL0_VAL(pf, vf) (PCIE_LCTRL0_PF(pf) | \
1549 + PCIE_LCTRL0_VF(vf) | \
1550 + ((vf) == 0 ? 0 : PCIE_LCTRL0_VF_ACTIVE) | \
1551 + PCIE_LCTRL0_CFG2_ENABLE)
1553 +#define PCIE_NO_SRIOV_BAR_BASE 0x1000
1555 +#define PCIE_SRIOV_POS 0x178
1556 +#define PCIE_PF_NUM 2
1557 +#define PCIE_VF_NUM 64
1559 +struct ls_pcie_ep_drvdata {
1566 + struct list_head ep_list;
1567 + struct device *dev;
1568 + struct dentry *dir;
1569 + const struct ls_pcie_ep_drvdata *drvdata;
1570 + void __iomem *dbi;
1571 + void __iomem *lut;
1572 + phys_addr_t out_base;
1578 + struct list_head node;
1579 + struct ls_pcie *pcie;
1580 + struct device dev;
1581 + struct dentry *dir;
1585 + void *driver_data;
1588 +struct ls_ep_dev *ls_pci_ep_find(struct ls_pcie *pcie, int dev_id);
1590 +void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
1591 + u64 cpu_addr, u64 pci_addr, u32 size);
1593 +/* Use bar match mode and MEM type as default */
1594 +void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
1595 + int bar, u64 phys);
1597 +void ls_pcie_ep_dev_setup_bar(struct ls_ep_dev *ep, int bar, u32 size);
1600 +void ls_pcie_ep_dev_cfg_enable(struct ls_ep_dev *ep);
1602 +int ls_pcie_ep_dbgfs_init(struct ls_pcie *pcie);
1603 +int ls_pcie_ep_dbgfs_remove(struct ls_pcie *pcie);
1605 +#endif /* _PCIE_LAYERSCAPE_EP_H */
1606 --- a/drivers/pci/host/pci-layerscape.c
1607 +++ b/drivers/pci/host/pci-layerscape.c
1609 #define PCIE_STRFMR1 0x71c /* Symbol Timer & Filter Mask Register1 */
1610 #define PCIE_DBI_RO_WR_EN 0x8bc /* DBI Read-Only Write Enable Register */
1612 -/* PEX LUT registers */
1613 -#define PCIE_LUT_DBG 0x7FC /* PEX LUT Debug Register */
1614 +#define PCIE_IATU_NUM 6
1616 +static void ls_pcie_host_init(struct pcie_port *pp);
1618 struct ls_pcie_drvdata {
1622 struct pcie_host_ops *ops;
1625 @@ -86,6 +88,14 @@ static void ls_pcie_drop_msg_tlp(struct
1626 iowrite32(val, pcie->pp.dbi_base + PCIE_STRFMR1);
1629 +static void ls_pcie_disable_outbound_atus(struct ls_pcie *pcie)
1633 + for (i = 0; i < PCIE_IATU_NUM; i++)
1634 + dw_pcie_disable_outbound_atu(&pcie->pp, i);
1637 static int ls1021_pcie_link_up(struct pcie_port *pp)
1640 @@ -134,7 +144,7 @@ static int ls_pcie_link_up(struct pcie_p
1641 struct ls_pcie *pcie = to_ls_pcie(pp);
1644 - state = (ioread32(pcie->lut + PCIE_LUT_DBG) >>
1645 + state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
1646 pcie->drvdata->ltssm_shift) &
1649 @@ -153,6 +163,9 @@ static void ls_pcie_host_init(struct pci
1650 ls_pcie_clear_multifunction(pcie);
1651 ls_pcie_drop_msg_tlp(pcie);
1652 iowrite32(0, pcie->pp.dbi_base + PCIE_DBI_RO_WR_EN);
1654 + ls_pcie_disable_outbound_atus(pcie);
1655 + dw_pcie_setup_rc(pp);
1658 static int ls_pcie_msi_host_init(struct pcie_port *pp,
1659 @@ -196,20 +209,38 @@ static struct ls_pcie_drvdata ls1021_drv
1660 static struct ls_pcie_drvdata ls1043_drvdata = {
1661 .lut_offset = 0x10000,
1664 + .ops = &ls_pcie_host_ops,
1667 +static struct ls_pcie_drvdata ls1046_drvdata = {
1668 + .lut_offset = 0x80000,
1669 + .ltssm_shift = 24,
1670 + .lut_dbg = 0x407fc,
1671 .ops = &ls_pcie_host_ops,
1674 static struct ls_pcie_drvdata ls2080_drvdata = {
1675 .lut_offset = 0x80000,
1678 + .ops = &ls_pcie_host_ops,
1681 +static struct ls_pcie_drvdata ls2088_drvdata = {
1682 + .lut_offset = 0x80000,
1684 + .lut_dbg = 0x407fc,
1685 .ops = &ls_pcie_host_ops,
1688 static const struct of_device_id ls_pcie_of_match[] = {
1689 { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
1690 { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
1691 + { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
1692 { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
1693 { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
1694 + { .compatible = "fsl,ls2088a-pcie", .data = &ls2088_drvdata },
1698 --- a/drivers/pci/host/pcie-designware.c
1699 +++ b/drivers/pci/host/pcie-designware.c
1700 @@ -478,6 +478,12 @@ int dw_pcie_wait_for_link(struct pcie_po
1704 +void dw_pcie_disable_outbound_atu(struct pcie_port *pp, int index)
1706 + dw_pcie_writel_rc(pp, PCIE_ATU_VIEWPORT, PCIE_ATU_REGION_OUTBOUND | index);
1707 + dw_pcie_writel_rc(pp, PCIE_ATU_CR2, 0);
1710 int dw_pcie_link_up(struct pcie_port *pp)
1713 --- a/drivers/pci/host/pcie-designware.h
1714 +++ b/drivers/pci/host/pcie-designware.h
1715 @@ -82,5 +82,6 @@ int dw_pcie_wait_for_link(struct pcie_po
1716 int dw_pcie_link_up(struct pcie_port *pp);
1717 void dw_pcie_setup_rc(struct pcie_port *pp);
1718 int dw_pcie_host_init(struct pcie_port *pp);
1719 +void dw_pcie_disable_outbound_atu(struct pcie_port *pp, int index);
1721 #endif /* _PCIE_DESIGNWARE_H */
1722 --- a/drivers/pci/pcie/portdrv_core.c
1723 +++ b/drivers/pci/pcie/portdrv_core.c
1724 @@ -44,52 +44,30 @@ static void release_pcie_device(struct d
1728 - * pcie_port_msix_add_entry - add entry to given array of MSI-X entries
1729 - * @entries: Array of MSI-X entries
1730 - * @new_entry: Index of the entry to add to the array
1731 - * @nr_entries: Number of entries already in the array
1732 + * pcibios_check_service_irqs - check irqs in the device tree
1733 + * @dev: PCI Express port to handle
1734 + * @irqs: Array of irqs to populate
1735 + * @mask: Bitmask of port capabilities returned by get_port_device_capability()
1737 + * Return value: 0 means no service irqs in the device tree
1739 - * Return value: Position of the added entry in the array
1741 -static int pcie_port_msix_add_entry(
1742 - struct msix_entry *entries, int new_entry, int nr_entries)
1743 +int __weak pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask)
1747 - for (j = 0; j < nr_entries; j++)
1748 - if (entries[j].entry == new_entry)
1751 - entries[j].entry = new_entry;
1757 * pcie_port_enable_msix - try to set up MSI-X as interrupt mode for given port
1758 * @dev: PCI Express port to handle
1759 - * @vectors: Array of interrupt vectors to populate
1760 + * @irqs: Array of interrupt vectors to populate
1761 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
1763 * Return value: 0 on success, error code on failure
1765 -static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
1766 +static int pcie_port_enable_msix(struct pci_dev *dev, int *irqs, int mask)
1768 - struct msix_entry *msix_entries;
1769 - int idx[PCIE_PORT_DEVICE_MAXSERVICES];
1770 - int nr_entries, status, pos, i, nvec;
1774 - nr_entries = pci_msix_vec_count(dev);
1775 - if (nr_entries < 0)
1776 - return nr_entries;
1777 - BUG_ON(!nr_entries);
1778 - if (nr_entries > PCIE_PORT_MAX_MSIX_ENTRIES)
1779 - nr_entries = PCIE_PORT_MAX_MSIX_ENTRIES;
1781 - msix_entries = kzalloc(sizeof(*msix_entries) * nr_entries, GFP_KERNEL);
1782 - if (!msix_entries)
1784 + int nr_entries, entry, nvec = 0;
1787 * Allocate as many entries as the port wants, so that we can check
1788 @@ -97,20 +75,13 @@ static int pcie_port_enable_msix(struct
1789 * equal to the number of entries this port actually uses, we'll happily
1790 * go through without any tricks.
1792 - for (i = 0; i < nr_entries; i++)
1793 - msix_entries[i].entry = i;
1795 - status = pci_enable_msix_exact(dev, msix_entries, nr_entries);
1799 - for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1803 + nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSIX_ENTRIES,
1805 + if (nr_entries < 0)
1806 + return nr_entries;
1808 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
1813 * The code below follows the PCI Express Base Specification 2.0
1814 @@ -125,18 +96,16 @@ static int pcie_port_enable_msix(struct
1815 pcie_capability_read_word(dev, PCI_EXP_FLAGS, ®16);
1816 entry = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
1817 if (entry >= nr_entries)
1819 + goto out_free_irqs;
1821 - i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
1824 + irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, entry);
1825 + irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, entry);
1827 - idx[PCIE_PORT_SERVICE_PME_SHIFT] = i;
1828 - idx[PCIE_PORT_SERVICE_HP_SHIFT] = i;
1829 + nvec = max(nvec, entry + 1);
1832 if (mask & PCIE_PORT_SERVICE_AER) {
1837 * The code below follows Section 7.10.10 of the PCI Express
1838 @@ -151,13 +120,11 @@ static int pcie_port_enable_msix(struct
1839 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, ®32);
1840 entry = reg32 >> 27;
1841 if (entry >= nr_entries)
1843 + goto out_free_irqs;
1845 - i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
1848 + irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, entry);
1850 - idx[PCIE_PORT_SERVICE_AER_SHIFT] = i;
1851 + nvec = max(nvec, entry + 1);
1855 @@ -165,41 +132,54 @@ static int pcie_port_enable_msix(struct
1856 * what we have. Otherwise, the port has some extra entries not for the
1857 * services we know and we need to work around that.
1859 - if (nvec == nr_entries) {
1862 + if (nvec != nr_entries) {
1863 /* Drop the temporary MSI-X setup */
1864 - pci_disable_msix(dev);
1865 + pci_free_irq_vectors(dev);
1867 /* Now allocate the MSI-X vectors for real */
1868 - status = pci_enable_msix_exact(dev, msix_entries, nvec);
1871 + nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
1873 + if (nr_entries < 0)
1874 + return nr_entries;
1877 - for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1878 - vectors[i] = idx[i] >= 0 ? msix_entries[idx[i]].vector : -1;
1881 - kfree(msix_entries);
1886 - pci_disable_msix(dev);
1889 + pci_free_irq_vectors(dev);
1894 - * init_service_irqs - initialize irqs for PCI Express port services
1895 + * pcie_init_service_irqs - initialize irqs for PCI Express port services
1896 * @dev: PCI Express port to handle
1897 * @irqs: Array of irqs to populate
1898 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
1900 * Return value: Interrupt mode associated with the port
1902 -static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
1903 +static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
1906 + unsigned flags = PCI_IRQ_LEGACY | PCI_IRQ_MSI;
1910 + for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1913 + /* Check if some platforms owns independent irq pins for AER/PME etc.
1914 + * Some platforms may own independent AER/PME interrupts and set
1915 + * them in the device tree file.
1917 + ret = pcibios_check_service_irqs(dev, irqs, mask);
1921 + for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1922 + if (irqs[i] == -1 && i != PCIE_PORT_SERVICE_VC_SHIFT)
1928 * If MSI cannot be used for PCIe PME or hotplug, we have to use
1929 @@ -207,41 +187,25 @@ static int init_service_irqs(struct pci_
1931 if (((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) ||
1932 ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())) {
1936 + flags &= ~PCI_IRQ_MSI;
1938 + /* Try to use MSI-X if supported */
1939 + if (!pcie_port_enable_msix(dev, irqs, mask))
1943 - /* Try to use MSI-X if supported */
1944 - if (!pcie_port_enable_msix(dev, irqs, mask))
1948 - * We're not going to use MSI-X, so try MSI and fall back to INTx.
1949 - * If neither MSI/MSI-X nor INTx available, try other interrupt. On
1950 - * some platforms, root port doesn't support MSI/MSI-X/INTx in RC mode.
1952 - if (!pci_enable_msi(dev) || dev->irq)
1954 + ret = pci_alloc_irq_vectors(dev, 1, 1, flags);
1959 - for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
1961 - irqs[PCIE_PORT_SERVICE_VC_SHIFT] = -1;
1962 + for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
1963 + if (i != PCIE_PORT_SERVICE_VC_SHIFT)
1964 + irqs[i] = pci_irq_vector(dev, 0);
1972 -static void cleanup_service_irqs(struct pci_dev *dev)
1974 - if (dev->msix_enabled)
1975 - pci_disable_msix(dev);
1976 - else if (dev->msi_enabled)
1977 - pci_disable_msi(dev);
1981 * get_port_device_capability - discover capabilities of a PCI Express port
1982 * @dev: PCI Express port to examine
1983 @@ -378,7 +342,7 @@ int pcie_port_device_register(struct pci
1984 * that can be used in the absence of irqs. Allow them to determine
1985 * if that is to be used.
1987 - status = init_service_irqs(dev, irqs, capabilities);
1988 + status = pcie_init_service_irqs(dev, irqs, capabilities);
1990 capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
1992 @@ -401,7 +365,7 @@ int pcie_port_device_register(struct pci
1996 - cleanup_service_irqs(dev);
1997 + pci_free_irq_vectors(dev);
1999 pci_disable_device(dev);
2001 @@ -469,7 +433,7 @@ static int remove_iter(struct device *de
2002 void pcie_port_device_remove(struct pci_dev *dev)
2004 device_for_each_child(&dev->dev, NULL, remove_iter);
2005 - cleanup_service_irqs(dev);
2006 + pci_free_irq_vectors(dev);
2007 pci_disable_device(dev);
2010 @@ -499,7 +463,6 @@ static int pcie_port_probe_service(struc
2014 - dev_printk(KERN_DEBUG, dev, "service driver %s loaded\n", driver->name);
2018 @@ -524,8 +487,6 @@ static int pcie_port_remove_service(stru
2019 pciedev = to_pcie_device(dev);
2020 driver = to_service_driver(dev->driver);
2021 if (driver && driver->remove) {
2022 - dev_printk(KERN_DEBUG, dev, "unloading service driver %s\n",
2024 driver->remove(pciedev);
2027 --- a/include/linux/pci.h
2028 +++ b/include/linux/pci.h
2029 @@ -1823,6 +1823,7 @@ void pcibios_release_device(struct pci_d
2030 void pcibios_penalize_isa_irq(int irq, int active);
2031 int pcibios_alloc_irq(struct pci_dev *dev);
2032 void pcibios_free_irq(struct pci_dev *dev);
2033 +int pcibios_check_service_irqs(struct pci_dev *dev, int *irqs, int mask);
2035 #ifdef CONFIG_HIBERNATE_CALLBACKS
2036 extern struct dev_pm_ops pcibios_pm_ops;