Remove unneeded inclusions of <sys/cdefs.h> throughout the tree.
[games.git] / sys / boot / pc32 / libi386 / biosacpi.c
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  * $FreeBSD: src/sys/boot/i386/libi386/biosacpi.c,v 1.7 2003/08/25 23:28:31 obrien Exp $
27  */
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 "acdragonfly.h"
36 #include "acconfig.h"
37 #define ACPI_SYSTEM_XFACE
38 #include "actypes.h"
39 #include "actbl.h"
40
41 /*
42  * Detect ACPI and export information about the APCI BIOS into the
43  * environment.
44  */
45
46 static ACPI_TABLE_RSDP  *biosacpi_find_rsdp(void);
47 static ACPI_TABLE_RSDP  *biosacpi_search_rsdp(char *base, int length);
48
49 #define RSDP_CHECKSUM_LENGTH 20
50
51 void
52 biosacpi_detect(void)
53 {
54     ACPI_TABLE_RSDP     *rsdp;
55     char                buf[160];
56     int                 revision;
57
58     /* XXX check the BIOS datestamp */
59
60     /* locate and validate the RSDP */
61     if ((rsdp = biosacpi_find_rsdp()) == NULL)
62         return;
63
64     /* export values from the RSDP */
65     revision = rsdp->Revision;
66     if (revision == 0)
67         revision = 1;
68     sprintf(buf, "%d", revision);
69     setenv("hint.acpi.0.revision", buf, 1);
70     strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
71     buf[sizeof(rsdp->OemId)] = '\0';
72     setenv("hint.acpi.0.oem", buf, 1);
73     sprintf(buf, "0x%08x", rsdp->RsdtPhysicalAddress);
74     setenv("hint.acpi.0.rsdt", buf, 1);
75     if (revision >= 2) {
76         /* XXX extended checksum? */
77         sprintf(buf, "0x%016jx", (uintmax_t)rsdp->XsdtPhysicalAddress);
78         setenv("hint.acpi.0.xsdt", buf, 1);
79         sprintf(buf, "%d", rsdp->Length);
80         setenv("hint.acpi.0.xsdt_length", buf, 1);
81     }
82     /* XXX other tables? */
83
84     setenv("acpi_load", "YES", 1);
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     u_int8_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 = (u_int8_t *)rsdp;
122             sum = 0;
123             for (idx = 0; idx < RSDP_CHECKSUM_LENGTH; idx++)
124                 sum += *(cp + idx);
125             if (sum != 0) {
126                 printf("acpi: bad RSDP checksum (%d)\n", sum);
127                 continue;
128             }
129             return(rsdp);
130         }
131     }
132     return(NULL);
133 }