pl330.c 68 KB

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