xgene-dma.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089
  1. /*
  2. * Applied Micro X-Gene SoC DMA engine Driver
  3. *
  4. * Copyright (c) 2015, Applied Micro Circuits Corporation
  5. * Authors: Rameshwar Prasad Sahu <rsahu@apm.com>
  6. * Loc Ho <lho@apm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * NOTE: PM support is currently not available.
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/dmapool.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/module.h>
  31. #include <linux/of_device.h>
  32. #include "dmaengine.h"
  33. /* X-Gene DMA ring csr registers and bit definations */
  34. #define XGENE_DMA_RING_CONFIG 0x04
  35. #define XGENE_DMA_RING_ENABLE BIT(31)
  36. #define XGENE_DMA_RING_ID 0x08
  37. #define XGENE_DMA_RING_ID_SETUP(v) ((v) | BIT(31))
  38. #define XGENE_DMA_RING_ID_BUF 0x0C
  39. #define XGENE_DMA_RING_ID_BUF_SETUP(v) (((v) << 9) | BIT(21))
  40. #define XGENE_DMA_RING_THRESLD0_SET1 0x30
  41. #define XGENE_DMA_RING_THRESLD0_SET1_VAL 0X64
  42. #define XGENE_DMA_RING_THRESLD1_SET1 0x34
  43. #define XGENE_DMA_RING_THRESLD1_SET1_VAL 0xC8
  44. #define XGENE_DMA_RING_HYSTERESIS 0x68
  45. #define XGENE_DMA_RING_HYSTERESIS_VAL 0xFFFFFFFF
  46. #define XGENE_DMA_RING_STATE 0x6C
  47. #define XGENE_DMA_RING_STATE_WR_BASE 0x70
  48. #define XGENE_DMA_RING_NE_INT_MODE 0x017C
  49. #define XGENE_DMA_RING_NE_INT_MODE_SET(m, v) \
  50. ((m) = ((m) & ~BIT(31 - (v))) | BIT(31 - (v)))
  51. #define XGENE_DMA_RING_NE_INT_MODE_RESET(m, v) \
  52. ((m) &= (~BIT(31 - (v))))
  53. #define XGENE_DMA_RING_CLKEN 0xC208
  54. #define XGENE_DMA_RING_SRST 0xC200
  55. #define XGENE_DMA_RING_MEM_RAM_SHUTDOWN 0xD070
  56. #define XGENE_DMA_RING_BLK_MEM_RDY 0xD074
  57. #define XGENE_DMA_RING_BLK_MEM_RDY_VAL 0xFFFFFFFF
  58. #define XGENE_DMA_RING_DESC_CNT(v) (((v) & 0x0001FFFE) >> 1)
  59. #define XGENE_DMA_RING_ID_GET(owner, num) (((owner) << 6) | (num))
  60. #define XGENE_DMA_RING_DST_ID(v) ((1 << 10) | (v))
  61. #define XGENE_DMA_RING_CMD_OFFSET 0x2C
  62. #define XGENE_DMA_RING_CMD_BASE_OFFSET(v) ((v) << 6)
  63. #define XGENE_DMA_RING_COHERENT_SET(m) \
  64. (((u32 *)(m))[2] |= BIT(4))
  65. #define XGENE_DMA_RING_ADDRL_SET(m, v) \
  66. (((u32 *)(m))[2] |= (((v) >> 8) << 5))
  67. #define XGENE_DMA_RING_ADDRH_SET(m, v) \
  68. (((u32 *)(m))[3] |= ((v) >> 35))
  69. #define XGENE_DMA_RING_ACCEPTLERR_SET(m) \
  70. (((u32 *)(m))[3] |= BIT(19))
  71. #define XGENE_DMA_RING_SIZE_SET(m, v) \
  72. (((u32 *)(m))[3] |= ((v) << 23))
  73. #define XGENE_DMA_RING_RECOMBBUF_SET(m) \
  74. (((u32 *)(m))[3] |= BIT(27))
  75. #define XGENE_DMA_RING_RECOMTIMEOUTL_SET(m) \
  76. (((u32 *)(m))[3] |= (0x7 << 28))
  77. #define XGENE_DMA_RING_RECOMTIMEOUTH_SET(m) \
  78. (((u32 *)(m))[4] |= 0x3)
  79. #define XGENE_DMA_RING_SELTHRSH_SET(m) \
  80. (((u32 *)(m))[4] |= BIT(3))
  81. #define XGENE_DMA_RING_TYPE_SET(m, v) \
  82. (((u32 *)(m))[4] |= ((v) << 19))
  83. /* X-Gene DMA device csr registers and bit definitions */
  84. #define XGENE_DMA_IPBRR 0x0
  85. #define XGENE_DMA_DEV_ID_RD(v) ((v) & 0x00000FFF)
  86. #define XGENE_DMA_BUS_ID_RD(v) (((v) >> 12) & 3)
  87. #define XGENE_DMA_REV_NO_RD(v) (((v) >> 14) & 3)
  88. #define XGENE_DMA_GCR 0x10
  89. #define XGENE_DMA_CH_SETUP(v) \
  90. ((v) = ((v) & ~0x000FFFFF) | 0x000AAFFF)
  91. #define XGENE_DMA_ENABLE(v) ((v) |= BIT(31))
  92. #define XGENE_DMA_DISABLE(v) ((v) &= ~BIT(31))
  93. #define XGENE_DMA_RAID6_CONT 0x14
  94. #define XGENE_DMA_RAID6_MULTI_CTRL(v) ((v) << 24)
  95. #define XGENE_DMA_INT 0x70
  96. #define XGENE_DMA_INT_MASK 0x74
  97. #define XGENE_DMA_INT_ALL_MASK 0xFFFFFFFF
  98. #define XGENE_DMA_INT_ALL_UNMASK 0x0
  99. #define XGENE_DMA_INT_MASK_SHIFT 0x14
  100. #define XGENE_DMA_RING_INT0_MASK 0x90A0
  101. #define XGENE_DMA_RING_INT1_MASK 0x90A8
  102. #define XGENE_DMA_RING_INT2_MASK 0x90B0
  103. #define XGENE_DMA_RING_INT3_MASK 0x90B8
  104. #define XGENE_DMA_RING_INT4_MASK 0x90C0
  105. #define XGENE_DMA_CFG_RING_WQ_ASSOC 0x90E0
  106. #define XGENE_DMA_ASSOC_RING_MNGR1 0xFFFFFFFF
  107. #define XGENE_DMA_MEM_RAM_SHUTDOWN 0xD070
  108. #define XGENE_DMA_BLK_MEM_RDY 0xD074
  109. #define XGENE_DMA_BLK_MEM_RDY_VAL 0xFFFFFFFF
  110. /* X-Gene SoC EFUSE csr register and bit defination */
  111. #define XGENE_SOC_JTAG1_SHADOW 0x18
  112. #define XGENE_DMA_PQ_DISABLE_MASK BIT(13)
  113. /* X-Gene DMA Descriptor format */
  114. #define XGENE_DMA_DESC_NV_BIT BIT_ULL(50)
  115. #define XGENE_DMA_DESC_IN_BIT BIT_ULL(55)
  116. #define XGENE_DMA_DESC_C_BIT BIT_ULL(63)
  117. #define XGENE_DMA_DESC_DR_BIT BIT_ULL(61)
  118. #define XGENE_DMA_DESC_ELERR_POS 46
  119. #define XGENE_DMA_DESC_RTYPE_POS 56
  120. #define XGENE_DMA_DESC_LERR_POS 60
  121. #define XGENE_DMA_DESC_FLYBY_POS 4
  122. #define XGENE_DMA_DESC_BUFLEN_POS 48
  123. #define XGENE_DMA_DESC_HOENQ_NUM_POS 48
  124. #define XGENE_DMA_DESC_NV_SET(m) \
  125. (((u64 *)(m))[0] |= XGENE_DMA_DESC_NV_BIT)
  126. #define XGENE_DMA_DESC_IN_SET(m) \
  127. (((u64 *)(m))[0] |= XGENE_DMA_DESC_IN_BIT)
  128. #define XGENE_DMA_DESC_RTYPE_SET(m, v) \
  129. (((u64 *)(m))[0] |= ((u64)(v) << XGENE_DMA_DESC_RTYPE_POS))
  130. #define XGENE_DMA_DESC_BUFADDR_SET(m, v) \
  131. (((u64 *)(m))[0] |= (v))
  132. #define XGENE_DMA_DESC_BUFLEN_SET(m, v) \
  133. (((u64 *)(m))[0] |= ((u64)(v) << XGENE_DMA_DESC_BUFLEN_POS))
  134. #define XGENE_DMA_DESC_C_SET(m) \
  135. (((u64 *)(m))[1] |= XGENE_DMA_DESC_C_BIT)
  136. #define XGENE_DMA_DESC_FLYBY_SET(m, v) \
  137. (((u64 *)(m))[2] |= ((v) << XGENE_DMA_DESC_FLYBY_POS))
  138. #define XGENE_DMA_DESC_MULTI_SET(m, v, i) \
  139. (((u64 *)(m))[2] |= ((u64)(v) << (((i) + 1) * 8)))
  140. #define XGENE_DMA_DESC_DR_SET(m) \
  141. (((u64 *)(m))[2] |= XGENE_DMA_DESC_DR_BIT)
  142. #define XGENE_DMA_DESC_DST_ADDR_SET(m, v) \
  143. (((u64 *)(m))[3] |= (v))
  144. #define XGENE_DMA_DESC_H0ENQ_NUM_SET(m, v) \
  145. (((u64 *)(m))[3] |= ((u64)(v) << XGENE_DMA_DESC_HOENQ_NUM_POS))
  146. #define XGENE_DMA_DESC_ELERR_RD(m) \
  147. (((m) >> XGENE_DMA_DESC_ELERR_POS) & 0x3)
  148. #define XGENE_DMA_DESC_LERR_RD(m) \
  149. (((m) >> XGENE_DMA_DESC_LERR_POS) & 0x7)
  150. #define XGENE_DMA_DESC_STATUS(elerr, lerr) \
  151. (((elerr) << 4) | (lerr))
  152. /* X-Gene DMA descriptor empty s/w signature */
  153. #define XGENE_DMA_DESC_EMPTY_INDEX 0
  154. #define XGENE_DMA_DESC_EMPTY_SIGNATURE ~0ULL
  155. #define XGENE_DMA_DESC_SET_EMPTY(m) \
  156. (((u64 *)(m))[XGENE_DMA_DESC_EMPTY_INDEX] = \
  157. XGENE_DMA_DESC_EMPTY_SIGNATURE)
  158. #define XGENE_DMA_DESC_IS_EMPTY(m) \
  159. (((u64 *)(m))[XGENE_DMA_DESC_EMPTY_INDEX] == \
  160. XGENE_DMA_DESC_EMPTY_SIGNATURE)
  161. /* X-Gene DMA configurable parameters defines */
  162. #define XGENE_DMA_RING_NUM 512
  163. #define XGENE_DMA_BUFNUM 0x0
  164. #define XGENE_DMA_CPU_BUFNUM 0x18
  165. #define XGENE_DMA_RING_OWNER_DMA 0x03
  166. #define XGENE_DMA_RING_OWNER_CPU 0x0F
  167. #define XGENE_DMA_RING_TYPE_REGULAR 0x01
  168. #define XGENE_DMA_RING_WQ_DESC_SIZE 32 /* 32 Bytes */
  169. #define XGENE_DMA_RING_NUM_CONFIG 5
  170. #define XGENE_DMA_MAX_CHANNEL 4
  171. #define XGENE_DMA_XOR_CHANNEL 0
  172. #define XGENE_DMA_PQ_CHANNEL 1
  173. #define XGENE_DMA_MAX_BYTE_CNT 0x4000 /* 16 KB */
  174. #define XGENE_DMA_MAX_64B_DESC_BYTE_CNT 0x14000 /* 80 KB */
  175. #define XGENE_DMA_XOR_ALIGNMENT 6 /* 64 Bytes */
  176. #define XGENE_DMA_MAX_XOR_SRC 5
  177. #define XGENE_DMA_16K_BUFFER_LEN_CODE 0x0
  178. #define XGENE_DMA_INVALID_LEN_CODE 0x7800
  179. /* X-Gene DMA descriptor error codes */
  180. #define ERR_DESC_AXI 0x01
  181. #define ERR_BAD_DESC 0x02
  182. #define ERR_READ_DATA_AXI 0x03
  183. #define ERR_WRITE_DATA_AXI 0x04
  184. #define ERR_FBP_TIMEOUT 0x05
  185. #define ERR_ECC 0x06
  186. #define ERR_DIFF_SIZE 0x08
  187. #define ERR_SCT_GAT_LEN 0x09
  188. #define ERR_CRC_ERR 0x11
  189. #define ERR_CHKSUM 0x12
  190. #define ERR_DIF 0x13
  191. /* X-Gene DMA error interrupt codes */
  192. #define ERR_DIF_SIZE_INT 0x0
  193. #define ERR_GS_ERR_INT 0x1
  194. #define ERR_FPB_TIMEO_INT 0x2
  195. #define ERR_WFIFO_OVF_INT 0x3
  196. #define ERR_RFIFO_OVF_INT 0x4
  197. #define ERR_WR_TIMEO_INT 0x5
  198. #define ERR_RD_TIMEO_INT 0x6
  199. #define ERR_WR_ERR_INT 0x7
  200. #define ERR_RD_ERR_INT 0x8
  201. #define ERR_BAD_DESC_INT 0x9
  202. #define ERR_DESC_DST_INT 0xA
  203. #define ERR_DESC_SRC_INT 0xB
  204. /* X-Gene DMA flyby operation code */
  205. #define FLYBY_2SRC_XOR 0x8
  206. #define FLYBY_3SRC_XOR 0x9
  207. #define FLYBY_4SRC_XOR 0xA
  208. #define FLYBY_5SRC_XOR 0xB
  209. /* X-Gene DMA SW descriptor flags */
  210. #define XGENE_DMA_FLAG_64B_DESC BIT(0)
  211. /* Define to dump X-Gene DMA descriptor */
  212. #define XGENE_DMA_DESC_DUMP(desc, m) \
  213. print_hex_dump(KERN_ERR, (m), \
  214. DUMP_PREFIX_ADDRESS, 16, 8, (desc), 32, 0)
  215. #define to_dma_desc_sw(tx) \
  216. container_of(tx, struct xgene_dma_desc_sw, tx)
  217. #define to_dma_chan(dchan) \
  218. container_of(dchan, struct xgene_dma_chan, dma_chan)
  219. #define chan_dbg(chan, fmt, arg...) \
  220. dev_dbg(chan->dev, "%s: " fmt, chan->name, ##arg)
  221. #define chan_err(chan, fmt, arg...) \
  222. dev_err(chan->dev, "%s: " fmt, chan->name, ##arg)
  223. struct xgene_dma_desc_hw {
  224. u64 m0;
  225. u64 m1;
  226. u64 m2;
  227. u64 m3;
  228. };
  229. enum xgene_dma_ring_cfgsize {
  230. XGENE_DMA_RING_CFG_SIZE_512B,
  231. XGENE_DMA_RING_CFG_SIZE_2KB,
  232. XGENE_DMA_RING_CFG_SIZE_16KB,
  233. XGENE_DMA_RING_CFG_SIZE_64KB,
  234. XGENE_DMA_RING_CFG_SIZE_512KB,
  235. XGENE_DMA_RING_CFG_SIZE_INVALID
  236. };
  237. struct xgene_dma_ring {
  238. struct xgene_dma *pdma;
  239. u8 buf_num;
  240. u16 id;
  241. u16 num;
  242. u16 head;
  243. u16 owner;
  244. u16 slots;
  245. u16 dst_ring_num;
  246. u32 size;
  247. void __iomem *cmd;
  248. void __iomem *cmd_base;
  249. dma_addr_t desc_paddr;
  250. u32 state[XGENE_DMA_RING_NUM_CONFIG];
  251. enum xgene_dma_ring_cfgsize cfgsize;
  252. union {
  253. void *desc_vaddr;
  254. struct xgene_dma_desc_hw *desc_hw;
  255. };
  256. };
  257. struct xgene_dma_desc_sw {
  258. struct xgene_dma_desc_hw desc1;
  259. struct xgene_dma_desc_hw desc2;
  260. u32 flags;
  261. struct list_head node;
  262. struct list_head tx_list;
  263. struct dma_async_tx_descriptor tx;
  264. };
  265. /**
  266. * struct xgene_dma_chan - internal representation of an X-Gene DMA channel
  267. * @dma_chan: dmaengine channel object member
  268. * @pdma: X-Gene DMA device structure reference
  269. * @dev: struct device reference for dma mapping api
  270. * @id: raw id of this channel
  271. * @rx_irq: channel IRQ
  272. * @name: name of X-Gene DMA channel
  273. * @lock: serializes enqueue/dequeue operations to the descriptor pool
  274. * @pending: number of transaction request pushed to DMA controller for
  275. * execution, but still waiting for completion,
  276. * @max_outstanding: max number of outstanding request we can push to channel
  277. * @ld_pending: descriptors which are queued to run, but have not yet been
  278. * submitted to the hardware for execution
  279. * @ld_running: descriptors which are currently being executing by the hardware
  280. * @ld_completed: descriptors which have finished execution by the hardware.
  281. * These descriptors have already had their cleanup actions run. They
  282. * are waiting for the ACK bit to be set by the async tx API.
  283. * @desc_pool: descriptor pool for DMA operations
  284. * @tasklet: bottom half where all completed descriptors cleans
  285. * @tx_ring: transmit ring descriptor that we use to prepare actual
  286. * descriptors for further executions
  287. * @rx_ring: receive ring descriptor that we use to get completed DMA
  288. * descriptors during cleanup time
  289. */
  290. struct xgene_dma_chan {
  291. struct dma_chan dma_chan;
  292. struct xgene_dma *pdma;
  293. struct device *dev;
  294. int id;
  295. int rx_irq;
  296. char name[10];
  297. spinlock_t lock;
  298. int pending;
  299. int max_outstanding;
  300. struct list_head ld_pending;
  301. struct list_head ld_running;
  302. struct list_head ld_completed;
  303. struct dma_pool *desc_pool;
  304. struct tasklet_struct tasklet;
  305. struct xgene_dma_ring tx_ring;
  306. struct xgene_dma_ring rx_ring;
  307. };
  308. /**
  309. * struct xgene_dma - internal representation of an X-Gene DMA device
  310. * @err_irq: DMA error irq number
  311. * @ring_num: start id number for DMA ring
  312. * @csr_dma: base for DMA register access
  313. * @csr_ring: base for DMA ring register access
  314. * @csr_ring_cmd: base for DMA ring command register access
  315. * @csr_efuse: base for efuse register access
  316. * @dma_dev: embedded struct dma_device
  317. * @chan: reference to X-Gene DMA channels
  318. */
  319. struct xgene_dma {
  320. struct device *dev;
  321. struct clk *clk;
  322. int err_irq;
  323. int ring_num;
  324. void __iomem *csr_dma;
  325. void __iomem *csr_ring;
  326. void __iomem *csr_ring_cmd;
  327. void __iomem *csr_efuse;
  328. struct dma_device dma_dev[XGENE_DMA_MAX_CHANNEL];
  329. struct xgene_dma_chan chan[XGENE_DMA_MAX_CHANNEL];
  330. };
  331. static const char * const xgene_dma_desc_err[] = {
  332. [ERR_DESC_AXI] = "AXI error when reading src/dst link list",
  333. [ERR_BAD_DESC] = "ERR or El_ERR fields not set to zero in desc",
  334. [ERR_READ_DATA_AXI] = "AXI error when reading data",
  335. [ERR_WRITE_DATA_AXI] = "AXI error when writing data",
  336. [ERR_FBP_TIMEOUT] = "Timeout on bufpool fetch",
  337. [ERR_ECC] = "ECC double bit error",
  338. [ERR_DIFF_SIZE] = "Bufpool too small to hold all the DIF result",
  339. [ERR_SCT_GAT_LEN] = "Gather and scatter data length not same",
  340. [ERR_CRC_ERR] = "CRC error",
  341. [ERR_CHKSUM] = "Checksum error",
  342. [ERR_DIF] = "DIF error",
  343. };
  344. static const char * const xgene_dma_err[] = {
  345. [ERR_DIF_SIZE_INT] = "DIF size error",
  346. [ERR_GS_ERR_INT] = "Gather scatter not same size error",
  347. [ERR_FPB_TIMEO_INT] = "Free pool time out error",
  348. [ERR_WFIFO_OVF_INT] = "Write FIFO over flow error",
  349. [ERR_RFIFO_OVF_INT] = "Read FIFO over flow error",
  350. [ERR_WR_TIMEO_INT] = "Write time out error",
  351. [ERR_RD_TIMEO_INT] = "Read time out error",
  352. [ERR_WR_ERR_INT] = "HBF bus write error",
  353. [ERR_RD_ERR_INT] = "HBF bus read error",
  354. [ERR_BAD_DESC_INT] = "Ring descriptor HE0 not set error",
  355. [ERR_DESC_DST_INT] = "HFB reading dst link address error",
  356. [ERR_DESC_SRC_INT] = "HFB reading src link address error",
  357. };
  358. static bool is_pq_enabled(struct xgene_dma *pdma)
  359. {
  360. u32 val;
  361. val = ioread32(pdma->csr_efuse + XGENE_SOC_JTAG1_SHADOW);
  362. return !(val & XGENE_DMA_PQ_DISABLE_MASK);
  363. }
  364. static void xgene_dma_cpu_to_le64(u64 *desc, int count)
  365. {
  366. int i;
  367. for (i = 0; i < count; i++)
  368. desc[i] = cpu_to_le64(desc[i]);
  369. }
  370. static u16 xgene_dma_encode_len(u32 len)
  371. {
  372. return (len < XGENE_DMA_MAX_BYTE_CNT) ?
  373. len : XGENE_DMA_16K_BUFFER_LEN_CODE;
  374. }
  375. static u8 xgene_dma_encode_xor_flyby(u32 src_cnt)
  376. {
  377. static u8 flyby_type[] = {
  378. FLYBY_2SRC_XOR, /* Dummy */
  379. FLYBY_2SRC_XOR, /* Dummy */
  380. FLYBY_2SRC_XOR,
  381. FLYBY_3SRC_XOR,
  382. FLYBY_4SRC_XOR,
  383. FLYBY_5SRC_XOR
  384. };
  385. return flyby_type[src_cnt];
  386. }
  387. static u32 xgene_dma_ring_desc_cnt(struct xgene_dma_ring *ring)
  388. {
  389. u32 __iomem *cmd_base = ring->cmd_base;
  390. u32 ring_state = ioread32(&cmd_base[1]);
  391. return XGENE_DMA_RING_DESC_CNT(ring_state);
  392. }
  393. static void xgene_dma_set_src_buffer(void *ext8, size_t *len,
  394. dma_addr_t *paddr)
  395. {
  396. size_t nbytes = (*len < XGENE_DMA_MAX_BYTE_CNT) ?
  397. *len : XGENE_DMA_MAX_BYTE_CNT;
  398. XGENE_DMA_DESC_BUFADDR_SET(ext8, *paddr);
  399. XGENE_DMA_DESC_BUFLEN_SET(ext8, xgene_dma_encode_len(nbytes));
  400. *len -= nbytes;
  401. *paddr += nbytes;
  402. }
  403. static void xgene_dma_invalidate_buffer(void *ext8)
  404. {
  405. XGENE_DMA_DESC_BUFLEN_SET(ext8, XGENE_DMA_INVALID_LEN_CODE);
  406. }
  407. static void *xgene_dma_lookup_ext8(u64 *desc, int idx)
  408. {
  409. return (idx % 2) ? (desc + idx - 1) : (desc + idx + 1);
  410. }
  411. static void xgene_dma_init_desc(void *desc, u16 dst_ring_num)
  412. {
  413. XGENE_DMA_DESC_C_SET(desc); /* Coherent IO */
  414. XGENE_DMA_DESC_IN_SET(desc);
  415. XGENE_DMA_DESC_H0ENQ_NUM_SET(desc, dst_ring_num);
  416. XGENE_DMA_DESC_RTYPE_SET(desc, XGENE_DMA_RING_OWNER_DMA);
  417. }
  418. static void xgene_dma_prep_cpy_desc(struct xgene_dma_chan *chan,
  419. struct xgene_dma_desc_sw *desc_sw,
  420. dma_addr_t dst, dma_addr_t src,
  421. size_t len)
  422. {
  423. void *desc1, *desc2;
  424. int i;
  425. /* Get 1st descriptor */
  426. desc1 = &desc_sw->desc1;
  427. xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
  428. /* Set destination address */
  429. XGENE_DMA_DESC_DR_SET(desc1);
  430. XGENE_DMA_DESC_DST_ADDR_SET(desc1, dst);
  431. /* Set 1st source address */
  432. xgene_dma_set_src_buffer(desc1 + 8, &len, &src);
  433. if (len <= 0) {
  434. desc2 = NULL;
  435. goto skip_additional_src;
  436. }
  437. /*
  438. * We need to split this source buffer,
  439. * and need to use 2nd descriptor
  440. */
  441. desc2 = &desc_sw->desc2;
  442. XGENE_DMA_DESC_NV_SET(desc1);
  443. /* Set 2nd to 5th source address */
  444. for (i = 0; i < 4 && len; i++)
  445. xgene_dma_set_src_buffer(xgene_dma_lookup_ext8(desc2, i),
  446. &len, &src);
  447. /* Invalidate unused source address field */
  448. for (; i < 4; i++)
  449. xgene_dma_invalidate_buffer(xgene_dma_lookup_ext8(desc2, i));
  450. /* Updated flag that we have prepared 64B descriptor */
  451. desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
  452. skip_additional_src:
  453. /* Hardware stores descriptor in little endian format */
  454. xgene_dma_cpu_to_le64(desc1, 4);
  455. if (desc2)
  456. xgene_dma_cpu_to_le64(desc2, 4);
  457. }
  458. static void xgene_dma_prep_xor_desc(struct xgene_dma_chan *chan,
  459. struct xgene_dma_desc_sw *desc_sw,
  460. dma_addr_t *dst, dma_addr_t *src,
  461. u32 src_cnt, size_t *nbytes,
  462. const u8 *scf)
  463. {
  464. void *desc1, *desc2;
  465. size_t len = *nbytes;
  466. int i;
  467. desc1 = &desc_sw->desc1;
  468. desc2 = &desc_sw->desc2;
  469. /* Initialize DMA descriptor */
  470. xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
  471. /* Set destination address */
  472. XGENE_DMA_DESC_DR_SET(desc1);
  473. XGENE_DMA_DESC_DST_ADDR_SET(desc1, *dst);
  474. /* We have multiple source addresses, so need to set NV bit*/
  475. XGENE_DMA_DESC_NV_SET(desc1);
  476. /* Set flyby opcode */
  477. XGENE_DMA_DESC_FLYBY_SET(desc1, xgene_dma_encode_xor_flyby(src_cnt));
  478. /* Set 1st to 5th source addresses */
  479. for (i = 0; i < src_cnt; i++) {
  480. len = *nbytes;
  481. xgene_dma_set_src_buffer((i == 0) ? (desc1 + 8) :
  482. xgene_dma_lookup_ext8(desc2, i - 1),
  483. &len, &src[i]);
  484. XGENE_DMA_DESC_MULTI_SET(desc1, scf[i], i);
  485. }
  486. /* Hardware stores descriptor in little endian format */
  487. xgene_dma_cpu_to_le64(desc1, 4);
  488. xgene_dma_cpu_to_le64(desc2, 4);
  489. /* Update meta data */
  490. *nbytes = len;
  491. *dst += XGENE_DMA_MAX_BYTE_CNT;
  492. /* We need always 64B descriptor to perform xor or pq operations */
  493. desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
  494. }
  495. static dma_cookie_t xgene_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  496. {
  497. struct xgene_dma_desc_sw *desc;
  498. struct xgene_dma_chan *chan;
  499. dma_cookie_t cookie;
  500. if (unlikely(!tx))
  501. return -EINVAL;
  502. chan = to_dma_chan(tx->chan);
  503. desc = to_dma_desc_sw(tx);
  504. spin_lock_bh(&chan->lock);
  505. cookie = dma_cookie_assign(tx);
  506. /* Add this transaction list onto the tail of the pending queue */
  507. list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
  508. spin_unlock_bh(&chan->lock);
  509. return cookie;
  510. }
  511. static void xgene_dma_clean_descriptor(struct xgene_dma_chan *chan,
  512. struct xgene_dma_desc_sw *desc)
  513. {
  514. list_del(&desc->node);
  515. chan_dbg(chan, "LD %p free\n", desc);
  516. dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
  517. }
  518. static struct xgene_dma_desc_sw *xgene_dma_alloc_descriptor(
  519. struct xgene_dma_chan *chan)
  520. {
  521. struct xgene_dma_desc_sw *desc;
  522. dma_addr_t phys;
  523. desc = dma_pool_alloc(chan->desc_pool, GFP_NOWAIT, &phys);
  524. if (!desc) {
  525. chan_err(chan, "Failed to allocate LDs\n");
  526. return NULL;
  527. }
  528. memset(desc, 0, sizeof(*desc));
  529. INIT_LIST_HEAD(&desc->tx_list);
  530. desc->tx.phys = phys;
  531. desc->tx.tx_submit = xgene_dma_tx_submit;
  532. dma_async_tx_descriptor_init(&desc->tx, &chan->dma_chan);
  533. chan_dbg(chan, "LD %p allocated\n", desc);
  534. return desc;
  535. }
  536. /**
  537. * xgene_dma_clean_completed_descriptor - free all descriptors which
  538. * has been completed and acked
  539. * @chan: X-Gene DMA channel
  540. *
  541. * This function is used on all completed and acked descriptors.
  542. */
  543. static void xgene_dma_clean_completed_descriptor(struct xgene_dma_chan *chan)
  544. {
  545. struct xgene_dma_desc_sw *desc, *_desc;
  546. /* Run the callback for each descriptor, in order */
  547. list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node) {
  548. if (async_tx_test_ack(&desc->tx))
  549. xgene_dma_clean_descriptor(chan, desc);
  550. }
  551. }
  552. /**
  553. * xgene_dma_run_tx_complete_actions - cleanup a single link descriptor
  554. * @chan: X-Gene DMA channel
  555. * @desc: descriptor to cleanup and free
  556. *
  557. * This function is used on a descriptor which has been executed by the DMA
  558. * controller. It will run any callbacks, submit any dependencies.
  559. */
  560. static void xgene_dma_run_tx_complete_actions(struct xgene_dma_chan *chan,
  561. struct xgene_dma_desc_sw *desc)
  562. {
  563. struct dma_async_tx_descriptor *tx = &desc->tx;
  564. /*
  565. * If this is not the last transaction in the group,
  566. * then no need to complete cookie and run any callback as
  567. * this is not the tx_descriptor which had been sent to caller
  568. * of this DMA request
  569. */
  570. if (tx->cookie == 0)
  571. return;
  572. dma_cookie_complete(tx);
  573. /* Run the link descriptor callback function */
  574. if (tx->callback)
  575. tx->callback(tx->callback_param);
  576. dma_descriptor_unmap(tx);
  577. /* Run any dependencies */
  578. dma_run_dependencies(tx);
  579. }
  580. /**
  581. * xgene_dma_clean_running_descriptor - move the completed descriptor from
  582. * ld_running to ld_completed
  583. * @chan: X-Gene DMA channel
  584. * @desc: the descriptor which is completed
  585. *
  586. * Free the descriptor directly if acked by async_tx api,
  587. * else move it to queue ld_completed.
  588. */
  589. static void xgene_dma_clean_running_descriptor(struct xgene_dma_chan *chan,
  590. struct xgene_dma_desc_sw *desc)
  591. {
  592. /* Remove from the list of running transactions */
  593. list_del(&desc->node);
  594. /*
  595. * the client is allowed to attach dependent operations
  596. * until 'ack' is set
  597. */
  598. if (!async_tx_test_ack(&desc->tx)) {
  599. /*
  600. * Move this descriptor to the list of descriptors which is
  601. * completed, but still awaiting the 'ack' bit to be set.
  602. */
  603. list_add_tail(&desc->node, &chan->ld_completed);
  604. return;
  605. }
  606. chan_dbg(chan, "LD %p free\n", desc);
  607. dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
  608. }
  609. static int xgene_chan_xfer_request(struct xgene_dma_ring *ring,
  610. struct xgene_dma_desc_sw *desc_sw)
  611. {
  612. struct xgene_dma_desc_hw *desc_hw;
  613. /* Check if can push more descriptor to hw for execution */
  614. if (xgene_dma_ring_desc_cnt(ring) > (ring->slots - 2))
  615. return -EBUSY;
  616. /* Get hw descriptor from DMA tx ring */
  617. desc_hw = &ring->desc_hw[ring->head];
  618. /*
  619. * Increment the head count to point next
  620. * descriptor for next time
  621. */
  622. if (++ring->head == ring->slots)
  623. ring->head = 0;
  624. /* Copy prepared sw descriptor data to hw descriptor */
  625. memcpy(desc_hw, &desc_sw->desc1, sizeof(*desc_hw));
  626. /*
  627. * Check if we have prepared 64B descriptor,
  628. * in this case we need one more hw descriptor
  629. */
  630. if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) {
  631. desc_hw = &ring->desc_hw[ring->head];
  632. if (++ring->head == ring->slots)
  633. ring->head = 0;
  634. memcpy(desc_hw, &desc_sw->desc2, sizeof(*desc_hw));
  635. }
  636. /* Notify the hw that we have descriptor ready for execution */
  637. iowrite32((desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) ?
  638. 2 : 1, ring->cmd);
  639. return 0;
  640. }
  641. /**
  642. * xgene_chan_xfer_ld_pending - push any pending transactions to hw
  643. * @chan : X-Gene DMA channel
  644. *
  645. * LOCKING: must hold chan->desc_lock
  646. */
  647. static void xgene_chan_xfer_ld_pending(struct xgene_dma_chan *chan)
  648. {
  649. struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
  650. int ret;
  651. /*
  652. * If the list of pending descriptors is empty, then we
  653. * don't need to do any work at all
  654. */
  655. if (list_empty(&chan->ld_pending)) {
  656. chan_dbg(chan, "No pending LDs\n");
  657. return;
  658. }
  659. /*
  660. * Move elements from the queue of pending transactions onto the list
  661. * of running transactions and push it to hw for further executions
  662. */
  663. list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_pending, node) {
  664. /*
  665. * Check if have pushed max number of transactions to hw
  666. * as capable, so let's stop here and will push remaining
  667. * elements from pening ld queue after completing some
  668. * descriptors that we have already pushed
  669. */
  670. if (chan->pending >= chan->max_outstanding)
  671. return;
  672. ret = xgene_chan_xfer_request(&chan->tx_ring, desc_sw);
  673. if (ret)
  674. return;
  675. /*
  676. * Delete this element from ld pending queue and append it to
  677. * ld running queue
  678. */
  679. list_move_tail(&desc_sw->node, &chan->ld_running);
  680. /* Increment the pending transaction count */
  681. chan->pending++;
  682. }
  683. }
  684. /**
  685. * xgene_dma_cleanup_descriptors - cleanup link descriptors which are completed
  686. * and move them to ld_completed to free until flag 'ack' is set
  687. * @chan: X-Gene DMA channel
  688. *
  689. * This function is used on descriptors which have been executed by the DMA
  690. * controller. It will run any callbacks, submit any dependencies, then
  691. * free these descriptors if flag 'ack' is set.
  692. */
  693. static void xgene_dma_cleanup_descriptors(struct xgene_dma_chan *chan)
  694. {
  695. struct xgene_dma_ring *ring = &chan->rx_ring;
  696. struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
  697. struct xgene_dma_desc_hw *desc_hw;
  698. u8 status;
  699. /* Clean already completed and acked descriptors */
  700. xgene_dma_clean_completed_descriptor(chan);
  701. /* Run the callback for each descriptor, in order */
  702. list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_running, node) {
  703. /* Get subsequent hw descriptor from DMA rx ring */
  704. desc_hw = &ring->desc_hw[ring->head];
  705. /* Check if this descriptor has been completed */
  706. if (unlikely(XGENE_DMA_DESC_IS_EMPTY(desc_hw)))
  707. break;
  708. if (++ring->head == ring->slots)
  709. ring->head = 0;
  710. /* Check if we have any error with DMA transactions */
  711. status = XGENE_DMA_DESC_STATUS(
  712. XGENE_DMA_DESC_ELERR_RD(le64_to_cpu(
  713. desc_hw->m0)),
  714. XGENE_DMA_DESC_LERR_RD(le64_to_cpu(
  715. desc_hw->m0)));
  716. if (status) {
  717. /* Print the DMA error type */
  718. chan_err(chan, "%s\n", xgene_dma_desc_err[status]);
  719. /*
  720. * We have DMA transactions error here. Dump DMA Tx
  721. * and Rx descriptors for this request */
  722. XGENE_DMA_DESC_DUMP(&desc_sw->desc1,
  723. "X-Gene DMA TX DESC1: ");
  724. if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC)
  725. XGENE_DMA_DESC_DUMP(&desc_sw->desc2,
  726. "X-Gene DMA TX DESC2: ");
  727. XGENE_DMA_DESC_DUMP(desc_hw,
  728. "X-Gene DMA RX ERR DESC: ");
  729. }
  730. /* Notify the hw about this completed descriptor */
  731. iowrite32(-1, ring->cmd);
  732. /* Mark this hw descriptor as processed */
  733. XGENE_DMA_DESC_SET_EMPTY(desc_hw);
  734. xgene_dma_run_tx_complete_actions(chan, desc_sw);
  735. xgene_dma_clean_running_descriptor(chan, desc_sw);
  736. /*
  737. * Decrement the pending transaction count
  738. * as we have processed one
  739. */
  740. chan->pending--;
  741. }
  742. /*
  743. * Start any pending transactions automatically
  744. * In the ideal case, we keep the DMA controller busy while we go
  745. * ahead and free the descriptors below.
  746. */
  747. xgene_chan_xfer_ld_pending(chan);
  748. }
  749. static int xgene_dma_alloc_chan_resources(struct dma_chan *dchan)
  750. {
  751. struct xgene_dma_chan *chan = to_dma_chan(dchan);
  752. /* Has this channel already been allocated? */
  753. if (chan->desc_pool)
  754. return 1;
  755. chan->desc_pool = dma_pool_create(chan->name, chan->dev,
  756. sizeof(struct xgene_dma_desc_sw),
  757. 0, 0);
  758. if (!chan->desc_pool) {
  759. chan_err(chan, "Failed to allocate descriptor pool\n");
  760. return -ENOMEM;
  761. }
  762. chan_dbg(chan, "Allocate descripto pool\n");
  763. return 1;
  764. }
  765. /**
  766. * xgene_dma_free_desc_list - Free all descriptors in a queue
  767. * @chan: X-Gene DMA channel
  768. * @list: the list to free
  769. *
  770. * LOCKING: must hold chan->desc_lock
  771. */
  772. static void xgene_dma_free_desc_list(struct xgene_dma_chan *chan,
  773. struct list_head *list)
  774. {
  775. struct xgene_dma_desc_sw *desc, *_desc;
  776. list_for_each_entry_safe(desc, _desc, list, node)
  777. xgene_dma_clean_descriptor(chan, desc);
  778. }
  779. static void xgene_dma_free_tx_desc_list(struct xgene_dma_chan *chan,
  780. struct list_head *list)
  781. {
  782. struct xgene_dma_desc_sw *desc, *_desc;
  783. list_for_each_entry_safe(desc, _desc, list, node)
  784. xgene_dma_clean_descriptor(chan, desc);
  785. }
  786. static void xgene_dma_free_chan_resources(struct dma_chan *dchan)
  787. {
  788. struct xgene_dma_chan *chan = to_dma_chan(dchan);
  789. chan_dbg(chan, "Free all resources\n");
  790. if (!chan->desc_pool)
  791. return;
  792. spin_lock_bh(&chan->lock);
  793. /* Process all running descriptor */
  794. xgene_dma_cleanup_descriptors(chan);
  795. /* Clean all link descriptor queues */
  796. xgene_dma_free_desc_list(chan, &chan->ld_pending);
  797. xgene_dma_free_desc_list(chan, &chan->ld_running);
  798. xgene_dma_free_desc_list(chan, &chan->ld_completed);
  799. spin_unlock_bh(&chan->lock);
  800. /* Delete this channel DMA pool */
  801. dma_pool_destroy(chan->desc_pool);
  802. chan->desc_pool = NULL;
  803. }
  804. static struct dma_async_tx_descriptor *xgene_dma_prep_memcpy(
  805. struct dma_chan *dchan, dma_addr_t dst, dma_addr_t src,
  806. size_t len, unsigned long flags)
  807. {
  808. struct xgene_dma_desc_sw *first = NULL, *new;
  809. struct xgene_dma_chan *chan;
  810. size_t copy;
  811. if (unlikely(!dchan || !len))
  812. return NULL;
  813. chan = to_dma_chan(dchan);
  814. do {
  815. /* Allocate the link descriptor from DMA pool */
  816. new = xgene_dma_alloc_descriptor(chan);
  817. if (!new)
  818. goto fail;
  819. /* Create the largest transaction possible */
  820. copy = min_t(size_t, len, XGENE_DMA_MAX_64B_DESC_BYTE_CNT);
  821. /* Prepare DMA descriptor */
  822. xgene_dma_prep_cpy_desc(chan, new, dst, src, copy);
  823. if (!first)
  824. first = new;
  825. new->tx.cookie = 0;
  826. async_tx_ack(&new->tx);
  827. /* Update metadata */
  828. len -= copy;
  829. dst += copy;
  830. src += copy;
  831. /* Insert the link descriptor to the LD ring */
  832. list_add_tail(&new->node, &first->tx_list);
  833. } while (len);
  834. new->tx.flags = flags; /* client is in control of this ack */
  835. new->tx.cookie = -EBUSY;
  836. list_splice(&first->tx_list, &new->tx_list);
  837. return &new->tx;
  838. fail:
  839. if (!first)
  840. return NULL;
  841. xgene_dma_free_tx_desc_list(chan, &first->tx_list);
  842. return NULL;
  843. }
  844. static struct dma_async_tx_descriptor *xgene_dma_prep_sg(
  845. struct dma_chan *dchan, struct scatterlist *dst_sg,
  846. u32 dst_nents, struct scatterlist *src_sg,
  847. u32 src_nents, unsigned long flags)
  848. {
  849. struct xgene_dma_desc_sw *first = NULL, *new = NULL;
  850. struct xgene_dma_chan *chan;
  851. size_t dst_avail, src_avail;
  852. dma_addr_t dst, src;
  853. size_t len;
  854. if (unlikely(!dchan))
  855. return NULL;
  856. if (unlikely(!dst_nents || !src_nents))
  857. return NULL;
  858. if (unlikely(!dst_sg || !src_sg))
  859. return NULL;
  860. chan = to_dma_chan(dchan);
  861. /* Get prepared for the loop */
  862. dst_avail = sg_dma_len(dst_sg);
  863. src_avail = sg_dma_len(src_sg);
  864. dst_nents--;
  865. src_nents--;
  866. /* Run until we are out of scatterlist entries */
  867. while (true) {
  868. /* Create the largest transaction possible */
  869. len = min_t(size_t, src_avail, dst_avail);
  870. len = min_t(size_t, len, XGENE_DMA_MAX_64B_DESC_BYTE_CNT);
  871. if (len == 0)
  872. goto fetch;
  873. dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) - dst_avail;
  874. src = sg_dma_address(src_sg) + sg_dma_len(src_sg) - src_avail;
  875. /* Allocate the link descriptor from DMA pool */
  876. new = xgene_dma_alloc_descriptor(chan);
  877. if (!new)
  878. goto fail;
  879. /* Prepare DMA descriptor */
  880. xgene_dma_prep_cpy_desc(chan, new, dst, src, len);
  881. if (!first)
  882. first = new;
  883. new->tx.cookie = 0;
  884. async_tx_ack(&new->tx);
  885. /* update metadata */
  886. dst_avail -= len;
  887. src_avail -= len;
  888. /* Insert the link descriptor to the LD ring */
  889. list_add_tail(&new->node, &first->tx_list);
  890. fetch:
  891. /* fetch the next dst scatterlist entry */
  892. if (dst_avail == 0) {
  893. /* no more entries: we're done */
  894. if (dst_nents == 0)
  895. break;
  896. /* fetch the next entry: if there are no more: done */
  897. dst_sg = sg_next(dst_sg);
  898. if (!dst_sg)
  899. break;
  900. dst_nents--;
  901. dst_avail = sg_dma_len(dst_sg);
  902. }
  903. /* fetch the next src scatterlist entry */
  904. if (src_avail == 0) {
  905. /* no more entries: we're done */
  906. if (src_nents == 0)
  907. break;
  908. /* fetch the next entry: if there are no more: done */
  909. src_sg = sg_next(src_sg);
  910. if (!src_sg)
  911. break;
  912. src_nents--;
  913. src_avail = sg_dma_len(src_sg);
  914. }
  915. }
  916. if (!new)
  917. return NULL;
  918. new->tx.flags = flags; /* client is in control of this ack */
  919. new->tx.cookie = -EBUSY;
  920. list_splice(&first->tx_list, &new->tx_list);
  921. return &new->tx;
  922. fail:
  923. if (!first)
  924. return NULL;
  925. xgene_dma_free_tx_desc_list(chan, &first->tx_list);
  926. return NULL;
  927. }
  928. static struct dma_async_tx_descriptor *xgene_dma_prep_xor(
  929. struct dma_chan *dchan, dma_addr_t dst, dma_addr_t *src,
  930. u32 src_cnt, size_t len, unsigned long flags)
  931. {
  932. struct xgene_dma_desc_sw *first = NULL, *new;
  933. struct xgene_dma_chan *chan;
  934. static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {
  935. 0x01, 0x01, 0x01, 0x01, 0x01};
  936. if (unlikely(!dchan || !len))
  937. return NULL;
  938. chan = to_dma_chan(dchan);
  939. do {
  940. /* Allocate the link descriptor from DMA pool */
  941. new = xgene_dma_alloc_descriptor(chan);
  942. if (!new)
  943. goto fail;
  944. /* Prepare xor DMA descriptor */
  945. xgene_dma_prep_xor_desc(chan, new, &dst, src,
  946. src_cnt, &len, multi);
  947. if (!first)
  948. first = new;
  949. new->tx.cookie = 0;
  950. async_tx_ack(&new->tx);
  951. /* Insert the link descriptor to the LD ring */
  952. list_add_tail(&new->node, &first->tx_list);
  953. } while (len);
  954. new->tx.flags = flags; /* client is in control of this ack */
  955. new->tx.cookie = -EBUSY;
  956. list_splice(&first->tx_list, &new->tx_list);
  957. return &new->tx;
  958. fail:
  959. if (!first)
  960. return NULL;
  961. xgene_dma_free_tx_desc_list(chan, &first->tx_list);
  962. return NULL;
  963. }
  964. static struct dma_async_tx_descriptor *xgene_dma_prep_pq(
  965. struct dma_chan *dchan, dma_addr_t *dst, dma_addr_t *src,
  966. u32 src_cnt, const u8 *scf, size_t len, unsigned long flags)
  967. {
  968. struct xgene_dma_desc_sw *first = NULL, *new;
  969. struct xgene_dma_chan *chan;
  970. size_t _len = len;
  971. dma_addr_t _src[XGENE_DMA_MAX_XOR_SRC];
  972. static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {0x01, 0x01, 0x01, 0x01, 0x01};
  973. if (unlikely(!dchan || !len))
  974. return NULL;
  975. chan = to_dma_chan(dchan);
  976. /*
  977. * Save source addresses on local variable, may be we have to
  978. * prepare two descriptor to generate P and Q if both enabled
  979. * in the flags by client
  980. */
  981. memcpy(_src, src, sizeof(*src) * src_cnt);
  982. if (flags & DMA_PREP_PQ_DISABLE_P)
  983. len = 0;
  984. if (flags & DMA_PREP_PQ_DISABLE_Q)
  985. _len = 0;
  986. do {
  987. /* Allocate the link descriptor from DMA pool */
  988. new = xgene_dma_alloc_descriptor(chan);
  989. if (!new)
  990. goto fail;
  991. if (!first)
  992. first = new;
  993. new->tx.cookie = 0;
  994. async_tx_ack(&new->tx);
  995. /* Insert the link descriptor to the LD ring */
  996. list_add_tail(&new->node, &first->tx_list);
  997. /*
  998. * Prepare DMA descriptor to generate P,
  999. * if DMA_PREP_PQ_DISABLE_P flag is not set
  1000. */
  1001. if (len) {
  1002. xgene_dma_prep_xor_desc(chan, new, &dst[0], src,
  1003. src_cnt, &len, multi);
  1004. continue;
  1005. }
  1006. /*
  1007. * Prepare DMA descriptor to generate Q,
  1008. * if DMA_PREP_PQ_DISABLE_Q flag is not set
  1009. */
  1010. if (_len) {
  1011. xgene_dma_prep_xor_desc(chan, new, &dst[1], _src,
  1012. src_cnt, &_len, scf);
  1013. }
  1014. } while (len || _len);
  1015. new->tx.flags = flags; /* client is in control of this ack */
  1016. new->tx.cookie = -EBUSY;
  1017. list_splice(&first->tx_list, &new->tx_list);
  1018. return &new->tx;
  1019. fail:
  1020. if (!first)
  1021. return NULL;
  1022. xgene_dma_free_tx_desc_list(chan, &first->tx_list);
  1023. return NULL;
  1024. }
  1025. static void xgene_dma_issue_pending(struct dma_chan *dchan)
  1026. {
  1027. struct xgene_dma_chan *chan = to_dma_chan(dchan);
  1028. spin_lock_bh(&chan->lock);
  1029. xgene_chan_xfer_ld_pending(chan);
  1030. spin_unlock_bh(&chan->lock);
  1031. }
  1032. static enum dma_status xgene_dma_tx_status(struct dma_chan *dchan,
  1033. dma_cookie_t cookie,
  1034. struct dma_tx_state *txstate)
  1035. {
  1036. return dma_cookie_status(dchan, cookie, txstate);
  1037. }
  1038. static void xgene_dma_tasklet_cb(unsigned long data)
  1039. {
  1040. struct xgene_dma_chan *chan = (struct xgene_dma_chan *)data;
  1041. spin_lock_bh(&chan->lock);
  1042. /* Run all cleanup for descriptors which have been completed */
  1043. xgene_dma_cleanup_descriptors(chan);
  1044. /* Re-enable DMA channel IRQ */
  1045. enable_irq(chan->rx_irq);
  1046. spin_unlock_bh(&chan->lock);
  1047. }
  1048. static irqreturn_t xgene_dma_chan_ring_isr(int irq, void *id)
  1049. {
  1050. struct xgene_dma_chan *chan = (struct xgene_dma_chan *)id;
  1051. BUG_ON(!chan);
  1052. /*
  1053. * Disable DMA channel IRQ until we process completed
  1054. * descriptors
  1055. */
  1056. disable_irq_nosync(chan->rx_irq);
  1057. /*
  1058. * Schedule the tasklet to handle all cleanup of the current
  1059. * transaction. It will start a new transaction if there is
  1060. * one pending.
  1061. */
  1062. tasklet_schedule(&chan->tasklet);
  1063. return IRQ_HANDLED;
  1064. }
  1065. static irqreturn_t xgene_dma_err_isr(int irq, void *id)
  1066. {
  1067. struct xgene_dma *pdma = (struct xgene_dma *)id;
  1068. unsigned long int_mask;
  1069. u32 val, i;
  1070. val = ioread32(pdma->csr_dma + XGENE_DMA_INT);
  1071. /* Clear DMA interrupts */
  1072. iowrite32(val, pdma->csr_dma + XGENE_DMA_INT);
  1073. /* Print DMA error info */
  1074. int_mask = val >> XGENE_DMA_INT_MASK_SHIFT;
  1075. for_each_set_bit(i, &int_mask, ARRAY_SIZE(xgene_dma_err))
  1076. dev_err(pdma->dev,
  1077. "Interrupt status 0x%08X %s\n", val, xgene_dma_err[i]);
  1078. return IRQ_HANDLED;
  1079. }
  1080. static void xgene_dma_wr_ring_state(struct xgene_dma_ring *ring)
  1081. {
  1082. int i;
  1083. iowrite32(ring->num, ring->pdma->csr_ring + XGENE_DMA_RING_STATE);
  1084. for (i = 0; i < XGENE_DMA_RING_NUM_CONFIG; i++)
  1085. iowrite32(ring->state[i], ring->pdma->csr_ring +
  1086. XGENE_DMA_RING_STATE_WR_BASE + (i * 4));
  1087. }
  1088. static void xgene_dma_clr_ring_state(struct xgene_dma_ring *ring)
  1089. {
  1090. memset(ring->state, 0, sizeof(u32) * XGENE_DMA_RING_NUM_CONFIG);
  1091. xgene_dma_wr_ring_state(ring);
  1092. }
  1093. static void xgene_dma_setup_ring(struct xgene_dma_ring *ring)
  1094. {
  1095. void *ring_cfg = ring->state;
  1096. u64 addr = ring->desc_paddr;
  1097. void *desc;
  1098. u32 i, val;
  1099. ring->slots = ring->size / XGENE_DMA_RING_WQ_DESC_SIZE;
  1100. /* Clear DMA ring state */
  1101. xgene_dma_clr_ring_state(ring);
  1102. /* Set DMA ring type */
  1103. XGENE_DMA_RING_TYPE_SET(ring_cfg, XGENE_DMA_RING_TYPE_REGULAR);
  1104. if (ring->owner == XGENE_DMA_RING_OWNER_DMA) {
  1105. /* Set recombination buffer and timeout */
  1106. XGENE_DMA_RING_RECOMBBUF_SET(ring_cfg);
  1107. XGENE_DMA_RING_RECOMTIMEOUTL_SET(ring_cfg);
  1108. XGENE_DMA_RING_RECOMTIMEOUTH_SET(ring_cfg);
  1109. }
  1110. /* Initialize DMA ring state */
  1111. XGENE_DMA_RING_SELTHRSH_SET(ring_cfg);
  1112. XGENE_DMA_RING_ACCEPTLERR_SET(ring_cfg);
  1113. XGENE_DMA_RING_COHERENT_SET(ring_cfg);
  1114. XGENE_DMA_RING_ADDRL_SET(ring_cfg, addr);
  1115. XGENE_DMA_RING_ADDRH_SET(ring_cfg, addr);
  1116. XGENE_DMA_RING_SIZE_SET(ring_cfg, ring->cfgsize);
  1117. /* Write DMA ring configurations */
  1118. xgene_dma_wr_ring_state(ring);
  1119. /* Set DMA ring id */
  1120. iowrite32(XGENE_DMA_RING_ID_SETUP(ring->id),
  1121. ring->pdma->csr_ring + XGENE_DMA_RING_ID);
  1122. /* Set DMA ring buffer */
  1123. iowrite32(XGENE_DMA_RING_ID_BUF_SETUP(ring->num),
  1124. ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
  1125. if (ring->owner != XGENE_DMA_RING_OWNER_CPU)
  1126. return;
  1127. /* Set empty signature to DMA Rx ring descriptors */
  1128. for (i = 0; i < ring->slots; i++) {
  1129. desc = &ring->desc_hw[i];
  1130. XGENE_DMA_DESC_SET_EMPTY(desc);
  1131. }
  1132. /* Enable DMA Rx ring interrupt */
  1133. val = ioread32(ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
  1134. XGENE_DMA_RING_NE_INT_MODE_SET(val, ring->buf_num);
  1135. iowrite32(val, ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
  1136. }
  1137. static void xgene_dma_clear_ring(struct xgene_dma_ring *ring)
  1138. {
  1139. u32 ring_id, val;
  1140. if (ring->owner == XGENE_DMA_RING_OWNER_CPU) {
  1141. /* Disable DMA Rx ring interrupt */
  1142. val = ioread32(ring->pdma->csr_ring +
  1143. XGENE_DMA_RING_NE_INT_MODE);
  1144. XGENE_DMA_RING_NE_INT_MODE_RESET(val, ring->buf_num);
  1145. iowrite32(val, ring->pdma->csr_ring +
  1146. XGENE_DMA_RING_NE_INT_MODE);
  1147. }
  1148. /* Clear DMA ring state */
  1149. ring_id = XGENE_DMA_RING_ID_SETUP(ring->id);
  1150. iowrite32(ring_id, ring->pdma->csr_ring + XGENE_DMA_RING_ID);
  1151. iowrite32(0, ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
  1152. xgene_dma_clr_ring_state(ring);
  1153. }
  1154. static void xgene_dma_set_ring_cmd(struct xgene_dma_ring *ring)
  1155. {
  1156. ring->cmd_base = ring->pdma->csr_ring_cmd +
  1157. XGENE_DMA_RING_CMD_BASE_OFFSET((ring->num -
  1158. XGENE_DMA_RING_NUM));
  1159. ring->cmd = ring->cmd_base + XGENE_DMA_RING_CMD_OFFSET;
  1160. }
  1161. static int xgene_dma_get_ring_size(struct xgene_dma_chan *chan,
  1162. enum xgene_dma_ring_cfgsize cfgsize)
  1163. {
  1164. int size;
  1165. switch (cfgsize) {
  1166. case XGENE_DMA_RING_CFG_SIZE_512B:
  1167. size = 0x200;
  1168. break;
  1169. case XGENE_DMA_RING_CFG_SIZE_2KB:
  1170. size = 0x800;
  1171. break;
  1172. case XGENE_DMA_RING_CFG_SIZE_16KB:
  1173. size = 0x4000;
  1174. break;
  1175. case XGENE_DMA_RING_CFG_SIZE_64KB:
  1176. size = 0x10000;
  1177. break;
  1178. case XGENE_DMA_RING_CFG_SIZE_512KB:
  1179. size = 0x80000;
  1180. break;
  1181. default:
  1182. chan_err(chan, "Unsupported cfg ring size %d\n", cfgsize);
  1183. return -EINVAL;
  1184. }
  1185. return size;
  1186. }
  1187. static void xgene_dma_delete_ring_one(struct xgene_dma_ring *ring)
  1188. {
  1189. /* Clear DMA ring configurations */
  1190. xgene_dma_clear_ring(ring);
  1191. /* De-allocate DMA ring descriptor */
  1192. if (ring->desc_vaddr) {
  1193. dma_free_coherent(ring->pdma->dev, ring->size,
  1194. ring->desc_vaddr, ring->desc_paddr);
  1195. ring->desc_vaddr = NULL;
  1196. }
  1197. }
  1198. static void xgene_dma_delete_chan_rings(struct xgene_dma_chan *chan)
  1199. {
  1200. xgene_dma_delete_ring_one(&chan->rx_ring);
  1201. xgene_dma_delete_ring_one(&chan->tx_ring);
  1202. }
  1203. static int xgene_dma_create_ring_one(struct xgene_dma_chan *chan,
  1204. struct xgene_dma_ring *ring,
  1205. enum xgene_dma_ring_cfgsize cfgsize)
  1206. {
  1207. /* Setup DMA ring descriptor variables */
  1208. ring->pdma = chan->pdma;
  1209. ring->cfgsize = cfgsize;
  1210. ring->num = chan->pdma->ring_num++;
  1211. ring->id = XGENE_DMA_RING_ID_GET(ring->owner, ring->buf_num);
  1212. ring->size = xgene_dma_get_ring_size(chan, cfgsize);
  1213. if (ring->size <= 0)
  1214. return ring->size;
  1215. /* Allocate memory for DMA ring descriptor */
  1216. ring->desc_vaddr = dma_zalloc_coherent(chan->dev, ring->size,
  1217. &ring->desc_paddr, GFP_KERNEL);
  1218. if (!ring->desc_vaddr) {
  1219. chan_err(chan, "Failed to allocate ring desc\n");
  1220. return -ENOMEM;
  1221. }
  1222. /* Configure and enable DMA ring */
  1223. xgene_dma_set_ring_cmd(ring);
  1224. xgene_dma_setup_ring(ring);
  1225. return 0;
  1226. }
  1227. static int xgene_dma_create_chan_rings(struct xgene_dma_chan *chan)
  1228. {
  1229. struct xgene_dma_ring *rx_ring = &chan->rx_ring;
  1230. struct xgene_dma_ring *tx_ring = &chan->tx_ring;
  1231. int ret;
  1232. /* Create DMA Rx ring descriptor */
  1233. rx_ring->owner = XGENE_DMA_RING_OWNER_CPU;
  1234. rx_ring->buf_num = XGENE_DMA_CPU_BUFNUM + chan->id;
  1235. ret = xgene_dma_create_ring_one(chan, rx_ring,
  1236. XGENE_DMA_RING_CFG_SIZE_64KB);
  1237. if (ret)
  1238. return ret;
  1239. chan_dbg(chan, "Rx ring id 0x%X num %d desc 0x%p\n",
  1240. rx_ring->id, rx_ring->num, rx_ring->desc_vaddr);
  1241. /* Create DMA Tx ring descriptor */
  1242. tx_ring->owner = XGENE_DMA_RING_OWNER_DMA;
  1243. tx_ring->buf_num = XGENE_DMA_BUFNUM + chan->id;
  1244. ret = xgene_dma_create_ring_one(chan, tx_ring,
  1245. XGENE_DMA_RING_CFG_SIZE_64KB);
  1246. if (ret) {
  1247. xgene_dma_delete_ring_one(rx_ring);
  1248. return ret;
  1249. }
  1250. tx_ring->dst_ring_num = XGENE_DMA_RING_DST_ID(rx_ring->num);
  1251. chan_dbg(chan,
  1252. "Tx ring id 0x%X num %d desc 0x%p\n",
  1253. tx_ring->id, tx_ring->num, tx_ring->desc_vaddr);
  1254. /* Set the max outstanding request possible to this channel */
  1255. chan->max_outstanding = rx_ring->slots;
  1256. return ret;
  1257. }
  1258. static int xgene_dma_init_rings(struct xgene_dma *pdma)
  1259. {
  1260. int ret, i, j;
  1261. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1262. ret = xgene_dma_create_chan_rings(&pdma->chan[i]);
  1263. if (ret) {
  1264. for (j = 0; j < i; j++)
  1265. xgene_dma_delete_chan_rings(&pdma->chan[j]);
  1266. return ret;
  1267. }
  1268. }
  1269. return ret;
  1270. }
  1271. static void xgene_dma_enable(struct xgene_dma *pdma)
  1272. {
  1273. u32 val;
  1274. /* Configure and enable DMA engine */
  1275. val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
  1276. XGENE_DMA_CH_SETUP(val);
  1277. XGENE_DMA_ENABLE(val);
  1278. iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
  1279. }
  1280. static void xgene_dma_disable(struct xgene_dma *pdma)
  1281. {
  1282. u32 val;
  1283. val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
  1284. XGENE_DMA_DISABLE(val);
  1285. iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
  1286. }
  1287. static void xgene_dma_mask_interrupts(struct xgene_dma *pdma)
  1288. {
  1289. /*
  1290. * Mask DMA ring overflow, underflow and
  1291. * AXI write/read error interrupts
  1292. */
  1293. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1294. pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
  1295. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1296. pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
  1297. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1298. pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
  1299. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1300. pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
  1301. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1302. pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
  1303. /* Mask DMA error interrupts */
  1304. iowrite32(XGENE_DMA_INT_ALL_MASK, pdma->csr_dma + XGENE_DMA_INT_MASK);
  1305. }
  1306. static void xgene_dma_unmask_interrupts(struct xgene_dma *pdma)
  1307. {
  1308. /*
  1309. * Unmask DMA ring overflow, underflow and
  1310. * AXI write/read error interrupts
  1311. */
  1312. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1313. pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
  1314. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1315. pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
  1316. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1317. pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
  1318. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1319. pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
  1320. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1321. pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
  1322. /* Unmask DMA error interrupts */
  1323. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1324. pdma->csr_dma + XGENE_DMA_INT_MASK);
  1325. }
  1326. static void xgene_dma_init_hw(struct xgene_dma *pdma)
  1327. {
  1328. u32 val;
  1329. /* Associate DMA ring to corresponding ring HW */
  1330. iowrite32(XGENE_DMA_ASSOC_RING_MNGR1,
  1331. pdma->csr_dma + XGENE_DMA_CFG_RING_WQ_ASSOC);
  1332. /* Configure RAID6 polynomial control setting */
  1333. if (is_pq_enabled(pdma))
  1334. iowrite32(XGENE_DMA_RAID6_MULTI_CTRL(0x1D),
  1335. pdma->csr_dma + XGENE_DMA_RAID6_CONT);
  1336. else
  1337. dev_info(pdma->dev, "PQ is disabled in HW\n");
  1338. xgene_dma_enable(pdma);
  1339. xgene_dma_unmask_interrupts(pdma);
  1340. /* Get DMA id and version info */
  1341. val = ioread32(pdma->csr_dma + XGENE_DMA_IPBRR);
  1342. /* DMA device info */
  1343. dev_info(pdma->dev,
  1344. "X-Gene DMA v%d.%02d.%02d driver registered %d channels",
  1345. XGENE_DMA_REV_NO_RD(val), XGENE_DMA_BUS_ID_RD(val),
  1346. XGENE_DMA_DEV_ID_RD(val), XGENE_DMA_MAX_CHANNEL);
  1347. }
  1348. static int xgene_dma_init_ring_mngr(struct xgene_dma *pdma)
  1349. {
  1350. if (ioread32(pdma->csr_ring + XGENE_DMA_RING_CLKEN) &&
  1351. (!ioread32(pdma->csr_ring + XGENE_DMA_RING_SRST)))
  1352. return 0;
  1353. iowrite32(0x3, pdma->csr_ring + XGENE_DMA_RING_CLKEN);
  1354. iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_SRST);
  1355. /* Bring up memory */
  1356. iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
  1357. /* Force a barrier */
  1358. ioread32(pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
  1359. /* reset may take up to 1ms */
  1360. usleep_range(1000, 1100);
  1361. if (ioread32(pdma->csr_ring + XGENE_DMA_RING_BLK_MEM_RDY)
  1362. != XGENE_DMA_RING_BLK_MEM_RDY_VAL) {
  1363. dev_err(pdma->dev,
  1364. "Failed to release ring mngr memory from shutdown\n");
  1365. return -ENODEV;
  1366. }
  1367. /* program threshold set 1 and all hysteresis */
  1368. iowrite32(XGENE_DMA_RING_THRESLD0_SET1_VAL,
  1369. pdma->csr_ring + XGENE_DMA_RING_THRESLD0_SET1);
  1370. iowrite32(XGENE_DMA_RING_THRESLD1_SET1_VAL,
  1371. pdma->csr_ring + XGENE_DMA_RING_THRESLD1_SET1);
  1372. iowrite32(XGENE_DMA_RING_HYSTERESIS_VAL,
  1373. pdma->csr_ring + XGENE_DMA_RING_HYSTERESIS);
  1374. /* Enable QPcore and assign error queue */
  1375. iowrite32(XGENE_DMA_RING_ENABLE,
  1376. pdma->csr_ring + XGENE_DMA_RING_CONFIG);
  1377. return 0;
  1378. }
  1379. static int xgene_dma_init_mem(struct xgene_dma *pdma)
  1380. {
  1381. int ret;
  1382. ret = xgene_dma_init_ring_mngr(pdma);
  1383. if (ret)
  1384. return ret;
  1385. /* Bring up memory */
  1386. iowrite32(0x0, pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
  1387. /* Force a barrier */
  1388. ioread32(pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
  1389. /* reset may take up to 1ms */
  1390. usleep_range(1000, 1100);
  1391. if (ioread32(pdma->csr_dma + XGENE_DMA_BLK_MEM_RDY)
  1392. != XGENE_DMA_BLK_MEM_RDY_VAL) {
  1393. dev_err(pdma->dev,
  1394. "Failed to release DMA memory from shutdown\n");
  1395. return -ENODEV;
  1396. }
  1397. return 0;
  1398. }
  1399. static int xgene_dma_request_irqs(struct xgene_dma *pdma)
  1400. {
  1401. struct xgene_dma_chan *chan;
  1402. int ret, i, j;
  1403. /* Register DMA error irq */
  1404. ret = devm_request_irq(pdma->dev, pdma->err_irq, xgene_dma_err_isr,
  1405. 0, "dma_error", pdma);
  1406. if (ret) {
  1407. dev_err(pdma->dev,
  1408. "Failed to register error IRQ %d\n", pdma->err_irq);
  1409. return ret;
  1410. }
  1411. /* Register DMA channel rx irq */
  1412. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1413. chan = &pdma->chan[i];
  1414. ret = devm_request_irq(chan->dev, chan->rx_irq,
  1415. xgene_dma_chan_ring_isr,
  1416. 0, chan->name, chan);
  1417. if (ret) {
  1418. chan_err(chan, "Failed to register Rx IRQ %d\n",
  1419. chan->rx_irq);
  1420. devm_free_irq(pdma->dev, pdma->err_irq, pdma);
  1421. for (j = 0; j < i; j++) {
  1422. chan = &pdma->chan[i];
  1423. devm_free_irq(chan->dev, chan->rx_irq, chan);
  1424. }
  1425. return ret;
  1426. }
  1427. }
  1428. return 0;
  1429. }
  1430. static void xgene_dma_free_irqs(struct xgene_dma *pdma)
  1431. {
  1432. struct xgene_dma_chan *chan;
  1433. int i;
  1434. /* Free DMA device error irq */
  1435. devm_free_irq(pdma->dev, pdma->err_irq, pdma);
  1436. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1437. chan = &pdma->chan[i];
  1438. devm_free_irq(chan->dev, chan->rx_irq, chan);
  1439. }
  1440. }
  1441. static void xgene_dma_set_caps(struct xgene_dma_chan *chan,
  1442. struct dma_device *dma_dev)
  1443. {
  1444. /* Initialize DMA device capability mask */
  1445. dma_cap_zero(dma_dev->cap_mask);
  1446. /* Set DMA device capability */
  1447. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  1448. dma_cap_set(DMA_SG, dma_dev->cap_mask);
  1449. /* Basically here, the X-Gene SoC DMA engine channel 0 supports XOR
  1450. * and channel 1 supports XOR, PQ both. First thing here is we have
  1451. * mechanism in hw to enable/disable PQ/XOR supports on channel 1,
  1452. * we can make sure this by reading SoC Efuse register.
  1453. * Second thing, we have hw errata that if we run channel 0 and
  1454. * channel 1 simultaneously with executing XOR and PQ request,
  1455. * suddenly DMA engine hangs, So here we enable XOR on channel 0 only
  1456. * if XOR and PQ supports on channel 1 is disabled.
  1457. */
  1458. if ((chan->id == XGENE_DMA_PQ_CHANNEL) &&
  1459. is_pq_enabled(chan->pdma)) {
  1460. dma_cap_set(DMA_PQ, dma_dev->cap_mask);
  1461. dma_cap_set(DMA_XOR, dma_dev->cap_mask);
  1462. } else if ((chan->id == XGENE_DMA_XOR_CHANNEL) &&
  1463. !is_pq_enabled(chan->pdma)) {
  1464. dma_cap_set(DMA_XOR, dma_dev->cap_mask);
  1465. }
  1466. /* Set base and prep routines */
  1467. dma_dev->dev = chan->dev;
  1468. dma_dev->device_alloc_chan_resources = xgene_dma_alloc_chan_resources;
  1469. dma_dev->device_free_chan_resources = xgene_dma_free_chan_resources;
  1470. dma_dev->device_issue_pending = xgene_dma_issue_pending;
  1471. dma_dev->device_tx_status = xgene_dma_tx_status;
  1472. dma_dev->device_prep_dma_memcpy = xgene_dma_prep_memcpy;
  1473. dma_dev->device_prep_dma_sg = xgene_dma_prep_sg;
  1474. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  1475. dma_dev->device_prep_dma_xor = xgene_dma_prep_xor;
  1476. dma_dev->max_xor = XGENE_DMA_MAX_XOR_SRC;
  1477. dma_dev->xor_align = XGENE_DMA_XOR_ALIGNMENT;
  1478. }
  1479. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  1480. dma_dev->device_prep_dma_pq = xgene_dma_prep_pq;
  1481. dma_dev->max_pq = XGENE_DMA_MAX_XOR_SRC;
  1482. dma_dev->pq_align = XGENE_DMA_XOR_ALIGNMENT;
  1483. }
  1484. }
  1485. static int xgene_dma_async_register(struct xgene_dma *pdma, int id)
  1486. {
  1487. struct xgene_dma_chan *chan = &pdma->chan[id];
  1488. struct dma_device *dma_dev = &pdma->dma_dev[id];
  1489. int ret;
  1490. chan->dma_chan.device = dma_dev;
  1491. spin_lock_init(&chan->lock);
  1492. INIT_LIST_HEAD(&chan->ld_pending);
  1493. INIT_LIST_HEAD(&chan->ld_running);
  1494. INIT_LIST_HEAD(&chan->ld_completed);
  1495. tasklet_init(&chan->tasklet, xgene_dma_tasklet_cb,
  1496. (unsigned long)chan);
  1497. chan->pending = 0;
  1498. chan->desc_pool = NULL;
  1499. dma_cookie_init(&chan->dma_chan);
  1500. /* Setup dma device capabilities and prep routines */
  1501. xgene_dma_set_caps(chan, dma_dev);
  1502. /* Initialize DMA device list head */
  1503. INIT_LIST_HEAD(&dma_dev->channels);
  1504. list_add_tail(&chan->dma_chan.device_node, &dma_dev->channels);
  1505. /* Register with Linux async DMA framework*/
  1506. ret = dma_async_device_register(dma_dev);
  1507. if (ret) {
  1508. chan_err(chan, "Failed to register async device %d", ret);
  1509. tasklet_kill(&chan->tasklet);
  1510. return ret;
  1511. }
  1512. /* DMA capability info */
  1513. dev_info(pdma->dev,
  1514. "%s: CAPABILITY ( %s%s%s%s)\n", dma_chan_name(&chan->dma_chan),
  1515. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "MEMCPY " : "",
  1516. dma_has_cap(DMA_SG, dma_dev->cap_mask) ? "SGCPY " : "",
  1517. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "XOR " : "",
  1518. dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "PQ " : "");
  1519. return 0;
  1520. }
  1521. static int xgene_dma_init_async(struct xgene_dma *pdma)
  1522. {
  1523. int ret, i, j;
  1524. for (i = 0; i < XGENE_DMA_MAX_CHANNEL ; i++) {
  1525. ret = xgene_dma_async_register(pdma, i);
  1526. if (ret) {
  1527. for (j = 0; j < i; j++) {
  1528. dma_async_device_unregister(&pdma->dma_dev[j]);
  1529. tasklet_kill(&pdma->chan[j].tasklet);
  1530. }
  1531. return ret;
  1532. }
  1533. }
  1534. return ret;
  1535. }
  1536. static void xgene_dma_async_unregister(struct xgene_dma *pdma)
  1537. {
  1538. int i;
  1539. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
  1540. dma_async_device_unregister(&pdma->dma_dev[i]);
  1541. }
  1542. static void xgene_dma_init_channels(struct xgene_dma *pdma)
  1543. {
  1544. struct xgene_dma_chan *chan;
  1545. int i;
  1546. pdma->ring_num = XGENE_DMA_RING_NUM;
  1547. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1548. chan = &pdma->chan[i];
  1549. chan->dev = pdma->dev;
  1550. chan->pdma = pdma;
  1551. chan->id = i;
  1552. snprintf(chan->name, sizeof(chan->name), "dmachan%d", chan->id);
  1553. }
  1554. }
  1555. static int xgene_dma_get_resources(struct platform_device *pdev,
  1556. struct xgene_dma *pdma)
  1557. {
  1558. struct resource *res;
  1559. int irq, i;
  1560. /* Get DMA csr region */
  1561. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1562. if (!res) {
  1563. dev_err(&pdev->dev, "Failed to get csr region\n");
  1564. return -ENXIO;
  1565. }
  1566. pdma->csr_dma = devm_ioremap(&pdev->dev, res->start,
  1567. resource_size(res));
  1568. if (!pdma->csr_dma) {
  1569. dev_err(&pdev->dev, "Failed to ioremap csr region");
  1570. return -ENOMEM;
  1571. }
  1572. /* Get DMA ring csr region */
  1573. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1574. if (!res) {
  1575. dev_err(&pdev->dev, "Failed to get ring csr region\n");
  1576. return -ENXIO;
  1577. }
  1578. pdma->csr_ring = devm_ioremap(&pdev->dev, res->start,
  1579. resource_size(res));
  1580. if (!pdma->csr_ring) {
  1581. dev_err(&pdev->dev, "Failed to ioremap ring csr region");
  1582. return -ENOMEM;
  1583. }
  1584. /* Get DMA ring cmd csr region */
  1585. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1586. if (!res) {
  1587. dev_err(&pdev->dev, "Failed to get ring cmd csr region\n");
  1588. return -ENXIO;
  1589. }
  1590. pdma->csr_ring_cmd = devm_ioremap(&pdev->dev, res->start,
  1591. resource_size(res));
  1592. if (!pdma->csr_ring_cmd) {
  1593. dev_err(&pdev->dev, "Failed to ioremap ring cmd csr region");
  1594. return -ENOMEM;
  1595. }
  1596. /* Get efuse csr region */
  1597. res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  1598. if (!res) {
  1599. dev_err(&pdev->dev, "Failed to get efuse csr region\n");
  1600. return -ENXIO;
  1601. }
  1602. pdma->csr_efuse = devm_ioremap(&pdev->dev, res->start,
  1603. resource_size(res));
  1604. if (!pdma->csr_efuse) {
  1605. dev_err(&pdev->dev, "Failed to ioremap efuse csr region");
  1606. return -ENOMEM;
  1607. }
  1608. /* Get DMA error interrupt */
  1609. irq = platform_get_irq(pdev, 0);
  1610. if (irq <= 0) {
  1611. dev_err(&pdev->dev, "Failed to get Error IRQ\n");
  1612. return -ENXIO;
  1613. }
  1614. pdma->err_irq = irq;
  1615. /* Get DMA Rx ring descriptor interrupts for all DMA channels */
  1616. for (i = 1; i <= XGENE_DMA_MAX_CHANNEL; i++) {
  1617. irq = platform_get_irq(pdev, i);
  1618. if (irq <= 0) {
  1619. dev_err(&pdev->dev, "Failed to get Rx IRQ\n");
  1620. return -ENXIO;
  1621. }
  1622. pdma->chan[i - 1].rx_irq = irq;
  1623. }
  1624. return 0;
  1625. }
  1626. static int xgene_dma_probe(struct platform_device *pdev)
  1627. {
  1628. struct xgene_dma *pdma;
  1629. int ret, i;
  1630. pdma = devm_kzalloc(&pdev->dev, sizeof(*pdma), GFP_KERNEL);
  1631. if (!pdma)
  1632. return -ENOMEM;
  1633. pdma->dev = &pdev->dev;
  1634. platform_set_drvdata(pdev, pdma);
  1635. ret = xgene_dma_get_resources(pdev, pdma);
  1636. if (ret)
  1637. return ret;
  1638. pdma->clk = devm_clk_get(&pdev->dev, NULL);
  1639. if (IS_ERR(pdma->clk)) {
  1640. dev_err(&pdev->dev, "Failed to get clk\n");
  1641. return PTR_ERR(pdma->clk);
  1642. }
  1643. /* Enable clk before accessing registers */
  1644. ret = clk_prepare_enable(pdma->clk);
  1645. if (ret) {
  1646. dev_err(&pdev->dev, "Failed to enable clk %d\n", ret);
  1647. return ret;
  1648. }
  1649. /* Remove DMA RAM out of shutdown */
  1650. ret = xgene_dma_init_mem(pdma);
  1651. if (ret)
  1652. goto err_clk_enable;
  1653. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(42));
  1654. if (ret) {
  1655. dev_err(&pdev->dev, "No usable DMA configuration\n");
  1656. goto err_dma_mask;
  1657. }
  1658. /* Initialize DMA channels software state */
  1659. xgene_dma_init_channels(pdma);
  1660. /* Configue DMA rings */
  1661. ret = xgene_dma_init_rings(pdma);
  1662. if (ret)
  1663. goto err_clk_enable;
  1664. ret = xgene_dma_request_irqs(pdma);
  1665. if (ret)
  1666. goto err_request_irq;
  1667. /* Configure and enable DMA engine */
  1668. xgene_dma_init_hw(pdma);
  1669. /* Register DMA device with linux async framework */
  1670. ret = xgene_dma_init_async(pdma);
  1671. if (ret)
  1672. goto err_async_init;
  1673. return 0;
  1674. err_async_init:
  1675. xgene_dma_free_irqs(pdma);
  1676. err_request_irq:
  1677. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
  1678. xgene_dma_delete_chan_rings(&pdma->chan[i]);
  1679. err_dma_mask:
  1680. err_clk_enable:
  1681. clk_disable_unprepare(pdma->clk);
  1682. return ret;
  1683. }
  1684. static int xgene_dma_remove(struct platform_device *pdev)
  1685. {
  1686. struct xgene_dma *pdma = platform_get_drvdata(pdev);
  1687. struct xgene_dma_chan *chan;
  1688. int i;
  1689. xgene_dma_async_unregister(pdma);
  1690. /* Mask interrupts and disable DMA engine */
  1691. xgene_dma_mask_interrupts(pdma);
  1692. xgene_dma_disable(pdma);
  1693. xgene_dma_free_irqs(pdma);
  1694. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1695. chan = &pdma->chan[i];
  1696. tasklet_kill(&chan->tasklet);
  1697. xgene_dma_delete_chan_rings(chan);
  1698. }
  1699. clk_disable_unprepare(pdma->clk);
  1700. return 0;
  1701. }
  1702. static const struct of_device_id xgene_dma_of_match_ptr[] = {
  1703. {.compatible = "apm,xgene-storm-dma",},
  1704. {},
  1705. };
  1706. MODULE_DEVICE_TABLE(of, xgene_dma_of_match_ptr);
  1707. static struct platform_driver xgene_dma_driver = {
  1708. .probe = xgene_dma_probe,
  1709. .remove = xgene_dma_remove,
  1710. .driver = {
  1711. .name = "X-Gene-DMA",
  1712. .of_match_table = xgene_dma_of_match_ptr,
  1713. },
  1714. };
  1715. module_platform_driver(xgene_dma_driver);
  1716. MODULE_DESCRIPTION("APM X-Gene SoC DMA driver");
  1717. MODULE_AUTHOR("Rameshwar Prasad Sahu <rsahu@apm.com>");
  1718. MODULE_AUTHOR("Loc Ho <lho@apm.com>");
  1719. MODULE_LICENSE("GPL");
  1720. MODULE_VERSION("1.0");