IoLibMmioBuffer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /** @file
  2. I/O Library MMIO Buffer Functions.
  3. Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. Module Name: IoLibMmioBuffer.c
  6. **/
  7. #include "DxeCpuIoLibInternal.h"
  8. /**
  9. Copy data from MMIO region to system memory by using 8-bit access.
  10. Copy data from MMIO region specified by starting address StartAddress
  11. to system memory specified by Buffer by using 8-bit access. The total
  12. number of byte to be copied is specified by Length. Buffer is returned.
  13. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  14. If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
  15. @param StartAddress Starting address for the MMIO region to be copied from.
  16. @param Length Size in bytes of the copy.
  17. @param Buffer Pointer to a system memory buffer receiving the data read.
  18. @return Buffer
  19. **/
  20. UINT8 *
  21. EFIAPI
  22. MmioReadBuffer8 (
  23. IN UINTN StartAddress,
  24. IN UINTN Length,
  25. OUT UINT8 *Buffer
  26. )
  27. {
  28. UINT8 *ReturnBuffer;
  29. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  30. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  31. ReturnBuffer = Buffer;
  32. while (Length-- > 0) {
  33. *(Buffer++) = MmioRead8 (StartAddress++);
  34. }
  35. return ReturnBuffer;
  36. }
  37. /**
  38. Copy data from MMIO region to system memory by using 16-bit access.
  39. Copy data from MMIO region specified by starting address StartAddress
  40. to system memory specified by Buffer by using 16-bit access. The total
  41. number of byte to be copied is specified by Length. Buffer is returned.
  42. If StartAddress is not aligned on a 16-bit boundary, then ASSERT().
  43. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  44. If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
  45. If Length is not aligned on a 16-bit boundary, then ASSERT().
  46. If Buffer is not aligned on a 16-bit boundary, then ASSERT().
  47. @param StartAddress Starting address for the MMIO region to be copied from.
  48. @param Length Size in bytes of the copy.
  49. @param Buffer Pointer to a system memory buffer receiving the data read.
  50. @return Buffer
  51. **/
  52. UINT16 *
  53. EFIAPI
  54. MmioReadBuffer16 (
  55. IN UINTN StartAddress,
  56. IN UINTN Length,
  57. OUT UINT16 *Buffer
  58. )
  59. {
  60. UINT16 *ReturnBuffer;
  61. ASSERT ((StartAddress & (sizeof (UINT16) - 1)) == 0);
  62. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  63. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  64. ASSERT ((Length & (sizeof (UINT16) - 1)) == 0);
  65. ASSERT (((UINTN) Buffer & (sizeof (UINT16) - 1)) == 0);
  66. ReturnBuffer = Buffer;
  67. while (Length > 0) {
  68. *(Buffer++) = MmioRead16 (StartAddress);
  69. StartAddress += sizeof (UINT16);
  70. Length -= sizeof (UINT16);
  71. }
  72. return ReturnBuffer;
  73. }
  74. /**
  75. Copy data from MMIO region to system memory by using 32-bit access.
  76. Copy data from MMIO region specified by starting address StartAddress
  77. to system memory specified by Buffer by using 32-bit access. The total
  78. number of byte to be copied is specified by Length. Buffer is returned.
  79. If StartAddress is not aligned on a 32-bit boundary, then ASSERT().
  80. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  81. If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
  82. If Length is not aligned on a 32-bit boundary, then ASSERT().
  83. If Buffer is not aligned on a 32-bit boundary, then ASSERT().
  84. @param StartAddress Starting address for the MMIO region to be copied from.
  85. @param Length Size in bytes of the copy.
  86. @param Buffer Pointer to a system memory buffer receiving the data read.
  87. @return Buffer
  88. **/
  89. UINT32 *
  90. EFIAPI
  91. MmioReadBuffer32 (
  92. IN UINTN StartAddress,
  93. IN UINTN Length,
  94. OUT UINT32 *Buffer
  95. )
  96. {
  97. UINT32 *ReturnBuffer;
  98. ASSERT ((StartAddress & (sizeof (UINT32) - 1)) == 0);
  99. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  100. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  101. ASSERT ((Length & (sizeof (UINT32) - 1)) == 0);
  102. ASSERT (((UINTN) Buffer & (sizeof (UINT32) - 1)) == 0);
  103. ReturnBuffer = Buffer;
  104. while (Length > 0) {
  105. *(Buffer++) = MmioRead32 (StartAddress);
  106. StartAddress += sizeof (UINT32);
  107. Length -= sizeof (UINT32);
  108. }
  109. return ReturnBuffer;
  110. }
  111. /**
  112. Copy data from MMIO region to system memory by using 64-bit access.
  113. Copy data from MMIO region specified by starting address StartAddress
  114. to system memory specified by Buffer by using 64-bit access. The total
  115. number of byte to be copied is specified by Length. Buffer is returned.
  116. If StartAddress is not aligned on a 64-bit boundary, then ASSERT().
  117. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  118. If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
  119. If Length is not aligned on a 64-bit boundary, then ASSERT().
  120. If Buffer is not aligned on a 64-bit boundary, then ASSERT().
  121. @param StartAddress Starting address for the MMIO region to be copied from.
  122. @param Length Size in bytes of the copy.
  123. @param Buffer Pointer to a system memory buffer receiving the data read.
  124. @return Buffer
  125. **/
  126. UINT64 *
  127. EFIAPI
  128. MmioReadBuffer64 (
  129. IN UINTN StartAddress,
  130. IN UINTN Length,
  131. OUT UINT64 *Buffer
  132. )
  133. {
  134. UINT64 *ReturnBuffer;
  135. ASSERT ((StartAddress & (sizeof (UINT64) - 1)) == 0);
  136. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  137. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  138. ASSERT ((Length & (sizeof (UINT64) - 1)) == 0);
  139. ASSERT (((UINTN) Buffer & (sizeof (UINT64) - 1)) == 0);
  140. ReturnBuffer = Buffer;
  141. while (Length > 0) {
  142. *(Buffer++) = MmioRead64 (StartAddress);
  143. StartAddress += sizeof (UINT64);
  144. Length -= sizeof (UINT64);
  145. }
  146. return ReturnBuffer;
  147. }
  148. /**
  149. Copy data from system memory to MMIO region by using 8-bit access.
  150. Copy data from system memory specified by Buffer to MMIO region specified
  151. by starting address StartAddress by using 8-bit access. The total number
  152. of byte to be copied is specified by Length. Buffer is returned.
  153. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  154. If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
  155. @param StartAddress Starting address for the MMIO region to be copied to.
  156. @param Length Size in bytes of the copy.
  157. @param Buffer Pointer to a system memory buffer containing the data to write.
  158. @return Buffer
  159. **/
  160. UINT8 *
  161. EFIAPI
  162. MmioWriteBuffer8 (
  163. IN UINTN StartAddress,
  164. IN UINTN Length,
  165. IN CONST UINT8 *Buffer
  166. )
  167. {
  168. VOID* ReturnBuffer;
  169. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  170. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  171. ReturnBuffer = (UINT8 *) Buffer;
  172. while (Length-- > 0) {
  173. MmioWrite8 (StartAddress++, *(Buffer++));
  174. }
  175. return ReturnBuffer;
  176. }
  177. /**
  178. Copy data from system memory to MMIO region by using 16-bit access.
  179. Copy data from system memory specified by Buffer to MMIO region specified
  180. by starting address StartAddress by using 16-bit access. The total number
  181. of byte to be copied is specified by Length. Buffer is returned.
  182. If StartAddress is not aligned on a 16-bit boundary, then ASSERT().
  183. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  184. If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
  185. If Length is not aligned on a 16-bit boundary, then ASSERT().
  186. If Buffer is not aligned on a 16-bit boundary, then ASSERT().
  187. @param StartAddress Starting address for the MMIO region to be copied to.
  188. @param Length Size in bytes of the copy.
  189. @param Buffer Pointer to a system memory buffer containing the data to write.
  190. @return Buffer
  191. **/
  192. UINT16 *
  193. EFIAPI
  194. MmioWriteBuffer16 (
  195. IN UINTN StartAddress,
  196. IN UINTN Length,
  197. IN CONST UINT16 *Buffer
  198. )
  199. {
  200. UINT16 *ReturnBuffer;
  201. ASSERT ((StartAddress & (sizeof (UINT16) - 1)) == 0);
  202. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  203. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  204. ASSERT ((Length & (sizeof (UINT16) - 1)) == 0);
  205. ASSERT (((UINTN) Buffer & (sizeof (UINT16) - 1)) == 0);
  206. ReturnBuffer = (UINT16 *) Buffer;
  207. while (Length > 0) {
  208. MmioWrite16 (StartAddress, *(Buffer++));
  209. StartAddress += sizeof (UINT16);
  210. Length -= sizeof (UINT16);
  211. }
  212. return ReturnBuffer;
  213. }
  214. /**
  215. Copy data from system memory to MMIO region by using 32-bit access.
  216. Copy data from system memory specified by Buffer to MMIO region specified
  217. by starting address StartAddress by using 32-bit access. The total number
  218. of byte to be copied is specified by Length. Buffer is returned.
  219. If StartAddress is not aligned on a 32-bit boundary, then ASSERT().
  220. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  221. If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
  222. If Length is not aligned on a 32-bit boundary, then ASSERT().
  223. If Buffer is not aligned on a 32-bit boundary, then ASSERT().
  224. @param StartAddress Starting address for the MMIO region to be copied to.
  225. @param Length Size in bytes of the copy.
  226. @param Buffer Pointer to a system memory buffer containing the data to write.
  227. @return Buffer
  228. **/
  229. UINT32 *
  230. EFIAPI
  231. MmioWriteBuffer32 (
  232. IN UINTN StartAddress,
  233. IN UINTN Length,
  234. IN CONST UINT32 *Buffer
  235. )
  236. {
  237. UINT32 *ReturnBuffer;
  238. ASSERT ((StartAddress & (sizeof (UINT32) - 1)) == 0);
  239. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  240. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  241. ASSERT ((Length & (sizeof (UINT32) - 1)) == 0);
  242. ASSERT (((UINTN) Buffer & (sizeof (UINT32) - 1)) == 0);
  243. ReturnBuffer = (UINT32 *) Buffer;
  244. while (Length > 0) {
  245. MmioWrite32 (StartAddress, *(Buffer++));
  246. StartAddress += sizeof (UINT32);
  247. Length -= sizeof (UINT32);
  248. }
  249. return ReturnBuffer;
  250. }
  251. /**
  252. Copy data from system memory to MMIO region by using 64-bit access.
  253. Copy data from system memory specified by Buffer to MMIO region specified
  254. by starting address StartAddress by using 64-bit access. The total number
  255. of byte to be copied is specified by Length. Buffer is returned.
  256. If StartAddress is not aligned on a 64-bit boundary, then ASSERT().
  257. If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
  258. If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
  259. If Length is not aligned on a 64-bit boundary, then ASSERT().
  260. If Buffer is not aligned on a 64-bit boundary, then ASSERT().
  261. @param StartAddress Starting address for the MMIO region to be copied to.
  262. @param Length Size in bytes of the copy.
  263. @param Buffer Pointer to a system memory buffer containing the data to write.
  264. @return Buffer
  265. **/
  266. UINT64 *
  267. EFIAPI
  268. MmioWriteBuffer64 (
  269. IN UINTN StartAddress,
  270. IN UINTN Length,
  271. IN CONST UINT64 *Buffer
  272. )
  273. {
  274. UINT64 *ReturnBuffer;
  275. ASSERT ((StartAddress & (sizeof (UINT64) - 1)) == 0);
  276. ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
  277. ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
  278. ASSERT ((Length & (sizeof (UINT64) - 1)) == 0);
  279. ASSERT (((UINTN) Buffer & (sizeof (UINT64) - 1)) == 0);
  280. ReturnBuffer = (UINT64 *) Buffer;
  281. while (Length > 0) {
  282. MmioWrite64 (StartAddress, *(Buffer++));
  283. StartAddress += sizeof (UINT64);
  284. Length -= sizeof (UINT64);
  285. }
  286. return ReturnBuffer;
  287. }