acpi_machdep.c revision 1.25 1 /* $NetBSD: acpi_machdep.c,v 1.25 2019/03/09 10:04:41 kre Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Machine-dependent routines for ACPICA.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.25 2019/03/09 10:04:41 kre Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/bus.h>
48 #include <sys/cpu.h>
49 #include <sys/device.h>
50
51 #include <uvm/uvm_extern.h>
52
53 #include <machine/cpufunc.h>
54 #include <machine/bootinfo.h>
55 #include <machine/autoconf.h>
56
57 #include <dev/acpi/acpica.h>
58 #include <dev/acpi/acpivar.h>
59 #include <dev/acpi/acpi_mcfg.h>
60
61 #include <machine/acpi_machdep.h>
62 #include <machine/mpbiosvar.h>
63 #include <machine/mpacpi.h>
64 #include <machine/i82093reg.h>
65 #include <machine/i82093var.h>
66 #include <machine/pic.h>
67
68 #include <x86/efi.h>
69
70 #include <dev/pci/pcivar.h>
71
72 #include <dev/isa/isareg.h>
73 #include <dev/isa/isavar.h>
74
75 #include "ioapic.h"
76
77 #include "acpica.h"
78 #include "opt_mpbios.h"
79 #include "opt_acpi.h"
80 #include "opt_vga.h"
81
82 /*
83 * Default VBIOS reset method for non-HW accelerated VGA drivers.
84 */
85 #ifdef VGA_POST
86 # define VBIOS_RESET_DEFAULT 2
87 #else
88 # define VBIOS_RESET_DEFAULT 1
89 #endif
90
91 ACPI_STATUS
92 acpi_md_OsInitialize(void)
93 {
94 return AE_OK;
95 }
96
97 ACPI_PHYSICAL_ADDRESS
98 acpi_md_OsGetRootPointer(void)
99 {
100 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
101 ACPI_STATUS Status;
102
103 #ifndef XENPV
104 /* If EFI is available, attempt to use it to locate the ACPI table. */
105 if (efi_probe()) {
106 PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI20);
107 if (!PhysicalAddress)
108 PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI10);
109 if (PhysicalAddress)
110 return PhysicalAddress;
111 }
112
113 #endif
114 Status = AcpiFindRootPointer(&PhysicalAddress);
115 if (ACPI_FAILURE(Status))
116 PhysicalAddress = 0;
117
118 return PhysicalAddress;
119 }
120
121 struct acpi_md_override {
122 int irq;
123 int pin;
124 int flags;
125 };
126
127 #if NIOAPIC > 0
128 static ACPI_STATUS
129 acpi_md_findoverride(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
130 {
131 ACPI_MADT_INTERRUPT_OVERRIDE *iop;
132 struct acpi_md_override *ovrp;
133
134 if (hdrp->Type != ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
135 return AE_OK;
136 }
137
138 iop = (void *)hdrp;
139 ovrp = aux;
140 if (iop->SourceIrq == ovrp->irq) {
141 ovrp->pin = iop->GlobalIrq;
142 ovrp->flags = iop->IntiFlags;
143 }
144 return AE_OK;
145 }
146 #endif
147
148 ACPI_STATUS
149 acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
150 ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep,
151 const char *xname)
152 {
153 void *ih;
154
155 ih = acpi_md_intr_establish(InterruptNumber, IPL_TTY, IST_LEVEL,
156 (int (*)(void *))ServiceRoutine, Context, false, xname);
157 if (ih == NULL)
158 return AE_NO_MEMORY;
159
160 *cookiep = ih;
161
162 return AE_OK;
163 }
164
165 void
166 acpi_md_OsRemoveInterruptHandler(void *cookie)
167 {
168 intr_disestablish(cookie);
169 }
170
171 void *
172 acpi_md_intr_establish(uint32_t InterruptNumber, int ipl, int type,
173 int (*handler)(void *), void *arg, bool mpsafe, const char *xname)
174 {
175 void *ih;
176 struct pic *pic;
177 int irq, pin;
178 #if NIOAPIC > 0
179 struct ioapic_softc *sc;
180 struct acpi_md_override ovr;
181 struct mp_intr_map tmpmap, *mip, **mipp = NULL;
182 int redir, mpflags;
183
184 /*
185 * ACPI interrupts default to level-triggered active-low.
186 */
187
188 mpflags = (MPS_INTTR_LEVEL << 2) | MPS_INTPO_ACTLO;
189 redir = IOAPIC_REDLO_LEVEL | IOAPIC_REDLO_ACTLO;
190
191
192 /*
193 * Apply any MADT override setting.
194 */
195
196 ovr.irq = InterruptNumber;
197 ovr.pin = -1;
198 if (acpi_madt_map() == AE_OK) {
199 acpi_madt_walk(acpi_md_findoverride, &ovr);
200 acpi_madt_unmap();
201 } else {
202 aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
203 }
204
205 if (ovr.pin != -1) {
206 bool sci = InterruptNumber == AcpiGbl_FADT.SciInterrupt;
207 int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
208 int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;
209
210 InterruptNumber = ovr.pin;
211 if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
212 (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
213 mpflags &= ~MPS_INTPO_ACTLO;
214 mpflags |= MPS_INTPO_ACTHI;
215 redir &= ~IOAPIC_REDLO_ACTLO;
216 }
217 if (trigger == ACPI_MADT_TRIGGER_EDGE ||
218 (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
219 type = IST_EDGE;
220 mpflags &= ~(MPS_INTTR_LEVEL << 2);
221 mpflags |= (MPS_INTTR_EDGE << 2);
222 redir &= ~IOAPIC_REDLO_LEVEL;
223 }
224 }
225
226 /*
227 * If the interrupt is handled via IOAPIC, update the map.
228 * If the map isn't set up yet, install a temporary one.
229 */
230
231 sc = ioapic_find_bybase(InterruptNumber);
232 if (sc != NULL) {
233 pic = &sc->sc_pic;
234
235 if (pic->pic_type == PIC_IOAPIC) {
236 pin = (int)InterruptNumber - pic->pic_vecbase;
237 irq = -1;
238 } else {
239 irq = pin = (int)InterruptNumber;
240 }
241
242 mip = sc->sc_pins[pin].ip_map;
243 if (mip) {
244 mip->flags &= ~0xf;
245 mip->flags |= mpflags;
246 mip->redir &= ~(IOAPIC_REDLO_LEVEL |
247 IOAPIC_REDLO_ACTLO);
248 mip->redir |= redir;
249 } else {
250 mipp = &sc->sc_pins[pin].ip_map;
251 *mipp = &tmpmap;
252 tmpmap.redir = redir;
253 tmpmap.flags = mpflags;
254 }
255 } else
256 #endif
257 {
258 pic = &i8259_pic;
259 irq = pin = (int)InterruptNumber;
260 }
261
262 ih = intr_establish_xname(irq, pic, pin, type, ipl,
263 handler, arg, mpsafe, xname);
264
265 #if NIOAPIC > 0
266 if (mipp) {
267 *mipp = NULL;
268 }
269 #endif
270
271 return ih;
272 }
273
274 void
275 acpi_md_intr_disestablish(void *ih)
276 {
277 intr_disestablish(ih);
278 }
279
280 ACPI_STATUS
281 acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
282 uint32_t Length, void **LogicalAddress)
283 {
284 int rv;
285
286 rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
287 Length, 0, (bus_space_handle_t *)LogicalAddress);
288
289 return (rv != 0) ? AE_NO_MEMORY : AE_OK;
290 }
291
292 void
293 acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
294 {
295 (void) _x86_memio_unmap(x86_bus_space_mem,
296 (bus_space_handle_t)LogicalAddress, Length, NULL);
297 }
298
299 ACPI_STATUS
300 acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
301 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
302 {
303 paddr_t pa;
304
305 if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
306 *PhysicalAddress = pa;
307 return AE_OK;
308 }
309
310 return AE_ERROR;
311 }
312
313 BOOLEAN
314 acpi_md_OsReadable(void *Pointer, uint32_t Length)
315 {
316 BOOLEAN rv = TRUE;
317 vaddr_t sva, eva;
318 pt_entry_t *pte;
319
320 sva = trunc_page((vaddr_t) Pointer);
321 eva = round_page((vaddr_t) Pointer + Length);
322
323 if (sva < VM_MIN_KERNEL_ADDRESS)
324 return FALSE;
325
326 for (; sva < eva; sva += PAGE_SIZE) {
327 pte = kvtopte(sva);
328 if ((*pte & PTE_P) == 0) {
329 rv = FALSE;
330 break;
331 }
332 }
333
334 return rv;
335 }
336
337 BOOLEAN
338 acpi_md_OsWritable(void *Pointer, uint32_t Length)
339 {
340 BOOLEAN rv = TRUE;
341 vaddr_t sva, eva;
342 pt_entry_t *pte;
343
344 sva = trunc_page((vaddr_t) Pointer);
345 eva = round_page((vaddr_t) Pointer + Length);
346
347 if (sva < VM_MIN_KERNEL_ADDRESS)
348 return FALSE;
349
350 for (; sva < eva; sva += PAGE_SIZE) {
351 pte = kvtopte(sva);
352 if ((*pte & (PTE_P|PTE_W)) != (PTE_P|PTE_W)) {
353 rv = FALSE;
354 break;
355 }
356 }
357
358 return rv;
359 }
360
361 void
362 acpi_md_OsDisableInterrupt(void)
363 {
364 x86_disable_intr();
365 }
366
367 void
368 acpi_md_OsEnableInterrupt(void)
369 {
370 x86_enable_intr();
371 }
372
373 uint32_t
374 acpi_md_ncpus(void)
375 {
376 return kcpuset_countset(kcpuset_attached);
377 }
378
379 static bool
380 acpi_md_mcfg_validate(uint64_t addr, int bus_start, int *bus_end)
381 {
382 struct btinfo_memmap *bim;
383 uint64_t size, mapaddr, mapsize;
384 uint32_t type;
385 int i, n;
386
387 #ifndef XENPV
388 if (lookup_bootinfo(BTINFO_EFIMEMMAP) != NULL)
389 bim = efi_get_e820memmap();
390 else
391 #endif
392 bim = lookup_bootinfo(BTINFO_MEMMAP);
393 if (bim == NULL)
394 return false;
395
396 size = *bus_end - bus_start + 1;
397 size *= ACPIMCFG_SIZE_PER_BUS;
398 for (i = 0; i < bim->num; i++) {
399 mapaddr = bim->entry[i].addr;
400 mapsize = bim->entry[i].size;
401 type = bim->entry[i].type;
402
403 aprint_debug("MCFG: MEMMAP: 0x%016" PRIx64
404 "-0x%016" PRIx64 ", size=0x%016" PRIx64
405 ", type=%d(%s)\n",
406 mapaddr, mapaddr + mapsize - 1, mapsize, type,
407 (type == BIM_Memory) ? "Memory" :
408 (type == BIM_Reserved) ? "Reserved" :
409 (type == BIM_ACPI) ? "ACPI" :
410 (type == BIM_NVS) ? "NVS" :
411 (type == BIM_PMEM) ? "Persistent" :
412 (type == BIM_PRAM) ? "Persistent (Legacy)" :
413 "unknown");
414
415 switch (type) {
416 case BIM_ACPI:
417 case BIM_Reserved:
418 if (addr < mapaddr || addr >= mapaddr + mapsize)
419 break;
420
421 /* full map */
422 if (addr + size <= mapaddr + mapsize)
423 return true;
424
425 /* partial map */
426 n = (mapsize - (addr - mapaddr)) /
427 ACPIMCFG_SIZE_PER_BUS;
428 /* bus_start == bus_end is not allowed. */
429 if (n > 1) {
430 *bus_end = bus_start + n - 1;
431 return true;
432 }
433 aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64
434 ": invalid size: request 0x%016" PRIx64 ", "
435 "actual 0x%016" PRIx64 "\n",
436 bus_start, *bus_end, addr, size, mapsize);
437 break;
438 }
439 }
440 aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64 ": "
441 "no valid region\n", bus_start, *bus_end, addr);
442 return false;
443 }
444
445 static uint32_t
446 acpi_md_mcfg_read(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr)
447 {
448 vaddr_t va = bsh + addr;
449 uint32_t data = (uint32_t) -1;
450
451 KASSERT(bst == x86_bus_space_mem);
452
453 __asm("movl %1, %0" : "=a" (data) : "m" (*(volatile uint32_t *)va));
454
455 return data;
456 }
457
458 static void
459 acpi_md_mcfg_write(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr,
460 uint32_t data)
461 {
462 vaddr_t va = bsh + addr;
463
464 KASSERT(bst == x86_bus_space_mem);
465
466 __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va) : "a" (data));
467 }
468
469 static const struct acpimcfg_ops acpi_md_mcfg_ops = {
470 .ao_validate = acpi_md_mcfg_validate,
471
472 .ao_read = acpi_md_mcfg_read,
473 .ao_write = acpi_md_mcfg_write,
474 };
475
476 void
477 acpi_md_callback(struct acpi_softc *sc)
478 {
479 #ifdef MPBIOS
480 if (!mpbios_scanned)
481 #endif
482 mpacpi_find_interrupts(sc);
483
484 #ifndef XENPV
485 acpi_md_sleep_init();
486 #endif
487
488 acpimcfg_init(x86_bus_space_mem, &acpi_md_mcfg_ops);
489 }
490
491 #ifndef XENPV
492 void
493 device_acpi_register(device_t dev, void *aux)
494 {
495 device_t parent;
496 bool device_is_vga, device_is_pci, device_is_isa;
497
498 parent = device_parent(dev);
499 if (parent == NULL)
500 return;
501
502 device_is_vga = device_is_a(dev, "vga") || device_is_a(dev, "genfb");
503 device_is_pci = device_is_a(parent, "pci");
504 device_is_isa = device_is_a(parent, "isa");
505
506 if (device_is_vga && (device_is_pci || device_is_isa)) {
507 extern int acpi_md_vbios_reset;
508
509 acpi_md_vbios_reset = VBIOS_RESET_DEFAULT;
510 }
511 }
512 #endif
513