Chameleon

Chameleon Svn Source Tree

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

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

Archive Download this file

Revision: 485