]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/dma/mv_xor_v2.c
dmaengine: mv_xor_v2: do not use descriptors not acked by async_tx
[karo-tx-linux.git] / drivers / dma / mv_xor_v2.c
1 /*
2  * Copyright (C) 2015-2016 Marvell International Ltd.
3
4  * This program is free software: you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation, either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/msi.h>
21 #include <linux/of.h>
22 #include <linux/of_irq.h>
23 #include <linux/platform_device.h>
24 #include <linux/spinlock.h>
25
26 #include "dmaengine.h"
27
28 /* DMA Engine Registers */
29 #define MV_XOR_V2_DMA_DESQ_BALR_OFF                     0x000
30 #define MV_XOR_V2_DMA_DESQ_BAHR_OFF                     0x004
31 #define MV_XOR_V2_DMA_DESQ_SIZE_OFF                     0x008
32 #define MV_XOR_V2_DMA_DESQ_DONE_OFF                     0x00C
33 #define   MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK          0x7FFF
34 #define   MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT         0
35 #define   MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_MASK         0x1FFF
36 #define   MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_SHIFT        16
37 #define MV_XOR_V2_DMA_DESQ_ARATTR_OFF                   0x010
38 #define   MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK            0x3F3F
39 #define   MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE       0x202
40 #define   MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE             0x3C3C
41 #define MV_XOR_V2_DMA_IMSG_CDAT_OFF                     0x014
42 #define MV_XOR_V2_DMA_IMSG_THRD_OFF                     0x018
43 #define   MV_XOR_V2_DMA_IMSG_THRD_MASK                  0x7FFF
44 #define   MV_XOR_V2_DMA_IMSG_THRD_SHIFT                 0x0
45 #define MV_XOR_V2_DMA_DESQ_AWATTR_OFF                   0x01C
46   /* Same flags as MV_XOR_V2_DMA_DESQ_ARATTR_OFF */
47 #define MV_XOR_V2_DMA_DESQ_ALLOC_OFF                    0x04C
48 #define   MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_MASK           0xFFFF
49 #define   MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_SHIFT          16
50 #define MV_XOR_V2_DMA_IMSG_BALR_OFF                     0x050
51 #define MV_XOR_V2_DMA_IMSG_BAHR_OFF                     0x054
52 #define MV_XOR_V2_DMA_DESQ_CTRL_OFF                     0x100
53 #define   MV_XOR_V2_DMA_DESQ_CTRL_32B                   1
54 #define   MV_XOR_V2_DMA_DESQ_CTRL_128B                  7
55 #define MV_XOR_V2_DMA_DESQ_STOP_OFF                     0x800
56 #define MV_XOR_V2_DMA_DESQ_DEALLOC_OFF                  0x804
57 #define MV_XOR_V2_DMA_DESQ_ADD_OFF                      0x808
58
59 /* XOR Global registers */
60 #define MV_XOR_V2_GLOB_BW_CTRL                          0x4
61 #define   MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_SHIFT      0
62 #define   MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_VAL        64
63 #define   MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_SHIFT      8
64 #define   MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_VAL        8
65 #define   MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_SHIFT     12
66 #define   MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_VAL       4
67 #define   MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_SHIFT     16
68 #define   MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_VAL       4
69 #define MV_XOR_V2_GLOB_PAUSE                            0x014
70 #define   MV_XOR_V2_GLOB_PAUSE_AXI_TIME_DIS_VAL         0x8
71 #define MV_XOR_V2_GLOB_SYS_INT_CAUSE                    0x200
72 #define MV_XOR_V2_GLOB_SYS_INT_MASK                     0x204
73 #define MV_XOR_V2_GLOB_MEM_INT_CAUSE                    0x220
74 #define MV_XOR_V2_GLOB_MEM_INT_MASK                     0x224
75
76 #define MV_XOR_V2_MIN_DESC_SIZE                         32
77 #define MV_XOR_V2_EXT_DESC_SIZE                         128
78
79 #define MV_XOR_V2_DESC_RESERVED_SIZE                    12
80 #define MV_XOR_V2_DESC_BUFF_D_ADDR_SIZE                 12
81
82 #define MV_XOR_V2_CMD_LINE_NUM_MAX_D_BUF                8
83
84 /*
85  * Descriptors queue size. With 32 bytes descriptors, up to 2^14
86  * descriptors are allowed, with 128 bytes descriptors, up to 2^12
87  * descriptors are allowed. This driver uses 128 bytes descriptors,
88  * but experimentation has shown that a set of 1024 descriptors is
89  * sufficient to reach a good level of performance.
90  */
91 #define MV_XOR_V2_DESC_NUM                              1024
92
93 /**
94  * struct mv_xor_v2_descriptor - DMA HW descriptor
95  * @desc_id: used by S/W and is not affected by H/W.
96  * @flags: error and status flags
97  * @crc32_result: CRC32 calculation result
98  * @desc_ctrl: operation mode and control flags
99  * @buff_size: amount of bytes to be processed
100  * @fill_pattern_src_addr: Fill-Pattern or Source-Address and
101  * AW-Attributes
102  * @data_buff_addr: Source (and might be RAID6 destination)
103  * addresses of data buffers in RAID5 and RAID6
104  * @reserved: reserved
105  */
106 struct mv_xor_v2_descriptor {
107         u16 desc_id;
108         u16 flags;
109         u32 crc32_result;
110         u32 desc_ctrl;
111
112         /* Definitions for desc_ctrl */
113 #define DESC_NUM_ACTIVE_D_BUF_SHIFT     22
114 #define DESC_OP_MODE_SHIFT              28
115 #define DESC_OP_MODE_NOP                0       /* Idle operation */
116 #define DESC_OP_MODE_MEMCPY             1       /* Pure-DMA operation */
117 #define DESC_OP_MODE_MEMSET             2       /* Mem-Fill operation */
118 #define DESC_OP_MODE_MEMINIT            3       /* Mem-Init operation */
119 #define DESC_OP_MODE_MEM_COMPARE        4       /* Mem-Compare operation */
120 #define DESC_OP_MODE_CRC32              5       /* CRC32 calculation */
121 #define DESC_OP_MODE_XOR                6       /* RAID5 (XOR) operation */
122 #define DESC_OP_MODE_RAID6              7       /* RAID6 P&Q-generation */
123 #define DESC_OP_MODE_RAID6_REC          8       /* RAID6 Recovery */
124 #define DESC_Q_BUFFER_ENABLE            BIT(16)
125 #define DESC_P_BUFFER_ENABLE            BIT(17)
126 #define DESC_IOD                        BIT(27)
127
128         u32 buff_size;
129         u32 fill_pattern_src_addr[4];
130         u32 data_buff_addr[MV_XOR_V2_DESC_BUFF_D_ADDR_SIZE];
131         u32 reserved[MV_XOR_V2_DESC_RESERVED_SIZE];
132 };
133
134 /**
135  * struct mv_xor_v2_device - implements a xor device
136  * @lock: lock for the engine
137  * @dma_base: memory mapped DMA register base
138  * @glob_base: memory mapped global register base
139  * @irq_tasklet:
140  * @free_sw_desc: linked list of free SW descriptors
141  * @dmadev: dma device
142  * @dmachan: dma channel
143  * @hw_desq: HW descriptors queue
144  * @hw_desq_virt: virtual address of DESCQ
145  * @sw_desq: SW descriptors queue
146  * @desc_size: HW descriptor size
147  * @npendings: number of pending descriptors (for which tx_submit has
148  * been called, but not yet issue_pending)
149  */
150 struct mv_xor_v2_device {
151         spinlock_t lock;
152         void __iomem *dma_base;
153         void __iomem *glob_base;
154         struct clk *clk;
155         struct tasklet_struct irq_tasklet;
156         struct list_head free_sw_desc;
157         struct dma_device dmadev;
158         struct dma_chan dmachan;
159         dma_addr_t hw_desq;
160         struct mv_xor_v2_descriptor *hw_desq_virt;
161         struct mv_xor_v2_sw_desc *sw_desq;
162         int desc_size;
163         unsigned int npendings;
164 };
165
166 /**
167  * struct mv_xor_v2_sw_desc - implements a xor SW descriptor
168  * @idx: descriptor index
169  * @async_tx: support for the async_tx api
170  * @hw_desc: assosiated HW descriptor
171  * @free_list: node of the free SW descriprots list
172 */
173 struct mv_xor_v2_sw_desc {
174         int idx;
175         struct dma_async_tx_descriptor async_tx;
176         struct mv_xor_v2_descriptor hw_desc;
177         struct list_head free_list;
178 };
179
180 /*
181  * Fill the data buffers to a HW descriptor
182  */
183 static void mv_xor_v2_set_data_buffers(struct mv_xor_v2_device *xor_dev,
184                                         struct mv_xor_v2_descriptor *desc,
185                                         dma_addr_t src, int index)
186 {
187         int arr_index = ((index >> 1) * 3);
188
189         /*
190          * Fill the buffer's addresses to the descriptor.
191          *
192          * The format of the buffers address for 2 sequential buffers
193          * X and X + 1:
194          *
195          *  First word:  Buffer-DX-Address-Low[31:0]
196          *  Second word: Buffer-DX+1-Address-Low[31:0]
197          *  Third word:  DX+1-Buffer-Address-High[47:32] [31:16]
198          *               DX-Buffer-Address-High[47:32] [15:0]
199          */
200         if ((index & 0x1) == 0) {
201                 desc->data_buff_addr[arr_index] = lower_32_bits(src);
202
203                 desc->data_buff_addr[arr_index + 2] &= ~0xFFFF;
204                 desc->data_buff_addr[arr_index + 2] |=
205                         upper_32_bits(src) & 0xFFFF;
206         } else {
207                 desc->data_buff_addr[arr_index + 1] =
208                         lower_32_bits(src);
209
210                 desc->data_buff_addr[arr_index + 2] &= ~0xFFFF0000;
211                 desc->data_buff_addr[arr_index + 2] |=
212                         (upper_32_bits(src) & 0xFFFF) << 16;
213         }
214 }
215
216 /*
217  * Return the next available index in the DESQ.
218  */
219 static int mv_xor_v2_get_desq_write_ptr(struct mv_xor_v2_device *xor_dev)
220 {
221         /* read the index for the next available descriptor in the DESQ */
222         u32 reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ALLOC_OFF);
223
224         return ((reg >> MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_SHIFT)
225                 & MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_MASK);
226 }
227
228 /*
229  * notify the engine of new descriptors, and update the available index.
230  */
231 static void mv_xor_v2_add_desc_to_desq(struct mv_xor_v2_device *xor_dev,
232                                        int num_of_desc)
233 {
234         /* write the number of new descriptors in the DESQ. */
235         writel(num_of_desc, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ADD_OFF);
236 }
237
238 /*
239  * free HW descriptors
240  */
241 static void mv_xor_v2_free_desc_from_desq(struct mv_xor_v2_device *xor_dev,
242                                           int num_of_desc)
243 {
244         /* write the number of new descriptors in the DESQ. */
245         writel(num_of_desc, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DEALLOC_OFF);
246 }
247
248 /*
249  * Set descriptor size
250  * Return the HW descriptor size in bytes
251  */
252 static int mv_xor_v2_set_desc_size(struct mv_xor_v2_device *xor_dev)
253 {
254         writel(MV_XOR_V2_DMA_DESQ_CTRL_128B,
255                xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_CTRL_OFF);
256
257         return MV_XOR_V2_EXT_DESC_SIZE;
258 }
259
260 /*
261  * Set the IMSG threshold
262  */
263 static inline
264 void mv_xor_v2_set_imsg_thrd(struct mv_xor_v2_device *xor_dev, int thrd_val)
265 {
266         u32 reg;
267
268         reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_THRD_OFF);
269
270         reg &= (~MV_XOR_V2_DMA_IMSG_THRD_MASK << MV_XOR_V2_DMA_IMSG_THRD_SHIFT);
271         reg |= (thrd_val << MV_XOR_V2_DMA_IMSG_THRD_SHIFT);
272
273         writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_THRD_OFF);
274 }
275
276 static irqreturn_t mv_xor_v2_interrupt_handler(int irq, void *data)
277 {
278         struct mv_xor_v2_device *xor_dev = data;
279         unsigned int ndescs;
280         u32 reg;
281
282         reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DONE_OFF);
283
284         ndescs = ((reg >> MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT) &
285                   MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK);
286
287         /* No descriptors to process */
288         if (!ndescs)
289                 return IRQ_NONE;
290
291         /*
292          * Update IMSG threshold, to disable new IMSG interrupts until
293          * end of the tasklet
294          */
295         mv_xor_v2_set_imsg_thrd(xor_dev, MV_XOR_V2_DESC_NUM);
296
297         /* schedule a tasklet to handle descriptors callbacks */
298         tasklet_schedule(&xor_dev->irq_tasklet);
299
300         return IRQ_HANDLED;
301 }
302
303 /*
304  * submit a descriptor to the DMA engine
305  */
306 static dma_cookie_t
307 mv_xor_v2_tx_submit(struct dma_async_tx_descriptor *tx)
308 {
309         int desq_ptr;
310         void *dest_hw_desc;
311         dma_cookie_t cookie;
312         struct mv_xor_v2_sw_desc *sw_desc =
313                 container_of(tx, struct mv_xor_v2_sw_desc, async_tx);
314         struct mv_xor_v2_device *xor_dev =
315                 container_of(tx->chan, struct mv_xor_v2_device, dmachan);
316
317         dev_dbg(xor_dev->dmadev.dev,
318                 "%s sw_desc %p: async_tx %p\n",
319                 __func__, sw_desc, &sw_desc->async_tx);
320
321         /* assign coookie */
322         spin_lock_bh(&xor_dev->lock);
323         cookie = dma_cookie_assign(tx);
324
325         /* get the next available slot in the DESQ */
326         desq_ptr = mv_xor_v2_get_desq_write_ptr(xor_dev);
327
328         /* copy the HW descriptor from the SW descriptor to the DESQ */
329         dest_hw_desc = xor_dev->hw_desq_virt + desq_ptr;
330
331         memcpy(dest_hw_desc, &sw_desc->hw_desc, xor_dev->desc_size);
332
333         xor_dev->npendings++;
334
335         spin_unlock_bh(&xor_dev->lock);
336
337         return cookie;
338 }
339
340 /*
341  * Prepare a SW descriptor
342  */
343 static struct mv_xor_v2_sw_desc *
344 mv_xor_v2_prep_sw_desc(struct mv_xor_v2_device *xor_dev)
345 {
346         struct mv_xor_v2_sw_desc *sw_desc;
347         bool found = false;
348
349         /* Lock the channel */
350         spin_lock_bh(&xor_dev->lock);
351
352         if (list_empty(&xor_dev->free_sw_desc)) {
353                 spin_unlock_bh(&xor_dev->lock);
354                 /* schedule tasklet to free some descriptors */
355                 tasklet_schedule(&xor_dev->irq_tasklet);
356                 return NULL;
357         }
358
359         list_for_each_entry(sw_desc, &xor_dev->free_sw_desc, free_list) {
360                 if (async_tx_test_ack(&sw_desc->async_tx)) {
361                         found = true;
362                         break;
363                 }
364         }
365
366         if (!found) {
367                 spin_unlock_bh(&xor_dev->lock);
368                 return NULL;
369         }
370
371         list_del(&sw_desc->free_list);
372
373         /* Release the channel */
374         spin_unlock_bh(&xor_dev->lock);
375
376         return sw_desc;
377 }
378
379 /*
380  * Prepare a HW descriptor for a memcpy operation
381  */
382 static struct dma_async_tx_descriptor *
383 mv_xor_v2_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
384                           dma_addr_t src, size_t len, unsigned long flags)
385 {
386         struct mv_xor_v2_sw_desc *sw_desc;
387         struct mv_xor_v2_descriptor *hw_descriptor;
388         struct mv_xor_v2_device *xor_dev;
389
390         xor_dev = container_of(chan, struct mv_xor_v2_device, dmachan);
391
392         dev_dbg(xor_dev->dmadev.dev,
393                 "%s len: %zu src %pad dest %pad flags: %ld\n",
394                 __func__, len, &src, &dest, flags);
395
396         sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
397         if (!sw_desc)
398                 return NULL;
399
400         sw_desc->async_tx.flags = flags;
401
402         /* set the HW descriptor */
403         hw_descriptor = &sw_desc->hw_desc;
404
405         /* save the SW descriptor ID to restore when operation is done */
406         hw_descriptor->desc_id = sw_desc->idx;
407
408         /* Set the MEMCPY control word */
409         hw_descriptor->desc_ctrl =
410                 DESC_OP_MODE_MEMCPY << DESC_OP_MODE_SHIFT;
411
412         if (flags & DMA_PREP_INTERRUPT)
413                 hw_descriptor->desc_ctrl |= DESC_IOD;
414
415         /* Set source address */
416         hw_descriptor->fill_pattern_src_addr[0] = lower_32_bits(src);
417         hw_descriptor->fill_pattern_src_addr[1] =
418                 upper_32_bits(src) & 0xFFFF;
419
420         /* Set Destination address */
421         hw_descriptor->fill_pattern_src_addr[2] = lower_32_bits(dest);
422         hw_descriptor->fill_pattern_src_addr[3] =
423                 upper_32_bits(dest) & 0xFFFF;
424
425         /* Set buffers size */
426         hw_descriptor->buff_size = len;
427
428         /* return the async tx descriptor */
429         return &sw_desc->async_tx;
430 }
431
432 /*
433  * Prepare a HW descriptor for a XOR operation
434  */
435 static struct dma_async_tx_descriptor *
436 mv_xor_v2_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
437                        unsigned int src_cnt, size_t len, unsigned long flags)
438 {
439         struct mv_xor_v2_sw_desc *sw_desc;
440         struct mv_xor_v2_descriptor *hw_descriptor;
441         struct mv_xor_v2_device *xor_dev =
442                 container_of(chan, struct mv_xor_v2_device, dmachan);
443         int i;
444
445         if (src_cnt > MV_XOR_V2_CMD_LINE_NUM_MAX_D_BUF || src_cnt < 1)
446                 return NULL;
447
448         dev_dbg(xor_dev->dmadev.dev,
449                 "%s src_cnt: %d len: %zu dest %pad flags: %ld\n",
450                 __func__, src_cnt, len, &dest, flags);
451
452         sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
453         if (!sw_desc)
454                 return NULL;
455
456         sw_desc->async_tx.flags = flags;
457
458         /* set the HW descriptor */
459         hw_descriptor = &sw_desc->hw_desc;
460
461         /* save the SW descriptor ID to restore when operation is done */
462         hw_descriptor->desc_id = sw_desc->idx;
463
464         /* Set the XOR control word */
465         hw_descriptor->desc_ctrl =
466                 DESC_OP_MODE_XOR << DESC_OP_MODE_SHIFT;
467         hw_descriptor->desc_ctrl |= DESC_P_BUFFER_ENABLE;
468
469         if (flags & DMA_PREP_INTERRUPT)
470                 hw_descriptor->desc_ctrl |= DESC_IOD;
471
472         /* Set the data buffers */
473         for (i = 0; i < src_cnt; i++)
474                 mv_xor_v2_set_data_buffers(xor_dev, hw_descriptor, src[i], i);
475
476         hw_descriptor->desc_ctrl |=
477                 src_cnt << DESC_NUM_ACTIVE_D_BUF_SHIFT;
478
479         /* Set Destination address */
480         hw_descriptor->fill_pattern_src_addr[2] = lower_32_bits(dest);
481         hw_descriptor->fill_pattern_src_addr[3] =
482                 upper_32_bits(dest) & 0xFFFF;
483
484         /* Set buffers size */
485         hw_descriptor->buff_size = len;
486
487         /* return the async tx descriptor */
488         return &sw_desc->async_tx;
489 }
490
491 /*
492  * Prepare a HW descriptor for interrupt operation.
493  */
494 static struct dma_async_tx_descriptor *
495 mv_xor_v2_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
496 {
497         struct mv_xor_v2_sw_desc *sw_desc;
498         struct mv_xor_v2_descriptor *hw_descriptor;
499         struct mv_xor_v2_device *xor_dev =
500                 container_of(chan, struct mv_xor_v2_device, dmachan);
501
502         sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
503         if (!sw_desc)
504                 return NULL;
505
506         /* set the HW descriptor */
507         hw_descriptor = &sw_desc->hw_desc;
508
509         /* save the SW descriptor ID to restore when operation is done */
510         hw_descriptor->desc_id = sw_desc->idx;
511
512         /* Set the INTERRUPT control word */
513         hw_descriptor->desc_ctrl =
514                 DESC_OP_MODE_NOP << DESC_OP_MODE_SHIFT;
515         hw_descriptor->desc_ctrl |= DESC_IOD;
516
517         /* return the async tx descriptor */
518         return &sw_desc->async_tx;
519 }
520
521 /*
522  * push pending transactions to hardware
523  */
524 static void mv_xor_v2_issue_pending(struct dma_chan *chan)
525 {
526         struct mv_xor_v2_device *xor_dev =
527                 container_of(chan, struct mv_xor_v2_device, dmachan);
528
529         spin_lock_bh(&xor_dev->lock);
530
531         /*
532          * update the engine with the number of descriptors to
533          * process
534          */
535         mv_xor_v2_add_desc_to_desq(xor_dev, xor_dev->npendings);
536         xor_dev->npendings = 0;
537
538         /* Activate the channel */
539         writel(0, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_STOP_OFF);
540
541         spin_unlock_bh(&xor_dev->lock);
542 }
543
544 static inline
545 int mv_xor_v2_get_pending_params(struct mv_xor_v2_device *xor_dev,
546                                  int *pending_ptr)
547 {
548         u32 reg;
549
550         reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DONE_OFF);
551
552         /* get the next pending descriptor index */
553         *pending_ptr = ((reg >> MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_SHIFT) &
554                         MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_MASK);
555
556         /* get the number of descriptors pending handle */
557         return ((reg >> MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT) &
558                 MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK);
559 }
560
561 /*
562  * handle the descriptors after HW process
563  */
564 static void mv_xor_v2_tasklet(unsigned long data)
565 {
566         struct mv_xor_v2_device *xor_dev = (struct mv_xor_v2_device *) data;
567         int pending_ptr, num_of_pending, i;
568         struct mv_xor_v2_sw_desc *next_pending_sw_desc = NULL;
569
570         dev_dbg(xor_dev->dmadev.dev, "%s %d\n", __func__, __LINE__);
571
572         /* get the pending descriptors parameters */
573         num_of_pending = mv_xor_v2_get_pending_params(xor_dev, &pending_ptr);
574
575         /* loop over free descriptors */
576         for (i = 0; i < num_of_pending; i++) {
577                 struct mv_xor_v2_descriptor *next_pending_hw_desc =
578                         xor_dev->hw_desq_virt + pending_ptr;
579
580                 /* get the SW descriptor related to the HW descriptor */
581                 next_pending_sw_desc =
582                         &xor_dev->sw_desq[next_pending_hw_desc->desc_id];
583
584                 /* call the callback */
585                 if (next_pending_sw_desc->async_tx.cookie > 0) {
586                         /*
587                          * update the channel's completed cookie - no
588                          * lock is required the IMSG threshold provide
589                          * the locking
590                          */
591                         dma_cookie_complete(&next_pending_sw_desc->async_tx);
592
593                         if (next_pending_sw_desc->async_tx.callback)
594                                 next_pending_sw_desc->async_tx.callback(
595                                 next_pending_sw_desc->async_tx.callback_param);
596
597                         dma_descriptor_unmap(&next_pending_sw_desc->async_tx);
598                 }
599
600                 dma_run_dependencies(&next_pending_sw_desc->async_tx);
601
602                 /* Lock the channel */
603                 spin_lock_bh(&xor_dev->lock);
604
605                 /* add the SW descriptor to the free descriptors list */
606                 list_add(&next_pending_sw_desc->free_list,
607                          &xor_dev->free_sw_desc);
608
609                 /* Release the channel */
610                 spin_unlock_bh(&xor_dev->lock);
611
612                 /* increment the next descriptor */
613                 pending_ptr++;
614                 if (pending_ptr >= MV_XOR_V2_DESC_NUM)
615                         pending_ptr = 0;
616         }
617
618         if (num_of_pending != 0) {
619                 /* free the descriptores */
620                 mv_xor_v2_free_desc_from_desq(xor_dev, num_of_pending);
621         }
622
623         /* Update IMSG threshold, to enable new IMSG interrupts */
624         mv_xor_v2_set_imsg_thrd(xor_dev, 0);
625 }
626
627 /*
628  *      Set DMA Interrupt-message (IMSG) parameters
629  */
630 static void mv_xor_v2_set_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
631 {
632         struct mv_xor_v2_device *xor_dev = dev_get_drvdata(desc->dev);
633
634         writel(msg->address_lo,
635                xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_BALR_OFF);
636         writel(msg->address_hi & 0xFFFF,
637                xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_BAHR_OFF);
638         writel(msg->data,
639                xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_CDAT_OFF);
640 }
641
642 static int mv_xor_v2_descq_init(struct mv_xor_v2_device *xor_dev)
643 {
644         u32 reg;
645
646         /* write the DESQ size to the DMA engine */
647         writel(MV_XOR_V2_DESC_NUM,
648                xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_SIZE_OFF);
649
650         /* write the DESQ address to the DMA enngine*/
651         writel(xor_dev->hw_desq & 0xFFFFFFFF,
652                xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_BALR_OFF);
653         writel((xor_dev->hw_desq & 0xFFFF00000000) >> 32,
654                xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_BAHR_OFF);
655
656         /* enable the DMA engine */
657         writel(0, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_STOP_OFF);
658
659         /*
660          * This is a temporary solution, until we activate the
661          * SMMU. Set the attributes for reading & writing data buffers
662          * & descriptors to:
663          *
664          *  - OuterShareable - Snoops will be performed on CPU caches
665          *  - Enable cacheable - Bufferable, Modifiable, Other Allocate
666          *    and Allocate
667          */
668         reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ARATTR_OFF);
669         reg &= ~MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK;
670         reg |= MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE |
671                 MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE;
672         writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ARATTR_OFF);
673
674         reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_AWATTR_OFF);
675         reg &= ~MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK;
676         reg |= MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE |
677                 MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE;
678         writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_AWATTR_OFF);
679
680         /* BW CTRL - set values to optimize the XOR performance:
681          *
682          *  - Set WrBurstLen & RdBurstLen - the unit will issue
683          *    maximum of 256B write/read transactions.
684          * -  Limit the number of outstanding write & read data
685          *    (OBB/IBB) requests to the maximal value.
686         */
687         reg = ((MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_VAL <<
688                 MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_SHIFT) |
689                (MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_VAL  <<
690                 MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_SHIFT) |
691                (MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_VAL <<
692                 MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_SHIFT) |
693                (MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_VAL <<
694                 MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_SHIFT));
695         writel(reg, xor_dev->glob_base + MV_XOR_V2_GLOB_BW_CTRL);
696
697         /* Disable the AXI timer feature */
698         reg = readl(xor_dev->glob_base + MV_XOR_V2_GLOB_PAUSE);
699         reg |= MV_XOR_V2_GLOB_PAUSE_AXI_TIME_DIS_VAL;
700         writel(reg, xor_dev->glob_base + MV_XOR_V2_GLOB_PAUSE);
701
702         return 0;
703 }
704
705 static int mv_xor_v2_probe(struct platform_device *pdev)
706 {
707         struct mv_xor_v2_device *xor_dev;
708         struct resource *res;
709         int i, ret = 0;
710         struct dma_device *dma_dev;
711         struct mv_xor_v2_sw_desc *sw_desc;
712         struct msi_desc *msi_desc;
713
714         BUILD_BUG_ON(sizeof(struct mv_xor_v2_descriptor) !=
715                      MV_XOR_V2_EXT_DESC_SIZE);
716
717         xor_dev = devm_kzalloc(&pdev->dev, sizeof(*xor_dev), GFP_KERNEL);
718         if (!xor_dev)
719                 return -ENOMEM;
720
721         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
722         xor_dev->dma_base = devm_ioremap_resource(&pdev->dev, res);
723         if (IS_ERR(xor_dev->dma_base))
724                 return PTR_ERR(xor_dev->dma_base);
725
726         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
727         xor_dev->glob_base = devm_ioremap_resource(&pdev->dev, res);
728         if (IS_ERR(xor_dev->glob_base))
729                 return PTR_ERR(xor_dev->glob_base);
730
731         platform_set_drvdata(pdev, xor_dev);
732
733         xor_dev->clk = devm_clk_get(&pdev->dev, NULL);
734         if (IS_ERR(xor_dev->clk) && PTR_ERR(xor_dev->clk) == -EPROBE_DEFER)
735                 return -EPROBE_DEFER;
736         if (!IS_ERR(xor_dev->clk)) {
737                 ret = clk_prepare_enable(xor_dev->clk);
738                 if (ret)
739                         return ret;
740         }
741
742         ret = platform_msi_domain_alloc_irqs(&pdev->dev, 1,
743                                              mv_xor_v2_set_msi_msg);
744         if (ret)
745                 goto disable_clk;
746
747         msi_desc = first_msi_entry(&pdev->dev);
748         if (!msi_desc)
749                 goto free_msi_irqs;
750
751         ret = devm_request_irq(&pdev->dev, msi_desc->irq,
752                                mv_xor_v2_interrupt_handler, 0,
753                                dev_name(&pdev->dev), xor_dev);
754         if (ret)
755                 goto free_msi_irqs;
756
757         tasklet_init(&xor_dev->irq_tasklet, mv_xor_v2_tasklet,
758                      (unsigned long) xor_dev);
759
760         xor_dev->desc_size = mv_xor_v2_set_desc_size(xor_dev);
761
762         dma_cookie_init(&xor_dev->dmachan);
763
764         /*
765          * allocate coherent memory for hardware descriptors
766          * note: writecombine gives slightly better performance, but
767          * requires that we explicitly flush the writes
768          */
769         xor_dev->hw_desq_virt =
770                 dma_alloc_coherent(&pdev->dev,
771                                    xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
772                                    &xor_dev->hw_desq, GFP_KERNEL);
773         if (!xor_dev->hw_desq_virt) {
774                 ret = -ENOMEM;
775                 goto free_msi_irqs;
776         }
777
778         /* alloc memory for the SW descriptors */
779         xor_dev->sw_desq = devm_kzalloc(&pdev->dev, sizeof(*sw_desc) *
780                                         MV_XOR_V2_DESC_NUM, GFP_KERNEL);
781         if (!xor_dev->sw_desq) {
782                 ret = -ENOMEM;
783                 goto free_hw_desq;
784         }
785
786         spin_lock_init(&xor_dev->lock);
787
788         /* init the free SW descriptors list */
789         INIT_LIST_HEAD(&xor_dev->free_sw_desc);
790
791         /* add all SW descriptors to the free list */
792         for (i = 0; i < MV_XOR_V2_DESC_NUM; i++) {
793                 struct mv_xor_v2_sw_desc *sw_desc =
794                         xor_dev->sw_desq + i;
795                 sw_desc->idx = i;
796                 dma_async_tx_descriptor_init(&sw_desc->async_tx,
797                                              &xor_dev->dmachan);
798                 sw_desc->async_tx.tx_submit = mv_xor_v2_tx_submit;
799                 async_tx_ack(&sw_desc->async_tx);
800
801                 list_add(&sw_desc->free_list,
802                          &xor_dev->free_sw_desc);
803         }
804
805         dma_dev = &xor_dev->dmadev;
806
807         /* set DMA capabilities */
808         dma_cap_zero(dma_dev->cap_mask);
809         dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
810         dma_cap_set(DMA_XOR, dma_dev->cap_mask);
811         dma_cap_set(DMA_INTERRUPT, dma_dev->cap_mask);
812
813         /* init dma link list */
814         INIT_LIST_HEAD(&dma_dev->channels);
815
816         /* set base routines */
817         dma_dev->device_tx_status = dma_cookie_status;
818         dma_dev->device_issue_pending = mv_xor_v2_issue_pending;
819         dma_dev->dev = &pdev->dev;
820
821         dma_dev->device_prep_dma_memcpy = mv_xor_v2_prep_dma_memcpy;
822         dma_dev->device_prep_dma_interrupt = mv_xor_v2_prep_dma_interrupt;
823         dma_dev->max_xor = 8;
824         dma_dev->device_prep_dma_xor = mv_xor_v2_prep_dma_xor;
825
826         xor_dev->dmachan.device = dma_dev;
827
828         list_add_tail(&xor_dev->dmachan.device_node,
829                       &dma_dev->channels);
830
831         mv_xor_v2_descq_init(xor_dev);
832
833         ret = dma_async_device_register(dma_dev);
834         if (ret)
835                 goto free_hw_desq;
836
837         dev_notice(&pdev->dev, "Marvell Version 2 XOR driver\n");
838
839         return 0;
840
841 free_hw_desq:
842         dma_free_coherent(&pdev->dev,
843                           xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
844                           xor_dev->hw_desq_virt, xor_dev->hw_desq);
845 free_msi_irqs:
846         platform_msi_domain_free_irqs(&pdev->dev);
847 disable_clk:
848         if (!IS_ERR(xor_dev->clk))
849                 clk_disable_unprepare(xor_dev->clk);
850         return ret;
851 }
852
853 static int mv_xor_v2_remove(struct platform_device *pdev)
854 {
855         struct mv_xor_v2_device *xor_dev = platform_get_drvdata(pdev);
856
857         dma_async_device_unregister(&xor_dev->dmadev);
858
859         dma_free_coherent(&pdev->dev,
860                           xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
861                           xor_dev->hw_desq_virt, xor_dev->hw_desq);
862
863         platform_msi_domain_free_irqs(&pdev->dev);
864
865         clk_disable_unprepare(xor_dev->clk);
866
867         return 0;
868 }
869
870 #ifdef CONFIG_OF
871 static const struct of_device_id mv_xor_v2_dt_ids[] = {
872         { .compatible = "marvell,xor-v2", },
873         {},
874 };
875 MODULE_DEVICE_TABLE(of, mv_xor_v2_dt_ids);
876 #endif
877
878 static struct platform_driver mv_xor_v2_driver = {
879         .probe          = mv_xor_v2_probe,
880         .remove         = mv_xor_v2_remove,
881         .driver         = {
882                 .name   = "mv_xor_v2",
883                 .of_match_table = of_match_ptr(mv_xor_v2_dt_ids),
884         },
885 };
886
887 module_platform_driver(mv_xor_v2_driver);
888
889 MODULE_DESCRIPTION("DMA engine driver for Marvell's Version 2 of XOR engine");
890 MODULE_LICENSE("GPL");