xref: /openbmc/linux/drivers/iommu/hyperv-iommu.c (revision bef7a78d)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Hyper-V stub IOMMU driver.
5  *
6  * Copyright (C) 2019, Microsoft, Inc.
7  *
8  * Author : Lan Tianyu <Tianyu.Lan@microsoft.com>
9  */
10 
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>
16 
17 #include <asm/apic.h>
18 #include <asm/cpu.h>
19 #include <asm/hw_irq.h>
20 #include <asm/io_apic.h>
21 #include <asm/irq_remapping.h>
22 #include <asm/hypervisor.h>
23 
24 #include "irq_remapping.h"
25 
26 #ifdef CONFIG_IRQ_REMAP
27 
28 /*
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.
32  */
33 #define IOAPIC_REMAPPING_ENTRY 24
34 
35 static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
36 static struct irq_domain *ioapic_ir_domain;
37 
38 static int hyperv_ir_set_affinity(struct irq_data *data,
39 		const struct cpumask *mask, bool force)
40 {
41 	struct irq_data *parent = data->parent_data;
42 	struct irq_cfg *cfg = irqd_cfg(data);
43 	int ret;
44 
45 	/* Return error If new irq affinity is out of ioapic_max_cpumask. */
46 	if (!cpumask_subset(mask, &ioapic_max_cpumask))
47 		return -EINVAL;
48 
49 	ret = parent->chip->irq_set_affinity(parent, mask, force);
50 	if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
51 		return ret;
52 
53 	send_cleanup_vector(cfg);
54 
55 	return 0;
56 }
57 
58 static struct irq_chip hyperv_ir_chip = {
59 	.name			= "HYPERV-IR",
60 	.irq_ack		= apic_ack_irq,
61 	.irq_set_affinity	= hyperv_ir_set_affinity,
62 };
63 
64 static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
65 				     unsigned int virq, unsigned int nr_irqs,
66 				     void *arg)
67 {
68 	struct irq_alloc_info *info = arg;
69 	struct irq_data *irq_data;
70 	struct irq_desc *desc;
71 	int ret = 0;
72 
73 	if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
74 		return -EINVAL;
75 
76 	ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
77 	if (ret < 0)
78 		return ret;
79 
80 	irq_data = irq_domain_get_irq_data(domain, virq);
81 	if (!irq_data) {
82 		irq_domain_free_irqs_common(domain, virq, nr_irqs);
83 		return -EINVAL;
84 	}
85 
86 	irq_data->chip = &hyperv_ir_chip;
87 
88 	/*
89 	 * Hypver-V IO APIC irq affinity should be in the scope of
90 	 * ioapic_max_cpumask because no irq remapping support.
91 	 */
92 	desc = irq_data_to_desc(irq_data);
93 	cpumask_copy(desc->irq_common_data.affinity, &ioapic_max_cpumask);
94 
95 	return 0;
96 }
97 
98 static void hyperv_irq_remapping_free(struct irq_domain *domain,
99 				 unsigned int virq, unsigned int nr_irqs)
100 {
101 	irq_domain_free_irqs_common(domain, virq, nr_irqs);
102 }
103 
104 static int hyperv_irq_remapping_select(struct irq_domain *d,
105 				       struct irq_fwspec *fwspec,
106 				       enum irq_domain_bus_token bus_token)
107 {
108 	/* Claim the only I/O APIC emulated by Hyper-V */
109 	return x86_fwspec_is_ioapic(fwspec);
110 }
111 
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,
116 };
117 
118 static int __init hyperv_prepare_irq_remapping(void)
119 {
120 	struct fwnode_handle *fn;
121 	int i;
122 
123 	if (!hypervisor_is_type(X86_HYPER_MS_HYPERV) ||
124 	    x86_init.hyper.msi_ext_dest_id() ||
125 	    !x2apic_supported())
126 		return -ENODEV;
127 
128 	fn = irq_domain_alloc_named_id_fwnode("HYPERV-IR", 0);
129 	if (!fn)
130 		return -ENOMEM;
131 
132 	ioapic_ir_domain =
133 		irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
134 				0, IOAPIC_REMAPPING_ENTRY, fn,
135 				&hyperv_ir_domain_ops, NULL);
136 
137 	if (!ioapic_ir_domain) {
138 		irq_domain_free_fwnode(fn);
139 		return -ENOMEM;
140 	}
141 
142 	/*
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.
151 	 */
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);
155 
156 	return 0;
157 }
158 
159 static int __init hyperv_enable_irq_remapping(void)
160 {
161 	return IRQ_REMAP_X2APIC_MODE;
162 }
163 
164 struct irq_remap_ops hyperv_irq_remap_ops = {
165 	.prepare		= hyperv_prepare_irq_remapping,
166 	.enable			= hyperv_enable_irq_remapping,
167 };
168 
169 #endif
170