1 // SPDX-License-Identifier: GPL-2.0
3 * Dmaengine driver base library for DMA controllers, found on SH-based SoCs
5 * extracted from shdma.c
7 * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
8 * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
9 * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
10 * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
13 #include <linux/delay.h>
14 #include <linux/shdma-base.h>
15 #include <linux/dmaengine.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
23 #include "../dmaengine.h"
25 /* DMA descriptor control */
26 enum shdma_desc_status
{
30 DESC_COMPLETED
, /* completed, have to call callback */
31 DESC_WAITING
, /* callback called, waiting for ack / re-submit */
34 #define NR_DESCS_PER_CHANNEL 32
36 #define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan)
37 #define to_shdma_dev(d) container_of(d, struct shdma_dev, dma_dev)
40 * For slave DMA we assume, that there is a finite number of DMA slaves in the
41 * system, and that each such slave can only use a finite number of channels.
42 * We use slave channel IDs to make sure, that no such slave channel ID is
43 * allocated more than once.
45 static unsigned int slave_num
= 256;
46 module_param(slave_num
, uint
, 0444);
48 /* A bitmask with slave_num bits */
49 static unsigned long *shdma_slave_used
;
51 /* Called under spin_lock_irq(&schan->chan_lock") */
52 static void shdma_chan_xfer_ld_queue(struct shdma_chan
*schan
)
54 struct shdma_dev
*sdev
= to_shdma_dev(schan
->dma_chan
.device
);
55 const struct shdma_ops
*ops
= sdev
->ops
;
56 struct shdma_desc
*sdesc
;
59 if (ops
->channel_busy(schan
))
62 /* Find the first not transferred descriptor */
63 list_for_each_entry(sdesc
, &schan
->ld_queue
, node
)
64 if (sdesc
->mark
== DESC_SUBMITTED
) {
65 ops
->start_xfer(schan
, sdesc
);
70 static dma_cookie_t
shdma_tx_submit(struct dma_async_tx_descriptor
*tx
)
72 struct shdma_desc
*chunk
, *c
, *desc
=
73 container_of(tx
, struct shdma_desc
, async_tx
);
74 struct shdma_chan
*schan
= to_shdma_chan(tx
->chan
);
75 dma_async_tx_callback callback
= tx
->callback
;
79 spin_lock_irq(&schan
->chan_lock
);
81 power_up
= list_empty(&schan
->ld_queue
);
83 cookie
= dma_cookie_assign(tx
);
85 /* Mark all chunks of this descriptor as submitted, move to the queue */
86 list_for_each_entry_safe(chunk
, c
, desc
->node
.prev
, node
) {
88 * All chunks are on the global ld_free, so, we have to find
89 * the end of the chain ourselves
91 if (chunk
!= desc
&& (chunk
->mark
== DESC_IDLE
||
92 chunk
->async_tx
.cookie
> 0 ||
93 chunk
->async_tx
.cookie
== -EBUSY
||
94 &chunk
->node
== &schan
->ld_free
))
96 chunk
->mark
= DESC_SUBMITTED
;
97 if (chunk
->chunks
== 1) {
98 chunk
->async_tx
.callback
= callback
;
99 chunk
->async_tx
.callback_param
= tx
->callback_param
;
101 /* Callback goes to the last chunk */
102 chunk
->async_tx
.callback
= NULL
;
104 chunk
->cookie
= cookie
;
105 list_move_tail(&chunk
->node
, &schan
->ld_queue
);
107 dev_dbg(schan
->dev
, "submit #%d@%p on %d\n",
108 tx
->cookie
, &chunk
->async_tx
, schan
->id
);
113 schan
->pm_state
= SHDMA_PM_BUSY
;
115 ret
= pm_runtime_get(schan
->dev
);
117 spin_unlock_irq(&schan
->chan_lock
);
119 dev_err(schan
->dev
, "%s(): GET = %d\n", __func__
, ret
);
121 pm_runtime_barrier(schan
->dev
);
123 spin_lock_irq(&schan
->chan_lock
);
125 /* Have we been reset, while waiting? */
126 if (schan
->pm_state
!= SHDMA_PM_ESTABLISHED
) {
127 struct shdma_dev
*sdev
=
128 to_shdma_dev(schan
->dma_chan
.device
);
129 const struct shdma_ops
*ops
= sdev
->ops
;
130 dev_dbg(schan
->dev
, "Bring up channel %d\n",
133 * TODO: .xfer_setup() might fail on some platforms.
134 * Make it int then, on error remove chunks from the
137 ops
->setup_xfer(schan
, schan
->slave_id
);
139 if (schan
->pm_state
== SHDMA_PM_PENDING
)
140 shdma_chan_xfer_ld_queue(schan
);
141 schan
->pm_state
= SHDMA_PM_ESTABLISHED
;
145 * Tell .device_issue_pending() not to run the queue, interrupts
148 schan
->pm_state
= SHDMA_PM_PENDING
;
151 spin_unlock_irq(&schan
->chan_lock
);
156 /* Called with desc_lock held */
157 static struct shdma_desc
*shdma_get_desc(struct shdma_chan
*schan
)
159 struct shdma_desc
*sdesc
;
161 list_for_each_entry(sdesc
, &schan
->ld_free
, node
)
162 if (sdesc
->mark
!= DESC_PREPARED
) {
163 BUG_ON(sdesc
->mark
!= DESC_IDLE
);
164 list_del(&sdesc
->node
);
171 static int shdma_setup_slave(struct shdma_chan
*schan
, dma_addr_t slave_addr
)
173 struct shdma_dev
*sdev
= to_shdma_dev(schan
->dma_chan
.device
);
174 const struct shdma_ops
*ops
= sdev
->ops
;
177 if (schan
->dev
->of_node
) {
178 match
= schan
->hw_req
;
179 ret
= ops
->set_slave(schan
, match
, slave_addr
, true);
183 match
= schan
->real_slave_id
;
186 if (schan
->real_slave_id
< 0 || schan
->real_slave_id
>= slave_num
)
189 if (test_and_set_bit(schan
->real_slave_id
, shdma_slave_used
))
192 ret
= ops
->set_slave(schan
, match
, slave_addr
, false);
194 clear_bit(schan
->real_slave_id
, shdma_slave_used
);
198 schan
->slave_id
= schan
->real_slave_id
;
203 static int shdma_alloc_chan_resources(struct dma_chan
*chan
)
205 struct shdma_chan
*schan
= to_shdma_chan(chan
);
206 struct shdma_dev
*sdev
= to_shdma_dev(schan
->dma_chan
.device
);
207 const struct shdma_ops
*ops
= sdev
->ops
;
208 struct shdma_desc
*desc
;
209 struct shdma_slave
*slave
= chan
->private;
213 * This relies on the guarantee from dmaengine that alloc_chan_resources
214 * never runs concurrently with itself or free_chan_resources.
217 /* Legacy mode: .private is set in filter */
218 schan
->real_slave_id
= slave
->slave_id
;
219 ret
= shdma_setup_slave(schan
, 0);
223 /* Normal mode: real_slave_id was set by filter */
224 schan
->slave_id
= -EINVAL
;
227 schan
->desc
= kcalloc(NR_DESCS_PER_CHANNEL
,
228 sdev
->desc_size
, GFP_KERNEL
);
233 schan
->desc_num
= NR_DESCS_PER_CHANNEL
;
235 for (i
= 0; i
< NR_DESCS_PER_CHANNEL
; i
++) {
236 desc
= ops
->embedded_desc(schan
->desc
, i
);
237 dma_async_tx_descriptor_init(&desc
->async_tx
,
239 desc
->async_tx
.tx_submit
= shdma_tx_submit
;
240 desc
->mark
= DESC_IDLE
;
242 list_add(&desc
->node
, &schan
->ld_free
);
245 return NR_DESCS_PER_CHANNEL
;
250 clear_bit(slave
->slave_id
, shdma_slave_used
);
251 chan
->private = NULL
;
256 * This is the standard shdma filter function to be used as a replacement to the
257 * "old" method, using the .private pointer.
258 * You always have to pass a valid slave id as the argument, old drivers that
259 * pass ERR_PTR(-EINVAL) as a filter parameter and set it up in dma_slave_config
260 * need to be updated so we can remove the slave_id field from dma_slave_config.
261 * parameter. If this filter is used, the slave driver, after calling
262 * dma_request_channel(), will also have to call dmaengine_slave_config() with
263 * .direction, and either .src_addr or .dst_addr set.
265 * NOTE: this filter doesn't support multiple DMAC drivers with the DMA_SLAVE
266 * capability! If this becomes a requirement, hardware glue drivers, using this
267 * services would have to provide their own filters, which first would check
268 * the device driver, similar to how other DMAC drivers, e.g., sa11x0-dma.c, do
269 * this, and only then, in case of a match, call this common filter.
270 * NOTE 2: This filter function is also used in the DT case by shdma_of_xlate().
271 * In that case the MID-RID value is used for slave channel filtering and is
272 * passed to this function in the "arg" parameter.
274 bool shdma_chan_filter(struct dma_chan
*chan
, void *arg
)
276 struct shdma_chan
*schan
;
277 struct shdma_dev
*sdev
;
278 int slave_id
= (long)arg
;
281 /* Only support channels handled by this driver. */
282 if (chan
->device
->device_alloc_chan_resources
!=
283 shdma_alloc_chan_resources
)
286 schan
= to_shdma_chan(chan
);
287 sdev
= to_shdma_dev(chan
->device
);
290 * For DT, the schan->slave_id field is generated by the
291 * set_slave function from the slave ID that is passed in
292 * from xlate. For the non-DT case, the slave ID is
293 * directly passed into the filter function by the driver
295 if (schan
->dev
->of_node
) {
296 ret
= sdev
->ops
->set_slave(schan
, slave_id
, 0, true);
300 schan
->real_slave_id
= schan
->slave_id
;
305 /* No slave requested - arbitrary channel */
306 dev_warn(sdev
->dma_dev
.dev
, "invalid slave ID passed to dma_request_slave\n");
310 if (slave_id
>= slave_num
)
313 ret
= sdev
->ops
->set_slave(schan
, slave_id
, 0, true);
317 schan
->real_slave_id
= slave_id
;
321 EXPORT_SYMBOL(shdma_chan_filter
);
323 static dma_async_tx_callback
__ld_cleanup(struct shdma_chan
*schan
, bool all
)
325 struct shdma_desc
*desc
, *_desc
;
326 /* Is the "exposed" head of a chain acked? */
327 bool head_acked
= false;
328 dma_cookie_t cookie
= 0;
329 dma_async_tx_callback callback
= NULL
;
330 struct dmaengine_desc_callback cb
;
332 LIST_HEAD(cyclic_list
);
334 memset(&cb
, 0, sizeof(cb
));
335 spin_lock_irqsave(&schan
->chan_lock
, flags
);
336 list_for_each_entry_safe(desc
, _desc
, &schan
->ld_queue
, node
) {
337 struct dma_async_tx_descriptor
*tx
= &desc
->async_tx
;
339 BUG_ON(tx
->cookie
> 0 && tx
->cookie
!= desc
->cookie
);
340 BUG_ON(desc
->mark
!= DESC_SUBMITTED
&&
341 desc
->mark
!= DESC_COMPLETED
&&
342 desc
->mark
!= DESC_WAITING
);
345 * queue is ordered, and we use this loop to (1) clean up all
346 * completed descriptors, and to (2) update descriptor flags of
347 * any chunks in a (partially) completed chain
349 if (!all
&& desc
->mark
== DESC_SUBMITTED
&&
350 desc
->cookie
!= cookie
)
356 if (desc
->mark
== DESC_COMPLETED
&& desc
->chunks
== 1) {
357 if (schan
->dma_chan
.completed_cookie
!= desc
->cookie
- 1)
359 "Completing cookie %d, expected %d\n",
361 schan
->dma_chan
.completed_cookie
+ 1);
362 schan
->dma_chan
.completed_cookie
= desc
->cookie
;
365 /* Call callback on the last chunk */
366 if (desc
->mark
== DESC_COMPLETED
&& tx
->callback
) {
367 desc
->mark
= DESC_WAITING
;
368 dmaengine_desc_get_callback(tx
, &cb
);
369 callback
= tx
->callback
;
370 dev_dbg(schan
->dev
, "descriptor #%d@%p on %d callback\n",
371 tx
->cookie
, tx
, schan
->id
);
372 BUG_ON(desc
->chunks
!= 1);
376 if (tx
->cookie
> 0 || tx
->cookie
== -EBUSY
) {
377 if (desc
->mark
== DESC_COMPLETED
) {
378 BUG_ON(tx
->cookie
< 0);
379 desc
->mark
= DESC_WAITING
;
381 head_acked
= async_tx_test_ack(tx
);
383 switch (desc
->mark
) {
385 desc
->mark
= DESC_WAITING
;
389 async_tx_ack(&desc
->async_tx
);
393 dev_dbg(schan
->dev
, "descriptor %p #%d completed.\n",
396 if (((desc
->mark
== DESC_COMPLETED
||
397 desc
->mark
== DESC_WAITING
) &&
398 async_tx_test_ack(&desc
->async_tx
)) || all
) {
400 if (all
|| !desc
->cyclic
) {
401 /* Remove from ld_queue list */
402 desc
->mark
= DESC_IDLE
;
403 list_move(&desc
->node
, &schan
->ld_free
);
405 /* reuse as cyclic */
406 desc
->mark
= DESC_SUBMITTED
;
407 list_move_tail(&desc
->node
, &cyclic_list
);
410 if (list_empty(&schan
->ld_queue
)) {
411 dev_dbg(schan
->dev
, "Bring down channel %d\n", schan
->id
);
412 pm_runtime_put(schan
->dev
);
413 schan
->pm_state
= SHDMA_PM_ESTABLISHED
;
414 } else if (schan
->pm_state
== SHDMA_PM_PENDING
) {
415 shdma_chan_xfer_ld_queue(schan
);
420 if (all
&& !callback
)
422 * Terminating and the loop completed normally: forgive
423 * uncompleted cookies
425 schan
->dma_chan
.completed_cookie
= schan
->dma_chan
.cookie
;
427 list_splice_tail(&cyclic_list
, &schan
->ld_queue
);
429 spin_unlock_irqrestore(&schan
->chan_lock
, flags
);
431 dmaengine_desc_callback_invoke(&cb
, NULL
);
437 * shdma_chan_ld_cleanup - Clean up link descriptors
439 * Clean up the ld_queue of DMA channel.
441 static void shdma_chan_ld_cleanup(struct shdma_chan
*schan
, bool all
)
443 while (__ld_cleanup(schan
, all
))
448 * shdma_free_chan_resources - Free all resources of the channel.
450 static void shdma_free_chan_resources(struct dma_chan
*chan
)
452 struct shdma_chan
*schan
= to_shdma_chan(chan
);
453 struct shdma_dev
*sdev
= to_shdma_dev(chan
->device
);
454 const struct shdma_ops
*ops
= sdev
->ops
;
457 /* Protect against ISR */
458 spin_lock_irq(&schan
->chan_lock
);
459 ops
->halt_channel(schan
);
460 spin_unlock_irq(&schan
->chan_lock
);
462 /* Now no new interrupts will occur */
464 /* Prepared and not submitted descriptors can still be on the queue */
465 if (!list_empty(&schan
->ld_queue
))
466 shdma_chan_ld_cleanup(schan
, true);
468 if (schan
->slave_id
>= 0) {
469 /* The caller is holding dma_list_mutex */
470 clear_bit(schan
->slave_id
, shdma_slave_used
);
471 chan
->private = NULL
;
474 schan
->real_slave_id
= 0;
476 spin_lock_irq(&schan
->chan_lock
);
478 list_splice_init(&schan
->ld_free
, &list
);
481 spin_unlock_irq(&schan
->chan_lock
);
487 * shdma_add_desc - get, set up and return one transfer descriptor
488 * @schan: DMA channel
489 * @flags: DMA transfer flags
490 * @dst: destination DMA address, incremented when direction equals
491 * DMA_DEV_TO_MEM or DMA_MEM_TO_MEM
492 * @src: source DMA address, incremented when direction equals
493 * DMA_MEM_TO_DEV or DMA_MEM_TO_MEM
494 * @len: DMA transfer length
495 * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
496 * @direction: needed for slave DMA to decide which address to keep constant,
497 * equals DMA_MEM_TO_MEM for MEMCPY
498 * Returns 0 or an error
499 * Locks: called with desc_lock held
501 static struct shdma_desc
*shdma_add_desc(struct shdma_chan
*schan
,
502 unsigned long flags
, dma_addr_t
*dst
, dma_addr_t
*src
, size_t *len
,
503 struct shdma_desc
**first
, enum dma_transfer_direction direction
)
505 struct shdma_dev
*sdev
= to_shdma_dev(schan
->dma_chan
.device
);
506 const struct shdma_ops
*ops
= sdev
->ops
;
507 struct shdma_desc
*new;
508 size_t copy_size
= *len
;
513 /* Allocate the link descriptor from the free list */
514 new = shdma_get_desc(schan
);
516 dev_err(schan
->dev
, "No free link descriptor available\n");
520 ops
->desc_setup(schan
, new, *src
, *dst
, ©_size
);
524 new->async_tx
.cookie
= -EBUSY
;
527 /* Other desc - invisible to the user */
528 new->async_tx
.cookie
= -EINVAL
;
532 "chaining (%zu/%zu)@%pad -> %pad with %p, cookie %d\n",
533 copy_size
, *len
, src
, dst
, &new->async_tx
,
534 new->async_tx
.cookie
);
536 new->mark
= DESC_PREPARED
;
537 new->async_tx
.flags
= flags
;
538 new->direction
= direction
;
542 if (direction
== DMA_MEM_TO_MEM
|| direction
== DMA_MEM_TO_DEV
)
544 if (direction
== DMA_MEM_TO_MEM
|| direction
== DMA_DEV_TO_MEM
)
551 * shdma_prep_sg - prepare transfer descriptors from an SG list
553 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
554 * converted to scatter-gather to guarantee consistent locking and a correct
555 * list manipulation. For slave DMA direction carries the usual meaning, and,
556 * logically, the SG list is RAM and the addr variable contains slave address,
557 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
558 * and the SG list contains only one element and points at the source buffer.
560 static struct dma_async_tx_descriptor
*shdma_prep_sg(struct shdma_chan
*schan
,
561 struct scatterlist
*sgl
, unsigned int sg_len
, dma_addr_t
*addr
,
562 enum dma_transfer_direction direction
, unsigned long flags
, bool cyclic
)
564 struct scatterlist
*sg
;
565 struct shdma_desc
*first
= NULL
, *new = NULL
/* compiler... */;
568 unsigned long irq_flags
;
571 for_each_sg(sgl
, sg
, sg_len
, i
)
572 chunks
+= DIV_ROUND_UP(sg_dma_len(sg
), schan
->max_xfer_len
);
574 /* Have to lock the whole loop to protect against concurrent release */
575 spin_lock_irqsave(&schan
->chan_lock
, irq_flags
);
579 * first descriptor is what user is dealing with in all API calls, its
580 * cookie is at first set to -EBUSY, at tx-submit to a positive
582 * if more than one chunk is needed further chunks have cookie = -EINVAL
583 * the last chunk, if not equal to the first, has cookie = -ENOSPC
584 * all chunks are linked onto the tx_list head with their .node heads
585 * only during this function, then they are immediately spliced
586 * back onto the free list in form of a chain
588 for_each_sg(sgl
, sg
, sg_len
, i
) {
589 dma_addr_t sg_addr
= sg_dma_address(sg
);
590 size_t len
= sg_dma_len(sg
);
596 dev_dbg(schan
->dev
, "Add SG #%d@%p[%zu], dma %pad\n",
597 i
, sg
, len
, &sg_addr
);
599 if (direction
== DMA_DEV_TO_MEM
)
600 new = shdma_add_desc(schan
, flags
,
601 &sg_addr
, addr
, &len
, &first
,
604 new = shdma_add_desc(schan
, flags
,
605 addr
, &sg_addr
, &len
, &first
,
610 new->cyclic
= cyclic
;
614 new->chunks
= chunks
--;
615 list_add_tail(&new->node
, &tx_list
);
620 new->async_tx
.cookie
= -ENOSPC
;
622 /* Put them back on the free list, so, they don't get lost */
623 list_splice_tail(&tx_list
, &schan
->ld_free
);
625 spin_unlock_irqrestore(&schan
->chan_lock
, irq_flags
);
627 return &first
->async_tx
;
630 list_for_each_entry(new, &tx_list
, node
)
631 new->mark
= DESC_IDLE
;
632 list_splice(&tx_list
, &schan
->ld_free
);
634 spin_unlock_irqrestore(&schan
->chan_lock
, irq_flags
);
639 static struct dma_async_tx_descriptor
*shdma_prep_memcpy(
640 struct dma_chan
*chan
, dma_addr_t dma_dest
, dma_addr_t dma_src
,
641 size_t len
, unsigned long flags
)
643 struct shdma_chan
*schan
= to_shdma_chan(chan
);
644 struct scatterlist sg
;
649 BUG_ON(!schan
->desc_num
);
651 sg_init_table(&sg
, 1);
652 sg_set_page(&sg
, pfn_to_page(PFN_DOWN(dma_src
)), len
,
653 offset_in_page(dma_src
));
654 sg_dma_address(&sg
) = dma_src
;
655 sg_dma_len(&sg
) = len
;
657 return shdma_prep_sg(schan
, &sg
, 1, &dma_dest
, DMA_MEM_TO_MEM
,
661 static struct dma_async_tx_descriptor
*shdma_prep_slave_sg(
662 struct dma_chan
*chan
, struct scatterlist
*sgl
, unsigned int sg_len
,
663 enum dma_transfer_direction direction
, unsigned long flags
, void *context
)
665 struct shdma_chan
*schan
= to_shdma_chan(chan
);
666 struct shdma_dev
*sdev
= to_shdma_dev(schan
->dma_chan
.device
);
667 const struct shdma_ops
*ops
= sdev
->ops
;
668 int slave_id
= schan
->slave_id
;
669 dma_addr_t slave_addr
;
674 BUG_ON(!schan
->desc_num
);
676 /* Someone calling slave DMA on a generic channel? */
677 if (slave_id
< 0 || !sg_len
) {
678 dev_warn(schan
->dev
, "%s: bad parameter: len=%d, id=%d\n",
679 __func__
, sg_len
, slave_id
);
683 slave_addr
= ops
->slave_addr(schan
);
685 return shdma_prep_sg(schan
, sgl
, sg_len
, &slave_addr
,
686 direction
, flags
, false);
689 #define SHDMA_MAX_SG_LEN 32
691 static struct dma_async_tx_descriptor
*shdma_prep_dma_cyclic(
692 struct dma_chan
*chan
, dma_addr_t buf_addr
, size_t buf_len
,
693 size_t period_len
, enum dma_transfer_direction direction
,
696 struct shdma_chan
*schan
= to_shdma_chan(chan
);
697 struct shdma_dev
*sdev
= to_shdma_dev(schan
->dma_chan
.device
);
698 struct dma_async_tx_descriptor
*desc
;
699 const struct shdma_ops
*ops
= sdev
->ops
;
700 unsigned int sg_len
= buf_len
/ period_len
;
701 int slave_id
= schan
->slave_id
;
702 dma_addr_t slave_addr
;
703 struct scatterlist
*sgl
;
709 BUG_ON(!schan
->desc_num
);
711 if (sg_len
> SHDMA_MAX_SG_LEN
) {
712 dev_err(schan
->dev
, "sg length %d exceds limit %d",
713 sg_len
, SHDMA_MAX_SG_LEN
);
717 /* Someone calling slave DMA on a generic channel? */
718 if (slave_id
< 0 || (buf_len
< period_len
)) {
720 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
721 __func__
, buf_len
, period_len
, slave_id
);
725 slave_addr
= ops
->slave_addr(schan
);
728 * Allocate the sg list dynamically as it would consumer too much stack
731 sgl
= kcalloc(sg_len
, sizeof(*sgl
), GFP_KERNEL
);
735 sg_init_table(sgl
, sg_len
);
737 for (i
= 0; i
< sg_len
; i
++) {
738 dma_addr_t src
= buf_addr
+ (period_len
* i
);
740 sg_set_page(&sgl
[i
], pfn_to_page(PFN_DOWN(src
)), period_len
,
741 offset_in_page(src
));
742 sg_dma_address(&sgl
[i
]) = src
;
743 sg_dma_len(&sgl
[i
]) = period_len
;
746 desc
= shdma_prep_sg(schan
, sgl
, sg_len
, &slave_addr
,
747 direction
, flags
, true);
753 static int shdma_terminate_all(struct dma_chan
*chan
)
755 struct shdma_chan
*schan
= to_shdma_chan(chan
);
756 struct shdma_dev
*sdev
= to_shdma_dev(chan
->device
);
757 const struct shdma_ops
*ops
= sdev
->ops
;
760 spin_lock_irqsave(&schan
->chan_lock
, flags
);
761 ops
->halt_channel(schan
);
763 if (ops
->get_partial
&& !list_empty(&schan
->ld_queue
)) {
764 /* Record partial transfer */
765 struct shdma_desc
*desc
= list_first_entry(&schan
->ld_queue
,
766 struct shdma_desc
, node
);
767 desc
->partial
= ops
->get_partial(schan
, desc
);
770 spin_unlock_irqrestore(&schan
->chan_lock
, flags
);
772 shdma_chan_ld_cleanup(schan
, true);
777 static int shdma_config(struct dma_chan
*chan
,
778 struct dma_slave_config
*config
)
780 struct shdma_chan
*schan
= to_shdma_chan(chan
);
783 * So far only .slave_id is used, but the slave drivers are
784 * encouraged to also set a transfer direction and an address.
790 * overriding the slave_id through dma_slave_config is deprecated,
791 * but possibly some out-of-tree drivers still do it.
793 if (WARN_ON_ONCE(config
->slave_id
&&
794 config
->slave_id
!= schan
->real_slave_id
))
795 schan
->real_slave_id
= config
->slave_id
;
798 * We could lock this, but you shouldn't be configuring the
799 * channel, while using it...
801 return shdma_setup_slave(schan
,
802 config
->direction
== DMA_DEV_TO_MEM
?
803 config
->src_addr
: config
->dst_addr
);
806 static void shdma_issue_pending(struct dma_chan
*chan
)
808 struct shdma_chan
*schan
= to_shdma_chan(chan
);
810 spin_lock_irq(&schan
->chan_lock
);
811 if (schan
->pm_state
== SHDMA_PM_ESTABLISHED
)
812 shdma_chan_xfer_ld_queue(schan
);
814 schan
->pm_state
= SHDMA_PM_PENDING
;
815 spin_unlock_irq(&schan
->chan_lock
);
818 static enum dma_status
shdma_tx_status(struct dma_chan
*chan
,
820 struct dma_tx_state
*txstate
)
822 struct shdma_chan
*schan
= to_shdma_chan(chan
);
823 enum dma_status status
;
826 shdma_chan_ld_cleanup(schan
, false);
828 spin_lock_irqsave(&schan
->chan_lock
, flags
);
830 status
= dma_cookie_status(chan
, cookie
, txstate
);
833 * If we don't find cookie on the queue, it has been aborted and we have
836 if (status
!= DMA_COMPLETE
) {
837 struct shdma_desc
*sdesc
;
839 list_for_each_entry(sdesc
, &schan
->ld_queue
, node
)
840 if (sdesc
->cookie
== cookie
) {
841 status
= DMA_IN_PROGRESS
;
846 spin_unlock_irqrestore(&schan
->chan_lock
, flags
);
851 /* Called from error IRQ or NMI */
852 bool shdma_reset(struct shdma_dev
*sdev
)
854 const struct shdma_ops
*ops
= sdev
->ops
;
855 struct shdma_chan
*schan
;
856 unsigned int handled
= 0;
859 /* Reset all channels */
860 shdma_for_each_chan(schan
, sdev
, i
) {
861 struct shdma_desc
*sdesc
;
867 spin_lock(&schan
->chan_lock
);
869 /* Stop the channel */
870 ops
->halt_channel(schan
);
872 list_splice_init(&schan
->ld_queue
, &dl
);
874 if (!list_empty(&dl
)) {
875 dev_dbg(schan
->dev
, "Bring down channel %d\n", schan
->id
);
876 pm_runtime_put(schan
->dev
);
878 schan
->pm_state
= SHDMA_PM_ESTABLISHED
;
880 spin_unlock(&schan
->chan_lock
);
883 list_for_each_entry(sdesc
, &dl
, node
) {
884 struct dma_async_tx_descriptor
*tx
= &sdesc
->async_tx
;
886 sdesc
->mark
= DESC_IDLE
;
887 dmaengine_desc_get_callback_invoke(tx
, NULL
);
890 spin_lock(&schan
->chan_lock
);
891 list_splice(&dl
, &schan
->ld_free
);
892 spin_unlock(&schan
->chan_lock
);
899 EXPORT_SYMBOL(shdma_reset
);
901 static irqreturn_t
chan_irq(int irq
, void *dev
)
903 struct shdma_chan
*schan
= dev
;
904 const struct shdma_ops
*ops
=
905 to_shdma_dev(schan
->dma_chan
.device
)->ops
;
908 spin_lock(&schan
->chan_lock
);
910 ret
= ops
->chan_irq(schan
, irq
) ? IRQ_WAKE_THREAD
: IRQ_NONE
;
912 spin_unlock(&schan
->chan_lock
);
917 static irqreturn_t
chan_irqt(int irq
, void *dev
)
919 struct shdma_chan
*schan
= dev
;
920 const struct shdma_ops
*ops
=
921 to_shdma_dev(schan
->dma_chan
.device
)->ops
;
922 struct shdma_desc
*sdesc
;
924 spin_lock_irq(&schan
->chan_lock
);
925 list_for_each_entry(sdesc
, &schan
->ld_queue
, node
) {
926 if (sdesc
->mark
== DESC_SUBMITTED
&&
927 ops
->desc_completed(schan
, sdesc
)) {
928 dev_dbg(schan
->dev
, "done #%d@%p\n",
929 sdesc
->async_tx
.cookie
, &sdesc
->async_tx
);
930 sdesc
->mark
= DESC_COMPLETED
;
935 shdma_chan_xfer_ld_queue(schan
);
936 spin_unlock_irq(&schan
->chan_lock
);
938 shdma_chan_ld_cleanup(schan
, false);
943 int shdma_request_irq(struct shdma_chan
*schan
, int irq
,
944 unsigned long flags
, const char *name
)
946 int ret
= devm_request_threaded_irq(schan
->dev
, irq
, chan_irq
,
947 chan_irqt
, flags
, name
, schan
);
949 schan
->irq
= ret
< 0 ? ret
: irq
;
953 EXPORT_SYMBOL(shdma_request_irq
);
955 void shdma_chan_probe(struct shdma_dev
*sdev
,
956 struct shdma_chan
*schan
, int id
)
958 schan
->pm_state
= SHDMA_PM_ESTABLISHED
;
960 /* reference struct dma_device */
961 schan
->dma_chan
.device
= &sdev
->dma_dev
;
962 dma_cookie_init(&schan
->dma_chan
);
964 schan
->dev
= sdev
->dma_dev
.dev
;
967 if (!schan
->max_xfer_len
)
968 schan
->max_xfer_len
= PAGE_SIZE
;
970 spin_lock_init(&schan
->chan_lock
);
972 /* Init descripter manage list */
973 INIT_LIST_HEAD(&schan
->ld_queue
);
974 INIT_LIST_HEAD(&schan
->ld_free
);
976 /* Add the channel to DMA device channel list */
977 list_add_tail(&schan
->dma_chan
.device_node
,
978 &sdev
->dma_dev
.channels
);
979 sdev
->schan
[id
] = schan
;
981 EXPORT_SYMBOL(shdma_chan_probe
);
983 void shdma_chan_remove(struct shdma_chan
*schan
)
985 list_del(&schan
->dma_chan
.device_node
);
987 EXPORT_SYMBOL(shdma_chan_remove
);
989 int shdma_init(struct device
*dev
, struct shdma_dev
*sdev
,
992 struct dma_device
*dma_dev
= &sdev
->dma_dev
;
995 * Require all call-backs for now, they can trivially be made optional
1000 !sdev
->ops
->embedded_desc
||
1001 !sdev
->ops
->start_xfer
||
1002 !sdev
->ops
->setup_xfer
||
1003 !sdev
->ops
->set_slave
||
1004 !sdev
->ops
->desc_setup
||
1005 !sdev
->ops
->slave_addr
||
1006 !sdev
->ops
->channel_busy
||
1007 !sdev
->ops
->halt_channel
||
1008 !sdev
->ops
->desc_completed
)
1011 sdev
->schan
= kcalloc(chan_num
, sizeof(*sdev
->schan
), GFP_KERNEL
);
1015 INIT_LIST_HEAD(&dma_dev
->channels
);
1017 /* Common and MEMCPY operations */
1018 dma_dev
->device_alloc_chan_resources
1019 = shdma_alloc_chan_resources
;
1020 dma_dev
->device_free_chan_resources
= shdma_free_chan_resources
;
1021 dma_dev
->device_prep_dma_memcpy
= shdma_prep_memcpy
;
1022 dma_dev
->device_tx_status
= shdma_tx_status
;
1023 dma_dev
->device_issue_pending
= shdma_issue_pending
;
1025 /* Compulsory for DMA_SLAVE fields */
1026 dma_dev
->device_prep_slave_sg
= shdma_prep_slave_sg
;
1027 dma_dev
->device_prep_dma_cyclic
= shdma_prep_dma_cyclic
;
1028 dma_dev
->device_config
= shdma_config
;
1029 dma_dev
->device_terminate_all
= shdma_terminate_all
;
1035 EXPORT_SYMBOL(shdma_init
);
1037 void shdma_cleanup(struct shdma_dev
*sdev
)
1041 EXPORT_SYMBOL(shdma_cleanup
);
1043 static int __init
shdma_enter(void)
1045 shdma_slave_used
= kcalloc(DIV_ROUND_UP(slave_num
, BITS_PER_LONG
),
1048 if (!shdma_slave_used
)
1052 module_init(shdma_enter
);
1054 static void __exit
shdma_exit(void)
1056 kfree(shdma_slave_used
);
1058 module_exit(shdma_exit
);
1060 MODULE_LICENSE("GPL v2");
1061 MODULE_DESCRIPTION("SH-DMA driver base library");
1062 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");