acpi_machdep.c revision 1.24 1 /* $NetBSD: acpi_machdep.c,v 1.24 2019/03/09 08:42:25 maxv 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.24 2019/03/09 08:42:25 maxv 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 #if NIOAPIC > 0
178 struct ioapic_softc *sc;
179 struct acpi_md_override ovr;
180 struct mp_intr_map tmpmap, *mip, **mipp = NULL;
181 #endif
182 int irq, pin, 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 #if NIOAPIC > 0
192
193 /*
194 * Apply any MADT override setting.
195 */
196
197 ovr.irq = InterruptNumber;
198 ovr.pin = -1;
199 if (acpi_madt_map() == AE_OK) {
200 acpi_madt_walk(acpi_md_findoverride, &ovr);
201 acpi_madt_unmap();
202 } else {
203 aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
204 }
205
206 if (ovr.pin != -1) {
207 bool sci = InterruptNumber == AcpiGbl_FADT.SciInterrupt;
208 int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
209 int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;
210
211 InterruptNumber = ovr.pin;
212 if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
213 (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
214 mpflags &= ~MPS_INTPO_ACTLO;
215 mpflags |= MPS_INTPO_ACTHI;
216 redir &= ~IOAPIC_REDLO_ACTLO;
217 }
218 if (trigger == ACPI_MADT_TRIGGER_EDGE ||
219 (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
220 type = IST_EDGE;
221 mpflags &= ~(MPS_INTTR_LEVEL << 2);
222 mpflags |= (MPS_INTTR_EDGE << 2);
223 redir &= ~IOAPIC_REDLO_LEVEL;
224 }
225 }
226
227 /*
228 * If the interrupt is handled via IOAPIC, update the map.
229 * If the map isn't set up yet, install a temporary one.
230 */
231
232 sc = ioapic_find_bybase(InterruptNumber);
233 if (sc != NULL) {
234 pic = &sc->sc_pic;
235
236 if (pic->pic_type == PIC_IOAPIC) {
237 pin = (int)InterruptNumber - pic->pic_vecbase;
238 irq = -1;
239 } else {
240 irq = pin = (int)InterruptNumber;
241 }
242
243 mip = sc->sc_pins[pin].ip_map;
244 if (mip) {
245 mip->flags &= ~0xf;
246 mip->flags |= mpflags;
247 mip->redir &= ~(IOAPIC_REDLO_LEVEL |
248 IOAPIC_REDLO_ACTLO);
249 mip->redir |= redir;
250 } else {
251 mipp = &sc->sc_pins[pin].ip_map;
252 *mipp = &tmpmap;
253 tmpmap.redir = redir;
254 tmpmap.flags = mpflags;
255 }
256 } else
257 #endif
258 {
259 pic = &i8259_pic;
260 irq = pin = (int)InterruptNumber;
261 }
262
263 ih = intr_establish_xname(irq, pic, pin, type, ipl,
264 handler, arg, mpsafe, xname);
265
266 #if NIOAPIC > 0
267 if (mipp) {
268 *mipp = NULL;
269 }
270 #endif
271
272 return ih;
273 }
274
275 void
276 acpi_md_intr_disestablish(void *ih)
277 {
278 intr_disestablish(ih);
279 }
280
281 ACPI_STATUS
282 acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
283 uint32_t Length, void **LogicalAddress)
284 {
285 int rv;
286
287 rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
288 Length, 0, (bus_space_handle_t *)LogicalAddress);
289
290 return (rv != 0) ? AE_NO_MEMORY : AE_OK;
291 }
292
293 void
294 acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
295 {
296 (void) _x86_memio_unmap(x86_bus_space_mem,
297 (bus_space_handle_t)LogicalAddress, Length, NULL);
298 }
299
300 ACPI_STATUS
301 acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
302 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
303 {
304 paddr_t pa;
305
306 if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
307 *PhysicalAddress = pa;
308 return AE_OK;
309 }
310
311 return AE_ERROR;
312 }
313
314 BOOLEAN
315 acpi_md_OsReadable(void *Pointer, uint32_t Length)
316 {
317 BOOLEAN rv = TRUE;
318 vaddr_t sva, eva;
319 pt_entry_t *pte;
320
321 sva = trunc_page((vaddr_t) Pointer);
322 eva = round_page((vaddr_t) Pointer + Length);
323
324 if (sva < VM_MIN_KERNEL_ADDRESS)
325 return FALSE;
326
327 for (; sva < eva; sva += PAGE_SIZE) {
328 pte = kvtopte(sva);
329 if ((*pte & PTE_P) == 0) {
330 rv = FALSE;
331 break;
332 }
333 }
334
335 return rv;
336 }
337
338 BOOLEAN
339 acpi_md_OsWritable(void *Pointer, uint32_t Length)
340 {
341 BOOLEAN rv = TRUE;
342 vaddr_t sva, eva;
343 pt_entry_t *pte;
344
345 sva = trunc_page((vaddr_t) Pointer);
346 eva = round_page((vaddr_t) Pointer + Length);
347
348 if (sva < VM_MIN_KERNEL_ADDRESS)
349 return FALSE;
350
351 for (; sva < eva; sva += PAGE_SIZE) {
352 pte = kvtopte(sva);
353 if ((*pte & (PTE_P|PTE_W)) != (PTE_P|PTE_W)) {
354 rv = FALSE;
355 break;
356 }
357 }
358
359 return rv;
360 }
361
362 void
363 acpi_md_OsDisableInterrupt(void)
364 {
365 x86_disable_intr();
366 }
367
368 void
369 acpi_md_OsEnableInterrupt(void)
370 {
371 x86_enable_intr();
372 }
373
374 uint32_t
375 acpi_md_ncpus(void)
376 {
377 return kcpuset_countset(kcpuset_attached);
378 }
379
380 static bool
381 acpi_md_mcfg_validate(uint64_t addr, int bus_start, int *bus_end)
382 {
383 struct btinfo_memmap *bim;
384 uint64_t size, mapaddr, mapsize;
385 uint32_t type;
386 int i, n;
387
388 #ifndef XENPV
389 if (lookup_bootinfo(BTINFO_EFIMEMMAP) != NULL)
390 bim = efi_get_e820memmap();
391 else
392 #endif
393 bim = lookup_bootinfo(BTINFO_MEMMAP);
394 if (bim == NULL)
395 return false;
396
397 size = *bus_end - bus_start + 1;
398 size *= ACPIMCFG_SIZE_PER_BUS;
399 for (i = 0; i < bim->num; i++) {
400 mapaddr = bim->entry[i].addr;
401 mapsize = bim->entry[i].size;
402 type = bim->entry[i].type;
403
404 aprint_debug("MCFG: MEMMAP: 0x%016" PRIx64
405 "-0x%016" PRIx64 ", size=0x%016" PRIx64
406 ", type=%d(%s)\n",
407 mapaddr, mapaddr + mapsize - 1, mapsize, type,
408 (type == BIM_Memory) ? "Memory" :
409 (type == BIM_Reserved) ? "Reserved" :
410 (type == BIM_ACPI) ? "ACPI" :
411 (type == BIM_NVS) ? "NVS" :
412 (type == BIM_PMEM) ? "Persistent" :
413 (type == BIM_PRAM) ? "Persistent (Legacy)" :
414 "unknown");
415
416 switch (type) {
417 case BIM_ACPI:
418 case BIM_Reserved:
419 if (addr < mapaddr || addr >= mapaddr + mapsize)
420 break;
421
422 /* full map */
423 if (addr + size <= mapaddr + mapsize)
424 return true;
425
426 /* partial map */
427 n = (mapsize - (addr - mapaddr)) /
428 ACPIMCFG_SIZE_PER_BUS;
429 /* bus_start == bus_end is not allowed. */
430 if (n > 1) {
431 *bus_end = bus_start + n - 1;
432 return true;
433 }
434 aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64
435 ": invalid size: request 0x%016" PRIx64 ", "
436 "actual 0x%016" PRIx64 "\n",
437 bus_start, *bus_end, addr, size, mapsize);
438 break;
439 }
440 }
441 aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64 ": "
442 "no valid region\n", bus_start, *bus_end, addr);
443 return false;
444 }
445
446 static uint32_t
447 acpi_md_mcfg_read(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr)
448 {
449 vaddr_t va = bsh + addr;
450 uint32_t data = (uint32_t) -1;
451
452 KASSERT(bst == x86_bus_space_mem);
453
454 __asm("movl %1, %0" : "=a" (data) : "m" (*(volatile uint32_t *)va));
455
456 return data;
457 }
458
459 static void
460 acpi_md_mcfg_write(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr,
461 uint32_t data)
462 {
463 vaddr_t va = bsh + addr;
464
465 KASSERT(bst == x86_bus_space_mem);
466
467 __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va) : "a" (data));
468 }
469
470 static const struct acpimcfg_ops acpi_md_mcfg_ops = {
471 .ao_validate = acpi_md_mcfg_validate,
472
473 .ao_read = acpi_md_mcfg_read,
474 .ao_write = acpi_md_mcfg_write,
475 };
476
477 void
478 acpi_md_callback(struct acpi_softc *sc)
479 {
480 #ifdef MPBIOS
481 if (!mpbios_scanned)
482 #endif
483 mpacpi_find_interrupts(sc);
484
485 #ifndef XENPV
486 acpi_md_sleep_init();
487 #endif
488
489 acpimcfg_init(x86_bus_space_mem, &acpi_md_mcfg_ops);
490 }
491
492 #ifndef XENPV
493 void
494 device_acpi_register(device_t dev, void *aux)
495 {
496 device_t parent;
497 bool device_is_vga, device_is_pci, device_is_isa;
498
499 parent = device_parent(dev);
500 if (parent == NULL)
501 return;
502
503 device_is_vga = device_is_a(dev, "vga") || device_is_a(dev, "genfb");
504 device_is_pci = device_is_a(parent, "pci");
505 device_is_isa = device_is_a(parent, "isa");
506
507 if (device_is_vga && (device_is_pci || device_is_isa)) {
508 extern int acpi_md_vbios_reset;
509
510 acpi_md_vbios_reset = VBIOS_RESET_DEFAULT;
511 }
512 }
513 #endif
514