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