]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/SmmIoLibSmmCpuIo2/IoLib.c
Change SMM CPU I/O to SMM CPU I/O 2
[mirror_edk2.git] / MdePkg / Library / SmmIoLibSmmCpuIo2 / IoLib.c
1 /** @file
2 I/O Library.
3 The implementation of I/O operation for this library instance
4 are based on EFI_CPU_IO_PROTOCOL.
5
6 Copyright (c) 2009 - 2010, Intel Corporation<BR>
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "SmmCpuIoLibInternal.h"
18
19 //
20 // Globle varible to cache pointer to CpuIo protocol.
21 //
22 EFI_SMM_CPU_IO2_PROTOCOL *mCpuIo2 = NULL;
23
24 /**
25 The constructor function caches the pointer to CpuIo protocol.
26
27 The constructor function locates CpuIo protocol from protocol database.
28 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
29
30 @param ImageHandle The firmware allocated handle for the EFI image.
31 @param SystemTable A pointer to the EFI System Table.
32
33 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
34
35 **/
36 EFI_STATUS
37 EFIAPI
38 IoLibConstructor (
39 IN EFI_HANDLE ImageHandle,
40 IN EFI_SYSTEM_TABLE *SystemTable
41 )
42 {
43 EFI_STATUS Status;
44
45 Status = gSmst->SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, (VOID **) &mCpuIo2);
46 ASSERT_EFI_ERROR (Status);
47
48 return Status;
49 }
50
51 /**
52 Reads registers in the EFI CPU I/O space.
53
54 Reads the I/O port specified by Port with registers width specified by Width.
55 The read value is returned. If such operations are not supported, then ASSERT().
56 This function must guarantee that all I/O read and write operations are serialized.
57
58 @param Port The base address of the I/O operation.
59 The caller is responsible for aligning the Address if required.
60 @param Width The width of the I/O operation.
61
62 @return Data read from registers in the EFI CPU I/O space.
63
64 **/
65 UINT64
66 EFIAPI
67 IoReadWorker (
68 IN UINTN Port,
69 IN EFI_SMM_IO_WIDTH Width
70 )
71 {
72 EFI_STATUS Status;
73 UINT64 Data;
74
75 Status = mCpuIo2->Io.Read (mCpuIo2, Width, Port, 1, &Data);
76 ASSERT_EFI_ERROR (Status);
77
78 return Data;
79 }
80
81 /**
82 Writes registers in the EFI CPU I/O space.
83
84 Writes the I/O port specified by Port with registers width and value specified by Width
85 and Data respectively. Data is returned. If such operations are not supported, then ASSERT().
86 This function must guarantee that all I/O read and write operations are serialized.
87
88 @param Port The base address of the I/O operation.
89 The caller is responsible for aligning the Address if required.
90 @param Width The width of the I/O operation.
91 @param Data The value to write to the I/O port.
92
93 @return The paramter of Data.
94
95 **/
96 UINT64
97 EFIAPI
98 IoWriteWorker (
99 IN UINTN Port,
100 IN EFI_SMM_IO_WIDTH Width,
101 IN UINT64 Data
102 )
103 {
104 EFI_STATUS Status;
105
106 Status = mCpuIo2->Io.Write (mCpuIo2, Width, Port, 1, &Data);
107 ASSERT_EFI_ERROR (Status);
108
109 return Data;
110 }
111
112 /**
113 Reads memory-mapped registers in the EFI system memory space.
114
115 Reads the MMIO registers specified by Address with registers width specified by Width.
116 The read value is returned. If such operations are not supported, then ASSERT().
117 This function must guarantee that all MMIO read and write operations are serialized.
118
119 @param Address The MMIO register to read.
120 The caller is responsible for aligning the Address if required.
121 @param Width The width of the I/O operation.
122
123 @return Data read from registers in the EFI system memory space.
124
125 **/
126 UINT64
127 EFIAPI
128 MmioReadWorker (
129 IN UINTN Address,
130 IN EFI_SMM_IO_WIDTH Width
131 )
132 {
133 EFI_STATUS Status;
134 UINT64 Data;
135
136 Status = mCpuIo2->Mem.Read (mCpuIo2, Width, Address, 1, &Data);
137 ASSERT_EFI_ERROR (Status);
138
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 the I/O port.
153
154 @return Data read from registers in the EFI system memory space.
155
156 **/
157 UINT64
158 EFIAPI
159 MmioWriteWorker (
160 IN UINTN Address,
161 IN EFI_SMM_IO_WIDTH Width,
162 IN UINT64 Data
163 )
164 {
165 EFI_STATUS Status;
166
167 Status = mCpuIo2->Mem.Write (mCpuIo2, Width, Address, 1, &Data);
168 ASSERT_EFI_ERROR (Status);
169
170 return Data;
171 }
172
173 /**
174 Reads an 8-bit I/O port.
175
176 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
177 This function must guarantee that all I/O read and write operations are
178 serialized.
179
180 If 8-bit I/O port operations are not supported, then ASSERT().
181
182 @param Port The I/O port to read.
183
184 @return The value read.
185
186 **/
187 UINT8
188 EFIAPI
189 IoRead8 (
190 IN UINTN Port
191 )
192 {
193 return (UINT8)IoReadWorker (Port, SMM_IO_UINT8);
194 }
195
196 /**
197 Writes an 8-bit I/O port.
198
199 Writes the 8-bit I/O port specified by Port with the value specified by Value
200 and returns Value. This function must guarantee that all I/O read and write
201 operations are serialized.
202
203 If 8-bit I/O port operations are not supported, then ASSERT().
204
205 @param Port The I/O port to write.
206 @param Value The value to write to the I/O port.
207
208 @return The value written the I/O port.
209
210 **/
211 UINT8
212 EFIAPI
213 IoWrite8 (
214 IN UINTN Port,
215 IN UINT8 Value
216 )
217 {
218 return (UINT8)IoWriteWorker (Port, SMM_IO_UINT8, Value);
219 }
220
221 /**
222 Reads a 16-bit I/O port.
223
224 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
225 This function must guarantee that all I/O read and write operations are
226 serialized.
227
228 If Port is not aligned on a 16-bit boundary, then ASSERT().
229
230 If 16-bit I/O port operations are not supported, then ASSERT().
231
232 @param Port The I/O port to read.
233
234 @return The value read.
235
236 **/
237 UINT16
238 EFIAPI
239 IoRead16 (
240 IN UINTN Port
241 )
242 {
243 //
244 // Make sure Port is aligned on a 16-bit boundary.
245 //
246 ASSERT ((Port & 1) == 0);
247 return (UINT16)IoReadWorker (Port, SMM_IO_UINT16);
248 }
249
250 /**
251 Writes a 16-bit I/O port.
252
253 Writes the 16-bit I/O port specified by Port with the value specified by Value
254 and returns Value. This function must guarantee that all I/O read and write
255 operations are serialized.
256
257 If Port is not aligned on a 16-bit boundary, then ASSERT().
258
259 If 16-bit I/O port operations are not supported, then ASSERT().
260
261 @param Port The I/O port to write.
262 @param Value The value to write to the I/O port.
263
264 @return The value written the I/O port.
265
266 **/
267 UINT16
268 EFIAPI
269 IoWrite16 (
270 IN UINTN Port,
271 IN UINT16 Value
272 )
273 {
274 //
275 // Make sure Port is aligned on a 16-bit boundary.
276 //
277 ASSERT ((Port & 1) == 0);
278 return (UINT16)IoWriteWorker (Port, SMM_IO_UINT16, Value);
279 }
280
281 /**
282 Reads a 32-bit I/O port.
283
284 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
285 This function must guarantee that all I/O read and write operations are
286 serialized.
287
288 If Port is not aligned on a 32-bit boundary, then ASSERT().
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, SMM_IO_UINT32);
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 Port is not aligned on a 32-bit boundary, then ASSERT().
318
319 If 32-bit I/O port operations are not supported, then ASSERT().
320
321 @param Port The I/O port to write.
322 @param Value The value to write to the I/O port.
323
324 @return The value written the I/O port.
325
326 **/
327 UINT32
328 EFIAPI
329 IoWrite32 (
330 IN UINTN Port,
331 IN UINT32 Value
332 )
333 {
334 //
335 // Make sure Port is aligned on a 32-bit boundary.
336 //
337 ASSERT ((Port & 3) == 0);
338 return (UINT32)IoWriteWorker (Port, SMM_IO_UINT32, Value);
339 }
340
341 /**
342 Reads a 64-bit I/O port.
343
344 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
345 This function must guarantee that all I/O read and write operations are
346 serialized.
347
348 If Port is not aligned on a 64-bit boundary, then ASSERT().
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
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, SMM_IO_UINT64);
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 Port is not aligned on a 64-bit boundary, then ASSERT().
378
379 If 64-bit I/O port operations are not supported, then ASSERT().
380
381 @param Port The I/O port to write.
382 @param Value The value to write to the I/O port.
383
384 @return The value written the I/O port.
385
386 **/
387 UINT64
388 EFIAPI
389 IoWrite64 (
390 IN UINTN Port,
391 IN UINT64 Value
392 )
393 {
394 //
395 // Make sure Port is aligned on a 64-bit boundary.
396 //
397 ASSERT ((Port & 7) == 0);
398 return IoWriteWorker (Port, SMM_IO_UINT64, Value);
399 }
400
401 /**
402 Reads an 8-bit MMIO register.
403
404 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
405 returned. This function must guarantee that all MMIO read and write
406 operations are serialized.
407
408 If 8-bit MMIO register operations are not supported, then ASSERT().
409
410 @param Address The MMIO register to read.
411
412 @return The value read.
413
414 **/
415 UINT8
416 EFIAPI
417 MmioRead8 (
418 IN UINTN Address
419 )
420 {
421 return (UINT8)MmioReadWorker (Address, SMM_IO_UINT8);
422 }
423
424 /**
425 Writes an 8-bit MMIO register.
426
427 Writes the 8-bit MMIO register specified by Address with the value specified
428 by Value and returns Value. This function must guarantee that all MMIO read
429 and write operations are serialized.
430
431 If 8-bit MMIO register operations are not supported, then ASSERT().
432
433 @param Address The MMIO register to write.
434 @param Value The value to write to the MMIO register.
435
436 **/
437 UINT8
438 EFIAPI
439 MmioWrite8 (
440 IN UINTN Address,
441 IN UINT8 Value
442 )
443 {
444 return (UINT8)MmioWriteWorker (Address, SMM_IO_UINT8, Value);
445 }
446
447 /**
448 Reads a 16-bit MMIO register.
449
450 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
451 returned. This function must guarantee that all MMIO read and write
452 operations are serialized.
453
454 If Address is not aligned on a 16-bit boundary, then ASSERT().
455
456 If 16-bit MMIO register operations are not supported, then ASSERT().
457
458 @param Address The MMIO register to read.
459
460 @return The value read.
461
462 **/
463 UINT16
464 EFIAPI
465 MmioRead16 (
466 IN UINTN Address
467 )
468 {
469 //
470 // Make sure Address is aligned on a 16-bit boundary.
471 //
472 ASSERT ((Address & 1) == 0);
473 return (UINT16)MmioReadWorker (Address, SMM_IO_UINT16);
474 }
475
476 /**
477 Writes a 16-bit MMIO register.
478
479 Writes the 16-bit MMIO register specified by Address with the value specified
480 by Value and returns Value. This function must guarantee that all MMIO read
481 and write operations are serialized.
482
483 If Address is not aligned on a 16-bit boundary, then ASSERT().
484
485 If 16-bit MMIO register operations are not supported, then ASSERT().
486
487 @param Address The MMIO register to write.
488 @param Value The value to write to the MMIO register.
489
490 **/
491 UINT16
492 EFIAPI
493 MmioWrite16 (
494 IN UINTN Address,
495 IN UINT16 Value
496 )
497 {
498 //
499 // Make sure Address is aligned on a 16-bit boundary.
500 //
501 ASSERT ((Address & 1) == 0);
502 return (UINT16)MmioWriteWorker (Address, SMM_IO_UINT16, Value);
503 }
504
505 /**
506 Reads a 32-bit MMIO register.
507
508 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
509 returned. This function must guarantee that all MMIO read and write
510 operations are serialized.
511
512 If Address is not aligned on a 32-bit boundary, then ASSERT().
513
514 If 32-bit MMIO register operations are not supported, then ASSERT().
515
516 @param Address The MMIO register to read.
517
518 @return The value read.
519
520 **/
521 UINT32
522 EFIAPI
523 MmioRead32 (
524 IN UINTN Address
525 )
526 {
527 //
528 // Make sure Address is aligned on a 32-bit boundary.
529 //
530 ASSERT ((Address & 3) == 0);
531 return (UINT32)MmioReadWorker (Address, SMM_IO_UINT32);
532 }
533
534 /**
535 Writes a 32-bit MMIO register.
536
537 Writes the 32-bit MMIO register specified by Address with the value specified
538 by Value and returns Value. This function must guarantee that all MMIO read
539 and write operations are serialized.
540
541 If Address is not aligned on a 32-bit boundary, then ASSERT().
542
543 If 32-bit MMIO register operations are not supported, then ASSERT().
544
545 @param Address The MMIO register to write.
546 @param Value The value to write to the MMIO register.
547
548 **/
549 UINT32
550 EFIAPI
551 MmioWrite32 (
552 IN UINTN Address,
553 IN UINT32 Value
554 )
555 {
556 //
557 // Make sure Address is aligned on a 32-bit boundary.
558 //
559 ASSERT ((Address & 3) == 0);
560 return (UINT32)MmioWriteWorker (Address, SMM_IO_UINT32, Value);
561 }
562
563 /**
564 Reads a 64-bit MMIO register.
565
566 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
567 returned. This function must guarantee that all MMIO read and write
568 operations are serialized.
569
570 If Address is not aligned on a 64-bit boundary, then ASSERT().
571
572 If 64-bit MMIO register operations are not supported, then ASSERT().
573
574 @param Address The MMIO register to read.
575
576 @return The value read.
577
578 **/
579 UINT64
580 EFIAPI
581 MmioRead64 (
582 IN UINTN Address
583 )
584 {
585 //
586 // Make sure Address is aligned on a 64-bit boundary.
587 //
588 ASSERT ((Address & 7) == 0);
589 return (UINT64)MmioReadWorker (Address, SMM_IO_UINT64);
590 }
591
592 /**
593 Writes a 64-bit MMIO register.
594
595 Writes the 64-bit MMIO register specified by Address with the value specified
596 by Value and returns Value. This function must guarantee that all MMIO read
597 and write operations are serialized.
598
599 If Address is not aligned on a 64-bit boundary, then ASSERT().
600
601 If 64-bit MMIO register operations are not supported, then ASSERT().
602
603 @param Address The MMIO register to write.
604 @param Value The value to write to the MMIO register.
605
606 **/
607 UINT64
608 EFIAPI
609 MmioWrite64 (
610 IN UINTN Address,
611 IN UINT64 Value
612 )
613 {
614 //
615 // Make sure Address is aligned on a 64-bit boundary.
616 //
617 ASSERT ((Address & 7) == 0);
618 return (UINT64)MmioWriteWorker (Address, SMM_IO_UINT64, Value);
619 }