Chameleon

Chameleon Svn Source Tree

Root/branches/blackosx/i386/libsaio/acpi_patcher.c

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

Archive Download this file

Revision: 223