Chameleon

Chameleon Svn Source Tree

Root/branches/slice/trunkM/i386/libsaio/acpi_patcher.c

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

Archive Download this file

Revision: 1194