]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibIpf.c
234c3369b29bbfc26bbfdff845d011b6a231bb44
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibIpf.c
1 /** @file
2 Common I/O Library routines.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: IoLibIpf.c
14
15 **/
16
17 #define BIT63 0x8000000000000000ULL
18
19 #define MAP_PORT_BASE_TO_MEM(_Port) \
20 ((((_Port) & 0xfffc) << 10) | ((_Port) & 0x0fff))
21
22 /**
23 Reads a 8-bit I/O port.
24
25 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
26 This function must guarantee that all I/O read and write operations are
27 serialized.
28
29 @param Port The I/O port to read.
30
31 @return The value read.
32
33 **/
34 UINT8
35 EFIAPI
36 IoRead8 (
37 IN UINT64 Port
38 )
39 {
40 UINT64 Address;
41
42 //
43 // Add the 64MB aligned IO Port space to the IO address
44 //
45 Address = MAP_PORT_BASE_TO_MEM (Port);
46 Address += PcdGet64(PcdIoBlockBaseAddressForIpf);
47
48 return MmioRead8 (Address);
49 }
50
51 /**
52 Reads a 16-bit I/O port.
53
54 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
55 This function must guarantee that all I/O read and write operations are
56 serialized.
57
58 @param Port The I/O port to read.
59
60 @return The value read.
61
62 **/
63 UINT16
64 EFIAPI
65 IoRead16 (
66 IN UINT64 Port
67 )
68 {
69 UINT64 Address;
70
71 //
72 // Add the 64MB aligned IO Port space to the IO address
73 //
74 Address = MAP_PORT_BASE_TO_MEM (Port);
75 Address += PcdGet64(PcdIoBlockBaseAddressForIpf);
76
77 return MmioRead16 (Address);
78 }
79
80 /**
81 Reads a 32-bit I/O port.
82
83 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
84 This function must guarantee that all I/O read and write operations are
85 serialized.
86
87 @param Port The I/O port to read.
88
89 @return The value read.
90
91 **/
92 UINT32
93 EFIAPI
94 IoRead32 (
95 IN UINT64 Port
96 )
97 {
98 UINT64 Address;
99
100 //
101 // Add the 64MB aligned IO Port space to the IO address
102 //
103 Address = MAP_PORT_BASE_TO_MEM (Port);
104 Address += PcdGet64(PcdIoBlockBaseAddressForIpf);
105
106 return MmioRead32 (Address);
107 }
108
109 /**
110 Writes a 8-bit I/O port.
111
112 Writes the 8-bit I/O port specified by Port with the value specified by Value
113 and returns Value. This function must guarantee that all I/O read and write
114 operations are serialized.
115
116 @param Port The I/O port to write.
117 @param Value The value to write to the I/O port.
118
119 @return The value written the I/O port.
120
121 **/
122 UINT8
123 EFIAPI
124 IoWrite8 (
125 IN UINT64 Port,
126 IN UINT8 Data
127 )
128 {
129 UINT64 Address;
130
131 //
132 // Add the 64MB aligned IO Port space to the IO address
133 //
134 Address = MAP_PORT_BASE_TO_MEM (Port);
135 Address += PcdGet64(PcdIoBlockBaseAddressForIpf);
136
137 return MmioWrite8 (Address, Data);
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 @param Port The I/O port to write.
148 @param Value The value to write to the I/O port.
149
150 @return The value written the I/O port.
151
152 **/
153 UINT16
154 EFIAPI
155 IoWrite16 (
156 IN UINT64 Port,
157 IN UINT16 Data
158 )
159 {
160 UINT64 Address;
161
162 //
163 // Add the 64MB aligned IO Port space to the IO address
164 //
165 Address = MAP_PORT_BASE_TO_MEM (Port);
166 Address += PcdGet64(PcdIoBlockBaseAddressForIpf);
167
168 return MmioWrite16 (Address, Data);
169 }
170
171 /**
172 Writes a 32-bit I/O port.
173
174 Writes the 32-bit I/O port specified by Port with the value specified by Value
175 and returns Value. This function must guarantee that all I/O read and write
176 operations are serialized.
177
178 @param Port The I/O port to write.
179 @param Value The value to write to the I/O port.
180
181 @return The value written the I/O port.
182
183 **/
184 UINT32
185 EFIAPI
186 IoWrite32 (
187 IN UINT64 Port,
188 IN UINT32 Data
189 )
190 {
191 UINT64 Address;
192
193 //
194 // Add the 64MB aligned IO Port space to the IO address
195 //
196 Address = MAP_PORT_BASE_TO_MEM (Port);
197 Address += PcdGet64(PcdIoBlockBaseAddressForIpf);
198
199 return MmioWrite32 (Address, Data);
200 }
201
202 /**
203 Reads a 8-bit MMIO register.
204
205 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
206 returned. This function must guarantee that all MMIO read and write
207 operations are serialized.
208
209 @param Address The MMIO register to read.
210
211 @return The value read.
212
213 **/
214 UINT8
215 EFIAPI
216 MmioRead8 (
217 IN UINT64 Address
218 )
219 {
220 UINT8 Data;
221
222 Address |= BIT63;
223
224 MemoryFence ();
225 Data = *((volatile UINT8 *) Address);
226 MemoryFence ();
227
228 return Data;
229 }
230
231 /**
232 Reads a 16-bit MMIO register.
233
234 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
235 returned. This function must guarantee that all MMIO read and write
236 operations are serialized.
237
238 @param Address The MMIO register to read.
239
240 @return The value read.
241
242 **/
243 UINT16
244 EFIAPI
245 MmioRead16 (
246 IN UINT64 Address
247 )
248 {
249 UINT16 Data;
250
251 Address |= BIT63;
252
253 MemoryFence ();
254 Data = *((volatile UINT16 *) Address);
255 MemoryFence ();
256
257 return Data;
258 }
259
260 /**
261 Reads a 32-bit MMIO register.
262
263 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
264 returned. This function must guarantee that all MMIO read and write
265 operations are serialized.
266
267 @param Address The MMIO register to read.
268
269 @return The value read.
270
271 **/
272 UINT32
273 EFIAPI
274 MmioRead32 (
275 IN UINT64 Address
276 )
277 {
278 UINT32 Data;
279
280 Address |= BIT63;
281
282 MemoryFence ();
283 Data = *((volatile UINT32 *) Address);
284 MemoryFence ();
285
286 return Data;
287 }
288
289 /**
290 Reads a 64-bit MMIO register.
291
292 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
293 returned. This function must guarantee that all MMIO read and write
294 operations are serialized.
295
296 @param Address The MMIO register to read.
297
298 @return The value read.
299
300 **/
301 UINT64
302 EFIAPI
303 MmioRead64 (
304 IN UINT64 Address
305 )
306 {
307 UINT64 Data;
308
309 Address |= BIT63;
310
311 MemoryFence ();
312 Data = *((volatile UINT64 *) Address);
313 MemoryFence ();
314
315 return Data;
316
317 }
318
319 /**
320 Writes a 8-bit MMIO register.
321
322 Writes the 8-bit MMIO register specified by Address with the value specified
323 by Value and returns Value. This function must guarantee that all MMIO read
324 and write operations are serialized.
325
326 @param Address The MMIO register to write.
327 @param Data The value to write to the MMIO register.
328
329 @return The value written the memory address.
330
331 **/
332 UINT8
333 EFIAPI
334 MmioWrite8 (
335 IN UINT64 Address,
336 IN UINT8 Data
337 )
338 {
339 Address |= BIT63;
340
341 MemoryFence ();
342 *((volatile UINT8 *) Address) = Data;
343 MemoryFence ();
344
345 return Data;
346 }
347
348 /**
349 Writes a 16-bit MMIO register.
350
351 Writes the 16-bit MMIO register specified by Address with the value specified
352 by Value and returns Value. This function must guarantee that all MMIO read
353 and write operations are serialized.
354
355 @param Address The MMIO register to write.
356 @param Data The value to write to the MMIO register.
357
358 @return The value written the memory address.
359
360 **/
361 UINT16
362 EFIAPI
363 MmioWrite16 (
364 IN UINT64 Address,
365 IN UINT16 Data
366 )
367 {
368 Address |= BIT63;
369
370 MemoryFence ();
371 *((volatile UINT16 *) Address) = Data;
372 MemoryFence ();
373
374 return Data;
375 }
376
377 /**
378 Writes a 32-bit MMIO register.
379
380 Writes the 32-bit MMIO register specified by Address with the value specified
381 by Value and returns Value. This function must guarantee that all MMIO read
382 and write operations are serialized.
383
384 @param Address The MMIO register to write.
385 @param Data The value to write to the MMIO register.
386
387 @return The value written the memory address.
388
389 **/
390 UINT32
391 EFIAPI
392 MmioWrite32 (
393 IN UINT64 Address,
394 IN UINT32 Data
395 )
396 {
397 Address |= BIT63;
398
399 MemoryFence ();
400 *((volatile UINT32 *) Address) = Data;
401 MemoryFence ();
402
403 return Data;
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 @param Address The MMIO register to write.
414 @param Data The value to write to the MMIO register.
415
416 @return The value written the memory address.
417
418 **/
419 UINT64
420 EFIAPI
421 MmioWrite64 (
422 IN UINT64 Address,
423 IN UINT64 Data
424 )
425 {
426 Address |= BIT63;
427
428 MemoryFence ();
429 *((volatile UINT64 *) Address) = Data;
430 MemoryFence ();
431
432 return Data;
433 }