]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/PciIncompatibleDeviceSupportLib/PciIncompatibleDeviceSupportLib.c
4934d4e2c1d0ff7e42c6349616e9d923425c473c
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / PciIncompatibleDeviceSupportLib / PciIncompatibleDeviceSupportLib.c
1 /** @file
2 The template of PCI incompatible device support libary.
3
4 Copyright (c) 2006 - 2009, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "IncompatiblePciDeviceList.h"
16
17 EFI_PCI_REGISTER_ACCESS_DATA mPciRegisterAccessData = {0, 0, 0};
18 EFI_PCI_REGISTER_VALUE_DATA mPciRegisterValueData = {0, 0};
19
20
21 /**
22 Check whether two PCI devices matched.
23
24 @param PciDeviceInfo A pointer to EFI_PCI_DEVICE_INFO.
25 @param Header A pointer to EFI_PCI_DEVICE_INFO.
26
27 @retval EFI_SUCCESS Two PCI devices matched.
28 @retval EFI_UNSUPPORTED Two PCI devices don't match.
29
30 **/
31 EFI_STATUS
32 DeviceCheck (
33 IN EFI_PCI_DEVICE_INFO *PciDeviceInfo,
34 IN EFI_PCI_DEVICE_INFO *Header
35 )
36 {
37 //
38 // See if the Header matches the parameters passed in
39 //
40 if (Header->VendorID != DEVICE_ID_NOCARE) {
41 if (PciDeviceInfo->VendorID != Header->VendorID) {
42 return EFI_UNSUPPORTED;
43 }
44 }
45
46 if (Header->DeviceID != DEVICE_ID_NOCARE) {
47 if (PciDeviceInfo->DeviceID != Header->DeviceID) {
48 return EFI_UNSUPPORTED;
49 }
50 }
51
52 if (Header->RevisionID != DEVICE_ID_NOCARE) {
53 if (PciDeviceInfo->RevisionID != Header->RevisionID) {
54 return EFI_UNSUPPORTED;
55 }
56 }
57
58 if (Header->SubsystemVendorID != DEVICE_ID_NOCARE) {
59 if (PciDeviceInfo->SubsystemVendorID != Header->SubsystemVendorID) {
60 return EFI_UNSUPPORTED;
61 }
62 }
63
64 if (Header->SubsystemID != DEVICE_ID_NOCARE) {
65 if (PciDeviceInfo->SubsystemID != Header->SubsystemID) {
66 return EFI_UNSUPPORTED;
67 }
68 }
69
70 return EFI_SUCCESS;
71 }
72
73
74 /**
75 Check the incompatible device list for ACPI resource update and return
76 the configuration.
77
78 This function searches the incompatible device list according to request
79 information. If the PCI device belongs to the devices list, corresponding
80 configuration informtion will be returned, in the meantime return EFI_SUCCESS.
81
82 @param PciDeviceInfo A pointer to PCI device information.
83 @param Configuration Returned information.
84
85 @retval EFI_SUCCESS If check incompatible device successfully.
86 @retval EFI_ABORTED No any resource type.
87 @retval EFI_OUT_OF_RESOURCES No memory available.
88 @retval EFI_UNSUPPORTED Invalid Tag encounted.
89
90 **/
91 EFI_STATUS
92 EFIAPI
93 PciResourceUpdateCheck (
94 IN EFI_PCI_DEVICE_INFO *PciDeviceInfo,
95 OUT VOID *Configuration
96 )
97 {
98 UINT64 Tag;
99 UINT64 *ListPtr;
100 UINT64 *TempListPtr;
101 EFI_PCI_DEVICE_INFO *Header;
102 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *AcpiPtr;
103 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *OldAcpiPtr;
104 EFI_PCI_RESOUCE_DESCRIPTOR *Dsc;
105 EFI_ACPI_END_TAG_DESCRIPTOR *PtrEnd;
106 UINTN Index;
107
108 ASSERT (PciDeviceInfo != NULL);
109
110 //
111 // Initialize the return value to NULL
112 //
113 * (VOID **) Configuration = NULL;
114
115 ListPtr = gIncompatiblePciDeviceListForResource;
116 while (*ListPtr != LIST_END_TAG) {
117
118 Tag = *ListPtr;
119
120 switch (Tag) {
121 case DEVICE_INF_TAG:
122 Header = (EFI_PCI_DEVICE_INFO *) (ListPtr + 1);
123 ListPtr = ListPtr + 1 + sizeof (EFI_PCI_DEVICE_INFO) / sizeof (UINT64);
124
125 if (DeviceCheck (PciDeviceInfo, Header) != EFI_SUCCESS) {
126 continue;
127 }
128
129 //
130 // Matched an item, so construct the ACPI descriptor for the resource.
131 //
132 //
133 // Count the resource items so that to allocate space
134 //
135 for (Index = 0, TempListPtr = ListPtr; *TempListPtr == DEVICE_RES_TAG; Index++) {
136 TempListPtr = TempListPtr + 1 + ((sizeof (EFI_PCI_RESOUCE_DESCRIPTOR)) / sizeof (UINT64));
137 }
138 //
139 // If there is at least one type of resource request,
140 // allocate a acpi resource node
141 //
142 if (Index == 0) {
143 return EFI_ABORTED;
144 }
145
146 AcpiPtr = AllocateZeroPool (
147 sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) * Index + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR)
148 );
149 if (AcpiPtr == NULL) {
150 return EFI_OUT_OF_RESOURCES;
151 }
152
153 OldAcpiPtr = AcpiPtr;
154
155 //
156 // Fill the EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR structure
157 // according to the EFI_PCI_RESOUCE_DESCRIPTOR structure
158 //
159 for (; *ListPtr == DEVICE_RES_TAG;) {
160
161 Dsc = (EFI_PCI_RESOUCE_DESCRIPTOR *) (ListPtr + 1);
162
163 AcpiPtr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
164 AcpiPtr->Len = sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR);
165 AcpiPtr->ResType = (UINT8) Dsc->ResType;
166 AcpiPtr->GenFlag = (UINT8) Dsc->GenFlag;
167 AcpiPtr->SpecificFlag = (UINT8) Dsc->SpecificFlag;
168 AcpiPtr->AddrSpaceGranularity = Dsc->AddrSpaceGranularity;;
169 AcpiPtr->AddrRangeMin = Dsc->AddrRangeMin;
170 AcpiPtr->AddrRangeMax = Dsc->AddrRangeMax;
171 AcpiPtr->AddrTranslationOffset = Dsc->AddrTranslationOffset;
172 AcpiPtr->AddrLen = Dsc->AddrLen;
173
174 ListPtr = ListPtr + 1 + ((sizeof (EFI_PCI_RESOUCE_DESCRIPTOR)) / sizeof (UINT64));
175 AcpiPtr++;
176 }
177 //
178 // put the checksum
179 //
180 PtrEnd = (EFI_ACPI_END_TAG_DESCRIPTOR *) (AcpiPtr);
181 PtrEnd->Desc = ACPI_END_TAG_DESCRIPTOR;
182 PtrEnd->Checksum = 0;
183
184 *(VOID **) Configuration = OldAcpiPtr;
185
186 return EFI_SUCCESS;
187
188 case DEVICE_RES_TAG:
189 //
190 // Adjust the pointer to the next PCI resource descriptor item
191 //
192 ListPtr = ListPtr + 1 + ((sizeof (EFI_PCI_RESOUCE_DESCRIPTOR)) / sizeof (UINT64));
193 break;
194
195 default:
196 return EFI_UNSUPPORTED;
197 }
198 }
199
200 return EFI_UNSUPPORTED;
201
202 }
203
204 /**
205 Check the incompatible device list and return configuraton register mask values.
206
207 This function searches the incompatible device list according to request
208 information. If the PCI device belongs to the devices list, corresponding
209 configuration informtion will be returned, in the meantime return EFI_SUCCESS.
210
211 @param PciDeviceInfo A pointer to EFI_PCI_DEVICE_INFO.
212 @param AccessType Access Type, READ or WRITE.
213 @param Offset The address within the PCI configuration space.
214 @param Configuration Returned information.
215
216 @retval EFI_SUCCESS If check incompatible device successfully.
217 @retval EFI_UNSUPPORTED Failed to check incompatibility device.
218
219 **/
220 EFI_STATUS
221 EFIAPI
222 PciRegisterUpdateCheck (
223 IN EFI_PCI_DEVICE_INFO *PciDeviceInfo,
224 IN UINT64 AccessType,
225 IN UINT64 Offset,
226 OUT VOID *Configuration
227 )
228 {
229 EFI_PCI_DEVICE_INFO *Header;
230 UINT64 Tag;
231 UINT64 *ListPtr;
232 EFI_PCI_REGISTER_VALUE_DATA *RegisterPtr;
233 EFI_PCI_REGISTER_VALUE_DATA *Dsc;
234
235 ASSERT (PciDeviceInfo != NULL);
236
237 ListPtr = gIncompatiblePciDeviceListForRegister;
238
239 //
240 // Initialize the return value to NULL
241 //
242 * (VOID **) Configuration = NULL;
243
244 while (*ListPtr != LIST_END_TAG) {
245
246 Tag = *ListPtr;
247
248 switch (Tag) {
249 case DEVICE_INF_TAG:
250 Header = (EFI_PCI_DEVICE_INFO *) (ListPtr + 1);
251 ListPtr = ListPtr + 1 + sizeof (EFI_PCI_DEVICE_INFO) / sizeof (UINT64);
252
253 //
254 // Check whether the PCI device matches the device in the incompatible devices list?
255 // If not, ship next
256 //
257 if (DeviceCheck (PciDeviceInfo, Header) != EFI_SUCCESS) {
258 continue;
259 }
260
261 //
262 // Matched an item, check whether access matches?
263 //
264 for (; *ListPtr == DEVICE_RES_TAG;) {
265 ListPtr ++;
266 if (((EFI_PCI_REGISTER_VALUE_DESCRIPTOR *)ListPtr)->Offset == (Offset & 0xfc)) {
267 if (((EFI_PCI_REGISTER_VALUE_DESCRIPTOR *)ListPtr)->AccessType == AccessType) {
268
269 Dsc = (EFI_PCI_REGISTER_VALUE_DATA *) (ListPtr + 2);
270
271 RegisterPtr = &mPciRegisterValueData;
272
273 RegisterPtr->AndValue = Dsc->AndValue;
274 RegisterPtr->OrValue = Dsc->OrValue;
275
276 *(VOID **) Configuration = RegisterPtr;
277
278 return EFI_SUCCESS;
279 }
280 }
281 ListPtr += sizeof (EFI_PCI_REGISTER_VALUE_DESCRIPTOR) / (sizeof (UINT64));
282 }
283 return EFI_UNSUPPORTED;
284
285 case DEVICE_RES_TAG:
286 //
287 // Adjust the pointer to the next item
288 //
289 ListPtr = ListPtr + 1 + ((sizeof (EFI_PCI_REGISTER_VALUE_DESCRIPTOR)) / sizeof (UINT64));
290 break;
291
292 default:
293 return EFI_UNSUPPORTED;
294 }
295 }
296
297 return EFI_UNSUPPORTED;
298 }
299
300 /**
301 Check the incompatible device list for access width incompatibility and
302 return the configuration
303
304 This function searches the incompatible device list for access width
305 incompatibility according to request information. If the PCI device
306 belongs to the devices list, corresponding configuration informtion
307 will be returned, in the meantime return EFI_SUCCESS.
308
309 @param PciDeviceInfo A pointer to PCI device information.
310 @param AccessType Access type, READ or WRITE.
311 @param Offset The address within the PCI configuration space.
312 @param AccessWidth Access width needs to check incompatibility.
313 @param Configuration Returned information.
314
315 @retval EFI_SUCCESS If check incompatible device successfully.
316 @retval EFI_UNSUPPORTED Failed to check incompatibility device.
317
318 **/
319 EFI_STATUS
320 EFIAPI
321 PciRegisterAccessCheck (
322 IN EFI_PCI_DEVICE_INFO *PciDeviceInfo,
323 IN UINT64 AccessType,
324 IN UINT64 Offset,
325 IN UINT64 AccessWidth,
326 OUT VOID *Configuration
327 )
328 {
329 EFI_PCI_DEVICE_INFO *Header;
330 UINT64 Tag;
331 UINT64 *ListPtr;
332 EFI_PCI_REGISTER_ACCESS_DATA *RegisterPtr;
333 EFI_PCI_REGISTER_ACCESS_DATA *Dsc;
334
335 ASSERT (PciDeviceInfo != NULL);
336
337 ListPtr = gDeviceListForAccessWidth;
338
339 //
340 // Initialize the return value to NULL
341 //
342 * (VOID **) Configuration = NULL;
343
344 while (*ListPtr != LIST_END_TAG) {
345
346 Tag = *ListPtr;
347
348 switch (Tag) {
349 case DEVICE_INF_TAG:
350 Header = (EFI_PCI_DEVICE_INFO *) (ListPtr + 1);
351 ListPtr = ListPtr + 1 + sizeof (EFI_PCI_DEVICE_INFO) / sizeof (UINT64);
352
353 //
354 // Check whether the PCI device matches the device in the incompatible devices list?
355 // If not, ship next
356 //
357 if (DeviceCheck (PciDeviceInfo, Header) != EFI_SUCCESS) {
358 continue;
359 }
360
361 //
362 // Matched an item, check whether access matches?
363 //
364 for (; *ListPtr == DEVICE_RES_TAG;) {
365 ListPtr ++;
366 if (((EFI_PCI_REGISTER_ACCESS_DESCRIPTOR *) ListPtr)->AccessType == AccessType &&
367 ((EFI_PCI_REGISTER_ACCESS_DESCRIPTOR *) ListPtr)->AccessWidth == AccessWidth ) {
368
369 Dsc = (EFI_PCI_REGISTER_ACCESS_DATA *) (ListPtr + 2);
370
371 if((Dsc->StartOffset <= Offset) && (Dsc->EndOffset > Offset)) {
372
373 RegisterPtr = &mPciRegisterAccessData;
374
375 RegisterPtr->StartOffset = Dsc->StartOffset;
376 RegisterPtr->EndOffset = Dsc->EndOffset;
377 RegisterPtr->Width = Dsc->Width;
378
379 *(VOID **) Configuration = RegisterPtr;
380
381 return EFI_SUCCESS;
382 }
383 }
384 ListPtr += sizeof (EFI_PCI_REGISTER_ACCESS_DESCRIPTOR) / (sizeof (UINT64));
385 }
386 return EFI_UNSUPPORTED;
387
388 case DEVICE_RES_TAG:
389 //
390 // Adjust the pointer to the next item
391 //
392 ListPtr = ListPtr + 1 + ((sizeof (EFI_PCI_REGISTER_ACCESS_DESCRIPTOR)) / sizeof (UINT64));
393 break;
394
395 default:
396 return EFI_UNSUPPORTED;
397 }
398 }
399
400 return EFI_UNSUPPORTED;
401 }
402