pl330.c 41 KB

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