pl330.c 67 KB

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