pl330.c 67 KB

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