2 * PROJECT: ReactOS PCI Bus Driver
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: drivers/bus/pci/enum.c
5 * PURPOSE: PCI Bus/Device Enumeration
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 /* GLOBALS ********************************************************************/
17 PIO_RESOURCE_REQUIREMENTS_LIST PciZeroIoResourceRequirements
;
19 PCI_CONFIGURATOR PciConfigurators
[] =
22 Device_MassageHeaderForLimitsDetermination
,
23 Device_RestoreCurrent
,
25 Device_SaveCurrentSettings
,
26 Device_ChangeResourceSettings
,
27 Device_GetAdditionalResourceDescriptors
,
31 PPBridge_MassageHeaderForLimitsDetermination
,
32 PPBridge_RestoreCurrent
,
34 PPBridge_SaveCurrentSettings
,
35 PPBridge_ChangeResourceSettings
,
36 PPBridge_GetAdditionalResourceDescriptors
,
40 Cardbus_MassageHeaderForLimitsDetermination
,
41 Cardbus_RestoreCurrent
,
43 Cardbus_SaveCurrentSettings
,
44 Cardbus_ChangeResourceSettings
,
45 Cardbus_GetAdditionalResourceDescriptors
,
50 /* FUNCTIONS ******************************************************************/
54 PciComputeNewCurrentSettings(IN PPCI_PDO_EXTENSION PdoExtension
,
55 IN PCM_RESOURCE_LIST ResourceList
)
57 PCM_PARTIAL_RESOURCE_DESCRIPTOR Partial
, InterruptResource
;
58 PCM_PARTIAL_RESOURCE_DESCRIPTOR BaseResource
, CurrentDescriptor
;
59 PCM_PARTIAL_RESOURCE_DESCRIPTOR PreviousDescriptor
;
60 CM_PARTIAL_RESOURCE_DESCRIPTOR ResourceArray
[7];
61 PCM_FULL_RESOURCE_DESCRIPTOR FullList
;
62 BOOLEAN DrainPartial
, RangeChange
;
64 PPCI_FUNCTION_RESOURCES PciResources
;
67 /* Make sure we have either no resources, or at least one */
68 ASSERT((ResourceList
== NULL
) || (ResourceList
->Count
== 1));
70 /* Initialize no partial, interrupt descriptor, or range change */
72 InterruptResource
= NULL
;
75 /* Check if there's not actually any resources */
76 if (!(ResourceList
) || !(ResourceList
->Count
))
78 /* Then just return the hardware update state */
79 return PdoExtension
->UpdateHardware
;
82 /* Print the new specified resource list */
83 PciDebugPrintCmResList(ResourceList
);
85 /* Clear the temporary resource array */
86 for (i
= 0; i
< 7; i
++) ResourceArray
[i
].Type
= CmResourceTypeNull
;
88 /* Loop the full resource descriptor */
89 FullList
= ResourceList
->List
;
90 for (i
= 0; i
< ResourceList
->Count
; i
++)
92 /* Initialize loop variables */
96 /* Loop the partial descriptors */
97 Partial
= FullList
->PartialResourceList
.PartialDescriptors
;
98 for (j
= 0; j
< FullList
->PartialResourceList
.Count
; j
++)
100 /* Check if we were supposed to drain a partial due to device data */
103 /* Draining complete, move on to the next descriptor then */
108 /* Check what kind of descriptor this was */
109 switch (Partial
->Type
)
111 /* Base BAR resources */
112 case CmResourceTypePort
:
113 case CmResourceTypeMemory
:
115 /* Set it as the base */
116 ASSERT(BaseResource
== NULL
);
117 BaseResource
= Partial
;
120 /* Interrupt resource */
121 case CmResourceTypeInterrupt
:
123 /* Make sure it's a compatible (and the only) PCI interrupt */
124 ASSERT(InterruptResource
== NULL
);
125 ASSERT(Partial
->u
.Interrupt
.Level
== Partial
->u
.Interrupt
.Vector
);
126 InterruptResource
= Partial
;
128 /* Only 255 interrupts on x86/x64 hardware */
129 if (Partial
->u
.Interrupt
.Level
< 256)
131 /* Use the passed interrupt line */
132 PdoExtension
->AdjustedInterruptLine
= Partial
->u
.Interrupt
.Level
;
136 /* Invalid vector, so ignore it */
137 PdoExtension
->AdjustedInterruptLine
= 0;
142 /* Check for specific device data */
143 case CmResourceTypeDevicePrivate
:
145 /* Check what kind of data this was */
146 switch (Partial
->u
.DevicePrivate
.Data
[0])
148 /* Not used in the driver yet */
154 /* Not used in the driver yet */
160 /* A drain request */
162 /* Shouldn't be a base resource, this is a drain */
163 ASSERT(BaseResource
== NULL
);
164 DrainPartial
= Partial
->u
.DevicePrivate
.Data
[1];
165 ASSERT(DrainPartial
== TRUE
);
171 /* Move to the next descriptor */
172 Partial
= PciNextPartialDescriptor(Partial
);
175 /* We should be starting a new list now */
176 ASSERT(BaseResource
== NULL
);
177 FullList
= (PVOID
)Partial
;
180 /* Check the current assigned PCI resources */
181 PciResources
= PdoExtension
->Resources
;
182 if (!PciResources
) return FALSE
;
184 //if... // MISSING CODE
186 DPRINT1("Missing sanity checking code!\n");
188 /* Loop all the PCI function resources */
189 for (i
= 0; i
< 7; i
++)
191 /* Get the current function resource descriptor, and the new one */
192 CurrentDescriptor
= &PciResources
->Current
[i
];
193 Partial
= &ResourceArray
[i
];
195 /* Previous is current during the first loop iteration */
196 PreviousDescriptor
= &PciResources
->Current
[(i
== 0) ? (0) : (i
- 1)];
198 /* Check if this new descriptor is different than the old one */
199 if (((Partial
->Type
!= CurrentDescriptor
->Type
) ||
200 (Partial
->Type
!= CmResourceTypeNull
)) &&
201 ((Partial
->u
.Generic
.Start
.QuadPart
!=
202 CurrentDescriptor
->u
.Generic
.Start
.QuadPart
) ||
203 (Partial
->u
.Generic
.Length
!= CurrentDescriptor
->u
.Generic
.Length
)))
205 /* Record a change */
208 /* Was there a range before? */
209 if (CurrentDescriptor
->Type
!= CmResourceTypeNull
)
212 DbgPrint(" Old range-\n");
213 PciDebugPrintPartialResource(CurrentDescriptor
);
217 /* There was no range */
218 DbgPrint(" Previously unset range\n");
222 DbgPrint(" changed to\n");
223 PciDebugPrintPartialResource(Partial
);
225 /* Update to new range */
226 CurrentDescriptor
->Type
= Partial
->Type
;
227 PreviousDescriptor
->u
.Generic
.Start
= Partial
->u
.Generic
.Start
;
228 PreviousDescriptor
->u
.Generic
.Length
= Partial
->u
.Generic
.Length
;
229 CurrentDescriptor
= PreviousDescriptor
;
233 /* Either the hardware was updated, or a resource range changed */
234 return ((RangeChange
) || (PdoExtension
->UpdateHardware
));
239 PcipUpdateHardware(IN PVOID Context
,
242 PPCI_PDO_EXTENSION PdoExtension
= Context
;
243 PPCI_COMMON_HEADER PciData
= Context2
;
245 /* Check if we're allowed to disable decodes */
246 PciData
->Command
= PdoExtension
->CommandEnables
;
247 if (!(PdoExtension
->HackFlags
& PCI_HACK_PRESERVE_COMMAND
))
249 /* Disable all decodes */
250 PciData
->Command
&= ~(PCI_ENABLE_IO_SPACE
|
251 PCI_ENABLE_MEMORY_SPACE
|
252 PCI_ENABLE_BUS_MASTER
|
253 PCI_ENABLE_WRITE_AND_INVALIDATE
);
256 /* Update the device configuration */
258 PciWriteDeviceConfig(PdoExtension
, PciData
, 0, PCI_COMMON_HDR_LENGTH
);
260 /* Turn decodes back on */
261 PciDecodeEnable(PdoExtension
, TRUE
, &PdoExtension
->CommandEnables
);
266 PciUpdateHardware(IN PPCI_PDO_EXTENSION PdoExtension
,
267 IN PPCI_COMMON_HEADER PciData
)
269 PCI_IPI_CONTEXT Context
;
271 /* Check for critical devices and PCI Debugging devices */
272 if ((PdoExtension
->HackFlags
& PCI_HACK_CRITICAL_DEVICE
) ||
273 (PdoExtension
->OnDebugPath
))
275 /* Build the context and send an IPI */
276 Context
.RunCount
= 1;
278 Context
.Context
= PciData
;
279 Context
.Function
= PcipUpdateHardware
;
280 Context
.DeviceExtension
= PdoExtension
;
281 KeIpiGenericCall(PciExecuteCriticalSystemRoutine
, (ULONG_PTR
)&Context
);
285 /* Just to the update inline */
286 PcipUpdateHardware(PdoExtension
, PciData
);
290 PIO_RESOURCE_REQUIREMENTS_LIST
292 PciAllocateIoRequirementsList(IN ULONG Count
,
297 PIO_RESOURCE_REQUIREMENTS_LIST RequirementsList
;
299 /* Calculate the final size of the list, including each descriptor */
300 Size
= sizeof(IO_RESOURCE_REQUIREMENTS_LIST
);
301 if (Count
> 1) Size
= sizeof(IO_RESOURCE_DESCRIPTOR
) * (Count
- 1) +
302 sizeof(IO_RESOURCE_REQUIREMENTS_LIST
);
304 /* Allocate the list */
305 RequirementsList
= ExAllocatePoolWithTag(PagedPool
, Size
, 'BicP');
306 if (!RequirementsList
) return NULL
;
309 RtlZeroMemory(RequirementsList
, Size
);
310 RequirementsList
->AlternativeLists
= 1;
311 RequirementsList
->BusNumber
= BusNumber
;
312 RequirementsList
->SlotNumber
= SlotNumber
;
313 RequirementsList
->InterfaceType
= PCIBus
;
314 RequirementsList
->ListSize
= Size
;
315 RequirementsList
->List
[0].Count
= Count
;
316 RequirementsList
->List
[0].Version
= 1;
317 RequirementsList
->List
[0].Revision
= 1;
320 return RequirementsList
;
325 PciAllocateCmResourceList(IN ULONG Count
,
329 PCM_RESOURCE_LIST ResourceList
;
331 /* Calculate the final size of the list, including each descriptor */
332 Size
= sizeof(CM_RESOURCE_LIST
);
333 if (Count
> 1) Size
= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
) * (Count
- 1) +
334 sizeof(CM_RESOURCE_LIST
);
336 /* Allocate the list */
337 ResourceList
= ExAllocatePoolWithTag(PagedPool
, Size
, 'BicP');
338 if (!ResourceList
) return NULL
;
341 RtlZeroMemory(ResourceList
, Size
);
342 ResourceList
->Count
= 1;
343 ResourceList
->List
[0].BusNumber
= BusNumber
;
344 ResourceList
->List
[0].InterfaceType
= PCIBus
;
345 ResourceList
->List
[0].PartialResourceList
.Version
= 1;
346 ResourceList
->List
[0].PartialResourceList
.Revision
= 1;
347 ResourceList
->List
[0].PartialResourceList
.Count
= Count
;
355 PciQueryResources(IN PPCI_PDO_EXTENSION PdoExtension
,
356 OUT PCM_RESOURCE_LIST
*Buffer
)
358 PPCI_FUNCTION_RESOURCES PciResources
;
359 BOOLEAN HaveVga
, HaveMemSpace
, HaveIoSpace
;
360 USHORT BridgeControl
, PciCommand
;
362 PCM_PARTIAL_RESOURCE_DESCRIPTOR Partial
, Resource
, LastResource
;
363 PCM_RESOURCE_LIST ResourceList
;
372 /* Make sure there's some resources to query */
373 PciResources
= PdoExtension
->Resources
;
374 if (!PciResources
) return STATUS_SUCCESS
;
376 /* Read the decodes */
377 PciReadDeviceConfig(PdoExtension
,
379 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
382 /* Check which ones are turned on */
383 HaveIoSpace
= PciCommand
& PCI_ENABLE_IO_SPACE
;
384 HaveMemSpace
= PciCommand
& PCI_ENABLE_MEMORY_SPACE
;
386 /* Loop maximum possible descriptors */
387 for (i
= 0; i
< 7; i
++)
389 /* Check if the decode for this descriptor is actually turned on */
390 Partial
= &PciResources
->Current
[i
];
391 if (((HaveMemSpace
) && (Partial
->Type
== CmResourceTypeMemory
)) ||
392 ((HaveIoSpace
) && (Partial
->Type
== CmResourceTypePort
)))
394 /* One more fully active descriptor */
399 /* If there's an interrupt pin associated, check at least one decode is on */
400 if ((PdoExtension
->InterruptPin
) && ((HaveMemSpace
) || (HaveIoSpace
)))
402 /* Read the interrupt line for the pin, add a descriptor if it's valid */
403 InterruptLine
= PdoExtension
->AdjustedInterruptLine
;
404 if ((InterruptLine
) && (InterruptLine
!= -1)) Count
++;
407 /* Check for PCI bridge */
408 if (PdoExtension
->HeaderType
== PCI_BRIDGE_TYPE
)
410 /* Read bridge settings, check if VGA is present */
411 PciReadDeviceConfig(PdoExtension
,
413 FIELD_OFFSET(PCI_COMMON_HEADER
, u
.type1
.BridgeControl
),
415 if (BridgeControl
& PCI_ENABLE_BRIDGE_VGA
)
417 /* Remember for later */
420 /* One memory descriptor for 0xA0000, plus the two I/O port ranges */
421 if (HaveMemSpace
) Count
++;
422 if (HaveIoSpace
) Count
+= 2;
426 /* If there's no descriptors in use, there's no resources, so return */
427 if (!Count
) return STATUS_SUCCESS
;
429 /* Allocate a resource list to hold the resources */
430 ResourceList
= PciAllocateCmResourceList(Count
,
431 PdoExtension
->ParentFdoExtension
->BaseBus
);
432 if (!ResourceList
) return STATUS_INSUFFICIENT_RESOURCES
;
434 /* This is where the descriptors will be copied into */
435 Resource
= ResourceList
->List
[0].PartialResourceList
.PartialDescriptors
;
436 LastResource
= Resource
+ Count
+ 1;
438 /* Loop maximum possible descriptors */
439 for (i
= 0; i
< 7; i
++)
441 /* Check if the decode for this descriptor is actually turned on */
442 Partial
= &PciResources
->Current
[i
];
443 if (((HaveMemSpace
) && (Partial
->Type
== CmResourceTypeMemory
)) ||
444 ((HaveIoSpace
) && (Partial
->Type
== CmResourceTypePort
)))
446 /* Copy the descriptor into the resource list */
447 *Resource
++ = *Partial
;
451 /* Check if earlier the code detected this was a PCI bridge with VGA on it */
454 /* Are the memory decodes enabled? */
457 /* Build a memory descriptor for a 128KB framebuffer at 0xA0000 */
458 Resource
->Flags
= CM_RESOURCE_MEMORY_READ_WRITE
;
459 Resource
->u
.Generic
.Start
.HighPart
= 0;
460 Resource
->Type
= CmResourceTypeMemory
;
461 Resource
->u
.Generic
.Start
.LowPart
= 0xA0000;
462 Resource
->u
.Generic
.Length
= 0x20000;
466 /* Are the I/O decodes enabled? */
469 /* Build an I/O descriptor for the graphic ports at 0x3B0 */
470 Resource
->Type
= CmResourceTypePort
;
471 Resource
->Flags
= CM_RESOURCE_PORT_POSITIVE_DECODE
| CM_RESOURCE_PORT_10_BIT_DECODE
;
472 Resource
->u
.Port
.Start
.QuadPart
= 0x3B0u
;
473 Resource
->u
.Port
.Length
= 0xC;
476 /* Build an I/O descriptor for the graphic ports at 0x3C0 */
477 Resource
->Type
= CmResourceTypePort
;
478 Resource
->Flags
= CM_RESOURCE_PORT_POSITIVE_DECODE
| CM_RESOURCE_PORT_10_BIT_DECODE
;
479 Resource
->u
.Port
.Start
.QuadPart
= 0x3C0u
;
480 Resource
->u
.Port
.Length
= 0x20;
485 /* If there's an interrupt pin associated, check at least one decode is on */
486 if ((PdoExtension
->InterruptPin
) && ((HaveMemSpace
) || (HaveIoSpace
)))
488 /* Read the interrupt line for the pin, check if it's valid */
489 InterruptLine
= PdoExtension
->AdjustedInterruptLine
;
490 if ((InterruptLine
) && (InterruptLine
!= -1))
492 /* Make sure there's still space */
493 ASSERT(Resource
< LastResource
);
495 /* Add the interrupt descriptor */
496 Resource
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
497 Resource
->Type
= CmResourceTypeInterrupt
;
498 Resource
->ShareDisposition
= CmResourceShareShared
;
499 Resource
->u
.Interrupt
.Affinity
= -1;
500 Resource
->u
.Interrupt
.Level
= InterruptLine
;
501 Resource
->u
.Interrupt
.Vector
= InterruptLine
;
505 /* Return the resouce list */
506 *Buffer
= ResourceList
;
507 return STATUS_SUCCESS
;
512 PciQueryTargetDeviceRelations(IN PPCI_PDO_EXTENSION PdoExtension
,
513 IN OUT PDEVICE_RELATIONS
*pDeviceRelations
)
515 PDEVICE_RELATIONS DeviceRelations
;
518 /* If there were existing relations, free them */
519 if (*pDeviceRelations
) ExFreePoolWithTag(*pDeviceRelations
, 0);
521 /* Allocate a new structure for the relations */
522 DeviceRelations
= ExAllocatePoolWithTag(NonPagedPool
,
523 sizeof(DEVICE_RELATIONS
),
525 if (!DeviceRelations
) return STATUS_INSUFFICIENT_RESOURCES
;
527 /* Only one relation: the PDO */
528 DeviceRelations
->Count
= 1;
529 DeviceRelations
->Objects
[0] = PdoExtension
->PhysicalDeviceObject
;
530 ObReferenceObject(DeviceRelations
->Objects
[0]);
532 /* Return the new relations */
533 *pDeviceRelations
= DeviceRelations
;
534 return STATUS_SUCCESS
;
539 PciQueryEjectionRelations(IN PPCI_PDO_EXTENSION PdoExtension
,
540 IN OUT PDEVICE_RELATIONS
*pDeviceRelations
)
542 /* Not yet implemented */
549 PciBuildRequirementsList(IN PPCI_PDO_EXTENSION PdoExtension
,
550 IN PPCI_COMMON_HEADER PciData
,
551 OUT PIO_RESOURCE_REQUIREMENTS_LIST
* Buffer
)
553 PIO_RESOURCE_REQUIREMENTS_LIST RequirementsList
;
555 /* There aren't, so use the zero descriptor */
556 RequirementsList
= PciZeroIoResourceRequirements
;
558 /* Does it actually exist yet? */
559 if (!PciZeroIoResourceRequirements
)
561 /* Allocate it, and use it for future use */
562 RequirementsList
= PciAllocateIoRequirementsList(0, 0, 0);
563 PciZeroIoResourceRequirements
= RequirementsList
;
564 if (!PciZeroIoResourceRequirements
) return STATUS_INSUFFICIENT_RESOURCES
;
567 /* Return the zero requirements list to the caller */
568 *Buffer
= RequirementsList
;
569 DPRINT1("PCI - build resource reqs - early out, 0 resources\n");
570 return STATUS_SUCCESS
;
572 return STATUS_SUCCESS
;
577 PciQueryRequirements(IN PPCI_PDO_EXTENSION PdoExtension
,
578 IN OUT PIO_RESOURCE_REQUIREMENTS_LIST
*RequirementsList
)
581 PCI_COMMON_HEADER PciHeader
;
584 /* Check if the PDO has any resources, or at least an interrupt pin */
585 if ((PdoExtension
->Resources
) || (PdoExtension
->InterruptPin
))
587 /* Read the current PCI header */
588 PciReadDeviceConfig(PdoExtension
, &PciHeader
, 0, PCI_COMMON_HDR_LENGTH
);
590 /* Use it to build a list of requirements */
591 Status
= PciBuildRequirementsList(PdoExtension
, &PciHeader
, RequirementsList
);
592 if (!NT_SUCCESS(Status
)) return Status
;
594 /* Is this a Compaq PCI Hotplug Controller (r17) on a PAE system ? */
595 if ((PciHeader
.VendorID
== 0xE11) &&
596 (PciHeader
.DeviceID
== 0xA0F7) &&
597 (PciHeader
.RevisionID
== 17) &&
598 (ExIsProcessorFeaturePresent(PF_PAE_ENABLED
)))
600 /* Have not tested this on eVb's machine yet */
605 /* Check if the requirements are actually the zero list */
606 if (*RequirementsList
== PciZeroIoResourceRequirements
)
608 /* A simple NULL will sufficie for the PnP Manager */
609 *RequirementsList
= NULL
;
610 DPRINT1("Returning NULL requirements list\n");
614 /* Otherwise, print out the requirements list */
615 PciDebugPrintIoResReqList(*RequirementsList
);
620 /* There aren't any resources, so simply return NULL */
621 DPRINT1("PciQueryRequirements returning NULL requirements list\n");
622 *RequirementsList
= NULL
;
625 /* This call always succeeds (but maybe with no requirements) */
626 return STATUS_SUCCESS
;
630 * 7. The IO/MEM/Busmaster decodes are disabled for the device.
631 * 8. The PCI bus driver sets the operating mode bits of the Programming
632 * Interface byte to switch the controller to native mode.
634 * Important: When the controller is set to native mode, it must quiet itself
635 * and must not decode I/O resources or generate interrupts until the operating
636 * system has enabled the ports in the PCI configuration header.
637 * The IO/MEM/BusMaster bits will be disabled before the mode change, but it
638 * is not possible to disable interrupts on the device. The device must not
639 * generate interrupts (either legacy or native mode) while the decodes are
640 * disabled in the command register.
642 * This operation is expected to be instantaneous and the operating system does
643 * not stall afterward. It is also expected that the interrupt pin register in
644 * the PCI Configuration space for this device is accurate. The operating system
645 * re-reads this data after previously ignoring it.
649 PciConfigureIdeController(IN PPCI_PDO_EXTENSION PdoExtension
,
650 IN PPCI_COMMON_HEADER PciData
,
653 UCHAR MasterMode
, SlaveMode
, MasterFixed
, SlaveFixed
, ProgIf
, NewProgIf
;
657 /* Assume it won't work */
660 /* Get master and slave current settings, and programmability flag */
661 ProgIf
= PciData
->ProgIf
;
662 MasterMode
= (ProgIf
& 1) == 1;
663 MasterFixed
= (ProgIf
& 2) == 0;
664 SlaveMode
= (ProgIf
& 4) == 4;
665 SlaveFixed
= (ProgIf
& 8) == 0;
668 * [..] In order for Windows XP SP1 and Windows Server 2003 to switch an ATA
669 * ATA controller from compatible mode to native mode, the following must be
672 * - The controller must indicate in its programming interface that both channels
673 * can be switched to native mode. Windows XP SP1 and Windows Server 2003 do
674 * not support switching only one IDE channel to native mode. See the PCI IDE
675 * Controller Specification Revision 1.0 for details.
677 if ((MasterMode
!= SlaveMode
) || (MasterFixed
!= SlaveFixed
))
679 /* Windows does not support this configuration, fail */
680 DPRINT1("PCI: Warning unsupported IDE controller configuration for VEN_%04x&DEV_%04x!",
681 PdoExtension
->VendorId
,
682 PdoExtension
->DeviceId
);
686 /* Check if the controller is already in native mode */
687 if ((MasterMode
) && (SlaveMode
))
689 /* Check if I/O decodes should be disabled */
690 if ((Initial
) || (PdoExtension
->IoSpaceUnderNativeIdeControl
))
692 /* Read the current command */
693 PciReadDeviceConfig(PdoExtension
,
695 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
698 /* Disable I/O space decode */
699 Command
&= ~PCI_ENABLE_IO_SPACE
;
701 /* Update new command in PCI IDE controller */
702 PciWriteDeviceConfig(PdoExtension
,
704 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
707 /* Save updated command value */
708 PciData
->Command
= Command
;
711 /* The controller is now in native mode */
714 else if (!(MasterFixed
) &&
716 (PdoExtension
->BIOSAllowsIDESwitchToNativeMode
) &&
717 !(PdoExtension
->HackFlags
& PCI_HACK_DISABLE_IDE_NATIVE_MODE
))
719 /* Turn off decodes */
720 PciDecodeEnable(PdoExtension
, FALSE
, NULL
);
722 /* Update the current command */
723 PciReadDeviceConfig(PdoExtension
,
725 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
728 /* Enable native mode */
729 ProgIf
= PciData
->ProgIf
| 5;
730 PciWriteDeviceConfig(PdoExtension
,
732 FIELD_OFFSET(PCI_COMMON_HEADER
, ProgIf
),
735 /* Verify the setting "stuck" */
736 PciReadDeviceConfig(PdoExtension
,
738 FIELD_OFFSET(PCI_COMMON_HEADER
, ProgIf
),
740 if (NewProgIf
== ProgIf
)
742 /* Update the header and PDO data with the new programming mode */
743 PciData
->ProgIf
= ProgIf
;
744 PdoExtension
->ProgIf
= NewProgIf
;
746 /* Clear the first four BARs to reset current BAR setttings */
747 PciData
->u
.type0
.BaseAddresses
[0] = 0;
748 PciData
->u
.type0
.BaseAddresses
[1] = 0;
749 PciData
->u
.type0
.BaseAddresses
[2] = 0;
750 PciData
->u
.type0
.BaseAddresses
[3] = 0;
751 PciWriteDeviceConfig(PdoExtension
,
752 PciData
->u
.type0
.BaseAddresses
,
753 FIELD_OFFSET(PCI_COMMON_HEADER
,
754 u
.type0
.BaseAddresses
),
757 /* Re-read the BARs to have the latest data for native mode IDE */
758 PciReadDeviceConfig(PdoExtension
,
759 PciData
->u
.type0
.BaseAddresses
,
760 FIELD_OFFSET(PCI_COMMON_HEADER
,
761 u
.type0
.BaseAddresses
),
764 /* Re-read the interrupt pin used for native mode IDE */
765 PciReadDeviceConfig(PdoExtension
,
766 &PciData
->u
.type0
.InterruptPin
,
767 FIELD_OFFSET(PCI_COMMON_HEADER
,
768 u
.type0
.InterruptPin
),
771 /* The IDE Controller is now in native mode */
776 /* Settings did not work, fail */
777 DPRINT1("PCI: Warning failed switch to native mode for IDE controller VEN_%04x&DEV_%04x!",
783 /* Return whether or not native mode was enabled on the IDE controller */
789 PciApplyHacks(IN PPCI_FDO_EXTENSION DeviceExtension
,
790 IN PPCI_COMMON_HEADER PciData
,
791 IN PCI_SLOT_NUMBER SlotNumber
,
792 IN ULONG OperationType
,
793 PPCI_PDO_EXTENSION PdoExtension
)
795 ULONG LegacyBaseAddress
;
799 /* Check what kind of hack operation this is */
800 switch (OperationType
)
803 * This is mostly concerned with fixing up incorrect class data that can
804 * exist on certain PCI hardware before the 2.0 spec was ratified.
806 case PCI_HACK_FIXUP_BEFORE_CONFIGURATION
:
808 /* Note that the i82375 PCI/EISA and the i82378 PCI/ISA bridges that
809 * are present on certain DEC/NT Alpha machines are pre-PCI 2.0 devices
810 * and appear as non-classified, so their correct class/subclass data
811 * is written here instead.
813 if ((PciData
->VendorID
== 0x8086) &&
814 ((PciData
->DeviceID
== 0x482) || (PciData
->DeviceID
== 0x484)))
816 /* Note that 0x482 is the i82375 (EISA), 0x484 is the i82378 (ISA) */
817 PciData
->SubClass
= PciData
->DeviceID
== 0x482 ?
818 PCI_SUBCLASS_BR_EISA
: PCI_SUBCLASS_BR_ISA
;
819 PciData
->BaseClass
= PCI_CLASS_BRIDGE_DEV
;
822 * Because the software is modifying the actual header data from
823 * the BIOS, this flag tells the driver to ignore failures when
824 * comparing the original BIOS data with the PCI data.
826 if (PdoExtension
) PdoExtension
->ExpectedWritebackFailure
= TRUE
;
829 /* Note that in this case, an immediate return is issued */
833 * This is concerned with setting up interrupts correctly for native IDE
834 * mode, but will also handle broken VGA decoding on older bridges as
835 * well as a PAE-specific hack for certain Compaq Hot-Plug Controllers.
837 case PCI_HACK_FIXUP_AFTER_CONFIGURATION
:
839 /* There should always be a PDO extension passed in */
840 ASSERT(PdoExtension
);
843 * On the OPTi Viper-M IDE controller, Linux doesn't support IDE-DMA
844 * and FreeBSD bug reports indicate that the system crashes when the
845 * feature is enabled (so it's disabled on that OS as well). In the
846 * NT PCI Bus Driver, it seems Microsoft too, completely disables
847 * Native IDE functionality on this controller, so it would seem OPTi
848 * simply frelled up this controller.
850 if ((PciData
->VendorID
== 0x1045) && (PciData
->DeviceID
!= 0xC621))
852 /* Disable native mode */
853 PciData
->ProgIf
&= ~5;
854 PciData
->u
.type0
.InterruptPin
= 0;
857 * Because the software is modifying the actual header data from
858 * the BIOS, this flag tells the driver to ignore failures when
859 * comparing the original BIOS data with the PCI data.
861 PdoExtension
->ExpectedWritebackFailure
= TRUE
;
863 else if ((PciData
->BaseClass
== PCI_CLASS_MASS_STORAGE_CTLR
) &&
864 (PciData
->SubClass
== PCI_SUBCLASS_MSC_IDE_CTLR
))
866 /* For other IDE controllers, start out in compatible mode */
867 PdoExtension
->BIOSAllowsIDESwitchToNativeMode
= FALSE
;
870 * Registry must have enabled native mode (typically as a result
871 * of an INF file directive part of the IDE controller's driver)
872 * and the system must not be booted in Safe Mode. If that checks
873 * out, then evaluate the ACPI NATA method to see if the platform
874 * supports this. See the section "BIOS and Platform Prerequisites
875 * for Switching a Native-Mode-Capable Controller" in the Storage
876 * section of the Windows Driver Kit for more details:
878 * 5. For each ATA controller enumerated, the PCI bus driver checks
879 * the Programming Interface register of the IDE controller to
880 * see if it supports switching both channels to native mode.
881 * 6. The PCI bus driver checks whether the BIOS/platform supports
882 * switching the controller by checking the NATA method described
883 * earlier in this article.
885 * If an ATA controller does not indicate that it is native
886 * mode-capable, or if the BIOS NATA control method is missing
887 * or does not list that device, the PCI bus driver does not
888 * switch the controller and it is assigned legacy resources.
890 * If both the controller and the BIOS indicate that the controller
891 * can be switched, the process of switching the controller begins
892 * with the next step.
894 if ((PciEnableNativeModeATA
) &&
895 !(InitSafeBootMode
) &&
896 (PciIsSlotPresentInParentMethod(PdoExtension
, 'ATAN')))
898 /* The platform supports it, remember that */
899 PdoExtension
->BIOSAllowsIDESwitchToNativeMode
= TRUE
;
902 * Now switch the controller into native mode if both channels
903 * support native IDE mode. See "How Windows Switches an ATA
904 * Controller to Native Mode" in the Storage section of the
905 * Windows Driver Kit for more details.
907 PdoExtension
->IDEInNativeMode
=
908 PciConfigureIdeController(PdoExtension
, PciData
, TRUE
);
911 /* Is native mode enabled after all? */
912 if ((PciData
->ProgIf
& 5) != 5)
914 /* Compatible mode, so force ISA-style IRQ14 and IRQ 15 */
915 PciData
->u
.type0
.InterruptPin
= 0;
919 /* Is this a PCI device with legacy VGA card decodes on the root bus? */
920 if ((PdoExtension
->HackFlags
& PCI_HACK_VIDEO_LEGACY_DECODE
) &&
921 (PCI_IS_ROOT_FDO(DeviceExtension
)) &&
922 !(DeviceExtension
->BrokenVideoHackApplied
))
924 /* Tell the arbiter to apply a hack for these older devices */
925 ario_ApplyBrokenVideoHack(DeviceExtension
);
928 /* Is this a Compaq PCI Hotplug Controller (r17) on a PAE system ? */
929 if ((PciData
->VendorID
== 0xE11) &&
930 (PciData
->DeviceID
== 0xA0F7) &&
931 (PciData
->RevisionID
== 17) &&
932 (ExIsProcessorFeaturePresent(PF_PAE_ENABLED
)))
934 /* Turn off the decodes immediately */
935 PciData
->Command
&= ~(PCI_ENABLE_IO_SPACE
|
936 PCI_ENABLE_MEMORY_SPACE
|
937 PCI_ENABLE_BUS_MASTER
);
938 PciWriteDeviceConfig(PdoExtension
,
940 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
943 /* Do not EVER turn them on again, this will blow up the system */
944 PdoExtension
->CommandEnables
&= ~(PCI_ENABLE_IO_SPACE
|
945 PCI_ENABLE_MEMORY_SPACE
|
946 PCI_ENABLE_BUS_MASTER
);
947 PdoExtension
->HackFlags
|= PCI_HACK_PRESERVE_COMMAND
;
952 * This is called whenever resources are changed and hardware needs to be
953 * updated. It is concerned with two highly specific erratas on an IBM
954 * hot-plug docking bridge used on the Thinkpad 600 Series and on Intel's
957 case PCI_HACK_FIXUP_BEFORE_UPDATE
:
959 /* There should always be a PDO extension passed in */
960 ASSERT(PdoExtension
);
962 /* Is this an IBM 20H2999 PCI Docking Bridge, used on Thinkpads? */
963 if ((PdoExtension
->VendorId
== 0x1014) &&
964 (PdoExtension
->DeviceId
== 0x95))
966 /* Read the current command */
967 PciReadDeviceConfig(PdoExtension
,
969 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
972 /* Turn off the decodes */
973 PciDecodeEnable(PdoExtension
, FALSE
, &Command
);
975 /* Apply the required IBM workaround */
976 PciReadDeviceConfig(PdoExtension
, &RegValue
, 0xE0, sizeof(UCHAR
));
979 PciWriteDeviceConfig(PdoExtension
, &RegValue
, 0xE0, sizeof(UCHAR
));
981 /* Restore the command to its original value */
982 PciWriteDeviceConfig(PdoExtension
,
984 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
990 * Check for Intel ICH PCI-to-PCI (i82801) bridges (used on the i810,
991 * i820, i840, i845 Chipsets) that have subtractive decode enabled,
992 * and whose hack flags do not specifiy that this support is broken.
994 if ((PdoExtension
->HeaderType
== PCI_BRIDGE_TYPE
) &&
995 (PdoExtension
->Dependent
.type1
.SubtractiveDecode
) &&
996 ((PdoExtension
->VendorId
== 0x8086) &&
997 ((PdoExtension
->DeviceId
== 0x2418) ||
998 (PdoExtension
->DeviceId
== 0x2428) ||
999 (PdoExtension
->DeviceId
== 0x244E) ||
1000 (PdoExtension
->DeviceId
== 0x2448))) &&
1001 !(PdoExtension
->HackFlags
& PCI_HACK_BROKEN_SUBTRACTIVE_DECODE
))
1004 * The positive decode window shouldn't be used, these values are
1005 * normally all read-only or initialized to 0 by the BIOS, but
1006 * it appears Intel doesn't do this, so the PCI Bus Driver will
1007 * do it in software instead. Note that this is used to prevent
1008 * certain non-compliant PCI devices from breaking down due to the
1009 * fact that these ICH bridges have a known "quirk" (which Intel
1010 * documents as a known "erratum", although it's not not really
1011 * an ICH bug since the PCI specification does allow for it) in
1012 * that they will sometimes send non-zero addresses during special
1013 * cycles (ie: non-zero data during the address phase). These
1014 * broken PCI cards will mistakenly attempt to claim the special
1015 * cycle and corrupt their I/O and RAM ranges. Again, in Intel's
1016 * defense, the PCI specification only requires stable data, not
1017 * necessarily zero data, during the address phase.
1019 PciData
->u
.type1
.MemoryBase
= 0xFFFF;
1020 PciData
->u
.type1
.PrefetchBase
= 0xFFFF;
1021 PciData
->u
.type1
.IOBase
= 0xFF;
1022 PciData
->u
.type1
.IOLimit
= 0;
1023 PciData
->u
.type1
.MemoryLimit
= 0;
1024 PciData
->u
.type1
.PrefetchLimit
= 0;
1025 PciData
->u
.type1
.PrefetchBaseUpper32
= 0;
1026 PciData
->u
.type1
.PrefetchLimitUpper32
= 0;
1027 PciData
->u
.type1
.IOBaseUpper16
= 0;
1028 PciData
->u
.type1
.IOLimitUpper16
= 0;
1036 /* Finally, also check if this is this a CardBUS device? */
1037 if (PCI_CONFIGURATION_TYPE(PciData
) == PCI_CARDBUS_BRIDGE_TYPE
)
1040 * At offset 44h the LegacyBaseAddress is stored, which is cleared by
1041 * ACPI-aware versions of Windows, to disable legacy-mode I/O access to
1042 * CardBus controllers. For more information, see "Supporting CardBus
1043 * Controllers under ACPI" in the "CardBus Controllers and Windows"
1044 * Whitepaper on WHDC.
1046 LegacyBaseAddress
= 0;
1047 PciWriteDeviceConfig(PdoExtension
,
1049 sizeof(PCI_COMMON_HEADER
) + sizeof(ULONG
),
1056 PcipIsSameDevice(IN PPCI_PDO_EXTENSION DeviceExtension
,
1057 IN PPCI_COMMON_HEADER PciData
)
1059 BOOLEAN IdMatch
, RevMatch
, SubsysMatch
;
1060 ULONGLONG HackFlags
= DeviceExtension
->HackFlags
;
1062 /* Check if the IDs match */
1063 IdMatch
= (PciData
->VendorID
== DeviceExtension
->VendorId
) &&
1064 (PciData
->DeviceID
== DeviceExtension
->DeviceId
);
1065 if (!IdMatch
) return FALSE
;
1067 /* If the device has a valid revision, check if it matches */
1068 RevMatch
= (HackFlags
& PCI_HACK_NO_REVISION_AFTER_D3
) ||
1069 (PciData
->RevisionID
== DeviceExtension
->RevisionId
);
1070 if (!RevMatch
) return FALSE
;
1072 /* For multifunction devices, this is enough to assume they're the same */
1073 if (PCI_MULTIFUNCTION_DEVICE(PciData
)) return TRUE
;
1075 /* For bridge devices, there's also nothing else that can be checked */
1076 if (DeviceExtension
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) return TRUE
;
1078 /* Devices, on the other hand, have subsystem data that can be compared */
1079 SubsysMatch
= (HackFlags
& (PCI_HACK_NO_SUBSYSTEM
|
1080 PCI_HACK_NO_SUBSYSTEM_AFTER_D3
)) ||
1081 ((DeviceExtension
->SubsystemVendorId
==
1082 PciData
->u
.type0
.SubVendorID
) &&
1083 (DeviceExtension
->SubsystemId
==
1084 PciData
->u
.type0
.SubSystemID
));
1090 PciSkipThisFunction(IN PPCI_COMMON_HEADER PciData
,
1091 IN PCI_SLOT_NUMBER Slot
,
1092 IN UCHAR OperationType
,
1093 IN ULONGLONG HackFlags
)
1097 /* Check if this is device enumeration */
1098 if (OperationType
== PCI_SKIP_DEVICE_ENUMERATION
)
1100 /* Check if there's a hackflag saying not to enumerate this device */
1101 if (HackFlags
& PCI_HACK_NO_ENUM_AT_ALL
) break;
1103 /* Check if this is the high end of a double decker device */
1104 if ((HackFlags
& PCI_HACK_DOUBLE_DECKER
) &&
1105 (Slot
.u
.bits
.DeviceNumber
>= 16))
1107 /* It belongs to the same device, so skip it */
1108 DPRINT1(" Device (Ven %04x Dev %04x (d=0x%x, f=0x%x)) is a ghost.\n",
1111 Slot
.u
.bits
.DeviceNumber
,
1112 Slot
.u
.bits
.FunctionNumber
);
1116 else if (OperationType
== PCI_SKIP_RESOURCE_ENUMERATION
)
1118 /* Resource enumeration, check for a hackflag saying not to do it */
1119 if (HackFlags
& PCI_HACK_ENUM_NO_RESOURCE
) break;
1123 /* Logic error in the driver */
1124 ASSERTMSG(FALSE
, "PCI Skip Function - Operation type unknown.");
1127 /* Check for legacy bridges during resource enumeration */
1128 if ((PciData
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
1129 (PciData
->SubClass
<= PCI_SUBCLASS_BR_MCA
) &&
1130 (OperationType
== PCI_SKIP_RESOURCE_ENUMERATION
))
1132 /* Their resources are not enumerated, only PCI and Cardbus/PCMCIA */
1135 else if (PciData
->BaseClass
== PCI_CLASS_NOT_DEFINED
)
1137 /* Undefined base class (usually a PCI BIOS/ROM bug) */
1138 DPRINT1(" Vendor %04x, Device %04x has class code of PCI_CLASS_NOT_DEFINED\n",
1143 * The Alder has an Intel Extended Express System Support Controller
1144 * which presents apparently spurious BARs. When the PCI resource
1145 * code tries to reassign these BARs, the second IO-APIC gets
1146 * disabled (with disastrous consequences). The first BAR is the
1147 * actual IO-APIC, the remaining five bars seem to be spurious
1148 * resources, so ignore this device completely.
1150 if ((PciData
->VendorID
== 0x8086) && (PciData
->DeviceID
== 8)) break;
1153 /* Other normal PCI cards and bridges are enumerated */
1154 if (PCI_CONFIGURATION_TYPE(PciData
) <= PCI_CARDBUS_BRIDGE_TYPE
) return FALSE
;
1157 /* Hit one of the known bugs/hackflags, or this is a new kind of PCI unit */
1158 DPRINT1(" Device skipped (not enumerated).\n");
1164 PciGetEnhancedCapabilities(IN PPCI_PDO_EXTENSION PdoExtension
,
1165 IN PPCI_COMMON_HEADER PciData
)
1167 ULONG HeaderType
, CapPtr
, TargetAgpCapabilityId
;
1168 DEVICE_POWER_STATE WakeLevel
;
1169 PCI_CAPABILITIES_HEADER AgpCapability
;
1170 PCI_PM_CAPABILITY PowerCapabilities
;
1173 /* Assume no known wake level */
1174 PdoExtension
->PowerState
.DeviceWakeLevel
= PowerDeviceUnspecified
;
1176 /* Make sure the device has capabilities */
1177 if (!(PciData
->Status
& PCI_STATUS_CAPABILITIES_LIST
))
1179 /* If it doesn't, there will be no power management */
1180 PdoExtension
->CapabilitiesPtr
= 0;
1181 PdoExtension
->HackFlags
|= PCI_HACK_NO_PM_CAPS
;
1185 /* There's capabilities, need to figure out where to get the offset */
1186 HeaderType
= PCI_CONFIGURATION_TYPE(PciData
);
1187 if (HeaderType
== PCI_CARDBUS_BRIDGE_TYPE
)
1189 /* Use the bridge's header */
1190 CapPtr
= PciData
->u
.type2
.CapabilitiesPtr
;
1194 /* Use the device header */
1195 ASSERT(HeaderType
<= PCI_CARDBUS_BRIDGE_TYPE
);
1196 CapPtr
= PciData
->u
.type0
.CapabilitiesPtr
;
1199 /* Make sure the pointer is spec-aligned and located, and save it */
1200 DPRINT1("Device has capabilities at: %lx\n", CapPtr
);
1201 ASSERT(((CapPtr
& 0x3) == 0) && (CapPtr
>= PCI_COMMON_HDR_LENGTH
));
1202 PdoExtension
->CapabilitiesPtr
= CapPtr
;
1204 /* Check for PCI-to-PCI Bridges and AGP bridges */
1205 if ((PdoExtension
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
1206 ((PdoExtension
->SubClass
== PCI_SUBCLASS_BR_HOST
) ||
1207 (PdoExtension
->SubClass
== PCI_SUBCLASS_BR_PCI_TO_PCI
)))
1209 /* Query either the raw AGP capabilitity, or the Target AGP one */
1210 TargetAgpCapabilityId
= (PdoExtension
->SubClass
==
1211 PCI_SUBCLASS_BR_PCI_TO_PCI
) ?
1212 PCI_CAPABILITY_ID_AGP_TARGET
:
1213 PCI_CAPABILITY_ID_AGP
;
1214 if (PciReadDeviceCapability(PdoExtension
,
1215 PdoExtension
->CapabilitiesPtr
,
1216 TargetAgpCapabilityId
,
1218 sizeof(PCI_CAPABILITIES_HEADER
)))
1220 /* AGP target ID was found, store it */
1221 DPRINT1("AGP ID: %lx\n", TargetAgpCapabilityId
);
1222 PdoExtension
->TargetAgpCapabilityId
= TargetAgpCapabilityId
;
1226 /* Check for devices that are known not to have proper power management */
1227 if (!(PdoExtension
->HackFlags
& PCI_HACK_NO_PM_CAPS
))
1229 /* Query if this device supports power management */
1230 if (!PciReadDeviceCapability(PdoExtension
,
1231 PdoExtension
->CapabilitiesPtr
,
1232 PCI_CAPABILITY_ID_POWER_MANAGEMENT
,
1233 &PowerCapabilities
.Header
,
1234 sizeof(PCI_PM_CAPABILITY
)))
1236 /* No power management, so act as if it had the hackflag set */
1237 DPRINT1("No PM caps, disabling PM\n");
1238 PdoExtension
->HackFlags
|= PCI_HACK_NO_PM_CAPS
;
1242 /* Otherwise, pick the highest wake level that is supported */
1243 WakeLevel
= PowerDeviceUnspecified
;
1244 if (PowerCapabilities
.PMC
.Capabilities
.Support
.PMED0
)
1245 WakeLevel
= PowerDeviceD0
;
1246 if (PowerCapabilities
.PMC
.Capabilities
.Support
.PMED1
)
1247 WakeLevel
= PowerDeviceD1
;
1248 if (PowerCapabilities
.PMC
.Capabilities
.Support
.PMED2
)
1249 WakeLevel
= PowerDeviceD2
;
1250 if (PowerCapabilities
.PMC
.Capabilities
.Support
.PMED3Hot
)
1251 WakeLevel
= PowerDeviceD3
;
1252 if (PowerCapabilities
.PMC
.Capabilities
.Support
.PMED3Cold
)
1253 WakeLevel
= PowerDeviceD3
;
1254 PdoExtension
->PowerState
.DeviceWakeLevel
= WakeLevel
;
1256 /* Convert the PCI power state to the NT power state */
1257 PdoExtension
->PowerState
.CurrentDeviceState
=
1258 PowerCapabilities
.PMCSR
.ControlStatus
.PowerState
+ 1;
1260 /* Save all the power capabilities */
1261 PdoExtension
->PowerCapabilities
= PowerCapabilities
.PMC
.Capabilities
;
1262 DPRINT1("PM Caps Found! Wake Level: %d Power State: %d\n",
1263 WakeLevel
, PdoExtension
->PowerState
.CurrentDeviceState
);
1268 /* At the very end of all this, does this device not have power management? */
1269 if (PdoExtension
->HackFlags
& PCI_HACK_NO_PM_CAPS
)
1271 /* Then guess the current state based on whether the decodes are on */
1272 PdoExtension
->PowerState
.CurrentDeviceState
=
1273 PciData
->Command
& (PCI_ENABLE_IO_SPACE
|
1274 PCI_ENABLE_MEMORY_SPACE
|
1275 PCI_ENABLE_BUS_MASTER
) ?
1276 PowerDeviceD0
: PowerDeviceD3
;
1277 DPRINT1("PM is off, so assumed device is: %d based on enables\n",
1278 PdoExtension
->PowerState
.CurrentDeviceState
);
1284 PciWriteLimitsAndRestoreCurrent(IN PVOID Reserved
,
1287 PPCI_CONFIGURATOR_CONTEXT Context
= Context2
;
1288 PPCI_COMMON_HEADER PciData
, Current
;
1289 PPCI_PDO_EXTENSION PdoExtension
;
1291 /* Grab all parameters from the context */
1292 PdoExtension
= Context
->PdoExtension
;
1293 Current
= Context
->Current
;
1294 PciData
= Context
->PciData
;
1296 /* Write the limit discovery header */
1297 PciWriteDeviceConfig(PdoExtension
, PciData
, 0, PCI_COMMON_HDR_LENGTH
);
1299 /* Now read what the device indicated the limits are */
1300 PciReadDeviceConfig(PdoExtension
, PciData
, 0, PCI_COMMON_HDR_LENGTH
);
1302 /* Then write back the original configuration header */
1303 PciWriteDeviceConfig(PdoExtension
, Current
, 0, PCI_COMMON_HDR_LENGTH
);
1305 /* Copy back the original command that was saved in the context */
1306 Current
->Command
= Context
->Command
;
1307 if (Context
->Command
)
1309 /* Program it back into the device */
1310 PciWriteDeviceConfig(PdoExtension
,
1312 FIELD_OFFSET(PCI_COMMON_HEADER
, Command
),
1316 /* Copy back the original status that was saved as well */
1317 Current
->Status
= Context
->Status
;
1319 /* Call the configurator to restore any other data that might've changed */
1320 Context
->Configurator
->RestoreCurrent(Context
);
1325 PcipGetFunctionLimits(IN PPCI_CONFIGURATOR_CONTEXT Context
)
1327 PPCI_CONFIGURATOR Configurator
;
1328 PPCI_COMMON_HEADER PciData
, Current
;
1329 PPCI_PDO_EXTENSION PdoExtension
;
1330 PCI_IPI_CONTEXT IpiContext
;
1331 PIO_RESOURCE_DESCRIPTOR IoDescriptor
;
1335 /* Grab all parameters from the context */
1336 PdoExtension
= Context
->PdoExtension
;
1337 Current
= Context
->Current
;
1338 PciData
= Context
->PciData
;
1340 /* Save the current PCI Command and Status word */
1341 Context
->Status
= Current
->Status
;
1342 Context
->Command
= Current
->Command
;
1344 /* Now that they're saved, clear the status, and disable all decodes */
1345 Current
->Status
= 0;
1346 Current
->Command
&= ~(PCI_ENABLE_IO_SPACE
|
1347 PCI_ENABLE_MEMORY_SPACE
|
1348 PCI_ENABLE_BUS_MASTER
);
1350 /* Make a copy of the current PCI configuration header (with decodes off) */
1351 RtlCopyMemory(PciData
, Current
, PCI_COMMON_HDR_LENGTH
);
1353 /* Locate the correct resource configurator for this type of device */
1354 Configurator
= &PciConfigurators
[PdoExtension
->HeaderType
];
1355 Context
->Configurator
= Configurator
;
1357 /* Initialize it, which will typically setup the BARs for limit discovery */
1358 Configurator
->Initialize(Context
);
1360 /* Check for critical devices and PCI Debugging devices */
1361 if ((PdoExtension
->HackFlags
& PCI_HACK_CRITICAL_DEVICE
) ||
1362 (PdoExtension
->OnDebugPath
))
1364 /* Specifically check for a PCI Debugging device */
1365 if (PdoExtension
->OnDebugPath
)
1367 /* Was it enabled for bus mastering? */
1368 if (Context
->Command
& PCI_ENABLE_BUS_MASTER
)
1370 /* This decode needs to be re-enabled so debugging can work */
1371 PciData
->Command
|= PCI_ENABLE_BUS_MASTER
;
1372 Current
->Command
|= PCI_ENABLE_BUS_MASTER
;
1375 /* Disable the debugger while the discovery is happening */
1376 KdDisableDebugger();
1379 /* For these devices, an IPI must be sent to force high-IRQL discovery */
1380 IpiContext
.Barrier
= 1;
1381 IpiContext
.RunCount
= 1;
1382 IpiContext
.DeviceExtension
= PdoExtension
;
1383 IpiContext
.Function
= PciWriteLimitsAndRestoreCurrent
;
1384 IpiContext
.Context
= Context
;
1385 KeIpiGenericCall(PciExecuteCriticalSystemRoutine
, (ULONG_PTR
)&IpiContext
);
1387 /* Re-enable the debugger if this was a PCI Debugging Device */
1388 if (PdoExtension
->OnDebugPath
) KdEnableDebugger();
1392 /* Otherwise, it's safe to do this in-line at low IRQL */
1393 PciWriteLimitsAndRestoreCurrent(PdoExtension
, Context
);
1397 * Check if it's valid to compare the headers to see if limit discovery mode
1398 * has properly exited (the expected case is that the PCI header would now
1399 * be equal to what it was before). In some cases, it is known that this will
1400 * fail, because during PciApplyHacks (among other places), software hacks
1401 * had to be applied to the header, which the hardware-side will not see, and
1402 * thus the headers would appear "different".
1404 if (!PdoExtension
->ExpectedWritebackFailure
)
1406 /* Read the current PCI header now, after discovery has completed */
1407 PciReadDeviceConfig(PdoExtension
, PciData
+ 1, 0, PCI_COMMON_HDR_LENGTH
);
1409 /* Check if the current header at entry, is equal to the header now */
1410 Offset
= RtlCompareMemory(PciData
+ 1, Current
, PCI_COMMON_HDR_LENGTH
);
1411 if (Offset
!= PCI_COMMON_HDR_LENGTH
)
1413 /* It's not, which means configuration somehow changed, dump this */
1414 DPRINT1("PCI - CFG space write verify failed at offset 0x%x\n", Offset
);
1415 PciDebugDumpCommonConfig(PciData
+ 1);
1416 DPRINT1("----------\n");
1417 PciDebugDumpCommonConfig(Current
);
1421 /* This PDO should not already have resources, since this is only done once */
1422 ASSERT(PdoExtension
->Resources
== NULL
);
1424 /* Allocate the structure that will hold the discovered resources and limits */
1425 PdoExtension
->Resources
= ExAllocatePoolWithTag(NonPagedPool
,
1426 sizeof(PCI_FUNCTION_RESOURCES
),
1428 if (!PdoExtension
->Resources
) return STATUS_INSUFFICIENT_RESOURCES
;
1430 /* Clear it out for now */
1431 RtlZeroMemory(PdoExtension
->Resources
, sizeof(PCI_FUNCTION_RESOURCES
));
1433 /* Now call the configurator, which will first store the limits... */
1434 Configurator
->SaveLimits(Context
);
1436 /* ...and then store the current resources being used */
1437 Configurator
->SaveCurrentSettings(Context
);
1439 /* Loop all the limit descriptors backwards */
1440 IoDescriptor
= &PdoExtension
->Resources
->Limit
[PCI_TYPE0_ADDRESSES
+ 1];
1443 /* Keep going until a non-null descriptor is found */
1445 if (IoDescriptor
->Type
!= CmResourceTypeNull
) break;
1447 /* This is a null descriptor, is it the last one? */
1448 if (IoDescriptor
== &PdoExtension
->Resources
->Limit
[PCI_TYPE0_ADDRESSES
+ 1])
1450 /* This means the descriptor is NULL, which means discovery failed */
1451 DPRINT1("PCI Resources fail!\n");
1453 /* No resources will be assigned for the device */
1454 ExFreePoolWithTag(PdoExtension
->Resources
, 0);
1455 PdoExtension
->Resources
= NULL
;
1460 /* Return success here, even if the device has no assigned resources */
1461 return STATUS_SUCCESS
;
1466 PciGetFunctionLimits(IN PPCI_PDO_EXTENSION PdoExtension
,
1467 IN PPCI_COMMON_HEADER Current
,
1468 IN ULONGLONG HackFlags
)
1471 PPCI_COMMON_HEADER PciData
;
1472 PCI_CONFIGURATOR_CONTEXT Context
;
1475 /* Do the hackflags indicate this device should be skipped? */
1476 if (PciSkipThisFunction(Current
,
1478 PCI_SKIP_RESOURCE_ENUMERATION
,
1481 /* Do not process its resources */
1482 return STATUS_SUCCESS
;
1485 /* Allocate a buffer to hold two PCI configuration headers */
1486 PciData
= ExAllocatePoolWithTag(0, 2 * PCI_COMMON_HDR_LENGTH
, 'BicP');
1487 if (!PciData
) return STATUS_INSUFFICIENT_RESOURCES
;
1489 /* Set up the context for the resource enumeration, and do it */
1490 Context
.Current
= Current
;
1491 Context
.PciData
= PciData
;
1492 Context
.PdoExtension
= PdoExtension
;
1493 Status
= PcipGetFunctionLimits(&Context
);
1495 /* Enumeration is completed, free the PCI headers and return the status */
1496 ExFreePoolWithTag(PciData
, 0);
1502 PciProcessBus(IN PPCI_FDO_EXTENSION DeviceExtension
)
1504 PPCI_PDO_EXTENSION PdoExtension
;
1505 PDEVICE_OBJECT PhysicalDeviceObject
;
1508 /* Get the PDO Extension */
1509 PhysicalDeviceObject
= DeviceExtension
->PhysicalDeviceObject
;
1510 PdoExtension
= (PPCI_PDO_EXTENSION
)PhysicalDeviceObject
->DeviceExtension
;
1512 /* Cheeck if this is the root bus */
1513 if (!PCI_IS_ROOT_FDO(DeviceExtension
))
1515 /* Not really handling this year */
1519 /* Check for PCI bridges with the ISA bit set, or required */
1520 if ((PdoExtension
) &&
1521 (PciClassifyDeviceType(PdoExtension
) == PciTypePciBridge
) &&
1522 ((PdoExtension
->Dependent
.type1
.IsaBitRequired
) ||
1523 (PdoExtension
->Dependent
.type1
.IsaBitSet
)))
1525 /* We'll need to do some legacy support */
1532 /* Scan all of the root bus' children bridges */
1533 for (PdoExtension
= DeviceExtension
->ChildBridgePdoList
;
1535 PdoExtension
= PdoExtension
->NextBridge
)
1537 /* Find any that have the VGA decode bit on */
1538 if (PdoExtension
->Dependent
.type1
.VgaBitSet
)
1540 /* Again, some more legacy support we'll have to do */
1547 /* Check for ACPI systems where the OS assigns bus numbers */
1548 if (PciAssignBusNumbers
)
1550 /* Not yet supported */
1558 PciScanBus(IN PPCI_FDO_EXTENSION DeviceExtension
)
1560 ULONG MaxDevice
= PCI_MAX_DEVICES
;
1561 BOOLEAN ProcessFlag
= FALSE
;
1562 ULONG i
, j
, k
, Size
;
1563 USHORT CapOffset
, TempOffset
;
1565 PDEVICE_OBJECT DeviceObject
;
1566 UCHAR Buffer
[PCI_COMMON_HDR_LENGTH
];
1567 UCHAR BiosBuffer
[PCI_COMMON_HDR_LENGTH
];
1568 PPCI_COMMON_HEADER PciData
= (PVOID
)Buffer
;
1569 PPCI_COMMON_HEADER BiosData
= (PVOID
)BiosBuffer
;
1570 PCI_SLOT_NUMBER PciSlot
;
1573 PPCI_PDO_EXTENSION PdoExtension
, NewExtension
;
1574 PPCI_PDO_EXTENSION
* BridgeExtension
;
1575 PWCHAR DescriptionText
;
1576 USHORT SubVendorId
, SubSystemId
;
1577 PCI_CAPABILITIES_HEADER CapHeader
, PcixCapHeader
;
1578 DPRINT1("PCI Scan Bus: FDO Extension @ 0x%x, Base Bus = 0x%x\n",
1579 DeviceExtension
, DeviceExtension
->BaseBus
);
1581 /* Is this the root FDO? */
1582 if (!PCI_IS_ROOT_FDO(DeviceExtension
))
1584 /* Other FDOs are not currently supported */
1589 /* Loop every device on the bus */
1590 PciSlot
.u
.bits
.Reserved
= 0;
1591 i
= DeviceExtension
->BaseBus
;
1592 for (j
= 0; j
< MaxDevice
; j
++)
1594 /* Loop every function of each device */
1595 PciSlot
.u
.bits
.DeviceNumber
= j
;
1596 for (k
= 0; k
< PCI_MAX_FUNCTION
; k
++)
1598 /* Build the final slot structure */
1599 PciSlot
.u
.bits
.FunctionNumber
= k
;
1601 /* Read the vendor for this slot */
1602 PciReadSlotConfig(DeviceExtension
,
1608 /* Skip invalid device */
1609 if (PciData
->VendorID
== PCI_INVALID_VENDORID
) continue;
1611 /* Now read the whole header */
1612 PciReadSlotConfig(DeviceExtension
,
1616 PCI_COMMON_HDR_LENGTH
- sizeof(USHORT
));
1618 /* Apply any hacks before even analyzing the configuration header */
1619 PciApplyHacks(DeviceExtension
,
1622 PCI_HACK_FIXUP_BEFORE_CONFIGURATION
,
1625 /* Dump device that was found */
1626 DPRINT1("Scan Found Device 0x%x (b=0x%x, d=0x%x, f=0x%x)\n",
1632 /* Dump the device's header */
1633 PciDebugDumpCommonConfig(PciData
);
1635 /* Find description for this device for the debugger's sake */
1636 DescriptionText
= PciGetDeviceDescriptionMessage(PciData
->BaseClass
,
1638 DPRINT1("Device Description \"%S\".\n",
1639 DescriptionText
? DescriptionText
: L
"(NULL)");
1640 if (DescriptionText
) ExFreePoolWithTag(DescriptionText
, 0);
1642 /* Check if there is an ACPI Watchdog Table */
1645 /* Check if this PCI device is the ACPI Watchdog Device... */
1650 /* Check for non-simple devices */
1651 if ((PCI_MULTIFUNCTION_DEVICE(PciData
)) ||
1652 (PciData
->BaseClass
== PCI_CLASS_BRIDGE_DEV
))
1654 /* No subsystem data defined for these kinds of bridges */
1660 /* Read the subsystem information from the PCI header */
1661 SubVendorId
= PciData
->u
.type0
.SubVendorID
;
1662 SubSystemId
= PciData
->u
.type0
.SubSystemID
;
1665 /* Get any hack flags for this device */
1666 HackFlags
= PciGetHackFlags(PciData
->VendorID
,
1670 PciData
->RevisionID
);
1672 /* Check if this device is considered critical by the OS */
1673 if (PciIsCriticalDeviceClass(PciData
->BaseClass
, PciData
->SubClass
))
1675 /* Check if normally the decodes would be disabled */
1676 if (!(HackFlags
& PCI_HACK_DONT_DISABLE_DECODES
))
1678 /* Because this device is critical, don't disable them */
1679 DPRINT1("Not allowing PM Because device is critical\n");
1680 HackFlags
|= PCI_HACK_CRITICAL_DEVICE
;
1684 /* PCI bridges with a VGA card are also considered critical */
1685 if ((PciData
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
1686 (PciData
->SubClass
== PCI_SUBCLASS_BR_PCI_TO_PCI
) &&
1687 (PciData
->u
.type1
.BridgeControl
& PCI_ENABLE_BRIDGE_VGA
) &&
1688 !(HackFlags
& PCI_HACK_DONT_DISABLE_DECODES
))
1690 /* Do not disable their decodes either */
1691 DPRINT1("Not allowing PM because device is VGA\n");
1692 HackFlags
|= PCI_HACK_CRITICAL_DEVICE
;
1695 /* Check if the device should be skipped for whatever reason */
1696 if (PciSkipThisFunction(PciData
,
1698 PCI_SKIP_DEVICE_ENUMERATION
,
1701 /* Skip this device */
1705 /* Check if a PDO has already been created for this device */
1706 PdoExtension
= PciFindPdoByFunction(DeviceExtension
,
1711 /* Rescan scenarios are not yet implemented */
1716 /* Bus processing will need to happen */
1719 /* Create the PDO for this device */
1720 Status
= PciPdoCreate(DeviceExtension
, PciSlot
, &DeviceObject
);
1721 ASSERT(NT_SUCCESS(Status
));
1722 NewExtension
= (PPCI_PDO_EXTENSION
)DeviceObject
->DeviceExtension
;
1724 /* Check for broken devices with wrong/no class codes */
1725 if (HackFlags
& PCI_HACK_FAKE_CLASS_CODE
)
1727 /* Setup a default one */
1728 PciData
->BaseClass
= PCI_CLASS_BASE_SYSTEM_DEV
;
1729 PciData
->SubClass
= PCI_SUBCLASS_SYS_OTHER
;
1731 /* Device will behave erratically when reading back data */
1732 NewExtension
->ExpectedWritebackFailure
= TRUE
;
1735 /* Clone all the information from the header */
1736 NewExtension
->VendorId
= PciData
->VendorID
;
1737 NewExtension
->DeviceId
= PciData
->DeviceID
;
1738 NewExtension
->RevisionId
= PciData
->RevisionID
;
1739 NewExtension
->ProgIf
= PciData
->ProgIf
;
1740 NewExtension
->SubClass
= PciData
->SubClass
;
1741 NewExtension
->BaseClass
= PciData
->BaseClass
;
1742 NewExtension
->HeaderType
= PCI_CONFIGURATION_TYPE(PciData
);
1744 /* Check for modern bridge types, which are managed by the driver */
1745 if ((NewExtension
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
1746 ((NewExtension
->SubClass
== PCI_SUBCLASS_BR_PCI_TO_PCI
) ||
1747 (NewExtension
->SubClass
== PCI_SUBCLASS_BR_CARDBUS
)))
1749 /* Acquire this device's lock */
1750 KeEnterCriticalRegion();
1751 KeWaitForSingleObject(&DeviceExtension
->ChildListLock
,
1757 /* Scan the bridge list until the first free entry */
1758 for (BridgeExtension
= &DeviceExtension
->ChildBridgePdoList
;
1760 BridgeExtension
= &(*BridgeExtension
)->NextBridge
);
1762 /* Add this PDO as a bridge */
1763 *BridgeExtension
= NewExtension
;
1764 ASSERT(NewExtension
->NextBridge
== NULL
);
1766 /* Release this device's lock */
1767 KeSetEvent(&DeviceExtension
->ChildListLock
,
1770 KeLeaveCriticalRegion();
1773 /* Get the PCI BIOS configuration saved in the registry */
1774 Status
= PciGetBiosConfig(NewExtension
, BiosData
);
1775 if (NT_SUCCESS(Status
))
1777 /* This path has not yet been fully tested by eVb */
1778 DPRINT1("Have BIOS configuration!\n");
1781 /* Check if the PCI BIOS configuration has changed */
1782 if (!PcipIsSameDevice(NewExtension
, BiosData
))
1784 /* This is considered failure, and new data will be saved */
1785 Status
= STATUS_UNSUCCESSFUL
;
1789 /* Data is still correct, check for interrupt line change */
1790 if (BiosData
->u
.type0
.InterruptLine
!=
1791 PciData
->u
.type0
.InterruptLine
)
1793 /* Update the current BIOS with the saved interrupt line */
1794 PciWriteDeviceConfig(NewExtension
,
1795 &BiosData
->u
.type0
.InterruptLine
,
1796 FIELD_OFFSET(PCI_COMMON_HEADER
,
1797 u
.type0
.InterruptLine
),
1801 /* Save the BIOS interrupt line and the initial command */
1802 NewExtension
->RawInterruptLine
= BiosData
->u
.type0
.InterruptLine
;
1803 NewExtension
->InitialCommand
= BiosData
->Command
;
1807 /* Check if no saved data was present or if it was a mismatch */
1808 if (!NT_SUCCESS(Status
))
1810 /* Save the new data */
1811 Status
= PciSaveBiosConfig(NewExtension
, PciData
);
1812 ASSERT(NT_SUCCESS(Status
));
1814 /* Save the interrupt line and command from the device */
1815 NewExtension
->RawInterruptLine
= PciData
->u
.type0
.InterruptLine
;
1816 NewExtension
->InitialCommand
= PciData
->Command
;
1819 /* Save original command from the device and hack flags */
1820 NewExtension
->CommandEnables
= PciData
->Command
;
1821 NewExtension
->HackFlags
= HackFlags
;
1823 /* Get power, AGP, and other capability data */
1824 PciGetEnhancedCapabilities(NewExtension
, PciData
);
1826 /* Now configure the BARs */
1827 Status
= PciGetFunctionLimits(NewExtension
, PciData
, HackFlags
);
1829 /* Power up the device */
1830 PciSetPowerManagedDevicePowerState(NewExtension
, PowerDeviceD0
, FALSE
);
1832 /* Apply any device hacks required for enumeration */
1833 PciApplyHacks(DeviceExtension
,
1836 PCI_HACK_FIXUP_AFTER_CONFIGURATION
,
1839 /* Save interrupt pin */
1840 NewExtension
->InterruptPin
= PciData
->u
.type0
.InterruptPin
;
1843 * Use either this device's actual IRQ line or, if it's connected on
1844 * a master bus whose IRQ line is actually connected to the host, use
1845 * the HAL to query the bus' IRQ line and store that as the adjusted
1846 * interrupt line instead
1848 NewExtension
->AdjustedInterruptLine
= PciGetAdjustedInterruptLine(NewExtension
);
1850 /* Check if this device is used for PCI debugger cards */
1851 NewExtension
->OnDebugPath
= PciIsDeviceOnDebugPath(NewExtension
);
1853 /* Check for devices with invalid/bogus subsystem data */
1854 if (HackFlags
& PCI_HACK_NO_SUBSYSTEM
)
1856 /* Set the subsystem information to zero instead */
1857 NewExtension
->SubsystemVendorId
= 0;
1858 NewExtension
->SubsystemId
= 0;
1861 /* Scan all capabilities */
1862 CapOffset
= NewExtension
->CapabilitiesPtr
;
1865 /* Read this header */
1866 TempOffset
= PciReadDeviceCapability(NewExtension
,
1870 sizeof(PCI_CAPABILITIES_HEADER
));
1871 if (TempOffset
!= CapOffset
)
1873 /* This is a strange issue that shouldn't happen normally */
1874 DPRINT1("PCI - Failed to read PCI capability at offset 0x%02x\n",
1876 ASSERT(TempOffset
== CapOffset
);
1879 /* Check for capabilities that this driver cares about */
1880 switch (CapHeader
.CapabilityID
)
1882 /* Power management capability is heavily used by the bus */
1883 case PCI_CAPABILITY_ID_POWER_MANAGEMENT
:
1885 /* Dump the capability */
1887 Size
= sizeof(PCI_PM_CAPABILITY
);
1890 /* AGP capability is required for AGP bus functionality */
1891 case PCI_CAPABILITY_ID_AGP
:
1893 /* Dump the capability */
1895 Size
= sizeof(PCI_AGP_CAPABILITY
);
1898 /* This driver doesn't really use anything other than that */
1901 /* Windows prints this, we could do a translation later */
1902 Name
= "UNKNOWN CAPABILITY";
1907 /* Check if this is a capability that should be dumped */
1910 /* Read the whole capability data */
1911 TempOffset
= PciReadDeviceCapability(NewExtension
,
1913 CapHeader
.CapabilityID
,
1917 if (TempOffset
!= CapOffset
)
1919 /* Again, a strange issue that shouldn't be seen */
1920 DPRINT1("- Failed to read capability data. ***\n");
1921 ASSERT(TempOffset
== CapOffset
);
1925 /* Dump this capability */
1926 DPRINT1("CAP @%02x ID %02x (%s)\n",
1927 CapOffset
, CapHeader
.CapabilityID
, Name
);
1928 for (i
= 0; i
< Size
; i
+= 2)
1929 DPRINT1(" %04x\n", *(PUSHORT
)((ULONG_PTR
)&CapHeader
+ i
));
1932 /* Check the next capability */
1933 CapOffset
= CapHeader
.Next
;
1936 /* Check for IDE controllers */
1937 if ((NewExtension
->BaseClass
== PCI_CLASS_MASS_STORAGE_CTLR
) &&
1938 (NewExtension
->SubClass
== PCI_SUBCLASS_MSC_IDE_CTLR
))
1940 /* Do not allow them to power down completely */
1941 NewExtension
->DisablePowerDown
= TRUE
;
1945 * Check if this is a legacy bridge. Note that the i82375 PCI/EISA
1946 * bridge that is present on certain NT Alpha machines appears as
1947 * non-classified so detect it manually by scanning for its VID/PID.
1949 if (((NewExtension
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
1950 ((NewExtension
->SubClass
== PCI_SUBCLASS_BR_ISA
) ||
1951 (NewExtension
->SubClass
== PCI_SUBCLASS_BR_EISA
) ||
1952 (NewExtension
->SubClass
== PCI_SUBCLASS_BR_MCA
))) ||
1953 ((NewExtension
->VendorId
== 0x8086) &&
1954 (NewExtension
->DeviceId
== 0x482)))
1956 /* Do not allow these legacy bridges to be powered down */
1957 NewExtension
->DisablePowerDown
= TRUE
;
1960 /* Check if the BIOS did not configure a cache line size */
1961 if (!PciData
->CacheLineSize
)
1963 /* Check if the device is disabled */
1964 if (!(NewExtension
->CommandEnables
& (PCI_ENABLE_IO_SPACE
|
1965 PCI_ENABLE_MEMORY_SPACE
|
1966 PCI_ENABLE_BUS_MASTER
)))
1968 /* Check if this is a PCI-X device*/
1969 TempOffset
= PciReadDeviceCapability(NewExtension
,
1970 NewExtension
->CapabilitiesPtr
,
1971 PCI_CAPABILITY_ID_PCIX
,
1973 sizeof(PCI_CAPABILITIES_HEADER
));
1976 * A device with default cache line size and latency timer
1977 * settings is considered to be unconfigured. Note that on
1978 * PCI-X, the reset value of the latency timer field in the
1979 * header is 64, not 0, hence why the check for PCI-X caps
1980 * was required, and the value used here below.
1982 if (!(PciData
->LatencyTimer
) ||
1983 ((TempOffset
) && (PciData
->LatencyTimer
== 64)))
1985 /* Keep track of the fact that it needs configuration */
1986 DPRINT1("PCI - ScanBus, PDOx %x found unconfigured\n",
1988 NewExtension
->NeedsHotPlugConfiguration
= TRUE
;
1993 /* Save latency and cache size information */
1994 NewExtension
->SavedLatencyTimer
= PciData
->LatencyTimer
;
1995 NewExtension
->SavedCacheLineSize
= PciData
->CacheLineSize
;
1997 /* The PDO is now ready to go */
1998 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
2002 /* Enumeration completed, do a final pass now that all devices are found */
2003 if (ProcessFlag
) PciProcessBus(DeviceExtension
);
2004 return STATUS_SUCCESS
;
2009 PciQueryDeviceRelations(IN PPCI_FDO_EXTENSION DeviceExtension
,
2010 IN OUT PDEVICE_RELATIONS
*pDeviceRelations
)
2013 PPCI_PDO_EXTENSION PdoExtension
;
2015 PDEVICE_RELATIONS DeviceRelations
, NewRelations
;
2017 PDEVICE_OBJECT DeviceObject
, *ObjectArray
;
2020 /* Make sure the FDO is started */
2021 ASSERT(DeviceExtension
->DeviceState
== PciStarted
);
2023 /* Synchronize while we enumerate the bus */
2024 Status
= PciBeginStateTransition(DeviceExtension
, PciSynchronizedOperation
);
2025 if (!NT_SUCCESS(Status
)) return Status
;
2027 /* Scan all children PDO */
2028 for (PdoExtension
= DeviceExtension
->ChildPdoList
;
2030 PdoExtension
= PdoExtension
->Next
)
2032 /* Invalidate them */
2033 PdoExtension
->NotPresent
= TRUE
;
2036 /* Scan the PCI Bus */
2037 Status
= PciScanBus(DeviceExtension
);
2038 ASSERT(NT_SUCCESS(Status
));
2040 /* Enumerate all children PDO again */
2041 for (PdoExtension
= DeviceExtension
->ChildPdoList
;
2043 PdoExtension
= PdoExtension
->Next
)
2045 /* Check for PDOs that are still invalidated */
2046 if (PdoExtension
->NotPresent
)
2048 /* This means this PDO existed before, but not anymore */
2049 PdoExtension
->ReportedMissing
= TRUE
;
2050 DPRINT1("PCI - Old device (pdox) %08x not found on rescan.\n",
2055 /* Increase count of detected PDOs */
2060 /* Read the current relations and add the newly discovered relations */
2061 DeviceRelations
= *pDeviceRelations
;
2062 Size
= FIELD_OFFSET(DEVICE_RELATIONS
, Objects
) +
2063 PdoCount
* sizeof(PDEVICE_OBJECT
);
2064 if (DeviceRelations
) Size
+= sizeof(PDEVICE_OBJECT
) * DeviceRelations
->Count
;
2066 /* Allocate the device relations */
2067 NewRelations
= (PDEVICE_RELATIONS
)ExAllocatePoolWithTag(0, Size
, 'BicP');
2070 /* Out of space, cancel the operation */
2071 PciCancelStateTransition(DeviceExtension
, PciSynchronizedOperation
);
2072 return STATUS_INSUFFICIENT_RESOURCES
;
2075 /* Check if there were any older relations */
2076 NewRelations
->Count
= 0;
2077 if (DeviceRelations
)
2079 /* Copy the old relations into the new buffer, then free the old one */
2080 RtlCopyMemory(NewRelations
,
2082 FIELD_OFFSET(DEVICE_RELATIONS
, Objects
) +
2083 DeviceRelations
->Count
* sizeof(PDEVICE_OBJECT
));
2084 ExFreePoolWithTag(DeviceRelations
, 0);
2087 /* Print out that we're ready to dump relations */
2088 DPRINT1("PCI QueryDeviceRelations/BusRelations FDOx %08x (bus 0x%02x)\n",
2090 DeviceExtension
->BaseBus
);
2092 /* Loop the current PDO children and the device relation object array */
2093 PdoExtension
= DeviceExtension
->ChildPdoList
;
2094 ObjectArray
= &NewRelations
->Objects
[NewRelations
->Count
];
2095 while (PdoExtension
)
2097 /* Dump this relation */
2098 DPRINT1(" QDR PDO %08x (x %08x)%s\n",
2099 PdoExtension
->PhysicalDeviceObject
,
2101 PdoExtension
->NotPresent
?
2102 "<Omitted, device flaged not present>" : "");
2104 /* Is this PDO present? */
2105 if (!PdoExtension
->NotPresent
)
2107 /* Reference it and add it to the array */
2108 DeviceObject
= PdoExtension
->PhysicalDeviceObject
;
2109 ObfReferenceObject(DeviceObject
);
2110 *ObjectArray
++ = DeviceObject
;
2113 /* Go to the next PDO */
2114 PdoExtension
= PdoExtension
->Next
;
2117 /* Terminate dumping the relations */
2118 DPRINT1(" QDR Total PDO count = %d (%d already in list)\n",
2119 NewRelations
->Count
+ PdoCount
,
2120 NewRelations
->Count
);
2122 /* Return the final count and the new buffer */
2123 NewRelations
->Count
+= PdoCount
;
2124 *pDeviceRelations
= NewRelations
;
2125 return STATUS_SUCCESS
;
2130 PciSetResources(IN PPCI_PDO_EXTENSION PdoExtension
,
2132 IN BOOLEAN SomethingSomethingDarkSide
)
2134 PPCI_FDO_EXTENSION FdoExtension
;
2135 UCHAR NewCacheLineSize
, NewLatencyTimer
;
2136 PCI_COMMON_HEADER PciData
;
2138 PPCI_CONFIGURATOR Configurator
;
2140 /* Get the FDO and read the configuration data */
2141 FdoExtension
= PdoExtension
->ParentFdoExtension
;
2142 PciReadDeviceConfig(PdoExtension
, &PciData
, 0, PCI_COMMON_HDR_LENGTH
);
2144 /* Make sure this is still the same device */
2145 if (!PcipIsSameDevice(PdoExtension
, &PciData
))
2148 ASSERTMSG(FALSE
, "PCI Set resources - not same device");
2149 return STATUS_DEVICE_DOES_NOT_EXIST
;
2152 /* Nothing to set for a host bridge */
2153 if ((PdoExtension
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
2154 (PdoExtension
->SubClass
== PCI_SUBCLASS_BR_HOST
))
2157 return STATUS_SUCCESS
;
2160 /* Check if an IDE controller is being reset */
2162 (PdoExtension
->BaseClass
== PCI_CLASS_MASS_STORAGE_CTLR
) &&
2163 (PdoExtension
->SubClass
== PCI_SUBCLASS_MSC_IDE_CTLR
))
2165 /* Turn off native mode */
2166 Native
= PciConfigureIdeController(PdoExtension
, &PciData
, FALSE
);
2167 ASSERT(Native
== PdoExtension
->IDEInNativeMode
);
2170 /* Check for update of a hotplug device, or first configuration of one */
2171 if ((PdoExtension
->NeedsHotPlugConfiguration
) &&
2172 (FdoExtension
->HotPlugParameters
.Acquired
))
2174 /* Don't have hotplug devices to test with yet, QEMU 0.14 should */
2179 /* Locate the correct resource configurator for this type of device */
2180 Configurator
= &PciConfigurators
[PdoExtension
->HeaderType
];
2182 /* Apply the settings change */
2183 Configurator
->ChangeResourceSettings(PdoExtension
, &PciData
);
2185 /* Assume no update needed */
2186 PdoExtension
->UpdateHardware
= FALSE
;
2188 /* Check if a reset is needed */
2191 /* Reset resources */
2192 Configurator
->ResetDevice(PdoExtension
, &PciData
);
2193 PciData
.u
.type0
.InterruptLine
= PdoExtension
->RawInterruptLine
;
2196 /* Check if the latency timer changed */
2197 NewLatencyTimer
= PdoExtension
->SavedLatencyTimer
;
2198 if (PciData
.LatencyTimer
!= NewLatencyTimer
)
2200 /* Debug notification */
2201 DPRINT1("PCI (pdox %08x) changing latency from %02x to %02x.\n",
2203 PciData
.LatencyTimer
,
2207 /* Check if the cache line changed */
2208 NewCacheLineSize
= PdoExtension
->SavedCacheLineSize
;
2209 if (PciData
.CacheLineSize
!= NewCacheLineSize
)
2211 /* Debug notification */
2212 DPRINT1("PCI (pdox %08x) changing cache line size from %02x to %02x.\n",
2214 PciData
.CacheLineSize
,
2218 /* Inherit data from PDO extension */
2219 PciData
.LatencyTimer
= PdoExtension
->SavedLatencyTimer
;
2220 PciData
.CacheLineSize
= PdoExtension
->SavedCacheLineSize
;
2221 PciData
.u
.type0
.InterruptLine
= PdoExtension
->RawInterruptLine
;
2223 /* Apply any resource hacks required */
2224 PciApplyHacks(FdoExtension
,
2227 PCI_HACK_FIXUP_BEFORE_UPDATE
,
2230 /* Check if I/O space was disabled by administrator or driver */
2231 if (PdoExtension
->IoSpaceNotRequired
)
2233 /* Don't turn on the decode */
2234 PdoExtension
->CommandEnables
&= ~PCI_ENABLE_IO_SPACE
;
2237 /* Update the device with the new settings */
2238 PciUpdateHardware(PdoExtension
, &PciData
);
2240 /* Update complete */
2241 PdoExtension
->RawInterruptLine
= PciData
.u
.type0
.InterruptLine
;
2242 PdoExtension
->NeedsHotPlugConfiguration
= FALSE
;
2243 return STATUS_SUCCESS
;