1 /*++
2 
3 Copyright (c)  1999  - 2014, Intel Corporation. All rights reserved
4 
5 
6   This program and the accompanying materials are licensed and made available under
7 
8   the terms and conditions of the BSD License that accompanies this distribution.
9 
10   The full text of the license may be found at
11 
12   http://opensource.org/licenses/bsd-license.php.
13 
14 
15 
16   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 
18   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 
20 
21 
22 
23 
24 Module Name:
25 
26   SmmIo.c
27 
28 Abstract:
29 
30   SMM I/O access utility implementation file, for Ia32
31 
32 --*/
33 
34 //
35 // Include files
36 //
37 #include "Library/StallSmmLib.h"
38 #include "Pi/PiSmmCis.h"
39 #include "PiDxe.h"
40 #include <Library/IoLib.h>
41 #include <Library/PcdLib.h>
42 #include "PchAccess.h"
43 
44 /**
SmmStall(IN UINTN Microseconds)45   Delay for at least the request number of microseconds.
46   Timer used is ACPI time counter, which has 1us granularity.
47 
48   @param Microseconds  Number of microseconds to delay.
49 
50   @retval None
51 
52 **/
53 VOID
54 SmmStall (
55   IN  UINTN   Microseconds
56   )
57 {
58   UINTN   Ticks;
59   UINTN   Counts;
60   UINTN   CurrentTick;
61   UINTN   OriginalTick;
62   UINTN   RemainingTick;
63   UINT16  AcpiBaseAddr;
64 
65   if (Microseconds == 0) {
66     return;
67   }
68 
69   AcpiBaseAddr = PchLpcPciCfg16 (R_PCH_LPC_ACPI_BASE) & B_PCH_LPC_ACPI_BASE_BAR;
70 
71   OriginalTick = IoRead32 (AcpiBaseAddr + R_PCH_ACPI_PM1_TMR);
72   CurrentTick = OriginalTick;
73 
74   //
75   // The timer frequency is 3.579545 MHz, so 1 ms corresponds 3.58 clocks
76   //
77   Ticks = Microseconds * 358 / 100 + OriginalTick + 1;
78 
79   //
80   // The loops needed by timer overflow
81   //
82   Counts = Ticks / V_PCH_ACPI_PM1_TMR_MAX_VAL;
83 
84   //
85   // Remaining clocks within one loop
86   //
87   RemainingTick = Ticks % V_PCH_ACPI_PM1_TMR_MAX_VAL;
88 
89   //
90   // not intend to use TMROF_STS bit of register PM1_STS, because this adds extra
91   // one I/O operation, and maybe generate SMI
92   //
93   while ((Counts != 0) || (RemainingTick > CurrentTick)) {
94     CurrentTick = IoRead32 (AcpiBaseAddr + R_PCH_ACPI_PM1_TMR);
95     //
96     // Check if timer overflow
97     //
98     if (CurrentTick < OriginalTick) {
99       Counts--;
100     }
101     OriginalTick = CurrentTick;
102   }
103 }
104