hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/** @file
  Serial Io Common Lib implementation.
  All function in this library is available for PEI, DXE, and SMM,
  But do not support UEFI RUNTIME environment call.
 
  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
 
#include <Base.h>
#include <Uefi/UefiBaseType.h>
#include <Include/PcieRegs.h>
#include <IndustryStandard/Pci30.h>
#include <Register/PchRegs.h>
#include <Library/IoLib.h>
#include <Library/BaseLib.h>
#include <Library/SerialIoAccessLib.h>
#include <Library/SerialIoPrivateLib.h>
#include <Library/PciSegmentLib.h>
#include <Library/PchInfoLib.h>
#include <Library/S3BootScriptLib.h>
#include <Library/DebugLib.h>
#include <PchLimits.h>
#include <PchReservedResources.h>
#include <Library/PchPciBdfLib.h>
 
/**
  Returns BAR0
 
  @param[in] PciCfgBase         Pci Config Base
 
  @retval    64bit MMIO BAR Address
**/
UINT64
GetSerialIoBar (
  IN UINT64        PciCfgBase
  )
{
  if (PciCfgBase < PCH_SERIAL_IO_BASE_ADDRESS) {
    return (UINT64) ((PciSegmentRead32 ((UINTN) (PciCfgBase + PCI_BASE_ADDRESSREG_OFFSET)) & 0xFFFFF000) + LShiftU64 (PciSegmentRead32 ((UINTN) (PciCfgBase + PCI_BASE_ADDRESSREG_OFFSET + 4)), 32));
  }
  return (UINT64) ((MmioRead32 ((UINTN) (PciCfgBase + PCI_BASE_ADDRESSREG_OFFSET)) & 0xFFFFF000) + LShiftU64 (MmioRead32 ((UINTN) (PciCfgBase + PCI_BASE_ADDRESSREG_OFFSET + 4)), 32));
}
 
/**
  Returns I2C Pci Config Space
 
  @param[in] I2cNumber         I2C Number
 
  @retval    I2C Pci Config Space Address
**/
UINT64
GetSerialIoI2cPciCfg (
  IN UINT8        I2cNumber
  )
{
  if (IsSerialIoI2cHidden (I2cNumber)) {
    return (UINTN) GetSerialIoI2cFixedPciCfgAddress (I2cNumber);
  }
  return SerialIoI2cPciCfgBase (I2cNumber);
}
 
/**
  Returns SPI Pci Config Space
 
  @param[in] SpiNumber         SPI Number
 
  @retval    SPI Pci Config Space Address
**/
UINT64
GetSerialIoSpiPciCfg (
  IN UINT8        SpiNumber
  )
{
  if (IsSerialIoSpiHidden (SpiNumber)) {
    return (UINTN) GetSerialIoSpiFixedPciCfgAddress (SpiNumber);
  }
  return SerialIoSpiPciCfgBase (SpiNumber);
}
 
/**
  Returns UART Pci Config Space
 
  @param[in] UartNumber         UART Number
 
  @retval    UART Pci Config Space Address
**/
UINT64
GetSerialIoUartPciCfg (
  IN UINT8        UartNumber
  )
{
  if (IsSerialIoUartHidden (UartNumber)) {
    return GetSerialIoUartFixedPciCfgAddress (UartNumber);
  }
  return SerialIoUartPciCfgBase (UartNumber);
}
 
/**
  Returns SPI S3 boot script PCI address
 
  @param[in] UartNumber         UART Number
 
  @retval    SPI S3 boot script PCI address
**/
UINT64
GetSerialIoSpiS3PciBase (
  IN UINT8        SpiNumber
  )
{
  if (IsSerialIoSpiHidden (SpiNumber)) {
  //
  // It's not expected to return Spi S3 Boot Script PCI address for non PCI mode.
  //
    ASSERT (TRUE);
  }
  return S3_BOOT_SCRIPT_LIB_PCI_ADDRESS (
           DEFAULT_PCI_BUS_NUMBER_PCH,
           SerialIoSpiDevNumber (SpiNumber),
           SerialIoSpiFuncNumber (SpiNumber),
           0
           );
}
 
/**
  Returns UART S3 boot script PCI address
 
  @param[in] UartNumber         UART Number
 
  @retval    UART  S3 boot script PCI address
**/
UINT64
GetSerialIoUartS3PciBase (
  IN UINT8        UartNumber
  )
{
  if (IsSerialIoUartHidden (UartNumber)) {
  //
  // It's not expected to return Uart S3 Boot Script PCI address for non PCI mode.
  //
    ASSERT (TRUE);
  }
  return S3_BOOT_SCRIPT_LIB_PCI_ADDRESS (
           DEFAULT_PCI_BUS_NUMBER_PCH,
           SerialIoUartDevNumber (UartNumber),
           SerialIoUartFuncNumber (UartNumber),
           0
           );
}
 
/**
  Returns I2C S3 boot script PCI address
 
  @param[in] I2cNumber         I2C Number
 
  @retval    I2C  S3 boot script PCI address
**/
UINT64
GetSerialIoI2cS3PciBase (
  IN UINT8        I2cNumber
  )
{
  if (IsSerialIoI2cHidden (I2cNumber)) {
  //
  // It's not expected to return I2c S3 Boot Script PCI address for non PCI mode.
  //
    ASSERT (TRUE);
  }
  return S3_BOOT_SCRIPT_LIB_PCI_ADDRESS (
           DEFAULT_PCI_BUS_NUMBER_PCH,
           SerialIoI2cDevNumber (I2cNumber),
           SerialIoI2cFuncNumber (I2cNumber),
           0
           );
}
 
/**
  Checks if Device with given PciDeviceId is one of SerialIo I2C controllers
  If yes, its number is returned through I2cIndex parameter, otherwise I2cIndex is not updated
 
  @param[in]  PciDevId                  Device ID
  @param[out] I2cNumber                 Number of SerialIo I2C controller
 
  @retval TRUE                          yes it is a SerialIo I2C controller
  @retval FALSE                         no it isn't a SerialIo I2C controller
**/
BOOLEAN
IsSerialIoI2cDeviceId (
  IN  UINT16    PciDevId,
  OUT UINT8     *I2cNumber
  )
{
  UINT8 Index;
 
  for (Index = 0; Index < GetPchMaxSerialIoI2cControllersNum (); Index++) {
    if (PciDevId == GetSerialIoI2cDeviceId (Index)) {
      *I2cNumber = Index;
      return TRUE;
    }
  }
  return FALSE;
}
 
/**
  Checks if I2c is Function 0 Enabled
 
  @param[in] I2cIndex                   Number of the SerialIo I2C controller
 
  @retval TRUE                          Enabled
  @retval FALSE                         Disabled
**/
BOOLEAN
IsSerialIoI2cFunction0Enabled (
  IN UINT8    I2cIndex
  )
{
  if (SerialIoI2cFuncNumber (I2cIndex) == 0) {
    if (SerialIoHigherFunctionsEnabled (SerialIoI2cDevNumber (I2cIndex))) {
      return TRUE;
    }
  }
  return FALSE;
}
 
/**
  Checks if Uart is Function 0 Enabled
 
  @param[in] UartIndex                   Number of the SerialIo Uart controller
 
  @retval TRUE                          Enabled
  @retval FALSE                         Disabled
**/
BOOLEAN
IsSerialIoUartFunction0Enabled (
  IN UINT8    UartIndex
  )
{
  if (SerialIoUartFuncNumber (UartIndex) == 0) {
    if (SerialIoHigherFunctionsEnabled (SerialIoUartDevNumber (UartIndex))) {
      return TRUE;
    }
  }
  return FALSE;
}
 
/**
  Checks if Spi is Function 0 Enabled
 
  @param[in] SpiIndex                   Number of the SerialIo Spi controller
 
  @retval TRUE                          Enabled
  @retval FALSE                         Disabled
**/
BOOLEAN
IsSerialIoSpiFunction0Enabled (
  IN UINT8    SpiIndex
  )
{
  if (SerialIoSpiFuncNumber (SpiIndex) == 0) {
    if (SerialIoHigherFunctionsEnabled (SerialIoSpiDevNumber (SpiIndex))) {
      return TRUE;
    }
  }
  return FALSE;
}