pl330.c 68 KB

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