xgene-dma.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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 __le64 *xgene_dma_lookup_ext8(struct xgene_dma_desc_hw *desc, int idx)
  364. {
  365. switch (idx) {
  366. case 0:
  367. return &desc->m1;
  368. case 1:
  369. return &desc->m0;
  370. case 2:
  371. return &desc->m3;
  372. case 3:
  373. return &desc->m2;
  374. default:
  375. pr_err("Invalid dma descriptor index\n");
  376. }
  377. return NULL;
  378. }
  379. static void xgene_dma_init_desc(struct xgene_dma_desc_hw *desc,
  380. u16 dst_ring_num)
  381. {
  382. desc->m0 |= cpu_to_le64(XGENE_DMA_DESC_IN_BIT);
  383. desc->m0 |= cpu_to_le64((u64)XGENE_DMA_RING_OWNER_DMA <<
  384. XGENE_DMA_DESC_RTYPE_POS);
  385. desc->m1 |= cpu_to_le64(XGENE_DMA_DESC_C_BIT);
  386. desc->m3 |= cpu_to_le64((u64)dst_ring_num <<
  387. XGENE_DMA_DESC_HOENQ_NUM_POS);
  388. }
  389. static void xgene_dma_prep_xor_desc(struct xgene_dma_chan *chan,
  390. struct xgene_dma_desc_sw *desc_sw,
  391. dma_addr_t *dst, dma_addr_t *src,
  392. u32 src_cnt, size_t *nbytes,
  393. const u8 *scf)
  394. {
  395. struct xgene_dma_desc_hw *desc1, *desc2;
  396. size_t len = *nbytes;
  397. int i;
  398. desc1 = &desc_sw->desc1;
  399. desc2 = &desc_sw->desc2;
  400. /* Initialize DMA descriptor */
  401. xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
  402. /* Set destination address */
  403. desc1->m2 |= cpu_to_le64(XGENE_DMA_DESC_DR_BIT);
  404. desc1->m3 |= cpu_to_le64(*dst);
  405. /* We have multiple source addresses, so need to set NV bit*/
  406. desc1->m0 |= cpu_to_le64(XGENE_DMA_DESC_NV_BIT);
  407. /* Set flyby opcode */
  408. desc1->m2 |= cpu_to_le64(xgene_dma_encode_xor_flyby(src_cnt));
  409. /* Set 1st to 5th source addresses */
  410. for (i = 0; i < src_cnt; i++) {
  411. len = *nbytes;
  412. xgene_dma_set_src_buffer((i == 0) ? &desc1->m1 :
  413. xgene_dma_lookup_ext8(desc2, i - 1),
  414. &len, &src[i]);
  415. desc1->m2 |= cpu_to_le64((scf[i] << ((i + 1) * 8)));
  416. }
  417. /* Update meta data */
  418. *nbytes = len;
  419. *dst += XGENE_DMA_MAX_BYTE_CNT;
  420. /* We need always 64B descriptor to perform xor or pq operations */
  421. desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
  422. }
  423. static dma_cookie_t xgene_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  424. {
  425. struct xgene_dma_desc_sw *desc;
  426. struct xgene_dma_chan *chan;
  427. dma_cookie_t cookie;
  428. if (unlikely(!tx))
  429. return -EINVAL;
  430. chan = to_dma_chan(tx->chan);
  431. desc = to_dma_desc_sw(tx);
  432. spin_lock_bh(&chan->lock);
  433. cookie = dma_cookie_assign(tx);
  434. /* Add this transaction list onto the tail of the pending queue */
  435. list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
  436. spin_unlock_bh(&chan->lock);
  437. return cookie;
  438. }
  439. static void xgene_dma_clean_descriptor(struct xgene_dma_chan *chan,
  440. struct xgene_dma_desc_sw *desc)
  441. {
  442. list_del(&desc->node);
  443. chan_dbg(chan, "LD %p free\n", desc);
  444. dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
  445. }
  446. static struct xgene_dma_desc_sw *xgene_dma_alloc_descriptor(
  447. struct xgene_dma_chan *chan)
  448. {
  449. struct xgene_dma_desc_sw *desc;
  450. dma_addr_t phys;
  451. desc = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
  452. if (!desc) {
  453. chan_err(chan, "Failed to allocate LDs\n");
  454. return NULL;
  455. }
  456. INIT_LIST_HEAD(&desc->tx_list);
  457. desc->tx.phys = phys;
  458. desc->tx.tx_submit = xgene_dma_tx_submit;
  459. dma_async_tx_descriptor_init(&desc->tx, &chan->dma_chan);
  460. chan_dbg(chan, "LD %p allocated\n", desc);
  461. return desc;
  462. }
  463. /**
  464. * xgene_dma_clean_completed_descriptor - free all descriptors which
  465. * has been completed and acked
  466. * @chan: X-Gene DMA channel
  467. *
  468. * This function is used on all completed and acked descriptors.
  469. */
  470. static void xgene_dma_clean_completed_descriptor(struct xgene_dma_chan *chan)
  471. {
  472. struct xgene_dma_desc_sw *desc, *_desc;
  473. /* Run the callback for each descriptor, in order */
  474. list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node) {
  475. if (async_tx_test_ack(&desc->tx))
  476. xgene_dma_clean_descriptor(chan, desc);
  477. }
  478. }
  479. /**
  480. * xgene_dma_run_tx_complete_actions - cleanup a single link descriptor
  481. * @chan: X-Gene DMA channel
  482. * @desc: descriptor to cleanup and free
  483. *
  484. * This function is used on a descriptor which has been executed by the DMA
  485. * controller. It will run any callbacks, submit any dependencies.
  486. */
  487. static void xgene_dma_run_tx_complete_actions(struct xgene_dma_chan *chan,
  488. struct xgene_dma_desc_sw *desc)
  489. {
  490. struct dma_async_tx_descriptor *tx = &desc->tx;
  491. /*
  492. * If this is not the last transaction in the group,
  493. * then no need to complete cookie and run any callback as
  494. * this is not the tx_descriptor which had been sent to caller
  495. * of this DMA request
  496. */
  497. if (tx->cookie == 0)
  498. return;
  499. dma_cookie_complete(tx);
  500. dma_descriptor_unmap(tx);
  501. /* Run the link descriptor callback function */
  502. dmaengine_desc_get_callback_invoke(tx, NULL);
  503. /* Run any dependencies */
  504. dma_run_dependencies(tx);
  505. }
  506. /**
  507. * xgene_dma_clean_running_descriptor - move the completed descriptor from
  508. * ld_running to ld_completed
  509. * @chan: X-Gene DMA channel
  510. * @desc: the descriptor which is completed
  511. *
  512. * Free the descriptor directly if acked by async_tx api,
  513. * else move it to queue ld_completed.
  514. */
  515. static void xgene_dma_clean_running_descriptor(struct xgene_dma_chan *chan,
  516. struct xgene_dma_desc_sw *desc)
  517. {
  518. /* Remove from the list of running transactions */
  519. list_del(&desc->node);
  520. /*
  521. * the client is allowed to attach dependent operations
  522. * until 'ack' is set
  523. */
  524. if (!async_tx_test_ack(&desc->tx)) {
  525. /*
  526. * Move this descriptor to the list of descriptors which is
  527. * completed, but still awaiting the 'ack' bit to be set.
  528. */
  529. list_add_tail(&desc->node, &chan->ld_completed);
  530. return;
  531. }
  532. chan_dbg(chan, "LD %p free\n", desc);
  533. dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
  534. }
  535. static void xgene_chan_xfer_request(struct xgene_dma_chan *chan,
  536. struct xgene_dma_desc_sw *desc_sw)
  537. {
  538. struct xgene_dma_ring *ring = &chan->tx_ring;
  539. struct xgene_dma_desc_hw *desc_hw;
  540. /* Get hw descriptor from DMA tx ring */
  541. desc_hw = &ring->desc_hw[ring->head];
  542. /*
  543. * Increment the head count to point next
  544. * descriptor for next time
  545. */
  546. if (++ring->head == ring->slots)
  547. ring->head = 0;
  548. /* Copy prepared sw descriptor data to hw descriptor */
  549. memcpy(desc_hw, &desc_sw->desc1, sizeof(*desc_hw));
  550. /*
  551. * Check if we have prepared 64B descriptor,
  552. * in this case we need one more hw descriptor
  553. */
  554. if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) {
  555. desc_hw = &ring->desc_hw[ring->head];
  556. if (++ring->head == ring->slots)
  557. ring->head = 0;
  558. memcpy(desc_hw, &desc_sw->desc2, sizeof(*desc_hw));
  559. }
  560. /* Increment the pending transaction count */
  561. chan->pending += ((desc_sw->flags &
  562. XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
  563. /* Notify the hw that we have descriptor ready for execution */
  564. iowrite32((desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) ?
  565. 2 : 1, ring->cmd);
  566. }
  567. /**
  568. * xgene_chan_xfer_ld_pending - push any pending transactions to hw
  569. * @chan : X-Gene DMA channel
  570. *
  571. * LOCKING: must hold chan->lock
  572. */
  573. static void xgene_chan_xfer_ld_pending(struct xgene_dma_chan *chan)
  574. {
  575. struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
  576. /*
  577. * If the list of pending descriptors is empty, then we
  578. * don't need to do any work at all
  579. */
  580. if (list_empty(&chan->ld_pending)) {
  581. chan_dbg(chan, "No pending LDs\n");
  582. return;
  583. }
  584. /*
  585. * Move elements from the queue of pending transactions onto the list
  586. * of running transactions and push it to hw for further executions
  587. */
  588. list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_pending, node) {
  589. /*
  590. * Check if have pushed max number of transactions to hw
  591. * as capable, so let's stop here and will push remaining
  592. * elements from pening ld queue after completing some
  593. * descriptors that we have already pushed
  594. */
  595. if (chan->pending >= chan->max_outstanding)
  596. return;
  597. xgene_chan_xfer_request(chan, desc_sw);
  598. /*
  599. * Delete this element from ld pending queue and append it to
  600. * ld running queue
  601. */
  602. list_move_tail(&desc_sw->node, &chan->ld_running);
  603. }
  604. }
  605. /**
  606. * xgene_dma_cleanup_descriptors - cleanup link descriptors which are completed
  607. * and move them to ld_completed to free until flag 'ack' is set
  608. * @chan: X-Gene DMA channel
  609. *
  610. * This function is used on descriptors which have been executed by the DMA
  611. * controller. It will run any callbacks, submit any dependencies, then
  612. * free these descriptors if flag 'ack' is set.
  613. */
  614. static void xgene_dma_cleanup_descriptors(struct xgene_dma_chan *chan)
  615. {
  616. struct xgene_dma_ring *ring = &chan->rx_ring;
  617. struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
  618. struct xgene_dma_desc_hw *desc_hw;
  619. struct list_head ld_completed;
  620. u8 status;
  621. INIT_LIST_HEAD(&ld_completed);
  622. spin_lock_bh(&chan->lock);
  623. /* Clean already completed and acked descriptors */
  624. xgene_dma_clean_completed_descriptor(chan);
  625. /* Move all completed descriptors to ld completed queue, in order */
  626. list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_running, node) {
  627. /* Get subsequent hw descriptor from DMA rx ring */
  628. desc_hw = &ring->desc_hw[ring->head];
  629. /* Check if this descriptor has been completed */
  630. if (unlikely(le64_to_cpu(desc_hw->m0) ==
  631. XGENE_DMA_DESC_EMPTY_SIGNATURE))
  632. break;
  633. if (++ring->head == ring->slots)
  634. ring->head = 0;
  635. /* Check if we have any error with DMA transactions */
  636. status = XGENE_DMA_DESC_STATUS(
  637. XGENE_DMA_DESC_ELERR_RD(le64_to_cpu(
  638. desc_hw->m0)),
  639. XGENE_DMA_DESC_LERR_RD(le64_to_cpu(
  640. desc_hw->m0)));
  641. if (status) {
  642. /* Print the DMA error type */
  643. chan_err(chan, "%s\n", xgene_dma_desc_err[status]);
  644. /*
  645. * We have DMA transactions error here. Dump DMA Tx
  646. * and Rx descriptors for this request */
  647. XGENE_DMA_DESC_DUMP(&desc_sw->desc1,
  648. "X-Gene DMA TX DESC1: ");
  649. if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC)
  650. XGENE_DMA_DESC_DUMP(&desc_sw->desc2,
  651. "X-Gene DMA TX DESC2: ");
  652. XGENE_DMA_DESC_DUMP(desc_hw,
  653. "X-Gene DMA RX ERR DESC: ");
  654. }
  655. /* Notify the hw about this completed descriptor */
  656. iowrite32(-1, ring->cmd);
  657. /* Mark this hw descriptor as processed */
  658. desc_hw->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
  659. /*
  660. * Decrement the pending transaction count
  661. * as we have processed one
  662. */
  663. chan->pending -= ((desc_sw->flags &
  664. XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
  665. /*
  666. * Delete this node from ld running queue and append it to
  667. * ld completed queue for further processing
  668. */
  669. list_move_tail(&desc_sw->node, &ld_completed);
  670. }
  671. /*
  672. * Start any pending transactions automatically
  673. * In the ideal case, we keep the DMA controller busy while we go
  674. * ahead and free the descriptors below.
  675. */
  676. xgene_chan_xfer_ld_pending(chan);
  677. spin_unlock_bh(&chan->lock);
  678. /* Run the callback for each descriptor, in order */
  679. list_for_each_entry_safe(desc_sw, _desc_sw, &ld_completed, node) {
  680. xgene_dma_run_tx_complete_actions(chan, desc_sw);
  681. xgene_dma_clean_running_descriptor(chan, desc_sw);
  682. }
  683. }
  684. static int xgene_dma_alloc_chan_resources(struct dma_chan *dchan)
  685. {
  686. struct xgene_dma_chan *chan = to_dma_chan(dchan);
  687. /* Has this channel already been allocated? */
  688. if (chan->desc_pool)
  689. return 1;
  690. chan->desc_pool = dma_pool_create(chan->name, chan->dev,
  691. sizeof(struct xgene_dma_desc_sw),
  692. 0, 0);
  693. if (!chan->desc_pool) {
  694. chan_err(chan, "Failed to allocate descriptor pool\n");
  695. return -ENOMEM;
  696. }
  697. chan_dbg(chan, "Allocate descripto pool\n");
  698. return 1;
  699. }
  700. /**
  701. * xgene_dma_free_desc_list - Free all descriptors in a queue
  702. * @chan: X-Gene DMA channel
  703. * @list: the list to free
  704. *
  705. * LOCKING: must hold chan->lock
  706. */
  707. static void xgene_dma_free_desc_list(struct xgene_dma_chan *chan,
  708. struct list_head *list)
  709. {
  710. struct xgene_dma_desc_sw *desc, *_desc;
  711. list_for_each_entry_safe(desc, _desc, list, node)
  712. xgene_dma_clean_descriptor(chan, desc);
  713. }
  714. static void xgene_dma_free_chan_resources(struct dma_chan *dchan)
  715. {
  716. struct xgene_dma_chan *chan = to_dma_chan(dchan);
  717. chan_dbg(chan, "Free all resources\n");
  718. if (!chan->desc_pool)
  719. return;
  720. /* Process all running descriptor */
  721. xgene_dma_cleanup_descriptors(chan);
  722. spin_lock_bh(&chan->lock);
  723. /* Clean all link descriptor queues */
  724. xgene_dma_free_desc_list(chan, &chan->ld_pending);
  725. xgene_dma_free_desc_list(chan, &chan->ld_running);
  726. xgene_dma_free_desc_list(chan, &chan->ld_completed);
  727. spin_unlock_bh(&chan->lock);
  728. /* Delete this channel DMA pool */
  729. dma_pool_destroy(chan->desc_pool);
  730. chan->desc_pool = NULL;
  731. }
  732. static struct dma_async_tx_descriptor *xgene_dma_prep_xor(
  733. struct dma_chan *dchan, dma_addr_t dst, dma_addr_t *src,
  734. u32 src_cnt, size_t len, unsigned long flags)
  735. {
  736. struct xgene_dma_desc_sw *first = NULL, *new;
  737. struct xgene_dma_chan *chan;
  738. static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {
  739. 0x01, 0x01, 0x01, 0x01, 0x01};
  740. if (unlikely(!dchan || !len))
  741. return NULL;
  742. chan = to_dma_chan(dchan);
  743. do {
  744. /* Allocate the link descriptor from DMA pool */
  745. new = xgene_dma_alloc_descriptor(chan);
  746. if (!new)
  747. goto fail;
  748. /* Prepare xor DMA descriptor */
  749. xgene_dma_prep_xor_desc(chan, new, &dst, src,
  750. src_cnt, &len, multi);
  751. if (!first)
  752. first = new;
  753. new->tx.cookie = 0;
  754. async_tx_ack(&new->tx);
  755. /* Insert the link descriptor to the LD ring */
  756. list_add_tail(&new->node, &first->tx_list);
  757. } while (len);
  758. new->tx.flags = flags; /* client is in control of this ack */
  759. new->tx.cookie = -EBUSY;
  760. list_splice(&first->tx_list, &new->tx_list);
  761. return &new->tx;
  762. fail:
  763. if (!first)
  764. return NULL;
  765. xgene_dma_free_desc_list(chan, &first->tx_list);
  766. return NULL;
  767. }
  768. static struct dma_async_tx_descriptor *xgene_dma_prep_pq(
  769. struct dma_chan *dchan, dma_addr_t *dst, dma_addr_t *src,
  770. u32 src_cnt, const u8 *scf, size_t len, unsigned long flags)
  771. {
  772. struct xgene_dma_desc_sw *first = NULL, *new;
  773. struct xgene_dma_chan *chan;
  774. size_t _len = len;
  775. dma_addr_t _src[XGENE_DMA_MAX_XOR_SRC];
  776. static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {0x01, 0x01, 0x01, 0x01, 0x01};
  777. if (unlikely(!dchan || !len))
  778. return NULL;
  779. chan = to_dma_chan(dchan);
  780. /*
  781. * Save source addresses on local variable, may be we have to
  782. * prepare two descriptor to generate P and Q if both enabled
  783. * in the flags by client
  784. */
  785. memcpy(_src, src, sizeof(*src) * src_cnt);
  786. if (flags & DMA_PREP_PQ_DISABLE_P)
  787. len = 0;
  788. if (flags & DMA_PREP_PQ_DISABLE_Q)
  789. _len = 0;
  790. do {
  791. /* Allocate the link descriptor from DMA pool */
  792. new = xgene_dma_alloc_descriptor(chan);
  793. if (!new)
  794. goto fail;
  795. if (!first)
  796. first = new;
  797. new->tx.cookie = 0;
  798. async_tx_ack(&new->tx);
  799. /* Insert the link descriptor to the LD ring */
  800. list_add_tail(&new->node, &first->tx_list);
  801. /*
  802. * Prepare DMA descriptor to generate P,
  803. * if DMA_PREP_PQ_DISABLE_P flag is not set
  804. */
  805. if (len) {
  806. xgene_dma_prep_xor_desc(chan, new, &dst[0], src,
  807. src_cnt, &len, multi);
  808. continue;
  809. }
  810. /*
  811. * Prepare DMA descriptor to generate Q,
  812. * if DMA_PREP_PQ_DISABLE_Q flag is not set
  813. */
  814. if (_len) {
  815. xgene_dma_prep_xor_desc(chan, new, &dst[1], _src,
  816. src_cnt, &_len, scf);
  817. }
  818. } while (len || _len);
  819. new->tx.flags = flags; /* client is in control of this ack */
  820. new->tx.cookie = -EBUSY;
  821. list_splice(&first->tx_list, &new->tx_list);
  822. return &new->tx;
  823. fail:
  824. if (!first)
  825. return NULL;
  826. xgene_dma_free_desc_list(chan, &first->tx_list);
  827. return NULL;
  828. }
  829. static void xgene_dma_issue_pending(struct dma_chan *dchan)
  830. {
  831. struct xgene_dma_chan *chan = to_dma_chan(dchan);
  832. spin_lock_bh(&chan->lock);
  833. xgene_chan_xfer_ld_pending(chan);
  834. spin_unlock_bh(&chan->lock);
  835. }
  836. static enum dma_status xgene_dma_tx_status(struct dma_chan *dchan,
  837. dma_cookie_t cookie,
  838. struct dma_tx_state *txstate)
  839. {
  840. return dma_cookie_status(dchan, cookie, txstate);
  841. }
  842. static void xgene_dma_tasklet_cb(unsigned long data)
  843. {
  844. struct xgene_dma_chan *chan = (struct xgene_dma_chan *)data;
  845. /* Run all cleanup for descriptors which have been completed */
  846. xgene_dma_cleanup_descriptors(chan);
  847. /* Re-enable DMA channel IRQ */
  848. enable_irq(chan->rx_irq);
  849. }
  850. static irqreturn_t xgene_dma_chan_ring_isr(int irq, void *id)
  851. {
  852. struct xgene_dma_chan *chan = (struct xgene_dma_chan *)id;
  853. BUG_ON(!chan);
  854. /*
  855. * Disable DMA channel IRQ until we process completed
  856. * descriptors
  857. */
  858. disable_irq_nosync(chan->rx_irq);
  859. /*
  860. * Schedule the tasklet to handle all cleanup of the current
  861. * transaction. It will start a new transaction if there is
  862. * one pending.
  863. */
  864. tasklet_schedule(&chan->tasklet);
  865. return IRQ_HANDLED;
  866. }
  867. static irqreturn_t xgene_dma_err_isr(int irq, void *id)
  868. {
  869. struct xgene_dma *pdma = (struct xgene_dma *)id;
  870. unsigned long int_mask;
  871. u32 val, i;
  872. val = ioread32(pdma->csr_dma + XGENE_DMA_INT);
  873. /* Clear DMA interrupts */
  874. iowrite32(val, pdma->csr_dma + XGENE_DMA_INT);
  875. /* Print DMA error info */
  876. int_mask = val >> XGENE_DMA_INT_MASK_SHIFT;
  877. for_each_set_bit(i, &int_mask, ARRAY_SIZE(xgene_dma_err))
  878. dev_err(pdma->dev,
  879. "Interrupt status 0x%08X %s\n", val, xgene_dma_err[i]);
  880. return IRQ_HANDLED;
  881. }
  882. static void xgene_dma_wr_ring_state(struct xgene_dma_ring *ring)
  883. {
  884. int i;
  885. iowrite32(ring->num, ring->pdma->csr_ring + XGENE_DMA_RING_STATE);
  886. for (i = 0; i < XGENE_DMA_RING_NUM_CONFIG; i++)
  887. iowrite32(ring->state[i], ring->pdma->csr_ring +
  888. XGENE_DMA_RING_STATE_WR_BASE + (i * 4));
  889. }
  890. static void xgene_dma_clr_ring_state(struct xgene_dma_ring *ring)
  891. {
  892. memset(ring->state, 0, sizeof(u32) * XGENE_DMA_RING_NUM_CONFIG);
  893. xgene_dma_wr_ring_state(ring);
  894. }
  895. static void xgene_dma_setup_ring(struct xgene_dma_ring *ring)
  896. {
  897. void *ring_cfg = ring->state;
  898. u64 addr = ring->desc_paddr;
  899. u32 i, val;
  900. ring->slots = ring->size / XGENE_DMA_RING_WQ_DESC_SIZE;
  901. /* Clear DMA ring state */
  902. xgene_dma_clr_ring_state(ring);
  903. /* Set DMA ring type */
  904. XGENE_DMA_RING_TYPE_SET(ring_cfg, XGENE_DMA_RING_TYPE_REGULAR);
  905. if (ring->owner == XGENE_DMA_RING_OWNER_DMA) {
  906. /* Set recombination buffer and timeout */
  907. XGENE_DMA_RING_RECOMBBUF_SET(ring_cfg);
  908. XGENE_DMA_RING_RECOMTIMEOUTL_SET(ring_cfg);
  909. XGENE_DMA_RING_RECOMTIMEOUTH_SET(ring_cfg);
  910. }
  911. /* Initialize DMA ring state */
  912. XGENE_DMA_RING_SELTHRSH_SET(ring_cfg);
  913. XGENE_DMA_RING_ACCEPTLERR_SET(ring_cfg);
  914. XGENE_DMA_RING_COHERENT_SET(ring_cfg);
  915. XGENE_DMA_RING_ADDRL_SET(ring_cfg, addr);
  916. XGENE_DMA_RING_ADDRH_SET(ring_cfg, addr);
  917. XGENE_DMA_RING_SIZE_SET(ring_cfg, ring->cfgsize);
  918. /* Write DMA ring configurations */
  919. xgene_dma_wr_ring_state(ring);
  920. /* Set DMA ring id */
  921. iowrite32(XGENE_DMA_RING_ID_SETUP(ring->id),
  922. ring->pdma->csr_ring + XGENE_DMA_RING_ID);
  923. /* Set DMA ring buffer */
  924. iowrite32(XGENE_DMA_RING_ID_BUF_SETUP(ring->num),
  925. ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
  926. if (ring->owner != XGENE_DMA_RING_OWNER_CPU)
  927. return;
  928. /* Set empty signature to DMA Rx ring descriptors */
  929. for (i = 0; i < ring->slots; i++) {
  930. struct xgene_dma_desc_hw *desc;
  931. desc = &ring->desc_hw[i];
  932. desc->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
  933. }
  934. /* Enable DMA Rx ring interrupt */
  935. val = ioread32(ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
  936. XGENE_DMA_RING_NE_INT_MODE_SET(val, ring->buf_num);
  937. iowrite32(val, ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
  938. }
  939. static void xgene_dma_clear_ring(struct xgene_dma_ring *ring)
  940. {
  941. u32 ring_id, val;
  942. if (ring->owner == XGENE_DMA_RING_OWNER_CPU) {
  943. /* Disable DMA Rx ring interrupt */
  944. val = ioread32(ring->pdma->csr_ring +
  945. XGENE_DMA_RING_NE_INT_MODE);
  946. XGENE_DMA_RING_NE_INT_MODE_RESET(val, ring->buf_num);
  947. iowrite32(val, ring->pdma->csr_ring +
  948. XGENE_DMA_RING_NE_INT_MODE);
  949. }
  950. /* Clear DMA ring state */
  951. ring_id = XGENE_DMA_RING_ID_SETUP(ring->id);
  952. iowrite32(ring_id, ring->pdma->csr_ring + XGENE_DMA_RING_ID);
  953. iowrite32(0, ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
  954. xgene_dma_clr_ring_state(ring);
  955. }
  956. static void xgene_dma_set_ring_cmd(struct xgene_dma_ring *ring)
  957. {
  958. ring->cmd_base = ring->pdma->csr_ring_cmd +
  959. XGENE_DMA_RING_CMD_BASE_OFFSET((ring->num -
  960. XGENE_DMA_RING_NUM));
  961. ring->cmd = ring->cmd_base + XGENE_DMA_RING_CMD_OFFSET;
  962. }
  963. static int xgene_dma_get_ring_size(struct xgene_dma_chan *chan,
  964. enum xgene_dma_ring_cfgsize cfgsize)
  965. {
  966. int size;
  967. switch (cfgsize) {
  968. case XGENE_DMA_RING_CFG_SIZE_512B:
  969. size = 0x200;
  970. break;
  971. case XGENE_DMA_RING_CFG_SIZE_2KB:
  972. size = 0x800;
  973. break;
  974. case XGENE_DMA_RING_CFG_SIZE_16KB:
  975. size = 0x4000;
  976. break;
  977. case XGENE_DMA_RING_CFG_SIZE_64KB:
  978. size = 0x10000;
  979. break;
  980. case XGENE_DMA_RING_CFG_SIZE_512KB:
  981. size = 0x80000;
  982. break;
  983. default:
  984. chan_err(chan, "Unsupported cfg ring size %d\n", cfgsize);
  985. return -EINVAL;
  986. }
  987. return size;
  988. }
  989. static void xgene_dma_delete_ring_one(struct xgene_dma_ring *ring)
  990. {
  991. /* Clear DMA ring configurations */
  992. xgene_dma_clear_ring(ring);
  993. /* De-allocate DMA ring descriptor */
  994. if (ring->desc_vaddr) {
  995. dma_free_coherent(ring->pdma->dev, ring->size,
  996. ring->desc_vaddr, ring->desc_paddr);
  997. ring->desc_vaddr = NULL;
  998. }
  999. }
  1000. static void xgene_dma_delete_chan_rings(struct xgene_dma_chan *chan)
  1001. {
  1002. xgene_dma_delete_ring_one(&chan->rx_ring);
  1003. xgene_dma_delete_ring_one(&chan->tx_ring);
  1004. }
  1005. static int xgene_dma_create_ring_one(struct xgene_dma_chan *chan,
  1006. struct xgene_dma_ring *ring,
  1007. enum xgene_dma_ring_cfgsize cfgsize)
  1008. {
  1009. int ret;
  1010. /* Setup DMA ring descriptor variables */
  1011. ring->pdma = chan->pdma;
  1012. ring->cfgsize = cfgsize;
  1013. ring->num = chan->pdma->ring_num++;
  1014. ring->id = XGENE_DMA_RING_ID_GET(ring->owner, ring->buf_num);
  1015. ret = xgene_dma_get_ring_size(chan, cfgsize);
  1016. if (ret <= 0)
  1017. return ret;
  1018. ring->size = ret;
  1019. /* Allocate memory for DMA ring descriptor */
  1020. ring->desc_vaddr = dma_zalloc_coherent(chan->dev, ring->size,
  1021. &ring->desc_paddr, GFP_KERNEL);
  1022. if (!ring->desc_vaddr) {
  1023. chan_err(chan, "Failed to allocate ring desc\n");
  1024. return -ENOMEM;
  1025. }
  1026. /* Configure and enable DMA ring */
  1027. xgene_dma_set_ring_cmd(ring);
  1028. xgene_dma_setup_ring(ring);
  1029. return 0;
  1030. }
  1031. static int xgene_dma_create_chan_rings(struct xgene_dma_chan *chan)
  1032. {
  1033. struct xgene_dma_ring *rx_ring = &chan->rx_ring;
  1034. struct xgene_dma_ring *tx_ring = &chan->tx_ring;
  1035. int ret;
  1036. /* Create DMA Rx ring descriptor */
  1037. rx_ring->owner = XGENE_DMA_RING_OWNER_CPU;
  1038. rx_ring->buf_num = XGENE_DMA_CPU_BUFNUM + chan->id;
  1039. ret = xgene_dma_create_ring_one(chan, rx_ring,
  1040. XGENE_DMA_RING_CFG_SIZE_64KB);
  1041. if (ret)
  1042. return ret;
  1043. chan_dbg(chan, "Rx ring id 0x%X num %d desc 0x%p\n",
  1044. rx_ring->id, rx_ring->num, rx_ring->desc_vaddr);
  1045. /* Create DMA Tx ring descriptor */
  1046. tx_ring->owner = XGENE_DMA_RING_OWNER_DMA;
  1047. tx_ring->buf_num = XGENE_DMA_BUFNUM + chan->id;
  1048. ret = xgene_dma_create_ring_one(chan, tx_ring,
  1049. XGENE_DMA_RING_CFG_SIZE_64KB);
  1050. if (ret) {
  1051. xgene_dma_delete_ring_one(rx_ring);
  1052. return ret;
  1053. }
  1054. tx_ring->dst_ring_num = XGENE_DMA_RING_DST_ID(rx_ring->num);
  1055. chan_dbg(chan,
  1056. "Tx ring id 0x%X num %d desc 0x%p\n",
  1057. tx_ring->id, tx_ring->num, tx_ring->desc_vaddr);
  1058. /* Set the max outstanding request possible to this channel */
  1059. chan->max_outstanding = tx_ring->slots;
  1060. return ret;
  1061. }
  1062. static int xgene_dma_init_rings(struct xgene_dma *pdma)
  1063. {
  1064. int ret, i, j;
  1065. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1066. ret = xgene_dma_create_chan_rings(&pdma->chan[i]);
  1067. if (ret) {
  1068. for (j = 0; j < i; j++)
  1069. xgene_dma_delete_chan_rings(&pdma->chan[j]);
  1070. return ret;
  1071. }
  1072. }
  1073. return ret;
  1074. }
  1075. static void xgene_dma_enable(struct xgene_dma *pdma)
  1076. {
  1077. u32 val;
  1078. /* Configure and enable DMA engine */
  1079. val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
  1080. XGENE_DMA_CH_SETUP(val);
  1081. XGENE_DMA_ENABLE(val);
  1082. iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
  1083. }
  1084. static void xgene_dma_disable(struct xgene_dma *pdma)
  1085. {
  1086. u32 val;
  1087. val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
  1088. XGENE_DMA_DISABLE(val);
  1089. iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
  1090. }
  1091. static void xgene_dma_mask_interrupts(struct xgene_dma *pdma)
  1092. {
  1093. /*
  1094. * Mask DMA ring overflow, underflow and
  1095. * AXI write/read error interrupts
  1096. */
  1097. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1098. pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
  1099. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1100. pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
  1101. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1102. pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
  1103. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1104. pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
  1105. iowrite32(XGENE_DMA_INT_ALL_MASK,
  1106. pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
  1107. /* Mask DMA error interrupts */
  1108. iowrite32(XGENE_DMA_INT_ALL_MASK, pdma->csr_dma + XGENE_DMA_INT_MASK);
  1109. }
  1110. static void xgene_dma_unmask_interrupts(struct xgene_dma *pdma)
  1111. {
  1112. /*
  1113. * Unmask DMA ring overflow, underflow and
  1114. * AXI write/read error interrupts
  1115. */
  1116. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1117. pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
  1118. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1119. pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
  1120. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1121. pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
  1122. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1123. pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
  1124. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1125. pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
  1126. /* Unmask DMA error interrupts */
  1127. iowrite32(XGENE_DMA_INT_ALL_UNMASK,
  1128. pdma->csr_dma + XGENE_DMA_INT_MASK);
  1129. }
  1130. static void xgene_dma_init_hw(struct xgene_dma *pdma)
  1131. {
  1132. u32 val;
  1133. /* Associate DMA ring to corresponding ring HW */
  1134. iowrite32(XGENE_DMA_ASSOC_RING_MNGR1,
  1135. pdma->csr_dma + XGENE_DMA_CFG_RING_WQ_ASSOC);
  1136. /* Configure RAID6 polynomial control setting */
  1137. if (is_pq_enabled(pdma))
  1138. iowrite32(XGENE_DMA_RAID6_MULTI_CTRL(0x1D),
  1139. pdma->csr_dma + XGENE_DMA_RAID6_CONT);
  1140. else
  1141. dev_info(pdma->dev, "PQ is disabled in HW\n");
  1142. xgene_dma_enable(pdma);
  1143. xgene_dma_unmask_interrupts(pdma);
  1144. /* Get DMA id and version info */
  1145. val = ioread32(pdma->csr_dma + XGENE_DMA_IPBRR);
  1146. /* DMA device info */
  1147. dev_info(pdma->dev,
  1148. "X-Gene DMA v%d.%02d.%02d driver registered %d channels",
  1149. XGENE_DMA_REV_NO_RD(val), XGENE_DMA_BUS_ID_RD(val),
  1150. XGENE_DMA_DEV_ID_RD(val), XGENE_DMA_MAX_CHANNEL);
  1151. }
  1152. static int xgene_dma_init_ring_mngr(struct xgene_dma *pdma)
  1153. {
  1154. if (ioread32(pdma->csr_ring + XGENE_DMA_RING_CLKEN) &&
  1155. (!ioread32(pdma->csr_ring + XGENE_DMA_RING_SRST)))
  1156. return 0;
  1157. iowrite32(0x3, pdma->csr_ring + XGENE_DMA_RING_CLKEN);
  1158. iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_SRST);
  1159. /* Bring up memory */
  1160. iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
  1161. /* Force a barrier */
  1162. ioread32(pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
  1163. /* reset may take up to 1ms */
  1164. usleep_range(1000, 1100);
  1165. if (ioread32(pdma->csr_ring + XGENE_DMA_RING_BLK_MEM_RDY)
  1166. != XGENE_DMA_RING_BLK_MEM_RDY_VAL) {
  1167. dev_err(pdma->dev,
  1168. "Failed to release ring mngr memory from shutdown\n");
  1169. return -ENODEV;
  1170. }
  1171. /* program threshold set 1 and all hysteresis */
  1172. iowrite32(XGENE_DMA_RING_THRESLD0_SET1_VAL,
  1173. pdma->csr_ring + XGENE_DMA_RING_THRESLD0_SET1);
  1174. iowrite32(XGENE_DMA_RING_THRESLD1_SET1_VAL,
  1175. pdma->csr_ring + XGENE_DMA_RING_THRESLD1_SET1);
  1176. iowrite32(XGENE_DMA_RING_HYSTERESIS_VAL,
  1177. pdma->csr_ring + XGENE_DMA_RING_HYSTERESIS);
  1178. /* Enable QPcore and assign error queue */
  1179. iowrite32(XGENE_DMA_RING_ENABLE,
  1180. pdma->csr_ring + XGENE_DMA_RING_CONFIG);
  1181. return 0;
  1182. }
  1183. static int xgene_dma_init_mem(struct xgene_dma *pdma)
  1184. {
  1185. int ret;
  1186. ret = xgene_dma_init_ring_mngr(pdma);
  1187. if (ret)
  1188. return ret;
  1189. /* Bring up memory */
  1190. iowrite32(0x0, pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
  1191. /* Force a barrier */
  1192. ioread32(pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
  1193. /* reset may take up to 1ms */
  1194. usleep_range(1000, 1100);
  1195. if (ioread32(pdma->csr_dma + XGENE_DMA_BLK_MEM_RDY)
  1196. != XGENE_DMA_BLK_MEM_RDY_VAL) {
  1197. dev_err(pdma->dev,
  1198. "Failed to release DMA memory from shutdown\n");
  1199. return -ENODEV;
  1200. }
  1201. return 0;
  1202. }
  1203. static int xgene_dma_request_irqs(struct xgene_dma *pdma)
  1204. {
  1205. struct xgene_dma_chan *chan;
  1206. int ret, i, j;
  1207. /* Register DMA error irq */
  1208. ret = devm_request_irq(pdma->dev, pdma->err_irq, xgene_dma_err_isr,
  1209. 0, "dma_error", pdma);
  1210. if (ret) {
  1211. dev_err(pdma->dev,
  1212. "Failed to register error IRQ %d\n", pdma->err_irq);
  1213. return ret;
  1214. }
  1215. /* Register DMA channel rx irq */
  1216. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1217. chan = &pdma->chan[i];
  1218. irq_set_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
  1219. ret = devm_request_irq(chan->dev, chan->rx_irq,
  1220. xgene_dma_chan_ring_isr,
  1221. 0, chan->name, chan);
  1222. if (ret) {
  1223. chan_err(chan, "Failed to register Rx IRQ %d\n",
  1224. chan->rx_irq);
  1225. devm_free_irq(pdma->dev, pdma->err_irq, pdma);
  1226. for (j = 0; j < i; j++) {
  1227. chan = &pdma->chan[i];
  1228. irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
  1229. devm_free_irq(chan->dev, chan->rx_irq, chan);
  1230. }
  1231. return ret;
  1232. }
  1233. }
  1234. return 0;
  1235. }
  1236. static void xgene_dma_free_irqs(struct xgene_dma *pdma)
  1237. {
  1238. struct xgene_dma_chan *chan;
  1239. int i;
  1240. /* Free DMA device error irq */
  1241. devm_free_irq(pdma->dev, pdma->err_irq, pdma);
  1242. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1243. chan = &pdma->chan[i];
  1244. irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
  1245. devm_free_irq(chan->dev, chan->rx_irq, chan);
  1246. }
  1247. }
  1248. static void xgene_dma_set_caps(struct xgene_dma_chan *chan,
  1249. struct dma_device *dma_dev)
  1250. {
  1251. /* Initialize DMA device capability mask */
  1252. dma_cap_zero(dma_dev->cap_mask);
  1253. /* Set DMA device capability */
  1254. /* Basically here, the X-Gene SoC DMA engine channel 0 supports XOR
  1255. * and channel 1 supports XOR, PQ both. First thing here is we have
  1256. * mechanism in hw to enable/disable PQ/XOR supports on channel 1,
  1257. * we can make sure this by reading SoC Efuse register.
  1258. * Second thing, we have hw errata that if we run channel 0 and
  1259. * channel 1 simultaneously with executing XOR and PQ request,
  1260. * suddenly DMA engine hangs, So here we enable XOR on channel 0 only
  1261. * if XOR and PQ supports on channel 1 is disabled.
  1262. */
  1263. if ((chan->id == XGENE_DMA_PQ_CHANNEL) &&
  1264. is_pq_enabled(chan->pdma)) {
  1265. dma_cap_set(DMA_PQ, dma_dev->cap_mask);
  1266. dma_cap_set(DMA_XOR, dma_dev->cap_mask);
  1267. } else if ((chan->id == XGENE_DMA_XOR_CHANNEL) &&
  1268. !is_pq_enabled(chan->pdma)) {
  1269. dma_cap_set(DMA_XOR, dma_dev->cap_mask);
  1270. }
  1271. /* Set base and prep routines */
  1272. dma_dev->dev = chan->dev;
  1273. dma_dev->device_alloc_chan_resources = xgene_dma_alloc_chan_resources;
  1274. dma_dev->device_free_chan_resources = xgene_dma_free_chan_resources;
  1275. dma_dev->device_issue_pending = xgene_dma_issue_pending;
  1276. dma_dev->device_tx_status = xgene_dma_tx_status;
  1277. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  1278. dma_dev->device_prep_dma_xor = xgene_dma_prep_xor;
  1279. dma_dev->max_xor = XGENE_DMA_MAX_XOR_SRC;
  1280. dma_dev->xor_align = DMAENGINE_ALIGN_64_BYTES;
  1281. }
  1282. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  1283. dma_dev->device_prep_dma_pq = xgene_dma_prep_pq;
  1284. dma_dev->max_pq = XGENE_DMA_MAX_XOR_SRC;
  1285. dma_dev->pq_align = DMAENGINE_ALIGN_64_BYTES;
  1286. }
  1287. }
  1288. static int xgene_dma_async_register(struct xgene_dma *pdma, int id)
  1289. {
  1290. struct xgene_dma_chan *chan = &pdma->chan[id];
  1291. struct dma_device *dma_dev = &pdma->dma_dev[id];
  1292. int ret;
  1293. chan->dma_chan.device = dma_dev;
  1294. spin_lock_init(&chan->lock);
  1295. INIT_LIST_HEAD(&chan->ld_pending);
  1296. INIT_LIST_HEAD(&chan->ld_running);
  1297. INIT_LIST_HEAD(&chan->ld_completed);
  1298. tasklet_init(&chan->tasklet, xgene_dma_tasklet_cb,
  1299. (unsigned long)chan);
  1300. chan->pending = 0;
  1301. chan->desc_pool = NULL;
  1302. dma_cookie_init(&chan->dma_chan);
  1303. /* Setup dma device capabilities and prep routines */
  1304. xgene_dma_set_caps(chan, dma_dev);
  1305. /* Initialize DMA device list head */
  1306. INIT_LIST_HEAD(&dma_dev->channels);
  1307. list_add_tail(&chan->dma_chan.device_node, &dma_dev->channels);
  1308. /* Register with Linux async DMA framework*/
  1309. ret = dma_async_device_register(dma_dev);
  1310. if (ret) {
  1311. chan_err(chan, "Failed to register async device %d", ret);
  1312. tasklet_kill(&chan->tasklet);
  1313. return ret;
  1314. }
  1315. /* DMA capability info */
  1316. dev_info(pdma->dev,
  1317. "%s: CAPABILITY ( %s%s)\n", dma_chan_name(&chan->dma_chan),
  1318. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "XOR " : "",
  1319. dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "PQ " : "");
  1320. return 0;
  1321. }
  1322. static int xgene_dma_init_async(struct xgene_dma *pdma)
  1323. {
  1324. int ret, i, j;
  1325. for (i = 0; i < XGENE_DMA_MAX_CHANNEL ; i++) {
  1326. ret = xgene_dma_async_register(pdma, i);
  1327. if (ret) {
  1328. for (j = 0; j < i; j++) {
  1329. dma_async_device_unregister(&pdma->dma_dev[j]);
  1330. tasklet_kill(&pdma->chan[j].tasklet);
  1331. }
  1332. return ret;
  1333. }
  1334. }
  1335. return ret;
  1336. }
  1337. static void xgene_dma_async_unregister(struct xgene_dma *pdma)
  1338. {
  1339. int i;
  1340. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
  1341. dma_async_device_unregister(&pdma->dma_dev[i]);
  1342. }
  1343. static void xgene_dma_init_channels(struct xgene_dma *pdma)
  1344. {
  1345. struct xgene_dma_chan *chan;
  1346. int i;
  1347. pdma->ring_num = XGENE_DMA_RING_NUM;
  1348. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1349. chan = &pdma->chan[i];
  1350. chan->dev = pdma->dev;
  1351. chan->pdma = pdma;
  1352. chan->id = i;
  1353. snprintf(chan->name, sizeof(chan->name), "dmachan%d", chan->id);
  1354. }
  1355. }
  1356. static int xgene_dma_get_resources(struct platform_device *pdev,
  1357. struct xgene_dma *pdma)
  1358. {
  1359. struct resource *res;
  1360. int irq, i;
  1361. /* Get DMA csr region */
  1362. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1363. if (!res) {
  1364. dev_err(&pdev->dev, "Failed to get csr region\n");
  1365. return -ENXIO;
  1366. }
  1367. pdma->csr_dma = devm_ioremap(&pdev->dev, res->start,
  1368. resource_size(res));
  1369. if (!pdma->csr_dma) {
  1370. dev_err(&pdev->dev, "Failed to ioremap csr region");
  1371. return -ENOMEM;
  1372. }
  1373. /* Get DMA ring csr region */
  1374. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1375. if (!res) {
  1376. dev_err(&pdev->dev, "Failed to get ring csr region\n");
  1377. return -ENXIO;
  1378. }
  1379. pdma->csr_ring = devm_ioremap(&pdev->dev, res->start,
  1380. resource_size(res));
  1381. if (!pdma->csr_ring) {
  1382. dev_err(&pdev->dev, "Failed to ioremap ring csr region");
  1383. return -ENOMEM;
  1384. }
  1385. /* Get DMA ring cmd csr region */
  1386. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1387. if (!res) {
  1388. dev_err(&pdev->dev, "Failed to get ring cmd csr region\n");
  1389. return -ENXIO;
  1390. }
  1391. pdma->csr_ring_cmd = devm_ioremap(&pdev->dev, res->start,
  1392. resource_size(res));
  1393. if (!pdma->csr_ring_cmd) {
  1394. dev_err(&pdev->dev, "Failed to ioremap ring cmd csr region");
  1395. return -ENOMEM;
  1396. }
  1397. pdma->csr_ring_cmd += XGENE_DMA_RING_CMD_SM_OFFSET;
  1398. /* Get efuse csr region */
  1399. res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  1400. if (!res) {
  1401. dev_err(&pdev->dev, "Failed to get efuse csr region\n");
  1402. return -ENXIO;
  1403. }
  1404. pdma->csr_efuse = devm_ioremap(&pdev->dev, res->start,
  1405. resource_size(res));
  1406. if (!pdma->csr_efuse) {
  1407. dev_err(&pdev->dev, "Failed to ioremap efuse csr region");
  1408. return -ENOMEM;
  1409. }
  1410. /* Get DMA error interrupt */
  1411. irq = platform_get_irq(pdev, 0);
  1412. if (irq <= 0) {
  1413. dev_err(&pdev->dev, "Failed to get Error IRQ\n");
  1414. return -ENXIO;
  1415. }
  1416. pdma->err_irq = irq;
  1417. /* Get DMA Rx ring descriptor interrupts for all DMA channels */
  1418. for (i = 1; i <= XGENE_DMA_MAX_CHANNEL; i++) {
  1419. irq = platform_get_irq(pdev, i);
  1420. if (irq <= 0) {
  1421. dev_err(&pdev->dev, "Failed to get Rx IRQ\n");
  1422. return -ENXIO;
  1423. }
  1424. pdma->chan[i - 1].rx_irq = irq;
  1425. }
  1426. return 0;
  1427. }
  1428. static int xgene_dma_probe(struct platform_device *pdev)
  1429. {
  1430. struct xgene_dma *pdma;
  1431. int ret, i;
  1432. pdma = devm_kzalloc(&pdev->dev, sizeof(*pdma), GFP_KERNEL);
  1433. if (!pdma)
  1434. return -ENOMEM;
  1435. pdma->dev = &pdev->dev;
  1436. platform_set_drvdata(pdev, pdma);
  1437. ret = xgene_dma_get_resources(pdev, pdma);
  1438. if (ret)
  1439. return ret;
  1440. pdma->clk = devm_clk_get(&pdev->dev, NULL);
  1441. if (IS_ERR(pdma->clk) && !ACPI_COMPANION(&pdev->dev)) {
  1442. dev_err(&pdev->dev, "Failed to get clk\n");
  1443. return PTR_ERR(pdma->clk);
  1444. }
  1445. /* Enable clk before accessing registers */
  1446. if (!IS_ERR(pdma->clk)) {
  1447. ret = clk_prepare_enable(pdma->clk);
  1448. if (ret) {
  1449. dev_err(&pdev->dev, "Failed to enable clk %d\n", ret);
  1450. return ret;
  1451. }
  1452. }
  1453. /* Remove DMA RAM out of shutdown */
  1454. ret = xgene_dma_init_mem(pdma);
  1455. if (ret)
  1456. goto err_clk_enable;
  1457. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(42));
  1458. if (ret) {
  1459. dev_err(&pdev->dev, "No usable DMA configuration\n");
  1460. goto err_dma_mask;
  1461. }
  1462. /* Initialize DMA channels software state */
  1463. xgene_dma_init_channels(pdma);
  1464. /* Configue DMA rings */
  1465. ret = xgene_dma_init_rings(pdma);
  1466. if (ret)
  1467. goto err_clk_enable;
  1468. ret = xgene_dma_request_irqs(pdma);
  1469. if (ret)
  1470. goto err_request_irq;
  1471. /* Configure and enable DMA engine */
  1472. xgene_dma_init_hw(pdma);
  1473. /* Register DMA device with linux async framework */
  1474. ret = xgene_dma_init_async(pdma);
  1475. if (ret)
  1476. goto err_async_init;
  1477. return 0;
  1478. err_async_init:
  1479. xgene_dma_free_irqs(pdma);
  1480. err_request_irq:
  1481. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
  1482. xgene_dma_delete_chan_rings(&pdma->chan[i]);
  1483. err_dma_mask:
  1484. err_clk_enable:
  1485. if (!IS_ERR(pdma->clk))
  1486. clk_disable_unprepare(pdma->clk);
  1487. return ret;
  1488. }
  1489. static int xgene_dma_remove(struct platform_device *pdev)
  1490. {
  1491. struct xgene_dma *pdma = platform_get_drvdata(pdev);
  1492. struct xgene_dma_chan *chan;
  1493. int i;
  1494. xgene_dma_async_unregister(pdma);
  1495. /* Mask interrupts and disable DMA engine */
  1496. xgene_dma_mask_interrupts(pdma);
  1497. xgene_dma_disable(pdma);
  1498. xgene_dma_free_irqs(pdma);
  1499. for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
  1500. chan = &pdma->chan[i];
  1501. tasklet_kill(&chan->tasklet);
  1502. xgene_dma_delete_chan_rings(chan);
  1503. }
  1504. if (!IS_ERR(pdma->clk))
  1505. clk_disable_unprepare(pdma->clk);
  1506. return 0;
  1507. }
  1508. #ifdef CONFIG_ACPI
  1509. static const struct acpi_device_id xgene_dma_acpi_match_ptr[] = {
  1510. {"APMC0D43", 0},
  1511. {},
  1512. };
  1513. MODULE_DEVICE_TABLE(acpi, xgene_dma_acpi_match_ptr);
  1514. #endif
  1515. static const struct of_device_id xgene_dma_of_match_ptr[] = {
  1516. {.compatible = "apm,xgene-storm-dma",},
  1517. {},
  1518. };
  1519. MODULE_DEVICE_TABLE(of, xgene_dma_of_match_ptr);
  1520. static struct platform_driver xgene_dma_driver = {
  1521. .probe = xgene_dma_probe,
  1522. .remove = xgene_dma_remove,
  1523. .driver = {
  1524. .name = "X-Gene-DMA",
  1525. .of_match_table = xgene_dma_of_match_ptr,
  1526. .acpi_match_table = ACPI_PTR(xgene_dma_acpi_match_ptr),
  1527. },
  1528. };
  1529. module_platform_driver(xgene_dma_driver);
  1530. MODULE_DESCRIPTION("APM X-Gene SoC DMA driver");
  1531. MODULE_AUTHOR("Rameshwar Prasad Sahu <rsahu@apm.com>");
  1532. MODULE_AUTHOR("Loc Ho <lho@apm.com>");
  1533. MODULE_LICENSE("GPL");
  1534. MODULE_VERSION("1.0");