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
,
42 if (!Bus
) return NULL
;
44 /* Check for a valid interface */
45 if (InterfaceType
!= InterfaceTypeUndefined
)
47 /* Allocate address ranges and zero them out */
48 Bus
->BusAddresses
= ExAllocatePoolWithTag(NonPagedPool
,
49 sizeof(SUPPORTED_RANGES
),
51 RtlZeroMemory(Bus
->BusAddresses
, sizeof(SUPPORTED_RANGES
));
53 /* Build the data structure */
54 Bus
->BusAddresses
->Version
= HAL_SUPPORTED_RANGE_VERSION
;
55 Bus
->BusAddresses
->Dma
.Limit
= 7;
56 Bus
->BusAddresses
->Memory
.Limit
= 0xFFFFFFFF;
57 Bus
->BusAddresses
->IO
.Limit
= 0xFFFF;
58 Bus
->BusAddresses
->IO
.SystemAddressSpace
= 1;
59 Bus
->BusAddresses
->PrefetchMemory
.Base
= 1;
62 /* Return the bus address */
69 HalpRegisterInternalBusHandlers(VOID
)
73 /* Only do processor 1 */
74 if (KeGetCurrentPrcb()->Number
) return;
76 /* Register root support */
79 /* Allocate the system bus */
80 Bus
= HalpAllocateBusHandler(Internal
,
81 ConfigurationSpaceUndefined
,
83 InterfaceTypeUndefined
,
89 Bus
->GetInterruptVector
= HalpGetSystemInterruptVector
;
90 Bus
->TranslateBusAddress
= HalpTranslateSystemBusAddress
;
93 /* Allocate the CMOS bus */
94 Bus
= HalpAllocateBusHandler(InterfaceTypeUndefined
,
97 InterfaceTypeUndefined
,
103 Bus
->GetBusData
= HalpcGetCmosData
;
104 Bus
->SetBusData
= HalpcSetCmosData
;
107 /* Allocate the CMOS bus */
108 Bus
= HalpAllocateBusHandler(InterfaceTypeUndefined
,
111 InterfaceTypeUndefined
,
117 Bus
->GetBusData
= HalpcGetCmosData
;
118 Bus
->SetBusData
= HalpcSetCmosData
;
121 /* Allocate ISA bus */
122 Bus
= HalpAllocateBusHandler(Isa
,
123 ConfigurationSpaceUndefined
,
131 Bus
->GetBusData
= HalpNoBusData
;
132 Bus
->BusAddresses
->Memory
.Limit
= 0xFFFFFF;
133 Bus
->TranslateBusAddress
= HalpTranslateIsaBusAddress
;
136 /* No support for EISA or MCA */
137 ASSERT(HalpBusType
== MACHINE_TYPE_ISA
);
144 HalpMarkChipsetDecode(BOOLEAN OverrideEnable
)
147 UNICODE_STRING KeyString
;
148 ULONG Data
= OverrideEnable
;
149 HANDLE KeyHandle
, Handle
;
152 RtlInitUnicodeString(&KeyString
,
153 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET");
154 Status
= HalpOpenRegistryKey(&Handle
, 0, &KeyString
, KEY_ALL_ACCESS
, FALSE
);
155 if (NT_SUCCESS(Status
))
157 /* Open PNP Bios key */
158 RtlInitUnicodeString(&KeyString
, L
"Control\\Biosinfo\\PNPBios");
159 Status
= HalpOpenRegistryKey(&KeyHandle
,
168 /* Check if PNP BIOS key exists */
169 if (NT_SUCCESS(Status
))
171 /* Set the override value */
172 RtlInitUnicodeString(&KeyString
, L
"FullDecodeChipsetOverride");
173 Status
= ZwSetValueKey(KeyHandle
,
192 HalpAllocateAndInitPciBusHandler(IN ULONG PciType
,
194 IN BOOLEAN TestAllocation
)
197 PPCIPBUSDATA BusData
;
199 /* Allocate the bus handler */
200 Bus
= HalpAllocateBusHandler(PCIBus
,
205 sizeof(PCIPBUSDATA
));
208 Bus
->GetBusData
= (PGETSETBUSDATA
)HalpGetPCIData
;
209 Bus
->SetBusData
= (PGETSETBUSDATA
)HalpSetPCIData
;
210 Bus
->GetInterruptVector
= (PGETINTERRUPTVECTOR
)HalpGetPCIIntOnISABus
;
211 Bus
->AdjustResourceList
= (PADJUSTRESOURCELIST
)HalpAdjustPCIResourceList
;
212 Bus
->AssignSlotResources
= (PASSIGNSLOTRESOURCES
)HalpAssignPCISlotResources
;
213 Bus
->BusAddresses
->Dma
.Limit
= 0;
215 /* Get our custom bus data */
216 BusData
= (PPCIPBUSDATA
)Bus
->BusData
;
218 /* Setup custom bus data */
219 BusData
->CommonData
.Tag
= PCI_DATA_TAG
;
220 BusData
->CommonData
.Version
= PCI_DATA_VERSION
;
221 BusData
->CommonData
.ReadConfig
= (PciReadWriteConfig
)HalpReadPCIConfig
;
222 BusData
->CommonData
.WriteConfig
= (PciReadWriteConfig
)HalpWritePCIConfig
;
223 BusData
->CommonData
.Pin2Line
= (PciPin2Line
)HalpPCIPin2ISALine
;
224 BusData
->CommonData
.Line2Pin
= (PciLine2Pin
)HalpPCIISALine2Pin
;
225 BusData
->MaxDevice
= PCI_MAX_DEVICES
;
226 BusData
->GetIrqRange
= (PciIrqRange
)HalpGetISAFixedPCIIrq
;
228 /* Initialize the bitmap */
229 RtlInitializeBitMap(&BusData
->DeviceConfigured
, BusData
->ConfiguredBits
, 256);
231 /* Check the type of PCI bus */
237 /* Copy the Type 1 handler data */
238 RtlCopyMemory(&PCIConfigHandler
,
239 &PCIConfigHandlerType1
,
240 sizeof(PCIConfigHandler
));
242 /* Set correct I/O Ports */
243 BusData
->Config
.Type1
.Address
= PCI_TYPE1_ADDRESS_PORT
;
244 BusData
->Config
.Type1
.Data
= PCI_TYPE1_DATA_PORT
;
250 /* Copy the Type 1 handler data */
251 RtlCopyMemory(&PCIConfigHandler
,
252 &PCIConfigHandlerType2
,
253 sizeof (PCIConfigHandler
));
255 /* Set correct I/O Ports */
256 BusData
->Config
.Type2
.CSE
= PCI_TYPE2_CSE_PORT
;
257 BusData
->Config
.Type2
.Forward
= PCI_TYPE2_FORWARD_PORT
;
258 BusData
->Config
.Type2
.Base
= PCI_TYPE2_ADDRESS_BASE
;
260 /* Only 16 devices supported, not 32 */
261 BusData
->MaxDevice
= 16;
267 DbgPrint("HAL: Unnkown PCI type\n");
270 /* Return the bus handler */
277 HalpIsValidPCIDevice(IN PBUS_HANDLER BusHandler
,
278 IN PCI_SLOT_NUMBER Slot
)
280 UCHAR DataBuffer
[PCI_COMMON_HDR_LENGTH
];
281 PPCI_COMMON_CONFIG PciHeader
= (PVOID
)DataBuffer
;
285 /* Read the PCI header */
286 HalpReadPCIConfig(BusHandler
, Slot
, PciHeader
, 0, PCI_COMMON_HDR_LENGTH
);
288 /* Make sure it's a valid device */
289 if ((PciHeader
->VendorID
== PCI_INVALID_VENDORID
) ||
290 (PCI_CONFIGURATION_TYPE(PciHeader
) != PCI_DEVICE_TYPE
))
296 /* Make sure interrupt numbers make sense */
297 if (((PciHeader
->u
.type0
.InterruptPin
) &&
298 (PciHeader
->u
.type0
.InterruptPin
> 4)) ||
299 (PciHeader
->u
.type0
.InterruptLine
& 0x70))
305 /* Now scan PCI BARs */
306 for (i
= 0; i
< PCI_TYPE0_ADDRESSES
; i
++)
308 /* Check what kind of address it is */
309 Address
= PciHeader
->u
.type0
.BaseAddresses
[i
];
310 if (Address
& PCI_ADDRESS_IO_SPACE
)
312 /* Highest I/O port is 65535 */
313 if (Address
> 0xFFFF) return FALSE
;
317 /* MMIO should be higher than 0x80000 */
318 if ((Address
> 0xF) && (Address
< 0x80000)) return FALSE
;
321 /* Is this a 64-bit address? */
322 if (!(Address
& PCI_ADDRESS_IO_SPACE
) &&
323 ((Address
& PCI_ADDRESS_MEMORY_TYPE_MASK
) == PCI_TYPE_64BIT
))
325 /* Check the next-next entry, since this one 64-bits wide */
330 /* Header, interrupt and address data all make sense */
334 static BOOLEAN WarningsGiven
[5];
339 HalpGetChipHacks(IN USHORT VendorId
,
344 UNICODE_STRING KeyName
, ValueName
;
346 OBJECT_ATTRIBUTES ObjectAttributes
;
349 KEY_VALUE_PARTIAL_INFORMATION PartialInfo
;
352 /* Setup the object attributes for the key */
353 RtlInitUnicodeString(&KeyName
,
354 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\"
356 InitializeObjectAttributes(&ObjectAttributes
,
358 OBJ_CASE_INSENSITIVE
,
363 Status
= ZwOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
364 if (!NT_SUCCESS(Status
)) return Status
;
367 swprintf(Buffer
, L
"%04X%04X", VendorId
, DeviceId
);
368 RtlInitUnicodeString(&ValueName
, Buffer
);
369 Status
= ZwQueryValueKey(KeyHandle
,
371 KeyValuePartialInformation
,
375 if (NT_SUCCESS(Status
))
377 /* Return the flags */
378 DbgPrint("\tFound HackFlags for your chipset\n");
379 *HackFlags
= *(PULONG
)PartialInfo
.Data
;
380 DbgPrint("\t\tHack Flags: %lx (Hack Revision: %lx-Your Revision: %lx)\n",
381 *HackFlags
, HALP_REVISION_FROM_HACK_FLAGS(*HackFlags
), RevisionId
);
383 /* Does it apply to this revision? */
384 if ((RevisionId
) && (RevisionId
>= (HALP_REVISION_FROM_HACK_FLAGS(*HackFlags
))))
386 /* Read the revision flags */
387 *HackFlags
= HALP_REVISION_HACK_FLAGS(*HackFlags
);
390 /* Throw out revision data */
391 *HackFlags
= HALP_HACK_FLAGS(*HackFlags
);
392 if (!*HackFlags
) DbgPrint("\tNo HackFlags for your chipset's revision!\n");
395 /* Close the handle and return */
403 HalpIsRecognizedCard(IN PPCI_REGISTRY_INFO_INTERNAL PciRegistryInfo
,
404 IN PPCI_COMMON_CONFIG PciData
,
407 ULONG ElementCount
, i
;
408 PPCI_CARD_DESCRIPTOR CardDescriptor
;
410 /* How many PCI Cards that we know about? */
411 ElementCount
= PciRegistryInfo
->ElementCount
;
412 if (!ElementCount
) return FALSE
;
414 /* Loop all descriptors */
415 CardDescriptor
= &PciRegistryInfo
->CardList
[0];
416 for (i
= 0; i
< ElementCount
; i
++, CardDescriptor
++)
418 /* Check for flag match */
419 if (CardDescriptor
->Flags
!= Flags
) continue;
421 /* Check for VID-PID match */
422 if ((CardDescriptor
->VendorID
!= PciData
->VendorID
) ||
423 (CardDescriptor
->DeviceID
!= PciData
->DeviceID
))
429 /* Check for revision match, if requested */
430 if ((CardDescriptor
->Flags
& HALP_CHECK_CARD_REVISION_ID
) &&
431 (CardDescriptor
->RevisionID
!= PciData
->RevisionID
))
437 /* Check what kind of device this is */
438 switch (PCI_CONFIGURATION_TYPE(PciData
))
441 case PCI_CARDBUS_BRIDGE_TYPE
:
443 /* This means the real device header is in the device-specific data */
444 PciData
= (PPCI_COMMON_CONFIG
)PciData
->DeviceSpecific
;
446 /* Normal PCI device */
447 case PCI_DEVICE_TYPE
:
449 /* Check for subvendor match, if requested */
450 if ((CardDescriptor
->Flags
& HALP_CHECK_CARD_SUBVENDOR_ID
) &&
451 (CardDescriptor
->SubsystemVendorID
!= PciData
->u
.type0
.SubVendorID
))
457 /* Check for subsystem match, if requested */
458 if ((CardDescriptor
->Flags
& HALP_CHECK_CARD_SUBSYSTEM_ID
) &&
459 (CardDescriptor
->SubsystemID
!= PciData
->u
.type0
.SubSystemID
))
468 /* PCI Bridge -- don't bother */
469 case PCI_BRIDGE_TYPE
:
477 /* This means the card isn't recognized */
484 HalpIsIdeDevice(IN PPCI_COMMON_CONFIG PciData
)
486 /* Simple test first */
487 if ((PciData
->BaseClass
== PCI_CLASS_MASS_STORAGE_CTLR
) &&
488 (PciData
->SubClass
== PCI_SUBCLASS_MSC_IDE_CTLR
))
490 /* The device is nice enough to admit it */
494 /* Symphony 82C101 */
495 if (PciData
->VendorID
== 0x1C1C) return TRUE
;
497 /* ALi MS4803 or M5219 */
498 if ((PciData
->VendorID
== 0x10B9) &&
499 ((PciData
->DeviceID
== 0x5215) || (PciData
->DeviceID
== 0x5219)))
504 /* Appian Technology */
505 if ((PciData
->VendorID
== 0x1097) && (PciData
->DeviceID
== 0x38)) return TRUE
;
507 /* Compaq Triflex Dual EIDE Controller */
508 if ((PciData
->VendorID
== 0xE11) && (PciData
->DeviceID
== 0xAE33)) return TRUE
;
510 /* Micron PC Tech RZ1000 */
511 if ((PciData
->VendorID
== 0x1042) && (PciData
->DeviceID
== 0x1000)) return TRUE
;
513 /* SiS 85C601 or 5513 [IDE] */
514 if ((PciData
->VendorID
== 0x1039) &&
515 ((PciData
->DeviceID
== 0x601) || (PciData
->DeviceID
== 0x5513)))
520 /* Symphony Labs W83769F */
521 if ((PciData
->VendorID
== 0x10AD) &&
522 ((PciData
->DeviceID
== 0x1) || (PciData
->DeviceID
== 0x150)))
528 if ((PciData
->VendorID
== 0x1060) && (PciData
->DeviceID
== 0x101)) return TRUE
;
530 /* You've survived */
537 HalpIsBridgeDevice(IN PPCI_COMMON_CONFIG PciData
)
539 /* Either this is a PCI-to-PCI Bridge, or a CardBUS Bridge */
540 return (((PCI_CONFIGURATION_TYPE(PciData
) == PCI_BRIDGE_TYPE
) &&
541 (PciData
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
542 (PciData
->SubClass
== PCI_SUBCLASS_BR_PCI_TO_PCI
)) ||
543 ((PCI_CONFIGURATION_TYPE(PciData
) == PCI_CARDBUS_BRIDGE_TYPE
) &&
544 (PciData
->BaseClass
== PCI_CLASS_BRIDGE_DEV
) &&
545 (PciData
->SubClass
== PCI_SUBCLASS_BR_CARDBUS
)));
551 HalpGetPciBridgeConfig(IN ULONG PciType
,
554 PCI_SLOT_NUMBER PciSlot
;
556 UCHAR DataBuffer
[PCI_COMMON_HDR_LENGTH
];
557 PPCI_COMMON_CONFIG PciData
= (PPCI_COMMON_CONFIG
)DataBuffer
;
558 PBUS_HANDLER BusHandler
;
561 PciSlot
.u
.bits
.Reserved
= 0;
562 for (i
= 0; i
< *BusCount
; i
++)
564 /* Get the bus handler */
565 BusHandler
= HalHandlerForBus(PCIBus
, i
);
567 /* Loop every device */
568 for (j
= 0; j
< PCI_MAX_DEVICES
; j
++)
570 /* Loop every function */
571 PciSlot
.u
.bits
.DeviceNumber
= j
;
572 for (k
= 0; k
< PCI_MAX_FUNCTION
; k
++)
574 /* Build the final slot structure */
575 PciSlot
.u
.bits
.FunctionNumber
= k
;
577 /* Read the configuration information */
578 HalpReadPCIConfig(BusHandler
,
582 PCI_COMMON_HDR_LENGTH
);
584 /* Skip if this is an invalid function */
585 if (PciData
->VendorID
== PCI_INVALID_VENDORID
) continue;
587 /* Make sure that this is a PCI bridge or a cardbus bridge */
588 if (!HalpIsBridgeDevice(PciData
)) continue;
591 if (!WarningsGiven
[2]++) DPRINT1("Your machine has a PCI-to-PCI or CardBUS Bridge. PCI devices may fail!\n");
597 /* If we exited the loop, then there's no bridge to worry about */
604 HalpFixupPciSupportedRanges(IN ULONG BusCount
)
607 PBUS_HANDLER Bus
, ParentBus
;
610 for (i
= 0; i
< BusCount
; i
++)
612 /* Get PCI bus handler */
613 Bus
= HalHandlerForBus(PCIBus
, i
);
615 /* Loop all parent buses */
616 ParentBus
= Bus
->ParentHandler
;
619 /* Should merge addresses */
620 if (!WarningsGiven
[0]++) DPRINT1("Found parent bus (indicating PCI Bridge). PCI devices may fail!\n");
622 /* Check the next parent */
623 ParentBus
= ParentBus
->ParentHandler
;
627 /* Loop all buses again */
628 for (i
= 0; i
< BusCount
; i
++)
630 /* Get PCI bus handler */
631 Bus
= HalHandlerForBus(PCIBus
, i
);
633 /* Check if this is a PCI 2.2 Bus with Subtractive Decode */
634 if (!((PPCIPBUSDATA
)Bus
->BusData
)->Subtractive
)
636 /* Loop all parent buses */
637 ParentBus
= Bus
->ParentHandler
;
640 /* But check only PCI parent buses specifically */
641 if (ParentBus
->InterfaceType
== PCIBus
)
643 /* Should trim addresses */
644 if (!WarningsGiven
[1]++) DPRINT1("Found parent PCI Bus (indicating PCI-to-PCI Bridge). PCI devices may fail!\n");
647 /* Check the next parent */
648 ParentBus
= ParentBus
->ParentHandler
;
653 /* Loop buses one last time */
654 for (i
= 0; i
< BusCount
; i
++)
656 /* Get the PCI bus handler */
657 Bus
= HalHandlerForBus(PCIBus
, i
);
659 /* Sort and combine (trim) bus address range information */
660 DPRINT("Warning: Bus addresses not being optimized!\n");
673 DbgPrint("%d", (int) x
);
675 else if (x
< 1048576)
677 DbgPrint("%dK", (int)(x
/ 1024));
679 else if (x
< 0x80000000)
681 DbgPrint("%dM", (int)(x
/ 1048576));
693 HalpDebugPciDumpBus(IN ULONG i
,
696 IN PPCI_COMMON_CONFIG PciData
)
698 extern CHAR ClassTable
[3922];
699 extern CHAR VendorTable
[642355];
700 PCHAR p
, ClassName
, SubClassName
, VendorName
, ProductName
, SubVendorName
;
702 CHAR LookupString
[16] = "";
703 CHAR bSubClassName
[64] = "";
704 CHAR bVendorName
[64] = "";
705 CHAR bProductName
[128] = "Unknown device";
706 CHAR bSubVendorName
[128] = "Unknown";
709 /* Isolate the class name */
710 sprintf(LookupString
, "C %02x", PciData
->BaseClass
);
711 ClassName
= strstr(ClassTable
, LookupString
);
714 /* Isolate the subclass name */
716 sprintf(LookupString
, "\t%02x", PciData
->SubClass
);
717 SubClassName
= strstr(ClassName
, LookupString
);
720 /* Copy the subclass into our buffer */
722 p
= strchr(SubClassName
, '\r');
723 Length
= p
- SubClassName
;
724 if (Length
>= sizeof(bSubClassName
)) Length
= sizeof(bSubClassName
) - 1;
725 strncpy(bSubClassName
, SubClassName
, Length
);
726 bSubClassName
[Length
] = '\0';
730 /* Isolate the vendor name */
731 sprintf(LookupString
, "\n%04x ", PciData
->VendorID
);
732 VendorName
= strstr(VendorTable
, LookupString
);
735 /* Copy the vendor name into our buffer */
737 p
= strchr(VendorName
, '\r');
738 Length
= p
- VendorName
;
739 if (Length
>= sizeof(bVendorName
)) Length
= sizeof(bVendorName
) - 1;
740 strncpy(bVendorName
, VendorName
, Length
);
741 bVendorName
[Length
] = '\0';
743 /* Isolate the product name */
744 sprintf(LookupString
, "\t%04x", PciData
->DeviceID
);
745 ProductName
= strstr(VendorName
, LookupString
);
748 /* Copy the product name into our buffer */
750 p
= strchr(ProductName
, '\r');
751 Length
= p
- ProductName
;
752 if (Length
>= sizeof(bProductName
)) Length
= sizeof(bProductName
) - 1;
753 strncpy(bProductName
, ProductName
, Length
);
754 bProductName
[Length
] = '\0';
756 /* Isolate the subvendor and subsystem name */
757 sprintf(LookupString
,
759 PciData
->u
.type0
.SubVendorID
,
760 PciData
->u
.type0
.SubSystemID
);
761 SubVendorName
= strstr(ProductName
, LookupString
);
764 /* Copy the subvendor name into our buffer */
766 p
= strchr(SubVendorName
, '\r');
767 Length
= p
- SubVendorName
;
768 if (Length
>= sizeof(bSubVendorName
)) Length
= sizeof(bSubVendorName
) - 1;
769 strncpy(bSubVendorName
, SubVendorName
, Length
);
770 bSubVendorName
[Length
] = '\0';
775 /* Print out the data */
776 DbgPrint("%02x:%02x.%x %s [%02x%02x]: %s %s [%04x:%04x] (rev %02x)\n"
777 "\tSubsystem: %s [%04x:%04x]\n",
790 PciData
->u
.type0
.SubVendorID
,
791 PciData
->u
.type0
.SubSystemID
);
793 /* Print out and decode flags */
794 DbgPrint("\tFlags:");
795 if (PciData
->Command
& PCI_ENABLE_BUS_MASTER
) DbgPrint(" bus master,");
796 if (PciData
->Status
& PCI_STATUS_66MHZ_CAPABLE
) DbgPrint(" 66MHz,");
797 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x000) DbgPrint(" fast devsel,");
798 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x200) DbgPrint(" medium devsel,");
799 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x400) DbgPrint(" slow devsel,");
800 if ((PciData
->Status
& PCI_STATUS_DEVSEL
) == 0x600) DbgPrint(" unknown devsel,");
801 DbgPrint(" latency %d", PciData
->LatencyTimer
);
802 if (PciData
->u
.type0
.InterruptPin
!= 0 &&
803 PciData
->u
.type0
.InterruptLine
!= 0 &&
804 PciData
->u
.type0
.InterruptLine
!= 0xFF) DbgPrint(", IRQ %02d", PciData
->u
.type0
.InterruptLine
);
805 else if (PciData
->u
.type0
.InterruptPin
!= 0) DbgPrint(", IRQ assignment required");
810 for (b
= 0; b
< PCI_TYPE0_ADDRESSES
; b
++)
812 /* Check for a BAR */
813 Mem
= PciData
->u
.type0
.BaseAddresses
[b
];
816 /* Decode the address type */
817 if (Mem
& PCI_ADDRESS_IO_SPACE
)
821 while (!(Mem
& Size
) && (Size
)) Size
<<= 1;
824 DbgPrint("\tI/O ports at %04lx", Mem
& PCI_ADDRESS_IO_ADDRESS_MASK
);
831 while (!(Mem
& Size
) && (Size
)) Size
<<= 1;
834 DbgPrint("\tMemory at %08lx (%d-bit, %sprefetchable)",
835 Mem
& PCI_ADDRESS_MEMORY_ADDRESS_MASK
,
836 (Mem
& PCI_ADDRESS_MEMORY_TYPE_MASK
) == PCI_TYPE_32BIT
? 32 : 64,
837 (Mem
& PCI_ADDRESS_MEMORY_PREFETCHABLE
) ? "" : "non-");
848 HalpInitializePciBus(VOID
)
851 PPCI_REGISTRY_INFO_INTERNAL PciRegistryInfo
;
853 PCI_SLOT_NUMBER PciSlot
;
855 UCHAR DataBuffer
[PCI_COMMON_HDR_LENGTH
];
856 PPCI_COMMON_CONFIG PciData
= (PPCI_COMMON_CONFIG
)DataBuffer
;
857 PBUS_HANDLER BusHandler
;
859 BOOLEAN ExtendedAddressDecoding
= FALSE
;
862 /* Query registry information */
863 PciRegistryInfo
= HalpQueryPciRegistryInfo();
864 if (!PciRegistryInfo
) return;
866 /* Initialize the PCI configuration lock */
867 KeInitializeSpinLock(&HalpPCIConfigLock
);
869 /* Get the type and free the info structure */
870 PciType
= PciRegistryInfo
->HardwareMechanism
& 0xF;
872 /* Check if this is a type 2 PCI bus with at least one bus */
873 if ((PciRegistryInfo
->NoBuses
) && (PciType
== 2))
875 /* Setup the PCI slot */
876 PciSlot
.u
.bits
.Reserved
= 0;
877 PciSlot
.u
.bits
.FunctionNumber
= 0;
880 for (i
= 0; i
< 32; i
++)
882 /* Try to setup a Type 2 PCI slot */
884 BusHandler
= HalpAllocateAndInitPciBusHandler(2, 0, TRUE
);
885 if (!BusHandler
) break;
887 /* Now check if it's valid */
888 if (HalpIsValidPCIDevice(BusHandler
, PciSlot
)) break;
890 /* Heh, the BIOS lied... try Type 1 */
892 BusHandler
= HalpAllocateAndInitPciBusHandler(1, 0, TRUE
);
893 if (!BusHandler
) break;
895 /* Now check if it's valid */
896 if (HalpIsValidPCIDevice(BusHandler
, PciSlot
)) break;
902 /* Now allocate the correct kind of handler */
903 HalpAllocateAndInitPciBusHandler(PciType
, 0, FALSE
);
906 /* Okay, now loop all PCI bridges */
909 /* Loop all PCI buses */
910 for (i
= 0; i
< PciRegistryInfo
->NoBuses
; i
++)
912 /* Check if we have a handler for it */
913 if (!HalHandlerForBus(PCIBus
, i
))
916 HalpAllocateAndInitPciBusHandler(PciType
, i
, FALSE
);
919 /* Go to the next bridge */
920 } while (HalpGetPciBridgeConfig(PciType
, &PciRegistryInfo
->NoBuses
));
922 /* Now build correct address range informaiton */
923 HalpFixupPciSupportedRanges(PciRegistryInfo
->NoBuses
);
926 DbgPrint("\n====== PCI BUS HARDWARE DETECTION =======\n\n");
927 PciSlot
.u
.bits
.Reserved
= 0;
928 for (i
= 0; i
< PciRegistryInfo
->NoBuses
; i
++)
930 /* Get the bus handler */
931 BusHandler
= HalHandlerForBus(PCIBus
, i
);
933 /* Loop every device */
934 for (j
= 0; j
< 32; j
++)
936 /* Loop every function */
937 PciSlot
.u
.bits
.DeviceNumber
= j
;
938 for (k
= 0; k
< 8; k
++)
940 /* Build the final slot structure */
941 PciSlot
.u
.bits
.FunctionNumber
= k
;
943 /* Read the configuration information */
944 HalpReadPCIConfig(BusHandler
,
948 PCI_COMMON_HDR_LENGTH
);
950 /* Skip if this is an invalid function */
951 if (PciData
->VendorID
== PCI_INVALID_VENDORID
) continue;
953 /* Print out the entry */
954 HalpDebugPciDumpBus(i
, j
, k
, PciData
);
956 /* Check if this is a Cardbus bridge */
957 if (PCI_CONFIGURATION_TYPE(PciData
) == PCI_CARDBUS_BRIDGE_TYPE
)
960 DbgPrint("\tDevice is a PCI Cardbus Bridge. It will not work!\n");
964 /* Check if this is a PCI device */
965 if (PCI_CONFIGURATION_TYPE(PciData
) != PCI_BRIDGE_TYPE
)
967 /* Check if it has an interrupt pin and line registered */
968 if ((PciData
->u
.type1
.InterruptPin
) &&
969 (PciData
->u
.type1
.InterruptLine
))
971 /* Check if this interrupt line is connected to the bus */
972 if (PciData
->u
.type1
.InterruptLine
< 16)
974 /* Is this an IDE device? */
975 if (!HalpIsIdeDevice(PciData
))
977 /* We'll mask out this interrupt then */
978 DbgPrint("\tDevice is using IRQ %d! ISA Cards using that IRQ may fail!\n",
979 PciData
->u
.type1
.InterruptLine
);
980 HalpPciIrqMask
|= (1 << PciData
->u
.type1
.InterruptLine
);
986 /* Check for broken Intel chips */
987 if (PciData
->VendorID
== 0x8086)
989 /* Check for broken 82830 PCI controller */
990 if ((PciData
->DeviceID
== 0x04A3) &&
991 (PciData
->RevisionID
< 0x11))
994 DbgPrint("\tDevice is a broken Intel 82430 PCI Controller. It will not work!\n\n");
998 /* Check for broken 82378 PCI-to-ISA Bridge */
999 if ((PciData
->DeviceID
== 0x0484) &&
1000 (PciData
->RevisionID
<= 3))
1003 DbgPrint("\tDevice is a broken Intel 82378 PCI-to-ISA Bridge. It will not work!\n\n");
1007 /* Check for broken 82450 PCI Bridge */
1008 if ((PciData
->DeviceID
== 0x84C4) &&
1009 (PciData
->RevisionID
<= 4))
1011 DbgPrint("\tDevice is a Intel Orion 82450 PCI Bridge. It will not work!\n\n");
1016 /* Do we know this card? */
1017 if (!ExtendedAddressDecoding
)
1020 if (HalpIsRecognizedCard(PciRegistryInfo
,
1022 HALP_CARD_FEATURE_FULL_DECODE
))
1024 /* We'll do chipset checks later */
1025 DbgPrint("\tDevice has Extended Address Decoding. It may fail to work on older BIOSes!\n");
1026 ExtendedAddressDecoding
= TRUE
;
1030 /* Check if this is a USB controller */
1031 if ((PciData
->BaseClass
== PCI_CLASS_SERIAL_BUS_CTLR
) &&
1032 (PciData
->SubClass
== PCI_SUBCLASS_SB_USB
))
1034 /* Check if this is an OHCI controller */
1035 if (PciData
->ProgIf
== 0x10)
1037 DbgPrint("\tDevice is an OHCI (USB) PCI Expansion Card. Turn off Legacy USB in your BIOS!\n\n");
1041 /* Check for Intel UHCI controller */
1042 if (PciData
->VendorID
== 0x8086)
1044 DbgPrint("\tDevice is an Intel UHCI (USB) Controller. Turn off Legacy USB in your BIOS!\n\n");
1048 /* Check for VIA UHCI controller */
1049 if (PciData
->VendorID
== 0x1106)
1051 DbgPrint("\tDevice is a VIA UHCI (USB) Controller. Turn off Legacy USB in your BIOS!\n\n");
1056 /* Now check the registry for chipset hacks */
1057 Status
= HalpGetChipHacks(PciData
->VendorID
,
1059 PciData
->RevisionID
,
1061 if (NT_SUCCESS(Status
))
1063 /* Check for broken ACPI routing */
1064 if (HackFlags
& HAL_PCI_CHIP_HACK_DISABLE_ACPI_IRQ_ROUTING
)
1066 DbgPrint("This chipset has broken ACPI IRQ Routing! Be aware!\n\n");
1070 /* Check for broken ACPI timer */
1071 if (HackFlags
& HAL_PCI_CHIP_HACK_BROKEN_ACPI_TIMER
)
1073 DbgPrint("This chipset has a broken ACPI timer! Be aware!\n\n");
1077 /* Check for hibernate-disable */
1078 if (HackFlags
& HAL_PCI_CHIP_HACK_DISABLE_HIBERNATE
)
1080 DbgPrint("This chipset has a broken PCI device which is incompatible with hibernation. Be aware!\n\n");
1084 /* Check for USB controllers that generate SMIs */
1085 if (HackFlags
& HAL_PCI_CHIP_HACK_USB_SMI_DISABLE
)
1087 DbgPrint("This chipset has a USB controller which generates SMIs. ReactOS will likely fail to boot!\n\n");
1092 /* Terminate the entry */
1098 /* Initialize NMI Crash Flag */
1099 HalpGetNMICrashFlag();
1101 /* Free the registry data */
1102 ExFreePoolWithTag(PciRegistryInfo
, TAG_HAL
);
1104 /* Tell PnP if this hard supports correct decoding */
1105 HalpMarkChipsetDecode(ExtendedAddressDecoding
);
1106 DbgPrint("====== PCI BUS DETECTION COMPLETE =======\n\n");
1113 HalpInitBusHandlers(VOID
)
1115 /* Register the HAL Bus Handler support */
1116 HalpRegisterInternalBusHandlers();
1122 HalpRegisterKdSupportFunctions(VOID
)
1124 /* Register PCI Device Functions */
1125 KdSetupPciDeviceForDebugging
= HalpSetupPciDeviceForDebugging
;
1126 KdReleasePciDeviceforDebugging
= HalpReleasePciDeviceForDebugging
;
1128 /* Register memory functions */
1130 KdMapPhysicalMemory64
= HalpMapPhysicalMemory64
;
1131 KdUnmapVirtualAddress
= HalpUnmapVirtualAddress
;
1134 /* Register ACPI stub */
1135 KdCheckPowerButton
= HalpCheckPowerButton
;
1140 HalpAssignSlotResources(IN PUNICODE_STRING RegistryPath
,
1141 IN PUNICODE_STRING DriverClassName
,
1142 IN PDRIVER_OBJECT DriverObject
,
1143 IN PDEVICE_OBJECT DeviceObject
,
1144 IN INTERFACE_TYPE BusType
,
1146 IN ULONG SlotNumber
,
1147 IN OUT PCM_RESOURCE_LIST
*AllocatedResources
)
1149 PBUS_HANDLER Handler
;
1152 DPRINT1("Slot assignment for %d on bus %d\n", BusType
, BusNumber
);
1154 /* Find the handler */
1155 Handler
= HalReferenceHandlerForBus(BusType
, BusNumber
);
1156 if (!Handler
) return STATUS_NOT_FOUND
;
1158 /* Do the assignment */
1159 Status
= Handler
->AssignSlotResources(Handler
,
1166 AllocatedResources
);
1168 /* Dereference the handler and return */
1169 HalDereferenceBusHandler(Handler
);
1175 HaliFindBusAddressTranslation(IN PHYSICAL_ADDRESS BusAddress
,
1176 IN OUT PULONG AddressSpace
,
1177 OUT PPHYSICAL_ADDRESS TranslatedAddress
,
1178 IN OUT PULONG_PTR Context
,
1181 PHAL_BUS_HANDLER BusHandler
;
1182 PBUS_HANDLER Handler
;
1183 PLIST_ENTRY NextEntry
;
1186 /* Make sure we have a context */
1187 if (!Context
) return FALSE
;
1188 ASSERT((*Context
) || (NextBus
== TRUE
));
1190 /* Read the context */
1191 ContextValue
= *Context
;
1193 /* Find the bus handler */
1194 Handler
= HalpContextToBusHandler(ContextValue
);
1195 if (!Handler
) return FALSE
;
1197 /* Check if this is an ongoing lookup */
1200 /* Get the HAL bus handler */
1201 BusHandler
= CONTAINING_RECORD(Handler
, HAL_BUS_HANDLER
, Handler
);
1202 NextEntry
= &BusHandler
->AllHandlers
;
1204 /* Get the next one if we were already with one */
1205 if (ContextValue
) NextEntry
= NextEntry
->Flink
;
1207 /* Start scanning */
1210 /* Check if this is the last one */
1211 if (NextEntry
== &HalpAllBusHandlers
)
1218 /* Call this translator */
1219 BusHandler
= CONTAINING_RECORD(NextEntry
, HAL_BUS_HANDLER
, AllHandlers
);
1220 if (HalTranslateBusAddress(BusHandler
->Handler
.InterfaceType
,
1221 BusHandler
->Handler
.BusNumber
,
1224 TranslatedAddress
)) break;
1226 /* Try the next one */
1227 NextEntry
= NextEntry
->Flink
;
1230 /* If we made it, we're done */
1231 *Context
= (ULONG_PTR
)Handler
;
1235 /* Try the first one through */
1236 if (!HalTranslateBusAddress(Handler
->InterfaceType
,
1240 TranslatedAddress
)) return FALSE
;
1242 /* Remember for next time */
1243 *Context
= (ULONG_PTR
)Handler
;
1249 HaliTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
1251 IN PHYSICAL_ADDRESS BusAddress
,
1252 IN OUT PULONG AddressSpace
,
1253 OUT PPHYSICAL_ADDRESS TranslatedAddress
)
1255 PBUS_HANDLER Handler
;
1258 /* Find the handler */
1259 Handler
= HalReferenceHandlerForBus(InterfaceType
, BusNumber
);
1260 if (!(Handler
) || !(Handler
->TranslateBusAddress
))
1262 DPRINT1("No translator Interface: %x, Bus: %x, Handler: %x!\n", InterfaceType
, BusNumber
, Handler
);
1266 /* Do the assignment */
1267 Status
= Handler
->TranslateBusAddress(Handler
,
1273 /* Dereference the handler and return */
1274 HalDereferenceBusHandler(Handler
);
1278 /* PUBLIC FUNCTIONS **********************************************************/
1285 HalAdjustResourceList(IN PIO_RESOURCE_REQUIREMENTS_LIST
*ResourceList
)
1287 PBUS_HANDLER Handler
;
1291 /* Find the handler */
1292 Handler
= HalReferenceHandlerForBus((*ResourceList
)->InterfaceType
,
1293 (*ResourceList
)->BusNumber
);
1294 if (!Handler
) return STATUS_SUCCESS
;
1296 /* Do the assignment */
1297 Status
= Handler
->AdjustResourceList(Handler
,
1301 /* Dereference the handler and return */
1302 HalDereferenceBusHandler(Handler
);
1311 HalAssignSlotResources(IN PUNICODE_STRING RegistryPath
,
1312 IN PUNICODE_STRING DriverClassName
,
1313 IN PDRIVER_OBJECT DriverObject
,
1314 IN PDEVICE_OBJECT DeviceObject
,
1315 IN INTERFACE_TYPE BusType
,
1317 IN ULONG SlotNumber
,
1318 IN OUT PCM_RESOURCE_LIST
*AllocatedResources
)
1322 /* Check the bus type */
1323 if (BusType
!= PCIBus
)
1325 /* Call our internal handler */
1326 return HalpAssignSlotResources(RegistryPath
,
1333 AllocatedResources
);
1337 /* Call the PCI registered function */
1338 return HalPciAssignSlotResources(RegistryPath
,
1345 AllocatedResources
);
1354 HalGetBusData(IN BUS_DATA_TYPE BusDataType
,
1356 IN ULONG SlotNumber
,
1360 /* Call the extended function */
1361 return HalGetBusDataByOffset(BusDataType
,
1374 HalGetBusDataByOffset(IN BUS_DATA_TYPE BusDataType
,
1376 IN ULONG SlotNumber
,
1381 PBUS_HANDLER Handler
;
1384 /* Find the handler */
1385 Handler
= HaliReferenceHandlerForConfigSpace(BusDataType
, BusNumber
);
1386 if (!Handler
) return 0;
1388 /* Do the assignment */
1389 Status
= Handler
->GetBusData(Handler
,
1396 /* Dereference the handler and return */
1397 HalDereferenceBusHandler(Handler
);
1406 HalGetInterruptVector(IN INTERFACE_TYPE InterfaceType
,
1408 IN ULONG BusInterruptLevel
,
1409 IN ULONG BusInterruptVector
,
1411 OUT PKAFFINITY Affinity
)
1413 PBUS_HANDLER Handler
;
1421 /* Find the handler */
1422 Handler
= HalReferenceHandlerForBus(InterfaceType
, BusNumber
);
1423 if (!Handler
) return 0;
1425 /* Do the assignment */
1426 Vector
= Handler
->GetInterruptVector(Handler
,
1432 if ((Vector
!= IRQ2VECTOR(BusInterruptLevel
)) ||
1433 (*Irql
!= VECTOR2IRQL(IRQ2VECTOR(BusInterruptLevel
))))
1435 DPRINT1("Returning IRQL %lx, Vector %lx for Level/Vector: %lx/%lx\n",
1436 *Irql
, Vector
, BusInterruptLevel
, BusInterruptVector
);
1437 DPRINT1("Old HAL would've returned IRQL %lx and Vector %lx\n",
1438 VECTOR2IRQL(IRQ2VECTOR(BusInterruptLevel
)),
1439 IRQ2VECTOR(BusInterruptLevel
));
1442 /* Dereference the handler and return */
1443 HalDereferenceBusHandler(Handler
);
1452 HalSetBusData(IN BUS_DATA_TYPE BusDataType
,
1454 IN ULONG SlotNumber
,
1458 /* Call the extended function */
1459 return HalSetBusDataByOffset(BusDataType
,
1472 HalSetBusDataByOffset(IN BUS_DATA_TYPE BusDataType
,
1474 IN ULONG SlotNumber
,
1479 PBUS_HANDLER Handler
;
1482 /* Find the handler */
1483 Handler
= HaliReferenceHandlerForConfigSpace(BusDataType
, BusNumber
);
1484 if (!Handler
) return 0;
1486 /* Do the assignment */
1487 Status
= Handler
->SetBusData(Handler
,
1494 /* Dereference the handler and return */
1495 HalDereferenceBusHandler(Handler
);
1504 HalTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
1506 IN PHYSICAL_ADDRESS BusAddress
,
1507 IN OUT PULONG AddressSpace
,
1508 OUT PPHYSICAL_ADDRESS TranslatedAddress
)
1510 /* Look as the bus type */
1511 if (InterfaceType
== PCIBus
)
1513 /* Call the PCI registered function */
1514 return HalPciTranslateBusAddress(PCIBus
,
1522 /* Call the bus handler */
1523 return HaliTranslateBusAddress(InterfaceType
,