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