xref: /freebsd/stand/i386/libi386/biosacpi.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
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 __FBSDID("$FreeBSD$");
29 
30 #include <stand.h>
31 #include <machine/stdarg.h>
32 #include <bootstrap.h>
33 #include <btxv86.h>
34 #include "libi386.h"
35 
36 #include "platform/acfreebsd.h"
37 #include "acconfig.h"
38 #define ACPI_SYSTEM_XFACE
39 #include "actypes.h"
40 #include "actbl.h"
41 
42 /*
43  * Detect ACPI and export information about the ACPI BIOS into the
44  * environment.
45  */
46 
47 static ACPI_TABLE_RSDP	*biosacpi_find_rsdp(void);
48 static ACPI_TABLE_RSDP	*biosacpi_search_rsdp(char *base, int length);
49 
50 #define RSDP_CHECKSUM_LENGTH 20
51 
52 void
53 biosacpi_detect(void)
54 {
55     ACPI_TABLE_RSDP	*rsdp;
56     char		buf[24];
57     int			revision;
58 
59     /* locate and validate the RSDP */
60     if ((rsdp = biosacpi_find_rsdp()) == NULL)
61 	return;
62 
63     /*
64      * Report the RSDP to the kernel. While this can be found with
65      * a BIOS boot, the RSDP may be elsewhere when booted from UEFI.
66      */
67     sprintf(buf, "0x%08x", VTOP(rsdp));
68     setenv("acpi.rsdp", buf, 1);
69     revision = rsdp->Revision;
70     if (revision == 0)
71 	revision = 1;
72     sprintf(buf, "%d", revision);
73     setenv("acpi.revision", buf, 1);
74     strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
75     buf[sizeof(rsdp->OemId)] = '\0';
76     setenv("acpi.oem", buf, 1);
77     sprintf(buf, "0x%08x", rsdp->RsdtPhysicalAddress);
78     setenv("acpi.rsdt", buf, 1);
79     if (revision >= 2) {
80 	/* XXX extended checksum? */
81 	sprintf(buf, "0x%016llx", rsdp->XsdtPhysicalAddress);
82 	setenv("acpi.xsdt", buf, 1);
83 	sprintf(buf, "%d", rsdp->Length);
84 	setenv("acpi.xsdt_length", buf, 1);
85     }
86 }
87 
88 /*
89  * Find the RSDP in low memory.  See section 5.2.2 of the ACPI spec.
90  */
91 static ACPI_TABLE_RSDP *
92 biosacpi_find_rsdp(void)
93 {
94     ACPI_TABLE_RSDP	*rsdp;
95     uint16_t		*addr;
96 
97     /* EBDA is the 1 KB addressed by the 16 bit pointer at 0x40E. */
98     addr = (uint16_t *)PTOV(0x40E);
99     if ((rsdp = biosacpi_search_rsdp((char *)(*addr << 4), 0x400)) != NULL)
100 	return (rsdp);
101 
102     /* Check the upper memory BIOS space, 0xe0000 - 0xfffff. */
103     if ((rsdp = biosacpi_search_rsdp((char *)0xe0000, 0x20000)) != NULL)
104 	return (rsdp);
105 
106     return (NULL);
107 }
108 
109 static ACPI_TABLE_RSDP *
110 biosacpi_search_rsdp(char *base, int length)
111 {
112     ACPI_TABLE_RSDP	*rsdp;
113     uint8_t		*cp, sum;
114     int			ofs, idx;
115 
116     /* search on 16-byte boundaries */
117     for (ofs = 0; ofs < length; ofs += 16) {
118 	rsdp = (ACPI_TABLE_RSDP *)PTOV(base + ofs);
119 
120 	/* compare signature, validate checksum */
121 	if (!strncmp(rsdp->Signature, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP))) {
122 	    cp = (uint8_t *)rsdp;
123 	    sum = 0;
124 	    for (idx = 0; idx < RSDP_CHECKSUM_LENGTH; idx++)
125 		sum += *(cp + idx);
126 	    if (sum != 0)
127 		continue;
128 	    return(rsdp);
129 	}
130     }
131     return(NULL);
132 }
133