2 Temporarily enable IO and MMIO decoding for all PCI devices while QEMU
3 regenerates the ACPI tables.
5 Copyright (C) 2016-2021, Red Hat, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/DebugLib.h> // DEBUG()
11 #include <Library/MemoryAllocationLib.h> // AllocatePool()
12 #include <Library/UefiBootServicesTableLib.h> // gBS
14 #include "AcpiPlatform.h"
18 Collect all PciIo protocol instances in the system. Save their original
19 attributes, and enable IO and MMIO decoding for each.
21 This is a best effort function; it doesn't return status codes. Its
22 caller is supposed to proceed even if this function fails.
24 @param[out] OriginalAttributes On output, a dynamically allocated array of
25 ORIGINAL_ATTRIBUTES elements. The array lists
26 the PciIo protocol instances found in the
27 system at the time of the call, plus the
28 original PCI attributes for each.
30 Before returning, the function enables IO and
31 MMIO decoding for each PciIo instance it
34 On error, or when no such instances are
35 found, OriginalAttributes is set to NULL.
37 @param[out] Count On output, the number of elements in
38 OriginalAttributes. On error it is set to
43 OUT ORIGINAL_ATTRIBUTES
**OriginalAttributes
,
50 ORIGINAL_ATTRIBUTES
*OrigAttrs
;
53 *OriginalAttributes
= NULL
;
56 if (PcdGetBool (PcdPciDisableBusEnumeration
)) {
58 // The platform downloads ACPI tables from QEMU in general, but there are
59 // no root bridges in this execution. We're done.
64 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiPciIoProtocolGuid
,
65 NULL
/* SearchKey */, &NoHandles
, &Handles
);
66 if (Status
== EFI_NOT_FOUND
) {
68 // No PCI devices were found on either of the root bridges. We're done.
73 if (EFI_ERROR (Status
)) {
74 DEBUG ((DEBUG_WARN
, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__
,
79 OrigAttrs
= AllocatePool (NoHandles
* sizeof *OrigAttrs
);
80 if (OrigAttrs
== NULL
) {
81 DEBUG ((DEBUG_WARN
, "%a: AllocatePool(): out of resources\n",
86 for (Idx
= 0; Idx
< NoHandles
; ++Idx
) {
87 EFI_PCI_IO_PROTOCOL
*PciIo
;
91 // Look up PciIo on the handle and stash it
93 Status
= gBS
->HandleProtocol (Handles
[Idx
], &gEfiPciIoProtocolGuid
,
95 ASSERT_EFI_ERROR (Status
);
96 OrigAttrs
[Idx
].PciIo
= PciIo
;
99 // Stash the current attributes
101 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationGet
, 0,
102 &OrigAttrs
[Idx
].PciAttributes
);
103 if (EFI_ERROR (Status
)) {
104 DEBUG ((DEBUG_WARN
, "%a: EfiPciIoAttributeOperationGet: %r\n",
105 __FUNCTION__
, Status
));
106 goto RestoreAttributes
;
110 // Retrieve supported attributes
112 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationSupported
, 0,
114 if (EFI_ERROR (Status
)) {
115 DEBUG ((DEBUG_WARN
, "%a: EfiPciIoAttributeOperationSupported: %r\n",
116 __FUNCTION__
, Status
));
117 goto RestoreAttributes
;
121 // Enable IO and MMIO decoding
123 Attributes
&= EFI_PCI_IO_ATTRIBUTE_IO
| EFI_PCI_IO_ATTRIBUTE_MEMORY
;
124 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationEnable
,
126 if (EFI_ERROR (Status
)) {
127 DEBUG ((DEBUG_WARN
, "%a: EfiPciIoAttributeOperationEnable: %r\n",
128 __FUNCTION__
, Status
));
129 goto RestoreAttributes
;
137 *OriginalAttributes
= OrigAttrs
;
144 OrigAttrs
[Idx
].PciIo
->Attributes (OrigAttrs
[Idx
].PciIo
,
145 EfiPciIoAttributeOperationSet
,
146 OrigAttrs
[Idx
].PciAttributes
,
150 FreePool (OrigAttrs
);
158 Restore the original PCI attributes saved with EnablePciDecoding().
160 @param[in] OriginalAttributes The array allocated and populated by
161 EnablePciDecoding(). This parameter may be
162 NULL. If OriginalAttributes is NULL, then the
163 function is a no-op; otherwise the PciIo
164 attributes will be restored, and the
165 OriginalAttributes array will be freed.
167 @param[in] Count The Count value stored by EnablePciDecoding(),
168 the number of elements in OriginalAttributes.
169 Count may be zero if and only if
170 OriginalAttributes is NULL.
174 IN ORIGINAL_ATTRIBUTES
*OriginalAttributes
,
180 ASSERT ((OriginalAttributes
== NULL
) == (Count
== 0));
181 if (OriginalAttributes
== NULL
) {
185 for (Idx
= 0; Idx
< Count
; ++Idx
) {
186 OriginalAttributes
[Idx
].PciIo
->Attributes (
187 OriginalAttributes
[Idx
].PciIo
,
188 EfiPciIoAttributeOperationSet
,
189 OriginalAttributes
[Idx
].PciAttributes
,
193 FreePool (OriginalAttributes
);