pl330.c 66 KB

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