hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/** @file
Common Lib function for QNC internal network access.
 
Copyright (c) 2013-2015 Intel Corporation.
 
SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
//
// The package level header files this module uses
//
#include <Uefi.h>
 
#include <IntelQNCRegs.h>
#include <Library/QNCAccessLib.h>
#include <Library/DebugLib.h>
#include <IndustryStandard/Pci22.h>
 
UINT32
EFIAPI
QNCPortRead(
  UINT8 Port,
  UINT32 RegAddress
  )
{
  McD0PciCfg32 (QNC_ACCESS_PORT_MEA) = (RegAddress & 0xFFFFFF00);
  McD0PciCfg32 (QNC_ACCESS_PORT_MCR) = MESSAGE_READ_DW (Port, RegAddress);
  return McD0PciCfg32 (QNC_ACCESS_PORT_MDR);
}
 
VOID
EFIAPI
QNCPortWrite (
  UINT8 Port,
  UINT32 RegAddress,
  UINT32 WriteValue
  )
{
  McD0PciCfg32 (QNC_ACCESS_PORT_MDR) = WriteValue;
  McD0PciCfg32 (QNC_ACCESS_PORT_MEA) = (RegAddress & 0xFFFFFF00);
  McD0PciCfg32 (QNC_ACCESS_PORT_MCR) = MESSAGE_WRITE_DW (Port, RegAddress);
}
 
UINT32
EFIAPI
QNCAltPortRead (
  UINT8 Port,
  UINT32 RegAddress
  )
{
  McD0PciCfg32 (QNC_ACCESS_PORT_MEA) = (RegAddress & 0xFFFFFF00);
  McD0PciCfg32 (QNC_ACCESS_PORT_MCR) = ALT_MESSAGE_READ_DW (Port, RegAddress);
  return McD0PciCfg32 (QNC_ACCESS_PORT_MDR);
}
 
VOID
EFIAPI
QNCAltPortWrite (
  UINT8 Port,
  UINT32 RegAddress,
  UINT32 WriteValue
  )
{
  McD0PciCfg32 (QNC_ACCESS_PORT_MDR) = WriteValue;
  McD0PciCfg32 (QNC_ACCESS_PORT_MEA) = (RegAddress & 0xFFFFFF00);
  McD0PciCfg32 (QNC_ACCESS_PORT_MCR) = ALT_MESSAGE_WRITE_DW (Port, RegAddress);
}
 
UINT32
EFIAPI
QNCPortIORead(
  UINT8 Port,
  UINT32 RegAddress
  )
{
  McD0PciCfg32 (QNC_ACCESS_PORT_MEA) = (RegAddress & 0xFFFFFF00);
  McD0PciCfg32 (QNC_ACCESS_PORT_MCR) = MESSAGE_IO_READ_DW (Port, RegAddress);
  return McD0PciCfg32 (QNC_ACCESS_PORT_MDR);
}
 
VOID
EFIAPI
QNCPortIOWrite (
  UINT8 Port,
  UINT32 RegAddress,
  UINT32 WriteValue
  )
{
  McD0PciCfg32 (QNC_ACCESS_PORT_MDR) = WriteValue;
  McD0PciCfg32 (QNC_ACCESS_PORT_MEA) = (RegAddress & 0xFFFFFF00);
  McD0PciCfg32 (QNC_ACCESS_PORT_MCR) = MESSAGE_IO_WRITE_DW (Port, RegAddress);
}
 
RETURN_STATUS
EFIAPI
QNCMmIoWrite (
  UINT32             MmIoAddress,
  QNC_MEM_IO_WIDTH    Width,
  UINT32             DataNumber,
  VOID               *pData
  )
/*++
 
Routine Description:
 
  This is for the special consideration for QNC MMIO write, as required by FWG, a reading must be performed after MMIO writing
to ensure the expected write is processed and data is flushed into chipset
 
Arguments:
 
  Row -- row number to be cleared ( start from 1 )
 
Returns:
 
  EFI_SUCCESS
 
--*/
{
  RETURN_STATUS  Status;
  UINTN          Index;
 
  Status = RETURN_SUCCESS;
 
  for (Index =0; Index < DataNumber; Index++) {
    switch (Width) {
      case QNCMmioWidthUint8:
        QNCMmio8 (MmIoAddress, 0) = ((UINT8 *)pData)[Index];
        if (QNCMmio8 (MmIoAddress, 0) != ((UINT8*)pData)[Index]) {
          Status = RETURN_DEVICE_ERROR;
          break;
        }
        break;
 
      case QNCMmioWidthUint16:
        QNCMmio16 (MmIoAddress, 0) = ((UINT16 *)pData)[Index];
        if (QNCMmio16 (MmIoAddress, 0) != ((UINT16 *)pData)[Index]) {
          Status = RETURN_DEVICE_ERROR;
          break;
        }
        break;
 
      case QNCMmioWidthUint32:
        QNCMmio32 (MmIoAddress, 0) = ((UINT32 *)pData)[Index];
        if (QNCMmio32 (MmIoAddress, 0) != ((UINT32 *)pData)[Index]) {
          Status = RETURN_DEVICE_ERROR;
          break;
        }
        break;
 
      case QNCMmioWidthUint64:
        QNCMmio64 (MmIoAddress, 0) = ((UINT64 *)pData)[Index];
        if (QNCMmio64 (MmIoAddress, 0) != ((UINT64 *)pData)[Index]) {
          Status = RETURN_DEVICE_ERROR;
          break;
        }
        break;
 
      default:
        break;
    }
  }
 
  return Status;
}
 
UINT32
EFIAPI
QncHsmmcRead (
  VOID
  )
{
  return QNCPortRead (QUARK_NC_HOST_BRIDGE_SB_PORT_ID, QNC_MSG_FSBIC_REG_HSMMC);
}
 
VOID
EFIAPI
QncHsmmcWrite (
  UINT32 WriteValue
  )
{
  UINT16  DeviceId;
  UINT32  Data32;
 
  //
  // Check what Soc we are running on (read Host bridge DeviceId)
  //
  DeviceId = QNCMmPci16(0, MC_BUS, MC_DEV, MC_FUN, PCI_DEVICE_ID_OFFSET);
 
  if (DeviceId == QUARK2_MC_DEVICE_ID) {
    //
    // Disable HSMMC configuration
    //
    Data32 = QncHsmmcRead ();
    Data32 &= ~SMM_CTL_EN;
    QNCPortWrite (QUARK_NC_HOST_BRIDGE_SB_PORT_ID, QNC_MSG_FSBIC_REG_HSMMC, Data32);
 
    //
    // Validate HSMMC configuration is disabled
    //
    Data32 = QncHsmmcRead ();
    ASSERT((Data32 & SMM_CTL_EN) == 0);
 
    //
    // Enable HSMMC configuration
    //
    WriteValue |= SMM_CTL_EN;
  }
 
  //
  // Write the register value
  //
  QNCPortWrite (QUARK_NC_HOST_BRIDGE_SB_PORT_ID, QNC_MSG_FSBIC_REG_HSMMC, WriteValue);
 
  if (DeviceId == QUARK2_MC_DEVICE_ID) {
    //
    // Validate HSMMC configuration is enabled
    //
    Data32 = QncHsmmcRead ();
    ASSERT((Data32 & SMM_CTL_EN) != 0);
  }
}
 
VOID
EFIAPI
QncImrWrite (
  UINT32 ImrBaseOffset,
  UINT32 ImrLow,
  UINT32 ImrHigh,
  UINT32 ImrReadMask,
  UINT32 ImrWriteMask
  )
{
  UINT16  DeviceId;
  UINT32  Data32;
 
  //
  // Check what Soc we are running on (read Host bridge DeviceId)
  //
  DeviceId = QNCMmPci16(0, MC_BUS, MC_DEV, MC_FUN, PCI_DEVICE_ID_OFFSET);
 
  //
  // Disable IMR protection
  //
  if (DeviceId == QUARK2_MC_DEVICE_ID) {
    //
    // Disable IMR protection
    //
    Data32 = QNCPortRead (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXL);
    Data32 &= ~IMR_EN;
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXL, Data32);
 
    //
    // Validate IMR protection is disabled
    //
    Data32 = QNCPortRead (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXL);
    ASSERT((Data32 & IMR_EN) == 0);
 
    //
    // Update the IMR (IMRXL must be last as it may enable IMR violation checking)
    //
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXRM, ImrReadMask);
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXWM, ImrWriteMask);
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXH, ImrHigh);
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXL, ImrLow);
 
    //
    // Validate IMR protection is enabled/disabled
    //
    Data32 = QNCPortRead (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXL);
    ASSERT((Data32 & IMR_EN) == (ImrLow & IMR_EN));
  } else {
    //
    // Disable IMR protection (allow all access)
    //
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXRM, (UINT32)IMRX_ALL_ACCESS);
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXWM, (UINT32)IMRX_ALL_ACCESS);
 
    //
    // Update the IMR (IMRXRM/IMRXWM must be last as they restrict IMR access)
    //
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXL, (ImrLow & ~IMR_EN));
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXH, ImrHigh);
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXRM, ImrReadMask);
    QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, ImrBaseOffset+QUARK_NC_MEMORY_MANAGER_IMRXWM, ImrWriteMask);
  }
}
 
VOID
EFIAPI
QncIClkAndThenOr (
  UINT32 RegAddress,
  UINT32 AndValue,
  UINT32 OrValue
  )
{
  UINT32 RegValue;
  //
  // Whenever an iCLK SB register (Endpoint 32h) is being programmed the access
  // should always consist of a READ from the address followed by 2 identical
  // WRITEs to that address.
  //
  RegValue = QNCAltPortRead (QUARK_ICLK_SB_PORT_ID, RegAddress);
  RegValue &= AndValue;
  RegValue |= OrValue;
  QNCAltPortWrite (QUARK_ICLK_SB_PORT_ID, RegAddress, RegValue);
  QNCAltPortWrite (QUARK_ICLK_SB_PORT_ID, RegAddress, RegValue);
}
 
VOID
EFIAPI
QncIClkOr (
  UINT32 RegAddress,
  UINT32 OrValue
  )
{
  UINT32 RegValue;
  //
  // Whenever an iCLK SB register (Endpoint 32h) is being programmed the access
  // should always consist of a READ from the address followed by 2 identical
  // WRITEs to that address.
  //
  RegValue = QNCAltPortRead (QUARK_ICLK_SB_PORT_ID, RegAddress);
  RegValue |= OrValue;
  QNCAltPortWrite (QUARK_ICLK_SB_PORT_ID, RegAddress, RegValue);
  QNCAltPortWrite (QUARK_ICLK_SB_PORT_ID, RegAddress, RegValue);
}