blob: fbec07d634ad28855d870d97ec51e51e100e3ffe [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +00002/*
3 * ARM GIC v2m MSI(-X) support
4 * Support for Message Signaled Interrupts for systems that
5 * implement ARM Generic Interrupt Controller: GICv2m.
6 *
7 * Copyright (C) 2014 Advanced Micro Devices, Inc.
8 * Authors: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
9 * Harish Kasiviswanathan <harish.kasiviswanathan@amd.com>
10 * Brandon Anderson <brandon.anderson@amd.com>
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000011 */
12
13#define pr_fmt(fmt) "GICv2m: " fmt
14
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -080015#include <linux/acpi.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010016#include <linux/dma-iommu.h>
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000017#include <linux/irq.h>
18#include <linux/irqdomain.h>
19#include <linux/kernel.h>
Krzysztof Wilczynskibbd8810d2019-09-03 13:30:59 +020020#include <linux/pci.h>
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -080021#include <linux/msi.h>
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000022#include <linux/of_address.h>
23#include <linux/of_pci.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
Ben Dooks7c034f12016-06-08 18:53:34 +010026#include <linux/irqchip/arm-gic.h>
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000027
28/*
29* MSI_TYPER:
30* [31:26] Reserved
31* [25:16] lowest SPI assigned to MSI
32* [15:10] Reserved
33* [9:0] Numer of SPIs assigned to MSI
34*/
35#define V2M_MSI_TYPER 0x008
36#define V2M_MSI_TYPER_BASE_SHIFT 16
37#define V2M_MSI_TYPER_BASE_MASK 0x3FF
38#define V2M_MSI_TYPER_NUM_MASK 0x3FF
39#define V2M_MSI_SETSPI_NS 0x040
40#define V2M_MIN_SPI 32
41#define V2M_MAX_SPI 1019
Duc Dangee5f7d62015-10-06 15:32:38 -070042#define V2M_MSI_IIDR 0xFCC
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000043
44#define V2M_MSI_TYPER_BASE_SPI(x) \
45 (((x) >> V2M_MSI_TYPER_BASE_SHIFT) & V2M_MSI_TYPER_BASE_MASK)
46
47#define V2M_MSI_TYPER_NUM_SPI(x) ((x) & V2M_MSI_TYPER_NUM_MASK)
48
Duc Dangee5f7d62015-10-06 15:32:38 -070049/* APM X-Gene with GICv2m MSI_IIDR register value */
50#define XGENE_GICV2M_MSI_IIDR 0x06000170
51
Ray Jui74c967a2016-05-05 09:32:01 -070052/* Broadcom NS2 GICv2m MSI_IIDR register value */
53#define BCM_NS2_GICV2M_MSI_IIDR 0x0000013f
54
Duc Dangee5f7d62015-10-06 15:32:38 -070055/* List of flags for specific v2m implementation */
56#define GICV2M_NEEDS_SPI_OFFSET 0x00000001
Zeev Zilberman90b4c552019-06-10 13:52:01 +030057#define GICV2M_GRAVITON_ADDRESS_ONLY 0x00000002
Duc Dangee5f7d62015-10-06 15:32:38 -070058
Marc Zyngiera71225e2015-10-14 12:27:17 +010059static LIST_HEAD(v2m_nodes);
60static DEFINE_SPINLOCK(v2m_lock);
61
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000062struct v2m_data {
Marc Zyngiera71225e2015-10-14 12:27:17 +010063 struct list_head entry;
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -080064 struct fwnode_handle *fwnode;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000065 struct resource res; /* GICv2m resource */
66 void __iomem *base; /* GICv2m virt address */
67 u32 spi_start; /* The SPI number that MSIs start */
68 u32 nr_spis; /* The number of SPIs for MSIs */
Ray Jui74c967a2016-05-05 09:32:01 -070069 u32 spi_offset; /* offset to be subtracted from SPI number */
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000070 unsigned long *bm; /* MSI vector bitmap */
Duc Dangee5f7d62015-10-06 15:32:38 -070071 u32 flags; /* v2m flags for specific implementation */
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000072};
73
74static void gicv2m_mask_msi_irq(struct irq_data *d)
75{
76 pci_msi_mask_irq(d);
77 irq_chip_mask_parent(d);
78}
79
80static void gicv2m_unmask_msi_irq(struct irq_data *d)
81{
82 pci_msi_unmask_irq(d);
83 irq_chip_unmask_parent(d);
84}
85
86static struct irq_chip gicv2m_msi_irq_chip = {
87 .name = "MSI",
88 .irq_mask = gicv2m_mask_msi_irq,
89 .irq_unmask = gicv2m_unmask_msi_irq,
90 .irq_eoi = irq_chip_eoi_parent,
91 .irq_write_msi_msg = pci_msi_domain_write_msg,
92};
93
94static struct msi_domain_info gicv2m_msi_domain_info = {
95 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
Marc Zyngierde337ee2018-02-06 18:55:33 +000096 MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +000097 .chip = &gicv2m_msi_irq_chip,
98};
99
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300100static phys_addr_t gicv2m_get_msi_addr(struct v2m_data *v2m, int hwirq)
101{
102 if (v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY)
103 return v2m->res.start | ((hwirq - 32) << 3);
104 else
105 return v2m->res.start + V2M_MSI_SETSPI_NS;
106}
107
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000108static void gicv2m_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
109{
110 struct v2m_data *v2m = irq_data_get_irq_chip_data(data);
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300111 phys_addr_t addr = gicv2m_get_msi_addr(v2m, data->hwirq);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000112
Pavel Fedin157add62015-09-13 12:14:33 +0100113 msg->address_hi = upper_32_bits(addr);
114 msg->address_lo = lower_32_bits(addr);
Duc Dangee5f7d62015-10-06 15:32:38 -0700115
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300116 if (v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY)
117 msg->data = 0;
118 else
119 msg->data = data->hwirq;
Duc Dangee5f7d62015-10-06 15:32:38 -0700120 if (v2m->flags & GICV2M_NEEDS_SPI_OFFSET)
Ray Jui74c967a2016-05-05 09:32:01 -0700121 msg->data -= v2m->spi_offset;
Robin Murphy44bb7e22016-09-12 17:13:59 +0100122
Julien Grall737be742019-05-01 14:58:20 +0100123 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(data), msg);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000124}
125
126static struct irq_chip gicv2m_irq_chip = {
127 .name = "GICv2m",
128 .irq_mask = irq_chip_mask_parent,
129 .irq_unmask = irq_chip_unmask_parent,
130 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier0407dac2016-02-19 15:00:29 +0000131 .irq_set_affinity = irq_chip_set_affinity_parent,
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000132 .irq_compose_msi_msg = gicv2m_compose_msi_msg,
133};
134
135static int gicv2m_irq_gic_domain_alloc(struct irq_domain *domain,
136 unsigned int virq,
137 irq_hw_number_t hwirq)
138{
Marc Zyngierf833f572015-10-13 12:51:33 +0100139 struct irq_fwspec fwspec;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000140 struct irq_data *d;
141 int err;
142
Marc Zyngierf833f572015-10-13 12:51:33 +0100143 if (is_of_node(domain->parent->fwnode)) {
144 fwspec.fwnode = domain->parent->fwnode;
145 fwspec.param_count = 3;
146 fwspec.param[0] = 0;
147 fwspec.param[1] = hwirq - 32;
148 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800149 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
150 fwspec.fwnode = domain->parent->fwnode;
151 fwspec.param_count = 2;
152 fwspec.param[0] = hwirq;
153 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +0100154 } else {
155 return -EINVAL;
156 }
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000157
Marc Zyngierf833f572015-10-13 12:51:33 +0100158 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000159 if (err)
160 return err;
161
162 /* Configure the interrupt line to be edge */
163 d = irq_domain_get_irq_data(domain->parent, virq);
164 d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
165 return 0;
166}
167
Marc Zyngierde337ee2018-02-06 18:55:33 +0000168static void gicv2m_unalloc_msi(struct v2m_data *v2m, unsigned int hwirq,
169 int nr_irqs)
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000170{
Marc Zyngiera71225e2015-10-14 12:27:17 +0100171 spin_lock(&v2m_lock);
Marc Zyngierde337ee2018-02-06 18:55:33 +0000172 bitmap_release_region(v2m->bm, hwirq - v2m->spi_start,
173 get_count_order(nr_irqs));
Marc Zyngiera71225e2015-10-14 12:27:17 +0100174 spin_unlock(&v2m_lock);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000175}
176
177static int gicv2m_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
178 unsigned int nr_irqs, void *args)
179{
Julien Grall737be742019-05-01 14:58:20 +0100180 msi_alloc_info_t *info = args;
Marc Zyngiera71225e2015-10-14 12:27:17 +0100181 struct v2m_data *v2m = NULL, *tmp;
Marc Zyngierde337ee2018-02-06 18:55:33 +0000182 int hwirq, offset, i, err = 0;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000183
Marc Zyngiera71225e2015-10-14 12:27:17 +0100184 spin_lock(&v2m_lock);
185 list_for_each_entry(tmp, &v2m_nodes, entry) {
Marc Zyngierde337ee2018-02-06 18:55:33 +0000186 offset = bitmap_find_free_region(tmp->bm, tmp->nr_spis,
187 get_count_order(nr_irqs));
188 if (offset >= 0) {
Marc Zyngiera71225e2015-10-14 12:27:17 +0100189 v2m = tmp;
190 break;
191 }
192 }
193 spin_unlock(&v2m_lock);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000194
Marc Zyngiera71225e2015-10-14 12:27:17 +0100195 if (!v2m)
196 return -ENOSPC;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000197
198 hwirq = v2m->spi_start + offset;
199
Julien Grall737be742019-05-01 14:58:20 +0100200 err = iommu_dma_prepare_msi(info->desc,
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300201 gicv2m_get_msi_addr(v2m, hwirq));
Julien Grall737be742019-05-01 14:58:20 +0100202 if (err)
203 return err;
204
Marc Zyngierde337ee2018-02-06 18:55:33 +0000205 for (i = 0; i < nr_irqs; i++) {
206 err = gicv2m_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
207 if (err)
208 goto fail;
209
210 irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
211 &gicv2m_irq_chip, v2m);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000212 }
213
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000214 return 0;
Marc Zyngierde337ee2018-02-06 18:55:33 +0000215
216fail:
217 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
Marc Zyngiercbaf45a2018-06-22 10:52:50 +0100218 gicv2m_unalloc_msi(v2m, hwirq, nr_irqs);
Marc Zyngierde337ee2018-02-06 18:55:33 +0000219 return err;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000220}
221
222static void gicv2m_irq_domain_free(struct irq_domain *domain,
223 unsigned int virq, unsigned int nr_irqs)
224{
225 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
226 struct v2m_data *v2m = irq_data_get_irq_chip_data(d);
227
Marc Zyngierde337ee2018-02-06 18:55:33 +0000228 gicv2m_unalloc_msi(v2m, d->hwirq, nr_irqs);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000229 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
230}
231
232static const struct irq_domain_ops gicv2m_domain_ops = {
233 .alloc = gicv2m_irq_domain_alloc,
234 .free = gicv2m_irq_domain_free,
235};
236
237static bool is_msi_spi_valid(u32 base, u32 num)
238{
239 if (base < V2M_MIN_SPI) {
240 pr_err("Invalid MSI base SPI (base:%u)\n", base);
241 return false;
242 }
243
244 if ((num == 0) || (base + num > V2M_MAX_SPI)) {
245 pr_err("Number of SPIs (%u) exceed maximum (%u)\n",
246 num, V2M_MAX_SPI - V2M_MIN_SPI + 1);
247 return false;
248 }
249
250 return true;
251}
252
Marc Zyngieref506452015-07-28 14:46:24 +0100253static struct irq_chip gicv2m_pmsi_irq_chip = {
254 .name = "pMSI",
255};
256
257static struct msi_domain_ops gicv2m_pmsi_ops = {
258};
259
260static struct msi_domain_info gicv2m_pmsi_domain_info = {
261 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
262 .ops = &gicv2m_pmsi_ops,
263 .chip = &gicv2m_pmsi_irq_chip,
264};
265
Marc Zyngiera71225e2015-10-14 12:27:17 +0100266static void gicv2m_teardown(void)
267{
268 struct v2m_data *v2m, *tmp;
269
270 list_for_each_entry_safe(v2m, tmp, &v2m_nodes, entry) {
271 list_del(&v2m->entry);
272 kfree(v2m->bm);
273 iounmap(v2m->base);
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800274 of_node_put(to_of_node(v2m->fwnode));
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800275 if (is_fwnode_irqchip(v2m->fwnode))
276 irq_domain_free_fwnode(v2m->fwnode);
Marc Zyngiera71225e2015-10-14 12:27:17 +0100277 kfree(v2m);
278 }
279}
280
281static int gicv2m_allocate_domains(struct irq_domain *parent)
282{
283 struct irq_domain *inner_domain, *pci_domain, *plat_domain;
284 struct v2m_data *v2m;
285
286 v2m = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
287 if (!v2m)
288 return 0;
289
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800290 inner_domain = irq_domain_create_tree(v2m->fwnode,
Marc Zyngiera71225e2015-10-14 12:27:17 +0100291 &gicv2m_domain_ops, v2m);
292 if (!inner_domain) {
293 pr_err("Failed to create GICv2m domain\n");
294 return -ENOMEM;
295 }
296
Marc Zyngier96f0d932017-06-22 11:42:50 +0100297 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Marc Zyngiera71225e2015-10-14 12:27:17 +0100298 inner_domain->parent = parent;
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800299 pci_domain = pci_msi_create_irq_domain(v2m->fwnode,
Marc Zyngiera71225e2015-10-14 12:27:17 +0100300 &gicv2m_msi_domain_info,
301 inner_domain);
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800302 plat_domain = platform_msi_create_irq_domain(v2m->fwnode,
Marc Zyngiera71225e2015-10-14 12:27:17 +0100303 &gicv2m_pmsi_domain_info,
304 inner_domain);
305 if (!pci_domain || !plat_domain) {
306 pr_err("Failed to create MSI domains\n");
307 if (plat_domain)
308 irq_domain_remove(plat_domain);
309 if (pci_domain)
310 irq_domain_remove(pci_domain);
311 irq_domain_remove(inner_domain);
312 return -ENOMEM;
313 }
314
315 return 0;
316}
317
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800318static int __init gicv2m_init_one(struct fwnode_handle *fwnode,
319 u32 spi_start, u32 nr_spis,
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300320 struct resource *res, u32 flags)
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000321{
322 int ret;
323 struct v2m_data *v2m;
324
325 v2m = kzalloc(sizeof(struct v2m_data), GFP_KERNEL);
326 if (!v2m) {
327 pr_err("Failed to allocate struct v2m_data.\n");
328 return -ENOMEM;
329 }
330
Marc Zyngiera71225e2015-10-14 12:27:17 +0100331 INIT_LIST_HEAD(&v2m->entry);
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800332 v2m->fwnode = fwnode;
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300333 v2m->flags = flags;
Marc Zyngiera71225e2015-10-14 12:27:17 +0100334
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800335 memcpy(&v2m->res, res, sizeof(struct resource));
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000336
337 v2m->base = ioremap(v2m->res.start, resource_size(&v2m->res));
338 if (!v2m->base) {
339 pr_err("Failed to map GICv2m resource\n");
340 ret = -ENOMEM;
341 goto err_free_v2m;
342 }
343
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800344 if (spi_start && nr_spis) {
345 v2m->spi_start = spi_start;
346 v2m->nr_spis = nr_spis;
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000347 } else {
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300348 u32 typer;
349
350 /* Graviton should always have explicit spi_start/nr_spis */
351 if (v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY) {
352 ret = -EINVAL;
353 goto err_iounmap;
354 }
355 typer = readl_relaxed(v2m->base + V2M_MSI_TYPER);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000356
357 v2m->spi_start = V2M_MSI_TYPER_BASE_SPI(typer);
358 v2m->nr_spis = V2M_MSI_TYPER_NUM_SPI(typer);
359 }
360
361 if (!is_msi_spi_valid(v2m->spi_start, v2m->nr_spis)) {
362 ret = -EINVAL;
363 goto err_iounmap;
364 }
365
Duc Dangee5f7d62015-10-06 15:32:38 -0700366 /*
367 * APM X-Gene GICv2m implementation has an erratum where
368 * the MSI data needs to be the offset from the spi_start
369 * in order to trigger the correct MSI interrupt. This is
370 * different from the standard GICv2m implementation where
371 * the MSI data is the absolute value within the range from
372 * spi_start to (spi_start + num_spis).
Ray Jui74c967a2016-05-05 09:32:01 -0700373 *
374 * Broadom NS2 GICv2m implementation has an erratum where the MSI data
375 * is 'spi_number - 32'
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300376 *
377 * Reading that register fails on the Graviton implementation
Duc Dangee5f7d62015-10-06 15:32:38 -0700378 */
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300379 if (!(v2m->flags & GICV2M_GRAVITON_ADDRESS_ONLY)) {
380 switch (readl_relaxed(v2m->base + V2M_MSI_IIDR)) {
381 case XGENE_GICV2M_MSI_IIDR:
382 v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
383 v2m->spi_offset = v2m->spi_start;
384 break;
385 case BCM_NS2_GICV2M_MSI_IIDR:
386 v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
387 v2m->spi_offset = 32;
388 break;
389 }
Ray Jui74c967a2016-05-05 09:32:01 -0700390 }
Kees Cook6396bb22018-06-12 14:03:40 -0700391 v2m->bm = kcalloc(BITS_TO_LONGS(v2m->nr_spis), sizeof(long),
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000392 GFP_KERNEL);
393 if (!v2m->bm) {
394 ret = -ENOMEM;
395 goto err_iounmap;
396 }
397
Marc Zyngiera71225e2015-10-14 12:27:17 +0100398 list_add_tail(&v2m->entry, &v2m_nodes);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000399
Suravee Suthikulpanit5a1ff4802015-12-22 16:24:23 -0800400 pr_info("range%pR, SPI[%d:%d]\n", res,
401 v2m->spi_start, (v2m->spi_start + v2m->nr_spis - 1));
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000402 return 0;
403
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000404err_iounmap:
405 iounmap(v2m->base);
406err_free_v2m:
407 kfree(v2m);
408 return ret;
409}
410
411static struct of_device_id gicv2m_device_id[] = {
412 { .compatible = "arm,gic-v2m-frame", },
413 {},
414};
415
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800416static int __init gicv2m_of_init(struct fwnode_handle *parent_handle,
417 struct irq_domain *parent)
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000418{
419 int ret = 0;
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800420 struct device_node *node = to_of_node(parent_handle);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000421 struct device_node *child;
422
423 for (child = of_find_matching_node(node, gicv2m_device_id); child;
424 child = of_find_matching_node(child, gicv2m_device_id)) {
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800425 u32 spi_start = 0, nr_spis = 0;
426 struct resource res;
427
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000428 if (!of_find_property(child, "msi-controller", NULL))
429 continue;
430
Suravee Suthikulpanit4266ab12015-12-10 08:55:29 -0800431 ret = of_address_to_resource(child, 0, &res);
432 if (ret) {
433 pr_err("Failed to allocate v2m resource.\n");
434 break;
435 }
436
437 if (!of_property_read_u32(child, "arm,msi-base-spi",
438 &spi_start) &&
439 !of_property_read_u32(child, "arm,msi-num-spis", &nr_spis))
440 pr_info("DT overriding V2M MSI_TYPER (base:%u, num:%u)\n",
441 spi_start, nr_spis);
442
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300443 ret = gicv2m_init_one(&child->fwnode, spi_start, nr_spis,
444 &res, 0);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000445 if (ret) {
Marc Zyngier86d14c72015-12-16 11:03:24 +0000446 of_node_put(child);
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000447 break;
448 }
449 }
450
Marc Zyngiera71225e2015-10-14 12:27:17 +0100451 if (!ret)
452 ret = gicv2m_allocate_domains(parent);
453 if (ret)
454 gicv2m_teardown();
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +0000455 return ret;
456}
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800457
458#ifdef CONFIG_ACPI
459static int acpi_num_msi;
460
461static struct fwnode_handle *gicv2m_get_fwnode(struct device *dev)
462{
463 struct v2m_data *data;
464
465 if (WARN_ON(acpi_num_msi <= 0))
466 return NULL;
467
468 /* We only return the fwnode of the first MSI frame. */
469 data = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
470 if (!data)
471 return NULL;
472
473 return data->fwnode;
474}
475
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300476static bool acpi_check_amazon_graviton_quirks(void)
477{
478 static struct acpi_table_madt *madt;
479 acpi_status status;
480 bool rc = false;
481
482#define ACPI_AMZN_OEM_ID "AMAZON"
483
484 status = acpi_get_table(ACPI_SIG_MADT, 0,
485 (struct acpi_table_header **)&madt);
486
487 if (ACPI_FAILURE(status) || !madt)
488 return rc;
489 rc = !memcmp(madt->header.oem_id, ACPI_AMZN_OEM_ID, ACPI_OEM_ID_SIZE);
490 acpi_put_table((struct acpi_table_header *)madt);
491
492 return rc;
493}
494
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800495static int __init
Keith Busch60574d12019-03-11 14:55:57 -0600496acpi_parse_madt_msi(union acpi_subtable_headers *header,
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800497 const unsigned long end)
498{
499 int ret;
500 struct resource res;
501 u32 spi_start = 0, nr_spis = 0;
502 struct acpi_madt_generic_msi_frame *m;
503 struct fwnode_handle *fwnode;
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300504 u32 flags = 0;
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800505
506 m = (struct acpi_madt_generic_msi_frame *)header;
507 if (BAD_MADT_ENTRY(m, end))
508 return -EINVAL;
509
510 res.start = m->base_address;
Suravee Suthikulpanit5a1ff4802015-12-22 16:24:23 -0800511 res.end = m->base_address + SZ_4K - 1;
512 res.flags = IORESOURCE_MEM;
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800513
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300514 if (acpi_check_amazon_graviton_quirks()) {
515 pr_info("applying Amazon Graviton quirk\n");
516 res.end = res.start + SZ_8K - 1;
517 flags |= GICV2M_GRAVITON_ADDRESS_ONLY;
518 gicv2m_msi_domain_info.flags &= ~MSI_FLAG_MULTI_PCI_MSI;
519 }
520
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800521 if (m->flags & ACPI_MADT_OVERRIDE_SPI_VALUES) {
522 spi_start = m->spi_base;
523 nr_spis = m->spi_count;
524
525 pr_info("ACPI overriding V2M MSI_TYPER (base:%u, num:%u)\n",
526 spi_start, nr_spis);
527 }
528
Marc Zyngier7d5b7692019-07-31 16:13:42 +0100529 fwnode = irq_domain_alloc_fwnode(&res.start);
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800530 if (!fwnode) {
531 pr_err("Unable to allocate GICv2m domain token\n");
532 return -EINVAL;
533 }
534
Zeev Zilberman90b4c552019-06-10 13:52:01 +0300535 ret = gicv2m_init_one(fwnode, spi_start, nr_spis, &res, flags);
Suravee Suthikulpanit0644b3d2015-12-10 08:55:30 -0800536 if (ret)
537 irq_domain_free_fwnode(fwnode);
538
539 return ret;
540}
541
542static int __init gicv2m_acpi_init(struct irq_domain *parent)
543{
544 int ret;
545
546 if (acpi_num_msi > 0)
547 return 0;
548
549 acpi_num_msi = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_MSI_FRAME,
550 acpi_parse_madt_msi, 0);
551
552 if (acpi_num_msi <= 0)
553 goto err_out;
554
555 ret = gicv2m_allocate_domains(parent);
556 if (ret)
557 goto err_out;
558
559 pci_msi_register_fwnode_provider(&gicv2m_get_fwnode);
560
561 return 0;
562
563err_out:
564 gicv2m_teardown();
565 return -EINVAL;
566}
567#else /* CONFIG_ACPI */
568static int __init gicv2m_acpi_init(struct irq_domain *parent)
569{
570 return -EINVAL;
571}
572#endif /* CONFIG_ACPI */
573
574int __init gicv2m_init(struct fwnode_handle *parent_handle,
575 struct irq_domain *parent)
576{
577 if (is_of_node(parent_handle))
578 return gicv2m_of_init(parent_handle, parent);
579
580 return gicv2m_acpi_init(parent);
581}