pl330.c 68 KB

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