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

Archive Download this file

Revision: 380