blob: 8648d2394ab66d9ea696685cfbe3387a39485b37 [file] [log] [blame]
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301/*
2 * Applied Micro X-Gene SoC DMA engine Driver
3 *
4 * Copyright (c) 2015, Applied Micro Circuits Corporation
5 * Authors: Rameshwar Prasad Sahu <rsahu@apm.com>
6 * Loc Ho <lho@apm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * NOTE: PM support is currently not available.
22 */
23
Rameshwar Prasad Sahu89079492015-07-21 18:44:39 +053024#include <linux/acpi.h>
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +053025#include <linux/clk.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/dmaengine.h>
29#include <linux/dmapool.h>
30#include <linux/interrupt.h>
31#include <linux/io.h>
Rameshwar Prasad Sahub0b79022015-12-23 18:28:15 +053032#include <linux/irq.h>
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +053033#include <linux/module.h>
34#include <linux/of_device.h>
35
36#include "dmaengine.h"
37
38/* X-Gene DMA ring csr registers and bit definations */
39#define XGENE_DMA_RING_CONFIG 0x04
40#define XGENE_DMA_RING_ENABLE BIT(31)
41#define XGENE_DMA_RING_ID 0x08
42#define XGENE_DMA_RING_ID_SETUP(v) ((v) | BIT(31))
43#define XGENE_DMA_RING_ID_BUF 0x0C
44#define XGENE_DMA_RING_ID_BUF_SETUP(v) (((v) << 9) | BIT(21))
45#define XGENE_DMA_RING_THRESLD0_SET1 0x30
46#define XGENE_DMA_RING_THRESLD0_SET1_VAL 0X64
47#define XGENE_DMA_RING_THRESLD1_SET1 0x34
48#define XGENE_DMA_RING_THRESLD1_SET1_VAL 0xC8
49#define XGENE_DMA_RING_HYSTERESIS 0x68
50#define XGENE_DMA_RING_HYSTERESIS_VAL 0xFFFFFFFF
51#define XGENE_DMA_RING_STATE 0x6C
52#define XGENE_DMA_RING_STATE_WR_BASE 0x70
53#define XGENE_DMA_RING_NE_INT_MODE 0x017C
54#define XGENE_DMA_RING_NE_INT_MODE_SET(m, v) \
55 ((m) = ((m) & ~BIT(31 - (v))) | BIT(31 - (v)))
56#define XGENE_DMA_RING_NE_INT_MODE_RESET(m, v) \
57 ((m) &= (~BIT(31 - (v))))
58#define XGENE_DMA_RING_CLKEN 0xC208
59#define XGENE_DMA_RING_SRST 0xC200
60#define XGENE_DMA_RING_MEM_RAM_SHUTDOWN 0xD070
61#define XGENE_DMA_RING_BLK_MEM_RDY 0xD074
62#define XGENE_DMA_RING_BLK_MEM_RDY_VAL 0xFFFFFFFF
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +053063#define XGENE_DMA_RING_ID_GET(owner, num) (((owner) << 6) | (num))
64#define XGENE_DMA_RING_DST_ID(v) ((1 << 10) | (v))
65#define XGENE_DMA_RING_CMD_OFFSET 0x2C
66#define XGENE_DMA_RING_CMD_BASE_OFFSET(v) ((v) << 6)
67#define XGENE_DMA_RING_COHERENT_SET(m) \
68 (((u32 *)(m))[2] |= BIT(4))
69#define XGENE_DMA_RING_ADDRL_SET(m, v) \
70 (((u32 *)(m))[2] |= (((v) >> 8) << 5))
71#define XGENE_DMA_RING_ADDRH_SET(m, v) \
72 (((u32 *)(m))[3] |= ((v) >> 35))
73#define XGENE_DMA_RING_ACCEPTLERR_SET(m) \
74 (((u32 *)(m))[3] |= BIT(19))
75#define XGENE_DMA_RING_SIZE_SET(m, v) \
76 (((u32 *)(m))[3] |= ((v) << 23))
77#define XGENE_DMA_RING_RECOMBBUF_SET(m) \
78 (((u32 *)(m))[3] |= BIT(27))
79#define XGENE_DMA_RING_RECOMTIMEOUTL_SET(m) \
80 (((u32 *)(m))[3] |= (0x7 << 28))
81#define XGENE_DMA_RING_RECOMTIMEOUTH_SET(m) \
82 (((u32 *)(m))[4] |= 0x3)
83#define XGENE_DMA_RING_SELTHRSH_SET(m) \
84 (((u32 *)(m))[4] |= BIT(3))
85#define XGENE_DMA_RING_TYPE_SET(m, v) \
86 (((u32 *)(m))[4] |= ((v) << 19))
87
88/* X-Gene DMA device csr registers and bit definitions */
89#define XGENE_DMA_IPBRR 0x0
90#define XGENE_DMA_DEV_ID_RD(v) ((v) & 0x00000FFF)
91#define XGENE_DMA_BUS_ID_RD(v) (((v) >> 12) & 3)
92#define XGENE_DMA_REV_NO_RD(v) (((v) >> 14) & 3)
93#define XGENE_DMA_GCR 0x10
94#define XGENE_DMA_CH_SETUP(v) \
95 ((v) = ((v) & ~0x000FFFFF) | 0x000AAFFF)
96#define XGENE_DMA_ENABLE(v) ((v) |= BIT(31))
97#define XGENE_DMA_DISABLE(v) ((v) &= ~BIT(31))
98#define XGENE_DMA_RAID6_CONT 0x14
99#define XGENE_DMA_RAID6_MULTI_CTRL(v) ((v) << 24)
100#define XGENE_DMA_INT 0x70
101#define XGENE_DMA_INT_MASK 0x74
102#define XGENE_DMA_INT_ALL_MASK 0xFFFFFFFF
103#define XGENE_DMA_INT_ALL_UNMASK 0x0
104#define XGENE_DMA_INT_MASK_SHIFT 0x14
105#define XGENE_DMA_RING_INT0_MASK 0x90A0
106#define XGENE_DMA_RING_INT1_MASK 0x90A8
107#define XGENE_DMA_RING_INT2_MASK 0x90B0
108#define XGENE_DMA_RING_INT3_MASK 0x90B8
109#define XGENE_DMA_RING_INT4_MASK 0x90C0
110#define XGENE_DMA_CFG_RING_WQ_ASSOC 0x90E0
111#define XGENE_DMA_ASSOC_RING_MNGR1 0xFFFFFFFF
112#define XGENE_DMA_MEM_RAM_SHUTDOWN 0xD070
113#define XGENE_DMA_BLK_MEM_RDY 0xD074
114#define XGENE_DMA_BLK_MEM_RDY_VAL 0xFFFFFFFF
Rameshwar Prasad Sahucda8e932015-07-07 15:34:25 +0530115#define XGENE_DMA_RING_CMD_SM_OFFSET 0x8000
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530116
117/* X-Gene SoC EFUSE csr register and bit defination */
118#define XGENE_SOC_JTAG1_SHADOW 0x18
119#define XGENE_DMA_PQ_DISABLE_MASK BIT(13)
120
121/* X-Gene DMA Descriptor format */
122#define XGENE_DMA_DESC_NV_BIT BIT_ULL(50)
123#define XGENE_DMA_DESC_IN_BIT BIT_ULL(55)
124#define XGENE_DMA_DESC_C_BIT BIT_ULL(63)
125#define XGENE_DMA_DESC_DR_BIT BIT_ULL(61)
126#define XGENE_DMA_DESC_ELERR_POS 46
127#define XGENE_DMA_DESC_RTYPE_POS 56
128#define XGENE_DMA_DESC_LERR_POS 60
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530129#define XGENE_DMA_DESC_BUFLEN_POS 48
130#define XGENE_DMA_DESC_HOENQ_NUM_POS 48
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530131#define XGENE_DMA_DESC_ELERR_RD(m) \
132 (((m) >> XGENE_DMA_DESC_ELERR_POS) & 0x3)
133#define XGENE_DMA_DESC_LERR_RD(m) \
134 (((m) >> XGENE_DMA_DESC_LERR_POS) & 0x7)
135#define XGENE_DMA_DESC_STATUS(elerr, lerr) \
136 (((elerr) << 4) | (lerr))
137
138/* X-Gene DMA descriptor empty s/w signature */
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530139#define XGENE_DMA_DESC_EMPTY_SIGNATURE ~0ULL
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530140
141/* X-Gene DMA configurable parameters defines */
142#define XGENE_DMA_RING_NUM 512
143#define XGENE_DMA_BUFNUM 0x0
144#define XGENE_DMA_CPU_BUFNUM 0x18
145#define XGENE_DMA_RING_OWNER_DMA 0x03
146#define XGENE_DMA_RING_OWNER_CPU 0x0F
147#define XGENE_DMA_RING_TYPE_REGULAR 0x01
148#define XGENE_DMA_RING_WQ_DESC_SIZE 32 /* 32 Bytes */
149#define XGENE_DMA_RING_NUM_CONFIG 5
150#define XGENE_DMA_MAX_CHANNEL 4
151#define XGENE_DMA_XOR_CHANNEL 0
152#define XGENE_DMA_PQ_CHANNEL 1
153#define XGENE_DMA_MAX_BYTE_CNT 0x4000 /* 16 KB */
154#define XGENE_DMA_MAX_64B_DESC_BYTE_CNT 0x14000 /* 80 KB */
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530155#define XGENE_DMA_MAX_XOR_SRC 5
156#define XGENE_DMA_16K_BUFFER_LEN_CODE 0x0
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530157#define XGENE_DMA_INVALID_LEN_CODE 0x7800000000000000ULL
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530158
159/* X-Gene DMA descriptor error codes */
160#define ERR_DESC_AXI 0x01
161#define ERR_BAD_DESC 0x02
162#define ERR_READ_DATA_AXI 0x03
163#define ERR_WRITE_DATA_AXI 0x04
164#define ERR_FBP_TIMEOUT 0x05
165#define ERR_ECC 0x06
166#define ERR_DIFF_SIZE 0x08
167#define ERR_SCT_GAT_LEN 0x09
168#define ERR_CRC_ERR 0x11
169#define ERR_CHKSUM 0x12
170#define ERR_DIF 0x13
171
172/* X-Gene DMA error interrupt codes */
173#define ERR_DIF_SIZE_INT 0x0
174#define ERR_GS_ERR_INT 0x1
175#define ERR_FPB_TIMEO_INT 0x2
176#define ERR_WFIFO_OVF_INT 0x3
177#define ERR_RFIFO_OVF_INT 0x4
178#define ERR_WR_TIMEO_INT 0x5
179#define ERR_RD_TIMEO_INT 0x6
180#define ERR_WR_ERR_INT 0x7
181#define ERR_RD_ERR_INT 0x8
182#define ERR_BAD_DESC_INT 0x9
183#define ERR_DESC_DST_INT 0xA
184#define ERR_DESC_SRC_INT 0xB
185
186/* X-Gene DMA flyby operation code */
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530187#define FLYBY_2SRC_XOR 0x80
188#define FLYBY_3SRC_XOR 0x90
189#define FLYBY_4SRC_XOR 0xA0
190#define FLYBY_5SRC_XOR 0xB0
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530191
192/* X-Gene DMA SW descriptor flags */
193#define XGENE_DMA_FLAG_64B_DESC BIT(0)
194
195/* Define to dump X-Gene DMA descriptor */
196#define XGENE_DMA_DESC_DUMP(desc, m) \
197 print_hex_dump(KERN_ERR, (m), \
198 DUMP_PREFIX_ADDRESS, 16, 8, (desc), 32, 0)
199
200#define to_dma_desc_sw(tx) \
201 container_of(tx, struct xgene_dma_desc_sw, tx)
202#define to_dma_chan(dchan) \
203 container_of(dchan, struct xgene_dma_chan, dma_chan)
204
205#define chan_dbg(chan, fmt, arg...) \
206 dev_dbg(chan->dev, "%s: " fmt, chan->name, ##arg)
207#define chan_err(chan, fmt, arg...) \
208 dev_err(chan->dev, "%s: " fmt, chan->name, ##arg)
209
210struct xgene_dma_desc_hw {
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530211 __le64 m0;
212 __le64 m1;
213 __le64 m2;
214 __le64 m3;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530215};
216
217enum xgene_dma_ring_cfgsize {
218 XGENE_DMA_RING_CFG_SIZE_512B,
219 XGENE_DMA_RING_CFG_SIZE_2KB,
220 XGENE_DMA_RING_CFG_SIZE_16KB,
221 XGENE_DMA_RING_CFG_SIZE_64KB,
222 XGENE_DMA_RING_CFG_SIZE_512KB,
223 XGENE_DMA_RING_CFG_SIZE_INVALID
224};
225
226struct xgene_dma_ring {
227 struct xgene_dma *pdma;
228 u8 buf_num;
229 u16 id;
230 u16 num;
231 u16 head;
232 u16 owner;
233 u16 slots;
234 u16 dst_ring_num;
235 u32 size;
236 void __iomem *cmd;
237 void __iomem *cmd_base;
238 dma_addr_t desc_paddr;
239 u32 state[XGENE_DMA_RING_NUM_CONFIG];
240 enum xgene_dma_ring_cfgsize cfgsize;
241 union {
242 void *desc_vaddr;
243 struct xgene_dma_desc_hw *desc_hw;
244 };
245};
246
247struct xgene_dma_desc_sw {
248 struct xgene_dma_desc_hw desc1;
249 struct xgene_dma_desc_hw desc2;
250 u32 flags;
251 struct list_head node;
252 struct list_head tx_list;
253 struct dma_async_tx_descriptor tx;
254};
255
256/**
257 * struct xgene_dma_chan - internal representation of an X-Gene DMA channel
258 * @dma_chan: dmaengine channel object member
259 * @pdma: X-Gene DMA device structure reference
260 * @dev: struct device reference for dma mapping api
261 * @id: raw id of this channel
262 * @rx_irq: channel IRQ
263 * @name: name of X-Gene DMA channel
264 * @lock: serializes enqueue/dequeue operations to the descriptor pool
265 * @pending: number of transaction request pushed to DMA controller for
266 * execution, but still waiting for completion,
267 * @max_outstanding: max number of outstanding request we can push to channel
268 * @ld_pending: descriptors which are queued to run, but have not yet been
269 * submitted to the hardware for execution
270 * @ld_running: descriptors which are currently being executing by the hardware
271 * @ld_completed: descriptors which have finished execution by the hardware.
272 * These descriptors have already had their cleanup actions run. They
273 * are waiting for the ACK bit to be set by the async tx API.
274 * @desc_pool: descriptor pool for DMA operations
275 * @tasklet: bottom half where all completed descriptors cleans
276 * @tx_ring: transmit ring descriptor that we use to prepare actual
277 * descriptors for further executions
278 * @rx_ring: receive ring descriptor that we use to get completed DMA
279 * descriptors during cleanup time
280 */
281struct xgene_dma_chan {
282 struct dma_chan dma_chan;
283 struct xgene_dma *pdma;
284 struct device *dev;
285 int id;
286 int rx_irq;
Dan Carpentered1f0412015-04-09 12:05:04 +0300287 char name[10];
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530288 spinlock_t lock;
289 int pending;
290 int max_outstanding;
291 struct list_head ld_pending;
292 struct list_head ld_running;
293 struct list_head ld_completed;
294 struct dma_pool *desc_pool;
295 struct tasklet_struct tasklet;
296 struct xgene_dma_ring tx_ring;
297 struct xgene_dma_ring rx_ring;
298};
299
300/**
301 * struct xgene_dma - internal representation of an X-Gene DMA device
302 * @err_irq: DMA error irq number
303 * @ring_num: start id number for DMA ring
304 * @csr_dma: base for DMA register access
305 * @csr_ring: base for DMA ring register access
306 * @csr_ring_cmd: base for DMA ring command register access
307 * @csr_efuse: base for efuse register access
308 * @dma_dev: embedded struct dma_device
309 * @chan: reference to X-Gene DMA channels
310 */
311struct xgene_dma {
312 struct device *dev;
313 struct clk *clk;
314 int err_irq;
315 int ring_num;
316 void __iomem *csr_dma;
317 void __iomem *csr_ring;
318 void __iomem *csr_ring_cmd;
319 void __iomem *csr_efuse;
320 struct dma_device dma_dev[XGENE_DMA_MAX_CHANNEL];
321 struct xgene_dma_chan chan[XGENE_DMA_MAX_CHANNEL];
322};
323
324static const char * const xgene_dma_desc_err[] = {
325 [ERR_DESC_AXI] = "AXI error when reading src/dst link list",
326 [ERR_BAD_DESC] = "ERR or El_ERR fields not set to zero in desc",
327 [ERR_READ_DATA_AXI] = "AXI error when reading data",
328 [ERR_WRITE_DATA_AXI] = "AXI error when writing data",
329 [ERR_FBP_TIMEOUT] = "Timeout on bufpool fetch",
330 [ERR_ECC] = "ECC double bit error",
331 [ERR_DIFF_SIZE] = "Bufpool too small to hold all the DIF result",
332 [ERR_SCT_GAT_LEN] = "Gather and scatter data length not same",
333 [ERR_CRC_ERR] = "CRC error",
334 [ERR_CHKSUM] = "Checksum error",
335 [ERR_DIF] = "DIF error",
336};
337
338static const char * const xgene_dma_err[] = {
339 [ERR_DIF_SIZE_INT] = "DIF size error",
340 [ERR_GS_ERR_INT] = "Gather scatter not same size error",
341 [ERR_FPB_TIMEO_INT] = "Free pool time out error",
342 [ERR_WFIFO_OVF_INT] = "Write FIFO over flow error",
343 [ERR_RFIFO_OVF_INT] = "Read FIFO over flow error",
344 [ERR_WR_TIMEO_INT] = "Write time out error",
345 [ERR_RD_TIMEO_INT] = "Read time out error",
346 [ERR_WR_ERR_INT] = "HBF bus write error",
347 [ERR_RD_ERR_INT] = "HBF bus read error",
348 [ERR_BAD_DESC_INT] = "Ring descriptor HE0 not set error",
349 [ERR_DESC_DST_INT] = "HFB reading dst link address error",
350 [ERR_DESC_SRC_INT] = "HFB reading src link address error",
351};
352
353static bool is_pq_enabled(struct xgene_dma *pdma)
354{
355 u32 val;
356
357 val = ioread32(pdma->csr_efuse + XGENE_SOC_JTAG1_SHADOW);
358 return !(val & XGENE_DMA_PQ_DISABLE_MASK);
359}
360
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530361static u64 xgene_dma_encode_len(size_t len)
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530362{
363 return (len < XGENE_DMA_MAX_BYTE_CNT) ?
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530364 ((u64)len << XGENE_DMA_DESC_BUFLEN_POS) :
365 XGENE_DMA_16K_BUFFER_LEN_CODE;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530366}
367
368static u8 xgene_dma_encode_xor_flyby(u32 src_cnt)
369{
370 static u8 flyby_type[] = {
371 FLYBY_2SRC_XOR, /* Dummy */
372 FLYBY_2SRC_XOR, /* Dummy */
373 FLYBY_2SRC_XOR,
374 FLYBY_3SRC_XOR,
375 FLYBY_4SRC_XOR,
376 FLYBY_5SRC_XOR
377 };
378
379 return flyby_type[src_cnt];
380}
381
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530382static void xgene_dma_set_src_buffer(__le64 *ext8, size_t *len,
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530383 dma_addr_t *paddr)
384{
385 size_t nbytes = (*len < XGENE_DMA_MAX_BYTE_CNT) ?
386 *len : XGENE_DMA_MAX_BYTE_CNT;
387
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530388 *ext8 |= cpu_to_le64(*paddr);
389 *ext8 |= cpu_to_le64(xgene_dma_encode_len(nbytes));
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530390 *len -= nbytes;
391 *paddr += nbytes;
392}
393
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530394static void xgene_dma_invalidate_buffer(__le64 *ext8)
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530395{
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530396 *ext8 |= cpu_to_le64(XGENE_DMA_INVALID_LEN_CODE);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530397}
398
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530399static __le64 *xgene_dma_lookup_ext8(struct xgene_dma_desc_hw *desc, int idx)
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530400{
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530401 switch (idx) {
402 case 0:
403 return &desc->m1;
404 case 1:
405 return &desc->m0;
406 case 2:
407 return &desc->m3;
408 case 3:
409 return &desc->m2;
410 default:
411 pr_err("Invalid dma descriptor index\n");
412 }
413
414 return NULL;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530415}
416
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530417static void xgene_dma_init_desc(struct xgene_dma_desc_hw *desc,
418 u16 dst_ring_num)
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530419{
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530420 desc->m0 |= cpu_to_le64(XGENE_DMA_DESC_IN_BIT);
421 desc->m0 |= cpu_to_le64((u64)XGENE_DMA_RING_OWNER_DMA <<
422 XGENE_DMA_DESC_RTYPE_POS);
423 desc->m1 |= cpu_to_le64(XGENE_DMA_DESC_C_BIT);
424 desc->m3 |= cpu_to_le64((u64)dst_ring_num <<
425 XGENE_DMA_DESC_HOENQ_NUM_POS);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530426}
427
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530428static void xgene_dma_prep_xor_desc(struct xgene_dma_chan *chan,
429 struct xgene_dma_desc_sw *desc_sw,
430 dma_addr_t *dst, dma_addr_t *src,
431 u32 src_cnt, size_t *nbytes,
432 const u8 *scf)
433{
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530434 struct xgene_dma_desc_hw *desc1, *desc2;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530435 size_t len = *nbytes;
436 int i;
437
438 desc1 = &desc_sw->desc1;
439 desc2 = &desc_sw->desc2;
440
441 /* Initialize DMA descriptor */
442 xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
443
444 /* Set destination address */
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530445 desc1->m2 |= cpu_to_le64(XGENE_DMA_DESC_DR_BIT);
446 desc1->m3 |= cpu_to_le64(*dst);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530447
448 /* We have multiple source addresses, so need to set NV bit*/
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530449 desc1->m0 |= cpu_to_le64(XGENE_DMA_DESC_NV_BIT);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530450
451 /* Set flyby opcode */
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530452 desc1->m2 |= cpu_to_le64(xgene_dma_encode_xor_flyby(src_cnt));
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530453
454 /* Set 1st to 5th source addresses */
455 for (i = 0; i < src_cnt; i++) {
456 len = *nbytes;
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530457 xgene_dma_set_src_buffer((i == 0) ? &desc1->m1 :
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530458 xgene_dma_lookup_ext8(desc2, i - 1),
459 &len, &src[i]);
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530460 desc1->m2 |= cpu_to_le64((scf[i] << ((i + 1) * 8)));
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530461 }
462
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530463 /* Update meta data */
464 *nbytes = len;
465 *dst += XGENE_DMA_MAX_BYTE_CNT;
466
467 /* We need always 64B descriptor to perform xor or pq operations */
468 desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
469}
470
471static dma_cookie_t xgene_dma_tx_submit(struct dma_async_tx_descriptor *tx)
472{
473 struct xgene_dma_desc_sw *desc;
474 struct xgene_dma_chan *chan;
475 dma_cookie_t cookie;
476
477 if (unlikely(!tx))
478 return -EINVAL;
479
480 chan = to_dma_chan(tx->chan);
481 desc = to_dma_desc_sw(tx);
482
483 spin_lock_bh(&chan->lock);
484
485 cookie = dma_cookie_assign(tx);
486
487 /* Add this transaction list onto the tail of the pending queue */
488 list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
489
490 spin_unlock_bh(&chan->lock);
491
492 return cookie;
493}
494
495static void xgene_dma_clean_descriptor(struct xgene_dma_chan *chan,
496 struct xgene_dma_desc_sw *desc)
497{
498 list_del(&desc->node);
499 chan_dbg(chan, "LD %p free\n", desc);
500 dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
501}
502
503static struct xgene_dma_desc_sw *xgene_dma_alloc_descriptor(
504 struct xgene_dma_chan *chan)
505{
506 struct xgene_dma_desc_sw *desc;
507 dma_addr_t phys;
508
Vinod Koul9c811202015-09-21 20:56:58 +0530509 desc = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530510 if (!desc) {
511 chan_err(chan, "Failed to allocate LDs\n");
512 return NULL;
513 }
514
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530515 INIT_LIST_HEAD(&desc->tx_list);
516 desc->tx.phys = phys;
517 desc->tx.tx_submit = xgene_dma_tx_submit;
518 dma_async_tx_descriptor_init(&desc->tx, &chan->dma_chan);
519
520 chan_dbg(chan, "LD %p allocated\n", desc);
521
522 return desc;
523}
524
525/**
526 * xgene_dma_clean_completed_descriptor - free all descriptors which
527 * has been completed and acked
528 * @chan: X-Gene DMA channel
529 *
530 * This function is used on all completed and acked descriptors.
531 */
532static void xgene_dma_clean_completed_descriptor(struct xgene_dma_chan *chan)
533{
534 struct xgene_dma_desc_sw *desc, *_desc;
535
536 /* Run the callback for each descriptor, in order */
537 list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node) {
538 if (async_tx_test_ack(&desc->tx))
539 xgene_dma_clean_descriptor(chan, desc);
540 }
541}
542
543/**
544 * xgene_dma_run_tx_complete_actions - cleanup a single link descriptor
545 * @chan: X-Gene DMA channel
546 * @desc: descriptor to cleanup and free
547 *
548 * This function is used on a descriptor which has been executed by the DMA
549 * controller. It will run any callbacks, submit any dependencies.
550 */
551static void xgene_dma_run_tx_complete_actions(struct xgene_dma_chan *chan,
552 struct xgene_dma_desc_sw *desc)
553{
554 struct dma_async_tx_descriptor *tx = &desc->tx;
555
556 /*
557 * If this is not the last transaction in the group,
558 * then no need to complete cookie and run any callback as
559 * this is not the tx_descriptor which had been sent to caller
560 * of this DMA request
561 */
562
563 if (tx->cookie == 0)
564 return;
565
566 dma_cookie_complete(tx);
Dave Jiangfd3c69b2016-07-25 10:34:19 -0700567 dma_descriptor_unmap(tx);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530568
569 /* Run the link descriptor callback function */
Dave Jiangb1f884a2016-07-20 13:13:39 -0700570 dmaengine_desc_get_callback_invoke(tx, NULL);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530571
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530572 /* Run any dependencies */
573 dma_run_dependencies(tx);
574}
575
576/**
577 * xgene_dma_clean_running_descriptor - move the completed descriptor from
578 * ld_running to ld_completed
579 * @chan: X-Gene DMA channel
580 * @desc: the descriptor which is completed
581 *
582 * Free the descriptor directly if acked by async_tx api,
583 * else move it to queue ld_completed.
584 */
585static void xgene_dma_clean_running_descriptor(struct xgene_dma_chan *chan,
586 struct xgene_dma_desc_sw *desc)
587{
588 /* Remove from the list of running transactions */
589 list_del(&desc->node);
590
591 /*
592 * the client is allowed to attach dependent operations
593 * until 'ack' is set
594 */
595 if (!async_tx_test_ack(&desc->tx)) {
596 /*
597 * Move this descriptor to the list of descriptors which is
598 * completed, but still awaiting the 'ack' bit to be set.
599 */
600 list_add_tail(&desc->node, &chan->ld_completed);
601 return;
602 }
603
604 chan_dbg(chan, "LD %p free\n", desc);
605 dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
606}
607
Rameshwar Prasad Sahuee08b592015-09-16 13:33:23 +0530608static void xgene_chan_xfer_request(struct xgene_dma_chan *chan,
609 struct xgene_dma_desc_sw *desc_sw)
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530610{
Rameshwar Prasad Sahuee08b592015-09-16 13:33:23 +0530611 struct xgene_dma_ring *ring = &chan->tx_ring;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530612 struct xgene_dma_desc_hw *desc_hw;
613
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530614 /* Get hw descriptor from DMA tx ring */
615 desc_hw = &ring->desc_hw[ring->head];
616
617 /*
618 * Increment the head count to point next
619 * descriptor for next time
620 */
621 if (++ring->head == ring->slots)
622 ring->head = 0;
623
624 /* Copy prepared sw descriptor data to hw descriptor */
625 memcpy(desc_hw, &desc_sw->desc1, sizeof(*desc_hw));
626
627 /*
628 * Check if we have prepared 64B descriptor,
629 * in this case we need one more hw descriptor
630 */
631 if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) {
632 desc_hw = &ring->desc_hw[ring->head];
633
634 if (++ring->head == ring->slots)
635 ring->head = 0;
636
637 memcpy(desc_hw, &desc_sw->desc2, sizeof(*desc_hw));
638 }
639
Rameshwar Prasad Sahuee08b592015-09-16 13:33:23 +0530640 /* Increment the pending transaction count */
641 chan->pending += ((desc_sw->flags &
642 XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
643
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530644 /* Notify the hw that we have descriptor ready for execution */
645 iowrite32((desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) ?
646 2 : 1, ring->cmd);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530647}
648
649/**
650 * xgene_chan_xfer_ld_pending - push any pending transactions to hw
651 * @chan : X-Gene DMA channel
652 *
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530653 * LOCKING: must hold chan->lock
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530654 */
655static void xgene_chan_xfer_ld_pending(struct xgene_dma_chan *chan)
656{
657 struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530658
659 /*
660 * If the list of pending descriptors is empty, then we
661 * don't need to do any work at all
662 */
663 if (list_empty(&chan->ld_pending)) {
664 chan_dbg(chan, "No pending LDs\n");
665 return;
666 }
667
668 /*
669 * Move elements from the queue of pending transactions onto the list
670 * of running transactions and push it to hw for further executions
671 */
672 list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_pending, node) {
673 /*
674 * Check if have pushed max number of transactions to hw
675 * as capable, so let's stop here and will push remaining
676 * elements from pening ld queue after completing some
677 * descriptors that we have already pushed
678 */
679 if (chan->pending >= chan->max_outstanding)
680 return;
681
Rameshwar Prasad Sahuee08b592015-09-16 13:33:23 +0530682 xgene_chan_xfer_request(chan, desc_sw);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530683
684 /*
685 * Delete this element from ld pending queue and append it to
686 * ld running queue
687 */
688 list_move_tail(&desc_sw->node, &chan->ld_running);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530689 }
690}
691
692/**
693 * xgene_dma_cleanup_descriptors - cleanup link descriptors which are completed
694 * and move them to ld_completed to free until flag 'ack' is set
695 * @chan: X-Gene DMA channel
696 *
697 * This function is used on descriptors which have been executed by the DMA
698 * controller. It will run any callbacks, submit any dependencies, then
699 * free these descriptors if flag 'ack' is set.
700 */
701static void xgene_dma_cleanup_descriptors(struct xgene_dma_chan *chan)
702{
703 struct xgene_dma_ring *ring = &chan->rx_ring;
704 struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
705 struct xgene_dma_desc_hw *desc_hw;
Rameshwar Prasad Sahu005ce702015-08-21 14:33:34 +0530706 struct list_head ld_completed;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530707 u8 status;
708
Rameshwar Prasad Sahu005ce702015-08-21 14:33:34 +0530709 INIT_LIST_HEAD(&ld_completed);
710
711 spin_lock_bh(&chan->lock);
712
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530713 /* Clean already completed and acked descriptors */
714 xgene_dma_clean_completed_descriptor(chan);
715
Rameshwar Prasad Sahu005ce702015-08-21 14:33:34 +0530716 /* Move all completed descriptors to ld completed queue, in order */
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530717 list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_running, node) {
718 /* Get subsequent hw descriptor from DMA rx ring */
719 desc_hw = &ring->desc_hw[ring->head];
720
721 /* Check if this descriptor has been completed */
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530722 if (unlikely(le64_to_cpu(desc_hw->m0) ==
723 XGENE_DMA_DESC_EMPTY_SIGNATURE))
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530724 break;
725
726 if (++ring->head == ring->slots)
727 ring->head = 0;
728
729 /* Check if we have any error with DMA transactions */
730 status = XGENE_DMA_DESC_STATUS(
731 XGENE_DMA_DESC_ELERR_RD(le64_to_cpu(
732 desc_hw->m0)),
733 XGENE_DMA_DESC_LERR_RD(le64_to_cpu(
734 desc_hw->m0)));
735 if (status) {
736 /* Print the DMA error type */
737 chan_err(chan, "%s\n", xgene_dma_desc_err[status]);
738
739 /*
740 * We have DMA transactions error here. Dump DMA Tx
741 * and Rx descriptors for this request */
742 XGENE_DMA_DESC_DUMP(&desc_sw->desc1,
743 "X-Gene DMA TX DESC1: ");
744
745 if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC)
746 XGENE_DMA_DESC_DUMP(&desc_sw->desc2,
747 "X-Gene DMA TX DESC2: ");
748
749 XGENE_DMA_DESC_DUMP(desc_hw,
750 "X-Gene DMA RX ERR DESC: ");
751 }
752
753 /* Notify the hw about this completed descriptor */
754 iowrite32(-1, ring->cmd);
755
756 /* Mark this hw descriptor as processed */
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530757 desc_hw->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530758
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530759 /*
760 * Decrement the pending transaction count
761 * as we have processed one
762 */
Rameshwar Prasad Sahuee08b592015-09-16 13:33:23 +0530763 chan->pending -= ((desc_sw->flags &
764 XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
Rameshwar Prasad Sahu005ce702015-08-21 14:33:34 +0530765
766 /*
767 * Delete this node from ld running queue and append it to
768 * ld completed queue for further processing
769 */
770 list_move_tail(&desc_sw->node, &ld_completed);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530771 }
772
773 /*
774 * Start any pending transactions automatically
775 * In the ideal case, we keep the DMA controller busy while we go
776 * ahead and free the descriptors below.
777 */
778 xgene_chan_xfer_ld_pending(chan);
Rameshwar Prasad Sahu005ce702015-08-21 14:33:34 +0530779
780 spin_unlock_bh(&chan->lock);
781
782 /* Run the callback for each descriptor, in order */
783 list_for_each_entry_safe(desc_sw, _desc_sw, &ld_completed, node) {
784 xgene_dma_run_tx_complete_actions(chan, desc_sw);
785 xgene_dma_clean_running_descriptor(chan, desc_sw);
786 }
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530787}
788
789static int xgene_dma_alloc_chan_resources(struct dma_chan *dchan)
790{
791 struct xgene_dma_chan *chan = to_dma_chan(dchan);
792
793 /* Has this channel already been allocated? */
794 if (chan->desc_pool)
795 return 1;
796
797 chan->desc_pool = dma_pool_create(chan->name, chan->dev,
798 sizeof(struct xgene_dma_desc_sw),
799 0, 0);
800 if (!chan->desc_pool) {
801 chan_err(chan, "Failed to allocate descriptor pool\n");
802 return -ENOMEM;
803 }
804
805 chan_dbg(chan, "Allocate descripto pool\n");
806
807 return 1;
808}
809
810/**
811 * xgene_dma_free_desc_list - Free all descriptors in a queue
812 * @chan: X-Gene DMA channel
813 * @list: the list to free
814 *
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530815 * LOCKING: must hold chan->lock
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530816 */
817static void xgene_dma_free_desc_list(struct xgene_dma_chan *chan,
818 struct list_head *list)
819{
820 struct xgene_dma_desc_sw *desc, *_desc;
821
822 list_for_each_entry_safe(desc, _desc, list, node)
823 xgene_dma_clean_descriptor(chan, desc);
824}
825
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530826static void xgene_dma_free_chan_resources(struct dma_chan *dchan)
827{
828 struct xgene_dma_chan *chan = to_dma_chan(dchan);
829
830 chan_dbg(chan, "Free all resources\n");
831
832 if (!chan->desc_pool)
833 return;
834
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530835 /* Process all running descriptor */
836 xgene_dma_cleanup_descriptors(chan);
837
Rameshwar Prasad Sahu005ce702015-08-21 14:33:34 +0530838 spin_lock_bh(&chan->lock);
839
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530840 /* Clean all link descriptor queues */
841 xgene_dma_free_desc_list(chan, &chan->ld_pending);
842 xgene_dma_free_desc_list(chan, &chan->ld_running);
843 xgene_dma_free_desc_list(chan, &chan->ld_completed);
844
845 spin_unlock_bh(&chan->lock);
846
847 /* Delete this channel DMA pool */
848 dma_pool_destroy(chan->desc_pool);
849 chan->desc_pool = NULL;
850}
851
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530852static struct dma_async_tx_descriptor *xgene_dma_prep_xor(
853 struct dma_chan *dchan, dma_addr_t dst, dma_addr_t *src,
854 u32 src_cnt, size_t len, unsigned long flags)
855{
856 struct xgene_dma_desc_sw *first = NULL, *new;
857 struct xgene_dma_chan *chan;
858 static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {
859 0x01, 0x01, 0x01, 0x01, 0x01};
860
861 if (unlikely(!dchan || !len))
862 return NULL;
863
864 chan = to_dma_chan(dchan);
865
866 do {
867 /* Allocate the link descriptor from DMA pool */
868 new = xgene_dma_alloc_descriptor(chan);
869 if (!new)
870 goto fail;
871
872 /* Prepare xor DMA descriptor */
873 xgene_dma_prep_xor_desc(chan, new, &dst, src,
874 src_cnt, &len, multi);
875
876 if (!first)
877 first = new;
878
879 new->tx.cookie = 0;
880 async_tx_ack(&new->tx);
881
882 /* Insert the link descriptor to the LD ring */
883 list_add_tail(&new->node, &first->tx_list);
884 } while (len);
885
886 new->tx.flags = flags; /* client is in control of this ack */
887 new->tx.cookie = -EBUSY;
888 list_splice(&first->tx_list, &new->tx_list);
889
890 return &new->tx;
891
892fail:
893 if (!first)
894 return NULL;
895
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530896 xgene_dma_free_desc_list(chan, &first->tx_list);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530897 return NULL;
898}
899
900static struct dma_async_tx_descriptor *xgene_dma_prep_pq(
901 struct dma_chan *dchan, dma_addr_t *dst, dma_addr_t *src,
902 u32 src_cnt, const u8 *scf, size_t len, unsigned long flags)
903{
904 struct xgene_dma_desc_sw *first = NULL, *new;
905 struct xgene_dma_chan *chan;
906 size_t _len = len;
907 dma_addr_t _src[XGENE_DMA_MAX_XOR_SRC];
908 static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {0x01, 0x01, 0x01, 0x01, 0x01};
909
910 if (unlikely(!dchan || !len))
911 return NULL;
912
913 chan = to_dma_chan(dchan);
914
915 /*
916 * Save source addresses on local variable, may be we have to
917 * prepare two descriptor to generate P and Q if both enabled
918 * in the flags by client
919 */
920 memcpy(_src, src, sizeof(*src) * src_cnt);
921
922 if (flags & DMA_PREP_PQ_DISABLE_P)
923 len = 0;
924
925 if (flags & DMA_PREP_PQ_DISABLE_Q)
926 _len = 0;
927
928 do {
929 /* Allocate the link descriptor from DMA pool */
930 new = xgene_dma_alloc_descriptor(chan);
931 if (!new)
932 goto fail;
933
934 if (!first)
935 first = new;
936
937 new->tx.cookie = 0;
938 async_tx_ack(&new->tx);
939
940 /* Insert the link descriptor to the LD ring */
941 list_add_tail(&new->node, &first->tx_list);
942
943 /*
944 * Prepare DMA descriptor to generate P,
945 * if DMA_PREP_PQ_DISABLE_P flag is not set
946 */
947 if (len) {
948 xgene_dma_prep_xor_desc(chan, new, &dst[0], src,
949 src_cnt, &len, multi);
950 continue;
951 }
952
953 /*
954 * Prepare DMA descriptor to generate Q,
955 * if DMA_PREP_PQ_DISABLE_Q flag is not set
956 */
957 if (_len) {
958 xgene_dma_prep_xor_desc(chan, new, &dst[1], _src,
959 src_cnt, &_len, scf);
960 }
961 } while (len || _len);
962
963 new->tx.flags = flags; /* client is in control of this ack */
964 new->tx.cookie = -EBUSY;
965 list_splice(&first->tx_list, &new->tx_list);
966
967 return &new->tx;
968
969fail:
970 if (!first)
971 return NULL;
972
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +0530973 xgene_dma_free_desc_list(chan, &first->tx_list);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530974 return NULL;
975}
976
977static void xgene_dma_issue_pending(struct dma_chan *dchan)
978{
979 struct xgene_dma_chan *chan = to_dma_chan(dchan);
980
981 spin_lock_bh(&chan->lock);
982 xgene_chan_xfer_ld_pending(chan);
983 spin_unlock_bh(&chan->lock);
984}
985
986static enum dma_status xgene_dma_tx_status(struct dma_chan *dchan,
987 dma_cookie_t cookie,
988 struct dma_tx_state *txstate)
989{
990 return dma_cookie_status(dchan, cookie, txstate);
991}
992
993static void xgene_dma_tasklet_cb(unsigned long data)
994{
995 struct xgene_dma_chan *chan = (struct xgene_dma_chan *)data;
996
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +0530997 /* Run all cleanup for descriptors which have been completed */
998 xgene_dma_cleanup_descriptors(chan);
999
1000 /* Re-enable DMA channel IRQ */
1001 enable_irq(chan->rx_irq);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301002}
1003
1004static irqreturn_t xgene_dma_chan_ring_isr(int irq, void *id)
1005{
1006 struct xgene_dma_chan *chan = (struct xgene_dma_chan *)id;
1007
1008 BUG_ON(!chan);
1009
1010 /*
1011 * Disable DMA channel IRQ until we process completed
1012 * descriptors
1013 */
1014 disable_irq_nosync(chan->rx_irq);
1015
1016 /*
1017 * Schedule the tasklet to handle all cleanup of the current
1018 * transaction. It will start a new transaction if there is
1019 * one pending.
1020 */
1021 tasklet_schedule(&chan->tasklet);
1022
1023 return IRQ_HANDLED;
1024}
1025
1026static irqreturn_t xgene_dma_err_isr(int irq, void *id)
1027{
1028 struct xgene_dma *pdma = (struct xgene_dma *)id;
1029 unsigned long int_mask;
1030 u32 val, i;
1031
1032 val = ioread32(pdma->csr_dma + XGENE_DMA_INT);
1033
1034 /* Clear DMA interrupts */
1035 iowrite32(val, pdma->csr_dma + XGENE_DMA_INT);
1036
1037 /* Print DMA error info */
1038 int_mask = val >> XGENE_DMA_INT_MASK_SHIFT;
1039 for_each_set_bit(i, &int_mask, ARRAY_SIZE(xgene_dma_err))
1040 dev_err(pdma->dev,
1041 "Interrupt status 0x%08X %s\n", val, xgene_dma_err[i]);
1042
1043 return IRQ_HANDLED;
1044}
1045
1046static void xgene_dma_wr_ring_state(struct xgene_dma_ring *ring)
1047{
1048 int i;
1049
1050 iowrite32(ring->num, ring->pdma->csr_ring + XGENE_DMA_RING_STATE);
1051
1052 for (i = 0; i < XGENE_DMA_RING_NUM_CONFIG; i++)
1053 iowrite32(ring->state[i], ring->pdma->csr_ring +
1054 XGENE_DMA_RING_STATE_WR_BASE + (i * 4));
1055}
1056
1057static void xgene_dma_clr_ring_state(struct xgene_dma_ring *ring)
1058{
1059 memset(ring->state, 0, sizeof(u32) * XGENE_DMA_RING_NUM_CONFIG);
1060 xgene_dma_wr_ring_state(ring);
1061}
1062
1063static void xgene_dma_setup_ring(struct xgene_dma_ring *ring)
1064{
1065 void *ring_cfg = ring->state;
1066 u64 addr = ring->desc_paddr;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301067 u32 i, val;
1068
1069 ring->slots = ring->size / XGENE_DMA_RING_WQ_DESC_SIZE;
1070
1071 /* Clear DMA ring state */
1072 xgene_dma_clr_ring_state(ring);
1073
1074 /* Set DMA ring type */
1075 XGENE_DMA_RING_TYPE_SET(ring_cfg, XGENE_DMA_RING_TYPE_REGULAR);
1076
1077 if (ring->owner == XGENE_DMA_RING_OWNER_DMA) {
1078 /* Set recombination buffer and timeout */
1079 XGENE_DMA_RING_RECOMBBUF_SET(ring_cfg);
1080 XGENE_DMA_RING_RECOMTIMEOUTL_SET(ring_cfg);
1081 XGENE_DMA_RING_RECOMTIMEOUTH_SET(ring_cfg);
1082 }
1083
1084 /* Initialize DMA ring state */
1085 XGENE_DMA_RING_SELTHRSH_SET(ring_cfg);
1086 XGENE_DMA_RING_ACCEPTLERR_SET(ring_cfg);
1087 XGENE_DMA_RING_COHERENT_SET(ring_cfg);
1088 XGENE_DMA_RING_ADDRL_SET(ring_cfg, addr);
1089 XGENE_DMA_RING_ADDRH_SET(ring_cfg, addr);
1090 XGENE_DMA_RING_SIZE_SET(ring_cfg, ring->cfgsize);
1091
1092 /* Write DMA ring configurations */
1093 xgene_dma_wr_ring_state(ring);
1094
1095 /* Set DMA ring id */
1096 iowrite32(XGENE_DMA_RING_ID_SETUP(ring->id),
1097 ring->pdma->csr_ring + XGENE_DMA_RING_ID);
1098
1099 /* Set DMA ring buffer */
1100 iowrite32(XGENE_DMA_RING_ID_BUF_SETUP(ring->num),
1101 ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
1102
1103 if (ring->owner != XGENE_DMA_RING_OWNER_CPU)
1104 return;
1105
1106 /* Set empty signature to DMA Rx ring descriptors */
1107 for (i = 0; i < ring->slots; i++) {
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +05301108 struct xgene_dma_desc_hw *desc;
1109
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301110 desc = &ring->desc_hw[i];
Rameshwar Prasad Sahu6d0767c2015-06-02 14:33:33 +05301111 desc->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301112 }
1113
1114 /* Enable DMA Rx ring interrupt */
1115 val = ioread32(ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
1116 XGENE_DMA_RING_NE_INT_MODE_SET(val, ring->buf_num);
1117 iowrite32(val, ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
1118}
1119
1120static void xgene_dma_clear_ring(struct xgene_dma_ring *ring)
1121{
1122 u32 ring_id, val;
1123
1124 if (ring->owner == XGENE_DMA_RING_OWNER_CPU) {
1125 /* Disable DMA Rx ring interrupt */
1126 val = ioread32(ring->pdma->csr_ring +
1127 XGENE_DMA_RING_NE_INT_MODE);
1128 XGENE_DMA_RING_NE_INT_MODE_RESET(val, ring->buf_num);
1129 iowrite32(val, ring->pdma->csr_ring +
1130 XGENE_DMA_RING_NE_INT_MODE);
1131 }
1132
1133 /* Clear DMA ring state */
1134 ring_id = XGENE_DMA_RING_ID_SETUP(ring->id);
1135 iowrite32(ring_id, ring->pdma->csr_ring + XGENE_DMA_RING_ID);
1136
1137 iowrite32(0, ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
1138 xgene_dma_clr_ring_state(ring);
1139}
1140
1141static void xgene_dma_set_ring_cmd(struct xgene_dma_ring *ring)
1142{
1143 ring->cmd_base = ring->pdma->csr_ring_cmd +
1144 XGENE_DMA_RING_CMD_BASE_OFFSET((ring->num -
1145 XGENE_DMA_RING_NUM));
1146
1147 ring->cmd = ring->cmd_base + XGENE_DMA_RING_CMD_OFFSET;
1148}
1149
1150static int xgene_dma_get_ring_size(struct xgene_dma_chan *chan,
1151 enum xgene_dma_ring_cfgsize cfgsize)
1152{
1153 int size;
1154
1155 switch (cfgsize) {
1156 case XGENE_DMA_RING_CFG_SIZE_512B:
1157 size = 0x200;
1158 break;
1159 case XGENE_DMA_RING_CFG_SIZE_2KB:
1160 size = 0x800;
1161 break;
1162 case XGENE_DMA_RING_CFG_SIZE_16KB:
1163 size = 0x4000;
1164 break;
1165 case XGENE_DMA_RING_CFG_SIZE_64KB:
1166 size = 0x10000;
1167 break;
1168 case XGENE_DMA_RING_CFG_SIZE_512KB:
1169 size = 0x80000;
1170 break;
1171 default:
1172 chan_err(chan, "Unsupported cfg ring size %d\n", cfgsize);
1173 return -EINVAL;
1174 }
1175
1176 return size;
1177}
1178
1179static void xgene_dma_delete_ring_one(struct xgene_dma_ring *ring)
1180{
1181 /* Clear DMA ring configurations */
1182 xgene_dma_clear_ring(ring);
1183
1184 /* De-allocate DMA ring descriptor */
1185 if (ring->desc_vaddr) {
1186 dma_free_coherent(ring->pdma->dev, ring->size,
1187 ring->desc_vaddr, ring->desc_paddr);
1188 ring->desc_vaddr = NULL;
1189 }
1190}
1191
1192static void xgene_dma_delete_chan_rings(struct xgene_dma_chan *chan)
1193{
1194 xgene_dma_delete_ring_one(&chan->rx_ring);
1195 xgene_dma_delete_ring_one(&chan->tx_ring);
1196}
1197
1198static int xgene_dma_create_ring_one(struct xgene_dma_chan *chan,
1199 struct xgene_dma_ring *ring,
1200 enum xgene_dma_ring_cfgsize cfgsize)
1201{
Andrzej Hajdac1492b4c2015-09-24 16:00:17 +02001202 int ret;
1203
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301204 /* Setup DMA ring descriptor variables */
1205 ring->pdma = chan->pdma;
1206 ring->cfgsize = cfgsize;
1207 ring->num = chan->pdma->ring_num++;
1208 ring->id = XGENE_DMA_RING_ID_GET(ring->owner, ring->buf_num);
1209
Andrzej Hajdac1492b4c2015-09-24 16:00:17 +02001210 ret = xgene_dma_get_ring_size(chan, cfgsize);
1211 if (ret <= 0)
1212 return ret;
1213 ring->size = ret;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301214
1215 /* Allocate memory for DMA ring descriptor */
1216 ring->desc_vaddr = dma_zalloc_coherent(chan->dev, ring->size,
1217 &ring->desc_paddr, GFP_KERNEL);
1218 if (!ring->desc_vaddr) {
1219 chan_err(chan, "Failed to allocate ring desc\n");
1220 return -ENOMEM;
1221 }
1222
1223 /* Configure and enable DMA ring */
1224 xgene_dma_set_ring_cmd(ring);
1225 xgene_dma_setup_ring(ring);
1226
1227 return 0;
1228}
1229
1230static int xgene_dma_create_chan_rings(struct xgene_dma_chan *chan)
1231{
1232 struct xgene_dma_ring *rx_ring = &chan->rx_ring;
1233 struct xgene_dma_ring *tx_ring = &chan->tx_ring;
1234 int ret;
1235
1236 /* Create DMA Rx ring descriptor */
1237 rx_ring->owner = XGENE_DMA_RING_OWNER_CPU;
1238 rx_ring->buf_num = XGENE_DMA_CPU_BUFNUM + chan->id;
1239
1240 ret = xgene_dma_create_ring_one(chan, rx_ring,
1241 XGENE_DMA_RING_CFG_SIZE_64KB);
1242 if (ret)
1243 return ret;
1244
1245 chan_dbg(chan, "Rx ring id 0x%X num %d desc 0x%p\n",
1246 rx_ring->id, rx_ring->num, rx_ring->desc_vaddr);
1247
1248 /* Create DMA Tx ring descriptor */
1249 tx_ring->owner = XGENE_DMA_RING_OWNER_DMA;
1250 tx_ring->buf_num = XGENE_DMA_BUFNUM + chan->id;
1251
1252 ret = xgene_dma_create_ring_one(chan, tx_ring,
1253 XGENE_DMA_RING_CFG_SIZE_64KB);
1254 if (ret) {
1255 xgene_dma_delete_ring_one(rx_ring);
1256 return ret;
1257 }
1258
1259 tx_ring->dst_ring_num = XGENE_DMA_RING_DST_ID(rx_ring->num);
1260
1261 chan_dbg(chan,
1262 "Tx ring id 0x%X num %d desc 0x%p\n",
1263 tx_ring->id, tx_ring->num, tx_ring->desc_vaddr);
1264
1265 /* Set the max outstanding request possible to this channel */
Rameshwar Prasad Sahuee08b592015-09-16 13:33:23 +05301266 chan->max_outstanding = tx_ring->slots;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301267
1268 return ret;
1269}
1270
1271static int xgene_dma_init_rings(struct xgene_dma *pdma)
1272{
1273 int ret, i, j;
1274
1275 for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1276 ret = xgene_dma_create_chan_rings(&pdma->chan[i]);
1277 if (ret) {
1278 for (j = 0; j < i; j++)
1279 xgene_dma_delete_chan_rings(&pdma->chan[j]);
1280 return ret;
1281 }
1282 }
1283
1284 return ret;
1285}
1286
1287static void xgene_dma_enable(struct xgene_dma *pdma)
1288{
1289 u32 val;
1290
1291 /* Configure and enable DMA engine */
1292 val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
1293 XGENE_DMA_CH_SETUP(val);
1294 XGENE_DMA_ENABLE(val);
1295 iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
1296}
1297
1298static void xgene_dma_disable(struct xgene_dma *pdma)
1299{
1300 u32 val;
1301
1302 val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
1303 XGENE_DMA_DISABLE(val);
1304 iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
1305}
1306
1307static void xgene_dma_mask_interrupts(struct xgene_dma *pdma)
1308{
1309 /*
1310 * Mask DMA ring overflow, underflow and
1311 * AXI write/read error interrupts
1312 */
1313 iowrite32(XGENE_DMA_INT_ALL_MASK,
1314 pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
1315 iowrite32(XGENE_DMA_INT_ALL_MASK,
1316 pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
1317 iowrite32(XGENE_DMA_INT_ALL_MASK,
1318 pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
1319 iowrite32(XGENE_DMA_INT_ALL_MASK,
1320 pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
1321 iowrite32(XGENE_DMA_INT_ALL_MASK,
1322 pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
1323
1324 /* Mask DMA error interrupts */
1325 iowrite32(XGENE_DMA_INT_ALL_MASK, pdma->csr_dma + XGENE_DMA_INT_MASK);
1326}
1327
1328static void xgene_dma_unmask_interrupts(struct xgene_dma *pdma)
1329{
1330 /*
1331 * Unmask DMA ring overflow, underflow and
1332 * AXI write/read error interrupts
1333 */
1334 iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1335 pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
1336 iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1337 pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
1338 iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1339 pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
1340 iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1341 pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
1342 iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1343 pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
1344
1345 /* Unmask DMA error interrupts */
1346 iowrite32(XGENE_DMA_INT_ALL_UNMASK,
1347 pdma->csr_dma + XGENE_DMA_INT_MASK);
1348}
1349
1350static void xgene_dma_init_hw(struct xgene_dma *pdma)
1351{
1352 u32 val;
1353
1354 /* Associate DMA ring to corresponding ring HW */
1355 iowrite32(XGENE_DMA_ASSOC_RING_MNGR1,
1356 pdma->csr_dma + XGENE_DMA_CFG_RING_WQ_ASSOC);
1357
1358 /* Configure RAID6 polynomial control setting */
1359 if (is_pq_enabled(pdma))
1360 iowrite32(XGENE_DMA_RAID6_MULTI_CTRL(0x1D),
1361 pdma->csr_dma + XGENE_DMA_RAID6_CONT);
1362 else
1363 dev_info(pdma->dev, "PQ is disabled in HW\n");
1364
1365 xgene_dma_enable(pdma);
1366 xgene_dma_unmask_interrupts(pdma);
1367
1368 /* Get DMA id and version info */
1369 val = ioread32(pdma->csr_dma + XGENE_DMA_IPBRR);
1370
1371 /* DMA device info */
1372 dev_info(pdma->dev,
1373 "X-Gene DMA v%d.%02d.%02d driver registered %d channels",
1374 XGENE_DMA_REV_NO_RD(val), XGENE_DMA_BUS_ID_RD(val),
1375 XGENE_DMA_DEV_ID_RD(val), XGENE_DMA_MAX_CHANNEL);
1376}
1377
kbuild test robota3f92e82015-04-02 17:50:56 +08001378static int xgene_dma_init_ring_mngr(struct xgene_dma *pdma)
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301379{
1380 if (ioread32(pdma->csr_ring + XGENE_DMA_RING_CLKEN) &&
1381 (!ioread32(pdma->csr_ring + XGENE_DMA_RING_SRST)))
1382 return 0;
1383
1384 iowrite32(0x3, pdma->csr_ring + XGENE_DMA_RING_CLKEN);
1385 iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_SRST);
1386
1387 /* Bring up memory */
1388 iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
1389
1390 /* Force a barrier */
1391 ioread32(pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
1392
1393 /* reset may take up to 1ms */
1394 usleep_range(1000, 1100);
1395
1396 if (ioread32(pdma->csr_ring + XGENE_DMA_RING_BLK_MEM_RDY)
1397 != XGENE_DMA_RING_BLK_MEM_RDY_VAL) {
1398 dev_err(pdma->dev,
1399 "Failed to release ring mngr memory from shutdown\n");
1400 return -ENODEV;
1401 }
1402
1403 /* program threshold set 1 and all hysteresis */
1404 iowrite32(XGENE_DMA_RING_THRESLD0_SET1_VAL,
1405 pdma->csr_ring + XGENE_DMA_RING_THRESLD0_SET1);
1406 iowrite32(XGENE_DMA_RING_THRESLD1_SET1_VAL,
1407 pdma->csr_ring + XGENE_DMA_RING_THRESLD1_SET1);
1408 iowrite32(XGENE_DMA_RING_HYSTERESIS_VAL,
1409 pdma->csr_ring + XGENE_DMA_RING_HYSTERESIS);
1410
1411 /* Enable QPcore and assign error queue */
1412 iowrite32(XGENE_DMA_RING_ENABLE,
1413 pdma->csr_ring + XGENE_DMA_RING_CONFIG);
1414
1415 return 0;
1416}
1417
1418static int xgene_dma_init_mem(struct xgene_dma *pdma)
1419{
1420 int ret;
1421
1422 ret = xgene_dma_init_ring_mngr(pdma);
1423 if (ret)
1424 return ret;
1425
1426 /* Bring up memory */
1427 iowrite32(0x0, pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
1428
1429 /* Force a barrier */
1430 ioread32(pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
1431
1432 /* reset may take up to 1ms */
1433 usleep_range(1000, 1100);
1434
1435 if (ioread32(pdma->csr_dma + XGENE_DMA_BLK_MEM_RDY)
1436 != XGENE_DMA_BLK_MEM_RDY_VAL) {
1437 dev_err(pdma->dev,
1438 "Failed to release DMA memory from shutdown\n");
1439 return -ENODEV;
1440 }
1441
1442 return 0;
1443}
1444
1445static int xgene_dma_request_irqs(struct xgene_dma *pdma)
1446{
1447 struct xgene_dma_chan *chan;
1448 int ret, i, j;
1449
1450 /* Register DMA error irq */
1451 ret = devm_request_irq(pdma->dev, pdma->err_irq, xgene_dma_err_isr,
1452 0, "dma_error", pdma);
1453 if (ret) {
1454 dev_err(pdma->dev,
1455 "Failed to register error IRQ %d\n", pdma->err_irq);
1456 return ret;
1457 }
1458
1459 /* Register DMA channel rx irq */
1460 for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1461 chan = &pdma->chan[i];
Rameshwar Prasad Sahub0b79022015-12-23 18:28:15 +05301462 irq_set_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301463 ret = devm_request_irq(chan->dev, chan->rx_irq,
1464 xgene_dma_chan_ring_isr,
1465 0, chan->name, chan);
1466 if (ret) {
1467 chan_err(chan, "Failed to register Rx IRQ %d\n",
1468 chan->rx_irq);
1469 devm_free_irq(pdma->dev, pdma->err_irq, pdma);
1470
1471 for (j = 0; j < i; j++) {
1472 chan = &pdma->chan[i];
Rameshwar Prasad Sahub0b79022015-12-23 18:28:15 +05301473 irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301474 devm_free_irq(chan->dev, chan->rx_irq, chan);
1475 }
1476
1477 return ret;
1478 }
1479 }
1480
1481 return 0;
1482}
1483
1484static void xgene_dma_free_irqs(struct xgene_dma *pdma)
1485{
1486 struct xgene_dma_chan *chan;
1487 int i;
1488
1489 /* Free DMA device error irq */
1490 devm_free_irq(pdma->dev, pdma->err_irq, pdma);
1491
1492 for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1493 chan = &pdma->chan[i];
Rameshwar Prasad Sahub0b79022015-12-23 18:28:15 +05301494 irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301495 devm_free_irq(chan->dev, chan->rx_irq, chan);
1496 }
1497}
1498
1499static void xgene_dma_set_caps(struct xgene_dma_chan *chan,
1500 struct dma_device *dma_dev)
1501{
1502 /* Initialize DMA device capability mask */
1503 dma_cap_zero(dma_dev->cap_mask);
1504
1505 /* Set DMA device capability */
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301506
1507 /* Basically here, the X-Gene SoC DMA engine channel 0 supports XOR
1508 * and channel 1 supports XOR, PQ both. First thing here is we have
1509 * mechanism in hw to enable/disable PQ/XOR supports on channel 1,
1510 * we can make sure this by reading SoC Efuse register.
1511 * Second thing, we have hw errata that if we run channel 0 and
1512 * channel 1 simultaneously with executing XOR and PQ request,
1513 * suddenly DMA engine hangs, So here we enable XOR on channel 0 only
1514 * if XOR and PQ supports on channel 1 is disabled.
1515 */
1516 if ((chan->id == XGENE_DMA_PQ_CHANNEL) &&
1517 is_pq_enabled(chan->pdma)) {
1518 dma_cap_set(DMA_PQ, dma_dev->cap_mask);
1519 dma_cap_set(DMA_XOR, dma_dev->cap_mask);
1520 } else if ((chan->id == XGENE_DMA_XOR_CHANNEL) &&
1521 !is_pq_enabled(chan->pdma)) {
1522 dma_cap_set(DMA_XOR, dma_dev->cap_mask);
1523 }
1524
1525 /* Set base and prep routines */
1526 dma_dev->dev = chan->dev;
1527 dma_dev->device_alloc_chan_resources = xgene_dma_alloc_chan_resources;
1528 dma_dev->device_free_chan_resources = xgene_dma_free_chan_resources;
1529 dma_dev->device_issue_pending = xgene_dma_issue_pending;
1530 dma_dev->device_tx_status = xgene_dma_tx_status;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301531
1532 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1533 dma_dev->device_prep_dma_xor = xgene_dma_prep_xor;
1534 dma_dev->max_xor = XGENE_DMA_MAX_XOR_SRC;
Maxime Ripard77a68e52015-07-20 10:41:32 +02001535 dma_dev->xor_align = DMAENGINE_ALIGN_64_BYTES;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301536 }
1537
1538 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
1539 dma_dev->device_prep_dma_pq = xgene_dma_prep_pq;
1540 dma_dev->max_pq = XGENE_DMA_MAX_XOR_SRC;
Maxime Ripard77a68e52015-07-20 10:41:32 +02001541 dma_dev->pq_align = DMAENGINE_ALIGN_64_BYTES;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301542 }
1543}
1544
1545static int xgene_dma_async_register(struct xgene_dma *pdma, int id)
1546{
1547 struct xgene_dma_chan *chan = &pdma->chan[id];
1548 struct dma_device *dma_dev = &pdma->dma_dev[id];
1549 int ret;
1550
1551 chan->dma_chan.device = dma_dev;
1552
1553 spin_lock_init(&chan->lock);
1554 INIT_LIST_HEAD(&chan->ld_pending);
1555 INIT_LIST_HEAD(&chan->ld_running);
1556 INIT_LIST_HEAD(&chan->ld_completed);
1557 tasklet_init(&chan->tasklet, xgene_dma_tasklet_cb,
1558 (unsigned long)chan);
1559
1560 chan->pending = 0;
1561 chan->desc_pool = NULL;
1562 dma_cookie_init(&chan->dma_chan);
1563
1564 /* Setup dma device capabilities and prep routines */
1565 xgene_dma_set_caps(chan, dma_dev);
1566
1567 /* Initialize DMA device list head */
1568 INIT_LIST_HEAD(&dma_dev->channels);
1569 list_add_tail(&chan->dma_chan.device_node, &dma_dev->channels);
1570
1571 /* Register with Linux async DMA framework*/
1572 ret = dma_async_device_register(dma_dev);
1573 if (ret) {
1574 chan_err(chan, "Failed to register async device %d", ret);
1575 tasklet_kill(&chan->tasklet);
1576
1577 return ret;
1578 }
1579
1580 /* DMA capability info */
1581 dev_info(pdma->dev,
Dave Jiangc678fa62017-08-21 10:23:13 -07001582 "%s: CAPABILITY ( %s%s)\n", dma_chan_name(&chan->dma_chan),
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301583 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "XOR " : "",
1584 dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "PQ " : "");
1585
1586 return 0;
1587}
1588
1589static int xgene_dma_init_async(struct xgene_dma *pdma)
1590{
1591 int ret, i, j;
1592
1593 for (i = 0; i < XGENE_DMA_MAX_CHANNEL ; i++) {
1594 ret = xgene_dma_async_register(pdma, i);
1595 if (ret) {
1596 for (j = 0; j < i; j++) {
1597 dma_async_device_unregister(&pdma->dma_dev[j]);
1598 tasklet_kill(&pdma->chan[j].tasklet);
1599 }
1600
1601 return ret;
1602 }
1603 }
1604
1605 return ret;
1606}
1607
1608static void xgene_dma_async_unregister(struct xgene_dma *pdma)
1609{
1610 int i;
1611
1612 for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
1613 dma_async_device_unregister(&pdma->dma_dev[i]);
1614}
1615
1616static void xgene_dma_init_channels(struct xgene_dma *pdma)
1617{
1618 struct xgene_dma_chan *chan;
1619 int i;
1620
1621 pdma->ring_num = XGENE_DMA_RING_NUM;
1622
1623 for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1624 chan = &pdma->chan[i];
1625 chan->dev = pdma->dev;
1626 chan->pdma = pdma;
1627 chan->id = i;
Dan Carpentered1f0412015-04-09 12:05:04 +03001628 snprintf(chan->name, sizeof(chan->name), "dmachan%d", chan->id);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301629 }
1630}
1631
1632static int xgene_dma_get_resources(struct platform_device *pdev,
1633 struct xgene_dma *pdma)
1634{
1635 struct resource *res;
1636 int irq, i;
1637
1638 /* Get DMA csr region */
1639 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1640 if (!res) {
1641 dev_err(&pdev->dev, "Failed to get csr region\n");
1642 return -ENXIO;
1643 }
1644
1645 pdma->csr_dma = devm_ioremap(&pdev->dev, res->start,
1646 resource_size(res));
Dan Carpenter9c361b12015-04-09 12:03:31 +03001647 if (!pdma->csr_dma) {
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301648 dev_err(&pdev->dev, "Failed to ioremap csr region");
Dan Carpenter9c361b12015-04-09 12:03:31 +03001649 return -ENOMEM;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301650 }
1651
1652 /* Get DMA ring csr region */
1653 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1654 if (!res) {
1655 dev_err(&pdev->dev, "Failed to get ring csr region\n");
1656 return -ENXIO;
1657 }
1658
1659 pdma->csr_ring = devm_ioremap(&pdev->dev, res->start,
1660 resource_size(res));
Dan Carpenter9c361b12015-04-09 12:03:31 +03001661 if (!pdma->csr_ring) {
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301662 dev_err(&pdev->dev, "Failed to ioremap ring csr region");
Dan Carpenter9c361b12015-04-09 12:03:31 +03001663 return -ENOMEM;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301664 }
1665
1666 /* Get DMA ring cmd csr region */
1667 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1668 if (!res) {
1669 dev_err(&pdev->dev, "Failed to get ring cmd csr region\n");
1670 return -ENXIO;
1671 }
1672
1673 pdma->csr_ring_cmd = devm_ioremap(&pdev->dev, res->start,
1674 resource_size(res));
Dan Carpenter9c361b12015-04-09 12:03:31 +03001675 if (!pdma->csr_ring_cmd) {
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301676 dev_err(&pdev->dev, "Failed to ioremap ring cmd csr region");
Dan Carpenter9c361b12015-04-09 12:03:31 +03001677 return -ENOMEM;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301678 }
1679
Rameshwar Prasad Sahucda8e932015-07-07 15:34:25 +05301680 pdma->csr_ring_cmd += XGENE_DMA_RING_CMD_SM_OFFSET;
1681
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301682 /* Get efuse csr region */
1683 res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1684 if (!res) {
1685 dev_err(&pdev->dev, "Failed to get efuse csr region\n");
1686 return -ENXIO;
1687 }
1688
1689 pdma->csr_efuse = devm_ioremap(&pdev->dev, res->start,
1690 resource_size(res));
Dan Carpenter9c361b12015-04-09 12:03:31 +03001691 if (!pdma->csr_efuse) {
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301692 dev_err(&pdev->dev, "Failed to ioremap efuse csr region");
Dan Carpenter9c361b12015-04-09 12:03:31 +03001693 return -ENOMEM;
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301694 }
1695
1696 /* Get DMA error interrupt */
1697 irq = platform_get_irq(pdev, 0);
1698 if (irq <= 0) {
1699 dev_err(&pdev->dev, "Failed to get Error IRQ\n");
1700 return -ENXIO;
1701 }
1702
1703 pdma->err_irq = irq;
1704
1705 /* Get DMA Rx ring descriptor interrupts for all DMA channels */
1706 for (i = 1; i <= XGENE_DMA_MAX_CHANNEL; i++) {
1707 irq = platform_get_irq(pdev, i);
1708 if (irq <= 0) {
1709 dev_err(&pdev->dev, "Failed to get Rx IRQ\n");
1710 return -ENXIO;
1711 }
1712
1713 pdma->chan[i - 1].rx_irq = irq;
1714 }
1715
1716 return 0;
1717}
1718
1719static int xgene_dma_probe(struct platform_device *pdev)
1720{
1721 struct xgene_dma *pdma;
1722 int ret, i;
1723
1724 pdma = devm_kzalloc(&pdev->dev, sizeof(*pdma), GFP_KERNEL);
1725 if (!pdma)
1726 return -ENOMEM;
1727
1728 pdma->dev = &pdev->dev;
1729 platform_set_drvdata(pdev, pdma);
1730
1731 ret = xgene_dma_get_resources(pdev, pdma);
1732 if (ret)
1733 return ret;
1734
1735 pdma->clk = devm_clk_get(&pdev->dev, NULL);
Rameshwar Prasad Sahu89079492015-07-21 18:44:39 +05301736 if (IS_ERR(pdma->clk) && !ACPI_COMPANION(&pdev->dev)) {
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301737 dev_err(&pdev->dev, "Failed to get clk\n");
1738 return PTR_ERR(pdma->clk);
1739 }
1740
1741 /* Enable clk before accessing registers */
Rameshwar Prasad Sahu89079492015-07-21 18:44:39 +05301742 if (!IS_ERR(pdma->clk)) {
1743 ret = clk_prepare_enable(pdma->clk);
1744 if (ret) {
1745 dev_err(&pdev->dev, "Failed to enable clk %d\n", ret);
1746 return ret;
1747 }
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301748 }
1749
1750 /* Remove DMA RAM out of shutdown */
1751 ret = xgene_dma_init_mem(pdma);
1752 if (ret)
1753 goto err_clk_enable;
1754
1755 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(42));
1756 if (ret) {
1757 dev_err(&pdev->dev, "No usable DMA configuration\n");
1758 goto err_dma_mask;
1759 }
1760
1761 /* Initialize DMA channels software state */
1762 xgene_dma_init_channels(pdma);
1763
1764 /* Configue DMA rings */
1765 ret = xgene_dma_init_rings(pdma);
1766 if (ret)
1767 goto err_clk_enable;
1768
1769 ret = xgene_dma_request_irqs(pdma);
1770 if (ret)
1771 goto err_request_irq;
1772
1773 /* Configure and enable DMA engine */
1774 xgene_dma_init_hw(pdma);
1775
1776 /* Register DMA device with linux async framework */
1777 ret = xgene_dma_init_async(pdma);
1778 if (ret)
1779 goto err_async_init;
1780
1781 return 0;
1782
1783err_async_init:
1784 xgene_dma_free_irqs(pdma);
1785
1786err_request_irq:
1787 for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
1788 xgene_dma_delete_chan_rings(&pdma->chan[i]);
1789
1790err_dma_mask:
1791err_clk_enable:
Rameshwar Prasad Sahu89079492015-07-21 18:44:39 +05301792 if (!IS_ERR(pdma->clk))
1793 clk_disable_unprepare(pdma->clk);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301794
1795 return ret;
1796}
1797
1798static int xgene_dma_remove(struct platform_device *pdev)
1799{
1800 struct xgene_dma *pdma = platform_get_drvdata(pdev);
1801 struct xgene_dma_chan *chan;
1802 int i;
1803
1804 xgene_dma_async_unregister(pdma);
1805
1806 /* Mask interrupts and disable DMA engine */
1807 xgene_dma_mask_interrupts(pdma);
1808 xgene_dma_disable(pdma);
1809 xgene_dma_free_irqs(pdma);
1810
1811 for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
1812 chan = &pdma->chan[i];
1813 tasklet_kill(&chan->tasklet);
1814 xgene_dma_delete_chan_rings(chan);
1815 }
1816
Rameshwar Prasad Sahu89079492015-07-21 18:44:39 +05301817 if (!IS_ERR(pdma->clk))
1818 clk_disable_unprepare(pdma->clk);
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301819
1820 return 0;
1821}
1822
Rameshwar Prasad Sahu89079492015-07-21 18:44:39 +05301823#ifdef CONFIG_ACPI
1824static const struct acpi_device_id xgene_dma_acpi_match_ptr[] = {
1825 {"APMC0D43", 0},
1826 {},
1827};
1828MODULE_DEVICE_TABLE(acpi, xgene_dma_acpi_match_ptr);
1829#endif
1830
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301831static const struct of_device_id xgene_dma_of_match_ptr[] = {
1832 {.compatible = "apm,xgene-storm-dma",},
1833 {},
1834};
1835MODULE_DEVICE_TABLE(of, xgene_dma_of_match_ptr);
1836
1837static struct platform_driver xgene_dma_driver = {
1838 .probe = xgene_dma_probe,
1839 .remove = xgene_dma_remove,
1840 .driver = {
1841 .name = "X-Gene-DMA",
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301842 .of_match_table = xgene_dma_of_match_ptr,
Rameshwar Prasad Sahu89079492015-07-21 18:44:39 +05301843 .acpi_match_table = ACPI_PTR(xgene_dma_acpi_match_ptr),
Rameshwar Prasad Sahu9f2fd0d2015-03-18 19:17:34 +05301844 },
1845};
1846
1847module_platform_driver(xgene_dma_driver);
1848
1849MODULE_DESCRIPTION("APM X-Gene SoC DMA driver");
1850MODULE_AUTHOR("Rameshwar Prasad Sahu <rsahu@apm.com>");
1851MODULE_AUTHOR("Loc Ho <lho@apm.com>");
1852MODULE_LICENSE("GPL");
1853MODULE_VERSION("1.0");