summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/SaInit/Dxe/LegacyRegion.c
blob: 5c9133635a7158c26e654dc787dbd32384ef7532 (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/** @file
  This code provides a private implementation of the Legacy Region protocol.

@copyright
  Copyright (c) 1999 - 2012 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement

**/
#include "LegacyRegion.h"

///
/// Module Global:
///   Since this driver will only ever produce one instance of the Private Data
///   protocol you are not required to dynamically allocate the PrivateData.
///
LEGACY_REGION_INSTANCE        mPrivateData;

UINT8                         mRomData[16] = {
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00
};

UINT8                         mLockData[16] = {
  0x01,
  0x10,
  0x01,
  0x10,
  0x01,
  0x10,
  0x01,
  0x10,
  0x01,
  0x10,
  0x01,
  0x10, /// it was: 0x03, 0x30, 0x03, 0x30, <-- why ??
  0x10,
  0x10,
  0x10,
  0x10
};

UINT8                         mUnlockData[16] = {
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x30,
  0x30,
  0x30,
  0x30
};

UINT8                         mMaskData[16] = {
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x30,
  0x03,
  0x00,
  0x00,
  0x00,
  0x00
};

UINT8                         mReg[16] = {
  R_SA_PAM1,
  R_SA_PAM1,
  R_SA_PAM2,
  R_SA_PAM2,
  R_SA_PAM3,
  R_SA_PAM3,
  R_SA_PAM4,
  R_SA_PAM4,
  R_SA_PAM5,
  R_SA_PAM5,
  R_SA_PAM6,
  R_SA_PAM6,
  R_SA_PAM0,
  R_SA_PAM0,
  R_SA_PAM0,
  R_SA_PAM0
};

EFI_STATUS
LegacyRegionManipulation (
  IN  EFI_LEGACY_REGION_PROTOCOL  * This,
  IN  UINT32                      Start,
  IN  UINT32                      Length,
  IN  UINT32                      Mode,
  OUT UINT32                      *Granularity OPTIONAL
  )
/**
  Modify PAM registers for region specified to MODE state.

  @param[in] This    - Pointer to EFI_LEGACY_REGION_PROTOCOL instance.
  @param[in] Start   - Starting address of a memory region covered by PAM registers (C0000h - FFFFFh).
  @param[in] Length  - Memory region length.
  @param[in] Mode    - Action to perform on a PAM region: UNLOCK, LOCK or BOOTLOCK.
  @param[out] Granularity - Granularity of region in bytes.

  @retval EFI_SUCCESS - PAM region(s) state modified as requested.
  @retval EFI_INVALID_PARAMETER - parameter out of boundary
**/
{
  UINT8                           Index;
  UINT8                           Data;
  UINT8                           TempData;
  UINT32                          TempAddr;
  UINT32                          NewStartAddr;
  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *Pci;
  EFI_CPU_ARCH_PROTOCOL           *CpuArch;
  UINT64                          PciAddress;
  UINT64                          Attributes;
  EFI_STATUS                      Status;

  Pci                     = mPrivateData.PciRootBridgeIo;
  if ((Start  < 0xC0000) ||
      (Start  > 0xFFFFF) ||
      (Length > 0x40000) ||
      ((Start + Length - 1) > 0xFFFFF)) {
    return EFI_INVALID_PARAMETER;
  }
  TempAddr                = Start;
  Index                   = (UINT8) ((TempAddr - 0xC0000) / PAM_GRANULARITY);
  NewStartAddr            = TempAddr = (TempAddr / PAM_GRANULARITY) * PAM_GRANULARITY;
  while (TempAddr <= (Start + Length - 1)) {
    if (Index >= 16) {
      return EFI_INVALID_PARAMETER;
    }
    if ((Mode == LOCK) || (Mode == BOOTLOCK)) {
      Data = mLockData[Index];
    } else {
      if (Mode == UNLOCK) {
        Data = mUnlockData[Index];
      } else {
        Data = mRomData[Index];
      }
    }

    PciAddress = EFI_PCI_ADDRESS (0, 0, 0, mReg[Index]);
    Pci->Pci.Read (Pci, EfiPciWidthUint8, PciAddress, 1, &TempData);
    TempData = (UINT8) ((TempData & mMaskData[Index]) | Data);
    Pci->Pci.Write (Pci, EfiPciWidthUint8, PciAddress, 1, &TempData);
    TempAddr += PAM_GRANULARITY;
    Index++;
  }

  if (Granularity != NULL) {
    *Granularity = PAM_GRANULARITY;
  }
  ///
  ///  Program the MTRRs
  ///
  switch (Mode) {

  case UNLOCK:
    Attributes = EFI_MEMORY_WT;
    break;

  case LOCK:
    Attributes = EFI_MEMORY_WP;
    break;

  case BOOTLOCK:
    Attributes = EFI_MEMORY_WP;
    break;

  default:
    Attributes = EFI_MEMORY_UC;

  }

  Status = gBS->LocateProtocol (
                  &gEfiCpuArchProtocolGuid,
                  NULL,
                  (VOID **) &CpuArch
                  );
  ASSERT_EFI_ERROR (Status);
  Status = CpuArch->SetMemoryAttributes (
                      CpuArch,
                      NewStartAddr,
                      TempAddr - NewStartAddr,
                      Attributes
                      );
  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
LegacyRegionDecode (
  IN  EFI_LEGACY_REGION_PROTOCOL   *This,
  IN  UINT32                       Start,
  IN  UINT32                       Length,
  IN  BOOLEAN                      *On
  )
/**
  Enable/Disable decoding of the given region

  @param[in] This    - Pointer to EFI_LEGACY_REGION_PROTOCOL instance.
  @param[in] Start  - Starting address of region.
  @param[in] Length - Length of region in bytes.
  @param[in] On     - 0 = Disable decoding, 1 = Enable decoding.

  @retval EFI_SUCCESS - Decoding change affected.
**/
{
  UINT32  Granularity;
  if (*On) {
    return LegacyRegionManipulation (This, Start, Length, UNLOCK, &Granularity);
  } else {
    return LegacyRegionManipulation (This, Start, Length, LEGACY_REGION_DECODE_ROM, &Granularity);
  }
}

EFI_STATUS
EFIAPI
LegacyRegionBootLock (
  IN  EFI_LEGACY_REGION_PROTOCOL   * This,
  IN  UINT32                       Start,
  IN  UINT32                       Length,
  OUT UINT32                       *Granularity OPTIONAL
  )
/**
  Make the indicated region read from RAM / write to ROM.

  @param[in] This        - Pointer to EFI_LEGACY_REGION_PROTOCOL instance.
  @param[in] Start       - Starting address of region.
  @param[in] Length      - Length of region in bytes.
  @param[out] Granularity - Granularity of region in bytes.

  @retval EFI_SUCCESS - Region locked or made R/O.
**/
{
  return LegacyRegionManipulation (This, Start, Length, BOOTLOCK, Granularity);
}

EFI_STATUS
EFIAPI
LegacyRegionLock (
  IN  EFI_LEGACY_REGION_PROTOCOL   * This,
  IN  UINT32                       Start,
  IN  UINT32                       Length,
  OUT UINT32                       *Granularity OPTIONAL
  )
/**
  Make the indicated region read from RAM / write to ROM.

  @param[in] This    - Pointer to EFI_LEGACY_REGION_PROTOCOL instance.
  @param[in] Start       - Starting address of region.
  @param[in] Length      - Length of region in bytes.
  @param[out] Granularity - Granularity of region in bytes.

  @retval EFI_SUCCESS - Region locked or made R/O.
**/
{
  return LegacyRegionManipulation (This, Start, Length, LOCK, Granularity);
}

EFI_STATUS
EFIAPI
LegacyRegionUnlock (
  IN  EFI_LEGACY_REGION_PROTOCOL   * This,
  IN  UINT32                       Start,
  IN  UINT32                       Length,
  OUT UINT32                       *Granularity OPTIONAL
  )
/**
  Make the indicated region read from RAM / write to RAM.

  @param[in] This    - Pointer to EFI_LEGACY_REGION_PROTOCOL instance.
  @param[in] Start       - Starting address of region.
  @param[in] Length      - Length of region in bytes.
  @param[out] Granularity - Granularity of region in bytes.

  @retval EFI_SUCCESS - Region unlocked or made R/W.
**/
{
  return LegacyRegionManipulation (This, Start, Length, UNLOCK, Granularity);
}

EFI_STATUS
LegacyRegionInstall (
  IN EFI_HANDLE           ImageHandle
  )
/**
  Install Driver to produce Legacy Region protocol.

  @param[in] ImageHandle             Handle for the image of this driver

  @retval EFI_SUCCESS - Legacy Region protocol installed
  @retval Other       - No protocol installed, unload driver.
**/
{
  EFI_STATUS              Status;
  LEGACY_REGION_INSTANCE  *Private;
  Private = &mPrivateData;

  ///
  /// Grab a copy of all the protocols we depend on. Any error would
  /// be a dispatcher bug!.
  ///
  Status = gBS->LocateProtocol (
                  &gEfiPciRootBridgeIoProtocolGuid,
                  NULL,
                  (VOID **) &Private->PciRootBridgeIo
                  );
  Private->Signature              = LEGACY_REGION_INSTANCE_SIGNATURE;
  Private->LegacyRegion.Decode    = LegacyRegionDecode;
  Private->LegacyRegion.Lock      = LegacyRegionLock;
  Private->LegacyRegion.BootLock  = LegacyRegionBootLock;
  Private->LegacyRegion.UnLock    = LegacyRegionUnlock;
  Private->ImageHandle            = ImageHandle;

  ///
  /// Make a new handle and install the protocol
  ///
  Private->Handle = NULL;
  return gBS->InstallProtocolInterface (
                &Private->Handle,
                &gEfiLegacyRegionProtocolGuid,
                EFI_NATIVE_INTERFACE,
                &Private->LegacyRegion
                );
}