pl330.c 41 KB

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