TianoCore EDK2 master
Loading...
Searching...
No Matches
PciDecoding.c
Go to the documentation of this file.
1
11#include <Library/DebugLib.h> // DEBUG()
12#include <Library/MemoryAllocationLib.h> // AllocatePool()
14
39VOID
41 OUT ORIGINAL_ATTRIBUTES **OriginalAttributes,
42 OUT UINTN *Count
43 )
44{
45 EFI_STATUS Status;
46 UINTN NoHandles;
47 EFI_HANDLE *Handles;
48 ORIGINAL_ATTRIBUTES *OrigAttrs;
49 UINTN Idx;
50
51 *OriginalAttributes = NULL;
52 *Count = 0;
53
54 if (PcdGetBool (PcdPciDisableBusEnumeration)) {
55 //
56 // The platform downloads ACPI tables from QEMU in general, but there are
57 // no root bridges in this execution. We're done.
58 //
59 return;
60 }
61
62 Status = gBS->LocateHandleBuffer (
64 &gEfiPciIoProtocolGuid,
65 NULL /* SearchKey */,
66 &NoHandles,
67 &Handles
68 );
69 if (Status == EFI_NOT_FOUND) {
70 //
71 // No PCI devices were found on either of the root bridges. We're done.
72 //
73 return;
74 }
75
76 if (EFI_ERROR (Status)) {
77 DEBUG ((
78 DEBUG_WARN,
79 "%a: LocateHandleBuffer(): %r\n",
80 __func__,
81 Status
82 ));
83 return;
84 }
85
86 OrigAttrs = AllocatePool (NoHandles * sizeof *OrigAttrs);
87 if (OrigAttrs == NULL) {
88 DEBUG ((
89 DEBUG_WARN,
90 "%a: AllocatePool(): out of resources\n",
91 __func__
92 ));
93 goto FreeHandles;
94 }
95
96 for (Idx = 0; Idx < NoHandles; ++Idx) {
98 UINT64 Attributes;
99
100 //
101 // Look up PciIo on the handle and stash it
102 //
103 Status = gBS->HandleProtocol (
104 Handles[Idx],
105 &gEfiPciIoProtocolGuid,
106 (VOID **)&PciIo
107 );
108 ASSERT_EFI_ERROR (Status);
109 OrigAttrs[Idx].PciIo = PciIo;
110
111 //
112 // Stash the current attributes
113 //
114 Status = PciIo->Attributes (
115 PciIo,
117 0,
118 &OrigAttrs[Idx].PciAttributes
119 );
120 if (EFI_ERROR (Status)) {
121 DEBUG ((
122 DEBUG_WARN,
123 "%a: EfiPciIoAttributeOperationGet: %r\n",
124 __func__,
125 Status
126 ));
127 goto RestoreAttributes;
128 }
129
130 //
131 // Retrieve supported attributes
132 //
133 Status = PciIo->Attributes (
134 PciIo,
136 0,
137 &Attributes
138 );
139 if (EFI_ERROR (Status)) {
140 DEBUG ((
141 DEBUG_WARN,
142 "%a: EfiPciIoAttributeOperationSupported: %r\n",
143 __func__,
144 Status
145 ));
146 goto RestoreAttributes;
147 }
148
149 //
150 // Enable IO and MMIO decoding
151 //
153 Status = PciIo->Attributes (
154 PciIo,
156 Attributes,
157 NULL
158 );
159 if (EFI_ERROR (Status)) {
160 DEBUG ((
161 DEBUG_WARN,
162 "%a: EfiPciIoAttributeOperationEnable: %r\n",
163 __func__,
164 Status
165 ));
166 goto RestoreAttributes;
167 }
168 }
169
170 //
171 // Success
172 //
173 FreePool (Handles);
174 *OriginalAttributes = OrigAttrs;
175 *Count = NoHandles;
176 return;
177
178RestoreAttributes:
179 while (Idx > 0) {
180 --Idx;
181 OrigAttrs[Idx].PciIo->Attributes (
182 OrigAttrs[Idx].PciIo,
184 OrigAttrs[Idx].PciAttributes,
185 NULL
186 );
187 }
188
189 FreePool (OrigAttrs);
190
191FreeHandles:
192 FreePool (Handles);
193}
194
210VOID
212 IN ORIGINAL_ATTRIBUTES *OriginalAttributes,
213 IN UINTN Count
214 )
215{
216 UINTN Idx;
217
218 ASSERT ((OriginalAttributes == NULL) == (Count == 0));
219 if (OriginalAttributes == NULL) {
220 return;
221 }
222
223 for (Idx = 0; Idx < Count; ++Idx) {
224 OriginalAttributes[Idx].PciIo->Attributes (
225 OriginalAttributes[Idx].PciIo,
227 OriginalAttributes[Idx].PciAttributes,
228 NULL
229 );
230 }
231
232 FreePool (OriginalAttributes);
233}
UINT64 UINTN
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
@ EfiPciIoAttributeOperationGet
Definition: PciIo.h:103
@ EfiPciIoAttributeOperationEnable
Definition: PciIo.h:111
@ EfiPciIoAttributeOperationSet
Definition: PciIo.h:107
@ EfiPciIoAttributeOperationSupported
Definition: PciIo.h:119
#define EFI_PCI_IO_ATTRIBUTE_MEMORY
Enable the Memory decode bit in the PCI Config Header.
Definition: PciIo.h:58
#define EFI_PCI_IO_ATTRIBUTE_IO
Enable the I/O decode bit in the PCI Config Header.
Definition: PciIo.h:57
#define PcdGetBool(TokenName)
Definition: PcdLib.h:401
VOID RestorePciDecoding(IN ORIGINAL_ATTRIBUTES *OriginalAttributes, IN UINTN Count)
Definition: PciDecoding.c:211
VOID EnablePciDecoding(OUT ORIGINAL_ATTRIBUTES **OriginalAttributes, OUT UINTN *Count)
Definition: PciDecoding.c:40
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_BOOT_SERVICES * gBS
@ ByProtocol
Definition: UefiSpec.h:1518