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_DALES:
503case CPU_MODEL_DALES_32NM:
504case CPU_MODEL_NEHALEM:
505case CPU_MODEL_NEHALEM_EX:
506case CPU_MODEL_WESTMERE:
507case CPU_MODEL_WESTMERE_EX:
508default:
509verbose ("Unsupported CPU: P-States not generated !!!\n");
510break;
511}
512}
513}
514
515// Generating SSDT
516if (p_states_count > 0)
517{
518int i;
519
520struct aml_chunk* root = aml_create_node(NULL);
521aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
522struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
523struct aml_chunk* name = aml_add_name(scop, "PSS_");
524struct aml_chunk* pack = aml_add_package(name);
525
526for (i = 0; i < p_states_count; i++)
527{
528struct aml_chunk* pstt = aml_add_package(pack);
529
530aml_add_dword(pstt, p_states[i].Frequency);
531aml_add_dword(pstt, 0x00000000); // Power
532aml_add_dword(pstt, 0x0000000A); // Latency
533aml_add_dword(pstt, 0x0000000A); // Latency
534aml_add_dword(pstt, p_states[i].Control);
535aml_add_dword(pstt, i+1); // Status
536}
537
538// Add aliaces
539for (i = 0; i < acpi_cpu_count; i++)
540{
541char name[9];
542sprintf(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]);
543
544scop = aml_add_scope(root, name);
545aml_add_alias(scop, "PSS_", "_PSS");
546}
547
548aml_calculate_size(root);
549
550struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
551
552aml_write_node(root, (void*)ssdt, 0);
553
554ssdt->Length = root->Size;
555ssdt->Checksum = 0;
556ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
557
558aml_destroy_node(root);
559
560//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);
561
562verbose ("SSDT with CPU P-States generated successfully\n");
563
564return ssdt;
565}
566}
567else
568{
569verbose ("ACPI CPUs not found: P-States not generated !!!\n");
570}
571
572return NULL;
573}
574
575struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)
576{
577extern void setupSystemType();
578
579struct acpi_2_fadt *fadt_mod;
580bool fadt_rev2_needed = false;
581bool fix_restart;
582const char * value;
583
584// Restart Fix
585if (Platform.CPU.Vendor == 0x756E6547) // Intel
586{
587fix_restart = false; //Azi: think this should be false by default; i never needed any.
588// On the other hand, i could use a shutdown fix now and then :)
589getBoolForKey(kRestartFix, &fix_restart, &bootInfo->bootConfig);
590}
591else
592{
593verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
594fix_restart = false;
595}
596
597if (fix_restart) fadt_rev2_needed = true;
598
599// Allocate new fadt table
600if (fadt->Length < 0x84 && fadt_rev2_needed)
601{
602fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
603memcpy(fadt_mod, fadt, fadt->Length);
604fadt_mod->Length = 0x84;
605fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
606}
607else
608{
609fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
610memcpy(fadt_mod, fadt, fadt->Length);
611}
612// Determine system type / PM_Model
613if ( (value=getStringForKey(kSystemType, &bootInfo->bootConfig))!=NULL)
614{
615if (Platform.Type > 6)
616{
617if(fadt_mod->PM_Profile<=6)
618Platform.Type = fadt_mod->PM_Profile; // get the fadt if correct
619else
620Platform.Type = 1; // Set a fixed value (Desktop)
621verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform.Type);
622}
623else
624Platform.Type = (unsigned char) strtoul(value, NULL, 10);
625}
626// Set PM_Profile from System-type only if user wanted this value to be forced
627if (fadt_mod->PM_Profile != Platform.Type)
628{
629 if (value)
630{ // user has overriden the SystemType so take care of it in FACP
631verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);
632fadt_mod->PM_Profile = Platform.Type;
633 }
634 else
635 { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
636Platform.Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
637 }
638}
639// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
640// because we need to take care of facp original content, if it is correct.
641setupSystemType();
642
643// Patch FADT to fix restart
644if (fix_restart)
645{
646fadt_mod->Flags|= 0x400;
647fadt_mod->Reset_SpaceID= 0x01; // System I/O
648fadt_mod->Reset_BitWidth= 0x08; // 1 byte
649fadt_mod->Reset_BitOffset= 0x00; // Offset 0
650fadt_mod->Reset_AccessWidth= 0x01; // Byte access
651fadt_mod->Reset_Address= 0x0cf9; // Address of the register
652fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
653verbose("FADT: Restart Fix applied!\n");
654}
655
656// Patch DSDT Address if we have loaded DSDT.aml
657if(new_dsdt)
658{
659DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
660
661fadt_mod->DSDT=(uint32_t)new_dsdt;
662if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
663fadt_mod->X_DSDT=(uint32_t)new_dsdt;
664
665DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
666
667verbose("FADT: Using custom DSDT!\n");
668}
669
670// Correct the checksum
671fadt_mod->Checksum=0;
672fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
673
674return fadt_mod;
675}
676
677/* Setup ACPI without replacing DSDT. - not needed atm.
678int setupAcpiNoMod()
679{
680//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
681//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
682// XXX aserebln why uint32 cast if pointer is uint64 ?
683acpi10_p = (uint32_t)getAddressOfAcpiTable();
684acpi20_p = (uint32_t)getAddressOfAcpi20Table();
685addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
686if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
687return 1;
688}*/
689
690/* Setup ACPI. Replace DSDT if DSDT.aml is found */
691int setupAcpi(void)
692{
693int version;
694void *new_dsdt;
695
696// Load replacement DSDT
697new_dsdt=loadACPITable("DSDT.aml");
698// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
699/*if (!new_dsdt)
700 {
701 return setupAcpiNoMod();
702 }*/
703
704// Mozodojo: Load additional SSDTs
705struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
706int ssdt_count=0;
707
708// SSDT Options
709bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
710
711getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->bootConfig);
712getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->bootConfig);
713getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->bootConfig);
714
715{
716int i;
717
718for (i=0; i<30; i++)
719{
720char filename[512];
721
722sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
723
724if(new_ssdt[ssdt_count] = loadACPITable(filename))
725{
726ssdt_count++;
727}
728else
729{
730break;
731}
732}
733}
734
735// Do the same procedure for both versions of ACPI
736for (version=0; version<2; version++) {
737struct acpi_2_rsdp *rsdp, *rsdp_mod;
738struct acpi_2_rsdt *rsdt, *rsdt_mod;
739int rsdplength;
740
741// Find original rsdp
742rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
743if (!rsdp)
744{
745DBG("No ACPI version %d found. Ignoring\n", version+1);
746if (version)
747addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
748else
749addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
750continue;
751}
752rsdplength=version?rsdp->Length:20;
753
754DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
755
756/* FIXME: no check that memory allocation succeeded
757 * Copy and patch RSDP,RSDT, XSDT and FADT
758 * For more info see ACPI Specification pages 110 and following
759 */
760
761rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
762memcpy(rsdp_mod, rsdp, rsdplength);
763rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
764
765DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
766
767if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
768{
769uint32_t *rsdt_entries;
770int rsdt_entries_num;
771int dropoffset=0, i;
772
773// mozo: using malloc cos I didn't found how to free already allocated kernel memory
774rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
775memcpy (rsdt_mod, rsdt, rsdt->Length);
776rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
777rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
778rsdt_entries=(uint32_t *)(rsdt_mod+1);
779for (i=0;i<rsdt_entries_num;i++)
780{
781char *table=(char *)(rsdt_entries[i]);
782if (!table)
783continue;
784
785DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
786
787rsdt_entries[i-dropoffset]=rsdt_entries[i];
788
789if (drop_ssdt && tableSign(table, "SSDT"))
790{
791dropoffset++;
792continue;
793}
794if (tableSign(table, "DSDT"))
795{
796DBG("DSDT found\n");
797
798if(new_dsdt)
799rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
800
801continue;
802}
803if (tableSign(table, "FACP"))
804{
805struct acpi_2_fadt *fadt, *fadt_mod;
806fadt=(struct acpi_2_fadt *)rsdt_entries[i];
807
808DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
809
810if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
811{
812printf("FADT incorrect. Not modified\n");
813continue;
814}
815
816fadt_mod = patch_fadt(fadt, new_dsdt);
817rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
818
819// Generate _CST SSDT
820if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
821{
822generate_cstates = false; // Generate SSDT only once!
823ssdt_count++;
824}
825
826// Generating _PSS SSDT
827if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
828{
829generate_pstates = false; // Generate SSDT only once!
830ssdt_count++;
831}
832
833continue;
834}
835}
836DBG("\n");
837
838// Allocate rsdt in Kernel memory area
839rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
840struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
841memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
842free(rsdt_mod); rsdt_mod = rsdt_copy;
843rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
844rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
845rsdt_entries=(uint32_t *)(rsdt_mod+1);
846
847// Mozodojo: Insert additional SSDTs into RSDT
848if(ssdt_count>0)
849{
850int j;
851
852for (j=0; j<ssdt_count; j++)
853rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
854
855verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
856}
857
858// Correct the checksum of RSDT
859DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
860
861rsdt_mod->Checksum=0;
862rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
863
864DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
865}
866else
867{
868rsdp_mod->RsdtAddress=0;
869printf("RSDT not found or RSDT incorrect\n");
870}
871
872if (version)
873{
874struct acpi_2_xsdt *xsdt, *xsdt_mod;
875
876// FIXME: handle 64-bit address correctly
877
878xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
879DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
880xsdt->Length);
881if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
882{
883uint64_t *xsdt_entries;
884int xsdt_entries_num, i;
885int dropoffset=0;
886
887// mozo: using malloc cos I didn't found how to free already allocated kernel memory
888xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
889memcpy(xsdt_mod, xsdt, xsdt->Length);
890rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
891xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
892xsdt_entries=(uint64_t *)(xsdt_mod+1);
893for (i=0;i<xsdt_entries_num;i++)
894{
895char *table=(char *)((uint32_t)(xsdt_entries[i]));
896if (!table)
897continue;
898
899xsdt_entries[i-dropoffset]=xsdt_entries[i];
900
901if (drop_ssdt && tableSign(table, "SSDT"))
902{
903dropoffset++;
904continue;
905}
906if (tableSign(table, "DSDT"))
907{
908DBG("DSDT found\n");
909
910if (new_dsdt)
911xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
912
913DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
914
915continue;
916}
917if (tableSign(table, "FACP"))
918{
919struct acpi_2_fadt *fadt, *fadt_mod;
920fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
921
922DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
923fadt->Length);
924
925if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
926{
927verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
928goto drop_xsdt;
929}
930
931fadt_mod = patch_fadt(fadt, new_dsdt);
932xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
933
934DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
935
936// Generate _CST SSDT
937if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
938{
939generate_cstates = false; // Generate SSDT only once!
940ssdt_count++;
941}
942
943// Generating _PSS SSDT
944if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
945{
946generate_pstates = false; // Generate SSDT only once!
947ssdt_count++;
948}
949
950continue;
951}
952
953DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
954
955}
956
957// Allocate xsdt in Kernel memory area
958xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
959struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
960memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
961free(xsdt_mod); xsdt_mod = xsdt_copy;
962rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
963xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
964xsdt_entries=(uint64_t *)(xsdt_mod+1);
965
966// Mozodojo: Insert additional SSDTs into XSDT
967if(ssdt_count>0)
968{
969int j;
970
971for (j=0; j<ssdt_count; j++)
972xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
973
974verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
975}
976
977// Correct the checksum of XSDT
978xsdt_mod->Checksum=0;
979xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
980}
981else
982{
983drop_xsdt:
984
985DBG("About to drop XSDT\n");
986
987/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
988 * A Better strategy would be to generate
989 */
990
991rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
992verbose("XSDT not found or XSDT incorrect\n");
993}
994}
995
996// Correct the checksum of RSDP
997
998DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
999
1000rsdp_mod->Checksum=0;
1001rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1002
1003DBG("New checksum %d\n", rsdp_mod->Checksum);
1004
1005if (version)
1006{
1007DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1008
1009rsdp_mod->ExtendedChecksum=0;
1010rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1011
1012DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1013
1014}
1015
1016//verbose("Patched ACPI version %d DSDT\n", version+1);
1017if (version)
1018{
1019/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1020acpi20_p = (uint32_t)rsdp_mod;
1021addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1022}
1023else
1024{
1025/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1026acpi10_p = (uint32_t)rsdp_mod;
1027addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1028}
1029}
1030#if DEBUG_ACPI
1031printf("Press a key to continue... (DEBUG_ACPI)\n");
1032getc();
1033#endif
1034return 1;
1035}
1036

Archive Download this file

Revision: 354