diff options
author | Marc Gonzalez <marc_gonzalez@sigmadesigns.com> | 2017-09-26 06:27:39 -0400 |
---|---|---|
committer | Bjorn Helgaas <bhelgaas@google.com> | 2017-10-05 16:52:35 -0400 |
commit | d76bdce394bf48140cbb33b07509c32df1cef8e7 (patch) | |
tree | f2ee013ddbae88d1b2d9482f78ff7053ebeae2fb | |
parent | 1e61a57cac560844580ab532a4e2f5061ef77039 (diff) |
PCI: tango: Add MSI controller support
Add support for the MSI controller in Tango, which supports 256
message-signaled interrupts and a single doorbell address.
Signed-off-by: Marc Gonzalez <marc_gonzalez@sigmadesigns.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
-rw-r--r-- | drivers/pci/host/pcie-tango.c | 205 |
1 files changed, 202 insertions, 3 deletions
diff --git a/drivers/pci/host/pcie-tango.c b/drivers/pci/host/pcie-tango.c index 6bbb81f06a53..e23f7383ac33 100644 --- a/drivers/pci/host/pcie-tango.c +++ b/drivers/pci/host/pcie-tango.c | |||
@@ -1,12 +1,172 @@ | |||
1 | #include <linux/irqchip/chained_irq.h> | ||
2 | #include <linux/irqdomain.h> | ||
1 | #include <linux/pci-ecam.h> | 3 | #include <linux/pci-ecam.h> |
2 | #include <linux/delay.h> | 4 | #include <linux/delay.h> |
3 | #include <linux/of.h> | 5 | #include <linux/msi.h> |
6 | #include <linux/of_address.h> | ||
7 | |||
8 | #define MSI_MAX 256 | ||
4 | 9 | ||
5 | #define SMP8759_MUX 0x48 | 10 | #define SMP8759_MUX 0x48 |
6 | #define SMP8759_TEST_OUT 0x74 | 11 | #define SMP8759_TEST_OUT 0x74 |
12 | #define SMP8759_DOORBELL 0x7c | ||
13 | #define SMP8759_STATUS 0x80 | ||
14 | #define SMP8759_ENABLE 0xa0 | ||
7 | 15 | ||
8 | struct tango_pcie { | 16 | struct tango_pcie { |
9 | void __iomem *base; | 17 | DECLARE_BITMAP(used_msi, MSI_MAX); |
18 | u64 msi_doorbell; | ||
19 | spinlock_t used_msi_lock; | ||
20 | void __iomem *base; | ||
21 | struct irq_domain *dom; | ||
22 | }; | ||
23 | |||
24 | static void tango_msi_isr(struct irq_desc *desc) | ||
25 | { | ||
26 | struct irq_chip *chip = irq_desc_get_chip(desc); | ||
27 | struct tango_pcie *pcie = irq_desc_get_handler_data(desc); | ||
28 | unsigned long status, base, virq, idx, pos = 0; | ||
29 | |||
30 | chained_irq_enter(chip, desc); | ||
31 | spin_lock(&pcie->used_msi_lock); | ||
32 | |||
33 | while ((pos = find_next_bit(pcie->used_msi, MSI_MAX, pos)) < MSI_MAX) { | ||
34 | base = round_down(pos, 32); | ||
35 | status = readl_relaxed(pcie->base + SMP8759_STATUS + base / 8); | ||
36 | for_each_set_bit(idx, &status, 32) { | ||
37 | virq = irq_find_mapping(pcie->dom, base + idx); | ||
38 | generic_handle_irq(virq); | ||
39 | } | ||
40 | pos = base + 32; | ||
41 | } | ||
42 | |||
43 | spin_unlock(&pcie->used_msi_lock); | ||
44 | chained_irq_exit(chip, desc); | ||
45 | } | ||
46 | |||
47 | static void tango_ack(struct irq_data *d) | ||
48 | { | ||
49 | struct tango_pcie *pcie = d->chip_data; | ||
50 | u32 offset = (d->hwirq / 32) * 4; | ||
51 | u32 bit = BIT(d->hwirq % 32); | ||
52 | |||
53 | writel_relaxed(bit, pcie->base + SMP8759_STATUS + offset); | ||
54 | } | ||
55 | |||
56 | static void update_msi_enable(struct irq_data *d, bool unmask) | ||
57 | { | ||
58 | unsigned long flags; | ||
59 | struct tango_pcie *pcie = d->chip_data; | ||
60 | u32 offset = (d->hwirq / 32) * 4; | ||
61 | u32 bit = BIT(d->hwirq % 32); | ||
62 | u32 val; | ||
63 | |||
64 | spin_lock_irqsave(&pcie->used_msi_lock, flags); | ||
65 | val = readl_relaxed(pcie->base + SMP8759_ENABLE + offset); | ||
66 | val = unmask ? val | bit : val & ~bit; | ||
67 | writel_relaxed(val, pcie->base + SMP8759_ENABLE + offset); | ||
68 | spin_unlock_irqrestore(&pcie->used_msi_lock, flags); | ||
69 | } | ||
70 | |||
71 | static void tango_mask(struct irq_data *d) | ||
72 | { | ||
73 | update_msi_enable(d, false); | ||
74 | } | ||
75 | |||
76 | static void tango_unmask(struct irq_data *d) | ||
77 | { | ||
78 | update_msi_enable(d, true); | ||
79 | } | ||
80 | |||
81 | static int tango_set_affinity(struct irq_data *d, const struct cpumask *mask, | ||
82 | bool force) | ||
83 | { | ||
84 | return -EINVAL; | ||
85 | } | ||
86 | |||
87 | static void tango_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) | ||
88 | { | ||
89 | struct tango_pcie *pcie = d->chip_data; | ||
90 | msg->address_lo = lower_32_bits(pcie->msi_doorbell); | ||
91 | msg->address_hi = upper_32_bits(pcie->msi_doorbell); | ||
92 | msg->data = d->hwirq; | ||
93 | } | ||
94 | |||
95 | static struct irq_chip tango_chip = { | ||
96 | .irq_ack = tango_ack, | ||
97 | .irq_mask = tango_mask, | ||
98 | .irq_unmask = tango_unmask, | ||
99 | .irq_set_affinity = tango_set_affinity, | ||
100 | .irq_compose_msi_msg = tango_compose_msi_msg, | ||
101 | }; | ||
102 | |||
103 | static void msi_ack(struct irq_data *d) | ||
104 | { | ||
105 | irq_chip_ack_parent(d); | ||
106 | } | ||
107 | |||
108 | static void msi_mask(struct irq_data *d) | ||
109 | { | ||
110 | pci_msi_mask_irq(d); | ||
111 | irq_chip_mask_parent(d); | ||
112 | } | ||
113 | |||
114 | static void msi_unmask(struct irq_data *d) | ||
115 | { | ||
116 | pci_msi_unmask_irq(d); | ||
117 | irq_chip_unmask_parent(d); | ||
118 | } | ||
119 | |||
120 | static struct irq_chip msi_chip = { | ||
121 | .name = "MSI", | ||
122 | .irq_ack = msi_ack, | ||
123 | .irq_mask = msi_mask, | ||
124 | .irq_unmask = msi_unmask, | ||
125 | }; | ||
126 | |||
127 | static struct msi_domain_info msi_dom_info = { | ||
128 | .flags = MSI_FLAG_PCI_MSIX | ||
129 | | MSI_FLAG_USE_DEF_DOM_OPS | ||
130 | | MSI_FLAG_USE_DEF_CHIP_OPS, | ||
131 | .chip = &msi_chip, | ||
132 | }; | ||
133 | |||
134 | static int tango_irq_domain_alloc(struct irq_domain *dom, unsigned int virq, | ||
135 | unsigned int nr_irqs, void *args) | ||
136 | { | ||
137 | struct tango_pcie *pcie = dom->host_data; | ||
138 | unsigned long flags; | ||
139 | int pos; | ||
140 | |||
141 | spin_lock_irqsave(&pcie->used_msi_lock, flags); | ||
142 | pos = find_first_zero_bit(pcie->used_msi, MSI_MAX); | ||
143 | if (pos >= MSI_MAX) { | ||
144 | spin_unlock_irqrestore(&pcie->used_msi_lock, flags); | ||
145 | return -ENOSPC; | ||
146 | } | ||
147 | __set_bit(pos, pcie->used_msi); | ||
148 | spin_unlock_irqrestore(&pcie->used_msi_lock, flags); | ||
149 | irq_domain_set_info(dom, virq, pos, &tango_chip, | ||
150 | pcie, handle_edge_irq, NULL, NULL); | ||
151 | |||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | static void tango_irq_domain_free(struct irq_domain *dom, unsigned int virq, | ||
156 | unsigned int nr_irqs) | ||
157 | { | ||
158 | unsigned long flags; | ||
159 | struct irq_data *d = irq_domain_get_irq_data(dom, virq); | ||
160 | struct tango_pcie *pcie = d->chip_data; | ||
161 | |||
162 | spin_lock_irqsave(&pcie->used_msi_lock, flags); | ||
163 | __clear_bit(d->hwirq, pcie->used_msi); | ||
164 | spin_unlock_irqrestore(&pcie->used_msi_lock, flags); | ||
165 | } | ||
166 | |||
167 | static const struct irq_domain_ops dom_ops = { | ||
168 | .alloc = tango_irq_domain_alloc, | ||
169 | .free = tango_irq_domain_free, | ||
10 | }; | 170 | }; |
11 | 171 | ||
12 | static int smp8759_config_read(struct pci_bus *bus, unsigned int devfn, | 172 | static int smp8759_config_read(struct pci_bus *bus, unsigned int devfn, |
@@ -76,7 +236,11 @@ static int tango_pcie_probe(struct platform_device *pdev) | |||
76 | struct device *dev = &pdev->dev; | 236 | struct device *dev = &pdev->dev; |
77 | struct tango_pcie *pcie; | 237 | struct tango_pcie *pcie; |
78 | struct resource *res; | 238 | struct resource *res; |
79 | int ret; | 239 | struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node); |
240 | struct irq_domain *msi_dom, *irq_dom; | ||
241 | struct of_pci_range_parser parser; | ||
242 | struct of_pci_range range; | ||
243 | int virq, offset; | ||
80 | 244 | ||
81 | dev_warn(dev, "simultaneous PCI config and MMIO accesses may cause data corruption\n"); | 245 | dev_warn(dev, "simultaneous PCI config and MMIO accesses may cause data corruption\n"); |
82 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); | 246 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
@@ -95,6 +259,41 @@ static int tango_pcie_probe(struct platform_device *pdev) | |||
95 | if (!tango_pcie_link_up(pcie)) | 259 | if (!tango_pcie_link_up(pcie)) |
96 | return -ENODEV; | 260 | return -ENODEV; |
97 | 261 | ||
262 | if (of_pci_dma_range_parser_init(&parser, dev->of_node) < 0) | ||
263 | return -ENOENT; | ||
264 | |||
265 | if (of_pci_range_parser_one(&parser, &range) == NULL) | ||
266 | return -ENOENT; | ||
267 | |||
268 | range.pci_addr += range.size; | ||
269 | pcie->msi_doorbell = range.pci_addr + res->start + SMP8759_DOORBELL; | ||
270 | |||
271 | for (offset = 0; offset < MSI_MAX / 8; offset += 4) | ||
272 | writel_relaxed(0, pcie->base + SMP8759_ENABLE + offset); | ||
273 | |||
274 | virq = platform_get_irq(pdev, 1); | ||
275 | if (virq <= 0) { | ||
276 | dev_err(dev, "Failed to map IRQ\n"); | ||
277 | return -ENXIO; | ||
278 | } | ||
279 | |||
280 | irq_dom = irq_domain_create_linear(fwnode, MSI_MAX, &dom_ops, pcie); | ||
281 | if (!irq_dom) { | ||
282 | dev_err(dev, "Failed to create IRQ domain\n"); | ||
283 | return -ENOMEM; | ||
284 | } | ||
285 | |||
286 | msi_dom = pci_msi_create_irq_domain(fwnode, &msi_dom_info, irq_dom); | ||
287 | if (!msi_dom) { | ||
288 | dev_err(dev, "Failed to create MSI domain\n"); | ||
289 | irq_domain_remove(irq_dom); | ||
290 | return -ENOMEM; | ||
291 | } | ||
292 | |||
293 | pcie->dom = irq_dom; | ||
294 | spin_lock_init(&pcie->used_msi_lock); | ||
295 | irq_set_chained_handler_and_data(virq, tango_msi_isr, pcie); | ||
296 | |||
98 | return pci_host_common_probe(pdev, &smp8759_ecam_ops); | 297 | return pci_host_common_probe(pdev, &smp8759_ecam_ops); |
99 | } | 298 | } |
100 | 299 | ||