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