]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/XenAcpiPlatformDxe/PciDecoding.c
OvmfPkg/XenAcpiPlatformDxe: create from AcpiPlatformDxe
[mirror_edk2.git] / OvmfPkg / XenAcpiPlatformDxe / 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-2021, Red Hat, Inc.
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8 **/
9
10 #include <Library/DebugLib.h> // DEBUG()
11 #include <Library/MemoryAllocationLib.h> // AllocatePool()
12 #include <Library/UefiBootServicesTableLib.h> // gBS
13
14 #include "AcpiPlatform.h"
15
16
17 /**
18 Collect all PciIo protocol instances in the system. Save their original
19 attributes, and enable IO and MMIO decoding for each.
20
21 This is a best effort function; it doesn't return status codes. Its
22 caller is supposed to proceed even if this function fails.
23
24 @param[out] OriginalAttributes On output, a dynamically allocated array of
25 ORIGINAL_ATTRIBUTES elements. The array lists
26 the PciIo protocol instances found in the
27 system at the time of the call, plus the
28 original PCI attributes for each.
29
30 Before returning, the function enables IO and
31 MMIO decoding for each PciIo instance it
32 finds.
33
34 On error, or when no such instances are
35 found, OriginalAttributes is set to NULL.
36
37 @param[out] Count On output, the number of elements in
38 OriginalAttributes. On error it is set to
39 zero.
40 **/
41 VOID
42 EnablePciDecoding (
43 OUT ORIGINAL_ATTRIBUTES **OriginalAttributes,
44 OUT UINTN *Count
45 )
46 {
47 EFI_STATUS Status;
48 UINTN NoHandles;
49 EFI_HANDLE *Handles;
50 ORIGINAL_ATTRIBUTES *OrigAttrs;
51 UINTN Idx;
52
53 *OriginalAttributes = NULL;
54 *Count = 0;
55
56 if (PcdGetBool (PcdPciDisableBusEnumeration)) {
57 //
58 // The platform downloads ACPI tables from QEMU in general, but there are
59 // no root bridges in this execution. We're done.
60 //
61 return;
62 }
63
64 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiPciIoProtocolGuid,
65 NULL /* SearchKey */, &NoHandles, &Handles);
66 if (Status == EFI_NOT_FOUND) {
67 //
68 // No PCI devices were found on either of the root bridges. We're done.
69 //
70 return;
71 }
72
73 if (EFI_ERROR (Status)) {
74 DEBUG ((DEBUG_WARN, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__,
75 Status));
76 return;
77 }
78
79 OrigAttrs = AllocatePool (NoHandles * sizeof *OrigAttrs);
80 if (OrigAttrs == NULL) {
81 DEBUG ((DEBUG_WARN, "%a: AllocatePool(): out of resources\n",
82 __FUNCTION__));
83 goto FreeHandles;
84 }
85
86 for (Idx = 0; Idx < NoHandles; ++Idx) {
87 EFI_PCI_IO_PROTOCOL *PciIo;
88 UINT64 Attributes;
89
90 //
91 // Look up PciIo on the handle and stash it
92 //
93 Status = gBS->HandleProtocol (Handles[Idx], &gEfiPciIoProtocolGuid,
94 (VOID**)&PciIo);
95 ASSERT_EFI_ERROR (Status);
96 OrigAttrs[Idx].PciIo = PciIo;
97
98 //
99 // Stash the current attributes
100 //
101 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationGet, 0,
102 &OrigAttrs[Idx].PciAttributes);
103 if (EFI_ERROR (Status)) {
104 DEBUG ((DEBUG_WARN, "%a: EfiPciIoAttributeOperationGet: %r\n",
105 __FUNCTION__, Status));
106 goto RestoreAttributes;
107 }
108
109 //
110 // Retrieve supported attributes
111 //
112 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationSupported, 0,
113 &Attributes);
114 if (EFI_ERROR (Status)) {
115 DEBUG ((DEBUG_WARN, "%a: EfiPciIoAttributeOperationSupported: %r\n",
116 __FUNCTION__, Status));
117 goto RestoreAttributes;
118 }
119
120 //
121 // Enable IO and MMIO decoding
122 //
123 Attributes &= EFI_PCI_IO_ATTRIBUTE_IO | EFI_PCI_IO_ATTRIBUTE_MEMORY;
124 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationEnable,
125 Attributes, NULL);
126 if (EFI_ERROR (Status)) {
127 DEBUG ((DEBUG_WARN, "%a: EfiPciIoAttributeOperationEnable: %r\n",
128 __FUNCTION__, Status));
129 goto RestoreAttributes;
130 }
131 }
132
133 //
134 // Success
135 //
136 FreePool (Handles);
137 *OriginalAttributes = OrigAttrs;
138 *Count = NoHandles;
139 return;
140
141 RestoreAttributes:
142 while (Idx > 0) {
143 --Idx;
144 OrigAttrs[Idx].PciIo->Attributes (OrigAttrs[Idx].PciIo,
145 EfiPciIoAttributeOperationSet,
146 OrigAttrs[Idx].PciAttributes,
147 NULL
148 );
149 }
150 FreePool (OrigAttrs);
151
152 FreeHandles:
153 FreePool (Handles);
154 }
155
156
157 /**
158 Restore the original PCI attributes saved with EnablePciDecoding().
159
160 @param[in] OriginalAttributes The array allocated and populated by
161 EnablePciDecoding(). This parameter may be
162 NULL. If OriginalAttributes is NULL, then the
163 function is a no-op; otherwise the PciIo
164 attributes will be restored, and the
165 OriginalAttributes array will be freed.
166
167 @param[in] Count The Count value stored by EnablePciDecoding(),
168 the number of elements in OriginalAttributes.
169 Count may be zero if and only if
170 OriginalAttributes is NULL.
171 **/
172 VOID
173 RestorePciDecoding (
174 IN ORIGINAL_ATTRIBUTES *OriginalAttributes,
175 IN UINTN Count
176 )
177 {
178 UINTN Idx;
179
180 ASSERT ((OriginalAttributes == NULL) == (Count == 0));
181 if (OriginalAttributes == NULL) {
182 return;
183 }
184
185 for (Idx = 0; Idx < Count; ++Idx) {
186 OriginalAttributes[Idx].PciIo->Attributes (
187 OriginalAttributes[Idx].PciIo,
188 EfiPciIoAttributeOperationSet,
189 OriginalAttributes[Idx].PciAttributes,
190 NULL
191 );
192 }
193 FreePool (OriginalAttributes);
194 }