]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c
MdePkg/IoLib: Filter/trace port IO/MMIO access
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibMsc.c
CommitLineData
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
16\r
1efcc4ae 17\r
f734a10a 18#include "BaseIoLibIntrinsicInternal.h"\r
e1f414b6 19\r
20//\r
42eedea9 21// Microsoft Visual Studio 7.1 Function Prototypes for I/O Intrinsics.\r
e1f414b6 22//\r
42eedea9 23\r
e1f414b6 24int _inp (unsigned short port);\r
25unsigned short _inpw (unsigned short port);\r
26unsigned long _inpd (unsigned short port);\r
27int _outp (unsigned short port, int databyte );\r
28unsigned short _outpw (unsigned short port, unsigned short dataword );\r
29unsigned long _outpd (unsigned short port, unsigned long dataword );\r
30void _ReadWriteBarrier (void);\r
31\r
32#pragma intrinsic(_inp)\r
33#pragma intrinsic(_inpw)\r
34#pragma intrinsic(_inpd)\r
35#pragma intrinsic(_outp)\r
36#pragma intrinsic(_outpw)\r
37#pragma intrinsic(_outpd)\r
38#pragma intrinsic(_ReadWriteBarrier)\r
986352be 39\r
e1f414b6 40//\r
41// _ReadWriteBarrier() forces memory reads and writes to complete at the point\r
42// in the call. This is only a hint to the compiler and does emit code.\r
43// In past versions of the compiler, _ReadWriteBarrier was enforced only\r
44// locally and did not affect functions up the call tree. In Visual C++\r
45// 2005, _ReadWriteBarrier is enforced all the way up the call tree.\r
46//\r
47\r
48/**\r
49 Reads an 8-bit I/O port.\r
50\r
51 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.\r
52 This function must guarantee that all I/O read and write operations are\r
53 serialized.\r
54\r
55 If 8-bit I/O port operations are not supported, then ASSERT().\r
56\r
57 @param Port The I/O port to read.\r
58\r
2281e7a9 59 @return The value read.\r
e1f414b6 60\r
61**/\r
62UINT8\r
63EFIAPI\r
64IoRead8 (\r
65 IN UINTN Port\r
66 )\r
67{\r
68 UINT8 Value;\r
38c8be12
DB
69 BOOLEAN Flag;\r
70\r
71 Flag = FilterBeforeIoRead (FilterWidth8, Port, &Value);\r
72 if (Flag) {\r
73 _ReadWriteBarrier ();\r
74 Value = (UINT8)_inp ((UINT16)Port);\r
75 _ReadWriteBarrier ();\r
76 }\r
77 FilterAfterIoRead (FilterWidth8, Port, &Value);\r
e1f414b6 78\r
e1f414b6 79 return Value;\r
80}\r
81\r
82/**\r
83 Writes an 8-bit I/O port.\r
84\r
85 Writes the 8-bit I/O port specified by Port with the value specified by Value\r
86 and returns Value. This function must guarantee that all I/O read and write\r
87 operations are serialized.\r
88\r
89 If 8-bit I/O port operations are not supported, then ASSERT().\r
90\r
91 @param Port The I/O port to write.\r
92 @param Value The value to write to the I/O port.\r
93\r
35a17154 94 @return The value written to the I/O port.\r
e1f414b6 95\r
96**/\r
97UINT8\r
98EFIAPI\r
99IoWrite8 (\r
100 IN UINTN Port,\r
101 IN UINT8 Value\r
102 )\r
103{\r
38c8be12
DB
104 BOOLEAN Flag;\r
105\r
106 Flag = FilterBeforeIoWrite(FilterWidth8, Port, &Value);\r
107 if (Flag) {\r
108 _ReadWriteBarrier ();\r
109 (UINT8)_outp ((UINT16)Port, Value);\r
110 _ReadWriteBarrier ();\r
111 }\r
112 FilterAfterIoWrite (FilterWidth8, Port, &Value);\r
113\r
e1f414b6 114 return Value;\r
115}\r
116\r
117/**\r
118 Reads a 16-bit I/O port.\r
119\r
120 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.\r
121 This function must guarantee that all I/O read and write operations are\r
122 serialized.\r
123\r
124 If 16-bit I/O port operations are not supported, then ASSERT().\r
2281e7a9 125 If Port is not aligned on a 16-bit boundary, then ASSERT().\r
e1f414b6 126\r
127 @param Port The I/O port to read.\r
128\r
2281e7a9 129 @return The value read.\r
e1f414b6 130\r
131**/\r
132UINT16\r
133EFIAPI\r
134IoRead16 (\r
135 IN UINTN Port\r
136 )\r
137{\r
138 UINT16 Value;\r
38c8be12 139 BOOLEAN Flag;\r
e1f414b6 140\r
141 ASSERT ((Port & 1) == 0);\r
38c8be12
DB
142\r
143 Flag = FilterBeforeIoRead (FilterWidth16, Port, &Value);\r
144 if (Flag) {\r
145 _ReadWriteBarrier ();\r
146 Value = _inpw ((UINT16)Port);\r
147 _ReadWriteBarrier ();\r
148 }\r
149 FilterBeforeIoRead (FilterWidth16, Port, &Value);\r
150\r
e1f414b6 151 return Value;\r
152}\r
153\r
154/**\r
155 Writes a 16-bit I/O port.\r
156\r
157 Writes the 16-bit I/O port specified by Port with the value specified by Value\r
158 and returns Value. This function must guarantee that all I/O read and write\r
159 operations are serialized.\r
160\r
161 If 16-bit I/O port operations are not supported, then ASSERT().\r
2281e7a9 162 If Port is not aligned on a 16-bit boundary, then ASSERT().\r
9095d37b 163\r
e1f414b6 164 @param Port The I/O port to write.\r
165 @param Value The value to write to the I/O port.\r
166\r
35a17154 167 @return The value written to the I/O port.\r
e1f414b6 168\r
169**/\r
170UINT16\r
171EFIAPI\r
172IoWrite16 (\r
173 IN UINTN Port,\r
174 IN UINT16 Value\r
175 )\r
176{\r
38c8be12
DB
177 BOOLEAN Flag;\r
178\r
e1f414b6 179 ASSERT ((Port & 1) == 0);\r
38c8be12
DB
180\r
181 Flag = FilterBeforeIoWrite(FilterWidth16, Port, &Value);\r
182 if (Flag) {\r
183 _ReadWriteBarrier ();\r
184 _outpw ((UINT16)Port, Value);\r
185 _ReadWriteBarrier ();\r
186 }\r
187 FilterAfterIoWrite (FilterWidth16, Port, &Value);\r
188\r
e1f414b6 189 return Value;\r
190}\r
191\r
192/**\r
193 Reads a 32-bit I/O port.\r
194\r
195 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.\r
196 This function must guarantee that all I/O read and write operations are\r
197 serialized.\r
198\r
199 If 32-bit I/O port operations are not supported, then ASSERT().\r
2281e7a9 200 If Port is not aligned on a 32-bit boundary, then ASSERT().\r
9095d37b 201\r
e1f414b6 202 @param Port The I/O port to read.\r
203\r
2281e7a9 204 @return The value read.\r
e1f414b6 205\r
206**/\r
207UINT32\r
208EFIAPI\r
209IoRead32 (\r
210 IN UINTN Port\r
211 )\r
212{\r
213 UINT32 Value;\r
38c8be12 214 BOOLEAN Flag;\r
e1f414b6 215\r
216 ASSERT ((Port & 3) == 0);\r
38c8be12
DB
217\r
218 Flag = FilterBeforeIoRead(FilterWidth32, Port, &Value);\r
219 if (Flag) {\r
220 _ReadWriteBarrier ();\r
221 Value = _inpd ((UINT16)Port);\r
222 _ReadWriteBarrier ();\r
223 }\r
224 FilterAfterIoRead (FilterWidth32, Port, &Value);\r
225\r
e1f414b6 226 return Value;\r
227}\r
228\r
229/**\r
230 Writes a 32-bit I/O port.\r
231\r
232 Writes the 32-bit I/O port specified by Port with the value specified by Value\r
233 and returns Value. This function must guarantee that all I/O read and write\r
234 operations are serialized.\r
235\r
236 If 32-bit I/O port operations are not supported, then ASSERT().\r
2281e7a9 237 If Port is not aligned on a 32-bit boundary, then ASSERT().\r
9095d37b 238\r
e1f414b6 239 @param Port The I/O port to write.\r
240 @param Value The value to write to the I/O port.\r
241\r
35a17154 242 @return The value written to the I/O port.\r
e1f414b6 243\r
244**/\r
245UINT32\r
246EFIAPI\r
247IoWrite32 (\r
248 IN UINTN Port,\r
249 IN UINT32 Value\r
250 )\r
251{\r
38c8be12
DB
252 BOOLEAN Flag;\r
253\r
e1f414b6 254 ASSERT ((Port & 3) == 0);\r
38c8be12
DB
255\r
256 Flag = FilterBeforeIoWrite(FilterWidth32, Port, &Value);\r
257 if (Flag) {\r
258 _ReadWriteBarrier ();\r
259 _outpd ((UINT16)Port, Value);\r
260 _ReadWriteBarrier ();\r
261 }\r
262 FilterAfterIoWrite (FilterWidth32, Port, &Value);\r
263\r
e1f414b6 264 return Value;\r
265}\r