summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.c
blob: aa40dc3052beb54011ef79603fb770305703f4dc (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
/** @file
  Library that implements the Arm CCA helper functions.

  Copyright (c) 2022 - 2026, Arm Limited. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

  @par Glossary:
    - Rsi or RSI   - Realm Service Interface
    - IPA          - Intermediate Physical Address
    - RIPAS        - Realm IPA state
**/
#include <Base.h>

#include <Library/ArmCcaLib.h>
#include <Library/ArmCcaRsiLib.h>
#include <Library/ArmLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>

#include <Uefi/UefiMultiPhase.h>
#include <Uefi/UefiSpec.h>
#include <Pi/PiBootMode.h>

#include <Pi/PiHob.h>
#include <Library/HobLib.h>

/**
  Queries to see if running in a Realm.

    @retval TRUE    The execution is within the context of a Realm.
    @retval FALSE   The execution is not within the context of a Realm.
**/
STATIC
BOOLEAN
EFIAPI
ArmCcaReadIsRealm (
  VOID
  )
{
  RETURN_STATUS  Status;
  UINT32         UefiImpl;
  UINT32         RmmImplLow;
  UINT32         RmmImplHigh;

  // Query the RSI_GET_VERSION. If it is implemented then
  // RMM is present and we must be running in a Realm.
  Status = ArmCcaRsiGetVersion (
             &UefiImpl,
             &RmmImplLow,
             &RmmImplHigh
             );
  if (!RETURN_ERROR (Status)) {
    return TRUE;
  }

  return FALSE;
}

/**
  Check if running in a Realm.

    @retval TRUE    The execution is within the context of a Realm.
    @retval FALSE   The execution is not within the context of a Realm.
**/
BOOLEAN
EFIAPI
ArmCcaIsRealm (
  VOID
  )
{
  BOOLEAN  RealmWorld;
  VOID     *HobList;

  VOID    *Hob;
  UINT64  *IsRealmHobData;

  if (!ArmHasRme ()) {
    return FALSE;
  }

  HobList = GetHobList ();
  if (HobList == NULL) {
    // HOB not initialised.
    return ArmCcaReadIsRealm ();
  }

  RealmWorld = FALSE;
  Hob        = GetFirstGuidHob (&gArmCcaIsRealmGuid);
  if ((Hob == NULL) ||
      (GET_GUID_HOB_DATA_SIZE (Hob) < sizeof (*IsRealmHobData)))
  {
    // The gArmCcaIsRealmGuid is created in the PlatformPeim() which
    // may not have been called for caching the IsRealm value yet.
    // So, read the Realm value.
    RealmWorld = ArmCcaReadIsRealm ();
  } else {
    // Read the value from the HOB
    IsRealmHobData = GET_GUID_HOB_DATA (Hob);
    RealmWorld     = *IsRealmHobData;
  }

  return RealmWorld;
}

/**
  Read the IPA width of the Realm.

  The IPA width of the Realm is used to configure the protection attribute
  for memory regions, see ArmCcaSetMemoryProtectionAttribute().

    @param [out] IpaWidth  IPA width of the Realm.

    @retval RETURN_SUCCESS            Success.
    @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
    @retval RETURN_OUT_OF_RESOURCES   Out of resources.

**/
STATIC
RETURN_STATUS
EFIAPI
ArmCcaReadIpaWidth (
  OUT UINT64  *IpaWidth
  )
{
  RETURN_STATUS         Status;
  ARM_CCA_REALM_CONFIG  *Config;

  if (IpaWidth == NULL) {
    return RETURN_INVALID_PARAMETER;
  }

  Config = AllocateAlignedPages (
             EFI_SIZE_TO_PAGES (sizeof (ARM_CCA_REALM_CONFIG)),
             ARM_CCA_REALM_GRANULE_SIZE
             );
  if (Config == NULL) {
    ASSERT (0);
    return RETURN_OUT_OF_RESOURCES;
  }

  ZeroMem (Config, sizeof (ARM_CCA_REALM_CONFIG));

  Status = ArmCcaRsiGetRealmConfig (Config);
  if (RETURN_ERROR (Status)) {
    ASSERT (0);
    goto ExitHandler;
  }

  if (Config->IpaWidth > ArmGetPhysicalAddressBits ()) {
    ASSERT (0);
    Status = RETURN_INVALID_PARAMETER;
    goto ExitHandler;
  }

  *IpaWidth = Config->IpaWidth;

ExitHandler:
  FreeAlignedPages (Config, EFI_SIZE_TO_PAGES (sizeof (ARM_CCA_REALM_CONFIG)));
  return Status;
}

/**
  Return the IPA width of the Realm.

  The IPA width of the Realm is used to configure the protection attribute
  for memory regions, see ArmCcaSetMemoryProtectionAttribute().

    @param [out] IpaWidth  IPA width of the Realm.

    @retval RETURN_SUCCESS            Success.
    @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
    @retval RETURN_UNSUPPORTED        The execution is not within the
                                      context of a Realm.
    @retval RETURN_OUT_OF_RESOURCES   Out of resources.
**/
RETURN_STATUS
EFIAPI
ArmCcaGetIpaWidth (
  OUT UINT64  *IpaWidth
  )
{
  RETURN_STATUS  Status;
  VOID           *HobList;
  VOID           *Hob;
  UINT64         *CcaIpaWidth;

  if (IpaWidth == NULL) {
    return RETURN_INVALID_PARAMETER;
  }

  if (!ArmCcaIsRealm ()) {
    *IpaWidth = 0;
    return RETURN_UNSUPPORTED;
  }

  HobList = GetHobList ();
  if (HobList == NULL) {
    // HOB not initialised.
    return ArmCcaReadIpaWidth (IpaWidth);
  }

  Hob = GetFirstGuidHob (&gArmCcaIpaWidthGuid);
  if ((Hob == NULL) ||
      (GET_GUID_HOB_DATA_SIZE (Hob) < sizeof (UINT64)))
  {
    // The gArmCcaIpaWidthGuid is created in the PlatformPeim() which
    // may not have been called for caching the IpaWidth value yet.
    // So, read the IPA Width value.
    Status = ArmCcaReadIpaWidth (IpaWidth);
    if (RETURN_ERROR (Status)) {
      ASSERT (0);
      return Status;
    }
  } else {
    // Read the value from the HOB.
    CcaIpaWidth = GET_GUID_HOB_DATA (Hob);
    if ((UINT64)*CcaIpaWidth == 0) {
      return RETURN_NOT_FOUND;
    }

    *IpaWidth = *CcaIpaWidth;
  }

  return RETURN_SUCCESS;
}

/** Check if the address range is protected MMIO

  @param [in]   BaseAddress      Base address of the device MMIO region.
  @param [in]   Length           Length of the device MMIO region.
  @param [out]  IsProtectedMmio  TRUE - if the RIPAS for the address range is
                                        protected MMIO.
                                 FALSE - if the RIPAS for the address range is
                                         not protected MMIO.

  @retval RETURN_SUCCESS            Success.
  @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
  @retval RETURN_UNSUPPORTED        The request is not initiated in a
                                    Realm.
**/
RETURN_STATUS
EFIAPI
ArmCcaMemRangeIsProtectedMmio (
  IN  UINT64   BaseAddress,
  IN  UINT64   Length,
  OUT BOOLEAN  *IsProtectedMmio
  )
{
  RETURN_STATUS  RetStatus;
  UINT64         *Base;
  UINT64         *Top;
  UINT64         *End;
  ARM_CCA_RIPAS  State;

  if ((Length == 0) || (IsProtectedMmio == NULL)) {
    return RETURN_INVALID_PARAMETER;
  }

  Base = (UINT64 *)BaseAddress;
  End  = (UINT64 *)(BaseAddress + Length);

  while (Base < End) {
    Top       = End;
    RetStatus = ArmCcaRsiGetIpaState (
                  Base,
                  &Top,
                  &State
                  );
    if (RETURN_ERROR (RetStatus)) {
      // An error occurred.
      return RetStatus;
    }

    if (State != RipasDev) {
      // The address range is not protected MMIO.
      *IsProtectedMmio = FALSE;
      return RetStatus;
    }

    Base = Top;
  } // while

  *IsProtectedMmio = TRUE;
  return RETURN_SUCCESS;
}