]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLib.c
MdePkg/IoLib: Filter/trace port IO/MMIO access
[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
11 /**
12 Reads a 64-bit I/O port.
13
14 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
15 This function must guarantee that all I/O read and write operations are
16 serialized.
17
18 If 64-bit I/O port operations are not supported, then ASSERT().
19 If Port is not aligned on a 64-bit boundary, then ASSERT().
20
21 @param Port The I/O port to read.
22
23 @return The value read.
24
25 **/
26 UINT64
27 EFIAPI
28 IoRead64 (
29 IN UINTN Port
30 )
31 {
32 ASSERT (FALSE);
33 return 0;
34 }
35
36 /**
37 Writes a 64-bit I/O port.
38
39 Writes the 64-bit I/O port specified by Port with the value specified by Value
40 and returns Value. This function must guarantee that all I/O read and write
41 operations are serialized.
42
43 If 64-bit I/O port operations are not supported, then ASSERT().
44 If Port is not aligned on a 64-bit boundary, then ASSERT().
45
46 @param Port The I/O port to write.
47 @param Value The value to write to the I/O port.
48
49 @return The value written the I/O port.
50
51 **/
52 UINT64
53 EFIAPI
54 IoWrite64 (
55 IN UINTN Port,
56 IN UINT64 Value
57 )
58 {
59 ASSERT (FALSE);
60 return 0;
61 }
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 @param Address The MMIO register to read.
74
75 @return The value read.
76
77 **/
78 UINT8
79 EFIAPI
80 MmioRead8 (
81 IN UINTN Address
82 )
83 {
84 UINT8 Value;
85 BOOLEAN Flag;
86
87 Flag = FilterBeforeMmIoRead (FilterWidth8, Address, &Value);
88 if (Flag) {
89 MemoryFence ();
90 Value = *(volatile UINT8*)Address;
91 MemoryFence ();
92 }
93 FilterAfterMmIoRead (FilterWidth8, Address, &Value);
94
95 return Value;
96 }
97
98 /**
99 Writes an 8-bit MMIO register.
100
101 Writes the 8-bit MMIO register specified by Address with the value specified
102 by Value and returns Value. This function must guarantee that all MMIO read
103 and write operations are serialized.
104
105 If 8-bit MMIO register operations are not supported, then ASSERT().
106
107 @param Address The MMIO register to write.
108 @param Value The value to write to the MMIO register.
109
110 @return Value.
111
112 **/
113 UINT8
114 EFIAPI
115 MmioWrite8 (
116 IN UINTN Address,
117 IN UINT8 Value
118 )
119 {
120 BOOLEAN Flag;
121
122 Flag = FilterBeforeMmIoWrite (FilterWidth8, Address, &Value);
123 if (Flag) {
124 MemoryFence ();
125 *(volatile UINT8*)Address = Value;
126 MemoryFence ();
127 }
128 FilterAfterMmIoWrite (FilterWidth8, Address, &Value);
129
130 return Value;
131 }
132
133 /**
134 Reads a 16-bit MMIO register.
135
136 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
137 returned. This function must guarantee that all MMIO read and write
138 operations are serialized.
139
140 If 16-bit MMIO register operations are not supported, then ASSERT().
141 If Address is not aligned on a 16-bit boundary, then ASSERT().
142
143 @param Address The MMIO register to read.
144
145 @return The value read.
146
147 **/
148 UINT16
149 EFIAPI
150 MmioRead16 (
151 IN UINTN Address
152 )
153 {
154 UINT16 Value;
155 BOOLEAN Flag;
156
157 ASSERT ((Address & 1) == 0);
158 Flag = FilterBeforeMmIoRead (FilterWidth16, Address, &Value);
159 if (Flag) {
160 MemoryFence ();
161 Value = *(volatile UINT16*)Address;
162 MemoryFence ();
163 }
164 FilterAfterMmIoRead (FilterWidth16, Address, &Value);
165
166 return Value;
167 }
168
169 /**
170 Writes a 16-bit MMIO register.
171
172 Writes the 16-bit MMIO register specified by Address with the value specified
173 by Value and returns Value. This function must guarantee that all MMIO read
174 and write operations are serialized.
175
176 If 16-bit MMIO register operations are not supported, then ASSERT().
177 If Address is not aligned on a 16-bit boundary, then ASSERT().
178
179 @param Address The MMIO register to write.
180 @param Value The value to write to the MMIO register.
181
182 @return Value.
183
184 **/
185 UINT16
186 EFIAPI
187 MmioWrite16 (
188 IN UINTN Address,
189 IN UINT16 Value
190 )
191 {
192 BOOLEAN Flag;
193
194 ASSERT ((Address & 1) == 0);
195
196 Flag = FilterBeforeMmIoWrite (FilterWidth16, Address, &Value);
197 if (Flag) {
198 MemoryFence ();
199 *(volatile UINT16*)Address = Value;
200 MemoryFence ();
201 }
202 FilterAfterMmIoWrite (FilterWidth16, Address, &Value);
203
204 return Value;
205 }
206
207 /**
208 Reads a 32-bit MMIO register.
209
210 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
211 returned. This function must guarantee that all MMIO read and write
212 operations are serialized.
213
214 If 32-bit MMIO register operations are not supported, then ASSERT().
215 If Address is not aligned on a 32-bit boundary, then ASSERT().
216
217 @param Address The MMIO register to read.
218
219 @return The value read.
220
221 **/
222 UINT32
223 EFIAPI
224 MmioRead32 (
225 IN UINTN Address
226 )
227 {
228 UINT32 Value;
229 BOOLEAN Flag;
230
231 ASSERT ((Address & 3) == 0);
232
233 Flag = FilterBeforeMmIoRead (FilterWidth32, Address, &Value);
234 if (Flag) {
235 MemoryFence ();
236 Value = *(volatile UINT32*)Address;
237 MemoryFence ();
238 }
239 FilterAfterMmIoRead (FilterWidth32, Address, &Value);
240
241 return Value;
242 }
243
244 /**
245 Writes a 32-bit MMIO register.
246
247 Writes the 32-bit MMIO register specified by Address with the value specified
248 by Value and returns Value. This function must guarantee that all MMIO read
249 and write operations are serialized.
250
251 If 32-bit MMIO register operations are not supported, then ASSERT().
252 If Address is not aligned on a 32-bit boundary, then ASSERT().
253
254 @param Address The MMIO register to write.
255 @param Value The value to write to the MMIO register.
256
257 @return Value.
258
259 **/
260 UINT32
261 EFIAPI
262 MmioWrite32 (
263 IN UINTN Address,
264 IN UINT32 Value
265 )
266 {
267 BOOLEAN Flag;
268
269 ASSERT ((Address & 3) == 0);
270
271 Flag = FilterBeforeMmIoWrite (FilterWidth32, Address, &Value);
272 if (Flag) {
273 MemoryFence ();
274 *(volatile UINT32*)Address = Value;
275 MemoryFence ();
276 }
277 FilterAfterMmIoWrite (FilterWidth32, Address, &Value);
278
279 return Value;
280 }
281
282 /**
283 Reads a 64-bit MMIO register.
284
285 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
286 returned. This function must guarantee that all MMIO read and write
287 operations are serialized.
288
289 If 64-bit MMIO register operations are not supported, then ASSERT().
290 If Address is not aligned on a 64-bit boundary, then ASSERT().
291
292 @param Address The MMIO register to read.
293
294 @return The value read.
295
296 **/
297 UINT64
298 EFIAPI
299 MmioRead64 (
300 IN UINTN Address
301 )
302 {
303 UINT64 Value;
304 BOOLEAN Flag;
305
306 ASSERT ((Address & 7) == 0);
307
308 Flag = FilterBeforeMmIoRead (FilterWidth64, Address, &Value);
309 if (Flag) {
310 MemoryFence ();
311 Value = *(volatile UINT64*)Address;
312 MemoryFence ();
313 }
314 FilterAfterMmIoRead (FilterWidth64, Address, &Value);
315
316 return Value;
317 }
318
319 /**
320 Writes a 64-bit MMIO register.
321
322 Writes the 64-bit MMIO register specified by Address with the value specified
323 by Value and returns Value. This function must guarantee that all MMIO read
324 and write operations are serialized.
325
326 If 64-bit MMIO register operations are not supported, then ASSERT().
327 If Address is not aligned on a 64-bit boundary, then ASSERT().
328
329 @param Address The MMIO register to write.
330 @param Value The value to write to the MMIO register.
331
332 **/
333 UINT64
334 EFIAPI
335 MmioWrite64 (
336 IN UINTN Address,
337 IN UINT64 Value
338 )
339 {
340 BOOLEAN Flag;
341
342 ASSERT ((Address & 7) == 0);
343
344 Flag = FilterBeforeMmIoWrite (FilterWidth64, Address, &Value);
345 if (Flag) {
346 MemoryFence ();
347 *(volatile UINT64*)Address = Value;
348 MemoryFence ();
349 }
350 FilterAfterMmIoWrite (FilterWidth64, Address, &Value);
351
352 return Value;
353 }
354