]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibIpf.c
Fix a prototype bug.
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibIpf.c
1 /** @file
2 Common I/O Library routines.
3
4 Copyright (c) 2006 - 2008, 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 #include "BaseIoLibIntrinsicInternal.h"
17 #include <Library/PcdLib.h>
18
19 #define MAP_PORT_BASE_TO_MEM(_Port) \
20 ((((_Port) & 0xfffc) << 10) | ((_Port) & 0x0fff))
21
22 /**
23 Translates I/O port address to memory address.
24
25 This function translates I/O port address to memory address by adding the 64MB
26 aligned I/O Port space to the I/O address.
27 If I/O Port space base is not 64MB aligned, then ASSERT ().
28
29 @param Port The I/O port to read.
30
31 @return The memory address.
32
33 **/
34 UINTN
35 InternalGetMemoryMapAddress (
36 IN UINTN Port
37 )
38 {
39 UINTN Address;
40 UINTN IoBlockBaseAddress;
41
42 Address = MAP_PORT_BASE_TO_MEM (Port);
43 IoBlockBaseAddress = PcdGet64(PcdIoBlockBaseAddressForIpf);
44
45 //
46 // Make sure that the I/O Port space base is 64MB aligned.
47 //
48 ASSERT ((IoBlockBaseAddress & 0x3ffffff) == 0);
49 Address += IoBlockBaseAddress;
50
51 return Address;
52 }
53
54 /**
55 Reads a 8-bit I/O port.
56
57 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
58 This function must guarantee that all I/O read and write operations are
59 serialized.
60
61 @param Port The I/O port to read.
62
63 @return The value read from Port.
64
65 **/
66 UINT8
67 EFIAPI
68 IoRead8 (
69 IN UINTN Port
70 )
71 {
72 return MmioRead8 (InternalGetMemoryMapAddress (Port));
73 }
74
75 /**
76 Reads a 16-bit I/O port.
77
78 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
79 This function must guarantee that all I/O read and write operations are
80 serialized.
81
82 @param Port The I/O port to read.
83
84 @return The value read from Port.
85
86 **/
87 UINT16
88 EFIAPI
89 IoRead16 (
90 IN UINTN Port
91 )
92 {
93 return MmioRead16 (InternalGetMemoryMapAddress (Port));
94 }
95
96 /**
97 Reads a 32-bit I/O port.
98
99 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
100 This function must guarantee that all I/O read and write operations are
101 serialized.
102
103 @param Port The I/O port to read.
104
105 @return The value read from Port.
106
107 **/
108 UINT32
109 EFIAPI
110 IoRead32 (
111 IN UINTN Port
112 )
113 {
114 return MmioRead32 (InternalGetMemoryMapAddress (Port));
115 }
116
117 /**
118 Reads a 64-bit I/O port.
119
120 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
121 This function must guarantee that all I/O read and write operations are
122 serialized.
123
124 If 64-bit I/O port operations are not supported, then ASSERT().
125
126 @param Port The I/O port to read.
127
128 @return The value read from Port.
129
130 **/
131 UINT64
132 EFIAPI
133 IoRead64 (
134 IN UINTN Port
135 )
136 {
137 ASSERT (FALSE);
138 return 0;
139 }
140
141 /**
142 Writes a 8-bit I/O port.
143
144 Writes the 8-bit I/O port specified by Port with the value specified by Value
145 and returns Value. This function must guarantee that all I/O read and write
146 operations are serialized.
147
148 @param Port The I/O port to write.
149 @param Data The value to write to the I/O port.
150
151 @return The value written to the I/O port. It equals to the
152 input Value instead of the actual value read back from
153 the I/O port.
154
155 **/
156 UINT8
157 EFIAPI
158 IoWrite8 (
159 IN UINTN Port,
160 IN UINT8 Data
161 )
162 {
163 return MmioWrite8 (InternalGetMemoryMapAddress (Port), Data);
164 }
165
166 /**
167 Writes a 16-bit I/O port.
168
169 Writes the 16-bit I/O port specified by Port with the value specified by Value
170 and returns Value. This function must guarantee that all I/O read and write
171 operations are serialized.
172
173 @param Port The I/O port to write.
174 @param Data The value to write to the I/O port.
175
176 @return The value written to the I/O port. It equals to the
177 input Value instead of the actual value read back from
178 the I/O port.
179
180 **/
181 UINT16
182 EFIAPI
183 IoWrite16 (
184 IN UINTN Port,
185 IN UINT16 Data
186 )
187 {
188 return MmioWrite16 (InternalGetMemoryMapAddress (Port), Data);
189 }
190
191 /**
192 Writes a 32-bit I/O port.
193
194 Writes the 32-bit I/O port specified by Port with the value specified by Value
195 and returns Value. This function must guarantee that all I/O read and write
196 operations are serialized.
197
198 @param Port The I/O port to write.
199 @param Data The value to write to the I/O port.
200
201 @return The value written to the I/O port. It equals to the
202 input Value instead of the actual value read back from
203 the I/O port.
204
205 **/
206 UINT32
207 EFIAPI
208 IoWrite32 (
209 IN UINTN Port,
210 IN UINT32 Data
211 )
212 {
213 return MmioWrite32 (InternalGetMemoryMapAddress (Port), Data);
214 }
215
216 /**
217 Writes a 64-bit I/O port.
218
219 Writes the 64-bit I/O port specified by Port with the value specified by Value
220 and returns Value. This function must guarantee that all I/O read and write
221 operations are serialized.
222
223 If 64-bit I/O port operations are not supported, then ASSERT().
224
225 @param Port The I/O port to write.
226 @param Value The value to write to the I/O port.
227
228 @return The value written to the I/O port. It equals to the
229 input Value instead of the actual value read back from
230 the I/O port.
231
232 **/
233 UINT64
234 EFIAPI
235 IoWrite64 (
236 IN UINTN Port,
237 IN UINT64 Value
238 )
239 {
240 ASSERT (FALSE);
241 return 0;
242 }
243
244 /**
245 Reads a 8-bit MMIO register.
246
247 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
248 returned. This function must guarantee that all MMIO read and write
249 operations are serialized.
250
251 @param Address The MMIO register to read.
252
253 @return The value read from Address.
254
255 **/
256 UINT8
257 EFIAPI
258 MmioRead8 (
259 IN UINT64 Address
260 )
261 {
262 UINT8 Data;
263
264 Address |= BIT63;
265
266 MemoryFence ();
267 Data = *((volatile UINT8 *) Address);
268 MemoryFence ();
269
270 return Data;
271 }
272
273 /**
274 Reads a 16-bit MMIO register.
275
276 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
277 returned. This function must guarantee that all MMIO read and write
278 operations are serialized.
279
280 @param Address The MMIO register to read.
281
282 @return The value read from Address.
283
284 **/
285 UINT16
286 EFIAPI
287 MmioRead16 (
288 IN UINT64 Address
289 )
290 {
291 UINT16 Data;
292
293 //
294 // Make sure that Address is 16-bit aligned.
295 //
296 ASSERT ((Address & 1) == 0);
297
298 Address |= BIT63;
299
300 MemoryFence ();
301 Data = *((volatile UINT16 *) Address);
302 MemoryFence ();
303
304 return Data;
305 }
306
307 /**
308 Reads a 32-bit MMIO register.
309
310 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
311 returned. This function must guarantee that all MMIO read and write
312 operations are serialized.
313
314 @param Address The MMIO register to read.
315
316 @return The value read from Address.
317
318 **/
319 UINT32
320 EFIAPI
321 MmioRead32 (
322 IN UINT64 Address
323 )
324 {
325 UINT32 Data;
326
327 //
328 // Make sure that Address is 32-bit aligned.
329 //
330 ASSERT ((Address & 3) == 0);
331
332 Address |= BIT63;
333
334 MemoryFence ();
335 Data = *((volatile UINT32 *) Address);
336 MemoryFence ();
337
338 return Data;
339 }
340
341 /**
342 Reads a 64-bit MMIO register.
343
344 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
345 returned. This function must guarantee that all MMIO read and write
346 operations are serialized.
347
348 @param Address The MMIO register to read.
349
350 @return The value read from Address.
351
352 **/
353 UINT64
354 EFIAPI
355 MmioRead64 (
356 IN UINT64 Address
357 )
358 {
359 UINT64 Data;
360
361 //
362 // Make sure that Address is 64-bit aligned.
363 //
364 ASSERT ((Address & 7) == 0);
365
366 Address |= BIT63;
367
368 MemoryFence ();
369 Data = *((volatile UINT64 *) Address);
370 MemoryFence ();
371
372 return Data;
373
374 }
375
376 /**
377 Writes a 8-bit MMIO register.
378
379 Writes the 8-bit MMIO register specified by Address with the value specified
380 by Value and returns Value. This function must guarantee that all MMIO read
381 and write operations are serialized.
382
383 @param Address The MMIO register to write.
384 @param Data The value to write to the MMIO register.
385
386 @return The value written to the Mmio. It equals to the
387 input Value instead of the actual value read back from
388 the Mmio.
389
390 **/
391 UINT8
392 EFIAPI
393 MmioWrite8 (
394 IN UINT64 Address,
395 IN UINT8 Data
396 )
397 {
398 Address |= BIT63;
399
400 MemoryFence ();
401 *((volatile UINT8 *) Address) = Data;
402 MemoryFence ();
403
404 return Data;
405 }
406
407 /**
408 Writes a 16-bit MMIO register.
409
410 Writes the 16-bit MMIO register specified by Address with the value specified
411 by Value and returns Value. This function must guarantee that all MMIO read
412 and write operations are serialized.
413
414 @param Address The MMIO register to write.
415 @param Data The value to write to the MMIO register.
416
417 @return The value written to the Mmio. It equals to the
418 input Value instead of the actual value read back from
419 the Mmio.
420
421 **/
422 UINT16
423 EFIAPI
424 MmioWrite16 (
425 IN UINT64 Address,
426 IN UINT16 Data
427 )
428 {
429 //
430 // Make sure that Address is 16-bit aligned.
431 //
432 ASSERT ((Address & 1) == 0);
433
434 Address |= BIT63;
435
436 MemoryFence ();
437 *((volatile UINT16 *) Address) = Data;
438 MemoryFence ();
439
440 return Data;
441 }
442
443 /**
444 Writes a 32-bit MMIO register.
445
446 Writes the 32-bit MMIO register specified by Address with the value specified
447 by Value and returns Value. This function must guarantee that all MMIO read
448 and write operations are serialized.
449
450 @param Address The MMIO register to write.
451 @param Data The value to write to the MMIO register.
452
453 @return The value written to the Mmio. It equals to the
454 input Value instead of the actual value read back from
455 the Mmio.
456
457 **/
458 UINT32
459 EFIAPI
460 MmioWrite32 (
461 IN UINT64 Address,
462 IN UINT32 Data
463 )
464 {
465 //
466 // Make sure that Address is 32-bit aligned.
467 //
468 ASSERT ((Address & 3) == 0);
469
470 Address |= BIT63;
471
472 MemoryFence ();
473 *((volatile UINT32 *) Address) = Data;
474 MemoryFence ();
475
476 return Data;
477 }
478
479 /**
480 Writes a 64-bit MMIO register.
481
482 Writes the 64-bit MMIO register specified by Address with the value specified
483 by Value and returns Value. This function must guarantee that all MMIO read
484 and write operations are serialized.
485
486 @param Address The MMIO register to write.
487 @param Data The value to write to the MMIO register.
488
489 @return The value written to the Mmio. It equals to the
490 input Value instead of the actual value read back from
491 the Mmio.
492
493 **/
494 UINT64
495 EFIAPI
496 MmioWrite64 (
497 IN UINT64 Address,
498 IN UINT64 Data
499 )
500 {
501 //
502 // Make sure that Address is 64-bit aligned.
503 //
504 ASSERT ((Address & 7) == 0);
505
506 Address |= BIT63;
507
508 MemoryFence ();
509 *((volatile UINT64 *) Address) = Data;
510 MemoryFence ();
511
512 return Data;
513 }