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
509
510
511
512
513
514
515
516
517
518
519
520
|
/** @file
This driver implements EFI_PCI_HOT_PLUG_INIT_PROTOCOL, providing the PCI bus
driver with resource padding information, for PCIe hotplug purposes.
Copyright (C) 2016, Red Hat, Inc.
This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this
distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <IndustryStandard/Acpi10.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/PciHotPlugInit.h>
#include <Protocol/PciRootBridgeIo.h>
//
// The protocol interface this driver produces.
//
// Refer to 12.6 "PCI Hot Plug PCI Initialization Protocol" in the Platform
// Init 1.4a Spec, Volume 5.
//
STATIC EFI_PCI_HOT_PLUG_INIT_PROTOCOL mPciHotPlugInit;
//
// Resource padding template for the GetResourcePadding() protocol member
// function.
//
// Refer to Table 8 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
// the Platform Init 1.4a Spec, Volume 5.
//
// This structure is interpreted by the ApplyResourcePadding() function in the
// edk2 PCI Bus UEFI_DRIVER.
//
// We can request padding for at most four resource types, each of which is
// optional, independently of the others:
// (a) bus numbers,
// (b) IO space,
// (c) non-prefetchable MMIO space (32-bit only),
// (d) prefetchable MMIO space (either 32-bit or 64-bit, never both).
//
#pragma pack (1)
typedef struct {
EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR Padding[4];
EFI_ACPI_END_TAG_DESCRIPTOR EndDesc;
} RESOURCE_PADDING;
#pragma pack ()
/**
Initialize a RESOURCE_PADDING object.
@param[out] ResourcePadding The caller-allocated RESOURCE_PADDING object to
initialize.
**/
STATIC
VOID
InitializeResourcePadding (
OUT RESOURCE_PADDING *ResourcePadding
)
{
UINTN Index;
ZeroMem (ResourcePadding, sizeof *ResourcePadding);
//
// Fill in the Padding fields that don't vary across resource types.
//
for (Index = 0; Index < ARRAY_SIZE (ResourcePadding->Padding); ++Index) {
EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;
Descriptor = ResourcePadding->Padding + Index;
Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
Descriptor->Len = (UINT16)(
sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -
OFFSET_OF (
EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,
ResType
)
);
}
//
// Fill in the End Tag.
//
ResourcePadding->EndDesc.Desc = ACPI_END_TAG_DESCRIPTOR;
}
/**
Set up a descriptor entry for reserving IO space.
@param[in,out] Descriptor The descriptor to configure. The caller shall have
initialized Descriptor earlier, with
InitializeResourcePadding().
@param[in] SizeExponent The size and natural alignment of the reservation
are determined by raising two to this power.
**/
STATIC
VOID
SetIoPadding (
IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,
IN UINTN SizeExponent
)
{
Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;
Descriptor->AddrLen = LShiftU64 (1, SizeExponent);
Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;
}
/**
Set up a descriptor entry for reserving MMIO space.
@param[in,out] Descriptor The descriptor to configure. The caller shall
have initialized Descriptor earlier, with
InitializeResourcePadding().
@param[in] Prefetchable TRUE if the descriptor should reserve
prefetchable MMIO space. Pass FALSE for
reserving non-prefetchable MMIO space.
@param[in] ThirtyTwoBitOnly TRUE if the reservation should be limited to
32-bit address space. FALSE if the reservation
can be satisfied from 64-bit address space.
ThirtyTwoBitOnly is ignored if Prefetchable is
FALSE; in that case ThirtyTwoBitOnly is always
considered TRUE.
@param[in] SizeExponent The size and natural alignment of the
reservation are determined by raising two to
this power.
**/
STATIC
VOID
SetMmioPadding (
IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,
IN BOOLEAN Prefetchable,
IN BOOLEAN ThirtyTwoBitOnly,
IN UINTN SizeExponent
)
{
Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
if (Prefetchable) {
Descriptor->SpecificFlag =
EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;
Descriptor->AddrSpaceGranularity = ThirtyTwoBitOnly ? 32 : 64;
} else {
Descriptor->SpecificFlag =
EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_NON_CACHEABLE;
Descriptor->AddrSpaceGranularity = 32;
}
Descriptor->AddrLen = LShiftU64 (1, SizeExponent);
Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;
}
/**
Round up a positive 32-bit value to the next whole power of two, and return
the bit position of the highest bit set in the result. Equivalent to
ceil(log2(x)).
@param[in] Operand The 32-bit operand to evaluate.
@retval -1 Operand is zero.
@retval -1 Operand is positive, not a whole power of two, and rounding it
up to the next power of two does not fit into 32 bits.
@retval 0..31 Otherwise, return ceil(log2(Value)).
**/
STATIC
INTN
HighBitSetRoundUp32 (
IN UINT32 Operand
)
{
INTN HighBit;
HighBit = HighBitSet32 (Operand);
if (HighBit == -1) {
//
// Operand is zero.
//
return HighBit;
}
if ((Operand & (Operand - 1)) != 0) {
//
// Operand is not a whole power of two.
//
++HighBit;
}
return (HighBit < 32) ? HighBit : -1;
}
/**
Round up a positive 64-bit value to the next whole power of two, and return
the bit position of the highest bit set in the result. Equivalent to
ceil(log2(x)).
@param[in] Operand The 64-bit operand to evaluate.
@retval -1 Operand is zero.
@retval -1 Operand is positive, not a whole power of two, and rounding it
up to the next power of two does not fit into 64 bits.
@retval 0..63 Otherwise, return ceil(log2(Value)).
**/
STATIC
INTN
HighBitSetRoundUp64 (
IN UINT64 Operand
)
{
INTN HighBit;
HighBit = HighBitSet64 (Operand);
if (HighBit == -1) {
//
// Operand is zero.
//
return HighBit;
}
if ((Operand & (Operand - 1)) != 0) {
//
// Operand is not a whole power of two.
//
++HighBit;
}
return (HighBit < 64) ? HighBit : -1;
}
/**
Returns a list of root Hot Plug Controllers (HPCs) that require
initialization during the boot process.
This procedure returns a list of root HPCs. The PCI bus driver must
initialize these controllers during the boot process. The PCI bus driver may
or may not be able to detect these HPCs. If the platform includes a
PCI-to-CardBus bridge, it can be included in this list if it requires
initialization. The HpcList must be self consistent. An HPC cannot control
any of its parent buses. Only one HPC can control a PCI bus. Because this
list includes only root HPCs, no HPC in the list can be a child of another
HPC. This policy must be enforced by the EFI_PCI_HOT_PLUG_INIT_PROTOCOL.
The PCI bus driver may not check for such invalid conditions. The callee
allocates the buffer HpcList
@param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
instance.
@param[out] HpcCount The number of root HPCs that were returned.
@param[out] HpcList The list of root HPCs. HpcCount defines the number of
elements in this list.
@retval EFI_SUCCESS HpcList was returned.
@retval EFI_OUT_OF_RESOURCES HpcList was not returned due to insufficient
resources.
@retval EFI_INVALID_PARAMETER HpcCount is NULL or HpcList is NULL.
**/
STATIC
EFI_STATUS
EFIAPI
GetRootHpcList (
IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,
OUT UINTN *HpcCount,
OUT EFI_HPC_LOCATION **HpcList
)
{
if (HpcCount == NULL || HpcList == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// There are no top-level (i.e., un-enumerable) hot-plug controllers in QEMU
// that would require special initialization.
//
*HpcCount = 0;
*HpcList = NULL;
return EFI_SUCCESS;
}
/**
Initializes one root Hot Plug Controller (HPC). This process may causes
initialization of its subordinate buses.
This function initializes the specified HPC. At the end of initialization,
the hot-plug slots or sockets (controlled by this HPC) are powered and are
connected to the bus. All the necessary registers in the HPC are set up. For
a Standard (PCI) Hot Plug Controller (SHPC), the registers that must be set
up are defined in the PCI Standard Hot Plug Controller and Subsystem
Specification.
@param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
instance.
@param[in] HpcDevicePath The device path to the HPC that is being
initialized.
@param[in] HpcPciAddress The address of the HPC function on the PCI bus.
@param[in] Event The event that should be signaled when the HPC
initialization is complete. Set to NULL if the
caller wants to wait until the entire
initialization process is complete.
@param[out] HpcState The state of the HPC hardware. The state is
EFI_HPC_STATE_INITIALIZED or
EFI_HPC_STATE_ENABLED.
@retval EFI_SUCCESS If Event is NULL, the specific HPC was
successfully initialized. If Event is not
NULL, Event will be signaled at a later time
when initialization is complete.
@retval EFI_UNSUPPORTED This instance of
EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
support the specified HPC.
@retval EFI_OUT_OF_RESOURCES Initialization failed due to insufficient
resources.
@retval EFI_INVALID_PARAMETER HpcState is NULL.
**/
STATIC
EFI_STATUS
EFIAPI
InitializeRootHpc (
IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,
IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,
IN UINT64 HpcPciAddress,
IN EFI_EVENT Event, OPTIONAL
OUT EFI_HPC_STATE *HpcState
)
{
//
// This function should never be called, due to the information returned by
// GetRootHpcList().
//
ASSERT (FALSE);
if (HpcState == NULL) {
return EFI_INVALID_PARAMETER;
}
return EFI_UNSUPPORTED;
}
/**
Returns the resource padding that is required by the PCI bus that is
controlled by the specified Hot Plug Controller (HPC).
This function returns the resource padding that is required by the PCI bus
that is controlled by the specified HPC. This member function is called for
all the root HPCs and nonroot HPCs that are detected by the PCI bus
enumerator. This function will be called before PCI resource allocation is
completed. This function must be called after all the root HPCs, with the
possible exception of a PCI-to-CardBus bridge, have completed
initialization.
@param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
instance.
@param[in] HpcDevicePath The device path to the HPC.
@param[in] HpcPciAddress The address of the HPC function on the PCI bus.
@param[in] HpcState The state of the HPC hardware.
@param[out] Padding The amount of resource padding that is required
by the PCI bus under the control of the specified
HPC.
@param[out] Attributes Describes how padding is accounted for. The
padding is returned in the form of ACPI 2.0
resource descriptors.
@retval EFI_SUCCESS The resource padding was successfully
returned.
@retval EFI_UNSUPPORTED This instance of the
EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
support the specified HPC.
@retval EFI_NOT_READY This function was called before HPC
initialization is complete.
@retval EFI_INVALID_PARAMETER HpcState or Padding or Attributes is NULL.
@retval EFI_OUT_OF_RESOURCES ACPI 2.0 resource descriptors for Padding
cannot be allocated due to insufficient
resources.
**/
STATIC
EFI_STATUS
EFIAPI
GetResourcePadding (
IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,
IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,
IN UINT64 HpcPciAddress,
OUT EFI_HPC_STATE *HpcState,
OUT VOID **Padding,
OUT EFI_HPC_PADDING_ATTRIBUTES *Attributes
)
{
BOOLEAN DefaultIo;
BOOLEAN DefaultMmio;
RESOURCE_PADDING ReservationRequest;
EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *FirstResource;
DEBUG_CODE (
EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *Address;
CHAR16 *DevicePathString;
Address = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *)&HpcPciAddress;
DevicePathString = ConvertDevicePathToText (HpcDevicePath, FALSE, FALSE);
DEBUG ((EFI_D_VERBOSE, "%a: Address=%02x:%02x.%x DevicePath=%s\n",
__FUNCTION__, Address->Bus, Address->Device, Address->Function,
(DevicePathString == NULL) ? L"<unavailable>" : DevicePathString));
if (DevicePathString != NULL) {
FreePool (DevicePathString);
}
);
if (HpcState == NULL || Padding == NULL || Attributes == NULL) {
return EFI_INVALID_PARAMETER;
}
DefaultIo = TRUE;
DefaultMmio = TRUE;
//
// Init ReservationRequest, and point FirstResource one past the last
// descriptor entry. We're going to build the entries backwards from
// ReservationRequest.EndDesc.
//
InitializeResourcePadding (&ReservationRequest);
FirstResource = ReservationRequest.Padding +
ARRAY_SIZE (ReservationRequest.Padding);
//
// (b) Reserve IO space.
//
if (DefaultIo) {
//
// Request defaults.
//
SetIoPadding (--FirstResource, (UINTN)HighBitSetRoundUp64 (512));
}
//
// (c) Reserve non-prefetchable MMIO space (32-bit only).
//
if (DefaultMmio) {
//
// Request defaults.
//
SetMmioPadding (
--FirstResource,
FALSE,
TRUE,
(UINTN)HighBitSetRoundUp32 (SIZE_2MB)
);
}
//
// Output a copy of ReservationRequest from the lowest-address populated
// entry until the end of the structure (including
// ReservationRequest.EndDesc). If no reservations are necessary, we'll only
// output the End Tag.
//
*Padding = AllocateCopyPool (
(UINT8 *)(&ReservationRequest + 1) - (UINT8 *)FirstResource,
FirstResource
);
if (*Padding == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Resource padding is required.
//
*HpcState = EFI_HPC_STATE_INITIALIZED | EFI_HPC_STATE_ENABLED;
//
// The padding should be applied at PCI bus level, and considered by upstream
// bridges, recursively.
//
*Attributes = EfiPaddingPciBus;
return EFI_SUCCESS;
}
/**
Entry point for this driver.
@param[in] ImageHandle Image handle of this driver.
@param[in] SystemTable Pointer to SystemTable.
@retval EFI_SUCESS Driver has loaded successfully.
@return Error codes from lower level functions.
**/
EFI_STATUS
EFIAPI
DriverInitialize (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
mPciHotPlugInit.GetRootHpcList = GetRootHpcList;
mPciHotPlugInit.InitializeRootHpc = InitializeRootHpc;
mPciHotPlugInit.GetResourcePadding = GetResourcePadding;
Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,
&gEfiPciHotPlugInitProtocolGuid, &mPciHotPlugInit, NULL);
return Status;
}
|