]>
Commit | Line | Data |
---|---|---|
a47463f2 | 1 | /** @file\r |
2 | CPU DXE Module.\r | |
3 | \r | |
e41aad15 | 4 | Copyright (c) 2008 - 2013, Intel Corporation. All rights reserved.<BR>\r |
01a1c0fc | 5 | This program and the accompanying materials\r |
a47463f2 | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "CpuDxe.h"\r | |
6022e28c | 16 | #include "CpuMp.h"\r |
a47463f2 | 17 | \r |
18 | //\r | |
19 | // Global Variables\r | |
20 | //\r | |
a47463f2 | 21 | BOOLEAN InterruptState = FALSE;\r |
22 | EFI_HANDLE mCpuHandle = NULL;\r | |
23 | BOOLEAN mIsFlushingGCD;\r | |
a47463f2 | 24 | UINT64 mValidMtrrAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS;\r |
25 | UINT64 mValidMtrrBitsMask = MTRR_LIB_MSR_VALID_MASK;\r | |
26 | \r | |
27 | FIXED_MTRR mFixedMtrrTable[] = {\r | |
28 | {\r | |
29 | MTRR_LIB_IA32_MTRR_FIX64K_00000,\r | |
30 | 0,\r | |
31 | 0x10000\r | |
32 | },\r | |
33 | {\r | |
34 | MTRR_LIB_IA32_MTRR_FIX16K_80000,\r | |
35 | 0x80000,\r | |
36 | 0x4000\r | |
37 | },\r | |
38 | {\r | |
39 | MTRR_LIB_IA32_MTRR_FIX16K_A0000,\r | |
40 | 0xA0000,\r | |
41 | 0x4000\r | |
42 | },\r | |
43 | {\r | |
44 | MTRR_LIB_IA32_MTRR_FIX4K_C0000,\r | |
45 | 0xC0000,\r | |
46 | 0x1000\r | |
47 | },\r | |
48 | {\r | |
49 | MTRR_LIB_IA32_MTRR_FIX4K_C8000,\r | |
50 | 0xC8000,\r | |
51 | 0x1000\r | |
52 | },\r | |
53 | {\r | |
54 | MTRR_LIB_IA32_MTRR_FIX4K_D0000,\r | |
55 | 0xD0000,\r | |
56 | 0x1000\r | |
57 | },\r | |
58 | {\r | |
59 | MTRR_LIB_IA32_MTRR_FIX4K_D8000,\r | |
60 | 0xD8000,\r | |
61 | 0x1000\r | |
62 | },\r | |
63 | {\r | |
64 | MTRR_LIB_IA32_MTRR_FIX4K_E0000,\r | |
65 | 0xE0000,\r | |
66 | 0x1000\r | |
67 | },\r | |
68 | {\r | |
69 | MTRR_LIB_IA32_MTRR_FIX4K_E8000,\r | |
70 | 0xE8000,\r | |
71 | 0x1000\r | |
72 | },\r | |
73 | {\r | |
74 | MTRR_LIB_IA32_MTRR_FIX4K_F0000,\r | |
75 | 0xF0000,\r | |
76 | 0x1000\r | |
77 | },\r | |
78 | {\r | |
79 | MTRR_LIB_IA32_MTRR_FIX4K_F8000,\r | |
80 | 0xF8000,\r | |
81 | 0x1000\r | |
82 | },\r | |
83 | };\r | |
84 | \r | |
85 | \r | |
86 | EFI_CPU_ARCH_PROTOCOL gCpu = {\r | |
87 | CpuFlushCpuDataCache,\r | |
88 | CpuEnableInterrupt,\r | |
89 | CpuDisableInterrupt,\r | |
90 | CpuGetInterruptState,\r | |
91 | CpuInit,\r | |
92 | CpuRegisterInterruptHandler,\r | |
93 | CpuGetTimerValue,\r | |
94 | CpuSetMemoryAttributes,\r | |
95 | 1, // NumberOfTimers\r | |
96 | 4 // DmaBufferAlignment\r | |
97 | };\r | |
98 | \r | |
a47463f2 | 99 | //\r |
100 | // CPU Arch Protocol Functions\r | |
101 | //\r | |
102 | \r | |
a47463f2 | 103 | /**\r |
104 | Flush CPU data cache. If the instruction cache is fully coherent\r | |
105 | with all DMA operations then function can just return EFI_SUCCESS.\r | |
106 | \r | |
107 | @param This Protocol instance structure\r | |
108 | @param Start Physical address to start flushing from.\r | |
109 | @param Length Number of bytes to flush. Round up to chipset\r | |
110 | granularity.\r | |
111 | @param FlushType Specifies the type of flush operation to perform.\r | |
112 | \r | |
113 | @retval EFI_SUCCESS If cache was flushed\r | |
114 | @retval EFI_UNSUPPORTED If flush type is not supported.\r | |
115 | @retval EFI_DEVICE_ERROR If requested range could not be flushed.\r | |
116 | \r | |
117 | **/\r | |
118 | EFI_STATUS\r | |
119 | EFIAPI\r | |
120 | CpuFlushCpuDataCache (\r | |
121 | IN EFI_CPU_ARCH_PROTOCOL *This,\r | |
122 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
123 | IN UINT64 Length,\r | |
124 | IN EFI_CPU_FLUSH_TYPE FlushType\r | |
125 | )\r | |
126 | {\r | |
127 | if (FlushType == EfiCpuFlushTypeWriteBackInvalidate) {\r | |
128 | AsmWbinvd ();\r | |
129 | return EFI_SUCCESS;\r | |
130 | } else if (FlushType == EfiCpuFlushTypeInvalidate) {\r | |
131 | AsmInvd ();\r | |
132 | return EFI_SUCCESS;\r | |
133 | } else {\r | |
134 | return EFI_UNSUPPORTED;\r | |
135 | }\r | |
136 | }\r | |
137 | \r | |
138 | \r | |
139 | /**\r | |
140 | Enables CPU interrupts.\r | |
141 | \r | |
142 | @param This Protocol instance structure\r | |
143 | \r | |
144 | @retval EFI_SUCCESS If interrupts were enabled in the CPU\r | |
145 | @retval EFI_DEVICE_ERROR If interrupts could not be enabled on the CPU.\r | |
146 | \r | |
147 | **/\r | |
148 | EFI_STATUS\r | |
149 | EFIAPI\r | |
150 | CpuEnableInterrupt (\r | |
151 | IN EFI_CPU_ARCH_PROTOCOL *This\r | |
152 | )\r | |
153 | {\r | |
154 | EnableInterrupts ();\r | |
155 | \r | |
156 | InterruptState = TRUE;\r | |
157 | return EFI_SUCCESS;\r | |
158 | }\r | |
159 | \r | |
160 | \r | |
161 | /**\r | |
162 | Disables CPU interrupts.\r | |
163 | \r | |
164 | @param This Protocol instance structure\r | |
165 | \r | |
166 | @retval EFI_SUCCESS If interrupts were disabled in the CPU.\r | |
167 | @retval EFI_DEVICE_ERROR If interrupts could not be disabled on the CPU.\r | |
168 | \r | |
169 | **/\r | |
170 | EFI_STATUS\r | |
171 | EFIAPI\r | |
172 | CpuDisableInterrupt (\r | |
173 | IN EFI_CPU_ARCH_PROTOCOL *This\r | |
174 | )\r | |
175 | {\r | |
176 | DisableInterrupts ();\r | |
177 | \r | |
178 | InterruptState = FALSE;\r | |
179 | return EFI_SUCCESS;\r | |
180 | }\r | |
181 | \r | |
182 | \r | |
183 | /**\r | |
184 | Return the state of interrupts.\r | |
185 | \r | |
186 | @param This Protocol instance structure\r | |
187 | @param State Pointer to the CPU's current interrupt state\r | |
188 | \r | |
189 | @retval EFI_SUCCESS If interrupts were disabled in the CPU.\r | |
190 | @retval EFI_INVALID_PARAMETER State is NULL.\r | |
191 | \r | |
192 | **/\r | |
193 | EFI_STATUS\r | |
194 | EFIAPI\r | |
195 | CpuGetInterruptState (\r | |
196 | IN EFI_CPU_ARCH_PROTOCOL *This,\r | |
197 | OUT BOOLEAN *State\r | |
198 | )\r | |
199 | {\r | |
200 | if (State == NULL) {\r | |
201 | return EFI_INVALID_PARAMETER;\r | |
202 | }\r | |
203 | \r | |
204 | *State = InterruptState;\r | |
205 | return EFI_SUCCESS;\r | |
206 | }\r | |
207 | \r | |
208 | \r | |
209 | /**\r | |
210 | Generates an INIT to the CPU.\r | |
211 | \r | |
212 | @param This Protocol instance structure\r | |
213 | @param InitType Type of CPU INIT to perform\r | |
214 | \r | |
215 | @retval EFI_SUCCESS If CPU INIT occurred. This value should never be\r | |
216 | seen.\r | |
217 | @retval EFI_DEVICE_ERROR If CPU INIT failed.\r | |
218 | @retval EFI_UNSUPPORTED Requested type of CPU INIT not supported.\r | |
219 | \r | |
220 | **/\r | |
221 | EFI_STATUS\r | |
222 | EFIAPI\r | |
223 | CpuInit (\r | |
224 | IN EFI_CPU_ARCH_PROTOCOL *This,\r | |
225 | IN EFI_CPU_INIT_TYPE InitType\r | |
226 | )\r | |
227 | {\r | |
228 | return EFI_UNSUPPORTED;\r | |
229 | }\r | |
230 | \r | |
231 | \r | |
232 | /**\r | |
233 | Registers a function to be called from the CPU interrupt handler.\r | |
234 | \r | |
235 | @param This Protocol instance structure\r | |
236 | @param InterruptType Defines which interrupt to hook. IA-32\r | |
237 | valid range is 0x00 through 0xFF\r | |
238 | @param InterruptHandler A pointer to a function of type\r | |
239 | EFI_CPU_INTERRUPT_HANDLER that is called\r | |
240 | when a processor interrupt occurs. A null\r | |
241 | pointer is an error condition.\r | |
242 | \r | |
243 | @retval EFI_SUCCESS If handler installed or uninstalled.\r | |
244 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler\r | |
245 | for InterruptType was previously installed.\r | |
246 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for\r | |
247 | InterruptType was not previously installed.\r | |
248 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType\r | |
249 | is not supported.\r | |
250 | \r | |
251 | **/\r | |
252 | EFI_STATUS\r | |
253 | EFIAPI\r | |
254 | CpuRegisterInterruptHandler (\r | |
255 | IN EFI_CPU_ARCH_PROTOCOL *This,\r | |
256 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
257 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
258 | )\r | |
259 | {\r | |
e41aad15 | 260 | return RegisterCpuInterruptHandler (InterruptType, InterruptHandler);\r |
a47463f2 | 261 | }\r |
262 | \r | |
263 | \r | |
264 | /**\r | |
265 | Returns a timer value from one of the CPU's internal timers. There is no\r | |
266 | inherent time interval between ticks but is a function of the CPU frequency.\r | |
267 | \r | |
268 | @param This - Protocol instance structure.\r | |
269 | @param TimerIndex - Specifies which CPU timer is requested.\r | |
270 | @param TimerValue - Pointer to the returned timer value.\r | |
271 | @param TimerPeriod - A pointer to the amount of time that passes\r | |
272 | in femtoseconds (10-15) for each increment\r | |
273 | of TimerValue. If TimerValue does not\r | |
274 | increment at a predictable rate, then 0 is\r | |
275 | returned. The amount of time that has\r | |
276 | passed between two calls to GetTimerValue()\r | |
277 | can be calculated with the formula\r | |
278 | (TimerValue2 - TimerValue1) * TimerPeriod.\r | |
279 | This parameter is optional and may be NULL.\r | |
280 | \r | |
281 | @retval EFI_SUCCESS - If the CPU timer count was returned.\r | |
282 | @retval EFI_UNSUPPORTED - If the CPU does not have any readable timers.\r | |
283 | @retval EFI_DEVICE_ERROR - If an error occurred while reading the timer.\r | |
284 | @retval EFI_INVALID_PARAMETER - TimerIndex is not valid or TimerValue is NULL.\r | |
285 | \r | |
286 | **/\r | |
287 | EFI_STATUS\r | |
288 | EFIAPI\r | |
289 | CpuGetTimerValue (\r | |
290 | IN EFI_CPU_ARCH_PROTOCOL *This,\r | |
291 | IN UINT32 TimerIndex,\r | |
292 | OUT UINT64 *TimerValue,\r | |
293 | OUT UINT64 *TimerPeriod OPTIONAL\r | |
294 | )\r | |
295 | {\r | |
296 | if (TimerValue == NULL) {\r | |
297 | return EFI_INVALID_PARAMETER;\r | |
298 | }\r | |
299 | \r | |
300 | if (TimerIndex != 0) {\r | |
301 | return EFI_INVALID_PARAMETER;\r | |
302 | }\r | |
303 | \r | |
304 | *TimerValue = AsmReadTsc ();\r | |
305 | \r | |
306 | if (TimerPeriod != NULL) {\r | |
307 | //\r | |
308 | // BugBug: Hard coded. Don't know how to do this generically\r | |
309 | //\r | |
310 | *TimerPeriod = 1000000000;\r | |
311 | }\r | |
312 | \r | |
313 | return EFI_SUCCESS;\r | |
314 | }\r | |
315 | \r | |
316 | \r | |
317 | /**\r | |
4ec21e8b | 318 | Implementation of SetMemoryAttributes() service of CPU Architecture Protocol.\r |
319 | \r | |
320 | This function modifies the attributes for the memory region specified by BaseAddress and\r | |
321 | Length from their current attributes to the attributes specified by Attributes.\r | |
322 | \r | |
323 | @param This The EFI_CPU_ARCH_PROTOCOL instance.\r | |
324 | @param BaseAddress The physical address that is the start address of a memory region.\r | |
325 | @param Length The size in bytes of the memory region.\r | |
326 | @param Attributes The bit mask of attributes to set for the memory region.\r | |
327 | \r | |
328 | @retval EFI_SUCCESS The attributes were set for the memory region.\r | |
329 | @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
330 | BaseAddress and Length cannot be modified.\r | |
331 | @retval EFI_INVALID_PARAMETER Length is zero.\r | |
332 | Attributes specified an illegal combination of attributes that\r | |
333 | cannot be set together.\r | |
334 | @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
335 | the memory resource range.\r | |
336 | @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r | |
337 | resource range specified by BaseAddress and Length.\r | |
338 | The bit mask of attributes is not support for the memory resource\r | |
339 | range specified by BaseAddress and Length.\r | |
a47463f2 | 340 | \r |
341 | **/\r | |
342 | EFI_STATUS\r | |
343 | EFIAPI\r | |
344 | CpuSetMemoryAttributes (\r | |
345 | IN EFI_CPU_ARCH_PROTOCOL *This,\r | |
346 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
347 | IN UINT64 Length,\r | |
348 | IN UINT64 Attributes\r | |
349 | )\r | |
350 | {\r | |
351 | RETURN_STATUS Status;\r | |
352 | MTRR_MEMORY_CACHE_TYPE CacheType;\r | |
353 | \r | |
947a573a | 354 | if (!IsMtrrSupported ()) {\r |
355 | return EFI_UNSUPPORTED;\r | |
356 | }\r | |
357 | \r | |
a47463f2 | 358 | //\r |
359 | // If this function is called because GCD SetMemorySpaceAttributes () is called\r | |
360 | // by RefreshGcdMemoryAttributes (), then we are just synchronzing GCD memory\r | |
361 | // map with MTRR values. So there is no need to modify MTRRs, just return immediately\r | |
362 | // to avoid unnecessary computing.\r | |
363 | //\r | |
364 | if (mIsFlushingGCD) {\r | |
79aca636 SEHM |
365 | DEBUG((EFI_D_INFO, " Flushing GCD\n"));\r |
366 | return EFI_SUCCESS;\r | |
367 | }\r | |
a47463f2 | 368 | \r |
369 | switch (Attributes) {\r | |
370 | case EFI_MEMORY_UC:\r | |
371 | CacheType = CacheUncacheable;\r | |
372 | break;\r | |
373 | \r | |
374 | case EFI_MEMORY_WC:\r | |
375 | CacheType = CacheWriteCombining;\r | |
376 | break;\r | |
377 | \r | |
378 | case EFI_MEMORY_WT:\r | |
379 | CacheType = CacheWriteThrough;\r | |
380 | break;\r | |
381 | \r | |
382 | case EFI_MEMORY_WP:\r | |
383 | CacheType = CacheWriteProtected;\r | |
384 | break;\r | |
385 | \r | |
386 | case EFI_MEMORY_WB:\r | |
387 | CacheType = CacheWriteBack;\r | |
388 | break;\r | |
389 | \r | |
4ec21e8b | 390 | case EFI_MEMORY_UCE:\r |
391 | case EFI_MEMORY_RP:\r | |
392 | case EFI_MEMORY_XP:\r | |
393 | case EFI_MEMORY_RUNTIME:\r | |
a47463f2 | 394 | return EFI_UNSUPPORTED;\r |
4ec21e8b | 395 | \r |
396 | default:\r | |
397 | return EFI_INVALID_PARAMETER;\r | |
a47463f2 | 398 | }\r |
399 | //\r | |
400 | // call MTRR libary function\r | |
401 | //\r | |
f877f300 | 402 | Status = MtrrSetMemoryAttribute (\r |
a47463f2 | 403 | BaseAddress,\r |
404 | Length,\r | |
405 | CacheType\r | |
406 | );\r | |
407 | \r | |
a47463f2 | 408 | return (EFI_STATUS) Status;\r |
409 | }\r | |
410 | \r | |
411 | /**\r | |
412 | Initializes the valid bits mask and valid address mask for MTRRs.\r | |
413 | \r | |
414 | This function initializes the valid bits mask and valid address mask for MTRRs.\r | |
415 | \r | |
416 | **/\r | |
417 | VOID\r | |
418 | InitializeMtrrMask (\r | |
419 | VOID\r | |
420 | )\r | |
421 | {\r | |
422 | UINT32 RegEax;\r | |
423 | UINT8 PhysicalAddressBits;\r | |
424 | \r | |
425 | AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r | |
426 | \r | |
427 | if (RegEax >= 0x80000008) {\r | |
428 | AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r | |
429 | \r | |
430 | PhysicalAddressBits = (UINT8) RegEax;\r | |
431 | \r | |
432 | mValidMtrrBitsMask = LShiftU64 (1, PhysicalAddressBits) - 1;\r | |
433 | mValidMtrrAddressMask = mValidMtrrBitsMask & 0xfffffffffffff000ULL;\r | |
434 | } else {\r | |
435 | mValidMtrrBitsMask = MTRR_LIB_MSR_VALID_MASK;\r | |
436 | mValidMtrrAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS;\r | |
437 | }\r | |
438 | }\r | |
439 | \r | |
440 | /**\r | |
430fbbe0 | 441 | Gets GCD Mem Space type from MTRR Type.\r |
a47463f2 | 442 | \r |
430fbbe0 | 443 | This function gets GCD Mem Space type from MTRR Type.\r |
a47463f2 | 444 | \r |
430fbbe0 | 445 | @param MtrrAttributes MTRR memory type\r |
a47463f2 | 446 | \r |
447 | @return GCD Mem Space type\r | |
448 | \r | |
449 | **/\r | |
450 | UINT64\r | |
451 | GetMemorySpaceAttributeFromMtrrType (\r | |
2c4b1bdc | 452 | IN UINT8 MtrrAttributes\r |
a47463f2 | 453 | )\r |
454 | {\r | |
455 | switch (MtrrAttributes) {\r | |
456 | case MTRR_CACHE_UNCACHEABLE:\r | |
457 | return EFI_MEMORY_UC;\r | |
458 | case MTRR_CACHE_WRITE_COMBINING:\r | |
459 | return EFI_MEMORY_WC;\r | |
460 | case MTRR_CACHE_WRITE_THROUGH:\r | |
461 | return EFI_MEMORY_WT;\r | |
462 | case MTRR_CACHE_WRITE_PROTECTED:\r | |
463 | return EFI_MEMORY_WP;\r | |
464 | case MTRR_CACHE_WRITE_BACK:\r | |
465 | return EFI_MEMORY_WB;\r | |
466 | default:\r | |
467 | return 0;\r | |
468 | }\r | |
469 | }\r | |
470 | \r | |
471 | /**\r | |
472 | Searches memory descriptors covered by given memory range.\r | |
473 | \r | |
474 | This function searches into the Gcd Memory Space for descriptors\r | |
475 | (from StartIndex to EndIndex) that contains the memory range\r | |
476 | specified by BaseAddress and Length.\r | |
477 | \r | |
478 | @param MemorySpaceMap Gcd Memory Space Map as array.\r | |
479 | @param NumberOfDescriptors Number of descriptors in map.\r | |
480 | @param BaseAddress BaseAddress for the requested range.\r | |
481 | @param Length Length for the requested range.\r | |
482 | @param StartIndex Start index into the Gcd Memory Space Map.\r | |
483 | @param EndIndex End index into the Gcd Memory Space Map.\r | |
484 | \r | |
485 | @retval EFI_SUCCESS Search successfully.\r | |
486 | @retval EFI_NOT_FOUND The requested descriptors does not exist.\r | |
487 | \r | |
488 | **/\r | |
489 | EFI_STATUS\r | |
490 | SearchGcdMemorySpaces (\r | |
491 | IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r | |
492 | IN UINTN NumberOfDescriptors,\r | |
493 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
494 | IN UINT64 Length,\r | |
495 | OUT UINTN *StartIndex,\r | |
496 | OUT UINTN *EndIndex\r | |
497 | )\r | |
498 | {\r | |
499 | UINTN Index;\r | |
500 | \r | |
501 | *StartIndex = 0;\r | |
502 | *EndIndex = 0;\r | |
503 | for (Index = 0; Index < NumberOfDescriptors; Index++) {\r | |
504 | if (BaseAddress >= MemorySpaceMap[Index].BaseAddress &&\r | |
505 | BaseAddress < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r | |
506 | *StartIndex = Index;\r | |
507 | }\r | |
508 | if (BaseAddress + Length - 1 >= MemorySpaceMap[Index].BaseAddress &&\r | |
509 | BaseAddress + Length - 1 < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r | |
510 | *EndIndex = Index;\r | |
511 | return EFI_SUCCESS;\r | |
512 | }\r | |
513 | }\r | |
514 | return EFI_NOT_FOUND;\r | |
515 | }\r | |
516 | \r | |
517 | /**\r | |
518 | Sets the attributes for a specified range in Gcd Memory Space Map.\r | |
519 | \r | |
520 | This function sets the attributes for a specified range in\r | |
521 | Gcd Memory Space Map.\r | |
522 | \r | |
523 | @param MemorySpaceMap Gcd Memory Space Map as array\r | |
524 | @param NumberOfDescriptors Number of descriptors in map\r | |
525 | @param BaseAddress BaseAddress for the range\r | |
526 | @param Length Length for the range\r | |
527 | @param Attributes Attributes to set\r | |
528 | \r | |
529 | @retval EFI_SUCCESS Memory attributes set successfully\r | |
530 | @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space\r | |
531 | \r | |
532 | **/\r | |
533 | EFI_STATUS\r | |
534 | SetGcdMemorySpaceAttributes (\r | |
535 | IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r | |
536 | IN UINTN NumberOfDescriptors,\r | |
537 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
538 | IN UINT64 Length,\r | |
539 | IN UINT64 Attributes\r | |
540 | )\r | |
541 | {\r | |
542 | EFI_STATUS Status;\r | |
543 | UINTN Index;\r | |
544 | UINTN StartIndex;\r | |
545 | UINTN EndIndex;\r | |
546 | EFI_PHYSICAL_ADDRESS RegionStart;\r | |
547 | UINT64 RegionLength;\r | |
548 | \r | |
549 | //\r | |
550 | // Get all memory descriptors covered by the memory range\r | |
551 | //\r | |
552 | Status = SearchGcdMemorySpaces (\r | |
553 | MemorySpaceMap,\r | |
554 | NumberOfDescriptors,\r | |
555 | BaseAddress,\r | |
556 | Length,\r | |
557 | &StartIndex,\r | |
558 | &EndIndex\r | |
559 | );\r | |
560 | if (EFI_ERROR (Status)) {\r | |
561 | return Status;\r | |
562 | }\r | |
563 | \r | |
564 | //\r | |
565 | // Go through all related descriptors and set attributes accordingly\r | |
566 | //\r | |
567 | for (Index = StartIndex; Index <= EndIndex; Index++) {\r | |
568 | if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r | |
569 | continue;\r | |
570 | }\r | |
571 | //\r | |
572 | // Calculate the start and end address of the overlapping range\r | |
573 | //\r | |
574 | if (BaseAddress >= MemorySpaceMap[Index].BaseAddress) {\r | |
575 | RegionStart = BaseAddress;\r | |
576 | } else {\r | |
577 | RegionStart = MemorySpaceMap[Index].BaseAddress;\r | |
578 | }\r | |
579 | if (BaseAddress + Length - 1 < MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length) {\r | |
580 | RegionLength = BaseAddress + Length - RegionStart;\r | |
581 | } else {\r | |
582 | RegionLength = MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - RegionStart;\r | |
583 | }\r | |
584 | //\r | |
585 | // Set memory attributes according to MTRR attribute and the original attribute of descriptor\r | |
586 | //\r | |
587 | gDS->SetMemorySpaceAttributes (\r | |
588 | RegionStart,\r | |
589 | RegionLength,\r | |
590 | (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) | (MemorySpaceMap[Index].Capabilities & Attributes)\r | |
591 | );\r | |
592 | }\r | |
593 | \r | |
594 | return EFI_SUCCESS;\r | |
595 | }\r | |
596 | \r | |
597 | \r | |
598 | /**\r | |
599 | Refreshes the GCD Memory Space attributes according to MTRRs.\r | |
600 | \r | |
601 | This function refreshes the GCD Memory Space attributes according to MTRRs.\r | |
602 | \r | |
603 | **/\r | |
604 | VOID\r | |
605 | RefreshGcdMemoryAttributes (\r | |
606 | VOID\r | |
607 | )\r | |
608 | {\r | |
609 | EFI_STATUS Status;\r | |
610 | UINTN Index;\r | |
611 | UINTN SubIndex;\r | |
612 | UINT64 RegValue;\r | |
613 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
614 | UINT64 Length;\r | |
615 | UINT64 Attributes;\r | |
616 | UINT64 CurrentAttributes;\r | |
2c4b1bdc | 617 | UINT8 MtrrType;\r |
a47463f2 | 618 | UINTN NumberOfDescriptors;\r |
619 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r | |
620 | UINT64 DefaultAttributes;\r | |
6640eb36 | 621 | VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];\r |
a47463f2 | 622 | MTRR_FIXED_SETTINGS MtrrFixedSettings;\r |
3b9be416 | 623 | UINT32 FirmwareVariableMtrrCount;\r |
2c4b1bdc | 624 | UINT8 DefaultMemoryType;\r |
3b9be416 | 625 | \r |
947a573a | 626 | if (!IsMtrrSupported ()) {\r |
627 | return;\r | |
628 | }\r | |
629 | \r | |
3b9be416 | 630 | FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount ();\r |
5bdfa4e5 | 631 | ASSERT (FirmwareVariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);\r |
a47463f2 | 632 | \r |
91ec7824 | 633 | mIsFlushingGCD = TRUE;\r |
a47463f2 | 634 | MemorySpaceMap = NULL;\r |
635 | \r | |
636 | //\r | |
637 | // Initialize the valid bits mask and valid address mask for MTRRs\r | |
638 | //\r | |
639 | InitializeMtrrMask ();\r | |
640 | \r | |
641 | //\r | |
642 | // Get the memory attribute of variable MTRRs\r | |
643 | //\r | |
644 | MtrrGetMemoryAttributeInVariableMtrr (\r | |
645 | mValidMtrrBitsMask,\r | |
646 | mValidMtrrAddressMask,\r | |
647 | VariableMtrr\r | |
648 | );\r | |
649 | \r | |
650 | //\r | |
651 | // Get the memory space map from GCD\r | |
652 | //\r | |
653 | Status = gDS->GetMemorySpaceMap (\r | |
654 | &NumberOfDescriptors,\r | |
655 | &MemorySpaceMap\r | |
656 | );\r | |
657 | ASSERT_EFI_ERROR (Status);\r | |
658 | \r | |
2c4b1bdc | 659 | DefaultMemoryType = (UINT8) MtrrGetDefaultMemoryType ();\r |
91ec7824 | 660 | DefaultAttributes = GetMemorySpaceAttributeFromMtrrType (DefaultMemoryType);\r |
a47463f2 | 661 | \r |
662 | //\r | |
663 | // Set default attributes to all spaces.\r | |
664 | //\r | |
665 | for (Index = 0; Index < NumberOfDescriptors; Index++) {\r | |
666 | if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r | |
667 | continue;\r | |
668 | }\r | |
669 | gDS->SetMemorySpaceAttributes (\r | |
670 | MemorySpaceMap[Index].BaseAddress,\r | |
671 | MemorySpaceMap[Index].Length,\r | |
672 | (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) |\r | |
673 | (MemorySpaceMap[Index].Capabilities & DefaultAttributes)\r | |
674 | );\r | |
675 | }\r | |
676 | \r | |
677 | //\r | |
678 | // Go for variable MTRRs with WB attribute\r | |
679 | //\r | |
3b9be416 | 680 | for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r |
a47463f2 | 681 | if (VariableMtrr[Index].Valid &&\r |
682 | VariableMtrr[Index].Type == MTRR_CACHE_WRITE_BACK) {\r | |
683 | SetGcdMemorySpaceAttributes (\r | |
684 | MemorySpaceMap,\r | |
685 | NumberOfDescriptors,\r | |
686 | VariableMtrr[Index].BaseAddress,\r | |
687 | VariableMtrr[Index].Length,\r | |
688 | EFI_MEMORY_WB\r | |
689 | );\r | |
690 | }\r | |
691 | }\r | |
91ec7824 | 692 | \r |
a47463f2 | 693 | //\r |
91ec7824 | 694 | // Go for variable MTRRs with the attribute except for WB and UC attributes\r |
a47463f2 | 695 | //\r |
3b9be416 | 696 | for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r |
d4605c23 | 697 | if (VariableMtrr[Index].Valid &&\r |
91ec7824 | 698 | VariableMtrr[Index].Type != MTRR_CACHE_WRITE_BACK &&\r |
699 | VariableMtrr[Index].Type != MTRR_CACHE_UNCACHEABLE) {\r | |
2c4b1bdc | 700 | Attributes = GetMemorySpaceAttributeFromMtrrType ((UINT8) VariableMtrr[Index].Type);\r |
a47463f2 | 701 | SetGcdMemorySpaceAttributes (\r |
702 | MemorySpaceMap,\r | |
703 | NumberOfDescriptors,\r | |
704 | VariableMtrr[Index].BaseAddress,\r | |
705 | VariableMtrr[Index].Length,\r | |
706 | Attributes\r | |
707 | );\r | |
708 | }\r | |
709 | }\r | |
710 | \r | |
91ec7824 | 711 | //\r |
712 | // Go for variable MTRRs with UC attribute\r | |
713 | //\r | |
714 | for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {\r | |
715 | if (VariableMtrr[Index].Valid &&\r | |
716 | VariableMtrr[Index].Type == MTRR_CACHE_UNCACHEABLE) {\r | |
717 | SetGcdMemorySpaceAttributes (\r | |
718 | MemorySpaceMap,\r | |
719 | NumberOfDescriptors,\r | |
720 | VariableMtrr[Index].BaseAddress,\r | |
721 | VariableMtrr[Index].Length,\r | |
722 | EFI_MEMORY_UC\r | |
723 | );\r | |
724 | }\r | |
725 | }\r | |
726 | \r | |
a47463f2 | 727 | //\r |
728 | // Go for fixed MTRRs\r | |
729 | //\r | |
730 | Attributes = 0;\r | |
731 | BaseAddress = 0;\r | |
732 | Length = 0;\r | |
733 | MtrrGetFixedMtrr (&MtrrFixedSettings);\r | |
734 | for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r | |
735 | RegValue = MtrrFixedSettings.Mtrr[Index];\r | |
736 | //\r | |
737 | // Check for continuous fixed MTRR sections\r | |
738 | //\r | |
739 | for (SubIndex = 0; SubIndex < 8; SubIndex++) {\r | |
2c4b1bdc | 740 | MtrrType = (UINT8) RShiftU64 (RegValue, SubIndex * 8);\r |
a47463f2 | 741 | CurrentAttributes = GetMemorySpaceAttributeFromMtrrType (MtrrType);\r |
742 | if (Length == 0) {\r | |
743 | //\r | |
744 | // A new MTRR attribute begins\r | |
745 | //\r | |
746 | Attributes = CurrentAttributes;\r | |
747 | } else {\r | |
748 | //\r | |
749 | // If fixed MTRR attribute changed, then set memory attribute for previous atrribute\r | |
750 | //\r | |
751 | if (CurrentAttributes != Attributes) {\r | |
752 | SetGcdMemorySpaceAttributes (\r | |
753 | MemorySpaceMap,\r | |
754 | NumberOfDescriptors,\r | |
755 | BaseAddress,\r | |
756 | Length,\r | |
757 | Attributes\r | |
758 | );\r | |
759 | BaseAddress = mFixedMtrrTable[Index].BaseAddress + mFixedMtrrTable[Index].Length * SubIndex;\r | |
760 | Length = 0;\r | |
761 | Attributes = CurrentAttributes;\r | |
762 | }\r | |
763 | }\r | |
764 | Length += mFixedMtrrTable[Index].Length;\r | |
765 | }\r | |
766 | }\r | |
767 | //\r | |
768 | // Handle the last fixed MTRR region\r | |
769 | //\r | |
770 | SetGcdMemorySpaceAttributes (\r | |
771 | MemorySpaceMap,\r | |
772 | NumberOfDescriptors,\r | |
773 | BaseAddress,\r | |
774 | Length,\r | |
775 | Attributes\r | |
776 | );\r | |
777 | \r | |
778 | //\r | |
779 | // Free memory space map allocated by GCD service GetMemorySpaceMap ()\r | |
780 | //\r | |
781 | if (MemorySpaceMap != NULL) {\r | |
782 | FreePool (MemorySpaceMap);\r | |
783 | }\r | |
784 | \r | |
785 | mIsFlushingGCD = FALSE;\r | |
786 | }\r | |
787 | \r | |
a47463f2 | 788 | /**\r |
789 | Initialize Interrupt Descriptor Table for interrupt handling.\r | |
790 | \r | |
791 | **/\r | |
a47463f2 | 792 | VOID\r |
793 | InitInterruptDescriptorTable (\r | |
794 | VOID\r | |
795 | )\r | |
796 | {\r | |
e41aad15 JF |
797 | EFI_STATUS Status;\r |
798 | EFI_VECTOR_HANDOFF_INFO *VectorInfoList;\r | |
799 | EFI_VECTOR_HANDOFF_INFO *VectorInfo;\r | |
800 | \r | |
801 | VectorInfo = NULL;\r | |
802 | Status = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid, (VOID **) &VectorInfoList);\r | |
803 | if (Status == EFI_SUCCESS && VectorInfoList != NULL) {\r | |
804 | VectorInfo = VectorInfoList;\r | |
a47463f2 | 805 | }\r |
e41aad15 JF |
806 | Status = InitializeCpuInterruptHandlers (VectorInfo);\r |
807 | ASSERT_EFI_ERROR (Status);\r | |
a47463f2 | 808 | }\r |
809 | \r | |
810 | \r | |
32394027 | 811 | /**\r |
812 | Callback function for idle events.\r | |
d4605c23 | 813 | \r |
32394027 | 814 | @param Event Event whose notification function is being invoked.\r |
815 | @param Context The pointer to the notification function's context,\r | |
816 | which is implementation-dependent.\r | |
817 | \r | |
818 | **/\r | |
819 | VOID\r | |
820 | EFIAPI\r | |
821 | IdleLoopEventCallback (\r | |
822 | IN EFI_EVENT Event,\r | |
823 | IN VOID *Context\r | |
824 | )\r | |
825 | {\r | |
826 | CpuSleep ();\r | |
827 | }\r | |
828 | \r | |
829 | \r | |
a47463f2 | 830 | /**\r |
831 | Initialize the state information for the CPU Architectural Protocol.\r | |
832 | \r | |
833 | @param ImageHandle Image handle this driver.\r | |
834 | @param SystemTable Pointer to the System Table.\r | |
835 | \r | |
836 | @retval EFI_SUCCESS Thread can be successfully created\r | |
837 | @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r | |
838 | @retval EFI_DEVICE_ERROR Cannot create the thread\r | |
839 | \r | |
840 | **/\r | |
841 | EFI_STATUS\r | |
842 | EFIAPI\r | |
843 | InitializeCpu (\r | |
844 | IN EFI_HANDLE ImageHandle,\r | |
845 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
846 | )\r | |
847 | {\r | |
848 | EFI_STATUS Status;\r | |
32394027 | 849 | EFI_EVENT IdleLoopEvent;\r |
a47463f2 | 850 | \r |
661cab5d | 851 | InitializeFloatingPointUnits ();\r |
852 | \r | |
a47463f2 | 853 | //\r |
854 | // Make sure interrupts are disabled\r | |
855 | //\r | |
856 | DisableInterrupts ();\r | |
857 | \r | |
858 | //\r | |
859 | // Init GDT for DXE\r | |
860 | //\r | |
861 | InitGlobalDescriptorTable ();\r | |
862 | \r | |
863 | //\r | |
864 | // Setup IDT pointer, IDT and interrupt entry points\r | |
865 | //\r | |
866 | InitInterruptDescriptorTable ();\r | |
867 | \r | |
d4605c23 EB |
868 | //\r |
869 | // Enable the local APIC for Virtual Wire Mode.\r | |
870 | //\r | |
871 | ProgramVirtualWireMode ();\r | |
872 | \r | |
a47463f2 | 873 | //\r |
874 | // Install CPU Architectural Protocol\r | |
875 | //\r | |
876 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
877 | &mCpuHandle,\r | |
878 | &gEfiCpuArchProtocolGuid, &gCpu,\r | |
879 | NULL\r | |
880 | );\r | |
881 | ASSERT_EFI_ERROR (Status);\r | |
882 | \r | |
883 | //\r | |
884 | // Refresh GCD memory space map according to MTRR value.\r | |
885 | //\r | |
886 | RefreshGcdMemoryAttributes ();\r | |
887 | \r | |
32394027 | 888 | //\r |
889 | // Setup a callback for idle events\r | |
890 | //\r | |
891 | Status = gBS->CreateEventEx (\r | |
892 | EVT_NOTIFY_SIGNAL,\r | |
893 | TPL_NOTIFY,\r | |
894 | IdleLoopEventCallback,\r | |
895 | NULL,\r | |
896 | &gIdleLoopEventGuid,\r | |
897 | &IdleLoopEvent\r | |
898 | );\r | |
899 | ASSERT_EFI_ERROR (Status);\r | |
900 | \r | |
6022e28c JJ |
901 | InitializeMpSupport ();\r |
902 | \r | |
a47463f2 | 903 | return Status;\r |
904 | }\r | |
905 | \r |