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// Is it allways the maximum multiplier?
547maximum.Control = rdmsr64(MSR_IA32_PERF_STATUS) & 0xff;
548
549// fix me: dirty method to get lowest multiplier... Hardcoded value!
550minimum.Control = 0x09;
551
552// Sanity check
553if (maximum.Control < minimum.Control)
554{
555DBG("Insane control values!");
556p_states_count = 0;
557}
558else
559{
560uint8_t i;
561p_states_count = 0;
562
563for (i = maximum.Control; i >= minimum.Control; i--)
564{
565p_states[p_states_count].Control = i;
566p_states[p_states_count].CID = p_states[p_states_count].Control << 1;
567p_states[p_states_count].Frequency = (Platform.CPU.FSBFrequency / 1000000) * i;
568p_states_count++;
569}
570}
571
572break;
573}
574default:
575verbose ("Unsupported CPU: P-States not generated !!!\n");
576break;
577}
578}
579}
580
581// Generating SSDT
582if (p_states_count > 0)
583{
584int i;
585
586struct aml_chunk* root = aml_create_node(NULL);
587aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
588struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
589struct aml_chunk* name = aml_add_name(scop, "PSS_");
590struct aml_chunk* pack = aml_add_package(name);
591
592for (i = 0; i < p_states_count; i++)
593{
594struct aml_chunk* pstt = aml_add_package(pack);
595
596aml_add_dword(pstt, p_states[i].Frequency);
597aml_add_dword(pstt, 0x00000000); // Power
598aml_add_dword(pstt, 0x0000000A); // Latency
599aml_add_dword(pstt, 0x0000000A); // Latency
600aml_add_dword(pstt, p_states[i].Control);
601aml_add_dword(pstt, i+1); // Status
602}
603
604// Add aliaces
605for (i = 0; i < acpi_cpu_count; i++)
606{
607char name[9];
608sprintf(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
610scop = aml_add_scope(root, name);
611aml_add_alias(scop, "PSS_", "_PSS");
612}
613
614aml_calculate_size(root);
615
616struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
617
618aml_write_node(root, (void*)ssdt, 0);
619
620ssdt->Length = root->Size;
621ssdt->Checksum = 0;
622ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
623
624aml_destroy_node(root);
625
626//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);
627
628verbose ("SSDT with CPU P-States generated successfully\n");
629
630return ssdt;
631}
632}
633else
634{
635verbose ("ACPI CPUs not found: P-States not generated !!!\n");
636}
637
638return NULL;
639}
640
641struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)
642{
643extern void setupSystemType();
644
645struct acpi_2_fadt *fadt_mod;
646bool fadt_rev2_needed = false;
647bool fix_restart;
648const char * value;
649
650// Restart Fix
651if (Platform.CPU.Vendor == 0x756E6547) // Intel
652{
653fix_restart = false; //Azi: think this should be false by default, but...
654
655getBoolForKey(kRestartFixKey, &fix_restart, &bootInfo->bootConfig);
656}
657else
658{
659verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
660fix_restart = false;
661}
662
663if (fix_restart) fadt_rev2_needed = true;
664
665// Allocate new fadt table
666if (fadt->Length < 0x84 && fadt_rev2_needed)
667{
668fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
669memcpy(fadt_mod, fadt, fadt->Length);
670fadt_mod->Length = 0x84;
671fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
672}
673else
674{
675fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
676memcpy(fadt_mod, fadt, fadt->Length);
677}
678// Determine system type / PM_Model
679if ( (value=getStringForKey(kSystemTypeKey, &bootInfo->bootConfig))!=NULL)
680{
681if (Platform.Type > 6)
682{
683if(fadt_mod->PM_Profile<=6)
684Platform.Type = fadt_mod->PM_Profile; // get the fadt if correct
685else
686Platform.Type = 1; // Set a fixed value (Desktop)
687verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform.Type);
688}
689else
690Platform.Type = (unsigned char) strtoul(value, NULL, 10);
691}
692// Set PM_Profile from SystemType only if user wanted this value to be forced
693if (fadt_mod->PM_Profile != Platform.Type)
694{
695 if (value)
696{ // user has overriden the SystemType so take care of it in FACP
697verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);
698fadt_mod->PM_Profile = Platform.Type;
699 }
700 else
701 { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
702Platform.Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
703 }
704}
705// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
706// because we need to take care of facp original content, if it is correct.
707setupSystemType();
708
709// Patch FADT to fix restart
710if (fix_restart)
711{
712fadt_mod->Flags|= 0x400;
713fadt_mod->Reset_SpaceID= 0x01; // System I/O
714fadt_mod->Reset_BitWidth= 0x08; // 1 byte
715fadt_mod->Reset_BitOffset= 0x00; // Offset 0
716fadt_mod->Reset_AccessWidth= 0x01; // Byte access
717fadt_mod->Reset_Address= 0x0cf9; // Address of the register
718fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
719verbose("FADT: Restart Fix applied!\n");
720}
721
722// Patch DSDT Address if we have loaded DSDT.aml
723if(new_dsdt)
724{
725DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
726
727fadt_mod->DSDT=(uint32_t)new_dsdt;
728if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
729fadt_mod->X_DSDT=(uint32_t)new_dsdt;
730
731DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
732
733verbose("FADT: Using custom DSDT!\n");
734}
735
736// Correct the checksum
737fadt_mod->Checksum=0;
738fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
739
740return fadt_mod;
741}
742
743/* Setup ACPI without replacing DSDT. - not needed atm.
744int setupAcpiNoMod()
745{
746//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
747//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
748// XXX aserebln why uint32 cast if pointer is uint64 ?
749acpi10_p = (uint32_t)getAddressOfAcpiTable();
750acpi20_p = (uint32_t)getAddressOfAcpi20Table();
751addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
752if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
753return 1;
754}*/
755
756/* Setup ACPI. Replace DSDT if DSDT.aml is found */
757int setupAcpi(void)
758{
759int version;
760void *new_dsdt;
761
762// Load replacement DSDT
763new_dsdt=loadACPITable("DSDT.aml");
764// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
765/*if (!new_dsdt)
766 {
767 return setupAcpiNoMod();
768 }*/
769
770// Mozodojo: Load additional SSDTs
771struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
772int ssdt_count=0;
773
774// SSDT Options
775bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
776
777getBoolForKey(kDropSSDTKey, &drop_ssdt, &bootInfo->bootConfig);
778getBoolForKey(kGeneratePStatesKey, &generate_pstates, &bootInfo->bootConfig);
779getBoolForKey(kGenerateCStatesKey, &generate_cstates, &bootInfo->bootConfig);
780
781{
782int i;
783
784for (i=0; i<30; i++)
785{
786char filename[512];
787
788sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
789
790if(new_ssdt[ssdt_count] = loadACPITable(filename))
791{
792ssdt_count++;
793}
794else
795{
796break;
797}
798}
799}
800
801// Do the same procedure for both versions of ACPI
802for (version=0; version<2; version++) {
803struct acpi_2_rsdp *rsdp, *rsdp_mod;
804struct acpi_2_rsdt *rsdt, *rsdt_mod;
805int rsdplength;
806
807// Find original rsdp
808rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
809if (!rsdp)
810{
811DBG("No ACPI version %d found. Ignoring\n", version+1);
812if (version)
813addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
814else
815addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
816continue;
817}
818rsdplength=version?rsdp->Length:20;
819
820DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
821
822/* FIXME: no check that memory allocation succeeded
823 * Copy and patch RSDP,RSDT, XSDT and FADT
824 * For more info see ACPI Specification pages 110 and following
825 */
826
827rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
828memcpy(rsdp_mod, rsdp, rsdplength);
829rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
830
831DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
832
833if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
834{
835uint32_t *rsdt_entries;
836int rsdt_entries_num;
837int dropoffset=0, i;
838
839// mozo: using malloc cos I didn't found how to free already allocated kernel memory
840rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
841memcpy (rsdt_mod, rsdt, rsdt->Length);
842rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
843rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
844rsdt_entries=(uint32_t *)(rsdt_mod+1);
845for (i=0;i<rsdt_entries_num;i++)
846{
847char *table=(char *)(rsdt_entries[i]);
848if (!table)
849continue;
850
851DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
852
853rsdt_entries[i-dropoffset]=rsdt_entries[i];
854
855if (drop_ssdt && tableSign(table, "SSDT"))
856{
857dropoffset++;
858continue;
859}
860if (tableSign(table, "DSDT"))
861{
862DBG("DSDT found\n");
863
864if(new_dsdt)
865rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
866
867continue;
868}
869if (tableSign(table, "FACP"))
870{
871struct acpi_2_fadt *fadt, *fadt_mod;
872fadt=(struct acpi_2_fadt *)rsdt_entries[i];
873
874DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
875
876if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
877{
878printf("FADT incorrect. Not modified\n");
879continue;
880}
881
882fadt_mod = patch_fadt(fadt, new_dsdt);
883rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
884
885// Generate _CST SSDT
886if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
887{
888generate_cstates = false; // Generate SSDT only once!
889ssdt_count++;
890}
891
892// Generating _PSS SSDT
893if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
894{
895generate_pstates = false; // Generate SSDT only once!
896ssdt_count++;
897}
898
899continue;
900}
901}
902DBG("\n");
903
904// Allocate rsdt in Kernel memory area
905rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
906struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
907memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
908free(rsdt_mod); rsdt_mod = rsdt_copy;
909rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
910rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
911rsdt_entries=(uint32_t *)(rsdt_mod+1);
912
913// Mozodojo: Insert additional SSDTs into RSDT
914if(ssdt_count>0)
915{
916int j;
917
918for (j=0; j<ssdt_count; j++)
919rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
920
921verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
922}
923
924// Correct the checksum of RSDT
925DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
926
927rsdt_mod->Checksum=0;
928rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
929
930DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
931}
932else
933{
934rsdp_mod->RsdtAddress=0;
935printf("RSDT not found or RSDT incorrect\n");
936}
937
938if (version)
939{
940struct acpi_2_xsdt *xsdt, *xsdt_mod;
941
942// FIXME: handle 64-bit address correctly
943
944xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
945DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
946xsdt->Length);
947if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
948{
949uint64_t *xsdt_entries;
950int xsdt_entries_num, i;
951int dropoffset=0;
952
953// mozo: using malloc cos I didn't found how to free already allocated kernel memory
954xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
955memcpy(xsdt_mod, xsdt, xsdt->Length);
956rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
957xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
958xsdt_entries=(uint64_t *)(xsdt_mod+1);
959for (i=0;i<xsdt_entries_num;i++)
960{
961char *table=(char *)((uint32_t)(xsdt_entries[i]));
962if (!table)
963continue;
964
965xsdt_entries[i-dropoffset]=xsdt_entries[i];
966
967if (drop_ssdt && tableSign(table, "SSDT"))
968{
969dropoffset++;
970continue;
971}
972if (tableSign(table, "DSDT"))
973{
974DBG("DSDT found\n");
975
976if (new_dsdt)
977xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
978
979DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
980
981continue;
982}
983if (tableSign(table, "FACP"))
984{
985struct acpi_2_fadt *fadt, *fadt_mod;
986fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
987
988DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
989fadt->Length);
990
991if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
992{
993verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
994goto drop_xsdt;
995}
996
997fadt_mod = patch_fadt(fadt, new_dsdt);
998xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
999
1000DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1001
1002// Generate _CST SSDT
1003if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1004{
1005generate_cstates = false; // Generate SSDT only once!
1006ssdt_count++;
1007}
1008
1009// Generating _PSS SSDT
1010if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1011{
1012generate_pstates = false; // Generate SSDT only once!
1013ssdt_count++;
1014}
1015
1016continue;
1017}
1018
1019DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1020
1021}
1022
1023// Allocate xsdt in Kernel memory area
1024xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
1025struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
1026memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
1027free(xsdt_mod); xsdt_mod = xsdt_copy;
1028rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1029xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1030xsdt_entries=(uint64_t *)(xsdt_mod+1);
1031
1032// Mozodojo: Insert additional SSDTs into XSDT
1033if(ssdt_count>0)
1034{
1035int j;
1036
1037for (j=0; j<ssdt_count; j++)
1038xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1039
1040verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1041}
1042
1043// Correct the checksum of XSDT
1044xsdt_mod->Checksum=0;
1045xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1046}
1047else
1048{
1049drop_xsdt:
1050
1051DBG("About to drop XSDT\n");
1052
1053/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1054 * A Better strategy would be to generate
1055 */
1056
1057rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1058verbose("XSDT not found or XSDT incorrect\n");
1059}
1060}
1061
1062// Correct the checksum of RSDP
1063
1064DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1065
1066rsdp_mod->Checksum=0;
1067rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1068
1069DBG("New checksum %d\n", rsdp_mod->Checksum);
1070
1071if (version)
1072{
1073DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1074
1075rsdp_mod->ExtendedChecksum=0;
1076rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1077
1078DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1079
1080}
1081
1082//verbose("Patched ACPI version %d DSDT\n", version+1);
1083if (version)
1084{
1085/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1086acpi20_p = (uint32_t)rsdp_mod;
1087addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1088}
1089else
1090{
1091/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1092acpi10_p = (uint32_t)rsdp_mod;
1093addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1094}
1095}
1096#if DEBUG_ACPI
1097printf("Press a key to continue... (DEBUG_ACPI)\n");
1098getc();
1099#endif
1100return 1;
1101}
1102

Archive Download this file

Revision: 624