TianoCore EDK2 master
Loading...
Searching...
No Matches
IoLibInternalTdx.c
Go to the documentation of this file.
1
9#include <Uefi/UefiBaseType.h>
11#include <Library/TdxLib.h>
13#include <Library/CcProbeLib.h>
14#include "IoLibTdx.h"
15
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
21
22// Direction of TDVMCALL Access, including IO and MMIO
23#define TDVMCALL_ACCESS_READ 0
24#define TDVMCALL_ACCESS_WRITE 1
25
33BOOLEAN
34EFIAPI
36 VOID
37 )
38{
39 return CcProbe () == CcGuestTypeIntelTdx;
40}
41
52UINT8
53EFIAPI
55 IN UINTN Port
56 )
57{
58 UINT64 Status;
59 UINT64 Val;
60
61 Status = TdVmCall (TDVMCALL_IO, TDVMCALL_ACCESS_SIZE_1, TDVMCALL_ACCESS_READ, Port, 0, &Val);
62 if (Status != 0) {
63 TdVmCall (TDVMCALL_HALT, 0, 0, 0, 0, 0);
64 }
65
66 return (UINT8)Val;
67}
68
79UINT16
80EFIAPI
82 IN UINTN Port
83 )
84{
85 UINT64 Status;
86 UINT64 Val;
87
88 ASSERT ((Port & 1) == 0);
89
90 Status = TdVmCall (TDVMCALL_IO, TDVMCALL_ACCESS_SIZE_2, TDVMCALL_ACCESS_READ, Port, 0, &Val);
91 if (Status != 0) {
92 TdVmCall (TDVMCALL_HALT, 0, 0, 0, 0, 0);
93 }
94
95 return (UINT16)Val;
96}
97
108UINT32
109EFIAPI
111 IN UINTN Port
112 )
113{
114 UINT64 Status;
115 UINT64 Val;
116
117 ASSERT ((Port & 3) == 0);
118
119 Status = TdVmCall (TDVMCALL_IO, TDVMCALL_ACCESS_SIZE_4, TDVMCALL_ACCESS_READ, Port, 0, &Val);
120 if (Status != 0) {
121 TdVmCall (TDVMCALL_HALT, 0, 0, 0, 0, 0);
122 }
123
124 return (UINT32)Val;
125}
126
138UINT8
139EFIAPI
141 IN UINTN Port,
142 IN UINT8 Value
143 )
144{
145 UINT64 Status;
146 UINT64 Val;
147
148 Val = Value;
149 Status = TdVmCall (TDVMCALL_IO, TDVMCALL_ACCESS_SIZE_1, TDVMCALL_ACCESS_WRITE, Port, Val, 0);
150 if (Status != 0) {
151 TdVmCall (TDVMCALL_HALT, 0, 0, 0, 0, 0);
152 }
153
154 return Value;
155}
156
168UINT16
169EFIAPI
171 IN UINTN Port,
172 IN UINT16 Value
173 )
174{
175 UINT64 Status;
176 UINT64 Val;
177
178 ASSERT ((Port & 1) == 0);
179 Val = Value;
180 Status = TdVmCall (TDVMCALL_IO, TDVMCALL_ACCESS_SIZE_2, TDVMCALL_ACCESS_WRITE, Port, Val, 0);
181 if (Status != 0) {
182 TdVmCall (TDVMCALL_HALT, 0, 0, 0, 0, 0);
183 }
184
185 return Value;
186}
187
199UINT32
200EFIAPI
202 IN UINTN Port,
203 IN UINT32 Value
204 )
205{
206 UINT64 Status;
207 UINT64 Val;
208
209 ASSERT ((Port & 3) == 0);
210 Val = Value;
211 Status = TdVmCall (TDVMCALL_IO, TDVMCALL_ACCESS_SIZE_4, TDVMCALL_ACCESS_WRITE, Port, Val, 0);
212 if (Status != 0) {
213 TdVmCall (TDVMCALL_HALT, 0, 0, 0, 0, 0);
214 }
215
216 return Value;
217}
218
229UINT8
230EFIAPI
232 IN UINTN Address
233 )
234{
235 UINT64 Value;
236 UINT64 Status;
237
238 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_1, TDVMCALL_ACCESS_READ, Address | TdSharedPageMask (), 0, &Value);
239 if (Status != 0) {
240 Value = *(volatile UINT8 *)Address;
241 }
242
243 return (UINT8)Value;
244}
245
257UINT8
258EFIAPI
260 IN UINTN Address,
261 IN UINT8 Value
262 )
263{
264 UINT64 Val;
265 UINT64 Status;
266
267 Val = Value;
268 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_1, TDVMCALL_ACCESS_WRITE, Address | TdSharedPageMask (), Val, 0);
269 if (Status != 0) {
270 *(volatile UINT8 *)Address = Value;
271 }
272
273 return Value;
274}
275
286UINT16
287EFIAPI
289 IN UINTN Address
290 )
291{
292 UINT64 Value;
293 UINT64 Status;
294
295 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_2, TDVMCALL_ACCESS_READ, Address | TdSharedPageMask (), 0, &Value);
296 if (Status != 0) {
297 Value = *(volatile UINT16 *)Address;
298 }
299
300 return (UINT16)Value;
301}
302
314UINT16
315EFIAPI
317 IN UINTN Address,
318 IN UINT16 Value
319 )
320{
321 UINT64 Val;
322 UINT64 Status;
323
324 ASSERT ((Address & 1) == 0);
325
326 Val = Value;
327 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_2, TDVMCALL_ACCESS_WRITE, Address | TdSharedPageMask (), Val, 0);
328 if (Status != 0) {
329 *(volatile UINT16 *)Address = Value;
330 }
331
332 return Value;
333}
334
345UINT32
346EFIAPI
348 IN UINTN Address
349 )
350{
351 UINT64 Value;
352 UINT64 Status;
353
354 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_4, TDVMCALL_ACCESS_READ, Address | TdSharedPageMask (), 0, &Value);
355 if (Status != 0) {
356 Value = *(volatile UINT32 *)Address;
357 }
358
359 return (UINT32)Value;
360}
361
373UINT32
374EFIAPI
376 IN UINTN Address,
377 IN UINT32 Value
378 )
379{
380 UINT64 Val;
381 UINT64 Status;
382
383 ASSERT ((Address & 3) == 0);
384
385 Val = Value;
386 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_4, TDVMCALL_ACCESS_WRITE, Address | TdSharedPageMask (), Val, 0);
387 if (Status != 0) {
388 *(volatile UINT32 *)Address = Value;
389 }
390
391 return Value;
392}
393
404UINT64
405EFIAPI
407 IN UINTN Address
408 )
409{
410 UINT64 Value;
411 UINT64 Status;
412
413 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_8, TDVMCALL_ACCESS_READ, Address | TdSharedPageMask (), 0, &Value);
414 if (Status != 0) {
415 Value = *(volatile UINT64 *)Address;
416 }
417
418 return Value;
419}
420
430UINT64
431EFIAPI
433 IN UINTN Address,
434 IN UINT64 Value
435 )
436{
437 UINT64 Status;
438 UINT64 Val;
439
440 ASSERT ((Address & 7) == 0);
441
442 Val = Value;
443 Status = TdVmCall (TDVMCALL_MMIO, TDVMCALL_ACCESS_SIZE_8, TDVMCALL_ACCESS_WRITE, Address | TdSharedPageMask (), Val, 0);
444 if (Status != 0) {
445 *(volatile UINT64 *)Address = Value;
446 }
447
448 return Value;
449}
450
470VOID
471EFIAPI
473 IN UINTN Port,
474 IN UINTN Count,
475 OUT VOID *Buffer
476 )
477{
478 UINT8 *Buf8;
479 UINTN Index;
480
481 Buf8 = (UINT8 *)Buffer;
482 for (Index = 0; Index < Count; Index++) {
483 Buf8[Index] = TdIoRead8 (Port);
484 }
485}
486
506VOID
507EFIAPI
509 IN UINTN Port,
510 IN UINTN Count,
511 IN VOID *Buffer
512 )
513{
514 UINT8 *Buf8;
515 UINTN Index;
516
517 Buf8 = (UINT8 *)Buffer;
518 for (Index = 0; Index < Count; Index++) {
519 TdIoWrite8 (Port, Buf8[Index]);
520 }
521}
522
542VOID
543EFIAPI
545 IN UINTN Port,
546 IN UINTN Count,
547 OUT VOID *Buffer
548 )
549{
550 UINT16 *Buf16;
551 UINTN Index;
552
553 Buf16 = (UINT16 *)Buffer;
554 for (Index = 0; Index < Count; Index++) {
555 Buf16[Index] = TdIoRead16 (Port);
556 }
557}
558
578VOID
579EFIAPI
581 IN UINTN Port,
582 IN UINTN Count,
583 IN VOID *Buffer
584 )
585{
586 UINT16 *Buf16;
587 UINTN Index;
588
589 Buf16 = (UINT16 *)Buffer;
590 for (Index = 0; Index < Count; Index++) {
591 TdIoWrite16 (Port, Buf16[Index]);
592 }
593}
594
614VOID
615EFIAPI
617 IN UINTN Port,
618 IN UINTN Count,
619 OUT VOID *Buffer
620 )
621{
622 UINT32 *Buf32;
623 UINTN Index;
624
625 Buf32 = (UINT32 *)Buffer;
626 for (Index = 0; Index < Count; Index++) {
627 Buf32[Index] = TdIoRead32 (Port);
628 }
629}
630
650VOID
651EFIAPI
653 IN UINTN Port,
654 IN UINTN Count,
655 IN VOID *Buffer
656 )
657{
658 UINT32 *Buf32;
659 UINTN Index;
660
661 Buf32 = (UINT32 *)Buffer;
662 for (Index = 0; Index < Count; Index++) {
663 TdIoWrite32 (Port, Buf32[Index]);
664 }
665}
UINT64 UINTN
UINTN EFIAPI TdVmCall(IN UINT64 Leaf, IN UINT64 Arg1, IN UINT64 Arg2, IN UINT64 Arg3, IN UINT64 Arg4, IN OUT VOID *Results)
Definition: IntelTdxNull.c:59
UINT8 EFIAPI CcProbe(VOID)
UINT32 EFIAPI TdMmioRead32(IN UINTN Address)
UINT64 EFIAPI TdMmioRead64(IN UINTN Address)
UINT8 EFIAPI TdMmioRead8(IN UINTN Address)
VOID EFIAPI TdIoWriteFifo32(IN UINTN Port, IN UINTN Count, IN VOID *Buffer)
UINT16 EFIAPI TdIoRead16(IN UINTN Port)
VOID EFIAPI TdIoWriteFifo16(IN UINTN Port, IN UINTN Count, IN VOID *Buffer)
VOID EFIAPI TdIoReadFifo16(IN UINTN Port, IN UINTN Count, OUT VOID *Buffer)
UINT32 EFIAPI TdIoWrite32(IN UINTN Port, IN UINT32 Value)
UINT16 EFIAPI TdMmioRead16(IN UINTN Address)
UINT8 EFIAPI TdMmioWrite8(IN UINTN Address, IN UINT8 Value)
UINT64 EFIAPI TdMmioWrite64(IN UINTN Address, IN UINT64 Value)
UINT16 EFIAPI TdIoWrite16(IN UINTN Port, IN UINT16 Value)
VOID EFIAPI TdIoReadFifo8(IN UINTN Port, IN UINTN Count, OUT VOID *Buffer)
UINT32 EFIAPI TdIoRead32(IN UINTN Port)
VOID EFIAPI TdIoWriteFifo8(IN UINTN Port, IN UINTN Count, IN VOID *Buffer)
UINT32 EFIAPI TdMmioWrite32(IN UINTN Address, IN UINT32 Value)
VOID EFIAPI TdIoReadFifo32(IN UINTN Port, IN UINTN Count, OUT VOID *Buffer)
UINT16 EFIAPI TdMmioWrite16(IN UINTN Address, IN UINT16 Value)
BOOLEAN EFIAPI IsTdxGuest(VOID)
UINT8 EFIAPI TdIoWrite8(IN UINTN Port, IN UINT8 Value)
UINT8 EFIAPI TdIoRead8(IN UINTN Port)
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
UINT64 EFIAPI TdSharedPageMask(VOID)
Definition: TdInfo.c:68