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