16
16
#include <linux/init.h>
17
17
#include <linux/interrupt.h>
18
18
#include <linux/io.h>
19
+ #include <linux/log2.h>
19
20
#include <linux/mm.h>
20
21
#include <linux/module.h>
21
22
#include <linux/slab.h>
@@ -621,12 +622,10 @@ dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
621
622
struct dw_desc * prev ;
622
623
struct dw_desc * first ;
623
624
u32 ctllo , ctlhi ;
624
- u8 m_master = dwc -> dws .m_master ;
625
- u8 lms = DWC_LLP_LMS (m_master );
625
+ u8 lms = DWC_LLP_LMS (dwc -> dws .m_master );
626
626
dma_addr_t reg ;
627
627
unsigned int reg_width ;
628
628
unsigned int mem_width ;
629
- unsigned int data_width = dw -> pdata -> data_width [m_master ];
630
629
unsigned int i ;
631
630
struct scatterlist * sg ;
632
631
size_t total_len = 0 ;
@@ -660,7 +659,7 @@ dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
660
659
mem = sg_dma_address (sg );
661
660
len = sg_dma_len (sg );
662
661
663
- mem_width = __ffs (data_width | mem | len );
662
+ mem_width = __ffs (sconfig -> src_addr_width | mem | len );
664
663
665
664
slave_sg_todev_fill_desc :
666
665
desc = dwc_desc_get (dwc );
@@ -720,7 +719,7 @@ dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
720
719
lli_write (desc , sar , reg );
721
720
lli_write (desc , dar , mem );
722
721
lli_write (desc , ctlhi , ctlhi );
723
- mem_width = __ffs (data_width | mem );
722
+ mem_width = __ffs (sconfig -> dst_addr_width | mem );
724
723
lli_write (desc , ctllo , ctllo | DWC_CTLL_DST_WIDTH (mem_width ));
725
724
desc -> len = dlen ;
726
725
@@ -780,20 +779,108 @@ bool dw_dma_filter(struct dma_chan *chan, void *param)
780
779
}
781
780
EXPORT_SYMBOL_GPL (dw_dma_filter );
782
781
783
- static int dwc_config (struct dma_chan * chan , struct dma_slave_config * sconfig )
782
+ static int dwc_verify_maxburst (struct dma_chan * chan )
784
783
{
785
784
struct dw_dma_chan * dwc = to_dw_dma_chan (chan );
786
- struct dw_dma * dw = to_dw_dma (chan -> device );
787
785
788
- memcpy (& dwc -> dma_sconfig , sconfig , sizeof (* sconfig ));
786
+ dwc -> dma_sconfig .src_maxburst =
787
+ clamp (dwc -> dma_sconfig .src_maxburst , 1U , dwc -> max_burst );
788
+ dwc -> dma_sconfig .dst_maxburst =
789
+ clamp (dwc -> dma_sconfig .dst_maxburst , 1U , dwc -> max_burst );
789
790
790
791
dwc -> dma_sconfig .src_maxburst =
791
- clamp (dwc -> dma_sconfig .src_maxburst , 0U , dwc -> max_burst );
792
+ rounddown_pow_of_two (dwc -> dma_sconfig .src_maxburst );
792
793
dwc -> dma_sconfig .dst_maxburst =
793
- clamp (dwc -> dma_sconfig .dst_maxburst , 0U , dwc -> max_burst );
794
+ rounddown_pow_of_two (dwc -> dma_sconfig .dst_maxburst );
794
795
795
- dw -> encode_maxburst (dwc , & dwc -> dma_sconfig .src_maxburst );
796
- dw -> encode_maxburst (dwc , & dwc -> dma_sconfig .dst_maxburst );
796
+ return 0 ;
797
+ }
798
+
799
+ static int dwc_verify_p_buswidth (struct dma_chan * chan )
800
+ {
801
+ struct dw_dma_chan * dwc = to_dw_dma_chan (chan );
802
+ struct dw_dma * dw = to_dw_dma (chan -> device );
803
+ u32 reg_width , max_width ;
804
+
805
+ if (dwc -> dma_sconfig .direction == DMA_MEM_TO_DEV )
806
+ reg_width = dwc -> dma_sconfig .dst_addr_width ;
807
+ else if (dwc -> dma_sconfig .direction == DMA_DEV_TO_MEM )
808
+ reg_width = dwc -> dma_sconfig .src_addr_width ;
809
+ else /* DMA_MEM_TO_MEM */
810
+ return 0 ;
811
+
812
+ max_width = dw -> pdata -> data_width [dwc -> dws .p_master ];
813
+
814
+ /* Fall-back to 1-byte transfer width if undefined */
815
+ if (reg_width == DMA_SLAVE_BUSWIDTH_UNDEFINED )
816
+ reg_width = DMA_SLAVE_BUSWIDTH_1_BYTE ;
817
+ else if (!is_power_of_2 (reg_width ) || reg_width > max_width )
818
+ return - EINVAL ;
819
+ else /* bus width is valid */
820
+ return 0 ;
821
+
822
+ /* Update undefined addr width value */
823
+ if (dwc -> dma_sconfig .direction == DMA_MEM_TO_DEV )
824
+ dwc -> dma_sconfig .dst_addr_width = reg_width ;
825
+ else /* DMA_DEV_TO_MEM */
826
+ dwc -> dma_sconfig .src_addr_width = reg_width ;
827
+
828
+ return 0 ;
829
+ }
830
+
831
+ static int dwc_verify_m_buswidth (struct dma_chan * chan )
832
+ {
833
+ struct dw_dma_chan * dwc = to_dw_dma_chan (chan );
834
+ struct dw_dma * dw = to_dw_dma (chan -> device );
835
+ u32 reg_width , reg_burst , mem_width ;
836
+
837
+ mem_width = dw -> pdata -> data_width [dwc -> dws .m_master ];
838
+
839
+ /*
840
+ * It's possible to have a data portion locked in the DMA FIFO in case
841
+ * of the channel suspension. Subsequent channel disabling will cause
842
+ * that data silent loss. In order to prevent that maintain the src and
843
+ * dst transfer widths coherency by means of the relation:
844
+ * (CTLx.SRC_TR_WIDTH * CTLx.SRC_MSIZE >= CTLx.DST_TR_WIDTH)
845
+ * Look for the details in the commit message that brings this change.
846
+ *
847
+ * Note the DMA configs utilized in the calculations below must have
848
+ * been verified to have correct values by this method call.
849
+ */
850
+ if (dwc -> dma_sconfig .direction == DMA_MEM_TO_DEV ) {
851
+ reg_width = dwc -> dma_sconfig .dst_addr_width ;
852
+ if (mem_width < reg_width )
853
+ return - EINVAL ;
854
+
855
+ dwc -> dma_sconfig .src_addr_width = mem_width ;
856
+ } else if (dwc -> dma_sconfig .direction == DMA_DEV_TO_MEM ) {
857
+ reg_width = dwc -> dma_sconfig .src_addr_width ;
858
+ reg_burst = dwc -> dma_sconfig .src_maxburst ;
859
+
860
+ dwc -> dma_sconfig .dst_addr_width = min (mem_width , reg_width * reg_burst );
861
+ }
862
+
863
+ return 0 ;
864
+ }
865
+
866
+ static int dwc_config (struct dma_chan * chan , struct dma_slave_config * sconfig )
867
+ {
868
+ struct dw_dma_chan * dwc = to_dw_dma_chan (chan );
869
+ int ret ;
870
+
871
+ memcpy (& dwc -> dma_sconfig , sconfig , sizeof (* sconfig ));
872
+
873
+ ret = dwc_verify_maxburst (chan );
874
+ if (ret )
875
+ return ret ;
876
+
877
+ ret = dwc_verify_p_buswidth (chan );
878
+ if (ret )
879
+ return ret ;
880
+
881
+ ret = dwc_verify_m_buswidth (chan );
882
+ if (ret )
883
+ return ret ;
797
884
798
885
return 0 ;
799
886
}
@@ -1068,7 +1155,7 @@ int do_dma_probe(struct dw_dma_chip *chip)
1068
1155
bool autocfg = false;
1069
1156
unsigned int dw_params ;
1070
1157
unsigned int i ;
1071
- int err ;
1158
+ int ret ;
1072
1159
1073
1160
dw -> pdata = devm_kzalloc (chip -> dev , sizeof (* dw -> pdata ), GFP_KERNEL );
1074
1161
if (!dw -> pdata )
@@ -1084,7 +1171,7 @@ int do_dma_probe(struct dw_dma_chip *chip)
1084
1171
1085
1172
autocfg = dw_params >> DW_PARAMS_EN & 1 ;
1086
1173
if (!autocfg ) {
1087
- err = - EINVAL ;
1174
+ ret = - EINVAL ;
1088
1175
goto err_pdata ;
1089
1176
}
1090
1177
@@ -1104,7 +1191,7 @@ int do_dma_probe(struct dw_dma_chip *chip)
1104
1191
pdata -> chan_allocation_order = CHAN_ALLOCATION_ASCENDING ;
1105
1192
pdata -> chan_priority = CHAN_PRIORITY_ASCENDING ;
1106
1193
} else if (chip -> pdata -> nr_channels > DW_DMA_MAX_NR_CHANNELS ) {
1107
- err = - EINVAL ;
1194
+ ret = - EINVAL ;
1108
1195
goto err_pdata ;
1109
1196
} else {
1110
1197
memcpy (dw -> pdata , chip -> pdata , sizeof (* dw -> pdata ));
@@ -1116,7 +1203,7 @@ int do_dma_probe(struct dw_dma_chip *chip)
1116
1203
dw -> chan = devm_kcalloc (chip -> dev , pdata -> nr_channels , sizeof (* dw -> chan ),
1117
1204
GFP_KERNEL );
1118
1205
if (!dw -> chan ) {
1119
- err = - ENOMEM ;
1206
+ ret = - ENOMEM ;
1120
1207
goto err_pdata ;
1121
1208
}
1122
1209
@@ -1134,15 +1221,15 @@ int do_dma_probe(struct dw_dma_chip *chip)
1134
1221
sizeof (struct dw_desc ), 4 , 0 );
1135
1222
if (!dw -> desc_pool ) {
1136
1223
dev_err (chip -> dev , "No memory for descriptors dma pool\n" );
1137
- err = - ENOMEM ;
1224
+ ret = - ENOMEM ;
1138
1225
goto err_pdata ;
1139
1226
}
1140
1227
1141
1228
tasklet_setup (& dw -> tasklet , dw_dma_tasklet );
1142
1229
1143
- err = request_irq (chip -> irq , dw_dma_interrupt , IRQF_SHARED ,
1230
+ ret = request_irq (chip -> irq , dw_dma_interrupt , IRQF_SHARED ,
1144
1231
dw -> name , dw );
1145
- if (err )
1232
+ if (ret )
1146
1233
goto err_pdata ;
1147
1234
1148
1235
INIT_LIST_HEAD (& dw -> dma .channels );
@@ -1254,8 +1341,8 @@ int do_dma_probe(struct dw_dma_chip *chip)
1254
1341
*/
1255
1342
dma_set_max_seg_size (dw -> dma .dev , dw -> chan [0 ].block_size );
1256
1343
1257
- err = dma_async_device_register (& dw -> dma );
1258
- if (err )
1344
+ ret = dma_async_device_register (& dw -> dma );
1345
+ if (ret )
1259
1346
goto err_dma_register ;
1260
1347
1261
1348
dev_info (chip -> dev , "DesignWare DMA Controller, %d channels\n" ,
@@ -1269,7 +1356,7 @@ int do_dma_probe(struct dw_dma_chip *chip)
1269
1356
free_irq (chip -> irq , dw );
1270
1357
err_pdata :
1271
1358
pm_runtime_put_sync_suspend (chip -> dev );
1272
- return err ;
1359
+ return ret ;
1273
1360
}
1274
1361
1275
1362
int do_dma_remove (struct dw_dma_chip * chip )
0 commit comments