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