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