]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseIoLibIntrinsic/IoLibIpf.c
Add alignment checking for IoLib functions to conform to MdeLib spec.
[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.
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.
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.
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.
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 the I/O port.
153
154 **/
155 UINT8
156 EFIAPI
157 IoWrite8 (
158 IN UINT64 Port,
159 IN UINT8 Data
160 )
161 {
162 return MmioWrite8 (InternalGetMemoryMapAddress (Port), Data);
163 }
164
165 /**
166 Writes a 16-bit I/O port.
167
168 Writes the 16-bit I/O port specified by Port with the value specified by Value
169 and returns Value. This function must guarantee that all I/O read and write
170 operations are serialized.
171
172 @param Port The I/O port to write.
173 @param Value The value to write to the I/O port.
174
175 @return The value written the I/O port.
176
177 **/
178 UINT16
179 EFIAPI
180 IoWrite16 (
181 IN UINT64 Port,
182 IN UINT16 Data
183 )
184 {
185 return MmioWrite16 (InternalGetMemoryMapAddress (Port), Data);
186 }
187
188 /**
189 Writes a 32-bit I/O port.
190
191 Writes the 32-bit I/O port specified by Port with the value specified by Value
192 and returns Value. This function must guarantee that all I/O read and write
193 operations are serialized.
194
195 @param Port The I/O port to write.
196 @param Value The value to write to the I/O port.
197
198 @return The value written the I/O port.
199
200 **/
201 UINT32
202 EFIAPI
203 IoWrite32 (
204 IN UINT64 Port,
205 IN UINT32 Data
206 )
207 {
208 return MmioWrite32 (InternalGetMemoryMapAddress (Port), Data);
209 }
210
211 /**
212 Writes a 64-bit I/O port.
213
214 Writes the 64-bit I/O port specified by Port with the value specified by Value
215 and returns Value. This function must guarantee that all I/O read and write
216 operations are serialized.
217
218 If 64-bit I/O port operations are not supported, then ASSERT().
219
220 @param Port The I/O port to write.
221 @param Value The value to write to the I/O port.
222
223 @return The value written the I/O port.
224
225 **/
226 UINT64
227 EFIAPI
228 IoWrite64 (
229 IN UINTN Port,
230 IN UINT64 Value
231 )
232 {
233 ASSERT (FALSE);
234 return 0;
235 }
236
237 /**
238 Reads a 8-bit MMIO register.
239
240 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
241 returned. This function must guarantee that all MMIO read and write
242 operations are serialized.
243
244 @param Address The MMIO register to read.
245
246 @return The value read.
247
248 **/
249 UINT8
250 EFIAPI
251 MmioRead8 (
252 IN UINT64 Address
253 )
254 {
255 UINT8 Data;
256
257 Address |= BIT63;
258
259 MemoryFence ();
260 Data = *((volatile UINT8 *) Address);
261 MemoryFence ();
262
263 return Data;
264 }
265
266 /**
267 Reads a 16-bit MMIO register.
268
269 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
270 returned. This function must guarantee that all MMIO read and write
271 operations are serialized.
272
273 @param Address The MMIO register to read.
274
275 @return The value read.
276
277 **/
278 UINT16
279 EFIAPI
280 MmioRead16 (
281 IN UINT64 Address
282 )
283 {
284 UINT16 Data;
285
286 //
287 // Make sure that Address is 16-bit aligned.
288 //
289 ASSERT ((Address & 1) == 0);
290
291 Address |= BIT63;
292
293 MemoryFence ();
294 Data = *((volatile UINT16 *) Address);
295 MemoryFence ();
296
297 return Data;
298 }
299
300 /**
301 Reads a 32-bit MMIO register.
302
303 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
304 returned. This function must guarantee that all MMIO read and write
305 operations are serialized.
306
307 @param Address The MMIO register to read.
308
309 @return The value read.
310
311 **/
312 UINT32
313 EFIAPI
314 MmioRead32 (
315 IN UINT64 Address
316 )
317 {
318 UINT32 Data;
319
320 //
321 // Make sure that Address is 32-bit aligned.
322 //
323 ASSERT ((Address & 3) == 0);
324
325 Address |= BIT63;
326
327 MemoryFence ();
328 Data = *((volatile UINT32 *) Address);
329 MemoryFence ();
330
331 return Data;
332 }
333
334 /**
335 Reads a 64-bit MMIO register.
336
337 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
338 returned. This function must guarantee that all MMIO read and write
339 operations are serialized.
340
341 @param Address The MMIO register to read.
342
343 @return The value read.
344
345 **/
346 UINT64
347 EFIAPI
348 MmioRead64 (
349 IN UINT64 Address
350 )
351 {
352 UINT64 Data;
353
354 //
355 // Make sure that Address is 64-bit aligned.
356 //
357 ASSERT ((Address & 7) == 0);
358
359 Address |= BIT63;
360
361 MemoryFence ();
362 Data = *((volatile UINT64 *) Address);
363 MemoryFence ();
364
365 return Data;
366
367 }
368
369 /**
370 Writes a 8-bit MMIO register.
371
372 Writes the 8-bit MMIO register specified by Address with the value specified
373 by Value and returns Value. This function must guarantee that all MMIO read
374 and write operations are serialized.
375
376 @param Address The MMIO register to write.
377 @param Data The value to write to the MMIO register.
378
379 @return The value written the memory address.
380
381 **/
382 UINT8
383 EFIAPI
384 MmioWrite8 (
385 IN UINT64 Address,
386 IN UINT8 Data
387 )
388 {
389 Address |= BIT63;
390
391 MemoryFence ();
392 *((volatile UINT8 *) Address) = Data;
393 MemoryFence ();
394
395 return Data;
396 }
397
398 /**
399 Writes a 16-bit MMIO register.
400
401 Writes the 16-bit MMIO register specified by Address with the value specified
402 by Value and returns Value. This function must guarantee that all MMIO read
403 and write operations are serialized.
404
405 @param Address The MMIO register to write.
406 @param Data The value to write to the MMIO register.
407
408 @return The value written the memory address.
409
410 **/
411 UINT16
412 EFIAPI
413 MmioWrite16 (
414 IN UINT64 Address,
415 IN UINT16 Data
416 )
417 {
418 //
419 // Make sure that Address is 16-bit aligned.
420 //
421 ASSERT ((Address & 1) == 0);
422
423 Address |= BIT63;
424
425 MemoryFence ();
426 *((volatile UINT16 *) Address) = Data;
427 MemoryFence ();
428
429 return Data;
430 }
431
432 /**
433 Writes a 32-bit MMIO register.
434
435 Writes the 32-bit MMIO register specified by Address with the value specified
436 by Value and returns Value. This function must guarantee that all MMIO read
437 and write operations are serialized.
438
439 @param Address The MMIO register to write.
440 @param Data The value to write to the MMIO register.
441
442 @return The value written the memory address.
443
444 **/
445 UINT32
446 EFIAPI
447 MmioWrite32 (
448 IN UINT64 Address,
449 IN UINT32 Data
450 )
451 {
452 //
453 // Make sure that Address is 32-bit aligned.
454 //
455 ASSERT ((Address & 3) == 0);
456
457 Address |= BIT63;
458
459 MemoryFence ();
460 *((volatile UINT32 *) Address) = Data;
461 MemoryFence ();
462
463 return Data;
464 }
465
466 /**
467 Writes a 64-bit MMIO register.
468
469 Writes the 64-bit MMIO register specified by Address with the value specified
470 by Value and returns Value. This function must guarantee that all MMIO read
471 and write operations are serialized.
472
473 @param Address The MMIO register to write.
474 @param Data The value to write to the MMIO register.
475
476 @return The value written the memory address.
477
478 **/
479 UINT64
480 EFIAPI
481 MmioWrite64 (
482 IN UINT64 Address,
483 IN UINT64 Data
484 )
485 {
486 //
487 // Make sure that Address is 64-bit aligned.
488 //
489 ASSERT ((Address & 7) == 0);
490
491 Address |= BIT63;
492
493 MemoryFence ();
494 *((volatile UINT64 *) Address) = Data;
495 MemoryFence ();
496
497 return Data;
498 }