upload tizen1.0 source
[kernel/linux-2.6.36.git] / arch / arm / common / pl330.c
1 /* linux/arch/arm/common/pl330.c
2  *
3  * Copyright (C) 2010 Samsung Electronics Co Ltd.
4  *      Jaswinder Singh <jassi.brar@samsung.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
26 #include <linux/io.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/dma-mapping.h>
30
31 #include <asm/hardware/pl330.h>
32
33 /* Register and Bit field Definitions */
34 #define DS              0x0
35 #define DS_ST_STOP      0x0
36 #define DS_ST_EXEC      0x1
37 #define DS_ST_CMISS     0x2
38 #define DS_ST_UPDTPC    0x3
39 #define DS_ST_WFE       0x4
40 #define DS_ST_ATBRR     0x5
41 #define DS_ST_QBUSY     0x6
42 #define DS_ST_WFP       0x7
43 #define DS_ST_KILL      0x8
44 #define DS_ST_CMPLT     0x9
45 #define DS_ST_FLTCMP    0xe
46 #define DS_ST_FAULT     0xf
47
48 #define DPC             0x4
49 #define INTEN           0x20
50 #define ES              0x24
51 #define INTSTATUS       0x28
52 #define INTCLR          0x2c
53 #define FSM             0x30
54 #define FSC             0x34
55 #define FTM             0x38
56
57 #define _FTC            0x40
58 #define FTC(n)          (_FTC + (n)*0x4)
59
60 #define _CS             0x100
61 #define CS(n)           (_CS + (n)*0x8)
62 #define CS_CNS          (1 << 21)
63
64 #define _CPC            0x104
65 #define CPC(n)          (_CPC + (n)*0x8)
66
67 #define _SA             0x400
68 #define SA(n)           (_SA + (n)*0x20)
69
70 #define _DA             0x404
71 #define DA(n)           (_DA + (n)*0x20)
72
73 #define _CC             0x408
74 #define CC(n)           (_CC + (n)*0x20)
75
76 #define CC_SRCINC       (1 << 0)
77 #define CC_DSTINC       (1 << 14)
78 #define CC_SRCPRI       (1 << 8)
79 #define CC_DSTPRI       (1 << 22)
80 #define CC_SRCNS        (1 << 9)
81 #define CC_DSTNS        (1 << 23)
82 #define CC_SRCIA        (1 << 10)
83 #define CC_DSTIA        (1 << 24)
84 #define CC_SRCBRSTLEN_SHFT      4
85 #define CC_DSTBRSTLEN_SHFT      18
86 #define CC_SRCBRSTSIZE_SHFT     1
87 #define CC_DSTBRSTSIZE_SHFT     15
88 #define CC_SRCCCTRL_SHFT        11
89 #define CC_SRCCCTRL_MASK        0x7
90 #define CC_DSTCCTRL_SHFT        25
91 #define CC_DRCCCTRL_MASK        0x7
92 #define CC_SWAP_SHFT    28
93
94 #define _LC0            0x40c
95 #define LC0(n)          (_LC0 + (n)*0x20)
96
97 #define _LC1            0x410
98 #define LC1(n)          (_LC1 + (n)*0x20)
99
100 #define DBGSTATUS       0xd00
101 #define DBG_BUSY        (1 << 0)
102
103 #define DBGCMD          0xd04
104 #define DBGINST0        0xd08
105 #define DBGINST1        0xd0c
106
107 #define CR0             0xe00
108 #define CR1             0xe04
109 #define CR2             0xe08
110 #define CR3             0xe0c
111 #define CR4             0xe10
112 #define CRD             0xe14
113
114 #define PERIPH_ID       0xfe0
115 #define PCELL_ID        0xff0
116
117 #define CR0_PERIPH_REQ_SET      (1 << 0)
118 #define CR0_BOOT_EN_SET         (1 << 1)
119 #define CR0_BOOT_MAN_NS         (1 << 2)
120 #define CR0_NUM_CHANS_SHIFT     4
121 #define CR0_NUM_CHANS_MASK      0x7
122 #define CR0_NUM_PERIPH_SHIFT    12
123 #define CR0_NUM_PERIPH_MASK     0x1f
124 #define CR0_NUM_EVENTS_SHIFT    17
125 #define CR0_NUM_EVENTS_MASK     0x1f
126
127 #define CR1_ICACHE_LEN_SHIFT    0
128 #define CR1_ICACHE_LEN_MASK     0x7
129 #define CR1_NUM_ICACHELINES_SHIFT       4
130 #define CR1_NUM_ICACHELINES_MASK        0xf
131
132 #define CRD_DATA_WIDTH_SHIFT    0
133 #define CRD_DATA_WIDTH_MASK     0x7
134 #define CRD_WR_CAP_SHIFT        4
135 #define CRD_WR_CAP_MASK         0x7
136 #define CRD_WR_Q_DEP_SHIFT      8
137 #define CRD_WR_Q_DEP_MASK       0xf
138 #define CRD_RD_CAP_SHIFT        12
139 #define CRD_RD_CAP_MASK         0x7
140 #define CRD_RD_Q_DEP_SHIFT      16
141 #define CRD_RD_Q_DEP_MASK       0xf
142 #define CRD_DATA_BUFF_SHIFT     20
143 #define CRD_DATA_BUFF_MASK      0x3ff
144
145 #define PART            0x330
146 #define DESIGNER        0x41
147 #define REVISION        0x0
148 #define INTEG_CFG       0x0
149 #define PERIPH_ID_VAL   ((PART << 0) | (DESIGNER << 12))
150
151 #define PCELL_ID_VAL    0xb105f00d
152
153 #define PL330_STATE_STOPPED             (1 << 0)
154 #define PL330_STATE_EXECUTING           (1 << 1)
155 #define PL330_STATE_WFE                 (1 << 2)
156 #define PL330_STATE_FAULTING            (1 << 3)
157 #define PL330_STATE_COMPLETING          (1 << 4)
158 #define PL330_STATE_WFP                 (1 << 5)
159 #define PL330_STATE_KILLING             (1 << 6)
160 #define PL330_STATE_FAULT_COMPLETING    (1 << 7)
161 #define PL330_STATE_CACHEMISS           (1 << 8)
162 #define PL330_STATE_UPDTPC              (1 << 9)
163 #define PL330_STATE_ATBARRIER           (1 << 10)
164 #define PL330_STATE_QUEUEBUSY           (1 << 11)
165 #define PL330_STATE_INVALID             (1 << 15)
166
167 #define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
168                                 | PL330_STATE_WFE | PL330_STATE_FAULTING)
169
170 #define CMD_DMAADDH     0x54
171 #define CMD_DMAEND      0x00
172 #define CMD_DMAFLUSHP   0x35
173 #define CMD_DMAGO       0xa0
174 #define CMD_DMALD       0x04
175 #define CMD_DMALDP      0x25
176 #define CMD_DMALP       0x20
177 #define CMD_DMALPEND    0x28
178 #define CMD_DMAKILL     0x01
179 #define CMD_DMAMOV      0xbc
180 #define CMD_DMANOP      0x18
181 #define CMD_DMARMB      0x12
182 #define CMD_DMASEV      0x34
183 #define CMD_DMAST       0x08
184 #define CMD_DMASTP      0x29
185 #define CMD_DMASTZ      0x0c
186 #define CMD_DMAWFE      0x36
187 #define CMD_DMAWFP      0x30
188 #define CMD_DMAWMB      0x13
189
190 #define SZ_DMAADDH      3
191 #define SZ_DMAEND       1
192 #define SZ_DMAFLUSHP    2
193 #define SZ_DMALD        1
194 #define SZ_DMALDP       2
195 #define SZ_DMALP        2
196 #define SZ_DMALPEND     2
197 #define SZ_DMAKILL      1
198 #define SZ_DMAMOV       6
199 #define SZ_DMANOP       1
200 #define SZ_DMARMB       1
201 #define SZ_DMASEV       2
202 #define SZ_DMAST        1
203 #define SZ_DMASTP       2
204 #define SZ_DMASTZ       1
205 #define SZ_DMAWFE       2
206 #define SZ_DMAWFP       2
207 #define SZ_DMAWMB       1
208 #define SZ_DMAGO        6
209
210 #define BRST_LEN(ccr)   ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
211 #define BRST_SIZE(ccr)  (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
212
213 #define BYTE_TO_BURST(b, ccr)  ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
214 #define BURST_TO_BYTE(c, ccr)  ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
215
216 /*
217  * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
218  * at 1byte/burst for P<->M and M<->M respectively.
219  * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
220  * should be enough for P<->M and M<->M respectively.
221  */
222 #define MCODE_BUFF_PER_REQ      256
223
224 /*
225  * Mark a _pl330_req as free.
226  * We do it by writing DMAEND as the first instruction
227  * because no valid request is going to have DMAEND as
228  * its first instruction to execute.
229  */
230 #define MARK_FREE(req)  do { \
231                                 _emit_END(0, (req)->mc_cpu); \
232                                 (req)->mc_len = 0; \
233                         } while (0)
234
235 /* If the _pl330_req is available to the client */
236 #define IS_FREE(req)    (*((u8 *)((req)->mc_cpu)) == CMD_DMAEND)
237
238 /* Use this _only_ to wait on transient states */
239 #define UNTIL(t, s)     while (!(_state(t) & (s))) cpu_relax();
240
241 #ifdef PL330_DEBUG_MCGEN
242 static unsigned cmd_line;
243 #define PL330_DBGCMD_DUMP(off, x...)    do { \
244                                                 printk("%x:", cmd_line); \
245                                                 printk(x); \
246                                                 cmd_line += off; \
247                                         } while (0)
248 #define PL330_DBGMC_START(addr)         (cmd_line = addr)
249 #else
250 #define PL330_DBGCMD_DUMP(off, x...)    do {} while (0)
251 #define PL330_DBGMC_START(addr)         do {} while (0)
252 #endif
253
254 struct _xfer_spec {
255         u32 ccr;
256         struct pl330_req *r;
257         struct pl330_xfer *x;
258 };
259
260 enum dmamov_dst {
261         SAR = 0,
262         CCR,
263         DAR,
264 };
265
266 enum pl330_dst {
267         SRC = 0,
268         DST,
269 };
270
271 enum pl330_cond {
272         SINGLE,
273         BURST,
274         ALWAYS,
275 };
276
277 struct _pl330_req {
278         u32 mc_bus;
279         void *mc_cpu;
280         /* Number of bytes taken to setup MC for the req */
281         u32 mc_len;
282         struct pl330_req *r;
283         /* Hook to attach to DMAC's list of reqs with due callback */
284         struct list_head rqd;
285 };
286
287 /* ToBeDone for tasklet */
288 struct _pl330_tbd {
289         bool reset_dmac;
290         bool reset_mngr;
291         u8 reset_chan;
292 };
293
294 /* A DMAC Thread */
295 struct pl330_thread {
296         u8 id;
297         int ev;
298         /* If the channel is not yet acquired by any client */
299         bool free;
300         /* Parent DMAC */
301         struct pl330_dmac *dmac;
302         /* Only two at a time */
303         struct _pl330_req req[2];
304         /* Index of the last submitted request */
305         unsigned lstenq;
306 };
307
308 enum pl330_dmac_state {
309         UNINIT,
310         INIT,
311         DYING,
312 };
313
314 /* A DMAC */
315 struct pl330_dmac {
316         spinlock_t              lock;
317         /* Holds list of reqs with due callbacks */
318         struct list_head        req_done;
319         /* Pointer to platform specific stuff */
320         struct pl330_info       *pinfo;
321         /* Maximum possible events/irqs */
322         int                     events[32];
323         /* BUS address of MicroCode buffer */
324         u32                     mcode_bus;
325         /* CPU address of MicroCode buffer */
326         void                    *mcode_cpu;
327         /* DMA address of MicroCode buffer */
328         u32                     mcode_dma;
329         /* List of all Channel threads */
330         struct pl330_thread     *channels;
331         /* Pointer to the MANAGER thread */
332         struct pl330_thread     *manager;
333         /* To handle bad news in interrupt */
334         struct tasklet_struct   tasks;
335         struct _pl330_tbd       dmac_tbd;
336         /* State of DMAC operation */
337         enum pl330_dmac_state   state;
338 };
339
340 static inline void _callback(struct pl330_req *r, enum pl330_op_err err)
341 {
342         if (r && r->xfer_cb)
343                 r->xfer_cb(r->token, err);
344 }
345
346 static inline bool _queue_empty(struct pl330_thread *thrd)
347 {
348         return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1]))
349                 ? true : false;
350 }
351
352 static inline bool _queue_full(struct pl330_thread *thrd)
353 {
354         return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1]))
355                 ? false : true;
356 }
357
358 static inline bool is_manager(struct pl330_thread *thrd)
359 {
360         struct pl330_dmac *pl330 = thrd->dmac;
361
362         /* MANAGER is indexed at the end */
363         if (thrd->id == pl330->pinfo->pcfg.num_chan)
364                 return true;
365         else
366                 return false;
367 }
368
369 /* If manager of the thread is in Non-Secure mode */
370 static inline bool _manager_ns(struct pl330_thread *thrd)
371 {
372         struct pl330_dmac *pl330 = thrd->dmac;
373
374         return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false;
375 }
376
377 static inline u32 get_id(struct pl330_info *pi, u32 off)
378 {
379         void __iomem *regs = pi->base;
380         u32 id = 0;
381
382         id |= (readb(regs + off + 0x0) << 0);
383         id |= (readb(regs + off + 0x4) << 8);
384         id |= (readb(regs + off + 0x8) << 16);
385         id |= (readb(regs + off + 0xc) << 24);
386
387         return id;
388 }
389
390 static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
391                 enum pl330_dst da, u16 val)
392 {
393         if (dry_run)
394                 return SZ_DMAADDH;
395
396         buf[0] = CMD_DMAADDH;
397         buf[0] |= (da << 1);
398         *((u16 *)&buf[1]) = val;
399
400         PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
401                 da == 1 ? "DA" : "SA", val);
402
403         return SZ_DMAADDH;
404 }
405
406 static inline u32 _emit_END(unsigned dry_run, u8 buf[])
407 {
408         if (dry_run)
409                 return SZ_DMAEND;
410
411         buf[0] = CMD_DMAEND;
412
413         PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
414
415         return SZ_DMAEND;
416 }
417
418 static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
419 {
420         if (dry_run)
421                 return SZ_DMAFLUSHP;
422
423         buf[0] = CMD_DMAFLUSHP;
424
425         peri &= 0x1f;
426         peri <<= 3;
427         buf[1] = peri;
428
429         PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
430
431         return SZ_DMAFLUSHP;
432 }
433
434 static inline u32 _emit_LD(unsigned dry_run, u8 buf[],  enum pl330_cond cond)
435 {
436         if (dry_run)
437                 return SZ_DMALD;
438
439         buf[0] = CMD_DMALD;
440
441         if (cond == SINGLE)
442                 buf[0] |= (0 << 1) | (1 << 0);
443         else if (cond == BURST)
444                 buf[0] |= (1 << 1) | (1 << 0);
445
446         PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
447                 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
448
449         return SZ_DMALD;
450 }
451
452 static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
453                 enum pl330_cond cond, u8 peri)
454 {
455         if (dry_run)
456                 return SZ_DMALDP;
457
458         buf[0] = CMD_DMALDP;
459
460         if (cond == BURST)
461                 buf[0] |= (1 << 1);
462
463         peri &= 0x1f;
464         peri <<= 3;
465         buf[1] = peri;
466
467         PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
468                 cond == SINGLE ? 'S' : 'B', peri >> 3);
469
470         return SZ_DMALDP;
471 }
472
473 static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
474                 unsigned loop, u8 cnt)
475 {
476         if (dry_run)
477                 return SZ_DMALP;
478
479         buf[0] = CMD_DMALP;
480
481         if (loop)
482                 buf[0] |= (1 << 1);
483
484         cnt--; /* DMAC increments by 1 internally */
485         buf[1] = cnt;
486
487         PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
488
489         return SZ_DMALP;
490 }
491
492 struct _arg_LPEND {
493         enum pl330_cond cond;
494         bool forever;
495         unsigned loop;
496         u8 bjump;
497 };
498
499 static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
500                 const struct _arg_LPEND *arg)
501 {
502         enum pl330_cond cond = arg->cond;
503         bool forever = arg->forever;
504         unsigned loop = arg->loop;
505         u8 bjump = arg->bjump;
506
507         if (dry_run)
508                 return SZ_DMALPEND;
509
510         buf[0] = CMD_DMALPEND;
511
512         if (loop)
513                 buf[0] |= (1 << 2);
514
515         if (!forever)
516                 buf[0] |= (1 << 4);
517
518         if (cond == SINGLE)
519                 buf[0] |= (0 << 1) | (1 << 0);
520         else if (cond == BURST)
521                 buf[0] |= (1 << 1) | (1 << 0);
522
523         buf[1] = bjump;
524
525         PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
526                         forever ? "FE" : "END",
527                         cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
528                         loop ? '1' : '0',
529                         bjump);
530
531         return SZ_DMALPEND;
532 }
533
534 static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
535 {
536         if (dry_run)
537                 return SZ_DMAKILL;
538
539         buf[0] = CMD_DMAKILL;
540
541         return SZ_DMAKILL;
542 }
543
544 static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
545                 enum dmamov_dst dst, u32 val)
546 {
547         if (dry_run)
548                 return SZ_DMAMOV;
549
550         buf[0] = CMD_DMAMOV;
551         buf[1] = dst;
552         *((u32 *)&buf[2]) = val;
553
554         PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
555                 dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
556
557         return SZ_DMAMOV;
558 }
559
560 static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
561 {
562         if (dry_run)
563                 return SZ_DMANOP;
564
565         buf[0] = CMD_DMANOP;
566
567         PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
568
569         return SZ_DMANOP;
570 }
571
572 static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
573 {
574         if (dry_run)
575                 return SZ_DMARMB;
576
577         buf[0] = CMD_DMARMB;
578
579         PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
580
581         return SZ_DMARMB;
582 }
583
584 static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
585 {
586         if (dry_run)
587                 return SZ_DMASEV;
588
589         buf[0] = CMD_DMASEV;
590
591         ev &= 0x1f;
592         ev <<= 3;
593         buf[1] = ev;
594
595         PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
596
597         return SZ_DMASEV;
598 }
599
600 static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
601 {
602         if (dry_run)
603                 return SZ_DMAST;
604
605         buf[0] = CMD_DMAST;
606
607         if (cond == SINGLE)
608                 buf[0] |= (0 << 1) | (1 << 0);
609         else if (cond == BURST)
610                 buf[0] |= (1 << 1) | (1 << 0);
611
612         PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
613                 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
614
615         return SZ_DMAST;
616 }
617
618 static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
619                 enum pl330_cond cond, u8 peri)
620 {
621         if (dry_run)
622                 return SZ_DMASTP;
623
624         buf[0] = CMD_DMASTP;
625
626         if (cond == BURST)
627                 buf[0] |= (1 << 1);
628
629         peri &= 0x1f;
630         peri <<= 3;
631         buf[1] = peri;
632
633         PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
634                 cond == SINGLE ? 'S' : 'B', peri >> 3);
635
636         return SZ_DMASTP;
637 }
638
639 static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
640 {
641         if (dry_run)
642                 return SZ_DMASTZ;
643
644         buf[0] = CMD_DMASTZ;
645
646         PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
647
648         return SZ_DMASTZ;
649 }
650
651 static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
652                 unsigned invalidate)
653 {
654         if (dry_run)
655                 return SZ_DMAWFE;
656
657         buf[0] = CMD_DMAWFE;
658
659         ev &= 0x1f;
660         ev <<= 3;
661         buf[1] = ev;
662
663         if (invalidate)
664                 buf[1] |= (1 << 1);
665
666         PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
667                 ev >> 3, invalidate ? ", I" : "");
668
669         return SZ_DMAWFE;
670 }
671
672 static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
673                 enum pl330_cond cond, u8 peri)
674 {
675         if (dry_run)
676                 return SZ_DMAWFP;
677
678         buf[0] = CMD_DMAWFP;
679
680         if (cond == SINGLE)
681                 buf[0] |= (0 << 1) | (0 << 0);
682         else if (cond == BURST)
683                 buf[0] |= (1 << 1) | (0 << 0);
684         else
685                 buf[0] |= (0 << 1) | (1 << 0);
686
687         peri &= 0x1f;
688         peri <<= 3;
689         buf[1] = peri;
690
691         PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
692                 cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
693
694         return SZ_DMAWFP;
695 }
696
697 static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
698 {
699         if (dry_run)
700                 return SZ_DMAWMB;
701
702         buf[0] = CMD_DMAWMB;
703
704         PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
705
706         return SZ_DMAWMB;
707 }
708
709 struct _arg_GO {
710         u8 chan;
711         u32 addr;
712         unsigned ns;
713 };
714
715 static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
716                 const struct _arg_GO *arg)
717 {
718         u8 chan = arg->chan;
719         u32 addr = arg->addr;
720         unsigned ns = arg->ns;
721
722         if (dry_run)
723                 return SZ_DMAGO;
724
725         buf[0] = CMD_DMAGO;
726         buf[0] |= (ns << 1);
727
728         buf[1] = chan & 0x7;
729
730         *((u32 *)&buf[2]) = addr;
731
732         return SZ_DMAGO;
733 }
734
735 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
736
737 /* Returns Time-Out */
738 static bool _until_dmac_idle(struct pl330_thread *thrd)
739 {
740         void __iomem *regs = thrd->dmac->pinfo->base;
741         unsigned long loops = msecs_to_loops(5);
742
743         do {
744                 /* Until Manager is Idle */
745                 if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
746                         break;
747
748                 cpu_relax();
749         } while (--loops);
750
751         if (!loops)
752                 return true;
753
754         return false;
755 }
756
757 static inline void _execute_DBGINSN(struct pl330_thread *thrd,
758                 u8 insn[], bool as_manager)
759 {
760         void __iomem *regs = thrd->dmac->pinfo->base;
761         u32 val;
762
763         val = (insn[0] << 16) | (insn[1] << 24);
764         if (!as_manager) {
765                 val |= (1 << 0);
766                 val |= (thrd->id << 8); /* Channel Number */
767         }
768         writel(val, regs + DBGINST0);
769
770         val = *((u32 *)&insn[2]);
771         writel(val, regs + DBGINST1);
772
773         /* If timed out due to halted state-machine */
774         if (_until_dmac_idle(thrd)) {
775                 dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n");
776                 return;
777         }
778
779         /* Get going */
780         writel(0, regs + DBGCMD);
781 }
782
783 static inline u32 _state(struct pl330_thread *thrd)
784 {
785         void __iomem *regs = thrd->dmac->pinfo->base;
786         u32 val;
787
788         if (is_manager(thrd))
789                 val = readl(regs + DS) & 0xf;
790         else
791                 val = readl(regs + CS(thrd->id)) & 0xf;
792
793         switch (val) {
794         case DS_ST_STOP:
795                 return PL330_STATE_STOPPED;
796         case DS_ST_EXEC:
797                 return PL330_STATE_EXECUTING;
798         case DS_ST_CMISS:
799                 return PL330_STATE_CACHEMISS;
800         case DS_ST_UPDTPC:
801                 return PL330_STATE_UPDTPC;
802         case DS_ST_WFE:
803                 return PL330_STATE_WFE;
804         case DS_ST_FAULT:
805                 return PL330_STATE_FAULTING;
806         case DS_ST_ATBRR:
807                 if (is_manager(thrd))
808                         return PL330_STATE_INVALID;
809                 else
810                         return PL330_STATE_ATBARRIER;
811         case DS_ST_QBUSY:
812                 if (is_manager(thrd))
813                         return PL330_STATE_INVALID;
814                 else
815                         return PL330_STATE_QUEUEBUSY;
816         case DS_ST_WFP:
817                 if (is_manager(thrd))
818                         return PL330_STATE_INVALID;
819                 else
820                         return PL330_STATE_WFP;
821         case DS_ST_KILL:
822                 if (is_manager(thrd))
823                         return PL330_STATE_INVALID;
824                 else
825                         return PL330_STATE_KILLING;
826         case DS_ST_CMPLT:
827                 if (is_manager(thrd))
828                         return PL330_STATE_INVALID;
829                 else
830                         return PL330_STATE_COMPLETING;
831         case DS_ST_FLTCMP:
832                 if (is_manager(thrd))
833                         return PL330_STATE_INVALID;
834                 else
835                         return PL330_STATE_FAULT_COMPLETING;
836         default:
837                 return PL330_STATE_INVALID;
838         }
839 }
840
841 /* If the request 'req' of thread 'thrd' is currently active */
842 static inline bool _req_active(struct pl330_thread *thrd,
843                 struct _pl330_req *req)
844 {
845         void __iomem *regs = thrd->dmac->pinfo->base;
846         u32 buf = req->mc_bus, pc = readl(regs + CPC(thrd->id));
847
848         if (IS_FREE(req))
849                 return false;
850
851         return (pc >= buf && pc <= buf + req->mc_len) ? true : false;
852 }
853
854 /* Returns 0 if the thread is inactive, ID of active req + 1 otherwise */
855 static inline unsigned _thrd_active(struct pl330_thread *thrd)
856 {
857         if (_req_active(thrd, &thrd->req[0]))
858                 return 1; /* First req active */
859
860         if (_req_active(thrd, &thrd->req[1]))
861                 return 2; /* Second req active */
862
863         return 0;
864 }
865
866 static void _stop(struct pl330_thread *thrd)
867 {
868         void __iomem *regs = thrd->dmac->pinfo->base;
869         u8 insn[6] = {0, 0, 0, 0, 0, 0};
870
871         if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
872                 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
873
874         /* Return if nothing needs to be done */
875         if (_state(thrd) == PL330_STATE_COMPLETING
876                   || _state(thrd) == PL330_STATE_KILLING
877                   || _state(thrd) == PL330_STATE_STOPPED)
878                 return;
879
880         _emit_KILL(0, insn);
881
882         /* Stop generating interrupts for SEV */
883         writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
884
885         _execute_DBGINSN(thrd, insn, is_manager(thrd));
886 }
887
888 /* Start doing req 'idx' of thread 'thrd' */
889 static bool _trigger(struct pl330_thread *thrd)
890 {
891         void __iomem *regs = thrd->dmac->pinfo->base;
892         struct _pl330_req *req;
893         struct pl330_req *r;
894         struct _arg_GO go;
895         unsigned ns;
896         u8 insn[6] = {0, 0, 0, 0, 0, 0};
897
898         /* Return if already ACTIVE */
899         if (_state(thrd) != PL330_STATE_STOPPED)
900                 return true;
901
902         if (!IS_FREE(&thrd->req[1 - thrd->lstenq]))
903                 req = &thrd->req[1 - thrd->lstenq];
904         else if (!IS_FREE(&thrd->req[thrd->lstenq]))
905                 req = &thrd->req[thrd->lstenq];
906         else
907                 req = NULL;
908
909         /* Return if no request */
910         if (!req || !req->r)
911                 return true;
912
913         r = req->r;
914
915         if (r->cfg)
916                 ns = r->cfg->nonsecure ? 1 : 0;
917         else if (readl(regs + CS(thrd->id)) & CS_CNS)
918                 ns = 1;
919         else
920                 ns = 0;
921
922         /* See 'Abort Sources' point-4 at Page 2-25 */
923         if (_manager_ns(thrd) && !ns)
924                 dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n",
925                         __func__, __LINE__);
926
927         go.chan = thrd->id;
928         go.addr = req->mc_bus;
929         go.ns = ns;
930         _emit_GO(0, insn, &go);
931
932         /* Set to generate interrupts for SEV */
933         writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
934
935         /* Only manager can execute GO */
936         _execute_DBGINSN(thrd, insn, true);
937
938         return true;
939 }
940
941 static bool _start(struct pl330_thread *thrd)
942 {
943         switch (_state(thrd)) {
944         case PL330_STATE_FAULT_COMPLETING:
945                 UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
946
947                 if (_state(thrd) == PL330_STATE_KILLING)
948                         UNTIL(thrd, PL330_STATE_STOPPED)
949
950         case PL330_STATE_FAULTING:
951                 _stop(thrd);
952
953         case PL330_STATE_KILLING:
954         case PL330_STATE_COMPLETING:
955                 UNTIL(thrd, PL330_STATE_STOPPED)
956
957         case PL330_STATE_STOPPED:
958                 return _trigger(thrd);
959
960         case PL330_STATE_WFP:
961         case PL330_STATE_QUEUEBUSY:
962         case PL330_STATE_ATBARRIER:
963         case PL330_STATE_UPDTPC:
964         case PL330_STATE_CACHEMISS:
965         case PL330_STATE_EXECUTING:
966                 return true;
967
968         case PL330_STATE_WFE: /* For RESUME, nothing yet */
969         default:
970                 return false;
971         }
972 }
973
974 static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
975                 const struct _xfer_spec *pxs, int cyc)
976 {
977         int off = 0;
978
979         while (cyc--) {
980                 off += _emit_LD(dry_run, &buf[off], ALWAYS);
981                 off += _emit_RMB(dry_run, &buf[off]);
982                 off += _emit_ST(dry_run, &buf[off], ALWAYS);
983                 off += _emit_WMB(dry_run, &buf[off]);
984         }
985
986         return off;
987 }
988
989 static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
990                 const struct _xfer_spec *pxs, int cyc)
991 {
992         int off = 0;
993
994         while (cyc--) {
995                 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
996                 off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri);
997                 off += _emit_ST(dry_run, &buf[off], ALWAYS);
998                 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
999         }
1000
1001         return off;
1002 }
1003
1004 static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
1005                 const struct _xfer_spec *pxs, int cyc)
1006 {
1007         int off = 0;
1008
1009         while (cyc--) {
1010                 off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1011                 off += _emit_LD(dry_run, &buf[off], ALWAYS);
1012                 off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri);
1013                 off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri);
1014         }
1015
1016         return off;
1017 }
1018
1019 static int _bursts(unsigned dry_run, u8 buf[],
1020                 const struct _xfer_spec *pxs, int cyc)
1021 {
1022         int off = 0;
1023
1024         switch (pxs->r->rqtype) {
1025         case MEMTODEV:
1026                 off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
1027                 break;
1028         case DEVTOMEM:
1029                 off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
1030                 break;
1031         case MEMTOMEM:
1032                 off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
1033                 break;
1034         default:
1035                 off += 0x40000000; /* Scare off the Client */
1036                 break;
1037         }
1038
1039         return off;
1040 }
1041
1042 /* Returns bytes consumed and updates bursts */
1043 static inline int _loop(unsigned dry_run, u8 buf[],
1044                 unsigned long *bursts, const struct _xfer_spec *pxs)
1045 {
1046         int cyc, cycmax, szlp, szlpend, szbrst, off;
1047         unsigned lcnt0, lcnt1, ljmp0, ljmp1;
1048         struct _arg_LPEND lpend;
1049
1050         /* Max iterations possibile in DMALP is 256 */
1051         if (*bursts >= 256*256) {
1052                 lcnt1 = 256;
1053                 lcnt0 = 256;
1054                 cyc = *bursts / lcnt1 / lcnt0;
1055         } else if (*bursts > 256) {
1056                 lcnt1 = 256;
1057                 lcnt0 = *bursts / lcnt1;
1058                 cyc = 1;
1059         } else {
1060                 lcnt1 = *bursts;
1061                 lcnt0 = 0;
1062                 cyc = 1;
1063         }
1064
1065         szlp = _emit_LP(1, buf, 0, 0);
1066         szbrst = _bursts(1, buf, pxs, 1);
1067
1068         lpend.cond = ALWAYS;
1069         lpend.forever = false;
1070         lpend.loop = 0;
1071         lpend.bjump = 0;
1072         szlpend = _emit_LPEND(1, buf, &lpend);
1073
1074         if (lcnt0) {
1075                 szlp *= 2;
1076                 szlpend *= 2;
1077         }
1078
1079         /*
1080          * Max bursts that we can unroll due to limit on the
1081          * size of backward jump that can be encoded in DMALPEND
1082          * which is 8-bits and hence 255
1083          */
1084         cycmax = (255 - (szlp + szlpend)) / szbrst;
1085
1086         cyc = (cycmax < cyc) ? cycmax : cyc;
1087
1088         off = 0;
1089
1090         if (lcnt0) {
1091                 off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
1092                 ljmp0 = off;
1093         }
1094
1095         off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1096         ljmp1 = off;
1097
1098         off += _bursts(dry_run, &buf[off], pxs, cyc);
1099
1100         lpend.cond = ALWAYS;
1101         lpend.forever = false;
1102         lpend.loop = 1;
1103         lpend.bjump = off - ljmp1;
1104         off += _emit_LPEND(dry_run, &buf[off], &lpend);
1105
1106         if (lcnt0) {
1107                 lpend.cond = ALWAYS;
1108                 lpend.forever = false;
1109                 lpend.loop = 0;
1110                 lpend.bjump = off - ljmp0;
1111                 off += _emit_LPEND(dry_run, &buf[off], &lpend);
1112         }
1113
1114         *bursts = lcnt1 * cyc;
1115         if (lcnt0)
1116                 *bursts *= lcnt0;
1117
1118         return off;
1119 }
1120
1121 /* Returns bytes consumed and updates bursts */
1122 static inline int _loop_infiniteloop(unsigned dry_run, u8 buf[],
1123                 unsigned long bursts, const struct _xfer_spec *pxs, int ev)
1124 {
1125         int cyc, off;
1126         unsigned lcnt0, lcnt1, ljmp0, ljmp1, ljmpfe;
1127         struct _arg_LPEND lpend;
1128
1129         off = 0;
1130         ljmpfe = off;
1131         lcnt0 = pxs->r->infiniteloop;
1132         lcnt1 = 256;
1133         cyc = bursts/256;
1134
1135         /* forever loop */
1136         off += _emit_MOV(dry_run, &buf[off], SAR, pxs->x->src_addr);
1137         off += _emit_MOV(dry_run, &buf[off], DAR, pxs->x->dst_addr);
1138
1139         /* loop0 */
1140         off += _emit_LP(dry_run, &buf[off], 0,  lcnt0);
1141         ljmp0 = off;
1142
1143         /* loop1 */
1144         off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
1145         ljmp1 = off;
1146         off += _bursts(dry_run, &buf[off], pxs, cyc);
1147         lpend.cond = ALWAYS;
1148         lpend.forever = false;
1149         lpend.loop = 1;
1150         lpend.bjump = off - ljmp1;
1151         off += _emit_LPEND(dry_run, &buf[off], &lpend);
1152         off += _emit_SEV(dry_run, &buf[off], ev);
1153
1154         lpend.cond = ALWAYS;
1155         lpend.forever = false;
1156         lpend.loop = 0;
1157         lpend.bjump = off - ljmp0;
1158         off += _emit_LPEND(dry_run, &buf[off], &lpend);
1159
1160         lpend.cond = ALWAYS;
1161         lpend.forever = true;
1162         lpend.loop = 1;
1163         lpend.bjump = off - ljmpfe;
1164         off +=  _emit_LPEND(dry_run, &buf[off], &lpend);
1165
1166         return off;
1167 }
1168
1169 static inline int _setup_loops(unsigned dry_run, u8 buf[],
1170                 const struct _xfer_spec *pxs)
1171 {
1172         struct pl330_xfer *x = pxs->x;
1173         u32 ccr = pxs->ccr;
1174         unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
1175         int off = 0;
1176
1177         while (bursts) {
1178                 c = bursts;
1179                 off += _loop(dry_run, &buf[off], &c, pxs);
1180                 bursts -= c;
1181         }
1182
1183         return off;
1184 }
1185
1186 static inline int _setup_xfer(unsigned dry_run, u8 buf[],
1187                 const struct _xfer_spec *pxs)
1188 {
1189         struct pl330_xfer *x = pxs->x;
1190         int off = 0;
1191
1192         /* DMAMOV SAR, x->src_addr */
1193         off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
1194         /* DMAMOV DAR, x->dst_addr */
1195         off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
1196
1197         /* Setup Loop(s) */
1198         off += _setup_loops(dry_run, &buf[off], pxs);
1199
1200         return off;
1201 }
1202
1203 static inline int _setup_xfer_infiniteloop(unsigned dry_run, u8 buf[],
1204                 const struct _xfer_spec *pxs, int ev)
1205 {
1206         struct pl330_xfer *x = pxs->x;
1207         u32 ccr = pxs->ccr;
1208         unsigned long bursts = BYTE_TO_BURST(x->bytes, ccr);
1209         int off = 0;
1210
1211         /* Setup Loop(s) */
1212         off += _loop_infiniteloop(dry_run, &buf[off], bursts, pxs, ev);
1213
1214         return off;
1215 }
1216
1217 /*
1218  * A req is a sequence of one or more xfer units.
1219  * Returns the number of bytes taken to setup the MC for the req.
1220  */
1221 static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
1222                 unsigned index, struct _xfer_spec *pxs)
1223 {
1224         struct _pl330_req *req = &thrd->req[index];
1225         struct pl330_xfer *x;
1226         u8 *buf = req->mc_cpu;
1227         int off = 0;
1228
1229         PL330_DBGMC_START(req->mc_bus);
1230
1231         /* DMAMOV CCR, ccr */
1232         off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
1233
1234         x = pxs->r->x;
1235
1236         if (!pxs->r->infiniteloop) {
1237                 do {
1238                         /* Error if xfer length is not aligned at burst size */
1239                         if (x->bytes % (BRST_SIZE(pxs->ccr)
1240                                                 * BRST_LEN(pxs->ccr)))
1241                                 return -EINVAL;
1242
1243                         pxs->x = x;
1244                         off += _setup_xfer(dry_run, &buf[off], pxs);
1245
1246                         x = x->next;
1247                 } while (x);
1248
1249                 /* DMASEV peripheral/event */
1250                 off += _emit_SEV(dry_run, &buf[off], thrd->ev);
1251                 /* DMAEND */
1252                 off += _emit_END(dry_run, &buf[off]);
1253         } else {
1254                 /* Error if xfer length is not aligned at burst size */
1255                 if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
1256                         return -EINVAL;
1257
1258                 pxs->x = x;
1259                 off += _setup_xfer_infiniteloop
1260                                 (dry_run, &buf[off], pxs, thrd->ev);
1261         }
1262         return off;
1263 }
1264
1265 static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
1266 {
1267         u32 ccr = 0;
1268
1269         if (rqc->src_inc)
1270                 ccr |= CC_SRCINC;
1271
1272         if (rqc->dst_inc)
1273                 ccr |= CC_DSTINC;
1274
1275         /* We set same protection levels for Src and DST for now */
1276         if (rqc->privileged)
1277                 ccr |= CC_SRCPRI | CC_DSTPRI;
1278         if (rqc->nonsecure)
1279                 ccr |= CC_SRCNS | CC_DSTNS;
1280         if (rqc->insnaccess)
1281                 ccr |= CC_SRCIA | CC_DSTIA;
1282
1283         ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
1284         ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
1285
1286         ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
1287         ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
1288
1289         ccr |= (rqc->dcctl << CC_SRCCCTRL_SHFT);
1290         ccr |= (rqc->scctl << CC_DSTCCTRL_SHFT);
1291
1292         ccr |= (rqc->swap << CC_SWAP_SHFT);
1293
1294         return ccr;
1295 }
1296
1297 static inline bool _is_valid(u32 ccr)
1298 {
1299         enum pl330_dstcachectrl dcctl;
1300         enum pl330_srccachectrl scctl;
1301
1302         dcctl = (ccr >> CC_DSTCCTRL_SHFT) & CC_DRCCCTRL_MASK;
1303         scctl = (ccr >> CC_SRCCCTRL_SHFT) & CC_SRCCCTRL_MASK;
1304
1305         if (dcctl == DINVALID1 || dcctl == DINVALID2
1306                         || scctl == SINVALID1 || scctl == SINVALID2)
1307                 return false;
1308         else
1309                 return true;
1310 }
1311
1312 /*
1313  * Submit a list of xfers after which the client wants notification.
1314  * Client is not notified after each xfer unit, just once after all
1315  * xfer units are done or some error occurs.
1316  */
1317 int pl330_submit_req(void *ch_id, struct pl330_req *r)
1318 {
1319         struct pl330_thread *thrd = ch_id;
1320         struct pl330_dmac *pl330;
1321         struct pl330_info *pi;
1322         struct _xfer_spec xs;
1323         unsigned long flags;
1324         void __iomem *regs;
1325         unsigned idx;
1326         u32 ccr;
1327         int ret = 0;
1328
1329         /* No Req or Unacquired Channel or DMAC */
1330         if (!r || !thrd || thrd->free)
1331                 return -EINVAL;
1332
1333         pl330 = thrd->dmac;
1334         pi = pl330->pinfo;
1335         regs = pi->base;
1336
1337         if (pl330->state == DYING
1338                 || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
1339                 dev_info(thrd->dmac->pinfo->dev, "%s:%d\n",
1340                         __func__, __LINE__);
1341                 return -EAGAIN;
1342         }
1343
1344         /* If request for non-existing peripheral */
1345         if (r->rqtype != MEMTOMEM && r->peri >= pi->pcfg.num_peri) {
1346                 dev_info(thrd->dmac->pinfo->dev,
1347                                 "%s:%d Invalid peripheral(%u)!\n",
1348                                 __func__, __LINE__, r->peri);
1349                 return -EINVAL;
1350         }
1351
1352         spin_lock_irqsave(&pl330->lock, flags);
1353
1354         if (_queue_full(thrd)) {
1355                 ret = -EAGAIN;
1356                 goto xfer_exit;
1357         }
1358
1359         /* Use last settings, if not provided */
1360         if (r->cfg) {
1361                 /* Prefer Secure Channel */
1362                 if (!_manager_ns(thrd))
1363                         r->cfg->nonsecure = 0;
1364                 else
1365                         r->cfg->nonsecure = 1;
1366                 ccr = _prepare_ccr(r->cfg);
1367         } else {
1368                 ccr = readl(regs + CC(thrd->id));
1369         }
1370
1371         /* If this req doesn't have valid xfer settings */
1372         if (!_is_valid(ccr)) {
1373                 ret = -EINVAL;
1374                 dev_info(thrd->dmac->pinfo->dev, "%s:%d Invalid CCR(%x)!\n",
1375                         __func__, __LINE__, ccr);
1376                 goto xfer_exit;
1377         }
1378
1379         idx = IS_FREE(&thrd->req[0]) ? 0 : 1;
1380
1381         xs.ccr = ccr;
1382         xs.r = r;
1383
1384         /* First dry run to check if req is acceptable */
1385         ret = _setup_req(1, thrd, idx, &xs);
1386         if (ret < 0)
1387                 goto xfer_exit;
1388
1389         if (ret > pi->mcbufsz / 2) {
1390                 dev_info(thrd->dmac->pinfo->dev,
1391                         "%s:%d Trying increasing mcbufsz\n",
1392                                 __func__, __LINE__);
1393                 ret = -ENOMEM;
1394                 goto xfer_exit;
1395         }
1396
1397         /* Hook the request */
1398         thrd->lstenq = idx;
1399         thrd->req[idx].mc_len = _setup_req(0, thrd, idx, &xs);
1400         thrd->req[idx].r = r;
1401
1402         ret = 0;
1403
1404 xfer_exit:
1405         spin_unlock_irqrestore(&pl330->lock, flags);
1406
1407         return ret;
1408 }
1409 EXPORT_SYMBOL(pl330_submit_req);
1410
1411 static void pl330_dotask(unsigned long data)
1412 {
1413         struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
1414         struct pl330_info *pi = pl330->pinfo;
1415         unsigned long flags;
1416         int i;
1417
1418         spin_lock_irqsave(&pl330->lock, flags);
1419
1420         /* The DMAC itself gone nuts */
1421         if (pl330->dmac_tbd.reset_dmac) {
1422                 pl330->state = DYING;
1423                 /* Reset the manager too */
1424                 pl330->dmac_tbd.reset_mngr = true;
1425                 /* Clear the reset flag */
1426                 pl330->dmac_tbd.reset_dmac = false;
1427         }
1428
1429         if (pl330->dmac_tbd.reset_mngr) {
1430                 _stop(pl330->manager);
1431                 /* Reset all channels */
1432                 pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1;
1433                 /* Clear the reset flag */
1434                 pl330->dmac_tbd.reset_mngr = false;
1435         }
1436
1437         for (i = 0; i < pi->pcfg.num_chan; i++) {
1438
1439                 if (pl330->dmac_tbd.reset_chan & (1 << i)) {
1440                         struct pl330_thread *thrd = &pl330->channels[i];
1441                         void __iomem *regs = pi->base;
1442                         enum pl330_op_err err;
1443
1444                         _stop(thrd);
1445
1446                         if (readl(regs + FSC) & (1 << thrd->id))
1447                                 err = PL330_ERR_FAIL;
1448                         else
1449                                 err = PL330_ERR_ABORT;
1450
1451                         spin_unlock_irqrestore(&pl330->lock, flags);
1452
1453                         _callback(thrd->req[1 - thrd->lstenq].r, err);
1454                         _callback(thrd->req[thrd->lstenq].r, err);
1455
1456                         spin_lock_irqsave(&pl330->lock, flags);
1457
1458                         thrd->req[0].r = NULL;
1459                         thrd->req[1].r = NULL;
1460                         MARK_FREE(&thrd->req[0]);
1461                         MARK_FREE(&thrd->req[1]);
1462
1463                         /* Clear the reset flag */
1464                         pl330->dmac_tbd.reset_chan &= ~(1 << i);
1465                 }
1466         }
1467
1468         spin_unlock_irqrestore(&pl330->lock, flags);
1469
1470         return;
1471 }
1472
1473 /* Returns 1 if state was updated, 0 otherwise */
1474 int pl330_update(const struct pl330_info *pi)
1475 {
1476         struct _pl330_req *rqdone;
1477         struct pl330_dmac *pl330;
1478         unsigned long flags;
1479         void __iomem *regs;
1480         u32 val;
1481         int id, ev, ret = 0;
1482
1483         if (!pi || !pi->pl330_data)
1484                 return 0;
1485
1486         regs = pi->base;
1487         pl330 = pi->pl330_data;
1488
1489         spin_lock_irqsave(&pl330->lock, flags);
1490
1491         val = readl(regs + FSM) & 0x1;
1492         if (val)
1493                 pl330->dmac_tbd.reset_mngr = true;
1494         else
1495                 pl330->dmac_tbd.reset_mngr = false;
1496
1497         val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1);
1498         pl330->dmac_tbd.reset_chan |= val;
1499         if (val) {
1500                 int i = 0;
1501                 while (i < pi->pcfg.num_chan) {
1502                         if (val & (1 << i)) {
1503                                 dev_info(pi->dev,
1504                                         "Reset Channel-%d\t CS-%x FTC-%x\n",
1505                                                 i, readl(regs + CS(i)),
1506                                                 readl(regs + FTC(i)));
1507                                 _stop(&pl330->channels[i]);
1508                         }
1509                         i++;
1510                 }
1511         }
1512
1513         /* Check which event happened i.e, thread notified */
1514         val = readl(regs + ES);
1515         if (pi->pcfg.num_events < 32
1516                         && val & ~((1 << pi->pcfg.num_events) - 1)) {
1517                 pl330->dmac_tbd.reset_dmac = true;
1518                 dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__);
1519                 ret = 1;
1520                 goto updt_exit;
1521         }
1522
1523         for (ev = 0; ev < pi->pcfg.num_events; ev++) {
1524                 if (val & (1 << ev)) { /* Event occured */
1525                         struct pl330_thread *thrd;
1526                         u32 inten = readl(regs + INTEN);
1527                         int active;
1528
1529                         /* Clear the event */
1530                         if (inten & (1 << ev))
1531                                 writel(1 << ev, regs + INTCLR);
1532
1533                         ret = 1;
1534
1535                         id = pl330->events[ev];
1536                         if (id == -1)
1537                                 continue;
1538
1539                         thrd = &pl330->channels[id];
1540
1541                         active = _thrd_active(thrd);
1542                         if (!active) /* Aborted */
1543                                 continue;
1544
1545                         active -= 1;
1546
1547                         rqdone = &thrd->req[active];
1548
1549                         if (!rqdone->r->infiniteloop) {
1550                                 MARK_FREE(rqdone);
1551
1552                                 /* Get going again ASAP */
1553                                 _start(thrd);
1554                         }
1555
1556                         /* For now, just make a list of callbacks to be done */
1557                         list_add_tail(&rqdone->rqd, &pl330->req_done);
1558                 }
1559         }
1560
1561         /* Now that we are in no hurry, do the callbacks */
1562         while (!list_empty(&pl330->req_done)) {
1563                 rqdone = container_of(pl330->req_done.next,
1564                                         struct _pl330_req, rqd);
1565
1566                 list_del_init(&rqdone->rqd);
1567
1568                 spin_unlock_irqrestore(&pl330->lock, flags);
1569                 _callback(rqdone->r, PL330_ERR_NONE);
1570                 spin_lock_irqsave(&pl330->lock, flags);
1571         }
1572
1573 updt_exit:
1574         spin_unlock_irqrestore(&pl330->lock, flags);
1575
1576         if (pl330->dmac_tbd.reset_dmac
1577                         || pl330->dmac_tbd.reset_mngr
1578                         || pl330->dmac_tbd.reset_chan) {
1579                 ret = 1;
1580                 tasklet_schedule(&pl330->tasks);
1581         }
1582
1583         return ret;
1584 }
1585 EXPORT_SYMBOL(pl330_update);
1586
1587 int pl330_chan_ctrl(void *ch_id, enum pl330_chan_op op)
1588 {
1589         struct pl330_thread *thrd = ch_id;
1590         struct pl330_dmac *pl330;
1591         unsigned long flags;
1592         int ret = 0, active;
1593
1594         if (!thrd || thrd->free || thrd->dmac->state == DYING)
1595                 return -EINVAL;
1596
1597         pl330 = thrd->dmac;
1598
1599         spin_lock_irqsave(&pl330->lock, flags);
1600
1601         switch (op) {
1602         case PL330_OP_FLUSH:
1603                 /* Make sure the channel is stopped */
1604                 _stop(thrd);
1605
1606                 thrd->req[0].r = NULL;
1607                 thrd->req[1].r = NULL;
1608                 MARK_FREE(&thrd->req[0]);
1609                 MARK_FREE(&thrd->req[1]);
1610                 break;
1611
1612         case PL330_OP_ABORT:
1613                 active = _thrd_active(thrd);
1614
1615                 /* Make sure the channel is stopped */
1616                 _stop(thrd);
1617
1618                 /* ABORT is only for the active req */
1619                 if (!active)
1620                         break;
1621
1622                 active--;
1623
1624                 thrd->req[active].r = NULL;
1625                 MARK_FREE(&thrd->req[active]);
1626
1627                 /* Start the next */
1628         case PL330_OP_START:
1629                 if (!_start(thrd))
1630                         ret = -EIO;
1631                 break;
1632
1633         default:
1634                 ret = -EINVAL;
1635         }
1636
1637         spin_unlock_irqrestore(&pl330->lock, flags);
1638         return ret;
1639 }
1640 EXPORT_SYMBOL(pl330_chan_ctrl);
1641
1642 int pl330_chan_status(void *ch_id, struct pl330_chanstatus *pstatus)
1643 {
1644         struct pl330_thread *thrd = ch_id;
1645         struct pl330_dmac *pl330;
1646         struct pl330_info *pi;
1647         void __iomem *regs;
1648         int active;
1649         u32 val;
1650
1651         if (!pstatus || !thrd || thrd->free)
1652                 return -EINVAL;
1653
1654         pl330 = thrd->dmac;
1655         pi = pl330->pinfo;
1656         regs = pi->base;
1657
1658         /* The client should remove the DMAC and add again */
1659         if (pl330->state == DYING)
1660                 pstatus->dmac_halted = true;
1661         else
1662                 pstatus->dmac_halted = false;
1663
1664         val = readl(regs + FSC);
1665         if (val & (1 << thrd->id))
1666                 pstatus->faulting = true;
1667         else
1668                 pstatus->faulting = false;
1669
1670         active = _thrd_active(thrd);
1671
1672         if (!active) {
1673                 /* Indicate that the thread is not running */
1674                 pstatus->top_req = NULL;
1675                 pstatus->wait_req = NULL;
1676         } else {
1677                 active--;
1678                 pstatus->top_req = thrd->req[active].r;
1679                 pstatus->wait_req = !IS_FREE(&thrd->req[1 - active])
1680                                         ? thrd->req[1 - active].r : NULL;
1681         }
1682
1683         pstatus->src_addr = readl(regs + SA(thrd->id));
1684         pstatus->dst_addr = readl(regs + DA(thrd->id));
1685
1686         return 0;
1687 }
1688 EXPORT_SYMBOL(pl330_chan_status);
1689
1690 /* Reserve an event */
1691 static inline int _alloc_event(struct pl330_thread *thrd)
1692 {
1693         struct pl330_dmac *pl330 = thrd->dmac;
1694         struct pl330_info *pi = pl330->pinfo;
1695         int ev;
1696
1697         for (ev = 0; ev < pi->pcfg.num_events; ev++)
1698                 if (pl330->events[ev] == -1) {
1699                         pl330->events[ev] = thrd->id;
1700                         return ev;
1701                 }
1702
1703         return -1;
1704 }
1705
1706 /* Upon success, returns IdentityToken for the
1707  * allocated channel, NULL otherwise.
1708  */
1709 void *pl330_request_channel(const struct pl330_info *pi)
1710 {
1711         struct pl330_thread *thrd = NULL;
1712         struct pl330_dmac *pl330;
1713         unsigned long flags;
1714         int chans, i;
1715
1716         if (!pi || !pi->pl330_data)
1717                 return NULL;
1718
1719         pl330 = pi->pl330_data;
1720
1721         if (pl330->state == DYING)
1722                 return NULL;
1723
1724         chans = pi->pcfg.num_chan;
1725
1726         spin_lock_irqsave(&pl330->lock, flags);
1727
1728         for (i = 0; i < chans; i++) {
1729                 thrd = &pl330->channels[i];
1730                 if (thrd->free) {
1731                         thrd->ev = _alloc_event(thrd);
1732                         if (thrd->ev >= 0) {
1733                                 thrd->free = false;
1734                                 thrd->lstenq = 1;
1735                                 thrd->req[0].r = NULL;
1736                                 MARK_FREE(&thrd->req[0]);
1737                                 thrd->req[1].r = NULL;
1738                                 MARK_FREE(&thrd->req[1]);
1739                                 break;
1740                         }
1741                 }
1742                 thrd = NULL;
1743         }
1744
1745         spin_unlock_irqrestore(&pl330->lock, flags);
1746
1747         return thrd;
1748 }
1749 EXPORT_SYMBOL(pl330_request_channel);
1750
1751 /* Release an event */
1752 static inline void _free_event(struct pl330_thread *thrd, int ev)
1753 {
1754         struct pl330_dmac *pl330 = thrd->dmac;
1755         struct pl330_info *pi = pl330->pinfo;
1756
1757         /* If the event is valid and was held by the thread */
1758         if (ev >= 0 && ev < pi->pcfg.num_events
1759                         && pl330->events[ev] == thrd->id)
1760                 pl330->events[ev] = -1;
1761 }
1762
1763 void pl330_release_channel(void *ch_id)
1764 {
1765         struct pl330_thread *thrd = ch_id;
1766         struct pl330_dmac *pl330;
1767         unsigned long flags;
1768
1769         if (!thrd || thrd->free)
1770                 return;
1771
1772         _stop(thrd);
1773
1774         _callback(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT);
1775         _callback(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT);
1776
1777         pl330 = thrd->dmac;
1778
1779         spin_lock_irqsave(&pl330->lock, flags);
1780         _free_event(thrd, thrd->ev);
1781         thrd->free = true;
1782         spin_unlock_irqrestore(&pl330->lock, flags);
1783 }
1784 EXPORT_SYMBOL(pl330_release_channel);
1785
1786 /* Initialize the structure for PL330 configuration, that can be used
1787  * by the client driver the make best use of the DMAC
1788  */
1789 static void read_dmac_config(struct pl330_info *pi)
1790 {
1791         void __iomem *regs = pi->base;
1792         u32 val;
1793
1794         val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
1795         val &= CRD_DATA_WIDTH_MASK;
1796         pi->pcfg.data_bus_width = 8 * (1 << val);
1797
1798         val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
1799         val &= CRD_DATA_BUFF_MASK;
1800         pi->pcfg.data_buf_dep = val + 1;
1801
1802         val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
1803         val &= CR0_NUM_CHANS_MASK;
1804         val += 1;
1805         pi->pcfg.num_chan = val;
1806
1807         val = readl(regs + CR0);
1808         if (val & CR0_PERIPH_REQ_SET) {
1809                 val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
1810                 val += 1;
1811                 pi->pcfg.num_peri = val;
1812                 pi->pcfg.peri_ns = readl(regs + CR4);
1813         } else {
1814                 pi->pcfg.num_peri = 0;
1815         }
1816
1817         val = readl(regs + CR0);
1818         if (val & CR0_BOOT_MAN_NS)
1819                 pi->pcfg.mode |= DMAC_MODE_NS;
1820         else
1821                 pi->pcfg.mode &= ~DMAC_MODE_NS;
1822
1823         val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
1824         val &= CR0_NUM_EVENTS_MASK;
1825         val += 1;
1826         pi->pcfg.num_events = val;
1827
1828         pi->pcfg.irq_ns = readl(regs + CR3);
1829
1830         pi->pcfg.periph_id = get_id(pi, PERIPH_ID);
1831         pi->pcfg.pcell_id = get_id(pi, PCELL_ID);
1832 }
1833
1834 static inline void _reset_thread(struct pl330_thread *thrd)
1835 {
1836         struct pl330_dmac *pl330 = thrd->dmac;
1837         struct pl330_info *pi = pl330->pinfo;
1838
1839         thrd->req[0].mc_cpu = pl330->mcode_cpu
1840                                 + (thrd->id * pi->mcbufsz);
1841         thrd->req[0].mc_bus = pl330->mcode_dma
1842                                 + (thrd->id * pi->mcbufsz);
1843         thrd->req[0].r = NULL;
1844         MARK_FREE(&thrd->req[0]);
1845
1846         thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
1847                                 + pi->mcbufsz / 2;
1848         thrd->req[1].mc_bus = thrd->req[0].mc_bus
1849                                 + pi->mcbufsz / 2;
1850         thrd->req[1].r = NULL;
1851         MARK_FREE(&thrd->req[1]);
1852 }
1853
1854 static int dmac_alloc_threads(struct pl330_dmac *pl330)
1855 {
1856         struct pl330_info *pi = pl330->pinfo;
1857         int chans = pi->pcfg.num_chan;
1858         struct pl330_thread *thrd;
1859         int i;
1860
1861         /* Allocate 1 Manager and 'chans' Channel threads */
1862         pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
1863                                         GFP_KERNEL);
1864         if (!pl330->channels)
1865                 return -ENOMEM;
1866
1867         /* Init Channel threads */
1868         for (i = 0; i < chans; i++) {
1869                 thrd = &pl330->channels[i];
1870                 thrd->id = i;
1871                 thrd->dmac = pl330;
1872                 _reset_thread(thrd);
1873                 thrd->free = true;
1874         }
1875
1876         /* MANAGER is indexed at the end */
1877         thrd = &pl330->channels[chans];
1878         thrd->id = chans;
1879         thrd->dmac = pl330;
1880         thrd->free = false;
1881         pl330->manager = thrd;
1882
1883         return 0;
1884 }
1885
1886 static int dmac_alloc_resources(struct pl330_dmac *pl330)
1887 {
1888         struct pl330_info *pi = pl330->pinfo;
1889         int chans = pi->pcfg.num_chan;
1890         int ret;
1891
1892         /*
1893          * Alloc MicroCode buffer for 'chans' Channel threads.
1894          * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
1895          */
1896         pl330->mcode_cpu = dma_alloc_coherent(pi->dev,
1897                                 chans * pi->mcbufsz,
1898                                 &pl330->mcode_bus, GFP_KERNEL);
1899         if (!pl330->mcode_cpu) {
1900                 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
1901                         __func__, __LINE__);
1902                 return -ENOMEM;
1903         }
1904
1905         if(pi->va_mode)
1906                 pl330->mcode_dma = (u32)pl330->mcode_cpu;
1907         else
1908                 pl330->mcode_dma = pl330->mcode_bus;
1909
1910         ret = dmac_alloc_threads(pl330);
1911         if (ret) {
1912                 dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n",
1913                         __func__, __LINE__);
1914                 dma_free_coherent(pi->dev,
1915                                 chans * pi->mcbufsz,
1916                                 pl330->mcode_cpu, pl330->mcode_bus);
1917                 return ret;
1918         }
1919
1920         return 0;
1921 }
1922
1923 int pl330_add(struct pl330_info *pi)
1924 {
1925         struct pl330_dmac *pl330;
1926         void __iomem *regs;
1927         int i, ret;
1928
1929         if (!pi || !pi->dev)
1930                 return -EINVAL;
1931
1932         /* If already added */
1933         if (pi->pl330_data)
1934                 return -EINVAL;
1935
1936         /*
1937          * If the SoC can perform reset on the DMAC, then do it
1938          * before reading its configuration.
1939          */
1940         if (pi->dmac_reset)
1941                 pi->dmac_reset(pi);
1942
1943         regs = pi->base;
1944
1945         /* Check if we can handle this DMAC */
1946         if ((get_id(pi, PERIPH_ID) & 0xfffff) != PERIPH_ID_VAL
1947            || get_id(pi, PCELL_ID) != PCELL_ID_VAL) {
1948                 dev_err(pi->dev, "PERIPH_ID 0x%x, PCELL_ID 0x%x !\n",
1949                         get_id(pi, PERIPH_ID), get_id(pi, PCELL_ID));
1950                 return -EINVAL;
1951         }
1952
1953         /* Read the configuration of the DMAC */
1954         read_dmac_config(pi);
1955
1956         if (pi->pcfg.num_events == 0) {
1957                 dev_err(pi->dev, "%s:%d Can't work without events!\n",
1958                         __func__, __LINE__);
1959                 return -EINVAL;
1960         }
1961
1962         pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL);
1963         if (!pl330) {
1964                 dev_err(pi->dev, "%s:%d Can't allocate memory!\n",
1965                         __func__, __LINE__);
1966                 return -ENOMEM;
1967         }
1968
1969         /* Assign the info structure and private data */
1970         pl330->pinfo = pi;
1971         pi->pl330_data = pl330;
1972
1973         spin_lock_init(&pl330->lock);
1974
1975         INIT_LIST_HEAD(&pl330->req_done);
1976
1977         /* Use default MC buffer size if not provided */
1978         if (!pi->mcbufsz)
1979                 pi->mcbufsz = MCODE_BUFF_PER_REQ * 2;
1980
1981         /* Mark all events as free */
1982         for (i = 0; i < pi->pcfg.num_events; i++)
1983                 pl330->events[i] = -1;
1984
1985         /* Allocate resources needed by the DMAC */
1986         ret = dmac_alloc_resources(pl330);
1987         if (ret) {
1988                 dev_err(pi->dev, "Unable to create channels for DMAC\n");
1989                 kfree(pl330);
1990                 return ret;
1991         }
1992
1993         tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
1994
1995         pl330->state = INIT;
1996
1997         return 0;
1998 }
1999 EXPORT_SYMBOL(pl330_add);
2000
2001 static int dmac_free_threads(struct pl330_dmac *pl330)
2002 {
2003         struct pl330_info *pi = pl330->pinfo;
2004         int chans = pi->pcfg.num_chan;
2005         struct pl330_thread *thrd;
2006         int i;
2007
2008         /* Release Channel threads */
2009         for (i = 0; i < chans; i++) {
2010                 thrd = &pl330->channels[i];
2011                 pl330_release_channel((void *)thrd);
2012         }
2013
2014         /* Free memory */
2015         kfree(pl330->channels);
2016
2017         return 0;
2018 }
2019
2020 static void dmac_free_resources(struct pl330_dmac *pl330)
2021 {
2022         struct pl330_info *pi = pl330->pinfo;
2023         int chans = pi->pcfg.num_chan;
2024
2025         dmac_free_threads(pl330);
2026
2027         dma_free_coherent(pi->dev, chans * pi->mcbufsz,
2028                                 pl330->mcode_cpu, pl330->mcode_bus);
2029 }
2030
2031 void pl330_del(struct pl330_info *pi)
2032 {
2033         struct pl330_dmac *pl330;
2034
2035         if (!pi || !pi->pl330_data)
2036                 return;
2037
2038         pl330 = pi->pl330_data;
2039
2040         pl330->state = UNINIT;
2041
2042         tasklet_kill(&pl330->tasks);
2043
2044         /* Free DMAC resources */
2045         dmac_free_resources(pl330);
2046
2047         kfree(pl330);
2048         pi->pl330_data = NULL;
2049 }
2050 EXPORT_SYMBOL(pl330_del);