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

Archive Download this file

Revision: 365