pl330.c 67 KB

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