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