pl330.c 67 KB

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