summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBridgeLib.c
blob: 26cd435adf5bc1950ade672dd3039e13a2a03b31 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**@file
  Hardware info library with types and accessors to parse information about
  PCI host bridges.

  Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Library/DebugLib.h>

#include "HardwareInfoPciHostBridgeLib.h"

#define IS_RANGE_INVALID(Start, Size, MaxValue)  (Start >= MaxValue || Size == 0)

/**
  Calculate the last (inclusive) address of a range.

  @param[in] Start  First address of the range
  @param[in] Size   Size of the range
  @return Last address of the range
**/
STATIC
UINT64
GetRangeEnd (
  IN  UINT64  Start,
  IN  UINT64  Size,
  IN  UINT64  MaxValue
  )
{
  if (IS_RANGE_INVALID (Start, Size, MaxValue)) {
    return 0;
  }

  return Start + Size - 1;
}

/**
  Internal helper to update LastAddress if the Limit address
  of the Mem aperture is higher than the provided value.

  @param[in]  Mem           Pointer to aperture whose limit is
                            to be compared against accumulative
                            last address.
  @param[out] LastAddress   Pointer to accumulative last address
                            to be updated if Limit is higher
**/
STATIC
VOID
UpdateLastAddressIfHigher (
  IN  PCI_ROOT_BRIDGE_APERTURE  *Mem,
  OUT UINT64                    *LastAddress
  )
{
  if (Mem->Limit > *LastAddress) {
    *LastAddress = Mem->Limit;
  }
}

EFI_STATUS
HardwareInfoPciHostBridgeLastMmioAddress (
  IN CONST  HOST_BRIDGE_INFO  *HostBridge,
  IN        UINTN             DataSize,
  IN        BOOLEAN           HighMem,
  OUT       UINT64            *LastMmioAddress
  )
{
  EFI_STATUS                Status;
  PCI_ROOT_BRIDGE_APERTURE  Mem;
  PCI_ROOT_BRIDGE_APERTURE  MemAbove4G;
  PCI_ROOT_BRIDGE_APERTURE  PMem;
  PCI_ROOT_BRIDGE_APERTURE  PMemAbove4G;

  if (LastMmioAddress == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Set the output to the lowest possible value so that if some step fails
  // the overall outcome reflects no information found
  //
  *LastMmioAddress = 0;

  Status = HardwareInfoPciHostBridgeGetApertures (
             HostBridge,
             DataSize,
             NULL,
             &Mem,
             &MemAbove4G,
             &PMem,
             &PMemAbove4G,
             NULL
             );

  //
  // Forward error to caller but ignore warnings given that, very likely,
  // the host bridge will have a PIO aperture we are explicitly
  // ignoring here since we care only about MMIO resources.
  //
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if (HighMem) {
    UpdateLastAddressIfHigher (&MemAbove4G, LastMmioAddress);
    UpdateLastAddressIfHigher (&PMemAbove4G, LastMmioAddress);
  } else {
    UpdateLastAddressIfHigher (&Mem, LastMmioAddress);
    UpdateLastAddressIfHigher (&PMem, LastMmioAddress);
  }

  return EFI_SUCCESS;
}

/**
  Set the boundaries of an aperture to invalid values having
  size zero and start MaxValue (yields Start > Limit which
  depicts an invalid range)

  @param[in]  MaxValue    Max value of the aperture's range (depends
                          on the data type)
  @param[out] Aperture    Aperture object to invalidate
**/
STATIC
VOID
InvalidateRootBridgeAperture (
  OUT PCI_ROOT_BRIDGE_APERTURE  *Aperture
  )
{
  if (Aperture == NULL) {
    return;
  }

  Aperture->Base  = MAX_UINT64;
  Aperture->Limit = 0;
}

/**
  Fill a PCI ROOT BRIDGE APERTURE with the proper values calculated
  from the provided start and size.

  @param[in]  Start       Start address of the aperture
  @param[in]  Size        Size, in bytes, of the aperture
  @param[in]  MaxValue    Max value a valid address could take and which
                          represents an invalid start address.
  @param[out] Aperture    Pointer to the aperture to be filled

  @retval EFI_SUCCESS                 Aperture was filled successfully
  @retval EFI_INVALID_PARAMETER       Range depicted by Start and Size is
                                      valid but ignored because aperture
                                      pointer is NULL
  @retval EFI_WARN_BUFFER_TOO_SMALL   Aperture pointer is invalid but the
                                      range also is so no harm.
**/
STATIC
EFI_STATUS
FillHostBridgeAperture (
  IN  UINT64                    Start,
  IN  UINT64                    Size,
  IN  UINT64                    MaxValue,
  OUT PCI_ROOT_BRIDGE_APERTURE  *Aperture
  )
{
  UINT64  End;

  End = GetRangeEnd (Start, Size, MaxValue);

  if (Aperture == NULL) {
    if (!IS_RANGE_INVALID (Start, Size, MaxValue)) {
      //
      // Report an error to the caller since the range specified in
      // the host bridge's resources is non-empty but the provided
      // aperture pointer is null, thus the valid range is ignored.
      //
      return EFI_INVALID_PARAMETER;
    }

    return EFI_WARN_BUFFER_TOO_SMALL;
  }

  if (IS_RANGE_INVALID (Start, Size, MaxValue)) {
    //
    // Fill Aperture with invalid range values to signal the
    // absence of an address space (empty range)
    //
    InvalidateRootBridgeAperture (Aperture);
  } else {
    Aperture->Base  = Start;
    Aperture->Limit = End;
  }

  return EFI_SUCCESS;
}

/**
  Merge 2 ranges (normal and prefetchable) into a single aperture
  comprehending the addresses encompassed by both of them. If both
  ranges are not empty they must be contiguous for correctness.

  @param[in]  Start       Range start address
  @param[in]  Size        Range size in bytes
  @param[in]  PStart      Prefetchable range start address
  @param[in]  PSize       Prefetchable range size in bytes
  @param[in]  MaxValue    Max value a valid address could take and which
                          represents an invalid start address.
  @param[out] Aperture    Pointer to the aperture to be filled

  @retval EFI_SUCCESS                 Aperture was filled successfully
  @retval EFI_INVALID_PARAMETER       Either range depicted by Start, Size
                                      or PStart, PSize or both are valid
                                      but ignored because aperture pointer
                                      is NULL
  @retval EFI_WARN_BUFFER_TOO_SMALL   Aperture pointer is invalid but both
                                      ranges are too so no harm.
**/
STATIC
EFI_STATUS
MergeHostBridgeApertures (
  IN  UINT64                    Start,
  IN  UINT64                    Size,
  IN  UINT64                    PStart,
  IN  UINT64                    PSize,
  IN  UINT64                    MaxValue,
  OUT PCI_ROOT_BRIDGE_APERTURE  *Aperture
  )
{
  UINT64  PEnd;

  if (Aperture == NULL) {
    if (!IS_RANGE_INVALID (Start, Size, MaxValue) ||
        !IS_RANGE_INVALID (PStart, PSize, MaxValue))
    {
      //
      // Report an error to the caller since the range specified in
      // the host bridge's resources is non-empty but the provided
      // aperture pointer is null, thus the valid range is ignored.
      //
      return EFI_INVALID_PARAMETER;
    }

    return EFI_WARN_BUFFER_TOO_SMALL;
  }

  //
  // Start from an empty range (Limit < Base)
  //
  InvalidateRootBridgeAperture (Aperture);

  if (!IS_RANGE_INVALID (Start, Size, MaxValue)) {
    Aperture->Base  = Start;
    Aperture->Limit = Start + Size - 1;
  }

  if (!IS_RANGE_INVALID (PStart, PSize, MaxValue)) {
    PEnd = PStart + PSize - 1;

    if (PStart < Aperture->Base) {
      Aperture->Base = PStart;
    }

    if (PEnd > Aperture->Limit) {
      Aperture->Limit = PEnd;
    }
  }

  return EFI_SUCCESS;
}

EFI_STATUS
HardwareInfoPciHostBridgeGetBusNrRange (
  IN CONST  HOST_BRIDGE_INFO  *HostBridge,
  IN        UINTN             DataSize,
  OUT       UINTN             *BusNrStart,
  OUT       UINTN             *BusNrLast
  )
{
  if ((HostBridge == NULL) || (DataSize == 0) ||
      (BusNrStart == NULL) || (BusNrLast == NULL))
  {
    return EFI_INVALID_PARAMETER;
  }

  //
  // For now only version 0 is supported
  //
  if (HostBridge->Version != 0) {
    return EFI_INCOMPATIBLE_VERSION;
  }

  *BusNrStart = HostBridge->BusNrStart;
  *BusNrLast  = HostBridge->BusNrLast;

  return EFI_SUCCESS;
}

EFI_STATUS
HardwareInfoPciHostBridgeGetApertures (
  IN CONST  HOST_BRIDGE_INFO          *HostBridge,
  IN        UINTN                     DataSize,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *Io,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *Mem,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *MemAbove4G,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *PMem,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *PMemAbove4G,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *PcieConfig
  )
{
  EFI_STATUS  Status;
  BOOLEAN     StickyError;

  StickyError = FALSE;
  if ((HostBridge == NULL) || (DataSize == 0)) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // For now only version 0 is supported
  //
  if (HostBridge->Version != 0) {
    return EFI_INCOMPATIBLE_VERSION;
  }

  Status = FillHostBridgeAperture (
             HostBridge->IoStart,
             HostBridge->IoSize,
             MAX_UINT32,
             Io
             );
  StickyError |= EFI_ERROR (Status);

  Status = FillHostBridgeAperture (
             HostBridge->PcieConfigStart,
             HostBridge->PcieConfigSize,
             MAX_UINT64,
             PcieConfig
             );
  StickyError |= EFI_ERROR (Status);

  if (HostBridge->Flags.Bits.CombineMemPMem) {
    Status = MergeHostBridgeApertures (
               HostBridge->MemStart,
               HostBridge->MemSize,
               HostBridge->PMemStart,
               HostBridge->PMemSize,
               MAX_UINT32,
               Mem
               );
    StickyError |= EFI_ERROR (Status);

    Status = MergeHostBridgeApertures (
               HostBridge->MemAbove4GStart,
               HostBridge->MemAbove4GSize,
               HostBridge->PMemAbove4GStart,
               HostBridge->PMemAbove4GSize,
               MAX_UINT64,
               MemAbove4G
               );
    StickyError |= EFI_ERROR (Status);

    //
    // Invalidate unused apertures
    //
    InvalidateRootBridgeAperture (PMem);
    InvalidateRootBridgeAperture (PMemAbove4G);
  } else {
    Status = FillHostBridgeAperture (
               HostBridge->MemStart,
               HostBridge->MemSize,
               MAX_UINT32,
               Mem
               );
    StickyError |= EFI_ERROR (Status);

    Status = FillHostBridgeAperture (
               HostBridge->PMemStart,
               HostBridge->PMemSize,
               MAX_UINT32,
               PMem
               );
    StickyError |= EFI_ERROR (Status);

    Status = FillHostBridgeAperture (
               HostBridge->MemAbove4GStart,
               HostBridge->MemAbove4GSize,
               MAX_UINT64,
               MemAbove4G
               );
    StickyError |= EFI_ERROR (Status);

    Status = FillHostBridgeAperture (
               HostBridge->PMemAbove4GStart,
               HostBridge->PMemAbove4GSize,
               MAX_UINT64,
               PMem
               );
    StickyError |= EFI_ERROR (Status);
  }

  if (StickyError) {
    //
    // If any function returned an error it is due to a valid range
    // specified in the host bridge that was ignored due to a NULL
    // pointer. Translate it to a warning to allow for calling with
    // only a subset of the apertures.
    //
    return EFI_WARN_STALE_DATA;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
HardwareInfoPciHostBridgeGetFlags (
  IN CONST  HOST_BRIDGE_INFO  *HostBridge,
  IN        UINTN             DataSize,
  OUT       UINT64            *Attributes               OPTIONAL,
  OUT       BOOLEAN           *DmaAbove4G               OPTIONAL,
  OUT       BOOLEAN           *NoExtendedConfigSpace    OPTIONAL,
  OUT       BOOLEAN           *CombineMemPMem           OPTIONAL
  )
{
  if ((HostBridge == NULL) || (DataSize == 0)) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // For now only version 0 is supported
  //
  if (HostBridge->Version != 0) {
    return EFI_INCOMPATIBLE_VERSION;
  }

  if (Attributes) {
    *Attributes = HostBridge->Attributes;
  }

  if (DmaAbove4G) {
    *DmaAbove4G = !!HostBridge->Flags.Bits.DmaAbove4G;
  }

  if (NoExtendedConfigSpace) {
    *NoExtendedConfigSpace = !!HostBridge->Flags.Bits.NoExtendedConfigSpace;
  }

  if (CombineMemPMem) {
    *CombineMemPMem = !!HostBridge->Flags.Bits.CombineMemPMem;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
HardwareInfoPciHostBridgeGet (
  IN CONST  HOST_BRIDGE_INFO          *HostBridge,
  IN        UINTN                     DataSize,
  OUT       UINTN                     *BusNrStart,
  OUT       UINTN                     *BusNrLast,
  OUT       UINT64                    *Attributes               OPTIONAL,
  OUT       BOOLEAN                   *DmaAbove4G               OPTIONAL,
  OUT       BOOLEAN                   *NoExtendedConfigSpace    OPTIONAL,
  OUT       BOOLEAN                   *CombineMemPMem           OPTIONAL,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *Io                       OPTIONAL,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *Mem                      OPTIONAL,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *MemAbove4G               OPTIONAL,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *PMem                     OPTIONAL,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *PMemAbove4G              OPTIONAL,
  OUT       PCI_ROOT_BRIDGE_APERTURE  *PcieConfig               OPTIONAL
  )
{
  EFI_STATUS  Status;

  Status = HardwareInfoPciHostBridgeGetBusNrRange (
             HostBridge,
             DataSize,
             BusNrStart,
             BusNrLast
             );

  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = HardwareInfoPciHostBridgeGetFlags (
             HostBridge,
             DataSize,
             Attributes,
             DmaAbove4G,
             NoExtendedConfigSpace,
             CombineMemPMem
             );

  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = HardwareInfoPciHostBridgeGetApertures (
             HostBridge,
             DataSize,
             Io,
             Mem,
             MemAbove4G,
             PMem,
             PMemAbove4G,
             PcieConfig
             );

  return Status;
}