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