Chameleon

Chameleon Svn Source Tree

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

Source at commit 648 created 9 years 4 months ago.
By meklort, ACPI patcher changes
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);
742if(!new_dsdt)
743{
744sprintf(dirSpec, "DSDT.%s.aml", gSMBIOSBoardModel);
745new_dsdt = loadACPITable(dirSpec);
746}
747// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
748/*if (!new_dsdt)
749 {
750 return setupAcpiNoMod();
751 }*/
752
753// Mozodojo: Load additional SSDTs
754struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
755int ssdt_count=0;
756
757// SSDT Options
758bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
759
760getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->bootConfig);
761getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->bootConfig);
762getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->bootConfig);
763
764{
765int i;
766
767for (i=0; i<30; i++)
768{
769char filename[512];
770
771sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
772
773if(new_ssdt[ssdt_count] = loadACPITable(filename))
774{
775ssdt_count++;
776}
777else
778{
779break;
780}
781}
782}
783
784// Do the same procedure for both versions of ACPI
785for (version=0; version<2; version++) {
786struct acpi_2_rsdp *rsdp, *rsdp_mod;
787struct acpi_2_rsdt *rsdt, *rsdt_mod;
788int rsdplength;
789
790// Find original rsdp
791rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
792if (!rsdp)
793{
794DBG("No ACPI version %d found. Ignoring\n", version+1);
795if (version)
796addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
797else
798addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
799continue;
800}
801rsdplength=version?rsdp->Length:20;
802
803DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
804
805/* FIXME: no check that memory allocation succeeded
806 * Copy and patch RSDP,RSDT, XSDT and FADT
807 * For more info see ACPI Specification pages 110 and following
808 */
809
810rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
811memcpy(rsdp_mod, rsdp, rsdplength);
812rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
813
814DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
815
816if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
817{
818uint32_t *rsdt_entries;
819int rsdt_entries_num;
820int dropoffset=0, i;
821
822// mozo: using malloc cos I didn't found how to free already allocated kernel memory
823rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
824memcpy (rsdt_mod, rsdt, rsdt->Length);
825rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
826rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
827rsdt_entries=(uint32_t *)(rsdt_mod+1);
828for (i=0;i<rsdt_entries_num;i++)
829{
830char *table=(char *)(rsdt_entries[i]);
831if (!table)
832continue;
833
834DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
835
836rsdt_entries[i-dropoffset]=rsdt_entries[i];
837
838if (drop_ssdt && tableSign(table, "SSDT"))
839{
840dropoffset++;
841continue;
842}
843if (tableSign(table, "DSDT"))
844{
845DBG("DSDT found\n");
846
847if(new_dsdt)
848rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
849
850continue;
851}
852if (tableSign(table, "FACP"))
853{
854struct acpi_2_fadt *fadt, *fadt_mod;
855fadt=(struct acpi_2_fadt *)rsdt_entries[i];
856
857DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
858
859if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
860{
861printf("FADT incorrect. Not modified\n");
862continue;
863}
864
865fadt_mod = patch_fadt(fadt, new_dsdt);
866rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
867
868// Generate _CST SSDT
869if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
870{
871generate_cstates = false; // Generate SSDT only once!
872ssdt_count++;
873}
874
875// Generating _PSS SSDT
876if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
877{
878generate_pstates = false; // Generate SSDT only once!
879ssdt_count++;
880}
881
882continue;
883}
884}
885DBG("\n");
886
887// Allocate rsdt in Kernel memory area
888rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
889struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
890memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
891free(rsdt_mod); rsdt_mod = rsdt_copy;
892rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
893rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
894rsdt_entries=(uint32_t *)(rsdt_mod+1);
895
896// Mozodojo: Insert additional SSDTs into RSDT
897if(ssdt_count>0)
898{
899int j;
900
901for (j=0; j<ssdt_count; j++)
902rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
903
904verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
905}
906
907// Correct the checksum of RSDT
908DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
909
910rsdt_mod->Checksum=0;
911rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
912
913DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
914}
915else
916{
917rsdp_mod->RsdtAddress=0;
918printf("RSDT not found or RSDT incorrect\n");
919}
920
921if (version)
922{
923struct acpi_2_xsdt *xsdt, *xsdt_mod;
924
925// FIXME: handle 64-bit address correctly
926
927xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
928DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
929xsdt->Length);
930if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
931{
932uint64_t *xsdt_entries;
933int xsdt_entries_num, i;
934int dropoffset=0;
935
936// mozo: using malloc cos I didn't found how to free already allocated kernel memory
937xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
938memcpy(xsdt_mod, xsdt, xsdt->Length);
939rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
940xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
941xsdt_entries=(uint64_t *)(xsdt_mod+1);
942for (i=0;i<xsdt_entries_num;i++)
943{
944char *table=(char *)((uint32_t)(xsdt_entries[i]));
945if (!table)
946continue;
947
948xsdt_entries[i-dropoffset]=xsdt_entries[i];
949
950if (drop_ssdt && tableSign(table, "SSDT"))
951{
952dropoffset++;
953continue;
954}
955if (tableSign(table, "DSDT"))
956{
957DBG("DSDT found\n");
958
959if (new_dsdt)
960xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
961
962DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
963
964continue;
965}
966if (tableSign(table, "FACP"))
967{
968struct acpi_2_fadt *fadt, *fadt_mod;
969fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
970
971DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
972fadt->Length);
973
974if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
975{
976verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
977goto drop_xsdt;
978}
979
980fadt_mod = patch_fadt(fadt, new_dsdt);
981xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
982
983DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
984
985// Generate _CST SSDT
986if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
987{
988generate_cstates = false; // Generate SSDT only once!
989ssdt_count++;
990}
991
992// Generating _PSS SSDT
993if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
994{
995generate_pstates = false; // Generate SSDT only once!
996ssdt_count++;
997}
998
999continue;
1000}
1001
1002DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1003
1004}
1005
1006// Allocate xsdt in Kernel memory area
1007xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
1008struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
1009memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
1010free(xsdt_mod); xsdt_mod = xsdt_copy;
1011rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1012xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1013xsdt_entries=(uint64_t *)(xsdt_mod+1);
1014
1015// Mozodojo: Insert additional SSDTs into XSDT
1016if(ssdt_count>0)
1017{
1018int j;
1019
1020for (j=0; j<ssdt_count; j++)
1021xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1022
1023verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1024}
1025
1026// Correct the checksum of XSDT
1027xsdt_mod->Checksum=0;
1028xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1029}
1030else
1031{
1032drop_xsdt:
1033
1034DBG("About to drop XSDT\n");
1035
1036/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1037 * A Better strategy would be to generate
1038 */
1039
1040rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1041verbose("XSDT not found or XSDT incorrect\n");
1042}
1043}
1044
1045// Correct the checksum of RSDP
1046
1047DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1048
1049rsdp_mod->Checksum=0;
1050rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1051
1052DBG("New checksum %d\n", rsdp_mod->Checksum);
1053
1054if (version)
1055{
1056DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1057
1058rsdp_mod->ExtendedChecksum=0;
1059rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1060
1061DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1062
1063}
1064
1065//verbose("Patched ACPI version %d DSDT\n", version+1);
1066if (version)
1067{
1068/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1069acpi20_p = (uint32_t)rsdp_mod;
1070addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1071}
1072else
1073{
1074/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1075acpi10_p = (uint32_t)rsdp_mod;
1076addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1077}
1078}
1079#if DEBUG_ACPI
1080printf("Press a key to continue... (DEBUG_ACPI)\n");
1081getc();
1082#endif
1083return 1;
1084}
1085

Archive Download this file

Revision: 648