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 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
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.
16 #include <Library/MemoryAllocationLib.h>
18 #include "AcpiPlatform.h"
22 Collect all PciIo protocol instances in the system. Save their original
23 attributes, and enable IO and MMIO decoding for each.
25 This is a best effort function; it doesn't return status codes. Its
26 caller is supposed to proceed even if this function fails.
28 @param[out] OriginalAttributes On output, a dynamically allocated array of
29 ORIGINAL_ATTRIBUTES elements. The array lists
30 the PciIo protocol instances found in the
31 system at the time of the call, plus the
32 original PCI attributes for each.
34 Before returning, the function enables IO and
35 MMIO decoding for each PciIo instance it
38 On error, or when no such instances are
39 found, OriginalAttributes is set to NULL.
41 @param[out] Count On output, the number of elements in
42 OriginalAttributes. On error it is set to
47 OUT ORIGINAL_ATTRIBUTES
**OriginalAttributes
,
54 ORIGINAL_ATTRIBUTES
*OrigAttrs
;
57 *OriginalAttributes
= NULL
;
60 if (PcdGetBool (PcdPciDisableBusEnumeration
)) {
62 // The platform downloads ACPI tables from QEMU in general, but there are
63 // no root bridges in this execution. We're done.
68 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiPciIoProtocolGuid
,
69 NULL
/* SearchKey */, &NoHandles
, &Handles
);
70 if (Status
== EFI_NOT_FOUND
) {
72 // No PCI devices were found on either of the root bridges. We're done.
77 if (EFI_ERROR (Status
)) {
78 DEBUG ((EFI_D_WARN
, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__
,
83 OrigAttrs
= AllocatePool (NoHandles
* sizeof *OrigAttrs
);
84 if (OrigAttrs
== NULL
) {
85 DEBUG ((EFI_D_WARN
, "%a: AllocatePool(): out of resources\n",
90 for (Idx
= 0; Idx
< NoHandles
; ++Idx
) {
91 EFI_PCI_IO_PROTOCOL
*PciIo
;
95 // Look up PciIo on the handle and stash it
97 Status
= gBS
->HandleProtocol (Handles
[Idx
], &gEfiPciIoProtocolGuid
,
99 ASSERT_EFI_ERROR (Status
);
100 OrigAttrs
[Idx
].PciIo
= PciIo
;
103 // Stash the current attributes
105 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationGet
, 0,
106 &OrigAttrs
[Idx
].PciAttributes
);
107 if (EFI_ERROR (Status
)) {
108 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationGet: %r\n",
109 __FUNCTION__
, Status
));
110 goto RestoreAttributes
;
114 // Retrieve supported attributes
116 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationSupported
, 0,
118 if (EFI_ERROR (Status
)) {
119 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationSupported: %r\n",
120 __FUNCTION__
, Status
));
121 goto RestoreAttributes
;
125 // Enable IO and MMIO decoding
127 Attributes
&= EFI_PCI_IO_ATTRIBUTE_IO
| EFI_PCI_IO_ATTRIBUTE_MEMORY
;
128 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationEnable
,
130 if (EFI_ERROR (Status
)) {
131 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationEnable: %r\n",
132 __FUNCTION__
, Status
));
133 goto RestoreAttributes
;
141 *OriginalAttributes
= OrigAttrs
;
148 OrigAttrs
[Idx
].PciIo
->Attributes (OrigAttrs
[Idx
].PciIo
,
149 EfiPciIoAttributeOperationSet
,
150 OrigAttrs
[Idx
].PciAttributes
,
154 FreePool (OrigAttrs
);
162 Restore the original PCI attributes saved with EnablePciDecoding().
164 @param[in] OriginalAttributes The array allocated and populated by
165 EnablePciDecoding(). This parameter may be
166 NULL. If OriginalAttributes is NULL, then the
167 function is a no-op; otherwise the PciIo
168 attributes will be restored, and the
169 OriginalAttributes array will be freed.
171 @param[in] Count The Count value stored by EnablePciDecoding(),
172 the number of elements in OriginalAttributes.
173 Count may be zero if and only if
174 OriginalAttributes is NULL.
178 IN ORIGINAL_ATTRIBUTES
*OriginalAttributes
,
184 ASSERT ((OriginalAttributes
== NULL
) == (Count
== 0));
185 if (OriginalAttributes
== NULL
) {
189 for (Idx
= 0; Idx
< Count
; ++Idx
) {
190 OriginalAttributes
[Idx
].PciIo
->Attributes (
191 OriginalAttributes
[Idx
].PciIo
,
192 EfiPciIoAttributeOperationSet
,
193 OriginalAttributes
[Idx
].PciAttributes
,
197 FreePool (OriginalAttributes
);