pl330.c 66.5 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
J
Jassi Brar 已提交
4 5 6 7 8 9 10 11 12 13
 *
 * Copyright (C) 2010 Samsung Electronics Co. Ltd.
 *	Jaswinder Singh <jassi.brar@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

14
#include <linux/kernel.h>
J
Jassi Brar 已提交
15 16 17 18
#include <linux/io.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
19 20 21 22
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
J
Jassi Brar 已提交
23 24 25
#include <linux/dmaengine.h>
#include <linux/amba/bus.h>
#include <linux/amba/pl330.h>
26
#include <linux/scatterlist.h>
27
#include <linux/of.h>
28
#include <linux/of_dma.h>
29
#include <linux/err.h>
30
#include <linux/pm_runtime.h>
J
Jassi Brar 已提交
31

32
#include "dmaengine.h"
33 34 35 36
#define PL330_MAX_CHAN		8
#define PL330_MAX_IRQS		32
#define PL330_MAX_PERI		32

37 38 39 40 41 42 43 44 45
enum pl330_cachectrl {
	CCTRL0,		/* Noncacheable and nonbufferable */
	CCTRL1,		/* Bufferable only */
	CCTRL2,		/* Cacheable, but do not allocate */
	CCTRL3,		/* Cacheable and bufferable, but do not allocate */
	INVALID1,	/* AWCACHE = 0x1000 */
	INVALID2,
	CCTRL6,		/* Cacheable write-through, allocate on writes only */
	CCTRL7,		/* Cacheable write-back, allocate on writes only */
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
};

enum pl330_byteswap {
	SWAP_NO,
	SWAP_2,
	SWAP_4,
	SWAP_8,
	SWAP_16,
};

/* Register and Bit field Definitions */
#define DS			0x0
#define DS_ST_STOP		0x0
#define DS_ST_EXEC		0x1
#define DS_ST_CMISS		0x2
#define DS_ST_UPDTPC		0x3
#define DS_ST_WFE		0x4
#define DS_ST_ATBRR		0x5
#define DS_ST_QBUSY		0x6
#define DS_ST_WFP		0x7
#define DS_ST_KILL		0x8
#define DS_ST_CMPLT		0x9
#define DS_ST_FLTCMP		0xe
#define DS_ST_FAULT		0xf

#define DPC			0x4
#define INTEN			0x20
#define ES			0x24
#define INTSTATUS		0x28
#define INTCLR			0x2c
#define FSM			0x30
#define FSC			0x34
#define FTM			0x38

#define _FTC			0x40
#define FTC(n)			(_FTC + (n)*0x4)

#define _CS			0x100
#define CS(n)			(_CS + (n)*0x8)
#define CS_CNS			(1 << 21)

#define _CPC			0x104
#define CPC(n)			(_CPC + (n)*0x8)

#define _SA			0x400
#define SA(n)			(_SA + (n)*0x20)

#define _DA			0x404
#define DA(n)			(_DA + (n)*0x20)

#define _CC			0x408
#define CC(n)			(_CC + (n)*0x20)

#define CC_SRCINC		(1 << 0)
#define CC_DSTINC		(1 << 14)
#define CC_SRCPRI		(1 << 8)
#define CC_DSTPRI		(1 << 22)
#define CC_SRCNS		(1 << 9)
#define CC_DSTNS		(1 << 23)
#define CC_SRCIA		(1 << 10)
#define CC_DSTIA		(1 << 24)
#define CC_SRCBRSTLEN_SHFT	4
#define CC_DSTBRSTLEN_SHFT	18
#define CC_SRCBRSTSIZE_SHFT	1
#define CC_DSTBRSTSIZE_SHFT	15
#define CC_SRCCCTRL_SHFT	11
#define CC_SRCCCTRL_MASK	0x7
#define CC_DSTCCTRL_SHFT	25
#define CC_DRCCCTRL_MASK	0x7
#define CC_SWAP_SHFT		28

#define _LC0			0x40c
#define LC0(n)			(_LC0 + (n)*0x20)

#define _LC1			0x410
#define LC1(n)			(_LC1 + (n)*0x20)

#define DBGSTATUS		0xd00
#define DBG_BUSY		(1 << 0)

#define DBGCMD			0xd04
#define DBGINST0		0xd08
#define DBGINST1		0xd0c

#define CR0			0xe00
#define CR1			0xe04
#define CR2			0xe08
#define CR3			0xe0c
#define CR4			0xe10
#define CRD			0xe14

#define PERIPH_ID		0xfe0
138 139 140 141 142
#define PERIPH_REV_SHIFT	20
#define PERIPH_REV_MASK		0xf
#define PERIPH_REV_R0P0		0
#define PERIPH_REV_R1P0		1
#define PERIPH_REV_R1P1		2
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

#define CR0_PERIPH_REQ_SET	(1 << 0)
#define CR0_BOOT_EN_SET		(1 << 1)
#define CR0_BOOT_MAN_NS		(1 << 2)
#define CR0_NUM_CHANS_SHIFT	4
#define CR0_NUM_CHANS_MASK	0x7
#define CR0_NUM_PERIPH_SHIFT	12
#define CR0_NUM_PERIPH_MASK	0x1f
#define CR0_NUM_EVENTS_SHIFT	17
#define CR0_NUM_EVENTS_MASK	0x1f

#define CR1_ICACHE_LEN_SHIFT	0
#define CR1_ICACHE_LEN_MASK	0x7
#define CR1_NUM_ICACHELINES_SHIFT	4
#define CR1_NUM_ICACHELINES_MASK	0xf

#define CRD_DATA_WIDTH_SHIFT	0
#define CRD_DATA_WIDTH_MASK	0x7
#define CRD_WR_CAP_SHIFT	4
#define CRD_WR_CAP_MASK		0x7
#define CRD_WR_Q_DEP_SHIFT	8
#define CRD_WR_Q_DEP_MASK	0xf
#define CRD_RD_CAP_SHIFT	12
#define CRD_RD_CAP_MASK		0x7
#define CRD_RD_Q_DEP_SHIFT	16
#define CRD_RD_Q_DEP_MASK	0xf
#define CRD_DATA_BUFF_SHIFT	20
#define CRD_DATA_BUFF_MASK	0x3ff

#define PART			0x330
#define DESIGNER		0x41
#define REVISION		0x0
#define INTEG_CFG		0x0
#define PERIPH_ID_VAL		((PART << 0) | (DESIGNER << 12))

#define PL330_STATE_STOPPED		(1 << 0)
#define PL330_STATE_EXECUTING		(1 << 1)
#define PL330_STATE_WFE			(1 << 2)
#define PL330_STATE_FAULTING		(1 << 3)
#define PL330_STATE_COMPLETING		(1 << 4)
#define PL330_STATE_WFP			(1 << 5)
#define PL330_STATE_KILLING		(1 << 6)
#define PL330_STATE_FAULT_COMPLETING	(1 << 7)
#define PL330_STATE_CACHEMISS		(1 << 8)
#define PL330_STATE_UPDTPC		(1 << 9)
#define PL330_STATE_ATBARRIER		(1 << 10)
#define PL330_STATE_QUEUEBUSY		(1 << 11)
#define PL330_STATE_INVALID		(1 << 15)

#define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
				| PL330_STATE_WFE | PL330_STATE_FAULTING)

#define CMD_DMAADDH		0x54
#define CMD_DMAEND		0x00
#define CMD_DMAFLUSHP		0x35
#define CMD_DMAGO		0xa0
#define CMD_DMALD		0x04
#define CMD_DMALDP		0x25
#define CMD_DMALP		0x20
#define CMD_DMALPEND		0x28
#define CMD_DMAKILL		0x01
#define CMD_DMAMOV		0xbc
#define CMD_DMANOP		0x18
#define CMD_DMARMB		0x12
#define CMD_DMASEV		0x34
#define CMD_DMAST		0x08
#define CMD_DMASTP		0x29
#define CMD_DMASTZ		0x0c
#define CMD_DMAWFE		0x36
#define CMD_DMAWFP		0x30
#define CMD_DMAWMB		0x13

#define SZ_DMAADDH		3
#define SZ_DMAEND		1
#define SZ_DMAFLUSHP		2
#define SZ_DMALD		1
#define SZ_DMALDP		2
#define SZ_DMALP		2
#define SZ_DMALPEND		2
#define SZ_DMAKILL		1
#define SZ_DMAMOV		6
#define SZ_DMANOP		1
#define SZ_DMARMB		1
#define SZ_DMASEV		2
#define SZ_DMAST		1
#define SZ_DMASTP		2
#define SZ_DMASTZ		1
#define SZ_DMAWFE		2
#define SZ_DMAWFP		2
#define SZ_DMAWMB		1
#define SZ_DMAGO		6

#define BRST_LEN(ccr)		((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
#define BRST_SIZE(ccr)		(1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))

#define BYTE_TO_BURST(b, ccr)	((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
#define BURST_TO_BYTE(c, ccr)	((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))

/*
 * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
 * at 1byte/burst for P<->M and M<->M respectively.
 * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
 * should be enough for P<->M and M<->M respectively.
 */
#define MCODE_BUFF_PER_REQ	256

/* Use this _only_ to wait on transient states */
#define UNTIL(t, s)	while (!(_state(t) & (s))) cpu_relax();

#ifdef PL330_DEBUG_MCGEN
static unsigned cmd_line;
#define PL330_DBGCMD_DUMP(off, x...)	do { \
						printk("%x:", cmd_line); \
						printk(x); \
						cmd_line += off; \
					} while (0)
#define PL330_DBGMC_START(addr)		(cmd_line = addr)
#else
#define PL330_DBGCMD_DUMP(off, x...)	do {} while (0)
#define PL330_DBGMC_START(addr)		do {} while (0)
#endif

/* The number of default descriptors */
266

J
Jassi Brar 已提交
267 268
#define NR_DEFAULT_DESC	16

269 270 271
/* Delay for runtime PM autosuspend, ms */
#define PL330_AUTOSUSPEND_DELAY 20

272 273 274 275 276 277
/* Populated by the PL330 core driver for DMA API driver's info */
struct pl330_config {
	u32	periph_id;
#define DMAC_MODE_NS	(1 << 0)
	unsigned int	mode;
	unsigned int	data_bus_width:10; /* In number of bits */
278
	unsigned int	data_buf_dep:11;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	unsigned int	num_chan:4;
	unsigned int	num_peri:6;
	u32		peri_ns;
	unsigned int	num_events:6;
	u32		irq_ns;
};

/**
 * Request Configuration.
 * The PL330 core does not modify this and uses the last
 * working configuration if the request doesn't provide any.
 *
 * The Client may want to provide this info only for the
 * first request and a request with new settings.
 */
struct pl330_reqcfg {
	/* Address Incrementing */
	unsigned dst_inc:1;
	unsigned src_inc:1;

	/*
	 * For now, the SRC & DST protection levels
	 * and burst size/length are assumed same.
	 */
	bool nonsecure;
	bool privileged;
	bool insnaccess;
	unsigned brst_len:5;
	unsigned brst_size:3; /* in power of 2 */

309 310
	enum pl330_cachectrl dcctl;
	enum pl330_cachectrl scctl;
311
	enum pl330_byteswap swap;
312
	struct pl330_config *pcfg;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
};

/*
 * One cycle of DMAC operation.
 * There may be more than one xfer in a request.
 */
struct pl330_xfer {
	u32 src_addr;
	u32 dst_addr;
	/* Size to xfer */
	u32 bytes;
};

/* The xfer callbacks are made with one of these arguments. */
enum pl330_op_err {
	/* The all xfers in the request were success. */
	PL330_ERR_NONE,
	/* If req aborted due to global error. */
	PL330_ERR_ABORT,
	/* If req failed due to problem with Channel. */
	PL330_ERR_FAIL,
};

enum dmamov_dst {
	SAR = 0,
	CCR,
	DAR,
};

enum pl330_dst {
	SRC = 0,
	DST,
};

enum pl330_cond {
	SINGLE,
	BURST,
	ALWAYS,
};

353 354
struct dma_pl330_desc;

355 356 357
struct _pl330_req {
	u32 mc_bus;
	void *mc_cpu;
358
	struct dma_pl330_desc *desc;
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
};

/* ToBeDone for tasklet */
struct _pl330_tbd {
	bool reset_dmac;
	bool reset_mngr;
	u8 reset_chan;
};

/* A DMAC Thread */
struct pl330_thread {
	u8 id;
	int ev;
	/* If the channel is not yet acquired by any client */
	bool free;
	/* Parent DMAC */
	struct pl330_dmac *dmac;
	/* Only two at a time */
	struct _pl330_req req[2];
	/* Index of the last enqueued request */
	unsigned lstenq;
	/* Index of the last submitted request or -1 if the DMA is stopped */
	int req_running;
};

enum pl330_dmac_state {
	UNINIT,
	INIT,
	DYING,
};

J
Jassi Brar 已提交
390 391 392 393
enum desc_status {
	/* In the DMAC pool */
	FREE,
	/*
M
Masanari Iida 已提交
394
	 * Allocated to some channel during prep_xxx
J
Jassi Brar 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	 * Also may be sitting on the work_list.
	 */
	PREP,
	/*
	 * Sitting on the work_list and already submitted
	 * to the PL330 core. Not more than two descriptors
	 * of a channel can be BUSY at any time.
	 */
	BUSY,
	/*
	 * Sitting on the channel work_list but xfer done
	 * by PL330 core
	 */
	DONE,
};

struct dma_pl330_chan {
	/* Schedule desc completion */
	struct tasklet_struct task;

	/* DMA-Engine Channel */
	struct dma_chan chan;

418 419 420
	/* List of submitted descriptors */
	struct list_head submitted_list;
	/* List of issued descriptors */
J
Jassi Brar 已提交
421
	struct list_head work_list;
422 423
	/* List of completed descriptors */
	struct list_head completed_list;
J
Jassi Brar 已提交
424 425 426 427 428 429

	/* Pointer to the DMAC that manages this channel,
	 * NULL if the channel is available to be acquired.
	 * As the parent, this DMAC also provides descriptors
	 * to the channel.
	 */
430
	struct pl330_dmac *dmac;
J
Jassi Brar 已提交
431 432 433 434

	/* To protect channel manipulation */
	spinlock_t lock;

435 436 437
	/*
	 * Hardware channel thread of PL330 DMAC. NULL if the channel is
	 * available.
J
Jassi Brar 已提交
438
	 */
439
	struct pl330_thread *thread;
440 441 442

	/* For D-to-M and M-to-D channels */
	int burst_sz; /* the peripheral fifo width */
443
	int burst_len; /* the number of burst */
444
	dma_addr_t fifo_addr;
445 446 447

	/* for cyclic capability */
	bool cyclic;
J
Jassi Brar 已提交
448 449
};

450
struct pl330_dmac {
J
Jassi Brar 已提交
451 452 453
	/* DMA-Engine Device */
	struct dma_device ddma;

454 455 456
	/* Holds info about sg limitations */
	struct device_dma_parameters dma_parms;

J
Jassi Brar 已提交
457 458 459 460 461
	/* Pool of descriptors available for the DMAC's channels */
	struct list_head desc_pool;
	/* To protect desc_pool manipulation */
	spinlock_t pool_lock;

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	/* Size of MicroCode buffers for each channel. */
	unsigned mcbufsz;
	/* ioremap'ed address of PL330 registers. */
	void __iomem	*base;
	/* Populated by the PL330 core driver during pl330_add */
	struct pl330_config	pcfg;

	spinlock_t		lock;
	/* Maximum possible events/irqs */
	int			events[32];
	/* BUS address of MicroCode buffer */
	dma_addr_t		mcode_bus;
	/* CPU address of MicroCode buffer */
	void			*mcode_cpu;
	/* List of all Channel threads */
	struct pl330_thread	*channels;
	/* Pointer to the MANAGER thread */
	struct pl330_thread	*manager;
	/* To handle bad news in interrupt */
	struct tasklet_struct	tasks;
	struct _pl330_tbd	dmac_tbd;
	/* State of DMAC operation */
	enum pl330_dmac_state	state;
	/* Holds list of reqs with due callbacks */
	struct list_head        req_done;

J
Jassi Brar 已提交
488
	/* Peripheral channels connected to this DMAC */
489
	unsigned int num_peripherals;
490
	struct dma_pl330_chan *peripherals; /* keep at end */
J
Jassi Brar 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
};

struct dma_pl330_desc {
	/* To attach to a queue as child */
	struct list_head node;

	/* Descriptor for the DMA Engine API */
	struct dma_async_tx_descriptor txd;

	/* Xfer for PL330 core */
	struct pl330_xfer px;

	struct pl330_reqcfg rqcfg;

	enum desc_status status;

507 508 509
	int bytes_requested;
	bool last;

J
Jassi Brar 已提交
510 511
	/* The channel which currently holds this desc */
	struct dma_pl330_chan *pchan;
512 513 514 515 516 517 518 519 520 521 522

	enum dma_transfer_direction rqtype;
	/* Index of peripheral for the xfer. */
	unsigned peri:5;
	/* Hook to attach to DMAC's list of reqs with due callback */
	struct list_head rqd;
};

struct _xfer_spec {
	u32 ccr;
	struct dma_pl330_desc *desc;
J
Jassi Brar 已提交
523 524
};

525 526
static inline bool _queue_empty(struct pl330_thread *thrd)
{
527
	return thrd->req[0].desc == NULL && thrd->req[1].desc == NULL;
528 529 530 531
}

static inline bool _queue_full(struct pl330_thread *thrd)
{
532
	return thrd->req[0].desc != NULL && thrd->req[1].desc != NULL;
533 534 535 536
}

static inline bool is_manager(struct pl330_thread *thrd)
{
537
	return thrd->dmac->manager == thrd;
538 539 540 541 542
}

/* If manager of the thread is in Non-Secure mode */
static inline bool _manager_ns(struct pl330_thread *thrd)
{
543
	return (thrd->dmac->pcfg.mode & DMAC_MODE_NS) ? true : false;
544 545
}

546 547 548 549 550
static inline u32 get_revision(u32 periph_id)
{
	return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
}

551 552 553 554 555 556 557 558
static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
		enum pl330_dst da, u16 val)
{
	if (dry_run)
		return SZ_DMAADDH;

	buf[0] = CMD_DMAADDH;
	buf[0] |= (da << 1);
559
	*((__le16 *)&buf[1]) = cpu_to_le16(val);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

	PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
		da == 1 ? "DA" : "SA", val);

	return SZ_DMAADDH;
}

static inline u32 _emit_END(unsigned dry_run, u8 buf[])
{
	if (dry_run)
		return SZ_DMAEND;

	buf[0] = CMD_DMAEND;

	PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");

	return SZ_DMAEND;
}

static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
{
	if (dry_run)
		return SZ_DMAFLUSHP;

	buf[0] = CMD_DMAFLUSHP;

	peri &= 0x1f;
	peri <<= 3;
	buf[1] = peri;

	PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);

	return SZ_DMAFLUSHP;
}

static inline u32 _emit_LD(unsigned dry_run, u8 buf[],	enum pl330_cond cond)
{
	if (dry_run)
		return SZ_DMALD;

	buf[0] = CMD_DMALD;

	if (cond == SINGLE)
		buf[0] |= (0 << 1) | (1 << 0);
	else if (cond == BURST)
		buf[0] |= (1 << 1) | (1 << 0);

	PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
		cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));

	return SZ_DMALD;
}

static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
		enum pl330_cond cond, u8 peri)
{
	if (dry_run)
		return SZ_DMALDP;

	buf[0] = CMD_DMALDP;

	if (cond == BURST)
		buf[0] |= (1 << 1);

	peri &= 0x1f;
	peri <<= 3;
	buf[1] = peri;

	PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
		cond == SINGLE ? 'S' : 'B', peri >> 3);

	return SZ_DMALDP;
}

static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
		unsigned loop, u8 cnt)
{
	if (dry_run)
		return SZ_DMALP;

	buf[0] = CMD_DMALP;

	if (loop)
		buf[0] |= (1 << 1);

	cnt--; /* DMAC increments by 1 internally */
	buf[1] = cnt;

	PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);

	return SZ_DMALP;
}

struct _arg_LPEND {
	enum pl330_cond cond;
	bool forever;
	unsigned loop;
	u8 bjump;
};

static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
		const struct _arg_LPEND *arg)
{
	enum pl330_cond cond = arg->cond;
	bool forever = arg->forever;
	unsigned loop = arg->loop;
	u8 bjump = arg->bjump;

	if (dry_run)
		return SZ_DMALPEND;

	buf[0] = CMD_DMALPEND;

	if (loop)
		buf[0] |= (1 << 2);

	if (!forever)
		buf[0] |= (1 << 4);

	if (cond == SINGLE)
		buf[0] |= (0 << 1) | (1 << 0);
	else if (cond == BURST)
		buf[0] |= (1 << 1) | (1 << 0);

	buf[1] = bjump;

	PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
			forever ? "FE" : "END",
			cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
			loop ? '1' : '0',
			bjump);

	return SZ_DMALPEND;
}

static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
{
	if (dry_run)
		return SZ_DMAKILL;

	buf[0] = CMD_DMAKILL;

	return SZ_DMAKILL;
}

static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
		enum dmamov_dst dst, u32 val)
{
	if (dry_run)
		return SZ_DMAMOV;

	buf[0] = CMD_DMAMOV;
	buf[1] = dst;
713
	*((__le32 *)&buf[2]) = cpu_to_le32(val);
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890

	PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
		dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);

	return SZ_DMAMOV;
}

static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
{
	if (dry_run)
		return SZ_DMANOP;

	buf[0] = CMD_DMANOP;

	PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");

	return SZ_DMANOP;
}

static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
{
	if (dry_run)
		return SZ_DMARMB;

	buf[0] = CMD_DMARMB;

	PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");

	return SZ_DMARMB;
}

static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
{
	if (dry_run)
		return SZ_DMASEV;

	buf[0] = CMD_DMASEV;

	ev &= 0x1f;
	ev <<= 3;
	buf[1] = ev;

	PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);

	return SZ_DMASEV;
}

static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
{
	if (dry_run)
		return SZ_DMAST;

	buf[0] = CMD_DMAST;

	if (cond == SINGLE)
		buf[0] |= (0 << 1) | (1 << 0);
	else if (cond == BURST)
		buf[0] |= (1 << 1) | (1 << 0);

	PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
		cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));

	return SZ_DMAST;
}

static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
		enum pl330_cond cond, u8 peri)
{
	if (dry_run)
		return SZ_DMASTP;

	buf[0] = CMD_DMASTP;

	if (cond == BURST)
		buf[0] |= (1 << 1);

	peri &= 0x1f;
	peri <<= 3;
	buf[1] = peri;

	PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
		cond == SINGLE ? 'S' : 'B', peri >> 3);

	return SZ_DMASTP;
}

static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
{
	if (dry_run)
		return SZ_DMASTZ;

	buf[0] = CMD_DMASTZ;

	PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");

	return SZ_DMASTZ;
}

static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
		unsigned invalidate)
{
	if (dry_run)
		return SZ_DMAWFE;

	buf[0] = CMD_DMAWFE;

	ev &= 0x1f;
	ev <<= 3;
	buf[1] = ev;

	if (invalidate)
		buf[1] |= (1 << 1);

	PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
		ev >> 3, invalidate ? ", I" : "");

	return SZ_DMAWFE;
}

static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
		enum pl330_cond cond, u8 peri)
{
	if (dry_run)
		return SZ_DMAWFP;

	buf[0] = CMD_DMAWFP;

	if (cond == SINGLE)
		buf[0] |= (0 << 1) | (0 << 0);
	else if (cond == BURST)
		buf[0] |= (1 << 1) | (0 << 0);
	else
		buf[0] |= (0 << 1) | (1 << 0);

	peri &= 0x1f;
	peri <<= 3;
	buf[1] = peri;

	PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
		cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);

	return SZ_DMAWFP;
}

static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
{
	if (dry_run)
		return SZ_DMAWMB;

	buf[0] = CMD_DMAWMB;

	PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");

	return SZ_DMAWMB;
}

struct _arg_GO {
	u8 chan;
	u32 addr;
	unsigned ns;
};

static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
		const struct _arg_GO *arg)
{
	u8 chan = arg->chan;
	u32 addr = arg->addr;
	unsigned ns = arg->ns;

	if (dry_run)
		return SZ_DMAGO;

	buf[0] = CMD_DMAGO;
	buf[0] |= (ns << 1);

	buf[1] = chan & 0x7;

891
	*((__le32 *)&buf[2]) = cpu_to_le32(addr);
892 893 894 895 896 897 898 899 900

	return SZ_DMAGO;
}

#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)

/* Returns Time-Out */
static bool _until_dmac_idle(struct pl330_thread *thrd)
{
901
	void __iomem *regs = thrd->dmac->base;
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	unsigned long loops = msecs_to_loops(5);

	do {
		/* Until Manager is Idle */
		if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
			break;

		cpu_relax();
	} while (--loops);

	if (!loops)
		return true;

	return false;
}

static inline void _execute_DBGINSN(struct pl330_thread *thrd,
		u8 insn[], bool as_manager)
{
921
	void __iomem *regs = thrd->dmac->base;
922 923 924 925 926 927 928 929 930
	u32 val;

	val = (insn[0] << 16) | (insn[1] << 24);
	if (!as_manager) {
		val |= (1 << 0);
		val |= (thrd->id << 8); /* Channel Number */
	}
	writel(val, regs + DBGINST0);

931
	val = le32_to_cpu(*((__le32 *)&insn[2]));
932 933 934 935
	writel(val, regs + DBGINST1);

	/* If timed out due to halted state-machine */
	if (_until_dmac_idle(thrd)) {
936
		dev_err(thrd->dmac->ddma.dev, "DMAC halted!\n");
937 938 939 940 941 942 943 944 945
		return;
	}

	/* Get going */
	writel(0, regs + DBGCMD);
}

static inline u32 _state(struct pl330_thread *thrd)
{
946
	void __iomem *regs = thrd->dmac->base;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	u32 val;

	if (is_manager(thrd))
		val = readl(regs + DS) & 0xf;
	else
		val = readl(regs + CS(thrd->id)) & 0xf;

	switch (val) {
	case DS_ST_STOP:
		return PL330_STATE_STOPPED;
	case DS_ST_EXEC:
		return PL330_STATE_EXECUTING;
	case DS_ST_CMISS:
		return PL330_STATE_CACHEMISS;
	case DS_ST_UPDTPC:
		return PL330_STATE_UPDTPC;
	case DS_ST_WFE:
		return PL330_STATE_WFE;
	case DS_ST_FAULT:
		return PL330_STATE_FAULTING;
	case DS_ST_ATBRR:
		if (is_manager(thrd))
			return PL330_STATE_INVALID;
		else
			return PL330_STATE_ATBARRIER;
	case DS_ST_QBUSY:
		if (is_manager(thrd))
			return PL330_STATE_INVALID;
		else
			return PL330_STATE_QUEUEBUSY;
	case DS_ST_WFP:
		if (is_manager(thrd))
			return PL330_STATE_INVALID;
		else
			return PL330_STATE_WFP;
	case DS_ST_KILL:
		if (is_manager(thrd))
			return PL330_STATE_INVALID;
		else
			return PL330_STATE_KILLING;
	case DS_ST_CMPLT:
		if (is_manager(thrd))
			return PL330_STATE_INVALID;
		else
			return PL330_STATE_COMPLETING;
	case DS_ST_FLTCMP:
		if (is_manager(thrd))
			return PL330_STATE_INVALID;
		else
			return PL330_STATE_FAULT_COMPLETING;
	default:
		return PL330_STATE_INVALID;
	}
}

static void _stop(struct pl330_thread *thrd)
{
1004
	void __iomem *regs = thrd->dmac->base;
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
	u8 insn[6] = {0, 0, 0, 0, 0, 0};

	if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
		UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);

	/* Return if nothing needs to be done */
	if (_state(thrd) == PL330_STATE_COMPLETING
		  || _state(thrd) == PL330_STATE_KILLING
		  || _state(thrd) == PL330_STATE_STOPPED)
		return;

	_emit_KILL(0, insn);

	/* Stop generating interrupts for SEV */
	writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);

	_execute_DBGINSN(thrd, insn, is_manager(thrd));
}

/* Start doing req 'idx' of thread 'thrd' */
static bool _trigger(struct pl330_thread *thrd)
{
1027
	void __iomem *regs = thrd->dmac->base;
1028
	struct _pl330_req *req;
1029
	struct dma_pl330_desc *desc;
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	struct _arg_GO go;
	unsigned ns;
	u8 insn[6] = {0, 0, 0, 0, 0, 0};
	int idx;

	/* Return if already ACTIVE */
	if (_state(thrd) != PL330_STATE_STOPPED)
		return true;

	idx = 1 - thrd->lstenq;
1040
	if (thrd->req[idx].desc != NULL) {
1041
		req = &thrd->req[idx];
1042
	} else {
1043
		idx = thrd->lstenq;
1044
		if (thrd->req[idx].desc != NULL)
1045 1046 1047 1048 1049 1050
			req = &thrd->req[idx];
		else
			req = NULL;
	}

	/* Return if no request */
1051
	if (!req)
1052 1053
		return true;

1054 1055 1056 1057
	/* Return if req is running */
	if (idx == thrd->req_running)
		return true;

1058
	desc = req->desc;
1059

1060
	ns = desc->rqcfg.nonsecure ? 1 : 0;
1061 1062 1063

	/* See 'Abort Sources' point-4 at Page 2-25 */
	if (_manager_ns(thrd) && !ns)
1064
		dev_info(thrd->dmac->ddma.dev, "%s:%d Recipe for ABORT!\n",
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
			__func__, __LINE__);

	go.chan = thrd->id;
	go.addr = req->mc_bus;
	go.ns = ns;
	_emit_GO(0, insn, &go);

	/* Set to generate interrupts for SEV */
	writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);

	/* Only manager can execute GO */
	_execute_DBGINSN(thrd, insn, true);

	thrd->req_running = idx;

	return true;
}

static bool _start(struct pl330_thread *thrd)
{
	switch (_state(thrd)) {
	case PL330_STATE_FAULT_COMPLETING:
		UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);

		if (_state(thrd) == PL330_STATE_KILLING)
			UNTIL(thrd, PL330_STATE_STOPPED)

	case PL330_STATE_FAULTING:
		_stop(thrd);

	case PL330_STATE_KILLING:
	case PL330_STATE_COMPLETING:
		UNTIL(thrd, PL330_STATE_STOPPED)

	case PL330_STATE_STOPPED:
		return _trigger(thrd);

	case PL330_STATE_WFP:
	case PL330_STATE_QUEUEBUSY:
	case PL330_STATE_ATBARRIER:
	case PL330_STATE_UPDTPC:
	case PL330_STATE_CACHEMISS:
	case PL330_STATE_EXECUTING:
		return true;

	case PL330_STATE_WFE: /* For RESUME, nothing yet */
	default:
		return false;
	}
}

static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
		const struct _xfer_spec *pxs, int cyc)
{
	int off = 0;
1120
	struct pl330_config *pcfg = pxs->desc->rqcfg.pcfg;
1121

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	/* check lock-up free version */
	if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
		while (cyc--) {
			off += _emit_LD(dry_run, &buf[off], ALWAYS);
			off += _emit_ST(dry_run, &buf[off], ALWAYS);
		}
	} else {
		while (cyc--) {
			off += _emit_LD(dry_run, &buf[off], ALWAYS);
			off += _emit_RMB(dry_run, &buf[off]);
			off += _emit_ST(dry_run, &buf[off], ALWAYS);
			off += _emit_WMB(dry_run, &buf[off]);
		}
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	}

	return off;
}

static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
		const struct _xfer_spec *pxs, int cyc)
{
	int off = 0;

	while (cyc--) {
1146 1147
		off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
		off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
1148
		off += _emit_ST(dry_run, &buf[off], ALWAYS);
1149
		off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	}

	return off;
}

static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
		const struct _xfer_spec *pxs, int cyc)
{
	int off = 0;

	while (cyc--) {
1161
		off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
1162
		off += _emit_LD(dry_run, &buf[off], ALWAYS);
1163 1164
		off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
		off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	}

	return off;
}

static int _bursts(unsigned dry_run, u8 buf[],
		const struct _xfer_spec *pxs, int cyc)
{
	int off = 0;

1175
	switch (pxs->desc->rqtype) {
1176
	case DMA_MEM_TO_DEV:
1177 1178
		off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
		break;
1179
	case DMA_DEV_TO_MEM:
1180 1181
		off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
		break;
1182
	case DMA_MEM_TO_MEM:
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
		off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
		break;
	default:
		off += 0x40000000; /* Scare off the Client */
		break;
	}

	return off;
}

/* Returns bytes consumed and updates bursts */
static inline int _loop(unsigned dry_run, u8 buf[],
		unsigned long *bursts, const struct _xfer_spec *pxs)
{
	int cyc, cycmax, szlp, szlpend, szbrst, off;
	unsigned lcnt0, lcnt1, ljmp0, ljmp1;
	struct _arg_LPEND lpend;

	/* Max iterations possible in DMALP is 256 */
	if (*bursts >= 256*256) {
		lcnt1 = 256;
		lcnt0 = 256;
		cyc = *bursts / lcnt1 / lcnt0;
	} else if (*bursts > 256) {
		lcnt1 = 256;
		lcnt0 = *bursts / lcnt1;
		cyc = 1;
	} else {
		lcnt1 = *bursts;
		lcnt0 = 0;
		cyc = 1;
	}

	szlp = _emit_LP(1, buf, 0, 0);
	szbrst = _bursts(1, buf, pxs, 1);

	lpend.cond = ALWAYS;
	lpend.forever = false;
	lpend.loop = 0;
	lpend.bjump = 0;
	szlpend = _emit_LPEND(1, buf, &lpend);

	if (lcnt0) {
		szlp *= 2;
		szlpend *= 2;
	}

	/*
	 * Max bursts that we can unroll due to limit on the
	 * size of backward jump that can be encoded in DMALPEND
	 * which is 8-bits and hence 255
	 */
	cycmax = (255 - (szlp + szlpend)) / szbrst;

	cyc = (cycmax < cyc) ? cycmax : cyc;

	off = 0;

	if (lcnt0) {
		off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
		ljmp0 = off;
	}

	off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
	ljmp1 = off;

	off += _bursts(dry_run, &buf[off], pxs, cyc);

	lpend.cond = ALWAYS;
	lpend.forever = false;
	lpend.loop = 1;
	lpend.bjump = off - ljmp1;
	off += _emit_LPEND(dry_run, &buf[off], &lpend);

	if (lcnt0) {
		lpend.cond = ALWAYS;
		lpend.forever = false;
		lpend.loop = 0;
		lpend.bjump = off - ljmp0;
		off += _emit_LPEND(dry_run, &buf[off], &lpend);
	}

	*bursts = lcnt1 * cyc;
	if (lcnt0)
		*bursts *= lcnt0;

	return off;
}

static inline int _setup_loops(unsigned dry_run, u8 buf[],
		const struct _xfer_spec *pxs)
{
1275
	struct pl330_xfer *x = &pxs->desc->px;
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	u32 ccr = pxs->ccr;
	unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
	int off = 0;

	while (bursts) {
		c = bursts;
		off += _loop(dry_run, &buf[off], &c, pxs);
		bursts -= c;
	}

	return off;
}

static inline int _setup_xfer(unsigned dry_run, u8 buf[],
		const struct _xfer_spec *pxs)
{
1292
	struct pl330_xfer *x = &pxs->desc->px;
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
	int off = 0;

	/* DMAMOV SAR, x->src_addr */
	off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
	/* DMAMOV DAR, x->dst_addr */
	off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);

	/* Setup Loop(s) */
	off += _setup_loops(dry_run, &buf[off], pxs);

	return off;
}

/*
 * A req is a sequence of one or more xfer units.
 * Returns the number of bytes taken to setup the MC for the req.
 */
static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
		unsigned index, struct _xfer_spec *pxs)
{
	struct _pl330_req *req = &thrd->req[index];
	struct pl330_xfer *x;
	u8 *buf = req->mc_cpu;
	int off = 0;

	PL330_DBGMC_START(req->mc_bus);

	/* DMAMOV CCR, ccr */
	off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);

1323
	x = &pxs->desc->px;
1324 1325 1326
	/* Error if xfer length is not aligned at burst size */
	if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
		return -EINVAL;
1327

1328
	off += _setup_xfer(dry_run, &buf[off], pxs);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374

	/* DMASEV peripheral/event */
	off += _emit_SEV(dry_run, &buf[off], thrd->ev);
	/* DMAEND */
	off += _emit_END(dry_run, &buf[off]);

	return off;
}

static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
{
	u32 ccr = 0;

	if (rqc->src_inc)
		ccr |= CC_SRCINC;

	if (rqc->dst_inc)
		ccr |= CC_DSTINC;

	/* We set same protection levels for Src and DST for now */
	if (rqc->privileged)
		ccr |= CC_SRCPRI | CC_DSTPRI;
	if (rqc->nonsecure)
		ccr |= CC_SRCNS | CC_DSTNS;
	if (rqc->insnaccess)
		ccr |= CC_SRCIA | CC_DSTIA;

	ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
	ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);

	ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
	ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);

	ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
	ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);

	ccr |= (rqc->swap << CC_SWAP_SHFT);

	return ccr;
}

/*
 * Submit a list of xfers after which the client wants notification.
 * Client is not notified after each xfer unit, just once after all
 * xfer units are done or some error occurs.
 */
1375 1376
static int pl330_submit_req(struct pl330_thread *thrd,
	struct dma_pl330_desc *desc)
1377
{
1378
	struct pl330_dmac *pl330 = thrd->dmac;
1379 1380 1381 1382 1383 1384 1385 1386
	struct _xfer_spec xs;
	unsigned long flags;
	unsigned idx;
	u32 ccr;
	int ret = 0;

	if (pl330->state == DYING
		|| pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1387
		dev_info(thrd->dmac->ddma.dev, "%s:%d\n",
1388 1389 1390 1391 1392
			__func__, __LINE__);
		return -EAGAIN;
	}

	/* If request for non-existing peripheral */
1393 1394
	if (desc->rqtype != DMA_MEM_TO_MEM &&
	    desc->peri >= pl330->pcfg.num_peri) {
1395
		dev_info(thrd->dmac->ddma.dev,
1396
				"%s:%d Invalid peripheral(%u)!\n",
1397
				__func__, __LINE__, desc->peri);
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
		return -EINVAL;
	}

	spin_lock_irqsave(&pl330->lock, flags);

	if (_queue_full(thrd)) {
		ret = -EAGAIN;
		goto xfer_exit;
	}

1408 1409 1410 1411 1412
	/* Prefer Secure Channel */
	if (!_manager_ns(thrd))
		desc->rqcfg.nonsecure = 0;
	else
		desc->rqcfg.nonsecure = 1;
1413

1414
	ccr = _prepare_ccr(&desc->rqcfg);
1415

1416
	idx = thrd->req[0].desc == NULL ? 0 : 1;
1417 1418

	xs.ccr = ccr;
1419
	xs.desc = desc;
1420 1421 1422 1423 1424 1425

	/* First dry run to check if req is acceptable */
	ret = _setup_req(1, thrd, idx, &xs);
	if (ret < 0)
		goto xfer_exit;

1426 1427
	if (ret > pl330->mcbufsz / 2) {
		dev_info(pl330->ddma.dev, "%s:%d Trying increasing mcbufsz\n",
1428 1429 1430 1431 1432 1433 1434
				__func__, __LINE__);
		ret = -ENOMEM;
		goto xfer_exit;
	}

	/* Hook the request */
	thrd->lstenq = idx;
1435
	thrd->req[idx].desc = desc;
1436
	_setup_req(0, thrd, idx, &xs);
1437 1438 1439 1440 1441 1442 1443 1444 1445

	ret = 0;

xfer_exit:
	spin_unlock_irqrestore(&pl330->lock, flags);

	return ret;
}

1446
static void dma_pl330_rqcb(struct dma_pl330_desc *desc, enum pl330_op_err err)
1447
{
1448
	struct dma_pl330_chan *pch;
1449 1450
	unsigned long flags;

1451 1452 1453 1454 1455
	if (!desc)
		return;

	pch = desc->pchan;

1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
	/* If desc aborted */
	if (!pch)
		return;

	spin_lock_irqsave(&pch->lock, flags);

	desc->status = DONE;

	spin_unlock_irqrestore(&pch->lock, flags);

	tasklet_schedule(&pch->task);
}

1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
static void pl330_dotask(unsigned long data)
{
	struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
	unsigned long flags;
	int i;

	spin_lock_irqsave(&pl330->lock, flags);

	/* The DMAC itself gone nuts */
	if (pl330->dmac_tbd.reset_dmac) {
		pl330->state = DYING;
		/* Reset the manager too */
		pl330->dmac_tbd.reset_mngr = true;
		/* Clear the reset flag */
		pl330->dmac_tbd.reset_dmac = false;
	}

	if (pl330->dmac_tbd.reset_mngr) {
		_stop(pl330->manager);
		/* Reset all channels */
1489
		pl330->dmac_tbd.reset_chan = (1 << pl330->pcfg.num_chan) - 1;
1490 1491 1492 1493
		/* Clear the reset flag */
		pl330->dmac_tbd.reset_mngr = false;
	}

1494
	for (i = 0; i < pl330->pcfg.num_chan; i++) {
1495 1496 1497

		if (pl330->dmac_tbd.reset_chan & (1 << i)) {
			struct pl330_thread *thrd = &pl330->channels[i];
1498
			void __iomem *regs = pl330->base;
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
			enum pl330_op_err err;

			_stop(thrd);

			if (readl(regs + FSC) & (1 << thrd->id))
				err = PL330_ERR_FAIL;
			else
				err = PL330_ERR_ABORT;

			spin_unlock_irqrestore(&pl330->lock, flags);
1509 1510
			dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, err);
			dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, err);
1511 1512
			spin_lock_irqsave(&pl330->lock, flags);

1513 1514
			thrd->req[0].desc = NULL;
			thrd->req[1].desc = NULL;
1515
			thrd->req_running = -1;
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527

			/* Clear the reset flag */
			pl330->dmac_tbd.reset_chan &= ~(1 << i);
		}
	}

	spin_unlock_irqrestore(&pl330->lock, flags);

	return;
}

/* Returns 1 if state was updated, 0 otherwise */
1528
static int pl330_update(struct pl330_dmac *pl330)
1529
{
1530
	struct dma_pl330_desc *descdone, *tmp;
1531 1532 1533 1534 1535
	unsigned long flags;
	void __iomem *regs;
	u32 val;
	int id, ev, ret = 0;

1536
	regs = pl330->base;
1537 1538 1539 1540 1541 1542 1543 1544 1545

	spin_lock_irqsave(&pl330->lock, flags);

	val = readl(regs + FSM) & 0x1;
	if (val)
		pl330->dmac_tbd.reset_mngr = true;
	else
		pl330->dmac_tbd.reset_mngr = false;

1546
	val = readl(regs + FSC) & ((1 << pl330->pcfg.num_chan) - 1);
1547 1548 1549
	pl330->dmac_tbd.reset_chan |= val;
	if (val) {
		int i = 0;
1550
		while (i < pl330->pcfg.num_chan) {
1551
			if (val & (1 << i)) {
1552
				dev_info(pl330->ddma.dev,
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
					"Reset Channel-%d\t CS-%x FTC-%x\n",
						i, readl(regs + CS(i)),
						readl(regs + FTC(i)));
				_stop(&pl330->channels[i]);
			}
			i++;
		}
	}

	/* Check which event happened i.e, thread notified */
	val = readl(regs + ES);
1564 1565
	if (pl330->pcfg.num_events < 32
			&& val & ~((1 << pl330->pcfg.num_events) - 1)) {
1566
		pl330->dmac_tbd.reset_dmac = true;
1567 1568
		dev_err(pl330->ddma.dev, "%s:%d Unexpected!\n", __func__,
			__LINE__);
1569 1570 1571 1572
		ret = 1;
		goto updt_exit;
	}

1573
	for (ev = 0; ev < pl330->pcfg.num_events; ev++) {
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
		if (val & (1 << ev)) { /* Event occurred */
			struct pl330_thread *thrd;
			u32 inten = readl(regs + INTEN);
			int active;

			/* Clear the event */
			if (inten & (1 << ev))
				writel(1 << ev, regs + INTCLR);

			ret = 1;

			id = pl330->events[ev];

			thrd = &pl330->channels[id];

			active = thrd->req_running;
			if (active == -1) /* Aborted */
				continue;

J
Javi Merino 已提交
1593
			/* Detach the req */
1594 1595
			descdone = thrd->req[active].desc;
			thrd->req[active].desc = NULL;
J
Javi Merino 已提交
1596

1597 1598
			thrd->req_running = -1;

1599 1600 1601 1602
			/* Get going again ASAP */
			_start(thrd);

			/* For now, just make a list of callbacks to be done */
1603
			list_add_tail(&descdone->rqd, &pl330->req_done);
1604 1605 1606 1607
		}
	}

	/* Now that we are in no hurry, do the callbacks */
1608 1609
	list_for_each_entry_safe(descdone, tmp, &pl330->req_done, rqd) {
		list_del(&descdone->rqd);
1610
		spin_unlock_irqrestore(&pl330->lock, flags);
1611
		dma_pl330_rqcb(descdone, PL330_ERR_NONE);
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
		spin_lock_irqsave(&pl330->lock, flags);
	}

updt_exit:
	spin_unlock_irqrestore(&pl330->lock, flags);

	if (pl330->dmac_tbd.reset_dmac
			|| pl330->dmac_tbd.reset_mngr
			|| pl330->dmac_tbd.reset_chan) {
		ret = 1;
		tasklet_schedule(&pl330->tasks);
	}

	return ret;
}

/* Reserve an event */
static inline int _alloc_event(struct pl330_thread *thrd)
{
	struct pl330_dmac *pl330 = thrd->dmac;
	int ev;

1634
	for (ev = 0; ev < pl330->pcfg.num_events; ev++)
1635 1636 1637 1638 1639 1640 1641 1642
		if (pl330->events[ev] == -1) {
			pl330->events[ev] = thrd->id;
			return ev;
		}

	return -1;
}

1643
static bool _chan_ns(const struct pl330_dmac *pl330, int i)
1644
{
1645
	return pl330->pcfg.irq_ns & (1 << i);
1646 1647 1648 1649 1650
}

/* Upon success, returns IdentityToken for the
 * allocated channel, NULL otherwise.
 */
1651
static struct pl330_thread *pl330_request_channel(struct pl330_dmac *pl330)
1652 1653 1654 1655 1656 1657 1658 1659
{
	struct pl330_thread *thrd = NULL;
	unsigned long flags;
	int chans, i;

	if (pl330->state == DYING)
		return NULL;

1660
	chans = pl330->pcfg.num_chan;
1661 1662 1663 1664 1665 1666

	spin_lock_irqsave(&pl330->lock, flags);

	for (i = 0; i < chans; i++) {
		thrd = &pl330->channels[i];
		if ((thrd->free) && (!_manager_ns(thrd) ||
1667
					_chan_ns(pl330, i))) {
1668 1669 1670 1671
			thrd->ev = _alloc_event(thrd);
			if (thrd->ev >= 0) {
				thrd->free = false;
				thrd->lstenq = 1;
1672 1673
				thrd->req[0].desc = NULL;
				thrd->req[1].desc = NULL;
1674
				thrd->req_running = -1;
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
				break;
			}
		}
		thrd = NULL;
	}

	spin_unlock_irqrestore(&pl330->lock, flags);

	return thrd;
}

/* Release an event */
static inline void _free_event(struct pl330_thread *thrd, int ev)
{
	struct pl330_dmac *pl330 = thrd->dmac;

	/* If the event is valid and was held by the thread */
1692
	if (ev >= 0 && ev < pl330->pcfg.num_events
1693 1694 1695 1696
			&& pl330->events[ev] == thrd->id)
		pl330->events[ev] = -1;
}

1697
static void pl330_release_channel(struct pl330_thread *thrd)
1698 1699 1700 1701 1702 1703 1704 1705 1706
{
	struct pl330_dmac *pl330;
	unsigned long flags;

	if (!thrd || thrd->free)
		return;

	_stop(thrd);

1707 1708
	dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, PL330_ERR_ABORT);
	dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, PL330_ERR_ABORT);
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720

	pl330 = thrd->dmac;

	spin_lock_irqsave(&pl330->lock, flags);
	_free_event(thrd, thrd->ev);
	thrd->free = true;
	spin_unlock_irqrestore(&pl330->lock, flags);
}

/* Initialize the structure for PL330 configuration, that can be used
 * by the client driver the make best use of the DMAC
 */
1721
static void read_dmac_config(struct pl330_dmac *pl330)
1722
{
1723
	void __iomem *regs = pl330->base;
1724 1725 1726 1727
	u32 val;

	val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
	val &= CRD_DATA_WIDTH_MASK;
1728
	pl330->pcfg.data_bus_width = 8 * (1 << val);
1729 1730 1731

	val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
	val &= CRD_DATA_BUFF_MASK;
1732
	pl330->pcfg.data_buf_dep = val + 1;
1733 1734 1735 1736

	val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
	val &= CR0_NUM_CHANS_MASK;
	val += 1;
1737
	pl330->pcfg.num_chan = val;
1738 1739 1740 1741 1742

	val = readl(regs + CR0);
	if (val & CR0_PERIPH_REQ_SET) {
		val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
		val += 1;
1743 1744
		pl330->pcfg.num_peri = val;
		pl330->pcfg.peri_ns = readl(regs + CR4);
1745
	} else {
1746
		pl330->pcfg.num_peri = 0;
1747 1748 1749 1750
	}

	val = readl(regs + CR0);
	if (val & CR0_BOOT_MAN_NS)
1751
		pl330->pcfg.mode |= DMAC_MODE_NS;
1752
	else
1753
		pl330->pcfg.mode &= ~DMAC_MODE_NS;
1754 1755 1756 1757

	val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
	val &= CR0_NUM_EVENTS_MASK;
	val += 1;
1758
	pl330->pcfg.num_events = val;
1759

1760
	pl330->pcfg.irq_ns = readl(regs + CR3);
1761 1762 1763 1764 1765 1766 1767
}

static inline void _reset_thread(struct pl330_thread *thrd)
{
	struct pl330_dmac *pl330 = thrd->dmac;

	thrd->req[0].mc_cpu = pl330->mcode_cpu
1768
				+ (thrd->id * pl330->mcbufsz);
1769
	thrd->req[0].mc_bus = pl330->mcode_bus
1770
				+ (thrd->id * pl330->mcbufsz);
1771
	thrd->req[0].desc = NULL;
1772 1773

	thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1774
				+ pl330->mcbufsz / 2;
1775
	thrd->req[1].mc_bus = thrd->req[0].mc_bus
1776
				+ pl330->mcbufsz / 2;
1777
	thrd->req[1].desc = NULL;
1778 1779

	thrd->req_running = -1;
1780 1781 1782 1783
}

static int dmac_alloc_threads(struct pl330_dmac *pl330)
{
1784
	int chans = pl330->pcfg.num_chan;
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
	struct pl330_thread *thrd;
	int i;

	/* Allocate 1 Manager and 'chans' Channel threads */
	pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
					GFP_KERNEL);
	if (!pl330->channels)
		return -ENOMEM;

	/* Init Channel threads */
	for (i = 0; i < chans; i++) {
		thrd = &pl330->channels[i];
		thrd->id = i;
		thrd->dmac = pl330;
		_reset_thread(thrd);
		thrd->free = true;
	}

	/* MANAGER is indexed at the end */
	thrd = &pl330->channels[chans];
	thrd->id = chans;
	thrd->dmac = pl330;
	thrd->free = false;
	pl330->manager = thrd;

	return 0;
}

static int dmac_alloc_resources(struct pl330_dmac *pl330)
{
1815
	int chans = pl330->pcfg.num_chan;
1816
	int ret;
J
Jassi Brar 已提交
1817 1818

	/*
1819 1820
	 * Alloc MicroCode buffer for 'chans' Channel threads.
	 * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
J
Jassi Brar 已提交
1821
	 */
1822 1823
	pl330->mcode_cpu = dma_alloc_coherent(pl330->ddma.dev,
				chans * pl330->mcbufsz,
1824 1825
				&pl330->mcode_bus, GFP_KERNEL);
	if (!pl330->mcode_cpu) {
1826
		dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n",
1827 1828 1829 1830 1831 1832
			__func__, __LINE__);
		return -ENOMEM;
	}

	ret = dmac_alloc_threads(pl330);
	if (ret) {
1833
		dev_err(pl330->ddma.dev, "%s:%d Can't to create channels for DMAC!\n",
1834
			__func__, __LINE__);
1835 1836
		dma_free_coherent(pl330->ddma.dev,
				chans * pl330->mcbufsz,
1837 1838 1839 1840 1841 1842 1843
				pl330->mcode_cpu, pl330->mcode_bus);
		return ret;
	}

	return 0;
}

1844
static int pl330_add(struct pl330_dmac *pl330)
1845 1846 1847 1848
{
	void __iomem *regs;
	int i, ret;

1849
	regs = pl330->base;
J
Jassi Brar 已提交
1850

1851
	/* Check if we can handle this DMAC */
1852 1853 1854
	if ((pl330->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
		dev_err(pl330->ddma.dev, "PERIPH_ID 0x%x !\n",
			pl330->pcfg.periph_id);
1855 1856
		return -EINVAL;
	}
J
Jassi Brar 已提交
1857

1858
	/* Read the configuration of the DMAC */
1859
	read_dmac_config(pl330);
J
Jassi Brar 已提交
1860

1861 1862
	if (pl330->pcfg.num_events == 0) {
		dev_err(pl330->ddma.dev, "%s:%d Can't work without events!\n",
1863 1864 1865
			__func__, __LINE__);
		return -EINVAL;
	}
J
Jassi Brar 已提交
1866

1867
	spin_lock_init(&pl330->lock);
1868

1869
	INIT_LIST_HEAD(&pl330->req_done);
1870

1871
	/* Use default MC buffer size if not provided */
1872 1873
	if (!pl330->mcbufsz)
		pl330->mcbufsz = MCODE_BUFF_PER_REQ * 2;
J
Jassi Brar 已提交
1874

1875
	/* Mark all events as free */
1876
	for (i = 0; i < pl330->pcfg.num_events; i++)
1877
		pl330->events[i] = -1;
J
Jassi Brar 已提交
1878

1879 1880 1881
	/* Allocate resources needed by the DMAC */
	ret = dmac_alloc_resources(pl330);
	if (ret) {
1882
		dev_err(pl330->ddma.dev, "Unable to create channels for DMAC\n");
1883 1884
		return ret;
	}
J
Jassi Brar 已提交
1885

1886
	tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
J
Jassi Brar 已提交
1887

1888
	pl330->state = INIT;
1889

1890 1891
	return 0;
}
J
Jassi Brar 已提交
1892

1893 1894 1895 1896
static int dmac_free_threads(struct pl330_dmac *pl330)
{
	struct pl330_thread *thrd;
	int i;
J
Jassi Brar 已提交
1897

1898
	/* Release Channel threads */
1899
	for (i = 0; i < pl330->pcfg.num_chan; i++) {
1900
		thrd = &pl330->channels[i];
1901
		pl330_release_channel(thrd);
1902
	}
J
Jassi Brar 已提交
1903

1904 1905
	/* Free memory */
	kfree(pl330->channels);
J
Jassi Brar 已提交
1906

1907 1908
	return 0;
}
J
Jassi Brar 已提交
1909

1910
static void pl330_del(struct pl330_dmac *pl330)
1911 1912 1913 1914 1915 1916
{
	pl330->state = UNINIT;

	tasklet_kill(&pl330->tasks);

	/* Free DMAC resources */
1917
	dmac_free_threads(pl330);
1918

1919 1920 1921
	dma_free_coherent(pl330->ddma.dev,
		pl330->pcfg.num_chan * pl330->mcbufsz, pl330->mcode_cpu,
		pl330->mcode_bus);
1922
}
J
Jassi Brar 已提交
1923

1924 1925 1926
/* forward declaration */
static struct amba_driver pl330_driver;

J
Jassi Brar 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
static inline struct dma_pl330_chan *
to_pchan(struct dma_chan *ch)
{
	if (!ch)
		return NULL;

	return container_of(ch, struct dma_pl330_chan, chan);
}

static inline struct dma_pl330_desc *
to_desc(struct dma_async_tx_descriptor *tx)
{
	return container_of(tx, struct dma_pl330_desc, txd);
}

static inline void fill_queue(struct dma_pl330_chan *pch)
{
	struct dma_pl330_desc *desc;
	int ret;

	list_for_each_entry(desc, &pch->work_list, node) {

		/* If already submitted */
		if (desc->status == BUSY)
1951
			continue;
J
Jassi Brar 已提交
1952

1953
		ret = pl330_submit_req(pch->thread, desc);
J
Jassi Brar 已提交
1954 1955 1956 1957 1958 1959 1960 1961
		if (!ret) {
			desc->status = BUSY;
		} else if (ret == -EAGAIN) {
			/* QFull or DMAC Dying */
			break;
		} else {
			/* Unacceptable request */
			desc->status = DONE;
1962
			dev_err(pch->dmac->ddma.dev, "%s:%d Bad Desc(%d)\n",
J
Jassi Brar 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
					__func__, __LINE__, desc->txd.cookie);
			tasklet_schedule(&pch->task);
		}
	}
}

static void pl330_tasklet(unsigned long data)
{
	struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
	struct dma_pl330_desc *desc, *_dt;
	unsigned long flags;
1974
	bool power_down = false;
J
Jassi Brar 已提交
1975 1976 1977 1978 1979 1980

	spin_lock_irqsave(&pch->lock, flags);

	/* Pick up ripe tomatoes */
	list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
		if (desc->status == DONE) {
1981
			if (!pch->cyclic)
1982
				dma_cookie_complete(&desc->txd);
1983
			list_move_tail(&desc->node, &pch->completed_list);
J
Jassi Brar 已提交
1984 1985 1986 1987 1988
		}

	/* Try to submit a req imm. next to the last completed cookie */
	fill_queue(pch);

1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
	if (list_empty(&pch->work_list)) {
		spin_lock(&pch->thread->dmac->lock);
		_stop(pch->thread);
		spin_unlock(&pch->thread->dmac->lock);
		power_down = true;
	} else {
		/* Make sure the PL330 Channel thread is active */
		spin_lock(&pch->thread->dmac->lock);
		_start(pch->thread);
		spin_unlock(&pch->thread->dmac->lock);
	}
J
Jassi Brar 已提交
2000

2001 2002 2003
	while (!list_empty(&pch->completed_list)) {
		dma_async_tx_callback callback;
		void *callback_param;
J
Jassi Brar 已提交
2004

2005 2006 2007 2008 2009 2010 2011 2012 2013
		desc = list_first_entry(&pch->completed_list,
					struct dma_pl330_desc, node);

		callback = desc->txd.callback;
		callback_param = desc->txd.callback_param;

		if (pch->cyclic) {
			desc->status = PREP;
			list_move_tail(&desc->node, &pch->work_list);
2014 2015 2016 2017 2018 2019
			if (power_down) {
				spin_lock(&pch->thread->dmac->lock);
				_start(pch->thread);
				spin_unlock(&pch->thread->dmac->lock);
				power_down = false;
			}
2020 2021 2022 2023 2024
		} else {
			desc->status = FREE;
			list_move_tail(&desc->node, &pch->dmac->desc_pool);
		}

2025 2026
		dma_descriptor_unmap(&desc->txd);

2027 2028 2029 2030 2031 2032 2033
		if (callback) {
			spin_unlock_irqrestore(&pch->lock, flags);
			callback(callback_param);
			spin_lock_irqsave(&pch->lock, flags);
		}
	}
	spin_unlock_irqrestore(&pch->lock, flags);
2034 2035 2036 2037 2038 2039

	/* If work list empty, power down */
	if (power_down) {
		pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
		pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
	}
J
Jassi Brar 已提交
2040 2041
}

2042 2043
bool pl330_filter(struct dma_chan *chan, void *param)
{
2044
	u8 *peri_id;
2045 2046 2047 2048

	if (chan->device->dev->driver != &pl330_driver.drv)
		return false;

2049
	peri_id = chan->private;
2050
	return *peri_id == (unsigned long)param;
2051 2052 2053
}
EXPORT_SYMBOL(pl330_filter);

2054 2055 2056 2057
static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
						struct of_dma *ofdma)
{
	int count = dma_spec->args_count;
2058
	struct pl330_dmac *pl330 = ofdma->of_dma_data;
2059
	unsigned int chan_id;
2060

2061 2062 2063
	if (!pl330)
		return NULL;

2064 2065 2066
	if (count != 1)
		return NULL;

2067
	chan_id = dma_spec->args[0];
2068
	if (chan_id >= pl330->num_peripherals)
2069
		return NULL;
2070

2071
	return dma_get_slave_channel(&pl330->peripherals[chan_id].chan);
2072 2073
}

J
Jassi Brar 已提交
2074 2075 2076
static int pl330_alloc_chan_resources(struct dma_chan *chan)
{
	struct dma_pl330_chan *pch = to_pchan(chan);
2077
	struct pl330_dmac *pl330 = pch->dmac;
J
Jassi Brar 已提交
2078 2079 2080 2081
	unsigned long flags;

	spin_lock_irqsave(&pch->lock, flags);

2082
	dma_cookie_init(chan);
2083
	pch->cyclic = false;
J
Jassi Brar 已提交
2084

2085
	pch->thread = pl330_request_channel(pl330);
2086
	if (!pch->thread) {
J
Jassi Brar 已提交
2087
		spin_unlock_irqrestore(&pch->lock, flags);
2088
		return -ENOMEM;
J
Jassi Brar 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097
	}

	tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);

	spin_unlock_irqrestore(&pch->lock, flags);

	return 1;
}

2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
static int pl330_config(struct dma_chan *chan,
			struct dma_slave_config *slave_config)
{
	struct dma_pl330_chan *pch = to_pchan(chan);

	if (slave_config->direction == DMA_MEM_TO_DEV) {
		if (slave_config->dst_addr)
			pch->fifo_addr = slave_config->dst_addr;
		if (slave_config->dst_addr_width)
			pch->burst_sz = __ffs(slave_config->dst_addr_width);
		if (slave_config->dst_maxburst)
			pch->burst_len = slave_config->dst_maxburst;
	} else if (slave_config->direction == DMA_DEV_TO_MEM) {
		if (slave_config->src_addr)
			pch->fifo_addr = slave_config->src_addr;
		if (slave_config->src_addr_width)
			pch->burst_sz = __ffs(slave_config->src_addr_width);
		if (slave_config->src_maxburst)
			pch->burst_len = slave_config->src_maxburst;
	}

	return 0;
}

static int pl330_terminate_all(struct dma_chan *chan)
J
Jassi Brar 已提交
2123 2124
{
	struct dma_pl330_chan *pch = to_pchan(chan);
2125
	struct dma_pl330_desc *desc;
J
Jassi Brar 已提交
2126
	unsigned long flags;
2127
	struct pl330_dmac *pl330 = pch->dmac;
2128
	LIST_HEAD(list);
J
Jassi Brar 已提交
2129

2130
	pm_runtime_get_sync(pl330->ddma.dev);
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
	spin_lock_irqsave(&pch->lock, flags);
	spin_lock(&pl330->lock);
	_stop(pch->thread);
	spin_unlock(&pl330->lock);

	pch->thread->req[0].desc = NULL;
	pch->thread->req[1].desc = NULL;
	pch->thread->req_running = -1;

	/* Mark all desc done */
	list_for_each_entry(desc, &pch->submitted_list, node) {
		desc->status = FREE;
		dma_cookie_complete(&desc->txd);
	}
2145

2146 2147 2148
	list_for_each_entry(desc, &pch->work_list , node) {
		desc->status = FREE;
		dma_cookie_complete(&desc->txd);
2149
	}
J
Jassi Brar 已提交
2150

2151 2152 2153 2154
	list_splice_tail_init(&pch->submitted_list, &pl330->desc_pool);
	list_splice_tail_init(&pch->work_list, &pl330->desc_pool);
	list_splice_tail_init(&pch->completed_list, &pl330->desc_pool);
	spin_unlock_irqrestore(&pch->lock, flags);
2155 2156
	pm_runtime_mark_last_busy(pl330->ddma.dev);
	pm_runtime_put_autosuspend(pl330->ddma.dev);
2157

J
Jassi Brar 已提交
2158 2159 2160
	return 0;
}

2161 2162 2163 2164 2165 2166 2167
/*
 * We don't support DMA_RESUME command because of hardware
 * limitations, so after pausing the channel we cannot restore
 * it to active state. We have to terminate channel and setup
 * DMA transfer again. This pause feature was implemented to
 * allow safely read residue before channel termination.
 */
2168
static int pl330_pause(struct dma_chan *chan)
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
{
	struct dma_pl330_chan *pch = to_pchan(chan);
	struct pl330_dmac *pl330 = pch->dmac;
	unsigned long flags;

	pm_runtime_get_sync(pl330->ddma.dev);
	spin_lock_irqsave(&pch->lock, flags);

	spin_lock(&pl330->lock);
	_stop(pch->thread);
	spin_unlock(&pl330->lock);

	spin_unlock_irqrestore(&pch->lock, flags);
	pm_runtime_mark_last_busy(pl330->ddma.dev);
	pm_runtime_put_autosuspend(pl330->ddma.dev);

	return 0;
}

J
Jassi Brar 已提交
2188 2189 2190 2191 2192 2193 2194
static void pl330_free_chan_resources(struct dma_chan *chan)
{
	struct dma_pl330_chan *pch = to_pchan(chan);
	unsigned long flags;

	tasklet_kill(&pch->task);

2195
	pm_runtime_get_sync(pch->dmac->ddma.dev);
2196 2197
	spin_lock_irqsave(&pch->lock, flags);

2198 2199
	pl330_release_channel(pch->thread);
	pch->thread = NULL;
J
Jassi Brar 已提交
2200

2201 2202 2203
	if (pch->cyclic)
		list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);

J
Jassi Brar 已提交
2204
	spin_unlock_irqrestore(&pch->lock, flags);
2205 2206
	pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
	pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
J
Jassi Brar 已提交
2207 2208
}

2209 2210
static int pl330_get_current_xferred_count(struct dma_pl330_chan *pch,
					   struct dma_pl330_desc *desc)
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
{
	struct pl330_thread *thrd = pch->thread;
	struct pl330_dmac *pl330 = pch->dmac;
	void __iomem *regs = thrd->dmac->base;
	u32 val, addr;

	pm_runtime_get_sync(pl330->ddma.dev);
	val = addr = 0;
	if (desc->rqcfg.src_inc) {
		val = readl(regs + SA(thrd->id));
		addr = desc->px.src_addr;
	} else {
		val = readl(regs + DA(thrd->id));
		addr = desc->px.dst_addr;
	}
	pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
	pm_runtime_put_autosuspend(pl330->ddma.dev);
	return val - addr;
}

J
Jassi Brar 已提交
2231 2232 2233 2234
static enum dma_status
pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
		 struct dma_tx_state *txstate)
{
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
	enum dma_status ret;
	unsigned long flags;
	struct dma_pl330_desc *desc, *running = NULL;
	struct dma_pl330_chan *pch = to_pchan(chan);
	unsigned int transferred, residual = 0;

	ret = dma_cookie_status(chan, cookie, txstate);

	if (!txstate)
		return ret;

	if (ret == DMA_COMPLETE)
		goto out;

	spin_lock_irqsave(&pch->lock, flags);

	if (pch->thread->req_running != -1)
		running = pch->thread->req[pch->thread->req_running].desc;

	/* Check in pending list */
	list_for_each_entry(desc, &pch->work_list, node) {
		if (desc->status == DONE)
			transferred = desc->bytes_requested;
		else if (running && desc == running)
			transferred =
				pl330_get_current_xferred_count(pch, desc);
		else
			transferred = 0;
		residual += desc->bytes_requested - transferred;
		if (desc->txd.cookie == cookie) {
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
			switch (desc->status) {
			case DONE:
				ret = DMA_COMPLETE;
				break;
			case PREP:
			case BUSY:
				ret = DMA_IN_PROGRESS;
				break;
			default:
				WARN_ON(1);
			}
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
			break;
		}
		if (desc->last)
			residual = 0;
	}
	spin_unlock_irqrestore(&pch->lock, flags);

out:
	dma_set_residue(txstate, residual);

	return ret;
J
Jassi Brar 已提交
2287 2288 2289 2290
}

static void pl330_issue_pending(struct dma_chan *chan)
{
2291 2292 2293 2294
	struct dma_pl330_chan *pch = to_pchan(chan);
	unsigned long flags;

	spin_lock_irqsave(&pch->lock, flags);
2295 2296 2297 2298 2299 2300 2301 2302 2303
	if (list_empty(&pch->work_list)) {
		/*
		 * Warn on nothing pending. Empty submitted_list may
		 * break our pm_runtime usage counter as it is
		 * updated on work_list emptiness status.
		 */
		WARN_ON(list_empty(&pch->submitted_list));
		pm_runtime_get_sync(pch->dmac->ddma.dev);
	}
2304 2305 2306 2307
	list_splice_tail_init(&pch->submitted_list, &pch->work_list);
	spin_unlock_irqrestore(&pch->lock, flags);

	pl330_tasklet((unsigned long)pch);
J
Jassi Brar 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
}

/*
 * We returned the last one of the circular list of descriptor(s)
 * from prep_xxx, so the argument to submit corresponds to the last
 * descriptor of the list.
 */
static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
{
	struct dma_pl330_desc *desc, *last = to_desc(tx);
	struct dma_pl330_chan *pch = to_pchan(tx->chan);
	dma_cookie_t cookie;
	unsigned long flags;

	spin_lock_irqsave(&pch->lock, flags);

	/* Assign cookies to all nodes */
	while (!list_empty(&last->node)) {
		desc = list_entry(last->node.next, struct dma_pl330_desc, node);
2327 2328 2329 2330
		if (pch->cyclic) {
			desc->txd.callback = last->txd.callback;
			desc->txd.callback_param = last->txd.callback_param;
		}
2331
		last->last = false;
J
Jassi Brar 已提交
2332

2333
		dma_cookie_assign(&desc->txd);
J
Jassi Brar 已提交
2334

2335
		list_move_tail(&desc->node, &pch->submitted_list);
J
Jassi Brar 已提交
2336 2337
	}

2338
	last->last = true;
2339
	cookie = dma_cookie_assign(&last->txd);
2340
	list_add_tail(&last->node, &pch->submitted_list);
J
Jassi Brar 已提交
2341 2342 2343 2344 2345 2346 2347 2348
	spin_unlock_irqrestore(&pch->lock, flags);

	return cookie;
}

static inline void _init_desc(struct dma_pl330_desc *desc)
{
	desc->rqcfg.swap = SWAP_NO;
2349 2350
	desc->rqcfg.scctl = CCTRL0;
	desc->rqcfg.dcctl = CCTRL0;
J
Jassi Brar 已提交
2351 2352 2353 2354 2355 2356
	desc->txd.tx_submit = pl330_tx_submit;

	INIT_LIST_HEAD(&desc->node);
}

/* Returns the number of descriptors added to the DMAC pool */
2357
static int add_desc(struct pl330_dmac *pl330, gfp_t flg, int count)
J
Jassi Brar 已提交
2358 2359 2360 2361 2362
{
	struct dma_pl330_desc *desc;
	unsigned long flags;
	int i;

2363
	desc = kcalloc(count, sizeof(*desc), flg);
J
Jassi Brar 已提交
2364 2365 2366
	if (!desc)
		return 0;

2367
	spin_lock_irqsave(&pl330->pool_lock, flags);
J
Jassi Brar 已提交
2368 2369 2370

	for (i = 0; i < count; i++) {
		_init_desc(&desc[i]);
2371
		list_add_tail(&desc[i].node, &pl330->desc_pool);
J
Jassi Brar 已提交
2372 2373
	}

2374
	spin_unlock_irqrestore(&pl330->pool_lock, flags);
J
Jassi Brar 已提交
2375 2376 2377 2378

	return count;
}

2379
static struct dma_pl330_desc *pluck_desc(struct pl330_dmac *pl330)
J
Jassi Brar 已提交
2380 2381 2382 2383
{
	struct dma_pl330_desc *desc = NULL;
	unsigned long flags;

2384
	spin_lock_irqsave(&pl330->pool_lock, flags);
J
Jassi Brar 已提交
2385

2386 2387
	if (!list_empty(&pl330->desc_pool)) {
		desc = list_entry(pl330->desc_pool.next,
J
Jassi Brar 已提交
2388 2389 2390 2391 2392 2393 2394 2395
				struct dma_pl330_desc, node);

		list_del_init(&desc->node);

		desc->status = PREP;
		desc->txd.callback = NULL;
	}

2396
	spin_unlock_irqrestore(&pl330->pool_lock, flags);
J
Jassi Brar 已提交
2397 2398 2399 2400 2401 2402

	return desc;
}

static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
{
2403
	struct pl330_dmac *pl330 = pch->dmac;
2404
	u8 *peri_id = pch->chan.private;
J
Jassi Brar 已提交
2405 2406 2407
	struct dma_pl330_desc *desc;

	/* Pluck one desc from the pool of DMAC */
2408
	desc = pluck_desc(pl330);
J
Jassi Brar 已提交
2409 2410 2411

	/* If the DMAC pool is empty, alloc new */
	if (!desc) {
2412
		if (!add_desc(pl330, GFP_ATOMIC, 1))
J
Jassi Brar 已提交
2413 2414 2415
			return NULL;

		/* Try again */
2416
		desc = pluck_desc(pl330);
J
Jassi Brar 已提交
2417
		if (!desc) {
2418
			dev_err(pch->dmac->ddma.dev,
J
Jassi Brar 已提交
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
				"%s:%d ALERT!\n", __func__, __LINE__);
			return NULL;
		}
	}

	/* Initialize the descriptor */
	desc->pchan = pch;
	desc->txd.cookie = 0;
	async_tx_ack(&desc->txd);

2429
	desc->peri = peri_id ? pch->chan.chan_id : 0;
2430
	desc->rqcfg.pcfg = &pch->dmac->pcfg;
J
Jassi Brar 已提交
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451

	dma_async_tx_descriptor_init(&desc->txd, &pch->chan);

	return desc;
}

static inline void fill_px(struct pl330_xfer *px,
		dma_addr_t dst, dma_addr_t src, size_t len)
{
	px->bytes = len;
	px->dst_addr = dst;
	px->src_addr = src;
}

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)
{
	struct dma_pl330_desc *desc = pl330_get_desc(pch);

	if (!desc) {
2452
		dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
J
Jassi Brar 已提交
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
			__func__, __LINE__);
		return NULL;
	}

	/*
	 * Ideally we should lookout for reqs bigger than
	 * those that can be programmed with 256 bytes of
	 * MC buffer, but considering a req size is seldom
	 * going to be word-unaligned and more than 200MB,
	 * we take it easy.
	 * Also, should the limit is reached we'd rather
	 * have the platform increase MC buffer size than
	 * complicating this API driver.
	 */
	fill_px(&desc->px, dst, src, len);

	return desc;
}

/* Call after fixing burst size */
static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
{
	struct dma_pl330_chan *pch = desc->pchan;
2476
	struct pl330_dmac *pl330 = pch->dmac;
J
Jassi Brar 已提交
2477 2478
	int burst_len;

2479
	burst_len = pl330->pcfg.data_bus_width / 8;
2480
	burst_len *= pl330->pcfg.data_buf_dep / pl330->pcfg.num_chan;
J
Jassi Brar 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
	burst_len >>= desc->rqcfg.brst_size;

	/* src/dst_burst_len can't be more than 16 */
	if (burst_len > 16)
		burst_len = 16;

	while (burst_len > 1) {
		if (!(len % (burst_len << desc->rqcfg.brst_size)))
			break;
		burst_len--;
	}

	return burst_len;
}

2496 2497
static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
		struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
2498
		size_t period_len, enum dma_transfer_direction direction,
2499
		unsigned long flags)
2500
{
2501
	struct dma_pl330_desc *desc = NULL, *first = NULL;
2502
	struct dma_pl330_chan *pch = to_pchan(chan);
2503
	struct pl330_dmac *pl330 = pch->dmac;
2504
	unsigned int i;
2505 2506 2507
	dma_addr_t dst;
	dma_addr_t src;

2508
	if (len % period_len != 0)
2509 2510
		return NULL;

2511
	if (!is_slave_direction(direction)) {
2512
		dev_err(pch->dmac->ddma.dev, "%s:%d Invalid dma direction\n",
2513 2514 2515 2516
		__func__, __LINE__);
		return NULL;
	}

2517 2518 2519
	for (i = 0; i < len / period_len; i++) {
		desc = pl330_get_desc(pch);
		if (!desc) {
2520
			dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
2521
				__func__, __LINE__);
2522

2523 2524 2525
			if (!first)
				return NULL;

2526
			spin_lock_irqsave(&pl330->pool_lock, flags);
2527 2528 2529 2530

			while (!list_empty(&first->node)) {
				desc = list_entry(first->node.next,
						struct dma_pl330_desc, node);
2531
				list_move_tail(&desc->node, &pl330->desc_pool);
2532 2533
			}

2534
			list_move_tail(&first->node, &pl330->desc_pool);
2535

2536
			spin_unlock_irqrestore(&pl330->pool_lock, flags);
2537

2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
			return NULL;
		}

		switch (direction) {
		case DMA_MEM_TO_DEV:
			desc->rqcfg.src_inc = 1;
			desc->rqcfg.dst_inc = 0;
			src = dma_addr;
			dst = pch->fifo_addr;
			break;
		case DMA_DEV_TO_MEM:
			desc->rqcfg.src_inc = 0;
			desc->rqcfg.dst_inc = 1;
			src = pch->fifo_addr;
			dst = dma_addr;
			break;
		default:
			break;
		}

2558
		desc->rqtype = direction;
2559 2560
		desc->rqcfg.brst_size = pch->burst_sz;
		desc->rqcfg.brst_len = 1;
2561
		desc->bytes_requested = period_len;
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
		fill_px(&desc->px, dst, src, period_len);

		if (!first)
			first = desc;
		else
			list_add_tail(&desc->node, &first->node);

		dma_addr += period_len;
	}

	if (!desc)
		return NULL;

	pch->cyclic = true;
	desc->txd.flags = flags;
2577 2578 2579 2580

	return &desc->txd;
}

J
Jassi Brar 已提交
2581 2582 2583 2584 2585 2586
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)
{
	struct dma_pl330_desc *desc;
	struct dma_pl330_chan *pch = to_pchan(chan);
2587
	struct pl330_dmac *pl330 = pch->dmac;
J
Jassi Brar 已提交
2588 2589
	int burst;

2590
	if (unlikely(!pch || !len))
J
Jassi Brar 已提交
2591 2592 2593 2594 2595 2596 2597 2598
		return NULL;

	desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
	if (!desc)
		return NULL;

	desc->rqcfg.src_inc = 1;
	desc->rqcfg.dst_inc = 1;
2599
	desc->rqtype = DMA_MEM_TO_MEM;
J
Jassi Brar 已提交
2600 2601

	/* Select max possible burst size */
2602
	burst = pl330->pcfg.data_bus_width / 8;
J
Jassi Brar 已提交
2603

2604 2605 2606 2607 2608 2609
	/*
	 * Make sure we use a burst size that aligns with all the memcpy
	 * parameters because our DMA programming algorithm doesn't cope with
	 * transfers which straddle an entry in the DMA device's MFIFO.
	 */
	while ((src | dst | len) & (burst - 1))
J
Jassi Brar 已提交
2610 2611 2612 2613 2614 2615
		burst /= 2;

	desc->rqcfg.brst_size = 0;
	while (burst != (1 << desc->rqcfg.brst_size))
		desc->rqcfg.brst_size++;

2616 2617 2618 2619 2620 2621 2622
	/*
	 * If burst size is smaller than bus width then make sure we only
	 * transfer one at a time to avoid a burst stradling an MFIFO entry.
	 */
	if (desc->rqcfg.brst_size * 8 < pl330->pcfg.data_bus_width)
		desc->rqcfg.brst_len = 1;

J
Jassi Brar 已提交
2623 2624 2625 2626 2627 2628 2629
	desc->rqcfg.brst_len = get_burst_len(desc, len);

	desc->txd.flags = flags;

	return &desc->txd;
}

2630
static void __pl330_giveback_desc(struct pl330_dmac *pl330,
2631 2632 2633 2634 2635 2636 2637 2638
				  struct dma_pl330_desc *first)
{
	unsigned long flags;
	struct dma_pl330_desc *desc;

	if (!first)
		return;

2639
	spin_lock_irqsave(&pl330->pool_lock, flags);
2640 2641 2642 2643

	while (!list_empty(&first->node)) {
		desc = list_entry(first->node.next,
				struct dma_pl330_desc, node);
2644
		list_move_tail(&desc->node, &pl330->desc_pool);
2645 2646
	}

2647
	list_move_tail(&first->node, &pl330->desc_pool);
2648

2649
	spin_unlock_irqrestore(&pl330->pool_lock, flags);
2650 2651
}

J
Jassi Brar 已提交
2652 2653
static struct dma_async_tx_descriptor *
pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2654
		unsigned int sg_len, enum dma_transfer_direction direction,
2655
		unsigned long flg, void *context)
J
Jassi Brar 已提交
2656 2657 2658 2659
{
	struct dma_pl330_desc *first, *desc = NULL;
	struct dma_pl330_chan *pch = to_pchan(chan);
	struct scatterlist *sg;
2660
	int i;
J
Jassi Brar 已提交
2661 2662
	dma_addr_t addr;

2663
	if (unlikely(!pch || !sgl || !sg_len))
J
Jassi Brar 已提交
2664 2665
		return NULL;

2666
	addr = pch->fifo_addr;
J
Jassi Brar 已提交
2667 2668 2669 2670 2671 2672 2673

	first = NULL;

	for_each_sg(sgl, sg, sg_len, i) {

		desc = pl330_get_desc(pch);
		if (!desc) {
2674
			struct pl330_dmac *pl330 = pch->dmac;
J
Jassi Brar 已提交
2675

2676
			dev_err(pch->dmac->ddma.dev,
J
Jassi Brar 已提交
2677 2678
				"%s:%d Unable to fetch desc\n",
				__func__, __LINE__);
2679
			__pl330_giveback_desc(pl330, first);
J
Jassi Brar 已提交
2680 2681 2682 2683 2684 2685 2686 2687 2688

			return NULL;
		}

		if (!first)
			first = desc;
		else
			list_add_tail(&desc->node, &first->node);

2689
		if (direction == DMA_MEM_TO_DEV) {
J
Jassi Brar 已提交
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
			desc->rqcfg.src_inc = 1;
			desc->rqcfg.dst_inc = 0;
			fill_px(&desc->px,
				addr, sg_dma_address(sg), sg_dma_len(sg));
		} else {
			desc->rqcfg.src_inc = 0;
			desc->rqcfg.dst_inc = 1;
			fill_px(&desc->px,
				sg_dma_address(sg), addr, sg_dma_len(sg));
		}

2701
		desc->rqcfg.brst_size = pch->burst_sz;
J
Jassi Brar 已提交
2702
		desc->rqcfg.brst_len = 1;
2703
		desc->rqtype = direction;
2704
		desc->bytes_requested = sg_dma_len(sg);
J
Jassi Brar 已提交
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
	}

	/* Return the last desc in the chain */
	desc->txd.flags = flg;
	return &desc->txd;
}

static irqreturn_t pl330_irq_handler(int irq, void *data)
{
	if (pl330_update(data))
		return IRQ_HANDLED;
	else
		return IRQ_NONE;
}

2720 2721 2722 2723 2724 2725 2726
#define PL330_DMA_BUSWIDTHS \
	BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
	BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
	BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
	BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
	BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)

2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
/*
 * Runtime PM callbacks are provided by amba/bus.c driver.
 *
 * It is assumed here that IRQ safe runtime PM is chosen in probe and amba
 * bus driver will only disable/enable the clock in runtime PM callbacks.
 */
static int __maybe_unused pl330_suspend(struct device *dev)
{
	struct amba_device *pcdev = to_amba_device(dev);

	pm_runtime_disable(dev);

	if (!pm_runtime_status_suspended(dev)) {
		/* amba did not disable the clock */
		amba_pclk_disable(pcdev);
	}
	amba_pclk_unprepare(pcdev);

	return 0;
}

static int __maybe_unused pl330_resume(struct device *dev)
{
	struct amba_device *pcdev = to_amba_device(dev);
	int ret;

	ret = amba_pclk_prepare(pcdev);
	if (ret)
		return ret;

	if (!pm_runtime_status_suspended(dev))
		ret = amba_pclk_enable(pcdev);

	pm_runtime_enable(dev);

	return ret;
}

static SIMPLE_DEV_PM_OPS(pl330_pm, pl330_suspend, pl330_resume);

B
Bill Pemberton 已提交
2767
static int
2768
pl330_probe(struct amba_device *adev, const struct amba_id *id)
J
Jassi Brar 已提交
2769 2770
{
	struct dma_pl330_platdata *pdat;
2771 2772
	struct pl330_config *pcfg;
	struct pl330_dmac *pl330;
2773
	struct dma_pl330_chan *pch, *_p;
J
Jassi Brar 已提交
2774 2775 2776
	struct dma_device *pd;
	struct resource *res;
	int i, ret, irq;
2777
	int num_chan;
J
Jassi Brar 已提交
2778

J
Jingoo Han 已提交
2779
	pdat = dev_get_platdata(&adev->dev);
J
Jassi Brar 已提交
2780

2781 2782 2783 2784
	ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
	if (ret)
		return ret;

J
Jassi Brar 已提交
2785
	/* Allocate a new DMAC and its Channels */
2786 2787
	pl330 = devm_kzalloc(&adev->dev, sizeof(*pl330), GFP_KERNEL);
	if (!pl330) {
J
Jassi Brar 已提交
2788 2789 2790 2791
		dev_err(&adev->dev, "unable to allocate mem\n");
		return -ENOMEM;
	}

2792 2793 2794
	pd = &pl330->ddma;
	pd->dev = &adev->dev;

2795
	pl330->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
J
Jassi Brar 已提交
2796 2797

	res = &adev->res;
2798 2799 2800
	pl330->base = devm_ioremap_resource(&adev->dev, res);
	if (IS_ERR(pl330->base))
		return PTR_ERR(pl330->base);
J
Jassi Brar 已提交
2801

2802
	amba_set_drvdata(adev, pl330);
2803

2804
	for (i = 0; i < AMBA_NR_IRQS; i++) {
2805 2806 2807 2808
		irq = adev->irq[i];
		if (irq) {
			ret = devm_request_irq(&adev->dev, irq,
					       pl330_irq_handler, 0,
2809
					       dev_name(&adev->dev), pl330);
2810 2811 2812 2813 2814 2815
			if (ret)
				return ret;
		} else {
			break;
		}
	}
J
Jassi Brar 已提交
2816

2817 2818 2819 2820
	pcfg = &pl330->pcfg;

	pcfg->periph_id = adev->periphid;
	ret = pl330_add(pl330);
J
Jassi Brar 已提交
2821
	if (ret)
M
Michal Simek 已提交
2822
		return ret;
J
Jassi Brar 已提交
2823

2824 2825
	INIT_LIST_HEAD(&pl330->desc_pool);
	spin_lock_init(&pl330->pool_lock);
J
Jassi Brar 已提交
2826 2827

	/* Create a descriptor pool of default size */
2828
	if (!add_desc(pl330, GFP_KERNEL, NR_DEFAULT_DESC))
J
Jassi Brar 已提交
2829 2830 2831 2832 2833
		dev_warn(&adev->dev, "unable to allocate desc\n");

	INIT_LIST_HEAD(&pd->channels);

	/* Initialize channel parameters */
2834
	if (pdat)
2835
		num_chan = max_t(int, pdat->nr_valid_peri, pcfg->num_chan);
2836
	else
2837
		num_chan = max_t(int, pcfg->num_peri, pcfg->num_chan);
2838

2839
	pl330->num_peripherals = num_chan;
2840

2841 2842
	pl330->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
	if (!pl330->peripherals) {
2843
		ret = -ENOMEM;
2844
		dev_err(&adev->dev, "unable to allocate pl330->peripherals\n");
S
Sachin Kamat 已提交
2845
		goto probe_err2;
2846
	}
J
Jassi Brar 已提交
2847

2848
	for (i = 0; i < num_chan; i++) {
2849
		pch = &pl330->peripherals[i];
2850 2851 2852 2853
		if (!adev->dev.of_node)
			pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
		else
			pch->chan.private = adev->dev.of_node;
J
Jassi Brar 已提交
2854

2855
		INIT_LIST_HEAD(&pch->submitted_list);
J
Jassi Brar 已提交
2856
		INIT_LIST_HEAD(&pch->work_list);
2857
		INIT_LIST_HEAD(&pch->completed_list);
J
Jassi Brar 已提交
2858
		spin_lock_init(&pch->lock);
2859
		pch->thread = NULL;
J
Jassi Brar 已提交
2860
		pch->chan.device = pd;
2861
		pch->dmac = pl330;
J
Jassi Brar 已提交
2862 2863 2864 2865 2866

		/* Add the channel to the DMAC list */
		list_add_tail(&pch->chan.device_node, &pd->channels);
	}

2867
	if (pdat) {
2868
		pd->cap_mask = pdat->cap_mask;
2869
	} else {
2870
		dma_cap_set(DMA_MEMCPY, pd->cap_mask);
2871
		if (pcfg->num_peri) {
2872 2873
			dma_cap_set(DMA_SLAVE, pd->cap_mask);
			dma_cap_set(DMA_CYCLIC, pd->cap_mask);
2874
			dma_cap_set(DMA_PRIVATE, pd->cap_mask);
2875 2876
		}
	}
J
Jassi Brar 已提交
2877 2878 2879 2880

	pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
	pd->device_free_chan_resources = pl330_free_chan_resources;
	pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
2881
	pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
J
Jassi Brar 已提交
2882 2883
	pd->device_tx_status = pl330_tx_status;
	pd->device_prep_slave_sg = pl330_prep_slave_sg;
2884
	pd->device_config = pl330_config;
2885
	pd->device_pause = pl330_pause;
2886
	pd->device_terminate_all = pl330_terminate_all;
J
Jassi Brar 已提交
2887
	pd->device_issue_pending = pl330_issue_pending;
2888 2889 2890
	pd->src_addr_widths = PL330_DMA_BUSWIDTHS;
	pd->dst_addr_widths = PL330_DMA_BUSWIDTHS;
	pd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2891
	pd->residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
J
Jassi Brar 已提交
2892 2893 2894 2895

	ret = dma_async_device_register(pd);
	if (ret) {
		dev_err(&adev->dev, "unable to register DMAC\n");
2896 2897 2898 2899 2900
		goto probe_err3;
	}

	if (adev->dev.of_node) {
		ret = of_dma_controller_register(adev->dev.of_node,
2901
					 of_dma_pl330_xlate, pl330);
2902 2903 2904 2905
		if (ret) {
			dev_err(&adev->dev,
			"unable to register DMA to the generic DT DMA helpers\n");
		}
J
Jassi Brar 已提交
2906
	}
2907

2908
	adev->dev.dma_parms = &pl330->dma_parms;
2909

2910 2911 2912 2913 2914 2915 2916 2917
	/*
	 * This is the limit for transfers with a buswidth of 1, larger
	 * buswidths will have larger limits.
	 */
	ret = dma_set_max_seg_size(&adev->dev, 1900800);
	if (ret)
		dev_err(&adev->dev, "unable to set the seg size\n");

J
Jassi Brar 已提交
2918 2919

	dev_info(&adev->dev,
2920
		"Loaded driver for PL330 DMAC-%x\n", adev->periphid);
J
Jassi Brar 已提交
2921 2922
	dev_info(&adev->dev,
		"\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
2923 2924
		pcfg->data_buf_dep, pcfg->data_bus_width / 8, pcfg->num_chan,
		pcfg->num_peri, pcfg->num_events);
J
Jassi Brar 已提交
2925

2926 2927 2928 2929 2930 2931
	pm_runtime_irq_safe(&adev->dev);
	pm_runtime_use_autosuspend(&adev->dev);
	pm_runtime_set_autosuspend_delay(&adev->dev, PL330_AUTOSUSPEND_DELAY);
	pm_runtime_mark_last_busy(&adev->dev);
	pm_runtime_put_autosuspend(&adev->dev);

J
Jassi Brar 已提交
2932
	return 0;
2933 2934
probe_err3:
	/* Idle the DMAC */
2935
	list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
2936 2937 2938 2939 2940 2941
			chan.device_node) {

		/* Remove the channel */
		list_del(&pch->chan.device_node);

		/* Flush the channel */
2942
		if (pch->thread) {
2943
			pl330_terminate_all(&pch->chan);
2944 2945
			pl330_free_chan_resources(&pch->chan);
		}
2946
	}
J
Jassi Brar 已提交
2947
probe_err2:
2948
	pl330_del(pl330);
J
Jassi Brar 已提交
2949 2950 2951 2952

	return ret;
}

2953
static int pl330_remove(struct amba_device *adev)
J
Jassi Brar 已提交
2954
{
2955
	struct pl330_dmac *pl330 = amba_get_drvdata(adev);
J
Jassi Brar 已提交
2956 2957
	struct dma_pl330_chan *pch, *_p;

2958 2959
	pm_runtime_get_noresume(pl330->ddma.dev);

2960 2961
	if (adev->dev.of_node)
		of_dma_controller_free(adev->dev.of_node);
2962

2963
	dma_async_device_unregister(&pl330->ddma);
J
Jassi Brar 已提交
2964 2965

	/* Idle the DMAC */
2966
	list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
J
Jassi Brar 已提交
2967 2968 2969 2970 2971 2972
			chan.device_node) {

		/* Remove the channel */
		list_del(&pch->chan.device_node);

		/* Flush the channel */
2973
		if (pch->thread) {
2974
			pl330_terminate_all(&pch->chan);
2975 2976
			pl330_free_chan_resources(&pch->chan);
		}
J
Jassi Brar 已提交
2977 2978
	}

2979
	pl330_del(pl330);
J
Jassi Brar 已提交
2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991

	return 0;
}

static struct amba_id pl330_ids[] = {
	{
		.id	= 0x00041330,
		.mask	= 0x000fffff,
	},
	{ 0, 0 },
};

2992 2993
MODULE_DEVICE_TABLE(amba, pl330_ids);

J
Jassi Brar 已提交
2994 2995 2996 2997
static struct amba_driver pl330_driver = {
	.drv = {
		.owner = THIS_MODULE,
		.name = "dma-pl330",
2998
		.pm = &pl330_pm,
J
Jassi Brar 已提交
2999 3000 3001 3002 3003 3004
	},
	.id_table = pl330_ids,
	.probe = pl330_probe,
	.remove = pl330_remove,
};

3005
module_amba_driver(pl330_driver);
J
Jassi Brar 已提交
3006

J
Jassi Brar 已提交
3007
MODULE_AUTHOR("Jaswinder Singh <jassisinghbrar@gmail.com>");
J
Jassi Brar 已提交
3008 3009
MODULE_DESCRIPTION("API Driver for PL330 DMAC");
MODULE_LICENSE("GPL");