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.

1288 lines
35 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. mpsysbus.c
  5. Abstract:
  6. Author:
  7. Environment:
  8. Revision History:
  9. --*/
  10. #include "halp.h"
  11. #include "pci.h"
  12. #include "apic.inc"
  13. #include "pcmp_nt.inc"
  14. ULONG HalpDefaultInterruptAffinity = 0;
  15. #ifndef ACPI_HAL
  16. ULONG
  17. HalpGetEisaInterruptVector(
  18. IN PBUS_HANDLER BusHandler,
  19. IN PBUS_HANDLER RootHandler,
  20. IN ULONG BusInterruptLevel,
  21. IN ULONG BusInterruptVector,
  22. OUT PKIRQL Irql,
  23. OUT PKAFFINITY Affinity
  24. );
  25. #else
  26. #undef HalpGetEisaInterruptVector
  27. #define HalpGetEisaInterruptVector HalpGetSystemInterruptVector
  28. extern BUS_HANDLER HalpFakePciBusHandler;
  29. #endif
  30. extern UCHAR HalpVectorToIRQL[];
  31. extern UCHAR HalpIRQLtoTPR[];
  32. extern USHORT HalpVectorToINTI[];
  33. extern KSPIN_LOCK HalpAccountingLock;
  34. extern struct HalpMpInfo HalpMpInfoTable;
  35. extern UCHAR HalpMaxProcsPerCluster;
  36. extern INTERRUPT_DEST HalpIntDestMap[];
  37. ULONG HalpINTItoVector[MAX_INTI];
  38. UCHAR HalpPICINTToVector[16];
  39. extern ULONG HalpMaxNode;
  40. extern ULONG HalpNodeAffinity[MAX_NODES];
  41. UCHAR HalpNodeBucket[MAX_NODES];
  42. #ifdef ALLOC_PRAGMA
  43. #pragma alloc_text(INIT,HalpSetInternalVector)
  44. #pragma alloc_text(PAGELK,HalpGetSystemInterruptVector)
  45. #pragma alloc_text(PAGE, HalIrqTranslateResourceRequirementsRoot)
  46. #pragma alloc_text(PAGE, HalTranslatorReference)
  47. #pragma alloc_text(PAGE, HalTranslatorDereference)
  48. #endif
  49. BOOLEAN
  50. HalpFindBusAddressTranslation(
  51. IN PHYSICAL_ADDRESS BusAddress,
  52. IN OUT PULONG AddressSpace,
  53. OUT PPHYSICAL_ADDRESS TranslatedAddress,
  54. IN OUT PULONG_PTR Context,
  55. IN BOOLEAN NextBus
  56. )
  57. /*++
  58. Routine Description:
  59. This routine performs a very similar function to HalTranslateBusAddress
  60. except that InterfaceType and BusNumber are not known by the caller.
  61. This function will walk all busses known by the HAL looking for a
  62. valid translation for the input BusAddress of type AddressSpace.
  63. This function is recallable using the input/output Context parameter.
  64. On the first call to this routine for a given translation the ULONG_PTR
  65. Context should be NULL. Note: Not the address of it but the contents.
  66. If the caller decides the returned translation is not the desired
  67. translation, it calls this routine again passing Context in as it
  68. was returned on the previous call. This allows this routine to
  69. traverse the bus structures until the correct translation is found
  70. and is provided because on multiple bus systems, it is possible for
  71. the same resource to exist in the independent address spaces of
  72. multiple busses.
  73. Arguments:
  74. BusAddress Address to be translated.
  75. AddressSpace 0 = Memory
  76. 1 = IO (There are other possibilities).
  77. N.B. This argument is a pointer, the value
  78. will be modified if the translated address
  79. is of a different address space type from
  80. the untranslated bus address.
  81. TranslatedAddress Pointer to where the translated address
  82. should be stored.
  83. Context Pointer to a ULONG_PTR. On the initial call,
  84. for a given BusAddress, it should contain
  85. 0. It will be modified by this routine,
  86. on subsequent calls for the same BusAddress
  87. the value should be handed in again,
  88. unmodified by the caller.
  89. NextBus FALSE if we should attempt this translation
  90. on the same bus as indicated by Context,
  91. TRUE if we should be looking for another
  92. bus.
  93. Return Value:
  94. TRUE if translation was successful,
  95. FALSE otherwise.
  96. --*/
  97. {
  98. //
  99. // First, make sure the context parameter was supplied and is
  100. // being used correctly. This also ensures that the caller
  101. // doesn't get stuck in a loop looking for subsequent translations
  102. // for the same thing. We won't succeed the same translation twice
  103. // unless the caller reinits the context.
  104. //
  105. if ((!Context) || (*Context && (NextBus == TRUE))) {
  106. return FALSE;
  107. }
  108. *Context = 1;
  109. //
  110. // PC/AT (halx86) case is simplest, there is no translation.
  111. //
  112. *TranslatedAddress = BusAddress;
  113. return TRUE;
  114. }
  115. BOOLEAN
  116. HalpTranslateSystemBusAddress(
  117. IN PBUS_HANDLER BusHandler,
  118. IN PBUS_HANDLER RootHandler,
  119. IN PHYSICAL_ADDRESS BusAddress,
  120. IN OUT PULONG AddressSpace,
  121. OUT PPHYSICAL_ADDRESS TranslatedAddress
  122. )
  123. /*++
  124. Routine Description:
  125. This function translates a bus-relative address space and address into
  126. a system physical address.
  127. Arguments:
  128. BusAddress - Supplies the bus-relative address
  129. AddressSpace - Supplies the address space number.
  130. Returns the host address space number.
  131. AddressSpace == 0 => memory space
  132. AddressSpace == 1 => I/O space
  133. TranslatedAddress - Supplies a pointer to return the translated address
  134. Return Value:
  135. A return value of TRUE indicates that a system physical address
  136. corresponding to the supplied bus relative address and bus address
  137. number has been returned in TranslatedAddress.
  138. A return value of FALSE occurs if the translation for the address was
  139. not possible
  140. --*/
  141. {
  142. BOOLEAN status;
  143. PSUPPORTED_RANGE pRange;
  144. status = FALSE;
  145. switch (*AddressSpace) {
  146. case 0:
  147. if (BusHandler->InterfaceType != PCIBus) {
  148. // verify memory address is within buses memory limits
  149. pRange = &BusHandler->BusAddresses->Memory;
  150. while (!status && pRange) {
  151. status = BusAddress.QuadPart >= pRange->Base &&
  152. BusAddress.QuadPart <= pRange->Limit;
  153. pRange = pRange->Next;
  154. }
  155. pRange = &BusHandler->BusAddresses->PrefetchMemory;
  156. while (!status && pRange) {
  157. status = BusAddress.QuadPart >= pRange->Base &&
  158. BusAddress.QuadPart <= pRange->Limit;
  159. pRange = pRange->Next;
  160. }
  161. } else {
  162. //
  163. // This is a PCI bus and SystemBase is constant for all ranges
  164. //
  165. pRange = &BusHandler->BusAddresses->Memory;
  166. status = TRUE;
  167. }
  168. break;
  169. case 1:
  170. if (BusHandler->InterfaceType != PCIBus) {
  171. // verify IO address is within buses IO limits
  172. pRange = &BusHandler->BusAddresses->IO;
  173. while (!status && pRange) {
  174. status = BusAddress.QuadPart >= pRange->Base &&
  175. BusAddress.QuadPart <= pRange->Limit;
  176. pRange = pRange->Next;
  177. }
  178. } else {
  179. //
  180. // This is a PCI bus and SystemBase is constant for all ranges
  181. //
  182. pRange = &BusHandler->BusAddresses->IO;
  183. status = TRUE;
  184. }
  185. break;
  186. default:
  187. status = FALSE;
  188. break;
  189. }
  190. if (status) {
  191. *TranslatedAddress = BusAddress;
  192. } else {
  193. _asm { nop }; // good for debugging
  194. }
  195. return status;
  196. }
  197. #define MAX_SYSTEM_IRQL 31
  198. #define MAX_FREE_IRQL 26
  199. #define MIN_FREE_IRQL 4
  200. #define MAX_FREE_IDTENTRY 0xbf
  201. #define MIN_FREE_IDTENTRY 0x51
  202. #define IDTENTRY_BASE 0x50
  203. #define MAX_VBUCKET 7
  204. #define AllocateVectorIn(index) \
  205. vBucket[index]++; \
  206. ASSERT (vBucket[index] < 16);
  207. #define GetIDTEntryFrom(index) \
  208. (UCHAR) ( index*16 + IDTENTRY_BASE + vBucket[index] )
  209. // note: device levels 50,60,70,80,90,A0,B0 are not allocatable
  210. #define GetIrqlFrom(index) (KIRQL) ( index + MIN_FREE_IRQL )
  211. UCHAR nPriority[MAX_NODES][MAX_VBUCKET];
  212. ULONG
  213. HalpGetSystemInterruptVector (
  214. IN PBUS_HANDLER BusHandler,
  215. IN PBUS_HANDLER RootHandler,
  216. IN ULONG InterruptLevel,
  217. IN ULONG InterruptVector,
  218. OUT PKIRQL Irql,
  219. OUT PKAFFINITY Affinity
  220. )
  221. /*++
  222. Routine Description:
  223. This function returns the system interrupt vector and IRQL
  224. corresponding to the specified bus interrupt level and/or
  225. vector. The system interrupt vector and IRQL are suitable
  226. for use in a subsequent call to KeInitializeInterrupt.
  227. Arguments:
  228. InterruptLevel - Supplies the bus specific interrupt level.
  229. InterruptVector - Supplies the bus specific interrupt vector.
  230. Irql - Returns the system request priority.
  231. Affinity - Returns the system wide irq affinity.
  232. Return Value:
  233. Returns the system interrupt vector corresponding to the specified device.
  234. --*/
  235. {
  236. ULONG SystemVector;
  237. USHORT ApicInti;
  238. UCHAR IDTEntry;
  239. ULONG Bucket, i, OldLevel;
  240. BOOLEAN Found;
  241. PVOID LockHandle;
  242. ULONG Node;
  243. PUCHAR vBucket;
  244. UNREFERENCED_PARAMETER( InterruptVector );
  245. //
  246. // TODO: Remove when Affinity becomes IN OUT.
  247. *Affinity = ~0;
  248. //
  249. // Restrict Affinity if required.
  250. if (HalpMaxProcsPerCluster == 0) {
  251. *Affinity &= HalpDefaultInterruptAffinity;
  252. }
  253. //
  254. // Find closest child bus to this handler
  255. //
  256. if (RootHandler != BusHandler) {
  257. while (RootHandler->ParentHandler != BusHandler) {
  258. RootHandler = RootHandler->ParentHandler;
  259. }
  260. }
  261. //
  262. // Find Interrupt's APIC Inti connection
  263. //
  264. Found = HalpGetApicInterruptDesc (
  265. RootHandler->InterfaceType,
  266. RootHandler->BusNumber,
  267. InterruptLevel,
  268. &ApicInti
  269. );
  270. if (!Found) {
  271. return 0;
  272. }
  273. //
  274. // If device interrupt vector mapping is not already allocated,
  275. // then do it now
  276. //
  277. if (!HalpINTItoVector[ApicInti]) {
  278. //
  279. // Vector is not allocated - synchronize and check again
  280. //
  281. LockHandle = MmLockPagableCodeSection (&HalpGetSystemInterruptVector);
  282. OldLevel = HalpAcquireHighLevelLock (&HalpAccountingLock);
  283. if (!HalpINTItoVector[ApicInti]) {
  284. //
  285. // Still not allocated
  286. //
  287. //
  288. // Pick a node. In the future, Affinity will be INOUT and
  289. // we will have to screen the node against the input affinity.
  290. if (HalpMaxNode == 1) {
  291. Node = 1;
  292. } else {
  293. //
  294. // Find a node that meets the affinity requirements.
  295. // Nodes are numbered 1..n, so 0 means we are done.
  296. for (i = HalpMaxNode; i; i--) {
  297. if ((*Affinity & HalpNodeAffinity[i-1]) == 0)
  298. continue;
  299. Node = i;
  300. break;
  301. }
  302. ASSERT(Node != 0);
  303. //
  304. // Look for a "less busy" alternative.
  305. for (i = Node-1; i; i--) {
  306. //
  307. // Check input Affinity to see if this node is permitted.
  308. if ((*Affinity & HalpNodeAffinity[i-1]) == 0)
  309. continue;
  310. //
  311. // Take the least busy of the permitted nodes.
  312. if (HalpNodeBucket[i-1] < HalpNodeBucket[Node-1]) {
  313. Node = i;
  314. }
  315. }
  316. }
  317. HalpNodeBucket[Node-1]++;
  318. *Affinity = HalpNodeAffinity[Node-1];
  319. vBucket = nPriority[Node-1];
  320. //
  321. // Choose the least busy priority on the node.
  322. Bucket = MAX_VBUCKET-1;
  323. for (i = Bucket-1; i; i--) {
  324. if (vBucket[i] < vBucket[Bucket]) {
  325. Bucket = i;
  326. }
  327. }
  328. AllocateVectorIn (Bucket);
  329. //
  330. // Now form the vector for the kernel.
  331. IDTEntry = GetIDTEntryFrom (Bucket);
  332. SystemVector = HalpVector(Node, IDTEntry);
  333. ASSERT(IDTEntry <= MAX_FREE_IDTENTRY);
  334. ASSERT(IDTEntry >= MIN_FREE_IDTENTRY);
  335. *Irql = GetIrqlFrom (Bucket);
  336. ASSERT(*Irql <= MAX_FREE_IRQL);
  337. ASSERT((UCHAR) (HalpIRQLtoTPR[*Irql] & 0xf0) == (UCHAR) (IDTEntry & 0xf0) );
  338. HalpVectorToIRQL[IDTEntry >> 4] = (UCHAR) *Irql;
  339. HalpVectorToINTI[SystemVector] = (USHORT) ApicInti;
  340. HalpINTItoVector[ApicInti] = SystemVector;
  341. //
  342. // If this assigned interrupt is connected to the machines PIC,
  343. // then remember the PIC->SystemVector mapping.
  344. //
  345. if (RootHandler->BusNumber == 0 && InterruptLevel < 16 &&
  346. RootHandler->InterfaceType == DEFAULT_PC_BUS) {
  347. HalpPICINTToVector[InterruptLevel] = (UCHAR) SystemVector;
  348. }
  349. }
  350. HalpReleaseHighLevelLock (&HalpAccountingLock, OldLevel);
  351. MmUnlockPagableImageSection (LockHandle);
  352. }
  353. //
  354. // Return this ApicInti's system vector & irql
  355. //
  356. SystemVector = HalpINTItoVector[ApicInti];
  357. *Irql = HalpVectorToIRQL[HalVectorToIDTEntry(SystemVector) >> 4];
  358. ASSERT(HalpVectorToINTI[SystemVector] == (USHORT) ApicInti);
  359. //
  360. // Find an appropriate affinity.
  361. //
  362. Node = HalpVectorToNode(SystemVector);
  363. *Affinity &= HalpNodeAffinity[Node-1];
  364. if (!*Affinity) {
  365. return 0;
  366. }
  367. return SystemVector;
  368. }
  369. VOID
  370. HalpSetInternalVector (
  371. IN ULONG InternalVector,
  372. IN VOID (*HalInterruptServiceRoutine)(VOID)
  373. )
  374. /*++
  375. Routine Description:
  376. Used at init time to set IDT vectors for internal use.
  377. --*/
  378. {
  379. //
  380. // Remember this vector so it's reported as Hal internal usage
  381. //
  382. // HalpRegisterVector (
  383. // InternalUsage,
  384. // InternalVector,
  385. // InternalVector,
  386. // HalpVectorToIRQL[InternalVector >> 4]
  387. // );
  388. //
  389. // Connect the IDT
  390. //
  391. KiSetHandlerAddressToIDT(InternalVector, HalInterruptServiceRoutine);
  392. }
  393. //
  394. // This section implements a "translator," which is the PnP-WDM way
  395. // of doing the same thing that the first part of this file does.
  396. //
  397. VOID
  398. HalTranslatorReference(
  399. PVOID Context
  400. )
  401. {
  402. return;
  403. }
  404. VOID
  405. HalTranslatorDereference(
  406. PVOID Context
  407. )
  408. {
  409. return;
  410. }
  411. NTSTATUS
  412. HalIrqTranslateResourcesRoot(
  413. IN PVOID Context,
  414. IN PCM_PARTIAL_RESOURCE_DESCRIPTOR Source,
  415. IN RESOURCE_TRANSLATION_DIRECTION Direction,
  416. IN ULONG AlternativesCount, OPTIONAL
  417. IN IO_RESOURCE_DESCRIPTOR Alternatives[], OPTIONAL
  418. IN PDEVICE_OBJECT PhysicalDeviceObject,
  419. OUT PCM_PARTIAL_RESOURCE_DESCRIPTOR Target
  420. )
  421. /*++
  422. Routine Description:
  423. This function takes a CM_PARTIAL_RESOURCE_DESCRIPTOR and translates
  424. it to an IO-bus-relative from a Processor-bus-relative form, or the other
  425. way around. In this x86-specific example, an IO-bus-relative form is the
  426. ISA IRQ and the Processor-bus-relative form is the IDT entry and the
  427. associated IRQL.
  428. N.B. This funtion has an associated "Direction." These are not exactly
  429. reciprocals. This has to be the case because the output from
  430. HalIrqTranslateResourceRequirementsRoot will be used as the input
  431. for the ParentToChild case.
  432. ChildToParent:
  433. Level (ISA IRQ) -> IRQL
  434. Vector (ISA IRQ) -> x86 IDT entry
  435. Affinity (not refereced)-> KAFFINITY
  436. ParentToChild:
  437. Level (not referenced) -> (ISA IRQ)
  438. Vector (IDT entry) -> (ISA IRQ)
  439. Affinity -> 0xffffffff
  440. Arguments:
  441. Context - unused
  442. Source - descriptor that we are translating
  443. Direction - direction of translation (parent to child or child to parent)
  444. AlternativesCount - unused
  445. Alternatives - unused
  446. PhysicalDeviceObject- unused
  447. Target - translated descriptor
  448. Return Value:
  449. status
  450. --*/
  451. {
  452. NTSTATUS status = STATUS_SUCCESS;
  453. PBUS_HANDLER bus;
  454. KAFFINITY affinity;
  455. KIRQL irql;
  456. ULONG vector;
  457. USHORT inti;
  458. #ifdef ACPI_HAL
  459. BUS_HANDLER fakeIsaBus;
  460. #endif
  461. PAGED_CODE();
  462. UNREFERENCED_PARAMETER(AlternativesCount);
  463. UNREFERENCED_PARAMETER(Alternatives);
  464. UNREFERENCED_PARAMETER(PhysicalDeviceObject);
  465. ASSERT(Source->Type == CmResourceTypeInterrupt);
  466. switch (Direction) {
  467. case TranslateChildToParent:
  468. #ifdef ACPI_HAL
  469. RtlCopyMemory(&fakeIsaBus, &HalpFakePciBusHandler, sizeof(BUS_HANDLER));
  470. fakeIsaBus.InterfaceType = Isa;
  471. fakeIsaBus.ParentHandler = &fakeIsaBus;
  472. bus = &fakeIsaBus;
  473. #else
  474. if ((INTERFACE_TYPE)Context == InterfaceTypeUndefined) { // special "IDE" cookie
  475. ASSERT(Source->u.Interrupt.Level == Source->u.Interrupt.Vector);
  476. bus = HalpFindIdeBus(Source->u.Interrupt.Vector);
  477. } else {
  478. bus = HaliHandlerForBus((INTERFACE_TYPE)Context, 0);
  479. }
  480. if (!bus) {
  481. return STATUS_NOT_FOUND;
  482. }
  483. #endif
  484. //
  485. // Copy everything
  486. //
  487. *Target = *Source;
  488. //
  489. // Translate the IRQ
  490. //
  491. vector = HalpGetEisaInterruptVector(bus,
  492. bus,
  493. Source->u.Interrupt.Level,
  494. Source->u.Interrupt.Vector,
  495. &irql,
  496. &affinity);
  497. if (vector == 0) {
  498. return STATUS_UNSUCCESSFUL;
  499. }
  500. Target->u.Interrupt.Level = irql;
  501. Target->u.Interrupt.Vector = vector;
  502. Target->u.Interrupt.Affinity = affinity;
  503. if (NT_SUCCESS(status)) {
  504. status = STATUS_TRANSLATION_COMPLETE;
  505. }
  506. break;
  507. case TranslateParentToChild:
  508. //
  509. // Copy everything
  510. //
  511. *Target = *Source;
  512. //
  513. // There is no inverse to HalpGetSystemInterruptVector, so we
  514. // just do what that function would do.
  515. //
  516. inti = HalpVectorToINTI[Source->u.Interrupt.Vector];
  517. Target->u.Interrupt.Level = Target->u.Interrupt.Vector =
  518. HalpInti2BusInterruptLevel(inti);
  519. Target->u.Interrupt.Affinity = 0xFFFFFFFF;
  520. status = STATUS_SUCCESS;
  521. break;
  522. default:
  523. status = STATUS_INVALID_PARAMETER;
  524. }
  525. return status;
  526. }
  527. NTSTATUS
  528. HalIrqTranslateResourceRequirementsRoot(
  529. IN PVOID Context,
  530. IN PIO_RESOURCE_DESCRIPTOR Source,
  531. IN PDEVICE_OBJECT PhysicalDeviceObject,
  532. OUT PULONG TargetCount,
  533. OUT PIO_RESOURCE_DESCRIPTOR *Target
  534. )
  535. /*++
  536. Routine Description:
  537. This function takes an IO_RESOURCE_DESCRIPTOR and translates
  538. it from an IO-bus-relative to a Processor-bus-relative form. In this
  539. x86-specific example, an IO-bus-relative form is the ISA IRQ and the
  540. Processor-bus-relative form is the IDT entry and the associated IRQL.
  541. This is essentially a PnP form of HalGetInterruptVector.
  542. Arguments:
  543. Context - unused
  544. Source - descriptor that we are translating
  545. PhysicalDeviceObject- unused
  546. TargetCount - 1
  547. Target - translated descriptor
  548. Return Value:
  549. status
  550. --*/
  551. {
  552. PBUS_HANDLER bus;
  553. KAFFINITY affinity;
  554. KIRQL irql;
  555. ULONG vector;
  556. BOOLEAN success = TRUE;
  557. #ifdef ACPI_HAL
  558. BUS_HANDLER fakeIsaBus;
  559. #endif
  560. PAGED_CODE();
  561. ASSERT(Source->Type == CmResourceTypeInterrupt);
  562. #ifdef ACPI_HAL
  563. RtlCopyMemory(&fakeIsaBus, &HalpFakePciBusHandler, sizeof(BUS_HANDLER));
  564. fakeIsaBus.InterfaceType = Isa;
  565. fakeIsaBus.ParentHandler = &fakeIsaBus;
  566. bus = &fakeIsaBus;
  567. #else
  568. if ((INTERFACE_TYPE)Context == InterfaceTypeUndefined) { // special "IDE" cookie
  569. ASSERT(Source->u.Interrupt.MinimumVector == Source->u.Interrupt.MaximumVector);
  570. bus = HalpFindIdeBus(Source->u.Interrupt.MinimumVector);
  571. } else {
  572. bus = HaliHandlerForBus((INTERFACE_TYPE)Context, 0);
  573. }
  574. if (!bus) {
  575. //
  576. // There is no valid translation.
  577. //
  578. *TargetCount = 0;
  579. return STATUS_TRANSLATION_COMPLETE;
  580. }
  581. #endif
  582. //
  583. // The interrupt requirements were obtained by calling HalAdjustResourceList
  584. // so we don't need to call it again.
  585. //
  586. *Target = ExAllocatePoolWithTag(PagedPool,
  587. sizeof(IO_RESOURCE_DESCRIPTOR),
  588. HAL_POOL_TAG
  589. );
  590. if (!*Target) {
  591. return STATUS_INSUFFICIENT_RESOURCES;
  592. }
  593. *TargetCount = 1;
  594. //
  595. // Copy the requirement unchanged
  596. //
  597. **Target = *Source;
  598. //
  599. // Perform the translation of the minimum & maximum
  600. //
  601. vector = HalpGetEisaInterruptVector(bus,
  602. bus,
  603. Source->u.Interrupt.MinimumVector,
  604. Source->u.Interrupt.MinimumVector,
  605. &irql,
  606. &affinity);
  607. if (!vector) {
  608. success = FALSE;
  609. }
  610. (*Target)->u.Interrupt.MinimumVector = vector;
  611. vector = HalpGetEisaInterruptVector(bus,
  612. bus,
  613. Source->u.Interrupt.MaximumVector,
  614. Source->u.Interrupt.MaximumVector,
  615. &irql,
  616. &affinity);
  617. if (!vector) {
  618. success = FALSE;
  619. }
  620. (*Target)->u.Interrupt.MaximumVector = vector;
  621. if (!success) {
  622. ExFreePool(*Target);
  623. *TargetCount = 0;
  624. }
  625. return STATUS_TRANSLATION_COMPLETE;
  626. }
  627. #if 0
  628. // HALMPS doesn't provide this function. It is left here as documentation
  629. // for HALs which must provide translation.
  630. NTSTATUS
  631. HalpTransMemIoResourceRequirement(
  632. IN PVOID Context,
  633. IN PIO_RESOURCE_DESCRIPTOR Source,
  634. IN PDEVICE_OBJECT PhysicalDeviceObject,
  635. OUT PULONG TargetCount,
  636. OUT PIO_RESOURCE_DESCRIPTOR *Target
  637. )
  638. /*++
  639. Routine Description:
  640. This routine translates memory and IO resource requirements.
  641. Parameters:
  642. Context - The context from the TRANSLATOR_INTERFACE
  643. Source - The interrupt requirement to translate
  644. PhysicalDeviceObject - The device requesting the resource
  645. TargetCount - Pointer to where to return the number of descriptors this
  646. requirement translates into
  647. Target - Pointer to where a pointer to a callee allocated buffer containing
  648. the translated descriptors should be placed.
  649. Return Value:
  650. STATUS_SUCCESS or an error status
  651. Note:
  652. We do not perform any translation.
  653. --*/
  654. {
  655. ASSERT(Source);
  656. ASSERT(Target);
  657. ASSERT(TargetCount);
  658. ASSERT(Source->Type == CmResourceTypeMemory ||
  659. Source->Type == CmResourceTypePort);
  660. //
  661. // Allocate space for the target
  662. //
  663. *Target = ExAllocatePoolWithTag(PagedPool,
  664. sizeof(IO_RESOURCE_DESCRIPTOR),
  665. HAL_POOL_TAG
  666. );
  667. if (!*Target) {
  668. return STATUS_INSUFFICIENT_RESOURCES;
  669. }
  670. //
  671. // Copy the source to target and update the fields that have changed
  672. //
  673. **Target = *Source;
  674. *TargetCount = 1;
  675. return STATUS_SUCCESS;
  676. }
  677. NTSTATUS
  678. HalpTransMemIoResource(
  679. IN PVOID Context,
  680. IN PCM_PARTIAL_RESOURCE_DESCRIPTOR Source,
  681. IN RESOURCE_TRANSLATION_DIRECTION Direction,
  682. IN ULONG AlternativesCount, OPTIONAL
  683. IN IO_RESOURCE_DESCRIPTOR Alternatives[], OPTIONAL
  684. IN PDEVICE_OBJECT PhysicalDeviceObject,
  685. OUT PCM_PARTIAL_RESOURCE_DESCRIPTOR Target
  686. )
  687. /*++
  688. Routine Description:
  689. This routine translates memory and IO resources. On generic x86
  690. machines, such as those that use this HAL, there isn't actually
  691. any translation.
  692. Parameters:
  693. Context - The context from the TRANSLATOR_INTERFACE
  694. Source - The interrupt resource to translate
  695. Direction - The direction in relation to the Pnp device tree translation
  696. should occur in.
  697. AlternativesCount - The number of alternatives this resource was selected
  698. from.
  699. Alternatives - Array of alternatives this resource was selected from.
  700. PhysicalDeviceObject - The device requesting the resource
  701. Target - Pointer to a caller allocated buffer to hold the translted resource
  702. descriptor.
  703. Return Value:
  704. STATUS_SUCCESS or an error status
  705. --*/
  706. {
  707. NTSTATUS status;
  708. //
  709. // Copy the target to the source
  710. //
  711. *Target = *Source;
  712. switch (Direction) {
  713. case TranslateChildToParent:
  714. //
  715. // Make sure PnP knows it doesn't have to walk up the tree
  716. // translating at each point.
  717. //
  718. status = STATUS_TRANSLATION_COMPLETE;
  719. break;
  720. case TranslateParentToChild:
  721. //
  722. // We do not translate requirements so do nothing...
  723. //
  724. status = STATUS_SUCCESS;
  725. break;
  726. default:
  727. status = STATUS_INVALID_PARAMETER;
  728. }
  729. return status;
  730. }
  731. #endif
  732. NTSTATUS
  733. HaliGetInterruptTranslator(
  734. IN INTERFACE_TYPE ParentInterfaceType,
  735. IN ULONG ParentBusNumber,
  736. IN INTERFACE_TYPE BridgeInterfaceType,
  737. IN USHORT Size,
  738. IN USHORT Version,
  739. OUT PTRANSLATOR_INTERFACE Translator,
  740. IN OUT PULONG BridgeBusNumber
  741. )
  742. /*++
  743. Routine Description:
  744. Arguments:
  745. ParentInterfaceType - The type of the bus the bridge lives on (normally PCI).
  746. ParentBusNumber - The number of the bus the bridge lives on.
  747. BridgeInterfaceType - The bus type the bridge provides (ie ISA for a PCI-ISA bridge).
  748. ResourceType - The resource type we want to translate.
  749. Size - The size of the translator buffer.
  750. Version - The version of the translator interface requested.
  751. Translator - Pointer to the buffer where the translator should be returned
  752. BridgeBusNumber - Pointer the bus number of the bus that the bridge represents
  753. Return Value:
  754. Returns the status of this operation.
  755. --*/
  756. #define BRIDGE_HEADER_BUFFER_SIZE (FIELD_OFFSET(PCI_COMMON_CONFIG, u.type1.SubordinateBus) + 1)
  757. #define USE_INT_LINE_REGISTER_TOKEN 0xffffffff
  758. #define DEFAULT_BRIDGE_TRANSLATOR 0x80000000
  759. {
  760. PAGED_CODE();
  761. ASSERT(Version == HAL_IRQ_TRANSLATOR_VERSION);
  762. ASSERT(Size >= sizeof(TRANSLATOR_INTERFACE));
  763. //
  764. // Fill in the common bits.
  765. //
  766. RtlZeroMemory(Translator, sizeof(TRANSLATOR_INTERFACE));
  767. Translator->Size = sizeof(TRANSLATOR_INTERFACE);
  768. Translator->Version = HAL_IRQ_TRANSLATOR_VERSION;
  769. Translator->Context = (PVOID)BridgeInterfaceType;
  770. Translator->InterfaceReference = HalTranslatorReference;
  771. Translator->InterfaceDereference = HalTranslatorDereference;
  772. switch (BridgeInterfaceType) {
  773. case Eisa:
  774. case Isa:
  775. case InterfaceTypeUndefined: // special "IDE" cookie
  776. //
  777. // Set IRQ translator for (E)ISA interrupts.
  778. //
  779. Translator->TranslateResources = HalIrqTranslateResourcesIsa;
  780. Translator->TranslateResourceRequirements =
  781. HalIrqTranslateResourceRequirementsIsa;
  782. return STATUS_SUCCESS;
  783. case MicroChannel:
  784. //
  785. // Set IRQ translator for MCA interrupts.
  786. //
  787. Translator->TranslateResources = HalIrqTranslateResourcesRoot;
  788. Translator->TranslateResourceRequirements =
  789. HalIrqTranslateResourceRequirementsRoot;
  790. return STATUS_SUCCESS;
  791. case PCIBus:
  792. #ifndef ACPI_HAL
  793. //
  794. // Set of of two IRQ translators for PCI busses.
  795. //
  796. {
  797. UCHAR mpsBusNumber = 0;
  798. UCHAR pciBusNumber, parentPci, childPci;
  799. PCI_SLOT_NUMBER bridgeSlot;
  800. PCI_COMMON_CONFIG pciData;
  801. ULONG bytesRead, d, f, possibleContext;
  802. BOOLEAN describedByMps;
  803. NTSTATUS status;
  804. Translator->TranslateResources = HalpIrqTranslateResourcesPci;
  805. Translator->TranslateResourceRequirements =
  806. HalpIrqTranslateRequirementsPci;
  807. //
  808. // Look for this bus in the MPS tables.
  809. //
  810. status = HalpPci2MpsBusNumber((UCHAR)*BridgeBusNumber,
  811. &mpsBusNumber);
  812. if (NT_SUCCESS(status)) {
  813. //
  814. // This bus has corresponding entries for its PCI
  815. // devices in the MPS tables. So eject the translator
  816. // that understands them.
  817. //
  818. if (HalpInterruptsDescribedByMpsTable(mpsBusNumber)) {
  819. Translator->Context = (PVOID)mpsBusNumber;
  820. return STATUS_SUCCESS;
  821. }
  822. }
  823. //
  824. // Do a quick check to see if we can avoid searching PCI
  825. // configuration space for a bridge. This code is really
  826. // redundant, but it's worth trying to avoid touching
  827. // PCI space.
  828. //
  829. if (ParentInterfaceType != PCIBus) {
  830. //
  831. // This was a PCI bus that doesn't contain
  832. // mappings for PCI devices.
  833. //
  834. Translator->TranslateResources =
  835. HalpIrqTranslateResourcesPci;
  836. Translator->TranslateResourceRequirements =
  837. HalpIrqTranslateRequirementsPci;
  838. Translator->Context = (PVOID)USE_INT_LINE_REGISTER_TOKEN;
  839. return STATUS_SUCCESS;
  840. }
  841. //
  842. // We didn't find this PCI bus in the MPS tables. So there
  843. // are two cases.
  844. //
  845. // 1) This matters, because the parent bus is fully described
  846. // in the MPS tables and we need to do translations on
  847. // the vector as it passes through the bridges.
  848. //
  849. // 2) This doesn't matter, because the parent busses, while
  850. // they may be in the MPS tables, they don't have any
  851. // of their interrupts described. So we just use the
  852. // interrupt line register anyhow.
  853. //
  854. // At this point we need to find the PCI bridge that
  855. // generates this bus, either because we will eventually
  856. // need to know the slot number to fill in the context, or
  857. // because we will need to know the primary bus number to
  858. // look up the tree.
  859. //
  860. parentPci = (UCHAR)ParentBusNumber;
  861. childPci = (UCHAR)(*BridgeBusNumber);
  862. while (TRUE) {
  863. //
  864. // Find the bridge.
  865. //
  866. bridgeSlot.u.AsULONG = 0;
  867. for (d = 0; d < PCI_MAX_DEVICES; d++) {
  868. for (f = 0; f < PCI_MAX_FUNCTION; f++) {
  869. bridgeSlot.u.bits.DeviceNumber = d;
  870. bridgeSlot.u.bits.FunctionNumber = f;
  871. bytesRead = HalGetBusDataByOffset(PCIConfiguration,
  872. parentPci,
  873. bridgeSlot.u.AsULONG,
  874. &pciData,
  875. 0,
  876. BRIDGE_HEADER_BUFFER_SIZE);
  877. if (bytesRead == (ULONG)BRIDGE_HEADER_BUFFER_SIZE) {
  878. if ((pciData.VendorID != PCI_INVALID_VENDORID) &&
  879. (PCI_CONFIGURATION_TYPE((&pciData)) != PCI_DEVICE_TYPE)) {
  880. //
  881. // This is a bridge of some sort.
  882. //
  883. if (pciData.u.type1.SecondaryBus == childPci) {
  884. //
  885. // And it is the bridge we are looking for.
  886. // Store information about
  887. //
  888. if (childPci == *BridgeBusNumber) {
  889. //
  890. // It is also the bridge that creates the
  891. // PCI bus that the translator is describing.
  892. //
  893. // N.B. This should only happen the first time
  894. // we search through a bus. (i.e. the first
  895. // trip through the outer while loop)
  896. //
  897. possibleContext = ((bridgeSlot.u.AsULONG & 0xffff) |
  898. (ParentBusNumber << 16));
  899. }
  900. goto HGITFoundBridge1;
  901. }
  902. }
  903. }
  904. }
  905. }
  906. //
  907. // No bridge found.
  908. //
  909. if (parentPci == 0) {
  910. return STATUS_NOT_FOUND;
  911. }
  912. parentPci--;
  913. continue;
  914. HGITFoundBridge1:
  915. status = HalpPci2MpsBusNumber(parentPci, &mpsBusNumber);
  916. if (NT_SUCCESS(status)) {
  917. if (HalpInterruptsDescribedByMpsTable(mpsBusNumber)) {
  918. //
  919. // Case 1 above.
  920. //
  921. Translator->TranslateResources = HalIrqTranslateResourcesPciBridge;
  922. Translator->TranslateResourceRequirements =
  923. HalIrqTranslateRequirementsPciBridge;
  924. Translator->Context = (PVOID)possibleContext;
  925. return STATUS_SUCCESS;
  926. }
  927. if (HalpMpsBusIsRootBus(mpsBusNumber)) {
  928. Translator->TranslateResources =
  929. HalpIrqTranslateResourcesPci;
  930. Translator->TranslateResourceRequirements =
  931. HalpIrqTranslateRequirementsPci;
  932. Translator->Context = (PVOID)USE_INT_LINE_REGISTER_TOKEN;
  933. return STATUS_SUCCESS;
  934. }
  935. }
  936. //
  937. // Try again one bus higher.
  938. //
  939. childPci = parentPci;
  940. parentPci--;
  941. }
  942. }
  943. #endif
  944. break;
  945. }
  946. //
  947. // If we got here, we don't have an interface.
  948. //
  949. return STATUS_NOT_IMPLEMENTED;
  950. }