pl330.c 66 KB

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