Chameleon

Chameleon Svn Source Tree

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

1/*
2 * Copyright 2008 mackerintel
3 */
4
5//Azi:include
6//#include "libsaio.h" // included on boot.h, acpi_patcher.h, platform.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
17#ifndef DEBUG_ACPI
18#define DEBUG_ACPI 0
19#endif
20
21#if DEBUG_ACPI==2
22#define DBG(x...) {printf(x); sleep(1);}
23#elif DEBUG_ACPI==1
24#define DBG(x...) printf(x)
25#else
26#define DBG(x...)
27#endif
28
29static uint64_t acpi10_p; //Azi: aserebln
30static uint64_t acpi20_p; //||
31
32// Slice: New signature compare function
33boolean_t tableSign(char *table, const char *sgn)
34{
35int i;
36for (i=0; i<4; i++) {
37if ((table[i] &~0x20) != (sgn[i] &~0x20)) {
38return false;
39}
40}
41return true;
42}
43
44/* Gets the ACPI 1.0 RSDP address */
45static struct acpi_2_rsdp* getAddressOfAcpiTable()
46{
47 /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
48
49 void *acpi_addr = (void*)ACPI_RANGE_START;
50 for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
51 {
52 if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
53 {
54 uint8_t csum = checksum8(acpi_addr, 20);
55 if(csum == 0)
56 {
57 // Only return the table if it is a true version 1.0 table (Revision 0)
58 if(((struct acpi_2_rsdp*)acpi_addr)->Revision == 0)
59 return acpi_addr;
60 }
61 }
62 }
63 return NULL;
64}
65
66/* Gets the ACPI 2.0 RSDP address */
67static struct acpi_2_rsdp* getAddressOfAcpi20Table()
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
78 /* Only assume this is a 2.0 or better table if the revision is greater than 0
79 * NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1
80 * and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.
81 */
82
83 if(csum == 0 && (((struct acpi_2_rsdp*)acpi_addr)->Revision > 0))
84 {
85 uint8_t csum2 = checksum8(acpi_addr, sizeof(struct acpi_2_rsdp));
86 if(csum2 == 0)
87 return acpi_addr;
88 }
89 }
90 }
91 return NULL;
92}
93
94/** The following ACPI Table search algo, should be reused anywhere needed: */
95int search_and_get_acpi_fd(const char * filename, const char ** outDirspec)
96{
97static bool first_time = true;
98static chardirSpecDSDT[128] = "";
99const char *override_pathname = NULL; // full path to a file.
100intlen = 0, fd = 0;
101
102// Take in account user overriding if it's DSDT only
103if (strstr(filename, "DSDT") &&
104getValueForKey(kDSDTKey, &override_pathname, &len, &bootInfo->bootConfig))
105{
106// Specify a path to a file, e.g. DSDT=/Extra/test.aml
107sprintf(dirSpecDSDT, override_pathname);
108fd = open(dirSpecDSDT, 0);
109if (fd >= 0) goto success_fd;
110}
111
112// Check that dirSpecDSDT is not already assigned with a path
113if (!first_time && *dirSpecDSDT)
114{ // it is so start searching this cached patch first
115//extract path
116for (len = strlen(dirSpecDSDT)-1; len; len--)
117{
118if (dirSpecDSDT[len] == '/' || len == 0)
119{
120dirSpecDSDT[len] = '\0';
121break;
122}
123}
124
125// now concat with the filename
126strncat(dirSpecDSDT, "/", sizeof(dirSpecDSDT));
127strncat(dirSpecDSDT, filename, sizeof(dirSpecDSDT));
128
129// and test to see if we don't have our big boy here:
130fd = open(dirSpecDSDT,0);
131if (fd >= 0)
132{
133verbose("ACPI file search cache hit: file found at %s\n", dirSpecDSDT);
134goto success_fd;
135}
136}
137
138// Start searching any potential location for ACPI Table
139// Check rd's root.
140sprintf(dirSpecDSDT, "rd(0,0)/%s", filename);
141fd = open(dirSpecDSDT, 0);
142if (fd >= 0) goto success_fd;
143
144// Check booter volume/rdbt for specific OS folders.
145sprintf(dirSpecDSDT, "bt(0,0)/Extra/%s/%s", &gMacOSVersion, filename);
146fd = open(dirSpecDSDT, 0);
147if (fd >= 0) goto success_fd;
148
149// Check booter volume/rdbt Extra.
150sprintf(dirSpecDSDT, "bt(0,0)/Extra/%s", filename);
151fd = open(dirSpecDSDT, 0);
152if (fd >= 0) goto success_fd;
153
154//Azi: All loaded files stay in Extra.. please!? :)
155//sprintf(dirspec, "/%s", filename); // search root
156//fd=open (dirspec,0);
157//if (fd>=0) goto success_fd;
158
159// NOT FOUND:
160//Azi: a reminder - handling this verbose only on pci_root.c, getPciRootUID()
161// (it's enough to check if *.aml file exists), to reduce number of printed messages
162// and the confusion caused by them on users.
163//verbose("ACPI Table not found: %s\n", filename);
164if (outDirspec) *outDirspec = "";
165first_time = false;
166return -1;
167// FOUND
168success_fd:
169first_time = false;
170if (outDirspec) *outDirspec = dirSpecDSDT;
171return fd;
172}
173
174void *loadACPITable (const char * filename) //Azi: called on setupAcpi()
175{
176void *tableAddr;
177const char * dsdt_filename = NULL; //Azi: = dirSpecDSDT
178
179//Azi:dsdt - call 2; call 1 on pci_root.c (getPciRootUID), changed dirspec to dsdt_filename according (review this!!!).
180int fd = search_and_get_acpi_fd(filename, &dsdt_filename);
181
182if (fd >= 0)
183{
184tableAddr = (void*)AllocateKernelMemory(file_size (fd));
185
186if (tableAddr)
187{
188if (read (fd, tableAddr, file_size (fd)) != file_size (fd))
189{
190printf("Couldn't read table %s\n",dsdt_filename);
191free (tableAddr);
192close (fd);
193return NULL;
194}
195
196DBG("Table %s read and stored at: %x\n", dsdt_filename, tableAddr);
197close (fd);
198return tableAddr;
199}
200close (fd);
201printf("Couldn't allocate memory for table \n", dsdt_filename);
202}
203return NULL;
204}
205
206uint8_tacpi_cpu_count = 0;
207char* acpi_cpu_name[32];
208
209void get_acpi_cpu_names(unsigned char* dsdt, uint32_t length)
210{
211uint32_t i;
212
213for (i=0; i<length-7; i++)
214{
215if (dsdt[i] == 0x5B && dsdt[i+1] == 0x83) // ProcessorOP
216{
217uint32_t offset = i + 3 + (dsdt[i+2] >> 6);
218
219bool add_name = true;
220
221uint8_t j;
222
223for (j=0; j<4; j++)
224{
225char c = dsdt[offset+j];
226
227if (!aml_isvalidchar(c))
228{
229add_name = false;
230verbose("Invalid character found in ProcessorOP 0x%x!\n", c);
231break;
232}
233}
234
235if (add_name)
236{
237acpi_cpu_name[acpi_cpu_count] = malloc(4);
238memcpy(acpi_cpu_name[acpi_cpu_count], dsdt+offset, 4);
239i = offset + 5;
240
241verbose("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]);
242
243if (++acpi_cpu_count == 32) return;
244}
245}
246}
247}
248
249struct acpi_2_ssdt *generate_cst_ssdt(struct acpi_2_fadt* fadt)
250{
251char ssdt_header[] =
252{
2530x53, 0x53, 0x44, 0x54, 0xE7, 0x00, 0x00, 0x00, /* SSDT.... */
2540x01, 0x17, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x41, /* ..PmRefA */
2550x43, 0x70, 0x75, 0x43, 0x73, 0x74, 0x00, 0x00, /* CpuCst.. */
2560x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* ....INTL */
2570x31, 0x03, 0x10, 0x20 /* 1.._*/
258};
259
260char cstate_resource_template[] =
261{
2620x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F,
2630x01, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
2640x00, 0x00, 0x00, 0x79, 0x00
265};
266
267if (Platform.CPU.Vendor != 0x756E6547) {
268verbose ("Not an Intel platform: C-States will not be generated !!!\n");
269return NULL;
270}
271
272if (fadt == NULL) {
273verbose ("FACP not exists: C-States will not be generated !!!\n");
274return NULL;
275}
276
277struct acpi_2_dsdt* dsdt = (void*)fadt->DSDT;
278
279if (dsdt == NULL) {
280verbose ("DSDT not found: C-States will not be generated !!!\n");
281return NULL;
282}
283
284if (acpi_cpu_count == 0)
285get_acpi_cpu_names((void*)dsdt, dsdt->Length);
286
287if (acpi_cpu_count > 0)
288{
289bool c2_enabled = fadt->C2_Latency < 100;
290bool c3_enabled = fadt->C3_Latency < 1000;
291bool c4_enabled = false;
292
293getBoolForKey(kEnableC4States, &c4_enabled, &bootInfo->bootConfig);
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 = 1;
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} break;
536case CPU_MODEL_FIELDS:
537case CPU_MODEL_DALES:
538case CPU_MODEL_DALES_32NM:
539case CPU_MODEL_NEHALEM:
540case CPU_MODEL_NEHALEM_EX:
541case CPU_MODEL_WESTMERE:
542case CPU_MODEL_WESTMERE_EX:
543default:
544verbose ("Unsupported CPU: P-States not generated !!!\n");
545break;
546}
547}
548}
549
550// Generating SSDT
551if (p_states_count > 0)
552{
553int i;
554
555struct aml_chunk* root = aml_create_node(NULL);
556aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
557struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
558struct aml_chunk* name = aml_add_name(scop, "PSS_");
559struct aml_chunk* pack = aml_add_package(name);
560
561for (i = 0; i < p_states_count; i++)
562{
563struct aml_chunk* pstt = aml_add_package(pack);
564
565aml_add_dword(pstt, p_states[i].Frequency);
566aml_add_dword(pstt, 0x00000000); // Power
567aml_add_dword(pstt, 0x0000000A); // Latency
568aml_add_dword(pstt, 0x0000000A); // Latency
569aml_add_dword(pstt, p_states[i].Control);
570aml_add_dword(pstt, i+1); // Status
571}
572
573// Add aliaces
574for (i = 0; i < acpi_cpu_count; i++)
575{
576char name[9];
577sprintf(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]);
578
579scop = aml_add_scope(root, name);
580aml_add_alias(scop, "PSS_", "_PSS");
581}
582
583aml_calculate_size(root);
584
585struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
586
587aml_write_node(root, (void*)ssdt, 0);
588
589ssdt->Length = root->Size;
590ssdt->Checksum = 0;
591ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
592
593aml_destroy_node(root);
594
595//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);
596
597verbose ("SSDT with CPU P-States generated successfully\n");
598
599return ssdt;
600}
601}
602else
603{
604verbose ("ACPI CPUs not found: P-States not generated !!!\n");
605}
606
607return NULL;
608}
609
610struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)
611{
612extern void setupSystemType();
613
614struct acpi_2_fadt *fadt_mod;
615bool fadt_rev2_needed = false;
616bool fix_restart;
617const char * value;
618
619// Restart Fix
620if (Platform.CPU.Vendor == 0x756E6547) // Intel
621{
622fix_restart = false; //Azi: think this should be false by default!?
623// On the other hand, i could use a shutdown fix now and then :)
624getBoolForKey(kRestartFixKey, &fix_restart, &bootInfo->bootConfig);
625}
626else
627{
628verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
629fix_restart = false;
630}
631
632if (fix_restart) fadt_rev2_needed = true;
633
634// Allocate new fadt table
635if (fadt->Length < 0x84 && fadt_rev2_needed)
636{
637fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
638memcpy(fadt_mod, fadt, fadt->Length);
639fadt_mod->Length = 0x84;
640fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
641}
642else
643{
644fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
645memcpy(fadt_mod, fadt, fadt->Length);
646}
647// Determine system type / PM_Model
648if ( (value=getStringForKey(kSystemTypeKey, &bootInfo->bootConfig))!=NULL)
649{
650if (Platform.Type > 6)
651{
652if(fadt_mod->PM_Profile<=6)
653Platform.Type = fadt_mod->PM_Profile; // get the fadt if correct
654else
655Platform.Type = 1;/* Set a fixed value (Desktop) */
656verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform.Type);
657}
658else
659Platform.Type = (unsigned char) strtoul(value, NULL, 10);
660}
661
662// Set PM_Profile from System-type only if user wanted this value to be forced
663if (fadt_mod->PM_Profile != Platform.Type)
664{
665 if (value)
666{ // user has overriden the SystemType so take care of it in FACP
667verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);
668fadt_mod->PM_Profile = Platform.Type;
669 }
670 else
671 { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
672Platform.Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
673 }
674}
675// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
676// because we need to take care of facp original content, if it is correct.
677setupSystemType();
678
679// Patch FADT to fix restart
680if (fix_restart)
681{
682fadt_mod->Flags|= 0x400;
683fadt_mod->Reset_SpaceID= 0x01; // System I/O
684fadt_mod->Reset_BitWidth= 0x08; // 1 byte
685fadt_mod->Reset_BitOffset= 0x00; // Offset 0
686fadt_mod->Reset_AccessWidth= 0x01; // Byte access
687fadt_mod->Reset_Address= 0x0cf9; // Address of the register
688fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
689verbose("FADT: Restart Fix applied!\n");
690}
691
692// Patch DSDT Address if we have loaded DSDT.aml
693if(new_dsdt)
694{
695DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
696
697fadt_mod->DSDT=(uint32_t)new_dsdt;
698if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
699fadt_mod->X_DSDT=(uint32_t)new_dsdt;
700
701DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
702
703//Azi: using only getPciRootUID() to signal user supplied acpi table.
704//verbose("FADT: Using custom DSDT!\n");
705}
706
707// Correct the checksum
708fadt_mod->Checksum=0;
709fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
710
711return fadt_mod;
712}
713
714/* Setup ACPI without replacing DSDT. */
715int setupAcpiNoMod() //Azi:mozo - not used atm.
716{
717//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
718//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
719// XXX aserebln why uint32 cast if pointer is uint64?
720//Azi: "Slice: because rsdp_mod is a pointer (32bit in i386) to a structure uint64_t*"
721acpi10_p = /*(uint64_t)*/(uint32_t)getAddressOfAcpiTable();
722acpi20_p = /*(uint64_t)*/(uint32_t)getAddressOfAcpi20Table();
723addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
724if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
725return 1;
726}
727
728/* Setup ACPI. Replace DSDT if DSDT.aml is found */
729int setupAcpi(void)
730{
731int version;
732void *new_dsdt;
733
734// Load replacement DSDT
735new_dsdt = loadACPITable("DSDT.aml"); //Azi:dsdt
736// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
737/*if (!new_dsdt)
738{
739return setupAcpiNoMod();
740}*/
741
742// Mozodojo: Load additional SSDTs
743struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
744int ssdt_count=0;
745
746// SSDT Options
747bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
748
749getBoolForKey(kDropSSDTKey, &drop_ssdt, &bootInfo->bootConfig);
750getBoolForKey(kGeneratePStatesKey, &generate_pstates, &bootInfo->bootConfig);
751getBoolForKey(kGenerateCStatesKey, &generate_cstates, &bootInfo->bootConfig);
752
753{
754int i;
755
756for (i=0; i<30; i++)
757{
758char filename[512];
759
760sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
761
762if(new_ssdt[ssdt_count] = loadACPITable(filename))
763{
764ssdt_count++;
765}
766else
767{
768break;
769}
770}
771}
772
773// Do the same procedure for both versions of ACPI
774for (version=0; version<2; version++) {
775struct acpi_2_rsdp *rsdp, *rsdp_mod;
776struct acpi_2_rsdt *rsdt, *rsdt_mod;
777int rsdplength;
778
779// Find original rsdp
780rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
781if (!rsdp)
782{
783DBG("No ACPI version %d found. Ignoring\n", version+1);
784if (version)
785addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
786else
787addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
788continue;
789}
790rsdplength=version?rsdp->Length:20;
791
792DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
793
794/* FIXME: no check that memory allocation succeeded
795 * Copy and patch RSDP,RSDT, XSDT and FADT
796 * For more info see ACPI Specification pages 110 and following
797 */
798
799rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
800memcpy(rsdp_mod, rsdp, rsdplength);
801rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
802
803DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
804
805if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
806{
807uint32_t *rsdt_entries;
808int rsdt_entries_num;
809int dropoffset=0, i;
810
811// mozo: using malloc cos I didn't found how to free already allocated kernel memory
812rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
813memcpy (rsdt_mod, rsdt, rsdt->Length);
814rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
815rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
816rsdt_entries=(uint32_t *)(rsdt_mod+1);
817for (i=0;i<rsdt_entries_num;i++)
818{
819char *table=(char *)(rsdt_entries[i]);
820if (!table)
821continue;
822
823DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
824
825rsdt_entries[i-dropoffset]=rsdt_entries[i];
826
827if (drop_ssdt && tableSign(table, "SSDT"))
828{
829dropoffset++;
830continue;
831}
832if (tableSign(table, "DSDT"))
833{
834DBG("DSDT found\n");
835
836if(new_dsdt)
837rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
838
839continue;
840}
841if (tableSign(table, "FACP"))
842{
843struct acpi_2_fadt *fadt, *fadt_mod;
844fadt=(struct acpi_2_fadt *)rsdt_entries[i];
845
846DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
847
848if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
849{
850printf("FADT incorrect. Not modified\n");
851continue;
852}
853
854fadt_mod = patch_fadt(fadt, new_dsdt);
855rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
856
857// Generate _CST SSDT
858if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
859{
860generate_cstates = false; // Generate SSDT only once!
861ssdt_count++;
862}
863
864// Generating _PSS SSDT
865if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
866{
867generate_pstates = false; // Generate SSDT only once!
868ssdt_count++;
869}
870
871continue;
872}
873}
874DBG("\n");
875
876// Allocate rsdt in Kernel memory area
877rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
878struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
879memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
880free(rsdt_mod); rsdt_mod = rsdt_copy;
881rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
882rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
883rsdt_entries=(uint32_t *)(rsdt_mod+1);
884
885// Mozodojo: Insert additional SSDTs into RSDT
886if(ssdt_count>0)
887{
888int j;
889
890for (j=0; j<ssdt_count; j++)
891rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
892
893verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
894}
895
896// Correct the checksum of RSDT
897DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
898
899rsdt_mod->Checksum=0;
900rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
901
902DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
903}
904else
905{
906rsdp_mod->RsdtAddress=0;
907printf("RSDT not found or RSDT incorrect\n");
908}
909
910if (version)
911{
912struct acpi_2_xsdt *xsdt, *xsdt_mod;
913
914// FIXME: handle 64-bit address correctly
915
916xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
917DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
918xsdt->Length);
919if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
920{
921uint64_t *xsdt_entries;
922int xsdt_entries_num, i;
923int dropoffset=0;
924
925// mozo: using malloc cos I didn't found how to free already allocated kernel memory
926xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
927memcpy(xsdt_mod, xsdt, xsdt->Length);
928rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
929xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
930xsdt_entries=(uint64_t *)(xsdt_mod+1);
931for (i=0;i<xsdt_entries_num;i++)
932{
933char *table=(char *)((uint32_t)(xsdt_entries[i]));
934if (!table)
935continue;
936
937xsdt_entries[i-dropoffset]=xsdt_entries[i];
938
939if (drop_ssdt && tableSign(table, "SSDT"))
940{
941dropoffset++;
942continue;
943}
944if (tableSign(table, "DSDT"))
945{
946DBG("DSDT found\n");
947
948if (new_dsdt)
949xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
950
951DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
952
953continue;
954}
955if (tableSign(table, "FACP"))
956{
957struct acpi_2_fadt *fadt, *fadt_mod;
958fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
959
960DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
961fadt->Length);
962
963if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
964{
965verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
966goto drop_xsdt;
967}
968
969fadt_mod = patch_fadt(fadt, new_dsdt);
970xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
971
972DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
973
974// Generate _CST SSDT
975if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
976{
977generate_cstates = false; // Generate SSDT only once!
978ssdt_count++;
979}
980
981// Generating _PSS SSDT
982if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
983{
984generate_pstates = false; // Generate SSDT only once!
985ssdt_count++;
986}
987
988continue;
989}
990
991DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
992
993}
994
995// Allocate xsdt in Kernel memory area
996xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
997struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
998memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
999free(xsdt_mod); xsdt_mod = xsdt_copy;
1000rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1001xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1002xsdt_entries=(uint64_t *)(xsdt_mod+1);
1003
1004// Mozodojo: Insert additional SSDTs into XSDT
1005if(ssdt_count>0)
1006{
1007int j;
1008
1009for (j=0; j<ssdt_count; j++)
1010xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1011
1012verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1013}
1014
1015// Correct the checksum of XSDT
1016xsdt_mod->Checksum=0;
1017xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1018}
1019else
1020{
1021drop_xsdt:
1022
1023DBG("About to drop XSDT\n");
1024
1025/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1026 * A Better strategy would be to generate
1027 */
1028
1029rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1030verbose("XSDT not found or XSDT incorrect\n");
1031}
1032}
1033
1034// Correct the checksum of RSDP
1035
1036DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1037
1038rsdp_mod->Checksum=0;
1039rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1040
1041DBG("New checksum %d\n", rsdp_mod->Checksum);
1042
1043if (version)
1044{
1045DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1046
1047rsdp_mod->ExtendedChecksum=0;
1048rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1049
1050DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1051
1052}
1053
1054//Azi:mozo - commented out; using instead of "FADT: Using custom DSDT!"
1055verbose("ACPI version %d\n", version + 1); // always prints so, just print acpi version.
1056if (version)
1057{
1058// XXX aserebln why uint32 cast if pointer is uint64?
1059//Azi: See setupAcpiNoMod.
1060acpi20_p = (/*uint64_t)(*/uint32_t)rsdp_mod;
1061addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1062}
1063else
1064{
1065// XXX aserebln why uint32 cast if pointer is uint64?
1066acpi10_p = (/*uint64_t)(*/uint32_t)rsdp_mod;
1067addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1068}
1069}
1070#if DEBUG_ACPI
1071printf("Press a key to continue... (DEBUG_ACPI)\n");
1072getc();
1073#endif
1074return 1;
1075}
1076

Archive Download this file

Revision: 459