pl330.c 66 KB

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