Chameleon

Chameleon Svn Source Tree

Root/branches/slice/trunkM/i386/libsaio/acpi_patcher.c

1/*
2 * Copyright 2008 mackerintel
3 * 2010 mojodojo, slice
4 */
5
6#include "libsaio.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#include "smbios_getters.h"
17
18#ifndef DEBUG_ACPI
19#define DEBUG_ACPI 0
20#endif
21
22#if DEBUG_ACPI==2
23#define DBG(x...) {printf(x); sleep(1);}
24#elif DEBUG_ACPI==3
25#define DBG(x...) verbose(x)
26#elif DEBUG_ACPI==1
27#define DBG(x...) printf(x)
28#else
29#define DBG(x...)
30#endif
31
32bool fix_restart;
33uint64_t acpi10_p;
34uint64_t acpi20_p;
35
36int rsdplength;
37void *new_dsdt=NULL;
38
39extern char* gSMBIOSBoardModel;
40
41// Slice: New signature compare function
42boolean_t tableSign(char *table, const char *sgn)
43{
44int i;
45for (i=0; i<4; i++) {
46if ((table[i] &~0x20) != (sgn[i] &~0x20)) {
47return false;
48}
49}
50return true;
51}
52
53char st[20];
54char * FourChar(char * s)
55{
56int i;
57for(i=0; i<4;i++){
58st[i] = (isalpha(s[i])?s[i]:'.');
59}
60//strncpy(st, s, 4);
61st[4] = '\0';
62return st;
63}
64
65
66/* Gets the ACPI 1.0 RSDP address */
67static struct acpi_2_rsdp* getAddressOfAcpiTable()
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 if(csum == 0)
78 {
79 // Only return the table if it is a true version 1.0 table (Revision 0)
80 if(((struct acpi_2_rsdp*)acpi_addr)->Revision == 0)
81 return acpi_addr;
82 }
83 }
84 }
85 return NULL;
86}
87
88/* Gets the ACPI 2.0 RSDP address */
89static struct acpi_2_rsdp* getAddressOfAcpi20Table()
90{
91 /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
92
93 void *acpi_addr = (void*)ACPI_RANGE_START;
94 for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
95 {
96 if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
97 {
98 uint8_t csum = checksum8(acpi_addr, 20);
99
100 /* Only assume this is a 2.0 or better table if the revision is greater than 0
101 * NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1
102 * and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.
103 */
104
105 if(csum == 0 && (((struct acpi_2_rsdp*)acpi_addr)->Revision > 0))
106 {
107 uint8_t csum2 = checksum8(acpi_addr, sizeof(struct acpi_2_rsdp));
108 if(csum2 == 0)
109 return acpi_addr;
110 }
111 }
112 }
113 return NULL;
114}
115/** The folowing ACPI Table search algo. should be reused anywhere needed:*/
116int search_and_get_acpi_fd(const char * filename, const char ** outDirspec)
117{
118int fd = -1;
119char dirSpec[512] = "";
120
121// Try finding 'filename' in the usual places
122// Start searching any potential location for ACPI Table
123sprintf(dirSpec, "%s", filename);
124fd = open(dirSpec, 0);
125if (fd < 0)
126{
127sprintf(dirSpec, "/Extra/%s", filename);
128fd = open(dirSpec, 0);
129if (fd < 0)
130{
131sprintf(dirSpec, "bt(0,0)/Extra/%s", filename);
132fd = open(dirSpec, 0);
133}
134}
135
136if (fd < 0)
137{
138// NOT FOUND:
139//verbose("ACPI table not found: %s\n", filename);
140*dirSpec = '\0';
141}
142
143if (outDirspec) *outDirspec = dirSpec;
144return fd;
145}
146
147
148void *loadACPITable (const char * filename)
149{
150void *tableAddr;
151const char * dirspec=NULL;
152
153int fd = search_and_get_acpi_fd(filename, &dirspec);
154
155if (fd>=0)
156{
157tableAddr=(void*)AllocateKernelMemory(file_size (fd));
158if (tableAddr)
159{
160if (read (fd, tableAddr, file_size (fd))!=file_size (fd))
161{
162//verbose("Couldn't read table %s\n",dirspec);
163free (tableAddr);
164close (fd);
165return NULL;
166}
167
168DBG("Table %s read and stored at: %x\n", dirspec, tableAddr);
169close (fd);
170return tableAddr;
171}
172close (fd);
173verbose("Couldn't allocate memory for table \n", dirspec);
174}
175//printf("Couldn't find table %s\n", filename);
176return NULL;
177}
178
179uint8_tacpi_cpu_count = 0;
180char* acpi_cpu_name[32];
181uint32_t acpi_cpu_p_blk = 0;
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;
204DBG("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
215 if (acpi_cpu_count == 0)
216 acpi_cpu_p_blk = dsdt[i] | (dsdt[i+1] << 8);
217
218verbose("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]);
219
220if (++acpi_cpu_count == 32) return;
221}
222}
223}
224}
225
226struct acpi_2_ssdt *generate_cst_ssdt(struct acpi_2_fadt* fadt)
227{
228char ssdt_header[] =
229{
2300x53, 0x53, 0x44, 0x54, 0xE7, 0x00, 0x00, 0x00, /* SSDT.... */
2310x01, 0x17, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x41, /* ..PmRefA */
2320x43, 0x70, 0x75, 0x43, 0x73, 0x74, 0x00, 0x00, /* CpuCst.. */
2330x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* ....INTL */
2340x31, 0x03, 0x10, 0x20 /* 1.._*/
235};
236
237char resource_template_register_fixedhw[] =
238{
2390x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F,
2400x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2410x00, 0x00, 0x01, 0x79, 0x00
242};
243
244 char resource_template_register_systemio[] =
245{
2460x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x01,
247 0x08, 0x00, 0x00, 0x15, 0x04, 0x00, 0x00, 0x00,
248 0x00, 0x00, 0x00, 0x79, 0x00,
249 };
250
251if (Platform->CPU.Vendor != 0x756E6547) {
252verbose ("Not an Intel platform: C-States will not be generated !!!\n");
253return NULL;
254}
255
256if (fadt == NULL) {
257verbose ("FACP not exists: C-States will not be generated !!!\n");
258return NULL;
259}
260
261struct acpi_2_dsdt* dsdt = (void*)fadt->DSDT;
262
263if (dsdt == NULL) {
264verbose ("DSDT not found: C-States will not be generated !!!\n");
265return NULL;
266}
267
268if (acpi_cpu_count == 0)
269get_acpi_cpu_names((void*)dsdt, dsdt->Length);
270
271if (acpi_cpu_count > 0)
272{
273bool c2_enabled = false;
274bool c3_enabled = false;
275bool c4_enabled = false;
276 bool cst_using_sustemio = false;
277
278getBoolForKey(kEnableC2State, &c2_enabled, &bootInfo->chameleonConfig);
279getBoolForKey(kEnableC3State, &c3_enabled, &bootInfo->chameleonConfig);
280getBoolForKey(kEnableC4State, &c4_enabled, &bootInfo->chameleonConfig);
281 getBoolForKey(kCSTUsingSystemIO, &cst_using_sustemio, &bootInfo->chameleonConfig);
282
283c2_enabled = c2_enabled | (fadt->C2_Latency < 100);
284c3_enabled = c3_enabled | (fadt->C3_Latency < 1000);
285
286unsigned char cstates_count = 1 + (c2_enabled ? 1 : 0) + (c3_enabled ? 1 : 0);
287
288struct aml_chunk* root = aml_create_node(NULL);
289aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
290struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
291struct aml_chunk* name = aml_add_name(scop, "CST_");
292struct aml_chunk* pack = aml_add_package(name);
293aml_add_byte(pack, cstates_count);
294
295struct aml_chunk* tmpl = aml_add_package(pack);
296 if (cst_using_sustemio)
297 {
298 // C1
299 resource_template_register_fixedhw[8] = 0x00;
300 resource_template_register_fixedhw[9] = 0x00;
301 resource_template_register_fixedhw[18] = 0x00;
302 aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
303 aml_add_byte(tmpl, 0x01); // C1
304 aml_add_word(tmpl, 0x0001); // Latency
305 aml_add_dword(tmpl, 0x000003e8); // Power
306
307 uint8_t p_blk_lo, p_blk_hi;
308
309 if (c2_enabled) // C2
310 {
311 p_blk_lo = acpi_cpu_p_blk + 4;
312 p_blk_hi = (acpi_cpu_p_blk + 4) >> 8;
313
314 tmpl = aml_add_package(pack);
315 resource_template_register_systemio[11] = p_blk_lo; // C2
316 resource_template_register_systemio[12] = p_blk_hi; // C2
317 aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
318 aml_add_byte(tmpl, 0x02); // C2
319 aml_add_word(tmpl, 0x0040); // Latency
320 aml_add_dword(tmpl, 0x000001f4); // Power
321 }
322
323 if (c4_enabled) // C4
324 {
325 p_blk_lo = acpi_cpu_p_blk + 5;
326 p_blk_hi = (acpi_cpu_p_blk + 5) >> 8;
327
328 tmpl = aml_add_package(pack);
329 resource_template_register_systemio[11] = p_blk_lo; // C4
330 resource_template_register_systemio[12] = p_blk_hi; // C4
331 aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
332 aml_add_byte(tmpl, 0x04); // C4
333 aml_add_word(tmpl, 0x0080); // Latency
334 aml_add_dword(tmpl, 0x000000C8); // Power
335 }
336 else if (c3_enabled) // C3
337 {
338 p_blk_lo = acpi_cpu_p_blk + 5;
339 p_blk_hi = (acpi_cpu_p_blk + 5) >> 8;
340
341 tmpl = aml_add_package(pack);
342 resource_template_register_systemio[11] = p_blk_lo; // C3
343 resource_template_register_systemio[12] = p_blk_hi; // C3
344 aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
345 aml_add_byte(tmpl, 0x03); // C3
346 aml_add_word(tmpl, 0x0060); // Latency
347 aml_add_dword(tmpl, 0x0000015e); // Power
348 }
349
350 }
351 else
352 {
353 // C1
354 resource_template_register_fixedhw[11] = 0x00; // C1
355 aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
356 aml_add_byte(tmpl, 0x01); // C1
357 aml_add_word(tmpl, 0x0001); // Latency
358 aml_add_dword(tmpl, 0x000003e8); // Power
359
360 resource_template_register_fixedhw[18] = 0x03;
361
362 if (c2_enabled) // C2
363 {
364 tmpl = aml_add_package(pack);
365 resource_template_register_fixedhw[11] = 0x10; // C2
366 aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
367 aml_add_byte(tmpl, 0x02); // C2
368 aml_add_word(tmpl, 0x0040); // Latency
369 aml_add_dword(tmpl, 0x000001f4); // Power
370 }
371
372 if (c4_enabled) // C4
373 {
374 tmpl = aml_add_package(pack);
375 resource_template_register_fixedhw[11] = 0x30; // C4
376 aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
377 aml_add_byte(tmpl, 0x04); // C4
378 aml_add_word(tmpl, 0x0080); // Latency
379 aml_add_dword(tmpl, 0x000000C8); // Power
380 }
381 else if (c3_enabled)
382 {
383 tmpl = aml_add_package(pack);
384 resource_template_register_fixedhw[11] = 0x20; // C3
385 aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
386 aml_add_byte(tmpl, 0x03); // C3
387 aml_add_word(tmpl, 0x0060); // Latency
388 aml_add_dword(tmpl, 0x0000015e); // Power
389 }
390 }
391
392
393// Aliaces
394int i;
395for (i = 0; i < acpi_cpu_count; i++)
396{
397char name[9];
398sprintf(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]);
399
400scop = aml_add_scope(root, name);
401aml_add_alias(scop, "CST_", "_CST");
402}
403
404aml_calculate_size(root);
405
406struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
407
408aml_write_node(root, (void*)ssdt, 0);
409
410ssdt->Length = root->Size;
411ssdt->Checksum = 0;
412ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
413
414aml_destroy_node(root);
415
416//dumpPhysAddr("C-States SSDT content: ", ssdt, ssdt->Length);
417
418verbose ("SSDT with CPU C-States generated successfully\n");
419
420return ssdt;
421}
422else
423{
424verbose ("ACPI CPUs not found: C-States not generated !!!\n");
425}
426
427return NULL;
428}
429
430struct acpi_2_ssdt *generate_pss_ssdt(struct acpi_2_dsdt* dsdt)
431{
432char ssdt_header[] =
433{
4340x53, 0x53, 0x44, 0x54, 0x7E, 0x00, 0x00, 0x00, /* SSDT.... */
4350x01, 0x6A, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x00, /* ..PmRef. */
4360x43, 0x70, 0x75, 0x50, 0x6D, 0x00, 0x00, 0x00, /* CpuPm... */
4370x00, 0x30, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* .0..INTL */
4380x31, 0x03, 0x10, 0x20,/* 1.._*/
439};
440//cpuid_update_generic_info();
441if (Platform->CPU.Vendor != 0x756E6547) {
442verbose ("Not an Intel platform: P-States will not be generated !!!\n");
443return NULL;
444}
445
446if (!(Platform->CPU.Features & CPU_FEATURE_MSR)) {
447verbose ("Unsupported CPU: P-States will not be generated !!!\n");
448return NULL;
449}
450
451if (acpi_cpu_count == 0)
452get_acpi_cpu_names((void*)dsdt, dsdt->Length);
453
454if (acpi_cpu_count > 0)
455{
456struct p_state initial, maximum, minimum, p_states[32];
457uint8_t p_states_count = 0;
458
459// Retrieving P-States, ported from code by superhai (c)
460switch (Platform->CPU.Family) {
461case 0x06:
462{
463switch (Platform->CPU.Model)
464{
465case CPU_MODEL_PENTIUM_M:// ???
466case CPU_MODEL_YONAH:// Intel Mobile Core Solo, Duo
467case CPU_MODEL_MEROM:// Intel Mobile Core 2 Solo, Duo, Xeon 30xx, Xeon 51xx, Xeon X53xx, Xeon E53xx, Xeon X32xx
468case CPU_MODEL_PENRYN:// Intel Core 2 Solo, Duo, Quad, Extreme, Xeon X54xx, Xeon X33xx
469case CPU_MODEL_ATOM:// Intel Atom (45nm)
470{
471bool cpu_dynamic_fsb = false;
472
473if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27))
474{
475wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28)));
476delay(1);
477cpu_dynamic_fsb = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);
478}
479
480bool cpu_noninteger_bus_ratio = (rdmsr64(MSR_IA32_PERF_STATUS) & (1ULL << 46));
481
482initial.Control = rdmsr64(MSR_IA32_PERF_STATUS);
483
484maximum.Control = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 32) & 0x1F3F) | (0x4000 * cpu_noninteger_bus_ratio);
485maximum.CID = ((maximum.FID & 0x1F) << 1) | cpu_noninteger_bus_ratio;
486
487minimum.FID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 24) & 0x1F) | (0x80 * cpu_dynamic_fsb);
488minimum.VID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 48) & 0x3F);
489
490if (minimum.FID == 0)
491{
492uint64_t msr;
493uint8_t i;
494// Probe for lowest fid
495for (i = maximum.FID; i >= 0x6; i--)
496{
497msr = rdmsr64(MSR_IA32_PERF_CONTROL);
498wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (i << 8) | minimum.VID);
499intel_waitforsts();
500minimum.FID = (rdmsr64(MSR_IA32_PERF_STATUS) >> 8) & 0x1F;
501delay(1);
502}
503
504msr = rdmsr64(MSR_IA32_PERF_CONTROL);
505wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
506intel_waitforsts();
507}
508
509if (minimum.VID == maximum.VID)
510{
511uint64_t msr;
512uint8_t i;
513// Probe for lowest vid
514for (i = maximum.VID; i > 0xA; i--)
515{
516msr = rdmsr64(MSR_IA32_PERF_CONTROL);
517wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (minimum.FID << 8) | i);
518intel_waitforsts();
519minimum.VID = rdmsr64(MSR_IA32_PERF_STATUS) & 0x3F;
520delay(1);
521}
522
523msr = rdmsr64(MSR_IA32_PERF_CONTROL);
524wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
525intel_waitforsts();
526}
527
528minimum.CID = ((minimum.FID & 0x1F) << 1) >> cpu_dynamic_fsb;
529
530// Sanity check
531if (maximum.CID < minimum.CID)
532{
533DBG("Insane FID values!");
534p_states_count = 0;
535}
536else
537{
538// Finalize P-States
539// Find how many P-States machine supports
540p_states_count = maximum.CID - minimum.CID + 1;
541
542if (p_states_count > 32)
543p_states_count = 32;
544
545uint8_t vidstep;
546uint8_t i = 0, u, invalid = 0;
547
548vidstep = ((maximum.VID << 2) - (minimum.VID << 2)) / (p_states_count - 1);
549
550for (u = 0; u < p_states_count; u++)
551{
552i = u - invalid;
553
554p_states[i].CID = maximum.CID - u;
555p_states[i].FID = (p_states[i].CID >> 1);
556
557if (p_states[i].FID < 0x6)
558{
559if (cpu_dynamic_fsb)
560p_states[i].FID = (p_states[i].FID << 1) | 0x80;
561}
562else if (cpu_noninteger_bus_ratio)
563{
564p_states[i].FID = p_states[i].FID | (0x40 * (p_states[i].CID & 0x1));
565}
566
567if (i && p_states[i].FID == p_states[i-1].FID)
568invalid++;
569
570p_states[i].VID = ((maximum.VID << 2) - (vidstep * u)) >> 2;
571
572uint32_t multiplier = p_states[i].FID & 0x1f;// = 0x08
573bool half = p_states[i].FID & 0x40;// = 0x01
574bool dfsb = p_states[i].FID & 0x80;// = 0x00
575uint32_t fsb = Platform->CPU.FSBFrequency / 1000000; // = 400
576uint32_t halffsb = (fsb + 1) >> 1;// = 200
577uint32_t frequency = (multiplier * fsb);// = 3200
578
579p_states[i].Frequency = (frequency + (half * halffsb)) >> dfsb;// = 3200 + 200 = 3400
580}
581
582p_states_count -= invalid;
583}
584
585break;
586}
587case CPU_MODEL_FIELDS:// Intel Core i5, i7, Xeon X34xx LGA1156 (45nm)
588case CPU_MODEL_DALES:
589case CPU_MODEL_DALES_32NM:// Intel Core i3, i5 LGA1156 (32nm)
590case CPU_MODEL_NEHALEM:// Intel Core i7, Xeon W35xx, Xeon X55xx, Xeon E55xx LGA1366 (45nm)
591case CPU_MODEL_NEHALEM_EX:// Intel Xeon X75xx, Xeon X65xx, Xeon E75xx, Xeon E65x
592case CPU_MODEL_WESTMERE:// Intel Core i7, Xeon X56xx, Xeon E56xx, Xeon W36xx LGA1366 (32nm) 6 Core
593case CPU_MODEL_WESTMERE_EX:// Intel Xeon E7
594 case CPU_MODEL_SANDY:// Intel Core i3, i5, i7 LGA1155 (32nm)
595 case CPU_MODEL_SANDY_XEON:// Intel Xeon E3
596{
597maximum.Control = rdmsr64(MSR_IA32_PERF_STATUS) & 0xff; // Seems it always contains maximum multiplier value (with turbo, that's we need)...
598minimum.Control = (rdmsr64(MSR_PLATFORM_INFO) >> 40) & 0xff;
599
600verbose("P-States: min 0x%x, max 0x%x\n", minimum.Control, maximum.Control);
601
602// Sanity check
603if (maximum.Control < minimum.Control)
604{
605DBG("Insane control values!");
606p_states_count = 0;
607}
608else
609{
610uint8_t i;
611p_states_count = 0;
612
613for (i = maximum.Control; i >= minimum.Control; i--)
614{
615p_states[p_states_count].Control = i;
616p_states[p_states_count].CID = p_states[p_states_count].Control << 1;
617p_states[p_states_count].Frequency = (Platform->CPU.FSBFrequency / 1000000) * i;
618p_states_count++;
619}
620}
621
622break;
623}
624default:
625verbose ("Unsupported CPU (0x%X): P-States not generated !!!\n", Platform->CPU.Family);
626break;
627}
628}
629}
630
631// Generating SSDT
632if (p_states_count > 0)
633{
634int i;
635
636struct aml_chunk* root = aml_create_node(NULL);
637aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
638struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
639struct aml_chunk* name = aml_add_name(scop, "PSS_");
640struct aml_chunk* pack = aml_add_package(name);
641
642for (i = 0; i < p_states_count; i++)
643{
644struct aml_chunk* pstt = aml_add_package(pack);
645
646aml_add_dword(pstt, p_states[i].Frequency);
647aml_add_dword(pstt, 0x00000000); // Power
648aml_add_dword(pstt, 0x0000000A); // Latency
649aml_add_dword(pstt, 0x0000000A); // Latency
650aml_add_dword(pstt, p_states[i].Control);
651aml_add_dword(pstt, i+1); // Status
652}
653
654// Add aliaces
655for (i = 0; i < acpi_cpu_count; i++)
656{
657char name[9];
658sprintf(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]);
659
660scop = aml_add_scope(root, name);
661aml_add_alias(scop, "PSS_", "_PSS");
662}
663
664aml_calculate_size(root);
665
666struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
667
668aml_write_node(root, (void*)ssdt, 0);
669
670ssdt->Length = root->Size;
671ssdt->Checksum = 0;
672ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
673
674aml_destroy_node(root);
675
676//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);
677
678verbose ("SSDT with CPU P-States generated successfully\n");
679
680return ssdt;
681}
682}
683else
684{
685verbose ("ACPI CPUs not found: P-States not generated !!!\n");
686}
687
688return NULL;
689}
690
691struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)
692{
693extern void setupSystemType();
694
695struct acpi_2_fadt *fadt_mod = NULL;
696bool fadt_rev2_needed = false;
697bool fix_restart_acpi = false;
698const char * value;
699
700// Restart Fix
701//if (Platform->CPU.Vendor == 0x756E6547) {/* Intel */
702fix_restart = true;
703value = getStringForKey(kRestartFix, &bootInfo->chameleonConfig);
704if (value[0] == 'A') {
705fix_restart_acpi = true;
706} else if (value[0] == 'P') {
707fix_restart_acpi = false;
708} else {
709fix_restart = false;
710}
711/*
712} else {
713DBG ("Not an Intel platform: Restart Fix not applied !!!\n");
714fix_restart = false;
715}
716*/
717if (fix_restart) fadt_rev2_needed = true;
718
719// Allocate new fadt table
720if (fadt->Length < 0x84 && fadt_rev2_needed)
721{
722fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
723memcpy(fadt_mod, fadt, fadt->Length);
724fadt_mod->Length = 0x84;
725fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
726}
727else
728{
729fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
730memcpy(fadt_mod, fadt, fadt->Length);
731}
732// Determine system type / PM_Model
733if (fadt_mod && Platform->Type) {
734fadt_mod->PM_Profile = Platform->Type;
735} else if(fadt)
736Platform->Type = fadt->PM_Profile;
737else
738Platform->Type = 1;
739
740//User override default value
741if ( (value=getStringForKey(kSystemType, &bootInfo->chameleonConfig))!=NULL)
742{
743if (Platform->Type > 6)
744{
745if(fadt_mod->PM_Profile<=6)
746Platform->Type = fadt_mod->PM_Profile; // get the fadt if correct
747else
748Platform->Type = 1;/* Set a fixed value (Desktop) */
749verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform->Type);
750}
751else
752Platform->Type = (unsigned char) strtoul(value, NULL, 10);
753}
754// Set PM_Profile from System-type if only user wanted this value to be forced
755if (fadt_mod->PM_Profile != Platform->Type)
756{
757 if (value)
758{ // user has overriden the SystemType so take care of it in FACP
759DBG("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform->Type);
760fadt_mod->PM_Profile = Platform->Type;
761 }
762 else
763 { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
764Platform->Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
765 }
766}
767// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
768// because we need to take care of facp original content, if it is correct.
769setupSystemType();
770
771// Patch FADT to fix restart
772if (fix_restart)
773{
774if (fix_restart_acpi) {
775fadt_mod->Flags|= 0x400;
776fadt_mod->Reset_SpaceID= 0x01; // System I/O
777fadt_mod->Reset_BitWidth= 0x08; // 1 byte
778fadt_mod->Reset_BitOffset= 0x00; // Offset 0
779fadt_mod->Reset_AccessWidth= 0x01; // Byte access
780fadt_mod->Reset_Address= 0x0cf9; // Address of the register
781fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
782msglog("FADT: ACPI Restart Fix applied!\n");
783} else {
784fadt_mod->Flags|= 0x400;
785fadt_mod->Reset_SpaceID= 0x01; // System I/O
786fadt_mod->Reset_BitWidth= 0x08; // 1 byte
787fadt_mod->Reset_BitOffset= 0x00; // Offset 0
788fadt_mod->Reset_AccessWidth= 0x01; // Byte access
789fadt_mod->Reset_Address= 0x64; // Address of the register
790fadt_mod->Reset_Value= 0xfe; // Value to write to reset the system
791msglog("FADT: PS2 Restart Fix applied!\n");
792}
793}
794
795// Patch DSDT Address if we have loaded DSDT.aml
796if(new_dsdt)
797{
798DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
799
800fadt_mod->DSDT=(uint32_t)new_dsdt;
801if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
802fadt_mod->X_DSDT=(uint32_t)new_dsdt;
803
804DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
805
806DBG("FADT: Using custom DSDT!\n");
807}
808
809// Correct the checksum
810fadt_mod->Checksum=0;
811fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
812
813return fadt_mod;
814}
815
816/* Setup ACPI without replacing DSDT. */
817int setupAcpiNoMod()
818{
819//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
820//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
821/* XXX aserebln why uint32 cast if pointer is uint64 ? */
822//Slice (uint64_t)
823acpi10_p = (uint64_t)(uint32_t)getAddressOfAcpiTable();
824acpi20_p = (uint64_t)(uint32_t)getAddressOfAcpi20Table();
825addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
826if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
827else {
828DBG("no ACPI 2\n");
829}
830return 1;
831}
832
833static struct acpi_2_xsdt* createNewXSDTfromRSDT(struct acpi_2_rsdt * rsdt)
834{
835struct acpi_2_xsdt *xsdt;
836int xsdt_entries_num, i;
837uint32_t xsdt_len;
838if (!rsdt) {
839return NULL;
840}
841//Create XSDT in addition to RSDT as a 64bit copy
842if((rsdt->Length<0) || (rsdt->Length>1000)){
843DBG(" incorrect RSDT length: %08lx\n", rsdt->Length);
844#if DEBUG_DSDT
845getc();
846#endif
847return NULL;
848}
849xsdt_entries_num = (rsdt->Length - sizeof(struct acpi_2_rsdt)) >> 2;
850xsdt_len = sizeof(struct acpi_2_rsdt) + (xsdt_entries_num << 3);
851xsdt = (struct acpi_2_xsdt*)AllocateKernelMemory(xsdt_len);
852bcopy(rsdt, xsdt, sizeof(struct acpi_2_rsdt));
853//Change signature from RSDT to XSDT
854strncpy(xsdt->Signature, FourChar("XSDT"), 4);
855//Now copy address table
856uint64_t *table1 = (uint64_t *)(xsdt+1);
857uint32_t *table2 = (uint32_t *)(rsdt+1);
858if ((xsdt_entries_num>0) && (xsdt_entries_num<20)) {
859for(i=0; i<xsdt_entries_num; i++){
860table1[i] = (uint64_t)table2[i];
861}
862} else {
863DBG("RSDT entries = %d xsdtAddr = %08lx rsdtAdrr = %08lx\n",
864xsdt_entries_num, (uint32_t)xsdt, (uint32_t)rsdt);
865}
866xsdt->Length = xsdt_len;
867//Correct checksums
868xsdt->Checksum=0;
869xsdt->Checksum=256-checksum8(xsdt,xsdt_len);
870return xsdt;
871}
872
873static struct acpi_2_rsdp* createNewACPI20(struct acpi_2_rsdp * old_rsdp)
874{
875if(!old_rsdp) return NULL;
876//Slice I want to copy acpi10 table to create ACPI20
877//uint64_t *xsdt_entries;
878if(*(uint64_t *)old_rsdp != ACPI_SIGNATURE_UINT64_LE){
879DBG(" wrong signature %08lx\n", (*(uint64_t *)old_rsdp));
880return NULL;
881}
882DBG(" old_rsdp = %08lx\n", (uint32_t)old_rsdp);
883//if(((int)old_rsdp->Length < 0) || ((int)old_rsdp->Length > 84))
884//return NULL;
885//No! ACPI10_rsdp_length=20 and no such field old_rsdp->Length
886
887struct acpi_2_rsdp * rsdp=(struct acpi_2_rsdp *) AllocateKernelMemory(36);
888DBG(" new_rsdp = %08lx\n", (uint32_t)rsdp);
889bzero(rsdp, 36);
890bcopy(old_rsdp, rsdp, 20);
891strncpy(rsdp->OEMID, "Apple ", 6);
892rsdp->Revision = 2;
893rsdp->Length = 36;
894struct acpi_2_rsdt *rsdt = (struct acpi_2_rsdt *)(rsdp->RsdtAddress); //copied from old
895//dumpRSDT(rsdt, 0);
896rsdp->XsdtAddress = (uint64_t)(uint32_t)createNewXSDTfromRSDT(rsdt);
897//dumpRSDT((struct acpi_2_rsdt *)(uint32_t)(rsdp->XsdtAddress), 0);
898rsdp->Checksum=0;
899rsdp->Checksum=256-checksum8(rsdp,20);
900rsdp->ExtendedChecksum=0;
901rsdp->ExtendedChecksum=256-checksum8(rsdp,36);
902return rsdp;
903}
904
905
906/* Setup ACPI. Replace DSDT if DSDT.aml is found */
907int setupAcpi(void)
908{
909int version;
910//void *new_dsdt;
911
912const char *filename;
913char dirSpec[128];
914int len = 0, old_dsdt_len = 0;
915struct acpi_2_rsdp *rsdp, *rsdp_mod, *rsdp1 = 0;
916struct acpi_2_rsdt *rsdt, *rsdt_mod = 0;
917struct acpi_2_rsdt *xsdt; //, *xsdt_mod = 0;
918char* old_dsdt = 0;
919
920// Try using the file specified with the DSDT option
921if (getValueForKey(kDSDT, &filename, &len, &bootInfo->chameleonConfig))
922{
923sprintf(dirSpec, filename);
924}
925else
926{
927sprintf(dirSpec, "DSDT.aml");
928}
929
930// Load replacement DSDT
931new_dsdt = loadACPITable(dirSpec);
932// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
933/*if (!new_dsdt)
934 {
935 return setupAcpiNoMod();
936 }*/
937
938// Mozodojo: Load additional SSDTs
939struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
940int ssdt_count=0;
941
942// SSDT Options
943bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
944
945getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->chameleonConfig);
946getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->chameleonConfig);
947getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->chameleonConfig);
948
949{
950int i;
951
952for (i=0; i<30; i++)
953{
954char filename[512];
955
956sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
957
958if(new_ssdt[ssdt_count] = loadACPITable(filename))
959{
960ssdt_count++;
961}
962/*else
963{
964break;
965}*/
966}
967}
968
969// Do the same procedure for both versions of ACPI
970for (version=0; version<2; version++) {
971//int rsdplength;
972//Here version=0 for ACPI 1.0 and version=1 for ACPI 2.0
973// Find original rsdp
974rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
975//Slice
976if (!rsdp)
977{
978if (version){
979DBG("No ACPI version 2 found. Creating...\n");
980rsdp=createNewACPI20(rsdp1);
981if(!rsdp){
982addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
983continue;
984}
985} else {
986DBG("No ACPI version 1 found. Ignoring\n");
987addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
988continue;
989}
990}
991if(!version) rsdp1 = rsdp; //save to create new.
992rsdplength=version?rsdp->Length:20;
993
994DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
995
996/* FIXME: no check that memory allocation succeeded
997 * Copy and patch RSDP,RSDT, XSDT and FADT
998 * For more info see ACPI Specification pages 110 and following
999 */
1000
1001rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
1002memcpy(rsdp_mod, rsdp, rsdplength);
1003rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
1004xsdt=(struct acpi_2_rsdt *)(uint32_t)(rsdp->XsdtAddress);
1005DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
1006
1007//if we have RSDT table
1008if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
1009{
1010uint32_t *rsdt_entries;
1011int rsdt_entries_num;
1012int dropoffset=0, i;
1013
1014// mozo: using malloc cos I didn't found how to free already allocated kernel memory
1015rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
1016memcpy (rsdt_mod, rsdt, rsdt->Length);
1017rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
1018rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
1019rsdt_entries=(uint32_t *)(rsdt_mod+1);
1020for (i=0;i<rsdt_entries_num;i++)
1021{
1022char *table=(char *)(rsdt_entries[i]);
1023if (!table)
1024continue;
1025
1026DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
1027
1028rsdt_entries[i-dropoffset]=rsdt_entries[i];
1029
1030if (drop_ssdt && tableSign(table, "SSDT"))
1031{
1032dropoffset++;
1033continue;
1034}
1035if (tableSign(table, "DSDT"))
1036{
1037DBG("DSDT found\n");
1038
1039if(new_dsdt)
1040rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
1041
1042continue;
1043} else {
1044//Slice - Correct header to MacModel & Vendor, correct chechsum
1045struct acpi_2_rsdt *t = (struct acpi_2_rsdt *)table;
1046strncpy(t->OEMID, "Apple ", 6);
1047strncpy(t->OEMTableId, MacModel, 8);
1048t->OEMRevision = ModelRev;
1049t->Checksum=0;
1050t->Checksum=256-checksum8(t,t->Length);
1051
1052}
1053if (tableSign(table, "FACP"))
1054{
1055struct acpi_2_fadt *fadt, *fadt_mod;
1056fadt=(struct acpi_2_fadt *)rsdt_entries[i];
1057
1058DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
1059
1060if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
1061{
1062printf("FADT incorrect. Not modified\n");
1063continue;
1064}
1065
1066if(version && fix_restart){ //ACPI 2.0
1067fadt_mod = patch_fadt(fadt, new_dsdt);
1068rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1069} else {
1070if (fadt && Platform->Type)
1071fadt->PM_Profile = Platform->Type;//For ACPI1.0 the only patch
1072fadt_mod = fadt;
1073rsdt_entries[i-dropoffset]=(uint32_t)fadt;
1074}
1075DBG("FADT_mod table sign: %s\n", fadt_mod->Signature);
1076//Slice
1077//Now I want to replace DSDT in place
1078// it is only way to patch DSDT on some platform
1079old_dsdt = (char *)fadt->DSDT;
1080if(!old_dsdt || !tableSign(old_dsdt, "DSDT")){
1081if(fadt_mod->Length > 140) old_dsdt = (char *)(uint32_t)fadt_mod->X_DSDT;
1082if(!old_dsdt || !tableSign(old_dsdt, "DSDT")) old_dsdt = (char *)new_dsdt;
1083}
1084DBG("New DSDT @%x, X_DSDT %x after FADT patch\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
1085
1086if(tableSign(old_dsdt, "DSDT"))
1087old_dsdt_len = ((struct acpi_2_rsdt *)old_dsdt)->Length;
1088int new_dsdt_len = ((struct acpi_2_rsdt *)new_dsdt)->Length;
1089if(new_dsdt_len > old_dsdt_len) {
1090old_dsdt = (char *)new_dsdt;
1091DBG(" New DSDT is longer then old\n");
1092len = 0;
1093}
1094else len = new_dsdt_len;
1095
1096
1097// Generate _CST SSDT
1098if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1099{
1100generate_cstates = false; // Generate SSDT only once!
1101ssdt_count++;
1102}
1103
1104// Generating _PSS SSDT
1105if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1106{
1107generate_pstates = false; // Generate SSDT only once!
1108ssdt_count++;
1109}
1110
1111continue;
1112}
1113}//for rsdt entries
1114DBG("\n");
1115//Slice - Correct header to MacModel & Vendor, correct chechsum
1116strncpy(rsdt_mod->OEMID, "Apple ", 6);
1117strncpy(rsdt_mod->OEMTableId, MacModel, 8);
1118rsdt_mod->OEMRevision = ModelRev;
1119
1120// Allocate rsdt in Kernel memory area
1121rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
1122struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
1123memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
1124free(rsdt_mod); rsdt_mod = rsdt_copy;
1125rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
1126rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
1127rsdt_entries=(uint32_t *)(rsdt_mod+1);
1128
1129// Mozodojo: Insert additional SSDTs into RSDT
1130if(ssdt_count>0)
1131{
1132int j;
1133
1134for (j=0; j<ssdt_count; j++)
1135rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1136
1137DBG("RSDT: Added %d SSDT table(s)\n", ssdt_count);
1138}
1139
1140// Correct the checksum of RSDT
1141//DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
1142
1143rsdt_mod->Checksum=0;
1144rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
1145
1146//DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
1147}
1148else
1149{
1150rsdp_mod->RsdtAddress=0;
1151verbose("RSDT not found or RSDT incorrect\n");
1152}
1153
1154if (version)
1155{
1156struct acpi_2_xsdt *xsdt, *xsdt_mod;
1157
1158// FIXME: handle 64-bit address correctly
1159
1160xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
1161DBG("XSDT @%x;%x, Length=%d Sign=%c%c%c%c\n", (uint32_t)(rsdp->XsdtAddress>>32),
1162(uint32_t)rsdp->XsdtAddress, xsdt->Length, xsdt[0], xsdt[1], xsdt[2], xsdt[3]);
1163if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
1164{
1165uint64_t *xsdt_entries;
1166int xsdt_entries_num, i;
1167int dropoffset=0;
1168
1169// mozo: using malloc cos I didn't found how to free already allocated kernel memory
1170xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
1171memcpy(xsdt_mod, xsdt, xsdt->Length);
1172rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1173xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1174xsdt_entries=(uint64_t *)(xsdt_mod+1);
1175for (i=0;i<xsdt_entries_num;i++)
1176{
1177char *table=(char *)((uint32_t)(xsdt_entries[i]));
1178if (!table)
1179continue;
1180
1181xsdt_entries[i-dropoffset]=xsdt_entries[i];
1182
1183if (drop_ssdt && tableSign(table, "SSDT"))
1184{
1185dropoffset++;
1186continue;
1187}
1188if (tableSign(table, "DSDT"))
1189{
1190DBG("DSDT found\n");
1191
1192if (new_dsdt)
1193xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
1194
1195DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);
1196
1197continue;
1198}
1199if (tableSign(table, "FACP"))
1200{
1201struct acpi_2_fadt *fadt, *fadt_mod;
1202fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
1203
1204DBG("FADT found @%x%x, Length %d\n", (uint32_t)(xsdt_entries[i]>>32),fadt,
1205fadt->Length);
1206
1207if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
1208{
1209DBG("FADT incorrect or after 4GB. Dropping XSDT\n");
1210goto drop_xsdt;
1211}
1212
1213fadt_mod = patch_fadt(fadt, new_dsdt);
1214xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1215
1216DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);
1217
1218// Generate _CST SSDT
1219if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1220{
1221generate_cstates = false; // Generate SSDT only once!
1222ssdt_count++;
1223}
1224
1225// Generating _PSS SSDT
1226if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1227{
1228generate_pstates = false; // Generate SSDT only once!
1229ssdt_count++;
1230}
1231
1232continue;
1233}
1234
1235DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);
1236
1237}
1238
1239// Allocate xsdt in Kernel memory area
1240xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
1241struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
1242memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
1243free(xsdt_mod); xsdt_mod = xsdt_copy;
1244rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1245xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1246xsdt_entries=(uint64_t *)(xsdt_mod+1);
1247
1248// Mozodojo: Insert additional SSDTs into XSDT
1249if(ssdt_count>0)
1250{
1251int j;
1252
1253for (j=0; j<ssdt_count; j++)
1254xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1255
1256DBG("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1257}
1258
1259// Correct the checksum of XSDT
1260xsdt_mod->Checksum=0;
1261xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1262}
1263else
1264{
1265drop_xsdt:
1266
1267DBG("About to drop XSDT\n");
1268
1269/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1270 * A Better strategy would be to generate
1271 */
1272//Slice - it is possible only for ACPI20
1273if (version) {
1274if(rsdp->RsdtAddress){
1275rsdp_mod->XsdtAddress =
1276(uint64_t)(uint32_t)createNewXSDTfromRSDT((struct acpi_2_rsdt*)rsdp->RsdtAddress);
1277continue;
1278}
1279else
1280DBG(" no sample to create XSDT, dropping\n");
1281}
1282
1283rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1284DBG("XSDT not found or XSDT incorrect\n");
1285}
1286}
1287//Slice - correct DSDT in place
1288if(len && old_dsdt && new_dsdt && (old_dsdt != new_dsdt)){
1289DBG("old_dsdt=%08x new_dsdt=%08x new_len=%d\n", (unsigned int)old_dsdt, (unsigned int)new_dsdt, len);
1290bcopy(new_dsdt, old_dsdt, len);
1291}
1292
1293// Correct the checksum of RSDP
1294
1295DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1296
1297rsdp_mod->Checksum=0;
1298rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1299
1300DBG("New checksum %d\n", rsdp_mod->Checksum);
1301
1302if (version)
1303{
1304DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1305
1306rsdp_mod->ExtendedChecksum=0;
1307rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1308
1309DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1310
1311}
1312
1313//verbose("Patched ACPI version %d DSDT\n", version+1);
1314if (version)
1315{
1316/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1317//Slice because rsdp_mod is a pointer (32bit in i386) to a structure uint64_t*
1318acpi20_p = (uint64_t)(uint32_t)rsdp_mod;
1319addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1320}
1321else
1322{
1323/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1324acpi10_p = (uint64_t)(uint32_t)rsdp_mod;
1325addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1326}
1327}
1328#if DEBUG_ACPI
1329printf("Press a key to continue... (DEBUG_ACPI)\n");
1330getchar();
1331#endif
1332return 1;
1333}
1334

Archive Download this file

Revision: 1185