]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/AcpiPlatformDxe/PciDecoding.c
OvmfPkg: AcpiPlatformDxe: enable PCI IO and MMIO while fetching QEMU tables
[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
92\r
93 //\r
94 // Look up PciIo on the handle and stash it\r
95 //\r
96 Status = gBS->HandleProtocol (Handles[Idx], &gEfiPciIoProtocolGuid,\r
97 (VOID**)&PciIo);\r
98 ASSERT_EFI_ERROR (Status);\r
99 OrigAttrs[Idx].PciIo = PciIo;\r
100\r
101 //\r
102 // Stash the current attributes\r
103 //\r
104 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationGet, 0,\r
105 &OrigAttrs[Idx].PciAttributes);\r
106 if (EFI_ERROR (Status)) {\r
107 DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationGet: %r\n",\r
108 __FUNCTION__, Status));\r
109 goto RestoreAttributes;\r
110 }\r
111\r
112 //\r
113 // Enable IO and MMIO decoding\r
114 //\r
115 Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationEnable,\r
116 EFI_PCI_IO_ATTRIBUTE_IO | EFI_PCI_IO_ATTRIBUTE_MEMORY,\r
117 NULL);\r
118 if (EFI_ERROR (Status)) {\r
119 DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationEnable: %r\n",\r
120 __FUNCTION__, Status));\r
121 goto RestoreAttributes;\r
122 }\r
123 }\r
124\r
125 //\r
126 // Success\r
127 //\r
128 FreePool (Handles);\r
129 *OriginalAttributes = OrigAttrs;\r
130 *Count = NoHandles;\r
131 return;\r
132\r
133RestoreAttributes:\r
134 while (Idx > 0) {\r
135 --Idx;\r
136 OrigAttrs[Idx].PciIo->Attributes (OrigAttrs[Idx].PciIo,\r
137 EfiPciIoAttributeOperationSet,\r
138 OrigAttrs[Idx].PciAttributes,\r
139 NULL\r
140 );\r
141 }\r
142 FreePool (OrigAttrs);\r
143\r
144FreeHandles:\r
145 FreePool (Handles);\r
146}\r
147\r
148\r
149/**\r
150 Restore the original PCI attributes saved with EnablePciDecoding().\r
151\r
152 @param[in] OriginalAttributes The array allocated and populated by\r
153 EnablePciDecoding(). This parameter may be\r
154 NULL. If OriginalAttributes is NULL, then the\r
155 function is a no-op; otherwise the PciIo\r
156 attributes will be restored, and the\r
157 OriginalAttributes array will be freed.\r
158\r
159 @param[in] Count The Count value stored by EnablePciDecoding(),\r
160 the number of elements in OriginalAttributes.\r
161 Count may be zero if and only if\r
162 OriginalAttributes is NULL.\r
163**/\r
164VOID\r
165RestorePciDecoding (\r
166 IN ORIGINAL_ATTRIBUTES *OriginalAttributes,\r
167 IN UINTN Count\r
168 )\r
169{\r
170 UINTN Idx;\r
171\r
172 ASSERT ((OriginalAttributes == NULL) == (Count == 0));\r
173 if (OriginalAttributes == NULL) {\r
174 return;\r
175 }\r
176\r
177 for (Idx = 0; Idx < Count; ++Idx) {\r
178 OriginalAttributes[Idx].PciIo->Attributes (\r
179 OriginalAttributes[Idx].PciIo,\r
180 EfiPciIoAttributeOperationSet,\r
181 OriginalAttributes[Idx].PciAttributes,\r
182 NULL\r
183 );\r
184 }\r
185 FreePool (OriginalAttributes);\r
186}\r