CpuDxe.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /** @file
  2. RISC-V CPU DXE driver.
  3. Copyright (c) 2016 - 2022, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "CpuDxe.h"
  7. //
  8. // Global Variables
  9. //
  10. STATIC BOOLEAN mInterruptState = FALSE;
  11. STATIC EFI_HANDLE mCpuHandle = NULL;
  12. EFI_CPU_ARCH_PROTOCOL gCpu = {
  13. CpuFlushCpuDataCache,
  14. CpuEnableInterrupt,
  15. CpuDisableInterrupt,
  16. CpuGetInterruptState,
  17. CpuInit,
  18. CpuRegisterInterruptHandler,
  19. CpuGetTimerValue,
  20. CpuSetMemoryAttributes,
  21. 1, // NumberOfTimers
  22. 4 // DmaBufferAlignment
  23. };
  24. //
  25. // CPU Arch Protocol Functions
  26. //
  27. /**
  28. Flush CPU data cache. If the instruction cache is fully coherent
  29. with all DMA operations then function can just return EFI_SUCCESS.
  30. @param This Protocol instance structure
  31. @param Start Physical address to start flushing from.
  32. @param Length Number of bytes to flush. Round up to chipset
  33. granularity.
  34. @param FlushType Specifies the type of flush operation to perform.
  35. @retval EFI_SUCCESS If cache was flushed
  36. @retval EFI_UNSUPPORTED If flush type is not supported.
  37. @retval EFI_DEVICE_ERROR If requested range could not be flushed.
  38. **/
  39. EFI_STATUS
  40. EFIAPI
  41. CpuFlushCpuDataCache (
  42. IN EFI_CPU_ARCH_PROTOCOL *This,
  43. IN EFI_PHYSICAL_ADDRESS Start,
  44. IN UINT64 Length,
  45. IN EFI_CPU_FLUSH_TYPE FlushType
  46. )
  47. {
  48. return EFI_SUCCESS;
  49. }
  50. /**
  51. Enables CPU interrupts.
  52. @param This Protocol instance structure
  53. @retval EFI_SUCCESS If interrupts were enabled in the CPU
  54. @retval EFI_DEVICE_ERROR If interrupts could not be enabled on the CPU.
  55. **/
  56. EFI_STATUS
  57. EFIAPI
  58. CpuEnableInterrupt (
  59. IN EFI_CPU_ARCH_PROTOCOL *This
  60. )
  61. {
  62. EnableInterrupts ();
  63. mInterruptState = TRUE;
  64. return EFI_SUCCESS;
  65. }
  66. /**
  67. Disables CPU interrupts.
  68. @param This Protocol instance structure
  69. @retval EFI_SUCCESS If interrupts were disabled in the CPU.
  70. @retval EFI_DEVICE_ERROR If interrupts could not be disabled on the CPU.
  71. **/
  72. EFI_STATUS
  73. EFIAPI
  74. CpuDisableInterrupt (
  75. IN EFI_CPU_ARCH_PROTOCOL *This
  76. )
  77. {
  78. DisableInterrupts ();
  79. mInterruptState = FALSE;
  80. return EFI_SUCCESS;
  81. }
  82. /**
  83. Return the state of interrupts.
  84. @param This Protocol instance structure
  85. @param State Pointer to the CPU's current interrupt state
  86. @retval EFI_SUCCESS If interrupts were disabled in the CPU.
  87. @retval EFI_INVALID_PARAMETER State is NULL.
  88. **/
  89. EFI_STATUS
  90. EFIAPI
  91. CpuGetInterruptState (
  92. IN EFI_CPU_ARCH_PROTOCOL *This,
  93. OUT BOOLEAN *State
  94. )
  95. {
  96. if (State == NULL) {
  97. return EFI_INVALID_PARAMETER;
  98. }
  99. *State = mInterruptState;
  100. return EFI_SUCCESS;
  101. }
  102. /**
  103. Generates an INIT to the CPU.
  104. @param This Protocol instance structure
  105. @param InitType Type of CPU INIT to perform
  106. @retval EFI_SUCCESS If CPU INIT occurred. This value should never be
  107. seen.
  108. @retval EFI_DEVICE_ERROR If CPU INIT failed.
  109. @retval EFI_UNSUPPORTED Requested type of CPU INIT not supported.
  110. **/
  111. EFI_STATUS
  112. EFIAPI
  113. CpuInit (
  114. IN EFI_CPU_ARCH_PROTOCOL *This,
  115. IN EFI_CPU_INIT_TYPE InitType
  116. )
  117. {
  118. return EFI_UNSUPPORTED;
  119. }
  120. /**
  121. Registers a function to be called from the CPU interrupt handler.
  122. @param This Protocol instance structure
  123. @param InterruptType Defines which interrupt to hook. IA-32
  124. valid range is 0x00 through 0xFF
  125. @param InterruptHandler A pointer to a function of type
  126. EFI_CPU_INTERRUPT_HANDLER that is called
  127. when a processor interrupt occurs. A null
  128. pointer is an error condition.
  129. @retval EFI_SUCCESS If handler installed or uninstalled.
  130. @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler
  131. for InterruptType was previously installed.
  132. @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
  133. InterruptType was not previously installed.
  134. @retval EFI_UNSUPPORTED The interrupt specified by InterruptType
  135. is not supported.
  136. **/
  137. EFI_STATUS
  138. EFIAPI
  139. CpuRegisterInterruptHandler (
  140. IN EFI_CPU_ARCH_PROTOCOL *This,
  141. IN EFI_EXCEPTION_TYPE InterruptType,
  142. IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
  143. )
  144. {
  145. return RegisterCpuInterruptHandler (InterruptType, InterruptHandler);
  146. }
  147. /**
  148. Returns a timer value from one of the CPU's internal timers. There is no
  149. inherent time interval between ticks but is a function of the CPU frequency.
  150. @param This - Protocol instance structure.
  151. @param TimerIndex - Specifies which CPU timer is requested.
  152. @param TimerValue - Pointer to the returned timer value.
  153. @param TimerPeriod - A pointer to the amount of time that passes
  154. in femtoseconds (10-15) for each increment
  155. of TimerValue. If TimerValue does not
  156. increment at a predictable rate, then 0 is
  157. returned. The amount of time that has
  158. passed between two calls to GetTimerValue()
  159. can be calculated with the formula
  160. (TimerValue2 - TimerValue1) * TimerPeriod.
  161. This parameter is optional and may be NULL.
  162. @retval EFI_SUCCESS - If the CPU timer count was returned.
  163. @retval EFI_UNSUPPORTED - If the CPU does not have any readable timers.
  164. @retval EFI_DEVICE_ERROR - If an error occurred while reading the timer.
  165. @retval EFI_INVALID_PARAMETER - TimerIndex is not valid or TimerValue is NULL.
  166. **/
  167. EFI_STATUS
  168. EFIAPI
  169. CpuGetTimerValue (
  170. IN EFI_CPU_ARCH_PROTOCOL *This,
  171. IN UINT32 TimerIndex,
  172. OUT UINT64 *TimerValue,
  173. OUT UINT64 *TimerPeriod OPTIONAL
  174. )
  175. {
  176. if (TimerValue == NULL) {
  177. return EFI_INVALID_PARAMETER;
  178. }
  179. if (TimerIndex != 0) {
  180. return EFI_INVALID_PARAMETER;
  181. }
  182. *TimerValue = (UINT64)RiscVReadMachineTimerInterface ();
  183. if (TimerPeriod != NULL) {
  184. *TimerPeriod = DivU64x32 (
  185. 1000000000000000u,
  186. PcdGet64 (PcdRiscVMachineTimerFrequencyInHerz)
  187. );
  188. }
  189. return EFI_SUCCESS;
  190. }
  191. /**
  192. Implementation of SetMemoryAttributes() service of CPU Architecture Protocol.
  193. This function modifies the attributes for the memory region specified by BaseAddress and
  194. Length from their current attributes to the attributes specified by Attributes.
  195. @param This The EFI_CPU_ARCH_PROTOCOL instance.
  196. @param BaseAddress The physical address that is the start address of a memory region.
  197. @param Length The size in bytes of the memory region.
  198. @param Attributes The bit mask of attributes to set for the memory region.
  199. @retval EFI_SUCCESS The attributes were set for the memory region.
  200. @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
  201. BaseAddress and Length cannot be modified.
  202. @retval EFI_INVALID_PARAMETER Length is zero.
  203. Attributes specified an illegal combination of attributes that
  204. cannot be set together.
  205. @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
  206. the memory resource range.
  207. @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
  208. resource range specified by BaseAddress and Length.
  209. The bit mask of attributes is not support for the memory resource
  210. range specified by BaseAddress and Length.
  211. **/
  212. EFI_STATUS
  213. EFIAPI
  214. CpuSetMemoryAttributes (
  215. IN EFI_CPU_ARCH_PROTOCOL *This,
  216. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  217. IN UINT64 Length,
  218. IN UINT64 Attributes
  219. )
  220. {
  221. DEBUG ((DEBUG_INFO, "%a: Set memory attributes not supported yet\n", __FUNCTION__));
  222. return EFI_SUCCESS;
  223. }
  224. /**
  225. Initialize the state information for the CPU Architectural Protocol.
  226. @param ImageHandle Image handle this driver.
  227. @param SystemTable Pointer to the System Table.
  228. @retval EFI_SUCCESS Thread can be successfully created
  229. @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
  230. @retval EFI_DEVICE_ERROR Cannot create the thread
  231. **/
  232. EFI_STATUS
  233. EFIAPI
  234. InitializeCpu (
  235. IN EFI_HANDLE ImageHandle,
  236. IN EFI_SYSTEM_TABLE *SystemTable
  237. )
  238. {
  239. EFI_STATUS Status;
  240. //
  241. // Machine mode handler is initiated in CpuExceptionHandlerLibConstructor in
  242. // CpuExecptionHandlerLib.
  243. //
  244. //
  245. // Make sure interrupts are disabled
  246. //
  247. DisableInterrupts ();
  248. //
  249. // Install CPU Architectural Protocol
  250. //
  251. Status = gBS->InstallMultipleProtocolInterfaces (
  252. &mCpuHandle,
  253. &gEfiCpuArchProtocolGuid,
  254. &gCpu,
  255. NULL
  256. );
  257. ASSERT_EFI_ERROR (Status);
  258. return Status;
  259. }