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"
15 Collect all PciIo protocol instances in the system. Save their original
16 attributes, and enable IO and MMIO decoding for each.
18 This is a best effort function; it doesn't return status codes. Its
19 caller is supposed to proceed even if this function fails.
21 @param[out] OriginalAttributes On output, a dynamically allocated array of
22 ORIGINAL_ATTRIBUTES elements. The array lists
23 the PciIo protocol instances found in the
24 system at the time of the call, plus the
25 original PCI attributes for each.
27 Before returning, the function enables IO and
28 MMIO decoding for each PciIo instance it
31 On error, or when no such instances are
32 found, OriginalAttributes is set to NULL.
34 @param[out] Count On output, the number of elements in
35 OriginalAttributes. On error it is set to
40 OUT ORIGINAL_ATTRIBUTES
**OriginalAttributes
,
47 ORIGINAL_ATTRIBUTES
*OrigAttrs
;
50 *OriginalAttributes
= NULL
;
53 if (PcdGetBool (PcdPciDisableBusEnumeration
)) {
55 // The platform downloads ACPI tables from QEMU in general, but there are
56 // no root bridges in this execution. We're done.
61 Status
= gBS
->LocateHandleBuffer (
63 &gEfiPciIoProtocolGuid
,
68 if (Status
== EFI_NOT_FOUND
) {
70 // No PCI devices were found on either of the root bridges. We're done.
75 if (EFI_ERROR (Status
)) {
78 "%a: LocateHandleBuffer(): %r\n",
85 OrigAttrs
= AllocatePool (NoHandles
* sizeof *OrigAttrs
);
86 if (OrigAttrs
== NULL
) {
89 "%a: AllocatePool(): out of resources\n",
95 for (Idx
= 0; Idx
< NoHandles
; ++Idx
) {
96 EFI_PCI_IO_PROTOCOL
*PciIo
;
100 // Look up PciIo on the handle and stash it
102 Status
= gBS
->HandleProtocol (
104 &gEfiPciIoProtocolGuid
,
107 ASSERT_EFI_ERROR (Status
);
108 OrigAttrs
[Idx
].PciIo
= PciIo
;
111 // Stash the current attributes
113 Status
= PciIo
->Attributes (
115 EfiPciIoAttributeOperationGet
,
117 &OrigAttrs
[Idx
].PciAttributes
119 if (EFI_ERROR (Status
)) {
122 "%a: EfiPciIoAttributeOperationGet: %r\n",
126 goto RestoreAttributes
;
130 // Retrieve supported attributes
132 Status
= PciIo
->Attributes (
134 EfiPciIoAttributeOperationSupported
,
138 if (EFI_ERROR (Status
)) {
141 "%a: EfiPciIoAttributeOperationSupported: %r\n",
145 goto RestoreAttributes
;
149 // Enable IO and MMIO decoding
151 Attributes
&= EFI_PCI_IO_ATTRIBUTE_IO
| EFI_PCI_IO_ATTRIBUTE_MEMORY
;
152 Status
= PciIo
->Attributes (
154 EfiPciIoAttributeOperationEnable
,
158 if (EFI_ERROR (Status
)) {
161 "%a: EfiPciIoAttributeOperationEnable: %r\n",
165 goto RestoreAttributes
;
173 *OriginalAttributes
= OrigAttrs
;
180 OrigAttrs
[Idx
].PciIo
->Attributes (
181 OrigAttrs
[Idx
].PciIo
,
182 EfiPciIoAttributeOperationSet
,
183 OrigAttrs
[Idx
].PciAttributes
,
188 FreePool (OrigAttrs
);
195 Restore the original PCI attributes saved with EnablePciDecoding().
197 @param[in] OriginalAttributes The array allocated and populated by
198 EnablePciDecoding(). This parameter may be
199 NULL. If OriginalAttributes is NULL, then the
200 function is a no-op; otherwise the PciIo
201 attributes will be restored, and the
202 OriginalAttributes array will be freed.
204 @param[in] Count The Count value stored by EnablePciDecoding(),
205 the number of elements in OriginalAttributes.
206 Count may be zero if and only if
207 OriginalAttributes is NULL.
211 IN ORIGINAL_ATTRIBUTES
*OriginalAttributes
,
217 ASSERT ((OriginalAttributes
== NULL
) == (Count
== 0));
218 if (OriginalAttributes
== NULL
) {
222 for (Idx
= 0; Idx
< Count
; ++Idx
) {
223 OriginalAttributes
[Idx
].PciIo
->Attributes (
224 OriginalAttributes
[Idx
].PciIo
,
225 EfiPciIoAttributeOperationSet
,
226 OriginalAttributes
[Idx
].PciAttributes
,
231 FreePool (OriginalAttributes
);