]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c
db94f0f47e657489a18cc52c8bebe01b18abe355
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibGcc.c
1 /** @file
2 I/O Library. This file has compiler specifics for GCC as there is no
3 ANSI C standard for doing IO.
4
5 GCC - uses EFIAPI assembler. __asm__ calls GAS. __volatile__ makes sure the
6 compiler puts the assembler in this exact location. The complex GNUC
7 operations are not optimzed. It would be possible to also write these
8 with EFIAPI assembler.
9
10 We don't advocate putting compiler specifics in libraries or drivers but there
11 is no other way to make this work.
12
13 Copyright (c) 2006 - 2007, Intel Corporation<BR>
14 All rights reserved. This program and the accompanying materials
15 are licensed and made available under the terms and conditions of the BSD License
16 which accompanies this distribution. The full text of the license may be found at
17 http://opensource.org/licenses/bsd-license.php
18
19 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21
22 **/
23
24 //
25 // Include common header file for this module.
26 //
27 #include "BaseIoLibIntrinsicInternal.h"
28
29 /**
30 Reads an 8-bit MMIO register.
31
32 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
33 returned. This function must guarantee that all MMIO read and write
34 operations are serialized.
35
36 If 8-bit MMIO register operations are not supported, then ASSERT().
37
38 @param Address The MMIO register to read.
39
40 @return The value read from Address.
41
42 **/
43 UINT8
44 EFIAPI
45 MmioRead8 (
46 IN UINTN Address
47 )
48 {
49 return *(volatile UINT8*)Address;
50 }
51
52 /**
53 Writes an 8-bit MMIO register.
54
55 Writes the 8-bit MMIO register specified by Address with the value specified
56 by Value and returns Value. This function must guarantee that all MMIO read
57 and write operations are serialized.
58
59 If 8-bit MMIO register operations are not supported, then ASSERT().
60
61 @param Address The MMIO register to write.
62 @param Value The value to write to the MMIO register.
63
64 @return The value written to the Mmio. It equals to the input
65 Value instead of the actual value read back from the
66 Mmio.
67 **/
68 UINT8
69 EFIAPI
70 MmioWrite8 (
71 IN UINTN Address,
72 IN UINT8 Value
73 )
74 {
75 return *(volatile UINT8*)Address = Value;
76 }
77
78 /**
79 Reads a 16-bit MMIO register.
80
81 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
82 returned. This function must guarantee that all MMIO read and write
83 operations are serialized.
84
85 If 16-bit MMIO register operations are not supported, then ASSERT().
86
87 @param Address The MMIO register to read.
88
89 @return The value read from Address.
90
91 **/
92 UINT16
93 EFIAPI
94 MmioRead16 (
95 IN UINTN Address
96 )
97 {
98 ASSERT ((Address & 1) == 0);
99 return *(volatile UINT16*)Address;
100 }
101
102 /**
103 Writes a 16-bit MMIO register.
104
105 Writes the 16-bit MMIO register specified by Address with the value specified
106 by Value and returns Value. This function must guarantee that all MMIO read
107 and write operations are serialized.
108
109 If 16-bit MMIO register operations are not supported, then ASSERT().
110
111 @param Address The MMIO register to write.
112 @param Value The value to write to the MMIO register.
113
114 @return The value written to the Mmio. It equals to the input
115 Value instead of the actual value read back from the
116 Mmio.
117 **/
118 UINT16
119 EFIAPI
120 MmioWrite16 (
121 IN UINTN Address,
122 IN UINT16 Value
123 )
124 {
125 ASSERT ((Address & 1) == 0);
126 return *(volatile UINT16*)Address = Value;
127 }
128
129 /**
130 Reads a 32-bit MMIO register.
131
132 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
133 returned. This function must guarantee that all MMIO read and write
134 operations are serialized.
135
136 If 32-bit MMIO register operations are not supported, then ASSERT().
137
138 @param Address The MMIO register to read.
139
140 @return The value read from Address.
141
142 **/
143 UINT32
144 EFIAPI
145 MmioRead32 (
146 IN UINTN Address
147 )
148 {
149 ASSERT ((Address & 3) == 0);
150 return *(volatile UINT32*)Address;
151 }
152
153 /**
154 Writes a 32-bit MMIO register.
155
156 Writes the 32-bit MMIO register specified by Address with the value specified
157 by Value and returns Value. This function must guarantee that all MMIO read
158 and write operations are serialized.
159
160 If 32-bit MMIO register operations are not supported, then ASSERT().
161
162 @param Address The MMIO register to write.
163 @param Value The value to write to the MMIO register.
164
165 @return The value written to the Mmio. It equals to the input
166 Value instead of the actual value read back from the
167 Mmio.
168 **/
169 UINT32
170 EFIAPI
171 MmioWrite32 (
172 IN UINTN Address,
173 IN UINT32 Value
174 )
175 {
176 ASSERT ((Address & 3) == 0);
177 return *(volatile UINT32*)Address = Value;
178 }
179
180 /**
181 Reads a 64-bit MMIO register.
182
183 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
184 returned. This function must guarantee that all MMIO read and write
185 operations are serialized.
186
187 If 64-bit MMIO register operations are not supported, then ASSERT().
188
189 @param Address The MMIO register to read.
190
191 @return The value read from Address.
192
193 **/
194 UINT64
195 EFIAPI
196 MmioRead64 (
197 IN UINTN Address
198 )
199 {
200 ASSERT ((Address & 7) == 0);
201 return *(volatile UINT64*)Address;
202 }
203
204 /**
205 Writes a 64-bit MMIO register.
206
207 Writes the 64-bit MMIO register specified by Address with the value specified
208 by Value and returns Value. This function must guarantee that all MMIO read
209 and write operations are serialized.
210
211 If 64-bit MMIO register operations are not supported, then ASSERT().
212
213 @param Address The MMIO register to write.
214 @param Value The value to write to the MMIO register.
215
216 @return The value written to the Mmio. It equals to the input
217 Value instead of the actual value read back from the
218 Mmio.
219 **/
220 UINT64
221 EFIAPI
222 MmioWrite64 (
223 IN UINTN Address,
224 IN UINT64 Value
225 )
226 {
227 ASSERT ((Address & 7) == 0);
228 return *(volatile UINT64*)Address = Value;
229 }
230
231
232
233 /**
234 Reads an 8-bit I/O port.
235
236 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
237 This function must guarantee that all I/O read and write operations are
238 serialized.
239
240 If 8-bit I/O port operations are not supported, then ASSERT().
241
242 @param Port The I/O port to read.
243
244 @return The value read from Port.
245
246 **/
247 __inline__
248 UINT8
249 EFIAPI
250 IoRead8 (
251 IN UINTN Port
252 )
253 {
254 UINT8 Data;
255
256 __asm__ __volatile__ ("inb %w1,%b0" : "=a" (Data) : "d" ((UINT16)Port));
257 return Data;
258 }
259
260 /**
261 Writes an 8-bit I/O port.
262
263 Writes the 8-bit I/O port specified by Port with the value specified by Value
264 and returns Value. This function must guarantee that all I/O read and write
265 operations are serialized.
266
267 If 8-bit I/O port operations are not supported, then ASSERT().
268
269 @param Port The I/O port to write.
270 @param Value The value to write to the I/O port.
271
272 @return The value written to the I/O port. It equals to the
273 input Value instead of the actual value read back from
274 the I/O port.
275
276 **/
277 __inline__
278 UINT8
279 EFIAPI
280 IoWrite8 (
281 IN UINTN Port,
282 IN UINT8 Value
283 )
284 {
285 __asm__ __volatile__ ("outb %b0,%w1" : : "a" (Value), "d" ((UINT16)Port));
286 return Value;;
287 }
288
289 /**
290 Reads a 16-bit I/O port.
291
292 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
293 This function must guarantee that all I/O read and write operations are
294 serialized.
295
296 If 16-bit I/O port operations are not supported, then ASSERT().
297
298 @param Port The I/O port to read.
299
300 @return The value read from Port.
301
302 **/
303 __inline__
304 UINT16
305 EFIAPI
306 IoRead16 (
307 IN UINTN Port
308 )
309 {
310 UINT16 Data;
311
312 ASSERT ((Port & 1) == 0);
313 __asm__ __volatile__ ("inw %w1,%w0" : "=a" (Data) : "d" ((UINT16)Port));
314 return Data;
315 }
316
317 /**
318 Writes a 16-bit I/O port.
319
320 Writes the 16-bit I/O port specified by Port with the value specified by Value
321 and returns Value. This function must guarantee that all I/O read and write
322 operations are serialized.
323
324 If 16-bit I/O port operations are not supported, then ASSERT().
325
326 @param Port The I/O port to write.
327 @param Value The value to write to the I/O port.
328
329 @return The value written to the I/O port. It equals to the
330 input Value instead of the actual value read back from
331 the I/O port.
332
333 **/
334 __inline__
335 UINT16
336 EFIAPI
337 IoWrite16 (
338 IN UINTN Port,
339 IN UINT16 Value
340 )
341 {
342 ASSERT ((Port & 1) == 0);
343 __asm__ __volatile__ ("outw %w0,%w1" : : "a" (Value), "d" ((UINT16)Port));
344 return Value;;
345 }
346
347 /**
348 Reads a 32-bit I/O port.
349
350 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
351 This function must guarantee that all I/O read and write operations are
352 serialized.
353
354 If 32-bit I/O port operations are not supported, then ASSERT().
355
356 @param Port The I/O port to read.
357
358 @return The value read from Port.
359
360 **/
361 __inline__
362 UINT32
363 EFIAPI
364 IoRead32 (
365 IN UINTN Port
366 )
367 {
368 UINT32 Data;
369
370 ASSERT ((Port & 3) == 0);
371 __asm__ __volatile__ ("inl %w1,%0" : "=a" (Data) : "d" ((UINT16)Port));
372 return Data;
373 }
374
375 /**
376 Writes a 32-bit I/O port.
377
378 Writes the 32-bit I/O port specified by Port with the value specified by Value
379 and returns Value. This function must guarantee that all I/O read and write
380 operations are serialized.
381
382 If 32-bit I/O port operations are not supported, then ASSERT().
383
384 @param Port The I/O port to write.
385 @param Value The value to write to the I/O port.
386
387 @return The value written to the I/O port. It equals to the
388 input Value instead of the actual value read back from
389 the I/O port.
390
391 **/
392 __inline__
393 UINT32
394 EFIAPI
395 IoWrite32 (
396 IN UINTN Port,
397 IN UINT32 Value
398 )
399 {
400 ASSERT ((Port & 3) == 0);
401 __asm__ __volatile__ ("outl %0,%w1" : : "a" (Value), "d" ((UINT16)Port));
402 return Value;
403 }
404