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

Archive Download this file

Revision: 2459