summaryrefslogtreecommitdiff
path: root/DynamicTablesPkg/Library/FdtHwInfoParserLib/Arm/Iort/RootComplexParser.c
blob: 5441b784996cedb416ec47ec4fc30a8ed45d9aad (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/** @file
  Root Complex parser.

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

  @par Reference(s):
  - linux/Documentation/devicetree/bindings/pci/host-generic-pci.yaml
**/

#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/FdtLib.h>
#include <IndustryStandard/IoRemappingTable.h>
#include "FdtHwInfoParser.h"
#include "CmObjectDescUtility.h"
#include "Arm/Iort/ArmIortParser.h"
#include "Arm/Iort/RootComplexParser.h"

#define IOMMU_MAP_CELL_COUNT  4

/** List of "compatible" property values for PciRootComplex nodes.

  Other "compatible" values are not supported by this module.
*/
STATIC CONST COMPATIBILITY_STR  RootComplexCompatibleStr[] = {
  { "pci-host-ecam-generic" }
};

/** PciRootComplex compatiblity information.
*/
STATIC CONST COMPATIBILITY_INFO  RootComplexCompatibleInfo = {
  ARRAY_SIZE (RootComplexCompatibleStr),
  RootComplexCompatibleStr
};

/** Determine the memory address size for the given root complex

  Determine the memory address size based on the dma-ranges property. If this
  property is not present then default to 32 bits.

  @param [in]  Fdt                Pointer to FDT
  @param [in]  RootComplexNode    Offset of root complex node
  @param [out] MemoryAddressSize  Location to store memory address size

  @retval EFI_SUCCESS             The function completed successfully.
  @retval EFI_INVALID_PARAMETER   Invalid parameter.
**/
STATIC
EFI_STATUS
RootComplexNodeGetMemoryAddressSize (
  IN  CONST VOID   *Fdt,
  IN  CONST INT32  RootComplexNode,
  OUT       UINT8  *MemoryAddressSize
  )
{
  CONST UINT32  *DmaRanges;
  INT32         ParentNode;
  INT32         DataSize;
  INT32         AddressCells;
  INT32         SizeCells;
  INT32         ParentAddressCells;
  INT32         Stride;
  INTN          DataOffset;
  INTN          SizeOffset;
  UINT64        DmaAddressEnd;
  UINT64        DmaAddress;
  UINT64        Size;

  DmaRanges = FdtGetProp (Fdt, RootComplexNode, "dma-ranges", &DataSize);
  if ((DmaRanges == NULL) || (DataSize <= 0)) {
    *MemoryAddressSize = 32;
    return 0;
  }

  AddressCells = FdtAddressCells (Fdt, RootComplexNode);
  if (AddressCells < 0) {
    ASSERT (AddressCells >= 0);
    return EFI_INVALID_PARAMETER;
  }

  SizeCells = FdtSizeCells (Fdt, RootComplexNode);
  if (SizeCells < 0) {
    ASSERT (SizeCells >= 0);
    return EFI_INVALID_PARAMETER;
  }

  // Find parent node with #address-cells
  ParentNode = FdtParentOffset (Fdt, RootComplexNode);
  if (ParentNode < 0) {
    ASSERT (ParentNode >= 0);
    return EFI_INVALID_PARAMETER;
  }

  ParentAddressCells = -1;

  do {
    ParentAddressCells = FdtAddressCells (Fdt, ParentNode);
    if (ParentAddressCells < 0) {
      ParentNode = FdtParentOffset (Fdt, ParentNode);
      if (ParentNode < 0) {
        ASSERT (ParentNode >= 0);
        return EFI_INVALID_PARAMETER;
      }
    }
  } while (ParentAddressCells < 0);

  Stride     = AddressCells + ParentAddressCells + SizeCells;
  SizeOffset = AddressCells + ParentAddressCells;
  if ((DataSize < Stride) || ((DataSize % Stride) != 0)) {
    ASSERT (DataSize >= Stride && (DataSize % Stride) == 0);
    return EFI_INVALID_PARAMETER;
  }

  DataOffset    = 0;
  DmaAddressEnd = 0;

  // Walk dma-ranges and find maximum DMA address
  while ((DataOffset + Stride) <= DataSize) {
    switch (AddressCells) {
      case 1:
        DmaAddress = Fdt32ToCpu (DmaRanges[DataOffset]);
        break;
      case 2:
        DmaAddress = Fdt64ToCpu (*(UINT64 *)&DmaRanges[DataOffset]);
        break;
      case 3:
        DmaAddress = Fdt64ToCpu (*(UINT64 *)&DmaRanges[DataOffset + 1]);
        break;
      default:
        ASSERT (0);
        return EFI_INVALID_PARAMETER;
    }

    switch (SizeCells) {
      case 1:
        Size = Fdt32ToCpu (DmaRanges[DataOffset + SizeOffset]);
        break;
      case 2:
        Size = Fdt64ToCpu (*(UINT64 *)&DmaRanges[DataOffset + SizeOffset]);
        break;
      default:
        ASSERT (0);
        return EFI_INVALID_PARAMETER;
    }

    if (DmaAddressEnd < (DmaAddress + Size)) {
      DmaAddressEnd = DmaAddress + Size;
    }

    DataOffset += Stride;
  }

  // Round up if required
  if (DmaAddressEnd & (DmaAddressEnd - 1)) {
    *MemoryAddressSize = HighBitSet64 (DmaAddressEnd) + 2;
  } else {
    *MemoryAddressSize = HighBitSet64 (DmaAddressEnd) + 1;
  }

  return EFI_SUCCESS;
}

/** Parse a PCI root complex node.

  @param [in]  FdtParserHandle    A handle to the parser instance.
  @param [in]  Fdt                Pointer to a Flattened Device Tree (Fdt).
  @param [in]  RootComplexNode    Offset of a root complex node.
  @param [in]  RootComplexInfo    The CM_ARM_ROOT_COMPLEX_NODE to populate.

  @retval EFI_SUCCESS             The function completed successfully.
  @retval EFI_ABORTED             An error occurred.
  @retval EFI_INVALID_PARAMETER   Invalid parameter.
**/
STATIC
EFI_STATUS
EFIAPI
RootComplexNodeParser (
  IN  CONST FDT_HW_INFO_PARSER_HANDLE  FdtParserHandle,
  IN  CONST VOID                       *Fdt,
  IN        INT32                      RootComplexNode,
  IN        CM_ARM_ROOT_COMPLEX_NODE   *RootComplexInfo
  )
{
  EFI_STATUS         Status;
  CONST UINT32       *Data;
  INT32              DataSize;
  CM_ARM_ID_MAPPING  *IdMappings;
  INT32              MapIndex;

  if ((Fdt == NULL) || (RootComplexInfo == NULL)) {
    ASSERT ((Fdt != NULL) && (RootComplexInfo != NULL));
    return EFI_INVALID_PARAMETER;
  }

  /// Memory access properties : Cache coherent attributes
  /// Memory access properties : Memory access flags
  Data = FdtGetProp (Fdt, RootComplexNode, "dma-coherent", &DataSize);
  if ((Data != NULL) && (DataSize >= 0)) {
    RootComplexInfo->CacheCoherent     = EFI_ACPI_IORT_MEM_ACCESS_PROP_CCA;
    RootComplexInfo->MemoryAccessFlags = EFI_ACPI_IORT_MEM_ACCESS_FLAGS_CPM |
                                         EFI_ACPI_IORT_MEM_ACCESS_FLAGS_DACS;
  } else {
    RootComplexInfo->CacheCoherent     = 0;
    RootComplexInfo->MemoryAccessFlags = 0;
  }

  /// Memory access properties : Allocation hints
  RootComplexInfo->AllocationHints = 0;

  /// ATS attributes
  Data = FdtGetProp (Fdt, RootComplexNode, "ats-supported", &DataSize);
  if ((Data != NULL) && (DataSize >= 0)) {
    RootComplexInfo->AtsAttribute = EFI_ACPI_IORT_ROOT_COMPLEX_ATS_SUPPORTED;
  } else {
    RootComplexInfo->AtsAttribute = EFI_ACPI_IORT_ROOT_COMPLEX_ATS_UNSUPPORTED;
  }

  /// PCI segment number
  Data = FdtGetProp (Fdt, RootComplexNode, "linux,pci-domain", &DataSize);
  if (Data != NULL) {
    RootComplexInfo->PciSegmentNumber = *Data;
  }

  /// Memory address size limit
  Status = RootComplexNodeGetMemoryAddressSize (Fdt, RootComplexNode, &RootComplexInfo->MemoryAddressSize);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  /// PASID capabilities
  RootComplexInfo->PasidCapabilities = 0;

  /// Flags
  RootComplexInfo->Flags = 0;

  /// Unique identifier for this node.
  RootComplexInfo->Identifier = GetNextIortIdentifier ();

  Data = FdtGetProp (Fdt, RootComplexNode, "iommu-map", &DataSize);
  if (Data == NULL) {
    // Some guests (e.g. kvmtool) do not provide a SMMU, so the PCI node
    // in the DTB lacks an 'iommu-map'. In such cases set the Number of
    // ID mappings to 0.
    RootComplexInfo->IdMappingCount = 0;
  } else {
    if (((DataSize % (IOMMU_MAP_CELL_COUNT * sizeof (UINT32))) != 0)) {
      // If error or invalid number of cells (not multiple of IOMMU_MAP_CELL_COUNT).
      ASSERT ((DataSize % (IOMMU_MAP_CELL_COUNT * sizeof (UINT32))) == 0);
      return EFI_ABORTED;
    }

    DataSize /= IOMMU_MAP_CELL_COUNT * sizeof (UINT32);

    IdMappings = AllocateZeroPool (DataSize * sizeof (CM_ARM_ID_MAPPING));
    if (IdMappings == NULL) {
      ASSERT (IdMappings != NULL);
      return EFI_OUT_OF_RESOURCES;
    }

    for (MapIndex = 0; MapIndex < DataSize; MapIndex++) {
      IdMappings[MapIndex].InputBase            = Fdt32ToCpu (((UINT32 *)Data)[MapIndex * IOMMU_MAP_CELL_COUNT]);
      IdMappings[MapIndex].NumIds               = Fdt32ToCpu (((UINT32 *)Data)[MapIndex * IOMMU_MAP_CELL_COUNT + 3]);
      IdMappings[MapIndex].OutputBase           = Fdt32ToCpu (((UINT32 *)Data)[MapIndex * IOMMU_MAP_CELL_COUNT + 2]);
      IdMappings[MapIndex].OutputReferenceToken = CM_ABSTRACT_TOKEN_MAKE (
                                                    ETokenNameSpaceFdtHwInfo,
                                                    EFdtHwInfoIortObject,
                                                    Fdt32ToCpu (((UINT32 *)Data)[MapIndex * IOMMU_MAP_CELL_COUNT + 1])
                                                    );
    }

    // Add the CmObj to the Configuration Manager.
    Status = AddSingleCmObjArray (
               FdtParserHandle,
               CREATE_CM_ARM_OBJECT_ID (EArmObjIdMappingArray),
               IdMappings,
               sizeof (CM_ARM_ID_MAPPING) * DataSize,
               DataSize,
               &RootComplexInfo->IdMappingToken
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      FreePool (IdMappings);
      return Status;
    }

    /// Number of ID mappings
    RootComplexInfo->IdMappingCount = DataSize;
  }

  return EFI_SUCCESS;
}

/** CM_ARM_ROOT_COMPLEX_NODE parser function.

  The following structure is populated:
  typedef struct CmArmRootComplexNode {
    CM_OBJECT_TOKEN    Token;
    UINT32             IdMappingCount;          // {Populated}
    CM_OBJECT_TOKEN    IdMappingToken;          // {Populated}
    UINT32             CacheCoherent;           // {Populated}
    UINT8              AllocationHints;         // {default = 0}
    UINT8              MemoryAccessFlags;       // {Populated}
    UINT32             AtsAttribute;            // {Populated}
    UINT32             PciSegmentNumber;        // {Populated}
    UINT8              MemoryAddressSize;       // {Populated}
    UINT16             PasidCapabilities;       // {default = 0}
    UINT32             Flags;                   // {default = 0}
    UINT32             Identifier;              // {Populated}
  } CM_ARM_ROOT_COMPLEX_NODE;

  A parser parses a Device Tree to populate a specific CmObj type. None,
  one or many CmObj can be created by the parser.
  The created CmObj are then handed to the parser's caller through the
  HW_INFO_ADD_OBJECT interface.
  This can also be a dispatcher. I.e. a function that not parsing a
  Device Tree but calling other parsers.

  @param [in]  FdtParserHandle A handle to the parser instance.
  @param [in]  FdtBranch       When searching for DT node name, restrict
                               the search to this Device Tree branch.

  @retval EFI_SUCCESS             The function completed successfully.
  @retval EFI_ABORTED             An error occurred.
  @retval EFI_INVALID_PARAMETER   Invalid parameter.
  @retval EFI_NOT_FOUND           Not found.
  @retval EFI_UNSUPPORTED         Unsupported.
**/
EFI_STATUS
EFIAPI
ArmPciRootComplexParser (
  IN  CONST FDT_HW_INFO_PARSER_HANDLE  FdtParserHandle,
  IN        INT32                      FdtBranch
  )
{
  EFI_STATUS                Status;
  UINT32                    Index;
  INT32                     RootComplexNode;
  UINT32                    RootComplexNodeCount;
  CM_ARM_ROOT_COMPLEX_NODE  RootComplexInfo;
  VOID                      *Fdt;

  if (FdtParserHandle == NULL) {
    ASSERT (FdtParserHandle != NULL);
    return EFI_INVALID_PARAMETER;
  }

  Fdt    = FdtParserHandle->Fdt;
  Status = FdtCountCompatNodeInBranch (
             Fdt,
             FdtBranch,
             &RootComplexCompatibleInfo,
             &RootComplexNodeCount
             );
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  if (RootComplexNodeCount == 0) {
    return EFI_NOT_FOUND;
  }

  // Parse each root complex node in the branch.
  RootComplexNode = FdtBranch;
  for (Index = 0; Index < RootComplexNodeCount; Index++) {
    ZeroMem (&RootComplexInfo, sizeof (CM_ARM_ROOT_COMPLEX_NODE));

    Status = FdtGetNextCompatNodeInBranch (
               Fdt,
               FdtBranch,
               &RootComplexCompatibleInfo,
               &RootComplexNode
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      if (Status == EFI_NOT_FOUND) {
        // Should have found the node.
        Status = EFI_ABORTED;
      }

      return Status;
    }

    Status = RootComplexNodeParser (FdtParserHandle, Fdt, RootComplexNode, &RootComplexInfo);
    if (EFI_ERROR (Status)) {
      if (Status != EFI_NOT_FOUND) {
        ASSERT_EFI_ERROR (Status);
      }

      return Status;
    }

    // Add the CmObj to the Configuration Manager.
    Status = AddSingleCmObj (
               FdtParserHandle,
               CREATE_CM_ARM_OBJECT_ID (EArmObjRootComplex),
               &RootComplexInfo,
               sizeof (CM_ARM_ROOT_COMPLEX_NODE),
               NULL
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }
  } // for

  return Status;
}