]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibIpf.c
Code scrub:
[mirror_edk2.git] / MdePkg / Library / BaseIoLibIntrinsic / IoLibIpf.c
1 /** @file
2 Common I/O Library routines.
3
4 Copyright (c) 2006 - 2007, 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 **/
14
15 //
16 // Include common header file for this module.
17 //
18 #include "BaseIoLibIntrinsicInternal.h"
19
20 #define MAP_PORT_BASE_TO_MEM(_Port) \
21 ((((_Port) & 0xfffc) << 10) | ((_Port) & 0x0fff))
22
23 /**
24 Translates I/O port address to memory address.
25
26 This function translates I/O port address to memory address by adding the 64MB
27 aligned I/O Port space to the I/O address.
28 If I/O Port space base is not 64MB aligned, then ASSERT ().
29
30 @param Port The I/O port to read.
31
32 @return The memory address.
33
34 **/
35 UINTN
36 InternalGetMemoryMapAddress (
37 IN UINTN Port
38 )
39 {
40 UINTN Address;
41 UINTN IoBlockBaseAddress;
42
43 Address = MAP_PORT_BASE_TO_MEM (Port);
44 IoBlockBaseAddress = PcdGet64(PcdIoBlockBaseAddressForIpf);
45
46 //
47 // Make sure that the I/O Port space base is 64MB aligned.
48 //
49 ASSERT ((IoBlockBaseAddress & 0x3ffffff) == 0);
50 Address += IoBlockBaseAddress;
51
52 return Address;
53 }
54
55 /**
56 Reads a 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 @param Port The I/O port to read.
63
64 @return The value read from Port.
65
66 **/
67 UINT8
68 EFIAPI
69 IoRead8 (
70 IN UINT64 Port
71 )
72 {
73 return MmioRead8 (InternalGetMemoryMapAddress (Port));
74 }
75
76 /**
77 Reads a 16-bit I/O port.
78
79 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
80 This function must guarantee that all I/O read and write operations are
81 serialized.
82
83 @param Port The I/O port to read.
84
85 @return The value read from Port.
86
87 **/
88 UINT16
89 EFIAPI
90 IoRead16 (
91 IN UINT64 Port
92 )
93 {
94 return MmioRead16 (InternalGetMemoryMapAddress (Port));
95 }
96
97 /**
98 Reads a 32-bit I/O port.
99
100 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
101 This function must guarantee that all I/O read and write operations are
102 serialized.
103
104 @param Port The I/O port to read.
105
106 @return The value read from Port.
107
108 **/
109 UINT32
110 EFIAPI
111 IoRead32 (
112 IN UINT64 Port
113 )
114 {
115 return MmioRead32 (InternalGetMemoryMapAddress (Port));
116 }
117
118 /**
119 Reads a 64-bit I/O port.
120
121 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
122 This function must guarantee that all I/O read and write operations are
123 serialized.
124
125 If 64-bit I/O port operations are not supported, then ASSERT().
126
127 @param Port The I/O port to read.
128
129 @return The value read from Port.
130
131 **/
132 UINT64
133 EFIAPI
134 IoRead64 (
135 IN UINTN Port
136 )
137 {
138 ASSERT (FALSE);
139 return 0;
140 }
141
142 /**
143 Writes a 8-bit I/O port.
144
145 Writes the 8-bit I/O port specified by Port with the value specified by Value
146 and returns Value. This function must guarantee that all I/O read and write
147 operations are serialized.
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 to the I/O port. It equals to the
153 input Value instead of the actual value read back from
154 the I/O port.
155
156 **/
157 UINT8
158 EFIAPI
159 IoWrite8 (
160 IN UINT64 Port,
161 IN UINT8 Data
162 )
163 {
164 return MmioWrite8 (InternalGetMemoryMapAddress (Port), Data);
165 }
166
167 /**
168 Writes a 16-bit I/O port.
169
170 Writes the 16-bit I/O port specified by Port with the value specified by Value
171 and returns Value. This function must guarantee that all I/O read and write
172 operations are serialized.
173
174 @param Port The I/O port to write.
175 @param Value The value to write to the I/O port.
176
177 @return The value written to the I/O port. It equals to the
178 input Value instead of the actual value read back from
179 the I/O port.
180
181 **/
182 UINT16
183 EFIAPI
184 IoWrite16 (
185 IN UINT64 Port,
186 IN UINT16 Data
187 )
188 {
189 return MmioWrite16 (InternalGetMemoryMapAddress (Port), Data);
190 }
191
192 /**
193 Writes a 32-bit I/O port.
194
195 Writes the 32-bit I/O port specified by Port with the value specified by Value
196 and returns Value. This function must guarantee that all I/O read and write
197 operations are serialized.
198
199 @param Port The I/O port to write.
200 @param Value The value to write to the I/O port.
201
202 @return The value written to the I/O port. It equals to the
203 input Value instead of the actual value read back from
204 the I/O port.
205
206 **/
207 UINT32
208 EFIAPI
209 IoWrite32 (
210 IN UINT64 Port,
211 IN UINT32 Data
212 )
213 {
214 return MmioWrite32 (InternalGetMemoryMapAddress (Port), Data);
215 }
216
217 /**
218 Writes a 64-bit I/O port.
219
220 Writes the 64-bit I/O port specified by Port with the value specified by Value
221 and returns Value. This function must guarantee that all I/O read and write
222 operations are serialized.
223
224 If 64-bit I/O port operations are not supported, then ASSERT().
225
226 @param Port The I/O port to write.
227 @param Value The value to write to the I/O port.
228
229 @return The value written to the I/O port. It equals to the
230 input Value instead of the actual value read back from
231 the I/O port.
232
233 **/
234 UINT64
235 EFIAPI
236 IoWrite64 (
237 IN UINTN Port,
238 IN UINT64 Value
239 )
240 {
241 ASSERT (FALSE);
242 return 0;
243 }
244
245 /**
246 Reads a 8-bit MMIO register.
247
248 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
249 returned. This function must guarantee that all MMIO read and write
250 operations are serialized.
251
252 @param Address The MMIO register to read.
253
254 @return The value read from Address.
255
256 **/
257 UINT8
258 EFIAPI
259 MmioRead8 (
260 IN UINT64 Address
261 )
262 {
263 UINT8 Data;
264
265 Address |= BIT63;
266
267 MemoryFence ();
268 Data = *((volatile UINT8 *) Address);
269 MemoryFence ();
270
271 return Data;
272 }
273
274 /**
275 Reads a 16-bit MMIO register.
276
277 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
278 returned. This function must guarantee that all MMIO read and write
279 operations are serialized.
280
281 @param Address The MMIO register to read.
282
283 @return The value read from Address.
284
285 **/
286 UINT16
287 EFIAPI
288 MmioRead16 (
289 IN UINT64 Address
290 )
291 {
292 UINT16 Data;
293
294 //
295 // Make sure that Address is 16-bit aligned.
296 //
297 ASSERT ((Address & 1) == 0);
298
299 Address |= BIT63;
300
301 MemoryFence ();
302 Data = *((volatile UINT16 *) Address);
303 MemoryFence ();
304
305 return Data;
306 }
307
308 /**
309 Reads a 32-bit MMIO register.
310
311 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
312 returned. This function must guarantee that all MMIO read and write
313 operations are serialized.
314
315 @param Address The MMIO register to read.
316
317 @return The value read from Address.
318
319 **/
320 UINT32
321 EFIAPI
322 MmioRead32 (
323 IN UINT64 Address
324 )
325 {
326 UINT32 Data;
327
328 //
329 // Make sure that Address is 32-bit aligned.
330 //
331 ASSERT ((Address & 3) == 0);
332
333 Address |= BIT63;
334
335 MemoryFence ();
336 Data = *((volatile UINT32 *) Address);
337 MemoryFence ();
338
339 return Data;
340 }
341
342 /**
343 Reads a 64-bit MMIO register.
344
345 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
346 returned. This function must guarantee that all MMIO read and write
347 operations are serialized.
348
349 @param Address The MMIO register to read.
350
351 @return The value read from Address.
352
353 **/
354 UINT64
355 EFIAPI
356 MmioRead64 (
357 IN UINT64 Address
358 )
359 {
360 UINT64 Data;
361
362 //
363 // Make sure that Address is 64-bit aligned.
364 //
365 ASSERT ((Address & 7) == 0);
366
367 Address |= BIT63;
368
369 MemoryFence ();
370 Data = *((volatile UINT64 *) Address);
371 MemoryFence ();
372
373 return Data;
374
375 }
376
377 /**
378 Writes a 8-bit MMIO register.
379
380 Writes the 8-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 to the Mmio. It equals to the
388 input Value instead of the actual value read back from
389 the Mmio.
390
391 **/
392 UINT8
393 EFIAPI
394 MmioWrite8 (
395 IN UINT64 Address,
396 IN UINT8 Data
397 )
398 {
399 Address |= BIT63;
400
401 MemoryFence ();
402 *((volatile UINT8 *) Address) = Data;
403 MemoryFence ();
404
405 return Data;
406 }
407
408 /**
409 Writes a 16-bit MMIO register.
410
411 Writes the 16-bit MMIO register specified by Address with the value specified
412 by Value and returns Value. This function must guarantee that all MMIO read
413 and write operations are serialized.
414
415 @param Address The MMIO register to write.
416 @param Data The value to write to the MMIO register.
417
418 @return The value written to the Mmio. It equals to the
419 input Value instead of the actual value read back from
420 the Mmio.
421
422 **/
423 UINT16
424 EFIAPI
425 MmioWrite16 (
426 IN UINT64 Address,
427 IN UINT16 Data
428 )
429 {
430 //
431 // Make sure that Address is 16-bit aligned.
432 //
433 ASSERT ((Address & 1) == 0);
434
435 Address |= BIT63;
436
437 MemoryFence ();
438 *((volatile UINT16 *) Address) = Data;
439 MemoryFence ();
440
441 return Data;
442 }
443
444 /**
445 Writes a 32-bit MMIO register.
446
447 Writes the 32-bit MMIO register specified by Address with the value specified
448 by Value and returns Value. This function must guarantee that all MMIO read
449 and write operations are serialized.
450
451 @param Address The MMIO register to write.
452 @param Data The value to write to the MMIO register.
453
454 @return The value written to the Mmio. It equals to the
455 input Value instead of the actual value read back from
456 the Mmio.
457
458 **/
459 UINT32
460 EFIAPI
461 MmioWrite32 (
462 IN UINT64 Address,
463 IN UINT32 Data
464 )
465 {
466 //
467 // Make sure that Address is 32-bit aligned.
468 //
469 ASSERT ((Address & 3) == 0);
470
471 Address |= BIT63;
472
473 MemoryFence ();
474 *((volatile UINT32 *) Address) = Data;
475 MemoryFence ();
476
477 return Data;
478 }
479
480 /**
481 Writes a 64-bit MMIO register.
482
483 Writes the 64-bit MMIO register specified by Address with the value specified
484 by Value and returns Value. This function must guarantee that all MMIO read
485 and write operations are serialized.
486
487 @param Address The MMIO register to write.
488 @param Data The value to write to the MMIO register.
489
490 @return The value written to the Mmio. It equals to the
491 input Value instead of the actual value read back from
492 the Mmio.
493
494 **/
495 UINT64
496 EFIAPI
497 MmioWrite64 (
498 IN UINT64 Address,
499 IN UINT64 Data
500 )
501 {
502 //
503 // Make sure that Address is 64-bit aligned.
504 //
505 ASSERT ((Address & 7) == 0);
506
507 Address |= BIT63;
508
509 MemoryFence ();
510 *((volatile UINT64 *) Address) = Data;
511 MemoryFence ();
512
513 return Data;
514 }