]> git.proxmox.com Git - mirror_edk2.git/blob - OldMdePkg/Library/DxeIoLibCpuIo/IoLib.c
64ba3480efa57ddcf1a9561f0f1365a84eb6d4ff
[mirror_edk2.git] / OldMdePkg / Library / DxeIoLibCpuIo / IoLib.c
1 /** @file
2 I/O Library.
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: IoLib.c
14
15 **/
16
17 #include "DxeCpuIoLibInternal.h"
18
19 //
20 // Globle varible to cache pointer to CpuIo protocol.
21 //
22 STATIC EFI_CPU_IO_PROTOCOL *mCpuIo = NULL;
23
24 /**
25 The constructor function caches the pointer to CpuIo protocol.
26
27 The constructor function locates CpuIo protocol from protocol database.
28 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
29
30 @param ImageHandle The firmware allocated handle for the EFI image.
31 @param SystemTable A pointer to the EFI System Table.
32
33 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
34
35 **/
36 EFI_STATUS
37 EFIAPI
38 IoLibConstructor (
39 IN EFI_HANDLE ImageHandle,
40 IN EFI_SYSTEM_TABLE *SystemTable
41 )
42 {
43 EFI_STATUS Status;
44
45 Status = gBS->LocateProtocol (&gEfiCpuIoProtocolGuid, NULL, (VOID**) &mCpuIo);
46 ASSERT_EFI_ERROR (Status);
47
48 return Status;
49 }
50
51 /**
52 Reads registers in the EFI CPU I/O space.
53
54 Reads the I/O port specified by Port with registers width specified by Width.
55 The read value is returned. If such operations are not supported, then ASSERT().
56 This function must guarantee that all I/O read and write operations are serialized.
57
58 @param Port The base address of the I/O operation.
59 The caller is responsible for aligning the Address if required.
60 @param Width The width of the I/O operation.
61
62 @return Data read from registers in the EFI CPU I/O space.
63
64 **/
65 UINT64
66 EFIAPI
67 IoReadWorker (
68 IN UINTN Port,
69 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
70 )
71 {
72 EFI_STATUS Status;
73 UINT64 Data;
74
75 Status = mCpuIo->Io.Read (mCpuIo, Width, Port, 1, &Data);
76 ASSERT_EFI_ERROR (Status);
77
78 return Data;
79 }
80
81 /**
82 Writes registers in the EFI CPU I/O space.
83
84 Writes the I/O port specified by Port with registers width and value specified by Width
85 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
86 This function must guarantee that all I/O read and write operations are serialized.
87
88 @param Port The base address of the I/O operation.
89 The caller is responsible for aligning the Address if required.
90 @param Width The width of the I/O operation.
91 @param Data The value to write to the I/O port.
92
93 @return The paramter of Data.
94
95 **/
96 UINT64
97 EFIAPI
98 IoWriteWorker (
99 IN UINTN Port,
100 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
101 IN UINT64 Data
102 )
103 {
104 EFI_STATUS Status;
105
106 Status = mCpuIo->Io.Write (mCpuIo, Width, Port, 1, &Data);
107 ASSERT_EFI_ERROR (Status);
108
109 return Data;
110 }
111
112 /**
113 Reads memory-mapped registers in the EFI system memory space.
114
115 Reads the MMIO registers specified by Address with registers width specified by Width.
116 The read value is returned. If such operations are not supported, then ASSERT().
117 This function must guarantee that all MMIO read and write operations are serialized.
118
119 @param Address The MMIO register to read.
120 The caller is responsible for aligning the Address if required.
121 @param Width The width of the I/O operation.
122
123 @return Data read from registers in the EFI system memory space.
124
125 **/
126 UINT64
127 EFIAPI
128 MmioReadWorker (
129 IN UINTN Address,
130 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
131 )
132 {
133 EFI_STATUS Status;
134 UINT64 Data;
135
136 Status = mCpuIo->Mem.Read (mCpuIo, Width, Address, 1, &Data);
137 ASSERT_EFI_ERROR (Status);
138
139 return Data;
140 }
141
142 /**
143 Writes memory-mapped registers in the EFI system memory space.
144
145 Writes the MMIO registers specified by Address with registers width and value specified by Width
146 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
147 This function must guarantee that all MMIO read and write operations are serialized.
148
149 @param Address The MMIO register to read.
150 The caller is responsible for aligning the Address if required.
151 @param Width The width of the I/O operation.
152
153 @return Data read from registers in the EFI system memory space.
154
155 **/
156 UINT64
157 EFIAPI
158 MmioWriteWorker (
159 IN UINTN Address,
160 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
161 IN UINT64 Data
162 )
163 {
164 EFI_STATUS Status;
165
166 Status = mCpuIo->Mem.Write (mCpuIo, Width, Address, 1, &Data);
167 ASSERT_EFI_ERROR (Status);
168
169 return Data;
170 }
171
172 /**
173 Reads an 8-bit I/O port.
174
175 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
176 This function must guarantee that all I/O read and write operations are
177 serialized.
178
179 If 8-bit I/O port operations are not supported, then ASSERT().
180
181 @param Port The I/O port to read.
182
183 @return The value read.
184
185 **/
186 UINT8
187 EFIAPI
188 IoRead8 (
189 IN UINTN Port
190 )
191 {
192 return (UINT8)IoReadWorker (Port, EfiCpuIoWidthUint8);
193 }
194
195 /**
196 Writes an 8-bit I/O port.
197
198 Writes the 8-bit I/O port specified by Port with the value specified by Value
199 and returns Value. This function must guarantee that all I/O read and write
200 operations are serialized.
201
202 If 8-bit I/O port operations are not supported, then ASSERT().
203
204 @param Port The I/O port to write.
205 @param Value The value to write to the I/O port.
206
207 @return The value written the I/O port.
208
209 **/
210 UINT8
211 EFIAPI
212 IoWrite8 (
213 IN UINTN Port,
214 IN UINT8 Value
215 )
216 {
217 return (UINT8)IoWriteWorker (Port, EfiCpuIoWidthUint8, Value);
218 }
219
220 /**
221 Reads a 16-bit I/O port.
222
223 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
224 This function must guarantee that all I/O read and write operations are
225 serialized.
226
227 If 16-bit I/O port operations are not supported, then ASSERT().
228
229 @param Port The I/O port to read.
230
231 @return The value read.
232
233 **/
234 UINT16
235 EFIAPI
236 IoRead16 (
237 IN UINTN Port
238 )
239 {
240 //
241 // Make sure Port is aligned on a 16-bit boundary.
242 //
243 ASSERT ((Port & 1) == 0);
244 return (UINT16)IoReadWorker (Port, EfiCpuIoWidthUint16);
245 }
246
247 /**
248 Writes a 16-bit I/O port.
249
250 Writes the 16-bit I/O port specified by Port with the value specified by Value
251 and returns Value. This function must guarantee that all I/O read and write
252 operations are serialized.
253
254 If 16-bit I/O port operations are not supported, then ASSERT().
255
256 @param Port The I/O port to write.
257 @param Value The value to write to the I/O port.
258
259 @return The value written the I/O port.
260
261 **/
262 UINT16
263 EFIAPI
264 IoWrite16 (
265 IN UINTN Port,
266 IN UINT16 Value
267 )
268 {
269 //
270 // Make sure Port is aligned on a 16-bit boundary.
271 //
272 ASSERT ((Port & 1) == 0);
273 return (UINT16)IoWriteWorker (Port, EfiCpuIoWidthUint16, Value);
274 }
275
276 /**
277 Reads a 32-bit I/O port.
278
279 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
280 This function must guarantee that all I/O read and write operations are
281 serialized.
282
283 If 32-bit I/O port operations are not supported, then ASSERT().
284
285 @param Port The I/O port to read.
286
287 @return The value read.
288
289 **/
290 UINT32
291 EFIAPI
292 IoRead32 (
293 IN UINTN Port
294 )
295 {
296 //
297 // Make sure Port is aligned on a 32-bit boundary.
298 //
299 ASSERT ((Port & 3) == 0);
300 return (UINT32)IoReadWorker (Port, EfiCpuIoWidthUint32);
301 }
302
303 /**
304 Writes a 32-bit I/O port.
305
306 Writes the 32-bit I/O port specified by Port with the value specified by Value
307 and returns Value. This function must guarantee that all I/O read and write
308 operations are serialized.
309
310 If 32-bit I/O port operations are not supported, then ASSERT().
311
312 @param Port The I/O port to write.
313 @param Value The value to write to the I/O port.
314
315 @return The value written the I/O port.
316
317 **/
318 UINT32
319 EFIAPI
320 IoWrite32 (
321 IN UINTN Port,
322 IN UINT32 Value
323 )
324 {
325 //
326 // Make sure Port is aligned on a 32-bit boundary.
327 //
328 ASSERT ((Port & 3) == 0);
329 return (UINT32)IoWriteWorker (Port, EfiCpuIoWidthUint32, Value);
330 }
331
332 /**
333 Reads a 64-bit I/O port.
334
335 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
336 This function must guarantee that all I/O read and write operations are
337 serialized.
338
339 If 64-bit I/O port operations are not supported, then ASSERT().
340
341 @param Port The I/O port to read.
342
343 @return The value read.
344
345 **/
346 UINT64
347 EFIAPI
348 IoRead64 (
349 IN UINTN Port
350 )
351 {
352 //
353 // Make sure Port is aligned on a 64-bit boundary.
354 //
355 ASSERT ((Port & 7) == 0);
356 return IoReadWorker (Port, EfiCpuIoWidthUint64);
357 }
358
359 /**
360 Writes a 64-bit I/O port.
361
362 Writes the 64-bit I/O port specified by Port with the value specified by Value
363 and returns Value. This function must guarantee that all I/O read and write
364 operations are serialized.
365
366 If 64-bit I/O port operations are not supported, then ASSERT().
367
368 @param Port The I/O port to write.
369 @param Value The value to write to the I/O port.
370
371 @return The value written the I/O port.
372
373 **/
374 UINT64
375 EFIAPI
376 IoWrite64 (
377 IN UINTN Port,
378 IN UINT64 Value
379 )
380 {
381 //
382 // Make sure Port is aligned on a 64-bit boundary.
383 //
384 ASSERT ((Port & 7) == 0);
385 return IoWriteWorker (Port, EfiCpuIoWidthUint64, Value);
386 }
387
388 /**
389 Reads an 8-bit MMIO register.
390
391 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
392 returned. This function must guarantee that all MMIO read and write
393 operations are serialized.
394
395 If 8-bit MMIO register operations are not supported, then ASSERT().
396
397 @param Address The MMIO register to read.
398
399 @return The value read.
400
401 **/
402 UINT8
403 EFIAPI
404 MmioRead8 (
405 IN UINTN Address
406 )
407 {
408 return (UINT8)MmioReadWorker (Address, EfiCpuIoWidthUint8);
409 }
410
411 /**
412 Writes an 8-bit MMIO register.
413
414 Writes the 8-bit MMIO register specified by Address with the value specified
415 by Value and returns Value. This function must guarantee that all MMIO read
416 and write operations are serialized.
417
418 If 8-bit MMIO register operations are not supported, then ASSERT().
419
420 @param Address The MMIO register to write.
421 @param Value The value to write to the MMIO register.
422
423 **/
424 UINT8
425 EFIAPI
426 MmioWrite8 (
427 IN UINTN Address,
428 IN UINT8 Value
429 )
430 {
431 return (UINT8)MmioWriteWorker (Address, EfiCpuIoWidthUint8, Value);
432 }
433
434 /**
435 Reads a 16-bit MMIO register.
436
437 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
438 returned. This function must guarantee that all MMIO read and write
439 operations are serialized.
440
441 If 16-bit MMIO register operations are not supported, then ASSERT().
442
443 @param Address The MMIO register to read.
444
445 @return The value read.
446
447 **/
448 UINT16
449 EFIAPI
450 MmioRead16 (
451 IN UINTN Address
452 )
453 {
454 //
455 // Make sure Address is aligned on a 16-bit boundary.
456 //
457 ASSERT ((Address & 1) == 0);
458 return (UINT16)MmioReadWorker (Address, EfiCpuIoWidthUint16);
459 }
460
461 /**
462 Writes a 16-bit MMIO register.
463
464 Writes the 16-bit MMIO register specified by Address with the value specified
465 by Value and returns Value. This function must guarantee that all MMIO read
466 and write operations are serialized.
467
468 If 16-bit MMIO register operations are not supported, then ASSERT().
469
470 @param Address The MMIO register to write.
471 @param Value The value to write to the MMIO register.
472
473 **/
474 UINT16
475 EFIAPI
476 MmioWrite16 (
477 IN UINTN Address,
478 IN UINT16 Value
479 )
480 {
481 //
482 // Make sure Address is aligned on a 16-bit boundary.
483 //
484 ASSERT ((Address & 1) == 0);
485 return (UINT16)MmioWriteWorker (Address, EfiCpuIoWidthUint16, Value);
486 }
487
488 /**
489 Reads a 32-bit MMIO register.
490
491 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
492 returned. This function must guarantee that all MMIO read and write
493 operations are serialized.
494
495 If 32-bit MMIO register operations are not supported, then ASSERT().
496
497 @param Address The MMIO register to read.
498
499 @return The value read.
500
501 **/
502 UINT32
503 EFIAPI
504 MmioRead32 (
505 IN UINTN Address
506 )
507 {
508 //
509 // Make sure Address is aligned on a 32-bit boundary.
510 //
511 ASSERT ((Address & 3) == 0);
512 return (UINT32)MmioReadWorker (Address, EfiCpuIoWidthUint32);
513 }
514
515 /**
516 Writes a 32-bit MMIO register.
517
518 Writes the 32-bit MMIO register specified by Address with the value specified
519 by Value and returns Value. This function must guarantee that all MMIO read
520 and write operations are serialized.
521
522 If 32-bit MMIO register operations are not supported, then ASSERT().
523
524 @param Address The MMIO register to write.
525 @param Value The value to write to the MMIO register.
526
527 **/
528 UINT32
529 EFIAPI
530 MmioWrite32 (
531 IN UINTN Address,
532 IN UINT32 Value
533 )
534 {
535 //
536 // Make sure Address is aligned on a 32-bit boundary.
537 //
538 ASSERT ((Address & 3) == 0);
539 return (UINT32)MmioWriteWorker (Address, EfiCpuIoWidthUint32, Value);
540 }
541
542 /**
543 Reads a 64-bit MMIO register.
544
545 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
546 returned. This function must guarantee that all MMIO read and write
547 operations are serialized.
548
549 If 64-bit MMIO register operations are not supported, then ASSERT().
550
551 @param Address The MMIO register to read.
552
553 @return The value read.
554
555 **/
556 UINT64
557 EFIAPI
558 MmioRead64 (
559 IN UINTN Address
560 )
561 {
562 //
563 // Make sure Address is aligned on a 64-bit boundary.
564 //
565 ASSERT ((Address & 7) == 0);
566 return (UINT64)MmioReadWorker (Address, EfiCpuIoWidthUint64);
567 }
568
569 /**
570 Writes a 64-bit MMIO register.
571
572 Writes the 64-bit MMIO register specified by Address with the value specified
573 by Value and returns Value. This function must guarantee that all MMIO read
574 and write operations are serialized.
575
576 If 64-bit MMIO register operations are not supported, then ASSERT().
577
578 @param Address The MMIO register to write.
579 @param Value The value to write to the MMIO register.
580
581 **/
582 UINT64
583 EFIAPI
584 MmioWrite64 (
585 IN UINTN Address,
586 IN UINT64 Value
587 )
588 {
589 //
590 // Make sure Address is aligned on a 64-bit boundary.
591 //
592 ASSERT ((Address & 7) == 0);
593 return (UINT64)MmioWriteWorker (Address, EfiCpuIoWidthUint64, Value);
594 }