Should dma_map_single take the dma controller or its consumer as an argument?

Li Chen me at linux.beauty
Wed Jun 28 03:57:35 PDT 2023


Hi all,

I recently encountered an issue where the dma_mask was set in the DMA controller's driver, but the consumer peripheral driver didn't set its own dma_mask.

If I utilize APIs such as dma_map_single or dma_alloc_coherent and pass a DMA controller as the argument, such as dma_map_single(dma_chan->device->dev, ...), the dma_mask is respected and there would be no issues. I also saw there are some user cases in the kernel:
```
# rg "dma_map_single.*chan"
drivers/i2c/busses/i2c-sh_mobile.c
536:    dma_addr = dma_map_single(chan->device->dev, pd->dma_buf, pd->msg->len, dir);

drivers/i2c/busses/i2c-imx.c
399:    dma->dma_buf = dma_map_single(chan_dev, msgs->buf,

drivers/i2c/busses/i2c-stm32.c
121:    dma->dma_buf = dma_map_single(chan_dev, buf, dma->dma_len,

drivers/i2c/busses/i2c-rcar.c
443:    dma_addr = dma_map_single(chan->device->dev, buf, len, dir);

drivers/net/ethernet/ti/davinci_cpdma.c
1049:           buffer = dma_map_single(ctlr->dev, si->data_virt, len, chan->dir);

drivers/tty/serial/ambarella_uart.c
826:            dma_phys = dma_map_single(dma_chan->device->dev,
836:            dma_phys = dma_map_single(dma_chan->device->dev,

drivers/tty/serial/samsung_tty.c
1105:   dma->rx_addr = dma_map_single(dma->rx_chan->device->dev, dma->rx_buf,
1114:   dma->tx_addr = dma_map_single(dma->tx_chan->device->dev,

drivers/tty/serial/8250/8250_dma.c
253:    dma->tx_addr = dma_map_single(dma->txchan->device->dev,

drivers/tty/serial/sh-sci.c
1600:           s->tx_dma_addr = dma_map_single(chan->device->dev,

drivers/mtd/hyperbus/hbmc-am654.c
87:     dma_dst = dma_map_single(rx_chan->device->dev, to, len, DMA_FROM_DEVICE);

drivers/mtd/nand/raw/intel-nand-controller.c
314:    buf_dma = dma_map_single(chan->device->dev, (void *)buf, len, dir);

drivers/mtd/nand/raw/sh_flctl.c
398:    dma_addr = dma_map_single(chan->device->dev, buf, len, dir);
...
```

However, if I pass the consumer peripheral's struct device to dma_map_single, the dma_mask would not be respected because the peripheral driver doesn't set it, which would lead to unexpected outcomes. For instance, even if the DMA controller is capable of handling 64-bit operations, it would still use SWIOTLB, which is really unnecessary.

So my question is which device should be dma_map_single's first argument? DMA controller or the consumer peripheral itself?

I know I could also set dma_mask in my peripheral driver, just the same as the DMA controller did, but I want to learn the best practice.

Thanks in advanced.

Regards,
Li



More information about the linux-arm-kernel mailing list