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