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