xref: /illumos-gate/usr/src/boot/i386/libi386/biosacpi.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 /*-
2  * Copyright (c) 2001 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 
29 #include <stand.h>
30 #include <machine/stdarg.h>
31 #include <bootstrap.h>
32 #include <btxv86.h>
33 #include "libi386.h"
34 
35 #include "platform/acfreebsd.h"
36 #include "acconfig.h"
37 #define ACPI_SYSTEM_XFACE
38 #include "actypes.h"
39 #include "actbl.h"
40 #include "actbl3.h"
41 
42 /*
43  * Detect ACPI and export information about the ACPI BIOS into the
44  * environment.
45  */
46 
47 ACPI_TABLE_RSDP	*rsdp;
48 static ACPI_TABLE_RSDP	*biosacpi_find_rsdp(void);
49 static ACPI_TABLE_RSDP	*biosacpi_search_rsdp(char *base, int length);
50 
51 #define RSDP_CHECKSUM_LENGTH 20
52 
53 static ACPI_TABLE_SPCR *
54 find_spcr(ACPI_TABLE_RSDP *rsdp)
55 {
56 	unsigned count, i;
57 	ACPI_TABLE_XSDT *xsdt;
58 	ACPI_TABLE_RSDT *rsdt;
59 	void *ptr;
60 
61 	if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
62 		xsdt = (ACPI_TABLE_XSDT *)
63 		    PTOV((uintptr_t)rsdp->XsdtPhysicalAddress);
64 
65 		count = xsdt->Header.Length - sizeof (ACPI_TABLE_HEADER);
66 		count /= sizeof (xsdt->TableOffsetEntry[0]);
67 		for (i = 0; i < count; i++) {
68 			ptr = PTOV((uintptr_t)xsdt->TableOffsetEntry[i]);
69 			if (memcmp(ptr, ACPI_SIG_SPCR,
70 			    sizeof (ACPI_SIG_SPCR) - 1) == 0) {
71 				return (ptr);
72 			}
73 		}
74 	}
75 	if (rsdp->RsdtPhysicalAddress != 0) {
76 		rsdt = (ACPI_TABLE_RSDT *)PTOV(rsdp->RsdtPhysicalAddress);
77 
78 		count = rsdt->Header.Length - sizeof (ACPI_TABLE_HEADER);
79 		count /= sizeof (rsdt->TableOffsetEntry[0]);
80 		for (i = 0; i < count; i++) {
81 			ptr = PTOV(rsdt->TableOffsetEntry[i]);
82 			if (memcmp(ptr, ACPI_SIG_SPCR,
83 			    sizeof (ACPI_SIG_SPCR) - 1) == 0) {
84 				return (ptr);
85 			}
86 		}
87 	}
88 	return (NULL);
89 }
90 
91 /*
92  * Find and parse SPCR table to set up serial console.
93  */
94 static void
95 biosacpi_setup_spcr(ACPI_TABLE_SPCR *spcr)
96 {
97 	unsigned baudrate;
98 	const char *port;
99 	char *name, *value;
100 
101 	if (spcr == NULL)
102 		return;
103 
104 	switch (spcr->BaudRate) {
105 	case 0:
106 		/* Use current port setting. */
107 		baudrate = 0;
108 		break;
109 	case 3:
110 		baudrate = 9600;
111 		break;
112 	case 4:
113 		baudrate = 19200;
114 		break;
115 	case 6:
116 		baudrate = 57600;
117 		break;
118 	case 7:
119 		baudrate = 115200;
120 		break;
121 	default:
122 		return;
123 	}
124 
125 	port = NULL;
126 	name = NULL;
127 	value = NULL;
128 
129 	switch (spcr->SerialPort.SpaceId) {
130 	case ACPI_ADR_SPACE_SYSTEM_IO:
131 		if (baudrate == 0)
132 			baudrate = comc_getspeed(spcr->SerialPort.Address);
133 
134 		if (asprintf(&value, "%u,8,N,1,-", baudrate) < 0)
135 			return;
136 
137 		switch (spcr->SerialPort.Address) {
138 		case 0x3F8:
139 			port = "ttya";
140 			break;
141 		case 0x2F8:
142 			port = "ttyb";
143 			break;
144 		case 0x3E8:
145 			port = "ttyc";
146 			break;
147 		case 0x2E8:
148 			port = "ttyd";
149 			break;
150 		default:
151 			break;
152 		}
153 		break;
154 	default:
155 		/* XXX not implemented. */
156 		break;
157 	}
158 
159 	/*
160 	 * We want to set console according to SPCR. Also
161 	 * we need to store the SPCR reference value.
162 	 */
163 	if (port != NULL) {
164 		if (asprintf(&name, "%s,text", port) > 0) {
165 			setenv("console", name, 1);
166 			free(name);
167 		}
168 		if (asprintf(&name, "%s-mode", port) > 0) {
169 			setenv(name, value, 1);
170 			free(name);
171 		}
172 		if (asprintf(&name, "%s-spcr-mode", port) > 0) {
173 			setenv(name, value, 1);
174 			free(name);
175 		}
176 	}
177 	free(value);
178 }
179 
180 void
181 biosacpi_detect(void)
182 {
183 	char	buf[24];
184 	int	revision;
185 
186 	/* locate and validate the RSDP */
187 	if ((rsdp = biosacpi_find_rsdp()) == NULL)
188 		return;
189 
190 	/* export values from the RSDP */
191 	sprintf(buf, "0x%08x", (unsigned int)VTOP(rsdp));
192 	setenv("acpi.rsdp", buf, 1);
193 	revision = rsdp->Revision;
194 	if (revision == 0)
195 		revision = 1;
196 	snprintf(buf, sizeof (buf), "%d", revision);
197 	setenv("acpi.revision", buf, 1);
198 	strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
199 	buf[sizeof (rsdp->OemId)] = '\0';
200 	setenv("acpi.oem", buf, 1);
201 	snprintf(buf, sizeof (buf), "0x%08x", rsdp->RsdtPhysicalAddress);
202 	setenv("acpi.rsdt", buf, 1);
203 	if (revision >= 2) {
204 		/* XXX extended checksum? */
205 		snprintf(buf, sizeof (buf), "0x%016llx",
206 		    (unsigned long long)rsdp->XsdtPhysicalAddress);
207 		setenv("acpi.xsdt", buf, 1);
208 		sprintf(buf, "%d", rsdp->Length);
209 		setenv("acpi.xsdt_length", buf, 1);
210 	}
211 	biosacpi_setup_spcr(find_spcr(rsdp));
212 }
213 
214 /*
215  * Find the RSDP in low memory.  See section 5.2.2 of the ACPI spec.
216  */
217 static ACPI_TABLE_RSDP *
218 biosacpi_find_rsdp(void)
219 {
220     ACPI_TABLE_RSDP	*rsdp;
221     uint16_t		*addr;
222 
223     /* EBDA is the 1 KB addressed by the 16 bit pointer at 0x40E. */
224     addr = (uint16_t *)PTOV(0x40E);
225     rsdp = biosacpi_search_rsdp((char *)(intptr_t)(*addr << 4), 0x400);
226     if (rsdp != NULL)
227 	return (rsdp);
228 
229     /* Check the upper memory BIOS space, 0xe0000 - 0xfffff. */
230     if ((rsdp = biosacpi_search_rsdp((char *)0xe0000, 0x20000)) != NULL)
231 	return (rsdp);
232 
233     return (NULL);
234 }
235 
236 static ACPI_TABLE_RSDP *
237 biosacpi_search_rsdp(char *base, int length)
238 {
239     ACPI_TABLE_RSDP	*rsdp;
240     u_int8_t		*cp, sum;
241     int			ofs, idx;
242 
243     /* search on 16-byte boundaries */
244     for (ofs = 0; ofs < length; ofs += 16) {
245 	rsdp = (ACPI_TABLE_RSDP *)PTOV(base + ofs);
246 
247 	/* compare signature, validate checksum */
248 	if (!strncmp(rsdp->Signature, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP))) {
249 	    cp = (u_int8_t *)rsdp;
250 	    sum = 0;
251 	    for (idx = 0; idx < RSDP_CHECKSUM_LENGTH; idx++)
252 		sum += *(cp + idx);
253 	    if (sum != 0)
254 		continue;
255 	    return(rsdp);
256 	}
257     }
258     return(NULL);
259 }
260