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