]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibMsc.c
1. Added ReadWriteBarrier() before and after each I/O read/write function.
[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, Intel Corporation<BR>
12 All rights reserved. This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php
16
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19
20 Module Name: IoLibMsc.c
21
22 **/
23
24
25 #if _MSC_EXTENSIONS
26
27 //
28 // Microsoft Visual Studio 7.1 Function Prototypes for I/O Intrinsics
29 //
30 int _inp (unsigned short port);
31 unsigned short _inpw (unsigned short port);
32 unsigned long _inpd (unsigned short port);
33 int _outp (unsigned short port, int databyte );
34 unsigned short _outpw (unsigned short port, unsigned short dataword );
35 unsigned long _outpd (unsigned short port, unsigned long dataword );
36 void _ReadWriteBarrier (void);
37
38 #pragma intrinsic(_inp)
39 #pragma intrinsic(_inpw)
40 #pragma intrinsic(_inpd)
41 #pragma intrinsic(_outp)
42 #pragma intrinsic(_outpw)
43 #pragma intrinsic(_outpd)
44 #pragma intrinsic(_ReadWriteBarrier)
45
46 //
47 // _ReadWriteBarrier() forces memory reads and writes to complete at the point
48 // in the call. This is only a hint to the compiler and does emit code.
49 // In past versions of the compiler, _ReadWriteBarrier was enforced only
50 // locally and did not affect functions up the call tree. In Visual C++
51 // 2005, _ReadWriteBarrier is enforced all the way up the call tree.
52 //
53
54 /**
55 Reads an 8-bit I/O port.
56
57 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
58 This function must guarantee that all I/O read and write operations are
59 serialized.
60
61 If 8-bit I/O port operations are not supported, then ASSERT().
62
63 @param Port The I/O port to read.
64
65 @return The value read.
66
67 **/
68 UINT8
69 EFIAPI
70 IoRead8 (
71 IN UINTN Port
72 )
73 {
74 UINT8 Value;
75
76 _ReadWriteBarrier ();
77 Value = (UINT8)_inp ((UINT16)Port);
78 _ReadWriteBarrier ();
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 the I/O port.
95
96 **/
97 UINT8
98 EFIAPI
99 IoWrite8 (
100 IN UINTN Port,
101 IN UINT8 Value
102 )
103 {
104 _ReadWriteBarrier ();
105 (UINT8)_outp ((UINT16)Port, Value);
106 _ReadWriteBarrier ();
107 return Value;
108 }
109
110 /**
111 Reads a 16-bit I/O port.
112
113 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
114 This function must guarantee that all I/O read and write operations are
115 serialized.
116
117 If 16-bit I/O port operations are not supported, then ASSERT().
118
119 @param Port The I/O port to read.
120
121 @return The value read.
122
123 **/
124 UINT16
125 EFIAPI
126 IoRead16 (
127 IN UINTN Port
128 )
129 {
130 UINT16 Value;
131
132 ASSERT ((Port & 1) == 0);
133 _ReadWriteBarrier ();
134 Value = _inpw ((UINT16)Port);
135 _ReadWriteBarrier ();
136 return Value;
137 }
138
139 /**
140 Writes a 16-bit I/O port.
141
142 Writes the 16-bit I/O port specified by Port with the value specified by Value
143 and returns Value. This function must guarantee that all I/O read and write
144 operations are serialized.
145
146 If 16-bit I/O port operations are not supported, then ASSERT().
147
148 @param Port The I/O port to write.
149 @param Value The value to write to the I/O port.
150
151 @return The value written the I/O port.
152
153 **/
154 UINT16
155 EFIAPI
156 IoWrite16 (
157 IN UINTN Port,
158 IN UINT16 Value
159 )
160 {
161 ASSERT ((Port & 1) == 0);
162 _ReadWriteBarrier ();
163 _outpw ((UINT16)Port, Value);
164 _ReadWriteBarrier ();
165 return Value;
166 }
167
168 /**
169 Reads a 32-bit I/O port.
170
171 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
172 This function must guarantee that all I/O read and write operations are
173 serialized.
174
175 If 32-bit I/O port operations are not supported, then ASSERT().
176
177 @param Port The I/O port to read.
178
179 @return The value read.
180
181 **/
182 UINT32
183 EFIAPI
184 IoRead32 (
185 IN UINTN Port
186 )
187 {
188 UINT32 Value;
189
190 ASSERT ((Port & 3) == 0);
191 _ReadWriteBarrier ();
192 Value = _inpd ((UINT16)Port);
193 _ReadWriteBarrier ();
194 return Value;
195 }
196
197 /**
198 Writes a 32-bit I/O port.
199
200 Writes the 32-bit I/O port specified by Port with the value specified by Value
201 and returns Value. This function must guarantee that all I/O read and write
202 operations are serialized.
203
204 If 32-bit I/O port operations are not supported, then ASSERT().
205
206 @param Port The I/O port to write.
207 @param Value The value to write to the I/O port.
208
209 @return The value written the I/O port.
210
211 **/
212 UINT32
213 EFIAPI
214 IoWrite32 (
215 IN UINTN Port,
216 IN UINT32 Value
217 )
218 {
219 ASSERT ((Port & 3) == 0);
220 _ReadWriteBarrier ();
221 _outpd ((UINT16)Port, Value);
222 _ReadWriteBarrier ();
223 return Value;
224 }
225
226
227 /**
228 Reads an 8-bit MMIO register.
229
230 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
231 returned. This function must guarantee that all MMIO read and write
232 operations are serialized.
233
234 If 8-bit MMIO register operations are not supported, then ASSERT().
235
236 @param Address The MMIO register to read.
237
238 @return The value read.
239
240 **/
241 UINT8
242 EFIAPI
243 MmioRead8 (
244 IN UINTN Address
245 )
246 {
247 UINT8 Value;
248
249 Value = *(volatile UINT8*)Address;
250 return Value;
251 }
252
253 /**
254 Writes an 8-bit MMIO register.
255
256 Writes the 8-bit MMIO register specified by Address with the value specified
257 by Value and returns Value. This function must guarantee that all MMIO read
258 and write operations are serialized.
259
260 If 8-bit MMIO register operations are not supported, then ASSERT().
261
262 @param Address The MMIO register to write.
263 @param Value The value to write to the MMIO register.
264
265 **/
266 UINT8
267 EFIAPI
268 MmioWrite8 (
269 IN UINTN Address,
270 IN UINT8 Value
271 )
272 {
273 return *(volatile UINT8*)Address = Value;
274 }
275
276 /**
277 Reads a 16-bit MMIO register.
278
279 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
280 returned. This function must guarantee that all MMIO read and write
281 operations are serialized.
282
283 If 16-bit MMIO register operations are not supported, then ASSERT().
284
285 @param Address The MMIO register to read.
286
287 @return The value read.
288
289 **/
290 UINT16
291 EFIAPI
292 MmioRead16 (
293 IN UINTN Address
294 )
295 {
296 UINT16 Value;
297
298 ASSERT ((Address & 1) == 0);
299 Value = *(volatile UINT16*)Address;
300 return Value;
301 }
302
303 /**
304 Writes a 16-bit MMIO register.
305
306 Writes the 16-bit MMIO register specified by Address with the value specified
307 by Value and returns Value. This function must guarantee that all MMIO read
308 and write operations are serialized.
309
310 If 16-bit MMIO register operations are not supported, then ASSERT().
311
312 @param Address The MMIO register to write.
313 @param Value The value to write to the MMIO register.
314
315 **/
316 UINT16
317 EFIAPI
318 MmioWrite16 (
319 IN UINTN Address,
320 IN UINT16 Value
321 )
322 {
323 ASSERT ((Address & 1) == 0);
324 return *(volatile UINT16*)Address = Value;
325 }
326
327 /**
328 Reads a 32-bit MMIO register.
329
330 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
331 returned. This function must guarantee that all MMIO read and write
332 operations are serialized.
333
334 If 32-bit MMIO register operations are not supported, then ASSERT().
335
336 @param Address The MMIO register to read.
337
338 @return The value read.
339
340 **/
341 UINT32
342 EFIAPI
343 MmioRead32 (
344 IN UINTN Address
345 )
346 {
347 UINT32 Value;
348
349 ASSERT ((Address & 3) == 0);
350 Value = *(volatile UINT32*)Address;
351 return Value;
352 }
353
354 /**
355 Writes a 32-bit MMIO register.
356
357 Writes the 32-bit MMIO register specified by Address with the value specified
358 by Value and returns Value. This function must guarantee that all MMIO read
359 and write operations are serialized.
360
361 If 32-bit MMIO register operations are not supported, then ASSERT().
362
363 @param Address The MMIO register to write.
364 @param Value The value to write to the MMIO register.
365
366 **/
367 UINT32
368 EFIAPI
369 MmioWrite32 (
370 IN UINTN Address,
371 IN UINT32 Value
372 )
373 {
374 ASSERT ((Address & 3) == 0);
375 return *(volatile UINT32*)Address = Value;
376 }
377
378 /**
379 Reads a 64-bit MMIO register.
380
381 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
382 returned. This function must guarantee that all MMIO read and write
383 operations are serialized.
384
385 If 64-bit MMIO register operations are not supported, then ASSERT().
386
387 @param Address The MMIO register to read.
388
389 @return The value read.
390
391 **/
392 UINT64
393 EFIAPI
394 MmioRead64 (
395 IN UINTN Address
396 )
397 {
398 UINT64 Value;
399
400 ASSERT ((Address & 7) == 0);
401 Value = *(volatile UINT64*)Address;
402 return Value;
403 }
404
405 /**
406 Writes a 64-bit MMIO register.
407
408 Writes the 64-bit MMIO register specified by Address with the value specified
409 by Value and returns Value. This function must guarantee that all MMIO read
410 and write operations are serialized.
411
412 If 64-bit MMIO register operations are not supported, then ASSERT().
413
414 @param Address The MMIO register to write.
415 @param Value The value to write to the MMIO register.
416
417 **/
418 UINT64
419 EFIAPI
420 MmioWrite64 (
421 IN UINTN Address,
422 IN UINT64 Value
423 )
424 {
425 ASSERT ((Address & 7) == 0);
426 return *(volatile UINT64*)Address = Value;
427 }
428
429 #endif