Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 207