source: mainline/kernel/genarch/src/acpi/acpi.c@ 6ae5e3f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6ae5e3f was 780edc7, checked in by Jakub Jermar <jakub@…>, 7 years ago

Fix test for memcmp() match

  • Property mode set to 100644
File size: 6.1 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
[6404aca]29/** @addtogroup kernel_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>
[bf05c74]45#include <mem.h>
[10a2e22]46
[793cf029]47#define RSDP_SIGNATURE "RSD PTR "
48#define RSDP_REVISION_OFFS 15
[85bfdcc8]49
[1075ac6]50#define CMP_SIGNATURE(left, right) \
51 (((left)[0] == (right)[0]) && \
52 ((left)[1] == (right)[1]) && \
53 ((left)[2] == (right)[2]) && \
54 ((left)[3] == (right)[3]))
55
[85bfdcc8]56struct acpi_rsdp *acpi_rsdp = NULL;
[10a2e22]57struct acpi_rsdt *acpi_rsdt = NULL;
58struct acpi_xsdt *acpi_xsdt = NULL;
59
[76fca31]60struct acpi_signature_map signature_map[] = {
61 {
62 (uint8_t *) "APIC",
63 (void *) &acpi_madt,
64 "Multiple APIC Description Table"
65 }
[10a2e22]66};
[85bfdcc8]67
[3bacee1]68static int rsdp_check(uint8_t *_rsdp)
69{
[793cf029]70 struct acpi_rsdp *rsdp = (struct acpi_rsdp *) _rsdp;
[7f1c620]71 uint8_t sum = 0;
[793cf029]72 uint32_t i;
[a35b458]73
[7d07bf3]74 for (i = 0; i < 20; i++)
[793cf029]75 sum = (uint8_t) (sum + _rsdp[i]);
[a35b458]76
[793cf029]77 if (sum)
[babcb148]78 return 0; /* bad checksum */
[a35b458]79
[793cf029]80 if (rsdp->revision == 0)
[babcb148]81 return 1; /* ACPI 1.0 */
[a35b458]82
[793cf029]83 for (; i < rsdp->length; i++)
84 sum = (uint8_t) (sum + _rsdp[i]);
[a35b458]85
[793cf029]86 return !sum;
[babcb148]87}
88
[7f1c620]89int acpi_sdt_check(uint8_t *sdt)
[10a2e22]90{
[793cf029]91 struct acpi_sdt_header *hdr = (struct acpi_sdt_header *) sdt;
[7f1c620]92 uint8_t sum = 0;
[7d07bf3]93 unsigned int i;
[a35b458]94
[793cf029]95 for (i = 0; i < hdr->length; i++)
[7f043c0]96 sum = (uint8_t) (sum + sdt[i]);
[a35b458]97
[10a2e22]98 return !sum;
99}
100
[93da799]101static struct acpi_sdt_header *map_sdt(struct acpi_sdt_header *psdt)
[10a2e22]102{
[93da799]103 struct acpi_sdt_header *vhdr;
104 struct acpi_sdt_header *vsdt;
105
106 /* Start with mapping the header only. */
[bf3dd35]107 vhdr = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
[a1b9f63]108 sizeof(struct acpi_sdt_header), KM_NATURAL_ALIGNMENT,
109 PAGE_READ | PAGE_NOT_CACHEABLE);
[93da799]110
111 /* Now we can map the entire structure. */
[bf3dd35]112 vsdt = (struct acpi_sdt_header *) km_map((uintptr_t) psdt,
[a1b9f63]113 vhdr->length, KM_NATURAL_ALIGNMENT,
114 PAGE_WRITE | PAGE_NOT_CACHEABLE);
[a35b458]115
[93da799]116 // TODO: do not leak vtmp
117
[1b20da0]118 return vsdt;
[10a2e22]119}
120
[e16e036a]121static void configure_via_rsdt(void)
122{
[793cf029]123 size_t i;
124 size_t j;
[3bacee1]125 size_t cnt = (acpi_rsdt->header.length - sizeof(struct acpi_sdt_header)) /
126 sizeof(uint32_t);
[a35b458]127
[7d07bf3]128 for (i = 0; i < cnt; i++) {
[3bacee1]129 for (j = 0; j < sizeof(signature_map) /
130 sizeof(struct acpi_signature_map); j++) {
[793cf029]131 struct acpi_sdt_header *hdr =
[96b02eb9]132 (struct acpi_sdt_header *) (sysarg_t) acpi_rsdt->entry[i];
[a35b458]133
[93da799]134 struct acpi_sdt_header *vhdr = map_sdt(hdr);
135 if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
136 if (!acpi_sdt_check((uint8_t *) vhdr))
[793cf029]137 break;
[a35b458]138
[93da799]139 *signature_map[j].sdt_ptr = vhdr;
[793cf029]140 LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
141 signature_map[j].description);
[e16e036a]142 }
143 }
144 }
145}
146
147static void configure_via_xsdt(void)
148{
[793cf029]149 size_t i;
150 size_t j;
[3bacee1]151 size_t cnt = (acpi_xsdt->header.length - sizeof(struct acpi_sdt_header)) /
152 sizeof(uint64_t);
[a35b458]153
[7d07bf3]154 for (i = 0; i < cnt; i++) {
[3bacee1]155 for (j = 0; j < sizeof(signature_map) /
156 sizeof(struct acpi_signature_map); j++) {
[793cf029]157 struct acpi_sdt_header *hdr =
158 (struct acpi_sdt_header *) ((uintptr_t) acpi_xsdt->entry[i]);
[a35b458]159
[93da799]160 struct acpi_sdt_header *vhdr = map_sdt(hdr);
161 if (CMP_SIGNATURE(vhdr->signature, signature_map[j].signature)) {
162 if (!acpi_sdt_check((uint8_t *) vhdr))
[793cf029]163 break;
[a35b458]164
[93da799]165 *signature_map[j].sdt_ptr = vhdr;
[793cf029]166 LOG("%p: ACPI %s", *signature_map[j].sdt_ptr,
167 signature_map[j].description);
[e16e036a]168 }
169 }
170 }
171}
172
[bf05c74]173static uint8_t *search_rsdp(uint8_t *base, size_t len)
[85bfdcc8]174{
[bf05c74]175 for (size_t i = 0; i < len; i += 16) {
[780edc7]176 if (!__builtin_memcmp(&base[i], RSDP_SIGNATURE, 8) &&
[159c1525]177 rsdp_check(&base[i]))
[bf05c74]178 return &base[i];
179 }
[a35b458]180
[bf05c74]181 return NULL;
182}
183
184void acpi_init(void)
185{
[76cec1e]186 /*
[babcb148]187 * Find Root System Description Pointer
[76cec1e]188 * 1. search first 1K of EBDA
189 * 2. search 128K starting at 0xe0000
190 */
[a35b458]191
[bf05c74]192 uint8_t *rsdp = NULL;
193
194 if (ebda)
195 rsdp = search_rsdp((uint8_t *) PA2KA(ebda), 1024);
196
197 if (!rsdp)
198 rsdp = search_rsdp((uint8_t *) PA2KA(0xe0000), 128 * 1024);
199
200 if (!rsdp)
201 return;
[a35b458]202
[bf05c74]203 acpi_rsdp = (struct acpi_rsdp *) rsdp;
[a35b458]204
[793cf029]205 LOG("%p: ACPI Root System Description Pointer", acpi_rsdp);
[a35b458]206
[93da799]207 uintptr_t acpi_rsdt_p = (uintptr_t) acpi_rsdp->rsdt_address;
208 uintptr_t acpi_xsdt_p = 0;
209
[103bb68]210 if (acpi_rsdp->revision)
[93da799]211 acpi_xsdt_p = (uintptr_t) acpi_rsdp->xsdt_address;
[a35b458]212
[93da799]213 if (acpi_rsdt_p)
214 acpi_rsdt = (struct acpi_rsdt *) map_sdt(
215 (struct acpi_sdt_header *) acpi_rsdt_p);
[a35b458]216
[93da799]217 if (acpi_xsdt_p)
218 acpi_xsdt = (struct acpi_xsdt *) map_sdt(
219 (struct acpi_sdt_header *) acpi_xsdt_p);
[a35b458]220
[793cf029]221 if ((acpi_rsdt) && (!acpi_sdt_check((uint8_t *) acpi_rsdt))) {
[b2fa1204]222 log(LF_ARCH, LVL_ERROR, "RSDT: bad checksum");
[10a2e22]223 return;
224 }
[a35b458]225
[793cf029]226 if ((acpi_xsdt) && (!acpi_sdt_check((uint8_t *) acpi_xsdt))) {
[b2fa1204]227 log(LF_ARCH, LVL_ERROR, "XSDT: bad checksum");
[10a2e22]228 return;
229 }
[a35b458]230
[103bb68]231 if (acpi_xsdt)
232 configure_via_xsdt();
233 else if (acpi_rsdt)
234 configure_via_rsdt();
[10a2e22]235}
236
[06e1e95]237/** @}
[b45c443]238 */
Note: See TracBrowser for help on using the repository browser.