blob: 3b88e17d237c08b6d21119e6840c44f3b3e5ec6d [file] [log] [blame]
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +00001/*
2 * ARM GIC v2m MSI(-X) support
3 * Support for Message Signaled Interrupts for systems that
4 * implement ARM Generic Interrupt Controller: GICv2m.
5 *
6 * Copyright (C) 2014 Advanced Micro Devices, Inc.
7 * Authors: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
8 * Harish Kasiviswanathan <harish.kasiviswanathan@amd.com>
9 * Brandon Anderson <brandon.anderson@amd.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
14 */
15
16#define pr_fmt(fmt) "GICv2m: " fmt
17
18#include <linux/irq.h>
19#include <linux/irqdomain.h>
20#include <linux/kernel.h>
21#include <linux/of_address.h>
22#include <linux/of_pci.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25
26/*
27* MSI_TYPER:
28* [31:26] Reserved
29* [25:16] lowest SPI assigned to MSI
30* [15:10] Reserved
31* [9:0] Numer of SPIs assigned to MSI
32*/
33#define V2M_MSI_TYPER 0x008
34#define V2M_MSI_TYPER_BASE_SHIFT 16
35#define V2M_MSI_TYPER_BASE_MASK 0x3FF
36#define V2M_MSI_TYPER_NUM_MASK 0x3FF
37#define V2M_MSI_SETSPI_NS 0x040
38#define V2M_MIN_SPI 32
39#define V2M_MAX_SPI 1019
Duc Dangee5f7d62015-10-06 15:32:38 -070040#define V2M_MSI_IIDR 0xFCC
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000041
42#define V2M_MSI_TYPER_BASE_SPI(x) \
43 (((x) >> V2M_MSI_TYPER_BASE_SHIFT) & V2M_MSI_TYPER_BASE_MASK)
44
45#define V2M_MSI_TYPER_NUM_SPI(x) ((x) & V2M_MSI_TYPER_NUM_MASK)
46
Duc Dangee5f7d62015-10-06 15:32:38 -070047/* APM X-Gene with GICv2m MSI_IIDR register value */
48#define XGENE_GICV2M_MSI_IIDR 0x06000170
49
50/* List of flags for specific v2m implementation */
51#define GICV2M_NEEDS_SPI_OFFSET 0x00000001
52
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000053struct v2m_data {
54 spinlock_t msi_cnt_lock;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000055 struct resource res; /* GICv2m resource */
56 void __iomem *base; /* GICv2m virt address */
57 u32 spi_start; /* The SPI number that MSIs start */
58 u32 nr_spis; /* The number of SPIs for MSIs */
59 unsigned long *bm; /* MSI vector bitmap */
Duc Dangee5f7d62015-10-06 15:32:38 -070060 u32 flags; /* v2m flags for specific implementation */
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000061};
62
63static void gicv2m_mask_msi_irq(struct irq_data *d)
64{
65 pci_msi_mask_irq(d);
66 irq_chip_mask_parent(d);
67}
68
69static void gicv2m_unmask_msi_irq(struct irq_data *d)
70{
71 pci_msi_unmask_irq(d);
72 irq_chip_unmask_parent(d);
73}
74
75static struct irq_chip gicv2m_msi_irq_chip = {
76 .name = "MSI",
77 .irq_mask = gicv2m_mask_msi_irq,
78 .irq_unmask = gicv2m_unmask_msi_irq,
79 .irq_eoi = irq_chip_eoi_parent,
80 .irq_write_msi_msg = pci_msi_domain_write_msg,
81};
82
83static struct msi_domain_info gicv2m_msi_domain_info = {
84 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
85 MSI_FLAG_PCI_MSIX),
86 .chip = &gicv2m_msi_irq_chip,
87};
88
89static int gicv2m_set_affinity(struct irq_data *irq_data,
90 const struct cpumask *mask, bool force)
91{
92 int ret;
93
94 ret = irq_chip_set_affinity_parent(irq_data, mask, force);
95 if (ret == IRQ_SET_MASK_OK)
96 ret = IRQ_SET_MASK_OK_DONE;
97
98 return ret;
99}
100
101static void gicv2m_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
102{
103 struct v2m_data *v2m = irq_data_get_irq_chip_data(data);
104 phys_addr_t addr = v2m->res.start + V2M_MSI_SETSPI_NS;
105
Pavel Fedin157add62015-09-13 12:14:33 +0100106 msg->address_hi = upper_32_bits(addr);
107 msg->address_lo = lower_32_bits(addr);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000108 msg->data = data->hwirq;
Duc Dangee5f7d62015-10-06 15:32:38 -0700109
110 if (v2m->flags & GICV2M_NEEDS_SPI_OFFSET)
111 msg->data -= v2m->spi_start;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000112}
113
114static struct irq_chip gicv2m_irq_chip = {
115 .name = "GICv2m",
116 .irq_mask = irq_chip_mask_parent,
117 .irq_unmask = irq_chip_unmask_parent,
118 .irq_eoi = irq_chip_eoi_parent,
119 .irq_set_affinity = gicv2m_set_affinity,
120 .irq_compose_msi_msg = gicv2m_compose_msi_msg,
121};
122
123static int gicv2m_irq_gic_domain_alloc(struct irq_domain *domain,
124 unsigned int virq,
125 irq_hw_number_t hwirq)
126{
Marc Zyngierf833f572015-10-13 12:51:33 +0100127 struct irq_fwspec fwspec;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000128 struct irq_data *d;
129 int err;
130
Marc Zyngierf833f572015-10-13 12:51:33 +0100131 if (is_of_node(domain->parent->fwnode)) {
132 fwspec.fwnode = domain->parent->fwnode;
133 fwspec.param_count = 3;
134 fwspec.param[0] = 0;
135 fwspec.param[1] = hwirq - 32;
136 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
137 } else {
138 return -EINVAL;
139 }
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000140
Marc Zyngierf833f572015-10-13 12:51:33 +0100141 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000142 if (err)
143 return err;
144
145 /* Configure the interrupt line to be edge */
146 d = irq_domain_get_irq_data(domain->parent, virq);
147 d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
148 return 0;
149}
150
151static void gicv2m_unalloc_msi(struct v2m_data *v2m, unsigned int hwirq)
152{
153 int pos;
154
155 pos = hwirq - v2m->spi_start;
156 if (pos < 0 || pos >= v2m->nr_spis) {
157 pr_err("Failed to teardown msi. Invalid hwirq %d\n", hwirq);
158 return;
159 }
160
161 spin_lock(&v2m->msi_cnt_lock);
162 __clear_bit(pos, v2m->bm);
163 spin_unlock(&v2m->msi_cnt_lock);
164}
165
166static int gicv2m_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
167 unsigned int nr_irqs, void *args)
168{
169 struct v2m_data *v2m = domain->host_data;
170 int hwirq, offset, err = 0;
171
172 spin_lock(&v2m->msi_cnt_lock);
173 offset = find_first_zero_bit(v2m->bm, v2m->nr_spis);
174 if (offset < v2m->nr_spis)
175 __set_bit(offset, v2m->bm);
176 else
177 err = -ENOSPC;
178 spin_unlock(&v2m->msi_cnt_lock);
179
180 if (err)
181 return err;
182
183 hwirq = v2m->spi_start + offset;
184
185 err = gicv2m_irq_gic_domain_alloc(domain, virq, hwirq);
186 if (err) {
187 gicv2m_unalloc_msi(v2m, hwirq);
188 return err;
189 }
190
191 irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
192 &gicv2m_irq_chip, v2m);
193
194 return 0;
195}
196
197static void gicv2m_irq_domain_free(struct irq_domain *domain,
198 unsigned int virq, unsigned int nr_irqs)
199{
200 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
201 struct v2m_data *v2m = irq_data_get_irq_chip_data(d);
202
203 BUG_ON(nr_irqs != 1);
204 gicv2m_unalloc_msi(v2m, d->hwirq);
205 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
206}
207
208static const struct irq_domain_ops gicv2m_domain_ops = {
209 .alloc = gicv2m_irq_domain_alloc,
210 .free = gicv2m_irq_domain_free,
211};
212
213static bool is_msi_spi_valid(u32 base, u32 num)
214{
215 if (base < V2M_MIN_SPI) {
216 pr_err("Invalid MSI base SPI (base:%u)\n", base);
217 return false;
218 }
219
220 if ((num == 0) || (base + num > V2M_MAX_SPI)) {
221 pr_err("Number of SPIs (%u) exceed maximum (%u)\n",
222 num, V2M_MAX_SPI - V2M_MIN_SPI + 1);
223 return false;
224 }
225
226 return true;
227}
228
Marc Zyngieref506452015-07-28 14:46:24 +0100229static struct irq_chip gicv2m_pmsi_irq_chip = {
230 .name = "pMSI",
231};
232
233static struct msi_domain_ops gicv2m_pmsi_ops = {
234};
235
236static struct msi_domain_info gicv2m_pmsi_domain_info = {
237 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
238 .ops = &gicv2m_pmsi_ops,
239 .chip = &gicv2m_pmsi_irq_chip,
240};
241
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000242static int __init gicv2m_init_one(struct device_node *node,
243 struct irq_domain *parent)
244{
245 int ret;
246 struct v2m_data *v2m;
Marc Zyngieref506452015-07-28 14:46:24 +0100247 struct irq_domain *inner_domain, *pci_domain, *plat_domain;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000248
249 v2m = kzalloc(sizeof(struct v2m_data), GFP_KERNEL);
250 if (!v2m) {
251 pr_err("Failed to allocate struct v2m_data.\n");
252 return -ENOMEM;
253 }
254
255 ret = of_address_to_resource(node, 0, &v2m->res);
256 if (ret) {
257 pr_err("Failed to allocate v2m resource.\n");
258 goto err_free_v2m;
259 }
260
261 v2m->base = ioremap(v2m->res.start, resource_size(&v2m->res));
262 if (!v2m->base) {
263 pr_err("Failed to map GICv2m resource\n");
264 ret = -ENOMEM;
265 goto err_free_v2m;
266 }
267
268 if (!of_property_read_u32(node, "arm,msi-base-spi", &v2m->spi_start) &&
269 !of_property_read_u32(node, "arm,msi-num-spis", &v2m->nr_spis)) {
270 pr_info("Overriding V2M MSI_TYPER (base:%u, num:%u)\n",
271 v2m->spi_start, v2m->nr_spis);
272 } else {
273 u32 typer = readl_relaxed(v2m->base + V2M_MSI_TYPER);
274
275 v2m->spi_start = V2M_MSI_TYPER_BASE_SPI(typer);
276 v2m->nr_spis = V2M_MSI_TYPER_NUM_SPI(typer);
277 }
278
279 if (!is_msi_spi_valid(v2m->spi_start, v2m->nr_spis)) {
280 ret = -EINVAL;
281 goto err_iounmap;
282 }
283
Duc Dangee5f7d62015-10-06 15:32:38 -0700284 /*
285 * APM X-Gene GICv2m implementation has an erratum where
286 * the MSI data needs to be the offset from the spi_start
287 * in order to trigger the correct MSI interrupt. This is
288 * different from the standard GICv2m implementation where
289 * the MSI data is the absolute value within the range from
290 * spi_start to (spi_start + num_spis).
291 */
292 if (readl_relaxed(v2m->base + V2M_MSI_IIDR) == XGENE_GICV2M_MSI_IIDR)
293 v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
294
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000295 v2m->bm = kzalloc(sizeof(long) * BITS_TO_LONGS(v2m->nr_spis),
296 GFP_KERNEL);
297 if (!v2m->bm) {
298 ret = -ENOMEM;
299 goto err_iounmap;
300 }
301
Marc Zyngier5cedceb2015-07-28 14:46:23 +0100302 inner_domain = irq_domain_add_tree(node, &gicv2m_domain_ops, v2m);
303 if (!inner_domain) {
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000304 pr_err("Failed to create GICv2m domain\n");
305 ret = -ENOMEM;
306 goto err_free_bm;
307 }
308
Marc Zyngier5cedceb2015-07-28 14:46:23 +0100309 inner_domain->bus_token = DOMAIN_BUS_NEXUS;
310 inner_domain->parent = parent;
Marc Zyngieref506452015-07-28 14:46:24 +0100311 pci_domain = pci_msi_create_irq_domain(node, &gicv2m_msi_domain_info,
312 inner_domain);
313 plat_domain = platform_msi_create_irq_domain(node,
314 &gicv2m_pmsi_domain_info,
315 inner_domain);
316 if (!pci_domain || !plat_domain) {
317 pr_err("Failed to create MSI domains\n");
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000318 ret = -ENOMEM;
319 goto err_free_domains;
320 }
321
322 spin_lock_init(&v2m->msi_cnt_lock);
323
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000324 pr_info("Node %s: range[%#lx:%#lx], SPI[%d:%d]\n", node->name,
325 (unsigned long)v2m->res.start, (unsigned long)v2m->res.end,
326 v2m->spi_start, (v2m->spi_start + v2m->nr_spis));
327
328 return 0;
329
330err_free_domains:
Marc Zyngieref506452015-07-28 14:46:24 +0100331 if (plat_domain)
332 irq_domain_remove(plat_domain);
333 if (pci_domain)
334 irq_domain_remove(pci_domain);
Marc Zyngier5cedceb2015-07-28 14:46:23 +0100335 if (inner_domain)
336 irq_domain_remove(inner_domain);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000337err_free_bm:
338 kfree(v2m->bm);
339err_iounmap:
340 iounmap(v2m->base);
341err_free_v2m:
342 kfree(v2m);
343 return ret;
344}
345
346static struct of_device_id gicv2m_device_id[] = {
347 { .compatible = "arm,gic-v2m-frame", },
348 {},
349};
350
351int __init gicv2m_of_init(struct device_node *node, struct irq_domain *parent)
352{
353 int ret = 0;
354 struct device_node *child;
355
356 for (child = of_find_matching_node(node, gicv2m_device_id); child;
357 child = of_find_matching_node(child, gicv2m_device_id)) {
358 if (!of_find_property(child, "msi-controller", NULL))
359 continue;
360
361 ret = gicv2m_init_one(child, parent);
362 if (ret) {
363 of_node_put(node);
364 break;
365 }
366 }
367
368 return ret;
369}