Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 350