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