pl330.c 68 KB

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