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