3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: hal/halx86/generic/legacy/bussupp.c
5 * PURPOSE: HAL Legacy Bus Support Code
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 /* GLOBALS ********************************************************************/
17 extern KSPIN_LOCK HalpPCIConfigLock
;
20 /* PRIVATE FUNCTIONS **********************************************************/
24 HalpAllocateBusHandler(IN INTERFACE_TYPE InterfaceType
,
25 IN BUS_DATA_TYPE BusDataType
,
27 IN INTERFACE_TYPE ParentBusInterfaceType
,
28 IN ULONG ParentBusNumber
,
29 IN ULONG BusSpecificData
)
33 /* Register the bus handler */
34 HalRegisterBusHandler(InterfaceType
,
37 ParentBusInterfaceType
,
47 /* Check for a valid interface */
48 if (InterfaceType
!= InterfaceTypeUndefined
)
50 /* Allocate address ranges and zero them out */
51 Bus
->BusAddresses
= ExAllocatePoolWithTag(NonPagedPoolMustSucceed
,
52 sizeof(SUPPORTED_RANGES
),
54 RtlZeroMemory(Bus
->BusAddresses
, sizeof(SUPPORTED_RANGES
));
56 /* Build the data structure */
57 Bus
->BusAddresses
->Version
= HAL_SUPPORTED_RANGE_VERSION
;
58 Bus
->BusAddresses
->Dma
.Limit
= 7;
59 Bus
->BusAddresses
->Memory
.Limit
= 0xFFFFFFFF;
60 Bus
->BusAddresses
->IO
.Limit
= 0xFFFF;
61 Bus
->BusAddresses
->IO
.SystemAddressSpace
= 1;
62 Bus
->BusAddresses
->PrefetchMemory
.Base
= 1;
65 /* Return the bus address */
72 HalpRegisterInternalBusHandlers(VOID
)
76 /* Only do processor 1 */
77 if (KeGetCurrentPrcb()->Number
) return;
79 /* Register root support */
82 /* Allocate the system bus */
83 Bus
= HalpAllocateBusHandler(Internal
,
84 ConfigurationSpaceUndefined
,
86 InterfaceTypeUndefined
,
92 Bus
->GetInterruptVector
= HalpGetSystemInterruptVector
;
93 Bus
->TranslateBusAddress
= HalpTranslateSystemBusAddress
;
96 /* Allocate the CMOS bus */
97 Bus
= HalpAllocateBusHandler(InterfaceTypeUndefined
,
100 InterfaceTypeUndefined
,
106 Bus
->GetBusData
= HalpcGetCmosData
;
107 Bus
->SetBusData
= HalpcSetCmosData
;
110 /* Allocate the CMOS bus */
111 Bus
= HalpAllocateBusHandler(InterfaceTypeUndefined
,
114 InterfaceTypeUndefined
,
120 Bus
->GetBusData
= HalpcGetCmosData
;
121 Bus
->SetBusData
= HalpcSetCmosData
;
124 /* Allocate ISA bus */
125 Bus
= HalpAllocateBusHandler(Isa
,
126 ConfigurationSpaceUndefined
,
134 Bus
->GetBusData
= HalpNoBusData
;
135 Bus
->BusAddresses
->Memory
.Limit
= 0xFFFFFF;
136 Bus
->TranslateBusAddress
= HalpTranslateIsaBusAddress
;
139 /* No support for EISA or MCA */
140 ASSERT(HalpBusType
== MACHINE_TYPE_ISA
);
147 HalpMarkChipsetDecode(BOOLEAN OverrideEnable
)
150 UNICODE_STRING KeyString
;
151 ULONG Data
= OverrideEnable
;
152 HANDLE KeyHandle
, Handle
;
155 RtlInitUnicodeString(&KeyString
,
156 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET");
157 Status
= HalpOpenRegistryKey(&Handle
, 0, &KeyString
, KEY_ALL_ACCESS
, FALSE
);
158 if (NT_SUCCESS(Status
))
160 /* Open PNP Bios key */
161 RtlInitUnicodeString(&KeyString
, L
"Control\\Biosinfo\\PNPBios");
162 Status
= HalpOpenRegistryKey(&KeyHandle
,
171 /* Check if PNP BIOS key exists */
172 if (NT_SUCCESS(Status
))
174 /* Set the override value */
175 RtlInitUnicodeString(&KeyString
, L
"FullDecodeChipsetOverride");
176 Status
= ZwSetValueKey(KeyHandle
,
195 HalpAllocateAndInitPciBusHandler(IN ULONG PciType
,
197 IN BOOLEAN TestAllocation
)
200 PPCIPBUSDATA BusData
;
202 /* Allocate the bus handler */
203 Bus
= HalpAllocateBusHandler(PCIBus
,
208 sizeof(PCIPBUSDATA
));
211 Bus
->GetBusData
= (PGETSETBUSDATA
)HalpGetPCIData
;
212 Bus
->SetBusData
= (PGETSETBUSDATA
)HalpSetPCIData
;
213 Bus
->GetInterruptVector
= (PGETINTERRUPTVECTOR
)HalpGetPCIIntOnISABus
;
214 Bus
->AdjustResourceList
= (PADJUSTRESOURCELIST
)HalpAdjustPCIResourceList
;
215 Bus
->AssignSlotResources
= (PASSIGNSLOTRESOURCES
)HalpAssignPCISlotResources
;
216 Bus
->BusAddresses
->Dma
.Limit
= 0;
218 /* Get our custom bus data */
219 BusData
= (PPCIPBUSDATA
)Bus
->BusData
;
221 /* Setup custom bus data */
222 BusData
->CommonData
.Tag
= PCI_DATA_TAG
;
223 BusData
->CommonData
.Version
= PCI_DATA_VERSION
;
224 BusData
->CommonData
.ReadConfig
= (PciReadWriteConfig
)HalpReadPCIConfig
;
225 BusData
->CommonData
.WriteConfig
= (PciReadWriteConfig
)HalpWritePCIConfig
;
226 BusData
->CommonData
.Pin2Line
= (PciPin2Line
)HalpPCIPin2ISALine
;
227 BusData
->CommonData
.Line2Pin
= (PciLine2Pin
)HalpPCIISALine2Pin
;
228 BusData
->MaxDevice
= PCI_MAX_DEVICES
;
229 BusData
->GetIrqRange
= (PciIrqRange
)HalpGetISAFixedPCIIrq
;
231 /* Initialize the bitmap */
232 RtlInitializeBitMap(&BusData
->DeviceConfigured
, BusData
->ConfiguredBits
, 256);
234 /* Check the type of PCI bus */
240 /* Copy the Type 1 handler data */
241 RtlCopyMemory(&PCIConfigHandler
,
242 &PCIConfigHandlerType1
,
243 sizeof(PCIConfigHandler
));
245 /* Set correct I/O Ports */
246 BusData
->Config
.Type1
.Address
= PCI_TYPE1_ADDRESS_PORT
;
247 BusData
->Config
.Type1
.Data
= PCI_TYPE1_DATA_PORT
;
253 /* Copy the Type 1 handler data */
254 RtlCopyMemory(&PCIConfigHandler
,
255 &PCIConfigHandlerType2
,
256 sizeof (PCIConfigHandler
));
258 /* Set correct I/O Ports */
259 BusData
->Config
.Type2
.CSE
= PCI_TYPE2_CSE_PORT
;
260 BusData
->Config
.Type2
.Forward
= PCI_TYPE2_FORWARD_PORT
;
261 BusData
->Config
.Type2
.Base
= PCI_TYPE2_ADDRESS_BASE
;
263 /* Only 16 devices supported, not 32 */
264 BusData
->MaxDevice
= 16;
270 DbgPrint("HAL: Unnkown PCI type\n");
273 /* Return the bus handler */
280 HalpIsValidPCIDevice(IN PBUS_HANDLER BusHandler
,
281 IN PCI_SLOT_NUMBER Slot
)
283 UCHAR DataBuffer
[PCI_COMMON_HDR_LENGTH
];
284 PPCI_COMMON_CONFIG PciHeader
= (PVOID
)DataBuffer
;
288 /* Read the PCI header */
289 HalpReadPCIConfig(BusHandler
, Slot
, PciHeader
, 0, PCI_COMMON_HDR_LENGTH
);
291 /* Make sure it's a valid device */
292 if ((PciHeader
->VendorID
== PCI_INVALID_VENDORID
) ||
293 (PCI_CONFIGURATION_TYPE(PciHeader
) != PCI_DEVICE_TYPE
))
299 /* Make sure interrupt numbers make sense */
300 if (((PciHeader
->u
.type0
.InterruptPin
) &&
301 (PciHeader
->u
.type0
.InterruptPin
> 4)) ||
302 (PciHeader
->u
.type0
.InterruptLine
& 0x70))
308 /* Now scan PCI BARs */
309 for (i
= 0; i
< PCI_TYPE0_ADDRESSES
; i
++)
311 /* Check what kind of address it is */
312 Address
= PciHeader
->u
.type0
.BaseAddresses
[i
];
313 if (Address
& PCI_ADDRESS_IO_SPACE
)
315 /* Highest I/O port is 65535 */
316 if (Address
> 0xFFFF) return FALSE
;
320 /* MMIO should be higher than 0x80000 */
321 if ((Address
> 0xF) && (Address
< 0x80000)) return FALSE
;
324 /* Is this a 64-bit address? */
325 if (!(Address
& PCI_ADDRESS_IO_SPACE
) &&
326 ((Address
& PCI_ADDRESS_MEMORY_TYPE_MASK
) == PCI_TYPE_64BIT
))
328 /* Check the next-next entry, since this one 64-bits wide */
333 /* Header, interrupt and address data all make sense */
337 static BOOLEAN WarningsGiven
[5];
342 HalpGetChipHacks(IN USHORT VendorId
,
347 UNICODE_STRING KeyName
, ValueName
;
349 OBJECT_ATTRIBUTES ObjectAttributes
;
352 KEY_VALUE_PARTIAL_INFORMATION PartialInfo
;
355 /* Setup the object attributes for the key */
356 RtlInitUnicodeString(&KeyName
,
357 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\"
359 InitializeObjectAttributes(&ObjectAttributes
,
361 OBJ_CASE_INSENSITIVE
,
366 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
367 if (!NT_SUCCESS(Status
)) return Status
;
370 swprintf(Buffer
, L
"%04X%04X", VendorId
, DeviceId
);
371 RtlInitUnicodeString(&ValueName
, Buffer
);
372 Status
= ZwQueryValueKey(KeyHandle
,
374 KeyValuePartialInformation
,
378 if (NT_SUCCESS(Status
))
380 /* Return the flags */
381 DbgPrint("\tFound HackFlags for your chipset\n");
382 *HackFlags
= *(PULONG
)PartialInfo
.Data
;
383 DbgPrint("\t\tHack Flags: %lx (Hack Revision: %lx-Your Revision: %lx)\n",
384 *HackFlags
, HALP_REVISION_FROM_HACK_FLAGS(*HackFlags
), RevisionId
);
386 /* Does it apply to this revision? */
387 if ((RevisionId
) && (RevisionId
>= (HALP_REVISION_FROM_HACK_FLAGS(*HackFlags
))))
389 /* Read the revision flags */
390 *HackFlags
= HALP_REVISION_HACK_FLAGS(*HackFlags
);
393 /* Throw out revision data */
394 *HackFlags
= HALP_HACK_FLAGS(*HackFlags
);
395 if (!*HackFlags
) DbgPrint("\tNo HackFlags for your chipset's revision!\n");
398 /* Close the handle and return */
406 HalpIsRecognizedCard(IN PPCI_REGISTRY_INFO_INTERNAL PciRegistryInfo
,
407 IN PPCI_COMMON_CONFIG PciData
,
410 ULONG ElementCount
, i
;
411 PPCI_CARD_DESCRIPTOR CardDescriptor
;
413 /* How many PCI Cards that we know about? */
414 ElementCount
= PciRegistryInfo
->ElementCount
;
415 if (!ElementCount
) return FALSE
;
417 /* Loop all descriptors */
418 CardDescriptor
= &PciRegistryInfo
->CardList
[0];
419 for (i
= 0; i
< ElementCount
; i
++, CardDescriptor
++)
421 /* Check for flag match */
422 if (CardDescriptor
->Flags
!= Flags
) continue;
424 /* Check for VID-PID match */
425 if ((CardDescriptor
->VendorID
!= PciData
->VendorID
) ||
426 (CardDescriptor
->DeviceID
!= PciData
->DeviceID
))
432 /* Check for revision match, if requested */
433 if ((CardDescriptor
->Flags
& HALP_CHECK_CARD_REVISION_ID
) &&
434 (CardDescriptor
->RevisionID
!= PciData
->RevisionID
))
440 /* Check what kind of device this is */
441 switch (PCI_CONFIGURATION_TYPE(PciData
))
444 case PCI_CARDBUS_BRIDGE_TYPE
:
446 /* This means the real device header is in the device-specific data */
447 PciData
= (PPCI_COMMON_CONFIG
)PciData
->DeviceSpecific
;
449 /* Normal PCI device */
450 case PCI_DEVICE_TYPE
:
452 /* Check for subvendor match, if requested */
453 if ((CardDescriptor
->Flags
& HALP_CHECK_CARD_SUBVENDOR_ID
) &&
454 (CardDescriptor
->SubsystemVendorID
!= PciData
->u
.type0
.SubVendorID
))
460 /* Check for subsystem match, if requested */
461 if ((CardDescriptor
->Flags
& HALP_CHECK_CARD_SUBSYSTEM_ID
) &&
462 (CardDescriptor
->SubsystemID
!= PciData
->u
.type0
.SubSystemID
))
471 /* PCI Bridge -- don't bother */
472 case PCI_BRIDGE_TYPE
:
480 /* This means the card isn't recognized */
487 HalpIsIdeDevice(IN PPCI_COMMON_CONFIG PciData
)
489 /* Simple test first */
490 if ((PciData
->BaseClass
== PCI_CLASS_MASS_STORAGE_CTLR
) &&
491 (PciData
->SubClass
== PCI_SUBCLASS_MSC_IDE_CTLR
))
493 /* The device is nice enough to admit it */
497 /* Symphony 82C101 */
498 if (PciData
->VendorID
== 0x1C1C) return TRUE
;
500 /* ALi MS4803 or M5219 */
501 if ((PciData
->VendorID
== 0x10B9) &&
502 ((PciData
->DeviceID
== 0x5215) || (PciData
->DeviceID
== 0x5219)))
507 /* Appian Technology */
508 if ((PciData
->VendorID
== 0x1097) && (PciData
->DeviceID
== 0x38)) return TRUE
;
510 /* Compaq Triflex Dual EIDE Controller */
511 if ((PciData
->VendorID
== 0xE11) && (PciData
->DeviceID
== 0xAE33)) return TRUE
;
513 /* Micron PC Tech RZ1000 */
514 if ((PciData
->VendorID
== 0x1042) && (PciData
->DeviceID
== 0x1000)) return TRUE
;
516 /* SiS 85C601 or 5513 [IDE] */
517 if ((PciData
->VendorID
== 0x1039) &&
518 ((PciData
->DeviceID
== 0x601) || (PciData
->DeviceID
== 0x5513)))
523 /* Symphony Labs W83769F */
524 if ((PciData
->VendorID
== 0x10AD) &&
525 ((PciData
->DeviceID
== 0x1) || (PciData
->DeviceID
== 0x150)))
531 if ((PciData
->VendorID
== 0x1060) && (PciData
->DeviceID
== 0x101)) return TRUE
;
533 /* You've survived */
540 HalpIsBridgeDevice(IN PPCI_COMMON_CONFIG PciData
)
542 /* Either this is a PCI-to-PCI Bridge, or a CardBUS Bridge */
543 return (((PCI_CONFIGURATION_TYPE(PciData
) == PCI_BRIDGE_TYPE
) &&
544 (PciData
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
545 (PciData
->SubClass
== PCI_SUBCLASS_BR_PCI_TO_PCI
)) ||
546 ((PCI_CONFIGURATION_TYPE(PciData
) == PCI_CARDBUS_BRIDGE_TYPE
) &&
547 (PciData
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
548 (PciData
->SubClass
== PCI_SUBCLASS_BR_CARDBUS
)));
554 HalpGetPciBridgeConfig(IN ULONG PciType
,
557 PCI_SLOT_NUMBER PciSlot
;
559 UCHAR DataBuffer
[PCI_COMMON_HDR_LENGTH
];
560 PPCI_COMMON_CONFIG PciData
= (PPCI_COMMON_CONFIG
)DataBuffer
;
561 PBUS_HANDLER BusHandler
;
564 PciSlot
.u
.bits
.Reserved
= 0;
565 for (i
= 0; i
< *BusCount
; i
++)
567 /* Get the bus handler */
568 BusHandler
= HalHandlerForBus(PCIBus
, i
);
570 /* Loop every device */
571 for (j
= 0; j
< PCI_MAX_DEVICES
; j
++)
573 /* Loop every function */
574 PciSlot
.u
.bits
.DeviceNumber
= j
;
575 for (k
= 0; k
< PCI_MAX_FUNCTION
; k
++)
577 /* Build the final slot structure */
578 PciSlot
.u
.bits
.FunctionNumber
= k
;
580 /* Read the configuration information */
581 HalpReadPCIConfig(BusHandler
,
585 PCI_COMMON_HDR_LENGTH
);
587 /* Skip if this is an invalid function */
588 if (PciData
->VendorID
== PCI_INVALID_VENDORID
) continue;
590 /* Make sure that this is a PCI bridge or a cardbus bridge */
591 if (!HalpIsBridgeDevice(PciData
)) continue;
594 if (!WarningsGiven
[2]++) DPRINT1("Your machine has a PCI-to-PCI or CardBUS Bridge. PCI devices may fail!\n");
600 /* If we exited the loop, then there's no bridge to worry about */
607 HalpFixupPciSupportedRanges(IN ULONG BusCount
)
610 PBUS_HANDLER Bus
, ParentBus
;
613 for (i
= 0; i
< BusCount
; i
++)
615 /* Get PCI bus handler */
616 Bus
= HalHandlerForBus(PCIBus
, i
);
618 /* Loop all parent buses */
619 ParentBus
= Bus
->ParentHandler
;
622 /* Should merge addresses */
623 if (!WarningsGiven
[0]++) DPRINT1("Found parent bus (indicating PCI Bridge). PCI devices may fail!\n");
625 /* Check the next parent */
626 ParentBus
= ParentBus
->ParentHandler
;
630 /* Loop all buses again */
631 for (i
= 0; i
< BusCount
; i
++)
633 /* Get PCI bus handler */
634 Bus
= HalHandlerForBus(PCIBus
, i
);
636 /* Check if this is a PCI 2.2 Bus with Subtractive Decode */
637 if (!((PPCIPBUSDATA
)Bus
->BusData
)->Subtractive
)
639 /* Loop all parent buses */
640 ParentBus
= Bus
->ParentHandler
;
643 /* But check only PCI parent buses specifically */
644 if (ParentBus
->InterfaceType
== PCIBus
)
646 /* Should trim addresses */
647 if (!WarningsGiven
[1]++) DPRINT1("Found parent PCI Bus (indicating PCI-to-PCI Bridge). PCI devices may fail!\n");
650 /* Check the next parent */
651 ParentBus
= ParentBus
->ParentHandler
;
656 /* Loop buses one last time */
657 for (i
= 0; i
< BusCount
; i
++)
659 /* Get the PCI bus handler */
660 Bus
= HalHandlerForBus(PCIBus
, i
);
662 /* Sort and combine (trim) bus address range information */
663 DPRINT("Warning: Bus addresses not being optimized!\n");
676 DbgPrint("%d", (int) x
);
678 else if (x
< 1048576)
680 DbgPrint("%dK", (int)(x
/ 1024));
682 else if (x
< 0x80000000)
684 DbgPrint("%dM", (int)(x
/ 1048576));
694 * These includes are required to define
695 * the ClassTable and VendorTable arrays.
697 #include "pci_classes.h"
698 #include "pci_vendors.h"
702 HalpDebugPciDumpBus(IN ULONG i
,
705 IN PPCI_COMMON_CONFIG PciData
)
707 PCHAR p
, ClassName
, SubClassName
, VendorName
, ProductName
, SubVendorName
;
709 CHAR LookupString
[16] = "";
710 CHAR bSubClassName
[64] = "";
711 CHAR bVendorName
[64] = "";
712 CHAR bProductName
[128] = "Unknown device";
713 CHAR bSubVendorName
[128] = "Unknown";
716 /* Isolate the class name */
717 sprintf(LookupString
, "C %02x ", PciData
->BaseClass
);
718 ClassName
= strstr(ClassTable
, LookupString
);
721 /* Isolate the subclass name */
723 sprintf(LookupString
, "\t%02x ", PciData
->SubClass
);
724 SubClassName
= strstr(ClassName
, LookupString
);
727 /* Copy the subclass into our buffer */
729 p
= strpbrk(SubClassName
, "\r\n");
730 Length
= p
- SubClassName
;
731 if (Length
>= sizeof(bSubClassName
)) Length
= sizeof(bSubClassName
) - 1;
732 strncpy(bSubClassName
, SubClassName
, Length
);
733 bSubClassName
[Length
] = '\0';
737 /* Isolate the vendor name */
738 sprintf(LookupString
, "%04x ", PciData
->VendorID
);
739 VendorName
= strstr(VendorTable
, LookupString
);
742 /* Copy the vendor name into our buffer */
744 p
= strpbrk(VendorName
, "\r\n");
745 Length
= p
- VendorName
;
746 if (Length
>= sizeof(bVendorName
)) Length
= sizeof(bVendorName
) - 1;
747 strncpy(bVendorName
, VendorName
, Length
);
748 bVendorName
[Length
] = '\0';
750 /* Isolate the product name */
751 sprintf(LookupString
, "\t%04x ", PciData
->DeviceID
);
752 ProductName
= strstr(VendorName
, LookupString
);
755 /* Copy the product name into our buffer */
757 p
= strpbrk(ProductName
, "\r\n");
758 Length
= p
- ProductName
;
759 if (Length
>= sizeof(bProductName
)) Length
= sizeof(bProductName
) - 1;
760 strncpy(bProductName
, ProductName
, Length
);
761 bProductName
[Length
] = '\0';
763 /* Isolate the subvendor and subsystem name */
764 sprintf(LookupString
,
766 PciData
->u
.type0
.SubVendorID
,
767 PciData
->u
.type0
.SubSystemID
);
768 SubVendorName
= strstr(ProductName
, LookupString
);
771 /* Copy the subvendor name into our buffer */
773 p
= strpbrk(SubVendorName
, "\r\n");
774 Length
= p
- SubVendorName
;
775 if (Length
>= sizeof(bSubVendorName
)) Length
= sizeof(bSubVendorName
) - 1;
776 strncpy(bSubVendorName
, SubVendorName
, Length
);
777 bSubVendorName
[Length
] = '\0';
782 /* Print out the data */
783 DbgPrint("%02x:%02x.%x %s [%02x%02x]: %s %s [%04x:%04x] (rev %02x)\n"
784 "\tSubsystem: %s [%04x:%04x]\n",
797 PciData
->u
.type0
.SubVendorID
,
798 PciData
->u
.type0
.SubSystemID
);
800 /* Print out and decode flags */
801 DbgPrint("\tFlags:");
802 if (PciData
->Command
& PCI_ENABLE_BUS_MASTER
) DbgPrint(" bus master,");
803 if (PciData
->Status
& PCI_STATUS_66MHZ_CAPABLE
) DbgPrint(" 66MHz,");
804 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x000) DbgPrint(" fast devsel,");
805 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x200) DbgPrint(" medium devsel,");
806 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x400) DbgPrint(" slow devsel,");
807 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x600) DbgPrint(" unknown devsel,");
808 DbgPrint(" latency %d", PciData
->LatencyTimer
);
809 if (PciData
->u
.type0
.InterruptPin
!= 0 &&
810 PciData
->u
.type0
.InterruptLine
!= 0 &&
811 PciData
->u
.type0
.InterruptLine
!= 0xFF) DbgPrint(", IRQ %02d", PciData
->u
.type0
.InterruptLine
);
812 else if (PciData
->u
.type0
.InterruptPin
!= 0) DbgPrint(", IRQ assignment required");
817 for (b
= 0; b
< PCI_TYPE0_ADDRESSES
; b
++)
819 /* Check for a BAR */
820 Mem
= PciData
->u
.type0
.BaseAddresses
[b
];
823 /* Decode the address type */
824 if (Mem
& PCI_ADDRESS_IO_SPACE
)
828 while (!(Mem
& Size
) && (Size
)) Size
<<= 1;
831 DbgPrint("\tI/O ports at %04lx", Mem
& PCI_ADDRESS_IO_ADDRESS_MASK
);
838 while (!(Mem
& Size
) && (Size
)) Size
<<= 1;
841 DbgPrint("\tMemory at %08lx (%d-bit, %sprefetchable)",
842 Mem
& PCI_ADDRESS_MEMORY_ADDRESS_MASK
,
843 (Mem
& PCI_ADDRESS_MEMORY_TYPE_MASK
) == PCI_TYPE_32BIT
? 32 : 64,
844 (Mem
& PCI_ADDRESS_MEMORY_PREFETCHABLE
) ? "" : "non-");
855 HalpInitializePciBus(VOID
)
858 PPCI_REGISTRY_INFO_INTERNAL PciRegistryInfo
;
860 PCI_SLOT_NUMBER PciSlot
;
862 UCHAR DataBuffer
[PCI_COMMON_HDR_LENGTH
];
863 PPCI_COMMON_CONFIG PciData
= (PPCI_COMMON_CONFIG
)DataBuffer
;
864 PBUS_HANDLER BusHandler
;
866 BOOLEAN ExtendedAddressDecoding
= FALSE
;
869 /* Query registry information */
870 PciRegistryInfo
= HalpQueryPciRegistryInfo();
871 if (!PciRegistryInfo
) return;
873 /* Initialize the PCI configuration lock */
874 KeInitializeSpinLock(&HalpPCIConfigLock
);
876 /* Get the type and free the info structure */
877 PciType
= PciRegistryInfo
->HardwareMechanism
& 0xF;
879 /* Check if this is a type 2 PCI bus with at least one bus */
880 if ((PciRegistryInfo
->NoBuses
) && (PciType
== 2))
882 /* Setup the PCI slot */
883 PciSlot
.u
.bits
.Reserved
= 0;
884 PciSlot
.u
.bits
.FunctionNumber
= 0;
887 for (i
= 0; i
< 32; i
++)
889 /* Try to setup a Type 2 PCI slot */
891 BusHandler
= HalpAllocateAndInitPciBusHandler(2, 0, TRUE
);
892 if (!BusHandler
) break;
894 /* Now check if it's valid */
895 if (HalpIsValidPCIDevice(BusHandler
, PciSlot
)) break;
897 /* Heh, the BIOS lied... try Type 1 */
899 BusHandler
= HalpAllocateAndInitPciBusHandler(1, 0, TRUE
);
900 if (!BusHandler
) break;
902 /* Now check if it's valid */
903 if (HalpIsValidPCIDevice(BusHandler
, PciSlot
)) break;
909 /* Now allocate the correct kind of handler */
910 HalpAllocateAndInitPciBusHandler(PciType
, 0, FALSE
);
913 /* Okay, now loop all PCI bridges */
916 /* Loop all PCI buses */
917 for (i
= 0; i
< PciRegistryInfo
->NoBuses
; i
++)
919 /* Check if we have a handler for it */
920 if (!HalHandlerForBus(PCIBus
, i
))
923 HalpAllocateAndInitPciBusHandler(PciType
, i
, FALSE
);
926 /* Go to the next bridge */
927 } while (HalpGetPciBridgeConfig(PciType
, &PciRegistryInfo
->NoBuses
));
929 /* Now build correct address range informaiton */
930 HalpFixupPciSupportedRanges(PciRegistryInfo
->NoBuses
);
933 DbgPrint("\n====== PCI BUS HARDWARE DETECTION =======\n\n");
934 PciSlot
.u
.bits
.Reserved
= 0;
935 for (i
= 0; i
< PciRegistryInfo
->NoBuses
; i
++)
937 /* Get the bus handler */
938 BusHandler
= HalHandlerForBus(PCIBus
, i
);
940 /* Loop every device */
941 for (j
= 0; j
< 32; j
++)
943 /* Loop every function */
944 PciSlot
.u
.bits
.DeviceNumber
= j
;
945 for (k
= 0; k
< 8; k
++)
947 /* Build the final slot structure */
948 PciSlot
.u
.bits
.FunctionNumber
= k
;
950 /* Read the configuration information */
951 HalpReadPCIConfig(BusHandler
,
955 PCI_COMMON_HDR_LENGTH
);
957 /* Skip if this is an invalid function */
958 if (PciData
->VendorID
== PCI_INVALID_VENDORID
) continue;
960 /* Print out the entry */
961 HalpDebugPciDumpBus(i
, j
, k
, PciData
);
963 /* Check if this is a Cardbus bridge */
964 if (PCI_CONFIGURATION_TYPE(PciData
) == PCI_CARDBUS_BRIDGE_TYPE
)
967 DbgPrint("\tDevice is a PCI Cardbus Bridge. It will not work!\n");
971 /* Check if this is a PCI device */
972 if (PCI_CONFIGURATION_TYPE(PciData
) != PCI_BRIDGE_TYPE
)
974 /* Check if it has an interrupt pin and line registered */
975 if ((PciData
->u
.type1
.InterruptPin
) &&
976 (PciData
->u
.type1
.InterruptLine
))
978 /* Check if this interrupt line is connected to the bus */
979 if (PciData
->u
.type1
.InterruptLine
< 16)
981 /* Is this an IDE device? */
982 if (!HalpIsIdeDevice(PciData
))
984 /* We'll mask out this interrupt then */
985 DbgPrint("\tDevice is using IRQ %d! ISA Cards using that IRQ may fail!\n",
986 PciData
->u
.type1
.InterruptLine
);
987 HalpPciIrqMask
|= (1 << PciData
->u
.type1
.InterruptLine
);
993 /* Check for broken Intel chips */
994 if (PciData
->VendorID
== 0x8086)
996 /* Check for broken 82830 PCI controller */
997 if ((PciData
->DeviceID
== 0x04A3) &&
998 (PciData
->RevisionID
< 0x11))
1001 DbgPrint("\tDevice is a broken Intel 82430 PCI Controller. It will not work!\n\n");
1005 /* Check for broken 82378 PCI-to-ISA Bridge */
1006 if ((PciData
->DeviceID
== 0x0484) &&
1007 (PciData
->RevisionID
<= 3))
1010 DbgPrint("\tDevice is a broken Intel 82378 PCI-to-ISA Bridge. It will not work!\n\n");
1014 /* Check for broken 82450 PCI Bridge */
1015 if ((PciData
->DeviceID
== 0x84C4) &&
1016 (PciData
->RevisionID
<= 4))
1018 DbgPrint("\tDevice is a Intel Orion 82450 PCI Bridge. It will not work!\n\n");
1023 /* Do we know this card? */
1024 if (!ExtendedAddressDecoding
)
1027 if (HalpIsRecognizedCard(PciRegistryInfo
,
1029 HALP_CARD_FEATURE_FULL_DECODE
))
1031 /* We'll do chipset checks later */
1032 DbgPrint("\tDevice has Extended Address Decoding. It may fail to work on older BIOSes!\n");
1033 ExtendedAddressDecoding
= TRUE
;
1037 /* Check if this is a USB controller */
1038 if ((PciData
->BaseClass
== PCI_CLASS_SERIAL_BUS_CTLR
) &&
1039 (PciData
->SubClass
== PCI_SUBCLASS_SB_USB
))
1041 /* Check if this is an OHCI controller */
1042 if (PciData
->ProgIf
== 0x10)
1044 DbgPrint("\tDevice is an OHCI (USB) PCI Expansion Card. Turn off Legacy USB in your BIOS!\n\n");
1048 /* Check for Intel UHCI controller */
1049 if (PciData
->VendorID
== 0x8086)
1051 DbgPrint("\tDevice is an Intel UHCI (USB) Controller. Turn off Legacy USB in your BIOS!\n\n");
1055 /* Check for VIA UHCI controller */
1056 if (PciData
->VendorID
== 0x1106)
1058 DbgPrint("\tDevice is a VIA UHCI (USB) Controller. Turn off Legacy USB in your BIOS!\n\n");
1063 /* Now check the registry for chipset hacks */
1064 Status
= HalpGetChipHacks(PciData
->VendorID
,
1066 PciData
->RevisionID
,
1068 if (NT_SUCCESS(Status
))
1070 /* Check for broken ACPI routing */
1071 if (HackFlags
& HAL_PCI_CHIP_HACK_DISABLE_ACPI_IRQ_ROUTING
)
1073 DbgPrint("This chipset has broken ACPI IRQ Routing! Be aware!\n\n");
1077 /* Check for broken ACPI timer */
1078 if (HackFlags
& HAL_PCI_CHIP_HACK_BROKEN_ACPI_TIMER
)
1080 DbgPrint("This chipset has a broken ACPI timer! Be aware!\n\n");
1084 /* Check for hibernate-disable */
1085 if (HackFlags
& HAL_PCI_CHIP_HACK_DISABLE_HIBERNATE
)
1087 DbgPrint("This chipset has a broken PCI device which is incompatible with hibernation. Be aware!\n\n");
1091 /* Check for USB controllers that generate SMIs */
1092 if (HackFlags
& HAL_PCI_CHIP_HACK_USB_SMI_DISABLE
)
1094 DbgPrint("This chipset has a USB controller which generates SMIs. ReactOS will likely fail to boot!\n\n");
1099 /* Terminate the entry */
1105 /* Initialize NMI Crash Flag */
1106 HalpGetNMICrashFlag();
1108 /* Free the registry data */
1109 ExFreePoolWithTag(PciRegistryInfo
, TAG_HAL
);
1111 /* Tell PnP if this hard supports correct decoding */
1112 HalpMarkChipsetDecode(ExtendedAddressDecoding
);
1113 DbgPrint("====== PCI BUS DETECTION COMPLETE =======\n\n");
1120 HalpInitBusHandlers(VOID
)
1122 /* Register the HAL Bus Handler support */
1123 HalpRegisterInternalBusHandlers();
1129 HalpRegisterKdSupportFunctions(VOID
)
1131 /* Register PCI Device Functions */
1132 KdSetupPciDeviceForDebugging
= HalpSetupPciDeviceForDebugging
;
1133 KdReleasePciDeviceforDebugging
= HalpReleasePciDeviceForDebugging
;
1135 /* Register memory functions */
1137 KdMapPhysicalMemory64
= HalpMapPhysicalMemory64
;
1138 KdUnmapVirtualAddress
= HalpUnmapVirtualAddress
;
1141 /* Register ACPI stub */
1142 KdCheckPowerButton
= HalpCheckPowerButton
;
1147 HalpAssignSlotResources(IN PUNICODE_STRING RegistryPath
,
1148 IN PUNICODE_STRING DriverClassName
,
1149 IN PDRIVER_OBJECT DriverObject
,
1150 IN PDEVICE_OBJECT DeviceObject
,
1151 IN INTERFACE_TYPE BusType
,
1153 IN ULONG SlotNumber
,
1154 IN OUT PCM_RESOURCE_LIST
*AllocatedResources
)
1156 PBUS_HANDLER Handler
;
1159 DPRINT1("Slot assignment for %d on bus %u\n", BusType
, BusNumber
);
1161 /* Find the handler */
1162 Handler
= HalReferenceHandlerForBus(BusType
, BusNumber
);
1163 if (!Handler
) return STATUS_NOT_FOUND
;
1165 /* Do the assignment */
1166 Status
= Handler
->AssignSlotResources(Handler
,
1173 AllocatedResources
);
1175 /* Dereference the handler and return */
1176 HalDereferenceBusHandler(Handler
);
1182 HaliFindBusAddressTranslation(IN PHYSICAL_ADDRESS BusAddress
,
1183 IN OUT PULONG AddressSpace
,
1184 OUT PPHYSICAL_ADDRESS TranslatedAddress
,
1185 IN OUT PULONG_PTR Context
,
1188 PHAL_BUS_HANDLER BusHandler
;
1189 PBUS_HANDLER Handler
;
1190 PLIST_ENTRY NextEntry
;
1193 /* Make sure we have a context */
1194 if (!Context
) return FALSE
;
1195 ASSERT((*Context
) || (NextBus
== TRUE
));
1197 /* Read the context */
1198 ContextValue
= *Context
;
1200 /* Find the bus handler */
1201 Handler
= HalpContextToBusHandler(ContextValue
);
1202 if (!Handler
) return FALSE
;
1204 /* Check if this is an ongoing lookup */
1207 /* Get the HAL bus handler */
1208 BusHandler
= CONTAINING_RECORD(Handler
, HAL_BUS_HANDLER
, Handler
);
1209 NextEntry
= &BusHandler
->AllHandlers
;
1211 /* Get the next one if we were already with one */
1212 if (ContextValue
) NextEntry
= NextEntry
->Flink
;
1214 /* Start scanning */
1217 /* Check if this is the last one */
1218 if (NextEntry
== &HalpAllBusHandlers
)
1225 /* Call this translator */
1226 BusHandler
= CONTAINING_RECORD(NextEntry
, HAL_BUS_HANDLER
, AllHandlers
);
1227 if (HalTranslateBusAddress(BusHandler
->Handler
.InterfaceType
,
1228 BusHandler
->Handler
.BusNumber
,
1231 TranslatedAddress
)) break;
1233 /* Try the next one */
1234 NextEntry
= NextEntry
->Flink
;
1237 /* If we made it, we're done */
1238 *Context
= (ULONG_PTR
)Handler
;
1242 /* Try the first one through */
1243 if (!HalTranslateBusAddress(Handler
->InterfaceType
,
1247 TranslatedAddress
)) return FALSE
;
1249 /* Remember for next time */
1250 *Context
= (ULONG_PTR
)Handler
;
1256 HaliTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
1258 IN PHYSICAL_ADDRESS BusAddress
,
1259 IN OUT PULONG AddressSpace
,
1260 OUT PPHYSICAL_ADDRESS TranslatedAddress
)
1262 PBUS_HANDLER Handler
;
1265 /* Find the handler */
1266 Handler
= HalReferenceHandlerForBus(InterfaceType
, BusNumber
);
1267 if (!(Handler
) || !(Handler
->TranslateBusAddress
))
1269 DPRINT1("No translator Interface: %x, Bus: %x, Handler: %p!\n", InterfaceType
, BusNumber
, Handler
);
1273 /* Do the assignment */
1274 Status
= Handler
->TranslateBusAddress(Handler
,
1280 /* Dereference the handler and return */
1281 HalDereferenceBusHandler(Handler
);
1285 /* PUBLIC FUNCTIONS **********************************************************/
1292 HalAdjustResourceList(IN PIO_RESOURCE_REQUIREMENTS_LIST
*ResourceList
)
1294 PBUS_HANDLER Handler
;
1298 /* Find the handler */
1299 Handler
= HalReferenceHandlerForBus((*ResourceList
)->InterfaceType
,
1300 (*ResourceList
)->BusNumber
);
1301 if (!Handler
) return STATUS_SUCCESS
;
1303 /* Do the assignment */
1304 Status
= Handler
->AdjustResourceList(Handler
,
1308 /* Dereference the handler and return */
1309 HalDereferenceBusHandler(Handler
);
1318 HalAssignSlotResources(IN PUNICODE_STRING RegistryPath
,
1319 IN PUNICODE_STRING DriverClassName
,
1320 IN PDRIVER_OBJECT DriverObject
,
1321 IN PDEVICE_OBJECT DeviceObject
,
1322 IN INTERFACE_TYPE BusType
,
1324 IN ULONG SlotNumber
,
1325 IN OUT PCM_RESOURCE_LIST
*AllocatedResources
)
1329 /* Check the bus type */
1330 if (BusType
!= PCIBus
)
1332 /* Call our internal handler */
1333 return HalpAssignSlotResources(RegistryPath
,
1340 AllocatedResources
);
1344 /* Call the PCI registered function */
1345 return HalPciAssignSlotResources(RegistryPath
,
1352 AllocatedResources
);
1361 HalGetBusData(IN BUS_DATA_TYPE BusDataType
,
1363 IN ULONG SlotNumber
,
1367 /* Call the extended function */
1368 return HalGetBusDataByOffset(BusDataType
,
1381 HalGetBusDataByOffset(IN BUS_DATA_TYPE BusDataType
,
1383 IN ULONG SlotNumber
,
1388 PBUS_HANDLER Handler
;
1391 /* Find the handler */
1392 Handler
= HaliReferenceHandlerForConfigSpace(BusDataType
, BusNumber
);
1393 if (!Handler
) return 0;
1395 /* Do the assignment */
1396 Status
= Handler
->GetBusData(Handler
,
1403 /* Dereference the handler and return */
1404 HalDereferenceBusHandler(Handler
);
1413 HalGetInterruptVector(IN INTERFACE_TYPE InterfaceType
,
1415 IN ULONG BusInterruptLevel
,
1416 IN ULONG BusInterruptVector
,
1418 OUT PKAFFINITY Affinity
)
1420 PBUS_HANDLER Handler
;
1428 /* Find the handler */
1429 Handler
= HalReferenceHandlerForBus(InterfaceType
, BusNumber
);
1430 if (!Handler
) return 0;
1432 /* Do the assignment */
1433 Vector
= Handler
->GetInterruptVector(Handler
,
1439 if ((Vector
!= IRQ2VECTOR(BusInterruptLevel
)) ||
1440 (*Irql
!= VECTOR2IRQL(IRQ2VECTOR(BusInterruptLevel
))))
1442 DPRINT1("Returning IRQL %lx, Vector %lx for Level/Vector: %lx/%lx\n",
1443 *Irql
, Vector
, BusInterruptLevel
, BusInterruptVector
);
1444 DPRINT1("Old HAL would've returned IRQL %lx and Vector %lx\n",
1445 VECTOR2IRQL(IRQ2VECTOR(BusInterruptLevel
)),
1446 IRQ2VECTOR(BusInterruptLevel
));
1449 /* Dereference the handler and return */
1450 HalDereferenceBusHandler(Handler
);
1459 HalSetBusData(IN BUS_DATA_TYPE BusDataType
,
1461 IN ULONG SlotNumber
,
1465 /* Call the extended function */
1466 return HalSetBusDataByOffset(BusDataType
,
1479 HalSetBusDataByOffset(IN BUS_DATA_TYPE BusDataType
,
1481 IN ULONG SlotNumber
,
1486 PBUS_HANDLER Handler
;
1489 /* Find the handler */
1490 Handler
= HaliReferenceHandlerForConfigSpace(BusDataType
, BusNumber
);
1491 if (!Handler
) return 0;
1493 /* Do the assignment */
1494 Status
= Handler
->SetBusData(Handler
,
1501 /* Dereference the handler and return */
1502 HalDereferenceBusHandler(Handler
);
1511 HalTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
1513 IN PHYSICAL_ADDRESS BusAddress
,
1514 IN OUT PULONG AddressSpace
,
1515 OUT PPHYSICAL_ADDRESS TranslatedAddress
)
1517 /* Look as the bus type */
1518 if (InterfaceType
== PCIBus
)
1520 /* Call the PCI registered function */
1521 return HalPciTranslateBusAddress(PCIBus
,
1529 /* Call the bus handler */
1530 return HaliTranslateBusAddress(InterfaceType
,