1) Added BIT0, BIT1, …, BIT63 to the Base Defines
[mirror_edk2.git] / MdePkg / Library / DxeIoLibCpuIo / IoLibMmioBuffer.c
1 /** @file
2 I/O Library MMIO Buffer Functions.
3
4 Copyright (c) 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 /**
16 Copy data from MMIO region to system memory by using 8-bit access.
17
18 Copy data from MMIO region specified by starting address StartAddress
19 to system memory specified by Buffer by using 8-bit access. The total
20 number of byte to be copied is specified by Length. Buffer is returned.
21
22 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
23 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
24
25
26 @param StartAddress Starting address for the MMIO region to be copied from.
27 @param Length Size in bytes of the copy.
28 @param Buffer Pointer to a system memory buffer receiving the data read.
29
30 @return Buffer
31
32 **/
33 UINT8 *
34 EFIAPI
35 MmioReadBuffer8 (
36 IN UINTN StartAddress,
37 IN UINTN Length,
38 OUT UINT8 *Buffer
39 )
40 {
41 UINT8 *ReturnBuffer;
42
43 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
44 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
45
46 ReturnBuffer = Buffer;
47
48 while (Length--) {
49 *(Buffer++) = MmioRead8 (StartAddress++);
50 }
51
52 return ReturnBuffer;
53 }
54
55 /**
56 Copy data from MMIO region to system memory by using 16-bit access.
57
58 Copy data from MMIO region specified by starting address StartAddress
59 to system memory specified by Buffer by using 16-bit access. The total
60 number of byte to be copied is specified by Length. Buffer is returned.
61
62 If StartAddress is not aligned on a 16-bit boundary, then ASSERT().
63
64 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
65 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
66
67 If Length is not aligned on a 16-bit boundary, then ASSERT().
68 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
69
70 @param StartAddress Starting address for the MMIO region to be copied from.
71 @param Length Size in bytes of the copy.
72 @param Buffer Pointer to a system memory buffer receiving the data read.
73
74 @return Buffer
75
76 **/
77 UINT16 *
78 EFIAPI
79 MmioReadBuffer16 (
80 IN UINTN StartAddress,
81 IN UINTN Length,
82 OUT UINT16 *Buffer
83 )
84 {
85 UINT16 *ReturnBuffer;
86
87 ASSERT ((StartAddress & (sizeof (UINT16) - 1)) == 0);
88
89 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
90 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
91
92 ASSERT ((Length & (sizeof (UINT16) - 1)) == 0);
93 ASSERT (((UINTN) Buffer & (sizeof (UINT16) - 1)) == 0);
94
95 ReturnBuffer = Buffer;
96
97 while (Length) {
98 *(Buffer++) = MmioRead16 (StartAddress);
99 StartAddress += sizeof (UINT16);
100 Length -= sizeof (UINT16);
101 }
102
103 return ReturnBuffer;
104 }
105
106 /**
107 Copy data from MMIO region to system memory by using 32-bit access.
108
109 Copy data from MMIO region specified by starting address StartAddress
110 to system memory specified by Buffer by using 32-bit access. The total
111 number of byte to be copied is specified by Length. Buffer is returned.
112
113 If StartAddress is not aligned on a 32-bit boundary, then ASSERT().
114
115 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
116 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
117
118 If Length is not aligned on a 32-bit boundary, then ASSERT().
119 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
120
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
125 @return Buffer
126
127 **/
128 UINT32 *
129 EFIAPI
130 MmioReadBuffer32 (
131 IN UINTN StartAddress,
132 IN UINTN Length,
133 OUT UINT32 *Buffer
134 )
135 {
136 UINT32 *ReturnBuffer;
137
138 ASSERT ((StartAddress & (sizeof (UINT32) - 1)) == 0);
139
140 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
141 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
142
143 ASSERT ((Length & (sizeof (UINT32) - 1)) == 0);
144 ASSERT (((UINTN) Buffer & (sizeof (UINT32) - 1)) == 0);
145
146 ReturnBuffer = Buffer;
147
148 while (Length) {
149 *(Buffer++) = MmioRead32 (StartAddress);
150 StartAddress += sizeof (UINT32);
151 Length -= sizeof (UINT32);
152 }
153
154 return ReturnBuffer;
155 }
156
157 /**
158 Copy data from MMIO region to system memory by using 64-bit access.
159
160 Copy data from MMIO region specified by starting address StartAddress
161 to system memory specified by Buffer by using 64-bit access. The total
162 number of byte to be copied is specified by Length. Buffer is returned.
163
164 If StartAddress is not aligned on a 64-bit boundary, then ASSERT().
165
166 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
167 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
168
169 If Length is not aligned on a 64-bit boundary, then ASSERT().
170 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
171
172 @param StartAddress Starting address for the MMIO region to be copied from.
173 @param Length Size in bytes of the copy.
174 @param Buffer Pointer to a system memory buffer receiving the data read.
175
176 @return Buffer
177
178 **/
179 UINT64 *
180 EFIAPI
181 MmioReadBuffer64 (
182 IN UINTN StartAddress,
183 IN UINTN Length,
184 OUT UINT64 *Buffer
185 )
186 {
187 UINT64 *ReturnBuffer;
188
189 ASSERT ((StartAddress & (sizeof (UINT64) - 1)) == 0);
190
191 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
192 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
193
194 ASSERT ((Length & (sizeof (UINT64) - 1)) == 0);
195 ASSERT (((UINTN) Buffer & (sizeof (UINT64) - 1)) == 0);
196
197 ReturnBuffer = Buffer;
198
199 while (Length) {
200 *(Buffer++) = MmioRead64 (StartAddress);
201 StartAddress += sizeof (UINT64);
202 Length -= sizeof (UINT64);
203 }
204
205 return ReturnBuffer;
206 }
207
208
209 /**
210 Copy data from system memory to MMIO region by using 8-bit access.
211
212 Copy data from system memory specified by Buffer to MMIO region specified
213 by starting address StartAddress by using 8-bit access. The total number
214 of byte to be copied is specified by Length. Buffer is returned.
215
216 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
217 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
218
219
220 @param StartAddress Starting address for the MMIO region to be copied to.
221 @param Length Size in bytes of the copy.
222 @param Buffer Pointer to a system memory buffer containing the data to write.
223
224 @return Size in bytes of the copy.
225
226 **/
227 UINT8 *
228 EFIAPI
229 MmioWriteBuffer8 (
230 IN UINTN StartAddress,
231 IN UINTN Length,
232 IN CONST UINT8 *Buffer
233 )
234 {
235 VOID* ReturnBuffer;
236
237 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
238 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
239
240 ReturnBuffer = (UINT8 *) Buffer;
241
242 while (Length--) {
243 MmioWrite8 (StartAddress++, *(Buffer++));
244 }
245
246 return ReturnBuffer;
247
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. Length 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 Starting address for the MMIO region to be copied to.
267 @param Length Size in bytes of the copy.
268 @param Buffer Pointer to a system memory buffer containing the data to write.
269
270 @return Size in bytes of the copy.
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) {
294 MmioWrite16 (StartAddress, *(Buffer++));
295
296 StartAddress += sizeof (UINT16);
297 Length -= sizeof (UINT16);
298 }
299
300 return ReturnBuffer;
301 }
302
303
304 /**
305 Copy data from system memory to MMIO region by using 32-bit access.
306
307 Copy data from system memory specified by Buffer to MMIO region specified
308 by starting address StartAddress by using 32-bit access. The total number
309 of byte to be copied is specified by Length. Length is returned.
310
311 If StartAddress is not aligned on a 32-bit boundary, then ASSERT().
312
313 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
314 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
315
316 If Length is not aligned on a 32-bit boundary, then ASSERT().
317
318 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
319
320 @param StartAddress Starting address for the MMIO region to be copied to.
321 @param Length Size in bytes of the copy.
322 @param Buffer Pointer to a system memory buffer containing the data to write.
323
324 @return Size in bytes of the copy.
325
326 **/
327 UINT32 *
328 EFIAPI
329 MmioWriteBuffer32 (
330 IN UINTN StartAddress,
331 IN UINTN Length,
332 IN CONST UINT32 *Buffer
333 )
334 {
335 UINT32 *ReturnBuffer;
336
337 ASSERT ((StartAddress & (sizeof (UINT32) - 1)) == 0);
338
339 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
340 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
341
342 ASSERT ((Length & (sizeof (UINT32) - 1)) == 0);
343 ASSERT (((UINTN) Buffer & (sizeof (UINT32) - 1)) == 0);
344
345 ReturnBuffer = (UINT32 *) Buffer;
346
347 while (Length) {
348 MmioWrite32 (StartAddress, *(Buffer++));
349
350 StartAddress += sizeof (UINT32);
351 Length -= sizeof (UINT32);
352 }
353
354 return ReturnBuffer;
355 }
356
357 /**
358 Copy data from system memory to MMIO region by using 64-bit access.
359
360 Copy data from system memory specified by Buffer to MMIO region specified
361 by starting address StartAddress by using 64-bit access. The total number
362 of byte to be copied is specified by Length. Length is returned.
363
364 If StartAddress is not aligned on a 64-bit boundary, then ASSERT().
365
366 If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT().
367 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
368
369 If Length is not aligned on a 64-bit boundary, then ASSERT().
370
371 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
372
373 @param StartAddress Starting address for the MMIO region to be copied to.
374 @param Length Size in bytes of the copy.
375 @param Buffer Pointer to a system memory buffer containing the data to write.
376
377 @return Size in bytes of the copy.
378
379 **/
380 UINT64 *
381 EFIAPI
382 MmioWriteBuffer64 (
383 IN UINTN StartAddress,
384 IN UINTN Length,
385 IN CONST UINT64 *Buffer
386 )
387 {
388 UINT64 *ReturnBuffer;
389
390 ASSERT ((StartAddress & (sizeof (UINT64) - 1)) == 0);
391
392 ASSERT ((Length - 1) <= (MAX_ADDRESS - StartAddress));
393 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN) Buffer));
394
395 ASSERT ((Length & (sizeof (UINT64) - 1)) == 0);
396 ASSERT (((UINTN) Buffer & (sizeof (UINT64) - 1)) == 0);
397
398 ReturnBuffer = (UINT64 *) Buffer;
399
400 while (Length) {
401 MmioWrite64 (StartAddress, *(Buffer++));
402
403 StartAddress += sizeof (UINT64);
404 Length -= sizeof (UINT64);
405 }
406
407 return ReturnBuffer;
408 }
409