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

Archive Download this file

Revision: 2355