xgene-dma.c 53 KB

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