xref: /linux/drivers/dma/pl330.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *		http://www.samsung.com
5  *
6  * Copyright (C) 2010 Samsung Electronics Co. Ltd.
7  *	Jaswinder Singh <jassi.brar@samsung.com>
8  */
9 
10 #include <linux/debugfs.h>
11 #include <linux/kernel.h>
12 #include <linux/io.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/dmaengine.h>
21 #include <linux/amba/bus.h>
22 #include <linux/scatterlist.h>
23 #include <linux/of.h>
24 #include <linux/of_dma.h>
25 #include <linux/err.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/bug.h>
28 #include <linux/reset.h>
29 
30 #include "dmaengine.h"
31 #define PL330_MAX_CHAN		8
32 #define PL330_MAX_IRQS		32
33 #define PL330_MAX_PERI		32
34 #define PL330_MAX_BURST         16
35 
36 #define PL330_QUIRK_BROKEN_NO_FLUSHP	BIT(0)
37 #define PL330_QUIRK_PERIPH_BURST	BIT(1)
38 
39 enum pl330_cachectrl {
40 	CCTRL0,		/* Noncacheable and nonbufferable */
41 	CCTRL1,		/* Bufferable only */
42 	CCTRL2,		/* Cacheable, but do not allocate */
43 	CCTRL3,		/* Cacheable and bufferable, but do not allocate */
44 	INVALID1,	/* AWCACHE = 0x1000 */
45 	INVALID2,
46 	CCTRL6,		/* Cacheable write-through, allocate on writes only */
47 	CCTRL7,		/* Cacheable write-back, allocate on writes only */
48 };
49 
50 enum pl330_byteswap {
51 	SWAP_NO,
52 	SWAP_2,
53 	SWAP_4,
54 	SWAP_8,
55 	SWAP_16,
56 };
57 
58 /* Register and Bit field Definitions */
59 #define DS			0x0
60 #define DS_ST_STOP		0x0
61 #define DS_ST_EXEC		0x1
62 #define DS_ST_CMISS		0x2
63 #define DS_ST_UPDTPC		0x3
64 #define DS_ST_WFE		0x4
65 #define DS_ST_ATBRR		0x5
66 #define DS_ST_QBUSY		0x6
67 #define DS_ST_WFP		0x7
68 #define DS_ST_KILL		0x8
69 #define DS_ST_CMPLT		0x9
70 #define DS_ST_FLTCMP		0xe
71 #define DS_ST_FAULT		0xf
72 
73 #define DPC			0x4
74 #define INTEN			0x20
75 #define ES			0x24
76 #define INTSTATUS		0x28
77 #define INTCLR			0x2c
78 #define FSM			0x30
79 #define FSC			0x34
80 #define FTM			0x38
81 
82 #define _FTC			0x40
83 #define FTC(n)			(_FTC + (n)*0x4)
84 
85 #define _CS			0x100
86 #define CS(n)			(_CS + (n)*0x8)
87 #define CS_CNS			(1 << 21)
88 
89 #define _CPC			0x104
90 #define CPC(n)			(_CPC + (n)*0x8)
91 
92 #define _SA			0x400
93 #define SA(n)			(_SA + (n)*0x20)
94 
95 #define _DA			0x404
96 #define DA(n)			(_DA + (n)*0x20)
97 
98 #define _CC			0x408
99 #define CC(n)			(_CC + (n)*0x20)
100 
101 #define CC_SRCINC		(1 << 0)
102 #define CC_DSTINC		(1 << 14)
103 #define CC_SRCPRI		(1 << 8)
104 #define CC_DSTPRI		(1 << 22)
105 #define CC_SRCNS		(1 << 9)
106 #define CC_DSTNS		(1 << 23)
107 #define CC_SRCIA		(1 << 10)
108 #define CC_DSTIA		(1 << 24)
109 #define CC_SRCBRSTLEN_SHFT	4
110 #define CC_DSTBRSTLEN_SHFT	18
111 #define CC_SRCBRSTSIZE_SHFT	1
112 #define CC_DSTBRSTSIZE_SHFT	15
113 #define CC_SRCCCTRL_SHFT	11
114 #define CC_SRCCCTRL_MASK	0x7
115 #define CC_DSTCCTRL_SHFT	25
116 #define CC_DRCCCTRL_MASK	0x7
117 #define CC_SWAP_SHFT		28
118 
119 #define _LC0			0x40c
120 #define LC0(n)			(_LC0 + (n)*0x20)
121 
122 #define _LC1			0x410
123 #define LC1(n)			(_LC1 + (n)*0x20)
124 
125 #define DBGSTATUS		0xd00
126 #define DBG_BUSY		(1 << 0)
127 
128 #define DBGCMD			0xd04
129 #define DBGINST0		0xd08
130 #define DBGINST1		0xd0c
131 
132 #define CR0			0xe00
133 #define CR1			0xe04
134 #define CR2			0xe08
135 #define CR3			0xe0c
136 #define CR4			0xe10
137 #define CRD			0xe14
138 
139 #define PERIPH_ID		0xfe0
140 #define PERIPH_REV_SHIFT	20
141 #define PERIPH_REV_MASK		0xf
142 #define PERIPH_REV_R0P0		0
143 #define PERIPH_REV_R1P0		1
144 #define PERIPH_REV_R1P1		2
145 
146 #define CR0_PERIPH_REQ_SET	(1 << 0)
147 #define CR0_BOOT_EN_SET		(1 << 1)
148 #define CR0_BOOT_MAN_NS		(1 << 2)
149 #define CR0_NUM_CHANS_SHIFT	4
150 #define CR0_NUM_CHANS_MASK	0x7
151 #define CR0_NUM_PERIPH_SHIFT	12
152 #define CR0_NUM_PERIPH_MASK	0x1f
153 #define CR0_NUM_EVENTS_SHIFT	17
154 #define CR0_NUM_EVENTS_MASK	0x1f
155 
156 #define CR1_ICACHE_LEN_SHIFT	0
157 #define CR1_ICACHE_LEN_MASK	0x7
158 #define CR1_NUM_ICACHELINES_SHIFT	4
159 #define CR1_NUM_ICACHELINES_MASK	0xf
160 
161 #define CRD_DATA_WIDTH_SHIFT	0
162 #define CRD_DATA_WIDTH_MASK	0x7
163 #define CRD_WR_CAP_SHIFT	4
164 #define CRD_WR_CAP_MASK		0x7
165 #define CRD_WR_Q_DEP_SHIFT	8
166 #define CRD_WR_Q_DEP_MASK	0xf
167 #define CRD_RD_CAP_SHIFT	12
168 #define CRD_RD_CAP_MASK		0x7
169 #define CRD_RD_Q_DEP_SHIFT	16
170 #define CRD_RD_Q_DEP_MASK	0xf
171 #define CRD_DATA_BUFF_SHIFT	20
172 #define CRD_DATA_BUFF_MASK	0x3ff
173 
174 #define PART			0x330
175 #define DESIGNER		0x41
176 #define REVISION		0x0
177 #define INTEG_CFG		0x0
178 #define PERIPH_ID_VAL		((PART << 0) | (DESIGNER << 12))
179 
180 #define PL330_STATE_STOPPED		(1 << 0)
181 #define PL330_STATE_EXECUTING		(1 << 1)
182 #define PL330_STATE_WFE			(1 << 2)
183 #define PL330_STATE_FAULTING		(1 << 3)
184 #define PL330_STATE_COMPLETING		(1 << 4)
185 #define PL330_STATE_WFP			(1 << 5)
186 #define PL330_STATE_KILLING		(1 << 6)
187 #define PL330_STATE_FAULT_COMPLETING	(1 << 7)
188 #define PL330_STATE_CACHEMISS		(1 << 8)
189 #define PL330_STATE_UPDTPC		(1 << 9)
190 #define PL330_STATE_ATBARRIER		(1 << 10)
191 #define PL330_STATE_QUEUEBUSY		(1 << 11)
192 #define PL330_STATE_INVALID		(1 << 15)
193 
194 #define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
195 				| PL330_STATE_WFE | PL330_STATE_FAULTING)
196 
197 #define CMD_DMAADDH		0x54
198 #define CMD_DMAEND		0x00
199 #define CMD_DMAFLUSHP		0x35
200 #define CMD_DMAGO		0xa0
201 #define CMD_DMALD		0x04
202 #define CMD_DMALDP		0x25
203 #define CMD_DMALP		0x20
204 #define CMD_DMALPEND		0x28
205 #define CMD_DMAKILL		0x01
206 #define CMD_DMAMOV		0xbc
207 #define CMD_DMANOP		0x18
208 #define CMD_DMARMB		0x12
209 #define CMD_DMASEV		0x34
210 #define CMD_DMAST		0x08
211 #define CMD_DMASTP		0x29
212 #define CMD_DMASTZ		0x0c
213 #define CMD_DMAWFE		0x36
214 #define CMD_DMAWFP		0x30
215 #define CMD_DMAWMB		0x13
216 
217 #define SZ_DMAADDH		3
218 #define SZ_DMAEND		1
219 #define SZ_DMAFLUSHP		2
220 #define SZ_DMALD		1
221 #define SZ_DMALDP		2
222 #define SZ_DMALP		2
223 #define SZ_DMALPEND		2
224 #define SZ_DMAKILL		1
225 #define SZ_DMAMOV		6
226 #define SZ_DMANOP		1
227 #define SZ_DMARMB		1
228 #define SZ_DMASEV		2
229 #define SZ_DMAST		1
230 #define SZ_DMASTP		2
231 #define SZ_DMASTZ		1
232 #define SZ_DMAWFE		2
233 #define SZ_DMAWFP		2
234 #define SZ_DMAWMB		1
235 #define SZ_DMAGO		6
236 
237 #define BRST_LEN(ccr)		((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
238 #define BRST_SIZE(ccr)		(1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
239 
240 #define BYTE_TO_BURST(b, ccr)	((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
241 #define BURST_TO_BYTE(c, ccr)	((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
242 
243 /*
244  * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
245  * at 1byte/burst for P<->M and M<->M respectively.
246  * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
247  * should be enough for P<->M and M<->M respectively.
248  */
249 #define MCODE_BUFF_PER_REQ	256
250 
251 /* Use this _only_ to wait on transient states */
252 #define UNTIL(t, s)	while (!(_state(t) & (s))) cpu_relax();
253 
254 #ifdef PL330_DEBUG_MCGEN
255 static unsigned cmd_line;
256 #define PL330_DBGCMD_DUMP(off, x...)	do { \
257 						printk("%x:", cmd_line); \
258 						printk(KERN_CONT x); \
259 						cmd_line += off; \
260 					} while (0)
261 #define PL330_DBGMC_START(addr)		(cmd_line = addr)
262 #else
263 #define PL330_DBGCMD_DUMP(off, x...)	do {} while (0)
264 #define PL330_DBGMC_START(addr)		do {} while (0)
265 #endif
266 
267 /* The number of default descriptors */
268 
269 #define NR_DEFAULT_DESC	16
270 
271 /* Delay for runtime PM autosuspend, ms */
272 #define PL330_AUTOSUSPEND_DELAY 20
273 
274 /* Populated by the PL330 core driver for DMA API driver's info */
275 struct pl330_config {
276 	u32	periph_id;
277 #define DMAC_MODE_NS	(1 << 0)
278 	unsigned int	mode;
279 	unsigned int	data_bus_width:10; /* In number of bits */
280 	unsigned int	data_buf_dep:11;
281 	unsigned int	num_chan:4;
282 	unsigned int	num_peri:6;
283 	u32		peri_ns;
284 	unsigned int	num_events:6;
285 	u32		irq_ns;
286 };
287 
288 /*
289  * Request Configuration.
290  * The PL330 core does not modify this and uses the last
291  * working configuration if the request doesn't provide any.
292  *
293  * The Client may want to provide this info only for the
294  * first request and a request with new settings.
295  */
296 struct pl330_reqcfg {
297 	/* Address Incrementing */
298 	unsigned dst_inc:1;
299 	unsigned src_inc:1;
300 
301 	/*
302 	 * For now, the SRC & DST protection levels
303 	 * and burst size/length are assumed same.
304 	 */
305 	bool nonsecure;
306 	bool privileged;
307 	bool insnaccess;
308 	unsigned brst_len:5;
309 	unsigned brst_size:3; /* in power of 2 */
310 
311 	enum pl330_cachectrl dcctl;
312 	enum pl330_cachectrl scctl;
313 	enum pl330_byteswap swap;
314 	struct pl330_config *pcfg;
315 };
316 
317 /*
318  * One cycle of DMAC operation.
319  * There may be more than one xfer in a request.
320  */
321 struct pl330_xfer {
322 	u32 src_addr;
323 	u32 dst_addr;
324 	/* Size to xfer */
325 	u32 bytes;
326 };
327 
328 /* The xfer callbacks are made with one of these arguments. */
329 enum pl330_op_err {
330 	/* The all xfers in the request were success. */
331 	PL330_ERR_NONE,
332 	/* If req aborted due to global error. */
333 	PL330_ERR_ABORT,
334 	/* If req failed due to problem with Channel. */
335 	PL330_ERR_FAIL,
336 };
337 
338 enum dmamov_dst {
339 	SAR = 0,
340 	CCR,
341 	DAR,
342 };
343 
344 enum pl330_dst {
345 	SRC = 0,
346 	DST,
347 };
348 
349 enum pl330_cond {
350 	SINGLE,
351 	BURST,
352 	ALWAYS,
353 };
354 
355 struct dma_pl330_desc;
356 
357 struct _pl330_req {
358 	u32 mc_bus;
359 	void *mc_cpu;
360 	struct dma_pl330_desc *desc;
361 };
362 
363 /* ToBeDone for tasklet */
364 struct _pl330_tbd {
365 	bool reset_dmac;
366 	bool reset_mngr;
367 	u8 reset_chan;
368 };
369 
370 /* A DMAC Thread */
371 struct pl330_thread {
372 	u8 id;
373 	int ev;
374 	/* If the channel is not yet acquired by any client */
375 	bool free;
376 	/* Parent DMAC */
377 	struct pl330_dmac *dmac;
378 	/* Only two at a time */
379 	struct _pl330_req req[2];
380 	/* Index of the last enqueued request */
381 	unsigned lstenq;
382 	/* Index of the last submitted request or -1 if the DMA is stopped */
383 	int req_running;
384 };
385 
386 enum pl330_dmac_state {
387 	UNINIT,
388 	INIT,
389 	DYING,
390 };
391 
392 enum desc_status {
393 	/* In the DMAC pool */
394 	FREE,
395 	/*
396 	 * Allocated to some channel during prep_xxx
397 	 * Also may be sitting on the work_list.
398 	 */
399 	PREP,
400 	/*
401 	 * Sitting on the work_list and already submitted
402 	 * to the PL330 core. Not more than two descriptors
403 	 * of a channel can be BUSY at any time.
404 	 */
405 	BUSY,
406 	/*
407 	 * Pause was called while descriptor was BUSY. Due to hardware
408 	 * limitations, only termination is possible for descriptors
409 	 * that have been paused.
410 	 */
411 	PAUSED,
412 	/*
413 	 * Sitting on the channel work_list but xfer done
414 	 * by PL330 core
415 	 */
416 	DONE,
417 };
418 
419 struct dma_pl330_chan {
420 	/* Schedule desc completion */
421 	struct tasklet_struct task;
422 
423 	/* DMA-Engine Channel */
424 	struct dma_chan chan;
425 
426 	/* List of submitted descriptors */
427 	struct list_head submitted_list;
428 	/* List of issued descriptors */
429 	struct list_head work_list;
430 	/* List of completed descriptors */
431 	struct list_head completed_list;
432 
433 	/* Pointer to the DMAC that manages this channel,
434 	 * NULL if the channel is available to be acquired.
435 	 * As the parent, this DMAC also provides descriptors
436 	 * to the channel.
437 	 */
438 	struct pl330_dmac *dmac;
439 
440 	/* To protect channel manipulation */
441 	spinlock_t lock;
442 
443 	/*
444 	 * Hardware channel thread of PL330 DMAC. NULL if the channel is
445 	 * available.
446 	 */
447 	struct pl330_thread *thread;
448 
449 	/* For D-to-M and M-to-D channels */
450 	int burst_sz; /* the peripheral fifo width */
451 	int burst_len; /* the number of burst */
452 	phys_addr_t fifo_addr;
453 	/* DMA-mapped view of the FIFO; may differ if an IOMMU is present */
454 	dma_addr_t fifo_dma;
455 	enum dma_data_direction dir;
456 	struct dma_slave_config slave_config;
457 
458 	/* for cyclic capability */
459 	bool cyclic;
460 
461 	/* for runtime pm tracking */
462 	bool active;
463 };
464 
465 struct pl330_dmac {
466 	/* DMA-Engine Device */
467 	struct dma_device ddma;
468 
469 	/* Pool of descriptors available for the DMAC's channels */
470 	struct list_head desc_pool;
471 	/* To protect desc_pool manipulation */
472 	spinlock_t pool_lock;
473 
474 	/* Size of MicroCode buffers for each channel. */
475 	unsigned mcbufsz;
476 	/* ioremap'ed address of PL330 registers. */
477 	void __iomem	*base;
478 	/* Populated by the PL330 core driver during pl330_add */
479 	struct pl330_config	pcfg;
480 
481 	spinlock_t		lock;
482 	/* Maximum possible events/irqs */
483 	int			events[32];
484 	/* BUS address of MicroCode buffer */
485 	dma_addr_t		mcode_bus;
486 	/* CPU address of MicroCode buffer */
487 	void			*mcode_cpu;
488 	/* List of all Channel threads */
489 	struct pl330_thread	*channels;
490 	/* Pointer to the MANAGER thread */
491 	struct pl330_thread	*manager;
492 	/* To handle bad news in interrupt */
493 	struct tasklet_struct	tasks;
494 	struct _pl330_tbd	dmac_tbd;
495 	/* State of DMAC operation */
496 	enum pl330_dmac_state	state;
497 	/* Holds list of reqs with due callbacks */
498 	struct list_head        req_done;
499 
500 	/* Peripheral channels connected to this DMAC */
501 	unsigned int num_peripherals;
502 	struct dma_pl330_chan *peripherals; /* keep at end */
503 	int quirks;
504 
505 	struct reset_control	*rstc;
506 	struct reset_control	*rstc_ocp;
507 };
508 
509 static struct pl330_of_quirks {
510 	char *quirk;
511 	int id;
512 } of_quirks[] = {
513 	{
514 		.quirk = "arm,pl330-broken-no-flushp",
515 		.id = PL330_QUIRK_BROKEN_NO_FLUSHP,
516 	},
517 	{
518 		.quirk = "arm,pl330-periph-burst",
519 		.id = PL330_QUIRK_PERIPH_BURST,
520 	}
521 };
522 
523 struct dma_pl330_desc {
524 	/* To attach to a queue as child */
525 	struct list_head node;
526 
527 	/* Descriptor for the DMA Engine API */
528 	struct dma_async_tx_descriptor txd;
529 
530 	/* Xfer for PL330 core */
531 	struct pl330_xfer px;
532 
533 	struct pl330_reqcfg rqcfg;
534 
535 	enum desc_status status;
536 
537 	int bytes_requested;
538 	bool last;
539 
540 	/* The channel which currently holds this desc */
541 	struct dma_pl330_chan *pchan;
542 
543 	enum dma_transfer_direction rqtype;
544 	/* Index of peripheral for the xfer. */
545 	unsigned peri:5;
546 	/* Hook to attach to DMAC's list of reqs with due callback */
547 	struct list_head rqd;
548 };
549 
550 struct _xfer_spec {
551 	u32 ccr;
552 	struct dma_pl330_desc *desc;
553 };
554 
555 static int pl330_config_write(struct dma_chan *chan,
556 			struct dma_slave_config *slave_config,
557 			enum dma_transfer_direction direction);
558 
559 static inline bool _queue_full(struct pl330_thread *thrd)
560 {
561 	return thrd->req[0].desc != NULL && thrd->req[1].desc != NULL;
562 }
563 
564 static inline bool is_manager(struct pl330_thread *thrd)
565 {
566 	return thrd->dmac->manager == thrd;
567 }
568 
569 /* If manager of the thread is in Non-Secure mode */
570 static inline bool _manager_ns(struct pl330_thread *thrd)
571 {
572 	return (thrd->dmac->pcfg.mode & DMAC_MODE_NS) ? true : false;
573 }
574 
575 static inline u32 get_revision(u32 periph_id)
576 {
577 	return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
578 }
579 
580 static inline u32 _emit_END(unsigned dry_run, u8 buf[])
581 {
582 	if (dry_run)
583 		return SZ_DMAEND;
584 
585 	buf[0] = CMD_DMAEND;
586 
587 	PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
588 
589 	return SZ_DMAEND;
590 }
591 
592 static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
593 {
594 	if (dry_run)
595 		return SZ_DMAFLUSHP;
596 
597 	buf[0] = CMD_DMAFLUSHP;
598 
599 	peri &= 0x1f;
600 	peri <<= 3;
601 	buf[1] = peri;
602 
603 	PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
604 
605 	return SZ_DMAFLUSHP;
606 }
607 
608 static inline u32 _emit_LD(unsigned dry_run, u8 buf[],	enum pl330_cond cond)
609 {
610 	if (dry_run)
611 		return SZ_DMALD;
612 
613 	buf[0] = CMD_DMALD;
614 
615 	if (cond == SINGLE)
616 		buf[0] |= (0 << 1) | (1 << 0);
617 	else if (cond == BURST)
618 		buf[0] |= (1 << 1) | (1 << 0);
619 
620 	PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
621 		cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
622 
623 	return SZ_DMALD;
624 }
625 
626 static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
627 		enum pl330_cond cond, u8 peri)
628 {
629 	if (dry_run)
630 		return SZ_DMALDP;
631 
632 	buf[0] = CMD_DMALDP;
633 
634 	if (cond == BURST)
635 		buf[0] |= (1 << 1);
636 
637 	peri &= 0x1f;
638 	peri <<= 3;
639 	buf[1] = peri;
640 
641 	PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
642 		cond == SINGLE ? 'S' : 'B', peri >> 3);
643 
644 	return SZ_DMALDP;
645 }
646 
647 static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
648 		unsigned loop, u8 cnt)
649 {
650 	if (dry_run)
651 		return SZ_DMALP;
652 
653 	buf[0] = CMD_DMALP;
654 
655 	if (loop)
656 		buf[0] |= (1 << 1);
657 
658 	cnt--; /* DMAC increments by 1 internally */
659 	buf[1] = cnt;
660 
661 	PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
662 
663 	return SZ_DMALP;
664 }
665 
666 struct _arg_LPEND {
667 	enum pl330_cond cond;
668 	bool forever;
669 	unsigned loop;
670 	u8 bjump;
671 };
672 
673 static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
674 		const struct _arg_LPEND *arg)
675 {
676 	enum pl330_cond cond = arg->cond;
677 	bool forever = arg->forever;
678 	unsigned loop = arg->loop;
679 	u8 bjump = arg->bjump;
680 
681 	if (dry_run)
682 		return SZ_DMALPEND;
683 
684 	buf[0] = CMD_DMALPEND;
685 
686 	if (loop)
687 		buf[0] |= (1 << 2);
688 
689 	if (!forever)
690 		buf[0] |= (1 << 4);
691 
692 	if (cond == SINGLE)
693 		buf[0] |= (0 << 1) | (1 << 0);
694 	else if (cond == BURST)
695 		buf[0] |= (1 << 1) | (1 << 0);
696 
697 	buf[1] = bjump;
698 
699 	PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
700 			forever ? "FE" : "END",
701 			cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
702 			loop ? '1' : '0',
703 			bjump);
704 
705 	return SZ_DMALPEND;
706 }
707 
708 static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
709 {
710 	if (dry_run)
711 		return SZ_DMAKILL;
712 
713 	buf[0] = CMD_DMAKILL;
714 
715 	return SZ_DMAKILL;
716 }
717 
718 static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
719 		enum dmamov_dst dst, u32 val)
720 {
721 	if (dry_run)
722 		return SZ_DMAMOV;
723 
724 	buf[0] = CMD_DMAMOV;
725 	buf[1] = dst;
726 	buf[2] = val;
727 	buf[3] = val >> 8;
728 	buf[4] = val >> 16;
729 	buf[5] = val >> 24;
730 
731 	PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
732 		dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
733 
734 	return SZ_DMAMOV;
735 }
736 
737 static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
738 {
739 	if (dry_run)
740 		return SZ_DMARMB;
741 
742 	buf[0] = CMD_DMARMB;
743 
744 	PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
745 
746 	return SZ_DMARMB;
747 }
748 
749 static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
750 {
751 	if (dry_run)
752 		return SZ_DMASEV;
753 
754 	buf[0] = CMD_DMASEV;
755 
756 	ev &= 0x1f;
757 	ev <<= 3;
758 	buf[1] = ev;
759 
760 	PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
761 
762 	return SZ_DMASEV;
763 }
764 
765 static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
766 {
767 	if (dry_run)
768 		return SZ_DMAST;
769 
770 	buf[0] = CMD_DMAST;
771 
772 	if (cond == SINGLE)
773 		buf[0] |= (0 << 1) | (1 << 0);
774 	else if (cond == BURST)
775 		buf[0] |= (1 << 1) | (1 << 0);
776 
777 	PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
778 		cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
779 
780 	return SZ_DMAST;
781 }
782 
783 static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
784 		enum pl330_cond cond, u8 peri)
785 {
786 	if (dry_run)
787 		return SZ_DMASTP;
788 
789 	buf[0] = CMD_DMASTP;
790 
791 	if (cond == BURST)
792 		buf[0] |= (1 << 1);
793 
794 	peri &= 0x1f;
795 	peri <<= 3;
796 	buf[1] = peri;
797 
798 	PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
799 		cond == SINGLE ? 'S' : 'B', peri >> 3);
800 
801 	return SZ_DMASTP;
802 }
803 
804 static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
805 		enum pl330_cond cond, u8 peri)
806 {
807 	if (dry_run)
808 		return SZ_DMAWFP;
809 
810 	buf[0] = CMD_DMAWFP;
811 
812 	if (cond == SINGLE)
813 		buf[0] |= (0 << 1) | (0 << 0);
814 	else if (cond == BURST)
815 		buf[0] |= (1 << 1) | (0 << 0);
816 	else
817 		buf[0] |= (0 << 1) | (1 << 0);
818 
819 	peri &= 0x1f;
820 	peri <<= 3;
821 	buf[1] = peri;
822 
823 	PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
824 		cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
825 
826 	return SZ_DMAWFP;
827 }
828 
829 static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
830 {
831 	if (dry_run)
832 		return SZ_DMAWMB;
833 
834 	buf[0] = CMD_DMAWMB;
835 
836 	PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
837 
838 	return SZ_DMAWMB;
839 }
840 
841 struct _arg_GO {
842 	u8 chan;
843 	u32 addr;
844 	unsigned ns;
845 };
846 
847 static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
848 		const struct _arg_GO *arg)
849 {
850 	u8 chan = arg->chan;
851 	u32 addr = arg->addr;
852 	unsigned ns = arg->ns;
853 
854 	if (dry_run)
855 		return SZ_DMAGO;
856 
857 	buf[0] = CMD_DMAGO;
858 	buf[0] |= (ns << 1);
859 	buf[1] = chan & 0x7;
860 	buf[2] = addr;
861 	buf[3] = addr >> 8;
862 	buf[4] = addr >> 16;
863 	buf[5] = addr >> 24;
864 
865 	return SZ_DMAGO;
866 }
867 
868 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
869 
870 /* Returns Time-Out */
871 static bool _until_dmac_idle(struct pl330_thread *thrd)
872 {
873 	void __iomem *regs = thrd->dmac->base;
874 	unsigned long loops = msecs_to_loops(5);
875 
876 	do {
877 		/* Until Manager is Idle */
878 		if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
879 			break;
880 
881 		cpu_relax();
882 	} while (--loops);
883 
884 	if (!loops)
885 		return true;
886 
887 	return false;
888 }
889 
890 static inline void _execute_DBGINSN(struct pl330_thread *thrd,
891 		u8 insn[], bool as_manager)
892 {
893 	void __iomem *regs = thrd->dmac->base;
894 	u32 val;
895 
896 	/* If timed out due to halted state-machine */
897 	if (_until_dmac_idle(thrd)) {
898 		dev_err(thrd->dmac->ddma.dev, "DMAC halted!\n");
899 		return;
900 	}
901 
902 	val = (insn[0] << 16) | (insn[1] << 24);
903 	if (!as_manager) {
904 		val |= (1 << 0);
905 		val |= (thrd->id << 8); /* Channel Number */
906 	}
907 	writel(val, regs + DBGINST0);
908 
909 	val = le32_to_cpu(*((__le32 *)&insn[2]));
910 	writel(val, regs + DBGINST1);
911 
912 	/* Get going */
913 	writel(0, regs + DBGCMD);
914 }
915 
916 static inline u32 _state(struct pl330_thread *thrd)
917 {
918 	void __iomem *regs = thrd->dmac->base;
919 	u32 val;
920 
921 	if (is_manager(thrd))
922 		val = readl(regs + DS) & 0xf;
923 	else
924 		val = readl(regs + CS(thrd->id)) & 0xf;
925 
926 	switch (val) {
927 	case DS_ST_STOP:
928 		return PL330_STATE_STOPPED;
929 	case DS_ST_EXEC:
930 		return PL330_STATE_EXECUTING;
931 	case DS_ST_CMISS:
932 		return PL330_STATE_CACHEMISS;
933 	case DS_ST_UPDTPC:
934 		return PL330_STATE_UPDTPC;
935 	case DS_ST_WFE:
936 		return PL330_STATE_WFE;
937 	case DS_ST_FAULT:
938 		return PL330_STATE_FAULTING;
939 	case DS_ST_ATBRR:
940 		if (is_manager(thrd))
941 			return PL330_STATE_INVALID;
942 		else
943 			return PL330_STATE_ATBARRIER;
944 	case DS_ST_QBUSY:
945 		if (is_manager(thrd))
946 			return PL330_STATE_INVALID;
947 		else
948 			return PL330_STATE_QUEUEBUSY;
949 	case DS_ST_WFP:
950 		if (is_manager(thrd))
951 			return PL330_STATE_INVALID;
952 		else
953 			return PL330_STATE_WFP;
954 	case DS_ST_KILL:
955 		if (is_manager(thrd))
956 			return PL330_STATE_INVALID;
957 		else
958 			return PL330_STATE_KILLING;
959 	case DS_ST_CMPLT:
960 		if (is_manager(thrd))
961 			return PL330_STATE_INVALID;
962 		else
963 			return PL330_STATE_COMPLETING;
964 	case DS_ST_FLTCMP:
965 		if (is_manager(thrd))
966 			return PL330_STATE_INVALID;
967 		else
968 			return PL330_STATE_FAULT_COMPLETING;
969 	default:
970 		return PL330_STATE_INVALID;
971 	}
972 }
973 
974 static void _stop(struct pl330_thread *thrd)
975 {
976 	void __iomem *regs = thrd->dmac->base;
977 	u8 insn[6] = {0, 0, 0, 0, 0, 0};
978 	u32 inten = readl(regs + INTEN);
979 
980 	if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
981 		UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
982 
983 	/* Return if nothing needs to be done */
984 	if (_state(thrd) == PL330_STATE_COMPLETING
985 		  || _state(thrd) == PL330_STATE_KILLING
986 		  || _state(thrd) == PL330_STATE_STOPPED)
987 		return;
988 
989 	_emit_KILL(0, insn);
990 
991 	_execute_DBGINSN(thrd, insn, is_manager(thrd));
992 
993 	/* clear the event */
994 	if (inten & (1 << thrd->ev))
995 		writel(1 << thrd->ev, regs + INTCLR);
996 	/* Stop generating interrupts for SEV */
997 	writel(inten & ~(1 << thrd->ev), regs + INTEN);
998 }
999 
1000 /* Start doing req 'idx' of thread 'thrd' */
1001 static bool _trigger(struct pl330_thread *thrd)
1002 {
1003 	void __iomem *regs = thrd->dmac->base;
1004 	struct _pl330_req *req;
1005 	struct dma_pl330_desc *desc;
1006 	struct _arg_GO go;
1007 	unsigned ns;
1008 	u8 insn[6] = {0, 0, 0, 0, 0, 0};
1009 	int idx;
1010 
1011 	/* Return if already ACTIVE */
1012 	if (_state(thrd) != PL330_STATE_STOPPED)
1013 		return true;
1014 
1015 	idx = 1 - thrd->lstenq;
1016 	if (thrd->req[idx].desc != NULL) {
1017 		req = &thrd->req[idx];
1018 	} else {
1019 		idx = thrd->lstenq;
1020 		if (thrd->req[idx].desc != NULL)
1021 			req = &thrd->req[idx];
1022 		else
1023 			req = NULL;
1024 	}
1025 
1026 	/* Return if no request */
1027 	if (!req)
1028 		return true;
1029 
1030 	/* Return if req is running */
1031 	if (idx == thrd->req_running)
1032 		return true;
1033 
1034 	desc = req->desc;
1035 
1036 	ns = desc->rqcfg.nonsecure ? 1 : 0;
1037 
1038 	/* See 'Abort Sources' point-4 at Page 2-25 */
1039 	if (_manager_ns(thrd) && !ns)
1040 		dev_info(thrd->dmac->ddma.dev, "%s:%d Recipe for ABORT!\n",
1041 			__func__, __LINE__);
1042 
1043 	go.chan = thrd->id;
1044 	go.addr = req->mc_bus;
1045 	go.ns = ns;
1046 	_emit_GO(0, insn, &go);
1047 
1048 	/* Set to generate interrupts for SEV */
1049 	writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
1050 
1051 	/* Only manager can execute GO */
1052 	_execute_DBGINSN(thrd, insn, true);
1053 
1054 	thrd->req_running = idx;
1055 
1056 	return true;
1057 }
1058 
1059 static bool pl330_start_thread(struct pl330_thread *thrd)
1060 {
1061 	switch (_state(thrd)) {
1062 	case PL330_STATE_FAULT_COMPLETING:
1063 		UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
1064 
1065 		if (_state(thrd) == PL330_STATE_KILLING)
1066 			UNTIL(thrd, PL330_STATE_STOPPED)
1067 		fallthrough;
1068 
1069 	case PL330_STATE_FAULTING:
1070 		_stop(thrd);
1071 		fallthrough;
1072 
1073 	case PL330_STATE_KILLING:
1074 	case PL330_STATE_COMPLETING:
1075 		UNTIL(thrd, PL330_STATE_STOPPED)
1076 		fallthrough;
1077 
1078 	case PL330_STATE_STOPPED:
1079 		return _trigger(thrd);
1080 
1081 	case PL330_STATE_WFP:
1082 	case PL330_STATE_QUEUEBUSY:
1083 	case PL330_STATE_ATBARRIER:
1084 	case PL330_STATE_UPDTPC:
1085 	case PL330_STATE_CACHEMISS:
1086 	case PL330_STATE_EXECUTING:
1087 		return true;
1088 
1089 	case PL330_STATE_WFE: /* For RESUME, nothing yet */
1090 	default:
1091 		return false;
1092 	}
1093 }
1094 
1095 static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
1096 		const struct _xfer_spec *pxs, int cyc)
1097 {
1098 	int off = 0;
1099 	struct pl330_config *pcfg = pxs->desc->rqcfg.pcfg;
1100 
1101 	/* check lock-up free version */
1102 	if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
1103 		while (cyc--) {
1104 			off += _emit_LD(dry_run, &buf[off], ALWAYS);
1105 			off += _emit_ST(dry_run, &buf[off], ALWAYS);
1106 		}
1107 	} else {
1108 		while (cyc--) {
1109 			off += _emit_LD(dry_run, &buf[off], ALWAYS);
1110 			off += _emit_RMB(dry_run, &buf[off]);
1111 			off += _emit_ST(dry_run, &buf[off], ALWAYS);
1112 			off += _emit_WMB(dry_run, &buf[off]);
1113 		}
1114 	}
1115 
1116 	return off;
1117 }
1118 
1119 static u32 _emit_load(unsigned int dry_run, u8 buf[],
1120 	enum pl330_cond cond, enum dma_transfer_direction direction,
1121 	u8 peri)
1122 {
1123 	int off = 0;
1124 
1125 	switch (direction) {
1126 	case DMA_MEM_TO_MEM:
1127 	case DMA_MEM_TO_DEV:
1128 		off += _emit_LD(dry_run, &buf[off], cond);
1129 		break;
1130 
1131 	case DMA_DEV_TO_MEM:
1132 		if (cond == ALWAYS) {
1133 			off += _emit_LDP(dry_run, &buf[off], SINGLE,
1134 				peri);
1135 			off += _emit_LDP(dry_run, &buf[off], BURST,
1136 				peri);
1137 		} else {
1138 			off += _emit_LDP(dry_run, &buf[off], cond,
1139 				peri);
1140 		}
1141 		break;
1142 
1143 	default:
1144 		/* this code should be unreachable */
1145 		WARN_ON(1);
1146 		break;
1147 	}
1148 
1149 	return off;
1150 }
1151 
1152 static inline u32 _emit_store(unsigned int dry_run, u8 buf[],
1153 	enum pl330_cond cond, enum dma_transfer_direction direction,
1154 	u8 peri)
1155 {
1156 	int off = 0;
1157 
1158 	switch (direction) {
1159 	case DMA_MEM_TO_MEM:
1160 	case DMA_DEV_TO_MEM:
1161 		off += _emit_ST(dry_run, &buf[off], cond);
1162 		break;
1163 
1164 	case DMA_MEM_TO_DEV:
1165 		if (cond == ALWAYS) {
1166 			off += _emit_STP(dry_run, &buf[off], SINGLE,
1167 				peri);
1168 			off += _emit_STP(dry_run, &buf[off], BURST,
1169 				peri);
1170 		} else {
1171 			off += _emit_STP(dry_run, &buf[off], cond,
1172 				peri);
1173 		}
1174 		break;
1175 
1176 	default:
1177 		/* this code should be unreachable */
1178 		WARN_ON(1);
1179 		break;
1180 	}
1181 
1182 	return off;
1183 }
1184 
1185 static inline int _ldst_peripheral(struct pl330_dmac *pl330,
1186 				 unsigned dry_run, u8 buf[],
1187 				 const struct _xfer_spec *pxs, int cyc,
1188 				 enum pl330_cond cond)
1189 {
1190 	int off = 0;
1191 
1192 	/*
1193 	 * do FLUSHP at beginning to clear any stale dma requests before the
1194 	 * first WFP.
1195 	 */
1196 	if (!(pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP))
1197 		off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
1198 	while (cyc--) {
1199 		off += _emit_WFP(dry_run, &buf[off], cond, pxs->desc->peri);
1200 		off += _emit_load(dry_run, &buf[off], cond, pxs->desc->rqtype,
1201 			pxs->desc->peri);
1202 		off += _emit_store(dry_run, &buf[off], cond, pxs->desc->rqtype,
1203 			pxs->desc->peri);
1204 	}
1205 
1206 	return off;
1207 }
1208 
1209 static int _bursts(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
1210 		const struct _xfer_spec *pxs, int cyc)
1211 {
1212 	int off = 0;
1213 	enum pl330_cond cond = BRST_LEN(pxs->ccr) > 1 ? BURST : SINGLE;
1214 
1215 	if (pl330->quirks & PL330_QUIRK_PERIPH_BURST)
1216 		cond = BURST;
1217 
1218 	switch (pxs->desc->rqtype) {
1219 	case DMA_MEM_TO_DEV:
1220 	case DMA_DEV_TO_MEM:
1221 		off += _ldst_peripheral(pl330, dry_run, &buf[off], pxs, cyc,
1222 			cond);
1223 		break;
1224 
1225 	case DMA_MEM_TO_MEM:
1226 		off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1227 		break;
1228 
1229 	default:
1230 		/* this code should be unreachable */
1231 		WARN_ON(1);
1232 		break;
1233 	}
1234 
1235 	return off;
1236 }
1237 
1238 /*
1239  * only the unaligned burst transfers have the dregs.
1240  * so, still transfer dregs with a reduced size burst
1241  * for mem-to-mem, mem-to-dev or dev-to-mem.
1242  */
1243 static int _dregs(struct pl330_dmac *pl330, unsigned int dry_run, u8 buf[],
1244 		const struct _xfer_spec *pxs, int transfer_length)
1245 {
1246 	int off = 0;
1247 	int dregs_ccr;
1248 
1249 	if (transfer_length == 0)
1250 		return off;
1251 
1252 	/*
1253 	 * dregs_len = (total bytes - BURST_TO_BYTE(bursts, ccr)) /
1254 	 *             BRST_SIZE(ccr)
1255 	 * the dregs len must be smaller than burst len,
1256 	 * so, for higher efficiency, we can modify CCR
1257 	 * to use a reduced size burst len for the dregs.
1258 	 */
1259 	dregs_ccr = pxs->ccr;
1260 	dregs_ccr &= ~((0xf << CC_SRCBRSTLEN_SHFT) |
1261 		(0xf << CC_DSTBRSTLEN_SHFT));
1262 	dregs_ccr |= (((transfer_length - 1) & 0xf) <<
1263 		CC_SRCBRSTLEN_SHFT);
1264 	dregs_ccr |= (((transfer_length - 1) & 0xf) <<
1265 		CC_DSTBRSTLEN_SHFT);
1266 
1267 	switch (pxs->desc->rqtype) {
1268 	case DMA_MEM_TO_DEV:
1269 	case DMA_DEV_TO_MEM:
1270 		off += _emit_MOV(dry_run, &buf[off], CCR, dregs_ccr);
1271 		off += _ldst_peripheral(pl330, dry_run, &buf[off], pxs, 1,
1272 					BURST);
1273 		break;
1274 
1275 	case DMA_MEM_TO_MEM:
1276 		off += _emit_MOV(dry_run, &buf[off], CCR, dregs_ccr);
1277 		off += _ldst_memtomem(dry_run, &buf[off], pxs, 1);
1278 		break;
1279 
1280 	default:
1281 		/* this code should be unreachable */
1282 		WARN_ON(1);
1283 		break;
1284 	}
1285 
1286 	return off;
1287 }
1288 
1289 /* Returns bytes consumed and updates bursts */
1290 static inline int _loop(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
1291 		unsigned long *bursts, const struct _xfer_spec *pxs)
1292 {
1293 	int cyc, cycmax, szlp, szlpend, szbrst, off;
1294 	unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1295 	struct _arg_LPEND lpend;
1296 
1297 	if (*bursts == 1)
1298 		return _bursts(pl330, dry_run, buf, pxs, 1);
1299 
1300 	/* Max iterations possible in DMALP is 256 */
1301 	if (*bursts >= 256*256) {
1302 		lcnt1 = 256;
1303 		lcnt0 = 256;
1304 		cyc = *bursts / lcnt1 / lcnt0;
1305 	} else if (*bursts > 256) {
1306 		lcnt1 = 256;
1307 		lcnt0 = *bursts / lcnt1;
1308 		cyc = 1;
1309 	} else {
1310 		lcnt1 = *bursts;
1311 		lcnt0 = 0;
1312 		cyc = 1;
1313 	}
1314 
1315 	szlp = _emit_LP(1, buf, 0, 0);
1316 	szbrst = _bursts(pl330, 1, buf, pxs, 1);
1317 
1318 	lpend.cond = ALWAYS;
1319 	lpend.forever = false;
1320 	lpend.loop = 0;
1321 	lpend.bjump = 0;
1322 	szlpend = _emit_LPEND(1, buf, &lpend);
1323 
1324 	if (lcnt0) {
1325 		szlp *= 2;
1326 		szlpend *= 2;
1327 	}
1328 
1329 	/*
1330 	 * Max bursts that we can unroll due to limit on the
1331 	 * size of backward jump that can be encoded in DMALPEND
1332 	 * which is 8-bits and hence 255
1333 	 */
1334 	cycmax = (255 - (szlp + szlpend)) / szbrst;
1335 
1336 	cyc = (cycmax < cyc) ? cycmax : cyc;
1337 
1338 	off = 0;
1339 
1340 	if (lcnt0) {
1341 		off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1342 		ljmp0 = off;
1343 	}
1344 
1345 	off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1346 	ljmp1 = off;
1347 
1348 	off += _bursts(pl330, dry_run, &buf[off], pxs, cyc);
1349 
1350 	lpend.cond = ALWAYS;
1351 	lpend.forever = false;
1352 	lpend.loop = 1;
1353 	lpend.bjump = off - ljmp1;
1354 	off += _emit_LPEND(dry_run, &buf[off], &lpend);
1355 
1356 	if (lcnt0) {
1357 		lpend.cond = ALWAYS;
1358 		lpend.forever = false;
1359 		lpend.loop = 0;
1360 		lpend.bjump = off - ljmp0;
1361 		off += _emit_LPEND(dry_run, &buf[off], &lpend);
1362 	}
1363 
1364 	*bursts = lcnt1 * cyc;
1365 	if (lcnt0)
1366 		*bursts *= lcnt0;
1367 
1368 	return off;
1369 }
1370 
1371 static inline int _setup_loops(struct pl330_dmac *pl330,
1372 			       unsigned dry_run, u8 buf[],
1373 			       const struct _xfer_spec *pxs)
1374 {
1375 	struct pl330_xfer *x = &pxs->desc->px;
1376 	u32 ccr = pxs->ccr;
1377 	unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1378 	int num_dregs = (x->bytes - BURST_TO_BYTE(bursts, ccr)) /
1379 		BRST_SIZE(ccr);
1380 	int off = 0;
1381 
1382 	while (bursts) {
1383 		c = bursts;
1384 		off += _loop(pl330, dry_run, &buf[off], &c, pxs);
1385 		bursts -= c;
1386 	}
1387 	off += _dregs(pl330, dry_run, &buf[off], pxs, num_dregs);
1388 
1389 	return off;
1390 }
1391 
1392 static inline int _setup_xfer(struct pl330_dmac *pl330,
1393 			      unsigned dry_run, u8 buf[],
1394 			      const struct _xfer_spec *pxs)
1395 {
1396 	struct pl330_xfer *x = &pxs->desc->px;
1397 	int off = 0;
1398 
1399 	/* DMAMOV SAR, x->src_addr */
1400 	off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1401 	/* DMAMOV DAR, x->dst_addr */
1402 	off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1403 
1404 	/* Setup Loop(s) */
1405 	off += _setup_loops(pl330, dry_run, &buf[off], pxs);
1406 
1407 	return off;
1408 }
1409 
1410 /*
1411  * A req is a sequence of one or more xfer units.
1412  * Returns the number of bytes taken to setup the MC for the req.
1413  */
1414 static int _setup_req(struct pl330_dmac *pl330, unsigned dry_run,
1415 		      struct pl330_thread *thrd, unsigned index,
1416 		      struct _xfer_spec *pxs)
1417 {
1418 	struct _pl330_req *req = &thrd->req[index];
1419 	u8 *buf = req->mc_cpu;
1420 	int off = 0;
1421 
1422 	PL330_DBGMC_START(req->mc_bus);
1423 
1424 	/* DMAMOV CCR, ccr */
1425 	off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1426 
1427 	off += _setup_xfer(pl330, dry_run, &buf[off], pxs);
1428 
1429 	/* DMASEV peripheral/event */
1430 	off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1431 	/* DMAEND */
1432 	off += _emit_END(dry_run, &buf[off]);
1433 
1434 	return off;
1435 }
1436 
1437 static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1438 {
1439 	u32 ccr = 0;
1440 
1441 	if (rqc->src_inc)
1442 		ccr |= CC_SRCINC;
1443 
1444 	if (rqc->dst_inc)
1445 		ccr |= CC_DSTINC;
1446 
1447 	/* We set same protection levels for Src and DST for now */
1448 	if (rqc->privileged)
1449 		ccr |= CC_SRCPRI | CC_DSTPRI;
1450 	if (rqc->nonsecure)
1451 		ccr |= CC_SRCNS | CC_DSTNS;
1452 	if (rqc->insnaccess)
1453 		ccr |= CC_SRCIA | CC_DSTIA;
1454 
1455 	ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1456 	ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1457 
1458 	ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1459 	ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1460 
1461 	ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
1462 	ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
1463 
1464 	ccr |= (rqc->swap << CC_SWAP_SHFT);
1465 
1466 	return ccr;
1467 }
1468 
1469 /*
1470  * Submit a list of xfers after which the client wants notification.
1471  * Client is not notified after each xfer unit, just once after all
1472  * xfer units are done or some error occurs.
1473  */
1474 static int pl330_submit_req(struct pl330_thread *thrd,
1475 	struct dma_pl330_desc *desc)
1476 {
1477 	struct pl330_dmac *pl330 = thrd->dmac;
1478 	struct _xfer_spec xs;
1479 	unsigned long flags;
1480 	unsigned idx;
1481 	u32 ccr;
1482 	int ret = 0;
1483 
1484 	switch (desc->rqtype) {
1485 	case DMA_MEM_TO_DEV:
1486 		break;
1487 
1488 	case DMA_DEV_TO_MEM:
1489 		break;
1490 
1491 	case DMA_MEM_TO_MEM:
1492 		break;
1493 
1494 	default:
1495 		return -ENOTSUPP;
1496 	}
1497 
1498 	if (pl330->state == DYING
1499 		|| pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1500 		dev_info(thrd->dmac->ddma.dev, "%s:%d\n",
1501 			__func__, __LINE__);
1502 		return -EAGAIN;
1503 	}
1504 
1505 	/* If request for non-existing peripheral */
1506 	if (desc->rqtype != DMA_MEM_TO_MEM &&
1507 	    desc->peri >= pl330->pcfg.num_peri) {
1508 		dev_info(thrd->dmac->ddma.dev,
1509 				"%s:%d Invalid peripheral(%u)!\n",
1510 				__func__, __LINE__, desc->peri);
1511 		return -EINVAL;
1512 	}
1513 
1514 	spin_lock_irqsave(&pl330->lock, flags);
1515 
1516 	if (_queue_full(thrd)) {
1517 		ret = -EAGAIN;
1518 		goto xfer_exit;
1519 	}
1520 
1521 	/* Prefer Secure Channel */
1522 	if (!_manager_ns(thrd))
1523 		desc->rqcfg.nonsecure = 0;
1524 	else
1525 		desc->rqcfg.nonsecure = 1;
1526 
1527 	ccr = _prepare_ccr(&desc->rqcfg);
1528 
1529 	idx = thrd->req[0].desc == NULL ? 0 : 1;
1530 
1531 	xs.ccr = ccr;
1532 	xs.desc = desc;
1533 
1534 	/* First dry run to check if req is acceptable */
1535 	ret = _setup_req(pl330, 1, thrd, idx, &xs);
1536 
1537 	if (ret > pl330->mcbufsz / 2) {
1538 		dev_info(pl330->ddma.dev, "%s:%d Try increasing mcbufsz (%i/%i)\n",
1539 				__func__, __LINE__, ret, pl330->mcbufsz / 2);
1540 		ret = -ENOMEM;
1541 		goto xfer_exit;
1542 	}
1543 
1544 	/* Hook the request */
1545 	thrd->lstenq = idx;
1546 	thrd->req[idx].desc = desc;
1547 	_setup_req(pl330, 0, thrd, idx, &xs);
1548 
1549 	ret = 0;
1550 
1551 xfer_exit:
1552 	spin_unlock_irqrestore(&pl330->lock, flags);
1553 
1554 	return ret;
1555 }
1556 
1557 static void dma_pl330_rqcb(struct dma_pl330_desc *desc, enum pl330_op_err err)
1558 {
1559 	struct dma_pl330_chan *pch;
1560 	unsigned long flags;
1561 
1562 	if (!desc)
1563 		return;
1564 
1565 	pch = desc->pchan;
1566 
1567 	/* If desc aborted */
1568 	if (!pch)
1569 		return;
1570 
1571 	spin_lock_irqsave(&pch->lock, flags);
1572 
1573 	desc->status = DONE;
1574 
1575 	spin_unlock_irqrestore(&pch->lock, flags);
1576 
1577 	tasklet_schedule(&pch->task);
1578 }
1579 
1580 static void pl330_dotask(struct tasklet_struct *t)
1581 {
1582 	struct pl330_dmac *pl330 = from_tasklet(pl330, t, tasks);
1583 	unsigned long flags;
1584 	int i;
1585 
1586 	spin_lock_irqsave(&pl330->lock, flags);
1587 
1588 	/* The DMAC itself gone nuts */
1589 	if (pl330->dmac_tbd.reset_dmac) {
1590 		pl330->state = DYING;
1591 		/* Reset the manager too */
1592 		pl330->dmac_tbd.reset_mngr = true;
1593 		/* Clear the reset flag */
1594 		pl330->dmac_tbd.reset_dmac = false;
1595 	}
1596 
1597 	if (pl330->dmac_tbd.reset_mngr) {
1598 		_stop(pl330->manager);
1599 		/* Reset all channels */
1600 		pl330->dmac_tbd.reset_chan = (1 << pl330->pcfg.num_chan) - 1;
1601 		/* Clear the reset flag */
1602 		pl330->dmac_tbd.reset_mngr = false;
1603 	}
1604 
1605 	for (i = 0; i < pl330->pcfg.num_chan; i++) {
1606 
1607 		if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1608 			struct pl330_thread *thrd = &pl330->channels[i];
1609 			void __iomem *regs = pl330->base;
1610 			enum pl330_op_err err;
1611 
1612 			_stop(thrd);
1613 
1614 			if (readl(regs + FSC) & (1 << thrd->id))
1615 				err = PL330_ERR_FAIL;
1616 			else
1617 				err = PL330_ERR_ABORT;
1618 
1619 			spin_unlock_irqrestore(&pl330->lock, flags);
1620 			dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, err);
1621 			dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, err);
1622 			spin_lock_irqsave(&pl330->lock, flags);
1623 
1624 			thrd->req[0].desc = NULL;
1625 			thrd->req[1].desc = NULL;
1626 			thrd->req_running = -1;
1627 
1628 			/* Clear the reset flag */
1629 			pl330->dmac_tbd.reset_chan &= ~(1 << i);
1630 		}
1631 	}
1632 
1633 	spin_unlock_irqrestore(&pl330->lock, flags);
1634 
1635 	return;
1636 }
1637 
1638 /* Returns 1 if state was updated, 0 otherwise */
1639 static int pl330_update(struct pl330_dmac *pl330)
1640 {
1641 	struct dma_pl330_desc *descdone;
1642 	unsigned long flags;
1643 	void __iomem *regs;
1644 	u32 val;
1645 	int id, ev, ret = 0;
1646 
1647 	regs = pl330->base;
1648 
1649 	spin_lock_irqsave(&pl330->lock, flags);
1650 
1651 	val = readl(regs + FSM) & 0x1;
1652 	if (val)
1653 		pl330->dmac_tbd.reset_mngr = true;
1654 	else
1655 		pl330->dmac_tbd.reset_mngr = false;
1656 
1657 	val = readl(regs + FSC) & ((1 << pl330->pcfg.num_chan) - 1);
1658 	pl330->dmac_tbd.reset_chan |= val;
1659 	if (val) {
1660 		int i = 0;
1661 		while (i < pl330->pcfg.num_chan) {
1662 			if (val & (1 << i)) {
1663 				dev_info(pl330->ddma.dev,
1664 					"Reset Channel-%d\t CS-%x FTC-%x\n",
1665 						i, readl(regs + CS(i)),
1666 						readl(regs + FTC(i)));
1667 				_stop(&pl330->channels[i]);
1668 			}
1669 			i++;
1670 		}
1671 	}
1672 
1673 	/* Check which event happened i.e, thread notified */
1674 	val = readl(regs + ES);
1675 	if (pl330->pcfg.num_events < 32
1676 			&& val & ~((1 << pl330->pcfg.num_events) - 1)) {
1677 		pl330->dmac_tbd.reset_dmac = true;
1678 		dev_err(pl330->ddma.dev, "%s:%d Unexpected!\n", __func__,
1679 			__LINE__);
1680 		ret = 1;
1681 		goto updt_exit;
1682 	}
1683 
1684 	for (ev = 0; ev < pl330->pcfg.num_events; ev++) {
1685 		if (val & (1 << ev)) { /* Event occurred */
1686 			struct pl330_thread *thrd;
1687 			u32 inten = readl(regs + INTEN);
1688 			int active;
1689 
1690 			/* Clear the event */
1691 			if (inten & (1 << ev))
1692 				writel(1 << ev, regs + INTCLR);
1693 
1694 			ret = 1;
1695 
1696 			id = pl330->events[ev];
1697 
1698 			thrd = &pl330->channels[id];
1699 
1700 			active = thrd->req_running;
1701 			if (active == -1) /* Aborted */
1702 				continue;
1703 
1704 			/* Detach the req */
1705 			descdone = thrd->req[active].desc;
1706 			thrd->req[active].desc = NULL;
1707 
1708 			thrd->req_running = -1;
1709 
1710 			/* Get going again ASAP */
1711 			pl330_start_thread(thrd);
1712 
1713 			/* For now, just make a list of callbacks to be done */
1714 			list_add_tail(&descdone->rqd, &pl330->req_done);
1715 		}
1716 	}
1717 
1718 	/* Now that we are in no hurry, do the callbacks */
1719 	while (!list_empty(&pl330->req_done)) {
1720 		descdone = list_first_entry(&pl330->req_done,
1721 					    struct dma_pl330_desc, rqd);
1722 		list_del(&descdone->rqd);
1723 		spin_unlock_irqrestore(&pl330->lock, flags);
1724 		dma_pl330_rqcb(descdone, PL330_ERR_NONE);
1725 		spin_lock_irqsave(&pl330->lock, flags);
1726 	}
1727 
1728 updt_exit:
1729 	spin_unlock_irqrestore(&pl330->lock, flags);
1730 
1731 	if (pl330->dmac_tbd.reset_dmac
1732 			|| pl330->dmac_tbd.reset_mngr
1733 			|| pl330->dmac_tbd.reset_chan) {
1734 		ret = 1;
1735 		tasklet_schedule(&pl330->tasks);
1736 	}
1737 
1738 	return ret;
1739 }
1740 
1741 /* Reserve an event */
1742 static inline int _alloc_event(struct pl330_thread *thrd)
1743 {
1744 	struct pl330_dmac *pl330 = thrd->dmac;
1745 	int ev;
1746 
1747 	for (ev = 0; ev < pl330->pcfg.num_events; ev++)
1748 		if (pl330->events[ev] == -1) {
1749 			pl330->events[ev] = thrd->id;
1750 			return ev;
1751 		}
1752 
1753 	return -1;
1754 }
1755 
1756 static bool _chan_ns(const struct pl330_dmac *pl330, int i)
1757 {
1758 	return pl330->pcfg.irq_ns & (1 << i);
1759 }
1760 
1761 /* Upon success, returns IdentityToken for the
1762  * allocated channel, NULL otherwise.
1763  */
1764 static struct pl330_thread *pl330_request_channel(struct pl330_dmac *pl330)
1765 {
1766 	struct pl330_thread *thrd = NULL;
1767 	int chans, i;
1768 
1769 	if (pl330->state == DYING)
1770 		return NULL;
1771 
1772 	chans = pl330->pcfg.num_chan;
1773 
1774 	for (i = 0; i < chans; i++) {
1775 		thrd = &pl330->channels[i];
1776 		if ((thrd->free) && (!_manager_ns(thrd) ||
1777 					_chan_ns(pl330, i))) {
1778 			thrd->ev = _alloc_event(thrd);
1779 			if (thrd->ev >= 0) {
1780 				thrd->free = false;
1781 				thrd->lstenq = 1;
1782 				thrd->req[0].desc = NULL;
1783 				thrd->req[1].desc = NULL;
1784 				thrd->req_running = -1;
1785 				break;
1786 			}
1787 		}
1788 		thrd = NULL;
1789 	}
1790 
1791 	return thrd;
1792 }
1793 
1794 /* Release an event */
1795 static inline void _free_event(struct pl330_thread *thrd, int ev)
1796 {
1797 	struct pl330_dmac *pl330 = thrd->dmac;
1798 
1799 	/* If the event is valid and was held by the thread */
1800 	if (ev >= 0 && ev < pl330->pcfg.num_events
1801 			&& pl330->events[ev] == thrd->id)
1802 		pl330->events[ev] = -1;
1803 }
1804 
1805 static void pl330_release_channel(struct pl330_thread *thrd)
1806 {
1807 	if (!thrd || thrd->free)
1808 		return;
1809 
1810 	_stop(thrd);
1811 
1812 	dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, PL330_ERR_ABORT);
1813 	dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, PL330_ERR_ABORT);
1814 
1815 	_free_event(thrd, thrd->ev);
1816 	thrd->free = true;
1817 }
1818 
1819 /* Initialize the structure for PL330 configuration, that can be used
1820  * by the client driver the make best use of the DMAC
1821  */
1822 static void read_dmac_config(struct pl330_dmac *pl330)
1823 {
1824 	void __iomem *regs = pl330->base;
1825 	u32 val;
1826 
1827 	val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1828 	val &= CRD_DATA_WIDTH_MASK;
1829 	pl330->pcfg.data_bus_width = 8 * (1 << val);
1830 
1831 	val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1832 	val &= CRD_DATA_BUFF_MASK;
1833 	pl330->pcfg.data_buf_dep = val + 1;
1834 
1835 	val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1836 	val &= CR0_NUM_CHANS_MASK;
1837 	val += 1;
1838 	pl330->pcfg.num_chan = val;
1839 
1840 	val = readl(regs + CR0);
1841 	if (val & CR0_PERIPH_REQ_SET) {
1842 		val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1843 		val += 1;
1844 		pl330->pcfg.num_peri = val;
1845 		pl330->pcfg.peri_ns = readl(regs + CR4);
1846 	} else {
1847 		pl330->pcfg.num_peri = 0;
1848 	}
1849 
1850 	val = readl(regs + CR0);
1851 	if (val & CR0_BOOT_MAN_NS)
1852 		pl330->pcfg.mode |= DMAC_MODE_NS;
1853 	else
1854 		pl330->pcfg.mode &= ~DMAC_MODE_NS;
1855 
1856 	val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1857 	val &= CR0_NUM_EVENTS_MASK;
1858 	val += 1;
1859 	pl330->pcfg.num_events = val;
1860 
1861 	pl330->pcfg.irq_ns = readl(regs + CR3);
1862 }
1863 
1864 static inline void _reset_thread(struct pl330_thread *thrd)
1865 {
1866 	struct pl330_dmac *pl330 = thrd->dmac;
1867 
1868 	thrd->req[0].mc_cpu = pl330->mcode_cpu
1869 				+ (thrd->id * pl330->mcbufsz);
1870 	thrd->req[0].mc_bus = pl330->mcode_bus
1871 				+ (thrd->id * pl330->mcbufsz);
1872 	thrd->req[0].desc = NULL;
1873 
1874 	thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1875 				+ pl330->mcbufsz / 2;
1876 	thrd->req[1].mc_bus = thrd->req[0].mc_bus
1877 				+ pl330->mcbufsz / 2;
1878 	thrd->req[1].desc = NULL;
1879 
1880 	thrd->req_running = -1;
1881 }
1882 
1883 static int dmac_alloc_threads(struct pl330_dmac *pl330)
1884 {
1885 	int chans = pl330->pcfg.num_chan;
1886 	struct pl330_thread *thrd;
1887 	int i;
1888 
1889 	/* Allocate 1 Manager and 'chans' Channel threads */
1890 	pl330->channels = kzalloc_objs(*thrd, 1 + chans, GFP_KERNEL);
1891 	if (!pl330->channels)
1892 		return -ENOMEM;
1893 
1894 	/* Init Channel threads */
1895 	for (i = 0; i < chans; i++) {
1896 		thrd = &pl330->channels[i];
1897 		thrd->id = i;
1898 		thrd->dmac = pl330;
1899 		_reset_thread(thrd);
1900 		thrd->free = true;
1901 	}
1902 
1903 	/* MANAGER is indexed at the end */
1904 	thrd = &pl330->channels[chans];
1905 	thrd->id = chans;
1906 	thrd->dmac = pl330;
1907 	thrd->free = false;
1908 	pl330->manager = thrd;
1909 
1910 	return 0;
1911 }
1912 
1913 static int dmac_alloc_resources(struct pl330_dmac *pl330)
1914 {
1915 	int chans = pl330->pcfg.num_chan;
1916 	int ret;
1917 
1918 	/*
1919 	 * Alloc MicroCode buffer for 'chans' Channel threads.
1920 	 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
1921 	 */
1922 	pl330->mcode_cpu = dma_alloc_attrs(pl330->ddma.dev,
1923 				chans * pl330->mcbufsz,
1924 				&pl330->mcode_bus, GFP_KERNEL,
1925 				DMA_ATTR_PRIVILEGED);
1926 	if (!pl330->mcode_cpu) {
1927 		dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n",
1928 			__func__, __LINE__);
1929 		return -ENOMEM;
1930 	}
1931 
1932 	ret = dmac_alloc_threads(pl330);
1933 	if (ret) {
1934 		dev_err(pl330->ddma.dev, "%s:%d Can't to create channels for DMAC!\n",
1935 			__func__, __LINE__);
1936 		dma_free_attrs(pl330->ddma.dev,
1937 				chans * pl330->mcbufsz,
1938 				pl330->mcode_cpu, pl330->mcode_bus,
1939 				DMA_ATTR_PRIVILEGED);
1940 		return ret;
1941 	}
1942 
1943 	return 0;
1944 }
1945 
1946 static int pl330_add(struct pl330_dmac *pl330)
1947 {
1948 	int i, ret;
1949 
1950 	/* Check if we can handle this DMAC */
1951 	if ((pl330->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
1952 		dev_err(pl330->ddma.dev, "PERIPH_ID 0x%x !\n",
1953 			pl330->pcfg.periph_id);
1954 		return -EINVAL;
1955 	}
1956 
1957 	/* Read the configuration of the DMAC */
1958 	read_dmac_config(pl330);
1959 
1960 	if (pl330->pcfg.num_events == 0) {
1961 		dev_err(pl330->ddma.dev, "%s:%d Can't work without events!\n",
1962 			__func__, __LINE__);
1963 		return -EINVAL;
1964 	}
1965 
1966 	spin_lock_init(&pl330->lock);
1967 
1968 	INIT_LIST_HEAD(&pl330->req_done);
1969 
1970 	/* Use default MC buffer size if not provided */
1971 	if (!pl330->mcbufsz)
1972 		pl330->mcbufsz = MCODE_BUFF_PER_REQ * 2;
1973 
1974 	/* Mark all events as free */
1975 	for (i = 0; i < pl330->pcfg.num_events; i++)
1976 		pl330->events[i] = -1;
1977 
1978 	/* Allocate resources needed by the DMAC */
1979 	ret = dmac_alloc_resources(pl330);
1980 	if (ret) {
1981 		dev_err(pl330->ddma.dev, "Unable to create channels for DMAC\n");
1982 		return ret;
1983 	}
1984 
1985 	tasklet_setup(&pl330->tasks, pl330_dotask);
1986 
1987 	pl330->state = INIT;
1988 
1989 	return 0;
1990 }
1991 
1992 static int dmac_free_threads(struct pl330_dmac *pl330)
1993 {
1994 	struct pl330_thread *thrd;
1995 	int i;
1996 
1997 	/* Release Channel threads */
1998 	for (i = 0; i < pl330->pcfg.num_chan; i++) {
1999 		thrd = &pl330->channels[i];
2000 		pl330_release_channel(thrd);
2001 	}
2002 
2003 	/* Free memory */
2004 	kfree(pl330->channels);
2005 
2006 	return 0;
2007 }
2008 
2009 static void pl330_del(struct pl330_dmac *pl330)
2010 {
2011 	pl330->state = UNINIT;
2012 
2013 	tasklet_kill(&pl330->tasks);
2014 
2015 	/* Free DMAC resources */
2016 	dmac_free_threads(pl330);
2017 
2018 	dma_free_attrs(pl330->ddma.dev,
2019 		pl330->pcfg.num_chan * pl330->mcbufsz, pl330->mcode_cpu,
2020 		pl330->mcode_bus, DMA_ATTR_PRIVILEGED);
2021 }
2022 
2023 /* forward declaration */
2024 static struct amba_driver pl330_driver;
2025 
2026 static inline struct dma_pl330_chan *
2027 to_pchan(struct dma_chan *ch)
2028 {
2029 	if (!ch)
2030 		return NULL;
2031 
2032 	return container_of(ch, struct dma_pl330_chan, chan);
2033 }
2034 
2035 static inline struct dma_pl330_desc *
2036 to_desc(struct dma_async_tx_descriptor *tx)
2037 {
2038 	return container_of(tx, struct dma_pl330_desc, txd);
2039 }
2040 
2041 static inline void fill_queue(struct dma_pl330_chan *pch)
2042 {
2043 	struct dma_pl330_desc *desc;
2044 	int ret;
2045 
2046 	list_for_each_entry(desc, &pch->work_list, node) {
2047 
2048 		/* If already submitted */
2049 		if (desc->status == BUSY || desc->status == PAUSED)
2050 			continue;
2051 
2052 		ret = pl330_submit_req(pch->thread, desc);
2053 		if (!ret) {
2054 			desc->status = BUSY;
2055 		} else if (ret == -EAGAIN) {
2056 			/* QFull or DMAC Dying */
2057 			break;
2058 		} else {
2059 			/* Unacceptable request */
2060 			desc->status = DONE;
2061 			dev_err(pch->dmac->ddma.dev, "%s:%d Bad Desc(%d)\n",
2062 					__func__, __LINE__, desc->txd.cookie);
2063 			tasklet_schedule(&pch->task);
2064 		}
2065 	}
2066 }
2067 
2068 static void pl330_tasklet(struct tasklet_struct *t)
2069 {
2070 	struct dma_pl330_chan *pch = from_tasklet(pch, t, task);
2071 	struct dma_pl330_desc *desc, *_dt;
2072 	unsigned long flags;
2073 	bool power_down = false;
2074 
2075 	spin_lock_irqsave(&pch->lock, flags);
2076 
2077 	/* Pick up ripe tomatoes */
2078 	list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
2079 		if (desc->status == DONE) {
2080 			if (!pch->cyclic)
2081 				dma_cookie_complete(&desc->txd);
2082 			list_move_tail(&desc->node, &pch->completed_list);
2083 		}
2084 
2085 	/* Try to submit a req imm. next to the last completed cookie */
2086 	fill_queue(pch);
2087 
2088 	if (list_empty(&pch->work_list)) {
2089 		spin_lock(&pch->thread->dmac->lock);
2090 		_stop(pch->thread);
2091 		spin_unlock(&pch->thread->dmac->lock);
2092 		power_down = true;
2093 		pch->active = false;
2094 	} else {
2095 		/* Make sure the PL330 Channel thread is active */
2096 		spin_lock(&pch->thread->dmac->lock);
2097 		pl330_start_thread(pch->thread);
2098 		spin_unlock(&pch->thread->dmac->lock);
2099 	}
2100 
2101 	while (!list_empty(&pch->completed_list)) {
2102 		struct dmaengine_desc_callback cb;
2103 
2104 		desc = list_first_entry(&pch->completed_list,
2105 					struct dma_pl330_desc, node);
2106 
2107 		dmaengine_desc_get_callback(&desc->txd, &cb);
2108 
2109 		if (pch->cyclic) {
2110 			desc->status = PREP;
2111 			list_move_tail(&desc->node, &pch->work_list);
2112 			if (power_down) {
2113 				pch->active = true;
2114 				spin_lock(&pch->thread->dmac->lock);
2115 				pl330_start_thread(pch->thread);
2116 				spin_unlock(&pch->thread->dmac->lock);
2117 				power_down = false;
2118 			}
2119 		} else {
2120 			desc->status = FREE;
2121 			list_move_tail(&desc->node, &pch->dmac->desc_pool);
2122 		}
2123 
2124 		dma_descriptor_unmap(&desc->txd);
2125 
2126 		if (dmaengine_desc_callback_valid(&cb)) {
2127 			spin_unlock_irqrestore(&pch->lock, flags);
2128 			dmaengine_desc_callback_invoke(&cb, NULL);
2129 			spin_lock_irqsave(&pch->lock, flags);
2130 		}
2131 	}
2132 	spin_unlock_irqrestore(&pch->lock, flags);
2133 
2134 	/* If work list empty, power down */
2135 	if (power_down)
2136 		pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
2137 }
2138 
2139 static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
2140 						struct of_dma *ofdma)
2141 {
2142 	int count = dma_spec->args_count;
2143 	struct pl330_dmac *pl330 = ofdma->of_dma_data;
2144 	unsigned int chan_id;
2145 
2146 	if (!pl330)
2147 		return NULL;
2148 
2149 	if (count != 1)
2150 		return NULL;
2151 
2152 	chan_id = dma_spec->args[0];
2153 	if (chan_id >= pl330->num_peripherals)
2154 		return NULL;
2155 
2156 	return dma_get_slave_channel(&pl330->peripherals[chan_id].chan);
2157 }
2158 
2159 static int pl330_alloc_chan_resources(struct dma_chan *chan)
2160 {
2161 	struct dma_pl330_chan *pch = to_pchan(chan);
2162 	struct pl330_dmac *pl330 = pch->dmac;
2163 	unsigned long flags;
2164 
2165 	spin_lock_irqsave(&pl330->lock, flags);
2166 
2167 	dma_cookie_init(chan);
2168 	pch->cyclic = false;
2169 
2170 	pch->thread = pl330_request_channel(pl330);
2171 	if (!pch->thread) {
2172 		spin_unlock_irqrestore(&pl330->lock, flags);
2173 		return -ENOMEM;
2174 	}
2175 
2176 	tasklet_setup(&pch->task, pl330_tasklet);
2177 
2178 	spin_unlock_irqrestore(&pl330->lock, flags);
2179 
2180 	return 1;
2181 }
2182 
2183 /*
2184  * We need the data direction between the DMAC (the dma-mapping "device") and
2185  * the FIFO (the dmaengine "dev"), from the FIFO's point of view. Confusing!
2186  */
2187 static enum dma_data_direction
2188 pl330_dma_slave_map_dir(enum dma_transfer_direction dir)
2189 {
2190 	switch (dir) {
2191 	case DMA_MEM_TO_DEV:
2192 		return DMA_FROM_DEVICE;
2193 	case DMA_DEV_TO_MEM:
2194 		return DMA_TO_DEVICE;
2195 	case DMA_DEV_TO_DEV:
2196 		return DMA_BIDIRECTIONAL;
2197 	default:
2198 		return DMA_NONE;
2199 	}
2200 }
2201 
2202 static void pl330_unprep_slave_fifo(struct dma_pl330_chan *pch)
2203 {
2204 	if (pch->dir != DMA_NONE)
2205 		dma_unmap_resource(pch->chan.device->dev, pch->fifo_dma,
2206 				   1 << pch->burst_sz, pch->dir, 0);
2207 	pch->dir = DMA_NONE;
2208 }
2209 
2210 
2211 static bool pl330_prep_slave_fifo(struct dma_pl330_chan *pch,
2212 				  enum dma_transfer_direction dir)
2213 {
2214 	struct device *dev = pch->chan.device->dev;
2215 	enum dma_data_direction dma_dir = pl330_dma_slave_map_dir(dir);
2216 
2217 	/* Already mapped for this config? */
2218 	if (pch->dir == dma_dir)
2219 		return true;
2220 
2221 	pl330_unprep_slave_fifo(pch);
2222 	pch->fifo_dma = dma_map_resource(dev, pch->fifo_addr,
2223 					 1 << pch->burst_sz, dma_dir, 0);
2224 	if (dma_mapping_error(dev, pch->fifo_dma))
2225 		return false;
2226 
2227 	pch->dir = dma_dir;
2228 	return true;
2229 }
2230 
2231 static int fixup_burst_len(int max_burst_len, int quirks)
2232 {
2233 	if (max_burst_len > PL330_MAX_BURST)
2234 		return PL330_MAX_BURST;
2235 	else if (max_burst_len < 1)
2236 		return 1;
2237 	else
2238 		return max_burst_len;
2239 }
2240 
2241 static int pl330_config_write(struct dma_chan *chan,
2242 			struct dma_slave_config *slave_config,
2243 			enum dma_transfer_direction direction)
2244 {
2245 	struct dma_pl330_chan *pch = to_pchan(chan);
2246 
2247 	pl330_unprep_slave_fifo(pch);
2248 	if (direction == DMA_MEM_TO_DEV) {
2249 		if (slave_config->dst_addr)
2250 			pch->fifo_addr = slave_config->dst_addr;
2251 		if (slave_config->dst_addr_width)
2252 			pch->burst_sz = __ffs(slave_config->dst_addr_width);
2253 		pch->burst_len = fixup_burst_len(slave_config->dst_maxburst,
2254 			pch->dmac->quirks);
2255 	} else if (direction == DMA_DEV_TO_MEM) {
2256 		if (slave_config->src_addr)
2257 			pch->fifo_addr = slave_config->src_addr;
2258 		if (slave_config->src_addr_width)
2259 			pch->burst_sz = __ffs(slave_config->src_addr_width);
2260 		pch->burst_len = fixup_burst_len(slave_config->src_maxburst,
2261 			pch->dmac->quirks);
2262 	}
2263 
2264 	return 0;
2265 }
2266 
2267 static int pl330_config(struct dma_chan *chan,
2268 			struct dma_slave_config *slave_config)
2269 {
2270 	struct dma_pl330_chan *pch = to_pchan(chan);
2271 
2272 	memcpy(&pch->slave_config, slave_config, sizeof(*slave_config));
2273 
2274 	return 0;
2275 }
2276 
2277 static int pl330_terminate_all(struct dma_chan *chan)
2278 {
2279 	struct dma_pl330_chan *pch = to_pchan(chan);
2280 	struct dma_pl330_desc *desc;
2281 	unsigned long flags;
2282 	struct pl330_dmac *pl330 = pch->dmac;
2283 	bool power_down = false;
2284 
2285 	pm_runtime_get_sync(pl330->ddma.dev);
2286 	spin_lock_irqsave(&pch->lock, flags);
2287 
2288 	spin_lock(&pl330->lock);
2289 	_stop(pch->thread);
2290 	pch->thread->req[0].desc = NULL;
2291 	pch->thread->req[1].desc = NULL;
2292 	pch->thread->req_running = -1;
2293 	spin_unlock(&pl330->lock);
2294 
2295 	power_down = pch->active;
2296 	pch->active = false;
2297 
2298 	/* Mark all desc done */
2299 	list_for_each_entry(desc, &pch->submitted_list, node) {
2300 		desc->status = FREE;
2301 		dma_cookie_complete(&desc->txd);
2302 	}
2303 
2304 	list_for_each_entry(desc, &pch->work_list , node) {
2305 		desc->status = FREE;
2306 		dma_cookie_complete(&desc->txd);
2307 	}
2308 
2309 	list_splice_tail_init(&pch->submitted_list, &pl330->desc_pool);
2310 	list_splice_tail_init(&pch->work_list, &pl330->desc_pool);
2311 	list_splice_tail_init(&pch->completed_list, &pl330->desc_pool);
2312 	spin_unlock_irqrestore(&pch->lock, flags);
2313 	if (power_down)
2314 		pm_runtime_put_autosuspend(pl330->ddma.dev);
2315 	pm_runtime_put_autosuspend(pl330->ddma.dev);
2316 
2317 	return 0;
2318 }
2319 
2320 /*
2321  * We don't support DMA_RESUME command because of hardware
2322  * limitations, so after pausing the channel we cannot restore
2323  * it to active state. We have to terminate channel and setup
2324  * DMA transfer again. This pause feature was implemented to
2325  * allow safely read residue before channel termination.
2326  */
2327 static int pl330_pause(struct dma_chan *chan)
2328 {
2329 	struct dma_pl330_chan *pch = to_pchan(chan);
2330 	struct pl330_dmac *pl330 = pch->dmac;
2331 	struct dma_pl330_desc *desc;
2332 	unsigned long flags;
2333 
2334 	pm_runtime_get_sync(pl330->ddma.dev);
2335 	spin_lock_irqsave(&pch->lock, flags);
2336 
2337 	spin_lock(&pl330->lock);
2338 	_stop(pch->thread);
2339 	spin_unlock(&pl330->lock);
2340 
2341 	list_for_each_entry(desc, &pch->work_list, node) {
2342 		if (desc->status == BUSY)
2343 			desc->status = PAUSED;
2344 	}
2345 	spin_unlock_irqrestore(&pch->lock, flags);
2346 	pm_runtime_put_autosuspend(pl330->ddma.dev);
2347 
2348 	return 0;
2349 }
2350 
2351 static void pl330_free_chan_resources(struct dma_chan *chan)
2352 {
2353 	struct dma_pl330_chan *pch = to_pchan(chan);
2354 	struct pl330_dmac *pl330 = pch->dmac;
2355 	unsigned long flags;
2356 
2357 	tasklet_kill(&pch->task);
2358 
2359 	pm_runtime_get_sync(pch->dmac->ddma.dev);
2360 	spin_lock_irqsave(&pl330->lock, flags);
2361 
2362 	pl330_release_channel(pch->thread);
2363 	pch->thread = NULL;
2364 
2365 	if (pch->cyclic)
2366 		list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
2367 
2368 	spin_unlock_irqrestore(&pl330->lock, flags);
2369 	pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
2370 	pl330_unprep_slave_fifo(pch);
2371 }
2372 
2373 static int pl330_get_current_xferred_count(struct dma_pl330_chan *pch,
2374 					   struct dma_pl330_desc *desc)
2375 {
2376 	struct pl330_thread *thrd = pch->thread;
2377 	struct pl330_dmac *pl330 = pch->dmac;
2378 	void __iomem *regs = thrd->dmac->base;
2379 	u32 val, addr;
2380 
2381 	pm_runtime_get_sync(pl330->ddma.dev);
2382 	val = addr = 0;
2383 	if (desc->rqcfg.src_inc) {
2384 		val = readl(regs + SA(thrd->id));
2385 		addr = desc->px.src_addr;
2386 	} else {
2387 		val = readl(regs + DA(thrd->id));
2388 		addr = desc->px.dst_addr;
2389 	}
2390 	pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
2391 	pm_runtime_put_autosuspend(pl330->ddma.dev);
2392 
2393 	/* If DMAMOV hasn't finished yet, SAR/DAR can be zero */
2394 	if (!val)
2395 		return 0;
2396 
2397 	return val - addr;
2398 }
2399 
2400 static enum dma_status
2401 pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
2402 		 struct dma_tx_state *txstate)
2403 {
2404 	enum dma_status ret;
2405 	unsigned long flags;
2406 	struct dma_pl330_desc *desc, *running = NULL, *last_enq = NULL;
2407 	struct dma_pl330_chan *pch = to_pchan(chan);
2408 	unsigned int transferred, residual = 0;
2409 
2410 	ret = dma_cookie_status(chan, cookie, txstate);
2411 
2412 	if (!txstate)
2413 		return ret;
2414 
2415 	if (ret == DMA_COMPLETE)
2416 		goto out;
2417 
2418 	spin_lock_irqsave(&pch->lock, flags);
2419 	spin_lock(&pch->thread->dmac->lock);
2420 
2421 	if (pch->thread->req_running != -1)
2422 		running = pch->thread->req[pch->thread->req_running].desc;
2423 
2424 	last_enq = pch->thread->req[pch->thread->lstenq].desc;
2425 
2426 	/* Check in pending list */
2427 	list_for_each_entry(desc, &pch->work_list, node) {
2428 		if (desc->status == DONE)
2429 			transferred = desc->bytes_requested;
2430 		else if (running && desc == running)
2431 			transferred =
2432 				pl330_get_current_xferred_count(pch, desc);
2433 		else if (desc->status == BUSY || desc->status == PAUSED)
2434 			/*
2435 			 * Busy but not running means either just enqueued,
2436 			 * or finished and not yet marked done
2437 			 */
2438 			if (desc == last_enq)
2439 				transferred = 0;
2440 			else
2441 				transferred = desc->bytes_requested;
2442 		else
2443 			transferred = 0;
2444 		residual += desc->bytes_requested - transferred;
2445 		if (desc->txd.cookie == cookie) {
2446 			switch (desc->status) {
2447 			case DONE:
2448 				ret = DMA_COMPLETE;
2449 				break;
2450 			case PAUSED:
2451 				ret = DMA_PAUSED;
2452 				break;
2453 			case PREP:
2454 			case BUSY:
2455 				ret = DMA_IN_PROGRESS;
2456 				break;
2457 			default:
2458 				WARN_ON(1);
2459 			}
2460 			break;
2461 		}
2462 		if (desc->last)
2463 			residual = 0;
2464 	}
2465 	spin_unlock(&pch->thread->dmac->lock);
2466 	spin_unlock_irqrestore(&pch->lock, flags);
2467 
2468 out:
2469 	dma_set_residue(txstate, residual);
2470 
2471 	return ret;
2472 }
2473 
2474 static void pl330_issue_pending(struct dma_chan *chan)
2475 {
2476 	struct dma_pl330_chan *pch = to_pchan(chan);
2477 	unsigned long flags;
2478 
2479 	spin_lock_irqsave(&pch->lock, flags);
2480 	if (list_empty(&pch->work_list)) {
2481 		/*
2482 		 * Warn on nothing pending. Empty submitted_list may
2483 		 * break our pm_runtime usage counter as it is
2484 		 * updated on work_list emptiness status.
2485 		 */
2486 		WARN_ON(list_empty(&pch->submitted_list));
2487 		pch->active = true;
2488 		pm_runtime_get_sync(pch->dmac->ddma.dev);
2489 	}
2490 	list_splice_tail_init(&pch->submitted_list, &pch->work_list);
2491 	spin_unlock_irqrestore(&pch->lock, flags);
2492 
2493 	pl330_tasklet(&pch->task);
2494 }
2495 
2496 /*
2497  * We returned the last one of the circular list of descriptor(s)
2498  * from prep_xxx, so the argument to submit corresponds to the last
2499  * descriptor of the list.
2500  */
2501 static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
2502 {
2503 	struct dma_pl330_desc *desc, *last = to_desc(tx);
2504 	struct dma_pl330_chan *pch = to_pchan(tx->chan);
2505 	dma_cookie_t cookie;
2506 	unsigned long flags;
2507 
2508 	spin_lock_irqsave(&pch->lock, flags);
2509 
2510 	/* Assign cookies to all nodes */
2511 	while (!list_empty(&last->node)) {
2512 		desc = list_entry(last->node.next, struct dma_pl330_desc, node);
2513 		if (pch->cyclic) {
2514 			desc->txd.callback = last->txd.callback;
2515 			desc->txd.callback_param = last->txd.callback_param;
2516 		}
2517 		desc->last = false;
2518 
2519 		dma_cookie_assign(&desc->txd);
2520 
2521 		list_move_tail(&desc->node, &pch->submitted_list);
2522 	}
2523 
2524 	last->last = true;
2525 	cookie = dma_cookie_assign(&last->txd);
2526 	list_add_tail(&last->node, &pch->submitted_list);
2527 	spin_unlock_irqrestore(&pch->lock, flags);
2528 
2529 	return cookie;
2530 }
2531 
2532 static inline void _init_desc(struct dma_pl330_desc *desc)
2533 {
2534 	desc->rqcfg.swap = SWAP_NO;
2535 	desc->rqcfg.scctl = CCTRL0;
2536 	desc->rqcfg.dcctl = CCTRL0;
2537 	desc->txd.tx_submit = pl330_tx_submit;
2538 
2539 	INIT_LIST_HEAD(&desc->node);
2540 }
2541 
2542 /* Returns the number of descriptors added to the DMAC pool */
2543 static int add_desc(struct list_head *pool, spinlock_t *lock,
2544 		    gfp_t flg, int count)
2545 {
2546 	struct dma_pl330_desc *desc;
2547 	unsigned long flags;
2548 	int i;
2549 
2550 	desc = kzalloc_objs(*desc, count, flg);
2551 	if (!desc)
2552 		return 0;
2553 
2554 	spin_lock_irqsave(lock, flags);
2555 
2556 	for (i = 0; i < count; i++) {
2557 		_init_desc(&desc[i]);
2558 		list_add_tail(&desc[i].node, pool);
2559 	}
2560 
2561 	spin_unlock_irqrestore(lock, flags);
2562 
2563 	return count;
2564 }
2565 
2566 static struct dma_pl330_desc *pluck_desc(struct list_head *pool,
2567 					 spinlock_t *lock)
2568 {
2569 	struct dma_pl330_desc *desc = NULL;
2570 	unsigned long flags;
2571 
2572 	spin_lock_irqsave(lock, flags);
2573 
2574 	if (!list_empty(pool)) {
2575 		desc = list_entry(pool->next,
2576 				struct dma_pl330_desc, node);
2577 
2578 		list_del_init(&desc->node);
2579 
2580 		desc->status = PREP;
2581 		desc->txd.callback = NULL;
2582 		desc->txd.callback_result = NULL;
2583 	}
2584 
2585 	spin_unlock_irqrestore(lock, flags);
2586 
2587 	return desc;
2588 }
2589 
2590 static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
2591 {
2592 	struct pl330_dmac *pl330 = pch->dmac;
2593 	u8 *peri_id = pch->chan.private;
2594 	struct dma_pl330_desc *desc;
2595 
2596 	/* Pluck one desc from the pool of DMAC */
2597 	desc = pluck_desc(&pl330->desc_pool, &pl330->pool_lock);
2598 
2599 	/* If the DMAC pool is empty, alloc new */
2600 	if (!desc) {
2601 		static DEFINE_SPINLOCK(lock);
2602 		LIST_HEAD(pool);
2603 
2604 		if (!add_desc(&pool, &lock, GFP_ATOMIC, 1))
2605 			return NULL;
2606 
2607 		desc = pluck_desc(&pool, &lock);
2608 		WARN_ON(!desc || !list_empty(&pool));
2609 	}
2610 
2611 	/* Initialize the descriptor */
2612 	desc->pchan = pch;
2613 	desc->txd.cookie = 0;
2614 	async_tx_ack(&desc->txd);
2615 
2616 	desc->peri = peri_id ? pch->chan.chan_id : 0;
2617 	desc->rqcfg.pcfg = &pch->dmac->pcfg;
2618 
2619 	dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
2620 
2621 	return desc;
2622 }
2623 
2624 static inline void fill_px(struct pl330_xfer *px,
2625 		dma_addr_t dst, dma_addr_t src, size_t len)
2626 {
2627 	px->bytes = len;
2628 	px->dst_addr = dst;
2629 	px->src_addr = src;
2630 }
2631 
2632 static struct dma_pl330_desc *
2633 __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
2634 		dma_addr_t src, size_t len)
2635 {
2636 	struct dma_pl330_desc *desc = pl330_get_desc(pch);
2637 
2638 	if (!desc) {
2639 		dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
2640 			__func__, __LINE__);
2641 		return NULL;
2642 	}
2643 
2644 	/*
2645 	 * Ideally we should lookout for reqs bigger than
2646 	 * those that can be programmed with 256 bytes of
2647 	 * MC buffer, but considering a req size is seldom
2648 	 * going to be word-unaligned and more than 200MB,
2649 	 * we take it easy.
2650 	 * Also, should the limit is reached we'd rather
2651 	 * have the platform increase MC buffer size than
2652 	 * complicating this API driver.
2653 	 */
2654 	fill_px(&desc->px, dst, src, len);
2655 
2656 	return desc;
2657 }
2658 
2659 /* Call after fixing burst size */
2660 static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
2661 {
2662 	struct dma_pl330_chan *pch = desc->pchan;
2663 	struct pl330_dmac *pl330 = pch->dmac;
2664 	int burst_len;
2665 
2666 	burst_len = pl330->pcfg.data_bus_width / 8;
2667 	burst_len *= pl330->pcfg.data_buf_dep / pl330->pcfg.num_chan;
2668 	burst_len >>= desc->rqcfg.brst_size;
2669 
2670 	/* src/dst_burst_len can't be more than 16 */
2671 	if (burst_len > PL330_MAX_BURST)
2672 		burst_len = PL330_MAX_BURST;
2673 
2674 	return burst_len;
2675 }
2676 
2677 static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
2678 		struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
2679 		size_t period_len, enum dma_transfer_direction direction,
2680 		unsigned long flags)
2681 {
2682 	struct dma_pl330_desc *desc = NULL, *first = NULL;
2683 	struct dma_pl330_chan *pch = to_pchan(chan);
2684 	struct pl330_dmac *pl330 = pch->dmac;
2685 	unsigned int i;
2686 	dma_addr_t dst;
2687 	dma_addr_t src;
2688 
2689 	if (len % period_len != 0)
2690 		return NULL;
2691 
2692 	if (!is_slave_direction(direction)) {
2693 		dev_err(pch->dmac->ddma.dev, "%s:%d Invalid dma direction\n",
2694 		__func__, __LINE__);
2695 		return NULL;
2696 	}
2697 
2698 	pl330_config_write(chan, &pch->slave_config, direction);
2699 
2700 	if (!pl330_prep_slave_fifo(pch, direction))
2701 		return NULL;
2702 
2703 	for (i = 0; i < len / period_len; i++) {
2704 		desc = pl330_get_desc(pch);
2705 		if (!desc) {
2706 			unsigned long iflags;
2707 
2708 			dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
2709 				__func__, __LINE__);
2710 
2711 			if (!first)
2712 				return NULL;
2713 
2714 			spin_lock_irqsave(&pl330->pool_lock, iflags);
2715 
2716 			while (!list_empty(&first->node)) {
2717 				desc = list_entry(first->node.next,
2718 						struct dma_pl330_desc, node);
2719 				list_move_tail(&desc->node, &pl330->desc_pool);
2720 			}
2721 
2722 			list_move_tail(&first->node, &pl330->desc_pool);
2723 
2724 			spin_unlock_irqrestore(&pl330->pool_lock, iflags);
2725 
2726 			return NULL;
2727 		}
2728 
2729 		switch (direction) {
2730 		case DMA_MEM_TO_DEV:
2731 			desc->rqcfg.src_inc = 1;
2732 			desc->rqcfg.dst_inc = 0;
2733 			src = dma_addr;
2734 			dst = pch->fifo_dma;
2735 			break;
2736 		case DMA_DEV_TO_MEM:
2737 			desc->rqcfg.src_inc = 0;
2738 			desc->rqcfg.dst_inc = 1;
2739 			src = pch->fifo_dma;
2740 			dst = dma_addr;
2741 			break;
2742 		default:
2743 			break;
2744 		}
2745 
2746 		desc->rqtype = direction;
2747 		desc->rqcfg.brst_size = pch->burst_sz;
2748 		desc->rqcfg.brst_len = pch->burst_len;
2749 		desc->bytes_requested = period_len;
2750 		fill_px(&desc->px, dst, src, period_len);
2751 
2752 		if (!first)
2753 			first = desc;
2754 		else
2755 			list_add_tail(&desc->node, &first->node);
2756 
2757 		dma_addr += period_len;
2758 	}
2759 
2760 	if (!desc)
2761 		return NULL;
2762 
2763 	pch->cyclic = true;
2764 
2765 	return &desc->txd;
2766 }
2767 
2768 static struct dma_async_tx_descriptor *
2769 pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
2770 		dma_addr_t src, size_t len, unsigned long flags)
2771 {
2772 	struct dma_pl330_desc *desc;
2773 	struct dma_pl330_chan *pch = to_pchan(chan);
2774 	struct pl330_dmac *pl330;
2775 	int burst;
2776 
2777 	if (unlikely(!pch || !len))
2778 		return NULL;
2779 
2780 	pl330 = pch->dmac;
2781 
2782 	desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
2783 	if (!desc)
2784 		return NULL;
2785 
2786 	desc->rqcfg.src_inc = 1;
2787 	desc->rqcfg.dst_inc = 1;
2788 	desc->rqtype = DMA_MEM_TO_MEM;
2789 
2790 	/* Select max possible burst size */
2791 	burst = pl330->pcfg.data_bus_width / 8;
2792 
2793 	/*
2794 	 * Make sure we use a burst size that aligns with all the memcpy
2795 	 * parameters because our DMA programming algorithm doesn't cope with
2796 	 * transfers which straddle an entry in the DMA device's MFIFO.
2797 	 */
2798 	while ((src | dst | len) & (burst - 1))
2799 		burst /= 2;
2800 
2801 	desc->rqcfg.brst_size = 0;
2802 	while (burst != (1 << desc->rqcfg.brst_size))
2803 		desc->rqcfg.brst_size++;
2804 
2805 	desc->rqcfg.brst_len = get_burst_len(desc, len);
2806 	/*
2807 	 * If burst size is smaller than bus width then make sure we only
2808 	 * transfer one at a time to avoid a burst stradling an MFIFO entry.
2809 	 */
2810 	if (burst * 8 < pl330->pcfg.data_bus_width)
2811 		desc->rqcfg.brst_len = 1;
2812 
2813 	desc->bytes_requested = len;
2814 
2815 	return &desc->txd;
2816 }
2817 
2818 static void __pl330_giveback_desc(struct pl330_dmac *pl330,
2819 				  struct dma_pl330_desc *first)
2820 {
2821 	unsigned long flags;
2822 	struct dma_pl330_desc *desc;
2823 
2824 	if (!first)
2825 		return;
2826 
2827 	spin_lock_irqsave(&pl330->pool_lock, flags);
2828 
2829 	while (!list_empty(&first->node)) {
2830 		desc = list_entry(first->node.next,
2831 				struct dma_pl330_desc, node);
2832 		list_move_tail(&desc->node, &pl330->desc_pool);
2833 	}
2834 
2835 	list_move_tail(&first->node, &pl330->desc_pool);
2836 
2837 	spin_unlock_irqrestore(&pl330->pool_lock, flags);
2838 }
2839 
2840 static struct dma_async_tx_descriptor *
2841 pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2842 		unsigned int sg_len, enum dma_transfer_direction direction,
2843 		unsigned long flg, void *context)
2844 {
2845 	struct dma_pl330_desc *first, *desc = NULL;
2846 	struct dma_pl330_chan *pch = to_pchan(chan);
2847 	struct scatterlist *sg;
2848 	int i;
2849 
2850 	if (unlikely(!pch || !sgl || !sg_len))
2851 		return NULL;
2852 
2853 	pl330_config_write(chan, &pch->slave_config, direction);
2854 
2855 	if (!pl330_prep_slave_fifo(pch, direction))
2856 		return NULL;
2857 
2858 	first = NULL;
2859 
2860 	for_each_sg(sgl, sg, sg_len, i) {
2861 
2862 		desc = pl330_get_desc(pch);
2863 		if (!desc) {
2864 			struct pl330_dmac *pl330 = pch->dmac;
2865 
2866 			dev_err(pch->dmac->ddma.dev,
2867 				"%s:%d Unable to fetch desc\n",
2868 				__func__, __LINE__);
2869 			__pl330_giveback_desc(pl330, first);
2870 
2871 			return NULL;
2872 		}
2873 
2874 		if (!first)
2875 			first = desc;
2876 		else
2877 			list_add_tail(&desc->node, &first->node);
2878 
2879 		if (direction == DMA_MEM_TO_DEV) {
2880 			desc->rqcfg.src_inc = 1;
2881 			desc->rqcfg.dst_inc = 0;
2882 			fill_px(&desc->px, pch->fifo_dma, sg_dma_address(sg),
2883 				sg_dma_len(sg));
2884 		} else {
2885 			desc->rqcfg.src_inc = 0;
2886 			desc->rqcfg.dst_inc = 1;
2887 			fill_px(&desc->px, sg_dma_address(sg), pch->fifo_dma,
2888 				sg_dma_len(sg));
2889 		}
2890 
2891 		desc->rqcfg.brst_size = pch->burst_sz;
2892 		desc->rqcfg.brst_len = pch->burst_len;
2893 		desc->rqtype = direction;
2894 		desc->bytes_requested = sg_dma_len(sg);
2895 	}
2896 
2897 	/* Return the last desc in the chain */
2898 	return &desc->txd;
2899 }
2900 
2901 static irqreturn_t pl330_irq_handler(int irq, void *data)
2902 {
2903 	if (pl330_update(data))
2904 		return IRQ_HANDLED;
2905 	else
2906 		return IRQ_NONE;
2907 }
2908 
2909 #define PL330_DMA_BUSWIDTHS \
2910 	BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
2911 	BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
2912 	BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
2913 	BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
2914 	BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
2915 
2916 #ifdef CONFIG_DEBUG_FS
2917 static int pl330_debugfs_show(struct seq_file *s, void *data)
2918 {
2919 	struct pl330_dmac *pl330 = s->private;
2920 	int chans, pchs, ch, pr;
2921 
2922 	chans = pl330->pcfg.num_chan;
2923 	pchs = pl330->num_peripherals;
2924 
2925 	seq_puts(s, "PL330 physical channels:\n");
2926 	seq_puts(s, "THREAD:\t\tCHANNEL:\n");
2927 	seq_puts(s, "--------\t-----\n");
2928 	for (ch = 0; ch < chans; ch++) {
2929 		struct pl330_thread *thrd = &pl330->channels[ch];
2930 		int found = -1;
2931 
2932 		for (pr = 0; pr < pchs; pr++) {
2933 			struct dma_pl330_chan *pch = &pl330->peripherals[pr];
2934 
2935 			if (!pch->thread || thrd->id != pch->thread->id)
2936 				continue;
2937 
2938 			found = pr;
2939 		}
2940 
2941 		seq_printf(s, "%d\t\t", thrd->id);
2942 		if (found == -1)
2943 			seq_puts(s, "--\n");
2944 		else
2945 			seq_printf(s, "%d\n", found);
2946 	}
2947 
2948 	return 0;
2949 }
2950 
2951 DEFINE_SHOW_ATTRIBUTE(pl330_debugfs);
2952 
2953 static inline void init_pl330_debugfs(struct pl330_dmac *pl330)
2954 {
2955 	debugfs_create_file(dev_name(pl330->ddma.dev),
2956 			    S_IFREG | 0444, NULL, pl330,
2957 			    &pl330_debugfs_fops);
2958 }
2959 #else
2960 static inline void init_pl330_debugfs(struct pl330_dmac *pl330)
2961 {
2962 }
2963 #endif
2964 
2965 /*
2966  * Runtime PM callbacks are provided by amba/bus.c driver.
2967  *
2968  * It is assumed here that IRQ safe runtime PM is chosen in probe and amba
2969  * bus driver will only disable/enable the clock in runtime PM callbacks.
2970  */
2971 static int __maybe_unused pl330_suspend(struct device *dev)
2972 {
2973 	struct amba_device *pcdev = to_amba_device(dev);
2974 
2975 	pm_runtime_force_suspend(dev);
2976 	clk_unprepare(pcdev->pclk);
2977 
2978 	return 0;
2979 }
2980 
2981 static int __maybe_unused pl330_resume(struct device *dev)
2982 {
2983 	struct amba_device *pcdev = to_amba_device(dev);
2984 	int ret;
2985 
2986 	ret = clk_prepare(pcdev->pclk);
2987 	if (ret)
2988 		return ret;
2989 
2990 	pm_runtime_force_resume(dev);
2991 
2992 	return ret;
2993 }
2994 
2995 static const struct dev_pm_ops pl330_pm = {
2996 	SET_LATE_SYSTEM_SLEEP_PM_OPS(pl330_suspend, pl330_resume)
2997 };
2998 
2999 static int
3000 pl330_probe(struct amba_device *adev, const struct amba_id *id)
3001 {
3002 	struct pl330_config *pcfg;
3003 	struct pl330_dmac *pl330;
3004 	struct dma_pl330_chan *pch, *_p;
3005 	struct dma_device *pd;
3006 	struct resource *res;
3007 	int i, ret, irq;
3008 	int num_chan;
3009 	struct device_node *np = adev->dev.of_node;
3010 
3011 	ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
3012 	if (ret)
3013 		return ret;
3014 
3015 	/* Allocate a new DMAC and its Channels */
3016 	pl330 = devm_kzalloc(&adev->dev, sizeof(*pl330), GFP_KERNEL);
3017 	if (!pl330)
3018 		return -ENOMEM;
3019 
3020 	pd = &pl330->ddma;
3021 	pd->dev = &adev->dev;
3022 
3023 	pl330->mcbufsz = 0;
3024 
3025 	/* get quirk */
3026 	for (i = 0; i < ARRAY_SIZE(of_quirks); i++)
3027 		if (of_property_read_bool(np, of_quirks[i].quirk))
3028 			pl330->quirks |= of_quirks[i].id;
3029 
3030 	res = &adev->res;
3031 	pl330->base = devm_ioremap_resource(&adev->dev, res);
3032 	if (IS_ERR(pl330->base))
3033 		return PTR_ERR(pl330->base);
3034 
3035 	amba_set_drvdata(adev, pl330);
3036 
3037 	pl330->rstc = devm_reset_control_get_optional(&adev->dev, "dma");
3038 	if (IS_ERR(pl330->rstc)) {
3039 		return dev_err_probe(&adev->dev, PTR_ERR(pl330->rstc), "Failed to get reset!\n");
3040 	} else {
3041 		ret = reset_control_deassert(pl330->rstc);
3042 		if (ret) {
3043 			dev_err(&adev->dev, "Couldn't deassert the device from reset!\n");
3044 			return ret;
3045 		}
3046 	}
3047 
3048 	pl330->rstc_ocp = devm_reset_control_get_optional(&adev->dev, "dma-ocp");
3049 	if (IS_ERR(pl330->rstc_ocp)) {
3050 		return dev_err_probe(&adev->dev, PTR_ERR(pl330->rstc_ocp),
3051 				     "Failed to get OCP reset!\n");
3052 	} else {
3053 		ret = reset_control_deassert(pl330->rstc_ocp);
3054 		if (ret) {
3055 			dev_err(&adev->dev, "Couldn't deassert the device from OCP reset!\n");
3056 			return ret;
3057 		}
3058 	}
3059 
3060 	for (i = 0; i < AMBA_NR_IRQS; i++) {
3061 		irq = adev->irq[i];
3062 		if (irq) {
3063 			ret = devm_request_irq(&adev->dev, irq,
3064 					       pl330_irq_handler, 0,
3065 					       dev_name(&adev->dev), pl330);
3066 			if (ret)
3067 				return ret;
3068 		} else {
3069 			break;
3070 		}
3071 	}
3072 
3073 	pcfg = &pl330->pcfg;
3074 
3075 	pcfg->periph_id = adev->periphid;
3076 	ret = pl330_add(pl330);
3077 	if (ret)
3078 		return ret;
3079 
3080 	INIT_LIST_HEAD(&pl330->desc_pool);
3081 	spin_lock_init(&pl330->pool_lock);
3082 
3083 	/* Create a descriptor pool of default size */
3084 	if (!add_desc(&pl330->desc_pool, &pl330->pool_lock,
3085 		      GFP_KERNEL, NR_DEFAULT_DESC))
3086 		dev_warn(&adev->dev, "unable to allocate desc\n");
3087 
3088 	INIT_LIST_HEAD(&pd->channels);
3089 
3090 	/* Initialize channel parameters */
3091 	num_chan = max_t(int, pcfg->num_peri, pcfg->num_chan);
3092 
3093 	pl330->num_peripherals = num_chan;
3094 
3095 	pl330->peripherals = kzalloc_objs(*pch, num_chan, GFP_KERNEL);
3096 	if (!pl330->peripherals) {
3097 		ret = -ENOMEM;
3098 		goto probe_err2;
3099 	}
3100 
3101 	for (i = 0; i < num_chan; i++) {
3102 		pch = &pl330->peripherals[i];
3103 
3104 		pch->chan.private = adev->dev.of_node;
3105 		INIT_LIST_HEAD(&pch->submitted_list);
3106 		INIT_LIST_HEAD(&pch->work_list);
3107 		INIT_LIST_HEAD(&pch->completed_list);
3108 		spin_lock_init(&pch->lock);
3109 		pch->thread = NULL;
3110 		pch->chan.device = pd;
3111 		pch->dmac = pl330;
3112 		pch->dir = DMA_NONE;
3113 
3114 		/* Add the channel to the DMAC list */
3115 		list_add_tail(&pch->chan.device_node, &pd->channels);
3116 	}
3117 
3118 	dma_cap_set(DMA_MEMCPY, pd->cap_mask);
3119 	if (pcfg->num_peri) {
3120 		dma_cap_set(DMA_SLAVE, pd->cap_mask);
3121 		dma_cap_set(DMA_CYCLIC, pd->cap_mask);
3122 		dma_cap_set(DMA_PRIVATE, pd->cap_mask);
3123 	}
3124 
3125 	pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
3126 	pd->device_free_chan_resources = pl330_free_chan_resources;
3127 	pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
3128 	pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
3129 	pd->device_tx_status = pl330_tx_status;
3130 	pd->device_prep_slave_sg = pl330_prep_slave_sg;
3131 	pd->device_config = pl330_config;
3132 	pd->device_pause = pl330_pause;
3133 	pd->device_terminate_all = pl330_terminate_all;
3134 	pd->device_issue_pending = pl330_issue_pending;
3135 	pd->src_addr_widths = PL330_DMA_BUSWIDTHS;
3136 	pd->dst_addr_widths = PL330_DMA_BUSWIDTHS;
3137 	pd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
3138 	pd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
3139 	pd->max_burst = PL330_MAX_BURST;
3140 
3141 	ret = dma_async_device_register(pd);
3142 	if (ret) {
3143 		dev_err(&adev->dev, "unable to register DMAC\n");
3144 		goto probe_err3;
3145 	}
3146 
3147 	if (adev->dev.of_node) {
3148 		ret = of_dma_controller_register(adev->dev.of_node,
3149 					 of_dma_pl330_xlate, pl330);
3150 		if (ret) {
3151 			dev_err(&adev->dev,
3152 			"unable to register DMA to the generic DT DMA helpers\n");
3153 		}
3154 	}
3155 
3156 	/*
3157 	 * This is the limit for transfers with a buswidth of 1, larger
3158 	 * buswidths will have larger limits.
3159 	 */
3160 	dma_set_max_seg_size(&adev->dev, 1900800);
3161 
3162 	init_pl330_debugfs(pl330);
3163 	dev_info(&adev->dev,
3164 		"Loaded driver for PL330 DMAC-%x\n", adev->periphid);
3165 	dev_info(&adev->dev,
3166 		"\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
3167 		pcfg->data_buf_dep, pcfg->data_bus_width / 8, pcfg->num_chan,
3168 		pcfg->num_peri, pcfg->num_events);
3169 
3170 	pm_runtime_irq_safe(&adev->dev);
3171 	pm_runtime_use_autosuspend(&adev->dev);
3172 	pm_runtime_set_autosuspend_delay(&adev->dev, PL330_AUTOSUSPEND_DELAY);
3173 	pm_runtime_put_autosuspend(&adev->dev);
3174 
3175 	return 0;
3176 probe_err3:
3177 	/* Idle the DMAC */
3178 	list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
3179 			chan.device_node) {
3180 
3181 		/* Remove the channel */
3182 		list_del(&pch->chan.device_node);
3183 
3184 		/* Flush the channel */
3185 		if (pch->thread) {
3186 			pl330_terminate_all(&pch->chan);
3187 			pl330_free_chan_resources(&pch->chan);
3188 		}
3189 	}
3190 probe_err2:
3191 	pl330_del(pl330);
3192 
3193 	if (pl330->rstc_ocp)
3194 		reset_control_assert(pl330->rstc_ocp);
3195 
3196 	if (pl330->rstc)
3197 		reset_control_assert(pl330->rstc);
3198 	return ret;
3199 }
3200 
3201 static void pl330_remove(struct amba_device *adev)
3202 {
3203 	struct pl330_dmac *pl330 = amba_get_drvdata(adev);
3204 	struct dma_pl330_chan *pch, *_p;
3205 	int i, irq;
3206 
3207 	pm_runtime_get_noresume(pl330->ddma.dev);
3208 
3209 	if (adev->dev.of_node)
3210 		of_dma_controller_free(adev->dev.of_node);
3211 
3212 	for (i = 0; i < AMBA_NR_IRQS; i++) {
3213 		irq = adev->irq[i];
3214 		if (irq)
3215 			devm_free_irq(&adev->dev, irq, pl330);
3216 	}
3217 
3218 	dma_async_device_unregister(&pl330->ddma);
3219 
3220 	/* Idle the DMAC */
3221 	list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
3222 			chan.device_node) {
3223 
3224 		/* Remove the channel */
3225 		list_del(&pch->chan.device_node);
3226 
3227 		/* Flush the channel */
3228 		if (pch->thread) {
3229 			pl330_terminate_all(&pch->chan);
3230 			pl330_free_chan_resources(&pch->chan);
3231 		}
3232 	}
3233 
3234 	pl330_del(pl330);
3235 
3236 	if (pl330->rstc_ocp)
3237 		reset_control_assert(pl330->rstc_ocp);
3238 
3239 	if (pl330->rstc)
3240 		reset_control_assert(pl330->rstc);
3241 }
3242 
3243 static const struct amba_id pl330_ids[] = {
3244 	{
3245 		.id	= 0x00041330,
3246 		.mask	= 0x000fffff,
3247 	},
3248 	{ 0, 0 },
3249 };
3250 
3251 MODULE_DEVICE_TABLE(amba, pl330_ids);
3252 
3253 static struct amba_driver pl330_driver = {
3254 	.drv = {
3255 		.name = "dma-pl330",
3256 		.pm = &pl330_pm,
3257 	},
3258 	.id_table = pl330_ids,
3259 	.probe = pl330_probe,
3260 	.remove = pl330_remove,
3261 };
3262 
3263 module_amba_driver(pl330_driver);
3264 
3265 MODULE_AUTHOR("Jaswinder Singh <jassisinghbrar@gmail.com>");
3266 MODULE_DESCRIPTION("API Driver for PL330 DMAC");
3267 MODULE_LICENSE("GPL");
3268