]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c
OvmfPkg/PciHotPlugInitDxe: add helper functions for setting up paddings
[mirror_edk2.git] / OvmfPkg / PciHotPlugInitDxe / PciHotPlugInit.c
1 /** @file
2 This driver implements EFI_PCI_HOT_PLUG_INIT_PROTOCOL, providing the PCI bus
3 driver with resource padding information, for PCIe hotplug purposes.
4
5 Copyright (C) 2016, Red Hat, Inc.
6
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 **/
15
16 #include <IndustryStandard/Acpi10.h>
17
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/DevicePathLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24
25 #include <Protocol/PciHotPlugInit.h>
26 #include <Protocol/PciRootBridgeIo.h>
27
28 //
29 // The protocol interface this driver produces.
30 //
31 // Refer to 12.6 "PCI Hot Plug PCI Initialization Protocol" in the Platform
32 // Init 1.4a Spec, Volume 5.
33 //
34 STATIC EFI_PCI_HOT_PLUG_INIT_PROTOCOL mPciHotPlugInit;
35
36
37 //
38 // Resource padding template for the GetResourcePadding() protocol member
39 // function.
40 //
41 // Refer to Table 8 "ACPI 2.0 & 3.0 QWORD Address Space Descriptor Usage" in
42 // the Platform Init 1.4a Spec, Volume 5.
43 //
44 // This structure is interpreted by the ApplyResourcePadding() function in the
45 // edk2 PCI Bus UEFI_DRIVER.
46 //
47 // We can request padding for at most four resource types, each of which is
48 // optional, independently of the others:
49 // (a) bus numbers,
50 // (b) IO space,
51 // (c) non-prefetchable MMIO space (32-bit only),
52 // (d) prefetchable MMIO space (either 32-bit or 64-bit, never both).
53 //
54 #pragma pack (1)
55 typedef struct {
56 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR Padding[4];
57 EFI_ACPI_END_TAG_DESCRIPTOR EndDesc;
58 } RESOURCE_PADDING;
59 #pragma pack ()
60
61
62 /**
63 Initialize a RESOURCE_PADDING object.
64
65 @param[out] ResourcePadding The caller-allocated RESOURCE_PADDING object to
66 initialize.
67 **/
68 STATIC
69 VOID
70 InitializeResourcePadding (
71 OUT RESOURCE_PADDING *ResourcePadding
72 )
73 {
74 UINTN Index;
75
76 ZeroMem (ResourcePadding, sizeof *ResourcePadding);
77
78 //
79 // Fill in the Padding fields that don't vary across resource types.
80 //
81 for (Index = 0; Index < ARRAY_SIZE (ResourcePadding->Padding); ++Index) {
82 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;
83
84 Descriptor = ResourcePadding->Padding + Index;
85 Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
86 Descriptor->Len = (UINT16)(
87 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) -
88 OFFSET_OF (
89 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR,
90 ResType
91 )
92 );
93 }
94
95 //
96 // Fill in the End Tag.
97 //
98 ResourcePadding->EndDesc.Desc = ACPI_END_TAG_DESCRIPTOR;
99 }
100
101
102 /**
103 Set up a descriptor entry for reserving IO space.
104
105 @param[in,out] Descriptor The descriptor to configure. The caller shall have
106 initialized Descriptor earlier, with
107 InitializeResourcePadding().
108
109 @param[in] SizeExponent The size and natural alignment of the reservation
110 are determined by raising two to this power.
111 **/
112 STATIC
113 VOID
114 SetIoPadding (
115 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,
116 IN UINTN SizeExponent
117 )
118 {
119 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;
120 Descriptor->AddrLen = LShiftU64 (1, SizeExponent);
121 Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;
122 }
123
124
125 /**
126 Set up a descriptor entry for reserving MMIO space.
127
128 @param[in,out] Descriptor The descriptor to configure. The caller shall
129 have initialized Descriptor earlier, with
130 InitializeResourcePadding().
131
132 @param[in] Prefetchable TRUE if the descriptor should reserve
133 prefetchable MMIO space. Pass FALSE for
134 reserving non-prefetchable MMIO space.
135
136 @param[in] ThirtyTwoBitOnly TRUE if the reservation should be limited to
137 32-bit address space. FALSE if the reservation
138 can be satisfied from 64-bit address space.
139 ThirtyTwoBitOnly is ignored if Prefetchable is
140 FALSE; in that case ThirtyTwoBitOnly is always
141 considered TRUE.
142
143 @param[in] SizeExponent The size and natural alignment of the
144 reservation are determined by raising two to
145 this power.
146 **/
147 STATIC
148 VOID
149 SetMmioPadding (
150 IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,
151 IN BOOLEAN Prefetchable,
152 IN BOOLEAN ThirtyTwoBitOnly,
153 IN UINTN SizeExponent
154 )
155 {
156 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
157 if (Prefetchable) {
158 Descriptor->SpecificFlag =
159 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;
160 Descriptor->AddrSpaceGranularity = ThirtyTwoBitOnly ? 32 : 64;
161 } else {
162 Descriptor->SpecificFlag =
163 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_NON_CACHEABLE;
164 Descriptor->AddrSpaceGranularity = 32;
165 }
166 Descriptor->AddrLen = LShiftU64 (1, SizeExponent);
167 Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;
168 }
169
170
171 /**
172 Round up a positive 32-bit value to the next whole power of two, and return
173 the bit position of the highest bit set in the result. Equivalent to
174 ceil(log2(x)).
175
176 @param[in] Operand The 32-bit operand to evaluate.
177
178 @retval -1 Operand is zero.
179
180 @retval -1 Operand is positive, not a whole power of two, and rounding it
181 up to the next power of two does not fit into 32 bits.
182
183 @retval 0..31 Otherwise, return ceil(log2(Value)).
184 **/
185 STATIC
186 INTN
187 HighBitSetRoundUp32 (
188 IN UINT32 Operand
189 )
190 {
191 INTN HighBit;
192
193 HighBit = HighBitSet32 (Operand);
194 if (HighBit == -1) {
195 //
196 // Operand is zero.
197 //
198 return HighBit;
199 }
200 if ((Operand & (Operand - 1)) != 0) {
201 //
202 // Operand is not a whole power of two.
203 //
204 ++HighBit;
205 }
206 return (HighBit < 32) ? HighBit : -1;
207 }
208
209
210 /**
211 Round up a positive 64-bit value to the next whole power of two, and return
212 the bit position of the highest bit set in the result. Equivalent to
213 ceil(log2(x)).
214
215 @param[in] Operand The 64-bit operand to evaluate.
216
217 @retval -1 Operand is zero.
218
219 @retval -1 Operand is positive, not a whole power of two, and rounding it
220 up to the next power of two does not fit into 64 bits.
221
222 @retval 0..63 Otherwise, return ceil(log2(Value)).
223 **/
224 STATIC
225 INTN
226 HighBitSetRoundUp64 (
227 IN UINT64 Operand
228 )
229 {
230 INTN HighBit;
231
232 HighBit = HighBitSet64 (Operand);
233 if (HighBit == -1) {
234 //
235 // Operand is zero.
236 //
237 return HighBit;
238 }
239 if ((Operand & (Operand - 1)) != 0) {
240 //
241 // Operand is not a whole power of two.
242 //
243 ++HighBit;
244 }
245 return (HighBit < 64) ? HighBit : -1;
246 }
247
248
249 /**
250 Returns a list of root Hot Plug Controllers (HPCs) that require
251 initialization during the boot process.
252
253 This procedure returns a list of root HPCs. The PCI bus driver must
254 initialize these controllers during the boot process. The PCI bus driver may
255 or may not be able to detect these HPCs. If the platform includes a
256 PCI-to-CardBus bridge, it can be included in this list if it requires
257 initialization. The HpcList must be self consistent. An HPC cannot control
258 any of its parent buses. Only one HPC can control a PCI bus. Because this
259 list includes only root HPCs, no HPC in the list can be a child of another
260 HPC. This policy must be enforced by the EFI_PCI_HOT_PLUG_INIT_PROTOCOL.
261 The PCI bus driver may not check for such invalid conditions. The callee
262 allocates the buffer HpcList
263
264 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
265 instance.
266 @param[out] HpcCount The number of root HPCs that were returned.
267 @param[out] HpcList The list of root HPCs. HpcCount defines the number of
268 elements in this list.
269
270 @retval EFI_SUCCESS HpcList was returned.
271 @retval EFI_OUT_OF_RESOURCES HpcList was not returned due to insufficient
272 resources.
273 @retval EFI_INVALID_PARAMETER HpcCount is NULL or HpcList is NULL.
274 **/
275 STATIC
276 EFI_STATUS
277 EFIAPI
278 GetRootHpcList (
279 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,
280 OUT UINTN *HpcCount,
281 OUT EFI_HPC_LOCATION **HpcList
282 )
283 {
284 if (HpcCount == NULL || HpcList == NULL) {
285 return EFI_INVALID_PARAMETER;
286 }
287
288 //
289 // There are no top-level (i.e., un-enumerable) hot-plug controllers in QEMU
290 // that would require special initialization.
291 //
292 *HpcCount = 0;
293 *HpcList = NULL;
294 return EFI_SUCCESS;
295 }
296
297
298 /**
299 Initializes one root Hot Plug Controller (HPC). This process may causes
300 initialization of its subordinate buses.
301
302 This function initializes the specified HPC. At the end of initialization,
303 the hot-plug slots or sockets (controlled by this HPC) are powered and are
304 connected to the bus. All the necessary registers in the HPC are set up. For
305 a Standard (PCI) Hot Plug Controller (SHPC), the registers that must be set
306 up are defined in the PCI Standard Hot Plug Controller and Subsystem
307 Specification.
308
309 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
310 instance.
311 @param[in] HpcDevicePath The device path to the HPC that is being
312 initialized.
313 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
314 @param[in] Event The event that should be signaled when the HPC
315 initialization is complete. Set to NULL if the
316 caller wants to wait until the entire
317 initialization process is complete.
318 @param[out] HpcState The state of the HPC hardware. The state is
319 EFI_HPC_STATE_INITIALIZED or
320 EFI_HPC_STATE_ENABLED.
321
322 @retval EFI_SUCCESS If Event is NULL, the specific HPC was
323 successfully initialized. If Event is not
324 NULL, Event will be signaled at a later time
325 when initialization is complete.
326 @retval EFI_UNSUPPORTED This instance of
327 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
328 support the specified HPC.
329 @retval EFI_OUT_OF_RESOURCES Initialization failed due to insufficient
330 resources.
331 @retval EFI_INVALID_PARAMETER HpcState is NULL.
332 **/
333 STATIC
334 EFI_STATUS
335 EFIAPI
336 InitializeRootHpc (
337 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,
338 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,
339 IN UINT64 HpcPciAddress,
340 IN EFI_EVENT Event, OPTIONAL
341 OUT EFI_HPC_STATE *HpcState
342 )
343 {
344 //
345 // This function should never be called, due to the information returned by
346 // GetRootHpcList().
347 //
348 ASSERT (FALSE);
349
350 if (HpcState == NULL) {
351 return EFI_INVALID_PARAMETER;
352 }
353 return EFI_UNSUPPORTED;
354 }
355
356
357 /**
358 Returns the resource padding that is required by the PCI bus that is
359 controlled by the specified Hot Plug Controller (HPC).
360
361 This function returns the resource padding that is required by the PCI bus
362 that is controlled by the specified HPC. This member function is called for
363 all the root HPCs and nonroot HPCs that are detected by the PCI bus
364 enumerator. This function will be called before PCI resource allocation is
365 completed. This function must be called after all the root HPCs, with the
366 possible exception of a PCI-to-CardBus bridge, have completed
367 initialization.
368
369 @param[in] This Pointer to the EFI_PCI_HOT_PLUG_INIT_PROTOCOL
370 instance.
371 @param[in] HpcDevicePath The device path to the HPC.
372 @param[in] HpcPciAddress The address of the HPC function on the PCI bus.
373 @param[in] HpcState The state of the HPC hardware.
374 @param[out] Padding The amount of resource padding that is required
375 by the PCI bus under the control of the specified
376 HPC.
377 @param[out] Attributes Describes how padding is accounted for. The
378 padding is returned in the form of ACPI 2.0
379 resource descriptors.
380
381 @retval EFI_SUCCESS The resource padding was successfully
382 returned.
383 @retval EFI_UNSUPPORTED This instance of the
384 EFI_PCI_HOT_PLUG_INIT_PROTOCOL does not
385 support the specified HPC.
386 @retval EFI_NOT_READY This function was called before HPC
387 initialization is complete.
388 @retval EFI_INVALID_PARAMETER HpcState or Padding or Attributes is NULL.
389 @retval EFI_OUT_OF_RESOURCES ACPI 2.0 resource descriptors for Padding
390 cannot be allocated due to insufficient
391 resources.
392 **/
393 STATIC
394 EFI_STATUS
395 EFIAPI
396 GetResourcePadding (
397 IN EFI_PCI_HOT_PLUG_INIT_PROTOCOL *This,
398 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,
399 IN UINT64 HpcPciAddress,
400 OUT EFI_HPC_STATE *HpcState,
401 OUT VOID **Padding,
402 OUT EFI_HPC_PADDING_ATTRIBUTES *Attributes
403 )
404 {
405 BOOLEAN DefaultIo;
406 BOOLEAN DefaultMmio;
407 RESOURCE_PADDING ReservationRequest;
408 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *FirstResource;
409
410 DEBUG_CODE (
411 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *Address;
412 CHAR16 *DevicePathString;
413
414 Address = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *)&HpcPciAddress;
415 DevicePathString = ConvertDevicePathToText (HpcDevicePath, FALSE, FALSE);
416
417 DEBUG ((EFI_D_VERBOSE, "%a: Address=%02x:%02x.%x DevicePath=%s\n",
418 __FUNCTION__, Address->Bus, Address->Device, Address->Function,
419 (DevicePathString == NULL) ? L"<unavailable>" : DevicePathString));
420
421 if (DevicePathString != NULL) {
422 FreePool (DevicePathString);
423 }
424 );
425
426 if (HpcState == NULL || Padding == NULL || Attributes == NULL) {
427 return EFI_INVALID_PARAMETER;
428 }
429
430 DefaultIo = TRUE;
431 DefaultMmio = TRUE;
432
433 //
434 // Init ReservationRequest, and point FirstResource one past the last
435 // descriptor entry. We're going to build the entries backwards from
436 // ReservationRequest.EndDesc.
437 //
438 InitializeResourcePadding (&ReservationRequest);
439 FirstResource = ReservationRequest.Padding +
440 ARRAY_SIZE (ReservationRequest.Padding);
441
442 //
443 // (b) Reserve IO space.
444 //
445 if (DefaultIo) {
446 //
447 // Request defaults.
448 //
449 SetIoPadding (--FirstResource, (UINTN)HighBitSetRoundUp64 (512));
450 }
451
452 //
453 // (c) Reserve non-prefetchable MMIO space (32-bit only).
454 //
455 if (DefaultMmio) {
456 //
457 // Request defaults.
458 //
459 SetMmioPadding (
460 --FirstResource,
461 FALSE,
462 TRUE,
463 (UINTN)HighBitSetRoundUp32 (SIZE_2MB)
464 );
465 }
466
467 //
468 // Output a copy of ReservationRequest from the lowest-address populated
469 // entry until the end of the structure (including
470 // ReservationRequest.EndDesc). If no reservations are necessary, we'll only
471 // output the End Tag.
472 //
473 *Padding = AllocateCopyPool (
474 (UINT8 *)(&ReservationRequest + 1) - (UINT8 *)FirstResource,
475 FirstResource
476 );
477 if (*Padding == NULL) {
478 return EFI_OUT_OF_RESOURCES;
479 }
480
481 //
482 // Resource padding is required.
483 //
484 *HpcState = EFI_HPC_STATE_INITIALIZED | EFI_HPC_STATE_ENABLED;
485
486 //
487 // The padding should be applied at PCI bus level, and considered by upstream
488 // bridges, recursively.
489 //
490 *Attributes = EfiPaddingPciBus;
491 return EFI_SUCCESS;
492 }
493
494
495 /**
496 Entry point for this driver.
497
498 @param[in] ImageHandle Image handle of this driver.
499 @param[in] SystemTable Pointer to SystemTable.
500
501 @retval EFI_SUCESS Driver has loaded successfully.
502 @return Error codes from lower level functions.
503
504 **/
505 EFI_STATUS
506 EFIAPI
507 DriverInitialize (
508 IN EFI_HANDLE ImageHandle,
509 IN EFI_SYSTEM_TABLE *SystemTable
510 )
511 {
512 EFI_STATUS Status;
513
514 mPciHotPlugInit.GetRootHpcList = GetRootHpcList;
515 mPciHotPlugInit.InitializeRootHpc = InitializeRootHpc;
516 mPciHotPlugInit.GetResourcePadding = GetResourcePadding;
517 Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,
518 &gEfiPciHotPlugInitProtocolGuid, &mPciHotPlugInit, NULL);
519 return Status;
520 }