VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UefiCpuPkg/CpuDxe/LoongArch64/CpuDxe.c@ 108794

Last change on this file since 108794 was 105670, checked in by vboxsync, 8 months ago

Devices/EFI/FirmwareNew: Merge edk2-stable-202405 and make it build on aarch64, bugref:4643

  • Property svn:eol-style set to native
File size: 15.8 KB
Line 
1/** @file CpuDxe.c
2
3 CPU DXE Module to produce CPU ARCH Protocol.
4
5 Copyright (c) 2024, Loongson Technology Corporation Limited. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8**/
9
10#include "CpuDxe.h"
11#include "CpuMp.h"
12#include <Guid/IdleLoopEvent.h>
13#include <Library/CpuMmuLib.h>
14#include <Library/TimerLib.h>
15#include <Register/LoongArch64/Csr.h>
16
17UINT64 mTimerPeriod = 0;
18
19/**
20 IPI Interrupt Handler.
21
22 @param InterruptType The type of interrupt that occurred
23 @param SystemContext A pointer to the system context when the interrupt occurred
24**/
25VOID
26EFIAPI
27IpiInterruptHandler (
28 IN EFI_EXCEPTION_TYPE InterruptType,
29 IN EFI_SYSTEM_CONTEXT SystemContext
30 );
31
32//
33// Globals used to initialize the protocol
34//
35EFI_HANDLE mCpuHandle = NULL;
36EFI_CPU_ARCH_PROTOCOL gCpu = {
37 CpuFlushCpuDataCache,
38 CpuEnableInterrupt,
39 CpuDisableInterrupt,
40 CpuGetInterruptState,
41 CpuInit,
42 CpuRegisterInterruptHandler,
43 CpuGetTimerValue,
44 CpuSetMemoryAttributes,
45 0, // NumberOfTimers
46 4, // DmaBufferAlignment
47};
48
49/**
50 This function flushes the range of addresses from Start to Start+Length
51 from the processor's data cache. If Start is not aligned to a cache line
52 boundary, then the bytes before Start to the preceding cache line boundary
53 are also flushed. If Start+Length is not aligned to a cache line boundary,
54 then the bytes past Start+Length to the end of the next cache line boundary
55 are also flushed. The FlushType of EfiCpuFlushTypeWriteBackInvalidate must be
56 supported. If the data cache is fully coherent with all DMA operations, then
57 this function can just return EFI_SUCCESS. If the processor does not support
58 flushing a range of the data cache, then the entire data cache can be flushed.
59
60 @param This The EFI_CPU_ARCH_PROTOCOL instance.
61 @param Start The beginning physical address to flush from the processor's data
62 cache.
63 @param Length The number of bytes to flush from the processor's data cache. This
64 function may flush more bytes than Length specifies depending upon
65 the granularity of the flush operation that the processor supports.
66 @param FlushType Specifies the type of flush operation to perform.
67
68 @retval EFI_SUCCESS The address range from Start to Start+Length was flushed from
69 the processor's data cache.
70 @retval EFI_INVALID_PARAMETER The processor does not support the cache flush type specified
71 by FlushType.
72
73**/
74EFI_STATUS
75EFIAPI
76CpuFlushCpuDataCache (
77 IN EFI_CPU_ARCH_PROTOCOL *This,
78 IN EFI_PHYSICAL_ADDRESS Start,
79 IN UINT64 Length,
80 IN EFI_CPU_FLUSH_TYPE FlushType
81 )
82{
83 switch (FlushType) {
84 case EfiCpuFlushTypeWriteBack:
85 WriteBackDataCacheRange ((VOID *)(UINTN)Start, (UINTN)Length);
86 break;
87 case EfiCpuFlushTypeInvalidate:
88 InvalidateDataCacheRange ((VOID *)(UINTN)Start, (UINTN)Length);
89 break;
90 case EfiCpuFlushTypeWriteBackInvalidate:
91 WriteBackInvalidateDataCacheRange ((VOID *)(UINTN)Start, (UINTN)Length);
92 break;
93 default:
94 return EFI_INVALID_PARAMETER;
95 }
96
97 return EFI_SUCCESS;
98}
99
100/**
101 This function enables interrupt processing by the processor.
102
103 @param This The EFI_CPU_ARCH_PROTOCOL instance.
104
105 @retval EFI_SUCCESS Interrupts are enabled on the processor.
106 @retval EFI_DEVICE_ERROR Interrupts could not be enabled on the processor.
107
108**/
109EFI_STATUS
110EFIAPI
111CpuEnableInterrupt (
112 IN EFI_CPU_ARCH_PROTOCOL *This
113 )
114{
115 EnableInterrupts ();
116
117 return EFI_SUCCESS;
118}
119
120/**
121 This function disables interrupt processing by the processor.
122
123 @param This The EFI_CPU_ARCH_PROTOCOL instance.
124
125 @retval EFI_SUCCESS Interrupts are disabled on the processor.
126 @retval EFI_DEVICE_ERROR Interrupts could not be disabled on the processor.
127
128**/
129EFI_STATUS
130EFIAPI
131CpuDisableInterrupt (
132 IN EFI_CPU_ARCH_PROTOCOL *This
133 )
134{
135 DisableInterrupts ();
136
137 return EFI_SUCCESS;
138}
139
140/**
141 This function retrieves the processor's current interrupt state a returns it in
142 State. If interrupts are currently enabled, then TRUE is returned. If interrupts
143 are currently disabled, then FALSE is returned.
144
145 @param This The EFI_CPU_ARCH_PROTOCOL instance.
146 @param State A pointer to the processor's current interrupt state. Set to TRUE if
147 interrupts are enabled and FALSE if interrupts are disabled.
148
149 @retval EFI_SUCCESS The processor's current interrupt state was returned in State.
150 @retval EFI_INVALID_PARAMETER State is NULL.
151
152**/
153EFI_STATUS
154EFIAPI
155CpuGetInterruptState (
156 IN EFI_CPU_ARCH_PROTOCOL *This,
157 OUT BOOLEAN *State
158 )
159{
160 if (State == NULL) {
161 return EFI_INVALID_PARAMETER;
162 }
163
164 *State = GetInterruptState ();
165 return EFI_SUCCESS;
166}
167
168/**
169 This function generates an INIT on the processor. If this function succeeds, then the
170 processor will be reset, and control will not be returned to the caller. If InitType is
171 not supported by this processor, or the processor cannot programmatically generate an
172 INIT without help from external hardware, then EFI_UNSUPPORTED is returned. If an error
173 occurs attempting to generate an INIT, then EFI_DEVICE_ERROR is returned.
174
175 @param This The EFI_CPU_ARCH_PROTOCOL instance.
176 @param InitType The type of processor INIT to perform.
177
178 @retval EFI_SUCCESS The processor INIT was performed. This return code should never be seen.
179 @retval EFI_UNSUPPORTED The processor INIT operation specified by InitType is not supported
180 by this processor.
181 @retval EFI_DEVICE_ERROR The processor INIT failed.
182
183**/
184EFI_STATUS
185EFIAPI
186CpuInit (
187 IN EFI_CPU_ARCH_PROTOCOL *This,
188 IN EFI_CPU_INIT_TYPE InitType
189 )
190{
191 return EFI_UNSUPPORTED;
192}
193
194/**
195 Registers a function to be called from the CPU interrupt handler.
196
197 @param This Protocol instance structure
198 @param InterruptType Defines which interrupt to hook. IA-32
199 valid range is 0x00 through 0xFF
200 @param InterruptHandler A pointer to a function of type
201 EFI_CPU_INTERRUPT_HANDLER that is called
202 when a processor interrupt occurs. A null
203 pointer is an error condition.
204
205 @retval EFI_SUCCESS If handler installed or uninstalled.
206 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler
207 for InterruptType was previously installed.
208 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
209 InterruptType was not previously installed.
210 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType
211 is not supported.
212
213**/
214EFI_STATUS
215EFIAPI
216CpuRegisterInterruptHandler (
217 IN EFI_CPU_ARCH_PROTOCOL *This,
218 IN EFI_EXCEPTION_TYPE InterruptType,
219 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
220 )
221{
222 return RegisterInterruptHandler (InterruptType, InterruptHandler);
223}
224
225/**
226 Returns a timer value from one of the CPU's internal timers. There is no
227 inherent time interval between ticks but is a function of the CPU frequency.
228
229 @param This - Protocol instance structure.
230 @param TimerIndex - Specifies which CPU timer is requested.
231 @param TimerValue - Pointer to the returned timer value.
232 @param TimerPeriod - A pointer to the amount of time that passes
233 in femtoseconds (10-15) for each increment
234 of TimerValue. If TimerValue does not
235 increment at a predictable rate, then 0 is
236 returned. The amount of time that has
237 passed between two calls to GetTimerValue()
238 can be calculated with the formula
239 (TimerValue2 - TimerValue1) * TimerPeriod.
240 This parameter is optional and may be NULL.
241
242 @retval EFI_SUCCESS - If the CPU timer count was returned.
243 @retval EFI_UNSUPPORTED - If the CPU does not have any readable timers.
244 @retval EFI_DEVICE_ERROR - If an error occurred while reading the timer.
245 @retval EFI_INVALID_PARAMETER - TimerIndex is not valid or TimerValue is NULL.
246
247**/
248EFI_STATUS
249EFIAPI
250CpuGetTimerValue (
251 IN EFI_CPU_ARCH_PROTOCOL *This,
252 IN UINT32 TimerIndex,
253 OUT UINT64 *TimerValue,
254 OUT UINT64 *TimerPeriod OPTIONAL
255 )
256{
257 UINT64 BeginValue;
258 UINT64 EndValue;
259
260 if (TimerValue == NULL) {
261 return EFI_INVALID_PARAMETER;
262 }
263
264 if (TimerIndex != 0) {
265 return EFI_INVALID_PARAMETER;
266 }
267
268 *TimerValue = AsmReadStableCounter ();
269
270 if (TimerPeriod != NULL) {
271 if (mTimerPeriod == 0) {
272 //
273 // Read time stamp counter before and after delay of 100 microseconds
274 //
275 BeginValue = AsmReadStableCounter ();
276 MicroSecondDelay (100);
277 EndValue = AsmReadStableCounter ();
278 //
279 // Calculate the actual frequency
280 //
281 mTimerPeriod = DivU64x64Remainder (
282 MultU64x32 (
283 1000 * 1000 * 1000,
284 100
285 ),
286 EndValue - BeginValue,
287 NULL
288 );
289 }
290
291 *TimerPeriod = mTimerPeriod;
292 }
293
294 return EFI_SUCCESS;
295}
296
297/**
298 This function modifies the attributes for the memory region specified by BaseAddress and
299 Length from their current attributes to the attributes specified by Attributes.
300
301 @param This The EFI_CPU_ARCH_PROTOCOL instance.
302 @param BaseAddress The physical address that is the start address of a memory region.
303 @param Length The size in bytes of the memory region.
304 @param EfiAttributes The bit mask of attributes to set for the memory region.
305
306 @retval EFI_SUCCESS The attributes were set for the memory region.
307 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
308 BaseAddress and Length cannot be modified.
309 @retval EFI_INVALID_PARAMETER Length is zero.
310 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
311 the memory resource range.
312 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
313 resource range specified by BaseAddress and Length.
314 The bit mask of attributes is not support for the memory resource
315 range specified by BaseAddress and Length.
316
317**/
318EFI_STATUS
319EFIAPI
320CpuSetMemoryAttributes (
321 IN EFI_CPU_ARCH_PROTOCOL *This,
322 IN EFI_PHYSICAL_ADDRESS BaseAddress,
323 IN UINT64 Length,
324 IN UINT64 EfiAttributes
325 )
326{
327 EFI_STATUS Status;
328 UINTN PageTable;
329 UINT64 PageWalkCfg;
330
331 Status = EFI_SUCCESS;
332 PageTable = CsrRead (LOONGARCH_CSR_PGDL);
333 PageWalkCfg = ((UINT64)CsrRead (LOONGARCH_CSR_PWCTL1)) << 32 | CsrRead (LOONGARCH_CSR_PWCTL0);
334
335 if ((BaseAddress & (EFI_PAGE_SIZE - 1)) != 0) {
336 //
337 // Minimum granularity is SIZE_4KB.
338 //
339 DEBUG ((
340 DEBUG_INFO,
341 "MemoryRegionMap(%lx, %lx, %lx, %lx, %lx): Minimum granularity is SIZE_4KB\n",
342 &PageTable,
343 PageWalkCfg,
344 BaseAddress,
345 Length,
346 EfiAttributes
347 ));
348
349 Status = EFI_UNSUPPORTED;
350
351 return Status;
352 }
353
354 Status = MemoryRegionMap (
355 &PageTable,
356 PageWalkCfg,
357 BaseAddress,
358 Length,
359 EfiAttributes,
360 0x0
361 );
362
363 ASSERT_EFI_ERROR (Status);
364
365 return Status;
366}
367
368/**
369 Callback function for idle events.
370
371 @param Event Event whose notification function is being invoked.
372 @param Context The pointer to the notification function's context,
373 which is implementation-dependent.
374
375**/
376VOID
377EFIAPI
378IdleLoopEventCallback (
379 IN EFI_EVENT Event,
380 IN VOID *Context
381 )
382{
383 CpuSleep ();
384}
385
386/**
387 Refreshes the GCD Memory Space attributes according to Default Config
388
389 This function refreshes the GCD Memory Space attributes according to DefaultConfig
390
391 @retval EFI_SUCCESS Refresh GCD success.
392
393**/
394EFI_STATUS
395RefreshGcdMemoryAttributes (
396 VOID
397 )
398{
399 EFI_STATUS Status;
400 UINT32 Index;
401 UINTN NumberOfDescriptors;
402 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;
403
404 DEBUG ((DEBUG_PAGE, "RefreshGcdMemoryAttributes()\n"));
405
406 //
407 // Get the memory space map from GCD
408 //
409 MemorySpaceMap = NULL;
410 Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);
411
412 if (EFI_ERROR (Status)) {
413 DEBUG ((
414 DEBUG_ERROR,
415 "RefreshGcdMemoryAttributes - GetMemorySpaceMap() failed! Status: %r\n",
416 Status
417 ));
418
419 ASSERT_EFI_ERROR (Status);
420
421 return Status;
422 }
423
424 for ( Index = 0; Index < NumberOfDescriptors; Index++ ) {
425 //
426 // If this is system memory, not a class resource like MMIO, and the capability
427 // contains a Memory cacheability attributes and the attribute feature is set
428 // to 0, we will set its attribute to the WriteBack memory of the LoongArch
429 // architecture for the first time.
430 //
431 if (((MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) &&
432 MemorySpaceMap[Index].Capabilities & EFI_CACHE_ATTRIBUTE_MASK) &&
433 (MemorySpaceMap[Index].Attributes == 0))
434 {
435 if (!(MemorySpaceMap[Index].Capabilities & EFI_MEMORY_WB)) {
436 DEBUG ((
437 DEBUG_WARN,
438 "RefreshGcdMemoryAttributes - SystemMemory Capabilities should support EFI_MEMORY_WB ! \n"
439 ));
440 }
441
442 //
443 // Refresh or Sync Gcd's memory attributes according to Default Paging (CACHE_CC)
444 //
445 gDS->SetMemorySpaceAttributes (
446 MemorySpaceMap[Index].BaseAddress,
447 MemorySpaceMap[Index].Length,
448 (MemorySpaceMap[Index].Attributes & ~EFI_CACHE_ATTRIBUTE_MASK) |
449 (MemorySpaceMap[Index].Capabilities & EFI_MEMORY_WB)
450 );
451 }
452 }
453
454 return EFI_SUCCESS;
455}
456
457/**
458 Initialize the state information for the CPU Architectural Protocol.
459
460 @param ImageHandle Image handle this driver.
461 @param SystemTable Pointer to the System Table.
462
463 @retval EFI_SUCCESS Thread can be successfully created
464 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
465 @retval EFI_DEVICE_ERROR Cannot create the thread
466
467**/
468EFI_STATUS
469InitializeCpu (
470 IN EFI_HANDLE ImageHandle,
471 IN EFI_SYSTEM_TABLE *SystemTable
472 )
473{
474 EFI_STATUS Status;
475 EFI_EVENT IdleLoopEvent;
476
477 InitializeExceptions (&gCpu);
478
479 Status = gBS->InstallMultipleProtocolInterfaces (
480 &mCpuHandle,
481 &gEfiCpuArchProtocolGuid,
482 &gCpu,
483 NULL
484 );
485 ASSERT_EFI_ERROR (Status);
486
487 //
488 // Refresh GCD memory space map according to Default Paging.
489 //
490 RefreshGcdMemoryAttributes ();
491
492 Status = gCpu.RegisterInterruptHandler (
493 &gCpu,
494 EXCEPT_LOONGARCH_INT_IPI,
495 IpiInterruptHandler
496 );
497 ASSERT_EFI_ERROR (Status);
498
499 //
500 // Setup a callback for idle events
501 //
502 Status = gBS->CreateEventEx (
503 EVT_NOTIFY_SIGNAL,
504 TPL_NOTIFY,
505 IdleLoopEventCallback,
506 NULL,
507 &gIdleLoopEventGuid,
508 &IdleLoopEvent
509 );
510 ASSERT_EFI_ERROR (Status);
511
512 InitializeMpSupport ();
513
514 return Status;
515}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette