xgene-dma.c 49 KB

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