pl330.c 67 KB

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