]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibMsc.c
... / ...
CommitLineData
1/** @file\r
2 I/O Library. This file has compiler specifics for Microsft C as there is no\r
3 ANSI C standard for doing IO.\r
4\r
5 MSC - uses intrinsic functions and the optimize will remove the function call\r
6 overhead.\r
7\r
8 We don't advocate putting compiler specifics in libraries or drivers but there\r
9 is no other way to make this work.\r
10\r
11 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
12 SPDX-License-Identifier: BSD-2-Clause-Patent\r
13\r
14**/\r
15\r
16#include "BaseIoLibIntrinsicInternal.h"\r
17#include "IoLibTdx.h"\r
18\r
19//\r
20// Microsoft Visual Studio 7.1 Function Prototypes for I/O Intrinsics.\r
21//\r
22\r
23int\r
24_inp (\r
25 unsigned short port\r
26 );\r
27\r
28unsigned short\r
29_inpw (\r
30 unsigned short port\r
31 );\r
32\r
33unsigned long\r
34_inpd (\r
35 unsigned short port\r
36 );\r
37\r
38int\r
39_outp (\r
40 unsigned short port,\r
41 int databyte\r
42 );\r
43\r
44unsigned short\r
45_outpw (\r
46 unsigned short port,\r
47 unsigned short dataword\r
48 );\r
49\r
50unsigned long\r
51_outpd (\r
52 unsigned short port,\r
53 unsigned long dataword\r
54 );\r
55\r
56void\r
57_ReadWriteBarrier (\r
58 void\r
59 );\r
60\r
61#pragma intrinsic(_inp)\r
62#pragma intrinsic(_inpw)\r
63#pragma intrinsic(_inpd)\r
64#pragma intrinsic(_outp)\r
65#pragma intrinsic(_outpw)\r
66#pragma intrinsic(_outpd)\r
67#pragma intrinsic(_ReadWriteBarrier)\r
68\r
69//\r
70// _ReadWriteBarrier() forces memory reads and writes to complete at the point\r
71// in the call. This is only a hint to the compiler and does emit code.\r
72// In past versions of the compiler, _ReadWriteBarrier was enforced only\r
73// locally and did not affect functions up the call tree. In Visual C++\r
74// 2005, _ReadWriteBarrier is enforced all the way up the call tree.\r
75//\r
76\r
77/**\r
78 Reads an 8-bit I/O port.\r
79\r
80 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.\r
81 This function must guarantee that all I/O read and write operations are\r
82 serialized.\r
83\r
84 If 8-bit I/O port operations are not supported, then ASSERT().\r
85\r
86 For Td guest TDVMCALL_IO is invoked to read I/O port.\r
87\r
88 @param Port The I/O port to read.\r
89\r
90 @return The value read.\r
91\r
92**/\r
93UINT8\r
94EFIAPI\r
95IoRead8 (\r
96 IN UINTN Port\r
97 )\r
98{\r
99 UINT8 Value;\r
100 BOOLEAN Flag;\r
101\r
102 Flag = FilterBeforeIoRead (FilterWidth8, Port, &Value);\r
103 if (Flag) {\r
104 if (IsTdxGuest ()) {\r
105 Value = TdIoRead8 (Port);\r
106 } else {\r
107 _ReadWriteBarrier ();\r
108 Value = (UINT8)_inp ((UINT16)Port);\r
109 _ReadWriteBarrier ();\r
110 }\r
111 }\r
112\r
113 FilterAfterIoRead (FilterWidth8, Port, &Value);\r
114\r
115 return Value;\r
116}\r
117\r
118/**\r
119 Writes an 8-bit I/O port.\r
120\r
121 Writes the 8-bit I/O port specified by Port with the value specified by Value\r
122 and returns Value. This function must guarantee that all I/O read and write\r
123 operations are serialized.\r
124\r
125 If 8-bit I/O port operations are not supported, then ASSERT().\r
126\r
127 For Td guest TDVMCALL_IO is invoked to write I/O port.\r
128\r
129 @param Port The I/O port to write.\r
130 @param Value The value to write to the I/O port.\r
131\r
132 @return The value written to the I/O port.\r
133\r
134**/\r
135UINT8\r
136EFIAPI\r
137IoWrite8 (\r
138 IN UINTN Port,\r
139 IN UINT8 Value\r
140 )\r
141{\r
142 BOOLEAN Flag;\r
143\r
144 Flag = FilterBeforeIoWrite (FilterWidth8, Port, &Value);\r
145 if (Flag) {\r
146 if (IsTdxGuest ()) {\r
147 TdIoWrite8 (Port, Value);\r
148 } else {\r
149 _ReadWriteBarrier ();\r
150 (UINT8)_outp ((UINT16)Port, Value);\r
151 _ReadWriteBarrier ();\r
152 }\r
153 }\r
154\r
155 FilterAfterIoWrite (FilterWidth8, Port, &Value);\r
156\r
157 return Value;\r
158}\r
159\r
160/**\r
161 Reads a 16-bit I/O port.\r
162\r
163 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.\r
164 This function must guarantee that all I/O read and write operations are\r
165 serialized.\r
166\r
167 If 16-bit I/O port operations are not supported, then ASSERT().\r
168 If Port is not aligned on a 16-bit boundary, then ASSERT().\r
169\r
170 For Td guest TDVMCALL_IO is invoked to read I/O port.\r
171\r
172 @param Port The I/O port to read.\r
173\r
174 @return The value read.\r
175\r
176**/\r
177UINT16\r
178EFIAPI\r
179IoRead16 (\r
180 IN UINTN Port\r
181 )\r
182{\r
183 UINT16 Value;\r
184 BOOLEAN Flag;\r
185\r
186 ASSERT ((Port & 1) == 0);\r
187\r
188 Flag = FilterBeforeIoRead (FilterWidth16, Port, &Value);\r
189 if (Flag) {\r
190 if (IsTdxGuest ()) {\r
191 Value = TdIoRead16 (Port);\r
192 } else {\r
193 _ReadWriteBarrier ();\r
194 Value = _inpw ((UINT16)Port);\r
195 _ReadWriteBarrier ();\r
196 }\r
197 }\r
198\r
199 FilterBeforeIoRead (FilterWidth16, Port, &Value);\r
200\r
201 return Value;\r
202}\r
203\r
204/**\r
205 Writes a 16-bit I/O port.\r
206\r
207 Writes the 16-bit I/O port specified by Port with the value specified by Value\r
208 and returns Value. This function must guarantee that all I/O read and write\r
209 operations are serialized.\r
210\r
211 If 16-bit I/O port operations are not supported, then ASSERT().\r
212 If Port is not aligned on a 16-bit boundary, then ASSERT().\r
213\r
214 For Td guest TDVMCALL_IO is invoked to write I/O port.\r
215\r
216 @param Port The I/O port to write.\r
217 @param Value The value to write to the I/O port.\r
218\r
219 @return The value written to the I/O port.\r
220\r
221**/\r
222UINT16\r
223EFIAPI\r
224IoWrite16 (\r
225 IN UINTN Port,\r
226 IN UINT16 Value\r
227 )\r
228{\r
229 BOOLEAN Flag;\r
230\r
231 ASSERT ((Port & 1) == 0);\r
232\r
233 Flag = FilterBeforeIoWrite (FilterWidth16, Port, &Value);\r
234 if (Flag) {\r
235 if (IsTdxGuest ()) {\r
236 TdIoWrite16 (Port, Value);\r
237 } else {\r
238 _ReadWriteBarrier ();\r
239 _outpw ((UINT16)Port, Value);\r
240 _ReadWriteBarrier ();\r
241 }\r
242 }\r
243\r
244 FilterAfterIoWrite (FilterWidth16, Port, &Value);\r
245\r
246 return Value;\r
247}\r
248\r
249/**\r
250 Reads a 32-bit I/O port.\r
251\r
252 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.\r
253 This function must guarantee that all I/O read and write operations are\r
254 serialized.\r
255\r
256 If 32-bit I/O port operations are not supported, then ASSERT().\r
257 If Port is not aligned on a 32-bit boundary, then ASSERT().\r
258\r
259 For Td guest TDVMCALL_IO is invoked to read I/O port.\r
260\r
261 @param Port The I/O port to read.\r
262\r
263 @return The value read.\r
264\r
265**/\r
266UINT32\r
267EFIAPI\r
268IoRead32 (\r
269 IN UINTN Port\r
270 )\r
271{\r
272 UINT32 Value;\r
273 BOOLEAN Flag;\r
274\r
275 ASSERT ((Port & 3) == 0);\r
276\r
277 Flag = FilterBeforeIoRead (FilterWidth32, Port, &Value);\r
278 if (Flag) {\r
279 if (IsTdxGuest ()) {\r
280 Value = TdIoRead32 (Port);\r
281 } else {\r
282 _ReadWriteBarrier ();\r
283 Value = _inpd ((UINT16)Port);\r
284 _ReadWriteBarrier ();\r
285 }\r
286 }\r
287\r
288 FilterAfterIoRead (FilterWidth32, Port, &Value);\r
289\r
290 return Value;\r
291}\r
292\r
293/**\r
294 Writes a 32-bit I/O port.\r
295\r
296 Writes the 32-bit I/O port specified by Port with the value specified by Value\r
297 and returns Value. This function must guarantee that all I/O read and write\r
298 operations are serialized.\r
299\r
300 If 32-bit I/O port operations are not supported, then ASSERT().\r
301 If Port is not aligned on a 32-bit boundary, then ASSERT().\r
302\r
303 For Td guest TDVMCALL_IO is invoked to write I/O port.\r
304\r
305 @param Port The I/O port to write.\r
306 @param Value The value to write to the I/O port.\r
307\r
308 @return The value written to the I/O port.\r
309\r
310**/\r
311UINT32\r
312EFIAPI\r
313IoWrite32 (\r
314 IN UINTN Port,\r
315 IN UINT32 Value\r
316 )\r
317{\r
318 BOOLEAN Flag;\r
319\r
320 ASSERT ((Port & 3) == 0);\r
321\r
322 Flag = FilterBeforeIoWrite (FilterWidth32, Port, &Value);\r
323 if (Flag) {\r
324 if (IsTdxGuest ()) {\r
325 TdIoWrite32 (Port, Value);\r
326 } else {\r
327 _ReadWriteBarrier ();\r
328 _outpd ((UINT16)Port, Value);\r
329 _ReadWriteBarrier ();\r
330 }\r
331 }\r
332\r
333 FilterAfterIoWrite (FilterWidth32, Port, &Value);\r
334\r
335 return Value;\r
336}\r