pl330.c 70 KB

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