1 // SPDX-License-Identifier: GPL-2.0
4 * Hyper-V stub IOMMU driver.
6 * Copyright (C) 2019, Microsoft, Inc.
8 * Author : Lan Tianyu <Tianyu.Lan@microsoft.com>
11 #include <linux/types.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/iommu.h>
15 #include <linux/module.h>
19 #include <asm/hw_irq.h>
20 #include <asm/io_apic.h>
21 #include <asm/irq_remapping.h>
22 #include <asm/hypervisor.h>
24 #include "irq_remapping.h"
26 #ifdef CONFIG_IRQ_REMAP
29 * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
30 * Redirection Table. Hyper-V exposes one single IO-APIC and so define
31 * 24 IO APIC remmapping entries.
33 #define IOAPIC_REMAPPING_ENTRY 24
35 static cpumask_t ioapic_max_cpumask
= { CPU_BITS_NONE
};
36 static struct irq_domain
*ioapic_ir_domain
;
38 static int hyperv_ir_set_affinity(struct irq_data
*data
,
39 const struct cpumask
*mask
, bool force
)
41 struct irq_data
*parent
= data
->parent_data
;
42 struct irq_cfg
*cfg
= irqd_cfg(data
);
45 /* Return error If new irq affinity is out of ioapic_max_cpumask. */
46 if (!cpumask_subset(mask
, &ioapic_max_cpumask
))
49 ret
= parent
->chip
->irq_set_affinity(parent
, mask
, force
);
50 if (ret
< 0 || ret
== IRQ_SET_MASK_OK_DONE
)
53 send_cleanup_vector(cfg
);
58 static struct irq_chip hyperv_ir_chip
= {
60 .irq_ack
= apic_ack_irq
,
61 .irq_set_affinity
= hyperv_ir_set_affinity
,
64 static int hyperv_irq_remapping_alloc(struct irq_domain
*domain
,
65 unsigned int virq
, unsigned int nr_irqs
,
68 struct irq_alloc_info
*info
= arg
;
69 struct irq_data
*irq_data
;
70 struct irq_desc
*desc
;
73 if (!info
|| info
->type
!= X86_IRQ_ALLOC_TYPE_IOAPIC
|| nr_irqs
> 1)
76 ret
= irq_domain_alloc_irqs_parent(domain
, virq
, nr_irqs
, arg
);
80 irq_data
= irq_domain_get_irq_data(domain
, virq
);
82 irq_domain_free_irqs_common(domain
, virq
, nr_irqs
);
86 irq_data
->chip
= &hyperv_ir_chip
;
89 * Hypver-V IO APIC irq affinity should be in the scope of
90 * ioapic_max_cpumask because no irq remapping support.
92 desc
= irq_data_to_desc(irq_data
);
93 cpumask_copy(desc
->irq_common_data
.affinity
, &ioapic_max_cpumask
);
98 static void hyperv_irq_remapping_free(struct irq_domain
*domain
,
99 unsigned int virq
, unsigned int nr_irqs
)
101 irq_domain_free_irqs_common(domain
, virq
, nr_irqs
);
104 static int hyperv_irq_remapping_select(struct irq_domain
*d
,
105 struct irq_fwspec
*fwspec
,
106 enum irq_domain_bus_token bus_token
)
108 /* Claim the only I/O APIC emulated by Hyper-V */
109 return x86_fwspec_is_ioapic(fwspec
);
112 static const struct irq_domain_ops hyperv_ir_domain_ops
= {
113 .select
= hyperv_irq_remapping_select
,
114 .alloc
= hyperv_irq_remapping_alloc
,
115 .free
= hyperv_irq_remapping_free
,
118 static int __init
hyperv_prepare_irq_remapping(void)
120 struct fwnode_handle
*fn
;
123 if (!hypervisor_is_type(X86_HYPER_MS_HYPERV
) ||
124 x86_init
.hyper
.msi_ext_dest_id() ||
128 fn
= irq_domain_alloc_named_id_fwnode("HYPERV-IR", 0);
133 irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
134 0, IOAPIC_REMAPPING_ENTRY
, fn
,
135 &hyperv_ir_domain_ops
, NULL
);
137 if (!ioapic_ir_domain
) {
138 irq_domain_free_fwnode(fn
);
143 * Hyper-V doesn't provide irq remapping function for
144 * IO-APIC and so IO-APIC only accepts 8-bit APIC ID.
145 * Cpu's APIC ID is read from ACPI MADT table and APIC IDs
146 * in the MADT table on Hyper-v are sorted monotonic increasingly.
147 * APIC ID reflects cpu topology. There maybe some APIC ID
148 * gaps when cpu number in a socket is not power of two. Prepare
149 * max cpu affinity for IOAPIC irqs. Scan cpu 0-255 and set cpu
150 * into ioapic_max_cpumask if its APIC ID is less than 256.
152 for (i
= min_t(unsigned int, num_possible_cpus() - 1, 255); i
>= 0; i
--)
153 if (cpu_physical_id(i
) < 256)
154 cpumask_set_cpu(i
, &ioapic_max_cpumask
);
159 static int __init
hyperv_enable_irq_remapping(void)
161 return IRQ_REMAP_X2APIC_MODE
;
164 struct irq_remap_ops hyperv_irq_remap_ops
= {
165 .prepare
= hyperv_prepare_irq_remapping
,
166 .enable
= hyperv_enable_irq_remapping
,