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