DMA Engine API Guide
====================
Vinod Koul <vinod dot koul at intel.com>
NOTE: For DMA Engine usage in async_tx please see:
Documentation/crypto/async-tx-api.txt
Below is a guide to device driver writers on how to use the Slave-DMA API of the
DMA Engine. This is applicable only for slave DMA usage only.
这篇文章仅仅针对dmaengine slave 操作模式,进行相关API接口的说明。
The slave DMA usage consists of following steps:
1. Allocate a DMA slave channel
2. Set slave and controller specific parameters
3. Get a descriptor for transaction
4. Submit the transaction
5. Issue pending requests and wait for callback notification
从模式dma的操作,需要5个步骤:
1.分配一个dma slave 通道
2.设置 slave 相关控制器的参数
3.获得一个数据传输的描述符
4.提交此次dma的动作
5.将此次动作的执行插入到可执行序列中,同时等待回调函数的通知。
1. Allocate a DMA slave channel
Channel allocation is slightly different in the slave DMA context,
client drivers typically need a channel from a particular DMA
controller only and even in some cases a specific channel is desired.
To request a channel dma_request_channel() API is used.
在slave dma的上下文中,通道的分配是稍微有些不同。客户端的驱动一般需要指定dma控制器下的通道,
甚至有时候要指定一个特殊的通道。此时,调用dma_request_channel接口函数。
Interface:
struct dma_chan *dma_request_channel(dma_cap_mask_t mask,
dma_filter_fn filter_fn,
void *filter_param);
where dma_filter_fn is defined as:
typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
The 'filter_fn' parameter is optional, but highly recommended for
slave and cyclic channels as they typically need to obtain a specific
DMA channel.
‘filter_fn’的参数是可选的。但是,强烈建议传递slave或者cyclic的通道指针,来获取一个他们所需要的通道。
When the optional 'filter_fn' parameter is NULL, dma_request_channel()
simply returns the first channel that satisfies the capability mask.
当参数是null时,dma_request_channel 只返回第一个符合掩码条件的channel。
Otherwise, the 'filter_fn' routine will be called once for each free
channel which has a capability in 'mask'. 'filter_fn' is expected to
return 'true' when the desired DMA channel is found.
另一方面,在每一次找到符合掩码条件的通道时,‘filter_fn’只调用一遍。如果函数返回‘true’,则说明已经找到
期望的DMA channel。
A channel allocated via this interface is exclusive to the caller,
until dma_release_channel() is called.
调用者通过这个接口函数进而分配一个dma channel,同时当寿命终结时,需要调用
dma_release_channel 来释放掉这个channel。
2. Set slave and controller specific parameters
Next step is always to pass some specific information to the DMA
driver. Most of the generic information which a slave DMA can use
is in struct dma_slave_config. This allows the clients to specify
DMA direction, DMA addresses, bus widths, DMA burst lengths etc
for the peripheral.
下一步就是传递一些特殊的参数给dma driver。一个slave dma,可以通过利用结构体
struct dma_slave_config 来传递大多数的需要的信息。其中,可以设置客户端的DMA传输方向,
DMA地址,总线宽度,外设的DMA突发宽度等。
If some DMA controllers have more parameters to be sent then they
should try to embed struct dma_slave_config in their controller
specific structure. That gives flexibility to client to pass more
parameters, if required.
如果一些dma控制器需要更多的参数需要传递,应该在dma_slave_config嵌入特殊结构体。
这样来说,就给了调用者更多的灵活性。
Interface:
int dmaengine_slave_config(struct dma_chan *chan,
struct dma_slave_config *config)
Please see the dma_slave_config structure definition in dmaengine.h
for a detailed explanation of the struct members. Please note
that the 'direction' member will be going away as it duplicates the
direction given in the prepare call.
更细节的描述,请参照 dmaengine.h 中的struct dma_slave_config结构体定义。
需要注意的是,‘direction’成员没什么作用,
因为在prepare函数调用时还会传递一个类似的参数。
3. Get a descriptor for transaction
For slave usage the various modes of slave transfers supported by the
DMA-engine are:
slave传输支持下面所描述的几种模式;
slave_sg - DMA a list of scatter gather buffers from/to a peripheral
slave_sg - 定义了一个buffer的链表,来进行与外设的数据传输。或者发送或者接收。
dma_cyclic - Perform a cyclic DMA operation from/to a peripheral till the
operation is explicitly stopped.
dma_cyclic - 对外设的循环操作,直到动作被中止。
interleaved_dma - This is common to Slave as well as M2M clients. For slave
address of devices' fifo could be already known to the driver.
Various types of operations could be expressed by setting
appropriate values to the 'dma_interleaved_template' members.
交叉 dma - 这种模式相当于一种M2M。设备的FIFO对于驱动程序来说是已知的。通过改变dma_interleaved_template
的值进而进行动作的切换。
A non-NULL return of this transfer API represents a "descriptor" for
the given transaction.
返回一个non-NULL,表示函数调用成功。
Interface:
struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
struct dma_chan *chan, struct scatterlist *sgl,
unsigned int sg_len, enum dma_data_direction direction,
unsigned long flags);
struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
size_t period_len, enum dma_data_direction direction);
struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
struct dma_chan *chan, struct dma_interleaved_template *xt,
unsigned long flags);
The peripheral driver is expected to have mapped the scatterlist for
the DMA operation prior to calling dmaengine_prep_slave_sg(), and must
keep the scatterlist mapped until the DMA operation has completed.
The scatterlist must be mapped using the DMA struct device.
If a mapping needs to be synchronized later, dma_sync_*_for_*() must be
called using the DMA struct device, too.
So, normal setup should look like this:
nr_sg = dma_map_sg(chan->device->dev, sgl, sg_len);
if (nr_sg == 0)
/* error */
desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags);
Once a descriptor has been obtained, the callback information can be
added and the descriptor must then be submitted. Some DMA engine
drivers may hold a spinlock between a successful preparation and
submission so it is important that these two operations are closely
paired.
一旦获得描述符,同时添加一些回调信息,同时描述符必须被submite。一些dma驱动,可能在prepare和sumbit时,
持有自旋锁,因此两个操作的连续性是非常重要的。
Note:
Although the async_tx API specifies that completion callback
routines cannot submit any new operations, this is not the
case for slave/cyclic DMA.
即使存在async_tx API 的特殊性,即完成的回调函数不能执行新的操作,但是对于
slave/cyclic来说,这并不是什么问题。
For slave DMA, the subsequent transaction may not be available
for submission prior to callback function being invoked, so
slave DMA callbacks are permitted to prepare and submit a new
transaction.
对slave DMA来说,子任务的回调函数被调用的优先级的信息,子任务是不可知的。
所以,slave DMA 的回调函数是不允许进行preprare 和submit新的任务。
For cyclic DMA, a callback function may wish to terminate the
DMA via dmaengine_terminate_all().
对于cyclic DMA,回调函数可以通过调用 dmaengine_terminate_all来结束此次dma动作。
Therefore, it is important that DMA engine drivers drop any
locks before calling the callback function which may cause a
deadlock.
因此,dma 驱动程序,在调用回调函数前,解锁动作是非常重要的,不然可能会导致死锁。
Note that callbacks will always be invoked from the DMA
engines tasklet, never from interrupt context.
注意的是,回调函数调用的时机是由DMA 引擎来调用,而不是中断上下文。
4. Submit the transaction
Once the descriptor has been prepared and the callback information
added, it must be placed on the DMA engine drivers pending queue.
一旦描述符就绪,回调函数添加完毕,必须要把驱动程序添加到就绪列表。
Interface:
dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
This returns a cookie can be used to check the progress of DMA engine
activity via other DMA engine calls not covered in this document.
函数返回一个cookie,可以用来检查dma动作的进度。
dmaengine_submit() will not start the DMA operation, it merely adds
it to the pending queue. For this, see step 5, dma_async_issue_pending.
submit动作并不实际出发dma真实的动作。仅仅是添加到就绪队列。
5. Issue pending DMA requests and wait for callback notification
The transactions in the pending queue can be activated by calling the
issue_pending API. If channel is idle then the first transaction in
queue is started and subsequent ones queued up.
通过调用issue_pending API激活dma进行传输。如果这个通道是空闲的,队列的第一个动作将启动
同时,后边的排队等候。
On completion of each DMA operation, the next in queue is started and
a tasklet triggered. The tasklet will then call the client driver
completion callback routine for notification, if set.
每一次dma动作的完成,队列的下一个动作就会被触发,同时对应的tasklet被触发。如果设置了回调函数
,动作完成后,回调函数将会被调用执行。
Interface:
void dma_async_issue_pending(struct dma_chan *chan);
Further APIs:
1. int dmaengine_terminate_all(struct dma_chan *chan)
This causes all activity for the DMA channel to be stopped, and may
discard data in the DMA FIFO which hasn't been fully transferred.
No callback functions will be called for any incomplete transfers.
强制中止传输过程,可能会丢掉FIFO中未完成转换的数据。
2. int dmaengine_pause(struct dma_chan *chan)
This pauses activity on the DMA channel without data loss.
3. int dmaengine_resume(struct dma_chan *chan)
Resume a previously paused DMA channel. It is invalid to resume a
channel which is not currently paused.
恢复一个暂停的DMA 通道。如果一个通道没有执行过暂停,那么恢复这个动作
是无效的。
4. enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
This can be used to check the status of the channel. Please see
the documentation in include/linux/dmaengine.h for a more complete
description of this API.
This can be used in conjunction with dma_async_is_complete() and
the cookie returned from dmaengine_submit() to check for
completion of a specific DMA transaction.
Note:
Not all DMA engine drivers can return reliable information for
a running DMA channel. It is recommended that DMA engine users
pause or stop (via dmaengine_terminate_all()) the channel before
using this API.
对于运行中的channel,不能保证所有的DMA接口都能返回可靠的数据。因此,
建议在调用此类接口之前,首先对通道执行 pause 或者stop。
====================
Vinod Koul <vinod dot koul at intel.com>
NOTE: For DMA Engine usage in async_tx please see:
Documentation/crypto/async-tx-api.txt
Below is a guide to device driver writers on how to use the Slave-DMA API of the
DMA Engine. This is applicable only for slave DMA usage only.
这篇文章仅仅针对dmaengine slave 操作模式,进行相关API接口的说明。
The slave DMA usage consists of following steps:
1. Allocate a DMA slave channel
2. Set slave and controller specific parameters
3. Get a descriptor for transaction
4. Submit the transaction
5. Issue pending requests and wait for callback notification
从模式dma的操作,需要5个步骤:
1.分配一个dma slave 通道
2.设置 slave 相关控制器的参数
3.获得一个数据传输的描述符
4.提交此次dma的动作
5.将此次动作的执行插入到可执行序列中,同时等待回调函数的通知。
1. Allocate a DMA slave channel
Channel allocation is slightly different in the slave DMA context,
client drivers typically need a channel from a particular DMA
controller only and even in some cases a specific channel is desired.
To request a channel dma_request_channel() API is used.
在slave dma的上下文中,通道的分配是稍微有些不同。客户端的驱动一般需要指定dma控制器下的通道,
甚至有时候要指定一个特殊的通道。此时,调用dma_request_channel接口函数。
Interface:
struct dma_chan *dma_request_channel(dma_cap_mask_t mask,
dma_filter_fn filter_fn,
void *filter_param);
where dma_filter_fn is defined as:
typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
The 'filter_fn' parameter is optional, but highly recommended for
slave and cyclic channels as they typically need to obtain a specific
DMA channel.
‘filter_fn’的参数是可选的。但是,强烈建议传递slave或者cyclic的通道指针,来获取一个他们所需要的通道。
When the optional 'filter_fn' parameter is NULL, dma_request_channel()
simply returns the first channel that satisfies the capability mask.
当参数是null时,dma_request_channel 只返回第一个符合掩码条件的channel。
Otherwise, the 'filter_fn' routine will be called once for each free
channel which has a capability in 'mask'. 'filter_fn' is expected to
return 'true' when the desired DMA channel is found.
另一方面,在每一次找到符合掩码条件的通道时,‘filter_fn’只调用一遍。如果函数返回‘true’,则说明已经找到
期望的DMA channel。
A channel allocated via this interface is exclusive to the caller,
until dma_release_channel() is called.
调用者通过这个接口函数进而分配一个dma channel,同时当寿命终结时,需要调用
dma_release_channel 来释放掉这个channel。
2. Set slave and controller specific parameters
Next step is always to pass some specific information to the DMA
driver. Most of the generic information which a slave DMA can use
is in struct dma_slave_config. This allows the clients to specify
DMA direction, DMA addresses, bus widths, DMA burst lengths etc
for the peripheral.
下一步就是传递一些特殊的参数给dma driver。一个slave dma,可以通过利用结构体
struct dma_slave_config 来传递大多数的需要的信息。其中,可以设置客户端的DMA传输方向,
DMA地址,总线宽度,外设的DMA突发宽度等。
If some DMA controllers have more parameters to be sent then they
should try to embed struct dma_slave_config in their controller
specific structure. That gives flexibility to client to pass more
parameters, if required.
如果一些dma控制器需要更多的参数需要传递,应该在dma_slave_config嵌入特殊结构体。
这样来说,就给了调用者更多的灵活性。
Interface:
int dmaengine_slave_config(struct dma_chan *chan,
struct dma_slave_config *config)
Please see the dma_slave_config structure definition in dmaengine.h
for a detailed explanation of the struct members. Please note
that the 'direction' member will be going away as it duplicates the
direction given in the prepare call.
更细节的描述,请参照 dmaengine.h 中的struct dma_slave_config结构体定义。
需要注意的是,‘direction’成员没什么作用,
因为在prepare函数调用时还会传递一个类似的参数。
3. Get a descriptor for transaction
For slave usage the various modes of slave transfers supported by the
DMA-engine are:
slave传输支持下面所描述的几种模式;
slave_sg - DMA a list of scatter gather buffers from/to a peripheral
slave_sg - 定义了一个buffer的链表,来进行与外设的数据传输。或者发送或者接收。
dma_cyclic - Perform a cyclic DMA operation from/to a peripheral till the
operation is explicitly stopped.
dma_cyclic - 对外设的循环操作,直到动作被中止。
interleaved_dma - This is common to Slave as well as M2M clients. For slave
address of devices' fifo could be already known to the driver.
Various types of operations could be expressed by setting
appropriate values to the 'dma_interleaved_template' members.
交叉 dma - 这种模式相当于一种M2M。设备的FIFO对于驱动程序来说是已知的。通过改变dma_interleaved_template
的值进而进行动作的切换。
A non-NULL return of this transfer API represents a "descriptor" for
the given transaction.
返回一个non-NULL,表示函数调用成功。
Interface:
struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
struct dma_chan *chan, struct scatterlist *sgl,
unsigned int sg_len, enum dma_data_direction direction,
unsigned long flags);
struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
size_t period_len, enum dma_data_direction direction);
struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
struct dma_chan *chan, struct dma_interleaved_template *xt,
unsigned long flags);
The peripheral driver is expected to have mapped the scatterlist for
the DMA operation prior to calling dmaengine_prep_slave_sg(), and must
keep the scatterlist mapped until the DMA operation has completed.
The scatterlist must be mapped using the DMA struct device.
If a mapping needs to be synchronized later, dma_sync_*_for_*() must be
called using the DMA struct device, too.
So, normal setup should look like this:
nr_sg = dma_map_sg(chan->device->dev, sgl, sg_len);
if (nr_sg == 0)
/* error */
desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags);
Once a descriptor has been obtained, the callback information can be
added and the descriptor must then be submitted. Some DMA engine
drivers may hold a spinlock between a successful preparation and
submission so it is important that these two operations are closely
paired.
一旦获得描述符,同时添加一些回调信息,同时描述符必须被submite。一些dma驱动,可能在prepare和sumbit时,
持有自旋锁,因此两个操作的连续性是非常重要的。
Note:
Although the async_tx API specifies that completion callback
routines cannot submit any new operations, this is not the
case for slave/cyclic DMA.
即使存在async_tx API 的特殊性,即完成的回调函数不能执行新的操作,但是对于
slave/cyclic来说,这并不是什么问题。
For slave DMA, the subsequent transaction may not be available
for submission prior to callback function being invoked, so
slave DMA callbacks are permitted to prepare and submit a new
transaction.
对slave DMA来说,子任务的回调函数被调用的优先级的信息,子任务是不可知的。
所以,slave DMA 的回调函数是不允许进行preprare 和submit新的任务。
For cyclic DMA, a callback function may wish to terminate the
DMA via dmaengine_terminate_all().
对于cyclic DMA,回调函数可以通过调用 dmaengine_terminate_all来结束此次dma动作。
Therefore, it is important that DMA engine drivers drop any
locks before calling the callback function which may cause a
deadlock.
因此,dma 驱动程序,在调用回调函数前,解锁动作是非常重要的,不然可能会导致死锁。
Note that callbacks will always be invoked from the DMA
engines tasklet, never from interrupt context.
注意的是,回调函数调用的时机是由DMA 引擎来调用,而不是中断上下文。
4. Submit the transaction
Once the descriptor has been prepared and the callback information
added, it must be placed on the DMA engine drivers pending queue.
一旦描述符就绪,回调函数添加完毕,必须要把驱动程序添加到就绪列表。
Interface:
dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
This returns a cookie can be used to check the progress of DMA engine
activity via other DMA engine calls not covered in this document.
函数返回一个cookie,可以用来检查dma动作的进度。
dmaengine_submit() will not start the DMA operation, it merely adds
it to the pending queue. For this, see step 5, dma_async_issue_pending.
submit动作并不实际出发dma真实的动作。仅仅是添加到就绪队列。
5. Issue pending DMA requests and wait for callback notification
The transactions in the pending queue can be activated by calling the
issue_pending API. If channel is idle then the first transaction in
queue is started and subsequent ones queued up.
通过调用issue_pending API激活dma进行传输。如果这个通道是空闲的,队列的第一个动作将启动
同时,后边的排队等候。
On completion of each DMA operation, the next in queue is started and
a tasklet triggered. The tasklet will then call the client driver
completion callback routine for notification, if set.
每一次dma动作的完成,队列的下一个动作就会被触发,同时对应的tasklet被触发。如果设置了回调函数
,动作完成后,回调函数将会被调用执行。
Interface:
void dma_async_issue_pending(struct dma_chan *chan);
Further APIs:
1. int dmaengine_terminate_all(struct dma_chan *chan)
This causes all activity for the DMA channel to be stopped, and may
discard data in the DMA FIFO which hasn't been fully transferred.
No callback functions will be called for any incomplete transfers.
强制中止传输过程,可能会丢掉FIFO中未完成转换的数据。
2. int dmaengine_pause(struct dma_chan *chan)
This pauses activity on the DMA channel without data loss.
3. int dmaengine_resume(struct dma_chan *chan)
Resume a previously paused DMA channel. It is invalid to resume a
channel which is not currently paused.
恢复一个暂停的DMA 通道。如果一个通道没有执行过暂停,那么恢复这个动作
是无效的。
4. enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
This can be used to check the status of the channel. Please see
the documentation in include/linux/dmaengine.h for a more complete
description of this API.
This can be used in conjunction with dma_async_is_complete() and
the cookie returned from dmaengine_submit() to check for
completion of a specific DMA transaction.
Note:
Not all DMA engine drivers can return reliable information for
a running DMA channel. It is recommended that DMA engine users
pause or stop (via dmaengine_terminate_all()) the channel before
using this API.
对于运行中的channel,不能保证所有的DMA接口都能返回可靠的数据。因此,
建议在调用此类接口之前,首先对通道执行 pause 或者stop。