Chameleon

Chameleon Svn Source Tree

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

1/*
2 * Copyright 2008 mackerintel
3 */
4
5#include "libsaio.h"
6#include "boot.h"
7#include "bootstruct.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 OS specific 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 = fadt->C2_Latency < 100;
285bool c3_enabled = fadt->C3_Latency < 1000;
286bool c4_enabled = false;
287
288getBoolForKey(kEnableC4States, &c4_enabled, &bootInfo->bootConfig);
289
290unsigned char cstates_count = 1 + (c2_enabled ? 1 : 0) + (c3_enabled ? 1 : 0);
291
292struct aml_chunk* root = aml_create_node(NULL);
293aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
294struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
295struct aml_chunk* name = aml_add_name(scop, "CST_");
296struct aml_chunk* pack = aml_add_package(name);
297aml_add_byte(pack, cstates_count);
298
299struct aml_chunk* tmpl = aml_add_package(pack);
300cstate_resource_template[11] = 0x00; // C1
301aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
302aml_add_byte(tmpl, 0x01); // C1
303aml_add_byte(tmpl, 0x01); // Latency
304aml_add_word(tmpl, 0x03e8); // Power
305
306// C2
307if (c2_enabled)
308{
309tmpl = aml_add_package(pack);
310cstate_resource_template[11] = 0x10; // C2
311aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
312aml_add_byte(tmpl, 0x02); // C2
313aml_add_byte(tmpl, fadt->C2_Latency);
314aml_add_word(tmpl, 0x01f4); // Power
315}
316// C4
317if (c4_enabled)
318{
319tmpl = aml_add_package(pack);
320cstate_resource_template[11] = 0x30; // C4
321aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
322aml_add_byte(tmpl, 0x04); // C4
323aml_add_word(tmpl, fadt->C3_Latency / 2); // TODO: right latency for C4
324aml_add_byte(tmpl, 0xfa); // Power
325}
326else
327// C3
328if (c3_enabled)
329{
330tmpl = aml_add_package(pack);
331cstate_resource_template[11] = 0x20; // C3
332aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
333aml_add_byte(tmpl, 0x03); // C3
334aml_add_word(tmpl, fadt->C3_Latency);
335aml_add_word(tmpl, 0x015e); // Power
336}
337
338
339// Aliaces
340int i;
341for (i = 0; i < acpi_cpu_count; i++)
342{
343char name[9];
344sprintf(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]);
345
346scop = aml_add_scope(root, name);
347aml_add_alias(scop, "CST_", "_CST");
348}
349
350aml_calculate_size(root);
351
352struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
353
354aml_write_node(root, (void*)ssdt, 0);
355
356ssdt->Length = root->Size;
357ssdt->Checksum = 0;
358ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
359
360aml_destroy_node(root);
361
362//dumpPhysAddr("C-States SSDT content: ", ssdt, ssdt->Length);
363
364verbose ("SSDT with CPU C-States generated successfully\n");
365
366return ssdt;
367}
368else
369{
370verbose ("ACPI CPUs not found: C-States not generated !!!\n");
371}
372
373return NULL;
374}
375
376struct acpi_2_ssdt *generate_pss_ssdt(struct acpi_2_dsdt* dsdt)
377{
378char ssdt_header[] =
379{
3800x53, 0x53, 0x44, 0x54, 0x7E, 0x00, 0x00, 0x00, /* SSDT.... */
3810x01, 0x6A, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x00, /* ..PmRef. */
3820x43, 0x70, 0x75, 0x50, 0x6D, 0x00, 0x00, 0x00, /* CpuPm... */
3830x00, 0x30, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* .0..INTL */
3840x31, 0x03, 0x10, 0x20,/* 1.._*/
385};
386
387if (Platform.CPU.Vendor != 0x756E6547) {
388verbose ("Not an Intel platform: P-States will not be generated !!!\n");
389return NULL;
390}
391
392if (!(Platform.CPU.Features & CPU_FEATURE_MSR)) {
393verbose ("Unsupported CPU: P-States will not be generated !!!\n");
394return NULL;
395}
396
397if (acpi_cpu_count == 0)
398get_acpi_cpu_names((void*)dsdt, dsdt->Length);
399
400if (acpi_cpu_count > 0)
401{
402struct p_state initial, maximum, minimum, p_states[32];
403uint8_t p_states_count = 0;
404
405// Retrieving P-States, ported from code by superhai (c)
406switch (Platform.CPU.Family) {
407case 0x06:
408{
409switch (Platform.CPU.Model)
410{
411case 0x0D: // ?
412case CPU_MODEL_YONAH: // Yonah
413case CPU_MODEL_MEROM: // Merom
414case CPU_MODEL_PENRYN: // Penryn
415case CPU_MODEL_ATOM: // Intel Atom (45nm)
416{
417bool cpu_dynamic_fsb = false;
418
419if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27))
420{
421wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28)));
422delay(1);
423cpu_dynamic_fsb = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);
424}
425
426bool cpu_noninteger_bus_ratio = (rdmsr64(MSR_IA32_PERF_STATUS) & (1ULL << 46));
427
428initial.Control = rdmsr64(MSR_IA32_PERF_STATUS);
429
430maximum.Control = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 32) & 0x1F3F) | (0x4000 * cpu_noninteger_bus_ratio);
431maximum.CID = ((maximum.FID & 0x1F) << 1) | cpu_noninteger_bus_ratio;
432
433minimum.FID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 24) & 0x1F) | (0x80 * cpu_dynamic_fsb);
434minimum.VID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 48) & 0x3F);
435
436if (minimum.FID == 0)
437{
438uint64_t msr;
439uint8_t i;
440// Probe for lowest fid
441for (i = maximum.FID; i >= 0x6; i--)
442{
443msr = rdmsr64(MSR_IA32_PERF_CONTROL);
444wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (i << 8) | minimum.VID);
445intel_waitforsts();
446minimum.FID = (rdmsr64(MSR_IA32_PERF_STATUS) >> 8) & 0x1F;
447delay(1);
448}
449
450msr = rdmsr64(MSR_IA32_PERF_CONTROL);
451wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
452intel_waitforsts();
453}
454
455if (minimum.VID == maximum.VID)
456{
457uint64_t msr;
458uint8_t i;
459// Probe for lowest vid
460for (i = maximum.VID; i > 0xA; i--)
461{
462msr = rdmsr64(MSR_IA32_PERF_CONTROL);
463wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (minimum.FID << 8) | i);
464intel_waitforsts();
465minimum.VID = rdmsr64(MSR_IA32_PERF_STATUS) & 0x3F;
466delay(1);
467}
468
469msr = rdmsr64(MSR_IA32_PERF_CONTROL);
470wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
471intel_waitforsts();
472}
473
474minimum.CID = ((minimum.FID & 0x1F) << 1) >> cpu_dynamic_fsb;
475
476// Sanity check
477if (maximum.CID < minimum.CID)
478{
479DBG("Insane FID values!");
480p_states_count = 1;
481}
482else
483{
484// Finalize P-States
485// Find how many P-States machine supports
486p_states_count = maximum.CID - minimum.CID + 1;
487
488if (p_states_count > 32)
489p_states_count = 32;
490
491uint8_t vidstep;
492uint8_t i = 0, u, invalid = 0;
493
494vidstep = ((maximum.VID << 2) - (minimum.VID << 2)) / (p_states_count - 1);
495
496for (u = 0; u < p_states_count; u++)
497{
498i = u - invalid;
499
500p_states[i].CID = maximum.CID - u;
501p_states[i].FID = (p_states[i].CID >> 1);
502
503if (p_states[i].FID < 0x6)
504{
505if (cpu_dynamic_fsb)
506p_states[i].FID = (p_states[i].FID << 1) | 0x80;
507}
508else if (cpu_noninteger_bus_ratio)
509{
510p_states[i].FID = p_states[i].FID | (0x40 * (p_states[i].CID & 0x1));
511}
512
513if (i && p_states[i].FID == p_states[i-1].FID)
514invalid++;
515
516p_states[i].VID = ((maximum.VID << 2) - (vidstep * u)) >> 2;
517
518uint32_t multiplier = p_states[i].FID & 0x1f;// = 0x08
519bool half = p_states[i].FID & 0x40;// = 0x01
520bool dfsb = p_states[i].FID & 0x80;// = 0x00
521uint32_t fsb = Platform.CPU.FSBFrequency / 1000000; // = 400
522uint32_t halffsb = (fsb + 1) >> 1;// = 200
523uint32_t frequency = (multiplier * fsb);// = 3200
524
525p_states[i].Frequency = (frequency + (half * halffsb)) >> dfsb;// = 3200 + 200 = 3400
526}
527
528p_states_count -= invalid;
529}
530} break;
531case CPU_MODEL_FIELDS:
532case CPU_MODEL_DALES:
533case CPU_MODEL_DALES_32NM:
534case CPU_MODEL_NEHALEM:
535case CPU_MODEL_NEHALEM_EX:
536case CPU_MODEL_WESTMERE:
537case CPU_MODEL_WESTMERE_EX:
538default:
539verbose ("Unsupported CPU: P-States not generated !!!\n");
540break;
541}
542}
543}
544
545// Generating SSDT
546if (p_states_count > 0)
547{
548int i;
549
550struct aml_chunk* root = aml_create_node(NULL);
551aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
552struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
553struct aml_chunk* name = aml_add_name(scop, "PSS_");
554struct aml_chunk* pack = aml_add_package(name);
555
556for (i = 0; i < p_states_count; i++)
557{
558struct aml_chunk* pstt = aml_add_package(pack);
559
560aml_add_dword(pstt, p_states[i].Frequency);
561aml_add_dword(pstt, 0x00000000); // Power
562aml_add_dword(pstt, 0x0000000A); // Latency
563aml_add_dword(pstt, 0x0000000A); // Latency
564aml_add_dword(pstt, p_states[i].Control);
565aml_add_dword(pstt, i+1); // Status
566}
567
568// Add aliaces
569for (i = 0; i < acpi_cpu_count; i++)
570{
571char name[9];
572sprintf(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]);
573
574scop = aml_add_scope(root, name);
575aml_add_alias(scop, "PSS_", "_PSS");
576}
577
578aml_calculate_size(root);
579
580struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
581
582aml_write_node(root, (void*)ssdt, 0);
583
584ssdt->Length = root->Size;
585ssdt->Checksum = 0;
586ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
587
588aml_destroy_node(root);
589
590//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);
591
592verbose ("SSDT with CPU P-States generated successfully\n");
593
594return ssdt;
595}
596}
597else
598{
599verbose ("ACPI CPUs not found: P-States not generated !!!\n");
600}
601
602return NULL;
603}
604
605struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)
606{
607extern void setupSystemType();
608
609struct acpi_2_fadt *fadt_mod;
610bool fadt_rev2_needed = false;
611bool fix_restart;
612const char * value;
613
614// Restart Fix
615if (Platform.CPU.Vendor == 0x756E6547) // Intel
616{
617fix_restart = false; //Azi: think this should be false by default!?
618// On the other hand, i could use a shutdown fix now and then :)
619getBoolForKey(kRestartFix, &fix_restart, &bootInfo->bootConfig);
620}
621else
622{
623verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
624fix_restart = false;
625}
626
627if (fix_restart) fadt_rev2_needed = true;
628
629// Allocate new fadt table
630if (fadt->Length < 0x84 && fadt_rev2_needed)
631{
632fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
633memcpy(fadt_mod, fadt, fadt->Length);
634fadt_mod->Length = 0x84;
635fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
636}
637else
638{
639fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
640memcpy(fadt_mod, fadt, fadt->Length);
641}
642// Determine system type / PM_Model
643if ( (value=getStringForKey(kSystemType, &bootInfo->bootConfig))!=NULL)
644{
645if (Platform.Type > 6)
646{
647if(fadt_mod->PM_Profile<=6)
648Platform.Type = fadt_mod->PM_Profile; // get the fadt if correct
649else
650Platform.Type = 1; // Set a fixed value (Desktop)
651verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform.Type);
652}
653else
654Platform.Type = (unsigned char) strtoul(value, NULL, 10);
655}
656// Set PM_Profile from System-type only if user wanted this value to be forced
657if (fadt_mod->PM_Profile != Platform.Type)
658{
659 if (value)
660{ // user has overriden the SystemType so take care of it in FACP
661verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);
662fadt_mod->PM_Profile = Platform.Type;
663 }
664 else
665 { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
666Platform.Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
667 }
668}
669// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
670// because we need to take care of facp original content, if it is correct.
671setupSystemType();
672
673// Patch FADT to fix restart
674if (fix_restart)
675{
676fadt_mod->Flags|= 0x400;
677fadt_mod->Reset_SpaceID= 0x01; // System I/O
678fadt_mod->Reset_BitWidth= 0x08; // 1 byte
679fadt_mod->Reset_BitOffset= 0x00; // Offset 0
680fadt_mod->Reset_AccessWidth= 0x01; // Byte access
681fadt_mod->Reset_Address= 0x0cf9; // Address of the register
682fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
683verbose("FADT: Restart Fix applied!\n");
684}
685
686// Patch DSDT Address if we have loaded DSDT.aml
687if(new_dsdt)
688{
689DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
690
691fadt_mod->DSDT=(uint32_t)new_dsdt;
692if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
693fadt_mod->X_DSDT=(uint32_t)new_dsdt;
694
695DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
696
697verbose("FADT: Using custom DSDT!\n");
698}
699
700// Correct the checksum
701fadt_mod->Checksum=0;
702fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
703
704return fadt_mod;
705}
706
707/* Setup ACPI without replacing DSDT. - not needed atm.
708int setupAcpiNoMod()
709{
710//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
711//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
712// XXX aserebln why uint32 cast if pointer is uint64 ?
713acpi10_p = (uint32_t)getAddressOfAcpiTable();
714acpi20_p = (uint32_t)getAddressOfAcpi20Table();
715addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
716if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
717return 1;
718}*/
719
720/* Setup ACPI. Replace DSDT if DSDT.aml is found */
721int setupAcpi(void)
722{
723int version;
724void *new_dsdt;
725
726// Load replacement DSDT
727new_dsdt=loadACPITable("DSDT.aml");
728// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
729/*if (!new_dsdt)
730 {
731 return setupAcpiNoMod();
732 }*/
733
734// Mozodojo: Load additional SSDTs
735struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
736int ssdt_count=0;
737
738// SSDT Options
739bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
740
741getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->bootConfig);
742getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->bootConfig);
743getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->bootConfig);
744
745{
746int i;
747
748for (i=0; i<30; i++)
749{
750char filename[512];
751
752sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
753
754if(new_ssdt[ssdt_count] = loadACPITable(filename))
755{
756ssdt_count++;
757}
758else
759{
760break;
761}
762}
763}
764
765// Do the same procedure for both versions of ACPI
766for (version=0; version<2; version++) {
767struct acpi_2_rsdp *rsdp, *rsdp_mod;
768struct acpi_2_rsdt *rsdt, *rsdt_mod;
769int rsdplength;
770
771// Find original rsdp
772rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
773if (!rsdp)
774{
775DBG("No ACPI version %d found. Ignoring\n", version+1);
776if (version)
777addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
778else
779addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
780continue;
781}
782rsdplength=version?rsdp->Length:20;
783
784DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
785
786/* FIXME: no check that memory allocation succeeded
787 * Copy and patch RSDP,RSDT, XSDT and FADT
788 * For more info see ACPI Specification pages 110 and following
789 */
790
791rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
792memcpy(rsdp_mod, rsdp, rsdplength);
793rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
794
795DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
796
797if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
798{
799uint32_t *rsdt_entries;
800int rsdt_entries_num;
801int dropoffset=0, i;
802
803// mozo: using malloc cos I didn't found how to free already allocated kernel memory
804rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
805memcpy (rsdt_mod, rsdt, rsdt->Length);
806rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
807rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
808rsdt_entries=(uint32_t *)(rsdt_mod+1);
809for (i=0;i<rsdt_entries_num;i++)
810{
811char *table=(char *)(rsdt_entries[i]);
812if (!table)
813continue;
814
815DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
816
817rsdt_entries[i-dropoffset]=rsdt_entries[i];
818
819if (drop_ssdt && tableSign(table, "SSDT"))
820{
821dropoffset++;
822continue;
823}
824if (tableSign(table, "DSDT"))
825{
826DBG("DSDT found\n");
827
828if(new_dsdt)
829rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
830
831continue;
832}
833if (tableSign(table, "FACP"))
834{
835struct acpi_2_fadt *fadt, *fadt_mod;
836fadt=(struct acpi_2_fadt *)rsdt_entries[i];
837
838DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
839
840if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
841{
842printf("FADT incorrect. Not modified\n");
843continue;
844}
845
846fadt_mod = patch_fadt(fadt, new_dsdt);
847rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
848
849// Generate _CST SSDT
850if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
851{
852generate_cstates = false; // Generate SSDT only once!
853ssdt_count++;
854}
855
856// Generating _PSS SSDT
857if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
858{
859generate_pstates = false; // Generate SSDT only once!
860ssdt_count++;
861}
862
863continue;
864}
865}
866DBG("\n");
867
868// Allocate rsdt in Kernel memory area
869rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
870struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
871memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
872free(rsdt_mod); rsdt_mod = rsdt_copy;
873rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
874rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
875rsdt_entries=(uint32_t *)(rsdt_mod+1);
876
877// Mozodojo: Insert additional SSDTs into RSDT
878if(ssdt_count>0)
879{
880int j;
881
882for (j=0; j<ssdt_count; j++)
883rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
884
885verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
886}
887
888// Correct the checksum of RSDT
889DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
890
891rsdt_mod->Checksum=0;
892rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
893
894DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
895}
896else
897{
898rsdp_mod->RsdtAddress=0;
899printf("RSDT not found or RSDT incorrect\n");
900}
901
902if (version)
903{
904struct acpi_2_xsdt *xsdt, *xsdt_mod;
905
906// FIXME: handle 64-bit address correctly
907
908xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
909DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
910xsdt->Length);
911if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
912{
913uint64_t *xsdt_entries;
914int xsdt_entries_num, i;
915int dropoffset=0;
916
917// mozo: using malloc cos I didn't found how to free already allocated kernel memory
918xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
919memcpy(xsdt_mod, xsdt, xsdt->Length);
920rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
921xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
922xsdt_entries=(uint64_t *)(xsdt_mod+1);
923for (i=0;i<xsdt_entries_num;i++)
924{
925char *table=(char *)((uint32_t)(xsdt_entries[i]));
926if (!table)
927continue;
928
929xsdt_entries[i-dropoffset]=xsdt_entries[i];
930
931if (drop_ssdt && tableSign(table, "SSDT"))
932{
933dropoffset++;
934continue;
935}
936if (tableSign(table, "DSDT"))
937{
938DBG("DSDT found\n");
939
940if (new_dsdt)
941xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
942
943DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
944
945continue;
946}
947if (tableSign(table, "FACP"))
948{
949struct acpi_2_fadt *fadt, *fadt_mod;
950fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
951
952DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
953fadt->Length);
954
955if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
956{
957verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
958goto drop_xsdt;
959}
960
961fadt_mod = patch_fadt(fadt, new_dsdt);
962xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
963
964DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
965
966// Generate _CST SSDT
967if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
968{
969generate_cstates = false; // Generate SSDT only once!
970ssdt_count++;
971}
972
973// Generating _PSS SSDT
974if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
975{
976generate_pstates = false; // Generate SSDT only once!
977ssdt_count++;
978}
979
980continue;
981}
982
983DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
984
985}
986
987// Allocate xsdt in Kernel memory area
988xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
989struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
990memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
991free(xsdt_mod); xsdt_mod = xsdt_copy;
992rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
993xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
994xsdt_entries=(uint64_t *)(xsdt_mod+1);
995
996// Mozodojo: Insert additional SSDTs into XSDT
997if(ssdt_count>0)
998{
999int j;
1000
1001for (j=0; j<ssdt_count; j++)
1002xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1003
1004verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1005}
1006
1007// Correct the checksum of XSDT
1008xsdt_mod->Checksum=0;
1009xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1010}
1011else
1012{
1013drop_xsdt:
1014
1015DBG("About to drop XSDT\n");
1016
1017/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1018 * A Better strategy would be to generate
1019 */
1020
1021rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1022verbose("XSDT not found or XSDT incorrect\n");
1023}
1024}
1025
1026// Correct the checksum of RSDP
1027
1028DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1029
1030rsdp_mod->Checksum=0;
1031rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1032
1033DBG("New checksum %d\n", rsdp_mod->Checksum);
1034
1035if (version)
1036{
1037DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1038
1039rsdp_mod->ExtendedChecksum=0;
1040rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1041
1042DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1043
1044}
1045
1046//verbose("Patched ACPI version %d DSDT\n", version+1);
1047if (version)
1048{
1049/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1050acpi20_p = (uint32_t)rsdp_mod;
1051addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1052}
1053else
1054{
1055/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1056acpi10_p = (uint32_t)rsdp_mod;
1057addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1058}
1059}
1060#if DEBUG_ACPI
1061printf("Press a key to continue... (DEBUG_ACPI)\n");
1062getc();
1063#endif
1064return 1;
1065}
1066

Archive Download this file

Revision: 400