]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLib.c
1 /** @file
2 Common I/O Library routines.
3
4 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "BaseIoLibIntrinsicInternal.h"
10 #include "IoLibTdx.h"
11
12 /**
13 Reads a 64-bit I/O port.
14
15 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
16 This function must guarantee that all I/O read and write operations are
17 serialized.
18
19 If 64-bit I/O port operations are not supported, then ASSERT().
20 If Port is not aligned on a 64-bit boundary, then ASSERT().
21
22 @param Port The I/O port to read.
23
24 @return The value read.
25
26 **/
27 UINT64
28 EFIAPI
29 IoRead64 (
30 IN UINTN Port
31 )
32 {
33 ASSERT (FALSE);
34 return 0;
35 }
36
37 /**
38 Writes a 64-bit I/O port.
39
40 Writes the 64-bit I/O port specified by Port with the value specified by Value
41 and returns Value. This function must guarantee that all I/O read and write
42 operations are serialized.
43
44 If 64-bit I/O port operations are not supported, then ASSERT().
45 If Port is not aligned on a 64-bit boundary, then ASSERT().
46
47 @param Port The I/O port to write.
48 @param Value The value to write to the I/O port.
49
50 @return The value written the I/O port.
51
52 **/
53 UINT64
54 EFIAPI
55 IoWrite64 (
56 IN UINTN Port,
57 IN UINT64 Value
58 )
59 {
60 ASSERT (FALSE);
61 return 0;
62 }
63
64 /**
65 Reads an 8-bit MMIO register.
66
67 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
68 returned. This function must guarantee that all MMIO read and write
69 operations are serialized.
70
71 If 8-bit MMIO register operations are not supported, then ASSERT().
72
73 For Td guest TDVMCALL_MMIO is invoked to read MMIO registers.
74
75 @param Address The MMIO register to read.
76
77 @return The value read.
78
79 **/
80 UINT8
81 EFIAPI
82 MmioRead8 (
83 IN UINTN Address
84 )
85 {
86 UINT8 Value;
87 BOOLEAN Flag;
88
89 Flag = FilterBeforeMmIoRead (FilterWidth8, Address, &Value);
90 if (Flag) {
91 MemoryFence ();
92
93 if (IsTdxGuest ()) {
94 Value = TdMmioRead8 (Address);
95 } else {
96 Value = *(volatile UINT8 *)Address;
97 }
98
99 MemoryFence ();
100 }
101
102 FilterAfterMmIoRead (FilterWidth8, Address, &Value);
103
104 return Value;
105 }
106
107 /**
108 Writes an 8-bit MMIO register.
109
110 Writes the 8-bit MMIO register specified by Address with the value specified
111 by Value and returns Value. This function must guarantee that all MMIO read
112 and write operations are serialized.
113
114 If 8-bit MMIO register operations are not supported, then ASSERT().
115
116 For Td guest TDVMCALL_MMIO is invoked to write MMIO registers.
117
118 @param Address The MMIO register to write.
119 @param Value The value to write to the MMIO register.
120
121 @return Value.
122
123 **/
124 UINT8
125 EFIAPI
126 MmioWrite8 (
127 IN UINTN Address,
128 IN UINT8 Value
129 )
130 {
131 BOOLEAN Flag;
132
133 Flag = FilterBeforeMmIoWrite (FilterWidth8, Address, &Value);
134 if (Flag) {
135 MemoryFence ();
136
137 if (IsTdxGuest ()) {
138 TdMmioWrite8 (Address, Value);
139 } else {
140 *(volatile UINT8 *)Address = Value;
141 }
142
143 MemoryFence ();
144 }
145
146 FilterAfterMmIoWrite (FilterWidth8, Address, &Value);
147
148 return Value;
149 }
150
151 /**
152 Reads a 16-bit MMIO register.
153
154 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
155 returned. This function must guarantee that all MMIO read and write
156 operations are serialized.
157
158 If 16-bit MMIO register operations are not supported, then ASSERT().
159 If Address is not aligned on a 16-bit boundary, then ASSERT().
160
161 For Td guest TDVMCALL_MMIO is invoked to read MMIO registers.
162
163 @param Address The MMIO register to read.
164
165 @return The value read.
166
167 **/
168 UINT16
169 EFIAPI
170 MmioRead16 (
171 IN UINTN Address
172 )
173 {
174 UINT16 Value;
175 BOOLEAN Flag;
176
177 ASSERT ((Address & 1) == 0);
178 Flag = FilterBeforeMmIoRead (FilterWidth16, Address, &Value);
179 if (Flag) {
180 MemoryFence ();
181
182 if (IsTdxGuest ()) {
183 Value = TdMmioRead16 (Address);
184 } else {
185 Value = *(volatile UINT16 *)Address;
186 }
187
188 MemoryFence ();
189 }
190
191 FilterAfterMmIoRead (FilterWidth16, Address, &Value);
192
193 return Value;
194 }
195
196 /**
197 Writes a 16-bit MMIO register.
198
199 Writes the 16-bit MMIO register specified by Address with the value specified
200 by Value and returns Value. This function must guarantee that all MMIO read
201 and write operations are serialized.
202
203 If 16-bit MMIO register operations are not supported, then ASSERT().
204 If Address is not aligned on a 16-bit boundary, then ASSERT().
205
206 For Td guest TDVMCALL_MMIO is invoked to write MMIO registers.
207
208 @param Address The MMIO register to write.
209 @param Value The value to write to the MMIO register.
210
211 @return Value.
212
213 **/
214 UINT16
215 EFIAPI
216 MmioWrite16 (
217 IN UINTN Address,
218 IN UINT16 Value
219 )
220 {
221 BOOLEAN Flag;
222
223 ASSERT ((Address & 1) == 0);
224
225 Flag = FilterBeforeMmIoWrite (FilterWidth16, Address, &Value);
226 if (Flag) {
227 MemoryFence ();
228
229 if (IsTdxGuest ()) {
230 TdMmioWrite16 (Address, Value);
231 } else {
232 *(volatile UINT16 *)Address = Value;
233 }
234
235 MemoryFence ();
236 }
237
238 FilterAfterMmIoWrite (FilterWidth16, Address, &Value);
239
240 return Value;
241 }
242
243 /**
244 Reads a 32-bit MMIO register.
245
246 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
247 returned. This function must guarantee that all MMIO read and write
248 operations are serialized.
249
250 If 32-bit MMIO register operations are not supported, then ASSERT().
251 If Address is not aligned on a 32-bit boundary, then ASSERT().
252
253 For Td guest TDVMCALL_MMIO is invoked to read MMIO registers.
254
255 @param Address The MMIO register to read.
256
257 @return The value read.
258
259 **/
260 UINT32
261 EFIAPI
262 MmioRead32 (
263 IN UINTN Address
264 )
265 {
266 UINT32 Value;
267 BOOLEAN Flag;
268
269 ASSERT ((Address & 3) == 0);
270
271 Flag = FilterBeforeMmIoRead (FilterWidth32, Address, &Value);
272 if (Flag) {
273 MemoryFence ();
274
275 if (IsTdxGuest ()) {
276 Value = TdMmioRead32 (Address);
277 } else {
278 Value = *(volatile UINT32 *)Address;
279 }
280
281 MemoryFence ();
282 }
283
284 FilterAfterMmIoRead (FilterWidth32, Address, &Value);
285
286 return Value;
287 }
288
289 /**
290 Writes a 32-bit MMIO register.
291
292 Writes the 32-bit MMIO register specified by Address with the value specified
293 by Value and returns Value. This function must guarantee that all MMIO read
294 and write operations are serialized.
295
296 If 32-bit MMIO register operations are not supported, then ASSERT().
297 If Address is not aligned on a 32-bit boundary, then ASSERT().
298
299 For Td guest TDVMCALL_MMIO is invoked to write MMIO registers.
300
301 @param Address The MMIO register to write.
302 @param Value The value to write to the MMIO register.
303
304 @return Value.
305
306 **/
307 UINT32
308 EFIAPI
309 MmioWrite32 (
310 IN UINTN Address,
311 IN UINT32 Value
312 )
313 {
314 BOOLEAN Flag;
315
316 ASSERT ((Address & 3) == 0);
317
318 Flag = FilterBeforeMmIoWrite (FilterWidth32, Address, &Value);
319 if (Flag) {
320 MemoryFence ();
321
322 if (IsTdxGuest ()) {
323 TdMmioWrite32 (Address, Value);
324 } else {
325 *(volatile UINT32 *)Address = Value;
326 }
327
328 MemoryFence ();
329 }
330
331 FilterAfterMmIoWrite (FilterWidth32, Address, &Value);
332
333 return Value;
334 }
335
336 /**
337 Reads a 64-bit MMIO register.
338
339 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
340 returned. This function must guarantee that all MMIO read and write
341 operations are serialized.
342
343 If 64-bit MMIO register operations are not supported, then ASSERT().
344 If Address is not aligned on a 64-bit boundary, then ASSERT().
345
346 For Td guest TDVMCALL_MMIO is invoked to read MMIO registers.
347
348 @param Address The MMIO register to read.
349
350 @return The value read.
351
352 **/
353 UINT64
354 EFIAPI
355 MmioRead64 (
356 IN UINTN Address
357 )
358 {
359 UINT64 Value;
360 BOOLEAN Flag;
361
362 ASSERT ((Address & 7) == 0);
363
364 Flag = FilterBeforeMmIoRead (FilterWidth64, Address, &Value);
365 if (Flag) {
366 MemoryFence ();
367
368 if (IsTdxGuest ()) {
369 Value = TdMmioRead64 (Address);
370 } else {
371 Value = *(volatile UINT64 *)Address;
372 }
373
374 MemoryFence ();
375 }
376
377 FilterAfterMmIoRead (FilterWidth64, Address, &Value);
378
379 return Value;
380 }
381
382 /**
383 Writes a 64-bit MMIO register.
384
385 Writes the 64-bit MMIO register specified by Address with the value specified
386 by Value and returns Value. This function must guarantee that all MMIO read
387 and write operations are serialized.
388
389 If 64-bit MMIO register operations are not supported, then ASSERT().
390 If Address is not aligned on a 64-bit boundary, then ASSERT().
391
392 For Td guest TDVMCALL_MMIO is invoked to write MMIO registers.
393
394 @param Address The MMIO register to write.
395 @param Value The value to write to the MMIO register.
396
397 **/
398 UINT64
399 EFIAPI
400 MmioWrite64 (
401 IN UINTN Address,
402 IN UINT64 Value
403 )
404 {
405 BOOLEAN Flag;
406
407 ASSERT ((Address & 7) == 0);
408
409 Flag = FilterBeforeMmIoWrite (FilterWidth64, Address, &Value);
410 if (Flag) {
411 MemoryFence ();
412
413 if (IsTdxGuest ()) {
414 TdMmioWrite64 (Address, Value);
415 } else {
416 *(volatile UINT64 *)Address = Value;
417 }
418
419 MemoryFence ();
420 }
421
422 FilterAfterMmIoWrite (FilterWidth64, Address, &Value);
423
424 return Value;
425 }