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

Archive Download this file

Revision: 316