xref: /openbmc/linux/drivers/dma/sh/shdma-base.c (revision 7b6d864b)
1 /*
2  * Dmaengine driver base library for DMA controllers, found on SH-based SoCs
3  *
4  * extracted from shdma.c
5  *
6  * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
7  * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
8  * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
9  * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
10  *
11  * This is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  */
15 
16 #include <linux/delay.h>
17 #include <linux/shdma-base.h>
18 #include <linux/dmaengine.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 
26 #include "../dmaengine.h"
27 
28 /* DMA descriptor control */
29 enum shdma_desc_status {
30 	DESC_IDLE,
31 	DESC_PREPARED,
32 	DESC_SUBMITTED,
33 	DESC_COMPLETED,	/* completed, have to call callback */
34 	DESC_WAITING,	/* callback called, waiting for ack / re-submit */
35 };
36 
37 #define NR_DESCS_PER_CHANNEL 32
38 
39 #define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan)
40 #define to_shdma_dev(d) container_of(d, struct shdma_dev, dma_dev)
41 
42 /*
43  * For slave DMA we assume, that there is a finite number of DMA slaves in the
44  * system, and that each such slave can only use a finite number of channels.
45  * We use slave channel IDs to make sure, that no such slave channel ID is
46  * allocated more than once.
47  */
48 static unsigned int slave_num = 256;
49 module_param(slave_num, uint, 0444);
50 
51 /* A bitmask with slave_num bits */
52 static unsigned long *shdma_slave_used;
53 
54 /* Called under spin_lock_irq(&schan->chan_lock") */
55 static void shdma_chan_xfer_ld_queue(struct shdma_chan *schan)
56 {
57 	struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
58 	const struct shdma_ops *ops = sdev->ops;
59 	struct shdma_desc *sdesc;
60 
61 	/* DMA work check */
62 	if (ops->channel_busy(schan))
63 		return;
64 
65 	/* Find the first not transferred descriptor */
66 	list_for_each_entry(sdesc, &schan->ld_queue, node)
67 		if (sdesc->mark == DESC_SUBMITTED) {
68 			ops->start_xfer(schan, sdesc);
69 			break;
70 		}
71 }
72 
73 static dma_cookie_t shdma_tx_submit(struct dma_async_tx_descriptor *tx)
74 {
75 	struct shdma_desc *chunk, *c, *desc =
76 		container_of(tx, struct shdma_desc, async_tx),
77 		*last = desc;
78 	struct shdma_chan *schan = to_shdma_chan(tx->chan);
79 	dma_async_tx_callback callback = tx->callback;
80 	dma_cookie_t cookie;
81 	bool power_up;
82 
83 	spin_lock_irq(&schan->chan_lock);
84 
85 	power_up = list_empty(&schan->ld_queue);
86 
87 	cookie = dma_cookie_assign(tx);
88 
89 	/* Mark all chunks of this descriptor as submitted, move to the queue */
90 	list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
91 		/*
92 		 * All chunks are on the global ld_free, so, we have to find
93 		 * the end of the chain ourselves
94 		 */
95 		if (chunk != desc && (chunk->mark == DESC_IDLE ||
96 				      chunk->async_tx.cookie > 0 ||
97 				      chunk->async_tx.cookie == -EBUSY ||
98 				      &chunk->node == &schan->ld_free))
99 			break;
100 		chunk->mark = DESC_SUBMITTED;
101 		/* Callback goes to the last chunk */
102 		chunk->async_tx.callback = NULL;
103 		chunk->cookie = cookie;
104 		list_move_tail(&chunk->node, &schan->ld_queue);
105 		last = chunk;
106 
107 		dev_dbg(schan->dev, "submit #%d@%p on %d\n",
108 			tx->cookie, &last->async_tx, schan->id);
109 	}
110 
111 	last->async_tx.callback = callback;
112 	last->async_tx.callback_param = tx->callback_param;
113 
114 	if (power_up) {
115 		int ret;
116 		schan->pm_state = SHDMA_PM_BUSY;
117 
118 		ret = pm_runtime_get(schan->dev);
119 
120 		spin_unlock_irq(&schan->chan_lock);
121 		if (ret < 0)
122 			dev_err(schan->dev, "%s(): GET = %d\n", __func__, ret);
123 
124 		pm_runtime_barrier(schan->dev);
125 
126 		spin_lock_irq(&schan->chan_lock);
127 
128 		/* Have we been reset, while waiting? */
129 		if (schan->pm_state != SHDMA_PM_ESTABLISHED) {
130 			struct shdma_dev *sdev =
131 				to_shdma_dev(schan->dma_chan.device);
132 			const struct shdma_ops *ops = sdev->ops;
133 			dev_dbg(schan->dev, "Bring up channel %d\n",
134 				schan->id);
135 			/*
136 			 * TODO: .xfer_setup() might fail on some platforms.
137 			 * Make it int then, on error remove chunks from the
138 			 * queue again
139 			 */
140 			ops->setup_xfer(schan, schan->slave_id);
141 
142 			if (schan->pm_state == SHDMA_PM_PENDING)
143 				shdma_chan_xfer_ld_queue(schan);
144 			schan->pm_state = SHDMA_PM_ESTABLISHED;
145 		}
146 	} else {
147 		/*
148 		 * Tell .device_issue_pending() not to run the queue, interrupts
149 		 * will do it anyway
150 		 */
151 		schan->pm_state = SHDMA_PM_PENDING;
152 	}
153 
154 	spin_unlock_irq(&schan->chan_lock);
155 
156 	return cookie;
157 }
158 
159 /* Called with desc_lock held */
160 static struct shdma_desc *shdma_get_desc(struct shdma_chan *schan)
161 {
162 	struct shdma_desc *sdesc;
163 
164 	list_for_each_entry(sdesc, &schan->ld_free, node)
165 		if (sdesc->mark != DESC_PREPARED) {
166 			BUG_ON(sdesc->mark != DESC_IDLE);
167 			list_del(&sdesc->node);
168 			return sdesc;
169 		}
170 
171 	return NULL;
172 }
173 
174 static int shdma_setup_slave(struct shdma_chan *schan, int slave_id)
175 {
176 	struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
177 	const struct shdma_ops *ops = sdev->ops;
178 	int ret, match;
179 
180 	if (schan->dev->of_node) {
181 		match = schan->hw_req;
182 		ret = ops->set_slave(schan, match, true);
183 		if (ret < 0)
184 			return ret;
185 
186 		slave_id = schan->slave_id;
187 	} else {
188 		match = slave_id;
189 	}
190 
191 	if (slave_id < 0 || slave_id >= slave_num)
192 		return -EINVAL;
193 
194 	if (test_and_set_bit(slave_id, shdma_slave_used))
195 		return -EBUSY;
196 
197 	ret = ops->set_slave(schan, match, false);
198 	if (ret < 0) {
199 		clear_bit(slave_id, shdma_slave_used);
200 		return ret;
201 	}
202 
203 	schan->slave_id = slave_id;
204 
205 	return 0;
206 }
207 
208 /*
209  * This is the standard shdma filter function to be used as a replacement to the
210  * "old" method, using the .private pointer. If for some reason you allocate a
211  * channel without slave data, use something like ERR_PTR(-EINVAL) as a filter
212  * parameter. If this filter is used, the slave driver, after calling
213  * dma_request_channel(), will also have to call dmaengine_slave_config() with
214  * .slave_id, .direction, and either .src_addr or .dst_addr set.
215  * NOTE: this filter doesn't support multiple DMAC drivers with the DMA_SLAVE
216  * capability! If this becomes a requirement, hardware glue drivers, using this
217  * services would have to provide their own filters, which first would check
218  * the device driver, similar to how other DMAC drivers, e.g., sa11x0-dma.c, do
219  * this, and only then, in case of a match, call this common filter.
220  * NOTE 2: This filter function is also used in the DT case by shdma_of_xlate().
221  * In that case the MID-RID value is used for slave channel filtering and is
222  * passed to this function in the "arg" parameter.
223  */
224 bool shdma_chan_filter(struct dma_chan *chan, void *arg)
225 {
226 	struct shdma_chan *schan = to_shdma_chan(chan);
227 	struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
228 	const struct shdma_ops *ops = sdev->ops;
229 	int match = (int)arg;
230 	int ret;
231 
232 	if (match < 0)
233 		/* No slave requested - arbitrary channel */
234 		return true;
235 
236 	if (!schan->dev->of_node && match >= slave_num)
237 		return false;
238 
239 	ret = ops->set_slave(schan, match, true);
240 	if (ret < 0)
241 		return false;
242 
243 	return true;
244 }
245 EXPORT_SYMBOL(shdma_chan_filter);
246 
247 static int shdma_alloc_chan_resources(struct dma_chan *chan)
248 {
249 	struct shdma_chan *schan = to_shdma_chan(chan);
250 	struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
251 	const struct shdma_ops *ops = sdev->ops;
252 	struct shdma_desc *desc;
253 	struct shdma_slave *slave = chan->private;
254 	int ret, i;
255 
256 	/*
257 	 * This relies on the guarantee from dmaengine that alloc_chan_resources
258 	 * never runs concurrently with itself or free_chan_resources.
259 	 */
260 	if (slave) {
261 		/* Legacy mode: .private is set in filter */
262 		ret = shdma_setup_slave(schan, slave->slave_id);
263 		if (ret < 0)
264 			goto esetslave;
265 	} else {
266 		schan->slave_id = -EINVAL;
267 	}
268 
269 	schan->desc = kcalloc(NR_DESCS_PER_CHANNEL,
270 			      sdev->desc_size, GFP_KERNEL);
271 	if (!schan->desc) {
272 		ret = -ENOMEM;
273 		goto edescalloc;
274 	}
275 	schan->desc_num = NR_DESCS_PER_CHANNEL;
276 
277 	for (i = 0; i < NR_DESCS_PER_CHANNEL; i++) {
278 		desc = ops->embedded_desc(schan->desc, i);
279 		dma_async_tx_descriptor_init(&desc->async_tx,
280 					     &schan->dma_chan);
281 		desc->async_tx.tx_submit = shdma_tx_submit;
282 		desc->mark = DESC_IDLE;
283 
284 		list_add(&desc->node, &schan->ld_free);
285 	}
286 
287 	return NR_DESCS_PER_CHANNEL;
288 
289 edescalloc:
290 	if (slave)
291 esetslave:
292 		clear_bit(slave->slave_id, shdma_slave_used);
293 	chan->private = NULL;
294 	return ret;
295 }
296 
297 static dma_async_tx_callback __ld_cleanup(struct shdma_chan *schan, bool all)
298 {
299 	struct shdma_desc *desc, *_desc;
300 	/* Is the "exposed" head of a chain acked? */
301 	bool head_acked = false;
302 	dma_cookie_t cookie = 0;
303 	dma_async_tx_callback callback = NULL;
304 	void *param = NULL;
305 	unsigned long flags;
306 
307 	spin_lock_irqsave(&schan->chan_lock, flags);
308 	list_for_each_entry_safe(desc, _desc, &schan->ld_queue, node) {
309 		struct dma_async_tx_descriptor *tx = &desc->async_tx;
310 
311 		BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
312 		BUG_ON(desc->mark != DESC_SUBMITTED &&
313 		       desc->mark != DESC_COMPLETED &&
314 		       desc->mark != DESC_WAITING);
315 
316 		/*
317 		 * queue is ordered, and we use this loop to (1) clean up all
318 		 * completed descriptors, and to (2) update descriptor flags of
319 		 * any chunks in a (partially) completed chain
320 		 */
321 		if (!all && desc->mark == DESC_SUBMITTED &&
322 		    desc->cookie != cookie)
323 			break;
324 
325 		if (tx->cookie > 0)
326 			cookie = tx->cookie;
327 
328 		if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
329 			if (schan->dma_chan.completed_cookie != desc->cookie - 1)
330 				dev_dbg(schan->dev,
331 					"Completing cookie %d, expected %d\n",
332 					desc->cookie,
333 					schan->dma_chan.completed_cookie + 1);
334 			schan->dma_chan.completed_cookie = desc->cookie;
335 		}
336 
337 		/* Call callback on the last chunk */
338 		if (desc->mark == DESC_COMPLETED && tx->callback) {
339 			desc->mark = DESC_WAITING;
340 			callback = tx->callback;
341 			param = tx->callback_param;
342 			dev_dbg(schan->dev, "descriptor #%d@%p on %d callback\n",
343 				tx->cookie, tx, schan->id);
344 			BUG_ON(desc->chunks != 1);
345 			break;
346 		}
347 
348 		if (tx->cookie > 0 || tx->cookie == -EBUSY) {
349 			if (desc->mark == DESC_COMPLETED) {
350 				BUG_ON(tx->cookie < 0);
351 				desc->mark = DESC_WAITING;
352 			}
353 			head_acked = async_tx_test_ack(tx);
354 		} else {
355 			switch (desc->mark) {
356 			case DESC_COMPLETED:
357 				desc->mark = DESC_WAITING;
358 				/* Fall through */
359 			case DESC_WAITING:
360 				if (head_acked)
361 					async_tx_ack(&desc->async_tx);
362 			}
363 		}
364 
365 		dev_dbg(schan->dev, "descriptor %p #%d completed.\n",
366 			tx, tx->cookie);
367 
368 		if (((desc->mark == DESC_COMPLETED ||
369 		      desc->mark == DESC_WAITING) &&
370 		     async_tx_test_ack(&desc->async_tx)) || all) {
371 			/* Remove from ld_queue list */
372 			desc->mark = DESC_IDLE;
373 
374 			list_move(&desc->node, &schan->ld_free);
375 
376 			if (list_empty(&schan->ld_queue)) {
377 				dev_dbg(schan->dev, "Bring down channel %d\n", schan->id);
378 				pm_runtime_put(schan->dev);
379 				schan->pm_state = SHDMA_PM_ESTABLISHED;
380 			}
381 		}
382 	}
383 
384 	if (all && !callback)
385 		/*
386 		 * Terminating and the loop completed normally: forgive
387 		 * uncompleted cookies
388 		 */
389 		schan->dma_chan.completed_cookie = schan->dma_chan.cookie;
390 
391 	spin_unlock_irqrestore(&schan->chan_lock, flags);
392 
393 	if (callback)
394 		callback(param);
395 
396 	return callback;
397 }
398 
399 /*
400  * shdma_chan_ld_cleanup - Clean up link descriptors
401  *
402  * Clean up the ld_queue of DMA channel.
403  */
404 static void shdma_chan_ld_cleanup(struct shdma_chan *schan, bool all)
405 {
406 	while (__ld_cleanup(schan, all))
407 		;
408 }
409 
410 /*
411  * shdma_free_chan_resources - Free all resources of the channel.
412  */
413 static void shdma_free_chan_resources(struct dma_chan *chan)
414 {
415 	struct shdma_chan *schan = to_shdma_chan(chan);
416 	struct shdma_dev *sdev = to_shdma_dev(chan->device);
417 	const struct shdma_ops *ops = sdev->ops;
418 	LIST_HEAD(list);
419 
420 	/* Protect against ISR */
421 	spin_lock_irq(&schan->chan_lock);
422 	ops->halt_channel(schan);
423 	spin_unlock_irq(&schan->chan_lock);
424 
425 	/* Now no new interrupts will occur */
426 
427 	/* Prepared and not submitted descriptors can still be on the queue */
428 	if (!list_empty(&schan->ld_queue))
429 		shdma_chan_ld_cleanup(schan, true);
430 
431 	if (schan->slave_id >= 0) {
432 		/* The caller is holding dma_list_mutex */
433 		clear_bit(schan->slave_id, shdma_slave_used);
434 		chan->private = NULL;
435 	}
436 
437 	spin_lock_irq(&schan->chan_lock);
438 
439 	list_splice_init(&schan->ld_free, &list);
440 	schan->desc_num = 0;
441 
442 	spin_unlock_irq(&schan->chan_lock);
443 
444 	kfree(schan->desc);
445 }
446 
447 /**
448  * shdma_add_desc - get, set up and return one transfer descriptor
449  * @schan:	DMA channel
450  * @flags:	DMA transfer flags
451  * @dst:	destination DMA address, incremented when direction equals
452  *		DMA_DEV_TO_MEM or DMA_MEM_TO_MEM
453  * @src:	source DMA address, incremented when direction equals
454  *		DMA_MEM_TO_DEV or DMA_MEM_TO_MEM
455  * @len:	DMA transfer length
456  * @first:	if NULL, set to the current descriptor and cookie set to -EBUSY
457  * @direction:	needed for slave DMA to decide which address to keep constant,
458  *		equals DMA_MEM_TO_MEM for MEMCPY
459  * Returns 0 or an error
460  * Locks: called with desc_lock held
461  */
462 static struct shdma_desc *shdma_add_desc(struct shdma_chan *schan,
463 	unsigned long flags, dma_addr_t *dst, dma_addr_t *src, size_t *len,
464 	struct shdma_desc **first, enum dma_transfer_direction direction)
465 {
466 	struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
467 	const struct shdma_ops *ops = sdev->ops;
468 	struct shdma_desc *new;
469 	size_t copy_size = *len;
470 
471 	if (!copy_size)
472 		return NULL;
473 
474 	/* Allocate the link descriptor from the free list */
475 	new = shdma_get_desc(schan);
476 	if (!new) {
477 		dev_err(schan->dev, "No free link descriptor available\n");
478 		return NULL;
479 	}
480 
481 	ops->desc_setup(schan, new, *src, *dst, &copy_size);
482 
483 	if (!*first) {
484 		/* First desc */
485 		new->async_tx.cookie = -EBUSY;
486 		*first = new;
487 	} else {
488 		/* Other desc - invisible to the user */
489 		new->async_tx.cookie = -EINVAL;
490 	}
491 
492 	dev_dbg(schan->dev,
493 		"chaining (%u/%u)@%x -> %x with %p, cookie %d\n",
494 		copy_size, *len, *src, *dst, &new->async_tx,
495 		new->async_tx.cookie);
496 
497 	new->mark = DESC_PREPARED;
498 	new->async_tx.flags = flags;
499 	new->direction = direction;
500 	new->partial = 0;
501 
502 	*len -= copy_size;
503 	if (direction == DMA_MEM_TO_MEM || direction == DMA_MEM_TO_DEV)
504 		*src += copy_size;
505 	if (direction == DMA_MEM_TO_MEM || direction == DMA_DEV_TO_MEM)
506 		*dst += copy_size;
507 
508 	return new;
509 }
510 
511 /*
512  * shdma_prep_sg - prepare transfer descriptors from an SG list
513  *
514  * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
515  * converted to scatter-gather to guarantee consistent locking and a correct
516  * list manipulation. For slave DMA direction carries the usual meaning, and,
517  * logically, the SG list is RAM and the addr variable contains slave address,
518  * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
519  * and the SG list contains only one element and points at the source buffer.
520  */
521 static struct dma_async_tx_descriptor *shdma_prep_sg(struct shdma_chan *schan,
522 	struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
523 	enum dma_transfer_direction direction, unsigned long flags)
524 {
525 	struct scatterlist *sg;
526 	struct shdma_desc *first = NULL, *new = NULL /* compiler... */;
527 	LIST_HEAD(tx_list);
528 	int chunks = 0;
529 	unsigned long irq_flags;
530 	int i;
531 
532 	for_each_sg(sgl, sg, sg_len, i)
533 		chunks += DIV_ROUND_UP(sg_dma_len(sg), schan->max_xfer_len);
534 
535 	/* Have to lock the whole loop to protect against concurrent release */
536 	spin_lock_irqsave(&schan->chan_lock, irq_flags);
537 
538 	/*
539 	 * Chaining:
540 	 * first descriptor is what user is dealing with in all API calls, its
541 	 *	cookie is at first set to -EBUSY, at tx-submit to a positive
542 	 *	number
543 	 * if more than one chunk is needed further chunks have cookie = -EINVAL
544 	 * the last chunk, if not equal to the first, has cookie = -ENOSPC
545 	 * all chunks are linked onto the tx_list head with their .node heads
546 	 *	only during this function, then they are immediately spliced
547 	 *	back onto the free list in form of a chain
548 	 */
549 	for_each_sg(sgl, sg, sg_len, i) {
550 		dma_addr_t sg_addr = sg_dma_address(sg);
551 		size_t len = sg_dma_len(sg);
552 
553 		if (!len)
554 			goto err_get_desc;
555 
556 		do {
557 			dev_dbg(schan->dev, "Add SG #%d@%p[%d], dma %llx\n",
558 				i, sg, len, (unsigned long long)sg_addr);
559 
560 			if (direction == DMA_DEV_TO_MEM)
561 				new = shdma_add_desc(schan, flags,
562 						&sg_addr, addr, &len, &first,
563 						direction);
564 			else
565 				new = shdma_add_desc(schan, flags,
566 						addr, &sg_addr, &len, &first,
567 						direction);
568 			if (!new)
569 				goto err_get_desc;
570 
571 			new->chunks = chunks--;
572 			list_add_tail(&new->node, &tx_list);
573 		} while (len);
574 	}
575 
576 	if (new != first)
577 		new->async_tx.cookie = -ENOSPC;
578 
579 	/* Put them back on the free list, so, they don't get lost */
580 	list_splice_tail(&tx_list, &schan->ld_free);
581 
582 	spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
583 
584 	return &first->async_tx;
585 
586 err_get_desc:
587 	list_for_each_entry(new, &tx_list, node)
588 		new->mark = DESC_IDLE;
589 	list_splice(&tx_list, &schan->ld_free);
590 
591 	spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
592 
593 	return NULL;
594 }
595 
596 static struct dma_async_tx_descriptor *shdma_prep_memcpy(
597 	struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
598 	size_t len, unsigned long flags)
599 {
600 	struct shdma_chan *schan = to_shdma_chan(chan);
601 	struct scatterlist sg;
602 
603 	if (!chan || !len)
604 		return NULL;
605 
606 	BUG_ON(!schan->desc_num);
607 
608 	sg_init_table(&sg, 1);
609 	sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
610 		    offset_in_page(dma_src));
611 	sg_dma_address(&sg) = dma_src;
612 	sg_dma_len(&sg) = len;
613 
614 	return shdma_prep_sg(schan, &sg, 1, &dma_dest, DMA_MEM_TO_MEM, flags);
615 }
616 
617 static struct dma_async_tx_descriptor *shdma_prep_slave_sg(
618 	struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
619 	enum dma_transfer_direction direction, unsigned long flags, void *context)
620 {
621 	struct shdma_chan *schan = to_shdma_chan(chan);
622 	struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
623 	const struct shdma_ops *ops = sdev->ops;
624 	int slave_id = schan->slave_id;
625 	dma_addr_t slave_addr;
626 
627 	if (!chan)
628 		return NULL;
629 
630 	BUG_ON(!schan->desc_num);
631 
632 	/* Someone calling slave DMA on a generic channel? */
633 	if (slave_id < 0 || !sg_len) {
634 		dev_warn(schan->dev, "%s: bad parameter: len=%d, id=%d\n",
635 			 __func__, sg_len, slave_id);
636 		return NULL;
637 	}
638 
639 	slave_addr = ops->slave_addr(schan);
640 
641 	return shdma_prep_sg(schan, sgl, sg_len, &slave_addr,
642 			      direction, flags);
643 }
644 
645 static int shdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
646 			  unsigned long arg)
647 {
648 	struct shdma_chan *schan = to_shdma_chan(chan);
649 	struct shdma_dev *sdev = to_shdma_dev(chan->device);
650 	const struct shdma_ops *ops = sdev->ops;
651 	struct dma_slave_config *config;
652 	unsigned long flags;
653 	int ret;
654 
655 	switch (cmd) {
656 	case DMA_TERMINATE_ALL:
657 		spin_lock_irqsave(&schan->chan_lock, flags);
658 		ops->halt_channel(schan);
659 
660 		if (ops->get_partial && !list_empty(&schan->ld_queue)) {
661 			/* Record partial transfer */
662 			struct shdma_desc *desc = list_first_entry(&schan->ld_queue,
663 						struct shdma_desc, node);
664 			desc->partial = ops->get_partial(schan, desc);
665 		}
666 
667 		spin_unlock_irqrestore(&schan->chan_lock, flags);
668 
669 		shdma_chan_ld_cleanup(schan, true);
670 		break;
671 	case DMA_SLAVE_CONFIG:
672 		/*
673 		 * So far only .slave_id is used, but the slave drivers are
674 		 * encouraged to also set a transfer direction and an address.
675 		 */
676 		if (!arg)
677 			return -EINVAL;
678 		/*
679 		 * We could lock this, but you shouldn't be configuring the
680 		 * channel, while using it...
681 		 */
682 		config = (struct dma_slave_config *)arg;
683 		ret = shdma_setup_slave(schan, config->slave_id);
684 		if (ret < 0)
685 			return ret;
686 		break;
687 	default:
688 		return -ENXIO;
689 	}
690 
691 	return 0;
692 }
693 
694 static void shdma_issue_pending(struct dma_chan *chan)
695 {
696 	struct shdma_chan *schan = to_shdma_chan(chan);
697 
698 	spin_lock_irq(&schan->chan_lock);
699 	if (schan->pm_state == SHDMA_PM_ESTABLISHED)
700 		shdma_chan_xfer_ld_queue(schan);
701 	else
702 		schan->pm_state = SHDMA_PM_PENDING;
703 	spin_unlock_irq(&schan->chan_lock);
704 }
705 
706 static enum dma_status shdma_tx_status(struct dma_chan *chan,
707 					dma_cookie_t cookie,
708 					struct dma_tx_state *txstate)
709 {
710 	struct shdma_chan *schan = to_shdma_chan(chan);
711 	enum dma_status status;
712 	unsigned long flags;
713 
714 	shdma_chan_ld_cleanup(schan, false);
715 
716 	spin_lock_irqsave(&schan->chan_lock, flags);
717 
718 	status = dma_cookie_status(chan, cookie, txstate);
719 
720 	/*
721 	 * If we don't find cookie on the queue, it has been aborted and we have
722 	 * to report error
723 	 */
724 	if (status != DMA_SUCCESS) {
725 		struct shdma_desc *sdesc;
726 		status = DMA_ERROR;
727 		list_for_each_entry(sdesc, &schan->ld_queue, node)
728 			if (sdesc->cookie == cookie) {
729 				status = DMA_IN_PROGRESS;
730 				break;
731 			}
732 	}
733 
734 	spin_unlock_irqrestore(&schan->chan_lock, flags);
735 
736 	return status;
737 }
738 
739 /* Called from error IRQ or NMI */
740 bool shdma_reset(struct shdma_dev *sdev)
741 {
742 	const struct shdma_ops *ops = sdev->ops;
743 	struct shdma_chan *schan;
744 	unsigned int handled = 0;
745 	int i;
746 
747 	/* Reset all channels */
748 	shdma_for_each_chan(schan, sdev, i) {
749 		struct shdma_desc *sdesc;
750 		LIST_HEAD(dl);
751 
752 		if (!schan)
753 			continue;
754 
755 		spin_lock(&schan->chan_lock);
756 
757 		/* Stop the channel */
758 		ops->halt_channel(schan);
759 
760 		list_splice_init(&schan->ld_queue, &dl);
761 
762 		if (!list_empty(&dl)) {
763 			dev_dbg(schan->dev, "Bring down channel %d\n", schan->id);
764 			pm_runtime_put(schan->dev);
765 		}
766 		schan->pm_state = SHDMA_PM_ESTABLISHED;
767 
768 		spin_unlock(&schan->chan_lock);
769 
770 		/* Complete all  */
771 		list_for_each_entry(sdesc, &dl, node) {
772 			struct dma_async_tx_descriptor *tx = &sdesc->async_tx;
773 			sdesc->mark = DESC_IDLE;
774 			if (tx->callback)
775 				tx->callback(tx->callback_param);
776 		}
777 
778 		spin_lock(&schan->chan_lock);
779 		list_splice(&dl, &schan->ld_free);
780 		spin_unlock(&schan->chan_lock);
781 
782 		handled++;
783 	}
784 
785 	return !!handled;
786 }
787 EXPORT_SYMBOL(shdma_reset);
788 
789 static irqreturn_t chan_irq(int irq, void *dev)
790 {
791 	struct shdma_chan *schan = dev;
792 	const struct shdma_ops *ops =
793 		to_shdma_dev(schan->dma_chan.device)->ops;
794 	irqreturn_t ret;
795 
796 	spin_lock(&schan->chan_lock);
797 
798 	ret = ops->chan_irq(schan, irq) ? IRQ_WAKE_THREAD : IRQ_NONE;
799 
800 	spin_unlock(&schan->chan_lock);
801 
802 	return ret;
803 }
804 
805 static irqreturn_t chan_irqt(int irq, void *dev)
806 {
807 	struct shdma_chan *schan = dev;
808 	const struct shdma_ops *ops =
809 		to_shdma_dev(schan->dma_chan.device)->ops;
810 	struct shdma_desc *sdesc;
811 
812 	spin_lock_irq(&schan->chan_lock);
813 	list_for_each_entry(sdesc, &schan->ld_queue, node) {
814 		if (sdesc->mark == DESC_SUBMITTED &&
815 		    ops->desc_completed(schan, sdesc)) {
816 			dev_dbg(schan->dev, "done #%d@%p\n",
817 				sdesc->async_tx.cookie, &sdesc->async_tx);
818 			sdesc->mark = DESC_COMPLETED;
819 			break;
820 		}
821 	}
822 	/* Next desc */
823 	shdma_chan_xfer_ld_queue(schan);
824 	spin_unlock_irq(&schan->chan_lock);
825 
826 	shdma_chan_ld_cleanup(schan, false);
827 
828 	return IRQ_HANDLED;
829 }
830 
831 int shdma_request_irq(struct shdma_chan *schan, int irq,
832 			   unsigned long flags, const char *name)
833 {
834 	int ret = request_threaded_irq(irq, chan_irq, chan_irqt,
835 				       flags, name, schan);
836 
837 	schan->irq = ret < 0 ? ret : irq;
838 
839 	return ret;
840 }
841 EXPORT_SYMBOL(shdma_request_irq);
842 
843 void shdma_free_irq(struct shdma_chan *schan)
844 {
845 	if (schan->irq >= 0)
846 		free_irq(schan->irq, schan);
847 }
848 EXPORT_SYMBOL(shdma_free_irq);
849 
850 void shdma_chan_probe(struct shdma_dev *sdev,
851 			   struct shdma_chan *schan, int id)
852 {
853 	schan->pm_state = SHDMA_PM_ESTABLISHED;
854 
855 	/* reference struct dma_device */
856 	schan->dma_chan.device = &sdev->dma_dev;
857 	dma_cookie_init(&schan->dma_chan);
858 
859 	schan->dev = sdev->dma_dev.dev;
860 	schan->id = id;
861 
862 	if (!schan->max_xfer_len)
863 		schan->max_xfer_len = PAGE_SIZE;
864 
865 	spin_lock_init(&schan->chan_lock);
866 
867 	/* Init descripter manage list */
868 	INIT_LIST_HEAD(&schan->ld_queue);
869 	INIT_LIST_HEAD(&schan->ld_free);
870 
871 	/* Add the channel to DMA device channel list */
872 	list_add_tail(&schan->dma_chan.device_node,
873 			&sdev->dma_dev.channels);
874 	sdev->schan[sdev->dma_dev.chancnt++] = schan;
875 }
876 EXPORT_SYMBOL(shdma_chan_probe);
877 
878 void shdma_chan_remove(struct shdma_chan *schan)
879 {
880 	list_del(&schan->dma_chan.device_node);
881 }
882 EXPORT_SYMBOL(shdma_chan_remove);
883 
884 int shdma_init(struct device *dev, struct shdma_dev *sdev,
885 		    int chan_num)
886 {
887 	struct dma_device *dma_dev = &sdev->dma_dev;
888 
889 	/*
890 	 * Require all call-backs for now, they can trivially be made optional
891 	 * later as required
892 	 */
893 	if (!sdev->ops ||
894 	    !sdev->desc_size ||
895 	    !sdev->ops->embedded_desc ||
896 	    !sdev->ops->start_xfer ||
897 	    !sdev->ops->setup_xfer ||
898 	    !sdev->ops->set_slave ||
899 	    !sdev->ops->desc_setup ||
900 	    !sdev->ops->slave_addr ||
901 	    !sdev->ops->channel_busy ||
902 	    !sdev->ops->halt_channel ||
903 	    !sdev->ops->desc_completed)
904 		return -EINVAL;
905 
906 	sdev->schan = kcalloc(chan_num, sizeof(*sdev->schan), GFP_KERNEL);
907 	if (!sdev->schan)
908 		return -ENOMEM;
909 
910 	INIT_LIST_HEAD(&dma_dev->channels);
911 
912 	/* Common and MEMCPY operations */
913 	dma_dev->device_alloc_chan_resources
914 		= shdma_alloc_chan_resources;
915 	dma_dev->device_free_chan_resources = shdma_free_chan_resources;
916 	dma_dev->device_prep_dma_memcpy = shdma_prep_memcpy;
917 	dma_dev->device_tx_status = shdma_tx_status;
918 	dma_dev->device_issue_pending = shdma_issue_pending;
919 
920 	/* Compulsory for DMA_SLAVE fields */
921 	dma_dev->device_prep_slave_sg = shdma_prep_slave_sg;
922 	dma_dev->device_control = shdma_control;
923 
924 	dma_dev->dev = dev;
925 
926 	return 0;
927 }
928 EXPORT_SYMBOL(shdma_init);
929 
930 void shdma_cleanup(struct shdma_dev *sdev)
931 {
932 	kfree(sdev->schan);
933 }
934 EXPORT_SYMBOL(shdma_cleanup);
935 
936 static int __init shdma_enter(void)
937 {
938 	shdma_slave_used = kzalloc(DIV_ROUND_UP(slave_num, BITS_PER_LONG) *
939 				    sizeof(long), GFP_KERNEL);
940 	if (!shdma_slave_used)
941 		return -ENOMEM;
942 	return 0;
943 }
944 module_init(shdma_enter);
945 
946 static void __exit shdma_exit(void)
947 {
948 	kfree(shdma_slave_used);
949 }
950 module_exit(shdma_exit);
951 
952 MODULE_LICENSE("GPL v2");
953 MODULE_DESCRIPTION("SH-DMA driver base library");
954 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
955