1
2
3
4
5
6
7
8
9
10
11
12
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>
22
23#include "../dmaengine.h"
24
25
26enum shdma_desc_status {
27 DESC_IDLE,
28 DESC_PREPARED,
29 DESC_SUBMITTED,
30 DESC_COMPLETED,
31 DESC_WAITING,
32};
33
34#define NR_DESCS_PER_CHANNEL 32
35
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)
38
39
40
41
42
43
44
45static unsigned int slave_num = 256;
46module_param(slave_num, uint, 0444);
47
48
49static unsigned long *shdma_slave_used;
50
51
52static void shdma_chan_xfer_ld_queue(struct shdma_chan *schan)
53{
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;
57
58
59 if (ops->channel_busy(schan))
60 return;
61
62
63 list_for_each_entry(sdesc, &schan->ld_queue, node)
64 if (sdesc->mark == DESC_SUBMITTED) {
65 ops->start_xfer(schan, sdesc);
66 break;
67 }
68}
69
70static dma_cookie_t shdma_tx_submit(struct dma_async_tx_descriptor *tx)
71{
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;
76 dma_cookie_t cookie;
77 bool power_up;
78
79 spin_lock_irq(&schan->chan_lock);
80
81 power_up = list_empty(&schan->ld_queue);
82
83 cookie = dma_cookie_assign(tx);
84
85
86 list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
87
88
89
90
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))
95 break;
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;
100 } else {
101
102 chunk->async_tx.callback = NULL;
103 }
104 chunk->cookie = cookie;
105 list_move_tail(&chunk->node, &schan->ld_queue);
106
107 dev_dbg(schan->dev, "submit #%d@%p on %d\n",
108 tx->cookie, &chunk->async_tx, schan->id);
109 }
110
111 if (power_up) {
112 int ret;
113 schan->pm_state = SHDMA_PM_BUSY;
114
115 ret = pm_runtime_get(schan->dev);
116
117 spin_unlock_irq(&schan->chan_lock);
118 if (ret < 0)
119 dev_err(schan->dev, "%s(): GET = %d\n", __func__, ret);
120
121 pm_runtime_barrier(schan->dev);
122
123 spin_lock_irq(&schan->chan_lock);
124
125
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",
131 schan->id);
132
133
134
135
136
137 ops->setup_xfer(schan, schan->slave_id);
138
139 if (schan->pm_state == SHDMA_PM_PENDING)
140 shdma_chan_xfer_ld_queue(schan);
141 schan->pm_state = SHDMA_PM_ESTABLISHED;
142 }
143 } else {
144
145
146
147
148 schan->pm_state = SHDMA_PM_PENDING;
149 }
150
151 spin_unlock_irq(&schan->chan_lock);
152
153 return cookie;
154}
155
156
157static struct shdma_desc *shdma_get_desc(struct shdma_chan *schan)
158{
159 struct shdma_desc *sdesc;
160
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);
165 return sdesc;
166 }
167
168 return NULL;
169}
170
171static int shdma_setup_slave(struct shdma_chan *schan, dma_addr_t slave_addr)
172{
173 struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
174 const struct shdma_ops *ops = sdev->ops;
175 int ret, match;
176
177 if (schan->dev->of_node) {
178 match = schan->hw_req;
179 ret = ops->set_slave(schan, match, slave_addr, true);
180 if (ret < 0)
181 return ret;
182 } else {
183 match = schan->real_slave_id;
184 }
185
186 if (schan->real_slave_id < 0 || schan->real_slave_id >= slave_num)
187 return -EINVAL;
188
189 if (test_and_set_bit(schan->real_slave_id, shdma_slave_used))
190 return -EBUSY;
191
192 ret = ops->set_slave(schan, match, slave_addr, false);
193 if (ret < 0) {
194 clear_bit(schan->real_slave_id, shdma_slave_used);
195 return ret;
196 }
197
198 schan->slave_id = schan->real_slave_id;
199
200 return 0;
201}
202
203static int shdma_alloc_chan_resources(struct dma_chan *chan)
204{
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;
210 int ret, i;
211
212
213
214
215
216 if (slave) {
217
218 schan->real_slave_id = slave->slave_id;
219 ret = shdma_setup_slave(schan, 0);
220 if (ret < 0)
221 goto esetslave;
222 } else {
223
224 schan->slave_id = -EINVAL;
225 }
226
227 schan->desc = kcalloc(NR_DESCS_PER_CHANNEL,
228 sdev->desc_size, GFP_KERNEL);
229 if (!schan->desc) {
230 ret = -ENOMEM;
231 goto edescalloc;
232 }
233 schan->desc_num = NR_DESCS_PER_CHANNEL;
234
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,
238 &schan->dma_chan);
239 desc->async_tx.tx_submit = shdma_tx_submit;
240 desc->mark = DESC_IDLE;
241
242 list_add(&desc->node, &schan->ld_free);
243 }
244
245 return NR_DESCS_PER_CHANNEL;
246
247edescalloc:
248 if (slave)
249esetslave:
250 clear_bit(slave->slave_id, shdma_slave_used);
251 chan->private = NULL;
252 return ret;
253}
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274bool shdma_chan_filter(struct dma_chan *chan, void *arg)
275{
276 struct shdma_chan *schan;
277 struct shdma_dev *sdev;
278 int slave_id = (long)arg;
279 int ret;
280
281
282 if (chan->device->device_alloc_chan_resources !=
283 shdma_alloc_chan_resources)
284 return false;
285
286 schan = to_shdma_chan(chan);
287 sdev = to_shdma_dev(chan->device);
288
289
290
291
292
293
294
295 if (schan->dev->of_node) {
296 ret = sdev->ops->set_slave(schan, slave_id, 0, true);
297 if (ret < 0)
298 return false;
299
300 schan->real_slave_id = schan->slave_id;
301 return true;
302 }
303
304 if (slave_id < 0) {
305
306 dev_warn(sdev->dma_dev.dev, "invalid slave ID passed to dma_request_slave\n");
307 return true;
308 }
309
310 if (slave_id >= slave_num)
311 return false;
312
313 ret = sdev->ops->set_slave(schan, slave_id, 0, true);
314 if (ret < 0)
315 return false;
316
317 schan->real_slave_id = slave_id;
318
319 return true;
320}
321EXPORT_SYMBOL(shdma_chan_filter);
322
323static dma_async_tx_callback __ld_cleanup(struct shdma_chan *schan, bool all)
324{
325 struct shdma_desc *desc, *_desc;
326
327 bool head_acked = false;
328 dma_cookie_t cookie = 0;
329 dma_async_tx_callback callback = NULL;
330 struct dmaengine_desc_callback cb;
331 unsigned long flags;
332 LIST_HEAD(cyclic_list);
333
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;
338
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);
343
344
345
346
347
348
349 if (!all && desc->mark == DESC_SUBMITTED &&
350 desc->cookie != cookie)
351 break;
352
353 if (tx->cookie > 0)
354 cookie = tx->cookie;
355
356 if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
357 if (schan->dma_chan.completed_cookie != desc->cookie - 1)
358 dev_dbg(schan->dev,
359 "Completing cookie %d, expected %d\n",
360 desc->cookie,
361 schan->dma_chan.completed_cookie + 1);
362 schan->dma_chan.completed_cookie = desc->cookie;
363 }
364
365
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);
373 break;
374 }
375
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;
380 }
381 head_acked = async_tx_test_ack(tx);
382 } else {
383 switch (desc->mark) {
384 case DESC_COMPLETED:
385 desc->mark = DESC_WAITING;
386 fallthrough;
387 case DESC_WAITING:
388 if (head_acked)
389 async_tx_ack(&desc->async_tx);
390 }
391 }
392
393 dev_dbg(schan->dev, "descriptor %p #%d completed.\n",
394 tx, tx->cookie);
395
396 if (((desc->mark == DESC_COMPLETED ||
397 desc->mark == DESC_WAITING) &&
398 async_tx_test_ack(&desc->async_tx)) || all) {
399
400 if (all || !desc->cyclic) {
401
402 desc->mark = DESC_IDLE;
403 list_move(&desc->node, &schan->ld_free);
404 } else {
405
406 desc->mark = DESC_SUBMITTED;
407 list_move_tail(&desc->node, &cyclic_list);
408 }
409
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);
416 }
417 }
418 }
419
420 if (all && !callback)
421
422
423
424
425 schan->dma_chan.completed_cookie = schan->dma_chan.cookie;
426
427 list_splice_tail(&cyclic_list, &schan->ld_queue);
428
429 spin_unlock_irqrestore(&schan->chan_lock, flags);
430
431 dmaengine_desc_callback_invoke(&cb, NULL);
432
433 return callback;
434}
435
436
437
438
439
440
441static void shdma_chan_ld_cleanup(struct shdma_chan *schan, bool all)
442{
443 while (__ld_cleanup(schan, all))
444 ;
445}
446
447
448
449
450static void shdma_free_chan_resources(struct dma_chan *chan)
451{
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;
455 LIST_HEAD(list);
456
457
458 spin_lock_irq(&schan->chan_lock);
459 ops->halt_channel(schan);
460 spin_unlock_irq(&schan->chan_lock);
461
462
463
464
465 if (!list_empty(&schan->ld_queue))
466 shdma_chan_ld_cleanup(schan, true);
467
468 if (schan->slave_id >= 0) {
469
470 clear_bit(schan->slave_id, shdma_slave_used);
471 chan->private = NULL;
472 }
473
474 schan->real_slave_id = 0;
475
476 spin_lock_irq(&schan->chan_lock);
477
478 list_splice_init(&schan->ld_free, &list);
479 schan->desc_num = 0;
480
481 spin_unlock_irq(&schan->chan_lock);
482
483 kfree(schan->desc);
484}
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501static 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)
504{
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;
509
510 if (!copy_size)
511 return NULL;
512
513
514 new = shdma_get_desc(schan);
515 if (!new) {
516 dev_err(schan->dev, "No free link descriptor available\n");
517 return NULL;
518 }
519
520 ops->desc_setup(schan, new, *src, *dst, ©_size);
521
522 if (!*first) {
523
524 new->async_tx.cookie = -EBUSY;
525 *first = new;
526 } else {
527
528 new->async_tx.cookie = -EINVAL;
529 }
530
531 dev_dbg(schan->dev,
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);
535
536 new->mark = DESC_PREPARED;
537 new->async_tx.flags = flags;
538 new->direction = direction;
539 new->partial = 0;
540
541 *len -= copy_size;
542 if (direction == DMA_MEM_TO_MEM || direction == DMA_MEM_TO_DEV)
543 *src += copy_size;
544 if (direction == DMA_MEM_TO_MEM || direction == DMA_DEV_TO_MEM)
545 *dst += copy_size;
546
547 return new;
548}
549
550
551
552
553
554
555
556
557
558
559
560static 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)
563{
564 struct scatterlist *sg;
565 struct shdma_desc *first = NULL, *new = NULL ;
566 LIST_HEAD(tx_list);
567 int chunks = 0;
568 unsigned long irq_flags;
569 int i;
570
571 for_each_sg(sgl, sg, sg_len, i)
572 chunks += DIV_ROUND_UP(sg_dma_len(sg), schan->max_xfer_len);
573
574
575 spin_lock_irqsave(&schan->chan_lock, irq_flags);
576
577
578
579
580
581
582
583
584
585
586
587
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);
591
592 if (!len)
593 goto err_get_desc;
594
595 do {
596 dev_dbg(schan->dev, "Add SG #%d@%p[%zu], dma %pad\n",
597 i, sg, len, &sg_addr);
598
599 if (direction == DMA_DEV_TO_MEM)
600 new = shdma_add_desc(schan, flags,
601 &sg_addr, addr, &len, &first,
602 direction);
603 else
604 new = shdma_add_desc(schan, flags,
605 addr, &sg_addr, &len, &first,
606 direction);
607 if (!new)
608 goto err_get_desc;
609
610 new->cyclic = cyclic;
611 if (cyclic)
612 new->chunks = 1;
613 else
614 new->chunks = chunks--;
615 list_add_tail(&new->node, &tx_list);
616 } while (len);
617 }
618
619 if (new != first)
620 new->async_tx.cookie = -ENOSPC;
621
622
623 list_splice_tail(&tx_list, &schan->ld_free);
624
625 spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
626
627 return &first->async_tx;
628
629err_get_desc:
630 list_for_each_entry(new, &tx_list, node)
631 new->mark = DESC_IDLE;
632 list_splice(&tx_list, &schan->ld_free);
633
634 spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
635
636 return NULL;
637}
638
639static 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)
642{
643 struct shdma_chan *schan = to_shdma_chan(chan);
644 struct scatterlist sg;
645
646 if (!chan || !len)
647 return NULL;
648
649 BUG_ON(!schan->desc_num);
650
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;
656
657 return shdma_prep_sg(schan, &sg, 1, &dma_dest, DMA_MEM_TO_MEM,
658 flags, false);
659}
660
661static 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)
664{
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;
670
671 if (!chan)
672 return NULL;
673
674 BUG_ON(!schan->desc_num);
675
676
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);
680 return NULL;
681 }
682
683 slave_addr = ops->slave_addr(schan);
684
685 return shdma_prep_sg(schan, sgl, sg_len, &slave_addr,
686 direction, flags, false);
687}
688
689#define SHDMA_MAX_SG_LEN 32
690
691static 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,
694 unsigned long flags)
695{
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;
704 int i;
705
706 if (!chan)
707 return NULL;
708
709 BUG_ON(!schan->desc_num);
710
711 if (sg_len > SHDMA_MAX_SG_LEN) {
712 dev_err(schan->dev, "sg length %d exceeds limit %d",
713 sg_len, SHDMA_MAX_SG_LEN);
714 return NULL;
715 }
716
717
718 if (slave_id < 0 || (buf_len < period_len)) {
719 dev_warn(schan->dev,
720 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
721 __func__, buf_len, period_len, slave_id);
722 return NULL;
723 }
724
725 slave_addr = ops->slave_addr(schan);
726
727
728
729
730
731 sgl = kmalloc_array(sg_len, sizeof(*sgl), GFP_KERNEL);
732 if (!sgl)
733 return NULL;
734
735 sg_init_table(sgl, sg_len);
736
737 for (i = 0; i < sg_len; i++) {
738 dma_addr_t src = buf_addr + (period_len * i);
739
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;
744 }
745
746 desc = shdma_prep_sg(schan, sgl, sg_len, &slave_addr,
747 direction, flags, true);
748
749 kfree(sgl);
750 return desc;
751}
752
753static int shdma_terminate_all(struct dma_chan *chan)
754{
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;
758 unsigned long flags;
759
760 spin_lock_irqsave(&schan->chan_lock, flags);
761 ops->halt_channel(schan);
762
763 if (ops->get_partial && !list_empty(&schan->ld_queue)) {
764
765 struct shdma_desc *desc = list_first_entry(&schan->ld_queue,
766 struct shdma_desc, node);
767 desc->partial = ops->get_partial(schan, desc);
768 }
769
770 spin_unlock_irqrestore(&schan->chan_lock, flags);
771
772 shdma_chan_ld_cleanup(schan, true);
773
774 return 0;
775}
776
777static int shdma_config(struct dma_chan *chan,
778 struct dma_slave_config *config)
779{
780 struct shdma_chan *schan = to_shdma_chan(chan);
781
782
783
784
785
786 if (!config)
787 return -EINVAL;
788
789
790
791
792
793 if (WARN_ON_ONCE(config->slave_id &&
794 config->slave_id != schan->real_slave_id))
795 schan->real_slave_id = config->slave_id;
796
797
798
799
800
801 return shdma_setup_slave(schan,
802 config->direction == DMA_DEV_TO_MEM ?
803 config->src_addr : config->dst_addr);
804}
805
806static void shdma_issue_pending(struct dma_chan *chan)
807{
808 struct shdma_chan *schan = to_shdma_chan(chan);
809
810 spin_lock_irq(&schan->chan_lock);
811 if (schan->pm_state == SHDMA_PM_ESTABLISHED)
812 shdma_chan_xfer_ld_queue(schan);
813 else
814 schan->pm_state = SHDMA_PM_PENDING;
815 spin_unlock_irq(&schan->chan_lock);
816}
817
818static enum dma_status shdma_tx_status(struct dma_chan *chan,
819 dma_cookie_t cookie,
820 struct dma_tx_state *txstate)
821{
822 struct shdma_chan *schan = to_shdma_chan(chan);
823 enum dma_status status;
824 unsigned long flags;
825
826 shdma_chan_ld_cleanup(schan, false);
827
828 spin_lock_irqsave(&schan->chan_lock, flags);
829
830 status = dma_cookie_status(chan, cookie, txstate);
831
832
833
834
835
836 if (status != DMA_COMPLETE) {
837 struct shdma_desc *sdesc;
838 status = DMA_ERROR;
839 list_for_each_entry(sdesc, &schan->ld_queue, node)
840 if (sdesc->cookie == cookie) {
841 status = DMA_IN_PROGRESS;
842 break;
843 }
844 }
845
846 spin_unlock_irqrestore(&schan->chan_lock, flags);
847
848 return status;
849}
850
851
852bool shdma_reset(struct shdma_dev *sdev)
853{
854 const struct shdma_ops *ops = sdev->ops;
855 struct shdma_chan *schan;
856 unsigned int handled = 0;
857 int i;
858
859
860 shdma_for_each_chan(schan, sdev, i) {
861 struct shdma_desc *sdesc;
862 LIST_HEAD(dl);
863
864 if (!schan)
865 continue;
866
867 spin_lock(&schan->chan_lock);
868
869
870 ops->halt_channel(schan);
871
872 list_splice_init(&schan->ld_queue, &dl);
873
874 if (!list_empty(&dl)) {
875 dev_dbg(schan->dev, "Bring down channel %d\n", schan->id);
876 pm_runtime_put(schan->dev);
877 }
878 schan->pm_state = SHDMA_PM_ESTABLISHED;
879
880 spin_unlock(&schan->chan_lock);
881
882
883 list_for_each_entry(sdesc, &dl, node) {
884 struct dma_async_tx_descriptor *tx = &sdesc->async_tx;
885
886 sdesc->mark = DESC_IDLE;
887 dmaengine_desc_get_callback_invoke(tx, NULL);
888 }
889
890 spin_lock(&schan->chan_lock);
891 list_splice(&dl, &schan->ld_free);
892 spin_unlock(&schan->chan_lock);
893
894 handled++;
895 }
896
897 return !!handled;
898}
899EXPORT_SYMBOL(shdma_reset);
900
901static irqreturn_t chan_irq(int irq, void *dev)
902{
903 struct shdma_chan *schan = dev;
904 const struct shdma_ops *ops =
905 to_shdma_dev(schan->dma_chan.device)->ops;
906 irqreturn_t ret;
907
908 spin_lock(&schan->chan_lock);
909
910 ret = ops->chan_irq(schan, irq) ? IRQ_WAKE_THREAD : IRQ_NONE;
911
912 spin_unlock(&schan->chan_lock);
913
914 return ret;
915}
916
917static irqreturn_t chan_irqt(int irq, void *dev)
918{
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;
923
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;
931 break;
932 }
933 }
934
935 shdma_chan_xfer_ld_queue(schan);
936 spin_unlock_irq(&schan->chan_lock);
937
938 shdma_chan_ld_cleanup(schan, false);
939
940 return IRQ_HANDLED;
941}
942
943int shdma_request_irq(struct shdma_chan *schan, int irq,
944 unsigned long flags, const char *name)
945{
946 int ret = devm_request_threaded_irq(schan->dev, irq, chan_irq,
947 chan_irqt, flags, name, schan);
948
949 schan->irq = ret < 0 ? ret : irq;
950
951 return ret;
952}
953EXPORT_SYMBOL(shdma_request_irq);
954
955void shdma_chan_probe(struct shdma_dev *sdev,
956 struct shdma_chan *schan, int id)
957{
958 schan->pm_state = SHDMA_PM_ESTABLISHED;
959
960
961 schan->dma_chan.device = &sdev->dma_dev;
962 dma_cookie_init(&schan->dma_chan);
963
964 schan->dev = sdev->dma_dev.dev;
965 schan->id = id;
966
967 if (!schan->max_xfer_len)
968 schan->max_xfer_len = PAGE_SIZE;
969
970 spin_lock_init(&schan->chan_lock);
971
972
973 INIT_LIST_HEAD(&schan->ld_queue);
974 INIT_LIST_HEAD(&schan->ld_free);
975
976
977 list_add_tail(&schan->dma_chan.device_node,
978 &sdev->dma_dev.channels);
979 sdev->schan[id] = schan;
980}
981EXPORT_SYMBOL(shdma_chan_probe);
982
983void shdma_chan_remove(struct shdma_chan *schan)
984{
985 list_del(&schan->dma_chan.device_node);
986}
987EXPORT_SYMBOL(shdma_chan_remove);
988
989int shdma_init(struct device *dev, struct shdma_dev *sdev,
990 int chan_num)
991{
992 struct dma_device *dma_dev = &sdev->dma_dev;
993
994
995
996
997
998 if (!sdev->ops ||
999 !sdev->desc_size ||
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)
1009 return -EINVAL;
1010
1011 sdev->schan = kcalloc(chan_num, sizeof(*sdev->schan), GFP_KERNEL);
1012 if (!sdev->schan)
1013 return -ENOMEM;
1014
1015 INIT_LIST_HEAD(&dma_dev->channels);
1016
1017
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;
1024
1025
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;
1030
1031 dma_dev->dev = dev;
1032
1033 return 0;
1034}
1035EXPORT_SYMBOL(shdma_init);
1036
1037void shdma_cleanup(struct shdma_dev *sdev)
1038{
1039 kfree(sdev->schan);
1040}
1041EXPORT_SYMBOL(shdma_cleanup);
1042
1043static int __init shdma_enter(void)
1044{
1045 shdma_slave_used = kcalloc(DIV_ROUND_UP(slave_num, BITS_PER_LONG),
1046 sizeof(long),
1047 GFP_KERNEL);
1048 if (!shdma_slave_used)
1049 return -ENOMEM;
1050 return 0;
1051}
1052module_init(shdma_enter);
1053
1054static void __exit shdma_exit(void)
1055{
1056 kfree(shdma_slave_used);
1057}
1058module_exit(shdma_exit);
1059
1060MODULE_LICENSE("GPL v2");
1061MODULE_DESCRIPTION("SH-DMA driver base library");
1062MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1063