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