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.

1167 lines
31 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. rangesup.c
  6. Abstract:
  7. Supplies support function for dealing with SUPPORTED_RANGEs.
  8. Author:
  9. Ken Reneris (kenr) March-27-1995
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. */
  14. #include "halp.h"
  15. #define STATIC
  16. STATIC ULONG
  17. HalpSortRanges (
  18. IN PSUPPORTED_RANGE pRange1
  19. );
  20. typedef struct tagNRParams {
  21. PIO_RESOURCE_DESCRIPTOR InDesc;
  22. PIO_RESOURCE_DESCRIPTOR OutDesc;
  23. PSUPPORTED_RANGE CurrentPosition;
  24. LONGLONG Base;
  25. LONGLONG Limit;
  26. UCHAR DescOpt;
  27. BOOLEAN AnotherListPending;
  28. } NRPARAMS, *PNRPARAMS;
  29. STATIC PIO_RESOURCE_DESCRIPTOR
  30. HalpGetNextSupportedRange (
  31. IN LONGLONG MinimumAddress,
  32. IN LONGLONG MaximumAddress,
  33. IN OUT PNRPARAMS PNRParams
  34. );
  35. //
  36. // These following functions are usable at to initialize
  37. // the supported_ranges information for a bus handler.
  38. // HalpMergeRanges - merges two bus supported ranges
  39. // HalpMergeRangeList - merges two single supported ranges lists
  40. // HalpCopyRanges - copy a bus supported ranges to a new supported ranges structure
  41. // HalpAddRangeList - adds a supported range list to another
  42. // HalpAddRange - adds a single range to a supported range list
  43. // HalpRemoveRanges - removes all ranges from one buses supported ranges from another
  44. // HalpRemoveRangeList - removes all ranges in one supported range list from another
  45. // HalpRemoveRange - removes a single range from a supported range list
  46. // HalpAllocateNewRangeList - allocates a new, "blank" bus supported ranges structure
  47. // HalpFreeRangeList - frees an entire bus supported ranges
  48. //
  49. // HalpConsolidateRanges - cleans up a supported ranges structure to be ready for usage
  50. //
  51. //
  52. // These functions are used to intersect a buses supported ranges
  53. // to an IO_RESOURCE_REQUIREMENTS_LIST:
  54. // HaliAdjustResourceListRange
  55. //
  56. // These functions are used internal to this module:
  57. // HalpSortRanges
  58. // HalpGetNextSupportedRange
  59. //
  60. #ifdef ALLOC_PRAGMA
  61. #pragma alloc_text(INIT,HalpMergeRanges)
  62. #pragma alloc_text(INIT,HalpMergeRangeList)
  63. #pragma alloc_text(INIT,HalpCopyRanges)
  64. #pragma alloc_text(INIT,HalpAddRangeList)
  65. #pragma alloc_text(INIT,HalpAddRange)
  66. #pragma alloc_text(INIT,HalpRemoveRanges)
  67. #pragma alloc_text(INIT,HalpRemoveRangeList)
  68. #pragma alloc_text(INIT,HalpRemoveRange)
  69. #pragma alloc_text(INIT,HalpConsolidateRanges)
  70. #pragma alloc_text(PAGE,HalpAllocateNewRangeList)
  71. #pragma alloc_text(PAGE,HalpFreeRangeList)
  72. #pragma alloc_text(PAGE,HaliAdjustResourceListRange)
  73. #pragma alloc_text(PAGE,HalpSortRanges)
  74. #pragma alloc_text(PAGE,HalpGetNextSupportedRange)
  75. #endif
  76. #ifdef ALLOC_DATA_PRAGMA
  77. #pragma const_seg("PAGECONST")
  78. #endif
  79. struct {
  80. ULONG Offset;
  81. } const HalpRangeList[] = {
  82. FIELD_OFFSET (SUPPORTED_RANGES, IO),
  83. FIELD_OFFSET (SUPPORTED_RANGES, Memory),
  84. FIELD_OFFSET (SUPPORTED_RANGES, PrefetchMemory),
  85. FIELD_OFFSET (SUPPORTED_RANGES, Dma),
  86. 0,
  87. };
  88. #ifdef ALLOC_DATA_PRAGMA
  89. #pragma const_seg()
  90. #endif
  91. #define RANGE_LIST(a,i) ((PSUPPORTED_RANGE) ((PUCHAR) a + HalpRangeList[i].Offset))
  92. PSUPPORTED_RANGES
  93. HalpMergeRanges (
  94. IN PSUPPORTED_RANGES Parent,
  95. IN PSUPPORTED_RANGES Child
  96. )
  97. /*++
  98. Routine Description:
  99. This function produces a NewList which is a subset of all overlapping
  100. ranges in Parent and Child for all range lists.
  101. The resulting SystemBaseAddresses and SystemAddressSpaces are taken
  102. from the Child supported ranges.
  103. Note: Resulting list needs consolidated
  104. --*/
  105. {
  106. PSUPPORTED_RANGES NewList;
  107. PSUPPORTED_RANGES List1;
  108. NewList = HalpAllocateNewRangeList();
  109. HalpMergeRangeList (&NewList->IO, &Parent->IO, &Child->IO);
  110. HalpMergeRangeList (&NewList->Dma, &Parent->Dma, &Child->Dma);
  111. HalpMergeRangeList (&NewList->Memory, &Parent->Memory, &Child->Memory);
  112. List1 = HalpAllocateNewRangeList();
  113. HalpAddRangeList (&List1->Memory, &Parent->Memory);
  114. HalpAddRangeList (&List1->Memory, &Parent->PrefetchMemory);
  115. HalpMergeRangeList (&NewList->PrefetchMemory, &List1->Memory, &Child->PrefetchMemory);
  116. HalpFreeRangeList (List1);
  117. return NewList;
  118. }
  119. VOID
  120. HalpMergeRangeList (
  121. OUT PSUPPORTED_RANGE NewList,
  122. IN PSUPPORTED_RANGE Parent,
  123. IN PSUPPORTED_RANGE Child
  124. )
  125. /*++
  126. Routine Description:
  127. Completes NewList to be a subset of all overlapping
  128. ranges in the Parent and Child list.
  129. The resulting SystemBaseAddresses and SystemAddressSpaces are
  130. taken from the Child supported ranges.
  131. Note: Resulting list needs consolidated
  132. --*/
  133. {
  134. BOOLEAN HeadCompleted;
  135. PSUPPORTED_RANGE List1, List2;
  136. LONGLONG Base, Limit;
  137. HeadCompleted = FALSE;
  138. for (List1 = Parent; List1; List1 = List1->Next) {
  139. for (List2 = Child; List2; List2 = List2->Next) {
  140. Base = List1->Base;
  141. Limit = List1->Limit;
  142. //
  143. // Clip to range supported by List2
  144. //
  145. if (Base < List2->Base) {
  146. Base = List2->Base;
  147. }
  148. if (Limit > List2->Limit) {
  149. Limit = List2->Limit;
  150. }
  151. //
  152. // If valid range, add it
  153. //
  154. if (Base <= Limit) {
  155. if (HeadCompleted) {
  156. NewList->Next = ExAllocatePoolWithTag (
  157. SPRANGEPOOL,
  158. sizeof (SUPPORTED_RANGE),
  159. HAL_POOL_TAG
  160. );
  161. RtlZeroMemory (NewList->Next, sizeof (SUPPORTED_RANGE));
  162. NewList = NewList->Next;
  163. NewList->Next = NULL;
  164. }
  165. HeadCompleted = TRUE;
  166. NewList->Base = Base;
  167. NewList->Limit = Limit;
  168. NewList->SystemBase = List2->SystemBase;
  169. NewList->SystemAddressSpace = List2->SystemAddressSpace;
  170. }
  171. }
  172. }
  173. }
  174. PSUPPORTED_RANGES
  175. HalpCopyRanges (
  176. PSUPPORTED_RANGES Source
  177. )
  178. /*++
  179. Routine Description:
  180. Builds a copy of the Source list to the destination list.
  181. Note that an invalid entry lands at the begining of the copy, but
  182. that's OK - it will be pulled out at consolidation time.
  183. Note: Resulting list needs consolidated
  184. --*/
  185. {
  186. PSUPPORTED_RANGES Dest;
  187. ULONG i;
  188. Dest = HalpAllocateNewRangeList ();
  189. for (i=0; HalpRangeList[i].Offset; i++) {
  190. HalpAddRangeList (RANGE_LIST(Dest, i), RANGE_LIST(Source, i));
  191. }
  192. return Dest;
  193. }
  194. VOID
  195. HalpAddRangeList (
  196. IN OUT PSUPPORTED_RANGE DRange,
  197. OUT PSUPPORTED_RANGE SRange
  198. )
  199. /*++
  200. Routine Description:
  201. Adds ranges from SRange to DRange.
  202. --*/
  203. {
  204. while (SRange) {
  205. HalpAddRange (
  206. DRange,
  207. SRange->SystemAddressSpace,
  208. SRange->SystemBase,
  209. SRange->Base,
  210. SRange->Limit
  211. );
  212. SRange = SRange->Next;
  213. }
  214. }
  215. VOID
  216. HalpAddRange (
  217. PSUPPORTED_RANGE HRange,
  218. ULONG AddressSpace,
  219. LONGLONG SystemBase,
  220. LONGLONG Base,
  221. LONGLONG Limit
  222. )
  223. /*++
  224. Routine Description:
  225. Adds a range to the supported list. Here we just add the range, if it's
  226. a duplicate it will be removed later at consolidation time.
  227. --*/
  228. {
  229. PSUPPORTED_RANGE Range;
  230. Range = ExAllocatePoolWithTag (
  231. SPRANGEPOOL,
  232. sizeof (SUPPORTED_RANGE),
  233. HAL_POOL_TAG
  234. );
  235. RtlZeroMemory (Range, sizeof (SUPPORTED_RANGE));
  236. Range->Next = HRange->Next;
  237. HRange->Next = Range;
  238. Range->Base = Base;
  239. Range->Limit = Limit;
  240. Range->SystemBase = SystemBase;
  241. Range->SystemAddressSpace = AddressSpace;
  242. }
  243. VOID
  244. HalpRemoveRanges (
  245. IN OUT PSUPPORTED_RANGES Minuend,
  246. IN PSUPPORTED_RANGES Subtrahend
  247. )
  248. /*++
  249. Routine Description:
  250. Returns a list where all ranges from Subtrahend are removed from Minuend.
  251. Note: Resulting list needs consolidated
  252. --*/
  253. {
  254. HalpRemoveRangeList (&Minuend->IO, &Subtrahend->IO);
  255. HalpRemoveRangeList (&Minuend->Dma, &Subtrahend->Dma);
  256. HalpRemoveRangeList (&Minuend->Memory, &Subtrahend->Memory);
  257. HalpRemoveRangeList (&Minuend->Memory, &Subtrahend->PrefetchMemory);
  258. HalpRemoveRangeList (&Minuend->PrefetchMemory, &Subtrahend->PrefetchMemory);
  259. HalpRemoveRangeList (&Minuend->PrefetchMemory, &Subtrahend->Memory);
  260. }
  261. VOID
  262. HalpRemoveRangeList (
  263. IN OUT PSUPPORTED_RANGE Minuend,
  264. IN PSUPPORTED_RANGE Subtrahend
  265. )
  266. /*++
  267. Routine Description:
  268. Removes all ranges from Subtrahend from Minuend
  269. ranges in Source1 and Source1 list
  270. --*/
  271. {
  272. while (Subtrahend) {
  273. HalpRemoveRange (
  274. Minuend,
  275. Subtrahend->Base,
  276. Subtrahend->Limit
  277. );
  278. Subtrahend = Subtrahend->Next;
  279. }
  280. }
  281. VOID
  282. HalpRemoveRange (
  283. PSUPPORTED_RANGE HRange,
  284. LONGLONG Base,
  285. LONGLONG Limit
  286. )
  287. /*++
  288. Routine Description:
  289. Removes the range Base-Limit from the the HRange list
  290. Note: The returned list needs consolidated, as some entries
  291. may be turned into "null ranges".
  292. --*/
  293. {
  294. PSUPPORTED_RANGE Range;
  295. //
  296. // If range isn't a range at all, then nothing to remove
  297. //
  298. if (Limit < Base) {
  299. return ;
  300. }
  301. //
  302. // Clip any area not to include this range
  303. //
  304. for (Range = HRange; Range; Range = Range->Next) {
  305. if (Range->Limit < Range->Base) {
  306. continue;
  307. }
  308. if (Range->Base < Base) {
  309. if (Range->Limit >= Base && Range->Limit <= Limit) {
  310. // truncate
  311. Range->Limit = Base - 1;
  312. }
  313. if (Range->Limit > Limit) {
  314. //
  315. // Target area is contained totally within this area.
  316. // Split into two ranges
  317. //
  318. HalpAddRange (
  319. HRange,
  320. Range->SystemAddressSpace,
  321. Range->SystemBase,
  322. Limit + 1,
  323. Range->Limit
  324. );
  325. Range->Limit = Base - 1;
  326. }
  327. } else {
  328. // Range->Base >= Base
  329. if (Range->Base <= Limit) {
  330. if (Range->Limit <= Limit) {
  331. //
  332. // This range is totally within the target area. Remove it.
  333. // (make it invalid - it will get remove when colsolidated)
  334. //
  335. Range->Base = 1;
  336. Range->Limit = 0;
  337. } else {
  338. // Bump begining
  339. Range->Base = Limit + 1;
  340. }
  341. }
  342. }
  343. }
  344. }
  345. PSUPPORTED_RANGES
  346. HalpConsolidateRanges (
  347. IN OUT PSUPPORTED_RANGES Ranges
  348. )
  349. /*++
  350. Routine Description:
  351. Cleans the Range list. Consolidates overlapping ranges, removes
  352. ranges which don't have any size, etc...
  353. The returned Ranges list is a clean as possible, and is now ready
  354. to be used.
  355. --*/
  356. {
  357. PSUPPORTED_RANGE RangeList, List1, List2;
  358. LONGLONG Base, Limit, SystemBase;
  359. ULONG i, AddressSpace;
  360. LONGLONG l;
  361. ASSERT (Ranges != NULL);
  362. for (i=0; HalpRangeList[i].Offset; i++) {
  363. RangeList = RANGE_LIST(Ranges, i);
  364. //
  365. // Sort the list by base address
  366. //
  367. for (List1 = RangeList; List1; List1 = List1->Next) {
  368. for (List2 = List1->Next; List2; List2 = List2->Next) {
  369. if (List2->Base < List1->Base) {
  370. Base = List1->Base;
  371. Limit = List1->Limit;
  372. SystemBase = List1->SystemBase;
  373. AddressSpace = List1->SystemAddressSpace;
  374. List1->Base = List2->Base;
  375. List1->Limit = List2->Limit;
  376. List1->SystemBase = List2->SystemBase;
  377. List1->SystemAddressSpace = List2->SystemAddressSpace;
  378. List2->Base = Base;
  379. List2->Limit = Limit;
  380. List2->SystemBase = SystemBase;
  381. List2->SystemAddressSpace = AddressSpace;
  382. }
  383. }
  384. }
  385. //
  386. // Check for adjacent/overlapping ranges and combined them
  387. //
  388. List1 = RangeList;
  389. while (List1 && List1->Next) {
  390. if (List1->Limit < List1->Base) {
  391. //
  392. // This range's limit is less then it's base. This
  393. // entry doesn't reprent anything uasable, remove it.
  394. //
  395. List2 = List1->Next;
  396. List1->Next = List2->Next;
  397. List1->Base = List2->Base;
  398. List1->Limit = List2->Limit;
  399. List1->SystemBase = List2->SystemBase;
  400. List1->SystemAddressSpace = List2->SystemAddressSpace;
  401. ExFreePool (List2);
  402. continue;
  403. }
  404. l = List1->Limit + 1;
  405. if (l > List1->Limit && l >= List1->Next->Base &&
  406. (List1->SystemBase == List1->Next->SystemBase)) {
  407. //
  408. // Overlapping. Combine them.
  409. //
  410. List2 = List1->Next;
  411. List1->Next = List2->Next;
  412. if (List2->Limit > List1->Limit) {
  413. List1->Limit = List2->Limit;
  414. ASSERT (List1->SystemAddressSpace == List2->SystemAddressSpace);
  415. }
  416. ExFreePool (List2);
  417. continue ;
  418. }
  419. List1 = List1->Next;
  420. }
  421. //
  422. // If the last range is invalid, and it's not the only
  423. // thing in the list - remove it
  424. //
  425. if (List1 != RangeList && List1->Limit < List1->Base) {
  426. for (List2=RangeList; List2->Next != List1; List2 = List2->Next) ;
  427. List2->Next = NULL;
  428. ExFreePool (List1);
  429. }
  430. }
  431. return Ranges;
  432. }
  433. PSUPPORTED_RANGES
  434. HalpAllocateNewRangeList (
  435. VOID
  436. )
  437. /*++
  438. Routine Description:
  439. Allocates a range list
  440. --*/
  441. {
  442. PSUPPORTED_RANGES RangeList;
  443. ULONG i;
  444. RangeList = (PSUPPORTED_RANGES) ExAllocatePoolWithTag (
  445. SPRANGEPOOL,
  446. sizeof (SUPPORTED_RANGES),
  447. HAL_POOL_TAG
  448. );
  449. RtlZeroMemory (RangeList, sizeof (SUPPORTED_RANGES));
  450. RangeList->Version = BUS_SUPPORTED_RANGE_VERSION;
  451. for (i=0; HalpRangeList[i].Offset; i++) {
  452. // Limit set to zero, set initial base to 1
  453. RANGE_LIST(RangeList, i)->Base = 1;
  454. }
  455. return RangeList;
  456. }
  457. VOID
  458. HalpFreeRangeList (
  459. PSUPPORTED_RANGES Ranges
  460. )
  461. /*++
  462. Routine Description:
  463. Frees a range list which was allocated via HalpAllocateNewRangeList, and
  464. extended / modified via the generic support functions.
  465. --*/
  466. {
  467. PSUPPORTED_RANGE Entry, NextEntry;
  468. ULONG i;
  469. for (i=0; HalpRangeList[i].Offset; i++) {
  470. Entry = RANGE_LIST(Ranges, i)->Next;
  471. while (Entry) {
  472. NextEntry = Entry->Next;
  473. ExFreePool (Entry);
  474. Entry = NextEntry;
  475. }
  476. }
  477. ExFreePool (Ranges);
  478. }
  479. #if DBG
  480. STATIC VOID
  481. HalpDisplayAddressRange (
  482. PSUPPORTED_RANGE Address,
  483. PUCHAR String
  484. )
  485. /*++
  486. Routine Description:
  487. Debugging code. Used only by HalpDisplayAllBusRanges
  488. --*/
  489. {
  490. ULONG i;
  491. i = 0;
  492. while (Address) {
  493. if (i == 0) {
  494. DbgPrint (String);
  495. i = 3;
  496. }
  497. i -= 1;
  498. DbgPrint (" %x:%08x - %x:%08x ",
  499. (ULONG) (Address->Base >> 32),
  500. (ULONG) (Address->Base),
  501. (ULONG) (Address->Limit >> 32),
  502. (ULONG) (Address->Limit)
  503. );
  504. Address = Address->Next;
  505. }
  506. }
  507. VOID
  508. HalpDisplayAllBusRanges (
  509. VOID
  510. )
  511. /*++
  512. Routine Description:
  513. Debugging code. Displays the current supported range information
  514. for all the registered buses in the system.
  515. --*/
  516. {
  517. PSUPPORTED_RANGES Addresses;
  518. PBUS_HANDLER Bus;
  519. PUCHAR p;
  520. ULONG i, j;
  521. DbgPrint ("\nHAL - dumping all supported bus ranges");
  522. for (i=0; i < MaximumInterfaceType; i++) {
  523. for (j=0; Bus = HaliHandlerForBus (i, j); j++) {
  524. Addresses = Bus->BusAddresses;
  525. if (Addresses) {
  526. p = NULL;
  527. switch (Bus->InterfaceType) {
  528. case Internal: p = "Internal"; break;
  529. case Isa: p = "Isa"; break;
  530. case Eisa: p = "Eisa"; break;
  531. case PCIBus: p = "PCI"; break;
  532. }
  533. if (p) {
  534. DbgPrint ("\n%s %d", p, Bus->BusNumber);
  535. } else {
  536. DbgPrint ("\nBus-%d %d", Bus->InterfaceType, Bus->BusNumber);
  537. }
  538. HalpDisplayAddressRange (&Addresses->IO, "\n IO......:");
  539. HalpDisplayAddressRange (&Addresses->Memory, "\n Memory..:");
  540. HalpDisplayAddressRange (&Addresses->PrefetchMemory,"\n PFMemory:");
  541. HalpDisplayAddressRange (&Addresses->Dma, "\n Dma.....:");
  542. DbgPrint ("\n");
  543. }
  544. }
  545. }
  546. }
  547. #endif
  548. NTSTATUS
  549. HaliAdjustResourceListRange (
  550. IN PSUPPORTED_RANGES SRanges,
  551. IN PSUPPORTED_RANGE InterruptRange,
  552. IN OUT PIO_RESOURCE_REQUIREMENTS_LIST *pResourceList
  553. )
  554. /*++
  555. Routine Description:
  556. This functions takes an IO_RESOURCE_REQUIREMENT_LIST and
  557. adjusts it such that all ranges in the list fit in the
  558. ranges specified by SRanges & InterruptRange.
  559. This function is used by some HALs to clip the possible
  560. settings to be contained on what the particular bus supports
  561. in reponse to a HalAdjustResourceList call.
  562. Arguments:
  563. SRanges - Valid IO, Memory, Prefetch Memory, and DMA ranges.
  564. InterruptRange - Valid InterruptRanges
  565. pResourceList - The resource requirements list which needs to
  566. be adjusted to only contain the ranges as
  567. described by SRanges & InterruptRange.
  568. Return Value:
  569. STATUS_SUCCESS or an appropiate error return.
  570. --*/
  571. {
  572. PIO_RESOURCE_REQUIREMENTS_LIST InCompleteList, OutCompleteList;
  573. PIO_RESOURCE_LIST InResourceList, OutResourceList;
  574. PIO_RESOURCE_DESCRIPTOR HeadOutDesc, SetDesc;
  575. NRPARAMS Pos;
  576. ULONG len, alt, cnt, i;
  577. ULONG icnt;
  578. //
  579. // Sanity check
  580. //
  581. if (!SRanges || SRanges->Version != BUS_SUPPORTED_RANGE_VERSION) {
  582. return STATUS_INVALID_PARAMETER;
  583. }
  584. //
  585. // If SupportedRanges aren't sorted, sort them and get the
  586. // number of ranges for each type
  587. //
  588. if (!SRanges->Sorted) {
  589. SRanges->NoIO = HalpSortRanges (&SRanges->IO);
  590. SRanges->NoMemory = HalpSortRanges (&SRanges->Memory);
  591. SRanges->NoPrefetchMemory = HalpSortRanges (&SRanges->PrefetchMemory);
  592. SRanges->NoDma = HalpSortRanges (&SRanges->Dma);
  593. SRanges->Sorted = TRUE;
  594. }
  595. icnt = HalpSortRanges (InterruptRange);
  596. InCompleteList = *pResourceList;
  597. len = InCompleteList->ListSize;
  598. //
  599. // Scan input list - verify revision #'s, and increase len varible
  600. // by amount output list may increase.
  601. //
  602. i = 1;
  603. InResourceList = InCompleteList->List;
  604. for (alt=0; alt < InCompleteList->AlternativeLists; alt++) {
  605. if (InResourceList->Version != 1 || InResourceList->Revision < 1) {
  606. return STATUS_INVALID_PARAMETER;
  607. }
  608. Pos.InDesc = InResourceList->Descriptors;
  609. for (cnt = InResourceList->Count; cnt; cnt--) {
  610. switch (Pos.InDesc->Type) {
  611. case CmResourceTypeInterrupt: i += icnt; break;
  612. case CmResourceTypePort: i += SRanges->NoIO; break;
  613. case CmResourceTypeDma: i += SRanges->NoDma; break;
  614. case CmResourceTypeMemory:
  615. i += SRanges->NoMemory;
  616. if (Pos.InDesc->Flags & CM_RESOURCE_MEMORY_PREFETCHABLE) {
  617. i += SRanges->NoPrefetchMemory;
  618. }
  619. break;
  620. default:
  621. return STATUS_INVALID_PARAMETER;
  622. }
  623. // take one off for the original which is already accounted for in 'len'
  624. i -= 1;
  625. // Next descriptor
  626. Pos.InDesc++;
  627. }
  628. // Next Resource List
  629. InResourceList = (PIO_RESOURCE_LIST) Pos.InDesc;
  630. }
  631. len += i * sizeof (IO_RESOURCE_DESCRIPTOR);
  632. //
  633. // Allocate output list
  634. //
  635. OutCompleteList = (PIO_RESOURCE_REQUIREMENTS_LIST)
  636. ExAllocatePoolWithTag (PagedPool,
  637. len,
  638. ' laH');
  639. if (!OutCompleteList) {
  640. return STATUS_INSUFFICIENT_RESOURCES;
  641. }
  642. RtlZeroMemory (OutCompleteList, len);
  643. //
  644. // Walk each ResourceList and build output structure
  645. //
  646. InResourceList = InCompleteList->List;
  647. *OutCompleteList = *InCompleteList;
  648. OutResourceList = OutCompleteList->List;
  649. for (alt=0; alt < InCompleteList->AlternativeLists; alt++) {
  650. OutResourceList->Version = 1;
  651. OutResourceList->Revision = 1;
  652. Pos.InDesc = InResourceList->Descriptors;
  653. Pos.OutDesc = OutResourceList->Descriptors;
  654. HeadOutDesc = Pos.OutDesc;
  655. for (cnt = InResourceList->Count; cnt; cnt--) {
  656. //
  657. // Limit desctiptor to be with the buses supported ranges
  658. //
  659. Pos.DescOpt = Pos.InDesc->Option;
  660. Pos.AnotherListPending = FALSE;
  661. switch (Pos.InDesc->Type) {
  662. case CmResourceTypePort:
  663. //
  664. // Get supported IO ranges
  665. //
  666. Pos.CurrentPosition = &SRanges->IO;
  667. do {
  668. SetDesc = HalpGetNextSupportedRange (
  669. Pos.InDesc->u.Port.MinimumAddress.QuadPart,
  670. Pos.InDesc->u.Port.MaximumAddress.QuadPart,
  671. &Pos
  672. );
  673. if (SetDesc) {
  674. SetDesc->u.Port.MinimumAddress.QuadPart = Pos.Base;
  675. SetDesc->u.Port.MaximumAddress.QuadPart = Pos.Limit;
  676. }
  677. } while (SetDesc) ;
  678. break;
  679. case CmResourceTypeInterrupt:
  680. //
  681. // Get supported Interrupt ranges
  682. //
  683. Pos.CurrentPosition = InterruptRange;
  684. do {
  685. SetDesc = HalpGetNextSupportedRange (
  686. Pos.InDesc->u.Interrupt.MinimumVector,
  687. Pos.InDesc->u.Interrupt.MaximumVector,
  688. &Pos
  689. );
  690. if (SetDesc) {
  691. SetDesc->u.Interrupt.MinimumVector = (ULONG) Pos.Base;
  692. SetDesc->u.Interrupt.MaximumVector = (ULONG) Pos.Limit;
  693. }
  694. } while (SetDesc) ;
  695. break;
  696. case CmResourceTypeMemory:
  697. //
  698. // Get supported memory ranges
  699. //
  700. if (Pos.InDesc->Flags & CM_RESOURCE_MEMORY_PREFETCHABLE) {
  701. //
  702. // This is a Prefetchable range.
  703. // First add in any supported prefetchable ranges, then
  704. // add in any regualer supported ranges
  705. //
  706. Pos.AnotherListPending = TRUE;
  707. Pos.CurrentPosition = &SRanges->PrefetchMemory;
  708. do {
  709. SetDesc = HalpGetNextSupportedRange (
  710. Pos.InDesc->u.Memory.MinimumAddress.QuadPart,
  711. Pos.InDesc->u.Memory.MaximumAddress.QuadPart,
  712. &Pos
  713. );
  714. if (SetDesc) {
  715. SetDesc->u.Memory.MinimumAddress.QuadPart = Pos.Base;
  716. SetDesc->u.Memory.MaximumAddress.QuadPart = Pos.Limit;
  717. SetDesc->Option |= IO_RESOURCE_PREFERRED;
  718. }
  719. } while (SetDesc) ;
  720. Pos.AnotherListPending = FALSE;
  721. }
  722. //
  723. // Add in supported bus memory ranges
  724. //
  725. Pos.CurrentPosition = &SRanges->Memory;
  726. do {
  727. SetDesc = HalpGetNextSupportedRange (
  728. Pos.InDesc->u.Memory.MinimumAddress.QuadPart,
  729. Pos.InDesc->u.Memory.MaximumAddress.QuadPart,
  730. &Pos
  731. );
  732. if (SetDesc) {
  733. SetDesc->u.Memory.MinimumAddress.QuadPart = Pos.Base;
  734. SetDesc->u.Memory.MaximumAddress.QuadPart = Pos.Limit;
  735. }
  736. } while (SetDesc);
  737. break;
  738. case CmResourceTypeDma:
  739. //
  740. // Get supported DMA ranges
  741. //
  742. Pos.CurrentPosition = &SRanges->Dma;
  743. do {
  744. SetDesc = HalpGetNextSupportedRange (
  745. Pos.InDesc->u.Dma.MinimumChannel,
  746. Pos.InDesc->u.Dma.MaximumChannel,
  747. &Pos
  748. );
  749. if (SetDesc) {
  750. SetDesc->u.Dma.MinimumChannel = (ULONG) Pos.Base;
  751. SetDesc->u.Dma.MaximumChannel = (ULONG) Pos.Limit;
  752. }
  753. } while (SetDesc) ;
  754. break;
  755. #if DBG
  756. default:
  757. DbgPrint ("HalAdjustResourceList: Unkown resource type\n");
  758. break;
  759. #endif
  760. }
  761. //
  762. // Next descriptor
  763. //
  764. Pos.InDesc++;
  765. }
  766. OutResourceList->Count = (ULONG)(Pos.OutDesc - HeadOutDesc);
  767. //
  768. // Next Resource List
  769. //
  770. InResourceList = (PIO_RESOURCE_LIST) Pos.InDesc;
  771. OutResourceList = (PIO_RESOURCE_LIST) Pos.OutDesc;
  772. }
  773. //
  774. // Free input list, and return output list
  775. //
  776. ExFreePool (InCompleteList);
  777. OutCompleteList->ListSize = (ULONG) ((PUCHAR) OutResourceList - (PUCHAR) OutCompleteList);
  778. *pResourceList = OutCompleteList;
  779. return STATUS_SUCCESS;
  780. }
  781. STATIC PIO_RESOURCE_DESCRIPTOR
  782. HalpGetNextSupportedRange (
  783. IN LONGLONG MinimumAddress,
  784. IN LONGLONG MaximumAddress,
  785. IN OUT PNRPARAMS Pos
  786. )
  787. /*++
  788. Routine Description:
  789. Support function for HaliAdjustResourceListRange.
  790. Returns the next supported range in the area passed in.
  791. Arguments:
  792. MinimumAddress
  793. MaximumAddress - Min & Max address of a range which needs
  794. to be clipped to match that of the supported
  795. ranges of the current bus.
  796. Pos - describes the current postion
  797. Return Value:
  798. NULL is no more returned ranges
  799. Otherwise, the IO_RESOURCE_DESCRIPTOR which needs to be set
  800. with the matching range returned in Pos.
  801. --*/
  802. {
  803. LONGLONG Base, Limit;
  804. //
  805. // Find next range which is supported
  806. //
  807. Base = MinimumAddress;
  808. Limit = MaximumAddress;
  809. while (Pos->CurrentPosition) {
  810. Pos->Base = Base;
  811. Pos->Limit = Limit;
  812. //
  813. // Clip to current range
  814. //
  815. if (Pos->Base < Pos->CurrentPosition->Base) {
  816. Pos->Base = Pos->CurrentPosition->Base;
  817. }
  818. if (Pos->Limit > Pos->CurrentPosition->Limit) {
  819. Pos->Limit = Pos->CurrentPosition->Limit;
  820. }
  821. //
  822. // set position to next range
  823. //
  824. Pos->CurrentPosition = Pos->CurrentPosition->Next;
  825. //
  826. // If valid range, return it
  827. //
  828. if (Pos->Base <= Pos->Limit) {
  829. *Pos->OutDesc = *Pos->InDesc;
  830. Pos->OutDesc->Option = Pos->DescOpt;
  831. //
  832. // next descriptor (if any) is an alternative
  833. // to the descriptor being returned now
  834. //
  835. Pos->OutDesc += 1;
  836. Pos->DescOpt |= IO_RESOURCE_ALTERNATIVE;
  837. return Pos->OutDesc - 1;
  838. }
  839. }
  840. //
  841. // There's no overlapping range. If this descriptor is
  842. // not an alternative and this descriptor is not going to
  843. // be processed by another range list, then return
  844. // a descriptor which can't be satisified.
  845. //
  846. if (!(Pos->DescOpt & IO_RESOURCE_ALTERNATIVE) &&
  847. Pos->AnotherListPending == FALSE) {
  848. #if DBG
  849. DbgPrint ("HAL: returning impossible range\n");
  850. #endif
  851. Pos->Base = MinimumAddress;
  852. Pos->Limit = Pos->Base - 1;
  853. if (Pos->Base == 0) { // if wrapped, fix it
  854. Pos->Base = 1;
  855. Pos->Limit = 0;
  856. }
  857. *Pos->OutDesc = *Pos->InDesc;
  858. Pos->OutDesc->Option = Pos->DescOpt;
  859. Pos->OutDesc += 1;
  860. Pos->DescOpt |= IO_RESOURCE_ALTERNATIVE;
  861. return Pos->OutDesc - 1;
  862. }
  863. //
  864. // No range found (or no more ranges)
  865. //
  866. return NULL;
  867. }
  868. STATIC ULONG
  869. HalpSortRanges (
  870. IN PSUPPORTED_RANGE RangeList
  871. )
  872. /*++
  873. Routine Description:
  874. Support function for HaliAdjustResourceListRange.
  875. Sorts a supported range list into decending order.
  876. Arguments:
  877. pRange - List to sort
  878. Return Value:
  879. --*/
  880. {
  881. ULONG cnt;
  882. LONGLONG hldBase, hldLimit, hldSystemBase;
  883. PSUPPORTED_RANGE Range1, Range2;
  884. //
  885. // Sort it
  886. //
  887. for (Range1 = RangeList; Range1; Range1 = Range1->Next) {
  888. for (Range2 = Range1->Next; Range2; Range2 = Range2->Next) {
  889. if (Range2->Base > Range1->Base) {
  890. hldBase = Range1->Base;
  891. hldLimit = Range1->Limit;
  892. hldSystemBase = Range1->SystemBase;
  893. Range1->Base = Range2->Base;
  894. Range1->Limit = Range2->Limit;
  895. Range1->SystemBase = Range2->SystemBase;
  896. Range2->Base = hldBase;
  897. Range2->Limit = hldLimit;
  898. Range2->SystemBase = hldSystemBase;
  899. }
  900. }
  901. }
  902. //
  903. // Count the number of ranges
  904. //
  905. cnt = 0;
  906. for (Range1 = RangeList; Range1; Range1 = Range1->Next) {
  907. cnt += 1;
  908. }
  909. return cnt;
  910. }