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