1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support of MSI, HPET and DMAR interrupts.
5 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
6 * Moved from arch/x86/kernel/apic/io_apic.c.
7 * Jiang Liu <jiang.liu@linux.intel.com>
8 * Convert to hierarchical irqdomain
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/pci.h>
14 #include <linux/dmar.h>
15 #include <linux/hpet.h>
16 #include <linux/msi.h>
17 #include <asm/irqdomain.h>
19 #include <asm/hw_irq.h>
21 #include <asm/irq_remapping.h>
22 #include <asm/xen/hypervisor.h>
24 struct irq_domain *x86_pci_msi_default_domain __ro_after_init;
26 static void irq_msi_update_msg(struct irq_data *irqd, struct irq_cfg *cfg)
28 struct msi_msg msg[2] = { [1] = { }, };
30 __irq_msi_compose_msg(cfg, msg, false);
31 irq_data_get_irq_chip(irqd)->irq_write_msi_msg(irqd, msg);
35 msi_set_affinity(struct irq_data *irqd, const struct cpumask *mask, bool force)
37 struct irq_cfg old_cfg, *cfg = irqd_cfg(irqd);
38 struct irq_data *parent = irqd->parent_data;
42 /* Save the current configuration */
43 cpu = cpumask_first(irq_data_get_effective_affinity_mask(irqd));
46 /* Allocate a new target vector */
47 ret = parent->chip->irq_set_affinity(parent, mask, force);
48 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
52 * For non-maskable and non-remapped MSI interrupts the migration
53 * to a different destination CPU and a different vector has to be
54 * done careful to handle the possible stray interrupt which can be
55 * caused by the non-atomic update of the address/data pair.
57 * Direct update is possible when:
58 * - The MSI is maskable (remapped MSI does not use this code path).
59 * The reservation mode bit is set in this case.
60 * - The new vector is the same as the old vector
61 * - The old vector is MANAGED_IRQ_SHUTDOWN_VECTOR (interrupt starts up)
62 * - The interrupt is not yet started up
63 * - The new destination CPU is the same as the old destination CPU
65 if (!irqd_can_reserve(irqd) ||
66 cfg->vector == old_cfg.vector ||
67 old_cfg.vector == MANAGED_IRQ_SHUTDOWN_VECTOR ||
68 !irqd_is_started(irqd) ||
69 cfg->dest_apicid == old_cfg.dest_apicid) {
70 irq_msi_update_msg(irqd, cfg);
75 * Paranoia: Validate that the interrupt target is the local
78 if (WARN_ON_ONCE(cpu != smp_processor_id())) {
79 irq_msi_update_msg(irqd, cfg);
84 * Redirect the interrupt to the new vector on the current CPU
85 * first. This might cause a spurious interrupt on this vector if
86 * the device raises an interrupt right between this update and the
87 * update to the final destination CPU.
89 * If the vector is in use then the installed device handler will
90 * denote it as spurious which is no harm as this is a rare event
91 * and interrupt handlers have to cope with spurious interrupts
92 * anyway. If the vector is unused, then it is marked so it won't
93 * trigger the 'No irq handler for vector' warning in
96 * This requires to hold vector lock to prevent concurrent updates to
97 * the affected vector.
102 * Mark the new target vector on the local CPU if it is currently
103 * unused. Reuse the VECTOR_RETRIGGERED state which is also used in
104 * the CPU hotplug path for a similar purpose. This cannot be
105 * undone here as the current CPU has interrupts disabled and
106 * cannot handle the interrupt before the whole set_affinity()
107 * section is done. In the CPU unplug case, the current CPU is
108 * about to vanish and will not handle any interrupts anymore. The
109 * vector is cleaned up when the CPU comes online again.
111 if (IS_ERR_OR_NULL(this_cpu_read(vector_irq[cfg->vector])))
112 this_cpu_write(vector_irq[cfg->vector], VECTOR_RETRIGGERED);
114 /* Redirect it to the new vector on the local CPU temporarily */
115 old_cfg.vector = cfg->vector;
116 irq_msi_update_msg(irqd, &old_cfg);
118 /* Now transition it to the target CPU */
119 irq_msi_update_msg(irqd, cfg);
122 * All interrupts after this point are now targeted at the new
125 * Drop vector lock before testing whether the temporary assignment
126 * to the local CPU was hit by an interrupt raised in the device,
127 * because the retrigger function acquires vector lock again.
129 unlock_vector_lock();
132 * Check whether the transition raced with a device interrupt and
133 * is pending in the local APICs IRR. It is safe to do this outside
134 * of vector lock as the irq_desc::lock of this interrupt is still
135 * held and interrupts are disabled: The check is not accessing the
136 * underlying vector store. It's just checking the local APIC's
139 if (lapic_vector_set_in_irr(cfg->vector))
140 irq_data_get_irq_chip(irqd)->irq_retrigger(irqd);
146 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
147 * which implement the MSI or MSI-X Capability Structure.
149 static struct irq_chip pci_msi_controller = {
151 .irq_unmask = pci_msi_unmask_irq,
152 .irq_mask = pci_msi_mask_irq,
153 .irq_ack = irq_chip_ack_parent,
154 .irq_retrigger = irq_chip_retrigger_hierarchy,
155 .irq_set_affinity = msi_set_affinity,
156 .flags = IRQCHIP_SKIP_SET_WAKE |
157 IRQCHIP_AFFINITY_PRE_STARTUP,
160 int pci_msi_prepare(struct irq_domain *domain, struct device *dev, int nvec,
161 msi_alloc_info_t *arg)
163 init_irq_alloc_info(arg, NULL);
164 if (to_pci_dev(dev)->msix_enabled) {
165 arg->type = X86_IRQ_ALLOC_TYPE_PCI_MSIX;
167 arg->type = X86_IRQ_ALLOC_TYPE_PCI_MSI;
168 arg->flags |= X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
173 EXPORT_SYMBOL_GPL(pci_msi_prepare);
175 static struct msi_domain_ops pci_msi_domain_ops = {
176 .msi_prepare = pci_msi_prepare,
179 static struct msi_domain_info pci_msi_domain_info = {
180 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
182 .ops = &pci_msi_domain_ops,
183 .chip = &pci_msi_controller,
184 .handler = handle_edge_irq,
185 .handler_name = "edge",
188 struct irq_domain * __init native_create_pci_msi_domain(void)
190 struct fwnode_handle *fn;
191 struct irq_domain *d;
196 fn = irq_domain_alloc_named_fwnode("PCI-MSI");
200 d = pci_msi_create_irq_domain(fn, &pci_msi_domain_info,
203 irq_domain_free_fwnode(fn);
204 pr_warn("Failed to initialize PCI-MSI irqdomain.\n");
209 void __init x86_create_pci_msi_domain(void)
211 x86_pci_msi_default_domain = x86_init.irqs.create_pci_msi_domain();
214 #ifdef CONFIG_IRQ_REMAP
215 static struct irq_chip pci_msi_ir_controller = {
216 .name = "IR-PCI-MSI",
217 .irq_unmask = pci_msi_unmask_irq,
218 .irq_mask = pci_msi_mask_irq,
219 .irq_ack = irq_chip_ack_parent,
220 .irq_retrigger = irq_chip_retrigger_hierarchy,
221 .flags = IRQCHIP_SKIP_SET_WAKE |
222 IRQCHIP_AFFINITY_PRE_STARTUP,
225 static struct msi_domain_info pci_msi_ir_domain_info = {
226 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
227 MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX,
228 .ops = &pci_msi_domain_ops,
229 .chip = &pci_msi_ir_controller,
230 .handler = handle_edge_irq,
231 .handler_name = "edge",
234 struct irq_domain *arch_create_remap_msi_irq_domain(struct irq_domain *parent,
235 const char *name, int id)
237 struct fwnode_handle *fn;
238 struct irq_domain *d;
240 fn = irq_domain_alloc_named_id_fwnode(name, id);
243 d = pci_msi_create_irq_domain(fn, &pci_msi_ir_domain_info, parent);
245 irq_domain_free_fwnode(fn);
250 #ifdef CONFIG_DMAR_TABLE
252 * The Intel IOMMU (ab)uses the high bits of the MSI address to contain the
253 * high bits of the destination APIC ID. This can't be done in the general
254 * case for MSIs as it would be targeting real memory above 4GiB not the
257 static void dmar_msi_compose_msg(struct irq_data *data, struct msi_msg *msg)
259 __irq_msi_compose_msg(irqd_cfg(data), msg, true);
262 static void dmar_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
264 dmar_msi_write(data->irq, msg);
267 static struct irq_chip dmar_msi_controller = {
269 .irq_unmask = dmar_msi_unmask,
270 .irq_mask = dmar_msi_mask,
271 .irq_ack = irq_chip_ack_parent,
272 .irq_set_affinity = msi_domain_set_affinity,
273 .irq_retrigger = irq_chip_retrigger_hierarchy,
274 .irq_compose_msi_msg = dmar_msi_compose_msg,
275 .irq_write_msi_msg = dmar_msi_write_msg,
276 .flags = IRQCHIP_SKIP_SET_WAKE |
277 IRQCHIP_AFFINITY_PRE_STARTUP,
280 static int dmar_msi_init(struct irq_domain *domain,
281 struct msi_domain_info *info, unsigned int virq,
282 irq_hw_number_t hwirq, msi_alloc_info_t *arg)
284 irq_domain_set_info(domain, virq, arg->devid, info->chip, NULL,
285 handle_edge_irq, arg->data, "edge");
290 static struct msi_domain_ops dmar_msi_domain_ops = {
291 .msi_init = dmar_msi_init,
294 static struct msi_domain_info dmar_msi_domain_info = {
295 .ops = &dmar_msi_domain_ops,
296 .chip = &dmar_msi_controller,
297 .flags = MSI_FLAG_USE_DEF_DOM_OPS,
300 static struct irq_domain *dmar_get_irq_domain(void)
302 static struct irq_domain *dmar_domain;
303 static DEFINE_MUTEX(dmar_lock);
304 struct fwnode_handle *fn;
306 mutex_lock(&dmar_lock);
310 fn = irq_domain_alloc_named_fwnode("DMAR-MSI");
312 dmar_domain = msi_create_irq_domain(fn, &dmar_msi_domain_info,
315 irq_domain_free_fwnode(fn);
318 mutex_unlock(&dmar_lock);
322 int dmar_alloc_hwirq(int id, int node, void *arg)
324 struct irq_domain *domain = dmar_get_irq_domain();
325 struct irq_alloc_info info;
330 init_irq_alloc_info(&info, NULL);
331 info.type = X86_IRQ_ALLOC_TYPE_DMAR;
336 return irq_domain_alloc_irqs(domain, 1, node, &info);
339 void dmar_free_hwirq(int irq)
341 irq_domain_free_irqs(irq, 1);
345 bool arch_restore_msi_irqs(struct pci_dev *dev)
347 return xen_initdom_restore_msi(dev);