1 | /*␊ |
2 | * Copyright 2008 mackerintel␊ |
3 | */␊ |
4 | ␊ |
5 | #include "libsaio.h"␊ |
6 | #include "boot.h"␊ |
7 | #include "bootstruct.h"␊ |
8 | #include "acpi.h"␊ |
9 | #include "efi_tables.h"␊ |
10 | #include "fake_efi.h"␊ |
11 | #include "acpi_patcher.h"␊ |
12 | #include "platform.h"␊ |
13 | #include "cpu.h"␊ |
14 | #include "aml_generator.h"␊ |
15 | #include "smbios_patcher.h"␊ |
16 | ␊ |
17 | #ifndef DEBUG_ACPI␊ |
18 | #define DEBUG_ACPI 0␊ |
19 | #endif␊ |
20 | ␊ |
21 | #if DEBUG_ACPI==2␊ |
22 | #define DBG(x...) {printf(x); sleep(1);}␊ |
23 | #elif DEBUG_ACPI==3␊ |
24 | #define DBG(x...) verbose(x)␊ |
25 | #elif DEBUG_ACPI==1␊ |
26 | #define DBG(x...) msglog(x)␊ |
27 | #else␊ |
28 | #define DBG(x...)␊ |
29 | #endif␊ |
30 | ␊ |
31 | bool fix_restart;␊ |
32 | uint64_t acpi10_p;␊ |
33 | uint64_t acpi20_p;␊ |
34 | ␊ |
35 | int rsdplength;␊ |
36 | void *new_dsdt=NULL;␊ |
37 | ␊ |
38 | extern char* gSMBIOSBoardModel;␊ |
39 | ␊ |
40 | // Slice: New signature compare function␊ |
41 | boolean_t tableSign(char *table, const char *sgn)␊ |
42 | {␊ |
43 | ␉int i;␊ |
44 | ␉for (i=0; i<4; i++) {␊ |
45 | ␉␉if ((table[i] &~0x20) != (sgn[i] &~0x20)) {␊ |
46 | ␉␉␉return false;␊ |
47 | ␉␉}␊ |
48 | ␉}␊ |
49 | ␉return true;␊ |
50 | }␊ |
51 | ␊ |
52 | char st[20];␊ |
53 | char * FourChar(char * s)␊ |
54 | {␊ |
55 | ␉int i;␊ |
56 | ␉for(i=0; i<4;i++){␊ |
57 | ␉␉st[i] = (isalpha(s[i])?s[i]:'.');␊ |
58 | ␉}␊ |
59 | ␉//␉strncpy(st, s, 4);␊ |
60 | ␉st[4] = '\0';␊ |
61 | ␉return st;␊ |
62 | }␊ |
63 | ␊ |
64 | ␊ |
65 | /* Gets the ACPI 1.0 RSDP address */␊ |
66 | static struct acpi_2_rsdp* getAddressOfAcpiTable()␊ |
67 | {␊ |
68 | /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */␊ |
69 | ␉␊ |
70 | void *acpi_addr = (void*)ACPI_RANGE_START;␊ |
71 | for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)␊ |
72 | {␊ |
73 | if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)␊ |
74 | {␊ |
75 | uint8_t csum = checksum8(acpi_addr, 20);␊ |
76 | if(csum == 0)␊ |
77 | {␊ |
78 | // Only return the table if it is a true version 1.0 table (Revision 0)␊ |
79 | if(((struct acpi_2_rsdp*)acpi_addr)->Revision == 0)␊ |
80 | return acpi_addr;␊ |
81 | }␊ |
82 | }␊ |
83 | }␊ |
84 | return NULL;␊ |
85 | }␊ |
86 | ␊ |
87 | /* Gets the ACPI 2.0 RSDP address */␊ |
88 | static struct acpi_2_rsdp* getAddressOfAcpi20Table()␊ |
89 | {␊ |
90 | /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */␊ |
91 | ␉␊ |
92 | void *acpi_addr = (void*)ACPI_RANGE_START;␊ |
93 | for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)␊ |
94 | {␊ |
95 | if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)␊ |
96 | {␊ |
97 | uint8_t csum = checksum8(acpi_addr, 20);␊ |
98 | ␉␉␉␊ |
99 | /* Only assume this is a 2.0 or better table if the revision is greater than 0␊ |
100 | * NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1␊ |
101 | * and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.␊ |
102 | */␊ |
103 | ␉␉␉␊ |
104 | if(csum == 0 && (((struct acpi_2_rsdp*)acpi_addr)->Revision > 0))␊ |
105 | {␊ |
106 | uint8_t csum2 = checksum8(acpi_addr, sizeof(struct acpi_2_rsdp));␊ |
107 | if(csum2 == 0)␊ |
108 | return acpi_addr;␊ |
109 | }␊ |
110 | }␊ |
111 | }␊ |
112 | return NULL;␊ |
113 | }␊ |
114 | /** The folowing ACPI Table search algo. should be reused anywhere needed:*/␊ |
115 | int search_and_get_acpi_fd(const char * filename, const char ** outDirspec)␊ |
116 | {␊ |
117 | ␉int fd = -1;␊ |
118 | ␉char dirSpec[512] = "";␊ |
119 | ␉␊ |
120 | ␉// Try finding 'filename' in the usual places␊ |
121 | ␉// Start searching any potential location for ACPI Table␊ |
122 | ␉␊ |
123 | ␉if(gSMBIOSBoardModel)␊ |
124 | ␉{␊ |
125 | ␉␉sprintf(dirSpec,"%s.%s", gSMBIOSBoardModel, filename);␊ |
126 | ␉␉fd = open(dirSpec, 0);␊ |
127 | ␉}␊ |
128 | ␉␊ |
129 | ␉if (fd < 0)␊ |
130 | ␉{␊ |
131 | ␉␉sprintf(dirSpec, "%s", filename); ␊ |
132 | ␉␉fd = open(dirSpec, 0);␊ |
133 | ␉␉if (fd < 0)␊ |
134 | ␉␉{␉␊ |
135 | ␉␉␉if(gSMBIOSBoardModel)␊ |
136 | ␉␉␉{␊ |
137 | ␉␉␉␉sprintf(dirSpec, "/Extra/%s.%s", gSMBIOSBoardModel, filename);␊ |
138 | ␉␉␉␉fd = open(dirSpec, 0);␊ |
139 | ␉␉␉}␊ |
140 | ␉␉␉if (fd < 0)␊ |
141 | ␉␉␉{␊ |
142 | ␉␉␉␉sprintf(dirSpec, "/Extra/%s", filename); ␊ |
143 | ␉␉␉␉fd = open(dirSpec, 0);␊ |
144 | ␉␉␉␉if (fd < 0)␊ |
145 | ␉␉␉␉{␊ |
146 | ␉␉␉␉␉if(gSMBIOSBoardModel)␊ |
147 | ␉␉␉␉␉{␊ |
148 | ␉␉␉␉␉␉sprintf(dirSpec, "bt(0,0)/Extra/%s.%s", gSMBIOSBoardModel, filename);␊ |
149 | ␉␉␉␉␉␉fd = open(dirSpec, 0);␊ |
150 | ␉␉␉␉␉}␊ |
151 | ␉␉␉␉␉if (fd < 0)␊ |
152 | ␉␉␉␉␉{␉␉␉␉␉␉␊ |
153 | ␉␉␉␉␉␉sprintf(dirSpec, "bt(0,0)/Extra/%s", filename);␊ |
154 | ␉␉␉␉␉␉fd = open(dirSpec, 0);␊ |
155 | ␉␉␉␉␉}␊ |
156 | ␉␉␉␉}␊ |
157 | ␉␉␉}␊ |
158 | ␉␉}␊ |
159 | ␉}␊ |
160 | ␉␊ |
161 | ␉if (fd < 0)␊ |
162 | ␉{␊ |
163 | ␉␉// NOT FOUND:␊ |
164 | ␉␉DBG("ACPI table not found: %s\n", filename);␊ |
165 | ␉␉*dirSpec = '\0';␊ |
166 | ␉}␊ |
167 | ␊ |
168 | ␉if (outDirspec) *outDirspec = dirSpec; ␊ |
169 | ␉return fd;␊ |
170 | }␊ |
171 | ␊ |
172 | ␊ |
173 | void *loadACPITable (const char * filename)␊ |
174 | {␊ |
175 | ␉void *tableAddr;␊ |
176 | ␉const char * dirspec=NULL;␊ |
177 | ␉␊ |
178 | ␉int fd = search_and_get_acpi_fd(filename, &dirspec);␊ |
179 | ␉␊ |
180 | ␉if (fd>=0)␊ |
181 | ␉{␊ |
182 | ␉␉tableAddr=(void*)AllocateKernelMemory(file_size (fd));␊ |
183 | ␉␉if (tableAddr)␊ |
184 | ␉␉{␊ |
185 | ␉␉␉if (read (fd, tableAddr, file_size (fd))!=file_size (fd))␊ |
186 | ␉␉␉{␊ |
187 | ␉␉␉␉verbose("Couldn't read table %s\n",dirspec);␊ |
188 | ␉␉␉␉free (tableAddr);␊ |
189 | ␉␉␉␉close (fd);␊ |
190 | ␉␉␉␉return NULL;␊ |
191 | ␉␉␉}␊ |
192 | ␉␉␉␊ |
193 | ␉␉␉DBG("Table %s read and stored at: %x\n", dirspec, tableAddr);␊ |
194 | ␉␉␉close (fd);␊ |
195 | ␉␉␉return tableAddr;␊ |
196 | ␉␉}␊ |
197 | ␉␉close (fd);␊ |
198 | ␉␉verbose("Couldn't allocate memory for table \n", dirspec);␊ |
199 | ␉} ␊ |
200 | ␉//printf("Couldn't find table %s\n", filename);␊ |
201 | ␉return NULL;␊ |
202 | }␊ |
203 | ␊ |
204 | uint8_t␉acpi_cpu_count = 0;␊ |
205 | char* acpi_cpu_name[32];␊ |
206 | ␊ |
207 | void get_acpi_cpu_names(unsigned char* dsdt, uint32_t length)␊ |
208 | {␊ |
209 | ␉uint32_t i;␊ |
210 | ␉␊ |
211 | ␉for (i=0; i<length-7; i++) ␊ |
212 | ␉{␊ |
213 | ␉␉if (dsdt[i] == 0x5B && dsdt[i+1] == 0x83) // ProcessorOP␊ |
214 | ␉␉{␊ |
215 | ␉␉␉uint32_t offset = i + 3 + (dsdt[i+2] >> 6);␊ |
216 | ␉␉␉␊ |
217 | ␉␉␉bool add_name = true;␊ |
218 | ␉␉␉␊ |
219 | ␉␉␉uint8_t j;␊ |
220 | ␉␉␉␊ |
221 | ␉␉␉for (j=0; j<4; j++) ␊ |
222 | ␉␉␉{␊ |
223 | ␉␉␉␉char c = dsdt[offset+j];␊ |
224 | ␉␉␉␉␊ |
225 | ␉␉␉␉if (!aml_isvalidchar(c)) ␊ |
226 | ␉␉␉␉{␊ |
227 | ␉␉␉␉␉add_name = false;␊ |
228 | ␉␉␉␉␉DBG("Invalid character found in ProcessorOP 0x%x!\n", c);␊ |
229 | ␉␉␉␉␉break;␊ |
230 | ␉␉␉␉}␊ |
231 | ␉␉␉}␊ |
232 | ␉␉␉␊ |
233 | ␉␉␉if (add_name) ␊ |
234 | ␉␉␉{␊ |
235 | ␉␉␉␉acpi_cpu_name[acpi_cpu_count] = malloc(4);␊ |
236 | ␉␉␉␉memcpy(acpi_cpu_name[acpi_cpu_count], dsdt+offset, 4);␊ |
237 | ␉␉␉␉i = offset + 5;␊ |
238 | ␉␉␉␉␊ |
239 | ␉␉␉␉DBG("Found ACPI CPU: %c%c%c%c\n", acpi_cpu_name[acpi_cpu_count][0], acpi_cpu_name[acpi_cpu_count][1], acpi_cpu_name[acpi_cpu_count][2], acpi_cpu_name[acpi_cpu_count][3]);␊ |
240 | ␉␉␉␉␊ |
241 | ␉␉␉␉if (++acpi_cpu_count == 32) return;␊ |
242 | ␉␉␉}␊ |
243 | ␉␉}␊ |
244 | ␉}␊ |
245 | }␊ |
246 | ␊ |
247 | struct acpi_2_ssdt *generate_cst_ssdt(struct acpi_2_fadt* fadt)␊ |
248 | {␊ |
249 | ␉char ssdt_header[] =␊ |
250 | ␉{␊ |
251 | ␉␉0x53, 0x53, 0x44, 0x54, 0xE7, 0x00, 0x00, 0x00, /* SSDT.... */␊ |
252 | ␉␉0x01, 0x17, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x41, /* ..PmRefA */␊ |
253 | ␉␉0x43, 0x70, 0x75, 0x43, 0x73, 0x74, 0x00, 0x00, /* CpuCst.. */␊ |
254 | ␉␉0x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* ....INTL */␊ |
255 | ␉␉0x31, 0x03, 0x10, 0x20 ␉␉␉␉␉␉␉/* 1.._␉␉*/␊ |
256 | ␉};␊ |
257 | ␉␊ |
258 | ␉char cstate_resource_template[] = ␊ |
259 | ␉{␊ |
260 | ␉␉0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, ␊ |
261 | ␉␉0x01, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, ␊ |
262 | ␉␉0x00, 0x00, 0x00, 0x79, 0x00␊ |
263 | ␉};␊ |
264 | ␉␊ |
265 | ␉if (Platform->CPU.Vendor != 0x756E6547) {␊ |
266 | ␉␉verbose ("Not an Intel platform: C-States will not be generated !!!\n");␊ |
267 | ␉␉return NULL;␊ |
268 | ␉}␊ |
269 | ␉␊ |
270 | ␉if (fadt == NULL) {␊ |
271 | ␉␉verbose ("FACP not exists: C-States will not be generated !!!\n");␊ |
272 | ␉␉return NULL;␊ |
273 | ␉}␊ |
274 | ␉␊ |
275 | ␉struct acpi_2_dsdt* dsdt = (void*)fadt->DSDT;␊ |
276 | ␉␊ |
277 | ␉if (dsdt == NULL) {␊ |
278 | ␉␉verbose ("DSDT not found: C-States will not be generated !!!\n");␊ |
279 | ␉␉return NULL;␊ |
280 | ␉}␊ |
281 | ␉␊ |
282 | ␉if (acpi_cpu_count == 0) ␊ |
283 | ␉␉get_acpi_cpu_names((void*)dsdt, dsdt->Length);␊ |
284 | ␉␊ |
285 | ␉if (acpi_cpu_count > 0) ␊ |
286 | ␉{␊ |
287 | ␉␉bool c2_enabled = false;␊ |
288 | ␉␉bool c3_enabled = false;␊ |
289 | ␉␉bool c4_enabled = false;␊ |
290 | ␉␉␊ |
291 | ␉␉getBoolForKey(kEnableC2States, &c2_enabled, &bootInfo->bootConfig);␊ |
292 | ␉␉getBoolForKey(kEnableC3States, &c3_enabled, &bootInfo->bootConfig);␊ |
293 | ␉␉getBoolForKey(kEnableC4States, &c4_enabled, &bootInfo->bootConfig);␊ |
294 | ␉␉␊ |
295 | ␉␉c2_enabled = c2_enabled | (fadt->C2_Latency < 100);␊ |
296 | ␉␉c3_enabled = c3_enabled | (fadt->C3_Latency < 1000);␊ |
297 | ␊ |
298 | ␉␉unsigned char cstates_count = 1 + (c2_enabled ? 1 : 0) + (c3_enabled ? 1 : 0);␊ |
299 | ␉␉␊ |
300 | ␉␉struct aml_chunk* root = aml_create_node(NULL);␊ |
301 | ␉␉aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header␊ |
302 | ␉␉struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");␊ |
303 | ␉␉struct aml_chunk* name = aml_add_name(scop, "CST_");␊ |
304 | ␉␉struct aml_chunk* pack = aml_add_package(name);␊ |
305 | ␉␉aml_add_byte(pack, cstates_count);␊ |
306 | ␉␉␊ |
307 | ␉␉struct aml_chunk* tmpl = aml_add_package(pack);␊ |
308 | ␉␉cstate_resource_template[11] = 0x00; // C1␊ |
309 | ␉␉aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));␊ |
310 | ␉␉aml_add_byte(tmpl, 0x01); // C1␊ |
311 | ␉␉aml_add_byte(tmpl, 0x01); // Latency␊ |
312 | ␉␉aml_add_word(tmpl, 0x03e8); // Power␊ |
313 | ␉␉␊ |
314 | ␉␉// C2␊ |
315 | ␉␉if (c2_enabled) ␊ |
316 | ␉␉{␊ |
317 | ␉␉␉tmpl = aml_add_package(pack);␊ |
318 | ␉␉␉cstate_resource_template[11] = 0x10; // C2␊ |
319 | ␉␉␉aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));␊ |
320 | ␉␉␉aml_add_byte(tmpl, 0x02); // C2␊ |
321 | ␉␉␉aml_add_byte(tmpl, fadt->C2_Latency);␊ |
322 | ␉␉␉aml_add_word(tmpl, 0x01f4); // Power␊ |
323 | ␉␉}␊ |
324 | ␉␉// C4␊ |
325 | ␉␉if (c4_enabled) ␊ |
326 | ␉␉{␊ |
327 | ␉␉␉tmpl = aml_add_package(pack);␊ |
328 | ␉␉␉cstate_resource_template[11] = 0x30; // C4␊ |
329 | ␉␉␉aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));␊ |
330 | ␉␉␉aml_add_byte(tmpl, 0x04); // C4␊ |
331 | ␉␉␉aml_add_word(tmpl, fadt->C3_Latency / 2); // TODO: right latency for C4␊ |
332 | ␉␉␉aml_add_byte(tmpl, 0xfa); // Power␊ |
333 | ␉␉}␊ |
334 | ␉␉else␊ |
335 | ␉␉␉// C3␊ |
336 | ␉␉␉if (c3_enabled) ␊ |
337 | ␉␉␉{␊ |
338 | ␉␉␉␉tmpl = aml_add_package(pack);␊ |
339 | ␉␉␉␉cstate_resource_template[11] = 0x20; // C3␊ |
340 | ␉␉␉␉aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));␊ |
341 | ␉␉␉␉aml_add_byte(tmpl, 0x03); // C3␊ |
342 | ␉␉␉␉aml_add_word(tmpl, fadt->C3_Latency);␊ |
343 | ␉␉␉␉aml_add_word(tmpl, 0x015e); // Power␊ |
344 | ␉␉␉}␊ |
345 | ␉␉␊ |
346 | ␉␉␊ |
347 | ␉␉// Aliaces␊ |
348 | ␉␉int i;␊ |
349 | ␉␉for (i = 0; i < acpi_cpu_count; i++) ␊ |
350 | ␉␉{␊ |
351 | ␉␉␉char name[9];␊ |
352 | ␉␉␉sprintf(name, "_PR_%c%c%c%c", acpi_cpu_name[i][0], acpi_cpu_name[i][1], acpi_cpu_name[i][2], acpi_cpu_name[i][3]);␊ |
353 | ␉␉␉␊ |
354 | ␉␉␉scop = aml_add_scope(root, name);␊ |
355 | ␉␉␉aml_add_alias(scop, "CST_", "_CST");␊ |
356 | ␉␉}␊ |
357 | ␉␉␊ |
358 | ␉␉aml_calculate_size(root);␊ |
359 | ␉␉␊ |
360 | ␉␉struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);␊ |
361 | ␉␉␊ |
362 | ␉␉aml_write_node(root, (void*)ssdt, 0);␊ |
363 | ␉␉␊ |
364 | ␉␉ssdt->Length = root->Size;␊ |
365 | ␉␉ssdt->Checksum = 0;␊ |
366 | ␉␉ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);␊ |
367 | ␉␉␊ |
368 | ␉␉aml_destroy_node(root);␊ |
369 | ␉␉␊ |
370 | ␉␉//dumpPhysAddr("C-States SSDT content: ", ssdt, ssdt->Length);␊ |
371 | ␉␉␊ |
372 | ␉␉verbose ("SSDT with CPU C-States generated successfully\n");␊ |
373 | ␉␉␊ |
374 | ␉␉return ssdt;␊ |
375 | ␉}␊ |
376 | ␉else ␊ |
377 | ␉{␊ |
378 | ␉␉verbose ("ACPI CPUs not found: C-States not generated !!!\n");␊ |
379 | ␉}␊ |
380 | ␉␊ |
381 | ␉return NULL;␊ |
382 | }␊ |
383 | ␊ |
384 | struct acpi_2_ssdt *generate_pss_ssdt(struct acpi_2_dsdt* dsdt)␊ |
385 | {␉␊ |
386 | ␉char ssdt_header[] =␊ |
387 | ␉{␊ |
388 | ␉␉0x53, 0x53, 0x44, 0x54, 0x7E, 0x00, 0x00, 0x00, /* SSDT.... */␊ |
389 | ␉␉0x01, 0x6A, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x00, /* ..PmRef. */␊ |
390 | ␉␉0x43, 0x70, 0x75, 0x50, 0x6D, 0x00, 0x00, 0x00, /* CpuPm... */␊ |
391 | ␉␉0x00, 0x30, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* .0..INTL */␊ |
392 | ␉␉0x31, 0x03, 0x10, 0x20,␉␉␉␉␉␉␉/* 1.._␉␉*/␊ |
393 | ␉};␊ |
394 | ␉cpuid_update_generic_info();␊ |
395 | ␉if (Platform->CPU.Vendor != 0x756E6547) {␊ |
396 | ␉␉verbose ("Not an Intel platform: P-States will not be generated !!!\n");␊ |
397 | ␉␉return NULL;␊ |
398 | ␉}␊ |
399 | ␉␊ |
400 | ␉if (!(Platform->CPU.Features & CPU_FEATURE_MSR)) {␊ |
401 | ␉␉verbose ("Unsupported CPU: P-States will not be generated !!!\n");␊ |
402 | ␉␉return NULL;␊ |
403 | ␉}␊ |
404 | ␉␊ |
405 | ␉if (acpi_cpu_count == 0) ␊ |
406 | ␉␉get_acpi_cpu_names((void*)dsdt, dsdt->Length);␊ |
407 | ␉␊ |
408 | ␉if (acpi_cpu_count > 0) ␊ |
409 | ␉{␊ |
410 | ␉␉struct p_state initial, maximum, minimum, p_states[32];␊ |
411 | ␉␉uint8_t p_states_count = 0;␉␉␊ |
412 | ␉␉␊ |
413 | ␉␉// Retrieving P-States, ported from code by superhai (c)␊ |
414 | ␉␉switch (Platform->CPU.Family) {␊ |
415 | ␉␉␉case 0x06: ␊ |
416 | ␉␉␉{␊ |
417 | ␉␉␉␉switch (Platform->CPU.Model) ␊ |
418 | ␉␉␉␉{␊ |
419 | ␉␉␉␉␉case CPU_MODEL_PENTIUM_M: // ?␊ |
420 | ␉␉␉␉␉case CPU_MODEL_YONAH: // Yonah␊ |
421 | ␉␉␉␉␉case CPU_MODEL_MEROM: // Merom␊ |
422 | ␉␉␉␉␉case CPU_MODEL_PENRYN: // Penryn␊ |
423 | ␉␉␉␉␉case CPU_MODEL_ATOM: // Intel Atom (45nm)␊ |
424 | ␉␉␉␉␉{␊ |
425 | ␉␉␉␉␉␉bool cpu_dynamic_fsb = false;␊ |
426 | ␉␉␉␉␉␉␊ |
427 | ␉␉␉␉␉␉if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27)) ␊ |
428 | ␉␉␉␉␉␉{␊ |
429 | ␉␉␉␉␉␉␉wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28))); ␊ |
430 | ␉␉␉␉␉␉␉delay(1);␊ |
431 | ␉␉␉␉␉␉␉cpu_dynamic_fsb = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);␊ |
432 | ␉␉␉␉␉␉}␊ |
433 | ␉␉␉␉␉␉␊ |
434 | ␉␉␉␉␉␉bool cpu_noninteger_bus_ratio = (rdmsr64(MSR_IA32_PERF_STATUS) & (1ULL << 46));␊ |
435 | ␉␉␉␉␉␉␊ |
436 | ␉␉␉␉␉␉initial.Control = rdmsr64(MSR_IA32_PERF_STATUS);␊ |
437 | ␉␉␉␉␉␉␊ |
438 | ␉␉␉␉␉␉maximum.Control = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 32) & 0x1F3F) | (0x4000 * cpu_noninteger_bus_ratio);␊ |
439 | ␉␉␉␉␉␉maximum.CID = ((maximum.FID & 0x1F) << 1) | cpu_noninteger_bus_ratio;␊ |
440 | ␉␉␉␉␉␉␊ |
441 | ␉␉␉␉␉␉minimum.FID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 24) & 0x1F) | (0x80 * cpu_dynamic_fsb);␊ |
442 | ␉␉␉␉␉␉minimum.VID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 48) & 0x3F);␊ |
443 | ␉␉␉␉␉␉␊ |
444 | ␉␉␉␉␉␉if (minimum.FID == 0) ␊ |
445 | ␉␉␉␉␉␉{␊ |
446 | ␉␉␉␉␉␉␉uint64_t msr;␊ |
447 | ␉␉␉␉␉␉␉uint8_t i;␊ |
448 | ␉␉␉␉␉␉␉// Probe for lowest fid␊ |
449 | ␉␉␉␉␉␉␉for (i = maximum.FID; i >= 0x6; i--) ␊ |
450 | ␉␉␉␉␉␉␉{␊ |
451 | ␉␉␉␉␉␉␉␉msr = rdmsr64(MSR_IA32_PERF_CONTROL);␊ |
452 | ␉␉␉␉␉␉␉␉wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (i << 8) | minimum.VID);␊ |
453 | ␉␉␉␉␉␉␉␉intel_waitforsts();␊ |
454 | ␉␉␉␉␉␉␉␉minimum.FID = (rdmsr64(MSR_IA32_PERF_STATUS) >> 8) & 0x1F; ␊ |
455 | ␉␉␉␉␉␉␉␉delay(1);␊ |
456 | ␉␉␉␉␉␉␉}␊ |
457 | ␉␉␉␉␉␉␉␊ |
458 | ␉␉␉␉␉␉␉msr = rdmsr64(MSR_IA32_PERF_CONTROL);␊ |
459 | ␉␉␉␉␉␉␉wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);␊ |
460 | ␉␉␉␉␉␉␉intel_waitforsts();␊ |
461 | ␉␉␉␉␉␉}␊ |
462 | ␉␉␉␉␉␉␊ |
463 | ␉␉␉␉␉␉if (minimum.VID == maximum.VID) ␊ |
464 | ␉␉␉␉␉␉{␉␊ |
465 | ␉␉␉␉␉␉␉uint64_t msr;␊ |
466 | ␉␉␉␉␉␉␉uint8_t i;␊ |
467 | ␉␉␉␉␉␉␉// Probe for lowest vid␊ |
468 | ␉␉␉␉␉␉␉for (i = maximum.VID; i > 0xA; i--) ␊ |
469 | ␉␉␉␉␉␉␉{␊ |
470 | ␉␉␉␉␉␉␉␉msr = rdmsr64(MSR_IA32_PERF_CONTROL);␊ |
471 | ␉␉␉␉␉␉␉␉wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (minimum.FID << 8) | i);␊ |
472 | ␉␉␉␉␉␉␉␉intel_waitforsts();␊ |
473 | ␉␉␉␉␉␉␉␉minimum.VID = rdmsr64(MSR_IA32_PERF_STATUS) & 0x3F; ␊ |
474 | ␉␉␉␉␉␉␉␉delay(1);␊ |
475 | ␉␉␉␉␉␉␉}␊ |
476 | ␉␉␉␉␉␉␉␊ |
477 | ␉␉␉␉␉␉␉msr = rdmsr64(MSR_IA32_PERF_CONTROL);␊ |
478 | ␉␉␉␉␉␉␉wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);␊ |
479 | ␉␉␉␉␉␉␉intel_waitforsts();␊ |
480 | ␉␉␉␉␉␉}␊ |
481 | ␉␉␉␉␉␉␊ |
482 | ␉␉␉␉␉␉minimum.CID = ((minimum.FID & 0x1F) << 1) >> cpu_dynamic_fsb;␊ |
483 | ␉␉␉␉␉␉␊ |
484 | ␉␉␉␉␉␉// Sanity check␊ |
485 | ␉␉␉␉␉␉if (maximum.CID < minimum.CID) ␊ |
486 | ␉␉␉␉␉␉{␊ |
487 | ␉␉␉␉␉␉␉DBG("Insane FID values!");␊ |
488 | ␉␉␉␉␉␉␉p_states_count = 0;␊ |
489 | ␉␉␉␉␉␉}␊ |
490 | ␉␉␉␉␉␉else␊ |
491 | ␉␉␉␉␉␉{␊ |
492 | ␉␉␉␉␉␉␉// Finalize P-States␊ |
493 | ␉␉␉␉␉␉␉// Find how many P-States machine supports␊ |
494 | ␉␉␉␉␉␉␉p_states_count = maximum.CID - minimum.CID + 1;␊ |
495 | ␉␉␉␉␉␉␉␊ |
496 | ␉␉␉␉␉␉␉if (p_states_count > 32) ␊ |
497 | ␉␉␉␉␉␉␉␉p_states_count = 32;␊ |
498 | ␉␉␉␉␉␉␉␊ |
499 | ␉␉␉␉␉␉␉uint8_t vidstep;␊ |
500 | ␉␉␉␉␉␉␉uint8_t i = 0, u, invalid = 0;␊ |
501 | ␉␉␉␉␉␉␉␊ |
502 | ␉␉␉␉␉␉␉vidstep = ((maximum.VID << 2) - (minimum.VID << 2)) / (p_states_count - 1);␊ |
503 | ␉␉␉␉␉␉␉␊ |
504 | ␉␉␉␉␉␉␉for (u = 0; u < p_states_count; u++) ␊ |
505 | ␉␉␉␉␉␉␉{␊ |
506 | ␉␉␉␉␉␉␉␉i = u - invalid;␊ |
507 | ␉␉␉␉␉␉␉␉␊ |
508 | ␉␉␉␉␉␉␉␉p_states[i].CID = maximum.CID - u;␊ |
509 | ␉␉␉␉␉␉␉␉p_states[i].FID = (p_states[i].CID >> 1);␊ |
510 | ␉␉␉␉␉␉␉␉␊ |
511 | ␉␉␉␉␉␉␉␉if (p_states[i].FID < 0x6) ␊ |
512 | ␉␉␉␉␉␉␉␉{␊ |
513 | ␉␉␉␉␉␉␉␉␉if (cpu_dynamic_fsb) ␊ |
514 | ␉␉␉␉␉␉␉␉␉␉p_states[i].FID = (p_states[i].FID << 1) | 0x80;␊ |
515 | ␉␉␉␉␉␉␉␉} ␊ |
516 | ␉␉␉␉␉␉␉␉else if (cpu_noninteger_bus_ratio) ␊ |
517 | ␉␉␉␉␉␉␉␉{␊ |
518 | ␉␉␉␉␉␉␉␉␉p_states[i].FID = p_states[i].FID | (0x40 * (p_states[i].CID & 0x1));␊ |
519 | ␉␉␉␉␉␉␉␉}␊ |
520 | ␉␉␉␉␉␉␉␉␊ |
521 | ␉␉␉␉␉␉␉␉if (i && p_states[i].FID == p_states[i-1].FID)␊ |
522 | ␉␉␉␉␉␉␉␉␉invalid++;␊ |
523 | ␉␉␉␉␉␉␉␉␊ |
524 | ␉␉␉␉␉␉␉␉p_states[i].VID = ((maximum.VID << 2) - (vidstep * u)) >> 2;␊ |
525 | ␉␉␉␉␉␉␉␉␊ |
526 | ␉␉␉␉␉␉␉␉uint32_t multiplier = p_states[i].FID & 0x1f;␉␉// = 0x08␊ |
527 | ␉␉␉␉␉␉␉␉bool half = p_states[i].FID & 0x40;␉␉␉␉␉// = 0x01␊ |
528 | ␉␉␉␉␉␉␉␉bool dfsb = p_states[i].FID & 0x80;␉␉␉␉␉// = 0x00␊ |
529 | ␉␉␉␉␉␉␉␉uint32_t fsb = Platform->CPU.FSBFrequency / 1000000; // = 400␊ |
530 | ␉␉␉␉␉␉␉␉uint32_t halffsb = (fsb + 1) >> 1;␉␉␉␉␉// = 200␊ |
531 | ␉␉␉␉␉␉␉␉uint32_t frequency = (multiplier * fsb);␉␉␉// = 3200␊ |
532 | ␉␉␉␉␉␉␉␉␊ |
533 | ␉␉␉␉␉␉␉␉p_states[i].Frequency = (frequency + (half * halffsb)) >> dfsb;␉// = 3200 + 200 = 3400␊ |
534 | ␉␉␉␉␉␉␉}␊ |
535 | ␉␉␉␉␉␉␉␊ |
536 | ␉␉␉␉␉␉␉p_states_count -= invalid;␊ |
537 | ␉␉␉␉␉␉}␊ |
538 | ␉␉␉␉␉} break;␊ |
539 | ␉␉␉␉␉case CPU_MODEL_FIELDS:␊ |
540 | ␉␉␉␉␉case CPU_MODEL_DALES:␊ |
541 | ␉␉␉␉␉case CPU_MODEL_DALES_32NM:␊ |
542 | ␉␉␉␉␉case CPU_MODEL_NEHALEM: ␊ |
543 | ␉␉␉␉␉case CPU_MODEL_NEHALEM_EX:␊ |
544 | ␉␉␉␉␉case CPU_MODEL_WESTMERE:␊ |
545 | ␉␉␉␉␉case CPU_MODEL_WESTMERE_EX:␊ |
546 | ␉␉␉␉␉{␊ |
547 | ␉␉␉␉␉␉maximum.Control = rdmsr64(MSR_IA32_PERF_STATUS) & 0xff; // Seems it always contains maximum multiplier value (with turbo, that's we need)...␊ |
548 | ␉␉␉␉␉␉minimum.Control = (rdmsr64(MSR_PLATFORM_INFO) >> 40) & 0xff;␊ |
549 | ␉␉␉␉␉␉␊ |
550 | ␉␉␉␉␉␉verbose("P-States: min 0x%x, max 0x%x\n", minimum.Control, maximum.Control);␉␉␉␊ |
551 | ␉␉␉␉␉␉␊ |
552 | ␉␉␉␉␉␉// Sanity check␊ |
553 | ␉␉␉␉␉␉if (maximum.Control < minimum.Control) ␊ |
554 | ␉␉␉␉␉␉{␊ |
555 | ␉␉␉␉␉␉␉DBG("Insane control values!");␊ |
556 | ␉␉␉␉␉␉␉p_states_count = 0;␊ |
557 | ␉␉␉␉␉␉}␊ |
558 | ␉␉␉␉␉␉else␊ |
559 | ␉␉␉␉␉␉{␊ |
560 | ␉␉␉␉␉␉␉uint8_t i;␊ |
561 | ␉␉␉␉␉␉␉p_states_count = 0;␊ |
562 | ␉␉␉␉␉␉␉␊ |
563 | ␉␉␉␉␉␉␉for (i = maximum.Control; i >= minimum.Control; i--) ␊ |
564 | ␉␉␉␉␉␉␉{␊ |
565 | ␉␉␉␉␉␉␉␉p_states[p_states_count].Control = i;␊ |
566 | ␉␉␉␉␉␉␉␉p_states[p_states_count].CID = p_states[p_states_count].Control << 1;␊ |
567 | ␉␉␉␉␉␉␉␉p_states[p_states_count].Frequency = (Platform->CPU.FSBFrequency / 1000000) * i;␊ |
568 | ␉␉␉␉␉␉␉␉p_states_count++;␊ |
569 | ␉␉␉␉␉␉␉}␊ |
570 | ␉␉␉␉␉␉}␊ |
571 | ␉␉␉␉␉␉␊ |
572 | ␉␉␉␉␉␉break;␊ |
573 | ␉␉␉␉␉}␉␊ |
574 | ␉␉␉␉␉default:␊ |
575 | ␉␉␉␉␉␉verbose ("Unsupported CPU (0x%X): P-States not generated !!!\n", Platform->CPU.Family);␊ |
576 | ␉␉␉␉␉␉break;␊ |
577 | ␉␉␉␉}␊ |
578 | ␉␉␉}␊ |
579 | ␉␉}␊ |
580 | ␉␉␊ |
581 | ␉␉// Generating SSDT␊ |
582 | ␉␉if (p_states_count > 0) ␊ |
583 | ␉␉{␉␊ |
584 | ␉␉␉int i;␊ |
585 | ␉␉␉␊ |
586 | ␉␉␉struct aml_chunk* root = aml_create_node(NULL);␊ |
587 | ␉␉␉aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header␊ |
588 | ␉␉␉struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");␊ |
589 | ␉␉␉struct aml_chunk* name = aml_add_name(scop, "PSS_");␊ |
590 | ␉␉␉struct aml_chunk* pack = aml_add_package(name);␊ |
591 | ␉␉␉␊ |
592 | ␉␉␉for (i = 0; i < p_states_count; i++) ␊ |
593 | ␉␉␉{␊ |
594 | ␉␉␉␉struct aml_chunk* pstt = aml_add_package(pack);␊ |
595 | ␉␉␉␉␊ |
596 | ␉␉␉␉aml_add_dword(pstt, p_states[i].Frequency);␊ |
597 | ␉␉␉␉aml_add_dword(pstt, 0x00000000); // Power␊ |
598 | ␉␉␉␉aml_add_dword(pstt, 0x0000000A); // Latency␊ |
599 | ␉␉␉␉aml_add_dword(pstt, 0x0000000A); // Latency␊ |
600 | ␉␉␉␉aml_add_dword(pstt, p_states[i].Control);␊ |
601 | ␉␉␉␉aml_add_dword(pstt, i+1); // Status␊ |
602 | ␉␉␉}␊ |
603 | ␉␉␉␊ |
604 | ␉␉␉// Add aliaces␊ |
605 | ␉␉␉for (i = 0; i < acpi_cpu_count; i++) ␊ |
606 | ␉␉␉{␊ |
607 | ␉␉␉␉char name[9];␊ |
608 | ␉␉␉␉sprintf(name, "_PR_%c%c%c%c", acpi_cpu_name[i][0], acpi_cpu_name[i][1], acpi_cpu_name[i][2], acpi_cpu_name[i][3]);␊ |
609 | ␉␉␉␉␊ |
610 | ␉␉␉␉scop = aml_add_scope(root, name);␊ |
611 | ␉␉␉␉aml_add_alias(scop, "PSS_", "_PSS");␊ |
612 | ␉␉␉}␊ |
613 | ␉␉␉␊ |
614 | ␉␉␉aml_calculate_size(root);␊ |
615 | ␉␉␉␊ |
616 | ␉␉␉struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);␊ |
617 | ␉␉␉␊ |
618 | ␉␉␉aml_write_node(root, (void*)ssdt, 0);␊ |
619 | ␉␉␉␊ |
620 | ␉␉␉ssdt->Length = root->Size;␊ |
621 | ␉␉␉ssdt->Checksum = 0;␊ |
622 | ␉␉␉ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);␊ |
623 | ␉␉␉␊ |
624 | ␉␉␉aml_destroy_node(root);␊ |
625 | ␉␉␉␊ |
626 | ␉␉␉//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);␊ |
627 | ␉␉␉␊ |
628 | ␉␉␉verbose ("SSDT with CPU P-States generated successfully\n");␊ |
629 | ␉␉␉␊ |
630 | ␉␉␉return ssdt;␊ |
631 | ␉␉}␊ |
632 | ␉}␊ |
633 | ␉else ␊ |
634 | ␉{␊ |
635 | ␉␉verbose ("ACPI CPUs not found: P-States not generated !!!\n");␊ |
636 | ␉}␊ |
637 | ␉␊ |
638 | ␉return NULL;␊ |
639 | }␊ |
640 | ␊ |
641 | struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)␊ |
642 | {␊ |
643 | ␉extern void setupSystemType(); ␊ |
644 | ␉␊ |
645 | ␉struct acpi_2_fadt *fadt_mod = NULL;␊ |
646 | ␉bool fadt_rev2_needed = false;␊ |
647 | //␉bool fix_restart;␊ |
648 | ␉const char * value;␊ |
649 | ␉␊ |
650 | ␉// Restart Fix␊ |
651 | ␉if (Platform->CPU.Vendor == 0x756E6547) {␉/* Intel */␊ |
652 | ␉␉fix_restart = true;␊ |
653 | ␉␉getBoolForKey(kRestartFix, &fix_restart, &bootInfo->bootConfig);␊ |
654 | ␉} else {␊ |
655 | ␉␉DBG ("Not an Intel platform: Restart Fix not applied !!!\n");␊ |
656 | ␉␉fix_restart = false;␊ |
657 | ␉}␊ |
658 | ␉␊ |
659 | ␉if (fix_restart) fadt_rev2_needed = true;␊ |
660 | ␉␊ |
661 | ␉// Allocate new fadt table␊ |
662 | ␉if (fadt->Length < 0x84 && fadt_rev2_needed)␊ |
663 | ␉{␊ |
664 | ␉␉fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);␊ |
665 | ␉␉memcpy(fadt_mod, fadt, fadt->Length);␊ |
666 | ␉␉fadt_mod->Length = 0x84;␊ |
667 | ␉␉fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)␊ |
668 | ␉}␊ |
669 | ␉else␊ |
670 | ␉{␊ |
671 | ␉␉fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);␊ |
672 | ␉␉memcpy(fadt_mod, fadt, fadt->Length);␊ |
673 | ␉}␊ |
674 | ␉// Determine system type / PM_Model␊ |
675 | ␉if (fadt_mod && Platform->Type) {␊ |
676 | ␉␉fadt_mod->PM_Profile = Platform->Type;␊ |
677 | ␉} else if(fadt)␊ |
678 | ␉␉Platform->Type = fadt->PM_Profile;␊ |
679 | ␉else␊ |
680 | ␉␉Platform->Type = 1;␊ |
681 | ␉␊ |
682 | ␉//User override default value␊ |
683 | ␉if ( (value=getStringForKey(kSystemType, &bootInfo->bootConfig))!=NULL)␊ |
684 | ␉{␊ |
685 | ␉␉if (Platform->Type > 6) ␊ |
686 | ␉␉{␊ |
687 | ␉␉␉if(fadt_mod->PM_Profile<=6)␊ |
688 | ␉␉␉␉Platform->Type = fadt_mod->PM_Profile; // get the fadt if correct␊ |
689 | ␉␉␉else ␊ |
690 | ␉␉␉␉Platform->Type = 1;␉␉/* Set a fixed value (Desktop) */␊ |
691 | ␉␉␉verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform->Type);␊ |
692 | ␉␉}␊ |
693 | ␉␉else␊ |
694 | ␉␉␉Platform->Type = (unsigned char) strtoul(value, NULL, 10);␊ |
695 | ␉}␊ |
696 | ␉// Set PM_Profile from System-type if only user wanted this value to be forced␊ |
697 | ␉if (fadt_mod->PM_Profile != Platform->Type) ␊ |
698 | ␉{␊ |
699 | ␉ if (value) ␊ |
700 | ␉␉{ // user has overriden the SystemType so take care of it in FACP␊ |
701 | ␉␉␉DBG("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform->Type);␊ |
702 | ␉␉␉fadt_mod->PM_Profile = Platform->Type;␊ |
703 | ␉ }␊ |
704 | ␉ else␊ |
705 | ␉ { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs␊ |
706 | ␉␉␉Platform->Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;␊ |
707 | ␉ } ␊ |
708 | ␉}␊ |
709 | ␉// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()␊ |
710 | ␉// because we need to take care of facp original content, if it is correct.␊ |
711 | ␉setupSystemType();␊ |
712 | ␉␊ |
713 | ␉// Patch FADT to fix restart␊ |
714 | ␉if (fix_restart)␊ |
715 | ␉{␊ |
716 | ␉␉fadt_mod->Flags|= 0x400;␊ |
717 | ␉␉fadt_mod->Reset_SpaceID␉␉= 0x01; // System I/O␊ |
718 | ␉␉fadt_mod->Reset_BitWidth␉= 0x08; // 1 byte␊ |
719 | ␉␉fadt_mod->Reset_BitOffset␉= 0x00; // Offset 0␊ |
720 | ␉␉fadt_mod->Reset_AccessWidth␉= 0x01; // Byte access␊ |
721 | ␉␉fadt_mod->Reset_Address␉␉= 0x0cf9; // Address of the register␊ |
722 | ␉␉fadt_mod->Reset_Value␉␉= 0x06; // Value to write to reset the system␊ |
723 | ␉␉msglog("FADT: Restart Fix applied!\n");␊ |
724 | ␉}␊ |
725 | ␉␊ |
726 | ␉// Patch DSDT Address if we have loaded DSDT.aml␊ |
727 | ␉if(new_dsdt)␊ |
728 | ␉{␊ |
729 | ␉␉DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);␊ |
730 | ␉␉␊ |
731 | ␉␉// Insert old dsdt into the IORegistery␊ |
732 | ␉␉Node* node = DT__FindNode("/dsdt", false);␊ |
733 | ␉␉if(node == NULL)␊ |
734 | ␉␉{␊ |
735 | ␉␉␉// Only add if not already here␊ |
736 | ␉␉␉Node* node = DT__FindNode("/", false);␊ |
737 | ␊ |
738 | ␉␉␉if(node != NULL)␊ |
739 | ␉␉␉{␊ |
740 | ␉␉␉␉node = DT__AddChild(node, "dsdt");␊ |
741 | ␉␉␉␉␊ |
742 | ␉␉␉␉struct acpi_2_dsdt *dsdt;␊ |
743 | ␉␉␉␉dsdt = (struct acpi_2_dsdt*) (fadt_mod->DSDT);␊ |
744 | ␉␉␉␉DT__AddProperty(node, "originaldsdt", (dsdt->Length + sizeof(struct acpi_2_dsdt) - 1), (void*)dsdt);␉/// Insert old dsdt. Length is header length (36) + dsdt length␊ |
745 | ␉␉␉␉␊ |
746 | ␉␉␉}␊ |
747 | ␉␉}␉␉␉␊ |
748 | ␉␉␊ |
749 | ␉␉fadt_mod->DSDT=(uint32_t)new_dsdt;␊ |
750 | ␉␉if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)␊ |
751 | ␉␉␉fadt_mod->X_DSDT=(uint32_t)new_dsdt;␊ |
752 | ␉␉␊ |
753 | ␉␉DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);␊ |
754 | ␉␉␊ |
755 | ␉␉DBG("FADT: Using custom DSDT!\n");␊ |
756 | ␉}␊ |
757 | ␉␊ |
758 | ␉// Correct the checksum␊ |
759 | ␉fadt_mod->Checksum=0;␊ |
760 | ␉fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);␊ |
761 | ␉␊ |
762 | ␉return fadt_mod;␊ |
763 | }␊ |
764 | ␊ |
765 | /* Setup ACPI without replacing DSDT. */␊ |
766 | int setupAcpiNoMod()␊ |
767 | {␊ |
768 | ␉//␉addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");␊ |
769 | ␉//␉addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");␊ |
770 | ␉/* XXX aserebln why uint32 cast if pointer is uint64 ? */␊ |
771 | ␉//Slice (uint64_t)␊ |
772 | ␉acpi10_p = (uint64_t)(uint32_t)getAddressOfAcpiTable();␊ |
773 | ␉acpi20_p = (uint64_t)(uint32_t)getAddressOfAcpi20Table();␊ |
774 | ␉addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");␊ |
775 | ␉if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");␊ |
776 | ␉else {␊ |
777 | ␉␉DBG("no ACPI 2\n");␊ |
778 | ␉}␊ |
779 | ␉return 1;␊ |
780 | }␊ |
781 | ␊ |
782 | static struct acpi_2_xsdt* createNewXSDTfromRSDT(struct acpi_2_rsdt * rsdt)␊ |
783 | {␊ |
784 | ␉struct acpi_2_xsdt *xsdt;␊ |
785 | ␉int xsdt_entries_num, i;␊ |
786 | ␉uint32_t xsdt_len;␊ |
787 | ␉if (!rsdt) {␊ |
788 | ␉␉return NULL;␊ |
789 | ␉}␊ |
790 | ␉//Create XSDT in addition to RSDT as a 64bit copy␉␊ |
791 | ␉if((rsdt->Length<0) || (rsdt->Length>1000)){␊ |
792 | ␉␉DBG(" incorrect RSDT length: %08lx\n", rsdt->Length); ␊ |
793 | #if DEBUG_DSDT␊ |
794 | ␉␉getc();␊ |
795 | #endif␊ |
796 | ␉␉return NULL;␊ |
797 | ␉}␊ |
798 | ␉xsdt_entries_num = (rsdt->Length - sizeof(struct acpi_2_rsdt)) >> 2;␊ |
799 | ␉xsdt_len = sizeof(struct acpi_2_rsdt) + (xsdt_entries_num << 3);␊ |
800 | ␉xsdt = (struct acpi_2_xsdt*)AllocateKernelMemory(xsdt_len);␊ |
801 | ␉bcopy(rsdt, xsdt, sizeof(struct acpi_2_rsdt));␊ |
802 | ␉//Change signature from RSDT to XSDT␊ |
803 | ␉strncpy(xsdt->Signature, FourChar("XSDT"), 4);␊ |
804 | ␉//Now copy address table␉␊ |
805 | ␉uint64_t *table1 = (uint64_t *)(xsdt+1);␉␉␊ |
806 | ␉uint32_t *table2 = (uint32_t *)(rsdt+1);␊ |
807 | ␉if ((xsdt_entries_num>0) && (xsdt_entries_num<20)) {␊ |
808 | ␉␉for(i=0; i<xsdt_entries_num; i++){␊ |
809 | ␉␉␉table1[i] = (uint64_t)table2[i];␊ |
810 | ␉␉}␊ |
811 | ␉} else {␊ |
812 | ␉␉DBG("RSDT entries = %d xsdtAddr = %08lx rsdtAdrr = %08lx\n", ␊ |
813 | ␉␉␉xsdt_entries_num, (uint32_t)xsdt, (uint32_t)rsdt);␊ |
814 | ␉}␊ |
815 | ␉xsdt->Length = xsdt_len;␊ |
816 | ␉//Correct checksums␉␊ |
817 | ␉xsdt->Checksum=0;␊ |
818 | ␉xsdt->Checksum=256-checksum8(xsdt,xsdt_len);␉␊ |
819 | ␉return xsdt;␊ |
820 | }␊ |
821 | ␊ |
822 | static struct acpi_2_rsdp* createNewACPI20(struct acpi_2_rsdp * old_rsdp)␊ |
823 | {␊ |
824 | ␉if(!old_rsdp) return NULL;␊ |
825 | ␉//Slice I want to copy acpi10 table to create ACPI20␊ |
826 | ␉//␉uint64_t *xsdt_entries;␊ |
827 | ␉if(*(uint64_t *)old_rsdp != ACPI_SIGNATURE_UINT64_LE){␊ |
828 | ␉␉DBG(" wrong signature %08lx\n", (*(uint64_t *)old_rsdp));␊ |
829 | ␉␉return NULL;␊ |
830 | ␉}␊ |
831 | ␉DBG(" old_rsdp = %08lx\n", (uint32_t)old_rsdp);␊ |
832 | ␉//␉if(((int)old_rsdp->Length < 0) || ((int)old_rsdp->Length > 84))␊ |
833 | ␉//␉␉return NULL;␊ |
834 | ␉//No! ACPI10_rsdp_length=20 and no such field old_rsdp->Length␊ |
835 | ␉␊ |
836 | ␉struct acpi_2_rsdp * rsdp=(struct acpi_2_rsdp *) AllocateKernelMemory(36);␊ |
837 | ␉DBG(" new_rsdp = %08lx\n", (uint32_t)rsdp);␊ |
838 | ␉bzero(rsdp, 36);␊ |
839 | ␉bcopy(old_rsdp, rsdp, 20);␊ |
840 | ␉strncpy(rsdp->OEMID, "Apple ", 6);␊ |
841 | ␉rsdp->Revision = 2;␊ |
842 | ␉rsdp->Length = 36;␊ |
843 | ␉struct acpi_2_rsdt *rsdt = (struct acpi_2_rsdt *)(rsdp->RsdtAddress); //copied from old␊ |
844 | //␉dumpRSDT(rsdt, 0);␊ |
845 | ␉rsdp->XsdtAddress = (uint64_t)(uint32_t)createNewXSDTfromRSDT(rsdt);␊ |
846 | //␉dumpRSDT((struct acpi_2_rsdt *)(uint32_t)(rsdp->XsdtAddress), 0);␉␊ |
847 | ␉rsdp->Checksum=0;␊ |
848 | ␉rsdp->Checksum=256-checksum8(rsdp,20);␊ |
849 | ␉rsdp->ExtendedChecksum=0;␊ |
850 | ␉rsdp->ExtendedChecksum=256-checksum8(rsdp,36);␉␊ |
851 | ␉return rsdp;␊ |
852 | }␊ |
853 | ␊ |
854 | ␊ |
855 | /* Setup ACPI. Replace DSDT if DSDT.aml is found */␊ |
856 | int setupAcpi(void)␊ |
857 | {␊ |
858 | ␉int version;␊ |
859 | //␉void *new_dsdt;␊ |
860 | ␉␊ |
861 | ␉const char *filename;␊ |
862 | ␉char dirSpec[128];␊ |
863 | ␉int len = 0, old_dsdt_len = 0;␊ |
864 | ␉struct acpi_2_rsdp *rsdp, *rsdp_mod, *rsdp1 = 0;␊ |
865 | ␉struct acpi_2_rsdt *rsdt, *rsdt_mod = 0;␊ |
866 | ␉struct acpi_2_rsdt *xsdt; //, *xsdt_mod = 0;␊ |
867 | ␉char* old_dsdt = 0;␊ |
868 | ␉␊ |
869 | ␉// Try using the file specified with the DSDT option␊ |
870 | ␉if (getValueForKey(kDSDT, &filename, &len, &bootInfo->bootConfig))␊ |
871 | ␉{␊ |
872 | ␉␉sprintf(dirSpec, filename);␊ |
873 | ␉}␊ |
874 | ␉else␊ |
875 | ␉{␊ |
876 | ␉␉sprintf(dirSpec, "DSDT.aml");␊ |
877 | ␉}␊ |
878 | ␉␊ |
879 | ␉// Load replacement DSDT␊ |
880 | ␉new_dsdt = loadACPITable(dirSpec);␊ |
881 | ␉if(!new_dsdt)␊ |
882 | ␉{␊ |
883 | ␉␉sprintf(dirSpec, "DSDT.%s.aml", gSMBIOSBoardModel);␊ |
884 | ␉␉new_dsdt = loadACPITable(dirSpec);␊ |
885 | ␉}␊ |
886 | ␉␊ |
887 | ␉// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present␊ |
888 | ␉/*if (!new_dsdt)␊ |
889 | ␉ {␊ |
890 | ␉ return setupAcpiNoMod();␊ |
891 | ␉ }*/␊ |
892 | ␉␊ |
893 | ␉// Mozodojo: Load additional SSDTs␊ |
894 | ␉struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst␊ |
895 | ␉int ssdt_count=0;␊ |
896 | ␉␊ |
897 | ␉// SSDT Options␊ |
898 | ␉bool drop_ssdt=false, generate_pstates=false, generate_cstates=false; ␊ |
899 | ␉␊ |
900 | ␉getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->bootConfig);␊ |
901 | ␉getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->bootConfig);␊ |
902 | ␉getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->bootConfig);␊ |
903 | ␉␊ |
904 | ␉{␊ |
905 | ␉␉int i;␊ |
906 | ␉␉␊ |
907 | ␉␉for (i=0; i<30; i++)␊ |
908 | ␉␉{␊ |
909 | ␉␉␉char filename[512];␊ |
910 | ␉␉␉␊ |
911 | ␉␉␉sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);␊ |
912 | ␉␉␉␊ |
913 | ␉␉␉if(new_ssdt[ssdt_count] = loadACPITable(filename)) ␊ |
914 | ␉␉␉{␉␉␉␉␊ |
915 | ␉␉␉␉ssdt_count++;␊ |
916 | ␉␉␉}␊ |
917 | ␉␉␉else ␊ |
918 | ␉␉␉{␊ |
919 | ␉␉␉␉break;␊ |
920 | ␉␉␉}␊ |
921 | ␉␉}␊ |
922 | ␉}␊ |
923 | ␉␊ |
924 | ␉// Do the same procedure for both versions of ACPI␊ |
925 | ␉for (version=0; version<2; version++) {␊ |
926 | //␉␉int rsdplength;␊ |
927 | ␉␉//Here version=0 for ACPI 1.0 and version=1 for ACPI 2.0␊ |
928 | ␉␉// Find original rsdp␊ |
929 | ␉␉rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());␊ |
930 | ␉␉//Slice␊ |
931 | ␉␉if (!rsdp)␊ |
932 | ␉␉{␊ |
933 | ␉␉␉if (version){␊ |
934 | ␉␉␉␉DBG("No ACPI version 2 found. Creating...\n");␊ |
935 | ␉␉␉␉rsdp=createNewACPI20(rsdp1);␊ |
936 | ␉␉␉␉if(!rsdp){␊ |
937 | ␉␉␉␉addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");␊ |
938 | ␉␉␉␉␉continue;␊ |
939 | ␉␉␉␉}␊ |
940 | ␉␉␉} else {␊ |
941 | ␉␉␉␉DBG("No ACPI version 1 found. Ignoring\n");␊ |
942 | ␉␉␉␉addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");␊ |
943 | ␉␉␉continue;␊ |
944 | ␉␉}␊ |
945 | ␉␉}␊ |
946 | ␉␉if(!version) rsdp1 = rsdp; //save to create new.␊ |
947 | ␉␉rsdplength=version?rsdp->Length:20;␊ |
948 | ␉␉␊ |
949 | ␉␉DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);␊ |
950 | ␉␉␊ |
951 | ␉␉/* FIXME: no check that memory allocation succeeded ␊ |
952 | ␉␉ * Copy and patch RSDP,RSDT, XSDT and FADT␊ |
953 | ␉␉ * For more info see ACPI Specification pages 110 and following␊ |
954 | ␉␉ */␊ |
955 | ␉␉␊ |
956 | ␉␉rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);␊ |
957 | ␉␉memcpy(rsdp_mod, rsdp, rsdplength); ␊ |
958 | ␉␉rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);␊ |
959 | ␉␉xsdt=(struct acpi_2_rsdt *)(uint32_t)(rsdp->XsdtAddress);␊ |
960 | ␉␉DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);␊ |
961 | ␉␉␊ |
962 | ␉␉//if we have RSDT table␊ |
963 | ␉␉if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)␊ |
964 | ␉␉{␊ |
965 | ␉␉␉uint32_t *rsdt_entries;␊ |
966 | ␉␉␉int rsdt_entries_num;␊ |
967 | ␉␉␉int dropoffset=0, i;␊ |
968 | ␉␉␉␊ |
969 | ␉␉␉// mozo: using malloc cos I didn't found how to free already allocated kernel memory␊ |
970 | ␉␉␉rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length); ␊ |
971 | ␉␉␉memcpy (rsdt_mod, rsdt, rsdt->Length);␊ |
972 | ␉␉␉rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;␊ |
973 | ␉␉␉rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;␊ |
974 | ␉␉␉rsdt_entries=(uint32_t *)(rsdt_mod+1);␊ |
975 | ␉␉␉for (i=0;i<rsdt_entries_num;i++)␊ |
976 | ␉␉␉{␊ |
977 | ␉␉␉␉char *table=(char *)(rsdt_entries[i]);␊ |
978 | ␉␉␉␉if (!table)␊ |
979 | ␉␉␉␉␉continue;␊ |
980 | ␉␉␉␉␊ |
981 | ␉␉␉␉DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);␊ |
982 | ␉␉␉␉␊ |
983 | ␉␉␉␉rsdt_entries[i-dropoffset]=rsdt_entries[i];␊ |
984 | ␉␉␉␉␊ |
985 | ␉␉␉␉if (drop_ssdt && tableSign(table, "SSDT"))␊ |
986 | ␉␉␉␉{␊ |
987 | ␉␉␉␉␉dropoffset++;␊ |
988 | ␉␉␉␉␉continue;␊ |
989 | ␉␉␉␉}␊ |
990 | ␉␉␉␉if (tableSign(table, "DSDT"))␊ |
991 | ␉␉␉␉{␊ |
992 | ␉␉␉␉␉DBG("DSDT found\n");␊ |
993 | ␉␉␉␉␉␊ |
994 | ␉␉␉␉␉if(new_dsdt)␊ |
995 | ␉␉␉␉␉␉rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;␊ |
996 | ␉␉␉␉␉␊ |
997 | ␉␉␉␉␉continue;␊ |
998 | ␉␉␉␉} else {␊ |
999 | ␉␉␉␉␉//Slice - Correct header to MacModel & Vendor, correct chechsum␊ |
1000 | ␉␉␉␉␉struct acpi_2_rsdt *t = (struct acpi_2_rsdt *)table;␊ |
1001 | ␉␉␉␉␉strncpy(t->OEMID, "Apple ", 6);␊ |
1002 | ␉␉␉␉␉strncpy(t->OEMTableId, MacModel, 8);␊ |
1003 | ␉␉␉␉␉t->OEMRevision = ModelRev;␊ |
1004 | ␉␉␉␉␉t->Checksum=0;␊ |
1005 | ␉␉␉␉␉t->Checksum=256-checksum8(t,t->Length);␊ |
1006 | ␉␉␉␉␉␊ |
1007 | ␉␉␉␉}␊ |
1008 | ␉␉␉␉if (tableSign(table, "FACP"))␊ |
1009 | ␉␉␉␉{␊ |
1010 | ␉␉␉␉␉struct acpi_2_fadt *fadt, *fadt_mod;␊ |
1011 | ␉␉␉␉␉fadt=(struct acpi_2_fadt *)rsdt_entries[i];␊ |
1012 | ␉␉␉␉␉␊ |
1013 | ␉␉␉␉␉DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);␊ |
1014 | ␉␉␉␉␉␊ |
1015 | ␉␉␉␉␉if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)␊ |
1016 | ␉␉␉␉␉{␊ |
1017 | ␉␉␉␉␉␉printf("FADT incorrect. Not modified\n");␊ |
1018 | ␉␉␉␉␉␉continue;␊ |
1019 | ␉␉␉␉␉}␊ |
1020 | ␉␉␉␉␉DBG("Old DSDT @%x, X_DSDT %x from FADT\n",fadt->DSDT,fadt->X_DSDT);␊ |
1021 | ␉␉␉␉␉␊ |
1022 | ␉␉␉␉␉if(version && fix_restart){ //ACPI 2.0␊ |
1023 | ␉␉␉␉␉fadt_mod = patch_fadt(fadt, new_dsdt);␊ |
1024 | ␉␉␉␉␉rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;␊ |
1025 | ␉␉␉␉␉} else {␊ |
1026 | ␉␉␉␉␉␉if (fadt && Platform->Type)␊ |
1027 | ␉␉␉␉␉␉␉fadt->PM_Profile = Platform->Type;␉//For ACPI1.0 the only patch␊ |
1028 | ␉␉␉␉␉␉fadt_mod = fadt;␊ |
1029 | ␉␉␉␉␉␉rsdt_entries[i-dropoffset]=(uint32_t)fadt;␊ |
1030 | ␉␉␉␉␉}␊ |
1031 | ␉␉␉␉␉DBG("FADT_mod table sign: %s\n", fadt_mod->Signature);␊ |
1032 | ␉␉␉␉␉//Slice␊ |
1033 | ␉␉␉␉␉//Now I want to replace DSDT in place␊ |
1034 | ␉␉␉␉␉// it is only way to patch DSDT on some platform␊ |
1035 | ␉␉␉␉␉old_dsdt = (char *)fadt->DSDT;␊ |
1036 | ␉␉␉␉␉if(!old_dsdt || !tableSign(old_dsdt, "DSDT")){␊ |
1037 | ␉␉␉␉␉␉if(fadt_mod->Length > 140) old_dsdt = (char *)(uint32_t)fadt_mod->X_DSDT;␊ |
1038 | ␉␉␉␉␉␉if(!old_dsdt || !tableSign(old_dsdt, "DSDT")) old_dsdt = (char *)new_dsdt;␊ |
1039 | ␉␉␉␉␉}␊ |
1040 | ␉␉␉␉␉DBG("New DSDT @%x, X_DSDT %x after FADT patch\n",fadt_mod->DSDT,fadt_mod->X_DSDT);␊ |
1041 | ␊ |
1042 | ␉␉␉␉␉if(tableSign(old_dsdt, "DSDT"))␊ |
1043 | ␉␉␉␉␉␉old_dsdt_len = ((struct acpi_2_rsdt *)old_dsdt)->Length;␊ |
1044 | ␉␉␉␉␉int new_dsdt_len = ((struct acpi_2_rsdt *)new_dsdt)->Length;␊ |
1045 | ␉␉␉␉␉if(new_dsdt_len > old_dsdt_len) {␊ |
1046 | ␉␉␉␉␉␉old_dsdt = (char *)new_dsdt;␊ |
1047 | ␉␉␉␉␉␉DBG(" New DSDT is longer then old\n");␊ |
1048 | ␉␉␉␉␉␉len = 0;␊ |
1049 | ␉␉␉␉␉}␊ |
1050 | ␉␉␉␉␉else len = new_dsdt_len;␊ |
1051 | ␉␉␉␉␉␊ |
1052 | ␉␉␉␉␉␊ |
1053 | ␉␉␉␉␉// Generate _CST SSDT␊ |
1054 | ␉␉␉␉␉if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))␊ |
1055 | ␉␉␉␉␉{␊ |
1056 | ␉␉␉␉␉␉generate_cstates = false; // Generate SSDT only once!␊ |
1057 | ␉␉␉␉␉␉ssdt_count++;␊ |
1058 | ␉␉␉␉␉}␊ |
1059 | ␉␉␉␉␉␊ |
1060 | ␉␉␉␉␉// Generating _PSS SSDT␊ |
1061 | ␉␉␉␉␉if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))␊ |
1062 | ␉␉␉␉␉{␊ |
1063 | ␉␉␉␉␉␉generate_pstates = false; // Generate SSDT only once!␊ |
1064 | ␉␉␉␉␉␉ssdt_count++;␊ |
1065 | ␉␉␉␉␉}␊ |
1066 | ␉␉␉␉␉␊ |
1067 | ␉␉␉␉␉continue;␊ |
1068 | ␉␉␉␉}␊ |
1069 | ␉␉␉}//for rsdt entries␊ |
1070 | ␉␉␉DBG("\n");␊ |
1071 | ␉//Slice - Correct header to MacModel & Vendor, correct chechsum␊ |
1072 | ␉␉␉strncpy(rsdt_mod->OEMID, "Apple ", 6);␊ |
1073 | ␉␉␉strncpy(rsdt_mod->OEMTableId, MacModel, 8);␊ |
1074 | ␉␉␉rsdt_mod->OEMRevision = ModelRev;␊ |
1075 | ␉␉␉␊ |
1076 | ␉␉␉// Allocate rsdt in Kernel memory area␊ |
1077 | ␉␉␉rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;␊ |
1078 | ␉␉␉struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);␊ |
1079 | ␉␉␉memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);␊ |
1080 | ␉␉␉free(rsdt_mod); rsdt_mod = rsdt_copy;␊ |
1081 | ␉␉␉rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;␊ |
1082 | ␉␉␉rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;␊ |
1083 | ␉␉␉rsdt_entries=(uint32_t *)(rsdt_mod+1);␊ |
1084 | ␉␉␉␊ |
1085 | ␉␉␉// Mozodojo: Insert additional SSDTs into RSDT␊ |
1086 | ␉␉␉if(ssdt_count>0)␊ |
1087 | ␉␉␉{␊ |
1088 | ␉␉␉␉int j;␊ |
1089 | ␉␉␉␉␊ |
1090 | ␉␉␉␉for (j=0; j<ssdt_count; j++)␊ |
1091 | ␉␉␉␉␉rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];␊ |
1092 | ␉␉␉␉␊ |
1093 | ␉␉␉␉DBG("RSDT: Added %d SSDT table(s)\n", ssdt_count);␊ |
1094 | ␉␉␉}␊ |
1095 | ␉␉␉␊ |
1096 | ␉␉␉// Correct the checksum of RSDT␊ |
1097 | //␉␉␉DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);␊ |
1098 | ␉␉␉␊ |
1099 | ␉␉␉rsdt_mod->Checksum=0;␊ |
1100 | ␉␉␉rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);␊ |
1101 | ␉␉␉␊ |
1102 | //␉␉␉DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);␊ |
1103 | ␉␉}␊ |
1104 | ␉␉else␊ |
1105 | ␉␉{␊ |
1106 | ␉␉␉rsdp_mod->RsdtAddress=0;␊ |
1107 | ␉␉␉verbose("RSDT not found or RSDT incorrect\n");␊ |
1108 | ␉␉}␊ |
1109 | ␉␉␊ |
1110 | ␉␉if (version)␊ |
1111 | ␉␉{␊ |
1112 | ␉␉␉struct acpi_2_xsdt *xsdt, *xsdt_mod;␊ |
1113 | ␉␉␉␊ |
1114 | ␉␉␉// FIXME: handle 64-bit address correctly␊ |
1115 | ␉␉␉␊ |
1116 | ␉␉␉xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);␊ |
1117 | ␉␉␉DBG("XSDT @%x;%x, Length=%d Sign=%c%c%c%c\n", (uint32_t)(rsdp->XsdtAddress>>32),␊ |
1118 | ␉␉␉␉(uint32_t)rsdp->XsdtAddress, xsdt->Length, xsdt[0], xsdt[1], xsdt[2], xsdt[3]);␊ |
1119 | ␉␉␉if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)␊ |
1120 | ␉␉␉{␊ |
1121 | ␉␉␉␉uint64_t *xsdt_entries;␊ |
1122 | ␉␉␉␉int xsdt_entries_num, i;␊ |
1123 | ␉␉␉␉int dropoffset=0;␊ |
1124 | ␉␉␉␉␊ |
1125 | ␉␉␉␉// mozo: using malloc cos I didn't found how to free already allocated kernel memory␊ |
1126 | ␉␉␉␉xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length); ␊ |
1127 | ␉␉␉␉memcpy(xsdt_mod, xsdt, xsdt->Length);␊ |
1128 | ␉␉␉␉rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;␊ |
1129 | ␉␉␉␉xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;␊ |
1130 | ␉␉␉␉xsdt_entries=(uint64_t *)(xsdt_mod+1);␊ |
1131 | ␉␉␉␉for (i=0;i<xsdt_entries_num;i++)␊ |
1132 | ␉␉␉␉{␊ |
1133 | ␉␉␉␉␉char *table=(char *)((uint32_t)(xsdt_entries[i]));␊ |
1134 | ␉␉␉␉␉if (!table)␊ |
1135 | ␉␉␉␉␉␉continue;␊ |
1136 | ␉␉␉␉␉␊ |
1137 | ␉␉␉␉␉xsdt_entries[i-dropoffset]=xsdt_entries[i];␊ |
1138 | ␉␉␉␉␉␊ |
1139 | ␉␉␉␉␉if (drop_ssdt && tableSign(table, "SSDT"))␊ |
1140 | ␉␉␉␉␉{␊ |
1141 | ␉␉␉␉␉␉dropoffset++;␊ |
1142 | ␉␉␉␉␉␉continue;␊ |
1143 | ␉␉␉␉␉}␉␉␉␉␉␊ |
1144 | ␉␉␉␉␉if (tableSign(table, "DSDT"))␊ |
1145 | ␉␉␉␉␉{␊ |
1146 | ␉␉␉␉␉␉DBG("DSDT found\n");␊ |
1147 | ␉␉␉␉␉␉␊ |
1148 | ␉␉␉␉␉␉if (new_dsdt) ␊ |
1149 | ␉␉␉␉␉␉␉xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;␊ |
1150 | ␉␉␉␉␉␉␊ |
1151 | ␉␉␉␉␉␉DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);␊ |
1152 | ␉␉␉␉␉␉␊ |
1153 | ␉␉␉␉␉␉continue;␊ |
1154 | ␉␉␉␉␉}␊ |
1155 | ␉␉␉␉␉if (tableSign(table, "FACP"))␊ |
1156 | ␉␉␉␉␉{␊ |
1157 | ␉␉␉␉␉␉struct acpi_2_fadt *fadt, *fadt_mod;␊ |
1158 | ␉␉␉␉␉␉fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];␊ |
1159 | ␉␉␉␉␉␉␊ |
1160 | ␉␉␉␉␉␉DBG("FADT found @%x%x, Length %d\n", (uint32_t)(xsdt_entries[i]>>32),fadt, ␊ |
1161 | ␉␉␉␉␉␉␉fadt->Length);␊ |
1162 | ␉␉␉␉␉␉␊ |
1163 | ␉␉␉␉␉␉if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)␊ |
1164 | ␉␉␉␉␉␉{␊ |
1165 | ␉␉␉␉␉␉␉DBG("FADT incorrect or after 4GB. Dropping XSDT\n");␊ |
1166 | ␉␉␉␉␉␉␉goto drop_xsdt;␊ |
1167 | ␉␉␉␉␉␉}␊ |
1168 | ␉␉␉␉␉␉␊ |
1169 | ␉␉␉␉␉␉fadt_mod = patch_fadt(fadt, new_dsdt);␊ |
1170 | ␉␉␉␉␉␉xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;␊ |
1171 | ␉␉␉␉␉␉␊ |
1172 | ␉␉␉␉␉␉DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);␊ |
1173 | ␉␉␉␉␉␉␊ |
1174 | ␉␉␉␉␉␉// Generate _CST SSDT␊ |
1175 | ␉␉␉␉␉␉if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod))) ␊ |
1176 | ␉␉␉␉␉␉{␊ |
1177 | ␉␉␉␉␉␉␉generate_cstates = false; // Generate SSDT only once!␊ |
1178 | ␉␉␉␉␉␉␉ssdt_count++;␊ |
1179 | ␉␉␉␉␉␉}␊ |
1180 | ␉␉␉␉␉␉␊ |
1181 | ␉␉␉␉␉␉// Generating _PSS SSDT␊ |
1182 | ␉␉␉␉␉␉if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))␊ |
1183 | ␉␉␉␉␉␉{␊ |
1184 | ␉␉␉␉␉␉␉generate_pstates = false; // Generate SSDT only once!␊ |
1185 | ␉␉␉␉␉␉␉ssdt_count++;␊ |
1186 | ␉␉␉␉␉␉}␊ |
1187 | ␉␉␉␉␉␉␊ |
1188 | ␉␉␉␉␉␉continue;␊ |
1189 | ␉␉␉␉␉}␊ |
1190 | ␉␉␉␉␉␊ |
1191 | ␉␉␉␉␉DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);␊ |
1192 | ␉␉␉␉␉␊ |
1193 | ␉␉␉␉}␊ |
1194 | ␉␉␉␉␊ |
1195 | ␉␉␉␉// Allocate xsdt in Kernel memory area␊ |
1196 | ␉␉␉␉xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;␊ |
1197 | ␉␉␉␉struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);␊ |
1198 | ␉␉␉␉memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);␊ |
1199 | ␉␉␉␉free(xsdt_mod); xsdt_mod = xsdt_copy;␊ |
1200 | ␉␉␉␉rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;␊ |
1201 | ␉␉␉␉xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;␊ |
1202 | ␉␉␉␉xsdt_entries=(uint64_t *)(xsdt_mod+1);␊ |
1203 | ␉␉␉␉␊ |
1204 | ␉␉␉␉// Mozodojo: Insert additional SSDTs into XSDT␊ |
1205 | ␉␉␉␉if(ssdt_count>0)␊ |
1206 | ␉␉␉␉{␊ |
1207 | ␉␉␉␉␉int j;␊ |
1208 | ␉␉␉␉␉␊ |
1209 | ␉␉␉␉␉for (j=0; j<ssdt_count; j++)␊ |
1210 | ␉␉␉␉␉␉xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];␊ |
1211 | ␉␉␉␉␉␊ |
1212 | ␉␉␉␉␉DBG("Added %d SSDT table(s) into XSDT\n", ssdt_count);␊ |
1213 | ␉␉␉␉}␊ |
1214 | ␉␉␉␉␊ |
1215 | ␉␉␉␉// Correct the checksum of XSDT␊ |
1216 | ␉␉␉␉xsdt_mod->Checksum=0;␊ |
1217 | ␉␉␉␉xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);␊ |
1218 | ␉␉␉}␊ |
1219 | ␉␉␉else␊ |
1220 | ␉␉␉{␊ |
1221 | ␉␉␉drop_xsdt:␊ |
1222 | ␉␉␉␉␊ |
1223 | ␉␉␉␉DBG("About to drop XSDT\n");␊ |
1224 | ␉␉␉␉␊ |
1225 | ␉␉␉␉/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT. ␊ |
1226 | ␉␉␉␉ * A Better strategy would be to generate␊ |
1227 | ␉␉␉␉ */␊ |
1228 | ␉␉␉␉//Slice - it is possible only for ACPI20␊ |
1229 | ␉␉␉␉if (version) {␊ |
1230 | ␉␉␉␉␉if(rsdp->RsdtAddress){␊ |
1231 | ␉␉␉␉␉␉rsdp_mod->XsdtAddress = ␊ |
1232 | ␉␉␉␉␉␉(uint64_t)(uint32_t)createNewXSDTfromRSDT((struct acpi_2_rsdt*)rsdp->RsdtAddress);␊ |
1233 | ␉␉␉␉␉␉continue;␊ |
1234 | ␉␉␉␉␉}␊ |
1235 | ␉␉␉␉␉else␊ |
1236 | ␉␉␉␉␉␉DBG(" no sample to create XSDT, dropping\n");␊ |
1237 | ␉␉␉␉} ␊ |
1238 | ␉␉␉␉␊ |
1239 | ␉␉␉␉rsdp_mod->XsdtAddress=0xffffffffffffffffLL;␊ |
1240 | ␉␉␉␉DBG("XSDT not found or XSDT incorrect\n");␊ |
1241 | ␉␉␉}␊ |
1242 | ␉␉}␊ |
1243 | ␉␉//Slice - correct DSDT in place␊ |
1244 | ␉␉if(len && old_dsdt && new_dsdt && (old_dsdt != new_dsdt)){␊ |
1245 | ␉␉␉DBG("old_dsdt=%08x new_dsdt=%08x new_len=%d\n", (unsigned int)old_dsdt, (unsigned int)new_dsdt, len);␊ |
1246 | ␉␉␉bcopy(new_dsdt, old_dsdt, len);␊ |
1247 | ␉␉}␊ |
1248 | ␉␉␊ |
1249 | ␉␉// Correct the checksum of RSDP ␊ |
1250 | ␉␉␊ |
1251 | ␉␉DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);␊ |
1252 | ␉␉␊ |
1253 | ␉␉rsdp_mod->Checksum=0;␊ |
1254 | ␉␉rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);␊ |
1255 | ␉␉␊ |
1256 | ␉␉DBG("New checksum %d\n", rsdp_mod->Checksum);␊ |
1257 | ␉␉␊ |
1258 | ␉␉if (version)␊ |
1259 | ␉␉{␊ |
1260 | ␉␉␉DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);␊ |
1261 | ␉␉␉␊ |
1262 | ␉␉␉rsdp_mod->ExtendedChecksum=0;␊ |
1263 | ␉␉␉rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);␊ |
1264 | ␉␉␉␊ |
1265 | ␉␉␉DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);␊ |
1266 | ␉␉␉␊ |
1267 | ␉␉}␊ |
1268 | ␉␉␊ |
1269 | ␉␉//verbose("Patched ACPI version %d DSDT\n", version+1);␊ |
1270 | ␉␉if (version)␊ |
1271 | ␉␉{␊ |
1272 | ␉␉␉/* XXX aserebln why uint32 cast if pointer is uint64 ? */␊ |
1273 | ␉␉␉//Slice because rsdp_mod is a pointer (32bit in i386) to a structure uint64_t*␊ |
1274 | ␉␉␉acpi20_p = (uint64_t)(uint32_t)rsdp_mod;␊ |
1275 | ␉␉␉addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");␊ |
1276 | ␉␉}␊ |
1277 | ␉␉else␊ |
1278 | ␉␉{␊ |
1279 | ␉␉␉/* XXX aserebln why uint32 cast if pointer is uint64 ? */␊ |
1280 | ␉␉␉acpi10_p = (uint64_t)(uint32_t)rsdp_mod;␊ |
1281 | ␉␉␉addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");␊ |
1282 | ␉␉}␊ |
1283 | ␉}␊ |
1284 | #if DEBUG_ACPI␊ |
1285 | ␉printf("Press a key to continue... (DEBUG_ACPI)\n");␊ |
1286 | ␉getc();␊ |
1287 | #endif␊ |
1288 | ␉return 1;␊ |
1289 | }␊ |
1290 | |