Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* $NetBSD: acpi_machdep.c,v 1.35 2023/01/24 09:35:20 riastradh Exp $ */

/*
 * Copyright 2001 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Machine-dependent routines for ACPICA.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.35 2023/01/24 09:35:20 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>

#include <uvm/uvm_extern.h>

#include <machine/cpufunc.h>
#include <machine/bootinfo.h>
#include <machine/autoconf.h>

#include <dev/acpi/acpica.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_mcfg.h>

#include <machine/acpi_machdep.h>
#include <machine/mpbiosvar.h>
#include <machine/mpacpi.h>
#include <machine/i82093reg.h>
#include <machine/i82093var.h>
#include <machine/pic.h>
#include <machine/pmap_private.h>

#include <x86/efi.h>

#include <dev/pci/pcivar.h>

#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>

#include "ioapic.h"

#include "acpica.h"
#include "opt_mpbios.h"
#include "opt_acpi.h"
#include "opt_vga.h"

#ifdef XEN
#include <xen/hypervisor.h>
#endif

/*
 * Default VBIOS reset method for non-HW accelerated VGA drivers.
 */
#ifdef VGA_POST
# define VBIOS_RESET_DEFAULT	2
#else
# define VBIOS_RESET_DEFAULT	1
#endif

ACPI_STATUS
acpi_md_OsInitialize(void)
{
	return AE_OK;
}

ACPI_PHYSICAL_ADDRESS
acpi_md_OsGetRootPointer(void)
{
	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
	ACPI_STATUS Status;

#ifdef XENPV
	/*
	 * Obtain the ACPI RSDP from the hypervisor.
	 * This is the only way to go if Xen booted from EFI: the
	 * Extended BIOS Data Area (EBDA) is not mapped, and Xen
	 * does not pass an EFI SystemTable to the kernel.
	 */
        struct xen_platform_op op = {
                .cmd = XENPF_firmware_info,
                .u.firmware_info = {
                        .type = XEN_FW_EFI_INFO,
                        .index = XEN_FW_EFI_CONFIG_TABLE
                }
        };
        union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;

        if (HYPERVISOR_platform_op(&op) == 0) {
		struct efi_cfgtbl *ct;
		int i;

		ct = AcpiOsMapMemory(info->cfg.addr,
		    sizeof(*ct) * info->cfg.nent);

		for (i = 0; i < info->cfg.nent; i++) {
                	if (memcmp(&ct[i].ct_uuid,
			    &EFI_UUID_ACPI20, sizeof(EFI_UUID_ACPI20)) == 0) {
				PhysicalAddress = (ACPI_PHYSICAL_ADDRESS)
				    (uintptr_t)ct[i].ct_data;
				if (PhysicalAddress)
					goto out;

			}
		}

		for (i = 0; i < info->cfg.nent; i++) {
                	if (memcmp(&ct[i].ct_uuid,
			    &EFI_UUID_ACPI10, sizeof(EFI_UUID_ACPI10)) == 0) {
				PhysicalAddress = (ACPI_PHYSICAL_ADDRESS)
				    (uintptr_t)ct[i].ct_data;
				if (PhysicalAddress)
					goto out;

			}
		}
out:
		AcpiOsUnmapMemory(ct, sizeof(*ct) * info->cfg.nent);

		if (PhysicalAddress)
			return PhysicalAddress;
	}
#else
#ifdef XEN
	if (vm_guest == VM_GUEST_XENPVH) {
		PhysicalAddress = hvm_start_info->rsdp_paddr;
		if (PhysicalAddress)
			return PhysicalAddress;
	}
#endif
	/*
	 * Get the ACPI RSDP from EFI SystemTable. This works when the
	 * kernel was loaded from EFI bootloader.
	 */
	if (efi_probe()) {
		PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI20);
		if (!PhysicalAddress)
			PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI10);
		if (PhysicalAddress)
			return PhysicalAddress;
	}

#endif
	/*
	 * Find ACPI RSDP from Extended BIOS Data Area (EBDA). This
	 * works when the kernel was started from BIOS bootloader,
	 * or for Xen PV when Xen was started from BIOS bootloader.
	 */
	Status = AcpiFindRootPointer(&PhysicalAddress);
	if (ACPI_FAILURE(Status))
		PhysicalAddress = 0;

	return PhysicalAddress;
}

struct acpi_md_override {
	int irq;
	int pin;
	int flags;
};

#if NIOAPIC > 0
static ACPI_STATUS
acpi_md_findoverride(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
{
	ACPI_MADT_INTERRUPT_OVERRIDE *iop;
	struct acpi_md_override *ovrp;

	if (hdrp->Type != ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
		return AE_OK;
	}

	iop = (void *)hdrp;
	ovrp = aux;
	if (iop->SourceIrq == ovrp->irq) {
		ovrp->pin = iop->GlobalIrq;
		ovrp->flags = iop->IntiFlags;
	}
	return AE_OK;
}
#endif

ACPI_STATUS
acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
    ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep,
    const char *xname)
{
	void *ih;

	ih = acpi_md_intr_establish(InterruptNumber, IPL_TTY, IST_LEVEL,
	    (int (*)(void *))ServiceRoutine, Context, /*mpsafe*/true, xname);
	if (ih == NULL)
		return AE_NO_MEMORY;

	*cookiep = ih;

	return AE_OK;
}

void
acpi_md_OsRemoveInterruptHandler(void *cookie)
{
	intr_disestablish(cookie);
}

void *
acpi_md_intr_establish(uint32_t InterruptNumber, int ipl, int type,
    int (*handler)(void *), void *arg, bool mpsafe, const char *xname)
{
	void *ih;
	struct pic *pic;
	int irq = InterruptNumber, pin;
#if NIOAPIC > 0
	struct ioapic_softc *ioapic;
	struct acpi_md_override ovr;
	struct mp_intr_map tmpmap, *mip, **mipp = NULL;
	intr_handle_t mpih;
	int redir, mpflags;

	/*
	 * ACPI interrupts default to level-triggered active-low.
	 */

	mpflags = (MPS_INTTR_LEVEL << 2) | MPS_INTPO_ACTLO;
	redir = IOAPIC_REDLO_LEVEL | IOAPIC_REDLO_ACTLO;

	/*
	 * Apply any MADT override setting.
	 */

	ovr.irq = irq;
	ovr.pin = -1;
	if (acpi_madt_map() == AE_OK) {
		acpi_madt_walk(acpi_md_findoverride, &ovr);
		acpi_madt_unmap();
	} else {
		aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
	}

	if (ovr.pin != -1) {
		bool sci = irq == AcpiGbl_FADT.SciInterrupt;
		int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
		int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;

		irq = ovr.pin;
		if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
		    (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
			mpflags &= ~MPS_INTPO_ACTLO;
			mpflags |= MPS_INTPO_ACTHI;
			redir &= ~IOAPIC_REDLO_ACTLO;
		}
		if (trigger == ACPI_MADT_TRIGGER_EDGE ||
		    (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
			type = IST_EDGE;
			mpflags &= ~(MPS_INTTR_LEVEL << 2);
			mpflags |= (MPS_INTTR_EDGE << 2);
			redir &= ~IOAPIC_REDLO_LEVEL;
		}
	}

	pic = NULL;
	pin = irq;

	/*
	 * If the interrupt is handled via IOAPIC, update the map.
	 * If the map isn't set up yet, install a temporary one.
	 * Identify ISA & EISA interrupts
	 */
	if (mp_busses != NULL) {
		if (intr_find_mpmapping(mp_isa_bus, irq, &mpih) == 0 ||
		    intr_find_mpmapping(mp_eisa_bus, irq, &mpih) == 0) {
			if (!APIC_IRQ_ISLEGACY(mpih)) {
				pin = APIC_IRQ_PIN(mpih);
				ioapic = ioapic_find(APIC_IRQ_APIC(mpih));
				if (ioapic != NULL)
					pic = &ioapic->sc_pic;
			}
		}
	}

	if (pic == NULL) {
		/*
		 * If the interrupt is handled via IOAPIC, update the map.
		 * If the map isn't set up yet, install a temporary one.
		 */
		ioapic = ioapic_find_bybase(irq);
		if (ioapic != NULL) {
			pic = &ioapic->sc_pic;

			if (pic->pic_type == PIC_IOAPIC) {
				pin = irq - pic->pic_vecbase;
				irq = -1;
			} else {
				pin = irq;
			}

			mip = ioapic->sc_pins[pin].ip_map;
			if (mip) {
				mip->flags &= ~0xf;
				mip->flags |= mpflags;
				mip->redir &= ~(IOAPIC_REDLO_LEVEL |
						IOAPIC_REDLO_ACTLO);
				mip->redir |= redir;
			} else {
				mipp = &ioapic->sc_pins[pin].ip_map;
				*mipp = &tmpmap;
				tmpmap.redir = redir;
				tmpmap.flags = mpflags;
			}
		}
	}

	if (pic == NULL)
#endif
	{
		pic = &i8259_pic;
		pin = irq;
	}

	ih = intr_establish_xname(irq, pic, pin, type, ipl,
	    handler, arg, mpsafe, xname);

#if NIOAPIC > 0
	if (mipp) {
		*mipp = NULL;
	}
#endif

	return ih;
}

void
acpi_md_intr_mask(void *ih)
{
	intr_mask(ih);
}

void
acpi_md_intr_unmask(void *ih)
{
	intr_unmask(ih);
}

void
acpi_md_intr_disestablish(void *ih)
{
	intr_disestablish(ih);
}

ACPI_STATUS
acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
    uint32_t Length, void **LogicalAddress)
{
	int rv;

	rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
	    Length, 0, (bus_space_handle_t *)LogicalAddress);

	return (rv != 0) ? AE_NO_MEMORY : AE_OK;
}

void
acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
{
	(void) _x86_memio_unmap(x86_bus_space_mem,
	    (bus_space_handle_t)LogicalAddress, Length, NULL);
}

ACPI_STATUS
acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
    ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
{
	paddr_t pa;

	if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
		*PhysicalAddress = pa;
		return AE_OK;
	}

	return AE_ERROR;
}

BOOLEAN
acpi_md_OsReadable(void *Pointer, uint32_t Length)
{
	BOOLEAN rv = TRUE;
	vaddr_t sva, eva;
	pt_entry_t *pte;

	sva = trunc_page((vaddr_t) Pointer);
	eva = round_page((vaddr_t) Pointer + Length);

	if (sva < VM_MIN_KERNEL_ADDRESS)
		return FALSE;

	for (; sva < eva; sva += PAGE_SIZE) {
		pte = kvtopte(sva);
		if ((*pte & PTE_P) == 0) {
			rv = FALSE;
			break;
		}
	}

	return rv;
}

BOOLEAN
acpi_md_OsWritable(void *Pointer, uint32_t Length)
{
	BOOLEAN rv = TRUE;
	vaddr_t sva, eva;
	pt_entry_t *pte;

	sva = trunc_page((vaddr_t) Pointer);
	eva = round_page((vaddr_t) Pointer + Length);

	if (sva < VM_MIN_KERNEL_ADDRESS)
		return FALSE;

	for (; sva < eva; sva += PAGE_SIZE) {
		pte = kvtopte(sva);
		if ((*pte & (PTE_P|PTE_W)) != (PTE_P|PTE_W)) {
			rv = FALSE;
			break;
		}
	}

	return rv;
}

void
acpi_md_OsDisableInterrupt(void)
{
	x86_disable_intr();
}

void
acpi_md_OsEnableInterrupt(void)
{
	x86_enable_intr();
}

uint32_t
acpi_md_ncpus(void)
{
	return kcpuset_countset(kcpuset_attached);
}

static bool
acpi_md_mcfg_validate(uint64_t addr, int bus_start, int *bus_end)
{
	struct btinfo_memmap *bim;
	uint64_t size, mapaddr, mapsize;
	uint32_t type;
	int i, n;

#ifndef XENPV
	if (lookup_bootinfo(BTINFO_EFIMEMMAP) != NULL)
		bim = efi_get_e820memmap();
	else
#endif
		bim = lookup_bootinfo(BTINFO_MEMMAP);
	if (bim == NULL)
		return false;

	size = *bus_end - bus_start + 1;
	size *= ACPIMCFG_SIZE_PER_BUS;
	for (i = 0; i < bim->num; i++) {
		mapaddr = bim->entry[i].addr;
		mapsize = bim->entry[i].size;
		type = bim->entry[i].type;

		aprint_debug("MCFG: MEMMAP: 0x%016" PRIx64
		    "-0x%016" PRIx64 ", size=0x%016" PRIx64
		    ", type=%d(%s)\n",
		    mapaddr, mapaddr + mapsize - 1, mapsize, type,
		    (type == BIM_Memory) ?  "Memory" :
		    (type == BIM_Reserved) ?  "Reserved" :
		    (type == BIM_ACPI) ? "ACPI" :
		    (type == BIM_NVS) ? "NVS" :
		    (type == BIM_PMEM) ? "Persistent" :
		    (type == BIM_PRAM) ? "Persistent (Legacy)" :
		    "unknown");

		switch (type) {
		case BIM_ACPI:
		case BIM_Reserved:
			if (addr < mapaddr || addr >= mapaddr + mapsize)
				break;

			/* full map */
			if (addr + size <= mapaddr + mapsize)
				return true;

			/* partial map */
			n = (mapsize - (addr - mapaddr)) /
			    ACPIMCFG_SIZE_PER_BUS;
			/* bus_start == bus_end is not allowed. */
			if (n > 1) {
				*bus_end = bus_start + n - 1;
				return true;
			}
			aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64
			    ": invalid size: request 0x%016" PRIx64 ", "
			    "actual 0x%016" PRIx64 "\n",
			    bus_start, *bus_end, addr, size, mapsize);
			break;
		}
	}
	aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64 ": "
	    "no valid region\n", bus_start, *bus_end, addr);
	return false;
}

static uint32_t
acpi_md_mcfg_read(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr)
{
	vaddr_t va = bsh + addr;
	uint32_t data = (uint32_t) -1;

	KASSERT(bst == x86_bus_space_mem);

	__asm("movl %1, %0" : "=a" (data) : "m" (*(volatile uint32_t *)va));

	return data;
}

static void
acpi_md_mcfg_write(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr,
    uint32_t data)
{
	vaddr_t va = bsh + addr;

	KASSERT(bst == x86_bus_space_mem);

	__asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va) : "a" (data));
}

static const struct acpimcfg_ops acpi_md_mcfg_ops = {
	.ao_validate = acpi_md_mcfg_validate,

	.ao_read = acpi_md_mcfg_read,
	.ao_write = acpi_md_mcfg_write,
};

void
acpi_md_callback(struct acpi_softc *sc)
{
#ifdef MPBIOS
	if (!mpbios_scanned)
#endif
	mpacpi_find_interrupts(sc);

#ifndef XENPV
	acpi_md_sleep_init();
#endif

	acpimcfg_init(x86_bus_space_mem, &acpi_md_mcfg_ops);
}

#ifndef XENPV
void
device_acpi_register(device_t dev, void *aux)
{
	device_t parent;
	bool device_is_vga, device_is_pci, device_is_isa;

	parent = device_parent(dev);
	if (parent == NULL)
		return;

	device_is_vga = device_is_a(dev, "vga") || device_is_a(dev, "genfb");
	device_is_pci = device_is_a(parent, "pci");
	device_is_isa = device_is_a(parent, "isa");

	if (device_is_vga && (device_is_pci || device_is_isa)) {
		extern int acpi_md_vbios_reset;

		acpi_md_vbios_reset = VBIOS_RESET_DEFAULT;
	}
}
#endif