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