1/* $NetBSD: acpi_table.c,v 1.2 2020/09/13 21:41:17 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill <jmcneill@invisible.ca>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * ACPI table functions for use in early boot before ACPICA can be safely
34 * initialized.
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: acpi_table.c,v 1.2 2020/09/13 21:41:17 jmcneill Exp $");
39
40#include <sys/param.h>
41#include <sys/bus.h>
42#include <sys/cpu.h>
43#include <sys/device.h>
44
45#include <uvm/uvm_extern.h>
46
47#include <dev/acpi/acpireg.h>
48#include <dev/acpi/acpivar.h>
49
50#include <machine/acpi_machdep.h>
51
52#include <arch/arm/acpi/acpi_table.h>
53
54ACPI_STATUS
55acpi_table_map(ACPI_PHYSICAL_ADDRESS pa, void **hdrp)
56{
57	ACPI_TABLE_HEADER *header;
58	ACPI_STATUS rv;
59	UINT32 length;
60
61	rv = acpi_md_OsMapMemory(pa, sizeof(*header), (void **)&header);
62	if (ACPI_FAILURE(rv))
63		return rv;
64	length = le32toh(header->Length);
65
66	acpi_md_OsUnmapMemory(header, sizeof(*header));
67
68	return acpi_md_OsMapMemory(pa, length, hdrp);
69}
70
71void
72acpi_table_unmap(ACPI_TABLE_HEADER *hdrp)
73{
74	acpi_md_OsUnmapMemory(hdrp, le32toh(hdrp->Length));
75}
76
77ACPI_STATUS
78acpi_table_find(const char *sig, void **hdrp)
79{
80	ACPI_TABLE_RSDP *rsdp;
81	ACPI_TABLE_XSDT *xsdt;
82	ACPI_TABLE_HEADER *header;
83	ACPI_PHYSICAL_ADDRESS pa;
84	ACPI_STATUS rv;
85
86	/* RDSP contains a pointer to the XSDT */
87	pa = 0;
88	rv = acpi_md_OsMapMemory(acpi_md_OsGetRootPointer(), sizeof(*rsdp), (void **)&rsdp);
89	if (ACPI_FAILURE(rv))
90		return rv;
91	if (memcmp(rsdp->Signature, ACPI_SIG_RSDP, sizeof(rsdp->Signature)) == 0)
92		pa = le64toh(rsdp->XsdtPhysicalAddress);
93	acpi_md_OsUnmapMemory(rsdp, sizeof(*rsdp));
94	if (pa == 0)
95		return AE_NOT_FOUND;
96
97	/* XSDT contains pointers to other tables */
98	rv = acpi_table_map(pa, (void **)&xsdt);
99	if (ACPI_FAILURE(rv))
100		return rv;
101	const u_int entries = (le32toh(xsdt->Header.Length) - sizeof(ACPI_TABLE_HEADER)) / ACPI_XSDT_ENTRY_SIZE;
102	for (u_int n = 0; n < entries; n++) {
103		rv = acpi_table_map(le64toh(xsdt->TableOffsetEntry[n]), (void **)&header);
104		if (ACPI_FAILURE(rv))
105			continue;
106		if (memcmp(header->Signature, sig, sizeof(header->Signature)) == 0) {
107			acpi_table_unmap((ACPI_TABLE_HEADER *)xsdt);
108
109			*hdrp = header;
110			return AE_OK;
111		}
112	}
113
114	/* Not found */
115	acpi_table_unmap((ACPI_TABLE_HEADER *)xsdt);
116	return AE_NOT_FOUND;
117}
118