{"id":419,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:00","modified_gmt":"2020-08-22T20:26:00","slug":"this-is-the-page-title-toplevel-252","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=419","title":{"rendered":"Direct Memory Access"},"content":{"rendered":"\n<title>Direct Memory Access<\/title> (DMA)\n<p>\nUsing interrupts driven <glossary>device driver<\/glossary>s to transfer data to or from hardware\ndevices works well when the amount of data is reasonably low.\nFor example a 9600 <glossary>baud<\/glossary> <glossary>modem<\/glossary> can transfer approximately one character every\nmillisecond (1\/1000&#8217;th second).\nIf the interrupt latency, the amount of time that it takes between the hardware\ndevice raising the interrupt and the device driver&#8217;s interrupt handling routine being called, is\nlow (say 2 milliseconds) then the overall system impact of the data transfer is very low.\nThe 9600 baud modem data transfer would only take 0.002{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63} of the CPU&#8217;s processing time.\nFor high speed devices, such as hard disk controllers or ethernet devices the\ndata transfer rate is a lot higher.\nA <glossary>SCSI<\/glossary> device can transfer up to 40 Mbytes of information per second.\n<p>\nDirect Memory Access, or DMA, was invented to solve this problem.\nA DMA controller allows devices to transfer data to or from the system&#8217;s memory\nwithout the intervention of the processor. A PC&#8217;s <glossary>ISA<\/glossary>\n<glossary>DMA<\/glossary> controller has 8 DMA channels of which 7 are available for use by\nthe device drivers. Each DMA channel has associated with it a 16 bit address register and a\n16 bit count <glossary>register<\/glossary>.\nTo initiate a data transfer the device driver sets up the DMA channel&#8217;s address\nand count registers together\nwith the direction of the data transfer, read or write.\nIt then tells the device that it may start the DMA when it wishes.\nWhen the transfer is complete the device interrupts the PC.\nWhilst the transfer is taking place the <glossary>CPU<\/glossary> is free to do other things.\n<p>\nDevice drivers have to be careful when using <glossary>DMA<\/glossary>. First of all the DMA\ncontroller knows nothing of virtual memory, it only has access to the physical memory\nin the system.\nTherefore the memory that is being DMA&#8217;d to or from must be a contiguous block\nof physical memory.\nThis means that you cannot DMA directly into the virtual address space of a\nprocess.\nYou can however lock the process&#8217;s physical pages into memory, preventing them\nfrom being swapped out\nto the swap device during a DMA operation.\nSecondly, the DMA controller cannot access the whole of physical memory.\nThe <glossary>DMA<\/glossary> channel&#8217;s <glossary>address<\/glossary>\n<glossary>register<\/glossary> represents the first 16 bits of the DMA\naddress, the next 8 bits come from the page register.\nThis means that DMA requests are limited to the bottom 16 Mbytes of memory.\n<p>\nDMA channels are scarce resources, there are only 7 of them, and they cannot be\nshared between device drivers. Just like interrupts, the device driver must be able to work out\nwhich DMA channel it should use.\nLike interrupts, some devices have a fixed DMA channel.\nThe floppy device, for example, always uses DMA channel 2.\nSometimes the DMA channel for a device can be set by jumpers; a number of\nethernet devices use this technique.\nThe more flexible devices can be told (via their CSRs) which DMA channels to use\nand, in this case, the device\ndriver can simply pick a free DMA channel to use.\n<p>\nLinux tracks the usage of the DMA channels using a vector of <tt>dma_chan<\/tt>\ndata structures (one per\nDMA channel).\nThe <tt>dma_chan<\/tt> data structure contains just two fields, a pointer to a\nstring describing the owner of\nthe DMA channel and a flag indicating if the DMA channel is allocated or not.\nIt is this vector of <tt>dma_chan<\/tt> data structures that is printed when you\n<font face=\"helvetica\">cat<\/font> <tt>\/proc\/dma<\/tt>.\n","protected":false},"excerpt":{"rendered":"<p>Direct Memory Access (DMA) Using interrupts driven device drivers to transfer data to or from hardware devices works well when the amount of data is reasonably low. For example a 9600 baud modem can transfer approximately one character every millisecond &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=419\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-419","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=419"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/419\/revisions"}],"predecessor-version":[{"id":543,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/419\/revisions\/543"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}