mtk-hsdma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-2018 MediaTek Inc.
  3. /*
  4. * Driver for MediaTek High-Speed DMA Controller
  5. *
  6. * Author: Sean Wang <sean.wang@mediatek.com>
  7. *
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/clk.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/err.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_dma.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/refcount.h>
  22. #include <linux/slab.h>
  23. #include "../virt-dma.h"
  24. #define MTK_HSDMA_USEC_POLL 20
  25. #define MTK_HSDMA_TIMEOUT_POLL 200000
  26. #define MTK_HSDMA_DMA_BUSWIDTHS BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  27. /* The default number of virtual channel */
  28. #define MTK_HSDMA_NR_VCHANS 3
  29. /* Only one physical channel supported */
  30. #define MTK_HSDMA_NR_MAX_PCHANS 1
  31. /* Macro for physical descriptor (PD) manipulation */
  32. /* The number of PD which must be 2 of power */
  33. #define MTK_DMA_SIZE 64
  34. #define MTK_HSDMA_NEXT_DESP_IDX(x, y) (((x) + 1) & ((y) - 1))
  35. #define MTK_HSDMA_LAST_DESP_IDX(x, y) (((x) - 1) & ((y) - 1))
  36. #define MTK_HSDMA_MAX_LEN 0x3f80
  37. #define MTK_HSDMA_ALIGN_SIZE 4
  38. #define MTK_HSDMA_PLEN_MASK 0x3fff
  39. #define MTK_HSDMA_DESC_PLEN(x) (((x) & MTK_HSDMA_PLEN_MASK) << 16)
  40. #define MTK_HSDMA_DESC_PLEN_GET(x) (((x) >> 16) & MTK_HSDMA_PLEN_MASK)
  41. /* Registers for underlying ring manipulation */
  42. #define MTK_HSDMA_TX_BASE 0x0
  43. #define MTK_HSDMA_TX_CNT 0x4
  44. #define MTK_HSDMA_TX_CPU 0x8
  45. #define MTK_HSDMA_TX_DMA 0xc
  46. #define MTK_HSDMA_RX_BASE 0x100
  47. #define MTK_HSDMA_RX_CNT 0x104
  48. #define MTK_HSDMA_RX_CPU 0x108
  49. #define MTK_HSDMA_RX_DMA 0x10c
  50. /* Registers for global setup */
  51. #define MTK_HSDMA_GLO 0x204
  52. #define MTK_HSDMA_GLO_MULTI_DMA BIT(10)
  53. #define MTK_HSDMA_TX_WB_DDONE BIT(6)
  54. #define MTK_HSDMA_BURST_64BYTES (0x2 << 4)
  55. #define MTK_HSDMA_GLO_RX_BUSY BIT(3)
  56. #define MTK_HSDMA_GLO_RX_DMA BIT(2)
  57. #define MTK_HSDMA_GLO_TX_BUSY BIT(1)
  58. #define MTK_HSDMA_GLO_TX_DMA BIT(0)
  59. #define MTK_HSDMA_GLO_DMA (MTK_HSDMA_GLO_TX_DMA | \
  60. MTK_HSDMA_GLO_RX_DMA)
  61. #define MTK_HSDMA_GLO_BUSY (MTK_HSDMA_GLO_RX_BUSY | \
  62. MTK_HSDMA_GLO_TX_BUSY)
  63. #define MTK_HSDMA_GLO_DEFAULT (MTK_HSDMA_GLO_TX_DMA | \
  64. MTK_HSDMA_GLO_RX_DMA | \
  65. MTK_HSDMA_TX_WB_DDONE | \
  66. MTK_HSDMA_BURST_64BYTES | \
  67. MTK_HSDMA_GLO_MULTI_DMA)
  68. /* Registers for reset */
  69. #define MTK_HSDMA_RESET 0x208
  70. #define MTK_HSDMA_RST_TX BIT(0)
  71. #define MTK_HSDMA_RST_RX BIT(16)
  72. /* Registers for interrupt control */
  73. #define MTK_HSDMA_DLYINT 0x20c
  74. #define MTK_HSDMA_RXDLY_INT_EN BIT(15)
  75. /* Interrupt fires when the pending number's more than the specified */
  76. #define MTK_HSDMA_RXMAX_PINT(x) (((x) & 0x7f) << 8)
  77. /* Interrupt fires when the pending time's more than the specified in 20 us */
  78. #define MTK_HSDMA_RXMAX_PTIME(x) ((x) & 0x7f)
  79. #define MTK_HSDMA_DLYINT_DEFAULT (MTK_HSDMA_RXDLY_INT_EN | \
  80. MTK_HSDMA_RXMAX_PINT(20) | \
  81. MTK_HSDMA_RXMAX_PTIME(20))
  82. #define MTK_HSDMA_INT_STATUS 0x220
  83. #define MTK_HSDMA_INT_ENABLE 0x228
  84. #define MTK_HSDMA_INT_RXDONE BIT(16)
  85. enum mtk_hsdma_vdesc_flag {
  86. MTK_HSDMA_VDESC_FINISHED = 0x01,
  87. };
  88. #define IS_MTK_HSDMA_VDESC_FINISHED(x) ((x) == MTK_HSDMA_VDESC_FINISHED)
  89. /**
  90. * struct mtk_hsdma_pdesc - This is the struct holding info describing physical
  91. * descriptor (PD) and its placement must be kept at
  92. * 4-bytes alignment in little endian order.
  93. * @desc1: | The control pad used to indicate hardware how to
  94. * @desc2: | deal with the descriptor such as source and
  95. * @desc3: | destination address and data length. The maximum
  96. * @desc4: | data length each pdesc can handle is 0x3f80 bytes
  97. */
  98. struct mtk_hsdma_pdesc {
  99. __le32 desc1;
  100. __le32 desc2;
  101. __le32 desc3;
  102. __le32 desc4;
  103. } __packed __aligned(4);
  104. /**
  105. * struct mtk_hsdma_vdesc - This is the struct holding info describing virtual
  106. * descriptor (VD)
  107. * @vd: An instance for struct virt_dma_desc
  108. * @len: The total data size device wants to move
  109. * @residue: The remaining data size device will move
  110. * @dest: The destination address device wants to move to
  111. * @src: The source address device wants to move from
  112. */
  113. struct mtk_hsdma_vdesc {
  114. struct virt_dma_desc vd;
  115. size_t len;
  116. size_t residue;
  117. dma_addr_t dest;
  118. dma_addr_t src;
  119. };
  120. /**
  121. * struct mtk_hsdma_cb - This is the struct holding extra info required for RX
  122. * ring to know what relevant VD the PD is being
  123. * mapped to.
  124. * @vd: Pointer to the relevant VD.
  125. * @flag: Flag indicating what action should be taken when VD
  126. * is completed.
  127. */
  128. struct mtk_hsdma_cb {
  129. struct virt_dma_desc *vd;
  130. enum mtk_hsdma_vdesc_flag flag;
  131. };
  132. /**
  133. * struct mtk_hsdma_ring - This struct holds info describing underlying ring
  134. * space
  135. * @txd: The descriptor TX ring which describes DMA source
  136. * information
  137. * @rxd: The descriptor RX ring which describes DMA
  138. * destination information
  139. * @cb: The extra information pointed at by RX ring
  140. * @tphys: The physical addr of TX ring
  141. * @rphys: The physical addr of RX ring
  142. * @cur_tptr: Pointer to the next free descriptor used by the host
  143. * @cur_rptr: Pointer to the last done descriptor by the device
  144. */
  145. struct mtk_hsdma_ring {
  146. struct mtk_hsdma_pdesc *txd;
  147. struct mtk_hsdma_pdesc *rxd;
  148. struct mtk_hsdma_cb *cb;
  149. dma_addr_t tphys;
  150. dma_addr_t rphys;
  151. u16 cur_tptr;
  152. u16 cur_rptr;
  153. };
  154. /**
  155. * struct mtk_hsdma_pchan - This is the struct holding info describing physical
  156. * channel (PC)
  157. * @ring: An instance for the underlying ring
  158. * @sz_ring: Total size allocated for the ring
  159. * @nr_free: Total number of free rooms in the ring. It would
  160. * be accessed and updated frequently between IRQ
  161. * context and user context to reflect whether ring
  162. * can accept requests from VD.
  163. */
  164. struct mtk_hsdma_pchan {
  165. struct mtk_hsdma_ring ring;
  166. size_t sz_ring;
  167. atomic_t nr_free;
  168. };
  169. /**
  170. * struct mtk_hsdma_vchan - This is the struct holding info describing virtual
  171. * channel (VC)
  172. * @vc: An instance for struct virt_dma_chan
  173. * @issue_completion: The wait for all issued descriptors completited
  174. * @issue_synchronize: Bool indicating channel synchronization starts
  175. * @desc_hw_processing: List those descriptors the hardware is processing,
  176. * which is protected by vc.lock
  177. */
  178. struct mtk_hsdma_vchan {
  179. struct virt_dma_chan vc;
  180. struct completion issue_completion;
  181. bool issue_synchronize;
  182. struct list_head desc_hw_processing;
  183. };
  184. /**
  185. * struct mtk_hsdma_soc - This is the struct holding differences among SoCs
  186. * @ddone: Bit mask for DDONE
  187. * @ls0: Bit mask for LS0
  188. */
  189. struct mtk_hsdma_soc {
  190. __le32 ddone;
  191. __le32 ls0;
  192. };
  193. /**
  194. * struct mtk_hsdma_device - This is the struct holding info describing HSDMA
  195. * device
  196. * @ddev: An instance for struct dma_device
  197. * @base: The mapped register I/O base
  198. * @clk: The clock that device internal is using
  199. * @irq: The IRQ that device are using
  200. * @dma_requests: The number of VCs the device supports to
  201. * @vc: The pointer to all available VCs
  202. * @pc: The pointer to the underlying PC
  203. * @pc_refcnt: Track how many VCs are using the PC
  204. * @lock: Lock protect agaisting multiple VCs access PC
  205. * @soc: The pointer to area holding differences among
  206. * various platform
  207. */
  208. struct mtk_hsdma_device {
  209. struct dma_device ddev;
  210. void __iomem *base;
  211. struct clk *clk;
  212. u32 irq;
  213. u32 dma_requests;
  214. struct mtk_hsdma_vchan *vc;
  215. struct mtk_hsdma_pchan *pc;
  216. refcount_t pc_refcnt;
  217. /* Lock used to protect against multiple VCs access PC */
  218. spinlock_t lock;
  219. const struct mtk_hsdma_soc *soc;
  220. };
  221. static struct mtk_hsdma_device *to_hsdma_dev(struct dma_chan *chan)
  222. {
  223. return container_of(chan->device, struct mtk_hsdma_device, ddev);
  224. }
  225. static inline struct mtk_hsdma_vchan *to_hsdma_vchan(struct dma_chan *chan)
  226. {
  227. return container_of(chan, struct mtk_hsdma_vchan, vc.chan);
  228. }
  229. static struct mtk_hsdma_vdesc *to_hsdma_vdesc(struct virt_dma_desc *vd)
  230. {
  231. return container_of(vd, struct mtk_hsdma_vdesc, vd);
  232. }
  233. static struct device *hsdma2dev(struct mtk_hsdma_device *hsdma)
  234. {
  235. return hsdma->ddev.dev;
  236. }
  237. static u32 mtk_dma_read(struct mtk_hsdma_device *hsdma, u32 reg)
  238. {
  239. return readl(hsdma->base + reg);
  240. }
  241. static void mtk_dma_write(struct mtk_hsdma_device *hsdma, u32 reg, u32 val)
  242. {
  243. writel(val, hsdma->base + reg);
  244. }
  245. static void mtk_dma_rmw(struct mtk_hsdma_device *hsdma, u32 reg,
  246. u32 mask, u32 set)
  247. {
  248. u32 val;
  249. val = mtk_dma_read(hsdma, reg);
  250. val &= ~mask;
  251. val |= set;
  252. mtk_dma_write(hsdma, reg, val);
  253. }
  254. static void mtk_dma_set(struct mtk_hsdma_device *hsdma, u32 reg, u32 val)
  255. {
  256. mtk_dma_rmw(hsdma, reg, 0, val);
  257. }
  258. static void mtk_dma_clr(struct mtk_hsdma_device *hsdma, u32 reg, u32 val)
  259. {
  260. mtk_dma_rmw(hsdma, reg, val, 0);
  261. }
  262. static void mtk_hsdma_vdesc_free(struct virt_dma_desc *vd)
  263. {
  264. kfree(container_of(vd, struct mtk_hsdma_vdesc, vd));
  265. }
  266. static int mtk_hsdma_busy_wait(struct mtk_hsdma_device *hsdma)
  267. {
  268. u32 status = 0;
  269. return readl_poll_timeout(hsdma->base + MTK_HSDMA_GLO, status,
  270. !(status & MTK_HSDMA_GLO_BUSY),
  271. MTK_HSDMA_USEC_POLL,
  272. MTK_HSDMA_TIMEOUT_POLL);
  273. }
  274. static int mtk_hsdma_alloc_pchan(struct mtk_hsdma_device *hsdma,
  275. struct mtk_hsdma_pchan *pc)
  276. {
  277. struct mtk_hsdma_ring *ring = &pc->ring;
  278. int err;
  279. memset(pc, 0, sizeof(*pc));
  280. /*
  281. * Allocate ring space where [0 ... MTK_DMA_SIZE - 1] is for TX ring
  282. * and [MTK_DMA_SIZE ... 2 * MTK_DMA_SIZE - 1] is for RX ring.
  283. */
  284. pc->sz_ring = 2 * MTK_DMA_SIZE * sizeof(*ring->txd);
  285. ring->txd = dma_alloc_coherent(hsdma2dev(hsdma), pc->sz_ring,
  286. &ring->tphys, GFP_NOWAIT);
  287. if (!ring->txd)
  288. return -ENOMEM;
  289. ring->rxd = &ring->txd[MTK_DMA_SIZE];
  290. ring->rphys = ring->tphys + MTK_DMA_SIZE * sizeof(*ring->txd);
  291. ring->cur_tptr = 0;
  292. ring->cur_rptr = MTK_DMA_SIZE - 1;
  293. ring->cb = kcalloc(MTK_DMA_SIZE, sizeof(*ring->cb), GFP_NOWAIT);
  294. if (!ring->cb) {
  295. err = -ENOMEM;
  296. goto err_free_dma;
  297. }
  298. atomic_set(&pc->nr_free, MTK_DMA_SIZE - 1);
  299. /* Disable HSDMA and wait for the completion */
  300. mtk_dma_clr(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DMA);
  301. err = mtk_hsdma_busy_wait(hsdma);
  302. if (err)
  303. goto err_free_cb;
  304. /* Reset */
  305. mtk_dma_set(hsdma, MTK_HSDMA_RESET,
  306. MTK_HSDMA_RST_TX | MTK_HSDMA_RST_RX);
  307. mtk_dma_clr(hsdma, MTK_HSDMA_RESET,
  308. MTK_HSDMA_RST_TX | MTK_HSDMA_RST_RX);
  309. /* Setup HSDMA initial pointer in the ring */
  310. mtk_dma_write(hsdma, MTK_HSDMA_TX_BASE, ring->tphys);
  311. mtk_dma_write(hsdma, MTK_HSDMA_TX_CNT, MTK_DMA_SIZE);
  312. mtk_dma_write(hsdma, MTK_HSDMA_TX_CPU, ring->cur_tptr);
  313. mtk_dma_write(hsdma, MTK_HSDMA_TX_DMA, 0);
  314. mtk_dma_write(hsdma, MTK_HSDMA_RX_BASE, ring->rphys);
  315. mtk_dma_write(hsdma, MTK_HSDMA_RX_CNT, MTK_DMA_SIZE);
  316. mtk_dma_write(hsdma, MTK_HSDMA_RX_CPU, ring->cur_rptr);
  317. mtk_dma_write(hsdma, MTK_HSDMA_RX_DMA, 0);
  318. /* Enable HSDMA */
  319. mtk_dma_set(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DMA);
  320. /* Setup delayed interrupt */
  321. mtk_dma_write(hsdma, MTK_HSDMA_DLYINT, MTK_HSDMA_DLYINT_DEFAULT);
  322. /* Enable interrupt */
  323. mtk_dma_set(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  324. return 0;
  325. err_free_cb:
  326. kfree(ring->cb);
  327. err_free_dma:
  328. dma_free_coherent(hsdma2dev(hsdma),
  329. pc->sz_ring, ring->txd, ring->tphys);
  330. return err;
  331. }
  332. static void mtk_hsdma_free_pchan(struct mtk_hsdma_device *hsdma,
  333. struct mtk_hsdma_pchan *pc)
  334. {
  335. struct mtk_hsdma_ring *ring = &pc->ring;
  336. /* Disable HSDMA and then wait for the completion */
  337. mtk_dma_clr(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DMA);
  338. mtk_hsdma_busy_wait(hsdma);
  339. /* Reset pointer in the ring */
  340. mtk_dma_clr(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  341. mtk_dma_write(hsdma, MTK_HSDMA_TX_BASE, 0);
  342. mtk_dma_write(hsdma, MTK_HSDMA_TX_CNT, 0);
  343. mtk_dma_write(hsdma, MTK_HSDMA_TX_CPU, 0);
  344. mtk_dma_write(hsdma, MTK_HSDMA_RX_BASE, 0);
  345. mtk_dma_write(hsdma, MTK_HSDMA_RX_CNT, 0);
  346. mtk_dma_write(hsdma, MTK_HSDMA_RX_CPU, MTK_DMA_SIZE - 1);
  347. kfree(ring->cb);
  348. dma_free_coherent(hsdma2dev(hsdma),
  349. pc->sz_ring, ring->txd, ring->tphys);
  350. }
  351. static int mtk_hsdma_issue_pending_vdesc(struct mtk_hsdma_device *hsdma,
  352. struct mtk_hsdma_pchan *pc,
  353. struct mtk_hsdma_vdesc *hvd)
  354. {
  355. struct mtk_hsdma_ring *ring = &pc->ring;
  356. struct mtk_hsdma_pdesc *txd, *rxd;
  357. u16 reserved, prev, tlen, num_sgs;
  358. unsigned long flags;
  359. /* Protect against PC is accessed by multiple VCs simultaneously */
  360. spin_lock_irqsave(&hsdma->lock, flags);
  361. /*
  362. * Reserve rooms, where pc->nr_free is used to track how many free
  363. * rooms in the ring being updated in user and IRQ context.
  364. */
  365. num_sgs = DIV_ROUND_UP(hvd->len, MTK_HSDMA_MAX_LEN);
  366. reserved = min_t(u16, num_sgs, atomic_read(&pc->nr_free));
  367. if (!reserved) {
  368. spin_unlock_irqrestore(&hsdma->lock, flags);
  369. return -ENOSPC;
  370. }
  371. atomic_sub(reserved, &pc->nr_free);
  372. while (reserved--) {
  373. /* Limit size by PD capability for valid data moving */
  374. tlen = (hvd->len > MTK_HSDMA_MAX_LEN) ?
  375. MTK_HSDMA_MAX_LEN : hvd->len;
  376. /*
  377. * Setup PDs using the remaining VD info mapped on those
  378. * reserved rooms. And since RXD is shared memory between the
  379. * host and the device allocated by dma_alloc_coherent call,
  380. * the helper macro WRITE_ONCE can ensure the data written to
  381. * RAM would really happens.
  382. */
  383. txd = &ring->txd[ring->cur_tptr];
  384. WRITE_ONCE(txd->desc1, hvd->src);
  385. WRITE_ONCE(txd->desc2,
  386. hsdma->soc->ls0 | MTK_HSDMA_DESC_PLEN(tlen));
  387. rxd = &ring->rxd[ring->cur_tptr];
  388. WRITE_ONCE(rxd->desc1, hvd->dest);
  389. WRITE_ONCE(rxd->desc2, MTK_HSDMA_DESC_PLEN(tlen));
  390. /* Associate VD, the PD belonged to */
  391. ring->cb[ring->cur_tptr].vd = &hvd->vd;
  392. /* Move forward the pointer of TX ring */
  393. ring->cur_tptr = MTK_HSDMA_NEXT_DESP_IDX(ring->cur_tptr,
  394. MTK_DMA_SIZE);
  395. /* Update VD with remaining data */
  396. hvd->src += tlen;
  397. hvd->dest += tlen;
  398. hvd->len -= tlen;
  399. }
  400. /*
  401. * Tagging flag for the last PD for VD will be responsible for
  402. * completing VD.
  403. */
  404. if (!hvd->len) {
  405. prev = MTK_HSDMA_LAST_DESP_IDX(ring->cur_tptr, MTK_DMA_SIZE);
  406. ring->cb[prev].flag = MTK_HSDMA_VDESC_FINISHED;
  407. }
  408. /* Ensure all changes indeed done before we're going on */
  409. wmb();
  410. /*
  411. * Updating into hardware the pointer of TX ring lets HSDMA to take
  412. * action for those pending PDs.
  413. */
  414. mtk_dma_write(hsdma, MTK_HSDMA_TX_CPU, ring->cur_tptr);
  415. spin_unlock_irqrestore(&hsdma->lock, flags);
  416. return 0;
  417. }
  418. static void mtk_hsdma_issue_vchan_pending(struct mtk_hsdma_device *hsdma,
  419. struct mtk_hsdma_vchan *hvc)
  420. {
  421. struct virt_dma_desc *vd, *vd2;
  422. int err;
  423. lockdep_assert_held(&hvc->vc.lock);
  424. list_for_each_entry_safe(vd, vd2, &hvc->vc.desc_issued, node) {
  425. struct mtk_hsdma_vdesc *hvd;
  426. hvd = to_hsdma_vdesc(vd);
  427. /* Map VD into PC and all VCs shares a single PC */
  428. err = mtk_hsdma_issue_pending_vdesc(hsdma, hsdma->pc, hvd);
  429. /*
  430. * Move VD from desc_issued to desc_hw_processing when entire
  431. * VD is fit into available PDs. Otherwise, the uncompleted
  432. * VDs would stay in list desc_issued and then restart the
  433. * processing as soon as possible once underlying ring space
  434. * got freed.
  435. */
  436. if (err == -ENOSPC || hvd->len > 0)
  437. break;
  438. /*
  439. * The extra list desc_hw_processing is used because
  440. * hardware can't provide sufficient information allowing us
  441. * to know what VDs are still working on the underlying ring.
  442. * Through the additional list, it can help us to implement
  443. * terminate_all, residue calculation and such thing needed
  444. * to know detail descriptor status on the hardware.
  445. */
  446. list_move_tail(&vd->node, &hvc->desc_hw_processing);
  447. }
  448. }
  449. static void mtk_hsdma_free_rooms_in_ring(struct mtk_hsdma_device *hsdma)
  450. {
  451. struct mtk_hsdma_vchan *hvc;
  452. struct mtk_hsdma_pdesc *rxd;
  453. struct mtk_hsdma_vdesc *hvd;
  454. struct mtk_hsdma_pchan *pc;
  455. struct mtk_hsdma_cb *cb;
  456. int i = MTK_DMA_SIZE;
  457. __le32 desc2;
  458. u32 status;
  459. u16 next;
  460. /* Read IRQ status */
  461. status = mtk_dma_read(hsdma, MTK_HSDMA_INT_STATUS);
  462. if (unlikely(!(status & MTK_HSDMA_INT_RXDONE)))
  463. goto rx_done;
  464. pc = hsdma->pc;
  465. /*
  466. * Using a fail-safe loop with iterations of up to MTK_DMA_SIZE to
  467. * reclaim these finished descriptors: The most number of PDs the ISR
  468. * can handle at one time shouldn't be more than MTK_DMA_SIZE so we
  469. * take it as limited count instead of just using a dangerous infinite
  470. * poll.
  471. */
  472. while (i--) {
  473. next = MTK_HSDMA_NEXT_DESP_IDX(pc->ring.cur_rptr,
  474. MTK_DMA_SIZE);
  475. rxd = &pc->ring.rxd[next];
  476. /*
  477. * If MTK_HSDMA_DESC_DDONE is no specified, that means data
  478. * moving for the PD is still under going.
  479. */
  480. desc2 = READ_ONCE(rxd->desc2);
  481. if (!(desc2 & hsdma->soc->ddone))
  482. break;
  483. cb = &pc->ring.cb[next];
  484. if (unlikely(!cb->vd)) {
  485. dev_err(hsdma2dev(hsdma), "cb->vd cannot be null\n");
  486. break;
  487. }
  488. /* Update residue of VD the associated PD belonged to */
  489. hvd = to_hsdma_vdesc(cb->vd);
  490. hvd->residue -= MTK_HSDMA_DESC_PLEN_GET(rxd->desc2);
  491. /* Complete VD until the relevant last PD is finished */
  492. if (IS_MTK_HSDMA_VDESC_FINISHED(cb->flag)) {
  493. hvc = to_hsdma_vchan(cb->vd->tx.chan);
  494. spin_lock(&hvc->vc.lock);
  495. /* Remove VD from list desc_hw_processing */
  496. list_del(&cb->vd->node);
  497. /* Add VD into list desc_completed */
  498. vchan_cookie_complete(cb->vd);
  499. if (hvc->issue_synchronize &&
  500. list_empty(&hvc->desc_hw_processing)) {
  501. complete(&hvc->issue_completion);
  502. hvc->issue_synchronize = false;
  503. }
  504. spin_unlock(&hvc->vc.lock);
  505. cb->flag = 0;
  506. }
  507. cb->vd = NULL;
  508. /*
  509. * Recycle the RXD with the helper WRITE_ONCE that can ensure
  510. * data written into RAM would really happens.
  511. */
  512. WRITE_ONCE(rxd->desc1, 0);
  513. WRITE_ONCE(rxd->desc2, 0);
  514. pc->ring.cur_rptr = next;
  515. /* Release rooms */
  516. atomic_inc(&pc->nr_free);
  517. }
  518. /* Ensure all changes indeed done before we're going on */
  519. wmb();
  520. /* Update CPU pointer for those completed PDs */
  521. mtk_dma_write(hsdma, MTK_HSDMA_RX_CPU, pc->ring.cur_rptr);
  522. /*
  523. * Acking the pending IRQ allows hardware no longer to keep the used
  524. * IRQ line in certain trigger state when software has completed all
  525. * the finished physical descriptors.
  526. */
  527. if (atomic_read(&pc->nr_free) >= MTK_DMA_SIZE - 1)
  528. mtk_dma_write(hsdma, MTK_HSDMA_INT_STATUS, status);
  529. /* ASAP handles pending VDs in all VCs after freeing some rooms */
  530. for (i = 0; i < hsdma->dma_requests; i++) {
  531. hvc = &hsdma->vc[i];
  532. spin_lock(&hvc->vc.lock);
  533. mtk_hsdma_issue_vchan_pending(hsdma, hvc);
  534. spin_unlock(&hvc->vc.lock);
  535. }
  536. rx_done:
  537. /* All completed PDs are cleaned up, so enable interrupt again */
  538. mtk_dma_set(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  539. }
  540. static irqreturn_t mtk_hsdma_irq(int irq, void *devid)
  541. {
  542. struct mtk_hsdma_device *hsdma = devid;
  543. /*
  544. * Disable interrupt until all completed PDs are cleaned up in
  545. * mtk_hsdma_free_rooms call.
  546. */
  547. mtk_dma_clr(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  548. mtk_hsdma_free_rooms_in_ring(hsdma);
  549. return IRQ_HANDLED;
  550. }
  551. static struct virt_dma_desc *mtk_hsdma_find_active_desc(struct dma_chan *c,
  552. dma_cookie_t cookie)
  553. {
  554. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  555. struct virt_dma_desc *vd;
  556. list_for_each_entry(vd, &hvc->desc_hw_processing, node)
  557. if (vd->tx.cookie == cookie)
  558. return vd;
  559. list_for_each_entry(vd, &hvc->vc.desc_issued, node)
  560. if (vd->tx.cookie == cookie)
  561. return vd;
  562. return NULL;
  563. }
  564. static enum dma_status mtk_hsdma_tx_status(struct dma_chan *c,
  565. dma_cookie_t cookie,
  566. struct dma_tx_state *txstate)
  567. {
  568. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  569. struct mtk_hsdma_vdesc *hvd;
  570. struct virt_dma_desc *vd;
  571. enum dma_status ret;
  572. unsigned long flags;
  573. size_t bytes = 0;
  574. ret = dma_cookie_status(c, cookie, txstate);
  575. if (ret == DMA_COMPLETE || !txstate)
  576. return ret;
  577. spin_lock_irqsave(&hvc->vc.lock, flags);
  578. vd = mtk_hsdma_find_active_desc(c, cookie);
  579. spin_unlock_irqrestore(&hvc->vc.lock, flags);
  580. if (vd) {
  581. hvd = to_hsdma_vdesc(vd);
  582. bytes = hvd->residue;
  583. }
  584. dma_set_residue(txstate, bytes);
  585. return ret;
  586. }
  587. static void mtk_hsdma_issue_pending(struct dma_chan *c)
  588. {
  589. struct mtk_hsdma_device *hsdma = to_hsdma_dev(c);
  590. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  591. unsigned long flags;
  592. spin_lock_irqsave(&hvc->vc.lock, flags);
  593. if (vchan_issue_pending(&hvc->vc))
  594. mtk_hsdma_issue_vchan_pending(hsdma, hvc);
  595. spin_unlock_irqrestore(&hvc->vc.lock, flags);
  596. }
  597. static struct dma_async_tx_descriptor *
  598. mtk_hsdma_prep_dma_memcpy(struct dma_chan *c, dma_addr_t dest,
  599. dma_addr_t src, size_t len, unsigned long flags)
  600. {
  601. struct mtk_hsdma_vdesc *hvd;
  602. hvd = kzalloc(sizeof(*hvd), GFP_NOWAIT);
  603. if (!hvd)
  604. return NULL;
  605. hvd->len = len;
  606. hvd->residue = len;
  607. hvd->src = src;
  608. hvd->dest = dest;
  609. return vchan_tx_prep(to_virt_chan(c), &hvd->vd, flags);
  610. }
  611. static int mtk_hsdma_free_inactive_desc(struct dma_chan *c)
  612. {
  613. struct virt_dma_chan *vc = to_virt_chan(c);
  614. unsigned long flags;
  615. LIST_HEAD(head);
  616. spin_lock_irqsave(&vc->lock, flags);
  617. list_splice_tail_init(&vc->desc_allocated, &head);
  618. list_splice_tail_init(&vc->desc_submitted, &head);
  619. list_splice_tail_init(&vc->desc_issued, &head);
  620. spin_unlock_irqrestore(&vc->lock, flags);
  621. /* At the point, we don't expect users put descriptor into VC again */
  622. vchan_dma_desc_free_list(vc, &head);
  623. return 0;
  624. }
  625. static void mtk_hsdma_free_active_desc(struct dma_chan *c)
  626. {
  627. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  628. bool sync_needed = false;
  629. /*
  630. * Once issue_synchronize is being set, which means once the hardware
  631. * consumes all descriptors for the channel in the ring, the
  632. * synchronization must be notified immediately it is completed.
  633. */
  634. spin_lock(&hvc->vc.lock);
  635. if (!list_empty(&hvc->desc_hw_processing)) {
  636. hvc->issue_synchronize = true;
  637. sync_needed = true;
  638. }
  639. spin_unlock(&hvc->vc.lock);
  640. if (sync_needed)
  641. wait_for_completion(&hvc->issue_completion);
  642. /*
  643. * At the point, we expect that all remaining descriptors in the ring
  644. * for the channel should be all processing done.
  645. */
  646. WARN_ONCE(!list_empty(&hvc->desc_hw_processing),
  647. "Desc pending still in list desc_hw_processing\n");
  648. /* Free all descriptors in list desc_completed */
  649. vchan_synchronize(&hvc->vc);
  650. WARN_ONCE(!list_empty(&hvc->vc.desc_completed),
  651. "Desc pending still in list desc_completed\n");
  652. }
  653. static int mtk_hsdma_terminate_all(struct dma_chan *c)
  654. {
  655. /*
  656. * Free pending descriptors not processed yet by hardware that have
  657. * previously been submitted to the channel.
  658. */
  659. mtk_hsdma_free_inactive_desc(c);
  660. /*
  661. * However, the DMA engine doesn't provide any way to stop these
  662. * descriptors being processed currently by hardware. The only way is
  663. * to just waiting until these descriptors are all processed completely
  664. * through mtk_hsdma_free_active_desc call.
  665. */
  666. mtk_hsdma_free_active_desc(c);
  667. return 0;
  668. }
  669. static int mtk_hsdma_alloc_chan_resources(struct dma_chan *c)
  670. {
  671. struct mtk_hsdma_device *hsdma = to_hsdma_dev(c);
  672. int err;
  673. /*
  674. * Since HSDMA has only one PC, the resource for PC is being allocated
  675. * when the first VC is being created and the other VCs would run on
  676. * the same PC.
  677. */
  678. if (!refcount_read(&hsdma->pc_refcnt)) {
  679. err = mtk_hsdma_alloc_pchan(hsdma, hsdma->pc);
  680. if (err)
  681. return err;
  682. /*
  683. * refcount_inc would complain increment on 0; use-after-free.
  684. * Thus, we need to explicitly set it as 1 initially.
  685. */
  686. refcount_set(&hsdma->pc_refcnt, 1);
  687. } else {
  688. refcount_inc(&hsdma->pc_refcnt);
  689. }
  690. return 0;
  691. }
  692. static void mtk_hsdma_free_chan_resources(struct dma_chan *c)
  693. {
  694. struct mtk_hsdma_device *hsdma = to_hsdma_dev(c);
  695. /* Free all descriptors in all lists on the VC */
  696. mtk_hsdma_terminate_all(c);
  697. /* The resource for PC is not freed until all the VCs are destroyed */
  698. if (!refcount_dec_and_test(&hsdma->pc_refcnt))
  699. return;
  700. mtk_hsdma_free_pchan(hsdma, hsdma->pc);
  701. }
  702. static int mtk_hsdma_hw_init(struct mtk_hsdma_device *hsdma)
  703. {
  704. int err;
  705. pm_runtime_enable(hsdma2dev(hsdma));
  706. pm_runtime_get_sync(hsdma2dev(hsdma));
  707. err = clk_prepare_enable(hsdma->clk);
  708. if (err)
  709. return err;
  710. mtk_dma_write(hsdma, MTK_HSDMA_INT_ENABLE, 0);
  711. mtk_dma_write(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DEFAULT);
  712. return 0;
  713. }
  714. static int mtk_hsdma_hw_deinit(struct mtk_hsdma_device *hsdma)
  715. {
  716. mtk_dma_write(hsdma, MTK_HSDMA_GLO, 0);
  717. clk_disable_unprepare(hsdma->clk);
  718. pm_runtime_put_sync(hsdma2dev(hsdma));
  719. pm_runtime_disable(hsdma2dev(hsdma));
  720. return 0;
  721. }
  722. static const struct mtk_hsdma_soc mt7623_soc = {
  723. .ddone = BIT(31),
  724. .ls0 = BIT(30),
  725. };
  726. static const struct mtk_hsdma_soc mt7622_soc = {
  727. .ddone = BIT(15),
  728. .ls0 = BIT(14),
  729. };
  730. static const struct of_device_id mtk_hsdma_match[] = {
  731. { .compatible = "mediatek,mt7623-hsdma", .data = &mt7623_soc},
  732. { .compatible = "mediatek,mt7622-hsdma", .data = &mt7622_soc},
  733. { /* sentinel */ }
  734. };
  735. MODULE_DEVICE_TABLE(of, mtk_hsdma_match);
  736. static int mtk_hsdma_probe(struct platform_device *pdev)
  737. {
  738. struct mtk_hsdma_device *hsdma;
  739. struct mtk_hsdma_vchan *vc;
  740. struct dma_device *dd;
  741. int i, err;
  742. hsdma = devm_kzalloc(&pdev->dev, sizeof(*hsdma), GFP_KERNEL);
  743. if (!hsdma)
  744. return -ENOMEM;
  745. dd = &hsdma->ddev;
  746. hsdma->base = devm_platform_ioremap_resource(pdev, 0);
  747. if (IS_ERR(hsdma->base))
  748. return PTR_ERR(hsdma->base);
  749. hsdma->soc = of_device_get_match_data(&pdev->dev);
  750. if (!hsdma->soc) {
  751. dev_err(&pdev->dev, "No device match found\n");
  752. return -ENODEV;
  753. }
  754. hsdma->clk = devm_clk_get(&pdev->dev, "hsdma");
  755. if (IS_ERR(hsdma->clk)) {
  756. dev_err(&pdev->dev, "No clock for %s\n",
  757. dev_name(&pdev->dev));
  758. return PTR_ERR(hsdma->clk);
  759. }
  760. err = platform_get_irq(pdev, 0);
  761. if (err < 0)
  762. return err;
  763. hsdma->irq = err;
  764. refcount_set(&hsdma->pc_refcnt, 0);
  765. spin_lock_init(&hsdma->lock);
  766. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  767. dd->copy_align = MTK_HSDMA_ALIGN_SIZE;
  768. dd->device_alloc_chan_resources = mtk_hsdma_alloc_chan_resources;
  769. dd->device_free_chan_resources = mtk_hsdma_free_chan_resources;
  770. dd->device_tx_status = mtk_hsdma_tx_status;
  771. dd->device_issue_pending = mtk_hsdma_issue_pending;
  772. dd->device_prep_dma_memcpy = mtk_hsdma_prep_dma_memcpy;
  773. dd->device_terminate_all = mtk_hsdma_terminate_all;
  774. dd->src_addr_widths = MTK_HSDMA_DMA_BUSWIDTHS;
  775. dd->dst_addr_widths = MTK_HSDMA_DMA_BUSWIDTHS;
  776. dd->directions = BIT(DMA_MEM_TO_MEM);
  777. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  778. dd->dev = &pdev->dev;
  779. INIT_LIST_HEAD(&dd->channels);
  780. hsdma->dma_requests = MTK_HSDMA_NR_VCHANS;
  781. if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
  782. "dma-requests",
  783. &hsdma->dma_requests)) {
  784. dev_info(&pdev->dev,
  785. "Using %u as missing dma-requests property\n",
  786. MTK_HSDMA_NR_VCHANS);
  787. }
  788. hsdma->pc = devm_kcalloc(&pdev->dev, MTK_HSDMA_NR_MAX_PCHANS,
  789. sizeof(*hsdma->pc), GFP_KERNEL);
  790. if (!hsdma->pc)
  791. return -ENOMEM;
  792. hsdma->vc = devm_kcalloc(&pdev->dev, hsdma->dma_requests,
  793. sizeof(*hsdma->vc), GFP_KERNEL);
  794. if (!hsdma->vc)
  795. return -ENOMEM;
  796. for (i = 0; i < hsdma->dma_requests; i++) {
  797. vc = &hsdma->vc[i];
  798. vc->vc.desc_free = mtk_hsdma_vdesc_free;
  799. vchan_init(&vc->vc, dd);
  800. init_completion(&vc->issue_completion);
  801. INIT_LIST_HEAD(&vc->desc_hw_processing);
  802. }
  803. err = dma_async_device_register(dd);
  804. if (err)
  805. return err;
  806. err = of_dma_controller_register(pdev->dev.of_node,
  807. of_dma_xlate_by_chan_id, hsdma);
  808. if (err) {
  809. dev_err(&pdev->dev,
  810. "MediaTek HSDMA OF registration failed %d\n", err);
  811. goto err_unregister;
  812. }
  813. mtk_hsdma_hw_init(hsdma);
  814. err = devm_request_irq(&pdev->dev, hsdma->irq,
  815. mtk_hsdma_irq, 0,
  816. dev_name(&pdev->dev), hsdma);
  817. if (err) {
  818. dev_err(&pdev->dev,
  819. "request_irq failed with err %d\n", err);
  820. goto err_free;
  821. }
  822. platform_set_drvdata(pdev, hsdma);
  823. dev_info(&pdev->dev, "MediaTek HSDMA driver registered\n");
  824. return 0;
  825. err_free:
  826. mtk_hsdma_hw_deinit(hsdma);
  827. of_dma_controller_free(pdev->dev.of_node);
  828. err_unregister:
  829. dma_async_device_unregister(dd);
  830. return err;
  831. }
  832. static void mtk_hsdma_remove(struct platform_device *pdev)
  833. {
  834. struct mtk_hsdma_device *hsdma = platform_get_drvdata(pdev);
  835. struct mtk_hsdma_vchan *vc;
  836. int i;
  837. /* Kill VC task */
  838. for (i = 0; i < hsdma->dma_requests; i++) {
  839. vc = &hsdma->vc[i];
  840. list_del(&vc->vc.chan.device_node);
  841. tasklet_kill(&vc->vc.task);
  842. }
  843. /* Disable DMA interrupt */
  844. mtk_dma_write(hsdma, MTK_HSDMA_INT_ENABLE, 0);
  845. /* Waits for any pending IRQ handlers to complete */
  846. synchronize_irq(hsdma->irq);
  847. /* Disable hardware */
  848. mtk_hsdma_hw_deinit(hsdma);
  849. dma_async_device_unregister(&hsdma->ddev);
  850. of_dma_controller_free(pdev->dev.of_node);
  851. }
  852. static struct platform_driver mtk_hsdma_driver = {
  853. .probe = mtk_hsdma_probe,
  854. .remove_new = mtk_hsdma_remove,
  855. .driver = {
  856. .name = KBUILD_MODNAME,
  857. .of_match_table = mtk_hsdma_match,
  858. },
  859. };
  860. module_platform_driver(mtk_hsdma_driver);
  861. MODULE_DESCRIPTION("MediaTek High-Speed DMA Controller Driver");
  862. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  863. MODULE_LICENSE("GPL v2");