source: mainline/kernel/genarch/src/acpi/acpi.c@ bf3dd35

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

There is no need to have km_map() for aligned addresses and
km_map_structure() for unaligned addresses.

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