hisi_dma.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2019-2022 HiSilicon Limited. */
  3. #include <linux/bitfield.h>
  4. #include <linux/dmaengine.h>
  5. #include <linux/init.h>
  6. #include <linux/iopoll.h>
  7. #include <linux/module.h>
  8. #include <linux/pci.h>
  9. #include <linux/spinlock.h>
  10. #include "virt-dma.h"
  11. /* HiSilicon DMA register common field define */
  12. #define HISI_DMA_Q_SQ_BASE_L 0x0
  13. #define HISI_DMA_Q_SQ_BASE_H 0x4
  14. #define HISI_DMA_Q_SQ_DEPTH 0x8
  15. #define HISI_DMA_Q_SQ_TAIL_PTR 0xc
  16. #define HISI_DMA_Q_CQ_BASE_L 0x10
  17. #define HISI_DMA_Q_CQ_BASE_H 0x14
  18. #define HISI_DMA_Q_CQ_DEPTH 0x18
  19. #define HISI_DMA_Q_CQ_HEAD_PTR 0x1c
  20. #define HISI_DMA_Q_CTRL0 0x20
  21. #define HISI_DMA_Q_CTRL0_QUEUE_EN BIT(0)
  22. #define HISI_DMA_Q_CTRL0_QUEUE_PAUSE BIT(4)
  23. #define HISI_DMA_Q_CTRL1 0x24
  24. #define HISI_DMA_Q_CTRL1_QUEUE_RESET BIT(0)
  25. #define HISI_DMA_Q_FSM_STS 0x30
  26. #define HISI_DMA_Q_FSM_STS_MASK GENMASK(3, 0)
  27. #define HISI_DMA_Q_ERR_INT_NUM0 0x84
  28. #define HISI_DMA_Q_ERR_INT_NUM1 0x88
  29. #define HISI_DMA_Q_ERR_INT_NUM2 0x8c
  30. /* HiSilicon IP08 DMA register and field define */
  31. #define HISI_DMA_HIP08_MODE 0x217C
  32. #define HISI_DMA_HIP08_Q_BASE 0x0
  33. #define HISI_DMA_HIP08_Q_CTRL0_ERR_ABORT_EN BIT(2)
  34. #define HISI_DMA_HIP08_Q_INT_STS 0x40
  35. #define HISI_DMA_HIP08_Q_INT_MSK 0x44
  36. #define HISI_DMA_HIP08_Q_INT_STS_MASK GENMASK(14, 0)
  37. #define HISI_DMA_HIP08_Q_ERR_INT_NUM3 0x90
  38. #define HISI_DMA_HIP08_Q_ERR_INT_NUM4 0x94
  39. #define HISI_DMA_HIP08_Q_ERR_INT_NUM5 0x98
  40. #define HISI_DMA_HIP08_Q_ERR_INT_NUM6 0x48
  41. #define HISI_DMA_HIP08_Q_CTRL0_SQCQ_DRCT BIT(24)
  42. /* HiSilicon IP09 DMA register and field define */
  43. #define HISI_DMA_HIP09_DMA_FLR_DISABLE 0xA00
  44. #define HISI_DMA_HIP09_DMA_FLR_DISABLE_B BIT(0)
  45. #define HISI_DMA_HIP09_Q_BASE 0x2000
  46. #define HISI_DMA_HIP09_Q_CTRL0_ERR_ABORT_EN GENMASK(31, 28)
  47. #define HISI_DMA_HIP09_Q_CTRL0_SQ_DRCT BIT(26)
  48. #define HISI_DMA_HIP09_Q_CTRL0_CQ_DRCT BIT(27)
  49. #define HISI_DMA_HIP09_Q_CTRL1_VA_ENABLE BIT(2)
  50. #define HISI_DMA_HIP09_Q_INT_STS 0x40
  51. #define HISI_DMA_HIP09_Q_INT_MSK 0x44
  52. #define HISI_DMA_HIP09_Q_INT_STS_MASK 0x1
  53. #define HISI_DMA_HIP09_Q_ERR_INT_STS 0x48
  54. #define HISI_DMA_HIP09_Q_ERR_INT_MSK 0x4C
  55. #define HISI_DMA_HIP09_Q_ERR_INT_STS_MASK GENMASK(18, 1)
  56. #define HISI_DMA_HIP09_PORT_CFG_REG(port_id) (0x800 + \
  57. (port_id) * 0x20)
  58. #define HISI_DMA_HIP09_PORT_CFG_LINK_DOWN_MASK_B BIT(16)
  59. #define HISI_DMA_HIP09_MAX_PORT_NUM 16
  60. #define HISI_DMA_HIP08_MSI_NUM 32
  61. #define HISI_DMA_HIP08_CHAN_NUM 30
  62. #define HISI_DMA_HIP09_MSI_NUM 4
  63. #define HISI_DMA_HIP09_CHAN_NUM 4
  64. #define HISI_DMA_REVISION_HIP08B 0x21
  65. #define HISI_DMA_REVISION_HIP09A 0x30
  66. #define HISI_DMA_Q_OFFSET 0x100
  67. #define HISI_DMA_Q_DEPTH_VAL 1024
  68. #define PCI_BAR_2 2
  69. #define HISI_DMA_POLL_Q_STS_DELAY_US 10
  70. #define HISI_DMA_POLL_Q_STS_TIME_OUT_US 1000
  71. #define HISI_DMA_MAX_DIR_NAME_LEN 128
  72. /*
  73. * The HIP08B(HiSilicon IP08) and HIP09A(HiSilicon IP09) are DMA iEPs, they
  74. * have the same pci device id but different pci revision.
  75. * Unfortunately, they have different register layouts, so two layout
  76. * enumerations are defined.
  77. */
  78. enum hisi_dma_reg_layout {
  79. HISI_DMA_REG_LAYOUT_INVALID = 0,
  80. HISI_DMA_REG_LAYOUT_HIP08,
  81. HISI_DMA_REG_LAYOUT_HIP09
  82. };
  83. enum hisi_dma_mode {
  84. EP = 0,
  85. RC,
  86. };
  87. enum hisi_dma_chan_status {
  88. DISABLE = -1,
  89. IDLE = 0,
  90. RUN,
  91. CPL,
  92. PAUSE,
  93. HALT,
  94. ABORT,
  95. WAIT,
  96. BUFFCLR,
  97. };
  98. struct hisi_dma_sqe {
  99. __le32 dw0;
  100. #define OPCODE_MASK GENMASK(3, 0)
  101. #define OPCODE_SMALL_PACKAGE 0x1
  102. #define OPCODE_M2M 0x4
  103. #define LOCAL_IRQ_EN BIT(8)
  104. #define ATTR_SRC_MASK GENMASK(14, 12)
  105. __le32 dw1;
  106. __le32 dw2;
  107. #define ATTR_DST_MASK GENMASK(26, 24)
  108. __le32 length;
  109. __le64 src_addr;
  110. __le64 dst_addr;
  111. };
  112. struct hisi_dma_cqe {
  113. __le32 rsv0;
  114. __le32 rsv1;
  115. __le16 sq_head;
  116. __le16 rsv2;
  117. __le16 rsv3;
  118. __le16 w0;
  119. #define STATUS_MASK GENMASK(15, 1)
  120. #define STATUS_SUCC 0x0
  121. #define VALID_BIT BIT(0)
  122. };
  123. struct hisi_dma_desc {
  124. struct virt_dma_desc vd;
  125. struct hisi_dma_sqe sqe;
  126. };
  127. struct hisi_dma_chan {
  128. struct virt_dma_chan vc;
  129. struct hisi_dma_dev *hdma_dev;
  130. struct hisi_dma_sqe *sq;
  131. struct hisi_dma_cqe *cq;
  132. dma_addr_t sq_dma;
  133. dma_addr_t cq_dma;
  134. u32 sq_tail;
  135. u32 cq_head;
  136. u32 qp_num;
  137. enum hisi_dma_chan_status status;
  138. struct hisi_dma_desc *desc;
  139. };
  140. struct hisi_dma_dev {
  141. struct pci_dev *pdev;
  142. void __iomem *base;
  143. struct dma_device dma_dev;
  144. u32 chan_num;
  145. u32 chan_depth;
  146. enum hisi_dma_reg_layout reg_layout;
  147. void __iomem *queue_base; /* queue region start of register */
  148. struct hisi_dma_chan chan[];
  149. };
  150. #ifdef CONFIG_DEBUG_FS
  151. static const struct debugfs_reg32 hisi_dma_comm_chan_regs[] = {
  152. {"DMA_QUEUE_SQ_DEPTH ", 0x0008ull},
  153. {"DMA_QUEUE_SQ_TAIL_PTR ", 0x000Cull},
  154. {"DMA_QUEUE_CQ_DEPTH ", 0x0018ull},
  155. {"DMA_QUEUE_CQ_HEAD_PTR ", 0x001Cull},
  156. {"DMA_QUEUE_CTRL0 ", 0x0020ull},
  157. {"DMA_QUEUE_CTRL1 ", 0x0024ull},
  158. {"DMA_QUEUE_FSM_STS ", 0x0030ull},
  159. {"DMA_QUEUE_SQ_STS ", 0x0034ull},
  160. {"DMA_QUEUE_CQ_TAIL_PTR ", 0x003Cull},
  161. {"DMA_QUEUE_INT_STS ", 0x0040ull},
  162. {"DMA_QUEUE_INT_MSK ", 0x0044ull},
  163. {"DMA_QUEUE_INT_RO ", 0x006Cull},
  164. };
  165. static const struct debugfs_reg32 hisi_dma_hip08_chan_regs[] = {
  166. {"DMA_QUEUE_BYTE_CNT ", 0x0038ull},
  167. {"DMA_ERR_INT_NUM6 ", 0x0048ull},
  168. {"DMA_QUEUE_DESP0 ", 0x0050ull},
  169. {"DMA_QUEUE_DESP1 ", 0x0054ull},
  170. {"DMA_QUEUE_DESP2 ", 0x0058ull},
  171. {"DMA_QUEUE_DESP3 ", 0x005Cull},
  172. {"DMA_QUEUE_DESP4 ", 0x0074ull},
  173. {"DMA_QUEUE_DESP5 ", 0x0078ull},
  174. {"DMA_QUEUE_DESP6 ", 0x007Cull},
  175. {"DMA_QUEUE_DESP7 ", 0x0080ull},
  176. {"DMA_ERR_INT_NUM0 ", 0x0084ull},
  177. {"DMA_ERR_INT_NUM1 ", 0x0088ull},
  178. {"DMA_ERR_INT_NUM2 ", 0x008Cull},
  179. {"DMA_ERR_INT_NUM3 ", 0x0090ull},
  180. {"DMA_ERR_INT_NUM4 ", 0x0094ull},
  181. {"DMA_ERR_INT_NUM5 ", 0x0098ull},
  182. {"DMA_QUEUE_SQ_STS2 ", 0x00A4ull},
  183. };
  184. static const struct debugfs_reg32 hisi_dma_hip09_chan_regs[] = {
  185. {"DMA_QUEUE_ERR_INT_STS ", 0x0048ull},
  186. {"DMA_QUEUE_ERR_INT_MSK ", 0x004Cull},
  187. {"DFX_SQ_READ_ERR_PTR ", 0x0068ull},
  188. {"DFX_DMA_ERR_INT_NUM0 ", 0x0084ull},
  189. {"DFX_DMA_ERR_INT_NUM1 ", 0x0088ull},
  190. {"DFX_DMA_ERR_INT_NUM2 ", 0x008Cull},
  191. {"DFX_DMA_QUEUE_SQ_STS2 ", 0x00A4ull},
  192. };
  193. static const struct debugfs_reg32 hisi_dma_hip08_comm_regs[] = {
  194. {"DMA_ECC_ERR_ADDR ", 0x2004ull},
  195. {"DMA_ECC_ECC_CNT ", 0x2014ull},
  196. {"COMMON_AND_CH_ERR_STS ", 0x2030ull},
  197. {"LOCAL_CPL_ID_STS_0 ", 0x20E0ull},
  198. {"LOCAL_CPL_ID_STS_1 ", 0x20E4ull},
  199. {"LOCAL_CPL_ID_STS_2 ", 0x20E8ull},
  200. {"LOCAL_CPL_ID_STS_3 ", 0x20ECull},
  201. {"LOCAL_TLP_NUM ", 0x2158ull},
  202. {"SQCQ_TLP_NUM ", 0x2164ull},
  203. {"CPL_NUM ", 0x2168ull},
  204. {"INF_BACK_PRESS_STS ", 0x2170ull},
  205. {"DMA_CH_RAS_LEVEL ", 0x2184ull},
  206. {"DMA_CM_RAS_LEVEL ", 0x2188ull},
  207. {"DMA_CH_ERR_STS ", 0x2190ull},
  208. {"DMA_CH_DONE_STS ", 0x2194ull},
  209. {"DMA_SQ_TAG_STS_0 ", 0x21A0ull},
  210. {"DMA_SQ_TAG_STS_1 ", 0x21A4ull},
  211. {"DMA_SQ_TAG_STS_2 ", 0x21A8ull},
  212. {"DMA_SQ_TAG_STS_3 ", 0x21ACull},
  213. {"LOCAL_P_ID_STS_0 ", 0x21B0ull},
  214. {"LOCAL_P_ID_STS_1 ", 0x21B4ull},
  215. {"LOCAL_P_ID_STS_2 ", 0x21B8ull},
  216. {"LOCAL_P_ID_STS_3 ", 0x21BCull},
  217. {"DMA_PREBUFF_INFO_0 ", 0x2200ull},
  218. {"DMA_CM_TABLE_INFO_0 ", 0x2220ull},
  219. {"DMA_CM_CE_RO ", 0x2244ull},
  220. {"DMA_CM_NFE_RO ", 0x2248ull},
  221. {"DMA_CM_FE_RO ", 0x224Cull},
  222. };
  223. static const struct debugfs_reg32 hisi_dma_hip09_comm_regs[] = {
  224. {"COMMON_AND_CH_ERR_STS ", 0x0030ull},
  225. {"DMA_PORT_IDLE_STS ", 0x0150ull},
  226. {"DMA_CH_RAS_LEVEL ", 0x0184ull},
  227. {"DMA_CM_RAS_LEVEL ", 0x0188ull},
  228. {"DMA_CM_CE_RO ", 0x0244ull},
  229. {"DMA_CM_NFE_RO ", 0x0248ull},
  230. {"DMA_CM_FE_RO ", 0x024Cull},
  231. {"DFX_INF_BACK_PRESS_STS0 ", 0x1A40ull},
  232. {"DFX_INF_BACK_PRESS_STS1 ", 0x1A44ull},
  233. {"DFX_INF_BACK_PRESS_STS2 ", 0x1A48ull},
  234. {"DFX_DMA_WRR_DISABLE ", 0x1A4Cull},
  235. {"DFX_PA_REQ_TLP_NUM ", 0x1C00ull},
  236. {"DFX_PA_BACK_TLP_NUM ", 0x1C04ull},
  237. {"DFX_PA_RETRY_TLP_NUM ", 0x1C08ull},
  238. {"DFX_LOCAL_NP_TLP_NUM ", 0x1C0Cull},
  239. {"DFX_LOCAL_CPL_HEAD_TLP_NUM ", 0x1C10ull},
  240. {"DFX_LOCAL_CPL_DATA_TLP_NUM ", 0x1C14ull},
  241. {"DFX_LOCAL_CPL_EXT_DATA_TLP_NUM ", 0x1C18ull},
  242. {"DFX_LOCAL_P_HEAD_TLP_NUM ", 0x1C1Cull},
  243. {"DFX_LOCAL_P_ACK_TLP_NUM ", 0x1C20ull},
  244. {"DFX_BUF_ALOC_PORT_REQ_NUM ", 0x1C24ull},
  245. {"DFX_BUF_ALOC_PORT_RESULT_NUM ", 0x1C28ull},
  246. {"DFX_BUF_FAIL_SIZE_NUM ", 0x1C2Cull},
  247. {"DFX_BUF_ALOC_SIZE_NUM ", 0x1C30ull},
  248. {"DFX_BUF_NP_RELEASE_SIZE_NUM ", 0x1C34ull},
  249. {"DFX_BUF_P_RELEASE_SIZE_NUM ", 0x1C38ull},
  250. {"DFX_BUF_PORT_RELEASE_SIZE_NUM ", 0x1C3Cull},
  251. {"DFX_DMA_PREBUF_MEM0_ECC_ERR_ADDR ", 0x1CA8ull},
  252. {"DFX_DMA_PREBUF_MEM0_ECC_CNT ", 0x1CACull},
  253. {"DFX_DMA_LOC_NP_OSTB_ECC_ERR_ADDR ", 0x1CB0ull},
  254. {"DFX_DMA_LOC_NP_OSTB_ECC_CNT ", 0x1CB4ull},
  255. {"DFX_DMA_PREBUF_MEM1_ECC_ERR_ADDR ", 0x1CC0ull},
  256. {"DFX_DMA_PREBUF_MEM1_ECC_CNT ", 0x1CC4ull},
  257. {"DMA_CH_DONE_STS ", 0x02E0ull},
  258. {"DMA_CH_ERR_STS ", 0x0320ull},
  259. };
  260. #endif /* CONFIG_DEBUG_FS*/
  261. static enum hisi_dma_reg_layout hisi_dma_get_reg_layout(struct pci_dev *pdev)
  262. {
  263. if (pdev->revision == HISI_DMA_REVISION_HIP08B)
  264. return HISI_DMA_REG_LAYOUT_HIP08;
  265. else if (pdev->revision >= HISI_DMA_REVISION_HIP09A)
  266. return HISI_DMA_REG_LAYOUT_HIP09;
  267. return HISI_DMA_REG_LAYOUT_INVALID;
  268. }
  269. static u32 hisi_dma_get_chan_num(struct pci_dev *pdev)
  270. {
  271. if (pdev->revision == HISI_DMA_REVISION_HIP08B)
  272. return HISI_DMA_HIP08_CHAN_NUM;
  273. return HISI_DMA_HIP09_CHAN_NUM;
  274. }
  275. static u32 hisi_dma_get_msi_num(struct pci_dev *pdev)
  276. {
  277. if (pdev->revision == HISI_DMA_REVISION_HIP08B)
  278. return HISI_DMA_HIP08_MSI_NUM;
  279. return HISI_DMA_HIP09_MSI_NUM;
  280. }
  281. static u32 hisi_dma_get_queue_base(struct pci_dev *pdev)
  282. {
  283. if (pdev->revision == HISI_DMA_REVISION_HIP08B)
  284. return HISI_DMA_HIP08_Q_BASE;
  285. return HISI_DMA_HIP09_Q_BASE;
  286. }
  287. static inline struct hisi_dma_chan *to_hisi_dma_chan(struct dma_chan *c)
  288. {
  289. return container_of(c, struct hisi_dma_chan, vc.chan);
  290. }
  291. static inline struct hisi_dma_desc *to_hisi_dma_desc(struct virt_dma_desc *vd)
  292. {
  293. return container_of(vd, struct hisi_dma_desc, vd);
  294. }
  295. static inline void hisi_dma_chan_write(void __iomem *base, u32 reg, u32 index,
  296. u32 val)
  297. {
  298. writel_relaxed(val, base + reg + index * HISI_DMA_Q_OFFSET);
  299. }
  300. static inline void hisi_dma_update_bit(void __iomem *addr, u32 pos, bool val)
  301. {
  302. u32 tmp;
  303. tmp = readl_relaxed(addr);
  304. tmp = val ? tmp | pos : tmp & ~pos;
  305. writel_relaxed(tmp, addr);
  306. }
  307. static void hisi_dma_pause_dma(struct hisi_dma_dev *hdma_dev, u32 index,
  308. bool pause)
  309. {
  310. void __iomem *addr;
  311. addr = hdma_dev->queue_base + HISI_DMA_Q_CTRL0 +
  312. index * HISI_DMA_Q_OFFSET;
  313. hisi_dma_update_bit(addr, HISI_DMA_Q_CTRL0_QUEUE_PAUSE, pause);
  314. }
  315. static void hisi_dma_enable_dma(struct hisi_dma_dev *hdma_dev, u32 index,
  316. bool enable)
  317. {
  318. void __iomem *addr;
  319. addr = hdma_dev->queue_base + HISI_DMA_Q_CTRL0 +
  320. index * HISI_DMA_Q_OFFSET;
  321. hisi_dma_update_bit(addr, HISI_DMA_Q_CTRL0_QUEUE_EN, enable);
  322. }
  323. static void hisi_dma_mask_irq(struct hisi_dma_dev *hdma_dev, u32 qp_index)
  324. {
  325. void __iomem *q_base = hdma_dev->queue_base;
  326. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP08)
  327. hisi_dma_chan_write(q_base, HISI_DMA_HIP08_Q_INT_MSK,
  328. qp_index, HISI_DMA_HIP08_Q_INT_STS_MASK);
  329. else {
  330. hisi_dma_chan_write(q_base, HISI_DMA_HIP09_Q_INT_MSK,
  331. qp_index, HISI_DMA_HIP09_Q_INT_STS_MASK);
  332. hisi_dma_chan_write(q_base, HISI_DMA_HIP09_Q_ERR_INT_MSK,
  333. qp_index,
  334. HISI_DMA_HIP09_Q_ERR_INT_STS_MASK);
  335. }
  336. }
  337. static void hisi_dma_unmask_irq(struct hisi_dma_dev *hdma_dev, u32 qp_index)
  338. {
  339. void __iomem *q_base = hdma_dev->queue_base;
  340. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP08) {
  341. hisi_dma_chan_write(q_base, HISI_DMA_HIP08_Q_INT_STS,
  342. qp_index, HISI_DMA_HIP08_Q_INT_STS_MASK);
  343. hisi_dma_chan_write(q_base, HISI_DMA_HIP08_Q_INT_MSK,
  344. qp_index, 0);
  345. } else {
  346. hisi_dma_chan_write(q_base, HISI_DMA_HIP09_Q_INT_STS,
  347. qp_index, HISI_DMA_HIP09_Q_INT_STS_MASK);
  348. hisi_dma_chan_write(q_base, HISI_DMA_HIP09_Q_ERR_INT_STS,
  349. qp_index,
  350. HISI_DMA_HIP09_Q_ERR_INT_STS_MASK);
  351. hisi_dma_chan_write(q_base, HISI_DMA_HIP09_Q_INT_MSK,
  352. qp_index, 0);
  353. hisi_dma_chan_write(q_base, HISI_DMA_HIP09_Q_ERR_INT_MSK,
  354. qp_index, 0);
  355. }
  356. }
  357. static void hisi_dma_do_reset(struct hisi_dma_dev *hdma_dev, u32 index)
  358. {
  359. void __iomem *addr;
  360. addr = hdma_dev->queue_base +
  361. HISI_DMA_Q_CTRL1 + index * HISI_DMA_Q_OFFSET;
  362. hisi_dma_update_bit(addr, HISI_DMA_Q_CTRL1_QUEUE_RESET, 1);
  363. }
  364. static void hisi_dma_reset_qp_point(struct hisi_dma_dev *hdma_dev, u32 index)
  365. {
  366. void __iomem *q_base = hdma_dev->queue_base;
  367. hisi_dma_chan_write(q_base, HISI_DMA_Q_SQ_TAIL_PTR, index, 0);
  368. hisi_dma_chan_write(q_base, HISI_DMA_Q_CQ_HEAD_PTR, index, 0);
  369. }
  370. static void hisi_dma_reset_or_disable_hw_chan(struct hisi_dma_chan *chan,
  371. bool disable)
  372. {
  373. struct hisi_dma_dev *hdma_dev = chan->hdma_dev;
  374. u32 index = chan->qp_num, tmp;
  375. void __iomem *addr;
  376. int ret;
  377. hisi_dma_pause_dma(hdma_dev, index, true);
  378. hisi_dma_enable_dma(hdma_dev, index, false);
  379. hisi_dma_mask_irq(hdma_dev, index);
  380. addr = hdma_dev->queue_base +
  381. HISI_DMA_Q_FSM_STS + index * HISI_DMA_Q_OFFSET;
  382. ret = readl_relaxed_poll_timeout(addr, tmp,
  383. FIELD_GET(HISI_DMA_Q_FSM_STS_MASK, tmp) != RUN,
  384. HISI_DMA_POLL_Q_STS_DELAY_US, HISI_DMA_POLL_Q_STS_TIME_OUT_US);
  385. if (ret) {
  386. dev_err(&hdma_dev->pdev->dev, "disable channel timeout!\n");
  387. WARN_ON(1);
  388. }
  389. hisi_dma_do_reset(hdma_dev, index);
  390. hisi_dma_reset_qp_point(hdma_dev, index);
  391. hisi_dma_pause_dma(hdma_dev, index, false);
  392. if (!disable) {
  393. hisi_dma_enable_dma(hdma_dev, index, true);
  394. hisi_dma_unmask_irq(hdma_dev, index);
  395. }
  396. ret = readl_relaxed_poll_timeout(addr, tmp,
  397. FIELD_GET(HISI_DMA_Q_FSM_STS_MASK, tmp) == IDLE,
  398. HISI_DMA_POLL_Q_STS_DELAY_US, HISI_DMA_POLL_Q_STS_TIME_OUT_US);
  399. if (ret) {
  400. dev_err(&hdma_dev->pdev->dev, "reset channel timeout!\n");
  401. WARN_ON(1);
  402. }
  403. }
  404. static void hisi_dma_free_chan_resources(struct dma_chan *c)
  405. {
  406. struct hisi_dma_chan *chan = to_hisi_dma_chan(c);
  407. struct hisi_dma_dev *hdma_dev = chan->hdma_dev;
  408. hisi_dma_reset_or_disable_hw_chan(chan, false);
  409. vchan_free_chan_resources(&chan->vc);
  410. memset(chan->sq, 0, sizeof(struct hisi_dma_sqe) * hdma_dev->chan_depth);
  411. memset(chan->cq, 0, sizeof(struct hisi_dma_cqe) * hdma_dev->chan_depth);
  412. chan->sq_tail = 0;
  413. chan->cq_head = 0;
  414. chan->status = DISABLE;
  415. }
  416. static void hisi_dma_desc_free(struct virt_dma_desc *vd)
  417. {
  418. kfree(to_hisi_dma_desc(vd));
  419. }
  420. static struct dma_async_tx_descriptor *
  421. hisi_dma_prep_dma_memcpy(struct dma_chan *c, dma_addr_t dst, dma_addr_t src,
  422. size_t len, unsigned long flags)
  423. {
  424. struct hisi_dma_chan *chan = to_hisi_dma_chan(c);
  425. struct hisi_dma_desc *desc;
  426. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  427. if (!desc)
  428. return NULL;
  429. desc->sqe.length = cpu_to_le32(len);
  430. desc->sqe.src_addr = cpu_to_le64(src);
  431. desc->sqe.dst_addr = cpu_to_le64(dst);
  432. return vchan_tx_prep(&chan->vc, &desc->vd, flags);
  433. }
  434. static enum dma_status
  435. hisi_dma_tx_status(struct dma_chan *c, dma_cookie_t cookie,
  436. struct dma_tx_state *txstate)
  437. {
  438. return dma_cookie_status(c, cookie, txstate);
  439. }
  440. static void hisi_dma_start_transfer(struct hisi_dma_chan *chan)
  441. {
  442. struct hisi_dma_sqe *sqe = chan->sq + chan->sq_tail;
  443. struct hisi_dma_dev *hdma_dev = chan->hdma_dev;
  444. struct hisi_dma_desc *desc;
  445. struct virt_dma_desc *vd;
  446. vd = vchan_next_desc(&chan->vc);
  447. if (!vd) {
  448. chan->desc = NULL;
  449. return;
  450. }
  451. list_del(&vd->node);
  452. desc = to_hisi_dma_desc(vd);
  453. chan->desc = desc;
  454. memcpy(sqe, &desc->sqe, sizeof(struct hisi_dma_sqe));
  455. /* update other field in sqe */
  456. sqe->dw0 = cpu_to_le32(FIELD_PREP(OPCODE_MASK, OPCODE_M2M));
  457. sqe->dw0 |= cpu_to_le32(LOCAL_IRQ_EN);
  458. /* make sure data has been updated in sqe */
  459. wmb();
  460. /* update sq tail, point to new sqe position */
  461. chan->sq_tail = (chan->sq_tail + 1) % hdma_dev->chan_depth;
  462. /* update sq_tail to trigger a new task */
  463. hisi_dma_chan_write(hdma_dev->queue_base, HISI_DMA_Q_SQ_TAIL_PTR,
  464. chan->qp_num, chan->sq_tail);
  465. }
  466. static void hisi_dma_issue_pending(struct dma_chan *c)
  467. {
  468. struct hisi_dma_chan *chan = to_hisi_dma_chan(c);
  469. unsigned long flags;
  470. spin_lock_irqsave(&chan->vc.lock, flags);
  471. if (vchan_issue_pending(&chan->vc) && !chan->desc)
  472. hisi_dma_start_transfer(chan);
  473. spin_unlock_irqrestore(&chan->vc.lock, flags);
  474. }
  475. static int hisi_dma_terminate_all(struct dma_chan *c)
  476. {
  477. struct hisi_dma_chan *chan = to_hisi_dma_chan(c);
  478. unsigned long flags;
  479. LIST_HEAD(head);
  480. spin_lock_irqsave(&chan->vc.lock, flags);
  481. hisi_dma_pause_dma(chan->hdma_dev, chan->qp_num, true);
  482. if (chan->desc) {
  483. vchan_terminate_vdesc(&chan->desc->vd);
  484. chan->desc = NULL;
  485. }
  486. vchan_get_all_descriptors(&chan->vc, &head);
  487. spin_unlock_irqrestore(&chan->vc.lock, flags);
  488. vchan_dma_desc_free_list(&chan->vc, &head);
  489. hisi_dma_pause_dma(chan->hdma_dev, chan->qp_num, false);
  490. return 0;
  491. }
  492. static void hisi_dma_synchronize(struct dma_chan *c)
  493. {
  494. struct hisi_dma_chan *chan = to_hisi_dma_chan(c);
  495. vchan_synchronize(&chan->vc);
  496. }
  497. static int hisi_dma_alloc_qps_mem(struct hisi_dma_dev *hdma_dev)
  498. {
  499. size_t sq_size = sizeof(struct hisi_dma_sqe) * hdma_dev->chan_depth;
  500. size_t cq_size = sizeof(struct hisi_dma_cqe) * hdma_dev->chan_depth;
  501. struct device *dev = &hdma_dev->pdev->dev;
  502. struct hisi_dma_chan *chan;
  503. int i;
  504. for (i = 0; i < hdma_dev->chan_num; i++) {
  505. chan = &hdma_dev->chan[i];
  506. chan->sq = dmam_alloc_coherent(dev, sq_size, &chan->sq_dma,
  507. GFP_KERNEL);
  508. if (!chan->sq)
  509. return -ENOMEM;
  510. chan->cq = dmam_alloc_coherent(dev, cq_size, &chan->cq_dma,
  511. GFP_KERNEL);
  512. if (!chan->cq)
  513. return -ENOMEM;
  514. }
  515. return 0;
  516. }
  517. static void hisi_dma_init_hw_qp(struct hisi_dma_dev *hdma_dev, u32 index)
  518. {
  519. struct hisi_dma_chan *chan = &hdma_dev->chan[index];
  520. void __iomem *q_base = hdma_dev->queue_base;
  521. u32 hw_depth = hdma_dev->chan_depth - 1;
  522. void __iomem *addr;
  523. u32 tmp;
  524. /* set sq, cq base */
  525. hisi_dma_chan_write(q_base, HISI_DMA_Q_SQ_BASE_L, index,
  526. lower_32_bits(chan->sq_dma));
  527. hisi_dma_chan_write(q_base, HISI_DMA_Q_SQ_BASE_H, index,
  528. upper_32_bits(chan->sq_dma));
  529. hisi_dma_chan_write(q_base, HISI_DMA_Q_CQ_BASE_L, index,
  530. lower_32_bits(chan->cq_dma));
  531. hisi_dma_chan_write(q_base, HISI_DMA_Q_CQ_BASE_H, index,
  532. upper_32_bits(chan->cq_dma));
  533. /* set sq, cq depth */
  534. hisi_dma_chan_write(q_base, HISI_DMA_Q_SQ_DEPTH, index, hw_depth);
  535. hisi_dma_chan_write(q_base, HISI_DMA_Q_CQ_DEPTH, index, hw_depth);
  536. /* init sq tail and cq head */
  537. hisi_dma_chan_write(q_base, HISI_DMA_Q_SQ_TAIL_PTR, index, 0);
  538. hisi_dma_chan_write(q_base, HISI_DMA_Q_CQ_HEAD_PTR, index, 0);
  539. /* init error interrupt stats */
  540. hisi_dma_chan_write(q_base, HISI_DMA_Q_ERR_INT_NUM0, index, 0);
  541. hisi_dma_chan_write(q_base, HISI_DMA_Q_ERR_INT_NUM1, index, 0);
  542. hisi_dma_chan_write(q_base, HISI_DMA_Q_ERR_INT_NUM2, index, 0);
  543. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP08) {
  544. hisi_dma_chan_write(q_base, HISI_DMA_HIP08_Q_ERR_INT_NUM3,
  545. index, 0);
  546. hisi_dma_chan_write(q_base, HISI_DMA_HIP08_Q_ERR_INT_NUM4,
  547. index, 0);
  548. hisi_dma_chan_write(q_base, HISI_DMA_HIP08_Q_ERR_INT_NUM5,
  549. index, 0);
  550. hisi_dma_chan_write(q_base, HISI_DMA_HIP08_Q_ERR_INT_NUM6,
  551. index, 0);
  552. /*
  553. * init SQ/CQ direction selecting register.
  554. * "0" is to local side and "1" is to remote side.
  555. */
  556. addr = q_base + HISI_DMA_Q_CTRL0 + index * HISI_DMA_Q_OFFSET;
  557. hisi_dma_update_bit(addr, HISI_DMA_HIP08_Q_CTRL0_SQCQ_DRCT, 0);
  558. /*
  559. * 0 - Continue to next descriptor if error occurs.
  560. * 1 - Abort the DMA queue if error occurs.
  561. */
  562. hisi_dma_update_bit(addr,
  563. HISI_DMA_HIP08_Q_CTRL0_ERR_ABORT_EN, 0);
  564. } else {
  565. addr = q_base + HISI_DMA_Q_CTRL0 + index * HISI_DMA_Q_OFFSET;
  566. /*
  567. * init SQ/CQ direction selecting register.
  568. * "0" is to local side and "1" is to remote side.
  569. */
  570. hisi_dma_update_bit(addr, HISI_DMA_HIP09_Q_CTRL0_SQ_DRCT, 0);
  571. hisi_dma_update_bit(addr, HISI_DMA_HIP09_Q_CTRL0_CQ_DRCT, 0);
  572. /*
  573. * 0 - Continue to next descriptor if error occurs.
  574. * 1 - Abort the DMA queue if error occurs.
  575. */
  576. tmp = readl_relaxed(addr);
  577. tmp &= ~HISI_DMA_HIP09_Q_CTRL0_ERR_ABORT_EN;
  578. writel_relaxed(tmp, addr);
  579. /*
  580. * 0 - dma should process FLR whith CPU.
  581. * 1 - dma not process FLR, only cpu process FLR.
  582. */
  583. addr = q_base + HISI_DMA_HIP09_DMA_FLR_DISABLE +
  584. index * HISI_DMA_Q_OFFSET;
  585. hisi_dma_update_bit(addr, HISI_DMA_HIP09_DMA_FLR_DISABLE_B, 0);
  586. addr = q_base + HISI_DMA_Q_CTRL1 + index * HISI_DMA_Q_OFFSET;
  587. hisi_dma_update_bit(addr, HISI_DMA_HIP09_Q_CTRL1_VA_ENABLE, 1);
  588. }
  589. }
  590. static void hisi_dma_enable_qp(struct hisi_dma_dev *hdma_dev, u32 qp_index)
  591. {
  592. hisi_dma_init_hw_qp(hdma_dev, qp_index);
  593. hisi_dma_unmask_irq(hdma_dev, qp_index);
  594. hisi_dma_enable_dma(hdma_dev, qp_index, true);
  595. }
  596. static void hisi_dma_disable_qp(struct hisi_dma_dev *hdma_dev, u32 qp_index)
  597. {
  598. hisi_dma_reset_or_disable_hw_chan(&hdma_dev->chan[qp_index], true);
  599. }
  600. static void hisi_dma_enable_qps(struct hisi_dma_dev *hdma_dev)
  601. {
  602. int i;
  603. for (i = 0; i < hdma_dev->chan_num; i++) {
  604. hdma_dev->chan[i].qp_num = i;
  605. hdma_dev->chan[i].hdma_dev = hdma_dev;
  606. hdma_dev->chan[i].vc.desc_free = hisi_dma_desc_free;
  607. vchan_init(&hdma_dev->chan[i].vc, &hdma_dev->dma_dev);
  608. hisi_dma_enable_qp(hdma_dev, i);
  609. }
  610. }
  611. static void hisi_dma_disable_qps(struct hisi_dma_dev *hdma_dev)
  612. {
  613. int i;
  614. for (i = 0; i < hdma_dev->chan_num; i++) {
  615. hisi_dma_disable_qp(hdma_dev, i);
  616. tasklet_kill(&hdma_dev->chan[i].vc.task);
  617. }
  618. }
  619. static irqreturn_t hisi_dma_irq(int irq, void *data)
  620. {
  621. struct hisi_dma_chan *chan = data;
  622. struct hisi_dma_dev *hdma_dev = chan->hdma_dev;
  623. struct hisi_dma_desc *desc;
  624. struct hisi_dma_cqe *cqe;
  625. void __iomem *q_base;
  626. spin_lock(&chan->vc.lock);
  627. desc = chan->desc;
  628. cqe = chan->cq + chan->cq_head;
  629. q_base = hdma_dev->queue_base;
  630. if (desc) {
  631. chan->cq_head = (chan->cq_head + 1) % hdma_dev->chan_depth;
  632. hisi_dma_chan_write(q_base, HISI_DMA_Q_CQ_HEAD_PTR,
  633. chan->qp_num, chan->cq_head);
  634. if (FIELD_GET(STATUS_MASK, cqe->w0) == STATUS_SUCC) {
  635. vchan_cookie_complete(&desc->vd);
  636. hisi_dma_start_transfer(chan);
  637. } else {
  638. dev_err(&hdma_dev->pdev->dev, "task error!\n");
  639. }
  640. }
  641. spin_unlock(&chan->vc.lock);
  642. return IRQ_HANDLED;
  643. }
  644. static int hisi_dma_request_qps_irq(struct hisi_dma_dev *hdma_dev)
  645. {
  646. struct pci_dev *pdev = hdma_dev->pdev;
  647. int i, ret;
  648. for (i = 0; i < hdma_dev->chan_num; i++) {
  649. ret = devm_request_irq(&pdev->dev, pci_irq_vector(pdev, i),
  650. hisi_dma_irq, IRQF_SHARED, "hisi_dma",
  651. &hdma_dev->chan[i]);
  652. if (ret)
  653. return ret;
  654. }
  655. return 0;
  656. }
  657. /* This function enables all hw channels in a device */
  658. static int hisi_dma_enable_hw_channels(struct hisi_dma_dev *hdma_dev)
  659. {
  660. int ret;
  661. ret = hisi_dma_alloc_qps_mem(hdma_dev);
  662. if (ret) {
  663. dev_err(&hdma_dev->pdev->dev, "fail to allocate qp memory!\n");
  664. return ret;
  665. }
  666. ret = hisi_dma_request_qps_irq(hdma_dev);
  667. if (ret) {
  668. dev_err(&hdma_dev->pdev->dev, "fail to request qp irq!\n");
  669. return ret;
  670. }
  671. hisi_dma_enable_qps(hdma_dev);
  672. return 0;
  673. }
  674. static void hisi_dma_disable_hw_channels(void *data)
  675. {
  676. hisi_dma_disable_qps(data);
  677. }
  678. static void hisi_dma_set_mode(struct hisi_dma_dev *hdma_dev,
  679. enum hisi_dma_mode mode)
  680. {
  681. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP08)
  682. writel_relaxed(mode == RC ? 1 : 0,
  683. hdma_dev->base + HISI_DMA_HIP08_MODE);
  684. }
  685. static void hisi_dma_init_hw(struct hisi_dma_dev *hdma_dev)
  686. {
  687. void __iomem *addr;
  688. int i;
  689. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP09) {
  690. for (i = 0; i < HISI_DMA_HIP09_MAX_PORT_NUM; i++) {
  691. addr = hdma_dev->base + HISI_DMA_HIP09_PORT_CFG_REG(i);
  692. hisi_dma_update_bit(addr,
  693. HISI_DMA_HIP09_PORT_CFG_LINK_DOWN_MASK_B, 1);
  694. }
  695. }
  696. }
  697. static void hisi_dma_init_dma_dev(struct hisi_dma_dev *hdma_dev)
  698. {
  699. struct dma_device *dma_dev;
  700. dma_dev = &hdma_dev->dma_dev;
  701. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  702. dma_dev->device_free_chan_resources = hisi_dma_free_chan_resources;
  703. dma_dev->device_prep_dma_memcpy = hisi_dma_prep_dma_memcpy;
  704. dma_dev->device_tx_status = hisi_dma_tx_status;
  705. dma_dev->device_issue_pending = hisi_dma_issue_pending;
  706. dma_dev->device_terminate_all = hisi_dma_terminate_all;
  707. dma_dev->device_synchronize = hisi_dma_synchronize;
  708. dma_dev->directions = BIT(DMA_MEM_TO_MEM);
  709. dma_dev->dev = &hdma_dev->pdev->dev;
  710. INIT_LIST_HEAD(&dma_dev->channels);
  711. }
  712. /* --- debugfs implementation --- */
  713. #ifdef CONFIG_DEBUG_FS
  714. #include <linux/debugfs.h>
  715. static struct debugfs_reg32 *hisi_dma_get_ch_regs(struct hisi_dma_dev *hdma_dev,
  716. u32 *regs_sz)
  717. {
  718. struct device *dev = &hdma_dev->pdev->dev;
  719. struct debugfs_reg32 *regs;
  720. u32 regs_sz_comm;
  721. regs_sz_comm = ARRAY_SIZE(hisi_dma_comm_chan_regs);
  722. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP08)
  723. *regs_sz = regs_sz_comm + ARRAY_SIZE(hisi_dma_hip08_chan_regs);
  724. else
  725. *regs_sz = regs_sz_comm + ARRAY_SIZE(hisi_dma_hip09_chan_regs);
  726. regs = devm_kcalloc(dev, *regs_sz, sizeof(struct debugfs_reg32),
  727. GFP_KERNEL);
  728. if (!regs)
  729. return NULL;
  730. memcpy(regs, hisi_dma_comm_chan_regs, sizeof(hisi_dma_comm_chan_regs));
  731. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP08)
  732. memcpy(regs + regs_sz_comm, hisi_dma_hip08_chan_regs,
  733. sizeof(hisi_dma_hip08_chan_regs));
  734. else
  735. memcpy(regs + regs_sz_comm, hisi_dma_hip09_chan_regs,
  736. sizeof(hisi_dma_hip09_chan_regs));
  737. return regs;
  738. }
  739. static int hisi_dma_create_chan_dir(struct hisi_dma_dev *hdma_dev)
  740. {
  741. char dir_name[HISI_DMA_MAX_DIR_NAME_LEN];
  742. struct debugfs_regset32 *regsets;
  743. struct debugfs_reg32 *regs;
  744. struct dentry *chan_dir;
  745. struct device *dev;
  746. u32 regs_sz;
  747. int ret;
  748. int i;
  749. dev = &hdma_dev->pdev->dev;
  750. regsets = devm_kcalloc(dev, hdma_dev->chan_num,
  751. sizeof(*regsets), GFP_KERNEL);
  752. if (!regsets)
  753. return -ENOMEM;
  754. regs = hisi_dma_get_ch_regs(hdma_dev, &regs_sz);
  755. if (!regs)
  756. return -ENOMEM;
  757. for (i = 0; i < hdma_dev->chan_num; i++) {
  758. regsets[i].regs = regs;
  759. regsets[i].nregs = regs_sz;
  760. regsets[i].base = hdma_dev->queue_base + i * HISI_DMA_Q_OFFSET;
  761. regsets[i].dev = dev;
  762. memset(dir_name, 0, HISI_DMA_MAX_DIR_NAME_LEN);
  763. ret = sprintf(dir_name, "channel%d", i);
  764. if (ret < 0)
  765. return ret;
  766. chan_dir = debugfs_create_dir(dir_name,
  767. hdma_dev->dma_dev.dbg_dev_root);
  768. debugfs_create_regset32("regs", 0444, chan_dir, &regsets[i]);
  769. }
  770. return 0;
  771. }
  772. static void hisi_dma_create_debugfs(struct hisi_dma_dev *hdma_dev)
  773. {
  774. struct debugfs_regset32 *regset;
  775. struct device *dev;
  776. int ret;
  777. dev = &hdma_dev->pdev->dev;
  778. if (hdma_dev->dma_dev.dbg_dev_root == NULL)
  779. return;
  780. regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
  781. if (!regset)
  782. return;
  783. if (hdma_dev->reg_layout == HISI_DMA_REG_LAYOUT_HIP08) {
  784. regset->regs = hisi_dma_hip08_comm_regs;
  785. regset->nregs = ARRAY_SIZE(hisi_dma_hip08_comm_regs);
  786. } else {
  787. regset->regs = hisi_dma_hip09_comm_regs;
  788. regset->nregs = ARRAY_SIZE(hisi_dma_hip09_comm_regs);
  789. }
  790. regset->base = hdma_dev->base;
  791. regset->dev = dev;
  792. debugfs_create_regset32("regs", 0444,
  793. hdma_dev->dma_dev.dbg_dev_root, regset);
  794. ret = hisi_dma_create_chan_dir(hdma_dev);
  795. if (ret < 0)
  796. dev_info(&hdma_dev->pdev->dev, "fail to create debugfs for channels!\n");
  797. }
  798. #else
  799. static void hisi_dma_create_debugfs(struct hisi_dma_dev *hdma_dev) { }
  800. #endif /* CONFIG_DEBUG_FS*/
  801. /* --- debugfs implementation --- */
  802. static int hisi_dma_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  803. {
  804. enum hisi_dma_reg_layout reg_layout;
  805. struct device *dev = &pdev->dev;
  806. struct hisi_dma_dev *hdma_dev;
  807. struct dma_device *dma_dev;
  808. u32 chan_num;
  809. u32 msi_num;
  810. int ret;
  811. reg_layout = hisi_dma_get_reg_layout(pdev);
  812. if (reg_layout == HISI_DMA_REG_LAYOUT_INVALID) {
  813. dev_err(dev, "unsupported device!\n");
  814. return -EINVAL;
  815. }
  816. ret = pcim_enable_device(pdev);
  817. if (ret) {
  818. dev_err(dev, "failed to enable device mem!\n");
  819. return ret;
  820. }
  821. ret = pcim_iomap_regions(pdev, 1 << PCI_BAR_2, pci_name(pdev));
  822. if (ret) {
  823. dev_err(dev, "failed to remap I/O region!\n");
  824. return ret;
  825. }
  826. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  827. if (ret)
  828. return ret;
  829. chan_num = hisi_dma_get_chan_num(pdev);
  830. hdma_dev = devm_kzalloc(dev, struct_size(hdma_dev, chan, chan_num),
  831. GFP_KERNEL);
  832. if (!hdma_dev)
  833. return -EINVAL;
  834. hdma_dev->base = pcim_iomap_table(pdev)[PCI_BAR_2];
  835. hdma_dev->pdev = pdev;
  836. hdma_dev->chan_depth = HISI_DMA_Q_DEPTH_VAL;
  837. hdma_dev->chan_num = chan_num;
  838. hdma_dev->reg_layout = reg_layout;
  839. hdma_dev->queue_base = hdma_dev->base + hisi_dma_get_queue_base(pdev);
  840. pci_set_drvdata(pdev, hdma_dev);
  841. pci_set_master(pdev);
  842. msi_num = hisi_dma_get_msi_num(pdev);
  843. /* This will be freed by 'pcim_release()'. See 'pcim_enable_device()' */
  844. ret = pci_alloc_irq_vectors(pdev, msi_num, msi_num, PCI_IRQ_MSI);
  845. if (ret < 0) {
  846. dev_err(dev, "Failed to allocate MSI vectors!\n");
  847. return ret;
  848. }
  849. hisi_dma_init_dma_dev(hdma_dev);
  850. hisi_dma_set_mode(hdma_dev, RC);
  851. hisi_dma_init_hw(hdma_dev);
  852. ret = hisi_dma_enable_hw_channels(hdma_dev);
  853. if (ret < 0) {
  854. dev_err(dev, "failed to enable hw channel!\n");
  855. return ret;
  856. }
  857. ret = devm_add_action_or_reset(dev, hisi_dma_disable_hw_channels,
  858. hdma_dev);
  859. if (ret)
  860. return ret;
  861. dma_dev = &hdma_dev->dma_dev;
  862. ret = dmaenginem_async_device_register(dma_dev);
  863. if (ret < 0) {
  864. dev_err(dev, "failed to register device!\n");
  865. return ret;
  866. }
  867. hisi_dma_create_debugfs(hdma_dev);
  868. return 0;
  869. }
  870. static const struct pci_device_id hisi_dma_pci_tbl[] = {
  871. { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, 0xa122) },
  872. { 0, }
  873. };
  874. static struct pci_driver hisi_dma_pci_driver = {
  875. .name = "hisi_dma",
  876. .id_table = hisi_dma_pci_tbl,
  877. .probe = hisi_dma_probe,
  878. };
  879. module_pci_driver(hisi_dma_pci_driver);
  880. MODULE_AUTHOR("Zhou Wang <[email protected]>");
  881. MODULE_AUTHOR("Zhenfa Qiu <[email protected]>");
  882. MODULE_DESCRIPTION("HiSilicon Kunpeng DMA controller driver");
  883. MODULE_LICENSE("GPL v2");
  884. MODULE_DEVICE_TABLE(pci, hisi_dma_pci_tbl);