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

Archive Download this file

Revision: 218