2 Temporarily enable IO and MMIO decoding for all PCI devices while QEMU
3 regenerates the ACPI tables.
5 Copyright (C) 2016, Red Hat, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/MemoryAllocationLib.h>
12 #include "AcpiPlatform.h"
16 Collect all PciIo protocol instances in the system. Save their original
17 attributes, and enable IO and MMIO decoding for each.
19 This is a best effort function; it doesn't return status codes. Its
20 caller is supposed to proceed even if this function fails.
22 @param[out] OriginalAttributes On output, a dynamically allocated array of
23 ORIGINAL_ATTRIBUTES elements. The array lists
24 the PciIo protocol instances found in the
25 system at the time of the call, plus the
26 original PCI attributes for each.
28 Before returning, the function enables IO and
29 MMIO decoding for each PciIo instance it
32 On error, or when no such instances are
33 found, OriginalAttributes is set to NULL.
35 @param[out] Count On output, the number of elements in
36 OriginalAttributes. On error it is set to
41 OUT ORIGINAL_ATTRIBUTES
**OriginalAttributes
,
48 ORIGINAL_ATTRIBUTES
*OrigAttrs
;
51 *OriginalAttributes
= NULL
;
54 if (PcdGetBool (PcdPciDisableBusEnumeration
)) {
56 // The platform downloads ACPI tables from QEMU in general, but there are
57 // no root bridges in this execution. We're done.
62 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiPciIoProtocolGuid
,
63 NULL
/* SearchKey */, &NoHandles
, &Handles
);
64 if (Status
== EFI_NOT_FOUND
) {
66 // No PCI devices were found on either of the root bridges. We're done.
71 if (EFI_ERROR (Status
)) {
72 DEBUG ((EFI_D_WARN
, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__
,
77 OrigAttrs
= AllocatePool (NoHandles
* sizeof *OrigAttrs
);
78 if (OrigAttrs
== NULL
) {
79 DEBUG ((EFI_D_WARN
, "%a: AllocatePool(): out of resources\n",
84 for (Idx
= 0; Idx
< NoHandles
; ++Idx
) {
85 EFI_PCI_IO_PROTOCOL
*PciIo
;
89 // Look up PciIo on the handle and stash it
91 Status
= gBS
->HandleProtocol (Handles
[Idx
], &gEfiPciIoProtocolGuid
,
93 ASSERT_EFI_ERROR (Status
);
94 OrigAttrs
[Idx
].PciIo
= PciIo
;
97 // Stash the current attributes
99 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationGet
, 0,
100 &OrigAttrs
[Idx
].PciAttributes
);
101 if (EFI_ERROR (Status
)) {
102 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationGet: %r\n",
103 __FUNCTION__
, Status
));
104 goto RestoreAttributes
;
108 // Retrieve supported attributes
110 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationSupported
, 0,
112 if (EFI_ERROR (Status
)) {
113 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationSupported: %r\n",
114 __FUNCTION__
, Status
));
115 goto RestoreAttributes
;
119 // Enable IO and MMIO decoding
121 Attributes
&= EFI_PCI_IO_ATTRIBUTE_IO
| EFI_PCI_IO_ATTRIBUTE_MEMORY
;
122 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationEnable
,
124 if (EFI_ERROR (Status
)) {
125 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationEnable: %r\n",
126 __FUNCTION__
, Status
));
127 goto RestoreAttributes
;
135 *OriginalAttributes
= OrigAttrs
;
142 OrigAttrs
[Idx
].PciIo
->Attributes (OrigAttrs
[Idx
].PciIo
,
143 EfiPciIoAttributeOperationSet
,
144 OrigAttrs
[Idx
].PciAttributes
,
148 FreePool (OrigAttrs
);
156 Restore the original PCI attributes saved with EnablePciDecoding().
158 @param[in] OriginalAttributes The array allocated and populated by
159 EnablePciDecoding(). This parameter may be
160 NULL. If OriginalAttributes is NULL, then the
161 function is a no-op; otherwise the PciIo
162 attributes will be restored, and the
163 OriginalAttributes array will be freed.
165 @param[in] Count The Count value stored by EnablePciDecoding(),
166 the number of elements in OriginalAttributes.
167 Count may be zero if and only if
168 OriginalAttributes is NULL.
172 IN ORIGINAL_ATTRIBUTES
*OriginalAttributes
,
178 ASSERT ((OriginalAttributes
== NULL
) == (Count
== 0));
179 if (OriginalAttributes
== NULL
) {
183 for (Idx
= 0; Idx
< Count
; ++Idx
) {
184 OriginalAttributes
[Idx
].PciIo
->Attributes (
185 OriginalAttributes
[Idx
].PciIo
,
186 EfiPciIoAttributeOperationSet
,
187 OriginalAttributes
[Idx
].PciAttributes
,
191 FreePool (OriginalAttributes
);