]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibGcc.c
b3a1a2025663ba970a9362a2da3624ed330cb487
[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 - 2018, Intel Corporation. All rights reserved.<BR>
14 SPDX-License-Identifier: BSD-2-Clause-Patent
15
16 **/
17
18
19 #include "BaseIoLibIntrinsicInternal.h"
20
21 /**
22 Reads an 8-bit I/O port.
23
24 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
25 This function must guarantee that all I/O read and write operations are
26 serialized.
27
28 If 8-bit I/O port operations are not supported, then ASSERT().
29
30 @param Port The I/O port to read.
31
32 @return The value read.
33
34 **/
35 UINT8
36 EFIAPI
37 IoRead8 (
38 IN UINTN Port
39 )
40 {
41 UINT8 Data;
42
43 __asm__ __volatile__ ("inb %w1,%b0" : "=a" (Data) : "d" ((UINT16)Port));
44 return Data;
45 }
46
47 /**
48 Writes an 8-bit I/O port.
49
50 Writes the 8-bit I/O port specified by Port with the value specified by Value
51 and returns Value. This function must guarantee that all I/O read and write
52 operations are serialized.
53
54 If 8-bit I/O port operations are not supported, then ASSERT().
55
56 @param Port The I/O port to write.
57 @param Value The value to write to the I/O port.
58
59 @return The value written the I/O port.
60
61 **/
62 UINT8
63 EFIAPI
64 IoWrite8 (
65 IN UINTN Port,
66 IN UINT8 Value
67 )
68 {
69 __asm__ __volatile__ ("outb %b0,%w1" : : "a" (Value), "d" ((UINT16)Port));
70 return Value;;
71 }
72
73 /**
74 Reads a 16-bit I/O port.
75
76 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
77 This function must guarantee that all I/O read and write operations are
78 serialized.
79
80 If 16-bit I/O port operations are not supported, then ASSERT().
81 If Port is not aligned on a 16-bit boundary, then ASSERT().
82
83 @param Port The I/O port to read.
84
85 @return The value read.
86
87 **/
88 UINT16
89 EFIAPI
90 IoRead16 (
91 IN UINTN Port
92 )
93 {
94 UINT16 Data;
95
96 ASSERT ((Port & 1) == 0);
97 __asm__ __volatile__ ("inw %w1,%w0" : "=a" (Data) : "d" ((UINT16)Port));
98 return Data;
99 }
100
101 /**
102 Writes a 16-bit I/O port.
103
104 Writes the 16-bit I/O port specified by Port with the value specified by Value
105 and returns Value. This function must guarantee that all I/O read and write
106 operations are serialized.
107
108 If 16-bit I/O port operations are not supported, then ASSERT().
109 If Port is not aligned on a 16-bit boundary, then ASSERT().
110
111 @param Port The I/O port to write.
112 @param Value The value to write to the I/O port.
113
114 @return The value written the I/O port.
115
116 **/
117 UINT16
118 EFIAPI
119 IoWrite16 (
120 IN UINTN Port,
121 IN UINT16 Value
122 )
123 {
124 ASSERT ((Port & 1) == 0);
125 __asm__ __volatile__ ("outw %w0,%w1" : : "a" (Value), "d" ((UINT16)Port));
126 return Value;;
127 }
128
129 /**
130 Reads a 32-bit I/O port.
131
132 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
133 This function must guarantee that all I/O read and write operations are
134 serialized.
135
136 If 32-bit I/O port operations are not supported, then ASSERT().
137 If Port is not aligned on a 32-bit boundary, then ASSERT().
138
139 @param Port The I/O port to read.
140
141 @return The value read.
142
143 **/
144 UINT32
145 EFIAPI
146 IoRead32 (
147 IN UINTN Port
148 )
149 {
150 UINT32 Data;
151
152 ASSERT ((Port & 3) == 0);
153 __asm__ __volatile__ ("inl %w1,%0" : "=a" (Data) : "d" ((UINT16)Port));
154 return Data;
155 }
156
157 /**
158 Writes a 32-bit I/O port.
159
160 Writes the 32-bit I/O port specified by Port with the value specified by Value
161 and returns Value. This function must guarantee that all I/O read and write
162 operations are serialized.
163
164 If 32-bit I/O port operations are not supported, then ASSERT().
165 If Port is not aligned on a 32-bit boundary, then ASSERT().
166
167 @param Port The I/O port to write.
168 @param Value The value to write to the I/O port.
169
170 @return The value written the I/O port.
171
172 **/
173 UINT32
174 EFIAPI
175 IoWrite32 (
176 IN UINTN Port,
177 IN UINT32 Value
178 )
179 {
180 ASSERT ((Port & 3) == 0);
181 __asm__ __volatile__ ("outl %0,%w1" : : "a" (Value), "d" ((UINT16)Port));
182 return Value;
183 }
184