]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/AcpiPlatformDxe/PciDecoding.c
OvmfPkg: AcpiPlatformDxe: enable PCI IO and MMIO while fetching QEMU tables
[mirror_edk2.git] / OvmfPkg / AcpiPlatformDxe / PciDecoding.c
1 /** @file
2 Temporarily enable IO and MMIO decoding for all PCI devices while QEMU
3 regenerates the ACPI tables.
4
5 Copyright (C) 2016, Red Hat, Inc.
6
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
11
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.
14 **/
15
16 #include <Library/MemoryAllocationLib.h>
17
18 #include "AcpiPlatform.h"
19
20
21 /**
22 Collect all PciIo protocol instances in the system. Save their original
23 attributes, and enable IO and MMIO decoding for each.
24
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.
27
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.
33
34 Before returning, the function enables IO and
35 MMIO decoding for each PciIo instance it
36 finds.
37
38 On error, or when no such instances are
39 found, OriginalAttributes is set to NULL.
40
41 @param[out] Count On output, the number of elements in
42 OriginalAttributes. On error it is set to
43 zero.
44 **/
45 VOID
46 EnablePciDecoding (
47 OUT ORIGINAL_ATTRIBUTES **OriginalAttributes,
48 OUT UINTN *Count
49 )
50 {
51 EFI_STATUS Status;
52 UINTN NoHandles;
53 EFI_HANDLE *Handles;
54 ORIGINAL_ATTRIBUTES *OrigAttrs;
55 UINTN Idx;
56
57 *OriginalAttributes = NULL;
58 *Count = 0;
59
60 if (PcdGetBool (PcdPciDisableBusEnumeration)) {
61 //
62 // The platform downloads ACPI tables from QEMU in general, but there are
63 // no root bridges in this execution. We're done.
64 //
65 return;
66 }
67
68 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiPciIoProtocolGuid,
69 NULL /* SearchKey */, &NoHandles, &Handles);
70 if (Status == EFI_NOT_FOUND) {
71 //
72 // No PCI devices were found on either of the root bridges. We're done.
73 //
74 return;
75 }
76
77 if (EFI_ERROR (Status)) {
78 DEBUG ((EFI_D_WARN, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__,
79 Status));
80 return;
81 }
82
83 OrigAttrs = AllocatePool (NoHandles * sizeof *OrigAttrs);
84 if (OrigAttrs == NULL) {
85 DEBUG ((EFI_D_WARN, "%a: AllocatePool(): out of resources\n",
86 __FUNCTION__));
87 goto FreeHandles;
88 }
89
90 for (Idx = 0; Idx < NoHandles; ++Idx) {
91 EFI_PCI_IO_PROTOCOL *PciIo;
92
93 //
94 // Look up PciIo on the handle and stash it
95 //
96 Status = gBS->HandleProtocol (Handles[Idx], &gEfiPciIoProtocolGuid,
97 (VOID**)&PciIo);
98 ASSERT_EFI_ERROR (Status);
99 OrigAttrs[Idx].PciIo = PciIo;
100
101 //
102 // Stash the current attributes
103 //
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;
110 }
111
112 //
113 // Enable IO and MMIO decoding
114 //
115 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationEnable,
116 EFI_PCI_IO_ATTRIBUTE_IO | EFI_PCI_IO_ATTRIBUTE_MEMORY,
117 NULL);
118 if (EFI_ERROR (Status)) {
119 DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationEnable: %r\n",
120 __FUNCTION__, Status));
121 goto RestoreAttributes;
122 }
123 }
124
125 //
126 // Success
127 //
128 FreePool (Handles);
129 *OriginalAttributes = OrigAttrs;
130 *Count = NoHandles;
131 return;
132
133 RestoreAttributes:
134 while (Idx > 0) {
135 --Idx;
136 OrigAttrs[Idx].PciIo->Attributes (OrigAttrs[Idx].PciIo,
137 EfiPciIoAttributeOperationSet,
138 OrigAttrs[Idx].PciAttributes,
139 NULL
140 );
141 }
142 FreePool (OrigAttrs);
143
144 FreeHandles:
145 FreePool (Handles);
146 }
147
148
149 /**
150 Restore the original PCI attributes saved with EnablePciDecoding().
151
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.
158
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.
163 **/
164 VOID
165 RestorePciDecoding (
166 IN ORIGINAL_ATTRIBUTES *OriginalAttributes,
167 IN UINTN Count
168 )
169 {
170 UINTN Idx;
171
172 ASSERT ((OriginalAttributes == NULL) == (Count == 0));
173 if (OriginalAttributes == NULL) {
174 return;
175 }
176
177 for (Idx = 0; Idx < Count; ++Idx) {
178 OriginalAttributes[Idx].PciIo->Attributes (
179 OriginalAttributes[Idx].PciIo,
180 EfiPciIoAttributeOperationSet,
181 OriginalAttributes[Idx].PciAttributes,
182 NULL
183 );
184 }
185 FreePool (OriginalAttributes);
186 }