pl330.c 68 KB

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