pl330.c 66 KB

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