]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibInternalTdx.c
2 TDX I/O Library routines.
4 Copyright (c) 2020-2021, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include "BaseIoLibIntrinsicInternal.h"
9 #include <Uefi/UefiBaseType.h>
10 #include <Include/IndustryStandard/Tdx.h>
11 #include <Library/TdxLib.h>
12 #include <Register/Intel/Cpuid.h>
13 #include <Library/CcProbeLib.h>
16 // Size of TDVMCALL Access, including IO and MMIO
17 #define TDVMCALL_ACCESS_SIZE_1 1
18 #define TDVMCALL_ACCESS_SIZE_2 2
19 #define TDVMCALL_ACCESS_SIZE_4 4
20 #define TDVMCALL_ACCESS_SIZE_8 8
22 // Direction of TDVMCALL Access, including IO and MMIO
23 #define TDVMCALL_ACCESS_READ 0
24 #define TDVMCALL_ACCESS_WRITE 1
27 Check if it is Tdx guest.
29 @return TRUE It is Tdx guest
30 @return FALSE It is not Tdx guest
39 return CcProbe () == CcGuestTypeIntelTdx
;
43 Reads an 8-bit I/O port.
45 TDVMCALL_IO is invoked to read I/O port.
47 @param Port The I/O port to read.
49 @return The value read.
61 Status
= TdVmCall (TDVMCALL_IO
, TDVMCALL_ACCESS_SIZE_1
, TDVMCALL_ACCESS_READ
, Port
, 0, &Val
);
63 TdVmCall (TDVMCALL_HALT
, 0, 0, 0, 0, 0);
70 Reads a 16-bit I/O port.
72 TDVMCALL_IO is invoked to write I/O port.
74 @param Port The I/O port to read.
76 @return The value read.
88 ASSERT ((Port
& 1) == 0);
90 Status
= TdVmCall (TDVMCALL_IO
, TDVMCALL_ACCESS_SIZE_2
, TDVMCALL_ACCESS_READ
, Port
, 0, &Val
);
92 TdVmCall (TDVMCALL_HALT
, 0, 0, 0, 0, 0);
99 Reads a 32-bit I/O port.
101 TDVMCALL_IO is invoked to read I/O port.
103 @param Port The I/O port to read.
105 @return The value read.
117 ASSERT ((Port
& 3) == 0);
119 Status
= TdVmCall (TDVMCALL_IO
, TDVMCALL_ACCESS_SIZE_4
, TDVMCALL_ACCESS_READ
, Port
, 0, &Val
);
121 TdVmCall (TDVMCALL_HALT
, 0, 0, 0, 0, 0);
128 Writes an 8-bit I/O port.
130 TDVMCALL_IO is invoked to write I/O port.
132 @param Port The I/O port to write.
133 @param Value The value to write to the I/O port.
135 @return The value written the I/O port.
149 Status
= TdVmCall (TDVMCALL_IO
, TDVMCALL_ACCESS_SIZE_1
, TDVMCALL_ACCESS_WRITE
, Port
, Val
, 0);
151 TdVmCall (TDVMCALL_HALT
, 0, 0, 0, 0, 0);
158 Writes a 16-bit I/O port.
160 TDVMCALL_IO is invoked to write I/O port.
162 @param Port The I/O port to write.
163 @param Value The value to write to the I/O port.
165 @return The value written the I/O port.
178 ASSERT ((Port
& 1) == 0);
180 Status
= TdVmCall (TDVMCALL_IO
, TDVMCALL_ACCESS_SIZE_2
, TDVMCALL_ACCESS_WRITE
, Port
, Val
, 0);
182 TdVmCall (TDVMCALL_HALT
, 0, 0, 0, 0, 0);
189 Writes a 32-bit I/O port.
191 TDVMCALL_IO is invoked to write I/O port.
193 @param Port The I/O port to write.
194 @param Value The value to write to the I/O port.
196 @return The value written the I/O port.
209 ASSERT ((Port
& 3) == 0);
211 Status
= TdVmCall (TDVMCALL_IO
, TDVMCALL_ACCESS_SIZE_4
, TDVMCALL_ACCESS_WRITE
, Port
, Val
, 0);
213 TdVmCall (TDVMCALL_HALT
, 0, 0, 0, 0, 0);
220 Reads an 8-bit MMIO register.
222 TDVMCALL_MMIO is invoked to read MMIO registers.
224 @param Address The MMIO register to read.
226 @return The value read.
238 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_1
, TDVMCALL_ACCESS_READ
, Address
| TdSharedPageMask (), 0, &Value
);
240 Value
= *(volatile UINT64
*)Address
;
247 Writes an 8-bit MMIO register.
249 TDVMCALL_MMIO is invoked to read write registers.
251 @param Address The MMIO register to write.
252 @param Value The value to write to the MMIO register.
268 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_1
, TDVMCALL_ACCESS_WRITE
, Address
| TdSharedPageMask (), Val
, 0);
270 *(volatile UINT8
*)Address
= Value
;
277 Reads a 16-bit MMIO register.
279 TDVMCALL_MMIO is invoked to read MMIO registers.
281 @param Address The MMIO register to read.
283 @return The value read.
295 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_2
, TDVMCALL_ACCESS_READ
, Address
| TdSharedPageMask (), 0, &Value
);
297 Value
= *(volatile UINT64
*)Address
;
300 return (UINT16
)Value
;
304 Writes a 16-bit MMIO register.
306 TDVMCALL_MMIO is invoked to write MMIO registers.
308 @param Address The MMIO register to write.
309 @param Value The value to write to the MMIO register.
324 ASSERT ((Address
& 1) == 0);
327 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_2
, TDVMCALL_ACCESS_WRITE
, Address
| TdSharedPageMask (), Val
, 0);
329 *(volatile UINT16
*)Address
= Value
;
336 Reads a 32-bit MMIO register.
338 TDVMCALL_MMIO is invoked to read MMIO registers.
340 @param Address The MMIO register to read.
342 @return The value read.
354 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_4
, TDVMCALL_ACCESS_READ
, Address
| TdSharedPageMask (), 0, &Value
);
356 Value
= *(volatile UINT64
*)Address
;
359 return (UINT32
)Value
;
363 Writes a 32-bit MMIO register.
365 TDVMCALL_MMIO is invoked to write MMIO registers.
367 @param Address The MMIO register to write.
368 @param Value The value to write to the MMIO register.
383 ASSERT ((Address
& 3) == 0);
386 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_4
, TDVMCALL_ACCESS_WRITE
, Address
| TdSharedPageMask (), Val
, 0);
388 *(volatile UINT32
*)Address
= Value
;
395 Reads a 64-bit MMIO register.
397 TDVMCALL_MMIO is invoked to read MMIO registers.
399 @param Address The MMIO register to read.
401 @return The value read.
413 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_8
, TDVMCALL_ACCESS_READ
, Address
| TdSharedPageMask (), 0, &Value
);
415 Value
= *(volatile UINT64
*)Address
;
422 Writes a 64-bit MMIO register.
424 TDVMCALL_MMIO is invoked to write MMIO registers.
426 @param Address The MMIO register to write.
427 @param Value The value to write to the MMIO register.
440 ASSERT ((Address
& 7) == 0);
443 Status
= TdVmCall (TDVMCALL_MMIO
, TDVMCALL_ACCESS_SIZE_8
, TDVMCALL_ACCESS_WRITE
, Address
| TdSharedPageMask (), Val
, 0);
445 *(volatile UINT64
*)Address
= Value
;
452 Reads an 8-bit I/O port fifo into a block of memory.
454 Reads the 8-bit I/O fifo port specified by Port.
455 The port is read Count times, and the read data is
456 stored in the provided Buffer.
458 This function must guarantee that all I/O read and write operations are
461 If 8-bit I/O port operations are not supported, then ASSERT().
463 In TDX a serial of TdIoRead8 is invoked to read the I/O port fifo.
465 @param Port The I/O port to read.
466 @param Count The number of times to read I/O port.
467 @param Buffer The buffer to store the read data into.
481 Buf8
= (UINT8
*)Buffer
;
482 for (Index
= 0; Index
< Count
; Index
++) {
483 Buf8
[Index
] = TdIoRead8 (Port
);
488 Writes a block of memory into an 8-bit I/O port fifo.
490 Writes the 8-bit I/O fifo port specified by Port.
491 The port is written Count times, and the write data is
492 retrieved from the provided Buffer.
494 This function must guarantee that all I/O write and write operations are
497 If 8-bit I/O port operations are not supported, then ASSERT().
499 In TDX a serial of TdIoWrite8 is invoked to write data to the I/O port.
501 @param Port The I/O port to write.
502 @param Count The number of times to write I/O port.
503 @param Buffer The buffer to retrieve the write data from.
517 Buf8
= (UINT8
*)Buffer
;
518 for (Index
= 0; Index
< Count
; Index
++) {
519 TdIoWrite8 (Port
, Buf8
[Index
]);
524 Reads a 16-bit I/O port fifo into a block of memory.
526 Reads the 16-bit I/O fifo port specified by Port.
527 The port is read Count times, and the read data is
528 stored in the provided Buffer.
530 This function must guarantee that all I/O read and write operations are
533 If 16-bit I/O port operations are not supported, then ASSERT().
535 In TDX a serial of TdIoRead16 is invoked to read data from the I/O port.
537 @param Port The I/O port to read.
538 @param Count The number of times to read I/O port.
539 @param Buffer The buffer to store the read data into.
553 Buf16
= (UINT16
*)Buffer
;
554 for (Index
= 0; Index
< Count
; Index
++) {
555 Buf16
[Index
] = TdIoRead16 (Port
);
560 Writes a block of memory into a 16-bit I/O port fifo.
562 Writes the 16-bit I/O fifo port specified by Port.
563 The port is written Count times, and the write data is
564 retrieved from the provided Buffer.
566 This function must guarantee that all I/O write and write operations are
569 If 16-bit I/O port operations are not supported, then ASSERT().
571 In TDX a serial of TdIoWrite16 is invoked to write data to the I/O port.
573 @param Port The I/O port to write.
574 @param Count The number of times to write I/O port.
575 @param Buffer The buffer to retrieve the write data from.
589 Buf16
= (UINT16
*)Buffer
;
590 for (Index
= 0; Index
< Count
; Index
++) {
591 TdIoWrite16 (Port
, Buf16
[Index
]);
596 Reads a 32-bit I/O port fifo into a block of memory.
598 Reads the 32-bit I/O fifo port specified by Port.
599 The port is read Count times, and the read data is
600 stored in the provided Buffer.
602 This function must guarantee that all I/O read and write operations are
605 If 32-bit I/O port operations are not supported, then ASSERT().
607 In TDX a serial of TdIoRead32 is invoked to read data from the I/O port.
609 @param Port The I/O port to read.
610 @param Count The number of times to read I/O port.
611 @param Buffer The buffer to store the read data into.
625 Buf32
= (UINT32
*)Buffer
;
626 for (Index
= 0; Index
< Count
; Index
++) {
627 Buf32
[Index
] = TdIoRead32 (Port
);
632 Writes a block of memory into a 32-bit I/O port fifo.
634 Writes the 32-bit I/O fifo port specified by Port.
635 The port is written Count times, and the write data is
636 retrieved from the provided Buffer.
638 This function must guarantee that all I/O write and write operations are
641 If 32-bit I/O port operations are not supported, then ASSERT().
643 In TDX a serial of TdIoWrite32 is invoked to write data to the I/O port.
645 @param Port The I/O port to write.
646 @param Count The number of times to write I/O port.
647 @param Buffer The buffer to retrieve the write data from.
661 Buf32
= (UINT32
*)Buffer
;
662 for (Index
= 0; Index
< Count
; Index
++) {
663 TdIoWrite32 (Port
, Buf32
[Index
]);