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;
698const char * value;
699
700// Restart Fix
701if (Platform->CPU.Vendor == 0x756E6547) {/* Intel */
702fix_restart = true;
703getBoolForKey(kRestartFix, &fix_restart, &bootInfo->chameleonConfig);
704} else {
705DBG ("Not an Intel platform: Restart Fix not applied !!!\n");
706fix_restart = false;
707}
708
709if (fix_restart) fadt_rev2_needed = true;
710
711// Allocate new fadt table
712if (fadt->Length < 0x84 && fadt_rev2_needed)
713{
714fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
715memcpy(fadt_mod, fadt, fadt->Length);
716fadt_mod->Length = 0x84;
717fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
718}
719else
720{
721fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
722memcpy(fadt_mod, fadt, fadt->Length);
723}
724// Determine system type / PM_Model
725if (fadt_mod && Platform->Type) {
726fadt_mod->PM_Profile = Platform->Type;
727} else if(fadt)
728Platform->Type = fadt->PM_Profile;
729else
730Platform->Type = 1;
731
732//User override default value
733if ( (value=getStringForKey(kSystemType, &bootInfo->chameleonConfig))!=NULL)
734{
735if (Platform->Type > 6)
736{
737if(fadt_mod->PM_Profile<=6)
738Platform->Type = fadt_mod->PM_Profile; // get the fadt if correct
739else
740Platform->Type = 1;/* Set a fixed value (Desktop) */
741verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform->Type);
742}
743else
744Platform->Type = (unsigned char) strtoul(value, NULL, 10);
745}
746// Set PM_Profile from System-type if only user wanted this value to be forced
747if (fadt_mod->PM_Profile != Platform->Type)
748{
749 if (value)
750{ // user has overriden the SystemType so take care of it in FACP
751DBG("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform->Type);
752fadt_mod->PM_Profile = Platform->Type;
753 }
754 else
755 { // PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
756Platform->Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
757 }
758}
759// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
760// because we need to take care of facp original content, if it is correct.
761setupSystemType();
762
763// Patch FADT to fix restart
764if (fix_restart)
765{
766fadt_mod->Flags|= 0x400;
767fadt_mod->Reset_SpaceID= 0x01; // System I/O
768fadt_mod->Reset_BitWidth= 0x08; // 1 byte
769fadt_mod->Reset_BitOffset= 0x00; // Offset 0
770fadt_mod->Reset_AccessWidth= 0x01; // Byte access
771fadt_mod->Reset_Address= 0x0cf9; // Address of the register
772fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
773msglog("FADT: Restart Fix applied!\n");
774}
775
776// Patch DSDT Address if we have loaded DSDT.aml
777if(new_dsdt)
778{
779DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
780
781fadt_mod->DSDT=(uint32_t)new_dsdt;
782if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
783fadt_mod->X_DSDT=(uint32_t)new_dsdt;
784
785DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
786
787DBG("FADT: Using custom DSDT!\n");
788}
789
790// Correct the checksum
791fadt_mod->Checksum=0;
792fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
793
794return fadt_mod;
795}
796
797/* Setup ACPI without replacing DSDT. */
798int setupAcpiNoMod()
799{
800//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
801//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
802/* XXX aserebln why uint32 cast if pointer is uint64 ? */
803//Slice (uint64_t)
804acpi10_p = (uint64_t)(uint32_t)getAddressOfAcpiTable();
805acpi20_p = (uint64_t)(uint32_t)getAddressOfAcpi20Table();
806addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
807if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
808else {
809DBG("no ACPI 2\n");
810}
811return 1;
812}
813
814static struct acpi_2_xsdt* createNewXSDTfromRSDT(struct acpi_2_rsdt * rsdt)
815{
816struct acpi_2_xsdt *xsdt;
817int xsdt_entries_num, i;
818uint32_t xsdt_len;
819if (!rsdt) {
820return NULL;
821}
822//Create XSDT in addition to RSDT as a 64bit copy
823if((rsdt->Length<0) || (rsdt->Length>1000)){
824DBG(" incorrect RSDT length: %08lx\n", rsdt->Length);
825#if DEBUG_DSDT
826getc();
827#endif
828return NULL;
829}
830xsdt_entries_num = (rsdt->Length - sizeof(struct acpi_2_rsdt)) >> 2;
831xsdt_len = sizeof(struct acpi_2_rsdt) + (xsdt_entries_num << 3);
832xsdt = (struct acpi_2_xsdt*)AllocateKernelMemory(xsdt_len);
833bcopy(rsdt, xsdt, sizeof(struct acpi_2_rsdt));
834//Change signature from RSDT to XSDT
835strncpy(xsdt->Signature, FourChar("XSDT"), 4);
836//Now copy address table
837uint64_t *table1 = (uint64_t *)(xsdt+1);
838uint32_t *table2 = (uint32_t *)(rsdt+1);
839if ((xsdt_entries_num>0) && (xsdt_entries_num<20)) {
840for(i=0; i<xsdt_entries_num; i++){
841table1[i] = (uint64_t)table2[i];
842}
843} else {
844DBG("RSDT entries = %d xsdtAddr = %08lx rsdtAdrr = %08lx\n",
845xsdt_entries_num, (uint32_t)xsdt, (uint32_t)rsdt);
846}
847xsdt->Length = xsdt_len;
848//Correct checksums
849xsdt->Checksum=0;
850xsdt->Checksum=256-checksum8(xsdt,xsdt_len);
851return xsdt;
852}
853
854static struct acpi_2_rsdp* createNewACPI20(struct acpi_2_rsdp * old_rsdp)
855{
856if(!old_rsdp) return NULL;
857//Slice I want to copy acpi10 table to create ACPI20
858//uint64_t *xsdt_entries;
859if(*(uint64_t *)old_rsdp != ACPI_SIGNATURE_UINT64_LE){
860DBG(" wrong signature %08lx\n", (*(uint64_t *)old_rsdp));
861return NULL;
862}
863DBG(" old_rsdp = %08lx\n", (uint32_t)old_rsdp);
864//if(((int)old_rsdp->Length < 0) || ((int)old_rsdp->Length > 84))
865//return NULL;
866//No! ACPI10_rsdp_length=20 and no such field old_rsdp->Length
867
868struct acpi_2_rsdp * rsdp=(struct acpi_2_rsdp *) AllocateKernelMemory(36);
869DBG(" new_rsdp = %08lx\n", (uint32_t)rsdp);
870bzero(rsdp, 36);
871bcopy(old_rsdp, rsdp, 20);
872strncpy(rsdp->OEMID, "Apple ", 6);
873rsdp->Revision = 2;
874rsdp->Length = 36;
875struct acpi_2_rsdt *rsdt = (struct acpi_2_rsdt *)(rsdp->RsdtAddress); //copied from old
876//dumpRSDT(rsdt, 0);
877rsdp->XsdtAddress = (uint64_t)(uint32_t)createNewXSDTfromRSDT(rsdt);
878//dumpRSDT((struct acpi_2_rsdt *)(uint32_t)(rsdp->XsdtAddress), 0);
879rsdp->Checksum=0;
880rsdp->Checksum=256-checksum8(rsdp,20);
881rsdp->ExtendedChecksum=0;
882rsdp->ExtendedChecksum=256-checksum8(rsdp,36);
883return rsdp;
884}
885
886
887/* Setup ACPI. Replace DSDT if DSDT.aml is found */
888int setupAcpi(void)
889{
890int version;
891//void *new_dsdt;
892
893const char *filename;
894char dirSpec[128];
895int len = 0, old_dsdt_len = 0;
896struct acpi_2_rsdp *rsdp, *rsdp_mod, *rsdp1 = 0;
897struct acpi_2_rsdt *rsdt, *rsdt_mod = 0;
898struct acpi_2_rsdt *xsdt; //, *xsdt_mod = 0;
899char* old_dsdt = 0;
900
901// Try using the file specified with the DSDT option
902if (getValueForKey(kDSDT, &filename, &len, &bootInfo->chameleonConfig))
903{
904sprintf(dirSpec, filename);
905}
906else
907{
908sprintf(dirSpec, "DSDT.aml");
909}
910
911// Load replacement DSDT
912new_dsdt = loadACPITable(dirSpec);
913// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
914/*if (!new_dsdt)
915 {
916 return setupAcpiNoMod();
917 }*/
918
919// Mozodojo: Load additional SSDTs
920struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
921int ssdt_count=0;
922
923// SSDT Options
924bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
925
926getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->chameleonConfig);
927getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->chameleonConfig);
928getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->chameleonConfig);
929
930{
931int i;
932
933for (i=0; i<30; i++)
934{
935char filename[512];
936
937sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
938
939if(new_ssdt[ssdt_count] = loadACPITable(filename))
940{
941ssdt_count++;
942}
943/*else
944{
945break;
946}*/
947}
948}
949
950// Do the same procedure for both versions of ACPI
951for (version=0; version<2; version++) {
952//int rsdplength;
953//Here version=0 for ACPI 1.0 and version=1 for ACPI 2.0
954// Find original rsdp
955rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
956//Slice
957if (!rsdp)
958{
959if (version){
960DBG("No ACPI version 2 found. Creating...\n");
961rsdp=createNewACPI20(rsdp1);
962if(!rsdp){
963addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
964continue;
965}
966} else {
967DBG("No ACPI version 1 found. Ignoring\n");
968addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
969continue;
970}
971}
972if(!version) rsdp1 = rsdp; //save to create new.
973rsdplength=version?rsdp->Length:20;
974
975DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
976
977/* FIXME: no check that memory allocation succeeded
978 * Copy and patch RSDP,RSDT, XSDT and FADT
979 * For more info see ACPI Specification pages 110 and following
980 */
981
982rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
983memcpy(rsdp_mod, rsdp, rsdplength);
984rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
985xsdt=(struct acpi_2_rsdt *)(uint32_t)(rsdp->XsdtAddress);
986DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
987
988//if we have RSDT table
989if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
990{
991uint32_t *rsdt_entries;
992int rsdt_entries_num;
993int dropoffset=0, i;
994
995// mozo: using malloc cos I didn't found how to free already allocated kernel memory
996rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
997memcpy (rsdt_mod, rsdt, rsdt->Length);
998rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
999rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
1000rsdt_entries=(uint32_t *)(rsdt_mod+1);
1001for (i=0;i<rsdt_entries_num;i++)
1002{
1003char *table=(char *)(rsdt_entries[i]);
1004if (!table)
1005continue;
1006
1007DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
1008
1009rsdt_entries[i-dropoffset]=rsdt_entries[i];
1010
1011if (drop_ssdt && tableSign(table, "SSDT"))
1012{
1013dropoffset++;
1014continue;
1015}
1016if (tableSign(table, "DSDT"))
1017{
1018DBG("DSDT found\n");
1019
1020if(new_dsdt)
1021rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
1022
1023continue;
1024} else {
1025//Slice - Correct header to MacModel & Vendor, correct chechsum
1026struct acpi_2_rsdt *t = (struct acpi_2_rsdt *)table;
1027strncpy(t->OEMID, "Apple ", 6);
1028strncpy(t->OEMTableId, MacModel, 8);
1029t->OEMRevision = ModelRev;
1030t->Checksum=0;
1031t->Checksum=256-checksum8(t,t->Length);
1032
1033}
1034if (tableSign(table, "FACP"))
1035{
1036struct acpi_2_fadt *fadt, *fadt_mod;
1037fadt=(struct acpi_2_fadt *)rsdt_entries[i];
1038
1039DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
1040
1041if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
1042{
1043printf("FADT incorrect. Not modified\n");
1044continue;
1045}
1046
1047if(version && fix_restart){ //ACPI 2.0
1048fadt_mod = patch_fadt(fadt, new_dsdt);
1049rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1050} else {
1051if (fadt && Platform->Type)
1052fadt->PM_Profile = Platform->Type;//For ACPI1.0 the only patch
1053fadt_mod = fadt;
1054rsdt_entries[i-dropoffset]=(uint32_t)fadt;
1055}
1056DBG("FADT_mod table sign: %s\n", fadt_mod->Signature);
1057//Slice
1058//Now I want to replace DSDT in place
1059// it is only way to patch DSDT on some platform
1060old_dsdt = (char *)fadt->DSDT;
1061if(!old_dsdt || !tableSign(old_dsdt, "DSDT")){
1062if(fadt_mod->Length > 140) old_dsdt = (char *)(uint32_t)fadt_mod->X_DSDT;
1063if(!old_dsdt || !tableSign(old_dsdt, "DSDT")) old_dsdt = (char *)new_dsdt;
1064}
1065DBG("New DSDT @%x, X_DSDT %x after FADT patch\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
1066
1067if(tableSign(old_dsdt, "DSDT"))
1068old_dsdt_len = ((struct acpi_2_rsdt *)old_dsdt)->Length;
1069int new_dsdt_len = ((struct acpi_2_rsdt *)new_dsdt)->Length;
1070if(new_dsdt_len > old_dsdt_len) {
1071old_dsdt = (char *)new_dsdt;
1072DBG(" New DSDT is longer then old\n");
1073len = 0;
1074}
1075else len = new_dsdt_len;
1076
1077
1078// Generate _CST SSDT
1079if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1080{
1081generate_cstates = false; // Generate SSDT only once!
1082ssdt_count++;
1083}
1084
1085// Generating _PSS SSDT
1086if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1087{
1088generate_pstates = false; // Generate SSDT only once!
1089ssdt_count++;
1090}
1091
1092continue;
1093}
1094}//for rsdt entries
1095DBG("\n");
1096//Slice - Correct header to MacModel & Vendor, correct chechsum
1097strncpy(rsdt_mod->OEMID, "Apple ", 6);
1098strncpy(rsdt_mod->OEMTableId, MacModel, 8);
1099rsdt_mod->OEMRevision = ModelRev;
1100
1101// Allocate rsdt in Kernel memory area
1102rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
1103struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
1104memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
1105free(rsdt_mod); rsdt_mod = rsdt_copy;
1106rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
1107rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
1108rsdt_entries=(uint32_t *)(rsdt_mod+1);
1109
1110// Mozodojo: Insert additional SSDTs into RSDT
1111if(ssdt_count>0)
1112{
1113int j;
1114
1115for (j=0; j<ssdt_count; j++)
1116rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1117
1118DBG("RSDT: Added %d SSDT table(s)\n", ssdt_count);
1119}
1120
1121// Correct the checksum of RSDT
1122//DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
1123
1124rsdt_mod->Checksum=0;
1125rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
1126
1127//DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
1128}
1129else
1130{
1131rsdp_mod->RsdtAddress=0;
1132verbose("RSDT not found or RSDT incorrect\n");
1133}
1134
1135if (version)
1136{
1137struct acpi_2_xsdt *xsdt, *xsdt_mod;
1138
1139// FIXME: handle 64-bit address correctly
1140
1141xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
1142DBG("XSDT @%x;%x, Length=%d Sign=%c%c%c%c\n", (uint32_t)(rsdp->XsdtAddress>>32),
1143(uint32_t)rsdp->XsdtAddress, xsdt->Length, xsdt[0], xsdt[1], xsdt[2], xsdt[3]);
1144if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
1145{
1146uint64_t *xsdt_entries;
1147int xsdt_entries_num, i;
1148int dropoffset=0;
1149
1150// mozo: using malloc cos I didn't found how to free already allocated kernel memory
1151xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
1152memcpy(xsdt_mod, xsdt, xsdt->Length);
1153rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1154xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1155xsdt_entries=(uint64_t *)(xsdt_mod+1);
1156for (i=0;i<xsdt_entries_num;i++)
1157{
1158char *table=(char *)((uint32_t)(xsdt_entries[i]));
1159if (!table)
1160continue;
1161
1162xsdt_entries[i-dropoffset]=xsdt_entries[i];
1163
1164if (drop_ssdt && tableSign(table, "SSDT"))
1165{
1166dropoffset++;
1167continue;
1168}
1169if (tableSign(table, "DSDT"))
1170{
1171DBG("DSDT found\n");
1172
1173if (new_dsdt)
1174xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
1175
1176DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);
1177
1178continue;
1179}
1180if (tableSign(table, "FACP"))
1181{
1182struct acpi_2_fadt *fadt, *fadt_mod;
1183fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
1184
1185DBG("FADT found @%x%x, Length %d\n", (uint32_t)(xsdt_entries[i]>>32),fadt,
1186fadt->Length);
1187
1188if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
1189{
1190DBG("FADT incorrect or after 4GB. Dropping XSDT\n");
1191goto drop_xsdt;
1192}
1193
1194fadt_mod = patch_fadt(fadt, new_dsdt);
1195xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1196
1197DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);
1198
1199// Generate _CST SSDT
1200if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1201{
1202generate_cstates = false; // Generate SSDT only once!
1203ssdt_count++;
1204}
1205
1206// Generating _PSS SSDT
1207if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1208{
1209generate_pstates = false; // Generate SSDT only once!
1210ssdt_count++;
1211}
1212
1213continue;
1214}
1215
1216DBG("TABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);
1217
1218}
1219
1220// Allocate xsdt in Kernel memory area
1221xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
1222struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
1223memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
1224free(xsdt_mod); xsdt_mod = xsdt_copy;
1225rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1226xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1227xsdt_entries=(uint64_t *)(xsdt_mod+1);
1228
1229// Mozodojo: Insert additional SSDTs into XSDT
1230if(ssdt_count>0)
1231{
1232int j;
1233
1234for (j=0; j<ssdt_count; j++)
1235xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1236
1237DBG("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1238}
1239
1240// Correct the checksum of XSDT
1241xsdt_mod->Checksum=0;
1242xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1243}
1244else
1245{
1246drop_xsdt:
1247
1248DBG("About to drop XSDT\n");
1249
1250/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1251 * A Better strategy would be to generate
1252 */
1253//Slice - it is possible only for ACPI20
1254if (version) {
1255if(rsdp->RsdtAddress){
1256rsdp_mod->XsdtAddress =
1257(uint64_t)(uint32_t)createNewXSDTfromRSDT((struct acpi_2_rsdt*)rsdp->RsdtAddress);
1258continue;
1259}
1260else
1261DBG(" no sample to create XSDT, dropping\n");
1262}
1263
1264rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1265DBG("XSDT not found or XSDT incorrect\n");
1266}
1267}
1268//Slice - correct DSDT in place
1269if(len && old_dsdt && new_dsdt && (old_dsdt != new_dsdt)){
1270DBG("old_dsdt=%08x new_dsdt=%08x new_len=%d\n", (unsigned int)old_dsdt, (unsigned int)new_dsdt, len);
1271bcopy(new_dsdt, old_dsdt, len);
1272}
1273
1274// Correct the checksum of RSDP
1275
1276DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1277
1278rsdp_mod->Checksum=0;
1279rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1280
1281DBG("New checksum %d\n", rsdp_mod->Checksum);
1282
1283if (version)
1284{
1285DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1286
1287rsdp_mod->ExtendedChecksum=0;
1288rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1289
1290DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1291
1292}
1293
1294//verbose("Patched ACPI version %d DSDT\n", version+1);
1295if (version)
1296{
1297/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1298//Slice because rsdp_mod is a pointer (32bit in i386) to a structure uint64_t*
1299acpi20_p = (uint64_t)(uint32_t)rsdp_mod;
1300addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1301}
1302else
1303{
1304/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1305acpi10_p = (uint64_t)(uint32_t)rsdp_mod;
1306addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1307}
1308}
1309#if DEBUG_ACPI
1310printf("Press a key to continue... (DEBUG_ACPI)\n");
1311getchar();
1312#endif
1313return 1;
1314}
1315

Archive Download this file

Revision: 1175