pl330.c 70 KB

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