pl330.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916
  1. /*
  2. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Copyright (C) 2010 Samsung Electronics Co. Ltd.
  6. * Jaswinder Singh <jassi.brar@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/io.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/string.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/amba/bus.h>
  24. #include <linux/amba/pl330.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/of.h>
  27. #include <linux/of_dma.h>
  28. #include <linux/err.h>
  29. #include <linux/pm_runtime.h>
  30. #include "dmaengine.h"
  31. #define PL330_MAX_CHAN 8
  32. #define PL330_MAX_IRQS 32
  33. #define PL330_MAX_PERI 32
  34. enum pl330_cachectrl {
  35. CCTRL0, /* Noncacheable and nonbufferable */
  36. CCTRL1, /* Bufferable only */
  37. CCTRL2, /* Cacheable, but do not allocate */
  38. CCTRL3, /* Cacheable and bufferable, but do not allocate */
  39. INVALID1, /* AWCACHE = 0x1000 */
  40. INVALID2,
  41. CCTRL6, /* Cacheable write-through, allocate on writes only */
  42. CCTRL7, /* Cacheable write-back, allocate on writes only */
  43. };
  44. enum pl330_byteswap {
  45. SWAP_NO,
  46. SWAP_2,
  47. SWAP_4,
  48. SWAP_8,
  49. SWAP_16,
  50. };
  51. /* Register and Bit field Definitions */
  52. #define DS 0x0
  53. #define DS_ST_STOP 0x0
  54. #define DS_ST_EXEC 0x1
  55. #define DS_ST_CMISS 0x2
  56. #define DS_ST_UPDTPC 0x3
  57. #define DS_ST_WFE 0x4
  58. #define DS_ST_ATBRR 0x5
  59. #define DS_ST_QBUSY 0x6
  60. #define DS_ST_WFP 0x7
  61. #define DS_ST_KILL 0x8
  62. #define DS_ST_CMPLT 0x9
  63. #define DS_ST_FLTCMP 0xe
  64. #define DS_ST_FAULT 0xf
  65. #define DPC 0x4
  66. #define INTEN 0x20
  67. #define ES 0x24
  68. #define INTSTATUS 0x28
  69. #define INTCLR 0x2c
  70. #define FSM 0x30
  71. #define FSC 0x34
  72. #define FTM 0x38
  73. #define _FTC 0x40
  74. #define FTC(n) (_FTC + (n)*0x4)
  75. #define _CS 0x100
  76. #define CS(n) (_CS + (n)*0x8)
  77. #define CS_CNS (1 << 21)
  78. #define _CPC 0x104
  79. #define CPC(n) (_CPC + (n)*0x8)
  80. #define _SA 0x400
  81. #define SA(n) (_SA + (n)*0x20)
  82. #define _DA 0x404
  83. #define DA(n) (_DA + (n)*0x20)
  84. #define _CC 0x408
  85. #define CC(n) (_CC + (n)*0x20)
  86. #define CC_SRCINC (1 << 0)
  87. #define CC_DSTINC (1 << 14)
  88. #define CC_SRCPRI (1 << 8)
  89. #define CC_DSTPRI (1 << 22)
  90. #define CC_SRCNS (1 << 9)
  91. #define CC_DSTNS (1 << 23)
  92. #define CC_SRCIA (1 << 10)
  93. #define CC_DSTIA (1 << 24)
  94. #define CC_SRCBRSTLEN_SHFT 4
  95. #define CC_DSTBRSTLEN_SHFT 18
  96. #define CC_SRCBRSTSIZE_SHFT 1
  97. #define CC_DSTBRSTSIZE_SHFT 15
  98. #define CC_SRCCCTRL_SHFT 11
  99. #define CC_SRCCCTRL_MASK 0x7
  100. #define CC_DSTCCTRL_SHFT 25
  101. #define CC_DRCCCTRL_MASK 0x7
  102. #define CC_SWAP_SHFT 28
  103. #define _LC0 0x40c
  104. #define LC0(n) (_LC0 + (n)*0x20)
  105. #define _LC1 0x410
  106. #define LC1(n) (_LC1 + (n)*0x20)
  107. #define DBGSTATUS 0xd00
  108. #define DBG_BUSY (1 << 0)
  109. #define DBGCMD 0xd04
  110. #define DBGINST0 0xd08
  111. #define DBGINST1 0xd0c
  112. #define CR0 0xe00
  113. #define CR1 0xe04
  114. #define CR2 0xe08
  115. #define CR3 0xe0c
  116. #define CR4 0xe10
  117. #define CRD 0xe14
  118. #define PERIPH_ID 0xfe0
  119. #define PERIPH_REV_SHIFT 20
  120. #define PERIPH_REV_MASK 0xf
  121. #define PERIPH_REV_R0P0 0
  122. #define PERIPH_REV_R1P0 1
  123. #define PERIPH_REV_R1P1 2
  124. #define CR0_PERIPH_REQ_SET (1 << 0)
  125. #define CR0_BOOT_EN_SET (1 << 1)
  126. #define CR0_BOOT_MAN_NS (1 << 2)
  127. #define CR0_NUM_CHANS_SHIFT 4
  128. #define CR0_NUM_CHANS_MASK 0x7
  129. #define CR0_NUM_PERIPH_SHIFT 12
  130. #define CR0_NUM_PERIPH_MASK 0x1f
  131. #define CR0_NUM_EVENTS_SHIFT 17
  132. #define CR0_NUM_EVENTS_MASK 0x1f
  133. #define CR1_ICACHE_LEN_SHIFT 0
  134. #define CR1_ICACHE_LEN_MASK 0x7
  135. #define CR1_NUM_ICACHELINES_SHIFT 4
  136. #define CR1_NUM_ICACHELINES_MASK 0xf
  137. #define CRD_DATA_WIDTH_SHIFT 0
  138. #define CRD_DATA_WIDTH_MASK 0x7
  139. #define CRD_WR_CAP_SHIFT 4
  140. #define CRD_WR_CAP_MASK 0x7
  141. #define CRD_WR_Q_DEP_SHIFT 8
  142. #define CRD_WR_Q_DEP_MASK 0xf
  143. #define CRD_RD_CAP_SHIFT 12
  144. #define CRD_RD_CAP_MASK 0x7
  145. #define CRD_RD_Q_DEP_SHIFT 16
  146. #define CRD_RD_Q_DEP_MASK 0xf
  147. #define CRD_DATA_BUFF_SHIFT 20
  148. #define CRD_DATA_BUFF_MASK 0x3ff
  149. #define PART 0x330
  150. #define DESIGNER 0x41
  151. #define REVISION 0x0
  152. #define INTEG_CFG 0x0
  153. #define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))
  154. #define PL330_STATE_STOPPED (1 << 0)
  155. #define PL330_STATE_EXECUTING (1 << 1)
  156. #define PL330_STATE_WFE (1 << 2)
  157. #define PL330_STATE_FAULTING (1 << 3)
  158. #define PL330_STATE_COMPLETING (1 << 4)
  159. #define PL330_STATE_WFP (1 << 5)
  160. #define PL330_STATE_KILLING (1 << 6)
  161. #define PL330_STATE_FAULT_COMPLETING (1 << 7)
  162. #define PL330_STATE_CACHEMISS (1 << 8)
  163. #define PL330_STATE_UPDTPC (1 << 9)
  164. #define PL330_STATE_ATBARRIER (1 << 10)
  165. #define PL330_STATE_QUEUEBUSY (1 << 11)
  166. #define PL330_STATE_INVALID (1 << 15)
  167. #define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \
  168. | PL330_STATE_WFE | PL330_STATE_FAULTING)
  169. #define CMD_DMAADDH 0x54
  170. #define CMD_DMAEND 0x00
  171. #define CMD_DMAFLUSHP 0x35
  172. #define CMD_DMAGO 0xa0
  173. #define CMD_DMALD 0x04
  174. #define CMD_DMALDP 0x25
  175. #define CMD_DMALP 0x20
  176. #define CMD_DMALPEND 0x28
  177. #define CMD_DMAKILL 0x01
  178. #define CMD_DMAMOV 0xbc
  179. #define CMD_DMANOP 0x18
  180. #define CMD_DMARMB 0x12
  181. #define CMD_DMASEV 0x34
  182. #define CMD_DMAST 0x08
  183. #define CMD_DMASTP 0x29
  184. #define CMD_DMASTZ 0x0c
  185. #define CMD_DMAWFE 0x36
  186. #define CMD_DMAWFP 0x30
  187. #define CMD_DMAWMB 0x13
  188. #define SZ_DMAADDH 3
  189. #define SZ_DMAEND 1
  190. #define SZ_DMAFLUSHP 2
  191. #define SZ_DMALD 1
  192. #define SZ_DMALDP 2
  193. #define SZ_DMALP 2
  194. #define SZ_DMALPEND 2
  195. #define SZ_DMAKILL 1
  196. #define SZ_DMAMOV 6
  197. #define SZ_DMANOP 1
  198. #define SZ_DMARMB 1
  199. #define SZ_DMASEV 2
  200. #define SZ_DMAST 1
  201. #define SZ_DMASTP 2
  202. #define SZ_DMASTZ 1
  203. #define SZ_DMAWFE 2
  204. #define SZ_DMAWFP 2
  205. #define SZ_DMAWMB 1
  206. #define SZ_DMAGO 6
  207. #define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
  208. #define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))
  209. #define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
  210. #define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
  211. /*
  212. * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
  213. * at 1byte/burst for P<->M and M<->M respectively.
  214. * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req
  215. * should be enough for P<->M and M<->M respectively.
  216. */
  217. #define MCODE_BUFF_PER_REQ 256
  218. /* Use this _only_ to wait on transient states */
  219. #define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax();
  220. #ifdef PL330_DEBUG_MCGEN
  221. static unsigned cmd_line;
  222. #define PL330_DBGCMD_DUMP(off, x...) do { \
  223. printk("%x:", cmd_line); \
  224. printk(x); \
  225. cmd_line += off; \
  226. } while (0)
  227. #define PL330_DBGMC_START(addr) (cmd_line = addr)
  228. #else
  229. #define PL330_DBGCMD_DUMP(off, x...) do {} while (0)
  230. #define PL330_DBGMC_START(addr) do {} while (0)
  231. #endif
  232. /* The number of default descriptors */
  233. #define NR_DEFAULT_DESC 16
  234. /* Delay for runtime PM autosuspend, ms */
  235. #define PL330_AUTOSUSPEND_DELAY 20
  236. /* Populated by the PL330 core driver for DMA API driver's info */
  237. struct pl330_config {
  238. u32 periph_id;
  239. #define DMAC_MODE_NS (1 << 0)
  240. unsigned int mode;
  241. unsigned int data_bus_width:10; /* In number of bits */
  242. unsigned int data_buf_dep:11;
  243. unsigned int num_chan:4;
  244. unsigned int num_peri:6;
  245. u32 peri_ns;
  246. unsigned int num_events:6;
  247. u32 irq_ns;
  248. };
  249. /**
  250. * Request Configuration.
  251. * The PL330 core does not modify this and uses the last
  252. * working configuration if the request doesn't provide any.
  253. *
  254. * The Client may want to provide this info only for the
  255. * first request and a request with new settings.
  256. */
  257. struct pl330_reqcfg {
  258. /* Address Incrementing */
  259. unsigned dst_inc:1;
  260. unsigned src_inc:1;
  261. /*
  262. * For now, the SRC & DST protection levels
  263. * and burst size/length are assumed same.
  264. */
  265. bool nonsecure;
  266. bool privileged;
  267. bool insnaccess;
  268. unsigned brst_len:5;
  269. unsigned brst_size:3; /* in power of 2 */
  270. enum pl330_cachectrl dcctl;
  271. enum pl330_cachectrl scctl;
  272. enum pl330_byteswap swap;
  273. struct pl330_config *pcfg;
  274. };
  275. /*
  276. * One cycle of DMAC operation.
  277. * There may be more than one xfer in a request.
  278. */
  279. struct pl330_xfer {
  280. u32 src_addr;
  281. u32 dst_addr;
  282. /* Size to xfer */
  283. u32 bytes;
  284. };
  285. /* The xfer callbacks are made with one of these arguments. */
  286. enum pl330_op_err {
  287. /* The all xfers in the request were success. */
  288. PL330_ERR_NONE,
  289. /* If req aborted due to global error. */
  290. PL330_ERR_ABORT,
  291. /* If req failed due to problem with Channel. */
  292. PL330_ERR_FAIL,
  293. };
  294. enum dmamov_dst {
  295. SAR = 0,
  296. CCR,
  297. DAR,
  298. };
  299. enum pl330_dst {
  300. SRC = 0,
  301. DST,
  302. };
  303. enum pl330_cond {
  304. SINGLE,
  305. BURST,
  306. ALWAYS,
  307. };
  308. struct dma_pl330_desc;
  309. struct _pl330_req {
  310. u32 mc_bus;
  311. void *mc_cpu;
  312. struct dma_pl330_desc *desc;
  313. };
  314. /* ToBeDone for tasklet */
  315. struct _pl330_tbd {
  316. bool reset_dmac;
  317. bool reset_mngr;
  318. u8 reset_chan;
  319. };
  320. /* A DMAC Thread */
  321. struct pl330_thread {
  322. u8 id;
  323. int ev;
  324. /* If the channel is not yet acquired by any client */
  325. bool free;
  326. /* Parent DMAC */
  327. struct pl330_dmac *dmac;
  328. /* Only two at a time */
  329. struct _pl330_req req[2];
  330. /* Index of the last enqueued request */
  331. unsigned lstenq;
  332. /* Index of the last submitted request or -1 if the DMA is stopped */
  333. int req_running;
  334. };
  335. enum pl330_dmac_state {
  336. UNINIT,
  337. INIT,
  338. DYING,
  339. };
  340. enum desc_status {
  341. /* In the DMAC pool */
  342. FREE,
  343. /*
  344. * Allocated to some channel during prep_xxx
  345. * Also may be sitting on the work_list.
  346. */
  347. PREP,
  348. /*
  349. * Sitting on the work_list and already submitted
  350. * to the PL330 core. Not more than two descriptors
  351. * of a channel can be BUSY at any time.
  352. */
  353. BUSY,
  354. /*
  355. * Sitting on the channel work_list but xfer done
  356. * by PL330 core
  357. */
  358. DONE,
  359. };
  360. struct dma_pl330_chan {
  361. /* Schedule desc completion */
  362. struct tasklet_struct task;
  363. /* DMA-Engine Channel */
  364. struct dma_chan chan;
  365. /* List of submitted descriptors */
  366. struct list_head submitted_list;
  367. /* List of issued descriptors */
  368. struct list_head work_list;
  369. /* List of completed descriptors */
  370. struct list_head completed_list;
  371. /* Pointer to the DMAC that manages this channel,
  372. * NULL if the channel is available to be acquired.
  373. * As the parent, this DMAC also provides descriptors
  374. * to the channel.
  375. */
  376. struct pl330_dmac *dmac;
  377. /* To protect channel manipulation */
  378. spinlock_t lock;
  379. /*
  380. * Hardware channel thread of PL330 DMAC. NULL if the channel is
  381. * available.
  382. */
  383. struct pl330_thread *thread;
  384. /* For D-to-M and M-to-D channels */
  385. int burst_sz; /* the peripheral fifo width */
  386. int burst_len; /* the number of burst */
  387. dma_addr_t fifo_addr;
  388. /* for cyclic capability */
  389. bool cyclic;
  390. };
  391. struct pl330_dmac {
  392. /* DMA-Engine Device */
  393. struct dma_device ddma;
  394. /* Holds info about sg limitations */
  395. struct device_dma_parameters dma_parms;
  396. /* Pool of descriptors available for the DMAC's channels */
  397. struct list_head desc_pool;
  398. /* To protect desc_pool manipulation */
  399. spinlock_t pool_lock;
  400. /* Size of MicroCode buffers for each channel. */
  401. unsigned mcbufsz;
  402. /* ioremap'ed address of PL330 registers. */
  403. void __iomem *base;
  404. /* Populated by the PL330 core driver during pl330_add */
  405. struct pl330_config pcfg;
  406. spinlock_t lock;
  407. /* Maximum possible events/irqs */
  408. int events[32];
  409. /* BUS address of MicroCode buffer */
  410. dma_addr_t mcode_bus;
  411. /* CPU address of MicroCode buffer */
  412. void *mcode_cpu;
  413. /* List of all Channel threads */
  414. struct pl330_thread *channels;
  415. /* Pointer to the MANAGER thread */
  416. struct pl330_thread *manager;
  417. /* To handle bad news in interrupt */
  418. struct tasklet_struct tasks;
  419. struct _pl330_tbd dmac_tbd;
  420. /* State of DMAC operation */
  421. enum pl330_dmac_state state;
  422. /* Holds list of reqs with due callbacks */
  423. struct list_head req_done;
  424. /* Peripheral channels connected to this DMAC */
  425. unsigned int num_peripherals;
  426. struct dma_pl330_chan *peripherals; /* keep at end */
  427. };
  428. struct dma_pl330_desc {
  429. /* To attach to a queue as child */
  430. struct list_head node;
  431. /* Descriptor for the DMA Engine API */
  432. struct dma_async_tx_descriptor txd;
  433. /* Xfer for PL330 core */
  434. struct pl330_xfer px;
  435. struct pl330_reqcfg rqcfg;
  436. enum desc_status status;
  437. /* The channel which currently holds this desc */
  438. struct dma_pl330_chan *pchan;
  439. enum dma_transfer_direction rqtype;
  440. /* Index of peripheral for the xfer. */
  441. unsigned peri:5;
  442. /* Hook to attach to DMAC's list of reqs with due callback */
  443. struct list_head rqd;
  444. };
  445. struct _xfer_spec {
  446. u32 ccr;
  447. struct dma_pl330_desc *desc;
  448. };
  449. static inline bool _queue_empty(struct pl330_thread *thrd)
  450. {
  451. return thrd->req[0].desc == NULL && thrd->req[1].desc == NULL;
  452. }
  453. static inline bool _queue_full(struct pl330_thread *thrd)
  454. {
  455. return thrd->req[0].desc != NULL && thrd->req[1].desc != NULL;
  456. }
  457. static inline bool is_manager(struct pl330_thread *thrd)
  458. {
  459. return thrd->dmac->manager == thrd;
  460. }
  461. /* If manager of the thread is in Non-Secure mode */
  462. static inline bool _manager_ns(struct pl330_thread *thrd)
  463. {
  464. return (thrd->dmac->pcfg.mode & DMAC_MODE_NS) ? true : false;
  465. }
  466. static inline u32 get_revision(u32 periph_id)
  467. {
  468. return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK;
  469. }
  470. static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[],
  471. enum pl330_dst da, u16 val)
  472. {
  473. if (dry_run)
  474. return SZ_DMAADDH;
  475. buf[0] = CMD_DMAADDH;
  476. buf[0] |= (da << 1);
  477. *((u16 *)&buf[1]) = val;
  478. PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
  479. da == 1 ? "DA" : "SA", val);
  480. return SZ_DMAADDH;
  481. }
  482. static inline u32 _emit_END(unsigned dry_run, u8 buf[])
  483. {
  484. if (dry_run)
  485. return SZ_DMAEND;
  486. buf[0] = CMD_DMAEND;
  487. PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");
  488. return SZ_DMAEND;
  489. }
  490. static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri)
  491. {
  492. if (dry_run)
  493. return SZ_DMAFLUSHP;
  494. buf[0] = CMD_DMAFLUSHP;
  495. peri &= 0x1f;
  496. peri <<= 3;
  497. buf[1] = peri;
  498. PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);
  499. return SZ_DMAFLUSHP;
  500. }
  501. static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond)
  502. {
  503. if (dry_run)
  504. return SZ_DMALD;
  505. buf[0] = CMD_DMALD;
  506. if (cond == SINGLE)
  507. buf[0] |= (0 << 1) | (1 << 0);
  508. else if (cond == BURST)
  509. buf[0] |= (1 << 1) | (1 << 0);
  510. PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
  511. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
  512. return SZ_DMALD;
  513. }
  514. static inline u32 _emit_LDP(unsigned dry_run, u8 buf[],
  515. enum pl330_cond cond, u8 peri)
  516. {
  517. if (dry_run)
  518. return SZ_DMALDP;
  519. buf[0] = CMD_DMALDP;
  520. if (cond == BURST)
  521. buf[0] |= (1 << 1);
  522. peri &= 0x1f;
  523. peri <<= 3;
  524. buf[1] = peri;
  525. PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n",
  526. cond == SINGLE ? 'S' : 'B', peri >> 3);
  527. return SZ_DMALDP;
  528. }
  529. static inline u32 _emit_LP(unsigned dry_run, u8 buf[],
  530. unsigned loop, u8 cnt)
  531. {
  532. if (dry_run)
  533. return SZ_DMALP;
  534. buf[0] = CMD_DMALP;
  535. if (loop)
  536. buf[0] |= (1 << 1);
  537. cnt--; /* DMAC increments by 1 internally */
  538. buf[1] = cnt;
  539. PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt);
  540. return SZ_DMALP;
  541. }
  542. struct _arg_LPEND {
  543. enum pl330_cond cond;
  544. bool forever;
  545. unsigned loop;
  546. u8 bjump;
  547. };
  548. static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[],
  549. const struct _arg_LPEND *arg)
  550. {
  551. enum pl330_cond cond = arg->cond;
  552. bool forever = arg->forever;
  553. unsigned loop = arg->loop;
  554. u8 bjump = arg->bjump;
  555. if (dry_run)
  556. return SZ_DMALPEND;
  557. buf[0] = CMD_DMALPEND;
  558. if (loop)
  559. buf[0] |= (1 << 2);
  560. if (!forever)
  561. buf[0] |= (1 << 4);
  562. if (cond == SINGLE)
  563. buf[0] |= (0 << 1) | (1 << 0);
  564. else if (cond == BURST)
  565. buf[0] |= (1 << 1) | (1 << 0);
  566. buf[1] = bjump;
  567. PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
  568. forever ? "FE" : "END",
  569. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
  570. loop ? '1' : '0',
  571. bjump);
  572. return SZ_DMALPEND;
  573. }
  574. static inline u32 _emit_KILL(unsigned dry_run, u8 buf[])
  575. {
  576. if (dry_run)
  577. return SZ_DMAKILL;
  578. buf[0] = CMD_DMAKILL;
  579. return SZ_DMAKILL;
  580. }
  581. static inline u32 _emit_MOV(unsigned dry_run, u8 buf[],
  582. enum dmamov_dst dst, u32 val)
  583. {
  584. if (dry_run)
  585. return SZ_DMAMOV;
  586. buf[0] = CMD_DMAMOV;
  587. buf[1] = dst;
  588. *((u32 *)&buf[2]) = val;
  589. PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n",
  590. dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val);
  591. return SZ_DMAMOV;
  592. }
  593. static inline u32 _emit_NOP(unsigned dry_run, u8 buf[])
  594. {
  595. if (dry_run)
  596. return SZ_DMANOP;
  597. buf[0] = CMD_DMANOP;
  598. PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");
  599. return SZ_DMANOP;
  600. }
  601. static inline u32 _emit_RMB(unsigned dry_run, u8 buf[])
  602. {
  603. if (dry_run)
  604. return SZ_DMARMB;
  605. buf[0] = CMD_DMARMB;
  606. PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");
  607. return SZ_DMARMB;
  608. }
  609. static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev)
  610. {
  611. if (dry_run)
  612. return SZ_DMASEV;
  613. buf[0] = CMD_DMASEV;
  614. ev &= 0x1f;
  615. ev <<= 3;
  616. buf[1] = ev;
  617. PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3);
  618. return SZ_DMASEV;
  619. }
  620. static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond)
  621. {
  622. if (dry_run)
  623. return SZ_DMAST;
  624. buf[0] = CMD_DMAST;
  625. if (cond == SINGLE)
  626. buf[0] |= (0 << 1) | (1 << 0);
  627. else if (cond == BURST)
  628. buf[0] |= (1 << 1) | (1 << 0);
  629. PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
  630. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));
  631. return SZ_DMAST;
  632. }
  633. static inline u32 _emit_STP(unsigned dry_run, u8 buf[],
  634. enum pl330_cond cond, u8 peri)
  635. {
  636. if (dry_run)
  637. return SZ_DMASTP;
  638. buf[0] = CMD_DMASTP;
  639. if (cond == BURST)
  640. buf[0] |= (1 << 1);
  641. peri &= 0x1f;
  642. peri <<= 3;
  643. buf[1] = peri;
  644. PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n",
  645. cond == SINGLE ? 'S' : 'B', peri >> 3);
  646. return SZ_DMASTP;
  647. }
  648. static inline u32 _emit_STZ(unsigned dry_run, u8 buf[])
  649. {
  650. if (dry_run)
  651. return SZ_DMASTZ;
  652. buf[0] = CMD_DMASTZ;
  653. PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");
  654. return SZ_DMASTZ;
  655. }
  656. static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev,
  657. unsigned invalidate)
  658. {
  659. if (dry_run)
  660. return SZ_DMAWFE;
  661. buf[0] = CMD_DMAWFE;
  662. ev &= 0x1f;
  663. ev <<= 3;
  664. buf[1] = ev;
  665. if (invalidate)
  666. buf[1] |= (1 << 1);
  667. PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n",
  668. ev >> 3, invalidate ? ", I" : "");
  669. return SZ_DMAWFE;
  670. }
  671. static inline u32 _emit_WFP(unsigned dry_run, u8 buf[],
  672. enum pl330_cond cond, u8 peri)
  673. {
  674. if (dry_run)
  675. return SZ_DMAWFP;
  676. buf[0] = CMD_DMAWFP;
  677. if (cond == SINGLE)
  678. buf[0] |= (0 << 1) | (0 << 0);
  679. else if (cond == BURST)
  680. buf[0] |= (1 << 1) | (0 << 0);
  681. else
  682. buf[0] |= (0 << 1) | (1 << 0);
  683. peri &= 0x1f;
  684. peri <<= 3;
  685. buf[1] = peri;
  686. PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
  687. cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3);
  688. return SZ_DMAWFP;
  689. }
  690. static inline u32 _emit_WMB(unsigned dry_run, u8 buf[])
  691. {
  692. if (dry_run)
  693. return SZ_DMAWMB;
  694. buf[0] = CMD_DMAWMB;
  695. PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");
  696. return SZ_DMAWMB;
  697. }
  698. struct _arg_GO {
  699. u8 chan;
  700. u32 addr;
  701. unsigned ns;
  702. };
  703. static inline u32 _emit_GO(unsigned dry_run, u8 buf[],
  704. const struct _arg_GO *arg)
  705. {
  706. u8 chan = arg->chan;
  707. u32 addr = arg->addr;
  708. unsigned ns = arg->ns;
  709. if (dry_run)
  710. return SZ_DMAGO;
  711. buf[0] = CMD_DMAGO;
  712. buf[0] |= (ns << 1);
  713. buf[1] = chan & 0x7;
  714. *((u32 *)&buf[2]) = addr;
  715. return SZ_DMAGO;
  716. }
  717. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  718. /* Returns Time-Out */
  719. static bool _until_dmac_idle(struct pl330_thread *thrd)
  720. {
  721. void __iomem *regs = thrd->dmac->base;
  722. unsigned long loops = msecs_to_loops(5);
  723. do {
  724. /* Until Manager is Idle */
  725. if (!(readl(regs + DBGSTATUS) & DBG_BUSY))
  726. break;
  727. cpu_relax();
  728. } while (--loops);
  729. if (!loops)
  730. return true;
  731. return false;
  732. }
  733. static inline void _execute_DBGINSN(struct pl330_thread *thrd,
  734. u8 insn[], bool as_manager)
  735. {
  736. void __iomem *regs = thrd->dmac->base;
  737. u32 val;
  738. val = (insn[0] << 16) | (insn[1] << 24);
  739. if (!as_manager) {
  740. val |= (1 << 0);
  741. val |= (thrd->id << 8); /* Channel Number */
  742. }
  743. writel(val, regs + DBGINST0);
  744. val = *((u32 *)&insn[2]);
  745. writel(val, regs + DBGINST1);
  746. /* If timed out due to halted state-machine */
  747. if (_until_dmac_idle(thrd)) {
  748. dev_err(thrd->dmac->ddma.dev, "DMAC halted!\n");
  749. return;
  750. }
  751. /* Get going */
  752. writel(0, regs + DBGCMD);
  753. }
  754. static inline u32 _state(struct pl330_thread *thrd)
  755. {
  756. void __iomem *regs = thrd->dmac->base;
  757. u32 val;
  758. if (is_manager(thrd))
  759. val = readl(regs + DS) & 0xf;
  760. else
  761. val = readl(regs + CS(thrd->id)) & 0xf;
  762. switch (val) {
  763. case DS_ST_STOP:
  764. return PL330_STATE_STOPPED;
  765. case DS_ST_EXEC:
  766. return PL330_STATE_EXECUTING;
  767. case DS_ST_CMISS:
  768. return PL330_STATE_CACHEMISS;
  769. case DS_ST_UPDTPC:
  770. return PL330_STATE_UPDTPC;
  771. case DS_ST_WFE:
  772. return PL330_STATE_WFE;
  773. case DS_ST_FAULT:
  774. return PL330_STATE_FAULTING;
  775. case DS_ST_ATBRR:
  776. if (is_manager(thrd))
  777. return PL330_STATE_INVALID;
  778. else
  779. return PL330_STATE_ATBARRIER;
  780. case DS_ST_QBUSY:
  781. if (is_manager(thrd))
  782. return PL330_STATE_INVALID;
  783. else
  784. return PL330_STATE_QUEUEBUSY;
  785. case DS_ST_WFP:
  786. if (is_manager(thrd))
  787. return PL330_STATE_INVALID;
  788. else
  789. return PL330_STATE_WFP;
  790. case DS_ST_KILL:
  791. if (is_manager(thrd))
  792. return PL330_STATE_INVALID;
  793. else
  794. return PL330_STATE_KILLING;
  795. case DS_ST_CMPLT:
  796. if (is_manager(thrd))
  797. return PL330_STATE_INVALID;
  798. else
  799. return PL330_STATE_COMPLETING;
  800. case DS_ST_FLTCMP:
  801. if (is_manager(thrd))
  802. return PL330_STATE_INVALID;
  803. else
  804. return PL330_STATE_FAULT_COMPLETING;
  805. default:
  806. return PL330_STATE_INVALID;
  807. }
  808. }
  809. static void _stop(struct pl330_thread *thrd)
  810. {
  811. void __iomem *regs = thrd->dmac->base;
  812. u8 insn[6] = {0, 0, 0, 0, 0, 0};
  813. if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
  814. UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
  815. /* Return if nothing needs to be done */
  816. if (_state(thrd) == PL330_STATE_COMPLETING
  817. || _state(thrd) == PL330_STATE_KILLING
  818. || _state(thrd) == PL330_STATE_STOPPED)
  819. return;
  820. _emit_KILL(0, insn);
  821. /* Stop generating interrupts for SEV */
  822. writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN);
  823. _execute_DBGINSN(thrd, insn, is_manager(thrd));
  824. }
  825. /* Start doing req 'idx' of thread 'thrd' */
  826. static bool _trigger(struct pl330_thread *thrd)
  827. {
  828. void __iomem *regs = thrd->dmac->base;
  829. struct _pl330_req *req;
  830. struct dma_pl330_desc *desc;
  831. struct _arg_GO go;
  832. unsigned ns;
  833. u8 insn[6] = {0, 0, 0, 0, 0, 0};
  834. int idx;
  835. /* Return if already ACTIVE */
  836. if (_state(thrd) != PL330_STATE_STOPPED)
  837. return true;
  838. idx = 1 - thrd->lstenq;
  839. if (thrd->req[idx].desc != NULL) {
  840. req = &thrd->req[idx];
  841. } else {
  842. idx = thrd->lstenq;
  843. if (thrd->req[idx].desc != NULL)
  844. req = &thrd->req[idx];
  845. else
  846. req = NULL;
  847. }
  848. /* Return if no request */
  849. if (!req)
  850. return true;
  851. desc = req->desc;
  852. ns = desc->rqcfg.nonsecure ? 1 : 0;
  853. /* See 'Abort Sources' point-4 at Page 2-25 */
  854. if (_manager_ns(thrd) && !ns)
  855. dev_info(thrd->dmac->ddma.dev, "%s:%d Recipe for ABORT!\n",
  856. __func__, __LINE__);
  857. go.chan = thrd->id;
  858. go.addr = req->mc_bus;
  859. go.ns = ns;
  860. _emit_GO(0, insn, &go);
  861. /* Set to generate interrupts for SEV */
  862. writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN);
  863. /* Only manager can execute GO */
  864. _execute_DBGINSN(thrd, insn, true);
  865. thrd->req_running = idx;
  866. return true;
  867. }
  868. static bool _start(struct pl330_thread *thrd)
  869. {
  870. switch (_state(thrd)) {
  871. case PL330_STATE_FAULT_COMPLETING:
  872. UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
  873. if (_state(thrd) == PL330_STATE_KILLING)
  874. UNTIL(thrd, PL330_STATE_STOPPED)
  875. case PL330_STATE_FAULTING:
  876. _stop(thrd);
  877. case PL330_STATE_KILLING:
  878. case PL330_STATE_COMPLETING:
  879. UNTIL(thrd, PL330_STATE_STOPPED)
  880. case PL330_STATE_STOPPED:
  881. return _trigger(thrd);
  882. case PL330_STATE_WFP:
  883. case PL330_STATE_QUEUEBUSY:
  884. case PL330_STATE_ATBARRIER:
  885. case PL330_STATE_UPDTPC:
  886. case PL330_STATE_CACHEMISS:
  887. case PL330_STATE_EXECUTING:
  888. return true;
  889. case PL330_STATE_WFE: /* For RESUME, nothing yet */
  890. default:
  891. return false;
  892. }
  893. }
  894. static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
  895. const struct _xfer_spec *pxs, int cyc)
  896. {
  897. int off = 0;
  898. struct pl330_config *pcfg = pxs->desc->rqcfg.pcfg;
  899. /* check lock-up free version */
  900. if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) {
  901. while (cyc--) {
  902. off += _emit_LD(dry_run, &buf[off], ALWAYS);
  903. off += _emit_ST(dry_run, &buf[off], ALWAYS);
  904. }
  905. } else {
  906. while (cyc--) {
  907. off += _emit_LD(dry_run, &buf[off], ALWAYS);
  908. off += _emit_RMB(dry_run, &buf[off]);
  909. off += _emit_ST(dry_run, &buf[off], ALWAYS);
  910. off += _emit_WMB(dry_run, &buf[off]);
  911. }
  912. }
  913. return off;
  914. }
  915. static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
  916. const struct _xfer_spec *pxs, int cyc)
  917. {
  918. int off = 0;
  919. while (cyc--) {
  920. off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
  921. off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
  922. off += _emit_ST(dry_run, &buf[off], ALWAYS);
  923. off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
  924. }
  925. return off;
  926. }
  927. static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
  928. const struct _xfer_spec *pxs, int cyc)
  929. {
  930. int off = 0;
  931. while (cyc--) {
  932. off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
  933. off += _emit_LD(dry_run, &buf[off], ALWAYS);
  934. off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
  935. off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
  936. }
  937. return off;
  938. }
  939. static int _bursts(unsigned dry_run, u8 buf[],
  940. const struct _xfer_spec *pxs, int cyc)
  941. {
  942. int off = 0;
  943. switch (pxs->desc->rqtype) {
  944. case DMA_MEM_TO_DEV:
  945. off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
  946. break;
  947. case DMA_DEV_TO_MEM:
  948. off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
  949. break;
  950. case DMA_MEM_TO_MEM:
  951. off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
  952. break;
  953. default:
  954. off += 0x40000000; /* Scare off the Client */
  955. break;
  956. }
  957. return off;
  958. }
  959. /* Returns bytes consumed and updates bursts */
  960. static inline int _loop(unsigned dry_run, u8 buf[],
  961. unsigned long *bursts, const struct _xfer_spec *pxs)
  962. {
  963. int cyc, cycmax, szlp, szlpend, szbrst, off;
  964. unsigned lcnt0, lcnt1, ljmp0, ljmp1;
  965. struct _arg_LPEND lpend;
  966. /* Max iterations possible in DMALP is 256 */
  967. if (*bursts >= 256*256) {
  968. lcnt1 = 256;
  969. lcnt0 = 256;
  970. cyc = *bursts / lcnt1 / lcnt0;
  971. } else if (*bursts > 256) {
  972. lcnt1 = 256;
  973. lcnt0 = *bursts / lcnt1;
  974. cyc = 1;
  975. } else {
  976. lcnt1 = *bursts;
  977. lcnt0 = 0;
  978. cyc = 1;
  979. }
  980. szlp = _emit_LP(1, buf, 0, 0);
  981. szbrst = _bursts(1, buf, pxs, 1);
  982. lpend.cond = ALWAYS;
  983. lpend.forever = false;
  984. lpend.loop = 0;
  985. lpend.bjump = 0;
  986. szlpend = _emit_LPEND(1, buf, &lpend);
  987. if (lcnt0) {
  988. szlp *= 2;
  989. szlpend *= 2;
  990. }
  991. /*
  992. * Max bursts that we can unroll due to limit on the
  993. * size of backward jump that can be encoded in DMALPEND
  994. * which is 8-bits and hence 255
  995. */
  996. cycmax = (255 - (szlp + szlpend)) / szbrst;
  997. cyc = (cycmax < cyc) ? cycmax : cyc;
  998. off = 0;
  999. if (lcnt0) {
  1000. off += _emit_LP(dry_run, &buf[off], 0, lcnt0);
  1001. ljmp0 = off;
  1002. }
  1003. off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
  1004. ljmp1 = off;
  1005. off += _bursts(dry_run, &buf[off], pxs, cyc);
  1006. lpend.cond = ALWAYS;
  1007. lpend.forever = false;
  1008. lpend.loop = 1;
  1009. lpend.bjump = off - ljmp1;
  1010. off += _emit_LPEND(dry_run, &buf[off], &lpend);
  1011. if (lcnt0) {
  1012. lpend.cond = ALWAYS;
  1013. lpend.forever = false;
  1014. lpend.loop = 0;
  1015. lpend.bjump = off - ljmp0;
  1016. off += _emit_LPEND(dry_run, &buf[off], &lpend);
  1017. }
  1018. *bursts = lcnt1 * cyc;
  1019. if (lcnt0)
  1020. *bursts *= lcnt0;
  1021. return off;
  1022. }
  1023. static inline int _setup_loops(unsigned dry_run, u8 buf[],
  1024. const struct _xfer_spec *pxs)
  1025. {
  1026. struct pl330_xfer *x = &pxs->desc->px;
  1027. u32 ccr = pxs->ccr;
  1028. unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr);
  1029. int off = 0;
  1030. while (bursts) {
  1031. c = bursts;
  1032. off += _loop(dry_run, &buf[off], &c, pxs);
  1033. bursts -= c;
  1034. }
  1035. return off;
  1036. }
  1037. static inline int _setup_xfer(unsigned dry_run, u8 buf[],
  1038. const struct _xfer_spec *pxs)
  1039. {
  1040. struct pl330_xfer *x = &pxs->desc->px;
  1041. int off = 0;
  1042. /* DMAMOV SAR, x->src_addr */
  1043. off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr);
  1044. /* DMAMOV DAR, x->dst_addr */
  1045. off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
  1046. /* Setup Loop(s) */
  1047. off += _setup_loops(dry_run, &buf[off], pxs);
  1048. return off;
  1049. }
  1050. /*
  1051. * A req is a sequence of one or more xfer units.
  1052. * Returns the number of bytes taken to setup the MC for the req.
  1053. */
  1054. static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
  1055. unsigned index, struct _xfer_spec *pxs)
  1056. {
  1057. struct _pl330_req *req = &thrd->req[index];
  1058. struct pl330_xfer *x;
  1059. u8 *buf = req->mc_cpu;
  1060. int off = 0;
  1061. PL330_DBGMC_START(req->mc_bus);
  1062. /* DMAMOV CCR, ccr */
  1063. off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
  1064. x = &pxs->desc->px;
  1065. /* Error if xfer length is not aligned at burst size */
  1066. if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
  1067. return -EINVAL;
  1068. off += _setup_xfer(dry_run, &buf[off], pxs);
  1069. /* DMASEV peripheral/event */
  1070. off += _emit_SEV(dry_run, &buf[off], thrd->ev);
  1071. /* DMAEND */
  1072. off += _emit_END(dry_run, &buf[off]);
  1073. return off;
  1074. }
  1075. static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc)
  1076. {
  1077. u32 ccr = 0;
  1078. if (rqc->src_inc)
  1079. ccr |= CC_SRCINC;
  1080. if (rqc->dst_inc)
  1081. ccr |= CC_DSTINC;
  1082. /* We set same protection levels for Src and DST for now */
  1083. if (rqc->privileged)
  1084. ccr |= CC_SRCPRI | CC_DSTPRI;
  1085. if (rqc->nonsecure)
  1086. ccr |= CC_SRCNS | CC_DSTNS;
  1087. if (rqc->insnaccess)
  1088. ccr |= CC_SRCIA | CC_DSTIA;
  1089. ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
  1090. ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);
  1091. ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT);
  1092. ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT);
  1093. ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
  1094. ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);
  1095. ccr |= (rqc->swap << CC_SWAP_SHFT);
  1096. return ccr;
  1097. }
  1098. /*
  1099. * Submit a list of xfers after which the client wants notification.
  1100. * Client is not notified after each xfer unit, just once after all
  1101. * xfer units are done or some error occurs.
  1102. */
  1103. static int pl330_submit_req(struct pl330_thread *thrd,
  1104. struct dma_pl330_desc *desc)
  1105. {
  1106. struct pl330_dmac *pl330 = thrd->dmac;
  1107. struct _xfer_spec xs;
  1108. unsigned long flags;
  1109. unsigned idx;
  1110. u32 ccr;
  1111. int ret = 0;
  1112. if (pl330->state == DYING
  1113. || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) {
  1114. dev_info(thrd->dmac->ddma.dev, "%s:%d\n",
  1115. __func__, __LINE__);
  1116. return -EAGAIN;
  1117. }
  1118. /* If request for non-existing peripheral */
  1119. if (desc->rqtype != DMA_MEM_TO_MEM &&
  1120. desc->peri >= pl330->pcfg.num_peri) {
  1121. dev_info(thrd->dmac->ddma.dev,
  1122. "%s:%d Invalid peripheral(%u)!\n",
  1123. __func__, __LINE__, desc->peri);
  1124. return -EINVAL;
  1125. }
  1126. spin_lock_irqsave(&pl330->lock, flags);
  1127. if (_queue_full(thrd)) {
  1128. ret = -EAGAIN;
  1129. goto xfer_exit;
  1130. }
  1131. /* Prefer Secure Channel */
  1132. if (!_manager_ns(thrd))
  1133. desc->rqcfg.nonsecure = 0;
  1134. else
  1135. desc->rqcfg.nonsecure = 1;
  1136. ccr = _prepare_ccr(&desc->rqcfg);
  1137. idx = thrd->req[0].desc == NULL ? 0 : 1;
  1138. xs.ccr = ccr;
  1139. xs.desc = desc;
  1140. /* First dry run to check if req is acceptable */
  1141. ret = _setup_req(1, thrd, idx, &xs);
  1142. if (ret < 0)
  1143. goto xfer_exit;
  1144. if (ret > pl330->mcbufsz / 2) {
  1145. dev_info(pl330->ddma.dev, "%s:%d Trying increasing mcbufsz\n",
  1146. __func__, __LINE__);
  1147. ret = -ENOMEM;
  1148. goto xfer_exit;
  1149. }
  1150. /* Hook the request */
  1151. thrd->lstenq = idx;
  1152. thrd->req[idx].desc = desc;
  1153. _setup_req(0, thrd, idx, &xs);
  1154. ret = 0;
  1155. xfer_exit:
  1156. spin_unlock_irqrestore(&pl330->lock, flags);
  1157. return ret;
  1158. }
  1159. static void dma_pl330_rqcb(struct dma_pl330_desc *desc, enum pl330_op_err err)
  1160. {
  1161. struct dma_pl330_chan *pch;
  1162. unsigned long flags;
  1163. if (!desc)
  1164. return;
  1165. pch = desc->pchan;
  1166. /* If desc aborted */
  1167. if (!pch)
  1168. return;
  1169. spin_lock_irqsave(&pch->lock, flags);
  1170. desc->status = DONE;
  1171. spin_unlock_irqrestore(&pch->lock, flags);
  1172. tasklet_schedule(&pch->task);
  1173. }
  1174. static void pl330_dotask(unsigned long data)
  1175. {
  1176. struct pl330_dmac *pl330 = (struct pl330_dmac *) data;
  1177. unsigned long flags;
  1178. int i;
  1179. spin_lock_irqsave(&pl330->lock, flags);
  1180. /* The DMAC itself gone nuts */
  1181. if (pl330->dmac_tbd.reset_dmac) {
  1182. pl330->state = DYING;
  1183. /* Reset the manager too */
  1184. pl330->dmac_tbd.reset_mngr = true;
  1185. /* Clear the reset flag */
  1186. pl330->dmac_tbd.reset_dmac = false;
  1187. }
  1188. if (pl330->dmac_tbd.reset_mngr) {
  1189. _stop(pl330->manager);
  1190. /* Reset all channels */
  1191. pl330->dmac_tbd.reset_chan = (1 << pl330->pcfg.num_chan) - 1;
  1192. /* Clear the reset flag */
  1193. pl330->dmac_tbd.reset_mngr = false;
  1194. }
  1195. for (i = 0; i < pl330->pcfg.num_chan; i++) {
  1196. if (pl330->dmac_tbd.reset_chan & (1 << i)) {
  1197. struct pl330_thread *thrd = &pl330->channels[i];
  1198. void __iomem *regs = pl330->base;
  1199. enum pl330_op_err err;
  1200. _stop(thrd);
  1201. if (readl(regs + FSC) & (1 << thrd->id))
  1202. err = PL330_ERR_FAIL;
  1203. else
  1204. err = PL330_ERR_ABORT;
  1205. spin_unlock_irqrestore(&pl330->lock, flags);
  1206. dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, err);
  1207. dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, err);
  1208. spin_lock_irqsave(&pl330->lock, flags);
  1209. thrd->req[0].desc = NULL;
  1210. thrd->req[1].desc = NULL;
  1211. thrd->req_running = -1;
  1212. /* Clear the reset flag */
  1213. pl330->dmac_tbd.reset_chan &= ~(1 << i);
  1214. }
  1215. }
  1216. spin_unlock_irqrestore(&pl330->lock, flags);
  1217. return;
  1218. }
  1219. /* Returns 1 if state was updated, 0 otherwise */
  1220. static int pl330_update(struct pl330_dmac *pl330)
  1221. {
  1222. struct dma_pl330_desc *descdone, *tmp;
  1223. unsigned long flags;
  1224. void __iomem *regs;
  1225. u32 val;
  1226. int id, ev, ret = 0;
  1227. regs = pl330->base;
  1228. spin_lock_irqsave(&pl330->lock, flags);
  1229. val = readl(regs + FSM) & 0x1;
  1230. if (val)
  1231. pl330->dmac_tbd.reset_mngr = true;
  1232. else
  1233. pl330->dmac_tbd.reset_mngr = false;
  1234. val = readl(regs + FSC) & ((1 << pl330->pcfg.num_chan) - 1);
  1235. pl330->dmac_tbd.reset_chan |= val;
  1236. if (val) {
  1237. int i = 0;
  1238. while (i < pl330->pcfg.num_chan) {
  1239. if (val & (1 << i)) {
  1240. dev_info(pl330->ddma.dev,
  1241. "Reset Channel-%d\t CS-%x FTC-%x\n",
  1242. i, readl(regs + CS(i)),
  1243. readl(regs + FTC(i)));
  1244. _stop(&pl330->channels[i]);
  1245. }
  1246. i++;
  1247. }
  1248. }
  1249. /* Check which event happened i.e, thread notified */
  1250. val = readl(regs + ES);
  1251. if (pl330->pcfg.num_events < 32
  1252. && val & ~((1 << pl330->pcfg.num_events) - 1)) {
  1253. pl330->dmac_tbd.reset_dmac = true;
  1254. dev_err(pl330->ddma.dev, "%s:%d Unexpected!\n", __func__,
  1255. __LINE__);
  1256. ret = 1;
  1257. goto updt_exit;
  1258. }
  1259. for (ev = 0; ev < pl330->pcfg.num_events; ev++) {
  1260. if (val & (1 << ev)) { /* Event occurred */
  1261. struct pl330_thread *thrd;
  1262. u32 inten = readl(regs + INTEN);
  1263. int active;
  1264. /* Clear the event */
  1265. if (inten & (1 << ev))
  1266. writel(1 << ev, regs + INTCLR);
  1267. ret = 1;
  1268. id = pl330->events[ev];
  1269. thrd = &pl330->channels[id];
  1270. active = thrd->req_running;
  1271. if (active == -1) /* Aborted */
  1272. continue;
  1273. /* Detach the req */
  1274. descdone = thrd->req[active].desc;
  1275. thrd->req[active].desc = NULL;
  1276. /* Get going again ASAP */
  1277. _start(thrd);
  1278. /* For now, just make a list of callbacks to be done */
  1279. list_add_tail(&descdone->rqd, &pl330->req_done);
  1280. }
  1281. }
  1282. /* Now that we are in no hurry, do the callbacks */
  1283. list_for_each_entry_safe(descdone, tmp, &pl330->req_done, rqd) {
  1284. list_del(&descdone->rqd);
  1285. spin_unlock_irqrestore(&pl330->lock, flags);
  1286. dma_pl330_rqcb(descdone, PL330_ERR_NONE);
  1287. spin_lock_irqsave(&pl330->lock, flags);
  1288. }
  1289. updt_exit:
  1290. spin_unlock_irqrestore(&pl330->lock, flags);
  1291. if (pl330->dmac_tbd.reset_dmac
  1292. || pl330->dmac_tbd.reset_mngr
  1293. || pl330->dmac_tbd.reset_chan) {
  1294. ret = 1;
  1295. tasklet_schedule(&pl330->tasks);
  1296. }
  1297. return ret;
  1298. }
  1299. /* Reserve an event */
  1300. static inline int _alloc_event(struct pl330_thread *thrd)
  1301. {
  1302. struct pl330_dmac *pl330 = thrd->dmac;
  1303. int ev;
  1304. for (ev = 0; ev < pl330->pcfg.num_events; ev++)
  1305. if (pl330->events[ev] == -1) {
  1306. pl330->events[ev] = thrd->id;
  1307. return ev;
  1308. }
  1309. return -1;
  1310. }
  1311. static bool _chan_ns(const struct pl330_dmac *pl330, int i)
  1312. {
  1313. return pl330->pcfg.irq_ns & (1 << i);
  1314. }
  1315. /* Upon success, returns IdentityToken for the
  1316. * allocated channel, NULL otherwise.
  1317. */
  1318. static struct pl330_thread *pl330_request_channel(struct pl330_dmac *pl330)
  1319. {
  1320. struct pl330_thread *thrd = NULL;
  1321. unsigned long flags;
  1322. int chans, i;
  1323. if (pl330->state == DYING)
  1324. return NULL;
  1325. chans = pl330->pcfg.num_chan;
  1326. spin_lock_irqsave(&pl330->lock, flags);
  1327. for (i = 0; i < chans; i++) {
  1328. thrd = &pl330->channels[i];
  1329. if ((thrd->free) && (!_manager_ns(thrd) ||
  1330. _chan_ns(pl330, i))) {
  1331. thrd->ev = _alloc_event(thrd);
  1332. if (thrd->ev >= 0) {
  1333. thrd->free = false;
  1334. thrd->lstenq = 1;
  1335. thrd->req[0].desc = NULL;
  1336. thrd->req[1].desc = NULL;
  1337. thrd->req_running = -1;
  1338. break;
  1339. }
  1340. }
  1341. thrd = NULL;
  1342. }
  1343. spin_unlock_irqrestore(&pl330->lock, flags);
  1344. return thrd;
  1345. }
  1346. /* Release an event */
  1347. static inline void _free_event(struct pl330_thread *thrd, int ev)
  1348. {
  1349. struct pl330_dmac *pl330 = thrd->dmac;
  1350. /* If the event is valid and was held by the thread */
  1351. if (ev >= 0 && ev < pl330->pcfg.num_events
  1352. && pl330->events[ev] == thrd->id)
  1353. pl330->events[ev] = -1;
  1354. }
  1355. static void pl330_release_channel(struct pl330_thread *thrd)
  1356. {
  1357. struct pl330_dmac *pl330;
  1358. unsigned long flags;
  1359. if (!thrd || thrd->free)
  1360. return;
  1361. _stop(thrd);
  1362. dma_pl330_rqcb(thrd->req[1 - thrd->lstenq].desc, PL330_ERR_ABORT);
  1363. dma_pl330_rqcb(thrd->req[thrd->lstenq].desc, PL330_ERR_ABORT);
  1364. pl330 = thrd->dmac;
  1365. spin_lock_irqsave(&pl330->lock, flags);
  1366. _free_event(thrd, thrd->ev);
  1367. thrd->free = true;
  1368. spin_unlock_irqrestore(&pl330->lock, flags);
  1369. }
  1370. /* Initialize the structure for PL330 configuration, that can be used
  1371. * by the client driver the make best use of the DMAC
  1372. */
  1373. static void read_dmac_config(struct pl330_dmac *pl330)
  1374. {
  1375. void __iomem *regs = pl330->base;
  1376. u32 val;
  1377. val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT;
  1378. val &= CRD_DATA_WIDTH_MASK;
  1379. pl330->pcfg.data_bus_width = 8 * (1 << val);
  1380. val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT;
  1381. val &= CRD_DATA_BUFF_MASK;
  1382. pl330->pcfg.data_buf_dep = val + 1;
  1383. val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT;
  1384. val &= CR0_NUM_CHANS_MASK;
  1385. val += 1;
  1386. pl330->pcfg.num_chan = val;
  1387. val = readl(regs + CR0);
  1388. if (val & CR0_PERIPH_REQ_SET) {
  1389. val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK;
  1390. val += 1;
  1391. pl330->pcfg.num_peri = val;
  1392. pl330->pcfg.peri_ns = readl(regs + CR4);
  1393. } else {
  1394. pl330->pcfg.num_peri = 0;
  1395. }
  1396. val = readl(regs + CR0);
  1397. if (val & CR0_BOOT_MAN_NS)
  1398. pl330->pcfg.mode |= DMAC_MODE_NS;
  1399. else
  1400. pl330->pcfg.mode &= ~DMAC_MODE_NS;
  1401. val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT;
  1402. val &= CR0_NUM_EVENTS_MASK;
  1403. val += 1;
  1404. pl330->pcfg.num_events = val;
  1405. pl330->pcfg.irq_ns = readl(regs + CR3);
  1406. }
  1407. static inline void _reset_thread(struct pl330_thread *thrd)
  1408. {
  1409. struct pl330_dmac *pl330 = thrd->dmac;
  1410. thrd->req[0].mc_cpu = pl330->mcode_cpu
  1411. + (thrd->id * pl330->mcbufsz);
  1412. thrd->req[0].mc_bus = pl330->mcode_bus
  1413. + (thrd->id * pl330->mcbufsz);
  1414. thrd->req[0].desc = NULL;
  1415. thrd->req[1].mc_cpu = thrd->req[0].mc_cpu
  1416. + pl330->mcbufsz / 2;
  1417. thrd->req[1].mc_bus = thrd->req[0].mc_bus
  1418. + pl330->mcbufsz / 2;
  1419. thrd->req[1].desc = NULL;
  1420. thrd->req_running = -1;
  1421. }
  1422. static int dmac_alloc_threads(struct pl330_dmac *pl330)
  1423. {
  1424. int chans = pl330->pcfg.num_chan;
  1425. struct pl330_thread *thrd;
  1426. int i;
  1427. /* Allocate 1 Manager and 'chans' Channel threads */
  1428. pl330->channels = kzalloc((1 + chans) * sizeof(*thrd),
  1429. GFP_KERNEL);
  1430. if (!pl330->channels)
  1431. return -ENOMEM;
  1432. /* Init Channel threads */
  1433. for (i = 0; i < chans; i++) {
  1434. thrd = &pl330->channels[i];
  1435. thrd->id = i;
  1436. thrd->dmac = pl330;
  1437. _reset_thread(thrd);
  1438. thrd->free = true;
  1439. }
  1440. /* MANAGER is indexed at the end */
  1441. thrd = &pl330->channels[chans];
  1442. thrd->id = chans;
  1443. thrd->dmac = pl330;
  1444. thrd->free = false;
  1445. pl330->manager = thrd;
  1446. return 0;
  1447. }
  1448. static int dmac_alloc_resources(struct pl330_dmac *pl330)
  1449. {
  1450. int chans = pl330->pcfg.num_chan;
  1451. int ret;
  1452. /*
  1453. * Alloc MicroCode buffer for 'chans' Channel threads.
  1454. * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
  1455. */
  1456. pl330->mcode_cpu = dma_alloc_coherent(pl330->ddma.dev,
  1457. chans * pl330->mcbufsz,
  1458. &pl330->mcode_bus, GFP_KERNEL);
  1459. if (!pl330->mcode_cpu) {
  1460. dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n",
  1461. __func__, __LINE__);
  1462. return -ENOMEM;
  1463. }
  1464. ret = dmac_alloc_threads(pl330);
  1465. if (ret) {
  1466. dev_err(pl330->ddma.dev, "%s:%d Can't to create channels for DMAC!\n",
  1467. __func__, __LINE__);
  1468. dma_free_coherent(pl330->ddma.dev,
  1469. chans * pl330->mcbufsz,
  1470. pl330->mcode_cpu, pl330->mcode_bus);
  1471. return ret;
  1472. }
  1473. return 0;
  1474. }
  1475. static int pl330_add(struct pl330_dmac *pl330)
  1476. {
  1477. void __iomem *regs;
  1478. int i, ret;
  1479. regs = pl330->base;
  1480. /* Check if we can handle this DMAC */
  1481. if ((pl330->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) {
  1482. dev_err(pl330->ddma.dev, "PERIPH_ID 0x%x !\n",
  1483. pl330->pcfg.periph_id);
  1484. return -EINVAL;
  1485. }
  1486. /* Read the configuration of the DMAC */
  1487. read_dmac_config(pl330);
  1488. if (pl330->pcfg.num_events == 0) {
  1489. dev_err(pl330->ddma.dev, "%s:%d Can't work without events!\n",
  1490. __func__, __LINE__);
  1491. return -EINVAL;
  1492. }
  1493. spin_lock_init(&pl330->lock);
  1494. INIT_LIST_HEAD(&pl330->req_done);
  1495. /* Use default MC buffer size if not provided */
  1496. if (!pl330->mcbufsz)
  1497. pl330->mcbufsz = MCODE_BUFF_PER_REQ * 2;
  1498. /* Mark all events as free */
  1499. for (i = 0; i < pl330->pcfg.num_events; i++)
  1500. pl330->events[i] = -1;
  1501. /* Allocate resources needed by the DMAC */
  1502. ret = dmac_alloc_resources(pl330);
  1503. if (ret) {
  1504. dev_err(pl330->ddma.dev, "Unable to create channels for DMAC\n");
  1505. return ret;
  1506. }
  1507. tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330);
  1508. pl330->state = INIT;
  1509. return 0;
  1510. }
  1511. static int dmac_free_threads(struct pl330_dmac *pl330)
  1512. {
  1513. struct pl330_thread *thrd;
  1514. int i;
  1515. /* Release Channel threads */
  1516. for (i = 0; i < pl330->pcfg.num_chan; i++) {
  1517. thrd = &pl330->channels[i];
  1518. pl330_release_channel(thrd);
  1519. }
  1520. /* Free memory */
  1521. kfree(pl330->channels);
  1522. return 0;
  1523. }
  1524. static void pl330_del(struct pl330_dmac *pl330)
  1525. {
  1526. pl330->state = UNINIT;
  1527. tasklet_kill(&pl330->tasks);
  1528. /* Free DMAC resources */
  1529. dmac_free_threads(pl330);
  1530. dma_free_coherent(pl330->ddma.dev,
  1531. pl330->pcfg.num_chan * pl330->mcbufsz, pl330->mcode_cpu,
  1532. pl330->mcode_bus);
  1533. }
  1534. /* forward declaration */
  1535. static struct amba_driver pl330_driver;
  1536. static inline struct dma_pl330_chan *
  1537. to_pchan(struct dma_chan *ch)
  1538. {
  1539. if (!ch)
  1540. return NULL;
  1541. return container_of(ch, struct dma_pl330_chan, chan);
  1542. }
  1543. static inline struct dma_pl330_desc *
  1544. to_desc(struct dma_async_tx_descriptor *tx)
  1545. {
  1546. return container_of(tx, struct dma_pl330_desc, txd);
  1547. }
  1548. static inline void fill_queue(struct dma_pl330_chan *pch)
  1549. {
  1550. struct dma_pl330_desc *desc;
  1551. int ret;
  1552. list_for_each_entry(desc, &pch->work_list, node) {
  1553. /* If already submitted */
  1554. if (desc->status == BUSY)
  1555. continue;
  1556. ret = pl330_submit_req(pch->thread, desc);
  1557. if (!ret) {
  1558. desc->status = BUSY;
  1559. } else if (ret == -EAGAIN) {
  1560. /* QFull or DMAC Dying */
  1561. break;
  1562. } else {
  1563. /* Unacceptable request */
  1564. desc->status = DONE;
  1565. dev_err(pch->dmac->ddma.dev, "%s:%d Bad Desc(%d)\n",
  1566. __func__, __LINE__, desc->txd.cookie);
  1567. tasklet_schedule(&pch->task);
  1568. }
  1569. }
  1570. }
  1571. static void pl330_tasklet(unsigned long data)
  1572. {
  1573. struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
  1574. struct dma_pl330_desc *desc, *_dt;
  1575. unsigned long flags;
  1576. bool power_down = false;
  1577. spin_lock_irqsave(&pch->lock, flags);
  1578. /* Pick up ripe tomatoes */
  1579. list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
  1580. if (desc->status == DONE) {
  1581. if (!pch->cyclic)
  1582. dma_cookie_complete(&desc->txd);
  1583. list_move_tail(&desc->node, &pch->completed_list);
  1584. }
  1585. /* Try to submit a req imm. next to the last completed cookie */
  1586. fill_queue(pch);
  1587. if (list_empty(&pch->work_list)) {
  1588. spin_lock(&pch->thread->dmac->lock);
  1589. _stop(pch->thread);
  1590. spin_unlock(&pch->thread->dmac->lock);
  1591. power_down = true;
  1592. } else {
  1593. /* Make sure the PL330 Channel thread is active */
  1594. spin_lock(&pch->thread->dmac->lock);
  1595. _start(pch->thread);
  1596. spin_unlock(&pch->thread->dmac->lock);
  1597. }
  1598. while (!list_empty(&pch->completed_list)) {
  1599. dma_async_tx_callback callback;
  1600. void *callback_param;
  1601. desc = list_first_entry(&pch->completed_list,
  1602. struct dma_pl330_desc, node);
  1603. callback = desc->txd.callback;
  1604. callback_param = desc->txd.callback_param;
  1605. if (pch->cyclic) {
  1606. desc->status = PREP;
  1607. list_move_tail(&desc->node, &pch->work_list);
  1608. if (power_down) {
  1609. spin_lock(&pch->thread->dmac->lock);
  1610. _start(pch->thread);
  1611. spin_unlock(&pch->thread->dmac->lock);
  1612. power_down = false;
  1613. }
  1614. } else {
  1615. desc->status = FREE;
  1616. list_move_tail(&desc->node, &pch->dmac->desc_pool);
  1617. }
  1618. dma_descriptor_unmap(&desc->txd);
  1619. if (callback) {
  1620. spin_unlock_irqrestore(&pch->lock, flags);
  1621. callback(callback_param);
  1622. spin_lock_irqsave(&pch->lock, flags);
  1623. }
  1624. }
  1625. spin_unlock_irqrestore(&pch->lock, flags);
  1626. /* If work list empty, power down */
  1627. if (power_down) {
  1628. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1629. pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
  1630. }
  1631. }
  1632. bool pl330_filter(struct dma_chan *chan, void *param)
  1633. {
  1634. u8 *peri_id;
  1635. if (chan->device->dev->driver != &pl330_driver.drv)
  1636. return false;
  1637. peri_id = chan->private;
  1638. return *peri_id == (unsigned long)param;
  1639. }
  1640. EXPORT_SYMBOL(pl330_filter);
  1641. static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec,
  1642. struct of_dma *ofdma)
  1643. {
  1644. int count = dma_spec->args_count;
  1645. struct pl330_dmac *pl330 = ofdma->of_dma_data;
  1646. unsigned int chan_id;
  1647. if (!pl330)
  1648. return NULL;
  1649. if (count != 1)
  1650. return NULL;
  1651. chan_id = dma_spec->args[0];
  1652. if (chan_id >= pl330->num_peripherals)
  1653. return NULL;
  1654. return dma_get_slave_channel(&pl330->peripherals[chan_id].chan);
  1655. }
  1656. static int pl330_alloc_chan_resources(struct dma_chan *chan)
  1657. {
  1658. struct dma_pl330_chan *pch = to_pchan(chan);
  1659. struct pl330_dmac *pl330 = pch->dmac;
  1660. unsigned long flags;
  1661. spin_lock_irqsave(&pch->lock, flags);
  1662. dma_cookie_init(chan);
  1663. pch->cyclic = false;
  1664. pch->thread = pl330_request_channel(pl330);
  1665. if (!pch->thread) {
  1666. spin_unlock_irqrestore(&pch->lock, flags);
  1667. return -ENOMEM;
  1668. }
  1669. tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
  1670. spin_unlock_irqrestore(&pch->lock, flags);
  1671. return 1;
  1672. }
  1673. static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
  1674. {
  1675. struct dma_pl330_chan *pch = to_pchan(chan);
  1676. struct dma_pl330_desc *desc;
  1677. unsigned long flags;
  1678. struct pl330_dmac *pl330 = pch->dmac;
  1679. struct dma_slave_config *slave_config;
  1680. LIST_HEAD(list);
  1681. switch (cmd) {
  1682. case DMA_TERMINATE_ALL:
  1683. pm_runtime_get_sync(pl330->ddma.dev);
  1684. spin_lock_irqsave(&pch->lock, flags);
  1685. spin_lock(&pl330->lock);
  1686. _stop(pch->thread);
  1687. spin_unlock(&pl330->lock);
  1688. pch->thread->req[0].desc = NULL;
  1689. pch->thread->req[1].desc = NULL;
  1690. pch->thread->req_running = -1;
  1691. /* Mark all desc done */
  1692. list_for_each_entry(desc, &pch->submitted_list, node) {
  1693. desc->status = FREE;
  1694. dma_cookie_complete(&desc->txd);
  1695. }
  1696. list_for_each_entry(desc, &pch->work_list , node) {
  1697. desc->status = FREE;
  1698. dma_cookie_complete(&desc->txd);
  1699. }
  1700. list_for_each_entry(desc, &pch->completed_list , node) {
  1701. desc->status = FREE;
  1702. dma_cookie_complete(&desc->txd);
  1703. }
  1704. if (!list_empty(&pch->work_list))
  1705. pm_runtime_put(pl330->ddma.dev);
  1706. list_splice_tail_init(&pch->submitted_list, &pl330->desc_pool);
  1707. list_splice_tail_init(&pch->work_list, &pl330->desc_pool);
  1708. list_splice_tail_init(&pch->completed_list, &pl330->desc_pool);
  1709. spin_unlock_irqrestore(&pch->lock, flags);
  1710. pm_runtime_mark_last_busy(pl330->ddma.dev);
  1711. pm_runtime_put_autosuspend(pl330->ddma.dev);
  1712. break;
  1713. case DMA_SLAVE_CONFIG:
  1714. slave_config = (struct dma_slave_config *)arg;
  1715. if (slave_config->direction == DMA_MEM_TO_DEV) {
  1716. if (slave_config->dst_addr)
  1717. pch->fifo_addr = slave_config->dst_addr;
  1718. if (slave_config->dst_addr_width)
  1719. pch->burst_sz = __ffs(slave_config->dst_addr_width);
  1720. if (slave_config->dst_maxburst)
  1721. pch->burst_len = slave_config->dst_maxburst;
  1722. } else if (slave_config->direction == DMA_DEV_TO_MEM) {
  1723. if (slave_config->src_addr)
  1724. pch->fifo_addr = slave_config->src_addr;
  1725. if (slave_config->src_addr_width)
  1726. pch->burst_sz = __ffs(slave_config->src_addr_width);
  1727. if (slave_config->src_maxburst)
  1728. pch->burst_len = slave_config->src_maxburst;
  1729. }
  1730. break;
  1731. default:
  1732. dev_err(pch->dmac->ddma.dev, "Not supported command.\n");
  1733. return -ENXIO;
  1734. }
  1735. return 0;
  1736. }
  1737. static void pl330_free_chan_resources(struct dma_chan *chan)
  1738. {
  1739. struct dma_pl330_chan *pch = to_pchan(chan);
  1740. unsigned long flags;
  1741. tasklet_kill(&pch->task);
  1742. pm_runtime_get_sync(pch->dmac->ddma.dev);
  1743. spin_lock_irqsave(&pch->lock, flags);
  1744. pl330_release_channel(pch->thread);
  1745. pch->thread = NULL;
  1746. if (pch->cyclic)
  1747. list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
  1748. spin_unlock_irqrestore(&pch->lock, flags);
  1749. pm_runtime_mark_last_busy(pch->dmac->ddma.dev);
  1750. pm_runtime_put_autosuspend(pch->dmac->ddma.dev);
  1751. }
  1752. static enum dma_status
  1753. pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  1754. struct dma_tx_state *txstate)
  1755. {
  1756. return dma_cookie_status(chan, cookie, txstate);
  1757. }
  1758. static void pl330_issue_pending(struct dma_chan *chan)
  1759. {
  1760. struct dma_pl330_chan *pch = to_pchan(chan);
  1761. unsigned long flags;
  1762. spin_lock_irqsave(&pch->lock, flags);
  1763. if (list_empty(&pch->work_list)) {
  1764. /*
  1765. * Warn on nothing pending. Empty submitted_list may
  1766. * break our pm_runtime usage counter as it is
  1767. * updated on work_list emptiness status.
  1768. */
  1769. WARN_ON(list_empty(&pch->submitted_list));
  1770. pm_runtime_get_sync(pch->dmac->ddma.dev);
  1771. }
  1772. list_splice_tail_init(&pch->submitted_list, &pch->work_list);
  1773. spin_unlock_irqrestore(&pch->lock, flags);
  1774. pl330_tasklet((unsigned long)pch);
  1775. }
  1776. /*
  1777. * We returned the last one of the circular list of descriptor(s)
  1778. * from prep_xxx, so the argument to submit corresponds to the last
  1779. * descriptor of the list.
  1780. */
  1781. static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
  1782. {
  1783. struct dma_pl330_desc *desc, *last = to_desc(tx);
  1784. struct dma_pl330_chan *pch = to_pchan(tx->chan);
  1785. dma_cookie_t cookie;
  1786. unsigned long flags;
  1787. spin_lock_irqsave(&pch->lock, flags);
  1788. /* Assign cookies to all nodes */
  1789. while (!list_empty(&last->node)) {
  1790. desc = list_entry(last->node.next, struct dma_pl330_desc, node);
  1791. if (pch->cyclic) {
  1792. desc->txd.callback = last->txd.callback;
  1793. desc->txd.callback_param = last->txd.callback_param;
  1794. }
  1795. dma_cookie_assign(&desc->txd);
  1796. list_move_tail(&desc->node, &pch->submitted_list);
  1797. }
  1798. cookie = dma_cookie_assign(&last->txd);
  1799. list_add_tail(&last->node, &pch->submitted_list);
  1800. spin_unlock_irqrestore(&pch->lock, flags);
  1801. return cookie;
  1802. }
  1803. static inline void _init_desc(struct dma_pl330_desc *desc)
  1804. {
  1805. desc->rqcfg.swap = SWAP_NO;
  1806. desc->rqcfg.scctl = CCTRL0;
  1807. desc->rqcfg.dcctl = CCTRL0;
  1808. desc->txd.tx_submit = pl330_tx_submit;
  1809. INIT_LIST_HEAD(&desc->node);
  1810. }
  1811. /* Returns the number of descriptors added to the DMAC pool */
  1812. static int add_desc(struct pl330_dmac *pl330, gfp_t flg, int count)
  1813. {
  1814. struct dma_pl330_desc *desc;
  1815. unsigned long flags;
  1816. int i;
  1817. desc = kcalloc(count, sizeof(*desc), flg);
  1818. if (!desc)
  1819. return 0;
  1820. spin_lock_irqsave(&pl330->pool_lock, flags);
  1821. for (i = 0; i < count; i++) {
  1822. _init_desc(&desc[i]);
  1823. list_add_tail(&desc[i].node, &pl330->desc_pool);
  1824. }
  1825. spin_unlock_irqrestore(&pl330->pool_lock, flags);
  1826. return count;
  1827. }
  1828. static struct dma_pl330_desc *pluck_desc(struct pl330_dmac *pl330)
  1829. {
  1830. struct dma_pl330_desc *desc = NULL;
  1831. unsigned long flags;
  1832. spin_lock_irqsave(&pl330->pool_lock, flags);
  1833. if (!list_empty(&pl330->desc_pool)) {
  1834. desc = list_entry(pl330->desc_pool.next,
  1835. struct dma_pl330_desc, node);
  1836. list_del_init(&desc->node);
  1837. desc->status = PREP;
  1838. desc->txd.callback = NULL;
  1839. }
  1840. spin_unlock_irqrestore(&pl330->pool_lock, flags);
  1841. return desc;
  1842. }
  1843. static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
  1844. {
  1845. struct pl330_dmac *pl330 = pch->dmac;
  1846. u8 *peri_id = pch->chan.private;
  1847. struct dma_pl330_desc *desc;
  1848. /* Pluck one desc from the pool of DMAC */
  1849. desc = pluck_desc(pl330);
  1850. /* If the DMAC pool is empty, alloc new */
  1851. if (!desc) {
  1852. if (!add_desc(pl330, GFP_ATOMIC, 1))
  1853. return NULL;
  1854. /* Try again */
  1855. desc = pluck_desc(pl330);
  1856. if (!desc) {
  1857. dev_err(pch->dmac->ddma.dev,
  1858. "%s:%d ALERT!\n", __func__, __LINE__);
  1859. return NULL;
  1860. }
  1861. }
  1862. /* Initialize the descriptor */
  1863. desc->pchan = pch;
  1864. desc->txd.cookie = 0;
  1865. async_tx_ack(&desc->txd);
  1866. desc->peri = peri_id ? pch->chan.chan_id : 0;
  1867. desc->rqcfg.pcfg = &pch->dmac->pcfg;
  1868. dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
  1869. return desc;
  1870. }
  1871. static inline void fill_px(struct pl330_xfer *px,
  1872. dma_addr_t dst, dma_addr_t src, size_t len)
  1873. {
  1874. px->bytes = len;
  1875. px->dst_addr = dst;
  1876. px->src_addr = src;
  1877. }
  1878. static struct dma_pl330_desc *
  1879. __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
  1880. dma_addr_t src, size_t len)
  1881. {
  1882. struct dma_pl330_desc *desc = pl330_get_desc(pch);
  1883. if (!desc) {
  1884. dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
  1885. __func__, __LINE__);
  1886. return NULL;
  1887. }
  1888. /*
  1889. * Ideally we should lookout for reqs bigger than
  1890. * those that can be programmed with 256 bytes of
  1891. * MC buffer, but considering a req size is seldom
  1892. * going to be word-unaligned and more than 200MB,
  1893. * we take it easy.
  1894. * Also, should the limit is reached we'd rather
  1895. * have the platform increase MC buffer size than
  1896. * complicating this API driver.
  1897. */
  1898. fill_px(&desc->px, dst, src, len);
  1899. return desc;
  1900. }
  1901. /* Call after fixing burst size */
  1902. static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
  1903. {
  1904. struct dma_pl330_chan *pch = desc->pchan;
  1905. struct pl330_dmac *pl330 = pch->dmac;
  1906. int burst_len;
  1907. burst_len = pl330->pcfg.data_bus_width / 8;
  1908. burst_len *= pl330->pcfg.data_buf_dep / pl330->pcfg.num_chan;
  1909. burst_len >>= desc->rqcfg.brst_size;
  1910. /* src/dst_burst_len can't be more than 16 */
  1911. if (burst_len > 16)
  1912. burst_len = 16;
  1913. while (burst_len > 1) {
  1914. if (!(len % (burst_len << desc->rqcfg.brst_size)))
  1915. break;
  1916. burst_len--;
  1917. }
  1918. return burst_len;
  1919. }
  1920. static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
  1921. struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
  1922. size_t period_len, enum dma_transfer_direction direction,
  1923. unsigned long flags)
  1924. {
  1925. struct dma_pl330_desc *desc = NULL, *first = NULL;
  1926. struct dma_pl330_chan *pch = to_pchan(chan);
  1927. struct pl330_dmac *pl330 = pch->dmac;
  1928. unsigned int i;
  1929. dma_addr_t dst;
  1930. dma_addr_t src;
  1931. if (len % period_len != 0)
  1932. return NULL;
  1933. if (!is_slave_direction(direction)) {
  1934. dev_err(pch->dmac->ddma.dev, "%s:%d Invalid dma direction\n",
  1935. __func__, __LINE__);
  1936. return NULL;
  1937. }
  1938. for (i = 0; i < len / period_len; i++) {
  1939. desc = pl330_get_desc(pch);
  1940. if (!desc) {
  1941. dev_err(pch->dmac->ddma.dev, "%s:%d Unable to fetch desc\n",
  1942. __func__, __LINE__);
  1943. if (!first)
  1944. return NULL;
  1945. spin_lock_irqsave(&pl330->pool_lock, flags);
  1946. while (!list_empty(&first->node)) {
  1947. desc = list_entry(first->node.next,
  1948. struct dma_pl330_desc, node);
  1949. list_move_tail(&desc->node, &pl330->desc_pool);
  1950. }
  1951. list_move_tail(&first->node, &pl330->desc_pool);
  1952. spin_unlock_irqrestore(&pl330->pool_lock, flags);
  1953. return NULL;
  1954. }
  1955. switch (direction) {
  1956. case DMA_MEM_TO_DEV:
  1957. desc->rqcfg.src_inc = 1;
  1958. desc->rqcfg.dst_inc = 0;
  1959. src = dma_addr;
  1960. dst = pch->fifo_addr;
  1961. break;
  1962. case DMA_DEV_TO_MEM:
  1963. desc->rqcfg.src_inc = 0;
  1964. desc->rqcfg.dst_inc = 1;
  1965. src = pch->fifo_addr;
  1966. dst = dma_addr;
  1967. break;
  1968. default:
  1969. break;
  1970. }
  1971. desc->rqtype = direction;
  1972. desc->rqcfg.brst_size = pch->burst_sz;
  1973. desc->rqcfg.brst_len = 1;
  1974. fill_px(&desc->px, dst, src, period_len);
  1975. if (!first)
  1976. first = desc;
  1977. else
  1978. list_add_tail(&desc->node, &first->node);
  1979. dma_addr += period_len;
  1980. }
  1981. if (!desc)
  1982. return NULL;
  1983. pch->cyclic = true;
  1984. desc->txd.flags = flags;
  1985. return &desc->txd;
  1986. }
  1987. static struct dma_async_tx_descriptor *
  1988. pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
  1989. dma_addr_t src, size_t len, unsigned long flags)
  1990. {
  1991. struct dma_pl330_desc *desc;
  1992. struct dma_pl330_chan *pch = to_pchan(chan);
  1993. struct pl330_dmac *pl330 = pch->dmac;
  1994. int burst;
  1995. if (unlikely(!pch || !len))
  1996. return NULL;
  1997. desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
  1998. if (!desc)
  1999. return NULL;
  2000. desc->rqcfg.src_inc = 1;
  2001. desc->rqcfg.dst_inc = 1;
  2002. desc->rqtype = DMA_MEM_TO_MEM;
  2003. /* Select max possible burst size */
  2004. burst = pl330->pcfg.data_bus_width / 8;
  2005. /*
  2006. * Make sure we use a burst size that aligns with all the memcpy
  2007. * parameters because our DMA programming algorithm doesn't cope with
  2008. * transfers which straddle an entry in the DMA device's MFIFO.
  2009. */
  2010. while ((src | dst | len) & (burst - 1))
  2011. burst /= 2;
  2012. desc->rqcfg.brst_size = 0;
  2013. while (burst != (1 << desc->rqcfg.brst_size))
  2014. desc->rqcfg.brst_size++;
  2015. /*
  2016. * If burst size is smaller than bus width then make sure we only
  2017. * transfer one at a time to avoid a burst stradling an MFIFO entry.
  2018. */
  2019. if (desc->rqcfg.brst_size * 8 < pl330->pcfg.data_bus_width)
  2020. desc->rqcfg.brst_len = 1;
  2021. desc->rqcfg.brst_len = get_burst_len(desc, len);
  2022. desc->txd.flags = flags;
  2023. return &desc->txd;
  2024. }
  2025. static void __pl330_giveback_desc(struct pl330_dmac *pl330,
  2026. struct dma_pl330_desc *first)
  2027. {
  2028. unsigned long flags;
  2029. struct dma_pl330_desc *desc;
  2030. if (!first)
  2031. return;
  2032. spin_lock_irqsave(&pl330->pool_lock, flags);
  2033. while (!list_empty(&first->node)) {
  2034. desc = list_entry(first->node.next,
  2035. struct dma_pl330_desc, node);
  2036. list_move_tail(&desc->node, &pl330->desc_pool);
  2037. }
  2038. list_move_tail(&first->node, &pl330->desc_pool);
  2039. spin_unlock_irqrestore(&pl330->pool_lock, flags);
  2040. }
  2041. static struct dma_async_tx_descriptor *
  2042. pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  2043. unsigned int sg_len, enum dma_transfer_direction direction,
  2044. unsigned long flg, void *context)
  2045. {
  2046. struct dma_pl330_desc *first, *desc = NULL;
  2047. struct dma_pl330_chan *pch = to_pchan(chan);
  2048. struct scatterlist *sg;
  2049. int i;
  2050. dma_addr_t addr;
  2051. if (unlikely(!pch || !sgl || !sg_len))
  2052. return NULL;
  2053. addr = pch->fifo_addr;
  2054. first = NULL;
  2055. for_each_sg(sgl, sg, sg_len, i) {
  2056. desc = pl330_get_desc(pch);
  2057. if (!desc) {
  2058. struct pl330_dmac *pl330 = pch->dmac;
  2059. dev_err(pch->dmac->ddma.dev,
  2060. "%s:%d Unable to fetch desc\n",
  2061. __func__, __LINE__);
  2062. __pl330_giveback_desc(pl330, first);
  2063. return NULL;
  2064. }
  2065. if (!first)
  2066. first = desc;
  2067. else
  2068. list_add_tail(&desc->node, &first->node);
  2069. if (direction == DMA_MEM_TO_DEV) {
  2070. desc->rqcfg.src_inc = 1;
  2071. desc->rqcfg.dst_inc = 0;
  2072. fill_px(&desc->px,
  2073. addr, sg_dma_address(sg), sg_dma_len(sg));
  2074. } else {
  2075. desc->rqcfg.src_inc = 0;
  2076. desc->rqcfg.dst_inc = 1;
  2077. fill_px(&desc->px,
  2078. sg_dma_address(sg), addr, sg_dma_len(sg));
  2079. }
  2080. desc->rqcfg.brst_size = pch->burst_sz;
  2081. desc->rqcfg.brst_len = 1;
  2082. desc->rqtype = direction;
  2083. }
  2084. /* Return the last desc in the chain */
  2085. desc->txd.flags = flg;
  2086. return &desc->txd;
  2087. }
  2088. static irqreturn_t pl330_irq_handler(int irq, void *data)
  2089. {
  2090. if (pl330_update(data))
  2091. return IRQ_HANDLED;
  2092. else
  2093. return IRQ_NONE;
  2094. }
  2095. #define PL330_DMA_BUSWIDTHS \
  2096. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  2097. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  2098. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  2099. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  2100. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)
  2101. static int pl330_dma_device_slave_caps(struct dma_chan *dchan,
  2102. struct dma_slave_caps *caps)
  2103. {
  2104. caps->src_addr_widths = PL330_DMA_BUSWIDTHS;
  2105. caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS;
  2106. caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  2107. caps->cmd_pause = false;
  2108. caps->cmd_terminate = true;
  2109. caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  2110. return 0;
  2111. }
  2112. /*
  2113. * Runtime PM callbacks are provided by amba/bus.c driver.
  2114. *
  2115. * It is assumed here that IRQ safe runtime PM is chosen in probe and amba
  2116. * bus driver will only disable/enable the clock in runtime PM callbacks.
  2117. */
  2118. static int __maybe_unused pl330_suspend(struct device *dev)
  2119. {
  2120. struct amba_device *pcdev = to_amba_device(dev);
  2121. pm_runtime_disable(dev);
  2122. if (!pm_runtime_status_suspended(dev)) {
  2123. /* amba did not disable the clock */
  2124. amba_pclk_disable(pcdev);
  2125. }
  2126. amba_pclk_unprepare(pcdev);
  2127. return 0;
  2128. }
  2129. static int __maybe_unused pl330_resume(struct device *dev)
  2130. {
  2131. struct amba_device *pcdev = to_amba_device(dev);
  2132. int ret;
  2133. ret = amba_pclk_prepare(pcdev);
  2134. if (ret)
  2135. return ret;
  2136. if (!pm_runtime_status_suspended(dev))
  2137. ret = amba_pclk_enable(pcdev);
  2138. pm_runtime_enable(dev);
  2139. return ret;
  2140. }
  2141. static SIMPLE_DEV_PM_OPS(pl330_pm, pl330_suspend, pl330_resume);
  2142. static int
  2143. pl330_probe(struct amba_device *adev, const struct amba_id *id)
  2144. {
  2145. struct dma_pl330_platdata *pdat;
  2146. struct pl330_config *pcfg;
  2147. struct pl330_dmac *pl330;
  2148. struct dma_pl330_chan *pch, *_p;
  2149. struct dma_device *pd;
  2150. struct resource *res;
  2151. int i, ret, irq;
  2152. int num_chan;
  2153. pdat = dev_get_platdata(&adev->dev);
  2154. ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
  2155. if (ret)
  2156. return ret;
  2157. /* Allocate a new DMAC and its Channels */
  2158. pl330 = devm_kzalloc(&adev->dev, sizeof(*pl330), GFP_KERNEL);
  2159. if (!pl330) {
  2160. dev_err(&adev->dev, "unable to allocate mem\n");
  2161. return -ENOMEM;
  2162. }
  2163. pd = &pl330->ddma;
  2164. pd->dev = &adev->dev;
  2165. pl330->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
  2166. res = &adev->res;
  2167. pl330->base = devm_ioremap_resource(&adev->dev, res);
  2168. if (IS_ERR(pl330->base))
  2169. return PTR_ERR(pl330->base);
  2170. amba_set_drvdata(adev, pl330);
  2171. for (i = 0; i < AMBA_NR_IRQS; i++) {
  2172. irq = adev->irq[i];
  2173. if (irq) {
  2174. ret = devm_request_irq(&adev->dev, irq,
  2175. pl330_irq_handler, 0,
  2176. dev_name(&adev->dev), pl330);
  2177. if (ret)
  2178. return ret;
  2179. } else {
  2180. break;
  2181. }
  2182. }
  2183. pcfg = &pl330->pcfg;
  2184. pcfg->periph_id = adev->periphid;
  2185. ret = pl330_add(pl330);
  2186. if (ret)
  2187. return ret;
  2188. INIT_LIST_HEAD(&pl330->desc_pool);
  2189. spin_lock_init(&pl330->pool_lock);
  2190. /* Create a descriptor pool of default size */
  2191. if (!add_desc(pl330, GFP_KERNEL, NR_DEFAULT_DESC))
  2192. dev_warn(&adev->dev, "unable to allocate desc\n");
  2193. INIT_LIST_HEAD(&pd->channels);
  2194. /* Initialize channel parameters */
  2195. if (pdat)
  2196. num_chan = max_t(int, pdat->nr_valid_peri, pcfg->num_chan);
  2197. else
  2198. num_chan = max_t(int, pcfg->num_peri, pcfg->num_chan);
  2199. pl330->num_peripherals = num_chan;
  2200. pl330->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
  2201. if (!pl330->peripherals) {
  2202. ret = -ENOMEM;
  2203. dev_err(&adev->dev, "unable to allocate pl330->peripherals\n");
  2204. goto probe_err2;
  2205. }
  2206. for (i = 0; i < num_chan; i++) {
  2207. pch = &pl330->peripherals[i];
  2208. if (!adev->dev.of_node)
  2209. pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
  2210. else
  2211. pch->chan.private = adev->dev.of_node;
  2212. INIT_LIST_HEAD(&pch->submitted_list);
  2213. INIT_LIST_HEAD(&pch->work_list);
  2214. INIT_LIST_HEAD(&pch->completed_list);
  2215. spin_lock_init(&pch->lock);
  2216. pch->thread = NULL;
  2217. pch->chan.device = pd;
  2218. pch->dmac = pl330;
  2219. /* Add the channel to the DMAC list */
  2220. list_add_tail(&pch->chan.device_node, &pd->channels);
  2221. }
  2222. if (pdat) {
  2223. pd->cap_mask = pdat->cap_mask;
  2224. } else {
  2225. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  2226. if (pcfg->num_peri) {
  2227. dma_cap_set(DMA_SLAVE, pd->cap_mask);
  2228. dma_cap_set(DMA_CYCLIC, pd->cap_mask);
  2229. dma_cap_set(DMA_PRIVATE, pd->cap_mask);
  2230. }
  2231. }
  2232. pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
  2233. pd->device_free_chan_resources = pl330_free_chan_resources;
  2234. pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
  2235. pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
  2236. pd->device_tx_status = pl330_tx_status;
  2237. pd->device_prep_slave_sg = pl330_prep_slave_sg;
  2238. pd->device_control = pl330_control;
  2239. pd->device_issue_pending = pl330_issue_pending;
  2240. pd->device_slave_caps = pl330_dma_device_slave_caps;
  2241. ret = dma_async_device_register(pd);
  2242. if (ret) {
  2243. dev_err(&adev->dev, "unable to register DMAC\n");
  2244. goto probe_err3;
  2245. }
  2246. if (adev->dev.of_node) {
  2247. ret = of_dma_controller_register(adev->dev.of_node,
  2248. of_dma_pl330_xlate, pl330);
  2249. if (ret) {
  2250. dev_err(&adev->dev,
  2251. "unable to register DMA to the generic DT DMA helpers\n");
  2252. }
  2253. }
  2254. adev->dev.dma_parms = &pl330->dma_parms;
  2255. /*
  2256. * This is the limit for transfers with a buswidth of 1, larger
  2257. * buswidths will have larger limits.
  2258. */
  2259. ret = dma_set_max_seg_size(&adev->dev, 1900800);
  2260. if (ret)
  2261. dev_err(&adev->dev, "unable to set the seg size\n");
  2262. dev_info(&adev->dev,
  2263. "Loaded driver for PL330 DMAC-%x\n", adev->periphid);
  2264. dev_info(&adev->dev,
  2265. "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
  2266. pcfg->data_buf_dep, pcfg->data_bus_width / 8, pcfg->num_chan,
  2267. pcfg->num_peri, pcfg->num_events);
  2268. pm_runtime_irq_safe(&adev->dev);
  2269. pm_runtime_use_autosuspend(&adev->dev);
  2270. pm_runtime_set_autosuspend_delay(&adev->dev, PL330_AUTOSUSPEND_DELAY);
  2271. pm_runtime_mark_last_busy(&adev->dev);
  2272. pm_runtime_put_autosuspend(&adev->dev);
  2273. return 0;
  2274. probe_err3:
  2275. /* Idle the DMAC */
  2276. list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
  2277. chan.device_node) {
  2278. /* Remove the channel */
  2279. list_del(&pch->chan.device_node);
  2280. /* Flush the channel */
  2281. if (pch->thread) {
  2282. pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
  2283. pl330_free_chan_resources(&pch->chan);
  2284. }
  2285. }
  2286. probe_err2:
  2287. pl330_del(pl330);
  2288. return ret;
  2289. }
  2290. static int pl330_remove(struct amba_device *adev)
  2291. {
  2292. struct pl330_dmac *pl330 = amba_get_drvdata(adev);
  2293. struct dma_pl330_chan *pch, *_p;
  2294. pm_runtime_get_noresume(pl330->ddma.dev);
  2295. if (adev->dev.of_node)
  2296. of_dma_controller_free(adev->dev.of_node);
  2297. dma_async_device_unregister(&pl330->ddma);
  2298. /* Idle the DMAC */
  2299. list_for_each_entry_safe(pch, _p, &pl330->ddma.channels,
  2300. chan.device_node) {
  2301. /* Remove the channel */
  2302. list_del(&pch->chan.device_node);
  2303. /* Flush the channel */
  2304. if (pch->thread) {
  2305. pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
  2306. pl330_free_chan_resources(&pch->chan);
  2307. }
  2308. }
  2309. pl330_del(pl330);
  2310. return 0;
  2311. }
  2312. static struct amba_id pl330_ids[] = {
  2313. {
  2314. .id = 0x00041330,
  2315. .mask = 0x000fffff,
  2316. },
  2317. { 0, 0 },
  2318. };
  2319. MODULE_DEVICE_TABLE(amba, pl330_ids);
  2320. static struct amba_driver pl330_driver = {
  2321. .drv = {
  2322. .owner = THIS_MODULE,
  2323. .name = "dma-pl330",
  2324. .pm = &pl330_pm,
  2325. },
  2326. .id_table = pl330_ids,
  2327. .probe = pl330_probe,
  2328. .remove = pl330_remove,
  2329. };
  2330. module_amba_driver(pl330_driver);
  2331. MODULE_AUTHOR("Jaswinder Singh <jassisinghbrar@gmail.com>");
  2332. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  2333. MODULE_LICENSE("GPL");