aboutsummaryrefslogtreecommitdiff
path: root/drivers/platform/msm/sps/sps_dma.c
blob: 0cc428399ecfd3b875dd2f377098411a7e204835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/* Copyright (c) 2011-2013, 2015, 2017, 2019, The Linux Foundation. All rights
 * reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/* BAM-DMA Manager. */

#ifdef CONFIG_SPS_SUPPORT_BAMDMA

#include <linux/export.h>
#include <linux/memory.h>	/* memset */

#include "spsi.h"
#include "bam.h"
#include "sps_bam.h"		/* bam_dma_thresh_dma */
#include "sps_core.h"		/* sps_h2bam() */

/**
 * registers
 */

#define DMA_ENBL			(0x00000000)
#ifdef CONFIG_SPS_SUPPORT_NDP_BAM
#define DMA_REVISION			(0x00000004)
#define DMA_CONFIG			(0x00000008)
#define DMA_CHNL_CONFIG(n)		(0x00001000 + 4096 * (n))
#else
#define DMA_CHNL_CONFIG(n)		(0x00000004 + 4 * (n))
#define DMA_CONFIG			(0x00000040)
#endif

/**
 * masks
 */

/* DMA_CHNL_confign */
#ifdef CONFIG_SPS_SUPPORT_NDP_BAM
#define DMA_CHNL_PRODUCER_PIPE_ENABLED	0x40000
#define DMA_CHNL_CONSUMER_PIPE_ENABLED	0x20000
#endif
#define DMA_CHNL_HALT_DONE		0x10000
#define DMA_CHNL_HALT			0x1000
#define DMA_CHNL_ENABLE                 0x100
#define DMA_CHNL_ACT_THRESH             0x30
#define DMA_CHNL_WEIGHT                 0x7

/* DMA_CONFIG */
#define TESTBUS_SELECT                  0x3

/**
 *
 * Write register with debug info.
 *
 * @base - bam base virtual address.
 * @offset - register offset.
 * @val - value to write.
 *
 */
static inline void dma_write_reg(void *base, u32 offset, u32 val)
{
	iowrite32(val, base + offset);
	SPS_DBG(sps, "sps:bamdma: write reg 0x%x w_val 0x%x.", offset, val);
}

/**
 * Write register masked field with debug info.
 *
 * @base - bam base virtual address.
 * @offset - register offset.
 * @mask - register bitmask.
 * @val - value to write.
 *
 */
static inline void dma_write_reg_field(void *base, u32 offset,
				       const u32 mask, u32 val)
{
	u32 shift = find_first_bit((void *)&mask, 32);
	u32 tmp = ioread32(base + offset);

	tmp &= ~mask;		/* clear written bits */
	val = tmp | (val << shift);
	iowrite32(val, base + offset);
	SPS_DBG(sps, "sps:bamdma: write reg 0x%x w_val 0x%x.", offset, val);
}

/* Round max number of pipes to nearest multiple of 2 */
#define DMA_MAX_PIPES         ((BAM_MAX_PIPES / 2) * 2)

/* Maximum number of BAM-DMAs supported */
#define MAX_BAM_DMA_DEVICES   1

/* Maximum number of BAMs that will be registered */
#define MAX_BAM_DMA_BAMS      1

/* Pipe enable check values */
#define DMA_PIPES_STATE_DIFF     0
#define DMA_PIPES_BOTH_DISABLED  1
#define DMA_PIPES_BOTH_ENABLED   2

/* Even pipe is tx/dest/input/write, odd pipe is rx/src/output/read */
#define DMA_PIPE_IS_DEST(p)   (((p) & 1) == 0)
#define DMA_PIPE_IS_SRC(p)    (((p) & 1) != 0)

/* BAM DMA pipe state */
enum bamdma_pipe_state {
	PIPE_INACTIVE = 0,
	PIPE_ACTIVE
};

/* BAM DMA channel state */
enum bamdma_chan_state {
	DMA_CHAN_STATE_FREE = 0,
	DMA_CHAN_STATE_ALLOC_EXT,	/* Client allocation */
	DMA_CHAN_STATE_ALLOC_INT	/* Internal (resource mgr) allocation */
};

struct bamdma_chan {
	/* Allocation state */
	enum bamdma_chan_state state;

	/* BAM DMA channel configuration parameters */
	u32 threshold;
	enum sps_dma_priority priority;

	/* HWIO channel configuration parameters */
	enum bam_dma_thresh_dma thresh;
	enum bam_dma_weight_dma weight;

};

/* BAM DMA device state */
struct bamdma_device {
	/* BAM-DMA device state */
	int enabled;
	int local;

	/* BAM device state */
	struct sps_bam *bam;

	/* BAM handle, for deregistration */
	unsigned long h;

	/* BAM DMA device virtual mapping */
	void *virt_addr;
	int virtual_mapped;
	phys_addr_t phys_addr;
	void *hwio;

	/* BAM DMA pipe/channel state */
	u32 num_pipes;
	enum bamdma_pipe_state pipes[DMA_MAX_PIPES];
	struct bamdma_chan chans[DMA_MAX_PIPES / 2];

};

/* BAM-DMA devices */
static struct bamdma_device bam_dma_dev[MAX_BAM_DMA_DEVICES];
static struct mutex bam_dma_lock;

/*
 * The BAM DMA module registers all BAMs in the BSP properties, but only
 * uses the first BAM-DMA device for allocations.  References to the others
 * are stored in the following data array.
 */
static int num_bams;
static unsigned long bam_handles[MAX_BAM_DMA_BAMS];

/**
 * Find BAM-DMA device
 *
 * This function finds the BAM-DMA device associated with the BAM handle.
 *
 * @h - BAM handle
 *
 * @return - pointer to BAM-DMA device, or NULL on error
 *
 */
static struct bamdma_device *sps_dma_find_device(unsigned long h)
{
	return &bam_dma_dev[0];
}

/**
 * BAM DMA device enable
 *
 * This function enables a BAM DMA device and the associated BAM.
 *
 * @dev - pointer to BAM DMA device context
 *
 * @return 0 on success, negative value on error
 *
 */
static int sps_dma_device_enable(struct bamdma_device *dev)
{
	if (dev->enabled)
		return 0;

	/*
	 *  If the BAM-DMA device is locally controlled then enable BAM-DMA
	 *  device
	 */
	if (dev->local)
		dma_write_reg(dev->virt_addr, DMA_ENBL, 1);

	/* Enable BAM device */
	if (sps_bam_enable(dev->bam)) {
		SPS_ERR(sps, "sps:Failed to enable BAM DMA's BAM: %pa",
			&dev->phys_addr);
		return SPS_ERROR;
	}

	dev->enabled = true;

	return 0;
}

/**
 * BAM DMA device enable
 *
 * This function initializes a BAM DMA device.
 *
 * @dev - pointer to BAM DMA device context
 *
 * @return 0 on success, negative value on error
 *
 */
static int sps_dma_device_disable(struct bamdma_device *dev)
{
	u32 pipe_index;

	if (!dev->enabled)
		return 0;

	/* Do not disable if channels active */
	for (pipe_index = 0; pipe_index < dev->num_pipes; pipe_index++) {
		if (dev->pipes[pipe_index] != PIPE_INACTIVE)
			break;
	}

	if (pipe_index < dev->num_pipes) {
		SPS_ERR(sps,
			"sps:Fail to disable BAM-DMA %pa:channels are active",
			&dev->phys_addr);
		return SPS_ERROR;
	}

	dev->enabled = false;

	/* Disable BAM device */
	if (sps_bam_disable(dev->bam)) {
		SPS_ERR(sps,
			"sps:Fail to disable BAM-DMA BAM:%pa", &dev->phys_addr);
		return SPS_ERROR;
	}

	/* Is the BAM-DMA device locally controlled? */
	if (dev->local)
		/* Disable BAM-DMA device */
		dma_write_reg(dev->virt_addr, DMA_ENBL, 0);

	return 0;
}

/**
 * Initialize BAM DMA device
 *
 */
int sps_dma_device_init(unsigned long h)
{
	struct bamdma_device *dev;
	struct sps_bam_props *props;
	int result = SPS_ERROR;

	mutex_lock(&bam_dma_lock);

	/* Find a free BAM-DMA device slot */
	dev = NULL;
	if (bam_dma_dev[0].bam != NULL) {
		SPS_ERR(sps,
			"sps:%s:BAM-DMA BAM device is already initialized.",
			__func__);
		goto exit_err;
	} else {
		dev = &bam_dma_dev[0];
	}

	/* Record BAM */
	memset(dev, 0, sizeof(*dev));
	dev->h = h;
	dev->bam = sps_h2bam(h);

	if (dev->bam == NULL) {
		SPS_ERR(sps,
			"sps:%s:BAM-DMA BAM device is not found from the handle.",
			__func__);
		goto exit_err;
	}

	/* Map the BAM DMA device into virtual space, if necessary */
	props = &dev->bam->props;
	dev->phys_addr = props->periph_phys_addr;
	if (props->periph_virt_addr != NULL) {
		dev->virt_addr = props->periph_virt_addr;
		dev->virtual_mapped = false;
	} else {
		if (props->periph_virt_size == 0) {
			SPS_ERR(sps,
				"sps:Unable to map BAM DMA IO memory: %pa %x",
				&dev->phys_addr, props->periph_virt_size);
			goto exit_err;
		}

		dev->virt_addr = ioremap(dev->phys_addr,
					  props->periph_virt_size);
		if (dev->virt_addr == NULL) {
			SPS_ERR(sps,
				"sps:Unable to map BAM DMA IO memory: %pa %x",
				&dev->phys_addr, props->periph_virt_size);
			goto exit_err;
		}
		dev->virtual_mapped = true;
	}
	dev->hwio = (void *) dev->virt_addr;

	/* Is the BAM-DMA device locally controlled? */
	if ((props->manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0) {
		SPS_DBG3(sps, "sps:BAM-DMA is controlled locally: %pa",
			&dev->phys_addr);
		dev->local = true;
	} else {
		SPS_DBG3(sps, "sps:BAM-DMA is controlled remotely: %pa",
			&dev->phys_addr);
		dev->local = false;
	}

	/*
	 * Enable the BAM DMA and determine the number of pipes/channels.
	 * Leave the BAM-DMA enabled, since it is always a shared device.
	 */
	if (sps_dma_device_enable(dev))
		goto exit_err;

	dev->num_pipes = dev->bam->props.num_pipes;

	result = 0;
exit_err:
	if (result) {
		if (dev != NULL) {
			if (dev->virtual_mapped)
				iounmap(dev->virt_addr);

			dev->bam = NULL;
		}
	}

	mutex_unlock(&bam_dma_lock);

	return result;
}

/**
 * De-initialize BAM DMA device
 *
 */
int sps_dma_device_de_init(unsigned long h)
{
	struct bamdma_device *dev;
	u32 pipe_index;
	u32 chan;
	int result = 0;

	mutex_lock(&bam_dma_lock);

	dev = sps_dma_find_device(h);
	if (dev == NULL) {
		SPS_ERR(sps, "sps:BAM-DMA: not registered: %pK", (void *)h);
		result = SPS_ERROR;
		goto exit_err;
	}

	/* Check for channel leaks */
	for (chan = 0; chan < dev->num_pipes / 2; chan++) {
		if (dev->chans[chan].state != DMA_CHAN_STATE_FREE) {
			SPS_ERR(sps, "sps:BAM-DMA: channel not free: %d", chan);
			result = SPS_ERROR;
			dev->chans[chan].state = DMA_CHAN_STATE_FREE;
		}
	}
	for (pipe_index = 0; pipe_index < dev->num_pipes; pipe_index++) {
		if (dev->pipes[pipe_index] != PIPE_INACTIVE) {
			SPS_ERR(sps, "sps:BAM-DMA: pipe not inactive: %d",
					pipe_index);
			result = SPS_ERROR;
			dev->pipes[pipe_index] = PIPE_INACTIVE;
		}
	}

	/* Disable BAM and BAM-DMA */
	if (sps_dma_device_disable(dev))
		result = SPS_ERROR;

	dev->h = BAM_HANDLE_INVALID;
	dev->bam = NULL;
	if (dev->virtual_mapped)
		iounmap(dev->virt_addr);

exit_err:
	mutex_unlock(&bam_dma_lock);

	return result;
}

/**
 * Initialize BAM DMA module
 *
 */
int sps_dma_init(const struct sps_bam_props *bam_props)
{
	struct sps_bam_props props;
	const struct sps_bam_props *bam_reg;
	unsigned long h;

	/* Init local data */
	memset(&bam_dma_dev, 0, sizeof(bam_dma_dev));
	num_bams = 0;
	memset(bam_handles, 0, sizeof(bam_handles));

	/* Create a mutex to control access to the BAM-DMA devices */
	mutex_init(&bam_dma_lock);

	/* Are there any BAM DMA devices? */
	if (bam_props == NULL)
		return 0;

	/*
	 * Registers all BAMs in the BSP properties, but only uses the first
	 * BAM-DMA device for allocations.
	 */
	if (bam_props->phys_addr) {
		/* Force multi-EE option for all BAM-DMAs */
		bam_reg = bam_props;
		if ((bam_props->options & SPS_BAM_OPT_BAMDMA) &&
		    (bam_props->manage & SPS_BAM_MGR_MULTI_EE) == 0) {
			SPS_DBG(sps,
				"sps:Setting multi-EE options for BAM-DMA: %pa",
				&bam_props->phys_addr);
			props = *bam_props;
			props.manage |= SPS_BAM_MGR_MULTI_EE;
			bam_reg = &props;
		}

		/* Register the BAM */
		if (sps_register_bam_device(bam_reg, &h)) {
			SPS_ERR(sps,
				"sps:Fail to register BAM-DMA BAM device: phys %pa",
				&bam_props->phys_addr);
			return SPS_ERROR;
		}

		/* Record the BAM so that it may be deregistered later */
		if (num_bams < MAX_BAM_DMA_BAMS) {
			bam_handles[num_bams] = h;
			num_bams++;
		} else {
			SPS_ERR(sps, "sps:BAM-DMA: BAM limit exceeded: %d",
					num_bams);
			return SPS_ERROR;
		}
	} else {
		SPS_ERR(sps,
			"sps:%s:BAM-DMA phys_addr is zero.",
			__func__);
		return SPS_ERROR;
	}


	return 0;
}

/**
 * De-initialize BAM DMA module
 *
 */
void sps_dma_de_init(void)
{
	int n;

	/* De-initialize the BAM devices */
	for (n = 0; n < num_bams; n++)
		sps_deregister_bam_device(bam_handles[n]);

	/* Clear local data */
	memset(&bam_dma_dev, 0, sizeof(bam_dma_dev));
	num_bams = 0;
	memset(bam_handles, 0, sizeof(bam_handles));
}

/**
 * Allocate a BAM DMA channel
 *
 */
int sps_alloc_dma_chan(const struct sps_alloc_dma_chan *alloc,
		       struct sps_dma_chan *chan_info)
{
	struct bamdma_device *dev;
	struct bamdma_chan *chan;
	u32 pipe_index;
	enum bam_dma_thresh_dma thresh = (enum bam_dma_thresh_dma) 0;
	enum bam_dma_weight_dma weight = (enum bam_dma_weight_dma) 0;
	int result = SPS_ERROR;

	if (alloc == NULL || chan_info == NULL) {
		SPS_ERR(sps,
			"sps:%s:invalid parameters", __func__);
		return SPS_ERROR;
	}

	/* Translate threshold and priority to hwio values */
	if (alloc->threshold != SPS_DMA_THRESHOLD_DEFAULT) {
		if (alloc->threshold >= 512)
			thresh = BAM_DMA_THRESH_512;
		else if (alloc->threshold >= 256)
			thresh = BAM_DMA_THRESH_256;
		else if (alloc->threshold >= 128)
			thresh = BAM_DMA_THRESH_128;
		else
			thresh = BAM_DMA_THRESH_64;
	}

	weight = alloc->priority;

	if ((u32)alloc->priority > (u32)BAM_DMA_WEIGHT_HIGH) {
		SPS_ERR(sps, "sps:BAM-DMA: invalid priority: %x",
						alloc->priority);
		return SPS_ERROR;
	}

	mutex_lock(&bam_dma_lock);

	dev = sps_dma_find_device(alloc->dev);
	if (dev == NULL) {
		SPS_ERR(sps, "sps:BAM-DMA: invalid BAM handle: %pK",
							(void *)alloc->dev);
		goto exit_err;
	}

	/* Search for a free set of pipes */
	for (pipe_index = 0, chan = dev->chans;
	      pipe_index < dev->num_pipes; pipe_index += 2, chan++) {
		if (chan->state == DMA_CHAN_STATE_FREE) {
			/* Just check pipes for safety */
			if (dev->pipes[pipe_index] != PIPE_INACTIVE ||
			    dev->pipes[pipe_index + 1] != PIPE_INACTIVE) {
				SPS_ERR(sps,
					"sps:BAM-DMA: channel %d state error:%d %d",
					pipe_index / 2, dev->pipes[pipe_index],
				 dev->pipes[pipe_index + 1]);
				goto exit_err;
			}
			break; /* Found free pipe */
		}
	}

	if (pipe_index >= dev->num_pipes) {
		SPS_ERR(sps, "sps:BAM-DMA: no free channel. num_pipes = %d",
			dev->num_pipes);
		goto exit_err;
	}

	chan->state = DMA_CHAN_STATE_ALLOC_EXT;

	/* Store config values for use when pipes are activated */
	chan = &dev->chans[pipe_index / 2];
	chan->threshold = alloc->threshold;
	chan->thresh = thresh;
	chan->priority = alloc->priority;
	chan->weight = weight;

	SPS_DBG3(sps, "sps:sps_alloc_dma_chan. pipe %d.\n", pipe_index);

	/* Report allocated pipes to client */
	chan_info->dev = dev->h;
	/* Dest/input/write pipex */
	chan_info->dest_pipe_index = pipe_index;
	/* Source/output/read pipe */
	chan_info->src_pipe_index = pipe_index + 1;

	result = 0;
exit_err:
	mutex_unlock(&bam_dma_lock);

	return result;
}
EXPORT_SYMBOL(sps_alloc_dma_chan);

/**
 * Free a BAM DMA channel
 *
 */
int sps_free_dma_chan(struct sps_dma_chan *chan)
{
	struct bamdma_device *dev;
	u32 pipe_index;
	int result = 0;

	if (chan == NULL) {
		SPS_ERR(sps,
			"sps:%s:chan is NULL", __func__);
		return SPS_ERROR;
	}

	mutex_lock(&bam_dma_lock);

	dev = sps_dma_find_device(chan->dev);
	if (dev == NULL) {
		SPS_ERR(sps, "sps:BAM-DMA: invalid BAM handle: %pK",
			(void *)chan->dev);
		result = SPS_ERROR;
		goto exit_err;
	}

	/* Verify the pipe indices */
	pipe_index = chan->dest_pipe_index;
	if (pipe_index >= dev->num_pipes || ((pipe_index & 1)) ||
	    (pipe_index + 1) != chan->src_pipe_index) {
		SPS_ERR(sps,
			"sps:sps_free_dma_chan. Invalid pipe indices. num_pipes=%d.dest=%d.src=%d.",
			dev->num_pipes,
			chan->dest_pipe_index,
			chan->src_pipe_index);
		result = SPS_ERROR;
		goto exit_err;
	}

	/* Are both pipes inactive? */
	if (dev->chans[pipe_index / 2].state != DMA_CHAN_STATE_ALLOC_EXT ||
	    dev->pipes[pipe_index] != PIPE_INACTIVE ||
	    dev->pipes[pipe_index + 1] != PIPE_INACTIVE) {
		SPS_ERR(sps,
			"sps:BAM-DMA: attempt to free active chan %d: %d %d",
			pipe_index / 2, dev->pipes[pipe_index],
			dev->pipes[pipe_index + 1]);
		result = SPS_ERROR;
		goto exit_err;
	}

	/* Free the channel */
	dev->chans[pipe_index / 2].state = DMA_CHAN_STATE_FREE;

exit_err:
	mutex_unlock(&bam_dma_lock);

	return result;
}
EXPORT_SYMBOL(sps_free_dma_chan);

/**
 * Activate a BAM DMA pipe
 *
 * This function activates a BAM DMA pipe.
 *
 * @dev - pointer to BAM-DMA device descriptor
 *
 * @pipe_index - pipe index
 *
 * @return 0 on success, negative value on error
 *
 */
static u32 sps_dma_check_pipes(struct bamdma_device *dev, u32 pipe_index)
{
	u32 pipe_in;
	u32 pipe_out;
	int enabled_in;
	int enabled_out;
	u32 check;

	pipe_in = pipe_index & ~1;
	pipe_out = pipe_in + 1;
	enabled_in = bam_pipe_is_enabled(&dev->bam->base, pipe_in);
	enabled_out = bam_pipe_is_enabled(&dev->bam->base, pipe_out);

	if (!enabled_in && !enabled_out)
		check = DMA_PIPES_BOTH_DISABLED;
	else if (enabled_in && enabled_out)
		check = DMA_PIPES_BOTH_ENABLED;
	else
		check = DMA_PIPES_STATE_DIFF;

	return check;
}

/**
 * Allocate a BAM DMA pipe
 *
 */
int sps_dma_pipe_alloc(void *bam_arg, u32 pipe_index, enum sps_mode dir)
{
	struct sps_bam *bam = bam_arg;
	struct bamdma_device *dev;
	struct bamdma_chan *chan;
	u32 channel;
	int result = SPS_ERROR;

	if (bam == NULL) {
		SPS_ERR(sps, "%s", "sps:BAM context is NULL");
		return SPS_ERROR;
	}

	/* Check pipe direction */
	if ((DMA_PIPE_IS_DEST(pipe_index) && dir != SPS_MODE_DEST) ||
	    (DMA_PIPE_IS_SRC(pipe_index) && dir != SPS_MODE_SRC)) {
		SPS_ERR(sps, "sps:BAM-DMA: wrong direction for BAM %pa pipe %d",
			&bam->props.phys_addr, pipe_index);
		return SPS_ERROR;
	}

	mutex_lock(&bam_dma_lock);

	dev = sps_dma_find_device((unsigned long) bam);
	if (dev == NULL) {
		SPS_ERR(sps, "sps:BAM-DMA: invalid BAM: %pa",
			&bam->props.phys_addr);
		goto exit_err;
	}
	if (pipe_index >= dev->num_pipes) {
		SPS_ERR(sps, "sps:BAM-DMA: BAM %pa invalid pipe: %d",
			&bam->props.phys_addr, pipe_index);
		goto exit_err;
	}
	if (dev->pipes[pipe_index] != PIPE_INACTIVE) {
		SPS_ERR(sps, "sps:BAM-DMA: BAM %pa pipe %d already active",
			&bam->props.phys_addr, pipe_index);
		goto exit_err;
	}

	/* Mark pipe active */
	dev->pipes[pipe_index] = PIPE_ACTIVE;

	/* If channel is not allocated, make an internal allocation */
	channel = pipe_index / 2;
	chan = &dev->chans[channel];
	if (chan->state != DMA_CHAN_STATE_ALLOC_EXT &&
	    chan->state != DMA_CHAN_STATE_ALLOC_INT) {
		chan->state = DMA_CHAN_STATE_ALLOC_INT;
	}

	result = 0;
exit_err:
	mutex_unlock(&bam_dma_lock);

	return result;
}

/**
 * Enable a BAM DMA pipe
 *
 */
int sps_dma_pipe_enable(void *bam_arg, u32 pipe_index)
{
	struct sps_bam *bam = bam_arg;
	struct bamdma_device *dev;
	struct bamdma_chan *chan;
	u32 channel;
	int result = SPS_ERROR;

	SPS_DBG3(sps, "sps:sps_dma_pipe_enable.pipe %d", pipe_index);

	mutex_lock(&bam_dma_lock);

	dev = sps_dma_find_device((unsigned long) bam);
	if (dev == NULL) {
		SPS_ERR(sps, "sps:%s:BAM-DMA: invalid BAM", __func__);
		goto exit_err;
	}
	if (pipe_index >= dev->num_pipes) {
		SPS_ERR(sps, "sps:BAM-DMA: BAM %pa invalid pipe: %d",
			&bam->props.phys_addr, pipe_index);
		goto exit_err;
	}
	if (dev->pipes[pipe_index] != PIPE_ACTIVE) {
		SPS_ERR(sps, "sps:BAM-DMA: BAM %pa pipe %d not active",
			&bam->props.phys_addr, pipe_index);
		goto exit_err;
	}

      /*
       * The channel must be enabled when the dest/input/write pipe
       * is enabled
       */
	if (DMA_PIPE_IS_DEST(pipe_index)) {
		/* Configure and enable the channel */
		channel = pipe_index / 2;
		chan = &dev->chans[channel];

		if (chan->threshold != SPS_DMA_THRESHOLD_DEFAULT)
			dma_write_reg_field(dev->virt_addr,
					    DMA_CHNL_CONFIG(channel),
					    DMA_CHNL_ACT_THRESH,
					    chan->thresh);

		if (chan->priority != SPS_DMA_PRI_DEFAULT)
			dma_write_reg_field(dev->virt_addr,
					    DMA_CHNL_CONFIG(channel),
					    DMA_CHNL_WEIGHT,
					    chan->weight);

		dma_write_reg_field(dev->virt_addr,
				    DMA_CHNL_CONFIG(channel),
				    DMA_CHNL_ENABLE, 1);
	}

	result = 0;
exit_err:
	mutex_unlock(&bam_dma_lock);

	return result;
}

/**
 * Deactivate a BAM DMA pipe
 *
 * This function deactivates a BAM DMA pipe.
 *
 * @dev - pointer to BAM-DMA device descriptor
 *
 * @bam - pointer to BAM device descriptor
 *
 * @pipe_index - pipe index
 *
 * @return 0 on success, negative value on error
 *
 */
static int sps_dma_deactivate_pipe_atomic(struct bamdma_device *dev,
					  struct sps_bam *bam,
					  u32 pipe_index)
{
	u32 channel;

	if (dev->bam != bam)
		return SPS_ERROR;
	if (pipe_index >= dev->num_pipes)
		return SPS_ERROR;
	if (dev->pipes[pipe_index] != PIPE_ACTIVE)
		return SPS_ERROR;	/* Pipe is not active */

	SPS_DBG3(sps, "sps:BAM-DMA: deactivate pipe %d", pipe_index);

	/* Mark pipe inactive */
	dev->pipes[pipe_index] = PIPE_INACTIVE;

	/*
	 * Channel must be reset when either pipe is disabled, so just always
	 * reset regardless of other pipe's state
	 */
	channel = pipe_index / 2;
	dma_write_reg_field(dev->virt_addr, DMA_CHNL_CONFIG(channel),
			    DMA_CHNL_ENABLE, 0);

	/* If the peer pipe is also inactive, reset the channel */
	if (sps_dma_check_pipes(dev, pipe_index) == DMA_PIPES_BOTH_DISABLED) {
		/* Free channel if allocated internally */
		if (dev->chans[channel].state == DMA_CHAN_STATE_ALLOC_INT)
			dev->chans[channel].state = DMA_CHAN_STATE_FREE;
	}

	return 0;
}

/**
 * Free a BAM DMA pipe
 *
 */
int sps_dma_pipe_free(void *bam_arg, u32 pipe_index)
{
	struct bamdma_device *dev;
	struct sps_bam *bam = bam_arg;
	int result;

	mutex_lock(&bam_dma_lock);

	dev = sps_dma_find_device((unsigned long) bam);
	if (dev == NULL) {
		SPS_ERR(sps, "sps:%s:BAM-DMA: invalid BAM", __func__);
		result = SPS_ERROR;
		goto exit_err;
	}

	result = sps_dma_deactivate_pipe_atomic(dev, bam, pipe_index);

exit_err:
	mutex_unlock(&bam_dma_lock);

	return result;
}

/**
 * Get the BAM handle for BAM-DMA.
 *
 * The BAM handle should be use as source/destination in the sps_connect().
 *
 * @return bam handle on success, zero on error
 */
unsigned long sps_dma_get_bam_handle(void)
{
	return (unsigned long)bam_dma_dev[0].bam;
}
EXPORT_SYMBOL(sps_dma_get_bam_handle);

/**
 * Free the BAM handle for BAM-DMA.
 *
 */
void sps_dma_free_bam_handle(unsigned long h)
{
}
EXPORT_SYMBOL(sps_dma_free_bam_handle);

#endif /* CONFIG_SPS_SUPPORT_BAMDMA */