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

Archive Download this file

Revision: 1200