xgene-dma.c 53 KB

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