xref: /freebsd-13-stable/sys/x86/xen/pvcpu_enum.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com>
5  * All rights reserved.
6  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/smp.h>
36 #include <sys/pcpu.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 
40 #include <machine/intr_machdep.h>
41 #include <x86/apicvar.h>
42 
43 #include <machine/cpu.h>
44 #include <machine/smp.h>
45 #include <machine/md_var.h>
46 
47 #include <xen/xen-os.h>
48 #include <xen/xen_intr.h>
49 #include <xen/hypervisor.h>
50 
51 #include <xen/interface/vcpu.h>
52 
53 #include <contrib/dev/acpica/include/acpi.h>
54 #include <contrib/dev/acpica/include/aclocal.h>
55 #include <contrib/dev/acpica/include/actables.h>
56 
57 #include <dev/acpica/acpivar.h>
58 
59 static int xenpv_probe(void);
60 static int xenpv_probe_cpus(void);
61 static int xenpv_setup_local(void);
62 static int xenpv_setup_io(void);
63 
64 static ACPI_TABLE_MADT *madt;
65 static vm_paddr_t madt_physaddr;
66 static vm_offset_t madt_length;
67 
68 static struct apic_enumerator xenpv_enumerator = {
69 	.apic_name = "Xen PV",
70 	.apic_probe = xenpv_probe,
71 	.apic_probe_cpus = xenpv_probe_cpus,
72 	.apic_setup_local = xenpv_setup_local,
73 	.apic_setup_io = xenpv_setup_io
74 };
75 
76 /*--------------------- Helper functions to parse MADT -----------------------*/
77 
78 /*
79  * Parse an interrupt source override for an ISA interrupt.
80  */
81 static void
madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE * intr)82 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
83 {
84 	enum intr_trigger trig;
85 	enum intr_polarity pol;
86 	int ret;
87 
88 	if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
89 	    intr->GlobalIrq == 2) {
90 		if (bootverbose)
91 			printf("MADT: Skipping timer override\n");
92 		return;
93 	}
94 
95 	madt_parse_interrupt_values(intr, &trig, &pol);
96 
97 	/* Remap the IRQ if it is mapped to a different interrupt vector. */
98 	if (intr->SourceIrq != intr->GlobalIrq && intr->GlobalIrq > 15 &&
99 	    intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
100 		/*
101 		 * If the SCI is remapped to a non-ISA global interrupt,
102 		 * then override the vector we use to setup.
103 		 */
104 		acpi_OverrideInterruptLevel(intr->GlobalIrq);
105 
106 	/* Register the IRQ with the polarity and trigger mode found. */
107 	ret = xen_register_pirq(intr->GlobalIrq, trig, pol);
108 	if (ret != 0)
109 		panic("Unable to register interrupt override");
110 }
111 
112 /*
113  * Call the handler routine for each entry in the MADT table.
114  */
115 static void
madt_walk_table(acpi_subtable_handler * handler,void * arg)116 madt_walk_table(acpi_subtable_handler *handler, void *arg)
117 {
118 
119 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
120 	    handler, arg);
121 }
122 
123 /*
124  * Parse interrupt entries.
125  */
126 static void
madt_parse_ints(ACPI_SUBTABLE_HEADER * entry,void * arg __unused)127 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
128 {
129 
130 	if (entry->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE)
131 		madt_parse_interrupt_override(
132 		    (ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
133 }
134 
135 /*---------------------------- Xen PV enumerator -----------------------------*/
136 
137 /*
138  * This enumerator will only be registered on PVH
139  */
140 static int
xenpv_probe(void)141 xenpv_probe(void)
142 {
143 	return (0);
144 }
145 
146 /*
147  * Test each possible vCPU in order to find the number of vCPUs
148  */
149 static int
xenpv_probe_cpus(void)150 xenpv_probe_cpus(void)
151 {
152 #ifdef SMP
153 	int i, ret;
154 
155 	for (i = 0; i < MAXCPU && (i * 2) < MAX_APIC_ID; i++) {
156 		ret = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
157 		mp_ncpus = min(mp_ncpus + 1, MAXCPU);
158 	}
159 	mp_maxid = mp_ncpus - 1;
160 	max_apic_id = mp_ncpus * 2;
161 #endif
162 	return (0);
163 }
164 
165 /*
166  * Initialize the vCPU id of the BSP
167  */
168 static int
xenpv_setup_local(void)169 xenpv_setup_local(void)
170 {
171 #ifdef SMP
172 	int i, ret;
173 
174 	for (i = 0; i < MAXCPU && (i * 2) < MAX_APIC_ID; i++) {
175 		ret = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
176 		if (ret >= 0)
177 			lapic_create((i * 2), (i == 0));
178 	}
179 #endif
180 
181 	PCPU_SET(vcpu_id, 0);
182 	lapic_init(0);
183 	return (0);
184 }
185 
186 /*
187  * On PVH guests there's no IO APIC
188  */
189 static int
xenpv_setup_io(void)190 xenpv_setup_io(void)
191 {
192 
193 	if (xen_initial_domain()) {
194 		/*
195 		 * NB: we could iterate over the MADT IOAPIC entries in order
196 		 * to figure out the exact number of IOAPIC interrupts, but
197 		 * this is legacy code so just keep using the previous
198 		 * behaviour and assume a maximum of 256 interrupts.
199 		 */
200 		num_io_irqs = max(255, num_io_irqs);
201 
202 		acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
203 	}
204 	return (0);
205 }
206 
207 void
xenpv_register_pirqs(struct pic * pic __unused)208 xenpv_register_pirqs(struct pic *pic __unused)
209 {
210 	unsigned int i;
211 	int ret;
212 
213 	/* Map MADT */
214 	madt_physaddr = acpi_find_table(ACPI_SIG_MADT);
215 	madt = acpi_map_table(madt_physaddr, ACPI_SIG_MADT);
216 	madt_length = madt->Header.Length;
217 
218 	/* Try to initialize ACPI so that we can access the FADT. */
219 	ret = acpi_Startup();
220 	if (ACPI_FAILURE(ret)) {
221 		printf("MADT: ACPI Startup failed with %s\n",
222 		    AcpiFormatException(ret));
223 		printf("Try disabling either ACPI or apic support.\n");
224 		panic("Using MADT but ACPI doesn't work");
225 	}
226 
227 	/* Run through the table to see if there are any overrides. */
228 	madt_walk_table(madt_parse_ints, NULL);
229 
230 	/*
231 	 * If there was not an explicit override entry for the SCI,
232 	 * force it to use level trigger and active-low polarity.
233 	 */
234 	if (!madt_found_sci_override) {
235 		printf(
236 "MADT: Forcing active-low polarity and level trigger for SCI\n");
237 		ret = xen_register_pirq(AcpiGbl_FADT.SciInterrupt,
238 		    INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
239 		if (ret != 0)
240 			panic("Unable to register SCI IRQ");
241 	}
242 
243 	/* Register legacy ISA IRQs */
244 	for (i = 1; i < 16; i++) {
245 		if (intr_lookup_source(i) != NULL)
246 			continue;
247 		ret = xen_register_pirq(i, INTR_TRIGGER_EDGE,
248 		    INTR_POLARITY_LOW);
249 		if (ret != 0 && bootverbose)
250 			printf("Unable to register legacy IRQ#%u: %d\n", i,
251 			    ret);
252 	}
253 }
254 
255 static void
xenpv_register(void * dummy __unused)256 xenpv_register(void *dummy __unused)
257 {
258 	if (xen_pv_domain()) {
259 		apic_register_enumerator(&xenpv_enumerator);
260 	}
261 }
262 SYSINIT(xenpv_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, xenpv_register, NULL);
263