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