3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/hal/x86/dma.c
6 * PURPOSE: DMA functions
7 * PROGRAMMERS: David Welch (welch@mcmail.com)
8 * Filip Navara (navaraf@reactos.com)
14 * @page DMA Implementation Notes
20 * Abstract encapsulation of physically contiguous buffer that resides
21 * in memory accessible by both the DMA device / controller and the system.
22 * The map registers are allocated and distributed on demand and are
25 * The actual use of map registers is to allow transfers from/to buffer
26 * located in physical memory at address inaccessible by the DMA device /
27 * controller directly. For such transfers the map register buffers
28 * are used as intermediate data storage.
32 * A container for map registers (typically corresponding to one physical
33 * bus connection type). There can be master adapters for 24-bit address
34 * ranges, 32-bit address ranges, etc. Every time a new DMA adapter is
35 * created it's associated with a corresponding master adapter that
36 * is used for any map register allocation requests.
38 * - Bus-master / Slave DMA
40 * Slave DMA is term used for DMA transfers done by the system (E)ISA
41 * controller as opposed to transfers mastered by the device itself
44 * For slave DMA special care is taken to actually access the system
45 * controller and handle the transfers. The relevant code is in
46 * HalpDmaInitializeEisaAdapter, HalReadDmaCounter, IoFlushAdapterBuffers
51 * - Allocation of map registers
53 * Initial set of map registers is allocated on the system start to
54 * ensure that low memory won't get filled up later. Additional map
55 * registers are allocated as needed by HalpGrowMapBuffers. This
56 * routine is called on two places:
58 * - HalGetAdapter, since we're at PASSIVE_LEVEL and it's known that
59 * more map registers will probably be needed.
60 * - IoAllocateAdapterChannel (indirectly using HalpGrowMapBufferWorker
61 * since we're at DISPATCH_LEVEL and call HalpGrowMapBuffers directly)
62 * when no more map registers are free.
64 * Note that even if no more map registers can be allocated it's not
65 * the end of the world. The adapters waiting for free map registers
66 * are queued in the master adapter's queue and once one driver hands
67 * back it's map registers (using IoFreeMapRegisters or indirectly using
68 * the execution routine callback in IoAllocateAdapterChannel) the
69 * queue gets processed and the map registers are reassigned.
72 /* INCLUDES *****************************************************************/
78 static KEVENT HalpDmaLock
;
79 static LIST_ENTRY HalpDmaAdapterList
;
80 static PADAPTER_OBJECT HalpEisaAdapter
[8];
81 static BOOLEAN HalpEisaDma
;
82 static PADAPTER_OBJECT HalpMasterAdapter
;
84 static const ULONG_PTR HalpEisaPortPage
[8] = {
85 FIELD_OFFSET(DMA_PAGE
, Channel0
),
86 FIELD_OFFSET(DMA_PAGE
, Channel1
),
87 FIELD_OFFSET(DMA_PAGE
, Channel2
),
88 FIELD_OFFSET(DMA_PAGE
, Channel3
),
90 FIELD_OFFSET(DMA_PAGE
, Channel5
),
91 FIELD_OFFSET(DMA_PAGE
, Channel6
),
92 FIELD_OFFSET(DMA_PAGE
, Channel7
)
95 static DMA_OPERATIONS HalpDmaOperations
= {
96 sizeof(DMA_OPERATIONS
),
97 (PPUT_DMA_ADAPTER
)HalPutDmaAdapter
,
98 (PALLOCATE_COMMON_BUFFER
)HalAllocateCommonBuffer
,
99 (PFREE_COMMON_BUFFER
)HalFreeCommonBuffer
,
100 (PALLOCATE_ADAPTER_CHANNEL
)IoAllocateAdapterChannel
,
101 (PFLUSH_ADAPTER_BUFFERS
)IoFlushAdapterBuffers
,
102 (PFREE_ADAPTER_CHANNEL
)IoFreeAdapterChannel
,
103 (PFREE_MAP_REGISTERS
)IoFreeMapRegisters
,
104 (PMAP_TRANSFER
)IoMapTransfer
,
105 (PGET_DMA_ALIGNMENT
)HalpDmaGetDmaAlignment
,
106 (PREAD_DMA_COUNTER
)HalReadDmaCounter
,
107 /* FIXME: Implement the S/G funtions. */
108 NULL
/*(PGET_SCATTER_GATHER_LIST)HalGetScatterGatherList*/,
109 NULL
/*(PPUT_SCATTER_GATHER_LIST)HalPutScatterGatherList*/,
110 NULL
/*(PCALCULATE_SCATTER_GATHER_LIST_SIZE)HalCalculateScatterGatherListSize*/,
111 NULL
/*(PBUILD_SCATTER_GATHER_LIST)HalBuildScatterGatherList*/,
112 NULL
/*(PBUILD_MDL_FROM_SCATTER_GATHER_LIST)HalBuildMdlFromScatterGatherList*/
115 #define MAX_MAP_REGISTERS 64
117 #define TAG_DMA TAG('D','M','A',' ')
119 /* FUNCTIONS *****************************************************************/
125 * Check if Extended DMA is available. We're just going to do a random
129 WRITE_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
.Channel2
), 0x2A);
130 if (READ_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
.Channel2
)) == 0x2A)
134 * Intialize all the global variables and allocate master adapter with
138 InitializeListHead(&HalpDmaAdapterList
);
139 KeInitializeEvent(&HalpDmaLock
, NotificationEvent
, TRUE
);
141 HalpMasterAdapter
= HalpDmaAllocateMasterAdapter();
144 * Setup the HalDispatchTable callback for creating PnP DMA adapters. It's
145 * used by IoGetDmaAdapter in the kernel.
148 HalGetDmaAdapter
= HalpGetDmaAdapter
;
152 * @name HalpGetAdapterMaximumPhysicalAddress
154 * Get the maximum physical address acceptable by the device represented
155 * by the passed DMA adapter.
158 PHYSICAL_ADDRESS STDCALL
159 HalpGetAdapterMaximumPhysicalAddress(
160 IN PADAPTER_OBJECT AdapterObject
)
162 PHYSICAL_ADDRESS HighestAddress
;
164 if (AdapterObject
->MasterDevice
)
166 if (AdapterObject
->Dma64BitAddresses
)
168 HighestAddress
.QuadPart
= 0xFFFFFFFFFFFFFFFFULL
;
169 return HighestAddress
;
171 else if (AdapterObject
->Dma32BitAddresses
)
173 HighestAddress
.QuadPart
= 0xFFFFFFFF;
174 return HighestAddress
;
178 HighestAddress
.QuadPart
= 0xFFFFFF;
179 return HighestAddress
;
183 * @name HalpGrowMapBuffers
185 * Allocate initial, or additional, map buffers for DMA master adapter.
187 * @param MasterAdapter
188 * DMA master adapter to allocate buffers for.
189 * @param SizeOfMapBuffers
190 * Size of the map buffers to allocate (not including the size
191 * already allocated).
196 IN PADAPTER_OBJECT AdapterObject
,
197 IN ULONG SizeOfMapBuffers
)
199 PVOID VirtualAddress
;
200 PHYSICAL_ADDRESS PhysicalAddress
;
201 PHYSICAL_ADDRESS HighestAcceptableAddress
;
202 PHYSICAL_ADDRESS LowestAcceptableAddress
;
203 PHYSICAL_ADDRESS BoundryAddressMultiple
;
205 ULONG MapRegisterCount
;
207 /* FIXME: Check if enough map register slots are available. */
209 MapRegisterCount
= BYTES_TO_PAGES(SizeOfMapBuffers
);
212 * Allocate memory for the new map registers. For 32-bit adapters we use
213 * two passes in order not to waste scare resource (low memory).
216 HighestAcceptableAddress
=
217 HalpGetAdapterMaximumPhysicalAddress(AdapterObject
);
218 LowestAcceptableAddress
.HighPart
= 0;
219 LowestAcceptableAddress
.LowPart
=
220 HighestAcceptableAddress
.LowPart
== 0xFFFFFFFF ? 0x1000000 : 0;
221 BoundryAddressMultiple
.QuadPart
= 0;
223 VirtualAddress
= MmAllocateContiguousMemorySpecifyCache(
224 MapRegisterCount
<< PAGE_SHIFT
, LowestAcceptableAddress
,
225 HighestAcceptableAddress
, BoundryAddressMultiple
, MmNonCached
);
227 if (VirtualAddress
== NULL
&& LowestAcceptableAddress
.LowPart
!= 0)
229 LowestAcceptableAddress
.LowPart
= 0;
230 VirtualAddress
= MmAllocateContiguousMemorySpecifyCache(
231 MapRegisterCount
<< PAGE_SHIFT
, LowestAcceptableAddress
,
232 HighestAcceptableAddress
, BoundryAddressMultiple
, MmNonCached
);
235 if (VirtualAddress
== NULL
)
239 * All the following must be done with the master adapter lock held
240 * to prevent corruption.
243 OldIrql
= KfAcquireSpinLock(&AdapterObject
->SpinLock
);
246 * Setup map register entries for the buffer allocated. Each entry has
247 * a virtual and physical address and corresponds to PAGE_SIZE large
251 if (MapRegisterCount
> 0)
253 PMAP_REGISTER_ENTRY CurrentEntry
, PreviousEntry
;
255 CurrentEntry
= AdapterObject
->MapRegisterBase
+
256 AdapterObject
->NumberOfMapRegisters
;
260 * Leave one entry free for every non-contiguous memory region
261 * in the map register bitmap. This ensures that we can search
262 * using RtlFindClearBits for contiguous map register regions.
264 * Also for non-EISA DMA leave one free entry for every 64Kb
265 * break, because the DMA controller can handle only coniguous
269 if (CurrentEntry
!= AdapterObject
->MapRegisterBase
)
271 PreviousEntry
= CurrentEntry
- 1;
272 if (PreviousEntry
->PhysicalAddress
.LowPart
+ PAGE_SIZE
==
273 PhysicalAddress
.LowPart
)
277 if ((PreviousEntry
->PhysicalAddress
.LowPart
^
278 PhysicalAddress
.LowPart
) & 0xFFFF0000)
281 AdapterObject
->NumberOfMapRegisters
++;
288 AdapterObject
->NumberOfMapRegisters
++;
292 RtlClearBit(AdapterObject
->MapRegisters
,
293 CurrentEntry
- AdapterObject
->MapRegisterBase
);
294 CurrentEntry
->VirtualAddress
= VirtualAddress
;
295 CurrentEntry
->PhysicalAddress
= PhysicalAddress
;
297 PhysicalAddress
.LowPart
+= PAGE_SIZE
;
298 VirtualAddress
= (PVOID
)((ULONG_PTR
)VirtualAddress
+ PAGE_SIZE
);
301 AdapterObject
->NumberOfMapRegisters
++;
304 while (MapRegisterCount
!= 0);
307 KfReleaseSpinLock(&AdapterObject
->SpinLock
, OldIrql
);
313 * @name HalpDmaAllocateMasterAdapter
315 * Helper routine to allocate and initialize master adapter object and it's
316 * associated map register buffers.
321 PADAPTER_OBJECT STDCALL
322 HalpDmaAllocateMasterAdapter(VOID
)
324 PADAPTER_OBJECT MasterAdapter
;
325 ULONG Size
, SizeOfBitmap
;
327 SizeOfBitmap
= MAX_MAP_REGISTERS
;
328 Size
= sizeof(ADAPTER_OBJECT
);
329 Size
+= sizeof(RTL_BITMAP
);
330 Size
+= (SizeOfBitmap
+ 7) >> 3;
332 MasterAdapter
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_DMA
);
333 if (MasterAdapter
== NULL
)
336 RtlZeroMemory(MasterAdapter
, Size
);
338 KeInitializeSpinLock(&MasterAdapter
->SpinLock
);
339 InitializeListHead(&MasterAdapter
->AdapterQueue
);
341 MasterAdapter
->MapRegisters
= (PVOID
)(MasterAdapter
+ 1);
343 MasterAdapter
->MapRegisters
,
344 (PULONG
)(MasterAdapter
->MapRegisters
+ 1),
346 RtlSetAllBits(MasterAdapter
->MapRegisters
);
347 MasterAdapter
->NumberOfMapRegisters
= 0;
348 MasterAdapter
->CommittedMapRegisters
= 0;
350 MasterAdapter
->MapRegisterBase
= ExAllocatePoolWithTag(
352 SizeOfBitmap
* sizeof(MAP_REGISTER_ENTRY
),
354 if (MasterAdapter
->MapRegisterBase
== NULL
)
356 ExFreePool(MasterAdapter
);
360 RtlZeroMemory(MasterAdapter
->MapRegisterBase
,
361 SizeOfBitmap
* sizeof(MAP_REGISTER_ENTRY
));
362 if (!HalpGrowMapBuffers(MasterAdapter
, 0x10000))
364 ExFreePool(MasterAdapter
);
368 return MasterAdapter
;
372 * @name HalpDmaAllocateChildAdapter
374 * Helper routine of HalGetAdapter. Allocate child adapter object and
375 * fill out some basic fields.
380 PADAPTER_OBJECT STDCALL
381 HalpDmaAllocateChildAdapter(
382 ULONG NumberOfMapRegisters
,
383 PDEVICE_DESCRIPTION DeviceDescription
)
385 PADAPTER_OBJECT AdapterObject
;
386 OBJECT_ATTRIBUTES ObjectAttributes
;
390 InitializeObjectAttributes(
393 OBJ_KERNEL_HANDLE
| OBJ_PERMANENT
,
397 Status
= ObCreateObject(
403 sizeof(ADAPTER_OBJECT
),
406 (PVOID
)&AdapterObject
);
407 if (!NT_SUCCESS(Status
))
410 Status
= ObReferenceObjectByPointer(
412 FILE_READ_DATA
| FILE_WRITE_DATA
,
415 if (!NT_SUCCESS(Status
))
418 RtlZeroMemory(AdapterObject
, sizeof(ADAPTER_OBJECT
));
420 Status
= ObInsertObject(
423 FILE_READ_DATA
| FILE_WRITE_DATA
,
427 if (!NT_SUCCESS(Status
))
432 AdapterObject
->DmaHeader
.Version
= DeviceDescription
->Version
;
433 AdapterObject
->DmaHeader
.Size
= sizeof(ADAPTER_OBJECT
);
434 AdapterObject
->DmaHeader
.DmaOperations
= &HalpDmaOperations
;
435 AdapterObject
->MapRegistersPerChannel
= 1;
436 AdapterObject
->Dma32BitAddresses
= DeviceDescription
->Dma32BitAddresses
;
437 AdapterObject
->ChannelNumber
= 0xFF;
438 AdapterObject
->MasterAdapter
= HalpMasterAdapter
;
439 KeInitializeDeviceQueue(&AdapterObject
->ChannelWaitQueue
);
441 return AdapterObject
;
445 * @name HalpDmaInitializeEisaAdapter
447 * Setup DMA modes and extended modes for (E)ISA DMA adapter object.
451 HalpDmaInitializeEisaAdapter(
452 PADAPTER_OBJECT AdapterObject
,
453 PDEVICE_DESCRIPTION DeviceDescription
)
456 DMA_MODE DmaMode
= { Byte
: 0 };
457 DMA_EXTENDED_MODE ExtendedMode
= { Byte
: 0 };
460 Controller
= (DeviceDescription
->DmaChannel
& 4) ? 2 : 1;
463 AdapterBaseVa
= (PVOID
)FIELD_OFFSET(EISA_CONTROL
, DmaController1
);
465 AdapterBaseVa
= (PVOID
)FIELD_OFFSET(EISA_CONTROL
, DmaController2
);
467 AdapterObject
->AdapterNumber
= Controller
;
468 AdapterObject
->ChannelNumber
= DeviceDescription
->DmaChannel
& 3;
469 AdapterObject
->PagePort
= (PUCHAR
)HalpEisaPortPage
[DeviceDescription
->DmaChannel
];
470 AdapterObject
->Width16Bits
= FALSE
;
471 AdapterObject
->AdapterBaseVa
= AdapterBaseVa
;
475 ExtendedMode
.ChannelNumber
= AdapterObject
->ChannelNumber
;
477 switch (DeviceDescription
->DmaSpeed
)
479 case Compatible
: ExtendedMode
.TimingMode
= COMPATIBLE_TIMING
; break;
480 case TypeA
: ExtendedMode
.TimingMode
= TYPE_A_TIMING
; break;
481 case TypeB
: ExtendedMode
.TimingMode
= TYPE_B_TIMING
; break;
482 case TypeC
: ExtendedMode
.TimingMode
= BURST_TIMING
; break;
487 switch (DeviceDescription
->DmaWidth
)
489 case Width8Bits
: ExtendedMode
.TransferSize
= B_8BITS
; break;
490 case Width16Bits
: ExtendedMode
.TransferSize
= B_16BITS
; break;
491 case Width32Bits
: ExtendedMode
.TransferSize
= B_32BITS
; break;
497 WRITE_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaExtendedMode1
),
500 WRITE_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaExtendedMode2
),
506 * Validate setup for non-busmaster DMA adapter. Secondary controller
507 * supports only 16-bit transfers and main controller supports only
508 * 8-bit transfers. Anything else is invalid.
511 if (!DeviceDescription
->Master
)
513 if (Controller
== 2 && DeviceDescription
->DmaWidth
== Width16Bits
)
514 AdapterObject
->Width16Bits
= TRUE
;
515 else if (Controller
!= 1 || DeviceDescription
->DmaWidth
!= Width8Bits
)
520 DmaMode
.Channel
= AdapterObject
->ChannelNumber
;
521 DmaMode
.AutoInitialize
= DeviceDescription
->AutoInitialize
;
524 * Set the DMA request mode.
526 * For (E)ISA bus master devices just unmask (enable) the DMA channel
527 * and set it to cascade mode. Otherwise just select the right one
528 * bases on the passed device description.
531 if (DeviceDescription
->Master
)
533 DmaMode
.RequestMode
= CASCADE_REQUEST_MODE
;
536 /* Set the Request Data */
537 WRITE_PORT_UCHAR(&((PDMA1_CONTROL
)AdapterBaseVa
)->Mode
,
539 /* Unmask DMA Channel */
540 WRITE_PORT_UCHAR(&((PDMA1_CONTROL
)AdapterBaseVa
)->SingleMask
,
541 AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
543 /* Set the Request Data */
544 WRITE_PORT_UCHAR(&((PDMA2_CONTROL
)AdapterBaseVa
)->Mode
,
546 /* Unmask DMA Channel */
547 WRITE_PORT_UCHAR(&((PDMA2_CONTROL
)AdapterBaseVa
)->SingleMask
,
548 AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
553 if (DeviceDescription
->DemandMode
)
554 DmaMode
.RequestMode
= DEMAND_REQUEST_MODE
;
556 DmaMode
.RequestMode
= SINGLE_REQUEST_MODE
;
559 AdapterObject
->AdapterMode
= DmaMode
;
565 * @name HalGetAdapter
567 * Allocate an adapter object for DMA device.
569 * @param DeviceDescription
570 * Structure describing the attributes of the device.
571 * @param NumberOfMapRegisters
572 * On return filled with the maximum number of map registers the
573 * device driver can allocate for DMA transfer operations.
575 * @return The DMA adapter on success, NULL otherwise.
580 PADAPTER_OBJECT STDCALL
582 PDEVICE_DESCRIPTION DeviceDescription
,
583 PULONG NumberOfMapRegisters
)
585 PADAPTER_OBJECT AdapterObject
= NULL
;
586 PADAPTER_OBJECT MasterAdapter
;
591 /* Validate parameters in device description */
592 if (DeviceDescription
->Version
> DEVICE_DESCRIPTION_VERSION2
)
596 * See if we're going to use ISA/EISA DMA adapter. These adapters are
597 * special since they're reused.
599 * Also note that we check for channel number since there are only 8 DMA
600 * channels on ISA, so any request above this requires new adapter.
603 if (DeviceDescription
->InterfaceType
== Isa
|| !DeviceDescription
->Master
)
605 if (DeviceDescription
->InterfaceType
== Isa
&&
606 DeviceDescription
->DmaChannel
>= 8)
617 * Disallow creating adapter for ISA/EISA DMA channel 4 since it's used
618 * for cascading the controllers and it's not available for software use.
621 if (EisaAdapter
&& DeviceDescription
->DmaChannel
== 4)
625 * Calculate the number of map registers.
627 * - For EISA and PCI scatter/gather no map registers are needed.
628 * - For ISA slave scatter/gather one map register is needed.
629 * - For all other cases the number of map registers depends on
630 * DeviceDescription->MaximumLength.
633 MaximumLength
= DeviceDescription
->MaximumLength
& MAXLONG
;
634 if (DeviceDescription
->ScatterGather
&&
635 (DeviceDescription
->InterfaceType
== Eisa
||
636 DeviceDescription
->InterfaceType
== PCIBus
))
640 else if (DeviceDescription
->ScatterGather
&&
641 !DeviceDescription
->Master
)
648 * In the equation below the additional map register added by
649 * the "+1" accounts for the case when a transfer does not start
650 * at a page-aligned address.
652 MapRegisters
= BYTES_TO_PAGES(MaximumLength
) + 1;
653 if (MapRegisters
> 16)
658 * Acquire the DMA lock that is used to protect adapter lists and
659 * EISA adapter array.
662 KeWaitForSingleObject(&HalpDmaLock
, Executive
, KernelMode
,
666 * Now we must get ahold of the adapter object. For first eight ISA/EISA
667 * channels there are static adapter objects that are reused and updated
668 * on succesive HalGetAdapter calls. In other cases a new adapter object
669 * is always created and it's to the DMA adapter list (HalpDmaAdapterList).
674 AdapterObject
= HalpEisaAdapter
[DeviceDescription
->DmaChannel
];
675 if (AdapterObject
!= NULL
)
677 if (AdapterObject
->NeedsMapRegisters
&&
678 MapRegisters
> AdapterObject
->MapRegistersPerChannel
)
679 AdapterObject
->MapRegistersPerChannel
= MapRegisters
;
683 if (AdapterObject
== NULL
)
685 AdapterObject
= HalpDmaAllocateChildAdapter(
686 MapRegisters
, DeviceDescription
);
687 if (AdapterObject
== NULL
)
689 KeSetEvent(&HalpDmaLock
, 0, 0);
695 HalpEisaAdapter
[DeviceDescription
->DmaChannel
] = AdapterObject
;
698 if (MapRegisters
> 0)
700 AdapterObject
->NeedsMapRegisters
= TRUE
;
701 MasterAdapter
= HalpMasterAdapter
;
702 AdapterObject
->MapRegistersPerChannel
= MapRegisters
;
705 * FIXME: Verify that the following makes sense. Actually
706 * MasterAdapter->NumberOfMapRegisters contains even the number
707 * of gaps, so this will not work correctly all the time. It
708 * doesn't matter much since it's only optimization to avoid
709 * queuing work items in HalAllocateAdapterChannel.
712 MasterAdapter
->CommittedMapRegisters
+= MapRegisters
;
713 if (MasterAdapter
->CommittedMapRegisters
> MasterAdapter
->NumberOfMapRegisters
)
714 HalpGrowMapBuffers(MasterAdapter
, 0x10000);
718 AdapterObject
->NeedsMapRegisters
= FALSE
;
719 if (DeviceDescription
->Master
)
720 AdapterObject
->MapRegistersPerChannel
= BYTES_TO_PAGES(MaximumLength
) + 1;
722 AdapterObject
->MapRegistersPerChannel
= 1;
727 InsertTailList(&HalpDmaAdapterList
, &AdapterObject
->AdapterList
);
730 * Release the DMA lock. HalpDmaAdapterList and HalpEisaAdapter will
731 * no longer be touched, so we don't need it.
734 KeSetEvent(&HalpDmaLock
, 0, 0);
737 * Setup the values in the adapter object that are common for all
741 if (DeviceDescription
->Version
>= DEVICE_DESCRIPTION_VERSION1
)
742 AdapterObject
->IgnoreCount
= DeviceDescription
->IgnoreCount
;
744 AdapterObject
->IgnoreCount
= 0;
746 AdapterObject
->Dma32BitAddresses
= DeviceDescription
->Dma32BitAddresses
;
747 AdapterObject
->Dma64BitAddresses
= DeviceDescription
->Dma64BitAddresses
;
748 AdapterObject
->ScatterGather
= DeviceDescription
->ScatterGather
;
749 AdapterObject
->MasterDevice
= DeviceDescription
->Master
;
750 *NumberOfMapRegisters
= AdapterObject
->MapRegistersPerChannel
;
753 * For non-(E)ISA adapters we have already done all the work. On the
754 * other hand for (E)ISA adapters we must still setup the DMA modes
755 * and prepare the controller.
760 if (!HalpDmaInitializeEisaAdapter(AdapterObject
, DeviceDescription
))
762 ObfDereferenceObject(AdapterObject
);
767 return AdapterObject
;
771 * @name HalpGetDmaAdapter
773 * Internal routine to allocate PnP DMA adapter object. It's exported through
774 * HalDispatchTable and used by IoGetDmaAdapter.
782 IN PDEVICE_DESCRIPTION DeviceDescription
,
783 OUT PULONG NumberOfMapRegisters
)
785 return &HalGetAdapter(DeviceDescription
, NumberOfMapRegisters
)->DmaHeader
;
789 * @name HalPutDmaAdapter
791 * Internal routine to free DMA adapter and resources for reuse. It's exported
792 * using the DMA_OPERATIONS interface by HalGetAdapter.
799 PADAPTER_OBJECT AdapterObject
)
801 if (AdapterObject
->ChannelNumber
== 0xFF)
803 KeWaitForSingleObject(&HalpDmaLock
, Executive
, KernelMode
,
805 RemoveEntryList(&AdapterObject
->AdapterList
);
806 KeSetEvent(&HalpDmaLock
, 0, 0);
809 ObfDereferenceObject(AdapterObject
);
813 * @name HalAllocateCommonBuffer
815 * Allocates memory that is visible to both the processor(s) and the DMA
818 * @param AdapterObject
819 * Adapter object representing the bus master or system dma controller.
821 * Number of bytes to allocate.
822 * @param LogicalAddress
823 * Logical address the driver can use to access the buffer.
824 * @param CacheEnabled
825 * Specifies if the memory can be cached.
827 * @return The base virtual address of the memory allocated or NULL on failure.
830 * On real NT x86 systems the CacheEnabled parameter is ignored, we honour
831 * it. If it proves to cause problems change it.
833 * @see HalFreeCommonBuffer
839 HalAllocateCommonBuffer(
840 PADAPTER_OBJECT AdapterObject
,
842 PPHYSICAL_ADDRESS LogicalAddress
,
843 BOOLEAN CacheEnabled
)
845 PHYSICAL_ADDRESS LowestAcceptableAddress
;
846 PHYSICAL_ADDRESS HighestAcceptableAddress
;
847 PHYSICAL_ADDRESS BoundryAddressMultiple
;
848 PVOID VirtualAddress
;
850 LowestAcceptableAddress
.QuadPart
= 0;
851 HighestAcceptableAddress
=
852 HalpGetAdapterMaximumPhysicalAddress(AdapterObject
);
853 BoundryAddressMultiple
.QuadPart
= 0;
856 * For bus-master DMA devices the buffer mustn't cross 4Gb boundary. For
857 * slave DMA devices the 64Kb boundary mustn't be crossed since the
858 * controller wouldn't be able to handle it.
861 if (AdapterObject
->MasterDevice
)
862 BoundryAddressMultiple
.HighPart
= 1;
864 BoundryAddressMultiple
.LowPart
= 0x10000;
866 VirtualAddress
= MmAllocateContiguousMemorySpecifyCache(
867 Length
, LowestAcceptableAddress
, HighestAcceptableAddress
,
868 BoundryAddressMultiple
, CacheEnabled
? MmCached
: MmNonCached
);
869 if (VirtualAddress
== NULL
)
872 *LogicalAddress
= MmGetPhysicalAddress(VirtualAddress
);
874 return VirtualAddress
;
878 * @name HalFreeCommonBuffer
880 * Free common buffer allocated with HalAllocateCommonBuffer.
882 * @see HalAllocateCommonBuffer
889 PADAPTER_OBJECT AdapterObject
,
891 PHYSICAL_ADDRESS LogicalAddress
,
892 PVOID VirtualAddress
,
893 BOOLEAN CacheEnabled
)
895 MmFreeContiguousMemory(VirtualAddress
);
899 * @name HalpDmaGetDmaAlignment
901 * Internal routine to return the DMA alignment requirement. It's exported
902 * using the DMA_OPERATIONS interface by HalGetAdapter.
908 HalpDmaGetDmaAlignment(
909 PADAPTER_OBJECT AdapterObject
)
915 * @name HalReadDmaCounter
917 * Read DMA operation progress counter.
924 PADAPTER_OBJECT AdapterObject
)
927 ULONG Count
, OldCount
;
929 ASSERT(!AdapterObject
->MasterDevice
);
932 * Acquire the master adapter lock since we're going to mess with the
933 * system DMA controller registers and we really don't want anyone
934 * to do the same at the same time.
937 KeAcquireSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
, &OldIrql
);
939 /* Send the request to the specific controller. */
940 if (AdapterObject
->AdapterNumber
== 1)
942 PDMA1_CONTROL DmaControl1
= AdapterObject
->AdapterBaseVa
;
949 WRITE_PORT_UCHAR(&DmaControl1
->ClearBytePointer
, 0);
951 Count
= READ_PORT_UCHAR(&DmaControl1
->DmaAddressCount
952 [AdapterObject
->ChannelNumber
].DmaBaseCount
);
953 Count
|= READ_PORT_UCHAR(&DmaControl1
->DmaAddressCount
954 [AdapterObject
->ChannelNumber
].DmaBaseCount
) << 8;
956 while (0xffff00 & (OldCount
^ Count
));
960 PDMA2_CONTROL DmaControl2
= AdapterObject
->AdapterBaseVa
;
967 WRITE_PORT_UCHAR(&DmaControl2
->ClearBytePointer
, 0);
969 Count
= READ_PORT_UCHAR(&DmaControl2
->DmaAddressCount
970 [AdapterObject
->ChannelNumber
].DmaBaseCount
);
971 Count
|= READ_PORT_UCHAR(&DmaControl2
->DmaAddressCount
972 [AdapterObject
->ChannelNumber
].DmaBaseCount
) << 8;
974 while (0xffff00 & (OldCount
^ Count
));
977 KeReleaseSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
, OldIrql
);
981 if (AdapterObject
->Width16Bits
)
988 * @name HalpGrowMapBufferWorker
990 * Helper routine of HalAllocateAdapterChannel for allocating map registers
991 * at PASSIVE_LEVEL in work item.
995 HalpGrowMapBufferWorker(PVOID DeferredContext
)
997 PGROW_WORK_ITEM WorkItem
= (PGROW_WORK_ITEM
)DeferredContext
;
1002 * Try to allocate new map registers for the adapter.
1004 * NOTE: The NT implementation actually tries to allocate more map
1005 * registers than needed as an optimization.
1008 KeWaitForSingleObject(&HalpDmaLock
, Executive
, KernelMode
,
1010 Succeeded
= HalpGrowMapBuffers(WorkItem
->AdapterObject
->MasterAdapter
,
1011 WorkItem
->NumberOfMapRegisters
);
1012 KeSetEvent(&HalpDmaLock
, 0, 0);
1017 * Flush the adapter queue now that new map registers are ready. The
1018 * easiest way to do that is to call IoFreeMapRegisters to not free
1019 * any registers. Note that we use the magic (PVOID)2 map register
1020 * base to bypass the parameter checking.
1023 OldIrql
= KfRaiseIrql(DISPATCH_LEVEL
);
1024 IoFreeMapRegisters(WorkItem
->AdapterObject
, (PVOID
)2, 0);
1025 KfLowerIrql(OldIrql
);
1028 ExFreePool(WorkItem
);
1032 * @name HalAllocateAdapterChannel
1034 * Setup map registers for an adapter object.
1036 * @param AdapterObject
1037 * Pointer to an ADAPTER_OBJECT to set up.
1038 * @param WaitContextBlock
1039 * Context block to be used with ExecutionRoutine.
1040 * @param NumberOfMapRegisters
1041 * Number of map registers requested.
1042 * @param ExecutionRoutine
1043 * Callback to call when map registers are allocated.
1046 * If not enough map registers can be allocated then
1047 * STATUS_INSUFFICIENT_RESOURCES is returned. If the function
1048 * succeeds or the callback is queued for later delivering then
1049 * STATUS_SUCCESS is returned.
1051 * @see IoFreeAdapterChannel
1057 HalAllocateAdapterChannel(
1058 PADAPTER_OBJECT AdapterObject
,
1059 PWAIT_CONTEXT_BLOCK WaitContextBlock
,
1060 ULONG NumberOfMapRegisters
,
1061 PDRIVER_CONTROL ExecutionRoutine
)
1063 PADAPTER_OBJECT MasterAdapter
;
1064 PGROW_WORK_ITEM WorkItem
;
1069 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1071 /* Set up the wait context block in case we can't run right away. */
1072 WaitContextBlock
->DeviceRoutine
= ExecutionRoutine
;
1073 WaitContextBlock
->NumberOfMapRegisters
= NumberOfMapRegisters
;
1075 /* Returns true if queued, else returns false and sets the queue to busy */
1076 if (KeInsertDeviceQueue(&AdapterObject
->ChannelWaitQueue
, &WaitContextBlock
->WaitQueueEntry
))
1077 return STATUS_SUCCESS
;
1079 MasterAdapter
= AdapterObject
->MasterAdapter
;
1081 AdapterObject
->NumberOfMapRegisters
= NumberOfMapRegisters
;
1082 AdapterObject
->CurrentWcb
= WaitContextBlock
;
1084 if (NumberOfMapRegisters
&& AdapterObject
->NeedsMapRegisters
)
1086 if (NumberOfMapRegisters
> AdapterObject
->MapRegistersPerChannel
)
1088 AdapterObject
->NumberOfMapRegisters
= 0;
1089 IoFreeAdapterChannel(AdapterObject
);
1090 return STATUS_INSUFFICIENT_RESOURCES
;
1094 * Get the map registers. This is partly complicated by the fact
1095 * that new map registers can only be allocated at PASSIVE_LEVEL
1096 * and we're currently at DISPATCH_LEVEL. The following code has
1099 * - If there is no adapter queued for map register allocation,
1100 * try to see if enough contiguous map registers are present.
1101 * In case they're we can just get them and proceed further.
1103 * - If some adapter is already present in the queue we must
1104 * respect the order of adapters asking for map registers and
1105 * so the fast case described above can't take place.
1106 * This case is also entered if not enough coniguous map
1107 * registers are present.
1109 * A work queue item is allocated and queued, the adapter is
1110 * also queued into the master adapter queue. The worker
1111 * routine does the job of allocating the map registers at
1112 * PASSIVE_LEVEL and calling the ExecutionRoutine.
1115 OldIrql
= KfAcquireSpinLock(&MasterAdapter
->SpinLock
);
1117 if (IsListEmpty(&MasterAdapter
->AdapterQueue
))
1119 Index
= RtlFindClearBitsAndSet(
1120 MasterAdapter
->MapRegisters
, NumberOfMapRegisters
, 0);
1123 AdapterObject
->MapRegisterBase
=
1124 MasterAdapter
->MapRegisterBase
+ Index
;
1125 if (!AdapterObject
->ScatterGather
)
1127 AdapterObject
->MapRegisterBase
=
1128 (PMAP_REGISTER_ENTRY
)(
1129 (ULONG_PTR
)AdapterObject
->MapRegisterBase
|
1137 WorkItem
= ExAllocatePoolWithTag(
1138 NonPagedPool
, sizeof(GROW_WORK_ITEM
), TAG_DMA
);
1139 if (WorkItem
== NULL
)
1141 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1142 AdapterObject
->NumberOfMapRegisters
= 0;
1143 IoFreeAdapterChannel(AdapterObject
);
1144 return STATUS_INSUFFICIENT_RESOURCES
;
1147 InsertTailList(&MasterAdapter
->AdapterQueue
, &AdapterObject
->AdapterQueue
);
1149 ExInitializeWorkItem(
1150 &WorkItem
->WorkQueueItem
, HalpGrowMapBufferWorker
, WorkItem
);
1151 WorkItem
->AdapterObject
= AdapterObject
;
1152 WorkItem
->NumberOfMapRegisters
= NumberOfMapRegisters
;
1154 ExQueueWorkItem(&WorkItem
->WorkQueueItem
, DelayedWorkQueue
);
1156 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1158 return STATUS_SUCCESS
;
1161 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1165 AdapterObject
->MapRegisterBase
= NULL
;
1166 AdapterObject
->NumberOfMapRegisters
= 0;
1169 AdapterObject
->CurrentWcb
= WaitContextBlock
;
1171 Result
= ExecutionRoutine(
1172 WaitContextBlock
->DeviceObject
, WaitContextBlock
->CurrentIrp
,
1173 AdapterObject
->MapRegisterBase
, WaitContextBlock
->DeviceContext
);
1176 * Possible return values:
1179 * Don't free any resources, the ADAPTER_OBJECT is still in use and
1180 * the caller will call IoFreeAdapterChannel later.
1182 * - DeallocateObject
1183 * Deallocate the map registers and release the ADAPTER_OBJECT, so
1184 * someone else can use it.
1186 * - DeallocateObjectKeepRegisters
1187 * Release the ADAPTER_OBJECT, but hang on to the map registers. The
1188 * client will later call IoFreeMapRegisters.
1191 * IoFreeAdapterChannel runs the queue, so it must be called unless
1192 * the adapter object is not to be freed.
1195 if (Result
== DeallocateObject
)
1197 IoFreeAdapterChannel(AdapterObject
);
1199 else if (Result
== DeallocateObjectKeepRegisters
)
1201 AdapterObject
->NumberOfMapRegisters
= 0;
1202 IoFreeAdapterChannel(AdapterObject
);
1205 return STATUS_SUCCESS
;
1209 * @name IoFreeAdapterChannel
1211 * Free DMA resources allocated by IoAllocateAdapterChannel.
1213 * @param AdapterObject
1214 * Adapter object with resources to free.
1217 * This function releases map registers registers assigned to the DMA
1218 * adapter. After releasing the adapter, it checks the adapter's queue
1219 * and runs each queued device object in series until the queue is
1220 * empty. This is the only way the device queue is emptied.
1222 * @see IoAllocateAdapterChannel
1228 IoFreeAdapterChannel(
1229 PADAPTER_OBJECT AdapterObject
)
1231 PADAPTER_OBJECT MasterAdapter
;
1232 PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
;
1233 PWAIT_CONTEXT_BLOCK WaitContextBlock
;
1238 MasterAdapter
= AdapterObject
->MasterAdapter
;
1243 * To keep map registers, call here with AdapterObject->
1244 * NumberOfMapRegisters set to zero. This trick is used in
1245 * HalAllocateAdapterChannel for example.
1247 if (AdapterObject
->NumberOfMapRegisters
)
1251 AdapterObject
->MapRegisterBase
,
1252 AdapterObject
->NumberOfMapRegisters
);
1255 DeviceQueueEntry
= KeRemoveDeviceQueue(&AdapterObject
->ChannelWaitQueue
);
1256 if (DeviceQueueEntry
== NULL
)
1261 WaitContextBlock
= CONTAINING_RECORD(
1266 AdapterObject
->CurrentWcb
= WaitContextBlock
;
1267 AdapterObject
->NumberOfMapRegisters
= WaitContextBlock
->NumberOfMapRegisters
;
1269 if (WaitContextBlock
->NumberOfMapRegisters
&&
1270 AdapterObject
->MasterAdapter
)
1272 OldIrql
= KfAcquireSpinLock(&MasterAdapter
->SpinLock
);
1274 if (IsListEmpty(&MasterAdapter
->AdapterQueue
))
1276 Index
= RtlFindClearBitsAndSet(
1277 MasterAdapter
->MapRegisters
,
1278 WaitContextBlock
->NumberOfMapRegisters
, 0);
1281 AdapterObject
->MapRegisterBase
=
1282 MasterAdapter
->MapRegisterBase
+ Index
;
1283 if (!AdapterObject
->ScatterGather
)
1285 AdapterObject
->MapRegisterBase
=
1286 (PMAP_REGISTER_ENTRY
)(
1287 (ULONG_PTR
)AdapterObject
->MapRegisterBase
|
1295 InsertTailList(&MasterAdapter
->AdapterQueue
, &AdapterObject
->AdapterQueue
);
1296 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1300 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1304 AdapterObject
->MapRegisterBase
= NULL
;
1305 AdapterObject
->NumberOfMapRegisters
= 0;
1308 /* Call the adapter control routine. */
1309 Result
= ((PDRIVER_CONTROL
)WaitContextBlock
->DeviceRoutine
)(
1310 WaitContextBlock
->DeviceObject
, WaitContextBlock
->CurrentIrp
,
1311 AdapterObject
->MapRegisterBase
, WaitContextBlock
->DeviceContext
);
1317 * We're done until the caller manually calls IoFreeAdapterChannel
1318 * or IoFreeMapRegisters.
1322 case DeallocateObjectKeepRegisters
:
1324 * Hide the map registers so they aren't deallocated next time
1327 AdapterObject
->NumberOfMapRegisters
= 0;
1337 * @name IoFreeMapRegisters
1339 * Free map registers reserved by the system for a DMA.
1341 * @param AdapterObject
1342 * DMA adapter to free map registers on.
1343 * @param MapRegisterBase
1344 * Handle to map registers to free.
1345 * @param NumberOfRegisters
1346 * Number of map registers to be freed.
1353 IN PADAPTER_OBJECT AdapterObject
,
1354 IN PVOID MapRegisterBase
,
1355 IN ULONG NumberOfMapRegisters
)
1357 PADAPTER_OBJECT MasterAdapter
= AdapterObject
->MasterAdapter
;
1358 PLIST_ENTRY ListEntry
;
1363 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1365 if (MasterAdapter
== NULL
|| MapRegisterBase
== NULL
)
1368 OldIrql
= KfAcquireSpinLock(&MasterAdapter
->SpinLock
);
1370 if (NumberOfMapRegisters
!= 0)
1372 PMAP_REGISTER_ENTRY RealMapRegisterBase
;
1374 RealMapRegisterBase
=
1375 (PMAP_REGISTER_ENTRY
)((ULONG_PTR
)MapRegisterBase
& ~MAP_BASE_SW_SG
);
1376 RtlClearBits(MasterAdapter
->MapRegisters
,
1377 RealMapRegisterBase
- MasterAdapter
->MapRegisterBase
,
1378 NumberOfMapRegisters
);
1382 * Now that we freed few map registers it's time to look at the master
1383 * adapter queue and see if there is someone waiting for map registers.
1386 while (!IsListEmpty(&MasterAdapter
->AdapterQueue
))
1388 ListEntry
= RemoveHeadList(&MasterAdapter
->AdapterQueue
);
1389 AdapterObject
= CONTAINING_RECORD(
1390 ListEntry
, struct _ADAPTER_OBJECT
, AdapterQueue
);
1392 Index
= RtlFindClearBitsAndSet(
1393 MasterAdapter
->MapRegisters
,
1394 AdapterObject
->NumberOfMapRegisters
,
1395 MasterAdapter
->NumberOfMapRegisters
);
1398 InsertHeadList(&MasterAdapter
->AdapterQueue
, ListEntry
);
1402 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1404 AdapterObject
->MapRegisterBase
=
1405 MasterAdapter
->MapRegisterBase
+ Index
;
1406 if (!AdapterObject
->ScatterGather
)
1408 AdapterObject
->MapRegisterBase
=
1409 (PMAP_REGISTER_ENTRY
)(
1410 (ULONG_PTR
)AdapterObject
->MapRegisterBase
|
1414 Result
= ((PDRIVER_CONTROL
)AdapterObject
->CurrentWcb
->DeviceRoutine
)(
1415 AdapterObject
->CurrentWcb
->DeviceObject
,
1416 AdapterObject
->CurrentWcb
->CurrentIrp
,
1417 AdapterObject
->MapRegisterBase
,
1418 AdapterObject
->CurrentWcb
->DeviceContext
);
1422 case DeallocateObjectKeepRegisters
:
1423 AdapterObject
->NumberOfMapRegisters
= 0;
1426 case DeallocateObject
:
1427 if (AdapterObject
->NumberOfMapRegisters
)
1429 OldIrql
= KfAcquireSpinLock(&MasterAdapter
->SpinLock
);
1430 RtlClearBits(MasterAdapter
->MapRegisters
,
1431 AdapterObject
->MapRegisterBase
-
1432 MasterAdapter
->MapRegisterBase
,
1433 AdapterObject
->NumberOfMapRegisters
);
1434 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1436 IoFreeAdapterChannel(AdapterObject
);
1443 OldIrql
= KfAcquireSpinLock(&MasterAdapter
->SpinLock
);
1446 KfReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1450 * @name HalpCopyBufferMap
1452 * Helper function for copying data from/to map register buffers.
1454 * @see IoFlushAdapterBuffers, IoMapTransfer
1460 PMAP_REGISTER_ENTRY MapRegisterBase
,
1463 BOOLEAN WriteToDevice
)
1465 ULONG CurrentLength
;
1466 ULONG_PTR CurrentAddress
;
1468 PVOID VirtualAddress
;
1470 VirtualAddress
= MmGetSystemAddressForMdlSafe(Mdl
, HighPagePriority
);
1471 if (VirtualAddress
== NULL
)
1474 * NOTE: On real NT a mechanism with reserved pages is implemented
1475 * to handle this case in a slow, but graceful non-fatal way.
1477 /* FIXME: The correct bug check code isn't defined. */
1478 /* KEBUGCHECKEX(HAL_MEMORY_ALLOCATION, PAGE_SIZE, 0, (ULONG_PTR)__FILE__, 0); */
1482 CurrentAddress
= (ULONG_PTR
)VirtualAddress
+
1483 (ULONG_PTR
)CurrentVa
-
1484 (ULONG_PTR
)MmGetMdlVirtualAddress(Mdl
);
1488 ByteOffset
= BYTE_OFFSET(CurrentAddress
);
1489 CurrentLength
= PAGE_SIZE
- ByteOffset
;
1490 if (CurrentLength
> Length
)
1491 CurrentLength
= Length
;
1496 (PVOID
)((ULONG_PTR
)MapRegisterBase
->VirtualAddress
+ ByteOffset
),
1497 (PVOID
)CurrentAddress
,
1503 (PVOID
)CurrentAddress
,
1504 (PVOID
)((ULONG_PTR
)MapRegisterBase
->VirtualAddress
+ ByteOffset
),
1508 Length
-= CurrentLength
;
1509 CurrentAddress
+= CurrentLength
;
1515 * @name IoFlushAdapterBuffers
1517 * Flush any data remaining in the DMA controller's memory into the host
1520 * @param AdapterObject
1521 * The adapter object to flush.
1523 * Original MDL to flush data into.
1524 * @param MapRegisterBase
1525 * Map register base that was just used by IoMapTransfer, etc.
1527 * Offset into Mdl to be flushed into, same as was passed to
1530 * Length of the buffer to be flushed into.
1531 * @param WriteToDevice
1532 * TRUE if it's a write, FALSE if it's a read.
1534 * @return TRUE in all cases.
1537 * This copies data from the map register-backed buffer to the user's
1538 * target buffer. Data are not in the user buffer until this function
1540 * For slave DMA transfers the controller channel is masked effectively
1541 * stopping the current transfer.
1547 IoFlushAdapterBuffers(
1548 PADAPTER_OBJECT AdapterObject
,
1550 PVOID MapRegisterBase
,
1553 BOOLEAN WriteToDevice
)
1555 BOOLEAN SlaveDma
= FALSE
;
1556 PMAP_REGISTER_ENTRY RealMapRegisterBase
;
1558 ASSERT_IRQL(DISPATCH_LEVEL
);
1560 if (AdapterObject
!= NULL
&& !AdapterObject
->MasterDevice
)
1562 /* Mask out (disable) the DMA channel. */
1563 if (AdapterObject
->AdapterNumber
== 1)
1565 PDMA1_CONTROL DmaControl1
= AdapterObject
->AdapterBaseVa
;
1566 WRITE_PORT_UCHAR(&DmaControl1
->SingleMask
,
1567 AdapterObject
->ChannelNumber
| DMA_SETMASK
);
1571 PDMA2_CONTROL DmaControl2
= AdapterObject
->AdapterBaseVa
;
1572 WRITE_PORT_UCHAR(&DmaControl2
->SingleMask
,
1573 AdapterObject
->ChannelNumber
| DMA_SETMASK
);
1578 /* This can happen if the device supports hardware scatter/gather. */
1579 if (MapRegisterBase
== NULL
)
1582 RealMapRegisterBase
=
1583 (PMAP_REGISTER_ENTRY
)((ULONG_PTR
)MapRegisterBase
& ~MAP_BASE_SW_SG
);
1587 if ((ULONG_PTR
)MapRegisterBase
& MAP_BASE_SW_SG
)
1589 if (RealMapRegisterBase
->Counter
!= ~0)
1591 if (SlaveDma
&& !AdapterObject
->IgnoreCount
)
1592 Length
-= HalReadDmaCounter(AdapterObject
);
1595 HalpCopyBufferMap(Mdl
, RealMapRegisterBase
, CurrentVa
, Length
, FALSE
);
1599 /* FIXME: Unimplemented case */
1604 RealMapRegisterBase
->Counter
= 0;
1610 * @name IoMapTransfer
1612 * Map a DMA for transfer and do the DMA if it's a slave.
1614 * @param AdapterObject
1615 * Adapter object to do the DMA on. Bus-master may pass NULL.
1617 * Locked-down user buffer to DMA in to or out of.
1618 * @param MapRegisterBase
1619 * Handle to map registers to use for this dma.
1621 * Index into Mdl to transfer into/out of.
1623 * Length of transfer. Number of bytes actually transferred on
1625 * @param WriteToDevice
1626 * TRUE if it's an output DMA, FALSE otherwise.
1629 * A logical address that can be used to program a DMA controller, it's
1630 * not meaningful for slave DMA device.
1633 * This function does a copyover to contiguous memory <16MB represented
1634 * by the map registers if needed. If the buffer described by MDL can be
1635 * used as is no copyover is done.
1636 * If it's a slave transfer, this function actually performs it.
1641 PHYSICAL_ADDRESS STDCALL
1643 IN PADAPTER_OBJECT AdapterObject
,
1645 IN PVOID MapRegisterBase
,
1647 IN OUT PULONG Length
,
1648 IN BOOLEAN WriteToDevice
)
1650 PPFN_NUMBER MdlPagesPtr
;
1651 PFN_NUMBER MdlPage1
, MdlPage2
;
1653 ULONG TransferOffset
;
1654 ULONG TransferLength
;
1655 BOOLEAN UseMapRegisters
;
1656 PMAP_REGISTER_ENTRY RealMapRegisterBase
;
1657 PHYSICAL_ADDRESS PhysicalAddress
;
1658 PHYSICAL_ADDRESS HighestAcceptableAddress
;
1660 DMA_MODE AdapterMode
;
1664 * Precalculate some values that are used in all cases.
1666 * ByteOffset is offset inside the page at which the transfer starts.
1667 * MdlPagesPtr is pointer inside the MDL page chain at the page where the
1669 * PhysicalAddress is physical address corresponding to the transfer
1670 * start page and offset.
1671 * TransferLength is the inital length of the transfer, which is reminder
1672 * of the first page. The actual value is calculated below.
1674 * Note that all the variables can change during the processing which
1675 * takes place below. These are just initial values.
1678 ByteOffset
= BYTE_OFFSET(CurrentVa
);
1680 MdlPagesPtr
= MmGetMdlPfnArray(Mdl
);
1681 MdlPagesPtr
+= ((ULONG_PTR
)CurrentVa
- (ULONG_PTR
)Mdl
->StartVa
) >> PAGE_SHIFT
;
1683 PhysicalAddress
.QuadPart
= *MdlPagesPtr
<< PAGE_SHIFT
;
1684 PhysicalAddress
.QuadPart
+= ByteOffset
;
1686 TransferLength
= PAGE_SIZE
- ByteOffset
;
1689 * Special case for bus master adapters with S/G support. We can directly
1690 * use the buffer specified by the MDL, so not much work has to be done.
1692 * Just return the passed VA's corresponding physical address and update
1693 * length to the number of physically contiguous bytes found. Also
1694 * pages crossing the 4Gb boundary aren't considered physically contiguous.
1697 if (MapRegisterBase
== NULL
)
1699 while (TransferLength
< *Length
)
1701 MdlPage1
= *MdlPagesPtr
;
1702 MdlPage2
= *(MdlPagesPtr
+ 1);
1703 if (MdlPage1
+ 1 != MdlPage2
)
1705 if ((MdlPage1
^ MdlPage2
) & ~0xFFFFF)
1707 TransferLength
+= PAGE_SIZE
;
1711 if (TransferLength
< *Length
)
1712 *Length
= TransferLength
;
1714 return PhysicalAddress
;
1718 * The code below applies to slave DMA adapters and bus master adapters
1719 * without hardward S/G support.
1722 RealMapRegisterBase
=
1723 (PMAP_REGISTER_ENTRY
)((ULONG_PTR
)MapRegisterBase
& ~MAP_BASE_SW_SG
);
1726 * Try to calculate the size of the transfer. We can only transfer
1727 * pages that are physically contiguous and that don't cross the
1728 * 64Kb boundary (this limitation applies only for ISA controllers).
1731 while (TransferLength
< *Length
)
1733 MdlPage1
= *MdlPagesPtr
;
1734 MdlPage2
= *(MdlPagesPtr
+ 1);
1735 if (MdlPage1
+ 1 != MdlPage2
)
1737 if (!HalpEisaDma
&& ((MdlPage1
^ MdlPage2
) & ~0xF))
1739 TransferLength
+= PAGE_SIZE
;
1743 if (TransferLength
> *Length
)
1744 TransferLength
= *Length
;
1747 * If we're about to simulate software S/G and not all the pages are
1748 * physically contiguous then we must use the map registers to store
1749 * the data and allow the whole transfer to proceed at once.
1752 if ((ULONG_PTR
)MapRegisterBase
& MAP_BASE_SW_SG
&&
1753 TransferLength
< *Length
)
1755 UseMapRegisters
= TRUE
;
1756 PhysicalAddress
= RealMapRegisterBase
->PhysicalAddress
;
1757 PhysicalAddress
.QuadPart
+= ByteOffset
;
1758 TransferLength
= *Length
;
1759 RealMapRegisterBase
->Counter
= ~0;
1765 * This is ordinary DMA transfer, so just update the progress
1766 * counters. These are used by IoFlushAdapterBuffers to track
1767 * the transfer progress.
1770 UseMapRegisters
= FALSE
;
1771 Counter
= RealMapRegisterBase
->Counter
;
1772 RealMapRegisterBase
->Counter
+= BYTES_TO_PAGES(ByteOffset
+ TransferLength
);
1775 * Check if the buffer doesn't exceed the highest physical address
1776 * limit of the device. In that case we must use the map registers to
1780 HighestAcceptableAddress
= HalpGetAdapterMaximumPhysicalAddress(AdapterObject
);
1781 if (PhysicalAddress
.QuadPart
+ TransferLength
>
1782 HighestAcceptableAddress
.QuadPart
)
1784 UseMapRegisters
= TRUE
;
1785 PhysicalAddress
= RealMapRegisterBase
->PhysicalAddress
;
1786 PhysicalAddress
.QuadPart
+= ByteOffset
;
1787 if ((ULONG_PTR
)MapRegisterBase
& MAP_BASE_SW_SG
)
1789 RealMapRegisterBase
->Counter
= ~0;
1795 * If we decided to use the map registers (see above) and we're about
1796 * to transfer data to the device then copy the buffers into the map
1800 if (UseMapRegisters
&& WriteToDevice
)
1802 HalpCopyBufferMap(Mdl
, RealMapRegisterBase
+ Counter
,
1803 CurrentVa
, TransferLength
, WriteToDevice
);
1807 * Return the length of transfer that actually takes place.
1810 *Length
= TransferLength
;
1813 * If we're doing slave (system) DMA then program the (E)ISA controller
1814 * to actually start the transfer.
1817 if (AdapterObject
!= NULL
&& !AdapterObject
->MasterDevice
)
1819 AdapterMode
= AdapterObject
->AdapterMode
;
1823 AdapterMode
.TransferType
= WRITE_TRANSFER
;
1827 AdapterMode
.TransferType
= READ_TRANSFER
;
1828 if (AdapterObject
->IgnoreCount
)
1830 RtlZeroMemory((PUCHAR
)RealMapRegisterBase
[Counter
].VirtualAddress
+
1831 ByteOffset
, TransferLength
);
1835 TransferOffset
= PhysicalAddress
.LowPart
& 0xFFFF;
1836 if (AdapterObject
->Width16Bits
)
1838 TransferLength
>>= 1;
1839 TransferOffset
>>= 1;
1842 OldIrql
= KfAcquireSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
);
1844 if (AdapterObject
->AdapterNumber
== 1)
1846 PDMA1_CONTROL DmaControl1
= AdapterObject
->AdapterBaseVa
;
1848 /* Reset Register */
1849 WRITE_PORT_UCHAR(&DmaControl1
->ClearBytePointer
, 0);
1851 WRITE_PORT_UCHAR(&DmaControl1
->Mode
, AdapterMode
.Byte
);
1852 /* Set the Offset Register */
1853 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
1854 (UCHAR
)(TransferOffset
));
1855 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
1856 (UCHAR
)(TransferOffset
>> 8));
1857 /* Set the Page Register */
1858 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+
1859 FIELD_OFFSET(EISA_CONTROL
, DmaController1Pages
),
1860 (UCHAR
)(PhysicalAddress
.LowPart
>> 16));
1863 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+
1864 FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
),
1867 /* Set the Length */
1868 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
1869 (UCHAR
)(TransferLength
- 1));
1870 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
1871 (UCHAR
)((TransferLength
- 1) >> 8));
1872 /* Unmask the Channel */
1873 WRITE_PORT_UCHAR(&DmaControl1
->SingleMask
,
1874 AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
1878 PDMA2_CONTROL DmaControl2
= AdapterObject
->AdapterBaseVa
;
1880 /* Reset Register */
1881 WRITE_PORT_UCHAR(&DmaControl2
->ClearBytePointer
, 0);
1883 WRITE_PORT_UCHAR(&DmaControl2
->Mode
, AdapterMode
.Byte
);
1884 /* Set the Offset Register */
1885 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
1886 (UCHAR
)(TransferOffset
));
1887 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
1888 (UCHAR
)(TransferOffset
>> 8));
1889 /* Set the Page Register */
1890 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+
1891 FIELD_OFFSET(EISA_CONTROL
, DmaController1Pages
),
1892 (UCHAR
)(PhysicalAddress
.u
.LowPart
>> 16));
1895 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+
1896 FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
),
1899 /* Set the Length */
1900 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
1901 (UCHAR
)(TransferLength
- 1));
1902 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
1903 (UCHAR
)((TransferLength
- 1) >> 8));
1904 /* Unmask the Channel */
1905 WRITE_PORT_UCHAR(&DmaControl2
->SingleMask
,
1906 AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
1909 KfReleaseSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
, OldIrql
);
1913 * Return physical address of the buffer with data that is used for the
1914 * transfer. It can either point inside the Mdl that was passed by the
1915 * caller or into the map registers if the Mdl buffer can't be used
1919 return PhysicalAddress
;
1923 * @name HalFlushCommonBuffer
1929 HalFlushCommonBuffer(