1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 ARM Limited, All Rights Reserved.
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #define pr_fmt(fmt) "GICv3: " fmt
9 #include <linux/iommu.h>
10 #include <linux/irq.h>
11 #include <linux/irqdomain.h>
12 #include <linux/kernel.h>
13 #include <linux/msi.h>
14 #include <linux/of_address.h>
15 #include <linux/of_pci.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
19 #include <linux/irqchip/arm-gic-v3.h>
21 #include "irq-msi-lib.h"
29 static DEFINE_MUTEX(mbi_lock
);
30 static phys_addr_t mbi_phys_base
;
31 static struct mbi_range
*mbi_ranges
;
32 static unsigned int mbi_range_nr
;
34 static struct irq_chip mbi_irq_chip
= {
36 .irq_mask
= irq_chip_mask_parent
,
37 .irq_unmask
= irq_chip_unmask_parent
,
38 .irq_eoi
= irq_chip_eoi_parent
,
39 .irq_set_type
= irq_chip_set_type_parent
,
40 .irq_set_affinity
= irq_chip_set_affinity_parent
,
43 static int mbi_irq_gic_domain_alloc(struct irq_domain
*domain
,
45 irq_hw_number_t hwirq
)
47 struct irq_fwspec fwspec
;
52 * Using ACPI? There is no MBI support in the spec, you
53 * shouldn't even be here.
55 if (!is_of_node(domain
->parent
->fwnode
))
59 * Let's default to edge. This is consistent with traditional
60 * MSIs, and systems requiring level signaling will just
61 * enforce the trigger on their own.
63 fwspec
.fwnode
= domain
->parent
->fwnode
;
64 fwspec
.param_count
= 3;
66 fwspec
.param
[1] = hwirq
- 32;
67 fwspec
.param
[2] = IRQ_TYPE_EDGE_RISING
;
69 err
= irq_domain_alloc_irqs_parent(domain
, virq
, 1, &fwspec
);
73 d
= irq_domain_get_irq_data(domain
->parent
, virq
);
74 return d
->chip
->irq_set_type(d
, IRQ_TYPE_EDGE_RISING
);
77 static void mbi_free_msi(struct mbi_range
*mbi
, unsigned int hwirq
,
80 mutex_lock(&mbi_lock
);
81 bitmap_release_region(mbi
->bm
, hwirq
- mbi
->spi_start
,
82 get_count_order(nr_irqs
));
83 mutex_unlock(&mbi_lock
);
86 static int mbi_irq_domain_alloc(struct irq_domain
*domain
, unsigned int virq
,
87 unsigned int nr_irqs
, void *args
)
89 msi_alloc_info_t
*info
= args
;
90 struct mbi_range
*mbi
= NULL
;
91 int hwirq
, offset
, i
, err
= 0;
93 mutex_lock(&mbi_lock
);
94 for (i
= 0; i
< mbi_range_nr
; i
++) {
95 offset
= bitmap_find_free_region(mbi_ranges
[i
].bm
,
96 mbi_ranges
[i
].nr_spis
,
97 get_count_order(nr_irqs
));
103 mutex_unlock(&mbi_lock
);
108 hwirq
= mbi
->spi_start
+ offset
;
110 err
= iommu_dma_prepare_msi(info
->desc
,
111 mbi_phys_base
+ GICD_SETSPI_NSR
);
115 for (i
= 0; i
< nr_irqs
; i
++) {
116 err
= mbi_irq_gic_domain_alloc(domain
, virq
+ i
, hwirq
+ i
);
120 irq_domain_set_hwirq_and_chip(domain
, virq
+ i
, hwirq
+ i
,
127 irq_domain_free_irqs_parent(domain
, virq
, nr_irqs
);
128 mbi_free_msi(mbi
, hwirq
, nr_irqs
);
132 static void mbi_irq_domain_free(struct irq_domain
*domain
,
133 unsigned int virq
, unsigned int nr_irqs
)
135 struct irq_data
*d
= irq_domain_get_irq_data(domain
, virq
);
136 struct mbi_range
*mbi
= irq_data_get_irq_chip_data(d
);
138 mbi_free_msi(mbi
, d
->hwirq
, nr_irqs
);
139 irq_domain_free_irqs_parent(domain
, virq
, nr_irqs
);
142 static const struct irq_domain_ops mbi_domain_ops
= {
143 .select
= msi_lib_irq_domain_select
,
144 .alloc
= mbi_irq_domain_alloc
,
145 .free
= mbi_irq_domain_free
,
148 static void mbi_compose_msi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
150 msg
[0].address_hi
= upper_32_bits(mbi_phys_base
+ GICD_SETSPI_NSR
);
151 msg
[0].address_lo
= lower_32_bits(mbi_phys_base
+ GICD_SETSPI_NSR
);
152 msg
[0].data
= data
->parent_data
->hwirq
;
154 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(data
), msg
);
157 static void mbi_compose_mbi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
159 mbi_compose_msi_msg(data
, msg
);
161 msg
[1].address_hi
= upper_32_bits(mbi_phys_base
+ GICD_CLRSPI_NSR
);
162 msg
[1].address_lo
= lower_32_bits(mbi_phys_base
+ GICD_CLRSPI_NSR
);
163 msg
[1].data
= data
->parent_data
->hwirq
;
165 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(data
), &msg
[1]);
168 static bool mbi_init_dev_msi_info(struct device
*dev
, struct irq_domain
*domain
,
169 struct irq_domain
*real_parent
, struct msi_domain_info
*info
)
171 if (!msi_lib_init_dev_msi_info(dev
, domain
, real_parent
, info
))
174 switch (info
->bus_token
) {
175 case DOMAIN_BUS_PCI_DEVICE_MSI
:
176 case DOMAIN_BUS_PCI_DEVICE_MSIX
:
177 info
->chip
->irq_compose_msi_msg
= mbi_compose_msi_msg
;
180 case DOMAIN_BUS_DEVICE_MSI
:
181 info
->chip
->irq_compose_msi_msg
= mbi_compose_mbi_msg
;
182 info
->chip
->irq_set_type
= irq_chip_set_type_parent
;
183 info
->chip
->flags
|= IRQCHIP_SUPPORTS_LEVEL_MSI
;
184 info
->flags
|= MSI_FLAG_LEVEL_CAPABLE
;
193 #define MBI_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
194 MSI_FLAG_USE_DEF_CHIP_OPS | \
195 MSI_FLAG_PCI_MSI_MASK_PARENT)
197 #define MBI_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
198 MSI_FLAG_PCI_MSIX | \
199 MSI_FLAG_MULTI_PCI_MSI)
201 static const struct msi_parent_ops gic_v3_mbi_msi_parent_ops
= {
202 .supported_flags
= MBI_MSI_FLAGS_SUPPORTED
,
203 .required_flags
= MBI_MSI_FLAGS_REQUIRED
,
204 .bus_select_token
= DOMAIN_BUS_NEXUS
,
205 .bus_select_mask
= MATCH_PCI_MSI
| MATCH_PLATFORM_MSI
,
207 .init_dev_msi_info
= mbi_init_dev_msi_info
,
210 static int mbi_allocate_domain(struct irq_domain
*parent
)
212 struct irq_domain
*nexus_domain
;
214 nexus_domain
= irq_domain_create_hierarchy(parent
, 0, 0, parent
->fwnode
,
215 &mbi_domain_ops
, NULL
);
219 irq_domain_update_bus_token(nexus_domain
, DOMAIN_BUS_NEXUS
);
220 nexus_domain
->flags
|= IRQ_DOMAIN_FLAG_MSI_PARENT
;
221 nexus_domain
->msi_parent_ops
= &gic_v3_mbi_msi_parent_ops
;
225 int __init
mbi_init(struct fwnode_handle
*fwnode
, struct irq_domain
*parent
)
227 struct device_node
*np
;
231 np
= to_of_node(fwnode
);
233 if (!of_property_read_bool(np
, "msi-controller"))
236 n
= of_property_count_elems_of_size(np
, "mbi-ranges", sizeof(u32
));
240 mbi_range_nr
= n
/ 2;
241 mbi_ranges
= kcalloc(mbi_range_nr
, sizeof(*mbi_ranges
), GFP_KERNEL
);
245 for (n
= 0; n
< mbi_range_nr
; n
++) {
246 ret
= of_property_read_u32_index(np
, "mbi-ranges", n
* 2,
247 &mbi_ranges
[n
].spi_start
);
250 ret
= of_property_read_u32_index(np
, "mbi-ranges", n
* 2 + 1,
251 &mbi_ranges
[n
].nr_spis
);
255 mbi_ranges
[n
].bm
= bitmap_zalloc(mbi_ranges
[n
].nr_spis
, GFP_KERNEL
);
256 if (!mbi_ranges
[n
].bm
) {
260 pr_info("MBI range [%d:%d]\n", mbi_ranges
[n
].spi_start
,
261 mbi_ranges
[n
].spi_start
+ mbi_ranges
[n
].nr_spis
- 1);
264 reg
= of_get_property(np
, "mbi-alias", NULL
);
266 mbi_phys_base
= of_translate_address(np
, reg
);
267 if (mbi_phys_base
== (phys_addr_t
)OF_BAD_ADDR
) {
274 if (of_address_to_resource(np
, 0, &res
)) {
279 mbi_phys_base
= res
.start
;
282 pr_info("Using MBI frame %pa\n", &mbi_phys_base
);
284 ret
= mbi_allocate_domain(parent
);
292 for (n
= 0; n
< mbi_range_nr
; n
++)
293 bitmap_free(mbi_ranges
[n
].bm
);