Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazi/i386/libsaio/acpi_patcher.c

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

Archive Download this file

Revision: 667