Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/modules/ACPIPatcher/acpi_patcher.c

Source at commit 656 created 13 years 4 months ago.
By meklort, ACPI patcher update, now in sync with trunk. Slight makefile change.
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
29extern char* gSMBIOSBoardModel;
30
31
32// Slice: New signature compare function
33boolean_t tableSign(char *table, const char *sgn)
34{
35int i;
36for (i=0; i<4; i++) {
37if ((table[i] &~0x20) != (sgn[i] &~0x20)) {
38return false;
39}
40}
41return true;
42}
43
44/* Gets the ACPI 1.0 RSDP address */
45static struct acpi_2_rsdp* getAddressOfAcpiTable()
46{
47 /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
48
49 void *acpi_addr = (void*)ACPI_RANGE_START;
50 for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
51 {
52 if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
53 {
54 uint8_t csum = checksum8(acpi_addr, 20);
55 if(csum == 0)
56 {
57 // Only return the table if it is a true version 1.0 table (Revision 0)
58 if(((struct acpi_2_rsdp*)acpi_addr)->Revision == 0)
59 return acpi_addr;
60 }
61 }
62 }
63 return NULL;
64}
65
66/* Gets the ACPI 2.0 RSDP address */
67static struct acpi_2_rsdp* getAddressOfAcpi20Table()
68{
69 /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
70
71 void *acpi_addr = (void*)ACPI_RANGE_START;
72 for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
73 {
74 if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
75 {
76 uint8_t csum = checksum8(acpi_addr, 20);
77
78 /* Only assume this is a 2.0 or better table if the revision is greater than 0
79 * NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1
80 * and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.
81 */
82
83 if(csum == 0 && (((struct acpi_2_rsdp*)acpi_addr)->Revision > 0))
84 {
85 uint8_t csum2 = checksum8(acpi_addr, sizeof(struct acpi_2_rsdp));
86 if(csum2 == 0)
87 return acpi_addr;
88 }
89 }
90 }
91 return NULL;
92}
93/** The folowing ACPI Table search algo. should be reused anywhere needed:*/
94int search_and_get_acpi_fd(const char * filename, const char ** outDirspec)
95{
96int fd = -1;
97char dirSpec[512] = "";
98
99// Try finding 'filename' in the usual places
100// Start searching any potential location for ACPI Table
101
102if(gSMBIOSBoardModel)
103{
104sprintf(dirSpec,"%s.%s", gSMBIOSBoardModel, filename);
105fd = open(dirSpec, 0);
106}
107if (fd < 0)
108{
109sprintf(dirSpec, "%s", filename);
110fd = open(dirSpec, 0);
111if (fd < 0)
112{
113if(gSMBIOSBoardModel)
114{
115sprintf(dirSpec, "/Extra/%s.%s", gSMBIOSBoardModel, filename);
116fd = open(dirSpec, 0);
117}
118if (fd < 0)
119{
120sprintf(dirSpec, "/Extra/%s", filename);
121fd = open(dirSpec, 0);
122if (fd < 0)
123{
124if(gSMBIOSBoardModel)
125{
126sprintf(dirSpec, "bt(0,0)/Extra/%s.%s", gSMBIOSBoardModel, filename);
127fd = open(dirSpec, 0);
128}
129if (fd < 0)
130{
131sprintf(dirSpec, "bt(0,0)/Extra/%s", filename);
132fd = open(dirSpec, 0);
133}
134}
135}
136}
137}
138
139if (fd < 0)
140{
141// NOT FOUND:
142verbose("ACPI table not found: %s\n", filename);
143*dirSpec = '\0';
144}
145
146if (outDirspec) *outDirspec = dirSpec;
147return fd;
148}
149
150
151void *loadACPITable (const char * filename)
152{
153void *tableAddr;
154const char * dirspec=NULL;
155
156int fd = search_and_get_acpi_fd(filename, &dirspec);
157
158if (fd>=0)
159{
160tableAddr=(void*)AllocateKernelMemory(file_size (fd));
161if (tableAddr)
162{
163if (read (fd, tableAddr, file_size (fd))!=file_size (fd))
164{
165printf("Couldn't read table %s\n",dirspec);
166free (tableAddr);
167close (fd);
168return NULL;
169}
170
171DBG("Table %s read and stored at: %x\n", dirspec, tableAddr);
172close (fd);
173return tableAddr;
174}
175close (fd);
176printf("Couldn't allocate memory for table \n", dirspec);
177}
178//printf("Couldn't find table %s\n", filename);
179return NULL;
180}
181
182uint8_tacpi_cpu_count = 0;
183char* acpi_cpu_name[32];
184
185void get_acpi_cpu_names(unsigned char* dsdt, uint32_t length)
186{
187uint32_t i;
188
189for (i=0; i<length-7; i++)
190{
191if (dsdt[i] == 0x5B && dsdt[i+1] == 0x83) // ProcessorOP
192{
193uint32_t offset = i + 3 + (dsdt[i+2] >> 6);
194
195bool add_name = true;
196
197uint8_t j;
198
199for (j=0; j<4; j++)
200{
201char c = dsdt[offset+j];
202
203if (!aml_isvalidchar(c))
204{
205add_name = false;
206verbose("Invalid character found in ProcessorOP 0x%x!\n", c);
207break;
208}
209}
210
211if (add_name)
212{
213acpi_cpu_name[acpi_cpu_count] = malloc(4);
214memcpy(acpi_cpu_name[acpi_cpu_count], dsdt+offset, 4);
215i = offset + 5;
216
217verbose("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]);
218
219if (++acpi_cpu_count == 32) return;
220}
221}
222}
223}
224
225struct acpi_2_ssdt *generate_cst_ssdt(struct acpi_2_fadt* fadt)
226{
227char ssdt_header[] =
228{
2290x53, 0x53, 0x44, 0x54, 0xE7, 0x00, 0x00, 0x00, /* SSDT.... */
2300x01, 0x17, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x41, /* ..PmRefA */
2310x43, 0x70, 0x75, 0x43, 0x73, 0x74, 0x00, 0x00, /* CpuCst.. */
2320x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* ....INTL */
2330x31, 0x03, 0x10, 0x20 /* 1.._*/
234};
235
236char cstate_resource_template[] =
237{
2380x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F,
2390x01, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
2400x00, 0x00, 0x00, 0x79, 0x00
241};
242
243if (Platform->CPU.Vendor != 0x756E6547) {
244verbose ("Not an Intel platform: C-States will not be generated !!!\n");
245return NULL;
246}
247
248if (fadt == NULL) {
249verbose ("FACP not exists: C-States will not be generated !!!\n");
250return NULL;
251}
252
253struct acpi_2_dsdt* dsdt = (void*)fadt->DSDT;
254
255if (dsdt == NULL) {
256verbose ("DSDT not found: C-States will not be generated !!!\n");
257return NULL;
258}
259
260if (acpi_cpu_count == 0)
261get_acpi_cpu_names((void*)dsdt, dsdt->Length);
262
263if (acpi_cpu_count > 0)
264{
265bool c2_enabled = false;
266bool c3_enabled = false;
267bool c4_enabled = false;
268
269getBoolForKey(kEnableC2States, &c2_enabled, &bootInfo->bootConfig);
270getBoolForKey(kEnableC3States, &c3_enabled, &bootInfo->bootConfig);
271getBoolForKey(kEnableC4States, &c4_enabled, &bootInfo->bootConfig);
272
273c2_enabled = c2_enabled | (fadt->C2_Latency < 100);
274c3_enabled = c3_enabled | (fadt->C3_Latency < 1000);
275
276unsigned char cstates_count = 1 + (c2_enabled ? 1 : 0) + (c3_enabled ? 1 : 0);
277
278struct aml_chunk* root = aml_create_node(NULL);
279aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
280struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
281struct aml_chunk* name = aml_add_name(scop, "CST_");
282struct aml_chunk* pack = aml_add_package(name);
283aml_add_byte(pack, cstates_count);
284
285struct aml_chunk* tmpl = aml_add_package(pack);
286cstate_resource_template[11] = 0x00; // C1
287aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
288aml_add_byte(tmpl, 0x01); // C1
289aml_add_byte(tmpl, 0x01); // Latency
290aml_add_word(tmpl, 0x03e8); // Power
291
292// C2
293if (c2_enabled)
294{
295tmpl = aml_add_package(pack);
296cstate_resource_template[11] = 0x10; // C2
297aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
298aml_add_byte(tmpl, 0x02); // C2
299aml_add_byte(tmpl, fadt->C2_Latency);
300aml_add_word(tmpl, 0x01f4); // Power
301}
302// C4
303if (c4_enabled)
304{
305tmpl = aml_add_package(pack);
306cstate_resource_template[11] = 0x30; // C4
307aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
308aml_add_byte(tmpl, 0x04); // C4
309aml_add_word(tmpl, fadt->C3_Latency / 2); // TODO: right latency for C4
310aml_add_byte(tmpl, 0xfa); // Power
311}
312else
313// C3
314if (c3_enabled)
315{
316tmpl = aml_add_package(pack);
317cstate_resource_template[11] = 0x20; // C3
318aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
319aml_add_byte(tmpl, 0x03); // C3
320aml_add_word(tmpl, fadt->C3_Latency);
321aml_add_word(tmpl, 0x015e); // Power
322}
323
324
325// Aliaces
326int i;
327for (i = 0; i < acpi_cpu_count; i++)
328{
329char name[9];
330sprintf(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]);
331
332scop = aml_add_scope(root, name);
333aml_add_alias(scop, "CST_", "_CST");
334}
335
336aml_calculate_size(root);
337
338struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
339
340aml_write_node(root, (void*)ssdt, 0);
341
342ssdt->Length = root->Size;
343ssdt->Checksum = 0;
344ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
345
346aml_destroy_node(root);
347
348//dumpPhysAddr("C-States SSDT content: ", ssdt, ssdt->Length);
349
350verbose ("SSDT with CPU C-States generated successfully\n");
351
352return ssdt;
353}
354else
355{
356verbose ("ACPI CPUs not found: C-States not generated !!!\n");
357}
358
359return NULL;
360}
361
362struct acpi_2_ssdt *generate_pss_ssdt(struct acpi_2_dsdt* dsdt)
363{
364char ssdt_header[] =
365{
3660x53, 0x53, 0x44, 0x54, 0x7E, 0x00, 0x00, 0x00, /* SSDT.... */
3670x01, 0x6A, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x00, /* ..PmRef. */
3680x43, 0x70, 0x75, 0x50, 0x6D, 0x00, 0x00, 0x00, /* CpuPm... */
3690x00, 0x30, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* .0..INTL */
3700x31, 0x03, 0x10, 0x20,/* 1.._*/
371};
372
373if (Platform->CPU.Vendor != 0x756E6547) {
374verbose ("Not an Intel platform: P-States will not be generated !!!\n");
375return NULL;
376}
377
378if (!(Platform->CPU.Features & CPU_FEATURE_MSR)) {
379verbose ("Unsupported CPU: P-States will not be generated !!!\n");
380return NULL;
381}
382
383if (acpi_cpu_count == 0)
384get_acpi_cpu_names((void*)dsdt, dsdt->Length);
385
386if (acpi_cpu_count > 0)
387{
388struct p_state initial, maximum, minimum, p_states[32];
389uint8_t p_states_count = 0;
390
391// Retrieving P-States, ported from code by superhai (c)
392switch (Platform->CPU.Family) {
393case 0x06:
394{
395switch (Platform->CPU.Model)
396{
397case 0x0D: // ?
398case CPU_MODEL_YONAH: // Yonah
399case CPU_MODEL_MEROM: // Merom
400case CPU_MODEL_PENRYN: // Penryn
401case CPU_MODEL_ATOM: // Intel Atom (45nm)
402{
403bool cpu_dynamic_fsb = false;
404
405if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27))
406{
407wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28)));
408delay(1);
409cpu_dynamic_fsb = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);
410}
411
412bool cpu_noninteger_bus_ratio = (rdmsr64(MSR_IA32_PERF_STATUS) & (1ULL << 46));
413
414initial.Control = rdmsr64(MSR_IA32_PERF_STATUS);
415
416maximum.Control = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 32) & 0x1F3F) | (0x4000 * cpu_noninteger_bus_ratio);
417maximum.CID = ((maximum.FID & 0x1F) << 1) | cpu_noninteger_bus_ratio;
418
419minimum.FID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 24) & 0x1F) | (0x80 * cpu_dynamic_fsb);
420minimum.VID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 48) & 0x3F);
421
422if (minimum.FID == 0)
423{
424uint64_t msr;
425uint8_t i;
426// Probe for lowest fid
427for (i = maximum.FID; i >= 0x6; i--)
428{
429msr = rdmsr64(MSR_IA32_PERF_CONTROL);
430wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (i << 8) | minimum.VID);
431intel_waitforsts();
432minimum.FID = (rdmsr64(MSR_IA32_PERF_STATUS) >> 8) & 0x1F;
433delay(1);
434}
435
436msr = rdmsr64(MSR_IA32_PERF_CONTROL);
437wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
438intel_waitforsts();
439}
440
441if (minimum.VID == maximum.VID)
442{
443uint64_t msr;
444uint8_t i;
445// Probe for lowest vid
446for (i = maximum.VID; i > 0xA; i--)
447{
448msr = rdmsr64(MSR_IA32_PERF_CONTROL);
449wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (minimum.FID << 8) | i);
450intel_waitforsts();
451minimum.VID = rdmsr64(MSR_IA32_PERF_STATUS) & 0x3F;
452delay(1);
453}
454
455msr = rdmsr64(MSR_IA32_PERF_CONTROL);
456wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
457intel_waitforsts();
458}
459
460minimum.CID = ((minimum.FID & 0x1F) << 1) >> cpu_dynamic_fsb;
461
462// Sanity check
463if (maximum.CID < minimum.CID)
464{
465DBG("Insane FID values!");
466p_states_count = 0;
467}
468else
469{
470// Finalize P-States
471// Find how many P-States machine supports
472p_states_count = maximum.CID - minimum.CID + 1;
473
474if (p_states_count > 32)
475p_states_count = 32;
476
477uint8_t vidstep;
478uint8_t i = 0, u, invalid = 0;
479
480vidstep = ((maximum.VID << 2) - (minimum.VID << 2)) / (p_states_count - 1);
481
482for (u = 0; u < p_states_count; u++)
483{
484i = u - invalid;
485
486p_states[i].CID = maximum.CID - u;
487p_states[i].FID = (p_states[i].CID >> 1);
488
489if (p_states[i].FID < 0x6)
490{
491if (cpu_dynamic_fsb)
492p_states[i].FID = (p_states[i].FID << 1) | 0x80;
493}
494else if (cpu_noninteger_bus_ratio)
495{
496p_states[i].FID = p_states[i].FID | (0x40 * (p_states[i].CID & 0x1));
497}
498
499if (i && p_states[i].FID == p_states[i-1].FID)
500invalid++;
501
502p_states[i].VID = ((maximum.VID << 2) - (vidstep * u)) >> 2;
503
504uint32_t multiplier = p_states[i].FID & 0x1f;// = 0x08
505bool half = p_states[i].FID & 0x40;// = 0x01
506bool dfsb = p_states[i].FID & 0x80;// = 0x00
507uint32_t fsb = Platform->CPU.FSBFrequency / 1000000; // = 400
508uint32_t halffsb = (fsb + 1) >> 1;// = 200
509uint32_t frequency = (multiplier * fsb);// = 3200
510
511p_states[i].Frequency = (frequency + (half * halffsb)) >> dfsb;// = 3200 + 200 = 3400
512}
513
514p_states_count -= invalid;
515}
516
517break;
518}
519case CPU_MODEL_FIELDS:
520case CPU_MODEL_DALES:
521case CPU_MODEL_DALES_32NM:
522case CPU_MODEL_NEHALEM:
523case CPU_MODEL_NEHALEM_EX:
524case CPU_MODEL_WESTMERE:
525case CPU_MODEL_WESTMERE_EX:
526{
527maximum.Control = rdmsr64(MSR_IA32_PERF_STATUS) & 0xff; // Seems it always contains maximum multiplier value (with turbo, that's we need)...
528minimum.Control = (rdmsr64(MSR_PLATFORM_INFO) >> 40) & 0xff;
529
530verbose("P-States: min 0x%x, max 0x%x\n", minimum.Control, maximum.Control);
531
532// Sanity check
533if (maximum.Control < minimum.Control)
534{
535DBG("Insane control values!");
536p_states_count = 0;
537}
538else
539{
540uint8_t i;
541p_states_count = 0;
542
543for (i = maximum.Control; i >= minimum.Control; i--)
544{
545p_states[p_states_count].Control = i;
546p_states[p_states_count].CID = p_states[p_states_count].Control << 1;
547p_states[p_states_count].Frequency = (Platform->CPU.FSBFrequency / 1000000) * i;
548p_states_count++;
549}
550}
551
552break;
553}
554default:
555verbose ("Unsupported CPU: P-States not generated !!!\n");
556break;
557}
558}
559}
560
561// Generating SSDT
562if (p_states_count > 0)
563{
564int i;
565
566struct aml_chunk* root = aml_create_node(NULL);
567aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
568struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
569struct aml_chunk* name = aml_add_name(scop, "PSS_");
570struct aml_chunk* pack = aml_add_package(name);
571
572for (i = 0; i < p_states_count; i++)
573{
574struct aml_chunk* pstt = aml_add_package(pack);
575
576aml_add_dword(pstt, p_states[i].Frequency);
577aml_add_dword(pstt, 0x00000000); // Power
578aml_add_dword(pstt, 0x0000000A); // Latency
579aml_add_dword(pstt, 0x0000000A); // Latency
580aml_add_dword(pstt, p_states[i].Control);
581aml_add_dword(pstt, i+1); // Status
582}
583
584// Add aliaces
585for (i = 0; i < acpi_cpu_count; i++)
586{
587char name[9];
588sprintf(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]);
589
590scop = aml_add_scope(root, name);
591aml_add_alias(scop, "PSS_", "_PSS");
592}
593
594aml_calculate_size(root);
595
596struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
597
598aml_write_node(root, (void*)ssdt, 0);
599
600ssdt->Length = root->Size;
601ssdt->Checksum = 0;
602ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
603
604aml_destroy_node(root);
605
606//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);
607
608verbose ("SSDT with CPU P-States generated successfully\n");
609
610return ssdt;
611}
612}
613else
614{
615verbose ("ACPI CPUs not found: P-States not generated !!!\n");
616}
617
618return NULL;
619}
620
621struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)
622{
623extern void setupSystemType();
624
625struct acpi_2_fadt *fadt_mod;
626bool fadt_rev2_needed = false;
627bool fix_restart;
628const char * value;
629
630// Restart Fix
631if (Platform->CPU.Vendor == 0x756E6547) {/* Intel */
632fix_restart = true;
633getBoolForKey(kRestartFix, &fix_restart, &bootInfo->bootConfig);
634} else {
635verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
636fix_restart = false;
637}
638
639if (fix_restart) fadt_rev2_needed = true;
640
641// Allocate new fadt table
642if (fadt->Length < 0x84 && fadt_rev2_needed)
643{
644fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
645memcpy(fadt_mod, fadt, fadt->Length);
646fadt_mod->Length = 0x84;
647fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
648}
649else
650{
651fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
652memcpy(fadt_mod, fadt, fadt->Length);
653}
654// Determine system type / PM_Model
655if ( (value=getStringForKey(kSystemType, &bootInfo->bootConfig))!=NULL)
656{
657if (Platform->Type > 6)
658{
659if(fadt_mod->PM_Profile<=6)
660Platform->Type = fadt_mod->PM_Profile; // get the fadt if correct
661else
662Platform->Type = 1;/* Set a fixed value (Desktop) */
663verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform->Type);
664}
665else
666Platform->Type = (unsigned char) strtoul(value, NULL, 10);
667}
668// Set PM_Profile from System-type if only user wanted this value to be forced
669if (fadt_mod->PM_Profile != Platform->Type)
670{
671 if (value)
672{ // user has overriden the SystemType so take care of it in FACP
673verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform->Type);
674fadt_mod->PM_Profile = Platform->Type;
675 }
676 else
677 { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
678Platform->Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
679 }
680}
681// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
682// because we need to take care of facp original content, if it is correct.
683setupSystemType();
684
685// Patch FADT to fix restart
686if (fix_restart)
687{
688fadt_mod->Flags|= 0x400;
689fadt_mod->Reset_SpaceID= 0x01; // System I/O
690fadt_mod->Reset_BitWidth= 0x08; // 1 byte
691fadt_mod->Reset_BitOffset= 0x00; // Offset 0
692fadt_mod->Reset_AccessWidth= 0x01; // Byte access
693fadt_mod->Reset_Address= 0x0cf9; // Address of the register
694fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
695verbose("FADT: Restart Fix applied!\n");
696}
697
698// Patch DSDT Address if we have loaded DSDT.aml
699if(new_dsdt)
700{
701DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
702
703// Insert old dsdt into the IORegistery
704Node* node = DT__FindNode("/dsdt", false);
705if(node == NULL)
706{
707// Only add if not already here
708Node* node = DT__FindNode("/", false);
709
710if(node != NULL)
711{
712node = DT__AddChild(node, "dsdt");
713
714struct acpi_2_dsdt *dsdt;
715dsdt = (struct acpi_2_dsdt*) (fadt_mod->DSDT);
716DT__AddProperty(node, "originaldsdt", (dsdt->Length + sizeof(struct acpi_2_dsdt) - 1), (void*)dsdt);/// Insert old dsdt. Length is header length (36) + dsdt length
717
718}
719}
720
721fadt_mod->DSDT=(uint32_t)new_dsdt;
722if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
723fadt_mod->X_DSDT=(uint32_t)new_dsdt;
724
725DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
726
727verbose("FADT: Using custom DSDT!\n");
728}
729
730// Correct the checksum
731fadt_mod->Checksum=0;
732fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
733
734return fadt_mod;
735}
736
737/* Setup ACPI without replacing DSDT. */
738int setupAcpiNoMod()
739{
740//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
741//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
742/* XXX aserebln why uint32 cast if pointer is uint64 ? */
743acpi10_p = (uint32_t)getAddressOfAcpiTable();
744acpi20_p = (uint32_t)getAddressOfAcpi20Table();
745addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
746if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
747return 1;
748}
749
750/* Setup ACPI. Replace DSDT if DSDT.aml is found */
751int setupAcpi(void)
752{
753int version;
754void *new_dsdt;
755
756const char *filename;
757char dirSpec[128];
758int len = 0;
759
760// Try using the file specified with the DSDT option
761if (getValueForKey(kDSDT, &filename, &len, &bootInfo->bootConfig))
762{
763sprintf(dirSpec, filename);
764}
765else
766{
767sprintf(dirSpec, "DSDT.aml");
768}
769
770// Load replacement DSDT
771new_dsdt = loadACPITable(dirSpec);
772if(!new_dsdt)
773{
774sprintf(dirSpec, "DSDT.%s.aml", gSMBIOSBoardModel);
775new_dsdt = loadACPITable(dirSpec);
776}
777
778// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
779/*if (!new_dsdt)
780 {
781 return setupAcpiNoMod();
782 }*/
783
784// Mozodojo: Load additional SSDTs
785struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
786int ssdt_count=0;
787
788// SSDT Options
789bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
790
791getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->bootConfig);
792getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->bootConfig);
793getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->bootConfig);
794
795{
796int i;
797
798for (i=0; i<30; i++)
799{
800char filename[512];
801
802sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
803
804if(new_ssdt[ssdt_count] = loadACPITable(filename))
805{
806ssdt_count++;
807}
808else
809{
810break;
811}
812}
813}
814
815// Do the same procedure for both versions of ACPI
816for (version=0; version<2; version++) {
817struct acpi_2_rsdp *rsdp, *rsdp_mod;
818struct acpi_2_rsdt *rsdt, *rsdt_mod;
819int rsdplength;
820
821// Find original rsdp
822rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
823if (!rsdp)
824{
825DBG("No ACPI version %d found. Ignoring\n", version+1);
826if (version)
827addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
828else
829addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
830continue;
831}
832rsdplength=version?rsdp->Length:20;
833
834DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
835
836/* FIXME: no check that memory allocation succeeded
837 * Copy and patch RSDP,RSDT, XSDT and FADT
838 * For more info see ACPI Specification pages 110 and following
839 */
840
841rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
842memcpy(rsdp_mod, rsdp, rsdplength);
843rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
844
845DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
846
847if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
848{
849uint32_t *rsdt_entries;
850int rsdt_entries_num;
851int dropoffset=0, i;
852
853// mozo: using malloc cos I didn't found how to free already allocated kernel memory
854rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
855memcpy (rsdt_mod, rsdt, rsdt->Length);
856rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
857rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
858rsdt_entries=(uint32_t *)(rsdt_mod+1);
859for (i=0;i<rsdt_entries_num;i++)
860{
861char *table=(char *)(rsdt_entries[i]);
862if (!table)
863continue;
864
865DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
866
867rsdt_entries[i-dropoffset]=rsdt_entries[i];
868
869if (drop_ssdt && tableSign(table, "SSDT"))
870{
871dropoffset++;
872continue;
873}
874if (tableSign(table, "DSDT"))
875{
876DBG("DSDT found\n");
877
878if(new_dsdt)
879rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
880
881continue;
882}
883if (tableSign(table, "FACP"))
884{
885struct acpi_2_fadt *fadt, *fadt_mod;
886fadt=(struct acpi_2_fadt *)rsdt_entries[i];
887
888DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
889
890if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
891{
892printf("FADT incorrect. Not modified\n");
893continue;
894}
895
896fadt_mod = patch_fadt(fadt, new_dsdt);
897rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
898
899// Generate _CST SSDT
900if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
901{
902generate_cstates = false; // Generate SSDT only once!
903ssdt_count++;
904}
905
906// Generating _PSS SSDT
907if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
908{
909generate_pstates = false; // Generate SSDT only once!
910ssdt_count++;
911}
912
913continue;
914}
915}
916DBG("\n");
917
918// Allocate rsdt in Kernel memory area
919rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
920struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
921memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
922free(rsdt_mod); rsdt_mod = rsdt_copy;
923rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
924rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
925rsdt_entries=(uint32_t *)(rsdt_mod+1);
926
927// Mozodojo: Insert additional SSDTs into RSDT
928if(ssdt_count>0)
929{
930int j;
931
932for (j=0; j<ssdt_count; j++)
933rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
934
935verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
936}
937
938// Correct the checksum of RSDT
939DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
940
941rsdt_mod->Checksum=0;
942rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
943
944DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
945}
946else
947{
948rsdp_mod->RsdtAddress=0;
949printf("RSDT not found or RSDT incorrect\n");
950}
951
952if (version)
953{
954struct acpi_2_xsdt *xsdt, *xsdt_mod;
955
956// FIXME: handle 64-bit address correctly
957
958xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
959DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
960xsdt->Length);
961if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
962{
963uint64_t *xsdt_entries;
964int xsdt_entries_num, i;
965int dropoffset=0;
966
967// mozo: using malloc cos I didn't found how to free already allocated kernel memory
968xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
969memcpy(xsdt_mod, xsdt, xsdt->Length);
970rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
971xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
972xsdt_entries=(uint64_t *)(xsdt_mod+1);
973for (i=0;i<xsdt_entries_num;i++)
974{
975char *table=(char *)((uint32_t)(xsdt_entries[i]));
976if (!table)
977continue;
978
979xsdt_entries[i-dropoffset]=xsdt_entries[i];
980
981if (drop_ssdt && tableSign(table, "SSDT"))
982{
983dropoffset++;
984continue;
985}
986if (tableSign(table, "DSDT"))
987{
988DBG("DSDT found\n");
989
990if (new_dsdt)
991xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
992
993DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
994
995continue;
996}
997if (tableSign(table, "FACP"))
998{
999struct acpi_2_fadt *fadt, *fadt_mod;
1000fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
1001
1002DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
1003fadt->Length);
1004
1005if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
1006{
1007verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
1008goto drop_xsdt;
1009}
1010
1011fadt_mod = patch_fadt(fadt, new_dsdt);
1012xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1013
1014DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1015
1016// Generate _CST SSDT
1017if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1018{
1019generate_cstates = false; // Generate SSDT only once!
1020ssdt_count++;
1021}
1022
1023// Generating _PSS SSDT
1024if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1025{
1026generate_pstates = false; // Generate SSDT only once!
1027ssdt_count++;
1028}
1029
1030continue;
1031}
1032
1033DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1034
1035}
1036
1037// Allocate xsdt in Kernel memory area
1038xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
1039struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
1040memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
1041free(xsdt_mod); xsdt_mod = xsdt_copy;
1042rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1043xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1044xsdt_entries=(uint64_t *)(xsdt_mod+1);
1045
1046// Mozodojo: Insert additional SSDTs into XSDT
1047if(ssdt_count>0)
1048{
1049int j;
1050
1051for (j=0; j<ssdt_count; j++)
1052xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1053
1054verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1055}
1056
1057// Correct the checksum of XSDT
1058xsdt_mod->Checksum=0;
1059xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1060}
1061else
1062{
1063drop_xsdt:
1064
1065DBG("About to drop XSDT\n");
1066
1067/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1068 * A Better strategy would be to generate
1069 */
1070
1071rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1072verbose("XSDT not found or XSDT incorrect\n");
1073}
1074}
1075
1076// Correct the checksum of RSDP
1077
1078DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1079
1080rsdp_mod->Checksum=0;
1081rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1082
1083DBG("New checksum %d\n", rsdp_mod->Checksum);
1084
1085if (version)
1086{
1087DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1088
1089rsdp_mod->ExtendedChecksum=0;
1090rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1091
1092DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1093
1094}
1095
1096//verbose("Patched ACPI version %d DSDT\n", version+1);
1097if (version)
1098{
1099/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1100acpi20_p = (uint32_t)rsdp_mod;
1101addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1102}
1103else
1104{
1105/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1106acpi10_p = (uint32_t)rsdp_mod;
1107addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1108}
1109}
1110#if DEBUG_ACPI
1111printf("Press a key to continue... (DEBUG_ACPI)\n");
1112getc();
1113#endif
1114return 1;
1115}
1116

Archive Download this file

Revision: 656