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

Archive Download this file

Revision: 2461