dw_dmac driver questions
Hi. I'm writing a new DMA driver, using dw_dmac driver in kernel 2.6.28 as a reference implementation.
I can see an outline of the dw_dmac driver and framework (well, hopefully), but have some questions in details.
1. map/unmap DMA buffers for slave transfer
For slave-DMA, it seems dmac driver is responsible for mapping DMA buffers, and client is responsible for unmapping them. Is it right?
Now dwc_descriptor_complete() unmaps DMA buffer unless DMA_COMPL_SKIP_XXX_UNMAP set. These unmapping should be done only for non-slave transfers? I.e. I suppose "if (!dwc->dws)" is required around dma_unmap_page() pair.
I also wonder why dwc_prep_slave_sg() calculates total_len. The total_len is stored in first descriptor of the chain and it is only referenced for unmapping DMA buffers. But the scatterlist may contain uncontinuous buffers, so they can not be unmaped at a time.
2. dwc_is_tx_complete() and dwc->lock
The function dwc_is_tx_complete() calls dwc_scan_descriptors() without dwc->lock (and disabling bh). Is it safe? All other callers of dwc_scan_descriptors() take dwc->lock and disable bh.
3. dwc->queue list management
The function dwc_tx_submit() add the descriptor to dwc->queue list if active list was not empty. But it does not manage lli.llp list. And all descriptors in the queue list will be moved to active list at a time. So it seems non-first descriptors in queue list will never processed by the hardware.
The dwc_tx_submit() should rewrite lli.llp of the last descriptor in queue list (it it had children, the last children of it) by txd.phys of newly queued descriptor. Or, only first elements of queue list should be moved to active list at a time.
Is my analysis correct?
BTW, I found some redundant code in the driver.
* memset(dw, 0, sizeof *dw) seems redundant while dw is allocated kzalloc().
* platform_get_drvdata(pdev) is called twice in dw_shutdown, dw_suspend_late.
Both of them harmless.
---
Atsushi Nemoto
Quoted 5 lines · Atsushi Nemoto
Hi. I'm writing a new DMA driver, using
dw_dmacdriver in kernel 2.6.28 as a reference implementation.I can see an outline of the
dw_dmacdriver and framework (well, hopefully), but have some questions in details.
One more.
4. This is a comment on head of dwc_handle_error().
/*
* The descriptor currently at the head of the active list is
* borked. Since we don't have any way to report errors, we'll
* just have to scream loudly and try to carry on. */
But, the bad descriptor can be at any place of the active list, no ? For example, if the active list contained two descriptor and latter was broken and tasklet was delayed by some reason, the head of the list should be good.
---
Atsushi Nemoto
Quoted 8 lines · Atsushi Nemoto
Quoted 5 lines · Atsushi Nemoto
Hi. I'm writing a new DMA driver, using
dw_dmacdriver in kernel 2.6.28 as a reference implementation.I can see an outline of the
dw_dmacdriver and framework (well, hopefully), but have some questions in details.One more.
One more again.
5. dwc_issue_pending()
Is this needed? The dwc_tx_submit might queue the descriptor, but it should be issued automatically after completion of all descriptors in active list.
It yes, it seems implementation like this would be possible:
* dwc_tx_submit() just always add the descriptor to queue list even if active list was empty.
* dwc_issue_pending() call dwc_dostart() and move queued descriptor(s) to active list, if the active list was empty.
But I'm not sure "chain as many as possible at a time" is really better then "start engine as soon as possible".
---
Atsushi Nemoto
Quoted 11 lines · Atsushi Nemoto
Hi. I'm writing a new DMA driver, using
dw_dmacdriver in kernel 2.6.28 as a reference implementation.I can see an outline of the
dw_dmacdriver and framework (well, hopefully), but have some questions in details.1. map/unmap DMA buffers for slave transfer
For slave-DMA, it seems dmac driver is responsible for mapping DMA buffers, and client is responsible for unmapping them. Is it right?
No, it's the other way around, unless DMA_COMPL_SKIP_*_UNMAP is set. But I think the dw_dmac driver wrongly maps the buffers before queuing them.
Quoted 4 lines
Now
dwc_descriptor_complete()unmaps DMA buffer unlessDMA_COMPL_SKIP_XXX_UNMAPset. These unmapping should be done only for non-slave transfers? I.e. I suppose "if (!dwc->dws)" is required arounddma_unmap_page()pair.
Yes, perhaps. Or we could just forcibly set those flags in prep_slave_sg().
Quoted 4 lines
I also wonder why
dwc_prep_slave_sg()calculatestotal_len. Thetotal_lenis stored in first descriptor of the chain and it is only referenced for unmapping DMA buffers. But the scatterlist may contain uncontinuous buffers, so they can not be unmaped at a time.
Yes, it is indeed a bit pointless to keep the total length around for scatterlists.
Quoted 5 lines
2.
dwc_is_tx_complete()anddwc->lockThe function
dwc_is_tx_complete()callsdwc_scan_descriptors()withoutdwc->lock(and disabling bh). Is it safe? All other callers ofdwc_scan_descriptors()takedwc->lockand disable bh.
No, that does indeed look unsafe. We should probably take dwc->lock and disable bh around the call to dwc_scan_descriptors().
Quoted 8 lines
3.
dwc->queuelist managementThe function
dwc_tx_submit()add the descriptor todwc->queuelist if active list was not empty. But it does not manage lli.llp list. And all descriptors in the queue list will be moved to active list at a time. So it seems non-first descriptors in queue list will never processed by the hardware.
Quoted 6 lines
The
dwc_tx_submit()should rewrite lli.llp of the last descriptor in queue list (it it had children, the last children of it) by txd.phys of newly queued descriptor. Or, only first elements of queue list should be moved to active list at a time.Is my analysis correct?
Yes, I think you're right. lli.llp of the last queued entry should be updated when adding new entries to the queue.
Quoted 5 lines
BTW, I found some redundant code in the driver.
*
memset(dw, 0, sizeof *dw)seems redundant while dw is allocatedkzalloc().
Indeed.
Quoted 4 lines
*
platform_get_drvdata(pdev)is called twice indw_shutdown,dw_suspend_late.Both of them harmless.
Right.
Quoted 8 lines
4. This is a comment on head of
dwc_handle_error()./*
* The descriptor currently at the head of the active list is
* borked. Since we don't have any way to report errors, we'll
* just have to scream loudly and try to carry on. */
Quoted 4 lines
But, the bad descriptor can be at any place of the active list, no ? For example, if the active list contained two descriptor and latter was broken and tasklet was delayed by some reason, the head of the list should be good.
Since dwc_scan_descriptors() was just called, all descriptors that were completed successfully have been removed from the active list. So the first entry must be the broken one.
Haavard
Quoted 8 lines · Haavard Skinnemoen
Quoted 4 lines
1. map/unmap DMA buffers for slave transfer
For slave-DMA, it seems dmac driver is responsible for mapping DMA buffers, and client is responsible for unmapping them. Is it right?
No, it's the other way around, unless DMA_COMPL_SKIP_*_UNMAP is set. But I think the
dw_dmacdriver wrongly maps the buffers before queuing them.
Well, I'm confused... I reference atmel-mci for client and dw_dmac for dma engine. Currently, dw_dmac calls dma_map_sg() and atmel-mci calls dma_unmap_sg(). Do you mean atmel-mci will be changed to call dma_map_sg()?
Quoted 8 lines
Quoted 8 lines
4. This is a comment on head of
dwc_handle_error()./*
* The descriptor currently at the head of the active list is
* borked. Since we don't have any way to report errors, we'll
* just have to scream loudly and try to carry on. */
Quoted 8 lines
Quoted 4 lines
But, the bad descriptor can be at any place of the active list, no ? For example, if the active list contained two descriptor and latter was broken and tasklet was delayed by some reason, the head of the list should be good.
Since
dwc_scan_descriptors()was just called, all descriptors that were completed successfully have been removed from the active list. So the first entry must be the broken one.
But the removal from the active list is done in tasklet too. If the CPU was slow and DMA engine was fast enough, DMA engine will process multiple descriptors before the tasklet called, no ?
---
Atsushi Nemoto
Quoted 7 lines · Atsushi Nemoto
Quoted 5 lines · Haavard Skinnemoen
Quoted 4 lines
1. map/unmap DMA buffers for slave transfer
For slave-DMA, it seems dmac driver is responsible for mapping DMA buffers, and client is responsible for unmapping them. Is it right?
Quoted 8 lines
Quoted 3 lines
No, it's the other way around, unless DMA_COMPL_SKIP_*_UNMAP is set. But I think the
dw_dmacdriver wrongly maps the buffers before queuing them.Well, I'm confused... I reference atmel-mci for client and
dw_dmacfor dma engine. Currently,dw_dmaccallsdma_map_sg()and atmel-mci callsdma_unmap_sg(). Do you mean atmel-mci will be changed to calldma_map_sg()?
Yes, I think it should.
Preferably, I'd like the client to do both mapping and unmapping in all cases since it knows best what's actually needed. For example, some drivers may be using DMA-coherent buffers, which don't need to be mapped at all.
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
4. This is a comment on head of
dwc_handle_error()./*
* The descriptor currently at the head of the active list is
* borked. Since we don't have any way to report errors, we'll
* just have to scream loudly and try to carry on. */But, the bad descriptor can be at any place of the active list, no ? For example, if the active list contained two descriptor and latter was broken and tasklet was delayed by some reason, the head of the list should be good.
Quoted 7 lines
Quoted 3 lines
Since
dwc_scan_descriptors()was just called, all descriptors that were completed successfully have been removed from the active list. So the first entry must be the broken one.But the removal from the active list is done in tasklet too. If the CPU was slow and DMA engine was fast enough, DMA engine will process multiple descriptors before the tasklet called, no ?
The DMA engine stops if it encounters a bad address.
Haavard
Quoted 11 lines · Haavard Skinnemoen
Quoted 4 lines
Well, I'm confused... I reference atmel-mci for client and
dw_dmacfor dma engine. Currently,dw_dmaccallsdma_map_sg()and atmel-mci callsdma_unmap_sg(). Do you mean atmel-mci will be changed to calldma_map_sg()?Yes, I think it should.
Preferably, I'd like the client to do both mapping and unmapping in all cases since it knows best what's actually needed. For example, some drivers may be using DMA-coherent buffers, which don't need to be mapped at all.
I see, thanks.
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 8 lines
4. This is a comment on head of
dwc_handle_error()./*
* The descriptor currently at the head of the active list is
* borked. Since we don't have any way to report errors, we'll
* just have to scream loudly and try to carry on. */Quoted 4 lines
But, the bad descriptor can be at any place of the active list, no ? For example, if the active list contained two descriptor and latter was broken and tasklet was delayed by some reason, the head of the list should be good.
Since
dwc_scan_descriptors()was just called, all descriptors that were completed successfully have been removed from the active list. So the first entry must be the broken one.
Quoted 5 lines
Quoted 3 lines
But the removal from the active list is done in tasklet too. If the CPU was slow and DMA engine was fast enough, DMA engine will process multiple descriptors before the tasklet called, no ?
The DMA engine stops if it encounters a bad address.
Yes. And it does not mean the bad descriptor is at head of the active queue.
1) driver enqueue desc A to active list and start DMA engine
2) driver enqueue desc B to queue list
3) driver enqueue desc C to queue list (desc C contains bad address)
4) DMA engine finish desc A and raise interrupt
5) tasklet remove desc A from active list and move desc B and C to active list
6) DMA engine finish desc B and raise interrupt
7) DMA engine abort desc C and raise interrupt
8) tasklet detect error
The point is the order of (7) and (8) cannot be expected. If (7) comes first, the head of the active list contains desc B at (8) and that is not a bad descriptor.
---
Atsushi Nemoto
Quoted 22 lines · Atsushi Nemoto
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Since
dwc_scan_descriptors()was just called, all descriptors that were completed successfully have been removed from the active list. So the first entry must be the broken one.But the removal from the active list is done in tasklet too. If the CPU was slow and DMA engine was fast enough, DMA engine will process multiple descriptors before the tasklet called, no ?
The DMA engine stops if it encounters a bad address.
Yes. And it does not mean the bad descriptor is at head of the active queue.
1) driver enqueue desc A to active list and start DMA engine
2) driver enqueue desc B to queue list
3) driver enqueue desc C to queue list (desc C contains bad address)
4) DMA engine finish desc A and raise interrupt
5) tasklet remove desc A from active list and move desc B and C to active list
6) DMA engine finish desc B and raise interrupt
7) DMA engine abort desc C and raise interrupt
8) tasklet detect error
Quoted 3 lines
The point is the order of (7) and (8) cannot be expected. If (7) comes first, the head of the active list contains desc B at (8) and that is not a bad descriptor.
The tasklet won't detect any errors unless the DMA controller flags it, so (7) must happen before (8). That does not necessarily mean that the interrupts from (6) and (7) get handled before (8), but I don't think it matters because dwc_handle_error() calls dwc_scan_descriptors(), which will remove desc B from the active list if it is finished. And it must be finished if desc C has failed, so the failed descriptor will always be at the head of the queue after dwc_scan_descriptors() returns.
Haavard
Quoted 13 lines · Haavard Skinnemoen
Quoted 9 lines
1) driver enqueue desc A to active list and start DMA engine
2) driver enqueue desc B to queue list
3) driver enqueue desc C to queue list (desc C contains bad address)
4) DMA engine finish desc A and raise interrupt
5) tasklet remove desc A from active list and move desc B and C to active list
6) DMA engine finish desc B and raise interrupt
7) DMA engine abort desc C and raise interrupt
8) tasklet detect errorQuoted 3 lines
The point is the order of (7) and (8) cannot be expected. If (7) comes first, the head of the active list contains desc B at (8) and that is not a bad descriptor.
Quoted 7 lines
The tasklet won't detect any errors unless the DMA controller flags it, so (7) must happen before (8). That does not necessarily mean that the interrupts from (6) and (7) get handled before (8), but I don't think it matters because
dwc_handle_error()callsdwc_scan_descriptors(), which will remove desc B from the active list if it is finished. And it must be finished if desc C has failed, so the failed descriptor will always be at the head of the queue afterdwc_scan_descriptors()returns.
Oh I understand. I overlooked dwc_scan_descriptors() call at beginning of dwc_handle_error(). Thank you for explanation.
---
Atsushi Nemoto