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