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
;
94 // Look up PciIo on the handle and stash it
96 Status
= gBS
->HandleProtocol (Handles
[Idx
], &gEfiPciIoProtocolGuid
,
98 ASSERT_EFI_ERROR (Status
);
99 OrigAttrs
[Idx
].PciIo
= PciIo
;
102 // Stash the current attributes
104 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationGet
, 0,
105 &OrigAttrs
[Idx
].PciAttributes
);
106 if (EFI_ERROR (Status
)) {
107 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationGet: %r\n",
108 __FUNCTION__
, Status
));
109 goto RestoreAttributes
;
113 // Enable IO and MMIO decoding
115 Status
= PciIo
->Attributes (PciIo
, EfiPciIoAttributeOperationEnable
,
116 EFI_PCI_IO_ATTRIBUTE_IO
| EFI_PCI_IO_ATTRIBUTE_MEMORY
,
118 if (EFI_ERROR (Status
)) {
119 DEBUG ((EFI_D_WARN
, "%a: EfiPciIoAttributeOperationEnable: %r\n",
120 __FUNCTION__
, Status
));
121 goto RestoreAttributes
;
129 *OriginalAttributes
= OrigAttrs
;
136 OrigAttrs
[Idx
].PciIo
->Attributes (OrigAttrs
[Idx
].PciIo
,
137 EfiPciIoAttributeOperationSet
,
138 OrigAttrs
[Idx
].PciAttributes
,
142 FreePool (OrigAttrs
);
150 Restore the original PCI attributes saved with EnablePciDecoding().
152 @param[in] OriginalAttributes The array allocated and populated by
153 EnablePciDecoding(). This parameter may be
154 NULL. If OriginalAttributes is NULL, then the
155 function is a no-op; otherwise the PciIo
156 attributes will be restored, and the
157 OriginalAttributes array will be freed.
159 @param[in] Count The Count value stored by EnablePciDecoding(),
160 the number of elements in OriginalAttributes.
161 Count may be zero if and only if
162 OriginalAttributes is NULL.
166 IN ORIGINAL_ATTRIBUTES
*OriginalAttributes
,
172 ASSERT ((OriginalAttributes
== NULL
) == (Count
== 0));
173 if (OriginalAttributes
== NULL
) {
177 for (Idx
= 0; Idx
< Count
; ++Idx
) {
178 OriginalAttributes
[Idx
].PciIo
->Attributes (
179 OriginalAttributes
[Idx
].PciIo
,
180 EfiPciIoAttributeOperationSet
,
181 OriginalAttributes
[Idx
].PciAttributes
,
185 FreePool (OriginalAttributes
);