-
Notifications
You must be signed in to change notification settings - Fork 0
/
acpi.c
286 lines (207 loc) · 6.77 KB
/
acpi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*Copyright 2018 Juan Bosco Garcia, Almudena Garcia Jurado-Centurion
*This file is part of Min_SMP.
*Min_SMP is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*Min_SMP is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*You should have received a copy of the GNU General Public License
*along with Min_SMP. If not, see <http://www.gnu.org/licenses/>.
*/
#include <acpi.h>
#include <mem.h>
#include <video.h>
#include <lapic.h>
#include <ioapic.h>
#include <mp.h>
#include <cpu.h>
struct acpi_rsdp *rsdp;
struct acpi_rsdt *rsdt;
int acpi_rsdt_n;
struct acpi_apic *apic;
static int acpi_get_rsdp();
static int acpi_check_rsdt(struct acpi_rsdt *);
static int acpi_get_rsdt();
static int acpi_apic_setup();
extern int ncpu;
extern volatile ApicLocalUnit* lapic;
extern struct cpu cpus[];
extern int nioapic;
extern struct list ioapics;
int
acpi_setup(){
//Try to get rsdp pointer
if(acpi_get_rsdp() || rsdp==0)
return -1;
//Try to get rsdt pointer
if(acpi_get_rsdt() || rsdt==0)
return -1;
//Search APIC entries in rsdt array
int i;
struct acpi_dhdr *descr_header;
for(i = 0;i < acpi_rsdt_n; i++){
descr_header = (struct acpi_dhdr*) rsdt->entry[i];
//Check if the entry contains an APIC
if(memcmp(descr_header->signature, ACPI_APIC_SIG,
sizeof(descr_header->signature)) == 0){
//If yes, store the entry in apic
apic = (struct acpi_apic*) rsdt->entry[i];
//continue;
break;
}
}
if(acpi_apic_setup())
return -1;
return 0;
}
void
acpi_print_info(){
printf("ACPI:\n");
printf(" rsdp = %x; rsdp->rsdt_addr = %x\n", rsdp, rsdp->rsdt_addr);
printf(" rsdt = %x; rsdt->length = %x (n = %x)\n", rsdt, rsdt->header.length,
acpi_rsdt_n);
int i;
struct acpi_dhdr *descr_header;
for(i=0;i<acpi_rsdt_n;i++){
descr_header = (struct acpi_dhdr*) rsdt->entry[i];
printf(" %x: %c%c%c%c (%x)\n", i, descr_header->signature[0],
descr_header->signature[1], descr_header->signature[2],
descr_header->signature[3], rsdt->entry[i]);
}
}
static int
acpi_checksum(void *addr, uint32 length){
int8 *bytes = addr;
int checksum = 0;
unsigned int i;
//Sum all bytes of addr
for(i = 0;i < length; i++){
checksum += bytes[i];
}
return checksum & 0xff;
}
static int
acpi_check_rsdp(struct acpi_rsdp *rsdp){
//Check is rsdp signature is equals to ACPI RSDP signature
if(memcmp(rsdp->signature, ACPI_RSDP_SIG, sizeof(rsdp->signature)) != 0)
return -1;
//If yes, calculates rdsp checksum and check It
uint32 checksum;
checksum = acpi_checksum(rsdp, sizeof(*rsdp));
if(checksum != 0)
return -1;
return 0;
}
static int
acpi_search_rsdp(void *addr, uint32 length){
void *end;
/* check alignment */
if((uint32)addr & (ACPI_RSDP_ALIGN-1))
return -1;
//Search RDSP in memory space between addr and addr+lenght
for(end = addr+length; addr < end; addr += ACPI_RSDP_ALIGN){
//Check if the current memory block store the RDSP
if(acpi_check_rsdp(addr) == 0){
//If yes, store RSDP address
rsdp = (struct acpi_rsdp*)addr;
return 0;
}
}
return -1;
}
static int
acpi_get_rsdp(){
uint32 base = 0x0;
//EDBA start address
//base = (uint32*) *((uint16*) 0x040e);
base = *((uint16*) 0x040e);
if(base != 0){ //Memory check
base <<= 4; //base = base * 16
//Search RSDP in first 1024 bytes from EDBA
if(acpi_search_rsdp((void*)base,1024) == 0)
return 0;
}
//If RSDP isn't in EDBA, search in the BIOS read-only memory space between 0E0000h and 0FFFFFh
if(acpi_search_rsdp((void*)0x0e0000, 0x100000 - 0x0e0000) == 0)
return 0;
return -1;
}
static int
acpi_check_rsdt(struct acpi_rsdt *rsdt){
return acpi_checksum(rsdt, rsdt->header.length);
}
static int
acpi_get_rsdt(){
//Get rsdt address from rsdp
rsdt = (struct acpi_rsdt*)rsdp->rsdt_addr;
//Check is rsdt signature is equals to ACPI RSDT signature
if(memcmp(rsdt->header.signature, ACPI_RSDT_SIG,
sizeof(rsdt->header.signature)) != 0)
return -1;
//Check if rsdt is correct
if(acpi_check_rsdt(rsdt))
return -1;
//Calculated number of elements stored in rsdt
acpi_rsdt_n = (rsdt->header.length - sizeof(rsdt->header))
/ sizeof(rsdt->entry[0]);
return 0;
}
static int
acpi_apic_setup(){
if(apic == 0)
return -1;
//Check the checksum of the APIC
if(acpi_checksum(apic, apic->header.length))
return -1;
ncpu = 0;
nioapic = 0;
lapic = (ApicLocalUnit*) apic->lapic_addr;
list_init(&ioapics);
struct acpi_apic_dhdr *apic_entry = apic->entry;
uint32 end = (uint32) apic + apic->header.length;
//Search in APIC entry
while((uint32)apic_entry < end){
struct acpi_apic_lapic *lapic_entry;
struct acpi_apic_ioapic *ioapic_entry;
//Check entry type
switch(apic_entry->type){
//If APIC entry is a CPU lapic
case ACPI_APIC_ENTRY_LAPIC:
//Store lapic
lapic_entry = (struct acpi_apic_lapic*) apic_entry;
//If cpu flag is correct, and the maximum number of CPUs is not reached
if((lapic_entry->flags & 0x1) && ncpu < NCPU){
//Enumerate CPU and add It to cpu/apic vector
cpus[ncpu].apic_id = lapic_entry->apic_id;
//Increase number of CPU
ncpu++;
}
break;
//If APIC entry is an IOAPIC
case ACPI_APIC_ENTRY_IOAPIC:
//Store ioapic
ioapic_entry = (struct acpi_apic_ioapic*) apic_entry;
//Initialice ioapic table
struct ioapic *ioapic_last;
ioapic_last = malloc(sizeof(struct ioapic));
list_init_node(&ioapic_last->node);
ioapic_last->apic_id = ioapic_entry->apic_id;
ioapic_last->addr = ioapic_entry->addr;
ioapic_last->base = ioapic_entry->base;
//Insert ioapic in ioapic's list
list_insert_tail(&ioapics, &ioapic_last->node);
//Increase number of ioapic
nioapic++;
break;
}
//Get next APIC entry
apic_entry =(struct acpi_apic_dhdr*)((uint32) apic_entry
+ apic_entry->length);
}
if(ncpu == 0 || nioapic == 0)
return -1;
return 0;
}