Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazileon/i386/libsaio/acpi_patcher.c

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

Archive Download this file

Revision: 353