How does a DMA controller work

iooperating systems

From Section 5.1.4 Direct Memory Access in Modern Operating Systems by Andrew S. Tanenbaum, Herbert Bos, 2014,

To simplify the explanation, we assume that the CPU accesses all devices and memory via a single system bus that connects the CPU, the memory, and the I/O devices, as shown in Fig. 5-4.

enter image description here

  1. To explain how DMA works, let us first look at how disk reads occur
    when DMA is not used.

    • First the disk controller reads the block (one or more sectors) from the drive serially, bit by bit, until the entire block is in
      the
      controller’s internal buffer.
    • Next, it computes the checksum to verify that no read errors have occurred. Then the controller causes an interrupt. When the
      operating
      system starts running, it can read the disk block from the
      controller’s buffer
      a byte or a word at a time by executing a loop,
      with each iteration reading one byte or word from a controller
      device register
      and storing it in main memory.

    Q: in the second step,

    • isn't the data transferred "from the controller's buffer" to the main memory? Why does it say both "from the controller’s
      buffer
      " and "from a controller device register"?

    • in the second step, can the controller transfer data from its buffer to the main memory, without interrupting to the cpu, and
      without involving OS again?

  2. When DMA is used, the procedure is different.

    • First the CPU programs the DMA controller by setting its registers so it knows what to transfer where (step 1 in Fig. 5-4).
      It also issues a command to the disk controller telling it to read
      data from the disk into its internal buffer
      and verify the checksum.
    • When valid data are in the disk controller’s buffer, DMA can begin. The DMA controller initiates the transfer by issuing a read
      request over the bus to the disk controller
      (step 2). This read
      request
      looks like any other read request, and the disk controller does not
      know (or care) whether it came from the CPU or from a DMA controller.
      Typically, the memory address to write to is on the bus’ address
      lines, so when the disk controller fetches the next word from its
      internal buffer, it knows where to write it.
      The write to memory is
      another standard bus cycle (step 3).
    • When the write is complete, the disk controller sends an acknowledgement signal to the DMA controller, also over the bus
      (step
      4). The DMA controller then increments the memory address to use and
      decrements the byte count. If the byte count is still greater
      than 0, steps 2 through 4 are repeated until the count
      reaches 0.
    • At that time, the DMA controller interrupts the CPU to let it know that the transfer is now complete. When the operating
      system
      starts up, it does not have to copy the disk block to memory; it is
      already there.

    Q: in the second step, the DMA controller requests the disk
    controller to transfer data from the disk controller's buffer to the
    main memory. In the first step, the CPU issues a command to the disk controller telling it to read data from the disk into its internal buffer. At the same time, can the CPU also tell the disk controller to transfer data
    from the disk controller's buffer to the main memory, when the disk controller finishes transfer data from the disk to the disk controller's buffer, so that there is no need for the DMA controller to tell the disk controller to transfer data from the disk controller's buffer to the main memory? (I can't understand why we need a DMA controller for data transfer between the disk and the main memory, so guess that I miss something important to understand the quote).

  3. A device controller of a device controls the device and performs
    operations on the device. What device does a DMA controller control
    and perform operations on?

Thanks!

Best Answer

Q1

In the first step, we're NOT using DMA, so the content of the disk controller is read piece by piece by the processor. The processor will of course (assuming the data is actually going to be used for something, and not just being thrown away) store it in the memory of the system.

The buffer in this case is a piece of memory on the hard-disk (controller) itself, and the controller device register a control register of the hard-disk (controller) itself.

Not involving the OS (or some other software) would require some kind of DMA operation, and the section of text you are discussing in this part of your question is NOT using DMA. So, no, it won't happen like that in this case.

Q2

So, the whole point of a DMA controller is to "perform the tedious task of storing stuff from the device's internal buffer into main memory". The CPU will work with both the DMA controller and the disk device. If the disk could do this itself, there would be no need for a DMA controller.

And indeed, in modern systems, the DMA capability is typically built into the hard-disk controller itself, in the sense that the controller has "bus mastering" capabilities, which means that the controller itself IS the DMA controller for the device. However, to look at them as two separate devices makes the whole concept of DMA a little less difficult to understand.

Q3 (kind of)

If you think of the hard disk as the stack of bricks just delivered to a building site, and the processor is the bricklayer that lays the bricks to build the house. The DMA controller is the labourer that carries the bricks from the stack of bricks to where they are needed for the bricklayer, meaning that the bricklayer can concentrate on doing the actual work of laying bricks (which is skilled work, if you have ever tried it yourself), and the simple work of "fetch and carry" can be done by a less skilled worker.

Anecdotal evidence: When I first learned about DMA transfer from disk to memory was about 1997 or so when IDE controllers begun using DMA, and you needed to get a "motherboard IDE controller" driver to allow the IDE to do DMA, and at that time, reading from the hard-disk would take about 6-10% of the CPU time, where DMA in the same setup would use about 1% of the CPU time. Before that time, only fancy systems with SCSI disk controllers would use DMA.