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