xref: /dragonfly/sys/platform/pc64/acpica/acpi_sdt.c (revision 9348a738)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 
39 #include <machine/pmap.h>
40 #include <machine/vmparam.h>
41 
42 #include <contrib/dev/acpica/source/include/acpi.h>
43 
44 #include "acpi_sdt_var.h"
45 
46 #define SDT_VPRINTF(fmt, arg...) \
47 do { \
48 	if (bootverbose) \
49 		kprintf("ACPI SDT: " fmt , ##arg); \
50 } while (0)
51 
52 typedef	vm_paddr_t		(*sdt_search_t)(vm_paddr_t, const uint8_t *);
53 
54 static const ACPI_TABLE_RSDP	*sdt_rsdp_search(const uint8_t *, int);
55 static vm_paddr_t		sdt_search_xsdt(vm_paddr_t, const uint8_t *);
56 static vm_paddr_t		sdt_search_rsdt(vm_paddr_t, const uint8_t *);
57 
58 extern u_long			ebda_addr;
59 
60 static sdt_search_t		sdt_search_func;
61 static vm_paddr_t		sdt_search_paddr;
62 
63 static void
64 sdt_probe(void)
65 {
66 	const ACPI_TABLE_RSDP *rsdp;
67 	vm_size_t mapsz;
68 	uint8_t *ptr;
69 	u_long rsdp_paddr;
70 
71 	if (kgetenv_ulong("hint.acpi.0.rsdp", &rsdp_paddr) == 1) {
72 		SDT_VPRINTF("RSDP in kenv hint\n");
73 		mapsz = PAGE_SIZE;
74 		ptr = pmap_mapdev(rsdp_paddr, mapsz);
75 		rsdp = (const ACPI_TABLE_RSDP *)ptr;
76 		goto found_rsdp;
77 	}
78 
79 	if (ebda_addr != 0) {
80 		mapsz = ACPI_EBDA_WINDOW_SIZE;
81 		ptr = pmap_mapdev(ebda_addr, mapsz);
82 
83 		rsdp = sdt_rsdp_search(ptr, mapsz);
84 		if (rsdp == NULL) {
85 			SDT_VPRINTF("RSDP not in EBDA\n");
86 			pmap_unmapdev((vm_offset_t)ptr, mapsz);
87 
88 			ptr = NULL;
89 			mapsz = 0;
90 		} else {
91 			SDT_VPRINTF("RSDP in EBDA\n");
92 			goto found_rsdp;
93 		}
94 	}
95 
96 	mapsz = ACPI_HI_RSDP_WINDOW_SIZE;
97 	ptr = pmap_mapdev(ACPI_HI_RSDP_WINDOW_BASE, mapsz);
98 
99 	rsdp = sdt_rsdp_search(ptr, mapsz);
100 	if (rsdp == NULL) {
101 		kprintf("sdt_probe: no RSDP\n");
102 		pmap_unmapdev((vm_offset_t)ptr, mapsz);
103 		return;
104 	} else {
105 		SDT_VPRINTF("RSDP in BIOS mem\n");
106 	}
107 
108 found_rsdp:
109 	if (rsdp->Revision != 2 /* || AcpiGbl_DoNotUseXsdt */) {
110 		sdt_search_func = sdt_search_rsdt;
111 		sdt_search_paddr = rsdp->RsdtPhysicalAddress;
112 	} else {
113 		sdt_search_func = sdt_search_xsdt;
114 		sdt_search_paddr = rsdp->XsdtPhysicalAddress;
115 	}
116 	pmap_unmapdev((vm_offset_t)ptr, mapsz);
117 }
118 SYSINIT(sdt_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, sdt_probe, 0);
119 
120 static const ACPI_TABLE_RSDP *
121 sdt_rsdp_search(const uint8_t *target, int size)
122 {
123 	const ACPI_TABLE_RSDP *rsdp;
124 	int i;
125 
126 	KKASSERT(size > sizeof(*rsdp));
127 
128 	for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_SCAN_STEP) {
129 		rsdp = (const ACPI_TABLE_RSDP *)&target[i];
130 		if (memcmp(rsdp->Signature, ACPI_SIG_RSDP,
131 			   sizeof(rsdp->Signature)) == 0)
132 			return rsdp;
133 	}
134 	return NULL;
135 }
136 
137 void *
138 sdt_sdth_map(vm_paddr_t paddr)
139 {
140 	ACPI_TABLE_HEADER *sdth;
141 	vm_size_t mapsz;
142 
143 	sdth = pmap_mapdev(paddr, sizeof(*sdth));
144 	mapsz = sdth->Length;
145 	pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
146 
147 	if (mapsz < sizeof(*sdth))
148 		return NULL;
149 
150 	return pmap_mapdev(paddr, mapsz);
151 }
152 
153 void
154 sdt_sdth_unmap(ACPI_TABLE_HEADER *sdth)
155 {
156 	pmap_unmapdev((vm_offset_t)sdth, sdth->Length);
157 }
158 
159 static vm_paddr_t
160 sdt_search_xsdt(vm_paddr_t xsdt_paddr, const uint8_t *sig)
161 {
162 	ACPI_TABLE_XSDT *xsdt;
163 	vm_paddr_t sdt_paddr = 0;
164 	int i, nent;
165 
166 	if (xsdt_paddr == 0) {
167 		kprintf("sdt_search_xsdt: XSDT paddr == 0\n");
168 		return 0;
169 	}
170 
171 	xsdt = sdt_sdth_map(xsdt_paddr);
172 	if (xsdt == NULL) {
173 		kprintf("sdt_search_xsdt: can't map XSDT\n");
174 		return 0;
175 	}
176 
177 	if (memcmp(xsdt->Header.Signature, ACPI_SIG_XSDT,
178 		   ACPI_NAME_SIZE) != 0) {
179 		kprintf("sdt_search_xsdt: not XSDT\n");
180 		goto back;
181 	}
182 
183 	if (xsdt->Header.Revision != 1) {
184 		kprintf("sdt_search_xsdt: unknown XSDT revision %d\n",
185 			xsdt->Header.Revision);
186 	}
187 
188 	if (xsdt->Header.Length < sizeof(xsdt->Header)) {
189 		kprintf("sdt_search_xsdt: invalid XSDT length %u\n",
190 			xsdt->Header.Length);
191 		goto back;
192 	}
193 
194 	nent = (xsdt->Header.Length - sizeof(xsdt->Header)) /
195 	       sizeof(xsdt->TableOffsetEntry[0]);
196 	for (i = 0; i < nent; ++i) {
197 		ACPI_TABLE_HEADER *sdth;
198 
199 		if (xsdt->TableOffsetEntry[i] == 0)
200 			continue;
201 
202 		sdth = sdt_sdth_map(xsdt->TableOffsetEntry[i]);
203 		if (sdth != NULL) {
204 			int ret;
205 
206 			ret = memcmp(sdth->Signature, sig, ACPI_NAME_SIZE);
207 			sdt_sdth_unmap(sdth);
208 
209 			if (ret == 0) {
210 				sdt_paddr = xsdt->TableOffsetEntry[i];
211 				break;
212 			}
213 		}
214 	}
215 back:
216 	sdt_sdth_unmap(&xsdt->Header);
217 	return sdt_paddr;
218 }
219 
220 static vm_paddr_t
221 sdt_search_rsdt(vm_paddr_t rsdt_paddr, const uint8_t *sig)
222 {
223 	ACPI_TABLE_RSDT *rsdt;
224 	vm_paddr_t sdt_paddr = 0;
225 	int i, nent;
226 
227 	if (rsdt_paddr == 0) {
228 		kprintf("sdt_search_rsdt: RSDT paddr == 0\n");
229 		return 0;
230 	}
231 
232 	rsdt = sdt_sdth_map(rsdt_paddr);
233 	if (rsdt == NULL) {
234 		kprintf("sdt_search_rsdt: can't map RSDT\n");
235 		return 0;
236 	}
237 
238 	if (memcmp(rsdt->Header.Signature, ACPI_SIG_RSDT,
239 		   ACPI_NAME_SIZE) != 0) {
240 		kprintf("sdt_search_rsdt: not RSDT\n");
241 		goto back;
242 	}
243 
244 	if (rsdt->Header.Revision != 1) {
245 		kprintf("sdt_search_rsdt: unknown RSDT revision %d\n",
246 			rsdt->Header.Revision);
247 	}
248 
249 	if (rsdt->Header.Length < sizeof(rsdt->Header)) {
250 		kprintf("sdt_search_rsdt: invalid RSDT length %u\n",
251 			rsdt->Header.Length);
252 		goto back;
253 	}
254 
255 	nent = (rsdt->Header.Length - sizeof(rsdt->Header)) /
256 	       sizeof(rsdt->TableOffsetEntry[0]);
257 	for (i = 0; i < nent; ++i) {
258 		ACPI_TABLE_HEADER *sdth;
259 
260 		if (rsdt->TableOffsetEntry[i] == 0)
261 			continue;
262 
263 		sdth = sdt_sdth_map(rsdt->TableOffsetEntry[i]);
264 		if (sdth != NULL) {
265 			int ret;
266 
267 			ret = memcmp(sdth->Signature, sig, ACPI_NAME_SIZE);
268 			sdt_sdth_unmap(sdth);
269 
270 			if (ret == 0) {
271 				sdt_paddr = rsdt->TableOffsetEntry[i];
272 				break;
273 			}
274 		}
275 	}
276 back:
277 	sdt_sdth_unmap(&rsdt->Header);
278 	return sdt_paddr;
279 }
280 
281 vm_paddr_t
282 sdt_search(const uint8_t *sig)
283 {
284 	if (sdt_search_func == NULL)
285 		return 0;
286 	return sdt_search_func(sdt_search_paddr, sig);
287 }
288