pl330.c 67 KB

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