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