pl330.c 67 KB

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