Initial import.
[mirror_edk2.git] / MdePkg / 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 static EFI_CPU_IO_PROTOCOL *gCpuIo;
18
19 EFI_STATUS
20 IoLibConstructor (
21 IN EFI_HANDLE ImageHandle,
22 IN EFI_SYSTEM_TABLE *SystemTable
23 )
24 {
25 EFI_STATUS Status;
26
27 Status = SystemTable->BootServices->LocateProtocol (
28 &gEfiCpuIoProtocolGuid,
29 NULL,
30 (VOID**)&gCpuIo
31 );
32 ASSERT_EFI_ERROR (Status);
33 return Status;
34 }
35
36 UINT64
37 EFIAPI
38 IoReadWorker (
39 IN UINTN Port,
40 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
41 )
42 {
43 UINT64 Data;
44
45 gCpuIo->Io.Read (gCpuIo, Width, Port, 1, &Data);
46 return Data;
47 }
48
49 UINT64
50 EFIAPI
51 IoWriteWorker (
52 IN UINTN Port,
53 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
54 IN UINT64 Data
55 )
56 {
57 gCpuIo->Io.Write (gCpuIo, Width, Port, 1, &Data);
58 return Data;
59 }
60
61 UINT64
62 EFIAPI
63 MmioReadWorker (
64 IN UINTN Address,
65 IN EFI_CPU_IO_PROTOCOL_WIDTH Width
66 )
67 {
68 UINT64 Data;
69
70 gCpuIo->Mem.Read (gCpuIo, Width, Address, 1, &Data);
71 return Data;
72 }
73
74 UINT64
75 EFIAPI
76 MmioWriteWorker (
77 IN UINTN Address,
78 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
79 IN UINT64 Data
80 )
81 {
82 gCpuIo->Mem.Write (gCpuIo, Width, Address, 1, &Data);
83 return Data;
84 }
85
86 /**
87 Reads an 8-bit I/O port.
88
89 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
90 This function must guarantee that all I/O read and write operations are
91 serialized.
92
93 If 8-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.
98
99 **/
100 UINT8
101 EFIAPI
102 IoRead8 (
103 IN UINTN Port
104 )
105 {
106 return (UINT8)IoReadWorker (Port, EfiCpuIoWidthUint8);
107 }
108
109 /**
110 Writes an 8-bit I/O port.
111
112 Writes the 8-bit I/O port specified by Port with the value specified by Value
113 and returns Value. This function must guarantee that all I/O read and write
114 operations are serialized.
115
116 If 8-bit I/O port operations are not supported, then ASSERT().
117
118 @param Port The I/O port to write.
119 @param Value The value to write to the I/O port.
120
121 @return The value written the I/O port.
122
123 **/
124 UINT8
125 EFIAPI
126 IoWrite8 (
127 IN UINTN Port,
128 IN UINT8 Value
129 )
130 {
131 return (UINT8)IoWriteWorker (Port, EfiCpuIoWidthUint8, Value);
132 }
133
134 /**
135 Reads a 16-bit I/O port.
136
137 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
138 This function must guarantee that all I/O read and write operations are
139 serialized.
140
141 If 16-bit I/O port operations are not supported, then ASSERT().
142
143 @param Port The I/O port to read.
144
145 @return The value read.
146
147 **/
148 UINT16
149 EFIAPI
150 IoRead16 (
151 IN UINTN Port
152 )
153 {
154 return (UINT16)IoReadWorker (Port, EfiCpuIoWidthUint16);
155 }
156
157 /**
158 Writes a 16-bit I/O port.
159
160 Writes the 16-bit I/O port specified by Port with the value specified by Value
161 and returns Value. This function must guarantee that all I/O read and write
162 operations are serialized.
163
164 If 16-bit I/O port operations are not supported, then ASSERT().
165
166 @param Port The I/O port to write.
167 @param Value The value to write to the I/O port.
168
169 @return The value written the I/O port.
170
171 **/
172 UINT16
173 EFIAPI
174 IoWrite16 (
175 IN UINTN Port,
176 IN UINT16 Value
177 )
178 {
179 return (UINT16)IoWriteWorker (Port, EfiCpuIoWidthUint16, Value);
180 }
181
182 /**
183 Reads a 32-bit I/O port.
184
185 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
186 This function must guarantee that all I/O read and write operations are
187 serialized.
188
189 If 32-bit I/O port operations are not supported, then ASSERT().
190
191 @param Port The I/O port to read.
192
193 @return The value read.
194
195 **/
196 UINT32
197 EFIAPI
198 IoRead32 (
199 IN UINTN Port
200 )
201 {
202 return (UINT32)IoReadWorker (Port, EfiCpuIoWidthUint32);
203 }
204
205 /**
206 Writes a 32-bit I/O port.
207
208 Writes the 32-bit I/O port specified by Port with the value specified by Value
209 and returns Value. This function must guarantee that all I/O read and write
210 operations are serialized.
211
212 If 32-bit I/O port operations are not supported, then ASSERT().
213
214 @param Port The I/O port to write.
215 @param Value The value to write to the I/O port.
216
217 @return The value written the I/O port.
218
219 **/
220 UINT32
221 EFIAPI
222 IoWrite32 (
223 IN UINTN Port,
224 IN UINT32 Value
225 )
226 {
227 return (UINT32)IoWriteWorker (Port, EfiCpuIoWidthUint32, Value);
228 }
229
230 /**
231 Reads a 64-bit I/O port.
232
233 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
234 This function must guarantee that all I/O read and write operations are
235 serialized.
236
237 If 64-bit I/O port operations are not supported, then ASSERT().
238
239 @param Port The I/O port to read.
240
241 @return The value read.
242
243 **/
244 UINT64
245 EFIAPI
246 IoRead64 (
247 IN UINTN Port
248 )
249 {
250 return IoReadWorker (Port, EfiCpuIoWidthUint64);
251 }
252
253 /**
254 Writes a 64-bit I/O port.
255
256 Writes the 64-bit I/O port specified by Port with the value specified by Value
257 and returns Value. This function must guarantee that all I/O read and write
258 operations are serialized.
259
260 If 64-bit I/O port operations are not supported, then ASSERT().
261
262 @param Port The I/O port to write.
263 @param Value The value to write to the I/O port.
264
265 @return The value written the I/O port.
266
267 **/
268 UINT64
269 EFIAPI
270 IoWrite64 (
271 IN UINTN Port,
272 IN UINT64 Value
273 )
274 {
275 return IoWriteWorker (Port, EfiCpuIoWidthUint64, Value);
276 }
277
278 /**
279 Reads an 8-bit MMIO register.
280
281 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
282 returned. This function must guarantee that all MMIO read and write
283 operations are serialized.
284
285 If 8-bit MMIO register operations are not supported, then ASSERT().
286
287 @param Address The MMIO register to read.
288
289 @return The value read.
290
291 **/
292 UINT8
293 EFIAPI
294 MmioRead8 (
295 IN UINTN Address
296 )
297 {
298 return (UINT8)MmioReadWorker (Address, EfiCpuIoWidthUint8);
299 }
300
301 /**
302 Writes an 8-bit MMIO register.
303
304 Writes the 8-bit MMIO register specified by Address with the value specified
305 by Value and returns Value. This function must guarantee that all MMIO read
306 and write operations are serialized.
307
308 If 8-bit MMIO register operations are not supported, then ASSERT().
309
310 @param Address The MMIO register to write.
311 @param Value The value to write to the MMIO register.
312
313 **/
314 UINT8
315 EFIAPI
316 MmioWrite8 (
317 IN UINTN Address,
318 IN UINT8 Value
319 )
320 {
321 return (UINT8)MmioWriteWorker (Address, EfiCpuIoWidthUint8, Value);
322 }
323
324 /**
325 Reads a 16-bit MMIO register.
326
327 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
328 returned. This function must guarantee that all MMIO read and write
329 operations are serialized.
330
331 If 16-bit MMIO register operations are not supported, then ASSERT().
332
333 @param Address The MMIO register to read.
334
335 @return The value read.
336
337 **/
338 UINT16
339 EFIAPI
340 MmioRead16 (
341 IN UINTN Address
342 )
343 {
344 return (UINT16)MmioReadWorker (Address, EfiCpuIoWidthUint16);
345 }
346
347 /**
348 Writes a 16-bit MMIO register.
349
350 Writes the 16-bit MMIO register specified by Address with the value specified
351 by Value and returns Value. This function must guarantee that all MMIO read
352 and write operations are serialized.
353
354 If 16-bit MMIO register operations are not supported, then ASSERT().
355
356 @param Address The MMIO register to write.
357 @param Value The value to write to the MMIO register.
358
359 **/
360 UINT16
361 EFIAPI
362 MmioWrite16 (
363 IN UINTN Address,
364 IN UINT16 Value
365 )
366 {
367 return (UINT16)MmioWriteWorker (Address, EfiCpuIoWidthUint16, Value);
368 }
369
370 /**
371 Reads a 32-bit MMIO register.
372
373 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
374 returned. This function must guarantee that all MMIO read and write
375 operations are serialized.
376
377 If 32-bit MMIO register operations are not supported, then ASSERT().
378
379 @param Address The MMIO register to read.
380
381 @return The value read.
382
383 **/
384 UINT32
385 EFIAPI
386 MmioRead32 (
387 IN UINTN Address
388 )
389 {
390 return (UINT32)MmioReadWorker (Address, EfiCpuIoWidthUint32);
391 }
392
393 /**
394 Writes a 32-bit MMIO register.
395
396 Writes the 32-bit MMIO register specified by Address with the value specified
397 by Value and returns Value. This function must guarantee that all MMIO read
398 and write operations are serialized.
399
400 If 32-bit MMIO register operations are not supported, then ASSERT().
401
402 @param Address The MMIO register to write.
403 @param Value The value to write to the MMIO register.
404
405 **/
406 UINT32
407 EFIAPI
408 MmioWrite32 (
409 IN UINTN Address,
410 IN UINT32 Value
411 )
412 {
413 return (UINT32)MmioWriteWorker (Address, EfiCpuIoWidthUint32, Value);
414 }
415
416 /**
417 Reads a 64-bit MMIO register.
418
419 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
420 returned. This function must guarantee that all MMIO read and write
421 operations are serialized.
422
423 If 64-bit MMIO register operations are not supported, then ASSERT().
424
425 @param Address The MMIO register to read.
426
427 @return The value read.
428
429 **/
430 UINT64
431 EFIAPI
432 MmioRead64 (
433 IN UINTN Address
434 )
435 {
436 return (UINT64)MmioReadWorker (Address, EfiCpuIoWidthUint64);
437 }
438
439 /**
440 Writes a 64-bit MMIO register.
441
442 Writes the 64-bit MMIO register specified by Address with the value specified
443 by Value and returns Value. This function must guarantee that all MMIO read
444 and write operations are serialized.
445
446 If 64-bit MMIO register operations are not supported, then ASSERT().
447
448 @param Address The MMIO register to write.
449 @param Value The value to write to the MMIO register.
450
451 **/
452 UINT64
453 EFIAPI
454 MmioWrite64 (
455 IN UINTN Address,
456 IN UINT64 Value
457 )
458 {
459 return (UINT64)MmioWriteWorker (Address, EfiCpuIoWidthUint64, Value);
460 }