source: mainline/kernel/genarch/src/acpi/acpi.c@ 4b1c7c6f

Last change on this file since 4b1c7c6f was 4b1c7c6f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Clean up PAGE_* flags.

Remove "nop flags", they are confusing for readers and any benefit they would
gain in self-documentation they lose in being used inconsistently.

Remove "PAGE_READ", the only place it's used meaningfully is the incomplete
RISC-V implementation, and most call sites assume read is implied.

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[85bfdcc8]1/*
[df4ed85]2 * Copyright (c) 2005 Jakub Jermar
[85bfdcc8]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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[06e1e95]29/** @addtogroup genarch
[b45c443]30 * @{
31 */
32
[0f27b4c]33/**
[b45c443]34 * @file
[793cf029]35 * @brief Advanced Configuration and Power Interface (ACPI) initialization.
[0f27b4c]36 */
[793cf029]37
[e16e036a]38#include <genarch/acpi/acpi.h>
39#include <genarch/acpi/madt.h>
[babcb148]40#include <arch/bios/bios.h>
[63e27ef]41#include <debug.h>
[10a2e22]42#include <mm/page.h>
[93da799]43#include <mm/km.h>
[b2fa1204]44#include <log.h>
[10a2e22]45
[793cf029]46#define RSDP_SIGNATURE "RSD PTR "
47#define RSDP_REVISION_OFFS 15
[85bfdcc8]48
[1075ac6]49#define CMP_SIGNATURE(left, right) \
50 (((left)[0] == (right)[0]) && \
51 ((left)[1] == (right)[1]) && \
52 ((left)[2] == (right)[2]) && \
53 ((left)[3] == (right)[3]))
54
[85bfdcc8]55struct acpi_rsdp *acpi_rsdp = NULL;
[10a2e22]56struct acpi_rsdt *acpi_rsdt = NULL;
57struct acpi_xsdt *acpi_xsdt = NULL;
58
[76fca31]59struct acpi_signature_map signature_map[] = {
60 {
61 (uint8_t *) "APIC",
62 (void *) &acpi_madt,
63 "Multiple APIC Description Table"
64 }
[10a2e22]65};
[85bfdcc8]66
[793cf029]67static int rsdp_check(uint8_t *_rsdp) {
68 struct acpi_rsdp *rsdp = (struct acpi_rsdp *) _rsdp;
[7f1c620]69 uint8_t sum = 0;
[793cf029]70 uint32_t i;
[a35b458]71
[7d07bf3]72 for (i = 0; i < 20; i++)
[793cf029]73 sum = (uint8_t) (sum + _rsdp[i]);
[a35b458]74
[793cf029]75 if (sum)
[babcb148]76 return 0; /* bad checksum */
[a35b458]77
[793cf029]78 if (rsdp->revision == 0)
[babcb148]79 return 1; /* ACPI 1.0 */
[a35b458]80
[793cf029]81 for (; i < rsdp->length; i++)
82 sum = (uint8_t) (sum + _rsdp[i]);
[a35b458]83
[793cf029]84 return !sum;
[babcb148]85}
86
[7f1c620]87int acpi_sdt_check(uint8_t *sdt)
[10a2e22]88{
[793cf029]89 struct acpi_sdt_header *hdr = (struct acpi_sdt_header *) sdt;
[7f1c620]90 uint8_t sum = 0;
[7d07bf3]91 unsigned int i;
[a35b458]92
[793cf029]93 for (i = 0; i < hdr->length; i++)
[7f043c0]94 sum = (uint8_t) (sum + sdt[i]);
[a35b458]95
[10a2e22]96 return !sum;
97}
98
[93da799]99static struct acpi_sdt_header *map_sdt(struct acpi_sdt_header *psdt)
[10a2e22]100{
[93da799]101 struct acpi_sdt_header *vhdr;
102 struct acpi_sdt_header *vsdt;
103
104 /* Start with mapping the header only. */
[bf3dd35]105 vhdr = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
[4b1c7c6f]106 sizeof(struct acpi_sdt_header), 0);
[93da799]107
108 /* Now we can map the entire structure. */
[bf3dd35]109 vsdt = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
[4b1c7c6f]110 vhdr->length, PAGE_WRITE);
[a35b458]111
[93da799]112 // TODO: do not leak vtmp
113
[1b20da0]114 return vsdt;
[10a2e22]115}
116
[e16e036a]117static void configure_via_rsdt(void)
118{
[793cf029]119 size_t i;
120 size_t j;
121 size_t cnt = (acpi_rsdt->header.length - sizeof(struct acpi_sdt_header))
122 / sizeof(uint32_t);
[a35b458]123
[7d07bf3]124 for (i = 0; i < cnt; i++) {
[793cf029]125 for (j = 0; j < sizeof(signature_map)
126 / sizeof(struct acpi_signature_map); j++) {
127 struct acpi_sdt_header *hdr =
[96b02eb9]128 (struct acpi_sdt_header *) (sysarg_t) acpi_rsdt->entry[i];
[a35b458]129
[93da799]130 struct acpi_sdt_header *vhdr = map_sdt(hdr);
131 if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
132 if (!acpi_sdt_check((uint8_t *) vhdr))
[793cf029]133 break;
[a35b458]134
[93da799]135 *signature_map[j].sdt_ptr = vhdr;
[793cf029]136 LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
137 signature_map[j].description);
[e16e036a]138 }
139 }
140 }
141}
142
143static void configure_via_xsdt(void)
144{
[793cf029]145 size_t i;
146 size_t j;
147 size_t cnt = (acpi_xsdt->header.length - sizeof(struct acpi_sdt_header))
148 / sizeof(uint64_t);
[a35b458]149
[7d07bf3]150 for (i = 0; i < cnt; i++) {
[793cf029]151 for (j = 0; j < sizeof(signature_map)
152 / sizeof(struct acpi_signature_map); j++) {
153 struct acpi_sdt_header *hdr =
154 (struct acpi_sdt_header *) ((uintptr_t) acpi_xsdt->entry[i]);
[a35b458]155
[93da799]156 struct acpi_sdt_header *vhdr = map_sdt(hdr);
157 if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
158 if (!acpi_sdt_check((uint8_t *) vhdr))
[793cf029]159 break;
[a35b458]160
[93da799]161 *signature_map[j].sdt_ptr = vhdr;
[793cf029]162 LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
163 signature_map[j].description);
[e16e036a]164 }
165 }
166 }
167}
168
[85bfdcc8]169void acpi_init(void)
170{
[7f1c620]171 uint8_t *addr[2] = { NULL, (uint8_t *) PA2KA(0xe0000) };
[793cf029]172 unsigned int i;
173 unsigned int j;
174 unsigned int length[2] = { 1024, 128 * 1024 };
[7f1c620]175 uint64_t *sig = (uint64_t *) RSDP_SIGNATURE;
[a35b458]176
[76cec1e]177 /*
[babcb148]178 * Find Root System Description Pointer
[76cec1e]179 * 1. search first 1K of EBDA
180 * 2. search 128K starting at 0xe0000
181 */
[a35b458]182
[7f1c620]183 addr[0] = (uint8_t *) PA2KA(ebda);
[babcb148]184 for (i = (ebda ? 0 : 1); i < 2; i++) {
[76cec1e]185 for (j = 0; j < length[i]; j += 16) {
[793cf029]186 if ((*((uint64_t *) &addr[i][j]) == *sig)
187 && (rsdp_check(&addr[i][j]))) {
[76cec1e]188 acpi_rsdp = (struct acpi_rsdp *) &addr[i][j];
189 goto rsdp_found;
190 }
191 }
192 }
[a35b458]193
[76cec1e]194 return;
[a35b458]195
[babcb148]196rsdp_found:
[793cf029]197 LOG("%p: ACPI Root System Description Pointer", acpi_rsdp);
[a35b458]198
[93da799]199 uintptr_t acpi_rsdt_p = (uintptr_t) acpi_rsdp->rsdt_address;
200 uintptr_t acpi_xsdt_p = 0;
201
[103bb68]202 if (acpi_rsdp->revision)
[93da799]203 acpi_xsdt_p = (uintptr_t) acpi_rsdp->xsdt_address;
[a35b458]204
[93da799]205 if (acpi_rsdt_p)
206 acpi_rsdt = (struct acpi_rsdt *) map_sdt(
207 (struct acpi_sdt_header *) acpi_rsdt_p);
[a35b458]208
[93da799]209 if (acpi_xsdt_p)
210 acpi_xsdt = (struct acpi_xsdt *) map_sdt(
211 (struct acpi_sdt_header *) acpi_xsdt_p);
[a35b458]212
[793cf029]213 if ((acpi_rsdt) && (!acpi_sdt_check((uint8_t *) acpi_rsdt))) {
[b2fa1204]214 log(LF_ARCH, LVL_ERROR, "RSDT: bad checksum");
[10a2e22]215 return;
216 }
[a35b458]217
[793cf029]218 if ((acpi_xsdt) && (!acpi_sdt_check((uint8_t *) acpi_xsdt))) {
[b2fa1204]219 log(LF_ARCH, LVL_ERROR, "XSDT: bad checksum");
[10a2e22]220 return;
221 }
[a35b458]222
[103bb68]223 if (acpi_xsdt)
224 configure_via_xsdt();
225 else if (acpi_rsdt)
226 configure_via_rsdt();
[10a2e22]227}
228
[06e1e95]229/** @}
[b45c443]230 */
Note: See TracBrowser for help on using the repository browser.