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