Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/acpi_patcher.c

1/*
2 * Copyright 2008 mackerintel
3 * 2010 mojodojo, 2012 slice
4 */
5
6#include "libsaio.h"
7#include "boot.h"
8#include "bootstruct.h"
9#include "acpi.h"
10#include "efi_tables.h"
11#include "fake_efi.h"
12#include "acpi_patcher.h"
13#include "platform.h"
14#include "cpu.h"
15#include "aml_generator.h"
16
17#ifndef DEBUG_ACPI
18#define DEBUG_ACPI 0
19#endif
20
21#if DEBUG_ACPI==2
22#define DBG(x...) {printf(x); sleep(1);}
23#elif DEBUG_ACPI==1
24#define DBG(x...) printf(x)
25#else
26#define DBG(x...)
27#endif
28
29// Slice: New signature compare function
30boolean_t tableSign(char *table, const char *sgn)
31{
32int i;
33for (i=0; i<4; i++)
34{
35if ((table[i] &~0x20) != (sgn[i] &~0x20))
36{
37return false;
38}
39}
40return true;
41}
42
43/* Gets the ACPI 1.0 RSDP address */
44static struct acpi_2_rsdp* getAddressOfAcpiTable()
45{
46/* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
47
48void *acpi_addr = (void*)ACPI_RANGE_START;
49for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
50{
51if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
52{
53uint8_t csum = checksum8(acpi_addr, 20);
54if(csum == 0)
55{
56// Only return the table if it is a true version 1.0 table (Revision 0)
57if(((struct acpi_2_rsdp*)acpi_addr)->Revision == 0)
58return acpi_addr;
59}
60}
61}
62return NULL;
63}
64
65/* Gets the ACPI 2.0 RSDP address */
66static struct acpi_2_rsdp* getAddressOfAcpi20Table()
67{
68/* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
69
70void *acpi_addr = (void*)ACPI_RANGE_START;
71for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
72{
73if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
74{
75uint8_t csum = checksum8(acpi_addr, 20);
76
77/* Only assume this is a 2.0 or better table if the revision is greater than 0
78 * NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1
79 * and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.
80 */
81
82if(csum == 0 && (((struct acpi_2_rsdp*)acpi_addr)->Revision > 0))
83{
84uint8_t csum2 = checksum8(acpi_addr, sizeof(struct acpi_2_rsdp));
85if(csum2 == 0)
86{
87return acpi_addr;
88}
89}
90}
91}
92return NULL;
93}
94
95/* The folowing ACPI Table search algo. should be reused anywhere needed:*/
96/* WARNING: outDirspec string will be overwritten by subsequent calls! */
97int search_and_get_acpi_fd(const char * filename, const char ** outDirspec)
98{
99int fd = 0;
100static char dirSpec[512];
101
102// Try finding 'filename' in the usual places
103// Start searching any potential location for ACPI Table
104snprintf(dirSpec, sizeof(dirSpec), "%s", filename);
105fd = open(dirSpec, 0);
106if (fd < 0) {
107snprintf(dirSpec, sizeof(dirSpec), "/Extra/%s", filename);
108fd = open(dirSpec, 0);
109if (fd < 0)
110{
111snprintf(dirSpec, sizeof(dirSpec), "bt(0,0)/Extra/%s", filename);
112fd = open(dirSpec, 0);
113if (fd < 0) {
114// NOT FOUND:
115verbose("ACPI Table not found: %s\n", filename);
116*dirSpec = '\0';
117}
118}
119}
120
121if (outDirspec) *outDirspec = dirSpec;
122return fd;
123}
124
125void *loadACPITable (const char * filename)
126{
127void *tableAddr;
128const char * dirspec=NULL;
129
130int fd = search_and_get_acpi_fd(filename, &dirspec);
131
132if (fd>=0)
133{
134tableAddr=(void*)AllocateKernelMemory(file_size (fd));
135if (tableAddr)
136{
137if (read (fd, tableAddr, file_size (fd))!=file_size (fd))
138{
139printf("Couldn't read table %s\n",dirspec);
140free (tableAddr);
141close (fd);
142return NULL;
143}
144
145DBG("Table %s read and stored at: %x\n", dirspec, tableAddr);
146close (fd);
147return tableAddr;
148}
149close (fd);
150printf("Couldn't allocate memory for table \n", dirspec);
151}
152//printf("Couldn't find table %s\n", filename);
153return NULL;
154}
155
156uint8_tacpi_cpu_count = 0;
157char* acpi_cpu_name[32];
158uint32_t acpi_cpu_p_blk = 0;
159
160void get_acpi_cpu_names(unsigned char* dsdt, uint32_t length)
161{
162uint32_t i;
163
164DBG("start finding cpu names. length %d\n", length);
165
166for (i=0; i<length-7; i++)
167{
168if (dsdt[i] == 0x5B && dsdt[i+1] == 0x83) // ProcessorOP
169{
170DBG("dsdt: %x%x\n", dsdt[i], dsdt[i+1]);
171
172uint32_t offset = i + 3 + (dsdt[i+2] >> 6);
173
174bool add_name = true;
175
176uint8_t j;
177
178for (j=0; j<4; j++)
179{
180char c = dsdt[offset+j];
181
182if (!aml_isvalidchar(c))
183{
184add_name = false;
185verbose("Invalid character found in ProcessorOP 0x%x!\n", c);
186break;
187}
188}
189
190if (add_name)
191{
192acpi_cpu_name[acpi_cpu_count] = malloc(4);
193memcpy(acpi_cpu_name[acpi_cpu_count], dsdt+offset, 4);
194i = offset + 5;
195
196if (acpi_cpu_count == 0)
197acpi_cpu_p_blk = dsdt[i] | (dsdt[i+1] << 8);
198
199verbose("Found ACPI CPU: %c%c%c%c\n", acpi_cpu_name[acpi_cpu_count][0], acpi_cpu_name[acpi_cpu_count][1], acpi_cpu_name[acpi_cpu_count][2], acpi_cpu_name[acpi_cpu_count][3]);
200
201if (++acpi_cpu_count == 32)
202{
203return;
204}
205}
206}
207}
208
209DBG("end finding cpu names: cpu names found: %d\n", acpi_cpu_count);
210}
211
212struct acpi_2_ssdt *generate_cst_ssdt(struct acpi_2_fadt* fadt)
213{
214char ssdt_header[] =
215{
2160x53, 0x53, 0x44, 0x54, 0xE7, 0x00, 0x00, 0x00, /* SSDT.... */
2170x01, 0x17, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x41, /* ..PmRefA */
2180x43, 0x70, 0x75, 0x43, 0x73, 0x74, 0x00, 0x00, /* CpuCst.. */
2190x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* ....INTL */
2200x31, 0x03, 0x10, 0x20/* 1.._*/
221};
222
223char resource_template_register_fixedhw[] =
224{
2250x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F,
2260x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2270x00, 0x00, 0x01, 0x79, 0x00
228};
229
230char resource_template_register_systemio[] =
231{
2320x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x01,
2330x08, 0x00, 0x00, 0x15, 0x04, 0x00, 0x00, 0x00,
2340x00, 0x00, 0x00, 0x79, 0x00,
235};
236
237if (Platform.CPU.Vendor != 0x756E6547) {
238verbose ("Not an Intel platform: C-States will not be generated !!!\n");
239return NULL;
240}
241
242if (fadt == NULL) {
243verbose ("FACP not exists: C-States will not be generated !!!\n");
244return NULL;
245}
246
247struct acpi_2_dsdt* dsdt = (void*)fadt->DSDT;
248
249if (dsdt == NULL) {
250verbose ("DSDT not found: C-States will not be generated !!!\n");
251return NULL;
252}
253
254if (acpi_cpu_count == 0)
255get_acpi_cpu_names((void*)dsdt, dsdt->Length);
256
257if (acpi_cpu_count > 0)
258{
259bool c2_enabled = false;
260bool c3_enabled = false;
261bool c4_enabled = false;
262bool cst_using_systemio = false;
263
264getBoolForKey(kEnableC2State, &c2_enabled, &bootInfo->chameleonConfig);
265getBoolForKey(kEnableC3State, &c3_enabled, &bootInfo->chameleonConfig);
266getBoolForKey(kEnableC4State, &c4_enabled, &bootInfo->chameleonConfig);
267getBoolForKey(kCSTUsingSystemIO, &cst_using_systemio, &bootInfo->chameleonConfig);
268
269c2_enabled = c2_enabled | (fadt->C2_Latency < 100);
270c3_enabled = c3_enabled | (fadt->C3_Latency < 1000);
271
272unsigned char cstates_count = 1 + (c2_enabled ? 1 : 0) + (c3_enabled ? 1 : 0);
273
274AML_CHUNK* root = aml_create_node(NULL);
275aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
276AML_CHUNK* scop = aml_add_scope(root, "\\_PR_");
277AML_CHUNK* name = aml_add_name(scop, "CST_");
278AML_CHUNK* pack = aml_add_package(name);
279aml_add_byte(pack, cstates_count);
280
281AML_CHUNK* tmpl = aml_add_package(pack);
282if (cst_using_systemio)
283{
284// C1
285resource_template_register_fixedhw[8] = 0x00;
286resource_template_register_fixedhw[9] = 0x00;
287resource_template_register_fixedhw[18] = 0x00;
288aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
289aml_add_byte(tmpl, 0x01);// C1
290aml_add_word(tmpl, 0x0001);// Latency
291aml_add_dword(tmpl, 0x000003e8);// Power
292
293uint8_t p_blk_lo, p_blk_hi;
294
295if (c2_enabled) // C2
296{
297p_blk_lo = acpi_cpu_p_blk + 4;
298p_blk_hi = (acpi_cpu_p_blk + 4) >> 8;
299
300tmpl = aml_add_package(pack);
301resource_template_register_systemio[11] = p_blk_lo; // C2
302resource_template_register_systemio[12] = p_blk_hi; // C2
303aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
304aml_add_byte(tmpl, 0x02);// C2
305aml_add_word(tmpl, 0x0040);// Latency
306aml_add_dword(tmpl, 0x000001f4);// Power
307}
308
309if (c4_enabled) // C4
310{
311p_blk_lo = acpi_cpu_p_blk + 5;
312p_blk_hi = (acpi_cpu_p_blk + 5) >> 8;
313
314tmpl = aml_add_package(pack);
315resource_template_register_systemio[11] = p_blk_lo; // C4
316resource_template_register_systemio[12] = p_blk_hi; // C4
317aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
318aml_add_byte(tmpl, 0x04);// C4
319aml_add_word(tmpl, 0x0080);// Latency
320aml_add_dword(tmpl, 0x000000C8);// Power
321}
322else if (c3_enabled) // C3
323{
324p_blk_lo = acpi_cpu_p_blk + 5;
325p_blk_hi = (acpi_cpu_p_blk + 5) >> 8;
326
327tmpl = aml_add_package(pack);
328resource_template_register_systemio[11] = p_blk_lo; // C3
329resource_template_register_systemio[12] = p_blk_hi; // C3
330aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
331aml_add_byte(tmpl, 0x03);// C3
332aml_add_word(tmpl, 0x0060);// Latency
333aml_add_dword(tmpl, 0x0000015e);// Power
334}
335}
336else
337{
338// C1
339resource_template_register_fixedhw[11] = 0x00; // C1
340aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
341aml_add_byte(tmpl, 0x01);// C1
342aml_add_word(tmpl, 0x0001);// Latency
343aml_add_dword(tmpl, 0x000003e8);// Power
344
345resource_template_register_fixedhw[18] = 0x03;
346
347if (c2_enabled) // C2
348{
349tmpl = aml_add_package(pack);
350resource_template_register_fixedhw[11] = 0x10; // C2
351aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
352aml_add_byte(tmpl, 0x02);// C2
353aml_add_word(tmpl, 0x0040);// Latency
354aml_add_dword(tmpl, 0x000001f4);// Power
355}
356
357if (c4_enabled) // C4
358{
359tmpl = aml_add_package(pack);
360resource_template_register_fixedhw[11] = 0x30; // C4
361aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
362aml_add_byte(tmpl, 0x04);// C4
363aml_add_word(tmpl, 0x0080);// Latency
364aml_add_dword(tmpl, 0x000000C8);// Power
365}
366else if (c3_enabled)
367{
368tmpl = aml_add_package(pack);
369resource_template_register_fixedhw[11] = 0x20; // C3
370aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
371aml_add_byte(tmpl, 0x03);// C3
372aml_add_word(tmpl, 0x0060);// Latency
373aml_add_dword(tmpl, 0x0000015e);// Power
374}
375}
376
377// Aliaces
378int i;
379for (i = 0; i < acpi_cpu_count; i++)
380{
381char name[9];
382sprintf(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]);
383
384scop = aml_add_scope(root, name);
385aml_add_alias(scop, "CST_", "_CST");
386}
387
388aml_calculate_size(root);
389
390struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
391
392aml_write_node(root, (void*)ssdt, 0);
393
394ssdt->Length = root->Size;
395ssdt->Checksum = 0;
396ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
397
398aml_destroy_node(root);
399
400// dumpPhysAddr("C-States SSDT content: ", ssdt, ssdt->Length);
401
402verbose ("SSDT with CPU C-States generated successfully\n");
403
404return ssdt;
405}
406else
407{
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)
516{
517DBG("P-States: Insane FID values!");
518p_states_count = 0;
519}
520else
521{
522// Finalize P-States
523// Find how many P-States machine supports
524p_states_count = maximum.CID - minimum.CID + 1;
525
526if (p_states_count > 32)
527p_states_count = 32;
528
529uint8_t vidstep;
530uint8_t i = 0, u, invalid = 0;
531
532vidstep = ((maximum.VID << 2) - (minimum.VID << 2)) / (p_states_count - 1);
533
534for (u = 0; u < p_states_count; u++)
535{
536i = u - invalid;
537
538p_states[i].CID = maximum.CID - u;
539p_states[i].FID = (p_states[i].CID >> 1);
540
541if (p_states[i].FID < 0x6)
542{
543if (cpu_dynamic_fsb)
544p_states[i].FID = (p_states[i].FID << 1) | 0x80;
545}
546else if (cpu_noninteger_bus_ratio)
547{
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;
555
556uint32_t multiplier = p_states[i].FID & 0x1f;// = 0x08
557bool half = p_states[i].FID & 0x40;// = 0x01
558bool dfsb = p_states[i].FID & 0x80;// = 0x00
559uint32_t fsb = Platform.CPU.FSBFrequency / 1000000; // = 400
560uint32_t halffsb = (fsb + 1) >> 1;// = 200
561uint32_t frequency = (multiplier * fsb);// = 3200
562
563p_states[i].Frequency = (frequency + (half * halffsb)) >> dfsb;// = 3200 + 200 = 3400
564}
565
566p_states_count -= invalid;
567}
568
569break;
570}
571case CPU_MODEL_FIELDS:// Intel Core i5, i7, Xeon X34xx LGA1156 (45nm)
572case CPU_MODEL_DALES:
573case CPU_MODEL_DALES_32NM:// Intel Core i3, i5 LGA1156 (32nm)
574case CPU_MODEL_NEHALEM:// Intel Core i7, Xeon W35xx, Xeon X55xx, Xeon E55xx LGA1366 (45nm)
575case CPU_MODEL_NEHALEM_EX:// Intel Xeon X75xx, Xeon X65xx, Xeon E75xx, Xeon E65xx
576case CPU_MODEL_WESTMERE:// Intel Core i7, Xeon X56xx, Xeon E56xx, Xeon W36xx LGA1366 (32nm) 6 Core
577case CPU_MODEL_WESTMERE_EX:// Intel Xeon E7
578case CPU_MODEL_SANDYBRIDGE:// Intel Core i3, i5, i7 LGA1155 (32nm)
579case CPU_MODEL_JAKETOWN:// Intel Core i7, Xeon E5 LGA2011 (32nm)
580case CPU_MODEL_IVYBRIDGE:// Intel Core i3, i5, i7 LGA1155 (22nm)
581case CPU_MODEL_HASWELL://
582case CPU_MODEL_IVYBRIDGE_XEON: //
583//case CPU_MODEL_HASWELL_H://
584case CPU_MODEL_HASWELL_MB://
585case CPU_MODEL_HASWELL_ULT://
586case CPU_MODEL_CRYSTALWELL://
587
588{
589if ((Platform.CPU.Model == CPU_MODEL_SANDYBRIDGE) || (Platform.CPU.Model == CPU_MODEL_JAKETOWN) ||
590(Platform.CPU.Model == CPU_MODEL_IVYBRIDGE) || (Platform.CPU.Model == CPU_MODEL_HASWELL) ||
591(Platform.CPU.Model == CPU_MODEL_IVYBRIDGE_XEON) || (Platform.CPU.Model == CPU_MODEL_HASWELL_MB) ||
592(Platform.CPU.Model == CPU_MODEL_HASWELL_ULT) || (Platform.CPU.Model == CPU_MODEL_CRYSTALWELL))
593{
594maximum.Control = (rdmsr64(MSR_IA32_PERF_STATUS) >> 8) & 0xff;
595}
596else
597{
598maximum.Control = rdmsr64(MSR_IA32_PERF_STATUS) & 0xff;
599}
600
601minimum.Control = (rdmsr64(MSR_PLATFORM_INFO) >> 40) & 0xff;
602
603verbose("P-States: min 0x%x, max 0x%x\n", minimum.Control, maximum.Control);
604
605// Sanity check
606if (maximum.Control < minimum.Control)
607{
608DBG("Insane control values!");
609p_states_count = 0;
610}
611else
612{
613uint8_t i;
614p_states_count = 0;
615
616for (i = maximum.Control; i >= minimum.Control; i--)
617{
618p_states[p_states_count].Control = i;
619p_states[p_states_count].CID = p_states[p_states_count].Control << 1;
620p_states[p_states_count].Frequency = (Platform.CPU.FSBFrequency / 1000000) * i;
621p_states_count++;
622}
623}
624
625break;
626}
627default:
628verbose ("Unsupported CPU: P-States not generated !!! Unknown CPU Type\n");
629break;
630}
631}
632}
633
634// Generating SSDT
635if (p_states_count > 0)
636{
637int i;
638
639AML_CHUNK* root = aml_create_node(NULL);
640aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
641
642AML_CHUNK* scop = aml_add_scope(root, "\\_PR_");
643AML_CHUNK* name = aml_add_name(scop, "PSS_");
644AML_CHUNK* pack = aml_add_package(name);
645
646for (i = 0; i < p_states_count; i++)
647{
648AML_CHUNK* pstt = aml_add_package(pack);
649
650aml_add_dword(pstt, p_states[i].Frequency);
651aml_add_dword(pstt, 0x00000000); // Power
652aml_add_dword(pstt, 0x0000000A); // Latency
653aml_add_dword(pstt, 0x0000000A); // Latency
654aml_add_dword(pstt, p_states[i].Control);
655aml_add_dword(pstt, i+1); // Status
656}
657
658// Add aliaces
659for (i = 0; i < acpi_cpu_count; i++)
660{
661char name[9];
662sprintf(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]);
663
664scop = aml_add_scope(root, name);
665aml_add_alias(scop, "PSS_", "_PSS");
666}
667
668aml_calculate_size(root);
669
670struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
671
672aml_write_node(root, (void*)ssdt, 0);
673
674ssdt->Length = root->Size;
675ssdt->Checksum = 0;
676ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
677
678aml_destroy_node(root);
679
680//dumpPhysAddr("P-States SSDT content: ", ssdt, ssdt->Length);
681
682verbose ("SSDT with CPU P-States generated successfully\n");
683
684return ssdt;
685}
686}
687else
688{
689verbose ("ACPI CPUs not found: P-States not generated !!!\n");
690}
691
692return NULL;
693}
694
695struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)
696{
697extern void setupSystemType();
698
699struct acpi_2_fadt *fadt_mod;
700bool fadt_rev2_needed = false;
701bool fix_restart;
702bool fix_restart_ps2;
703const char * value;
704
705// Restart Fix
706if (Platform.CPU.Vendor == 0x756E6547)
707{/* Intel */
708fix_restart = true;
709fix_restart_ps2 = false;
710if ( getBoolForKey(kPS2RestartFix, &fix_restart_ps2, &bootInfo->chameleonConfig) && fix_restart_ps2)
711{
712fix_restart = true;
713}
714else
715{
716getBoolForKey(kRestartFix, &fix_restart, &bootInfo->chameleonConfig);
717}
718}
719else
720{
721verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
722fix_restart = false;
723}
724
725if (fix_restart)
726{
727fadt_rev2_needed = true;
728}
729
730// Allocate new fadt table
731if (fadt->Length < 0x84 && fadt_rev2_needed)
732{
733fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
734memcpy(fadt_mod, fadt, fadt->Length);
735fadt_mod->Length = 0x84;
736fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
737}
738else
739{
740fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
741memcpy(fadt_mod, fadt, fadt->Length);
742}
743// Determine system type / PM_Model
744if ( (value=getStringForKey(kSystemType, &bootInfo->chameleonConfig))!=NULL)
745{
746if (Platform.Type > 6)
747{
748if(fadt_mod->PM_Profile<=6)
749{
750Platform.Type = fadt_mod->PM_Profile; // get the fadt if correct
751}
752else
753{
754Platform.Type = 1;/* Set a fixed value (Desktop) */
755}
756verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform.Type);
757}
758else
759{
760Platform.Type = (unsigned char) strtoul(value, NULL, 10);
761}
762}
763// Set PM_Profile from System-type if only user wanted this value to be forced
764if (fadt_mod->PM_Profile != Platform.Type)
765{
766if (value)
767{
768// user has overriden the SystemType so take care of it in FACP
769verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);
770fadt_mod->PM_Profile = Platform.Type;
771}
772else
773{
774// PM_Profile has a different value and no override has been set, so reflect the user value to ioregs
775Platform.Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;
776}
777}
778// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()
779// because we need to take care of facp original content, if it is correct.
780setupSystemType();
781
782// Patch FADT to fix restart
783if (fix_restart)
784{
785if (fix_restart_ps2)
786{
787fadt_mod->Flags|= 0x400;
788fadt_mod->Reset_SpaceID= 0x01; // System I/O
789fadt_mod->Reset_BitWidth= 0x08; // 1 byte
790fadt_mod->Reset_BitOffset= 0x00; // Offset 0
791fadt_mod->Reset_AccessWidth= 0x01; // Byte access
792fadt_mod->Reset_Address= 0x64; // Address of the register
793fadt_mod->Reset_Value= 0xfe; // Value to write to reset the system
794msglog("FADT: PS2 Restart Fix applied!\n");
795}
796else
797{
798fadt_mod->Flags|= 0x400;
799fadt_mod->Reset_SpaceID= 0x01; // System I/O
800fadt_mod->Reset_BitWidth= 0x08; // 1 byte
801fadt_mod->Reset_BitOffset= 0x00; // Offset 0
802fadt_mod->Reset_AccessWidth= 0x01; // Byte access
803fadt_mod->Reset_Address= 0x0cf9; // Address of the register
804fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
805verbose("FADT: ACPI Restart Fix applied!\n");
806}
807
808}
809
810// Patch DSDT Address if we have loaded DSDT.aml
811if(new_dsdt)
812{
813DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
814
815fadt_mod->DSDT=(uint32_t)new_dsdt;
816if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
817{
818fadt_mod->X_DSDT=(uint32_t)new_dsdt;
819}
820
821DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
822
823verbose("FADT: Using custom DSDT!\n");
824}
825
826// Correct the checksum
827fadt_mod->Checksum=0;
828fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
829
830return fadt_mod;
831}
832
833/* Setup ACPI without replacing DSDT. */
834int setupAcpiNoMod()
835{
836//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
837//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
838/* XXX aserebln why uint32 cast if pointer is uint64 ? */
839acpi10_p = (uint32_t)getAddressOfAcpiTable();
840acpi20_p = (uint32_t)getAddressOfAcpi20Table();
841addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
842if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
843return 1;
844}
845
846/* Setup ACPI. Replace DSDT if DSDT.aml is found */
847int setupAcpi(void)
848{
849int version;
850void *new_dsdt;
851
852
853const char *filename;
854char dirSpec[128];
855int len = 0;
856
857// always reset cpu count to 0 when injecting new acpi
858acpi_cpu_count = 0;
859
860/* Try using the file specified with the DSDT option */
861if (getValueForKey(kDSDT, &filename, &len, &bootInfo->chameleonConfig))
862{
863snprintf(dirSpec, sizeof(dirSpec), filename);
864}
865else
866{
867sprintf(dirSpec, "DSDT.aml");
868//verbose("dirSpec, DSDT.aml");
869}
870
871// Load replacement DSDT
872new_dsdt = loadACPITable(dirSpec);
873// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
874/*if (!new_dsdt)
875 {
876 return setupAcpiNoMod();
877 }*/
878
879// Mozodojo: Load additional SSDTs
880struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
881int ssdt_count=0;
882
883// SSDT Options
884bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
885
886getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->chameleonConfig);
887getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->chameleonConfig);
888getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->chameleonConfig);
889
890DBG("Generating P-States config: %d\n", generate_pstates);
891DBG("Generating C-States config: %d\n", generate_cstates);
892
893{
894int i;
895
896for (i = 0; i < 30; i++)
897{
898char filename[512];
899
900sprintf(filename, i > 0?"SSDT-%d.aml":"SSDT.aml", i);
901
902if ( (new_ssdt[ssdt_count] = loadACPITable(filename)) )
903{
904ssdt_count++;
905}
906else
907{
908break;
909}
910}
911}
912
913// Do the same procedure for both versions of ACPI
914for (version = 0; version < 2; version++) {
915struct acpi_2_rsdp *rsdp, *rsdp_mod;
916struct acpi_2_rsdt *rsdt, *rsdt_mod;
917int rsdplength;
918
919// Find original rsdp
920rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
921if (!rsdp)
922{
923DBG("No ACPI version %d found. Ignoring\n", version+1);
924if (version)
925{
926addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
927}
928else
929{
930addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
931}
932continue;
933}
934rsdplength=version?rsdp->Length:20;
935
936DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
937
938/* FIXME: no check that memory allocation succeeded
939 * Copy and patch RSDP,RSDT, XSDT and FADT
940 * For more info see ACPI Specification pages 110 and following
941 */
942
943rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
944memcpy(rsdp_mod, rsdp, rsdplength);
945
946rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
947
948DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
949
950if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
951{
952uint32_t *rsdt_entries;
953int rsdt_entries_num;
954int dropoffset=0, i;
955
956// mozo: using malloc cos I didn't found how to free already allocated kernel memory
957rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
958memcpy (rsdt_mod, rsdt, rsdt->Length);
959rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
960rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
961rsdt_entries=(uint32_t *)(rsdt_mod+1);
962for (i=0;i<rsdt_entries_num;i++)
963{
964char *table=(char *)(rsdt_entries[i]);
965if (!table)
966{
967continue;
968}
969
970DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
971
972rsdt_entries[i-dropoffset]=rsdt_entries[i];
973
974if (drop_ssdt && tableSign(table, "SSDT"))
975{
976verbose("OEM SSDT tables was dropped\n");
977dropoffset++;
978continue;
979}
980if (tableSign(table, "DSDT"))
981{
982DBG("DSDT found\n");
983verbose("Custom DSDT table was found\n");
984if(new_dsdt)
985{
986rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
987}
988
989continue;
990}
991
992if (tableSign(table, "FACP"))
993{
994struct acpi_2_fadt *fadt, *fadt_mod;
995fadt=(struct acpi_2_fadt *)rsdt_entries[i];
996
997DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
998
999if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
1000{
1001printf("FADT incorrect. Not modified\n");
1002continue;
1003}
1004
1005fadt_mod = patch_fadt(fadt, new_dsdt);
1006rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1007
1008// Generate _CST SSDT
1009if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1010{
1011DBG("C-States generated\n");
1012generate_cstates = false; // Generate SSDT only once!
1013ssdt_count++;
1014}
1015
1016// Generating _PSS SSDT
1017if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1018{
1019DBG("P-States generated\n");
1020generate_pstates = false; // Generate SSDT only once!
1021ssdt_count++;
1022}
1023
1024continue;
1025}
1026}
1027DBG("\n");
1028
1029// Allocate rsdt in Kernel memory area
1030rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
1031struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
1032memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
1033free(rsdt_mod); rsdt_mod = rsdt_copy;
1034rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
1035rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
1036rsdt_entries=(uint32_t *)(rsdt_mod+1);
1037
1038// Mozodojo: Insert additional SSDTs into RSDT
1039if(ssdt_count>0)
1040{
1041int j;
1042
1043for (j=0; j<ssdt_count; j++)
1044rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1045
1046verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
1047
1048}
1049
1050// Correct the checksum of RSDT
1051DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
1052
1053rsdt_mod->Checksum=0;
1054rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
1055
1056DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
1057}
1058else
1059{
1060rsdp_mod->RsdtAddress=0;
1061printf("RSDT not found or incorrect\n");
1062}
1063
1064if (version)
1065{
1066struct acpi_2_xsdt *xsdt, *xsdt_mod;
1067
1068// FIXME: handle 64-bit address correctly
1069
1070xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
1071DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress, xsdt->Length);
1072
1073if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
1074{
1075uint64_t *xsdt_entries;
1076int xsdt_entries_num, i;
1077int dropoffset=0;
1078
1079// mozo: using malloc cos I didn't found how to free already allocated kernel memory
1080xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
1081memcpy(xsdt_mod, xsdt, xsdt->Length);
1082
1083rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1084xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1085xsdt_entries=(uint64_t *)(xsdt_mod+1);
1086for (i=0;i<xsdt_entries_num;i++)
1087{
1088char *table=(char *)((uint32_t)(xsdt_entries[i]));
1089if (!table)
1090{
1091continue;
1092}
1093xsdt_entries[i-dropoffset]=xsdt_entries[i];
1094
1095if (drop_ssdt && tableSign(table, "SSDT"))
1096{
1097verbose("OEM SSDT tables was dropped\n");
1098dropoffset++;
1099continue;
1100}
1101if (tableSign(table, "DSDT"))
1102{
1103DBG("DSDT found\n");
1104
1105if (new_dsdt)
1106{
1107xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
1108}
1109
1110DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1111
1112continue;
1113}
1114if (tableSign(table, "FACP"))
1115{
1116struct acpi_2_fadt *fadt, *fadt_mod;
1117fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
1118
1119DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
1120fadt->Length);
1121
1122if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
1123{
1124verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
1125goto drop_xsdt;
1126}
1127
1128fadt_mod = patch_fadt(fadt, new_dsdt);
1129xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1130
1131DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1132
1133// Generate _CST SSDT
1134if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1135{
1136generate_cstates = false; // Generate SSDT only once!
1137ssdt_count++;
1138}
1139
1140// Generating _PSS SSDT
1141if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1142{
1143generate_pstates = false; // Generate SSDT only once!
1144ssdt_count++;
1145}
1146
1147continue;
1148}
1149
1150DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1151
1152}
1153
1154// Allocate xsdt in Kernel memory area
1155xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
1156struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
1157memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
1158free(xsdt_mod); xsdt_mod = xsdt_copy;
1159rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1160xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1161xsdt_entries=(uint64_t *)(xsdt_mod+1);
1162
1163// Mozodojo: Insert additional SSDTs into XSDT
1164if(ssdt_count > 0)
1165{
1166int j;
1167
1168for (j=0; j<ssdt_count; j++)
1169xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1170
1171verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1172
1173}
1174
1175// Correct the checksum of XSDT
1176xsdt_mod->Checksum=0;
1177xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1178}
1179else
1180{
1181drop_xsdt:
1182
1183DBG("About to drop XSDT\n");
1184
1185/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1186 * A Better strategy would be to generate
1187 */
1188
1189rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1190verbose("XSDT not found or incorrect\n");
1191}
1192}
1193
1194// Correct the checksum of RSDP
1195
1196DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1197
1198rsdp_mod->Checksum=0;
1199rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1200
1201DBG("New checksum %d\n", rsdp_mod->Checksum);
1202
1203if (version)
1204{
1205DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1206
1207rsdp_mod->ExtendedChecksum=0;
1208rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1209
1210DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1211
1212}
1213
1214//verbose("Patched ACPI version %d DSDT\n", version+1);
1215if (version)
1216{
1217/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1218acpi20_p = (uint32_t)rsdp_mod;
1219addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1220}
1221else
1222{
1223/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1224acpi10_p = (uint32_t)rsdp_mod;
1225addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1226}
1227}
1228#if DEBUG_ACPI
1229printf("Press a key to continue... (DEBUG_ACPI)\n");
1230getchar();
1231#endif
1232return 1;
1233}
1234

Archive Download this file

Revision: 2327