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