DMA question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Rolf

DMA question

Post by Rolf »

THere?s one thing I havent been able to figure out about DMA, and that is how to chose where to copy data (if I want to send data to some device). Suppose I know that when I send a command, this device will try to send some data using DMA channel 0. If I set up DMA and enable it, will the hardware do the rest for me? So I dont need to specify which device/ports I?d like to write/read from, just enabling the right DMA channel and the device(which is operating on the channel) will do the work for me and copy the data as soon as the channel is enabled?

The reason for me asking this is because I?m currently writing a floppy driver. Is it enought to just set up DMA, sending a read (for instance) command to the device, enable the channel that the floppy uses, and then the floppy will "sense" that the channel is enabled and then copy the data (and then trigger an IRQ of course)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:DMA question

Post by Candy »

Rolf wrote: THere?s one thing I havent been able to figure out about DMA, and that is how to chose where to copy data (if I want to send data to some device). Suppose I know that when I send a command, this device will try to send some data using DMA channel 0. If I set up DMA and enable it, will the hardware do the rest for me? So I dont need to specify which device/ports I?d like to write/read from, just enabling the right DMA channel and the device(which is operating on the channel) will do the work for me and copy the data as soon as the channel is enabled?
You use either DMA or ports. You cannot set a port number for DMA to use. What sort of twisted logic is that? ;)

You set up the DMA controller to read from channel X to address A. You set up the floppy drive to read from CHS Z to DMA channel X. That's all.
The reason for me asking this is because I?m currently writing a floppy driver. Is it enought to just set up DMA, sending a read (for instance) command to the device, enable the channel that the floppy uses, and then the floppy will "sense" that the channel is enabled and then copy the data (and then trigger an IRQ of course)
I'd enable the channel first (the dma chip waits when reading, the floppy drive can't wait when writing). Other than that, seems OK
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:DMA question

Post by Pype.Clicker »

actually, you need to tell the DMA where (in main memory) data should go/come from and what's the direction of the transfer (plus the size of the transfer).

That being done, the device that is ready to deliver data to memory will issue a bus cycle saying that it has (or is ready to receive) data to the DMA and the DMA controller will do what's needed to move data between the device and the memory.

Port numbers are required when the CPU initiates a cycles because it needs to *address* the device among all possible devices, but for device-initiated DMA cycles, the DMA channel is enough to select the listener among all 'dma channels' in the system.

HTH
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:DMA question

Post by Brendan »

Hi,

Without DMA the device would read/write data to/from an IO port. With DMA the data goes directly to/from the DMA controller instead (without any IO ports being used). Each DMA channel is hard-wired to a specific device so you don't need to tell the DMA chip where to get/put data (except for the physical memory address).

You'd configure the correct DMA channel in the DMA controller ready to do the transfer, then send the command to the device. Once the device starts transferring data the DMA chip will do it's thing. During the transfer you can check the DMA count to see how far it's got, and the device usually generates an IRQ when it has completed the transfer.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Rolf

Re:DMA question

Post by Rolf »

Thanks guys :)
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:DMA question

Post by Brendan »

Wow - three answers to the same question at the same time! Is this some kind of record? :)

- Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:DMA question

Post by Candy »

Brendan wrote: Wow - three answers to the same question at the same time! Is this some kind of record? :)

- Brendan
seeing as they were all typed concurrently and without regard to each other, I think we all surf this site the same (tabbed browsing, opening all at a time and replying when I feel like it) and started surfing at about 12:20 pm local time here.

Do think this is a record, although I've had a closer one with Pype (believe 18 seconds).
Rolf

Re:DMA question

Post by Rolf »

;D
One more thing. I read that the DMA chip (and mose devices) only operates in the low memory below 1 MB. Does this mean that the location where I want the data to be copied to (or where I store it before copying it) has to be under that 1 MB limit? If so, is there any other requirements? I?m storing my kernel at 0x1000 I got plenty of space in the kernel (before it goes over 1 MB). Would be great to just declare an array in the kernel.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:DMA question

Post by Brendan »

Hi,
Rolf wrote: ;D
One more thing. I read that the DMA chip (and mose devices) only operates in the low memory below 1 MB. Does this mean that the location where I want the data to be copied to (or where I store it before copying it) has to be under that 1 MB limit? If so, is there any other requirements? I?m storing my kernel at 0x1000 I got plenty of space in the kernel (before it goes over 1 MB). Would be great to just declare an array in the kernel.
The DMA chip needs to use memory below 16 Mb (not 1 Mb). In addition a transfer can't cross a 64 Kb boundary, so you can transfer 64 Kb from 0x10000 to 0x20000, but not from 0x18000 to 0x28000.

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Rolf

Re:DMA question

Post by Rolf »

So all I need is to find a location, below 16 MB, which starts at an address that looks like

x * 0x10000
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:DMA question

Post by Brendan »

Hi,
Rolf wrote: So all I need is to find a location, below 16 MB, which starts at an address that looks like
x * 0x10000
Possibly, although depending on how your OS does things this restriction can be reduced.

For example, if your floppy disk driver will only transfer a maximum of one complete track at a time, then it'd only need a DMA buffer of 18 Kb (for a 1440 Kb floppy disk). In this case you could have a DMA buffer from 0x10000 to 0x14800, from 0x12345 to 0x16B45, or from 0x1B800 to 0x20000 (as long as it doesn't cross a 64 Kb boundary).

It's also possible for the devices that use ISA DMA to dynamically allocate a buffer of the required size before each transfer. In this case if the user doesn't use a device that needs ISA DMA, then you can use the memory for something more important. Alternatively you could pre-allocate a 64 Kb buffer for each of the 7 DMA channels, which would consume 448 Kb even when it's never used.

Most devices in modern computers don't use the ISA DMA anymore (with the exception of floppy disks). PCI devices have their own DMA (bus mastering) hardware which doesn't have the restrictions of ISA DMA. Some of these even allow DMA to physical pages that are randomly scattered through physical memory. Because of this (and because the floppy is often unused) I probably wouldn't use pre-allocated/static DMA buffers..


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Rolf

Re:DMA question

Post by Rolf »

hehe by the way, what DMA channel is the floppy using?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:DMA question

Post by Brendan »

Hi,
Rolf wrote: hehe by the way, what DMA channel is the floppy using?
The primary floppy disk controller uses DMA channel 2. The secondary floppy disk controller might use DMA channel 3, or it could share channel 2, or it could be set to something else :)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
RMX

Re:DMA question

Post by RMX »

Do you know any tutorials discribing in details DMA in protected mode?
Any links?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:DMA question

Post by Brendan »

Hi,
RMX wrote: Do you know any tutorials discribing in details DMA in protected mode?
Any links?
Google is good - search for "DMA 8237" or "DMA 8237 tutorial":

http://www.osdever.net/tutorials/howto_ ... ?the_id=63
http://www.inversereality.org/files/dmaprogramming.pdf
http://dsplabs.utt.ro/aces/docs/daq_board/818LCH9.PDF

There's also the datasheet for the DMA controller chip (8237): http://www.stud.fh-hannover.de/~heineman/proginfo.htm

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply