xref: /openbsd-src/sys/dev/acpi/acpimadt.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /* $OpenBSD: acpimadt.c,v 1.26 2012/01/07 20:13:16 kettenis Exp $ */
2 /*
3  * Copyright (c) 2006 Mark Kettenis <kettenis@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 #include <sys/malloc.h>
22 
23 #include <machine/apicvar.h>
24 #include <machine/cpuvar.h>
25 #include <machine/bus.h>
26 
27 #include <dev/acpi/acpireg.h>
28 #include <dev/acpi/acpivar.h>
29 #include <dev/acpi/acpidev.h>
30 #include <dev/acpi/amltypes.h>
31 #include <dev/acpi/dsdt.h>
32 
33 #include <machine/i8259.h>
34 #include <machine/i82093reg.h>
35 #include <machine/i82093var.h>
36 #include <machine/i82489reg.h>
37 #include <machine/i82489var.h>
38 
39 #include <machine/mpbiosvar.h>
40 
41 #include "ioapic.h"
42 
43 u_int8_t acpi_lapic_flags[LAPIC_MAP_SIZE];
44 
45 int acpimadt_match(struct device *, void *, void *);
46 void acpimadt_attach(struct device *, struct device *, void *);
47 
48 struct cfattach acpimadt_ca = {
49 	sizeof(struct device), acpimadt_match, acpimadt_attach
50 };
51 
52 struct cfdriver acpimadt_cd = {
53 	NULL, "acpimadt", DV_DULL
54 };
55 
56 int acpimadt_validate(struct acpi_madt *);
57 void acpimadt_cfg_intr(int, u_int32_t *);
58 int acpimadt_print(void *, const char *);
59 
60 int
61 acpimadt_match(struct device *parent, void *match, void *aux)
62 {
63 	struct acpi_attach_args *aaa = aux;
64 	struct acpi_table_header *hdr;
65 
66 	/*
67 	 * If we do not have a table, it is not us
68 	 */
69 	if (aaa->aaa_table == NULL)
70 		return (0);
71 
72 	/*
73 	 * If it is an MADT table, we can attach
74 	 */
75 	hdr = (struct acpi_table_header *)aaa->aaa_table;
76 	if (memcmp(hdr->signature, MADT_SIG, sizeof(MADT_SIG) - 1) != 0)
77 		return (0);
78 
79 	return (1);
80 }
81 
82 int
83 acpimadt_validate(struct acpi_madt *madt)
84 {
85 	caddr_t addr = (caddr_t)(madt + 1);
86 
87 	while (addr < (caddr_t)madt + madt->hdr.length) {
88 		union acpi_madt_entry *entry = (union acpi_madt_entry *)addr;
89 		u_int8_t length = entry->madt_lapic.length;
90 
91 		if (length < 2)
92 			return (0);
93 
94 		if (addr + length > (caddr_t)madt + madt->hdr.length)
95 			return (0);
96 
97 		switch (entry->madt_lapic.apic_type) {
98 		case ACPI_MADT_LAPIC:
99 			if (length != sizeof(entry->madt_lapic))
100 				return (0);
101 			break;
102 		case ACPI_MADT_IOAPIC:
103 			if (length != sizeof(entry->madt_ioapic))
104 				return (0);
105 			break;
106 		case ACPI_MADT_OVERRIDE:
107 			if (length != sizeof(entry->madt_override))
108 				return (0);
109 			break;
110 		case ACPI_MADT_NMI:
111 			if (length != sizeof(entry->madt_nmi))
112 				return (0);
113 			break;
114 		case ACPI_MADT_LAPIC_NMI:
115 			if (length != sizeof(entry->madt_lapic_nmi))
116 				return (0);
117 			break;
118 		case ACPI_MADT_LAPIC_OVERRIDE:
119 			if (length != sizeof(entry->madt_lapic_override))
120 				return (0);
121 			break;
122 		case ACPI_MADT_IO_SAPIC:
123 			if (length != sizeof(entry->madt_io_sapic))
124 				return (0);
125 			break;
126 		case ACPI_MADT_LOCAL_SAPIC:
127 			if (length != sizeof(entry->madt_local_sapic))
128 				return (0);
129 			break;
130 		case ACPI_MADT_PLATFORM_INT:
131 			if (length != sizeof(entry->madt_platform_int))
132 				return (0);
133 			break;
134 		case ACPI_MADT_X2APIC:
135 			if (length != sizeof(entry->madt_x2apic))
136 				return (0);
137 			break;
138 		case ACPI_MADT_X2APIC_NMI:
139 			if (length != sizeof(entry->madt_x2apic_nmi))
140 				return (0);
141 			break;
142 		}
143 
144 		addr += length;
145 	}
146 
147 	return (1);
148 }
149 
150 struct mp_bus acpimadt_busses[256];
151 struct mp_bus acpimadt_isa_bus;
152 
153 void
154 acpimadt_cfg_intr(int flags, u_int32_t *redir)
155 {
156 	int mpspo = (flags >> MPS_INTPO_SHIFT) & MPS_INTPO_MASK;
157 	int mpstrig = (flags >> MPS_INTTR_SHIFT) & MPS_INTTR_MASK;
158 
159 	*redir &= ~IOAPIC_REDLO_DEL_MASK;
160 	switch (mpspo) {
161 	case MPS_INTPO_DEF:
162 	case MPS_INTPO_ACTHI:
163 		*redir &= ~IOAPIC_REDLO_ACTLO;
164 		break;
165 	case MPS_INTPO_ACTLO:
166 		*redir |= IOAPIC_REDLO_ACTLO;
167 		break;
168 	default:
169 		panic("unknown MPS interrupt polarity %d", mpspo);
170 	}
171 
172 	*redir |= (IOAPIC_REDLO_DEL_LOPRI << IOAPIC_REDLO_DEL_SHIFT);
173 
174 	switch (mpstrig) {
175 	case MPS_INTTR_LEVEL:
176 		*redir |= IOAPIC_REDLO_LEVEL;
177 		break;
178 	case MPS_INTTR_DEF:
179 	case MPS_INTTR_EDGE:
180 		*redir &= ~IOAPIC_REDLO_LEVEL;
181 		break;
182 	default:
183 		panic("unknown MPS interrupt trigger %d", mpstrig);
184 	}
185 }
186 
187 static u_int8_t lapic_map[256];
188 
189 void
190 acpimadt_attach(struct device *parent, struct device *self, void *aux)
191 {
192 	struct acpi_softc *acpi_sc = (struct acpi_softc *)parent;
193 	struct device *mainbus = parent->dv_parent->dv_parent;
194 	struct acpi_attach_args *aaa = aux;
195 	struct acpi_madt *madt = (struct acpi_madt *)aaa->aaa_table;
196 	caddr_t addr = (caddr_t)(madt + 1);
197 	struct aml_value arg;
198 	struct mp_intr_map *map;
199 	struct ioapic_softc *apic;
200 	int nlapic_nmis = 0;
201 	int pin;
202 
203 	/* Do some sanity checks before committing to run in APIC mode. */
204 	if (!acpimadt_validate(madt)) {
205 		printf(": invalid, skipping\n");
206 		return;
207 	}
208 
209 	printf(" addr 0x%x", madt->local_apic_address);
210 	if (madt->flags & ACPI_APIC_PCAT_COMPAT)
211 		printf(": PC-AT compat");
212 	printf("\n");
213 
214 	/* Tell the BIOS we will be using APIC mode. */
215 	memset(&arg, 0, sizeof(arg));
216 	arg.type = AML_OBJTYPE_INTEGER;
217 	arg.v_integer = 1;
218 
219 	if (aml_evalname(acpi_sc, NULL, "\\_PIC", 1, &arg, NULL) != 0)
220 		return;
221 
222 	mp_busses = acpimadt_busses;
223 	mp_nbusses = nitems(acpimadt_busses);
224 	mp_isa_bus = &acpimadt_isa_bus;
225 
226 	lapic_boot_init(madt->local_apic_address);
227 
228 	/* 1st pass, get CPUs and IOAPICs */
229 	while (addr < (caddr_t)madt + madt->hdr.length) {
230 		union acpi_madt_entry *entry = (union acpi_madt_entry *)addr;
231 		struct cpu_attach_args caa;
232 		struct apic_attach_args aaa;
233 
234 		switch (entry->madt_lapic.apic_type) {
235 		case ACPI_MADT_LAPIC:
236 			dprintf("%s: LAPIC: acpi_proc_id %x, apic_id %x, flags 0x%x\n",
237 			    self->dv_xname, entry->madt_lapic.acpi_proc_id,
238 			    entry->madt_lapic.apic_id,
239 			    entry->madt_lapic.flags);
240 
241 			lapic_map[entry->madt_lapic.acpi_proc_id] =
242 			    entry->madt_lapic.apic_id;
243 			acpi_lapic_flags[entry->madt_lapic.acpi_proc_id] =
244 			    entry->madt_lapic.flags;
245 
246 			if ((entry->madt_lapic.flags & ACPI_PROC_ENABLE) == 0)
247 				break;
248 
249 			memset(&caa, 0, sizeof(struct cpu_attach_args));
250 			if (lapic_cpu_number() == entry->madt_lapic.apic_id)
251 				caa.cpu_role = CPU_ROLE_BP;
252 			else {
253 				caa.cpu_role = CPU_ROLE_AP;
254 				ncpusfound++;
255 			}
256 			caa.caa_name = "cpu";
257 			caa.cpu_number = entry->madt_lapic.apic_id;
258 #ifdef MULTIPROCESSOR
259 			caa.cpu_func = &mp_cpu_funcs;
260 #endif
261 #ifdef __i386__
262 			/*
263 			 * XXX utterly wrong.  These are the
264 			 * cpu_feature/cpu_id from the BSP cpu, now
265 			 * being given to another cpu.  This is
266 			 * bullshit.
267 			 */
268 			extern int cpu_id, cpu_feature;
269 			caa.cpu_signature = cpu_id;
270 			caa.feature_flags = cpu_feature;
271 #endif
272 
273 			config_found(mainbus, &caa, acpimadt_print);
274 			break;
275 		case ACPI_MADT_IOAPIC:
276 			dprintf("%s: IOAPIC: acpi_ioapic_id %x, address 0x%x, global_int_base 0x%x\n",
277 			    self->dv_xname, entry->madt_ioapic.acpi_ioapic_id,
278 			    entry->madt_ioapic.address,
279 			    entry->madt_ioapic.global_int_base);
280 
281 			memset(&aaa, 0, sizeof(struct apic_attach_args));
282 			aaa.aaa_name = "ioapic";
283 			aaa.apic_id = entry->madt_ioapic.acpi_ioapic_id;
284 			aaa.apic_address = entry->madt_ioapic.address;
285 			aaa.apic_vecbase = entry->madt_ioapic.global_int_base;
286 
287 			config_found(mainbus, &aaa, acpimadt_print);
288 			break;
289 		case ACPI_MADT_LAPIC_NMI:
290 			nlapic_nmis++;
291 			break;
292 		}
293 		addr += entry->madt_lapic.length;
294 	}
295 
296 	mp_intrs = malloc(nlapic_nmis * sizeof (struct mp_intr_map), M_DEVBUF, M_NOWAIT);
297 	if (mp_intrs == NULL)
298 		return;
299 
300 	/* 2nd pass, get interrupt overrides */
301 	addr = (caddr_t)(madt + 1);
302 	while (addr < (caddr_t)madt + madt->hdr.length) {
303 		union acpi_madt_entry *entry = (union acpi_madt_entry *)addr;
304 
305 		switch (entry->madt_lapic.apic_type) {
306 		case ACPI_MADT_LAPIC:
307 		case ACPI_MADT_IOAPIC:
308 			break;
309 
310 		case ACPI_MADT_OVERRIDE:
311 			dprintf("%s: OVERRIDE: bus %x, source %x, global_int %x, flags %x\n",
312 			    self->dv_xname, entry->madt_override.bus,
313 			    entry->madt_override.source,
314 			    entry->madt_override.global_int,
315 			    entry->madt_override.flags);
316 
317 			pin = entry->madt_override.global_int;
318 			apic = ioapic_find_bybase(pin);
319 
320 			map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT | M_ZERO);
321 			if (map == NULL)
322 				return;
323 
324 			map->ioapic = apic;
325 			map->ioapic_pin = pin - apic->sc_apic_vecbase;
326 			map->bus_pin = entry->madt_override.source;
327 			map->flags = entry->madt_override.flags;
328 
329 			acpimadt_cfg_intr(entry->madt_override.flags, &map->redir);
330 
331 			map->ioapic_ih = APIC_INT_VIA_APIC |
332 			    ((apic->sc_apicid << APIC_INT_APIC_SHIFT) |
333 			    (pin << APIC_INT_PIN_SHIFT));
334 
335 			apic->sc_pins[pin].ip_map = map;
336 
337 			map->next = mp_isa_bus->mb_intrs;
338 			mp_isa_bus->mb_intrs = map;
339 			break;
340 
341 		case ACPI_MADT_LAPIC_NMI:
342 			dprintf("%s: LAPIC_NMI: acpi_proc_id %x, local_apic_lint %x, flags %x\n",
343 			    self->dv_xname, entry->madt_lapic_nmi.acpi_proc_id,
344 			    entry->madt_lapic_nmi.local_apic_lint,
345 			    entry->madt_lapic_nmi.flags);
346 
347 			pin = entry->madt_lapic_nmi.local_apic_lint;
348 
349 			map = &mp_intrs[mp_nintrs++];
350 			memset(map, 0, sizeof *map);
351 			map->cpu_id = lapic_map[entry->madt_lapic_nmi.acpi_proc_id];
352 			map->ioapic_pin = pin;
353 			map->flags = entry->madt_lapic_nmi.flags;
354 
355 			acpimadt_cfg_intr(entry->madt_lapic_nmi.flags, &map->redir);
356 			map->redir &= ~IOAPIC_REDLO_DEL_MASK;
357 			map->redir |= (IOAPIC_REDLO_DEL_NMI << IOAPIC_REDLO_DEL_SHIFT);
358 			break;
359 
360 		case ACPI_MADT_X2APIC:
361 		case ACPI_MADT_X2APIC_NMI:
362 			break;
363 
364 		default:
365 			printf("%s: unknown apic structure type %x\n",
366 			    self->dv_xname, entry->madt_lapic.apic_type);
367 		}
368 
369 		addr += entry->madt_lapic.length;
370 	}
371 
372 	/*
373 	 * ISA interrupts are supposed to be identity mapped unless
374 	 * there is an override, in which case we will already have a
375 	 * mapping for the interrupt.
376 	 */
377 	for (pin = 0; pin < ICU_LEN; pin++) {
378 		/* Skip if we already have a mapping for this interrupt. */
379 		for (map = mp_isa_bus->mb_intrs; map != NULL; map = map->next)
380 			if (map->bus_pin == pin)
381 				break;
382 		if (map != NULL)
383 			continue;
384 
385 		apic = ioapic_find_bybase(pin);
386 
387 		map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT | M_ZERO);
388 		if (map == NULL)
389 			return;
390 
391 		map->ioapic = apic;
392 		map->ioapic_pin = pin;
393 		map->bus_pin = pin;
394 		map->redir = (IOAPIC_REDLO_DEL_LOPRI << IOAPIC_REDLO_DEL_SHIFT);
395 
396 		map->ioapic_ih = APIC_INT_VIA_APIC |
397 		    ((apic->sc_apicid << APIC_INT_APIC_SHIFT) |
398 		    (pin << APIC_INT_PIN_SHIFT));
399 
400 		apic->sc_pins[pin].ip_map = map;
401 
402 		map->next = mp_isa_bus->mb_intrs;
403 		mp_isa_bus->mb_intrs = map;
404 	}
405 }
406 
407 int
408 acpimadt_print(void *aux, const char *pnp)
409 {
410 	struct apic_attach_args *aaa = aux;
411 
412 	if (pnp)
413 		printf("%s at %s:", aaa->aaa_name, pnp);
414 
415 	return (UNCONF);
416 }
417