Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/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:*/
96int search_and_get_acpi_fd(const char * filename, const char ** outDirspec)
97{
98int fd = 0;
99char dirSpec[512];
100
101// Try finding 'filename' in the usual places
102// Start searching any potential location for ACPI Table
103sprintf(dirSpec, "%s", filename);
104fd = open(dirSpec, 0);
105if (fd < 0)
106{
107sprintf(dirSpec, "/Extra/%s", filename);
108fd = open(dirSpec, 0);
109if (fd < 0)
110{
111sprintf(dirSpec, "bt(0,0)/Extra/%s", filename);
112fd = open(dirSpec, 0);
113}
114}
115
116if (fd < 0)
117{
118// NOT FOUND:
119verbose("ACPI Table not found: %s\n", filename);
120*dirSpec = '\0';
121}
122
123if (outDirspec) *outDirspec = dirSpec;
124return fd;
125}
126
127void *loadACPITable (const char * filename)
128{
129void *tableAddr;
130const char * dirspec=NULL;
131
132int fd = search_and_get_acpi_fd(filename, &dirspec);
133
134if (fd>=0)
135{
136tableAddr=(void*)AllocateKernelMemory(file_size (fd));
137if (tableAddr)
138{
139if (read (fd, tableAddr, file_size (fd))!=file_size (fd))
140{
141printf("Couldn't read table %s\n",dirspec);
142free (tableAddr);
143close (fd);
144return NULL;
145}
146
147DBG("Table %s read and stored at: %x\n", dirspec, tableAddr);
148close (fd);
149return tableAddr;
150}
151close (fd);
152printf("Couldn't allocate memory for table \n", dirspec);
153}
154//printf("Couldn't find table %s\n", filename);
155return NULL;
156}
157
158uint8_tacpi_cpu_count = 0;
159char* acpi_cpu_name[32];
160uint32_t acpi_cpu_p_blk = 0;
161
162void get_acpi_cpu_names(unsigned char* dsdt, uint32_t length)
163{
164uint32_t i;
165
166DBG("start finding cpu names. length %d\n", length);
167
168for (i=0; i<length-7; i++)
169{
170if (dsdt[i] == 0x5B && dsdt[i+1] == 0x83) // ProcessorOP
171{
172DBG("dsdt: %x%x\n", dsdt[i], dsdt[i+1]);
173
174uint32_t offset = i + 3 + (dsdt[i+2] >> 6);
175
176bool add_name = true;
177
178uint8_t j;
179
180for (j=0; j<4; j++)
181{
182char c = dsdt[offset+j];
183
184if (!aml_isvalidchar(c))
185{
186add_name = false;
187verbose("Invalid character found in ProcessorOP 0x%x!\n", c);
188break;
189}
190}
191
192if (add_name)
193{
194acpi_cpu_name[acpi_cpu_count] = malloc(4);
195memcpy(acpi_cpu_name[acpi_cpu_count], dsdt+offset, 4);
196i = offset + 5;
197
198if (acpi_cpu_count == 0)
199acpi_cpu_p_blk = dsdt[i] | (dsdt[i+1] << 8);
200
201verbose("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]);
202
203if (++acpi_cpu_count == 32)
204{
205return;
206}
207}
208}
209}
210
211DBG("end finding cpu names: cpu names found: %d\n", acpi_cpu_count);
212}
213
214struct acpi_2_ssdt *generate_cst_ssdt(struct acpi_2_fadt* fadt)
215{
216char ssdt_header[] =
217{
2180x53, 0x53, 0x44, 0x54, 0xE7, 0x00, 0x00, 0x00, /* SSDT.... */
2190x01, 0x17, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x41, /* ..PmRefA */
2200x43, 0x70, 0x75, 0x43, 0x73, 0x74, 0x00, 0x00, /* CpuCst.. */
2210x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* ....INTL */
2220x31, 0x03, 0x10, 0x20/* 1.._*/
223};
224
225char resource_template_register_fixedhw[] =
226{
2270x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F,
2280x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2290x00, 0x00, 0x01, 0x79, 0x00
230};
231
232char resource_template_register_systemio[] =
233{
2340x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x01,
2350x08, 0x00, 0x00, 0x15, 0x04, 0x00, 0x00, 0x00,
2360x00, 0x00, 0x00, 0x79, 0x00,
237};
238
239if (Platform.CPU.Vendor != 0x756E6547) {
240verbose ("Not an Intel platform: C-States will not be generated !!!\n");
241return NULL;
242}
243
244if (fadt == NULL) {
245verbose ("FACP not exists: C-States will not be generated !!!\n");
246return NULL;
247}
248
249struct acpi_2_dsdt* dsdt = (void*)fadt->DSDT;
250
251if (dsdt == NULL) {
252verbose ("DSDT not found: C-States will not be generated !!!\n");
253return NULL;
254}
255
256if (acpi_cpu_count == 0)
257get_acpi_cpu_names((void*)dsdt, dsdt->Length);
258
259if (acpi_cpu_count > 0)
260{
261bool c2_enabled = false;
262bool c3_enabled = false;
263bool c4_enabled = false;
264bool cst_using_systemio = false;
265
266getBoolForKey(kEnableC2State, &c2_enabled, &bootInfo->chameleonConfig);
267getBoolForKey(kEnableC3State, &c3_enabled, &bootInfo->chameleonConfig);
268getBoolForKey(kEnableC4State, &c4_enabled, &bootInfo->chameleonConfig);
269getBoolForKey(kCSTUsingSystemIO, &cst_using_systemio, &bootInfo->chameleonConfig);
270
271c2_enabled = c2_enabled | (fadt->C2_Latency < 100);
272c3_enabled = c3_enabled | (fadt->C3_Latency < 1000);
273
274unsigned char cstates_count = 1 + (c2_enabled ? 1 : 0) + (c3_enabled ? 1 : 0);
275
276struct aml_chunk* root = aml_create_node(NULL);
277aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
278struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
279struct aml_chunk* name = aml_add_name(scop, "CST_");
280struct aml_chunk* pack = aml_add_package(name);
281aml_add_byte(pack, cstates_count);
282
283struct aml_chunk* tmpl = aml_add_package(pack);
284if (cst_using_systemio)
285{
286// C1
287resource_template_register_fixedhw[8] = 0x00;
288resource_template_register_fixedhw[9] = 0x00;
289resource_template_register_fixedhw[18] = 0x00;
290aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
291aml_add_byte(tmpl, 0x01);// C1
292aml_add_word(tmpl, 0x0001);// Latency
293aml_add_dword(tmpl, 0x000003e8);// Power
294
295uint8_t p_blk_lo, p_blk_hi;
296
297if (c2_enabled) // C2
298{
299p_blk_lo = acpi_cpu_p_blk + 4;
300p_blk_hi = (acpi_cpu_p_blk + 4) >> 8;
301
302tmpl = aml_add_package(pack);
303resource_template_register_systemio[11] = p_blk_lo; // C2
304resource_template_register_systemio[12] = p_blk_hi; // C2
305aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
306aml_add_byte(tmpl, 0x02);// C2
307aml_add_word(tmpl, 0x0040);// Latency
308aml_add_dword(tmpl, 0x000001f4);// Power
309}
310
311if (c4_enabled) // C4
312{
313p_blk_lo = acpi_cpu_p_blk + 5;
314p_blk_hi = (acpi_cpu_p_blk + 5) >> 8;
315
316tmpl = aml_add_package(pack);
317resource_template_register_systemio[11] = p_blk_lo; // C4
318resource_template_register_systemio[12] = p_blk_hi; // C4
319aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
320aml_add_byte(tmpl, 0x04);// C4
321aml_add_word(tmpl, 0x0080);// Latency
322aml_add_dword(tmpl, 0x000000C8);// Power
323}
324else if (c3_enabled) // C3
325{
326p_blk_lo = acpi_cpu_p_blk + 5;
327p_blk_hi = (acpi_cpu_p_blk + 5) >> 8;
328
329tmpl = aml_add_package(pack);
330resource_template_register_systemio[11] = p_blk_lo; // C3
331resource_template_register_systemio[12] = p_blk_hi; // C3
332aml_add_buffer(tmpl, resource_template_register_systemio, sizeof(resource_template_register_systemio));
333aml_add_byte(tmpl, 0x03);// C3
334aml_add_word(tmpl, 0x0060);// Latency
335aml_add_dword(tmpl, 0x0000015e);// Power
336}
337}
338else
339{
340// C1
341resource_template_register_fixedhw[8] = 0x01;
342resource_template_register_fixedhw[9] = 0x02;
343resource_template_register_fixedhw[18] = 0x01;
344
345resource_template_register_fixedhw[11] = 0x00; // C1
346aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
347aml_add_byte(tmpl, 0x01);// C1
348aml_add_word(tmpl, 0x0001);// Latency
349aml_add_dword(tmpl, 0x000003e8);// Power
350
351resource_template_register_fixedhw[18] = 0x03;
352
353if (c2_enabled) // C2
354{
355tmpl = aml_add_package(pack);
356resource_template_register_fixedhw[11] = 0x10; // C2
357aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
358aml_add_byte(tmpl, 0x02);// C2
359aml_add_word(tmpl, 0x0040);// Latency
360aml_add_dword(tmpl, 0x000001f4);// Power
361}
362
363if (c4_enabled) // C4
364{
365tmpl = aml_add_package(pack);
366resource_template_register_fixedhw[11] = 0x30; // C4
367aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
368aml_add_byte(tmpl, 0x04);// C4
369aml_add_word(tmpl, 0x0080);// Latency
370aml_add_dword(tmpl, 0x000000C8);// Power
371}
372else if (c3_enabled)
373{
374tmpl = aml_add_package(pack);
375resource_template_register_fixedhw[11] = 0x20; // C3
376aml_add_buffer(tmpl, resource_template_register_fixedhw, sizeof(resource_template_register_fixedhw));
377aml_add_byte(tmpl, 0x03);// C3
378aml_add_word(tmpl, 0x0060);// Latency
379aml_add_dword(tmpl, 0x0000015e);// Power
380}
381}
382
383// Aliaces
384int i;
385for (i = 0; i < acpi_cpu_count; i++)
386{
387char name[9];
388sprintf(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]);
389
390scop = aml_add_scope(root, name);
391aml_add_alias(scop, "CST_", "_CST");
392}
393
394aml_calculate_size(root);
395
396struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
397
398aml_write_node(root, (void*)ssdt, 0);
399
400ssdt->Length = root->Size;
401ssdt->Checksum = 0;
402ssdt->Checksum = 256 - checksum8(ssdt, ssdt->Length);
403
404aml_destroy_node(root);
405
406// dumpPhysAddr("C-States SSDT content: ", ssdt, ssdt->Length);
407
408verbose ("SSDT with CPU C-States generated successfully\n");
409
410return ssdt;
411}
412else
413{
414verbose ("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{
422char 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)
432{
433verbose ("Not an Intel platform: P-States will not be generated !!!\n");
434return NULL;
435}
436
437if (!(Platform.CPU.Features & CPU_FEATURE_MSR))
438{
439verbose ("Unsupported CPU: P-States will not be generated !!! No MSR support\n");
440return NULL;
441}
442
443if (acpi_cpu_count == 0)
444get_acpi_cpu_names((void*)dsdt, dsdt->Length);
445
446if (acpi_cpu_count > 0)
447{
448struct p_state initial, maximum, minimum, p_states[32];
449uint8_t p_states_count = 0;
450
451// Retrieving P-States, ported from code by superhai (c)
452switch (Platform.CPU.Family) {
453case 0x06:
454{
455switch (Platform.CPU.Model)
456{
457case CPU_MODEL_DOTHAN:// Intel Pentium M
458case CPU_MODEL_YONAH:// Intel Mobile Core Solo, Duo
459case CPU_MODEL_MEROM:// Intel Mobile Core 2 Solo, Duo, Xeon 30xx, Xeon 51xx, Xeon X53xx, Xeon E53xx, Xeon X32xx
460case CPU_MODEL_PENRYN:// Intel Core 2 Solo, Duo, Quad, Extreme, Xeon X54xx, Xeon X33xx
461case CPU_MODEL_ATOM:// Intel Atom (45nm)
462{
463bool cpu_dynamic_fsb = false;
464
465if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27))
466{
467wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28)));
468delay(1);
469cpu_dynamic_fsb = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);
470}
471
472bool cpu_noninteger_bus_ratio = (rdmsr64(MSR_IA32_PERF_STATUS) & (1ULL << 46));
473
474initial.Control = rdmsr64(MSR_IA32_PERF_STATUS);
475
476maximum.Control = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 32) & 0x1F3F) | (0x4000 * cpu_noninteger_bus_ratio);
477maximum.CID = ((maximum.FID & 0x1F) << 1) | cpu_noninteger_bus_ratio;
478
479minimum.FID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 24) & 0x1F) | (0x80 * cpu_dynamic_fsb);
480minimum.VID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 48) & 0x3F);
481
482if (minimum.FID == 0)
483{
484uint64_t msr;
485uint8_t i;
486// Probe for lowest fid
487for (i = maximum.FID; i >= 0x6; i--)
488{
489msr = rdmsr64(MSR_IA32_PERF_CONTROL);
490wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (i << 8) | minimum.VID);
491intel_waitforsts();
492minimum.FID = (rdmsr64(MSR_IA32_PERF_STATUS) >> 8) & 0x1F;
493delay(1);
494}
495
496msr = rdmsr64(MSR_IA32_PERF_CONTROL);
497wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
498intel_waitforsts();
499}
500
501if (minimum.VID == maximum.VID)
502{
503uint64_t msr;
504uint8_t i;
505// Probe for lowest vid
506for (i = maximum.VID; i > 0xA; i--)
507{
508msr = rdmsr64(MSR_IA32_PERF_CONTROL);
509wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (minimum.FID << 8) | i);
510intel_waitforsts();
511minimum.VID = rdmsr64(MSR_IA32_PERF_STATUS) & 0x3F;
512delay(1);
513}
514
515msr = rdmsr64(MSR_IA32_PERF_CONTROL);
516wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
517intel_waitforsts();
518}
519
520minimum.CID = ((minimum.FID & 0x1F) << 1) >> cpu_dynamic_fsb;
521
522// Sanity check
523if (maximum.CID < minimum.CID)
524{
525DBG("P-States: Insane FID values!");
526p_states_count = 0;
527}
528else
529{
530// Finalize P-States
531// Find how many P-States machine supports
532p_states_count = maximum.CID - minimum.CID + 1;
533
534if (p_states_count > 32)
535p_states_count = 32;
536
537uint8_t vidstep;
538uint8_t i = 0, u, invalid = 0;
539
540vidstep = ((maximum.VID << 2) - (minimum.VID << 2)) / (p_states_count - 1);
541
542for (u = 0; u < p_states_count; u++)
543{
544i = u - invalid;
545
546p_states[i].CID = maximum.CID - u;
547p_states[i].FID = (p_states[i].CID >> 1);
548
549if (p_states[i].FID < 0x6)
550{
551if (cpu_dynamic_fsb)
552p_states[i].FID = (p_states[i].FID << 1) | 0x80;
553}
554else if (cpu_noninteger_bus_ratio)
555{
556p_states[i].FID = p_states[i].FID | (0x40 * (p_states[i].CID & 0x1));
557}
558
559if (i && p_states[i].FID == p_states[i-1].FID)
560invalid++;
561
562p_states[i].VID = ((maximum.VID << 2) - (vidstep * u)) >> 2;
563
564uint32_t multiplier = p_states[i].FID & 0x1f;// = 0x08
565bool half = p_states[i].FID & 0x40;// = 0x01
566bool dfsb = p_states[i].FID & 0x80;// = 0x00
567uint32_t fsb = Platform.CPU.FSBFrequency / 1000000; // = 400
568uint32_t halffsb = (fsb + 1) >> 1;// = 200
569uint32_t frequency = (multiplier * fsb);// = 3200
570
571p_states[i].Frequency = (frequency + (half * halffsb)) >> dfsb;// = 3200 + 200 = 3400
572}
573
574p_states_count -= invalid;
575}
576
577break;
578}
579case CPU_MODEL_FIELDS:// Intel Core i5, i7, Xeon X34xx LGA1156 (45nm)
580case CPU_MODEL_DALES:
581case CPU_MODEL_CLARKDALE:// Intel Core i3, i5 LGA1156 (32nm)
582case CPU_MODEL_NEHALEM:// Intel Core i7, Xeon W35xx, Xeon X55xx, Xeon E55xx LGA1366 (45nm)
583case CPU_MODEL_NEHALEM_EX:// Intel Xeon X75xx, Xeon X65xx, Xeon E75xx, Xeon E65xx
584case CPU_MODEL_WESTMERE:// Intel Core i7, Xeon X56xx, Xeon E56xx, Xeon W36xx LGA1366 (32nm) 6 Core
585case CPU_MODEL_WESTMERE_EX:// Intel Xeon E7
586case CPU_MODEL_SANDYBRIDGE:// Intel Core i3, i5, i7 LGA1155 (32nm)
587case CPU_MODEL_IVYBRIDGE:// Intel Core i3, i5, i7 LGA1155 (22nm)
588case CPU_MODEL_IVYBRIDGE_E5:
589case CPU_MODEL_JAKETOWN:// Intel Core i7, Xeon E5 LGA2011 (32nm)
590
591{
592if ((Platform.CPU.Model == CPU_MODEL_SANDYBRIDGE) || (Platform.CPU.Model == CPU_MODEL_IVYBRIDGE) || (Platform.CPU.Model == CPU_MODEL_IVYBRIDGE_E5) ||(Platform.CPU.Model == CPU_MODEL_JAKETOWN))
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
639struct aml_chunk* root = aml_create_node(NULL);
640aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
641
642struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
643struct aml_chunk* name = aml_add_name(scop, "PSS_");
644struct aml_chunk* pack = aml_add_package(name);
645
646for (i = 0; i < p_states_count; i++)
647{
648struct aml_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
852const char *filename;
853char dirSpec[128];
854int len = 0;
855
856// always reset cpu count to 0 when injecting new acpi
857acpi_cpu_count = 0;
858
859// Try using the file specified with the DSDT option
860if (getValueForKey(kDSDT, &filename, &len, &bootInfo->chameleonConfig))
861{
862sprintf(dirSpec, filename);
863}
864else
865{
866sprintf(dirSpec, "DSDT.aml");
867}
868
869// Load replacement DSDT
870new_dsdt = loadACPITable(dirSpec);
871// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present
872/*if (!new_dsdt)
873 {
874 return setupAcpiNoMod();
875 }*/
876
877// Mozodojo: Load additional SSDTs
878struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst
879int ssdt_count=0;
880
881// SSDT Options
882bool drop_ssdt=false, generate_pstates=false, generate_cstates=false;
883
884getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->chameleonConfig);
885getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->chameleonConfig);
886getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->chameleonConfig);
887
888DBG("generating p-states config: %d\n", generate_pstates);
889DBG("generating c-states config: %d\n", generate_cstates);
890
891{
892int i;
893
894for (i=0; i<30; i++)
895{
896char filename[512];
897
898sprintf(filename, i>0?"SSDT-%d.aml":"SSDT.aml", i);
899
900if ( (new_ssdt[ssdt_count] = loadACPITable(filename)) )
901{
902ssdt_count++;
903}
904else
905{
906break;
907}
908}
909}
910
911// Do the same procedure for both versions of ACPI
912for (version=0; version<2; version++)
913{
914struct acpi_2_rsdp *rsdp, *rsdp_mod;
915struct acpi_2_rsdt *rsdt, *rsdt_mod;
916int rsdplength;
917
918// Find original rsdp
919rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
920if (!rsdp)
921{
922DBG("No ACPI version %d found. Ignoring\n", version+1);
923if (version)
924{
925addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
926}
927else
928{
929addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
930}
931continue;
932}
933rsdplength=version?rsdp->Length:20;
934
935DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
936
937/* FIXME: no check that memory allocation succeeded
938 * Copy and patch RSDP,RSDT, XSDT and FADT
939 * For more info see ACPI Specification pages 110 and following
940 */
941
942rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
943memcpy(rsdp_mod, rsdp, rsdplength);
944
945rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
946
947DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
948
949if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
950{
951uint32_t *rsdt_entries;
952int rsdt_entries_num;
953int dropoffset=0, i;
954
955// mozo: using malloc cos I didn't found how to free already allocated kernel memory
956rsdt_mod=(struct acpi_2_rsdt *)malloc(rsdt->Length);
957memcpy (rsdt_mod, rsdt, rsdt->Length);
958rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
959rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
960rsdt_entries=(uint32_t *)(rsdt_mod+1);
961for (i=0;i<rsdt_entries_num;i++)
962{
963char *table=(char *)(rsdt_entries[i]);
964if (!table)
965{
966continue;
967}
968
969DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
970
971rsdt_entries[i-dropoffset]=rsdt_entries[i];
972
973if (drop_ssdt && tableSign(table, "SSDT"))
974{
975dropoffset++;
976continue;
977}
978if (tableSign(table, "DSDT"))
979{
980DBG("DSDT found\n");
981
982if(new_dsdt)
983{
984rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
985}
986
987continue;
988}
989if (tableSign(table, "FACP"))
990{
991struct acpi_2_fadt *fadt, *fadt_mod;
992fadt=(struct acpi_2_fadt *)rsdt_entries[i];
993
994DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
995
996if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
997{
998printf("FADT incorrect. Not modified\n");
999continue;
1000}
1001
1002fadt_mod = patch_fadt(fadt, new_dsdt);
1003rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1004
1005// Generate _CST SSDT
1006if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1007{
1008DBG("C-States generated\n");
1009generate_cstates = false; // Generate SSDT only once!
1010ssdt_count++;
1011}
1012
1013// Generating _PSS SSDT
1014if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1015{
1016DBG("P-States generated\n");
1017generate_pstates = false; // Generate SSDT only once!
1018ssdt_count++;
1019}
1020
1021continue;
1022}
1023}
1024DBG("\n");
1025
1026// Allocate rsdt in Kernel memory area
1027rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;
1028struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);
1029memcpy (rsdt_copy, rsdt_mod, rsdt_mod->Length);
1030free(rsdt_mod); rsdt_mod = rsdt_copy;
1031rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
1032rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
1033rsdt_entries=(uint32_t *)(rsdt_mod+1);
1034
1035// Mozodojo: Insert additional SSDTs into RSDT
1036if(ssdt_count>0)
1037{
1038int j;
1039
1040for (j=0; j<ssdt_count; j++)
1041rsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1042
1043verbose("RSDT: Added %d SSDT table(s)\n", ssdt_count);
1044
1045}
1046
1047// Correct the checksum of RSDT
1048DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
1049
1050rsdt_mod->Checksum=0;
1051rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
1052
1053DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
1054}
1055else
1056{
1057rsdp_mod->RsdtAddress=0;
1058printf("RSDT not found or incorrect\n");
1059}
1060
1061if (version)
1062{
1063struct acpi_2_xsdt *xsdt, *xsdt_mod;
1064
1065// FIXME: handle 64-bit address correctly
1066
1067xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
1068DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress, xsdt->Length);
1069
1070if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
1071{
1072uint64_t *xsdt_entries;
1073int xsdt_entries_num, i;
1074int dropoffset=0;
1075
1076// mozo: using malloc cos I didn't found how to free already allocated kernel memory
1077xsdt_mod=(struct acpi_2_xsdt*)malloc(xsdt->Length);
1078memcpy(xsdt_mod, xsdt, xsdt->Length);
1079
1080rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1081xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1082xsdt_entries=(uint64_t *)(xsdt_mod+1);
1083for (i=0;i<xsdt_entries_num;i++)
1084{
1085char *table=(char *)((uint32_t)(xsdt_entries[i]));
1086if (!table)
1087{
1088continue;
1089}
1090xsdt_entries[i-dropoffset]=xsdt_entries[i];
1091
1092if (drop_ssdt && tableSign(table, "SSDT"))
1093{
1094dropoffset++;
1095continue;
1096}
1097if (tableSign(table, "DSDT"))
1098{
1099DBG("DSDT found\n");
1100
1101if (new_dsdt)
1102{
1103xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
1104}
1105
1106DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1107
1108continue;
1109}
1110if (tableSign(table, "FACP"))
1111{
1112struct acpi_2_fadt *fadt, *fadt_mod;
1113fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
1114
1115DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
1116fadt->Length);
1117
1118if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
1119{
1120verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
1121goto drop_xsdt;
1122}
1123
1124fadt_mod = patch_fadt(fadt, new_dsdt);
1125xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
1126
1127DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1128
1129// Generate _CST SSDT
1130if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))
1131{
1132generate_cstates = false; // Generate SSDT only once!
1133ssdt_count++;
1134}
1135
1136// Generating _PSS SSDT
1137if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
1138{
1139generate_pstates = false; // Generate SSDT only once!
1140ssdt_count++;
1141}
1142
1143continue;
1144}
1145
1146DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
1147
1148}
1149
1150// Allocate xsdt in Kernel memory area
1151xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;
1152struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);
1153memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);
1154free(xsdt_mod); xsdt_mod = xsdt_copy;
1155rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
1156xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
1157xsdt_entries=(uint64_t *)(xsdt_mod+1);
1158
1159// Mozodojo: Insert additional SSDTs into XSDT
1160if(ssdt_count>0)
1161{
1162int j;
1163
1164for (j=0; j<ssdt_count; j++)
1165xsdt_entries[i-dropoffset+j]=(uint32_t)new_ssdt[j];
1166
1167verbose("Added %d SSDT table(s) into XSDT\n", ssdt_count);
1168
1169}
1170
1171// Correct the checksum of XSDT
1172xsdt_mod->Checksum=0;
1173xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
1174}
1175else
1176{
1177drop_xsdt:
1178
1179DBG("About to drop XSDT\n");
1180
1181/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
1182 * A Better strategy would be to generate
1183 */
1184
1185rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
1186verbose("XSDT not found or incorrect\n");
1187}
1188}
1189
1190// Correct the checksum of RSDP
1191
1192DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
1193
1194rsdp_mod->Checksum=0;
1195rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
1196
1197DBG("New checksum %d\n", rsdp_mod->Checksum);
1198
1199if (version)
1200{
1201DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
1202
1203rsdp_mod->ExtendedChecksum=0;
1204rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
1205
1206DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
1207
1208}
1209
1210//verbose("Patched ACPI version %d DSDT\n", version+1);
1211if (version)
1212{
1213/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1214acpi20_p = (uint32_t)rsdp_mod;
1215addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
1216}
1217else
1218{
1219/* XXX aserebln why uint32 cast if pointer is uint64 ? */
1220acpi10_p = (uint32_t)rsdp_mod;
1221addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
1222}
1223}
1224#if DEBUG_ACPI
1225printf("Press a key to continue... (DEBUG_ACPI)\n");
1226getchar();
1227#endif
1228return 1;
1229}
1230

Archive Download this file

Revision: 2131