Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1985 lines
59 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. pmpcibus.c
  5. Abstract:
  6. Implements simplified PCI configuration
  7. read and write functions for use in
  8. an ACPI HAL.
  9. Author:
  10. Jake Oshins (jakeo) 1-Dec-1997
  11. Chris Hyser (chrish@fc.hp.com) 23-Jun-1997
  12. Neal Vu (neal.vu@intel.com) 11-Jul-2000
  13. Environment:
  14. Kernel mode only.
  15. Revision History:
  16. --*/
  17. #include "halp.h"
  18. #include "pci.h"
  19. #include "pcip.h"
  20. #define MAX(a, b) \
  21. ((a) > (b) ? (a) : (b))
  22. #define MIN(a, b) \
  23. ((a) < (b) ? (a) : (b))
  24. NTSTATUS
  25. HalpSearchForPciDebuggingDevice(
  26. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice,
  27. IN ULONG StartBusNumber,
  28. IN ULONG EndBusNumber,
  29. IN ULONG MinMem,
  30. IN ULONG MaxMem,
  31. IN USHORT MinIo,
  32. IN USHORT MaxIo,
  33. IN BOOLEAN ConfigureBridges
  34. );
  35. PCIPBUSDATA HalpFakePciBusData = {
  36. {
  37. PCI_DATA_TAG,//Tag
  38. PCI_DATA_VERSION,//Version
  39. (PciReadWriteConfig)HalpReadPCIConfig,//ReadConfig
  40. (PciReadWriteConfig) HalpWritePCIConfig,//WriteConfig
  41. (PciPin2Line)HalpPCIPin2ISALine,//Pin2Line
  42. (PciLine2Pin)HalpPCIISALine2Pin,//Line2Pin
  43. {0},//ParentSlot
  44. NULL,NULL,NULL,NULL//Reserved[4]
  45. },
  46. {0},//Config
  47. PCI_MAX_DEVICES,//MaxDevice
  48. };
  49. BUS_HANDLER HalpFakePciBusHandler = {
  50. BUS_HANDLER_VERSION,//Version
  51. PCIBus,//InterfaceType
  52. PCIConfiguration,//ConfigurationType
  53. 0,//BusNumber
  54. NULL,//DeviceObject
  55. NULL,//ParentHandler
  56. (PPCIBUSDATA)&HalpFakePciBusData,//BusData
  57. 0,//DeviceControlExtensionSize
  58. NULL,//BusAddresses
  59. {0},//Reserved[4]
  60. (PGETSETBUSDATA)HalpGetPCIData,//GetBusData
  61. (PGETSETBUSDATA)HalpSetPCIData,//SetBusData
  62. NULL,//AdjustResourceList
  63. (PASSIGNSLOTRESOURCES)HalpAssignPCISlotResources,//AssignSlotResources
  64. NULL,//GetInterruptVector
  65. NULL,//TranslateBusAddress
  66. };
  67. ULONG HalpMinPciBus = 0;
  68. ULONG HalpMaxPciBus = 0;
  69. #define MAX_DEBUGGING_DEVICES_SUPPORTED 2
  70. PCI_TYPE1_CFG_CYCLE_BITS HalpPciDebuggingDevice[MAX_DEBUGGING_DEVICES_SUPPORTED] = {0};
  71. extern BOOLEAN HalpDoingCrashDump;
  72. extern KSPIN_LOCK HalpPCIConfigLock;
  73. PVOID
  74. HalpGetAcpiTablePhase0(
  75. IN PLOADER_PARAMETER_BLOCK LoaderBlock,
  76. IN ULONG Signature
  77. );
  78. VOID
  79. HalpFindFreeResourceLimits(
  80. IN ULONG Bus,
  81. IN OUT ULONG *MinIo,
  82. IN OUT ULONG *MaxIo,
  83. IN OUT ULONG *MinMem,
  84. IN OUT ULONG *MaxMem,
  85. IN OUT ULONG *MinBus,
  86. IN OUT ULONG *MaxBus
  87. );
  88. NTSTATUS
  89. HalpSetupUnconfiguredDebuggingDevice(
  90. IN ULONG Bus,
  91. IN ULONG Slot,
  92. IN ULONG IoMin,
  93. IN ULONG IoMax,
  94. IN ULONG MemMin,
  95. IN ULONG MemMax,
  96. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
  97. );
  98. NTSTATUS
  99. HalpConfigurePciBridge(
  100. IN PDEBUG_DEVICE_DESCRIPTOR PciDevice,
  101. IN ULONG Bus,
  102. IN ULONG Slot,
  103. IN ULONG IoMin,
  104. IN ULONG IoMax,
  105. IN ULONG MemMin,
  106. IN ULONG MemMax,
  107. IN ULONG BusMin,
  108. IN ULONG BusMax,
  109. IN OUT PPCI_COMMON_CONFIG PciData
  110. );
  111. VOID
  112. HalpUnconfigurePciBridge(
  113. IN ULONG Bus,
  114. IN ULONG Slot
  115. );
  116. VOID
  117. HalpRegisterKdSupportFunctions(
  118. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  119. );
  120. VOID
  121. HalpRegisterPciDebuggingDeviceInfo(
  122. VOID
  123. );
  124. ULONG
  125. HalpPhase0GetPciDataByOffset (
  126. ULONG BusNumber,
  127. ULONG SlotNumber,
  128. PVOID Buffer,
  129. ULONG Offset,
  130. ULONG Length
  131. );
  132. ULONG
  133. HalpPhase0SetPciDataByOffset (
  134. ULONG BusNumber,
  135. ULONG SlotNumber,
  136. PVOID Buffer,
  137. ULONG Offset,
  138. ULONG Length
  139. );
  140. NTSTATUS
  141. HalpReleasePciDeviceForDebugging(
  142. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
  143. );
  144. NTSTATUS
  145. HalpSetupPciDeviceForDebugging(
  146. IN PLOADER_PARAMETER_BLOCK LoaderBlock, OPTIONAL
  147. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
  148. );
  149. #ifdef ALLOC_PRAGMA
  150. #pragma alloc_text(INIT,HalpInitializePciBus)
  151. #pragma alloc_text(INIT,HalpRegisterKdSupportFunctions)
  152. #pragma alloc_text(INIT,HalpRegisterPciDebuggingDeviceInfo)
  153. #pragma alloc_text(PAGELK,HalpConfigurePciBridge)
  154. #pragma alloc_text(PAGELK,HalpFindFreeResourceLimits)
  155. #pragma alloc_text(PAGELK,HalpPhase0GetPciDataByOffset)
  156. #pragma alloc_text(PAGELK,HalpPhase0SetPciDataByOffset)
  157. #pragma alloc_text(PAGELK,HalpReleasePciDeviceForDebugging)
  158. #pragma alloc_text(PAGELK,HalpSearchForPciDebuggingDevice)
  159. #pragma alloc_text(PAGELK,HalpSetupPciDeviceForDebugging)
  160. #pragma alloc_text(PAGELK,HalpSetupUnconfiguredDebuggingDevice)
  161. #pragma alloc_text(PAGELK,HalpUnconfigurePciBridge)
  162. #endif
  163. VOID
  164. HalpInitializePciBus (
  165. VOID
  166. )
  167. {
  168. PPCIPBUSDATA BusData;
  169. UCHAR iBuffer[PCI_COMMON_HDR_LENGTH];
  170. //
  171. // Initialize spinlock for synchronizing access to PCI space
  172. //
  173. KeInitializeSpinLock (&HalpPCIConfigLock);
  174. RtlZeroMemory(&HalpFakePciBusHandler, sizeof(BUS_HANDLER));
  175. HalpFakePciBusHandler.Version = BUS_HANDLER_VERSION;
  176. HalpFakePciBusHandler.InterfaceType = PCIBus;
  177. HalpFakePciBusHandler.ConfigurationType = PCIConfiguration;
  178. //
  179. // Fill in PCI handlers
  180. //
  181. HalpFakePciBusHandler.GetBusData = (PGETSETBUSDATA) HalpGetPCIData;
  182. HalpFakePciBusHandler.SetBusData = (PGETSETBUSDATA) HalpSetPCIData;
  183. HalpFakePciBusHandler.AssignSlotResources = (PASSIGNSLOTRESOURCES) HalpAssignPCISlotResources;
  184. HalpFakePciBusHandler.BusData = &HalpFakePciBusData;
  185. BusData = (PPCIPBUSDATA) HalpFakePciBusHandler.BusData;
  186. //
  187. // Fill in common PCI data
  188. //
  189. BusData->CommonData.Tag = PCI_DATA_TAG;
  190. BusData->CommonData.Version = PCI_DATA_VERSION;
  191. BusData->CommonData.ReadConfig = (PciReadWriteConfig) HalpReadPCIConfig;
  192. BusData->CommonData.WriteConfig = (PciReadWriteConfig) HalpWritePCIConfig;
  193. BusData->CommonData.Pin2Line = (PciPin2Line) HalpPCIPin2ISALine;
  194. BusData->CommonData.Line2Pin = (PciLine2Pin) HalpPCIISALine2Pin;
  195. //
  196. // Set defaults
  197. //
  198. BusData->MaxDevice = PCI_MAX_DEVICES;
  199. //
  200. // There used to be a switch statment on HwType which installed
  201. // different handlers based on PCI Configuration Type. This
  202. // has been removed since SAL is always used for IA64.
  203. //
  204. }
  205. ULONG
  206. HaliPciInterfaceReadConfig(
  207. IN PVOID Context,
  208. IN UCHAR BusOffset,
  209. IN ULONG Slot,
  210. IN PVOID Buffer,
  211. IN ULONG Offset,
  212. IN ULONG Length
  213. )
  214. {
  215. PCI_SLOT_NUMBER slotNum;
  216. BUS_HANDLER busHand;
  217. UNREFERENCED_PARAMETER(Context);
  218. slotNum.u.AsULONG = Slot;
  219. //
  220. // Fake a bus handler.
  221. //
  222. RtlCopyMemory(&busHand, &HalpFakePciBusHandler, sizeof(BUS_HANDLER));
  223. //
  224. // Calculate the right bus number.
  225. //
  226. busHand.BusNumber = BusOffset;
  227. HalpReadPCIConfig(&busHand,
  228. slotNum,
  229. Buffer,
  230. Offset,
  231. Length
  232. );
  233. //
  234. // This is a hack. The legacy HAL interfaces need to be able
  235. // to distinguish between busses that exist and busses that
  236. // don't. And many users of the legacy interfaces implicitly
  237. // assume that PCI busses are tightly packed. (i.e. All busses
  238. // between the lowest numbered one and the highest numbered one
  239. // exist.) So here we are keeping track of the highest numbered
  240. // bus that we have seen so far.
  241. //
  242. if ((Length >= 2) &&
  243. (((PPCI_COMMON_CONFIG)Buffer)->VendorID != PCI_INVALID_VENDORID)) {
  244. //
  245. // This is a valid device.
  246. //
  247. if (busHand.BusNumber > HalpMaxPciBus) {
  248. //
  249. // This is the highest numbered bus we have
  250. // yet seen.
  251. //
  252. HalpMaxPciBus = busHand.BusNumber;
  253. }
  254. }
  255. return Length;
  256. }
  257. ULONG
  258. HaliPciInterfaceWriteConfig(
  259. IN PVOID Context,
  260. IN UCHAR BusOffset,
  261. IN ULONG Slot,
  262. IN PVOID Buffer,
  263. IN ULONG Offset,
  264. IN ULONG Length
  265. )
  266. {
  267. PCI_SLOT_NUMBER slotNum;
  268. BUS_HANDLER busHand;
  269. UNREFERENCED_PARAMETER(Context);
  270. slotNum.u.AsULONG = Slot;
  271. //
  272. // Fake a bus handler.
  273. //
  274. RtlCopyMemory(&busHand, &HalpFakePciBusHandler, sizeof(BUS_HANDLER));
  275. //
  276. // Calculate the right bus number.
  277. //
  278. busHand.BusNumber = BusOffset;
  279. HalpWritePCIConfig(&busHand,
  280. slotNum,
  281. Buffer,
  282. Offset,
  283. Length
  284. );
  285. return Length;
  286. }
  287. VOID
  288. HalpPCIPin2ISALine (
  289. IN PBUS_HANDLER BusHandler,
  290. IN PBUS_HANDLER RootHandler,
  291. IN PCI_SLOT_NUMBER SlotNumber,
  292. IN PPCI_COMMON_CONFIG PciData
  293. )
  294. /*++
  295. This function maps the device's InterruptPin to an InterruptLine
  296. value.
  297. On the current PC implementations, the bios has already filled in
  298. InterruptLine as it's ISA value and there's no portable way to
  299. change it.
  300. On a DBG build we adjust InterruptLine just to ensure driver's
  301. don't connect to it without translating it on the PCI bus.
  302. --*/
  303. {
  304. if (!PciData->u.type0.InterruptPin) {
  305. return ;
  306. }
  307. HalDebugPrint(( HAL_INFO, "HAL: HalpPCIPin2ISALine - non-zero InterruptPin value\n" ));
  308. }
  309. VOID
  310. HalpPCIISALine2Pin (
  311. IN PBUS_HANDLER BusHandler,
  312. IN PBUS_HANDLER RootHandler,
  313. IN PCI_SLOT_NUMBER SlotNumber,
  314. IN PPCI_COMMON_CONFIG PciNewData,
  315. IN PPCI_COMMON_CONFIG PciOldData
  316. )
  317. /*++
  318. This functions maps the device's InterruptLine to it's
  319. device specific InterruptPin value.
  320. On the current PC implementations, this information is
  321. fixed by the BIOS. Just make sure the value isn't being
  322. editted since PCI doesn't tell us how to dynically
  323. connect the interrupt.
  324. --*/
  325. {
  326. if (!PciNewData->u.type0.InterruptPin) {
  327. return ;
  328. }
  329. }
  330. VOID
  331. HalpSetMaxLegacyPciBusNumber(
  332. IN ULONG BusNumber
  333. )
  334. /*++
  335. Routine Description:
  336. This routine bumps the Legacy PCI bus maximum up to whatever
  337. is passed in. This may be necessary because the ACPI driver
  338. needs to run a configuration cycle to a PCI device before the
  339. PCI driver loads. This happens mostly in the context of a
  340. _REG method.
  341. Arguments:
  342. BusNumber - max PCI bus number
  343. Return Value:
  344. none
  345. --*/
  346. {
  347. if (BusNumber > HalpMaxPciBus) {
  348. HalpMaxPciBus = BusNumber;
  349. }
  350. }
  351. ULONG
  352. HalpPhase0SetPciDataByOffset (
  353. ULONG BusNumber,
  354. ULONG SlotNumber,
  355. PVOID Buffer,
  356. ULONG Offset,
  357. ULONG Length
  358. )
  359. /*++
  360. Routine Description:
  361. This routine writes to PCI configuration space prior to bus handler
  362. installation.
  363. Arguments:
  364. BusNumber PCI Bus Number. This is the 8 bit BUS Number which is
  365. bits 23-16 of the Configuration Address. In support of
  366. multiple top level busses, the upper 24 bits of this
  367. argument will supply the index into the table of
  368. configuration address registers.
  369. SlotNumber PCI Slot Number, 8 bits composed of the 5 bit device
  370. number (bits 15-11 of the configuration address) and
  371. the 3 bit function number (10-8).
  372. Buffer Address of source data.
  373. Offset Number of bytes to skip from base of PCI config area.
  374. Length Number of bytes to write
  375. Return Value:
  376. Returns length of data written.
  377. Notes:
  378. Caller is responsible for acquiring any necessary PCI config
  379. spinlocks.
  380. --*/
  381. {
  382. PCI_TYPE1_CFG_BITS ConfigAddress;
  383. ULONG ReturnLength;
  384. PCI_SLOT_NUMBER slot;
  385. PUCHAR Bfr = (PUCHAR)Buffer;
  386. ASSERT(!(Offset & ~0xff));
  387. ASSERT(Length);
  388. ASSERT((Offset + Length) <= 256);
  389. if ( Length + Offset > 256 ) {
  390. if ( Offset > 256 ) {
  391. return 0;
  392. }
  393. Length = 256 - Offset;
  394. }
  395. ReturnLength = Length;
  396. slot.u.AsULONG = SlotNumber;
  397. ConfigAddress.u.bits.BusNumber = BusNumber;
  398. ConfigAddress.u.bits.DeviceNumber = slot.u.bits.DeviceNumber;
  399. ConfigAddress.u.bits.FunctionNumber = slot.u.bits.FunctionNumber;
  400. ConfigAddress.u.bits.RegisterNumber = (Offset & 0xfc) >> 2;
  401. ConfigAddress.u.bits.Enable = TRUE;
  402. if ( Offset & 0x3 ) {
  403. //
  404. // Access begins at a non-register boundary in the config
  405. // space. We need to read the register containing the data
  406. // and rewrite only the changed data. (I wonder if this
  407. // ever really happens?)
  408. //
  409. ULONG SubOffset = Offset & 0x3;
  410. ULONG SubLength = 4 - SubOffset;
  411. union {
  412. ULONG All;
  413. UCHAR Bytes[4];
  414. } Tmp;
  415. if ( SubLength > Length ) {
  416. SubLength = Length;
  417. }
  418. //
  419. // Adjust Length (remaining) and (new) Offset by amount covered
  420. // in this first word.
  421. //
  422. Length -= SubLength;
  423. Offset += SubLength;
  424. //
  425. // Get the first word (register), replace only those bytes that
  426. // need to be changed, then write the whole thing back out again.
  427. //
  428. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_ADDR_PORT, ConfigAddress.u.AsULONG);
  429. Tmp.All = READ_PORT_ULONG((PULONG)PCI_TYPE1_DATA_PORT);
  430. while ( SubLength-- ) {
  431. Tmp.Bytes[SubOffset++] = *Bfr++;
  432. }
  433. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_DATA_PORT, Tmp.All);
  434. //
  435. // Aim ConfigAddressRegister at the next word (register).
  436. //
  437. ConfigAddress.u.bits.RegisterNumber++;
  438. }
  439. //
  440. // Do the majority of the transfer 4 bytes at a time.
  441. //
  442. while ( Length > sizeof(ULONG) ) {
  443. ULONG Tmp = *(PULONG)Bfr;
  444. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_ADDR_PORT, ConfigAddress.u.AsULONG);
  445. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_DATA_PORT, Tmp);
  446. ConfigAddress.u.bits.RegisterNumber++;
  447. Bfr += sizeof(ULONG);
  448. Length -= sizeof(ULONG);
  449. }
  450. //
  451. // Do bytes in last register.
  452. //
  453. if ( Length ) {
  454. union {
  455. ULONG All;
  456. UCHAR Bytes[4];
  457. } Tmp;
  458. ULONG i = 0;
  459. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_ADDR_PORT, ConfigAddress.u.AsULONG);
  460. Tmp.All = READ_PORT_ULONG((PULONG)PCI_TYPE1_DATA_PORT);
  461. while ( Length-- ) {
  462. Tmp.Bytes[i++] = *(PUCHAR)Bfr++;
  463. }
  464. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_DATA_PORT, Tmp.All);
  465. }
  466. return ReturnLength;
  467. }
  468. ULONG
  469. HalpPhase0GetPciDataByOffset (
  470. ULONG BusNumber,
  471. ULONG SlotNumber,
  472. PVOID Buffer,
  473. ULONG Offset,
  474. ULONG Length
  475. )
  476. /*++
  477. Routine Description:
  478. This routine reads PCI config space prior to bus handlder installation.
  479. Arguments:
  480. BusNumber PCI Bus Number. This is the 8 bit BUS Number which is
  481. bits 23-16 of the Configuration Address. In support of
  482. multiple top level busses, the upper 24 bits of this
  483. argument will supply the index into the table of
  484. configuration address registers.
  485. SlotNumber PCI Slot Number, 8 bits composed of the 5 bit device
  486. number (bits 15-11 of the configuration address) and
  487. the 3 bit function number (10-8).
  488. Buffer Address of source data.
  489. Offset Number of bytes to skip from base of PCI config area.
  490. Length Number of bytes to write
  491. Return Value:
  492. Amount of data read.
  493. --*/
  494. {
  495. PCI_TYPE1_CFG_BITS ConfigAddress;
  496. PCI_TYPE1_CFG_BITS ConfigAddressTemp;
  497. ULONG ReturnLength;
  498. ULONG i;
  499. PCI_SLOT_NUMBER slot;
  500. union {
  501. ULONG All;
  502. UCHAR Bytes[4];
  503. } Tmp;
  504. ASSERT(!(Offset & ~0xff));
  505. ASSERT(Length);
  506. ASSERT((Offset + Length) <= 256);
  507. if ( Length + Offset > 256 ) {
  508. if ( Offset > 256 ) {
  509. return 0;
  510. }
  511. Length = 256 - Offset;
  512. }
  513. ReturnLength = Length;
  514. slot.u.AsULONG = SlotNumber;
  515. ConfigAddress.u.bits.BusNumber = BusNumber;
  516. ConfigAddress.u.bits.DeviceNumber = slot.u.bits.DeviceNumber;
  517. ConfigAddress.u.bits.FunctionNumber = slot.u.bits.FunctionNumber;
  518. ConfigAddress.u.bits.RegisterNumber = (Offset & 0xfc) >> 2;
  519. ConfigAddress.u.bits.Enable = TRUE;
  520. //
  521. // If we are being asked to read data when function != 0, check
  522. // first to see if this device decares itself as a multi-function
  523. // device. If it doesn't, don't do this read.
  524. //
  525. if (ConfigAddress.u.bits.FunctionNumber != 0) {
  526. ConfigAddressTemp.u.bits.RegisterNumber = 3; // contains header type
  527. ConfigAddressTemp.u.bits.FunctionNumber = 0; // look at base package
  528. ConfigAddressTemp.u.bits.DeviceNumber = ConfigAddress.u.bits.DeviceNumber;
  529. ConfigAddressTemp.u.bits.BusNumber = ConfigAddress.u.bits.BusNumber;
  530. ConfigAddressTemp.u.bits.Enable = TRUE;
  531. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_ADDR_PORT, ConfigAddressTemp.u.AsULONG);
  532. Tmp.All = READ_PORT_ULONG((PULONG)PCI_TYPE1_DATA_PORT);
  533. if (!(Tmp.Bytes[2] & 0x80)) { // if the Header type field's multi-function bit is not set
  534. for (i = 0; i < Length; i++) {
  535. *((PUCHAR)Buffer)++ = 0xff; // Make this read as if the device isn't populated
  536. }
  537. return Length;
  538. }
  539. }
  540. i = Offset & 0x3;
  541. while ( Length ) {
  542. WRITE_PORT_ULONG((PULONG)PCI_TYPE1_ADDR_PORT, ConfigAddress.u.AsULONG);
  543. Tmp.All = READ_PORT_ULONG((PULONG)PCI_TYPE1_DATA_PORT);
  544. while ( (i < 4) && Length) {
  545. *((PUCHAR)Buffer)++ = Tmp.Bytes[i];
  546. i++;
  547. Length--;
  548. }
  549. i = 0;
  550. ConfigAddress.u.bits.RegisterNumber++;
  551. }
  552. return ReturnLength;
  553. }
  554. NTSTATUS
  555. HalpSetupPciDeviceForDebugging(
  556. IN PLOADER_PARAMETER_BLOCK LoaderBlock, OPTIONAL
  557. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
  558. )
  559. /*++
  560. Routine Description:
  561. This routine finds and initializes a PCI device to be
  562. used for communicating with a debugger.
  563. The caller fills in as much of DEBUG_DEVICE_DESCRIPTOR
  564. as it cares to, filling unused fields with (-1).
  565. This routine attempts to find a matching PCI device. It
  566. matches first based on Bus and Slot, if the caller has
  567. provided them. Then it matches on VendorID/DeviceID, if
  568. the caller has provided them. Last, it matches on
  569. BaseClass/SubClass.
  570. This routine will fill in any unused fields in the structure
  571. so that the caller can know specifically which PCI
  572. device matched the criteria.
  573. If the matching PCI device is not enabled, or it is
  574. behind a PCI to PCI bridge that is not enabled, this
  575. routine makes a best-effort attempt to find a safe
  576. configuration that allows the device (and possibly bridges)
  577. to function, and enables them.
  578. If the PCI device implements memory mapped Base Address
  579. registers, this function will create a virtual to physical
  580. mapping for the memory ranges implied by the Base Address
  581. Registers and fill in the TranslatedAddress field with
  582. virtual pointers to the bases of the ranges. It will then
  583. fill in the Type field with CmResourceTypeMemory. And
  584. the Valid field with be TRUE.
  585. If the PCI device implements I/O port Base Address registers,
  586. this function will put the translated port address in
  587. TranslatedAddress, setting the Type field to CmResourceTypePort
  588. and the Valid field to TRUE.
  589. If the PCI device does not implement a specific Base Address
  590. Register, the Valid field will be FALSE.
  591. Arguments:
  592. PciDevice - Structure indicating the device
  593. Return Value:
  594. STATUS_SUCCESS if the device is configured and usable.
  595. STATUS_NO_MORE_MATCHES if no device matched the criteria.
  596. STATUS_INSUFFICIENT_RESOURCES if the memory requirements
  597. couldn't be met.
  598. STATUS_UNSUCCESSFUL if the routine failed for other reasons.
  599. --*/
  600. {
  601. NTSTATUS status;
  602. PCI_SLOT_NUMBER slot;
  603. ULONG i, j;
  604. ULONG maxPhys;
  605. status = HalpSearchForPciDebuggingDevice(
  606. PciDevice,
  607. 0,
  608. 0xff,
  609. 0x10000000,
  610. 0xfc000000,
  611. 0x1000,
  612. 0xffff,
  613. FALSE);
  614. if (!NT_SUCCESS(status)) {
  615. //
  616. // We didn't find the device using a conservative
  617. // search. Try a more invasive one.
  618. //
  619. status = HalpSearchForPciDebuggingDevice(
  620. PciDevice,
  621. 0,
  622. 0xff,
  623. 0x10000000,
  624. 0xfc000000,
  625. 0x1000,
  626. 0xffff,
  627. TRUE);
  628. }
  629. //
  630. // Record the Bus/Dev/Func so that we can stuff it in the
  631. // registry later.
  632. //
  633. if (NT_SUCCESS(status)) {
  634. slot.u.AsULONG = PciDevice->Slot;
  635. for (i = 0;
  636. i < MAX_DEBUGGING_DEVICES_SUPPORTED;
  637. i++) {
  638. if ((HalpPciDebuggingDevice[i].u.bits.Reserved1 == TRUE) &&
  639. (HalpPciDebuggingDevice[i].u.bits.FunctionNumber ==
  640. slot.u.bits.FunctionNumber) &&
  641. (HalpPciDebuggingDevice[i].u.bits.DeviceNumber ==
  642. slot.u.bits.DeviceNumber) &&
  643. (HalpPciDebuggingDevice[i].u.bits.BusNumber ==
  644. PciDevice->Bus)) {
  645. //
  646. // This device has already been set up for
  647. // debugging. Thus we should refuse to set
  648. // it up again.
  649. //
  650. return STATUS_UNSUCCESSFUL;
  651. }
  652. }
  653. for (i = 0;
  654. i < MAX_DEBUGGING_DEVICES_SUPPORTED;
  655. i++) {
  656. if (HalpPciDebuggingDevice[i].u.bits.Reserved1 == FALSE) {
  657. //
  658. // This slot is available.
  659. //
  660. HalpPciDebuggingDevice[i].u.bits.FunctionNumber =
  661. slot.u.bits.FunctionNumber;
  662. HalpPciDebuggingDevice[i].u.bits.DeviceNumber =
  663. slot.u.bits.DeviceNumber;
  664. HalpPciDebuggingDevice[i].u.bits.BusNumber = PciDevice->Bus;
  665. HalpPciDebuggingDevice[i].u.bits.Reserved1 = TRUE;
  666. break;
  667. }
  668. }
  669. }
  670. //
  671. // Check to see if the caller wants any memory.
  672. //
  673. if (PciDevice->Memory.Length != 0) {
  674. if (!LoaderBlock) {
  675. return STATUS_INVALID_PARAMETER_1;
  676. }
  677. if (PciDevice->Memory.MaxEnd.QuadPart == 0) {
  678. PciDevice->Memory.MaxEnd.QuadPart = -1;
  679. }
  680. maxPhys = PciDevice->Memory.MaxEnd.HighPart ? 0xffffffff : PciDevice->Memory.MaxEnd.LowPart;
  681. maxPhys -= PciDevice->Memory.Length;
  682. //
  683. // The HAL APIs will always return page-aligned
  684. // memory. So ignore Aligned for now.
  685. //
  686. maxPhys = (ULONG)(ULONG_PTR)PAGE_ALIGN(maxPhys);
  687. maxPhys += ADDRESS_AND_SIZE_TO_SPAN_PAGES(maxPhys, PciDevice->Memory.Length);
  688. PciDevice->Memory.Start.HighPart = 0;
  689. PciDevice->Memory.Start.LowPart = (ULONG)(ULONG_PTR)
  690. HalpAllocPhysicalMemory(LoaderBlock,
  691. maxPhys,
  692. ADDRESS_AND_SIZE_TO_SPAN_PAGES(maxPhys, PciDevice->Memory.Length),
  693. FALSE);
  694. if (!PciDevice->Memory.Start.LowPart) {
  695. return STATUS_INSUFFICIENT_RESOURCES;
  696. }
  697. PciDevice->Memory.VirtualAddress =
  698. HalpMapPhysicalMemory(PciDevice->Memory.Start,
  699. ADDRESS_AND_SIZE_TO_SPAN_PAGES(maxPhys, PciDevice->Memory.Length),
  700. MmNonCached);
  701. }
  702. return status;
  703. }
  704. VOID
  705. HalpFindFreeResourceLimits(
  706. IN ULONG Bus,
  707. IN OUT ULONG *MinIo,
  708. IN OUT ULONG *MaxIo,
  709. IN OUT ULONG *MinMem,
  710. IN OUT ULONG *MaxMem,
  711. IN OUT ULONG *MinBus,
  712. IN OUT ULONG *MaxBus
  713. )
  714. {
  715. UCHAR buffer[PCI_COMMON_HDR_LENGTH];
  716. PPCI_COMMON_CONFIG pciData;
  717. UCHAR bus, dev, func, bytesRead;
  718. PCI_SLOT_NUMBER pciSlot, targetSlot;
  719. ULONG newMinMem, newMaxMem;
  720. ULONG newMinIo, newMaxIo;
  721. ULONG newMinBus, newMaxBus;
  722. UCHAR barNo;
  723. pciData = (PPCI_COMMON_CONFIG)buffer;
  724. pciSlot.u.AsULONG = 0;
  725. newMinMem = *MinMem;
  726. newMaxMem = *MaxMem;
  727. newMinIo = *MinIo;
  728. newMaxIo = *MaxIo;
  729. newMinBus = *MinBus;
  730. newMaxBus = *MaxBus;
  731. for (dev = 0; dev < PCI_MAX_DEVICES; dev++) {
  732. for (func = 0; func < PCI_MAX_FUNCTION; func++) {
  733. pciSlot.u.bits.DeviceNumber = dev;
  734. pciSlot.u.bits.FunctionNumber = func;
  735. bytesRead = (UCHAR)HalpPhase0GetPciDataByOffset(Bus,
  736. pciSlot.u.AsULONG,
  737. pciData,
  738. 0,
  739. PCI_COMMON_HDR_LENGTH);
  740. if (bytesRead == 0) continue;
  741. if (pciData->VendorID != PCI_INVALID_VENDORID) {
  742. switch (PCI_CONFIGURATION_TYPE(pciData)) {
  743. case PCI_DEVICE_TYPE:
  744. //
  745. // While we scan across the bus, keep track
  746. // of the minimum decoder values that we've seen.
  747. // This will be used if we have to configure the
  748. // device. This relies on the fact that most BIOSes
  749. // assign addresses from the top down.
  750. //
  751. for (barNo = 0; barNo < PCI_TYPE0_ADDRESSES; barNo++) {
  752. if (pciData->u.type0.BaseAddresses[barNo] &
  753. PCI_ADDRESS_IO_SPACE) {
  754. if (pciData->u.type0.BaseAddresses[barNo] &
  755. PCI_ADDRESS_IO_ADDRESS_MASK) {
  756. //
  757. // This BAR is implemented
  758. //
  759. if ((pciData->u.type0.BaseAddresses[barNo] &
  760. PCI_ADDRESS_IO_ADDRESS_MASK) <
  761. ((newMaxIo + newMinIo) / 2)) {
  762. //
  763. // This BAR is at the bottom of the range.
  764. // Bump up the min.
  765. //
  766. newMinIo = (USHORT)MAX (newMinIo,
  767. (pciData->u.type0.BaseAddresses[barNo] &
  768. PCI_ADDRESS_IO_ADDRESS_MASK) + 0x100);
  769. } else {
  770. //
  771. // This BAR is not at the bottom of the range.
  772. // Bump down the max.
  773. //
  774. newMaxIo = (USHORT)MIN (newMaxIo,
  775. pciData->u.type0.BaseAddresses[barNo] &
  776. PCI_ADDRESS_IO_ADDRESS_MASK);
  777. }
  778. }
  779. } else {
  780. if (pciData->u.type0.BaseAddresses[barNo] &
  781. PCI_ADDRESS_MEMORY_ADDRESS_MASK) {
  782. //
  783. // The BAR is populated.
  784. //
  785. if ((pciData->u.type0.BaseAddresses[barNo] &
  786. PCI_ADDRESS_MEMORY_ADDRESS_MASK) <
  787. ((newMaxMem / 2) + (newMinMem / 2))) {
  788. //
  789. // This BAR is at the bottom of the range.
  790. // Bump up the min.
  791. //
  792. newMinMem = MAX (newMinMem,
  793. (pciData->u.type0.BaseAddresses[barNo] &
  794. PCI_ADDRESS_MEMORY_ADDRESS_MASK) + 0x10000);
  795. } else {
  796. //
  797. // This BAR is not at the bottom of the range.
  798. // Bump down the max.
  799. //
  800. newMaxMem = MIN (newMaxMem,
  801. (pciData->u.type0.BaseAddresses[barNo] &
  802. PCI_ADDRESS_MEMORY_ADDRESS_MASK));
  803. }
  804. }
  805. }
  806. }
  807. break;
  808. case PCI_CARDBUS_BRIDGE_TYPE:
  809. case PCI_BRIDGE_TYPE:
  810. {
  811. ULONG bridgeMemMin = 0, bridgeMemMax = 0;
  812. USHORT bridgeIoMin, bridgeIoMax;
  813. if ((pciData->u.type1.SecondaryBus != 0) &&
  814. (pciData->u.type1.SubordinateBus !=0) &&
  815. (pciData->Command & PCI_ENABLE_MEMORY_SPACE) &&
  816. (pciData->Command & PCI_ENABLE_IO_SPACE)) {
  817. bridgeMemMin = PciBridgeMemory2Base(pciData->u.type1.MemoryBase);
  818. bridgeMemMax = PciBridgeMemory2Limit(pciData->u.type1.MemoryLimit);
  819. bridgeIoMin = (USHORT)PciBridgeIO2Base(pciData->u.type1.IOBase, 0);
  820. bridgeIoMax = (USHORT)PciBridgeIO2Limit(pciData->u.type1.IOLimit, 0);
  821. //
  822. // Keep track of address space allocation.
  823. //
  824. if (bridgeIoMin > ((newMaxIo + newMinIo) / 2)) {
  825. newMaxIo = MIN(newMaxIo, bridgeIoMin);
  826. }
  827. if (bridgeIoMax < ((newMaxIo + newMinIo) / 2)) {
  828. newMinIo = MAX(newMinIo, bridgeIoMax) + 1;
  829. }
  830. if (bridgeMemMin > ((newMaxMem + newMinMem) / 2)) {
  831. newMaxMem = MIN(newMaxMem, bridgeMemMin);
  832. }
  833. if (bridgeMemMax < ((newMaxMem + newMinMem) / 2)) {
  834. newMinMem = MAX(newMinMem, bridgeMemMax) + 1;
  835. }
  836. //
  837. // Keep track of bus numbers.
  838. //
  839. if (pciData->u.type1.PrimaryBus > ((newMaxBus + newMinBus) / 2)) {
  840. newMaxBus = MIN(newMaxBus, pciData->u.type1.PrimaryBus);
  841. }
  842. if (pciData->u.type1.SubordinateBus < ((newMaxBus + newMinBus) / 2)) {
  843. newMinBus = MAX(newMinBus, pciData->u.type1.SubordinateBus) + 1;
  844. }
  845. }
  846. break;
  847. default:
  848. break;
  849. }
  850. }
  851. if (!PCI_MULTIFUNCTION_DEVICE(pciData) &&
  852. (func == 0)) {
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. *MinMem = newMinMem;
  859. *MaxMem = newMaxMem;
  860. *MinIo = newMinIo;
  861. *MaxIo = newMaxIo;
  862. *MinBus = newMinBus;
  863. *MaxBus = newMaxBus;
  864. }
  865. NTSTATUS
  866. HalpSetupUnconfiguredDebuggingDevice(
  867. IN ULONG Bus,
  868. IN ULONG Slot,
  869. IN ULONG IoMin,
  870. IN ULONG IoMax,
  871. IN ULONG MemMin,
  872. IN ULONG MemMax,
  873. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
  874. )
  875. {
  876. UCHAR buffer[PCI_COMMON_HDR_LENGTH];
  877. PPCI_COMMON_CONFIG pciData;
  878. ULONG barLength, bytesRead;
  879. ULONG barContents = 0;
  880. PHYSICAL_ADDRESS physicalAddress;
  881. PCI_SLOT_NUMBER pciSlot;
  882. UCHAR barNo;
  883. pciSlot.u.AsULONG = Slot;
  884. pciData = (PPCI_COMMON_CONFIG)buffer;
  885. bytesRead = (UCHAR)HalpPhase0GetPciDataByOffset(Bus,
  886. pciSlot.u.AsULONG,
  887. pciData,
  888. 0,
  889. PCI_COMMON_HDR_LENGTH);
  890. ASSERT(bytesRead != 0);
  891. PciDevice->Bus = Bus;
  892. PciDevice->Slot = pciSlot.u.AsULONG;
  893. PciDevice->VendorID = pciData->VendorID;
  894. PciDevice->DeviceID = pciData->DeviceID;
  895. PciDevice->BaseClass = pciData->BaseClass;
  896. PciDevice->SubClass = pciData->SubClass;
  897. //DbgPrint("Configuring device between %x - %x\n",
  898. // MemMin, MemMax);
  899. //
  900. // Cycle through the BARs, turning them on if necessary,
  901. // and mapping them.
  902. //
  903. for (barNo = 0; barNo < PCI_TYPE0_ADDRESSES; barNo++) {
  904. barContents = 0xffffffff;
  905. PciDevice->BaseAddress[barNo].Valid = FALSE;
  906. HalpPhase0SetPciDataByOffset(Bus,
  907. pciSlot.u.AsULONG,
  908. &barContents,
  909. 0x10 + (4 * barNo),
  910. 4);
  911. HalpPhase0GetPciDataByOffset(Bus,
  912. pciSlot.u.AsULONG,
  913. &barContents,
  914. 0x10 + (4 * barNo),
  915. 4);
  916. if (pciData->u.type0.BaseAddresses[barNo] &
  917. PCI_ADDRESS_IO_SPACE) {
  918. //
  919. // This is an I/O BAR.
  920. //
  921. if (!(pciData->u.type0.BaseAddresses[barNo] &
  922. PCI_ADDRESS_IO_ADDRESS_MASK)) {
  923. //
  924. // And it's empty.
  925. //
  926. barLength = (((USHORT)barContents & PCI_ADDRESS_IO_ADDRESS_MASK) - 1) ^
  927. 0xffff;
  928. //
  929. // Try to fit this I/O window half-way between the min and the max.
  930. //
  931. if ((ULONG)(IoMax - IoMin) >= (barLength * 3)) {
  932. //
  933. // There is plenty of room, make a safe guess. Try
  934. // to put it half-way between the upper and lower
  935. // bounds, rounding up to the next natural alignment.
  936. //
  937. pciData->u.type0.BaseAddresses[barNo] =
  938. (((IoMax + IoMin) / 2) + barLength) & (barLength -1);
  939. } else if (barLength >= (IoMax -
  940. ((IoMin & (barLength -1)) ?
  941. ((IoMin + barLength) & (barLength -1)) :
  942. IoMin))) {
  943. //
  944. // Space is tight, make a not-so-safe guess. Try
  945. // to put it at the bottom of the range, rounded
  946. // up the the next natural alignment.
  947. //
  948. pciData->u.type0.BaseAddresses[barNo] =
  949. ((IoMin & (barLength -1)) ?
  950. ((IoMin + barLength) & (barLength -1)) :
  951. IoMin);
  952. }
  953. IoMin = (USHORT)pciData->u.type0.BaseAddresses[barNo];
  954. }
  955. pciData->Command |= PCI_ENABLE_IO_SPACE;
  956. PciDevice->BaseAddress[barNo].Type = CmResourceTypePort;
  957. PciDevice->BaseAddress[barNo].Valid = TRUE;
  958. PciDevice->BaseAddress[barNo].TranslatedAddress =
  959. (PUCHAR)(ULONG_PTR)(pciData->u.type0.BaseAddresses[barNo] &
  960. PCI_ADDRESS_IO_ADDRESS_MASK);
  961. PciDevice->BaseAddress[barNo].Length = barLength;
  962. } else {
  963. //
  964. // This is a memory BAR.
  965. //
  966. barLength = ((barContents & PCI_ADDRESS_MEMORY_ADDRESS_MASK) - 1) ^
  967. 0xffffffff;
  968. if (!(pciData->u.type0.BaseAddresses[barNo] &
  969. PCI_ADDRESS_MEMORY_ADDRESS_MASK)) {
  970. //
  971. // And it's empty.
  972. //
  973. if (barLength == 0) continue;
  974. //
  975. // Try to fit this memory window half-way between the min and the max.
  976. //
  977. if ((ULONG)(MemMax - MemMin) >= (barLength * 3)) {
  978. //
  979. // There is plenty of room, make a safe guess. Try
  980. // to put it half-way between the upper and lower
  981. // bounds, rounding up to the next natural alignment.
  982. //
  983. pciData->u.type0.BaseAddresses[barNo] =
  984. (ULONG)(((MemMax + MemMin) / 2)
  985. + barLength) & ~(barLength -1);
  986. } else if (barLength >= (ULONG)(MemMax -
  987. ((MemMin & ~(barLength -1)) ?
  988. ((MemMin + barLength) & ~(barLength-1)) :
  989. MemMin))) {
  990. //
  991. // Space is tight, make a not-so-safe guess. Try
  992. // to put it at the bottom of the range, rounded
  993. // up the the next natural alignment.
  994. //
  995. pciData->u.type0.BaseAddresses[barNo] =
  996. (ULONG)((MemMin & ~(barLength -1)) ?
  997. ((MemMin + barLength) & ~(barLength -1)) :
  998. MemMin);
  999. }
  1000. MemMin = pciData->u.type0.BaseAddresses[barNo] &
  1001. PCI_ADDRESS_MEMORY_ADDRESS_MASK;
  1002. }
  1003. pciData->Command |= PCI_ENABLE_MEMORY_SPACE;
  1004. physicalAddress.HighPart = 0;
  1005. physicalAddress.LowPart = pciData->u.type0.BaseAddresses[barNo]
  1006. & PCI_ADDRESS_MEMORY_ADDRESS_MASK;
  1007. PciDevice->BaseAddress[barNo].Type = CmResourceTypeMemory;
  1008. PciDevice->BaseAddress[barNo].Valid = TRUE;
  1009. PciDevice->BaseAddress[barNo].TranslatedAddress =
  1010. HalpMapPhysicalMemory(physicalAddress,
  1011. ADDRESS_AND_SIZE_TO_SPAN_PAGES(physicalAddress.LowPart, barLength),
  1012. MmNonCached);
  1013. PciDevice->BaseAddress[barNo].Length = barLength;
  1014. }
  1015. }
  1016. pciData->Command |= PCI_ENABLE_BUS_MASTER;
  1017. //
  1018. // Write back any changes we made.
  1019. //
  1020. HalpPhase0SetPciDataByOffset(Bus,
  1021. pciSlot.u.AsULONG,
  1022. pciData,
  1023. 0,
  1024. 0x40);
  1025. return STATUS_SUCCESS;
  1026. }
  1027. NTSTATUS
  1028. HalpSearchForPciDebuggingDevice(
  1029. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice,
  1030. IN ULONG StartBusNumber,
  1031. IN ULONG EndBusNumber,
  1032. IN ULONG MinMem,
  1033. IN ULONG MaxMem,
  1034. IN USHORT MinIo,
  1035. IN USHORT MaxIo,
  1036. IN BOOLEAN ConfigureBridges
  1037. )
  1038. /*++
  1039. Routine Description:
  1040. This routine is a helper function for
  1041. HalpSetupPciDeviceForDebugging.
  1042. Arguments:
  1043. PciDevice - Structure indicating the device
  1044. Return Value:
  1045. STATUS_SUCCESS if the device is configured and usable.
  1046. STATUS_NO_MORE_MATCHES if no device matched the criteria.
  1047. STATUS_UNSUCCESSFUL if the routine fails for other reasons.
  1048. --*/
  1049. #define TARGET_DEVICE_NOT_FOUND 0x10000
  1050. {
  1051. NTSTATUS status;
  1052. UCHAR buffer[PCI_COMMON_HDR_LENGTH];
  1053. PPCI_COMMON_CONFIG pciData;
  1054. UCHAR bus, dev, func, bytesRead;
  1055. PCI_SLOT_NUMBER pciSlot, targetSlot;
  1056. ULONG newMinMem, newMaxMem;
  1057. ULONG newMinIo, newMaxIo;
  1058. ULONG newMinBus, newMaxBus;
  1059. UCHAR barNo;
  1060. BOOLEAN unconfigureBridge = FALSE;
  1061. pciData = (PPCI_COMMON_CONFIG)buffer;
  1062. pciSlot.u.AsULONG = 0;
  1063. newMinMem = MinMem;
  1064. newMaxMem = MaxMem;
  1065. newMinIo = MinIo;
  1066. newMaxIo = MaxIo;
  1067. newMinBus = StartBusNumber;
  1068. newMaxBus = EndBusNumber;
  1069. bus = (UCHAR)StartBusNumber;
  1070. //DbgPrint("HalpSearchForPciDebuggingDevice:\n"
  1071. // "\tMem: %x-%x\n"
  1072. // "\tI/O: %x-%x\n"
  1073. // "\tBus: %x-%x\n"
  1074. // "\t%s Configuring Bridges\n",
  1075. // MinMem, MaxMem,
  1076. // MinIo, MaxIo,
  1077. // StartBusNumber, EndBusNumber,
  1078. // ConfigureBridges ? "" : "Not");
  1079. //
  1080. // This bit stays set to 1 until we find the device.
  1081. //
  1082. targetSlot.u.bits.Reserved = TARGET_DEVICE_NOT_FOUND;
  1083. while (TRUE) {
  1084. UCHAR nextBus;
  1085. nextBus = bus + 1;
  1086. HalpFindFreeResourceLimits(bus,
  1087. &newMinIo,
  1088. &newMaxIo,
  1089. &newMinMem,
  1090. &newMaxMem,
  1091. &newMinBus,
  1092. &newMaxBus
  1093. );
  1094. for (dev = 0; dev < PCI_MAX_DEVICES; dev++) {
  1095. for (func = 0; func < PCI_MAX_FUNCTION; func++) {
  1096. pciSlot.u.bits.DeviceNumber = dev;
  1097. pciSlot.u.bits.FunctionNumber = func;
  1098. bytesRead = (UCHAR)HalpPhase0GetPciDataByOffset(bus,
  1099. pciSlot.u.AsULONG,
  1100. pciData,
  1101. 0,
  1102. PCI_COMMON_HDR_LENGTH);
  1103. if (bytesRead == 0) continue;
  1104. if (pciData->VendorID != PCI_INVALID_VENDORID) {
  1105. //DbgPrint("%04x:%04x - %x/%x/%x - \tSlot: %x\n",
  1106. // pciData->VendorID,
  1107. // pciData->DeviceID,
  1108. // pciData->BaseClass,
  1109. // pciData->SubClass,
  1110. // pciData->ProgIf,
  1111. // pciSlot.u.AsULONG);
  1112. switch (PCI_CONFIGURATION_TYPE(pciData)) {
  1113. case PCI_DEVICE_TYPE:
  1114. //
  1115. // Match first on Bus/Dev/Func
  1116. //
  1117. if ((PciDevice->Bus == bus) &&
  1118. (PciDevice->Slot == pciSlot.u.AsULONG)) {
  1119. //DbgPrint("\n\nMatched on Bus/Slot\n\n");
  1120. return HalpSetupUnconfiguredDebuggingDevice(
  1121. bus,
  1122. pciSlot.u.AsULONG,
  1123. newMinIo,
  1124. newMaxIo,
  1125. newMinMem,
  1126. newMaxMem,
  1127. PciDevice
  1128. );
  1129. }
  1130. if ((PciDevice->Bus == MAXULONG) &&
  1131. (PciDevice->Slot == MAXULONG)) {
  1132. //
  1133. // Bus and Slot weren't specified. Match
  1134. // on VID/DID.
  1135. //
  1136. if ((pciData->VendorID == PciDevice->VendorID) &&
  1137. (pciData->DeviceID == PciDevice->DeviceID)) {
  1138. //DbgPrint("\n\nMatched on Vend/Dev\n\n");
  1139. return HalpSetupUnconfiguredDebuggingDevice(
  1140. bus,
  1141. pciSlot.u.AsULONG,
  1142. newMinIo,
  1143. newMaxIo,
  1144. newMinMem,
  1145. newMaxMem,
  1146. PciDevice
  1147. );
  1148. }
  1149. if ((PciDevice->VendorID == MAXUSHORT) &&
  1150. (PciDevice->DeviceID == MAXUSHORT)) {
  1151. //
  1152. // VID/DID weren't specified. Match
  1153. // on class codes.
  1154. //
  1155. if ((pciData->BaseClass == PciDevice->BaseClass) &&
  1156. (pciData->SubClass == PciDevice->SubClass)) {
  1157. //DbgPrint("\n\nMatched on Base/Sub\n\n");
  1158. //
  1159. // Further match on Programming Interface,
  1160. // if specified.
  1161. //
  1162. if ((PciDevice->ProgIf != MAXUCHAR) &&
  1163. (PciDevice->ProgIf != pciData->ProgIf)) {
  1164. break;
  1165. }
  1166. //DbgPrint("\n\nMatched on programming interface\n\n");
  1167. return HalpSetupUnconfiguredDebuggingDevice(
  1168. bus,
  1169. pciSlot.u.AsULONG,
  1170. newMinIo,
  1171. newMaxIo,
  1172. newMinMem,
  1173. newMaxMem,
  1174. PciDevice
  1175. );
  1176. }
  1177. }
  1178. }
  1179. break;
  1180. case PCI_CARDBUS_BRIDGE_TYPE:
  1181. //
  1182. // Cardbus bridge stuff here
  1183. //
  1184. case PCI_BRIDGE_TYPE:
  1185. {
  1186. ULONG bridgeMemMin = 0, bridgeMemMax = 0;
  1187. USHORT bridgeIoMin, bridgeIoMax;
  1188. //DbgPrint("Found a PCI to PCI bridge\n");
  1189. if (!((pciData->u.type1.SecondaryBus != 0) &&
  1190. (pciData->u.type1.SubordinateBus !=0) &&
  1191. (pciData->Command & PCI_ENABLE_MEMORY_SPACE) &&
  1192. (pciData->Command & PCI_ENABLE_IO_SPACE))) {
  1193. //
  1194. // The bridge is unconfigured.
  1195. //
  1196. if (ConfigureBridges){
  1197. //
  1198. // We should configure it now.
  1199. //
  1200. status = HalpConfigurePciBridge(
  1201. PciDevice,
  1202. bus,
  1203. pciSlot.u.AsULONG,
  1204. newMinIo,
  1205. newMaxIo,
  1206. newMinMem,
  1207. newMaxMem,
  1208. MAX((UCHAR)newMinBus, (bus + 1)),
  1209. newMaxBus,
  1210. pciData
  1211. );
  1212. if (!NT_SUCCESS(status)) {
  1213. break;
  1214. }
  1215. unconfigureBridge = TRUE;
  1216. } else {
  1217. //
  1218. // We aren't configuring bridges
  1219. // on this pass.
  1220. //
  1221. break;
  1222. }
  1223. }
  1224. bridgeMemMin = PciBridgeMemory2Base(pciData->u.type1.MemoryBase);
  1225. bridgeMemMax = PciBridgeMemory2Limit(pciData->u.type1.MemoryLimit);
  1226. bridgeIoMin = (USHORT)PciBridgeIO2Base(pciData->u.type1.IOBase, 0);
  1227. bridgeIoMax = (USHORT)PciBridgeIO2Limit(pciData->u.type1.IOLimit, 0);
  1228. //DbgPrint("Configured: I/O %x-%x Mem %x-%x\n",
  1229. // bridgeIoMin, bridgeIoMax,
  1230. // bridgeMemMin, bridgeMemMax);
  1231. //
  1232. // Recurse.
  1233. //
  1234. status = HalpSearchForPciDebuggingDevice(
  1235. PciDevice,
  1236. (ULONG)pciData->u.type1.SecondaryBus,
  1237. (ULONG)pciData->u.type1.SubordinateBus,
  1238. bridgeMemMin,
  1239. bridgeMemMax,
  1240. bridgeIoMin,
  1241. bridgeIoMax,
  1242. ConfigureBridges);
  1243. if (NT_SUCCESS(status)) {
  1244. return status;
  1245. }
  1246. if (!unconfigureBridge) {
  1247. //
  1248. // Bump up the bus number so that we don't
  1249. // scan down the busses we just recursed into.
  1250. //
  1251. nextBus = pciData->u.type1.SubordinateBus + 1;
  1252. } else {
  1253. HalpUnconfigurePciBridge(bus,
  1254. pciSlot.u.AsULONG);
  1255. }
  1256. }
  1257. break;
  1258. default:
  1259. break;
  1260. }
  1261. }
  1262. if (!PCI_MULTIFUNCTION_DEVICE(pciData) &&
  1263. (func == 0)) {
  1264. break;
  1265. }
  1266. }
  1267. }
  1268. if (nextBus >= EndBusNumber) {
  1269. break;
  1270. }
  1271. bus = nextBus;
  1272. }
  1273. return STATUS_NOT_FOUND;
  1274. }
  1275. NTSTATUS
  1276. HalpReleasePciDeviceForDebugging(
  1277. IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
  1278. )
  1279. /*++
  1280. Routine Description:
  1281. This routine de-allocates any resources acquired in
  1282. HalpSetupPciDeviceForDebugging.
  1283. Arguments:
  1284. PciDevice - Structure indicating the device
  1285. Return Value:
  1286. --*/
  1287. {
  1288. ULONG i;
  1289. for (i = 0; i < PCI_TYPE0_ADDRESSES; i++) {
  1290. if (PciDevice->BaseAddress[i].Valid &&
  1291. PciDevice->BaseAddress[i].Type == CmResourceTypeMemory) {
  1292. PciDevice->BaseAddress[i].Valid = FALSE;
  1293. HalpUnmapVirtualAddress(PciDevice->BaseAddress[i].TranslatedAddress,
  1294. ADDRESS_AND_SIZE_TO_SPAN_PAGES(
  1295. PciDevice->BaseAddress[i].TranslatedAddress,
  1296. PciDevice->BaseAddress[i].Length));
  1297. }
  1298. }
  1299. return STATUS_SUCCESS;
  1300. }
  1301. VOID
  1302. HalpRegisterKdSupportFunctions(
  1303. IN PLOADER_PARAMETER_BLOCK LoaderBlock
  1304. )
  1305. /*++
  1306. Routine Description:
  1307. This routine fills in the HalPrivateDispatchTable
  1308. with the functions needed for debugging through
  1309. PCI devices.
  1310. Arguments:
  1311. LoaderBlock - The Loader Block
  1312. Return Value:
  1313. --*/
  1314. {
  1315. KdSetupPciDeviceForDebugging = HalpSetupPciDeviceForDebugging;
  1316. KdReleasePciDeviceForDebugging = HalpReleasePciDeviceForDebugging;
  1317. KdGetAcpiTablePhase0 = HalpGetAcpiTablePhase0;
  1318. KdMapPhysicalMemory64 = HalpMapPhysicalMemory64;
  1319. KdCheckPowerButton = HalpCheckPowerButton;
  1320. }
  1321. VOID
  1322. HalpRegisterPciDebuggingDeviceInfo(
  1323. VOID
  1324. )
  1325. {
  1326. OBJECT_ATTRIBUTES ObjectAttributes;
  1327. UNICODE_STRING UnicodeString;
  1328. HANDLE BaseHandle = NULL;
  1329. HANDLE Handle = NULL;
  1330. ULONG disposition;
  1331. ULONG bus;
  1332. UCHAR i;
  1333. PCI_SLOT_NUMBER slot;
  1334. NTSTATUS status;
  1335. BOOLEAN debuggerFound = FALSE;
  1336. PAGED_CODE();
  1337. for (i = 0;
  1338. i < MAX_DEBUGGING_DEVICES_SUPPORTED;
  1339. i++) {
  1340. if (HalpPciDebuggingDevice[i].u.bits.Reserved1 == TRUE) {
  1341. //
  1342. // Must be using a PCI device for a debugger.
  1343. //
  1344. debuggerFound = TRUE;
  1345. }
  1346. }
  1347. if (!debuggerFound) {
  1348. return;
  1349. }
  1350. //
  1351. // Open PCI service key.
  1352. //
  1353. RtlInitUnicodeString (&UnicodeString,
  1354. L"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\SERVICES\\PCI");
  1355. InitializeObjectAttributes(&ObjectAttributes,
  1356. &UnicodeString,
  1357. OBJ_CASE_INSENSITIVE,
  1358. NULL,
  1359. (PSECURITY_DESCRIPTOR) NULL);
  1360. status = ZwOpenKey (&BaseHandle,
  1361. KEY_READ,
  1362. &ObjectAttributes);
  1363. if (!NT_SUCCESS(status)) {
  1364. return;
  1365. }
  1366. // Get the right key
  1367. RtlInitUnicodeString (&UnicodeString,
  1368. L"Debug");
  1369. InitializeObjectAttributes(&ObjectAttributes,
  1370. &UnicodeString,
  1371. OBJ_CASE_INSENSITIVE,
  1372. BaseHandle,
  1373. (PSECURITY_DESCRIPTOR) NULL);
  1374. status = ZwCreateKey (&Handle,
  1375. KEY_READ,
  1376. &ObjectAttributes,
  1377. 0,
  1378. (PUNICODE_STRING) NULL,
  1379. REG_OPTION_VOLATILE,
  1380. &disposition);
  1381. ZwClose(BaseHandle);
  1382. BaseHandle = Handle;
  1383. ASSERT(disposition == REG_CREATED_NEW_KEY);
  1384. if (!NT_SUCCESS(status)) {
  1385. return;
  1386. }
  1387. for (i = 0;
  1388. i < MAX_DEBUGGING_DEVICES_SUPPORTED;
  1389. i++) {
  1390. if (HalpPciDebuggingDevice[i].u.bits.Reserved1 == TRUE) {
  1391. //
  1392. // This entry is populated. Create a key for it.
  1393. //
  1394. RtlInitUnicodeString (&UnicodeString,
  1395. L"0");
  1396. (*(PCHAR)&(UnicodeString.Buffer[0])) += i;
  1397. InitializeObjectAttributes(&ObjectAttributes,
  1398. &UnicodeString,
  1399. OBJ_CASE_INSENSITIVE,
  1400. BaseHandle,
  1401. (PSECURITY_DESCRIPTOR) NULL);
  1402. status = ZwCreateKey (&Handle,
  1403. KEY_READ,
  1404. &ObjectAttributes,
  1405. 0,
  1406. (PUNICODE_STRING) NULL,
  1407. REG_OPTION_VOLATILE,
  1408. &disposition);
  1409. ASSERT(disposition == REG_CREATED_NEW_KEY);
  1410. //
  1411. // Fill in the values below this key.
  1412. //
  1413. bus = HalpPciDebuggingDevice[i].u.bits.BusNumber;
  1414. RtlInitUnicodeString (&UnicodeString,
  1415. L"Bus");
  1416. status = ZwSetValueKey (Handle,
  1417. &UnicodeString,
  1418. 0,
  1419. REG_DWORD,
  1420. &bus,
  1421. sizeof(ULONG));
  1422. //ASSERT(NT_SUCCESS(status));
  1423. slot.u.AsULONG = 0;
  1424. slot.u.bits.FunctionNumber = HalpPciDebuggingDevice[i].u.bits.FunctionNumber;
  1425. slot.u.bits.DeviceNumber = HalpPciDebuggingDevice[i].u.bits.DeviceNumber;
  1426. RtlInitUnicodeString (&UnicodeString,
  1427. L"Slot");
  1428. status = ZwSetValueKey (Handle,
  1429. &UnicodeString,
  1430. 0,
  1431. REG_DWORD,
  1432. &slot.u.AsULONG,
  1433. sizeof(ULONG));
  1434. //ASSERT(NT_SUCCESS(status));
  1435. ZwClose(Handle);
  1436. }
  1437. }
  1438. ZwClose(BaseHandle);
  1439. return;
  1440. }
  1441. NTSTATUS
  1442. HalpConfigurePciBridge(
  1443. IN PDEBUG_DEVICE_DESCRIPTOR PciDevice,
  1444. IN ULONG Bus,
  1445. IN ULONG Slot,
  1446. IN ULONG IoMin,
  1447. IN ULONG IoMax,
  1448. IN ULONG MemMin,
  1449. IN ULONG MemMax,
  1450. IN ULONG BusMin,
  1451. IN ULONG BusMax,
  1452. IN OUT PPCI_COMMON_CONFIG PciData
  1453. )
  1454. {
  1455. USHORT memUnits = 0;
  1456. ULONG memSize;
  1457. PciData->u.type1.PrimaryBus = (UCHAR)Bus;
  1458. PciData->u.type1.SecondaryBus = (UCHAR)BusMin;
  1459. PciData->u.type1.SubordinateBus = (UCHAR)(MIN(BusMax, (BusMin + 2)));
  1460. PciData->Command &= ~PCI_ENABLE_BUS_MASTER;
  1461. //DbgPrint("HalpConfigurePciBridge: P: %x S: %x S: %x\n"
  1462. // "\tI/O %x-%x Mem %x-%x Bus %x-%x\n",
  1463. // PciData->u.type1.PrimaryBus,
  1464. // PciData->u.type1.SecondaryBus,
  1465. // PciData->u.type1.SubordinateBus,
  1466. // IoMin, IoMax,
  1467. // MemMin, MemMax,
  1468. // BusMin, BusMax);
  1469. //
  1470. // Only enable I/O on the bridge if we are looking for
  1471. // something besides a 1394 controller.
  1472. //
  1473. if (!((PciDevice->BaseClass == PCI_CLASS_SERIAL_BUS_CTLR) &&
  1474. (PciDevice->SubClass == PCI_SUBCLASS_SB_IEEE1394))) {
  1475. if (((IoMax & 0xf000) - (IoMin & 0xf000)) >= 0X1000) {
  1476. //
  1477. // There is enough I/O space here to enable
  1478. // an I/O window.
  1479. //
  1480. PciData->u.type1.IOBase =
  1481. (UCHAR)((IoMax & 0xf000) >> 12) - 1;
  1482. PciData->u.type1.IOLimit = PciData->u.type1.IOBase;
  1483. PciData->Command |= PCI_ENABLE_IO_SPACE;
  1484. PciData->Command |= PCI_ENABLE_BUS_MASTER;
  1485. }
  1486. }
  1487. //
  1488. // Enable a memory window if possible.
  1489. //
  1490. memSize = ((MemMax + 1) & 0xfff00000) - (MemMin & 0xfff00000);
  1491. if (memSize >= 0x100000) {
  1492. memUnits = 1;
  1493. }
  1494. if (memSize >= 0x400000) {
  1495. memUnits = 4;
  1496. }
  1497. if (memUnits > 0) {
  1498. //
  1499. // There is enough space.
  1500. //
  1501. PciData->u.type1.MemoryBase =
  1502. (USHORT)((MemMax & 0xfff00000) >> 16) - (memUnits << 4);
  1503. PciData->u.type1.MemoryLimit = PciData->u.type1.MemoryBase + ((memUnits- 1) << 4);
  1504. PciData->Command |= PCI_ENABLE_MEMORY_SPACE;
  1505. PciData->Command |= PCI_ENABLE_BUS_MASTER;
  1506. }
  1507. if (PciData->Command & PCI_ENABLE_BUS_MASTER) {
  1508. HalpPhase0SetPciDataByOffset(Bus,
  1509. Slot,
  1510. PciData,
  1511. 0,
  1512. 0x24);
  1513. return STATUS_SUCCESS;
  1514. } else {
  1515. return STATUS_UNSUCCESSFUL;
  1516. }
  1517. }
  1518. VOID
  1519. HalpUnconfigurePciBridge(
  1520. IN ULONG Bus,
  1521. IN ULONG Slot
  1522. )
  1523. {
  1524. UCHAR buffer[0x20] = {0};
  1525. //
  1526. // Zero the command register.
  1527. //
  1528. HalpPhase0SetPciDataByOffset(Bus,
  1529. Slot,
  1530. buffer,
  1531. FIELD_OFFSET (PCI_COMMON_CONFIG, Command),
  1532. 2);
  1533. //
  1534. // Zero the address space and bus number registers.
  1535. //
  1536. HalpPhase0SetPciDataByOffset(Bus,
  1537. Slot,
  1538. buffer,
  1539. FIELD_OFFSET (PCI_COMMON_CONFIG, u),
  1540. 0x20);
  1541. }