Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/acpi_patcher.c

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

Archive Download this file

Revision: 1162