]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibIpf.c
MdePkg: Expand BaseIoLibIntrinsic (IoLib class) library
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibIpf.c
1 /** @file
2 Common I/O Library routines.
3
4 Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17
18 #include "BaseIoLibIntrinsicInternal.h"
19 #include <Library/PcdLib.h>
20
21 #define MAP_PORT_BASE_TO_MEM(_Port) \
22 ((((_Port) & 0xfffc) << 10) | ((_Port) & 0x0fff))
23
24 /**
25 Translates I/O port address to memory address.
26
27 This function translates I/O port address to memory address by adding the 64MB
28 aligned I/O Port space to the I/O address.
29 If I/O Port space base is not 64MB aligned, then ASSERT ().
30
31 @param Port The I/O port to read.
32
33 @return The memory address.
34
35 **/
36 UINTN
37 InternalGetMemoryMapAddress (
38 IN UINTN Port
39 )
40 {
41 UINTN Address;
42 UINTN IoBlockBaseAddress;
43
44 Address = MAP_PORT_BASE_TO_MEM (Port);
45 IoBlockBaseAddress = PcdGet64(PcdIoBlockBaseAddressForIpf);
46
47 //
48 // Make sure that the I/O Port space base is 64MB aligned.
49 //
50 ASSERT ((IoBlockBaseAddress & 0x3ffffff) == 0);
51 Address += IoBlockBaseAddress;
52
53 return Address;
54 }
55
56 /**
57 Reads an 8-bit I/O port.
58
59 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
60 This function must guarantee that all I/O read and write operations are
61 serialized.
62
63 If 8-bit I/O port operations are not supported, then ASSERT().
64
65 @param Port The I/O port to read.
66
67 @return The value read.
68
69 **/
70 UINT8
71 EFIAPI
72 IoRead8 (
73 IN UINTN Port
74 )
75 {
76 return MmioRead8 (InternalGetMemoryMapAddress (Port));
77 }
78
79 /**
80 Reads a 16-bit I/O port.
81
82 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
83 This function must guarantee that all I/O read and write operations are
84 serialized.
85
86 If 16-bit I/O port operations are not supported, then ASSERT().
87 If Port is not aligned on a 16-bit boundary, then ASSERT().
88
89 @param Port The I/O port to read.
90
91 @return The value read.
92
93 **/
94 UINT16
95 EFIAPI
96 IoRead16 (
97 IN UINTN Port
98 )
99 {
100 return MmioRead16 (InternalGetMemoryMapAddress (Port));
101 }
102
103 /**
104 Reads a 32-bit I/O port.
105
106 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
107 This function must guarantee that all I/O read and write operations are
108 serialized.
109
110 If 32-bit I/O port operations are not supported, then ASSERT().
111 If Port is not aligned on a 32-bit boundary, then ASSERT().
112
113 @param Port The I/O port to read.
114
115 @return The value read.
116
117 **/
118 UINT32
119 EFIAPI
120 IoRead32 (
121 IN UINTN Port
122 )
123 {
124 return MmioRead32 (InternalGetMemoryMapAddress (Port));
125 }
126
127 /**
128 Reads a 64-bit I/O port.
129
130 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
131 This function must guarantee that all I/O read and write operations are
132 serialized.
133
134 If 64-bit I/O port operations are not supported, then ASSERT().
135 If Port is not aligned on a 64-bit boundary, then ASSERT().
136
137 @param Port The I/O port to read.
138
139 @return The value read.
140
141 **/
142 UINT64
143 EFIAPI
144 IoRead64 (
145 IN UINTN Port
146 )
147 {
148 ASSERT (FALSE);
149 return 0;
150 }
151
152
153 /**
154 Writes an 8-bit I/O port.
155
156 Writes the 8-bit I/O port specified by Port with the value specified by Value
157 and returns Value. This function must guarantee that all I/O read and write
158 operations are serialized.
159
160 If 8-bit I/O port operations are not supported, then ASSERT().
161
162 @param Port The I/O port to write.
163 @param Value The value to write to the I/O port.
164
165 @return The value written the I/O port.
166
167 **/
168 UINT8
169 EFIAPI
170 IoWrite8 (
171 IN UINTN Port,
172 IN UINT8 Value
173 )
174 {
175 return MmioWrite8 (InternalGetMemoryMapAddress (Port), Value);
176 }
177
178 /**
179 Writes a 16-bit I/O port.
180
181 Writes the 16-bit I/O port specified by Port with the value specified by Value
182 and returns Value. This function must guarantee that all I/O read and write
183 operations are serialized.
184
185 If 16-bit I/O port operations are not supported, then ASSERT().
186 If Port is not aligned on a 16-bit boundary, then ASSERT().
187
188 @param Port The I/O port to write.
189 @param Value The value to write to the I/O port.
190
191 @return The value written the I/O port.
192
193 **/
194 UINT16
195 EFIAPI
196 IoWrite16 (
197 IN UINTN Port,
198 IN UINT16 Value
199 )
200 {
201 return MmioWrite16 (InternalGetMemoryMapAddress (Port), Value);
202 }
203
204 /**
205 Writes a 32-bit I/O port.
206
207 Writes the 32-bit I/O port specified by Port with the value specified by Value
208 and returns Value. This function must guarantee that all I/O read and write
209 operations are serialized.
210
211 If 32-bit I/O port operations are not supported, then ASSERT().
212 If Port is not aligned on a 32-bit boundary, then ASSERT().
213
214 @param Port The I/O port to write.
215 @param Value The value to write to the I/O port.
216
217 @return The value written the I/O port.
218
219 **/
220 UINT32
221 EFIAPI
222 IoWrite32 (
223 IN UINTN Port,
224 IN UINT32 Value
225 )
226 {
227 return MmioWrite32 (InternalGetMemoryMapAddress (Port), Value);
228 }
229
230 /**
231 Writes a 64-bit I/O port.
232
233 Writes the 64-bit I/O port specified by Port with the value specified by Value
234 and returns Value. This function must guarantee that all I/O read and write
235 operations are serialized.
236
237 If 64-bit I/O port operations are not supported, then ASSERT().
238 If Port is not aligned on a 64-bit boundary, then ASSERT().
239
240 @param Port The I/O port to write.
241 @param Value The value to write to the I/O port.
242
243 @return The value written the I/O port.
244
245 **/
246 UINT64
247 EFIAPI
248 IoWrite64 (
249 IN UINTN Port,
250 IN UINT64 Value
251 )
252 {
253 ASSERT (FALSE);
254 return 0;
255 }
256
257 /**
258 Reads an 8-bit I/O port fifo into a block of memory.
259
260 Reads the 8-bit I/O fifo port specified by Port.
261 The port is read Count times, and the read data is
262 stored in the provided Buffer.
263
264 This function must guarantee that all I/O read and write operations are
265 serialized.
266
267 If 8-bit I/O port operations are not supported, then ASSERT().
268
269 @param Port The I/O port to read.
270 @param Count The number of times to read I/O port.
271 @param Buffer The buffer to store the read data into.
272
273 **/
274 VOID
275 EFIAPI
276 IoReadFifo8 (
277 IN UINTN Port,
278 IN UINTN Count,
279 OUT VOID *Buffer
280 )
281 {
282 UINT8 *Buffer8;
283
284 Buffer8 = (UINT8 *)Buffer;
285 while (Count--) {
286 *Buffer8++ = IoRead8 (Port);
287 }
288 }
289
290 /**
291 Reads a 16-bit I/O port fifo into a block of memory.
292
293 Reads the 16-bit I/O fifo port specified by Port.
294 The port is read Count times, and the read data is
295 stored in the provided Buffer.
296
297 This function must guarantee that all I/O read and write operations are
298 serialized.
299
300 If 16-bit I/O port operations are not supported, then ASSERT().
301
302 @param Port The I/O port to read.
303 @param Count The number of times to read I/O port.
304 @param Buffer The buffer to store the read data into.
305
306 **/
307 VOID
308 EFIAPI
309 IoReadFifo16 (
310 IN UINTN Port,
311 IN UINTN Count,
312 OUT VOID *Buffer
313 )
314 {
315 UINT16 *Buffer16;
316
317 Buffer16 = (UINT16 *)Buffer;
318 while (Count--) {
319 *Buffer16++ = IoRead16 (Port);
320 }
321 }
322
323 /**
324 Reads a 32-bit I/O port fifo into a block of memory.
325
326 Reads the 32-bit I/O fifo port specified by Port.
327 The port is read Count times, and the read data is
328 stored in the provided Buffer.
329
330 This function must guarantee that all I/O read and write operations are
331 serialized.
332
333 If 32-bit I/O port operations are not supported, then ASSERT().
334
335 @param Port The I/O port to read.
336 @param Count The number of times to read I/O port.
337 @param Buffer The buffer to store the read data into.
338
339 **/
340 VOID
341 EFIAPI
342 IoReadFifo32 (
343 IN UINTN Port,
344 IN UINTN Count,
345 OUT VOID *Buffer
346 )
347 {
348 UINT32 *Buffer32;
349
350 Buffer32 = (UINT32 *)Buffer;
351 while (Count--) {
352 *Buffer32++ = IoRead32 (Port);
353 }
354 }
355
356 /**
357 Writes a block of memory into an 8-bit I/O port fifo.
358
359 Writes the 8-bit I/O fifo port specified by Port.
360 The port is written Count times, and the write data is
361 retrieved from the provided Buffer.
362
363 This function must guarantee that all I/O write and write operations are
364 serialized.
365
366 If 8-bit I/O port operations are not supported, then ASSERT().
367
368 @param Port The I/O port to write.
369 @param Count The number of times to write I/O port.
370 @param Buffer The buffer to retrieve the write data from.
371
372 **/
373 VOID
374 EFIAPI
375 IoWriteFifo8 (
376 IN UINTN Port,
377 IN UINTN Count,
378 IN VOID *Buffer
379 )
380 {
381 UINT8 *Buffer8;
382
383 Buffer8 = (UINT8 *)Buffer;
384 while (Count--) {
385 IoWrite8 (Port, *Buffer8++);
386 }
387 }
388
389 /**
390 Writes a block of memory into a 16-bit I/O port fifo.
391
392 Writes the 16-bit I/O fifo port specified by Port.
393 The port is written Count times, and the write data is
394 retrieved from the provided Buffer.
395
396 This function must guarantee that all I/O write and write operations are
397 serialized.
398
399 If 16-bit I/O port operations are not supported, then ASSERT().
400
401 @param Port The I/O port to write.
402 @param Count The number of times to write I/O port.
403 @param Buffer The buffer to retrieve the write data from.
404
405 **/
406 VOID
407 EFIAPI
408 IoWriteFifo16 (
409 IN UINTN Port,
410 IN UINTN Count,
411 IN VOID *Buffer
412 )
413 {
414 UINT16 *Buffer16;
415
416 Buffer16 = (UINT16 *)Buffer;
417 while (Count--) {
418 IoWrite16 (Port, *Buffer16++);
419 }
420 }
421
422 /**
423 Writes a block of memory into a 32-bit I/O port fifo.
424
425 Writes the 32-bit I/O fifo port specified by Port.
426 The port is written Count times, and the write data is
427 retrieved from the provided Buffer.
428
429 This function must guarantee that all I/O write and write operations are
430 serialized.
431
432 If 32-bit I/O port operations are not supported, then ASSERT().
433
434 @param Port The I/O port to write.
435 @param Count The number of times to write I/O port.
436 @param Buffer The buffer to retrieve the write data from.
437
438 **/
439 VOID
440 EFIAPI
441 IoWriteFifo32 (
442 IN UINTN Port,
443 IN UINTN Count,
444 IN VOID *Buffer
445 )
446 {
447 UINT32 *Buffer32;
448
449 Buffer32 = (UINT32 *)Buffer;
450 while (Count--) {
451 IoWrite32 (Port, *Buffer32++);
452 }
453 }
454
455 /**
456 Reads an 8-bit MMIO register.
457
458 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
459 returned. This function must guarantee that all MMIO read and write
460 operations are serialized.
461
462 If 8-bit MMIO register operations are not supported, then ASSERT().
463
464 @param Address The MMIO register to read.
465
466 @return The value read.
467
468 **/
469 UINT8
470 EFIAPI
471 MmioRead8 (
472 IN UINTN Address
473 )
474 {
475 UINT8 Data;
476
477 Address |= BIT63;
478
479 MemoryFence ();
480 Data = *((volatile UINT8 *) Address);
481 MemoryFence ();
482
483 return Data;
484 }
485
486 /**
487 Reads a 16-bit MMIO register.
488
489 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
490 returned. This function must guarantee that all MMIO read and write
491 operations are serialized.
492
493 If 16-bit MMIO register operations are not supported, then ASSERT().
494 If Address is not aligned on a 16-bit boundary, then ASSERT().
495
496 @param Address The MMIO register to read.
497
498 @return The value read.
499
500 **/
501 UINT16
502 EFIAPI
503 MmioRead16 (
504 IN UINTN Address
505 )
506 {
507 UINT16 Data;
508
509 //
510 // Make sure that Address is 16-bit aligned.
511 //
512 ASSERT ((Address & 1) == 0);
513
514 Address |= BIT63;
515
516 MemoryFence ();
517 Data = *((volatile UINT16 *) Address);
518 MemoryFence ();
519
520 return Data;
521 }
522
523 /**
524 Reads a 32-bit MMIO register.
525
526 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
527 returned. This function must guarantee that all MMIO read and write
528 operations are serialized.
529
530 If 32-bit MMIO register operations are not supported, then ASSERT().
531 If Address is not aligned on a 32-bit boundary, then ASSERT().
532
533 @param Address The MMIO register to read.
534
535 @return The value read.
536
537 **/
538 UINT32
539 EFIAPI
540 MmioRead32 (
541 IN UINTN Address
542 )
543 {
544 UINT32 Data;
545
546 //
547 // Make sure that Address is 32-bit aligned.
548 //
549 ASSERT ((Address & 3) == 0);
550
551 Address |= BIT63;
552
553 MemoryFence ();
554 Data = *((volatile UINT32 *) Address);
555 MemoryFence ();
556
557 return Data;
558 }
559
560 /**
561 Reads a 64-bit MMIO register.
562
563 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
564 returned. This function must guarantee that all MMIO read and write
565 operations are serialized.
566
567 If 64-bit MMIO register operations are not supported, then ASSERT().
568 If Address is not aligned on a 64-bit boundary, then ASSERT().
569
570 @param Address The MMIO register to read.
571
572 @return The value read.
573
574 **/
575 UINT64
576 EFIAPI
577 MmioRead64 (
578 IN UINTN Address
579 )
580 {
581 UINT64 Data;
582
583 //
584 // Make sure that Address is 64-bit aligned.
585 //
586 ASSERT ((Address & 7) == 0);
587
588 Address |= BIT63;
589
590 MemoryFence ();
591 Data = *((volatile UINT64 *) Address);
592 MemoryFence ();
593
594 return Data;
595
596 }
597
598 /**
599 Writes an 8-bit MMIO register.
600
601 Writes the 8-bit MMIO register specified by Address with the value specified
602 by Value and returns Value. This function must guarantee that all MMIO read
603 and write operations are serialized.
604
605 If 8-bit MMIO register operations are not supported, then ASSERT().
606
607 @param Address The MMIO register to write.
608 @param Value The value to write to the MMIO register.
609
610 @return Value.
611
612 **/
613 UINT8
614 EFIAPI
615 MmioWrite8 (
616 IN UINTN Address,
617 IN UINT8 Value
618 )
619 {
620 Address |= BIT63;
621
622 MemoryFence ();
623 *((volatile UINT8 *) Address) = Value;
624 MemoryFence ();
625
626 return Value;
627 }
628
629 /**
630 Writes a 16-bit MMIO register.
631
632 Writes the 16-bit MMIO register specified by Address with the value specified
633 by Value and returns Value. This function must guarantee that all MMIO read
634 and write operations are serialized.
635
636 If 16-bit MMIO register operations are not supported, then ASSERT().
637 If Address is not aligned on a 16-bit boundary, then ASSERT().
638
639 @param Address The MMIO register to write.
640 @param Value The value to write to the MMIO register.
641
642 @return Value.
643
644 **/
645 UINT16
646 EFIAPI
647 MmioWrite16 (
648 IN UINTN Address,
649 IN UINT16 Value
650 )
651 {
652 //
653 // Make sure that Address is 16-bit aligned.
654 //
655 ASSERT ((Address & 1) == 0);
656
657 Address |= BIT63;
658
659 MemoryFence ();
660 *((volatile UINT16 *) Address) = Value;
661 MemoryFence ();
662
663 return Value;
664 }
665
666 /**
667 Writes a 32-bit MMIO register.
668
669 Writes the 32-bit MMIO register specified by Address with the value specified
670 by Value and returns Value. This function must guarantee that all MMIO read
671 and write operations are serialized.
672
673 If 32-bit MMIO register operations are not supported, then ASSERT().
674 If Address is not aligned on a 32-bit boundary, then ASSERT().
675
676 @param Address The MMIO register to write.
677 @param Value The value to write to the MMIO register.
678
679 @return Value.
680
681 **/
682 UINT32
683 EFIAPI
684 MmioWrite32 (
685 IN UINTN Address,
686 IN UINT32 Value
687 )
688 {
689 //
690 // Make sure that Address is 32-bit aligned.
691 //
692 ASSERT ((Address & 3) == 0);
693
694 Address |= BIT63;
695
696 MemoryFence ();
697 *((volatile UINT32 *) Address) = Value;
698 MemoryFence ();
699
700 return Value;
701 }
702
703 /**
704 Writes a 64-bit MMIO register.
705
706 Writes the 64-bit MMIO register specified by Address with the value specified
707 by Value and returns Value. This function must guarantee that all MMIO read
708 and write operations are serialized.
709
710 If 64-bit MMIO register operations are not supported, then ASSERT().
711 If Address is not aligned on a 64-bit boundary, then ASSERT().
712
713 @param Address The MMIO register to write.
714 @param Value The value to write to the MMIO register.
715
716 **/
717 UINT64
718 EFIAPI
719 MmioWrite64 (
720 IN UINTN Address,
721 IN UINT64 Value
722 )
723 {
724 //
725 // Make sure that Address is 64-bit aligned.
726 //
727 ASSERT ((Address & 7) == 0);
728
729 Address |= BIT63;
730
731 MemoryFence ();
732 *((volatile UINT64 *) Address) = Value;
733 MemoryFence ();
734
735 return Value;
736 }