Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/i386/libsaio/acpi_patcher.c

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

Archive Download this file

Revision: 2341