Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/modules/ACPIPatcher/acpi_patcher.c

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

Archive Download this file

Revision: 632