forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xgene-dma.c
1848 lines (1534 loc) · 49.5 KB
/
xgene-dma.c
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Applied Micro X-Gene SoC DMA engine Driver
*
* Copyright (c) 2015, Applied Micro Circuits Corporation
* Authors: Rameshwar Prasad Sahu <[email protected]>
* Loc Ho <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* NOTE: PM support is currently not available.
*/
#include <linux/acpi.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/dmapool.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include "dmaengine.h"
/* X-Gene DMA ring csr registers and bit definations */
#define XGENE_DMA_RING_CONFIG 0x04
#define XGENE_DMA_RING_ENABLE BIT(31)
#define XGENE_DMA_RING_ID 0x08
#define XGENE_DMA_RING_ID_SETUP(v) ((v) | BIT(31))
#define XGENE_DMA_RING_ID_BUF 0x0C
#define XGENE_DMA_RING_ID_BUF_SETUP(v) (((v) << 9) | BIT(21))
#define XGENE_DMA_RING_THRESLD0_SET1 0x30
#define XGENE_DMA_RING_THRESLD0_SET1_VAL 0X64
#define XGENE_DMA_RING_THRESLD1_SET1 0x34
#define XGENE_DMA_RING_THRESLD1_SET1_VAL 0xC8
#define XGENE_DMA_RING_HYSTERESIS 0x68
#define XGENE_DMA_RING_HYSTERESIS_VAL 0xFFFFFFFF
#define XGENE_DMA_RING_STATE 0x6C
#define XGENE_DMA_RING_STATE_WR_BASE 0x70
#define XGENE_DMA_RING_NE_INT_MODE 0x017C
#define XGENE_DMA_RING_NE_INT_MODE_SET(m, v) \
((m) = ((m) & ~BIT(31 - (v))) | BIT(31 - (v)))
#define XGENE_DMA_RING_NE_INT_MODE_RESET(m, v) \
((m) &= (~BIT(31 - (v))))
#define XGENE_DMA_RING_CLKEN 0xC208
#define XGENE_DMA_RING_SRST 0xC200
#define XGENE_DMA_RING_MEM_RAM_SHUTDOWN 0xD070
#define XGENE_DMA_RING_BLK_MEM_RDY 0xD074
#define XGENE_DMA_RING_BLK_MEM_RDY_VAL 0xFFFFFFFF
#define XGENE_DMA_RING_ID_GET(owner, num) (((owner) << 6) | (num))
#define XGENE_DMA_RING_DST_ID(v) ((1 << 10) | (v))
#define XGENE_DMA_RING_CMD_OFFSET 0x2C
#define XGENE_DMA_RING_CMD_BASE_OFFSET(v) ((v) << 6)
#define XGENE_DMA_RING_COHERENT_SET(m) \
(((u32 *)(m))[2] |= BIT(4))
#define XGENE_DMA_RING_ADDRL_SET(m, v) \
(((u32 *)(m))[2] |= (((v) >> 8) << 5))
#define XGENE_DMA_RING_ADDRH_SET(m, v) \
(((u32 *)(m))[3] |= ((v) >> 35))
#define XGENE_DMA_RING_ACCEPTLERR_SET(m) \
(((u32 *)(m))[3] |= BIT(19))
#define XGENE_DMA_RING_SIZE_SET(m, v) \
(((u32 *)(m))[3] |= ((v) << 23))
#define XGENE_DMA_RING_RECOMBBUF_SET(m) \
(((u32 *)(m))[3] |= BIT(27))
#define XGENE_DMA_RING_RECOMTIMEOUTL_SET(m) \
(((u32 *)(m))[3] |= (0x7 << 28))
#define XGENE_DMA_RING_RECOMTIMEOUTH_SET(m) \
(((u32 *)(m))[4] |= 0x3)
#define XGENE_DMA_RING_SELTHRSH_SET(m) \
(((u32 *)(m))[4] |= BIT(3))
#define XGENE_DMA_RING_TYPE_SET(m, v) \
(((u32 *)(m))[4] |= ((v) << 19))
/* X-Gene DMA device csr registers and bit definitions */
#define XGENE_DMA_IPBRR 0x0
#define XGENE_DMA_DEV_ID_RD(v) ((v) & 0x00000FFF)
#define XGENE_DMA_BUS_ID_RD(v) (((v) >> 12) & 3)
#define XGENE_DMA_REV_NO_RD(v) (((v) >> 14) & 3)
#define XGENE_DMA_GCR 0x10
#define XGENE_DMA_CH_SETUP(v) \
((v) = ((v) & ~0x000FFFFF) | 0x000AAFFF)
#define XGENE_DMA_ENABLE(v) ((v) |= BIT(31))
#define XGENE_DMA_DISABLE(v) ((v) &= ~BIT(31))
#define XGENE_DMA_RAID6_CONT 0x14
#define XGENE_DMA_RAID6_MULTI_CTRL(v) ((v) << 24)
#define XGENE_DMA_INT 0x70
#define XGENE_DMA_INT_MASK 0x74
#define XGENE_DMA_INT_ALL_MASK 0xFFFFFFFF
#define XGENE_DMA_INT_ALL_UNMASK 0x0
#define XGENE_DMA_INT_MASK_SHIFT 0x14
#define XGENE_DMA_RING_INT0_MASK 0x90A0
#define XGENE_DMA_RING_INT1_MASK 0x90A8
#define XGENE_DMA_RING_INT2_MASK 0x90B0
#define XGENE_DMA_RING_INT3_MASK 0x90B8
#define XGENE_DMA_RING_INT4_MASK 0x90C0
#define XGENE_DMA_CFG_RING_WQ_ASSOC 0x90E0
#define XGENE_DMA_ASSOC_RING_MNGR1 0xFFFFFFFF
#define XGENE_DMA_MEM_RAM_SHUTDOWN 0xD070
#define XGENE_DMA_BLK_MEM_RDY 0xD074
#define XGENE_DMA_BLK_MEM_RDY_VAL 0xFFFFFFFF
#define XGENE_DMA_RING_CMD_SM_OFFSET 0x8000
/* X-Gene SoC EFUSE csr register and bit defination */
#define XGENE_SOC_JTAG1_SHADOW 0x18
#define XGENE_DMA_PQ_DISABLE_MASK BIT(13)
/* X-Gene DMA Descriptor format */
#define XGENE_DMA_DESC_NV_BIT BIT_ULL(50)
#define XGENE_DMA_DESC_IN_BIT BIT_ULL(55)
#define XGENE_DMA_DESC_C_BIT BIT_ULL(63)
#define XGENE_DMA_DESC_DR_BIT BIT_ULL(61)
#define XGENE_DMA_DESC_ELERR_POS 46
#define XGENE_DMA_DESC_RTYPE_POS 56
#define XGENE_DMA_DESC_LERR_POS 60
#define XGENE_DMA_DESC_BUFLEN_POS 48
#define XGENE_DMA_DESC_HOENQ_NUM_POS 48
#define XGENE_DMA_DESC_ELERR_RD(m) \
(((m) >> XGENE_DMA_DESC_ELERR_POS) & 0x3)
#define XGENE_DMA_DESC_LERR_RD(m) \
(((m) >> XGENE_DMA_DESC_LERR_POS) & 0x7)
#define XGENE_DMA_DESC_STATUS(elerr, lerr) \
(((elerr) << 4) | (lerr))
/* X-Gene DMA descriptor empty s/w signature */
#define XGENE_DMA_DESC_EMPTY_SIGNATURE ~0ULL
/* X-Gene DMA configurable parameters defines */
#define XGENE_DMA_RING_NUM 512
#define XGENE_DMA_BUFNUM 0x0
#define XGENE_DMA_CPU_BUFNUM 0x18
#define XGENE_DMA_RING_OWNER_DMA 0x03
#define XGENE_DMA_RING_OWNER_CPU 0x0F
#define XGENE_DMA_RING_TYPE_REGULAR 0x01
#define XGENE_DMA_RING_WQ_DESC_SIZE 32 /* 32 Bytes */
#define XGENE_DMA_RING_NUM_CONFIG 5
#define XGENE_DMA_MAX_CHANNEL 4
#define XGENE_DMA_XOR_CHANNEL 0
#define XGENE_DMA_PQ_CHANNEL 1
#define XGENE_DMA_MAX_BYTE_CNT 0x4000 /* 16 KB */
#define XGENE_DMA_MAX_64B_DESC_BYTE_CNT 0x14000 /* 80 KB */
#define XGENE_DMA_MAX_XOR_SRC 5
#define XGENE_DMA_16K_BUFFER_LEN_CODE 0x0
#define XGENE_DMA_INVALID_LEN_CODE 0x7800000000000000ULL
/* X-Gene DMA descriptor error codes */
#define ERR_DESC_AXI 0x01
#define ERR_BAD_DESC 0x02
#define ERR_READ_DATA_AXI 0x03
#define ERR_WRITE_DATA_AXI 0x04
#define ERR_FBP_TIMEOUT 0x05
#define ERR_ECC 0x06
#define ERR_DIFF_SIZE 0x08
#define ERR_SCT_GAT_LEN 0x09
#define ERR_CRC_ERR 0x11
#define ERR_CHKSUM 0x12
#define ERR_DIF 0x13
/* X-Gene DMA error interrupt codes */
#define ERR_DIF_SIZE_INT 0x0
#define ERR_GS_ERR_INT 0x1
#define ERR_FPB_TIMEO_INT 0x2
#define ERR_WFIFO_OVF_INT 0x3
#define ERR_RFIFO_OVF_INT 0x4
#define ERR_WR_TIMEO_INT 0x5
#define ERR_RD_TIMEO_INT 0x6
#define ERR_WR_ERR_INT 0x7
#define ERR_RD_ERR_INT 0x8
#define ERR_BAD_DESC_INT 0x9
#define ERR_DESC_DST_INT 0xA
#define ERR_DESC_SRC_INT 0xB
/* X-Gene DMA flyby operation code */
#define FLYBY_2SRC_XOR 0x80
#define FLYBY_3SRC_XOR 0x90
#define FLYBY_4SRC_XOR 0xA0
#define FLYBY_5SRC_XOR 0xB0
/* X-Gene DMA SW descriptor flags */
#define XGENE_DMA_FLAG_64B_DESC BIT(0)
/* Define to dump X-Gene DMA descriptor */
#define XGENE_DMA_DESC_DUMP(desc, m) \
print_hex_dump(KERN_ERR, (m), \
DUMP_PREFIX_ADDRESS, 16, 8, (desc), 32, 0)
#define to_dma_desc_sw(tx) \
container_of(tx, struct xgene_dma_desc_sw, tx)
#define to_dma_chan(dchan) \
container_of(dchan, struct xgene_dma_chan, dma_chan)
#define chan_dbg(chan, fmt, arg...) \
dev_dbg(chan->dev, "%s: " fmt, chan->name, ##arg)
#define chan_err(chan, fmt, arg...) \
dev_err(chan->dev, "%s: " fmt, chan->name, ##arg)
struct xgene_dma_desc_hw {
__le64 m0;
__le64 m1;
__le64 m2;
__le64 m3;
};
enum xgene_dma_ring_cfgsize {
XGENE_DMA_RING_CFG_SIZE_512B,
XGENE_DMA_RING_CFG_SIZE_2KB,
XGENE_DMA_RING_CFG_SIZE_16KB,
XGENE_DMA_RING_CFG_SIZE_64KB,
XGENE_DMA_RING_CFG_SIZE_512KB,
XGENE_DMA_RING_CFG_SIZE_INVALID
};
struct xgene_dma_ring {
struct xgene_dma *pdma;
u8 buf_num;
u16 id;
u16 num;
u16 head;
u16 owner;
u16 slots;
u16 dst_ring_num;
u32 size;
void __iomem *cmd;
void __iomem *cmd_base;
dma_addr_t desc_paddr;
u32 state[XGENE_DMA_RING_NUM_CONFIG];
enum xgene_dma_ring_cfgsize cfgsize;
union {
void *desc_vaddr;
struct xgene_dma_desc_hw *desc_hw;
};
};
struct xgene_dma_desc_sw {
struct xgene_dma_desc_hw desc1;
struct xgene_dma_desc_hw desc2;
u32 flags;
struct list_head node;
struct list_head tx_list;
struct dma_async_tx_descriptor tx;
};
/**
* struct xgene_dma_chan - internal representation of an X-Gene DMA channel
* @dma_chan: dmaengine channel object member
* @pdma: X-Gene DMA device structure reference
* @dev: struct device reference for dma mapping api
* @id: raw id of this channel
* @rx_irq: channel IRQ
* @name: name of X-Gene DMA channel
* @lock: serializes enqueue/dequeue operations to the descriptor pool
* @pending: number of transaction request pushed to DMA controller for
* execution, but still waiting for completion,
* @max_outstanding: max number of outstanding request we can push to channel
* @ld_pending: descriptors which are queued to run, but have not yet been
* submitted to the hardware for execution
* @ld_running: descriptors which are currently being executing by the hardware
* @ld_completed: descriptors which have finished execution by the hardware.
* These descriptors have already had their cleanup actions run. They
* are waiting for the ACK bit to be set by the async tx API.
* @desc_pool: descriptor pool for DMA operations
* @tasklet: bottom half where all completed descriptors cleans
* @tx_ring: transmit ring descriptor that we use to prepare actual
* descriptors for further executions
* @rx_ring: receive ring descriptor that we use to get completed DMA
* descriptors during cleanup time
*/
struct xgene_dma_chan {
struct dma_chan dma_chan;
struct xgene_dma *pdma;
struct device *dev;
int id;
int rx_irq;
char name[10];
spinlock_t lock;
int pending;
int max_outstanding;
struct list_head ld_pending;
struct list_head ld_running;
struct list_head ld_completed;
struct dma_pool *desc_pool;
struct tasklet_struct tasklet;
struct xgene_dma_ring tx_ring;
struct xgene_dma_ring rx_ring;
};
/**
* struct xgene_dma - internal representation of an X-Gene DMA device
* @err_irq: DMA error irq number
* @ring_num: start id number for DMA ring
* @csr_dma: base for DMA register access
* @csr_ring: base for DMA ring register access
* @csr_ring_cmd: base for DMA ring command register access
* @csr_efuse: base for efuse register access
* @dma_dev: embedded struct dma_device
* @chan: reference to X-Gene DMA channels
*/
struct xgene_dma {
struct device *dev;
struct clk *clk;
int err_irq;
int ring_num;
void __iomem *csr_dma;
void __iomem *csr_ring;
void __iomem *csr_ring_cmd;
void __iomem *csr_efuse;
struct dma_device dma_dev[XGENE_DMA_MAX_CHANNEL];
struct xgene_dma_chan chan[XGENE_DMA_MAX_CHANNEL];
};
static const char * const xgene_dma_desc_err[] = {
[ERR_DESC_AXI] = "AXI error when reading src/dst link list",
[ERR_BAD_DESC] = "ERR or El_ERR fields not set to zero in desc",
[ERR_READ_DATA_AXI] = "AXI error when reading data",
[ERR_WRITE_DATA_AXI] = "AXI error when writing data",
[ERR_FBP_TIMEOUT] = "Timeout on bufpool fetch",
[ERR_ECC] = "ECC double bit error",
[ERR_DIFF_SIZE] = "Bufpool too small to hold all the DIF result",
[ERR_SCT_GAT_LEN] = "Gather and scatter data length not same",
[ERR_CRC_ERR] = "CRC error",
[ERR_CHKSUM] = "Checksum error",
[ERR_DIF] = "DIF error",
};
static const char * const xgene_dma_err[] = {
[ERR_DIF_SIZE_INT] = "DIF size error",
[ERR_GS_ERR_INT] = "Gather scatter not same size error",
[ERR_FPB_TIMEO_INT] = "Free pool time out error",
[ERR_WFIFO_OVF_INT] = "Write FIFO over flow error",
[ERR_RFIFO_OVF_INT] = "Read FIFO over flow error",
[ERR_WR_TIMEO_INT] = "Write time out error",
[ERR_RD_TIMEO_INT] = "Read time out error",
[ERR_WR_ERR_INT] = "HBF bus write error",
[ERR_RD_ERR_INT] = "HBF bus read error",
[ERR_BAD_DESC_INT] = "Ring descriptor HE0 not set error",
[ERR_DESC_DST_INT] = "HFB reading dst link address error",
[ERR_DESC_SRC_INT] = "HFB reading src link address error",
};
static bool is_pq_enabled(struct xgene_dma *pdma)
{
u32 val;
val = ioread32(pdma->csr_efuse + XGENE_SOC_JTAG1_SHADOW);
return !(val & XGENE_DMA_PQ_DISABLE_MASK);
}
static u64 xgene_dma_encode_len(size_t len)
{
return (len < XGENE_DMA_MAX_BYTE_CNT) ?
((u64)len << XGENE_DMA_DESC_BUFLEN_POS) :
XGENE_DMA_16K_BUFFER_LEN_CODE;
}
static u8 xgene_dma_encode_xor_flyby(u32 src_cnt)
{
static u8 flyby_type[] = {
FLYBY_2SRC_XOR, /* Dummy */
FLYBY_2SRC_XOR, /* Dummy */
FLYBY_2SRC_XOR,
FLYBY_3SRC_XOR,
FLYBY_4SRC_XOR,
FLYBY_5SRC_XOR
};
return flyby_type[src_cnt];
}
static void xgene_dma_set_src_buffer(__le64 *ext8, size_t *len,
dma_addr_t *paddr)
{
size_t nbytes = (*len < XGENE_DMA_MAX_BYTE_CNT) ?
*len : XGENE_DMA_MAX_BYTE_CNT;
*ext8 |= cpu_to_le64(*paddr);
*ext8 |= cpu_to_le64(xgene_dma_encode_len(nbytes));
*len -= nbytes;
*paddr += nbytes;
}
static __le64 *xgene_dma_lookup_ext8(struct xgene_dma_desc_hw *desc, int idx)
{
switch (idx) {
case 0:
return &desc->m1;
case 1:
return &desc->m0;
case 2:
return &desc->m3;
case 3:
return &desc->m2;
default:
pr_err("Invalid dma descriptor index\n");
}
return NULL;
}
static void xgene_dma_init_desc(struct xgene_dma_desc_hw *desc,
u16 dst_ring_num)
{
desc->m0 |= cpu_to_le64(XGENE_DMA_DESC_IN_BIT);
desc->m0 |= cpu_to_le64((u64)XGENE_DMA_RING_OWNER_DMA <<
XGENE_DMA_DESC_RTYPE_POS);
desc->m1 |= cpu_to_le64(XGENE_DMA_DESC_C_BIT);
desc->m3 |= cpu_to_le64((u64)dst_ring_num <<
XGENE_DMA_DESC_HOENQ_NUM_POS);
}
static void xgene_dma_prep_xor_desc(struct xgene_dma_chan *chan,
struct xgene_dma_desc_sw *desc_sw,
dma_addr_t *dst, dma_addr_t *src,
u32 src_cnt, size_t *nbytes,
const u8 *scf)
{
struct xgene_dma_desc_hw *desc1, *desc2;
size_t len = *nbytes;
int i;
desc1 = &desc_sw->desc1;
desc2 = &desc_sw->desc2;
/* Initialize DMA descriptor */
xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
/* Set destination address */
desc1->m2 |= cpu_to_le64(XGENE_DMA_DESC_DR_BIT);
desc1->m3 |= cpu_to_le64(*dst);
/* We have multiple source addresses, so need to set NV bit*/
desc1->m0 |= cpu_to_le64(XGENE_DMA_DESC_NV_BIT);
/* Set flyby opcode */
desc1->m2 |= cpu_to_le64(xgene_dma_encode_xor_flyby(src_cnt));
/* Set 1st to 5th source addresses */
for (i = 0; i < src_cnt; i++) {
len = *nbytes;
xgene_dma_set_src_buffer((i == 0) ? &desc1->m1 :
xgene_dma_lookup_ext8(desc2, i - 1),
&len, &src[i]);
desc1->m2 |= cpu_to_le64((scf[i] << ((i + 1) * 8)));
}
/* Update meta data */
*nbytes = len;
*dst += XGENE_DMA_MAX_BYTE_CNT;
/* We need always 64B descriptor to perform xor or pq operations */
desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
}
static dma_cookie_t xgene_dma_tx_submit(struct dma_async_tx_descriptor *tx)
{
struct xgene_dma_desc_sw *desc;
struct xgene_dma_chan *chan;
dma_cookie_t cookie;
if (unlikely(!tx))
return -EINVAL;
chan = to_dma_chan(tx->chan);
desc = to_dma_desc_sw(tx);
spin_lock_bh(&chan->lock);
cookie = dma_cookie_assign(tx);
/* Add this transaction list onto the tail of the pending queue */
list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
spin_unlock_bh(&chan->lock);
return cookie;
}
static void xgene_dma_clean_descriptor(struct xgene_dma_chan *chan,
struct xgene_dma_desc_sw *desc)
{
list_del(&desc->node);
chan_dbg(chan, "LD %p free\n", desc);
dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
}
static struct xgene_dma_desc_sw *xgene_dma_alloc_descriptor(
struct xgene_dma_chan *chan)
{
struct xgene_dma_desc_sw *desc;
dma_addr_t phys;
desc = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
if (!desc) {
chan_err(chan, "Failed to allocate LDs\n");
return NULL;
}
INIT_LIST_HEAD(&desc->tx_list);
desc->tx.phys = phys;
desc->tx.tx_submit = xgene_dma_tx_submit;
dma_async_tx_descriptor_init(&desc->tx, &chan->dma_chan);
chan_dbg(chan, "LD %p allocated\n", desc);
return desc;
}
/**
* xgene_dma_clean_completed_descriptor - free all descriptors which
* has been completed and acked
* @chan: X-Gene DMA channel
*
* This function is used on all completed and acked descriptors.
*/
static void xgene_dma_clean_completed_descriptor(struct xgene_dma_chan *chan)
{
struct xgene_dma_desc_sw *desc, *_desc;
/* Run the callback for each descriptor, in order */
list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node) {
if (async_tx_test_ack(&desc->tx))
xgene_dma_clean_descriptor(chan, desc);
}
}
/**
* xgene_dma_run_tx_complete_actions - cleanup a single link descriptor
* @chan: X-Gene DMA channel
* @desc: descriptor to cleanup and free
*
* This function is used on a descriptor which has been executed by the DMA
* controller. It will run any callbacks, submit any dependencies.
*/
static void xgene_dma_run_tx_complete_actions(struct xgene_dma_chan *chan,
struct xgene_dma_desc_sw *desc)
{
struct dma_async_tx_descriptor *tx = &desc->tx;
/*
* If this is not the last transaction in the group,
* then no need to complete cookie and run any callback as
* this is not the tx_descriptor which had been sent to caller
* of this DMA request
*/
if (tx->cookie == 0)
return;
dma_cookie_complete(tx);
dma_descriptor_unmap(tx);
/* Run the link descriptor callback function */
dmaengine_desc_get_callback_invoke(tx, NULL);
/* Run any dependencies */
dma_run_dependencies(tx);
}
/**
* xgene_dma_clean_running_descriptor - move the completed descriptor from
* ld_running to ld_completed
* @chan: X-Gene DMA channel
* @desc: the descriptor which is completed
*
* Free the descriptor directly if acked by async_tx api,
* else move it to queue ld_completed.
*/
static void xgene_dma_clean_running_descriptor(struct xgene_dma_chan *chan,
struct xgene_dma_desc_sw *desc)
{
/* Remove from the list of running transactions */
list_del(&desc->node);
/*
* the client is allowed to attach dependent operations
* until 'ack' is set
*/
if (!async_tx_test_ack(&desc->tx)) {
/*
* Move this descriptor to the list of descriptors which is
* completed, but still awaiting the 'ack' bit to be set.
*/
list_add_tail(&desc->node, &chan->ld_completed);
return;
}
chan_dbg(chan, "LD %p free\n", desc);
dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
}
static void xgene_chan_xfer_request(struct xgene_dma_chan *chan,
struct xgene_dma_desc_sw *desc_sw)
{
struct xgene_dma_ring *ring = &chan->tx_ring;
struct xgene_dma_desc_hw *desc_hw;
/* Get hw descriptor from DMA tx ring */
desc_hw = &ring->desc_hw[ring->head];
/*
* Increment the head count to point next
* descriptor for next time
*/
if (++ring->head == ring->slots)
ring->head = 0;
/* Copy prepared sw descriptor data to hw descriptor */
memcpy(desc_hw, &desc_sw->desc1, sizeof(*desc_hw));
/*
* Check if we have prepared 64B descriptor,
* in this case we need one more hw descriptor
*/
if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) {
desc_hw = &ring->desc_hw[ring->head];
if (++ring->head == ring->slots)
ring->head = 0;
memcpy(desc_hw, &desc_sw->desc2, sizeof(*desc_hw));
}
/* Increment the pending transaction count */
chan->pending += ((desc_sw->flags &
XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
/* Notify the hw that we have descriptor ready for execution */
iowrite32((desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) ?
2 : 1, ring->cmd);
}
/**
* xgene_chan_xfer_ld_pending - push any pending transactions to hw
* @chan : X-Gene DMA channel
*
* LOCKING: must hold chan->lock
*/
static void xgene_chan_xfer_ld_pending(struct xgene_dma_chan *chan)
{
struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
/*
* If the list of pending descriptors is empty, then we
* don't need to do any work at all
*/
if (list_empty(&chan->ld_pending)) {
chan_dbg(chan, "No pending LDs\n");
return;
}
/*
* Move elements from the queue of pending transactions onto the list
* of running transactions and push it to hw for further executions
*/
list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_pending, node) {
/*
* Check if have pushed max number of transactions to hw
* as capable, so let's stop here and will push remaining
* elements from pening ld queue after completing some
* descriptors that we have already pushed
*/
if (chan->pending >= chan->max_outstanding)
return;
xgene_chan_xfer_request(chan, desc_sw);
/*
* Delete this element from ld pending queue and append it to
* ld running queue
*/
list_move_tail(&desc_sw->node, &chan->ld_running);
}
}
/**
* xgene_dma_cleanup_descriptors - cleanup link descriptors which are completed
* and move them to ld_completed to free until flag 'ack' is set
* @chan: X-Gene DMA channel
*
* This function is used on descriptors which have been executed by the DMA
* controller. It will run any callbacks, submit any dependencies, then
* free these descriptors if flag 'ack' is set.
*/
static void xgene_dma_cleanup_descriptors(struct xgene_dma_chan *chan)
{
struct xgene_dma_ring *ring = &chan->rx_ring;
struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
struct xgene_dma_desc_hw *desc_hw;
struct list_head ld_completed;
u8 status;
INIT_LIST_HEAD(&ld_completed);
spin_lock_bh(&chan->lock);
/* Clean already completed and acked descriptors */
xgene_dma_clean_completed_descriptor(chan);
/* Move all completed descriptors to ld completed queue, in order */
list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_running, node) {
/* Get subsequent hw descriptor from DMA rx ring */
desc_hw = &ring->desc_hw[ring->head];
/* Check if this descriptor has been completed */
if (unlikely(le64_to_cpu(desc_hw->m0) ==
XGENE_DMA_DESC_EMPTY_SIGNATURE))
break;
if (++ring->head == ring->slots)
ring->head = 0;
/* Check if we have any error with DMA transactions */
status = XGENE_DMA_DESC_STATUS(
XGENE_DMA_DESC_ELERR_RD(le64_to_cpu(
desc_hw->m0)),
XGENE_DMA_DESC_LERR_RD(le64_to_cpu(
desc_hw->m0)));
if (status) {
/* Print the DMA error type */
chan_err(chan, "%s\n", xgene_dma_desc_err[status]);
/*
* We have DMA transactions error here. Dump DMA Tx
* and Rx descriptors for this request */
XGENE_DMA_DESC_DUMP(&desc_sw->desc1,
"X-Gene DMA TX DESC1: ");
if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC)
XGENE_DMA_DESC_DUMP(&desc_sw->desc2,
"X-Gene DMA TX DESC2: ");
XGENE_DMA_DESC_DUMP(desc_hw,
"X-Gene DMA RX ERR DESC: ");
}
/* Notify the hw about this completed descriptor */
iowrite32(-1, ring->cmd);
/* Mark this hw descriptor as processed */
desc_hw->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
/*
* Decrement the pending transaction count
* as we have processed one
*/
chan->pending -= ((desc_sw->flags &
XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
/*
* Delete this node from ld running queue and append it to
* ld completed queue for further processing
*/
list_move_tail(&desc_sw->node, &ld_completed);
}
/*
* Start any pending transactions automatically
* In the ideal case, we keep the DMA controller busy while we go
* ahead and free the descriptors below.
*/
xgene_chan_xfer_ld_pending(chan);
spin_unlock_bh(&chan->lock);
/* Run the callback for each descriptor, in order */
list_for_each_entry_safe(desc_sw, _desc_sw, &ld_completed, node) {
xgene_dma_run_tx_complete_actions(chan, desc_sw);
xgene_dma_clean_running_descriptor(chan, desc_sw);
}
}
static int xgene_dma_alloc_chan_resources(struct dma_chan *dchan)
{
struct xgene_dma_chan *chan = to_dma_chan(dchan);
/* Has this channel already been allocated? */
if (chan->desc_pool)
return 1;
chan->desc_pool = dma_pool_create(chan->name, chan->dev,
sizeof(struct xgene_dma_desc_sw),
0, 0);
if (!chan->desc_pool) {
chan_err(chan, "Failed to allocate descriptor pool\n");
return -ENOMEM;
}
chan_dbg(chan, "Allocate descripto pool\n");
return 1;
}
/**
* xgene_dma_free_desc_list - Free all descriptors in a queue
* @chan: X-Gene DMA channel
* @list: the list to free
*
* LOCKING: must hold chan->lock
*/
static void xgene_dma_free_desc_list(struct xgene_dma_chan *chan,
struct list_head *list)
{
struct xgene_dma_desc_sw *desc, *_desc;
list_for_each_entry_safe(desc, _desc, list, node)
xgene_dma_clean_descriptor(chan, desc);
}
static void xgene_dma_free_chan_resources(struct dma_chan *dchan)
{
struct xgene_dma_chan *chan = to_dma_chan(dchan);
chan_dbg(chan, "Free all resources\n");
if (!chan->desc_pool)
return;
/* Process all running descriptor */
xgene_dma_cleanup_descriptors(chan);
spin_lock_bh(&chan->lock);
/* Clean all link descriptor queues */
xgene_dma_free_desc_list(chan, &chan->ld_pending);
xgene_dma_free_desc_list(chan, &chan->ld_running);
xgene_dma_free_desc_list(chan, &chan->ld_completed);
spin_unlock_bh(&chan->lock);
/* Delete this channel DMA pool */
dma_pool_destroy(chan->desc_pool);
chan->desc_pool = NULL;
}
static struct dma_async_tx_descriptor *xgene_dma_prep_xor(
struct dma_chan *dchan, dma_addr_t dst, dma_addr_t *src,
u32 src_cnt, size_t len, unsigned long flags)
{
struct xgene_dma_desc_sw *first = NULL, *new;
struct xgene_dma_chan *chan;
static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {
0x01, 0x01, 0x01, 0x01, 0x01};
if (unlikely(!dchan || !len))
return NULL;
chan = to_dma_chan(dchan);
do {
/* Allocate the link descriptor from DMA pool */
new = xgene_dma_alloc_descriptor(chan);
if (!new)
goto fail;
/* Prepare xor DMA descriptor */
xgene_dma_prep_xor_desc(chan, new, &dst, src,
src_cnt, &len, multi);
if (!first)
first = new;
new->tx.cookie = 0;
async_tx_ack(&new->tx);
/* Insert the link descriptor to the LD ring */
list_add_tail(&new->node, &first->tx_list);
} while (len);
new->tx.flags = flags; /* client is in control of this ack */
new->tx.cookie = -EBUSY;
list_splice(&first->tx_list, &new->tx_list);
return &new->tx;
fail:
if (!first)
return NULL;
xgene_dma_free_desc_list(chan, &first->tx_list);
return NULL;
}
static struct dma_async_tx_descriptor *xgene_dma_prep_pq(
struct dma_chan *dchan, dma_addr_t *dst, dma_addr_t *src,
u32 src_cnt, const u8 *scf, size_t len, unsigned long flags)
{
struct xgene_dma_desc_sw *first = NULL, *new;
struct xgene_dma_chan *chan;
size_t _len = len;
dma_addr_t _src[XGENE_DMA_MAX_XOR_SRC];
static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {0x01, 0x01, 0x01, 0x01, 0x01};
if (unlikely(!dchan || !len))
return NULL;
chan = to_dma_chan(dchan);
/*
* Save source addresses on local variable, may be we have to
* prepare two descriptor to generate P and Q if both enabled
* in the flags by client
*/
memcpy(_src, src, sizeof(*src) * src_cnt);
if (flags & DMA_PREP_PQ_DISABLE_P)
len = 0;
if (flags & DMA_PREP_PQ_DISABLE_Q)
_len = 0;
do {
/* Allocate the link descriptor from DMA pool */
new = xgene_dma_alloc_descriptor(chan);
if (!new)
goto fail;
if (!first)
first = new;
new->tx.cookie = 0;
async_tx_ack(&new->tx);
/* Insert the link descriptor to the LD ring */
list_add_tail(&new->node, &first->tx_list);
/*
* Prepare DMA descriptor to generate P,
* if DMA_PREP_PQ_DISABLE_P flag is not set
*/
if (len) {
xgene_dma_prep_xor_desc(chan, new, &dst[0], src,
src_cnt, &len, multi);
continue;
}
/*
* Prepare DMA descriptor to generate Q,
* if DMA_PREP_PQ_DISABLE_Q flag is not set
*/
if (_len) {
xgene_dma_prep_xor_desc(chan, new, &dst[1], _src,
src_cnt, &_len, scf);
}
} while (len || _len);
new->tx.flags = flags; /* client is in control of this ack */
new->tx.cookie = -EBUSY;
list_splice(&first->tx_list, &new->tx_list);
return &new->tx;
fail:
if (!first)
return NULL;
xgene_dma_free_desc_list(chan, &first->tx_list);
return NULL;
}
static void xgene_dma_issue_pending(struct dma_chan *dchan)
{
struct xgene_dma_chan *chan = to_dma_chan(dchan);
spin_lock_bh(&chan->lock);
xgene_chan_xfer_ld_pending(chan);
spin_unlock_bh(&chan->lock);
}
static enum dma_status xgene_dma_tx_status(struct dma_chan *dchan,
dma_cookie_t cookie,
struct dma_tx_state *txstate)
{
return dma_cookie_status(dchan, cookie, txstate);
}
static void xgene_dma_tasklet_cb(unsigned long data)
{
struct xgene_dma_chan *chan = (struct xgene_dma_chan *)data;
/* Run all cleanup for descriptors which have been completed */
xgene_dma_cleanup_descriptors(chan);
/* Re-enable DMA channel IRQ */
enable_irq(chan->rx_irq);
}
static irqreturn_t xgene_dma_chan_ring_isr(int irq, void *id)
{