]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiIoLibCpuIo/IoLibMmioBuffer.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / PeiIoLibCpuIo / IoLibMmioBuffer.c
CommitLineData
11f43dfd 1/** @file\r
2 I/O Library MMIO Buffer Functions.\r
4ca0802e 3 The implementations are based on EFI_PEI_SERVICE->CpuIo interface.\r
11f43dfd 4\r
9095d37b 5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
9344f092 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
11f43dfd 7\r
8**/\r
9\r
c892d846 10\r
11f43dfd 11#include <PiPei.h>\r
c892d846 12\r
11f43dfd 13#include <Library/IoLib.h>\r
14#include <Library/DebugLib.h>\r
15#include <Library/BaseLib.h>\r
16#include <Library/PeiServicesTablePointerLib.h>\r
17\r
18/**\r
19 Copy data from MMIO region to system memory by using 8-bit access.\r
20\r
9095d37b
LG
21 Copy data from MMIO region specified by starting address StartAddress\r
22 to system memory specified by Buffer by using 8-bit access. The total\r
11f43dfd 23 number of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b
LG
24\r
25 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 26 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
27\r
28\r
2fc59a00 29 @param StartAddress The starting address for the MMIO region to be copied from.\r
80f0c0c4 30 @param Length The size, in bytes, of Buffer.\r
2fc59a00 31 @param Buffer The pointer to a system memory buffer receiving the data read.\r
11f43dfd 32\r
80f0c0c4 33 @return Buffer\r
11f43dfd 34\r
35**/\r
36UINT8 *\r
37EFIAPI\r
38MmioReadBuffer8 (\r
39 IN UINTN StartAddress,\r
40 IN UINTN Length,\r
41 OUT UINT8 *Buffer\r
42 )\r
43{\r
44 UINT8 *ReturnBuffer;\r
45\r
46 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
47 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
9095d37b 48\r
11f43dfd 49 ReturnBuffer = Buffer;\r
9095d37b 50\r
42eedea9 51 while (Length-- != 0) {\r
11f43dfd 52 *(Buffer++) = MmioRead8 (StartAddress++);\r
53 }\r
54\r
55 return ReturnBuffer;\r
56}\r
57\r
58/**\r
59 Copy data from MMIO region to system memory by using 16-bit access.\r
60\r
9095d37b
LG
61 Copy data from MMIO region specified by starting address StartAddress\r
62 to system memory specified by Buffer by using 16-bit access. The total\r
11f43dfd 63 number of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b 64\r
11f43dfd 65 If StartAddress is not aligned on a 16-bit boundary, then ASSERT().\r
66\r
9095d37b 67 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 68 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
69\r
70 If Length is not aligned on a 16-bit boundary, then ASSERT().\r
71 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
72\r
2fc59a00 73 @param StartAddress The starting address for the MMIO region to be copied from.\r
80f0c0c4 74 @param Length The size, in bytes, of Buffer.\r
2fc59a00 75 @param Buffer The pointer to a system memory buffer receiving the data read.\r
11f43dfd 76\r
80f0c0c4 77 @return Buffer\r
11f43dfd 78\r
79**/\r
80UINT16 *\r
81EFIAPI\r
82MmioReadBuffer16 (\r
83 IN UINTN StartAddress,\r
84 IN UINTN Length,\r
85 OUT UINT16 *Buffer\r
86 )\r
87{\r
88 UINT16 *ReturnBuffer;\r
89\r
90 ASSERT ((StartAddress & (sizeof (UINT16) - 1)) == 0);\r
9095d37b 91\r
11f43dfd 92 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
93 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
94\r
95 ASSERT ((Length & (sizeof (UINT16) - 1)) == 0);\r
96 ASSERT (((UINTN) Buffer & (sizeof (UINT16) - 1)) == 0);\r
9095d37b 97\r
11f43dfd 98 ReturnBuffer = Buffer;\r
9095d37b 99\r
42eedea9 100 while (Length != 0) {\r
11f43dfd 101 *(Buffer++) = MmioRead16 (StartAddress);\r
102 StartAddress += sizeof (UINT16);\r
103 Length -= sizeof (UINT16);\r
104 }\r
105\r
106 return ReturnBuffer;\r
107}\r
108\r
109/**\r
110 Copy data from MMIO region to system memory by using 32-bit access.\r
111\r
9095d37b
LG
112 Copy data from MMIO region specified by starting address StartAddress\r
113 to system memory specified by Buffer by using 32-bit access. The total\r
11f43dfd 114 number of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b 115\r
11f43dfd 116 If StartAddress is not aligned on a 32-bit boundary, then ASSERT().\r
117\r
9095d37b 118 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 119 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
120\r
121 If Length is not aligned on a 32-bit boundary, then ASSERT().\r
122 If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r
123\r
2fc59a00 124 @param StartAddress The starting address for the MMIO region to be copied from.\r
80f0c0c4 125 @param Length The size, in bytes, of Buffer.\r
2fc59a00 126 @param Buffer The pointer to a system memory buffer receiving the data read.\r
11f43dfd 127\r
80f0c0c4 128 @return Buffer\r
11f43dfd 129\r
130**/\r
131UINT32 *\r
132EFIAPI\r
133MmioReadBuffer32 (\r
134 IN UINTN StartAddress,\r
135 IN UINTN Length,\r
136 OUT UINT32 *Buffer\r
137 )\r
138{\r
139 UINT32 *ReturnBuffer;\r
140\r
141 ASSERT ((StartAddress & (sizeof (UINT32) - 1)) == 0);\r
9095d37b 142\r
11f43dfd 143 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
144 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
145\r
146 ASSERT ((Length & (sizeof (UINT32) - 1)) == 0);\r
147 ASSERT (((UINTN) Buffer & (sizeof (UINT32) - 1)) == 0);\r
9095d37b 148\r
11f43dfd 149 ReturnBuffer = Buffer;\r
9095d37b 150\r
42eedea9 151 while (Length != 0) {\r
11f43dfd 152 *(Buffer++) = MmioRead32 (StartAddress);\r
153 StartAddress += sizeof (UINT32);\r
154 Length -= sizeof (UINT32);\r
155 }\r
156\r
157 return ReturnBuffer;\r
158}\r
159\r
160/**\r
161 Copy data from MMIO region to system memory by using 64-bit access.\r
162\r
9095d37b
LG
163 Copy data from MMIO region specified by starting address StartAddress\r
164 to system memory specified by Buffer by using 64-bit access. The total\r
11f43dfd 165 number of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b 166\r
11f43dfd 167 If StartAddress is not aligned on a 64-bit boundary, then ASSERT().\r
168\r
9095d37b 169 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 170 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
171\r
172 If Length is not aligned on a 64-bit boundary, then ASSERT().\r
173 If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r
174\r
2fc59a00 175 @param StartAddress The starting address for the MMIO region to be copied from.\r
80f0c0c4 176 @param Length The size, in bytes, of Buffer.\r
2fc59a00 177 @param Buffer The pointer to a system memory buffer receiving the data read.\r
11f43dfd 178\r
80f0c0c4 179 @return Buffer\r
11f43dfd 180\r
181**/\r
182UINT64 *\r
183EFIAPI\r
184MmioReadBuffer64 (\r
185 IN UINTN StartAddress,\r
186 IN UINTN Length,\r
187 OUT UINT64 *Buffer\r
188 )\r
189{\r
190 UINT64 *ReturnBuffer;\r
191\r
192 ASSERT ((StartAddress & (sizeof (UINT64) - 1)) == 0);\r
9095d37b 193\r
11f43dfd 194 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
195 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
196\r
197 ASSERT ((Length & (sizeof (UINT64) - 1)) == 0);\r
198 ASSERT (((UINTN) Buffer & (sizeof (UINT64) - 1)) == 0);\r
9095d37b 199\r
11f43dfd 200 ReturnBuffer = Buffer;\r
9095d37b 201\r
42eedea9 202 while (Length != 0) {\r
11f43dfd 203 *(Buffer++) = MmioRead64 (StartAddress);\r
204 StartAddress += sizeof (UINT64);\r
205 Length -= sizeof (UINT64);\r
206 }\r
207\r
208 return ReturnBuffer;\r
209}\r
210\r
211\r
212/**\r
213 Copy data from system memory to MMIO region by using 8-bit access.\r
214\r
9095d37b
LG
215 Copy data from system memory specified by Buffer to MMIO region specified\r
216 by starting address StartAddress by using 8-bit access. The total number\r
11f43dfd 217 of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b
LG
218\r
219 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 220 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().\r
221\r
222\r
2fc59a00 223 @param StartAddress The starting address for the MMIO region to be copied to.\r
80f0c0c4 224 @param Length The size, in bytes, of Buffer.\r
2fc59a00 225 @param Buffer The pointer to a system memory buffer containing the data to write.\r
11f43dfd 226\r
3222ffc0 227 @return Buffer\r
11f43dfd 228\r
229**/\r
230UINT8 *\r
231EFIAPI\r
232MmioWriteBuffer8 (\r
233 IN UINTN StartAddress,\r
234 IN UINTN Length,\r
235 IN CONST UINT8 *Buffer\r
236 )\r
237{\r
238 VOID* ReturnBuffer;\r
239\r
240 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
241 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
9095d37b 242\r
11f43dfd 243 ReturnBuffer = (UINT8 *) Buffer;\r
9095d37b 244\r
42eedea9 245 while (Length-- != 0) {\r
11f43dfd 246 MmioWrite8 (StartAddress++, *(Buffer++));\r
247 }\r
248\r
249 return ReturnBuffer;\r
9095d37b 250\r
11f43dfd 251}\r
252\r
253/**\r
254 Copy data from system memory to MMIO region by using 16-bit access.\r
255\r
9095d37b
LG
256 Copy data from system memory specified by Buffer to MMIO region specified\r
257 by starting address StartAddress by using 16-bit access. The total number\r
3222ffc0 258 of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b 259\r
11f43dfd 260 If StartAddress is not aligned on a 16-bit boundary, then ASSERT().\r
261\r
9095d37b 262 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 263 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().\r
264\r
265 If Length is not aligned on a 16-bit boundary, then ASSERT().\r
266\r
267 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
268\r
2fc59a00 269 @param StartAddress The starting address for the MMIO region to be copied to.\r
80f0c0c4 270 @param Length The size, in bytes, of Buffer.\r
2fc59a00 271 @param Buffer The pointer to a system memory buffer containing the data to write.\r
11f43dfd 272\r
3222ffc0 273 @return Buffer\r
11f43dfd 274\r
275**/\r
276UINT16 *\r
277EFIAPI\r
278MmioWriteBuffer16 (\r
279 IN UINTN StartAddress,\r
280 IN UINTN Length,\r
281 IN CONST UINT16 *Buffer\r
282 )\r
283{\r
284 UINT16 *ReturnBuffer;\r
285\r
286 ASSERT ((StartAddress & (sizeof (UINT16) - 1)) == 0);\r
9095d37b 287\r
11f43dfd 288 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
289 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
290\r
291 ASSERT ((Length & (sizeof (UINT16) - 1)) == 0);\r
292 ASSERT (((UINTN) Buffer & (sizeof (UINT16) - 1)) == 0);\r
293\r
294 ReturnBuffer = (UINT16 *) Buffer;\r
9095d37b 295\r
42eedea9 296 while (Length != 0) {\r
11f43dfd 297 MmioWrite16 (StartAddress, *(Buffer++));\r
9095d37b 298\r
11f43dfd 299 StartAddress += sizeof (UINT16);\r
300 Length -= sizeof (UINT16);\r
301 }\r
302\r
303 return ReturnBuffer;\r
304}\r
305\r
306\r
307/**\r
308 Copy data from system memory to MMIO region by using 32-bit access.\r
309\r
9095d37b
LG
310 Copy data from system memory specified by Buffer to MMIO region specified\r
311 by starting address StartAddress by using 32-bit access. The total number\r
3222ffc0 312 of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b 313\r
11f43dfd 314 If StartAddress is not aligned on a 32-bit boundary, then ASSERT().\r
315\r
9095d37b 316 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 317 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().\r
318\r
319 If Length is not aligned on a 32-bit boundary, then ASSERT().\r
320\r
321 If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r
322\r
2fc59a00 323 @param StartAddress The starting address for the MMIO region to be copied to.\r
80f0c0c4 324 @param Length The size, in bytes, of Buffer.\r
2fc59a00 325 @param Buffer The pointer to a system memory buffer containing the data to write.\r
11f43dfd 326\r
3222ffc0 327 @return Buffer\r
11f43dfd 328\r
329**/\r
330UINT32 *\r
331EFIAPI\r
332MmioWriteBuffer32 (\r
333 IN UINTN StartAddress,\r
334 IN UINTN Length,\r
335 IN CONST UINT32 *Buffer\r
336 )\r
337{\r
338 UINT32 *ReturnBuffer;\r
339\r
340 ASSERT ((StartAddress & (sizeof (UINT32) - 1)) == 0);\r
9095d37b 341\r
11f43dfd 342 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
343 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
344\r
345 ASSERT ((Length & (sizeof (UINT32) - 1)) == 0);\r
346 ASSERT (((UINTN) Buffer & (sizeof (UINT32) - 1)) == 0);\r
347\r
348 ReturnBuffer = (UINT32 *) Buffer;\r
9095d37b 349\r
42eedea9 350 while (Length != 0) {\r
11f43dfd 351 MmioWrite32 (StartAddress, *(Buffer++));\r
9095d37b 352\r
11f43dfd 353 StartAddress += sizeof (UINT32);\r
354 Length -= sizeof (UINT32);\r
355 }\r
356\r
357 return ReturnBuffer;\r
358}\r
359\r
360/**\r
361 Copy data from system memory to MMIO region by using 64-bit access.\r
362\r
9095d37b
LG
363 Copy data from system memory specified by Buffer to MMIO region specified\r
364 by starting address StartAddress by using 64-bit access. The total number\r
3222ffc0 365 of byte to be copied is specified by Length. Buffer is returned.\r
9095d37b 366\r
11f43dfd 367 If StartAddress is not aligned on a 64-bit boundary, then ASSERT().\r
368\r
9095d37b 369 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().\r
11f43dfd 370 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().\r
371\r
372 If Length is not aligned on a 64-bit boundary, then ASSERT().\r
373\r
374 If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r
375\r
2fc59a00 376 @param StartAddress The starting address for the MMIO region to be copied to.\r
80f0c0c4 377 @param Length The size, in bytes, of Buffer.\r
2fc59a00 378 @param Buffer The pointer to a system memory buffer containing the data to write.\r
11f43dfd 379\r
3222ffc0 380 @return Buffer\r
11f43dfd 381\r
382**/\r
383UINT64 *\r
384EFIAPI\r
385MmioWriteBuffer64 (\r
386 IN UINTN StartAddress,\r
387 IN UINTN Length,\r
388 IN CONST UINT64 *Buffer\r
389 )\r
390{\r
391 UINT64 *ReturnBuffer;\r
392\r
393 ASSERT ((StartAddress & (sizeof (UINT64) - 1)) == 0);\r
9095d37b 394\r
11f43dfd 395 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));\r
396 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));\r
397\r
398 ASSERT ((Length & (sizeof (UINT64) - 1)) == 0);\r
399 ASSERT (((UINTN) Buffer & (sizeof (UINT64) - 1)) == 0);\r
400\r
401 ReturnBuffer = (UINT64 *) Buffer;\r
9095d37b 402\r
42eedea9 403 while (Length != 0) {\r
11f43dfd 404 MmioWrite64 (StartAddress, *(Buffer++));\r
9095d37b 405\r
11f43dfd 406 StartAddress += sizeof (UINT64);\r
407 Length -= sizeof (UINT64);\r
408 }\r
409\r
410 return ReturnBuffer;\r
411}\r
412\r