]>
Commit | Line | Data |
---|---|---|
e1f414b6 | 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 | |
38c8be12 | 11 | Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r |
9344f092 | 12 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
e1f414b6 | 13 | \r |
e1f414b6 | 14 | **/\r |
15 | \r | |
f734a10a | 16 | #include "BaseIoLibIntrinsicInternal.h"\r |
3571fc90 | 17 | #include "IoLibTdx.h"\r |
e1f414b6 | 18 | \r |
19 | //\r | |
42eedea9 | 20 | // Microsoft Visual Studio 7.1 Function Prototypes for I/O Intrinsics.\r |
e1f414b6 | 21 | //\r |
42eedea9 | 22 | \r |
2f88bd3a MK |
23 | int\r |
24 | _inp (\r | |
25 | unsigned short port\r | |
26 | );\r | |
27 | \r | |
28 | unsigned short\r | |
29 | _inpw (\r | |
30 | unsigned short port\r | |
31 | );\r | |
32 | \r | |
33 | unsigned long\r | |
34 | _inpd (\r | |
35 | unsigned short port\r | |
36 | );\r | |
37 | \r | |
38 | int\r | |
39 | _outp (\r | |
40 | unsigned short port,\r | |
41 | int databyte\r | |
42 | );\r | |
43 | \r | |
44 | unsigned short\r | |
45 | _outpw (\r | |
46 | unsigned short port,\r | |
47 | unsigned short dataword\r | |
48 | );\r | |
49 | \r | |
50 | unsigned long\r | |
51 | _outpd (\r | |
52 | unsigned short port,\r | |
53 | unsigned long dataword\r | |
54 | );\r | |
55 | \r | |
56 | void\r | |
57 | _ReadWriteBarrier (\r | |
58 | void\r | |
59 | );\r | |
e1f414b6 | 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 | |
986352be | 68 | \r |
e1f414b6 | 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 | |
3571fc90 MX |
86 | For Td guest TDVMCALL_IO is invoked to read I/O port.\r |
87 | \r | |
e1f414b6 | 88 | @param Port The I/O port to read.\r |
89 | \r | |
2281e7a9 | 90 | @return The value read.\r |
e1f414b6 | 91 | \r |
92 | **/\r | |
93 | UINT8\r | |
94 | EFIAPI\r | |
95 | IoRead8 (\r | |
2f88bd3a | 96 | IN UINTN Port\r |
e1f414b6 | 97 | )\r |
98 | {\r | |
2f88bd3a MK |
99 | UINT8 Value;\r |
100 | BOOLEAN Flag;\r | |
38c8be12 DB |
101 | \r |
102 | Flag = FilterBeforeIoRead (FilterWidth8, Port, &Value);\r | |
103 | if (Flag) {\r | |
3571fc90 MX |
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 | |
38c8be12 | 111 | }\r |
2f88bd3a | 112 | \r |
38c8be12 | 113 | FilterAfterIoRead (FilterWidth8, Port, &Value);\r |
e1f414b6 | 114 | \r |
e1f414b6 | 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 | |
3571fc90 MX |
127 | For Td guest TDVMCALL_IO is invoked to write I/O port.\r |
128 | \r | |
e1f414b6 | 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 | |
35a17154 | 132 | @return The value written to the I/O port.\r |
e1f414b6 | 133 | \r |
134 | **/\r | |
135 | UINT8\r | |
136 | EFIAPI\r | |
137 | IoWrite8 (\r | |
2f88bd3a MK |
138 | IN UINTN Port,\r |
139 | IN UINT8 Value\r | |
e1f414b6 | 140 | )\r |
141 | {\r | |
2f88bd3a | 142 | BOOLEAN Flag;\r |
38c8be12 | 143 | \r |
2f88bd3a | 144 | Flag = FilterBeforeIoWrite (FilterWidth8, Port, &Value);\r |
38c8be12 | 145 | if (Flag) {\r |
3571fc90 MX |
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 | |
38c8be12 | 153 | }\r |
2f88bd3a | 154 | \r |
38c8be12 DB |
155 | FilterAfterIoWrite (FilterWidth8, Port, &Value);\r |
156 | \r | |
e1f414b6 | 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 | |
2281e7a9 | 168 | If Port is not aligned on a 16-bit boundary, then ASSERT().\r |
e1f414b6 | 169 | \r |
3571fc90 MX |
170 | For Td guest TDVMCALL_IO is invoked to read I/O port.\r |
171 | \r | |
e1f414b6 | 172 | @param Port The I/O port to read.\r |
173 | \r | |
2281e7a9 | 174 | @return The value read.\r |
e1f414b6 | 175 | \r |
176 | **/\r | |
177 | UINT16\r | |
178 | EFIAPI\r | |
179 | IoRead16 (\r | |
2f88bd3a | 180 | IN UINTN Port\r |
e1f414b6 | 181 | )\r |
182 | {\r | |
2f88bd3a MK |
183 | UINT16 Value;\r |
184 | BOOLEAN Flag;\r | |
e1f414b6 | 185 | \r |
186 | ASSERT ((Port & 1) == 0);\r | |
38c8be12 DB |
187 | \r |
188 | Flag = FilterBeforeIoRead (FilterWidth16, Port, &Value);\r | |
189 | if (Flag) {\r | |
3571fc90 MX |
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 | |
38c8be12 | 197 | }\r |
2f88bd3a | 198 | \r |
38c8be12 DB |
199 | FilterBeforeIoRead (FilterWidth16, Port, &Value);\r |
200 | \r | |
e1f414b6 | 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 | |
2281e7a9 | 212 | If Port is not aligned on a 16-bit boundary, then ASSERT().\r |
9095d37b | 213 | \r |
3571fc90 MX |
214 | For Td guest TDVMCALL_IO is invoked to write I/O port.\r |
215 | \r | |
e1f414b6 | 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 | |
35a17154 | 219 | @return The value written to the I/O port.\r |
e1f414b6 | 220 | \r |
221 | **/\r | |
222 | UINT16\r | |
223 | EFIAPI\r | |
224 | IoWrite16 (\r | |
2f88bd3a MK |
225 | IN UINTN Port,\r |
226 | IN UINT16 Value\r | |
e1f414b6 | 227 | )\r |
228 | {\r | |
2f88bd3a | 229 | BOOLEAN Flag;\r |
38c8be12 | 230 | \r |
e1f414b6 | 231 | ASSERT ((Port & 1) == 0);\r |
38c8be12 | 232 | \r |
2f88bd3a | 233 | Flag = FilterBeforeIoWrite (FilterWidth16, Port, &Value);\r |
38c8be12 | 234 | if (Flag) {\r |
3571fc90 MX |
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 | |
38c8be12 | 242 | }\r |
2f88bd3a | 243 | \r |
38c8be12 DB |
244 | FilterAfterIoWrite (FilterWidth16, Port, &Value);\r |
245 | \r | |
e1f414b6 | 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 | |
2281e7a9 | 257 | If Port is not aligned on a 32-bit boundary, then ASSERT().\r |
9095d37b | 258 | \r |
3571fc90 MX |
259 | For Td guest TDVMCALL_IO is invoked to read I/O port.\r |
260 | \r | |
e1f414b6 | 261 | @param Port The I/O port to read.\r |
262 | \r | |
2281e7a9 | 263 | @return The value read.\r |
e1f414b6 | 264 | \r |
265 | **/\r | |
266 | UINT32\r | |
267 | EFIAPI\r | |
268 | IoRead32 (\r | |
2f88bd3a | 269 | IN UINTN Port\r |
e1f414b6 | 270 | )\r |
271 | {\r | |
2f88bd3a MK |
272 | UINT32 Value;\r |
273 | BOOLEAN Flag;\r | |
e1f414b6 | 274 | \r |
275 | ASSERT ((Port & 3) == 0);\r | |
38c8be12 | 276 | \r |
2f88bd3a | 277 | Flag = FilterBeforeIoRead (FilterWidth32, Port, &Value);\r |
38c8be12 | 278 | if (Flag) {\r |
3571fc90 MX |
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 | |
38c8be12 | 286 | }\r |
2f88bd3a | 287 | \r |
38c8be12 DB |
288 | FilterAfterIoRead (FilterWidth32, Port, &Value);\r |
289 | \r | |
e1f414b6 | 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 | |
2281e7a9 | 301 | If Port is not aligned on a 32-bit boundary, then ASSERT().\r |
9095d37b | 302 | \r |
3571fc90 MX |
303 | For Td guest TDVMCALL_IO is invoked to write I/O port.\r |
304 | \r | |
e1f414b6 | 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 | |
35a17154 | 308 | @return The value written to the I/O port.\r |
e1f414b6 | 309 | \r |
310 | **/\r | |
311 | UINT32\r | |
312 | EFIAPI\r | |
313 | IoWrite32 (\r | |
2f88bd3a MK |
314 | IN UINTN Port,\r |
315 | IN UINT32 Value\r | |
e1f414b6 | 316 | )\r |
317 | {\r | |
2f88bd3a | 318 | BOOLEAN Flag;\r |
38c8be12 | 319 | \r |
e1f414b6 | 320 | ASSERT ((Port & 3) == 0);\r |
38c8be12 | 321 | \r |
2f88bd3a | 322 | Flag = FilterBeforeIoWrite (FilterWidth32, Port, &Value);\r |
38c8be12 | 323 | if (Flag) {\r |
3571fc90 MX |
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 | |
38c8be12 | 331 | }\r |
2f88bd3a | 332 | \r |
38c8be12 DB |
333 | FilterAfterIoWrite (FilterWidth32, Port, &Value);\r |
334 | \r | |
e1f414b6 | 335 | return Value;\r |
336 | }\r |