Blame view

drivers/irqchip/irq-gic-v2m.c 12.9 KB
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   * ARM GIC v2m MSI(-X) support
   * Support for Message Signaled Interrupts for systems that
   * implement ARM Generic Interrupt Controller: GICv2m.
   *
   * Copyright (C) 2014 Advanced Micro Devices, Inc.
   * Authors: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
   *	    Harish Kasiviswanathan <harish.kasiviswanathan@amd.com>
   *	    Brandon Anderson <brandon.anderson@amd.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published
   * by the Free Software Foundation.
   */
  
  #define pr_fmt(fmt) "GICv2m: " fmt
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
17
  #include <linux/acpi.h>
44bb7e243   Robin Murphy   iommu/dma: Add su...
18
  #include <linux/dma-iommu.h>
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
19
20
21
  #include <linux/irq.h>
  #include <linux/irqdomain.h>
  #include <linux/kernel.h>
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
22
  #include <linux/msi.h>
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
23
24
25
26
  #include <linux/of_address.h>
  #include <linux/of_pci.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
7c034f169   Ben Dooks   irqchip/gic-v2m: ...
27
  #include <linux/irqchip/arm-gic.h>
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  
  /*
  * MSI_TYPER:
  *     [31:26] Reserved
  *     [25:16] lowest SPI assigned to MSI
  *     [15:10] Reserved
  *     [9:0]   Numer of SPIs assigned to MSI
  */
  #define V2M_MSI_TYPER		       0x008
  #define V2M_MSI_TYPER_BASE_SHIFT       16
  #define V2M_MSI_TYPER_BASE_MASK	       0x3FF
  #define V2M_MSI_TYPER_NUM_MASK	       0x3FF
  #define V2M_MSI_SETSPI_NS	       0x040
  #define V2M_MIN_SPI		       32
  #define V2M_MAX_SPI		       1019
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
43
  #define V2M_MSI_IIDR		       0xFCC
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
44
45
46
47
48
  
  #define V2M_MSI_TYPER_BASE_SPI(x)      \
  	       (((x) >> V2M_MSI_TYPER_BASE_SHIFT) & V2M_MSI_TYPER_BASE_MASK)
  
  #define V2M_MSI_TYPER_NUM_SPI(x)       ((x) & V2M_MSI_TYPER_NUM_MASK)
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
49
50
  /* APM X-Gene with GICv2m MSI_IIDR register value */
  #define XGENE_GICV2M_MSI_IIDR		0x06000170
74c967aaf   Ray Jui   irqchip/gic-v2m: ...
51
52
  /* Broadcom NS2 GICv2m MSI_IIDR register value */
  #define BCM_NS2_GICV2M_MSI_IIDR		0x0000013f
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
53
54
  /* List of flags for specific v2m implementation */
  #define GICV2M_NEEDS_SPI_OFFSET		0x00000001
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
55
56
  static LIST_HEAD(v2m_nodes);
  static DEFINE_SPINLOCK(v2m_lock);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
57
  struct v2m_data {
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
58
  	struct list_head entry;
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
59
  	struct fwnode_handle *fwnode;
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
60
61
62
63
  	struct resource res;	/* GICv2m resource */
  	void __iomem *base;	/* GICv2m virt address */
  	u32 spi_start;		/* The SPI number that MSIs start */
  	u32 nr_spis;		/* The number of SPIs for MSIs */
74c967aaf   Ray Jui   irqchip/gic-v2m: ...
64
  	u32 spi_offset;		/* offset to be subtracted from SPI number */
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
65
  	unsigned long *bm;	/* MSI vector bitmap */
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
66
  	u32 flags;		/* v2m flags for specific implementation */
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  };
  
  static void gicv2m_mask_msi_irq(struct irq_data *d)
  {
  	pci_msi_mask_irq(d);
  	irq_chip_mask_parent(d);
  }
  
  static void gicv2m_unmask_msi_irq(struct irq_data *d)
  {
  	pci_msi_unmask_irq(d);
  	irq_chip_unmask_parent(d);
  }
  
  static struct irq_chip gicv2m_msi_irq_chip = {
  	.name			= "MSI",
  	.irq_mask		= gicv2m_mask_msi_irq,
  	.irq_unmask		= gicv2m_unmask_msi_irq,
  	.irq_eoi		= irq_chip_eoi_parent,
  	.irq_write_msi_msg	= pci_msi_domain_write_msg,
  };
  
  static struct msi_domain_info gicv2m_msi_domain_info = {
  	.flags	= (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  		   MSI_FLAG_PCI_MSIX),
  	.chip	= &gicv2m_msi_irq_chip,
  };
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
94
95
96
97
  static void gicv2m_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  {
  	struct v2m_data *v2m = irq_data_get_irq_chip_data(data);
  	phys_addr_t addr = v2m->res.start + V2M_MSI_SETSPI_NS;
157add60c   Pavel Fedin   irqchip/GICv2m: F...
98
99
  	msg->address_hi = upper_32_bits(addr);
  	msg->address_lo = lower_32_bits(addr);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
100
  	msg->data = data->hwirq;
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
101
102
  
  	if (v2m->flags & GICV2M_NEEDS_SPI_OFFSET)
74c967aaf   Ray Jui   irqchip/gic-v2m: ...
103
  		msg->data -= v2m->spi_offset;
44bb7e243   Robin Murphy   iommu/dma: Add su...
104
105
  
  	iommu_dma_map_msi_msg(data->irq, msg);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
106
107
108
109
110
111
112
  }
  
  static struct irq_chip gicv2m_irq_chip = {
  	.name			= "GICv2m",
  	.irq_mask		= irq_chip_mask_parent,
  	.irq_unmask		= irq_chip_unmask_parent,
  	.irq_eoi		= irq_chip_eoi_parent,
0407dacee   Marc Zyngier   irqchip/gic: Retu...
113
  	.irq_set_affinity	= irq_chip_set_affinity_parent,
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
114
115
116
117
118
119
120
  	.irq_compose_msi_msg	= gicv2m_compose_msi_msg,
  };
  
  static int gicv2m_irq_gic_domain_alloc(struct irq_domain *domain,
  				       unsigned int virq,
  				       irq_hw_number_t hwirq)
  {
f833f57ff   Marc Zyngier   irqchip: Convert ...
121
  	struct irq_fwspec fwspec;
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
122
123
  	struct irq_data *d;
  	int err;
f833f57ff   Marc Zyngier   irqchip: Convert ...
124
125
126
127
128
129
  	if (is_of_node(domain->parent->fwnode)) {
  		fwspec.fwnode = domain->parent->fwnode;
  		fwspec.param_count = 3;
  		fwspec.param[0] = 0;
  		fwspec.param[1] = hwirq - 32;
  		fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
130
131
132
133
134
  	} else if (is_fwnode_irqchip(domain->parent->fwnode)) {
  		fwspec.fwnode = domain->parent->fwnode;
  		fwspec.param_count = 2;
  		fwspec.param[0] = hwirq;
  		fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
f833f57ff   Marc Zyngier   irqchip: Convert ...
135
136
137
  	} else {
  		return -EINVAL;
  	}
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
138

f833f57ff   Marc Zyngier   irqchip: Convert ...
139
  	err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  	if (err)
  		return err;
  
  	/* Configure the interrupt line to be edge */
  	d = irq_domain_get_irq_data(domain->parent, virq);
  	d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  	return 0;
  }
  
  static void gicv2m_unalloc_msi(struct v2m_data *v2m, unsigned int hwirq)
  {
  	int pos;
  
  	pos = hwirq - v2m->spi_start;
  	if (pos < 0 || pos >= v2m->nr_spis) {
  		pr_err("Failed to teardown msi. Invalid hwirq %d
  ", hwirq);
  		return;
  	}
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
159
  	spin_lock(&v2m_lock);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
160
  	__clear_bit(pos, v2m->bm);
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
161
  	spin_unlock(&v2m_lock);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
162
163
164
165
166
  }
  
  static int gicv2m_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  				   unsigned int nr_irqs, void *args)
  {
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
167
  	struct v2m_data *v2m = NULL, *tmp;
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
168
  	int hwirq, offset, err = 0;
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
169
170
171
172
173
174
175
176
177
178
  	spin_lock(&v2m_lock);
  	list_for_each_entry(tmp, &v2m_nodes, entry) {
  		offset = find_first_zero_bit(tmp->bm, tmp->nr_spis);
  		if (offset < tmp->nr_spis) {
  			__set_bit(offset, tmp->bm);
  			v2m = tmp;
  			break;
  		}
  	}
  	spin_unlock(&v2m_lock);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
179

a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
180
181
  	if (!v2m)
  		return -ENOSPC;
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  
  	hwirq = v2m->spi_start + offset;
  
  	err = gicv2m_irq_gic_domain_alloc(domain, virq, hwirq);
  	if (err) {
  		gicv2m_unalloc_msi(v2m, hwirq);
  		return err;
  	}
  
  	irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  				      &gicv2m_irq_chip, v2m);
  
  	return 0;
  }
  
  static void gicv2m_irq_domain_free(struct irq_domain *domain,
  				   unsigned int virq, unsigned int nr_irqs)
  {
  	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  	struct v2m_data *v2m = irq_data_get_irq_chip_data(d);
  
  	BUG_ON(nr_irqs != 1);
  	gicv2m_unalloc_msi(v2m, d->hwirq);
  	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  }
  
  static const struct irq_domain_ops gicv2m_domain_ops = {
  	.alloc			= gicv2m_irq_domain_alloc,
  	.free			= gicv2m_irq_domain_free,
  };
  
  static bool is_msi_spi_valid(u32 base, u32 num)
  {
  	if (base < V2M_MIN_SPI) {
  		pr_err("Invalid MSI base SPI (base:%u)
  ", base);
  		return false;
  	}
  
  	if ((num == 0) || (base + num > V2M_MAX_SPI)) {
  		pr_err("Number of SPIs (%u) exceed maximum (%u)
  ",
  		       num, V2M_MAX_SPI - V2M_MIN_SPI + 1);
  		return false;
  	}
  
  	return true;
  }
ef50645aa   Marc Zyngier   irqchip/GICv2m: A...
230
231
232
233
234
235
236
237
238
239
240
241
  static struct irq_chip gicv2m_pmsi_irq_chip = {
  	.name			= "pMSI",
  };
  
  static struct msi_domain_ops gicv2m_pmsi_ops = {
  };
  
  static struct msi_domain_info gicv2m_pmsi_domain_info = {
  	.flags	= (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
  	.ops	= &gicv2m_pmsi_ops,
  	.chip	= &gicv2m_pmsi_irq_chip,
  };
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
242
243
244
245
246
247
248
249
  static void gicv2m_teardown(void)
  {
  	struct v2m_data *v2m, *tmp;
  
  	list_for_each_entry_safe(v2m, tmp, &v2m_nodes, entry) {
  		list_del(&v2m->entry);
  		kfree(v2m->bm);
  		iounmap(v2m->base);
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
250
  		of_node_put(to_of_node(v2m->fwnode));
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
251
252
  		if (is_fwnode_irqchip(v2m->fwnode))
  			irq_domain_free_fwnode(v2m->fwnode);
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
253
254
255
256
257
258
259
260
261
262
263
264
  		kfree(v2m);
  	}
  }
  
  static int gicv2m_allocate_domains(struct irq_domain *parent)
  {
  	struct irq_domain *inner_domain, *pci_domain, *plat_domain;
  	struct v2m_data *v2m;
  
  	v2m = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
  	if (!v2m)
  		return 0;
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
265
  	inner_domain = irq_domain_create_tree(v2m->fwnode,
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
266
267
268
269
270
271
272
273
274
  					      &gicv2m_domain_ops, v2m);
  	if (!inner_domain) {
  		pr_err("Failed to create GICv2m domain
  ");
  		return -ENOMEM;
  	}
  
  	inner_domain->bus_token = DOMAIN_BUS_NEXUS;
  	inner_domain->parent = parent;
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
275
  	pci_domain = pci_msi_create_irq_domain(v2m->fwnode,
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
276
277
  					       &gicv2m_msi_domain_info,
  					       inner_domain);
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
278
  	plat_domain = platform_msi_create_irq_domain(v2m->fwnode,
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  						     &gicv2m_pmsi_domain_info,
  						     inner_domain);
  	if (!pci_domain || !plat_domain) {
  		pr_err("Failed to create MSI domains
  ");
  		if (plat_domain)
  			irq_domain_remove(plat_domain);
  		if (pci_domain)
  			irq_domain_remove(pci_domain);
  		irq_domain_remove(inner_domain);
  		return -ENOMEM;
  	}
  
  	return 0;
  }
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
294
295
296
  static int __init gicv2m_init_one(struct fwnode_handle *fwnode,
  				  u32 spi_start, u32 nr_spis,
  				  struct resource *res)
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
297
298
299
300
301
302
303
304
305
306
  {
  	int ret;
  	struct v2m_data *v2m;
  
  	v2m = kzalloc(sizeof(struct v2m_data), GFP_KERNEL);
  	if (!v2m) {
  		pr_err("Failed to allocate struct v2m_data.
  ");
  		return -ENOMEM;
  	}
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
307
  	INIT_LIST_HEAD(&v2m->entry);
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
308
  	v2m->fwnode = fwnode;
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
309

4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
310
  	memcpy(&v2m->res, res, sizeof(struct resource));
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
311
312
313
314
315
316
317
318
  
  	v2m->base = ioremap(v2m->res.start, resource_size(&v2m->res));
  	if (!v2m->base) {
  		pr_err("Failed to map GICv2m resource
  ");
  		ret = -ENOMEM;
  		goto err_free_v2m;
  	}
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
319
320
321
  	if (spi_start && nr_spis) {
  		v2m->spi_start = spi_start;
  		v2m->nr_spis = nr_spis;
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
322
323
324
325
326
327
328
329
330
331
332
  	} else {
  		u32 typer = readl_relaxed(v2m->base + V2M_MSI_TYPER);
  
  		v2m->spi_start = V2M_MSI_TYPER_BASE_SPI(typer);
  		v2m->nr_spis = V2M_MSI_TYPER_NUM_SPI(typer);
  	}
  
  	if (!is_msi_spi_valid(v2m->spi_start, v2m->nr_spis)) {
  		ret = -EINVAL;
  		goto err_iounmap;
  	}
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
333
334
335
336
337
338
339
  	/*
  	 * APM X-Gene GICv2m implementation has an erratum where
  	 * the MSI data needs to be the offset from the spi_start
  	 * in order to trigger the correct MSI interrupt. This is
  	 * different from the standard GICv2m implementation where
  	 * the MSI data is the absolute value within the range from
  	 * spi_start to (spi_start + num_spis).
74c967aaf   Ray Jui   irqchip/gic-v2m: ...
340
341
342
  	 *
  	 * Broadom NS2 GICv2m implementation has an erratum where the MSI data
  	 * is 'spi_number - 32'
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
343
  	 */
74c967aaf   Ray Jui   irqchip/gic-v2m: ...
344
345
346
347
348
349
  	switch (readl_relaxed(v2m->base + V2M_MSI_IIDR)) {
  	case XGENE_GICV2M_MSI_IIDR:
  		v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
  		v2m->spi_offset = v2m->spi_start;
  		break;
  	case BCM_NS2_GICV2M_MSI_IIDR:
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
350
  		v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
74c967aaf   Ray Jui   irqchip/gic-v2m: ...
351
352
353
  		v2m->spi_offset = 32;
  		break;
  	}
ee5f7d646   Duc Dang   irqchip/gic-v2m: ...
354

853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
355
356
357
358
359
360
  	v2m->bm = kzalloc(sizeof(long) * BITS_TO_LONGS(v2m->nr_spis),
  			  GFP_KERNEL);
  	if (!v2m->bm) {
  		ret = -ENOMEM;
  		goto err_iounmap;
  	}
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
361
  	list_add_tail(&v2m->entry, &v2m_nodes);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
362

5a1ff480f   Suravee Suthikulpanit   irqchip/gicv2m: M...
363
364
365
  	pr_info("range%pR, SPI[%d:%d]
  ", res,
  		v2m->spi_start, (v2m->spi_start + v2m->nr_spis - 1));
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
366
  	return 0;
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
367
368
369
370
371
372
373
374
375
376
377
  err_iounmap:
  	iounmap(v2m->base);
  err_free_v2m:
  	kfree(v2m);
  	return ret;
  }
  
  static struct of_device_id gicv2m_device_id[] = {
  	{	.compatible	= "arm,gic-v2m-frame",	},
  	{},
  };
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
378
379
  static int __init gicv2m_of_init(struct fwnode_handle *parent_handle,
  				 struct irq_domain *parent)
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
380
381
  {
  	int ret = 0;
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
382
  	struct device_node *node = to_of_node(parent_handle);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
383
384
385
386
  	struct device_node *child;
  
  	for (child = of_find_matching_node(node, gicv2m_device_id); child;
  	     child = of_find_matching_node(child, gicv2m_device_id)) {
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
387
388
  		u32 spi_start = 0, nr_spis = 0;
  		struct resource res;
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
389
390
  		if (!of_find_property(child, "msi-controller", NULL))
  			continue;
4266ab1a8   Suravee Suthikulpanit   irqchip/gic-v2m: ...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  		ret = of_address_to_resource(child, 0, &res);
  		if (ret) {
  			pr_err("Failed to allocate v2m resource.
  ");
  			break;
  		}
  
  		if (!of_property_read_u32(child, "arm,msi-base-spi",
  					  &spi_start) &&
  		    !of_property_read_u32(child, "arm,msi-num-spis", &nr_spis))
  			pr_info("DT overriding V2M MSI_TYPER (base:%u, num:%u)
  ",
  				spi_start, nr_spis);
  
  		ret = gicv2m_init_one(&child->fwnode, spi_start, nr_spis, &res);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
406
  		if (ret) {
86d14c72b   Marc Zyngier   irqchip/gic-v2m: ...
407
  			of_node_put(child);
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
408
409
410
  			break;
  		}
  	}
a71225e20   Marc Zyngier   irqchip/gic-v2m: ...
411
412
413
414
  	if (!ret)
  		ret = gicv2m_allocate_domains(parent);
  	if (ret)
  		gicv2m_teardown();
853a33ce6   Suravee Suthikulpanit   irqchip: gic-v2m:...
415
416
  	return ret;
  }
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  
  #ifdef CONFIG_ACPI
  static int acpi_num_msi;
  
  static struct fwnode_handle *gicv2m_get_fwnode(struct device *dev)
  {
  	struct v2m_data *data;
  
  	if (WARN_ON(acpi_num_msi <= 0))
  		return NULL;
  
  	/* We only return the fwnode of the first MSI frame. */
  	data = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
  	if (!data)
  		return NULL;
  
  	return data->fwnode;
  }
  
  static int __init
  acpi_parse_madt_msi(struct acpi_subtable_header *header,
  		    const unsigned long end)
  {
  	int ret;
  	struct resource res;
  	u32 spi_start = 0, nr_spis = 0;
  	struct acpi_madt_generic_msi_frame *m;
  	struct fwnode_handle *fwnode;
  
  	m = (struct acpi_madt_generic_msi_frame *)header;
  	if (BAD_MADT_ENTRY(m, end))
  		return -EINVAL;
  
  	res.start = m->base_address;
5a1ff480f   Suravee Suthikulpanit   irqchip/gicv2m: M...
451
452
  	res.end = m->base_address + SZ_4K - 1;
  	res.flags = IORESOURCE_MEM;
0644b3dac   Suravee Suthikulpanit   irqchip/gic-v2m: ...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  
  	if (m->flags & ACPI_MADT_OVERRIDE_SPI_VALUES) {
  		spi_start = m->spi_base;
  		nr_spis = m->spi_count;
  
  		pr_info("ACPI overriding V2M MSI_TYPER (base:%u, num:%u)
  ",
  			spi_start, nr_spis);
  	}
  
  	fwnode = irq_domain_alloc_fwnode((void *)m->base_address);
  	if (!fwnode) {
  		pr_err("Unable to allocate GICv2m domain token
  ");
  		return -EINVAL;
  	}
  
  	ret = gicv2m_init_one(fwnode, spi_start, nr_spis, &res);
  	if (ret)
  		irq_domain_free_fwnode(fwnode);
  
  	return ret;
  }
  
  static int __init gicv2m_acpi_init(struct irq_domain *parent)
  {
  	int ret;
  
  	if (acpi_num_msi > 0)
  		return 0;
  
  	acpi_num_msi = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_MSI_FRAME,
  				      acpi_parse_madt_msi, 0);
  
  	if (acpi_num_msi <= 0)
  		goto err_out;
  
  	ret = gicv2m_allocate_domains(parent);
  	if (ret)
  		goto err_out;
  
  	pci_msi_register_fwnode_provider(&gicv2m_get_fwnode);
  
  	return 0;
  
  err_out:
  	gicv2m_teardown();
  	return -EINVAL;
  }
  #else /* CONFIG_ACPI */
  static int __init gicv2m_acpi_init(struct irq_domain *parent)
  {
  	return -EINVAL;
  }
  #endif /* CONFIG_ACPI */
  
  int __init gicv2m_init(struct fwnode_handle *parent_handle,
  		       struct irq_domain *parent)
  {
  	if (is_of_node(parent_handle))
  		return gicv2m_of_init(parent_handle, parent);
  
  	return gicv2m_acpi_init(parent);
  }