mtk-cqdma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018-2019 MediaTek Inc.
  3. /*
  4. * Driver for MediaTek Command-Queue DMA Controller
  5. *
  6. * Author: Shun-Chih Yu <shun-chih.yu@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/interrupt.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/refcount.h>
  23. #include <linux/slab.h>
  24. #include "../virt-dma.h"
  25. #define MTK_CQDMA_USEC_POLL 10
  26. #define MTK_CQDMA_TIMEOUT_POLL 1000
  27. #define MTK_CQDMA_DMA_BUSWIDTHS BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  28. #define MTK_CQDMA_ALIGN_SIZE 1
  29. /* The default number of virtual channel */
  30. #define MTK_CQDMA_NR_VCHANS 32
  31. /* The default number of physical channel */
  32. #define MTK_CQDMA_NR_PCHANS 3
  33. /* Registers for underlying dma manipulation */
  34. #define MTK_CQDMA_INT_FLAG 0x0
  35. #define MTK_CQDMA_INT_EN 0x4
  36. #define MTK_CQDMA_EN 0x8
  37. #define MTK_CQDMA_RESET 0xc
  38. #define MTK_CQDMA_FLUSH 0x14
  39. #define MTK_CQDMA_SRC 0x1c
  40. #define MTK_CQDMA_DST 0x20
  41. #define MTK_CQDMA_LEN1 0x24
  42. #define MTK_CQDMA_LEN2 0x28
  43. #define MTK_CQDMA_SRC2 0x60
  44. #define MTK_CQDMA_DST2 0x64
  45. /* Registers setting */
  46. #define MTK_CQDMA_EN_BIT BIT(0)
  47. #define MTK_CQDMA_INT_FLAG_BIT BIT(0)
  48. #define MTK_CQDMA_INT_EN_BIT BIT(0)
  49. #define MTK_CQDMA_FLUSH_BIT BIT(0)
  50. #define MTK_CQDMA_WARM_RST_BIT BIT(0)
  51. #define MTK_CQDMA_HARD_RST_BIT BIT(1)
  52. #define MTK_CQDMA_MAX_LEN GENMASK(27, 0)
  53. #define MTK_CQDMA_ADDR_LIMIT GENMASK(31, 0)
  54. #define MTK_CQDMA_ADDR2_SHFIT (32)
  55. /**
  56. * struct mtk_cqdma_vdesc - The struct holding info describing virtual
  57. * descriptor (CVD)
  58. * @vd: An instance for struct virt_dma_desc
  59. * @len: The total data size device wants to move
  60. * @residue: The remaining data size device will move
  61. * @dest: The destination address device wants to move to
  62. * @src: The source address device wants to move from
  63. * @ch: The pointer to the corresponding dma channel
  64. * @node: The lise_head struct to build link-list for VDs
  65. * @parent: The pointer to the parent CVD
  66. */
  67. struct mtk_cqdma_vdesc {
  68. struct virt_dma_desc vd;
  69. size_t len;
  70. size_t residue;
  71. dma_addr_t dest;
  72. dma_addr_t src;
  73. struct dma_chan *ch;
  74. struct list_head node;
  75. struct mtk_cqdma_vdesc *parent;
  76. };
  77. /**
  78. * struct mtk_cqdma_pchan - The struct holding info describing physical
  79. * channel (PC)
  80. * @queue: Queue for the PDs issued to this PC
  81. * @base: The mapped register I/O base of this PC
  82. * @irq: The IRQ that this PC are using
  83. * @refcnt: Track how many VCs are using this PC
  84. * @tasklet: Tasklet for this PC
  85. * @lock: Lock protect agaisting multiple VCs access PC
  86. */
  87. struct mtk_cqdma_pchan {
  88. struct list_head queue;
  89. void __iomem *base;
  90. u32 irq;
  91. refcount_t refcnt;
  92. struct tasklet_struct tasklet;
  93. /* lock to protect PC */
  94. spinlock_t lock;
  95. };
  96. /**
  97. * struct mtk_cqdma_vchan - The struct holding info describing virtual
  98. * channel (VC)
  99. * @vc: An instance for struct virt_dma_chan
  100. * @pc: The pointer to the underlying PC
  101. * @issue_completion: The wait for all issued descriptors completited
  102. * @issue_synchronize: Bool indicating channel synchronization starts
  103. */
  104. struct mtk_cqdma_vchan {
  105. struct virt_dma_chan vc;
  106. struct mtk_cqdma_pchan *pc;
  107. struct completion issue_completion;
  108. bool issue_synchronize;
  109. };
  110. /**
  111. * struct mtk_cqdma_device - The struct holding info describing CQDMA
  112. * device
  113. * @ddev: An instance for struct dma_device
  114. * @clk: The clock that device internal is using
  115. * @dma_requests: The number of VCs the device supports to
  116. * @dma_channels: The number of PCs the device supports to
  117. * @vc: The pointer to all available VCs
  118. * @pc: The pointer to all the underlying PCs
  119. */
  120. struct mtk_cqdma_device {
  121. struct dma_device ddev;
  122. struct clk *clk;
  123. u32 dma_requests;
  124. u32 dma_channels;
  125. struct mtk_cqdma_vchan *vc;
  126. struct mtk_cqdma_pchan **pc;
  127. };
  128. static struct mtk_cqdma_device *to_cqdma_dev(struct dma_chan *chan)
  129. {
  130. return container_of(chan->device, struct mtk_cqdma_device, ddev);
  131. }
  132. static struct mtk_cqdma_vchan *to_cqdma_vchan(struct dma_chan *chan)
  133. {
  134. return container_of(chan, struct mtk_cqdma_vchan, vc.chan);
  135. }
  136. static struct mtk_cqdma_vdesc *to_cqdma_vdesc(struct virt_dma_desc *vd)
  137. {
  138. return container_of(vd, struct mtk_cqdma_vdesc, vd);
  139. }
  140. static struct device *cqdma2dev(struct mtk_cqdma_device *cqdma)
  141. {
  142. return cqdma->ddev.dev;
  143. }
  144. static u32 mtk_dma_read(struct mtk_cqdma_pchan *pc, u32 reg)
  145. {
  146. return readl(pc->base + reg);
  147. }
  148. static void mtk_dma_write(struct mtk_cqdma_pchan *pc, u32 reg, u32 val)
  149. {
  150. writel_relaxed(val, pc->base + reg);
  151. }
  152. static void mtk_dma_rmw(struct mtk_cqdma_pchan *pc, u32 reg,
  153. u32 mask, u32 set)
  154. {
  155. u32 val;
  156. val = mtk_dma_read(pc, reg);
  157. val &= ~mask;
  158. val |= set;
  159. mtk_dma_write(pc, reg, val);
  160. }
  161. static void mtk_dma_set(struct mtk_cqdma_pchan *pc, u32 reg, u32 val)
  162. {
  163. mtk_dma_rmw(pc, reg, 0, val);
  164. }
  165. static void mtk_dma_clr(struct mtk_cqdma_pchan *pc, u32 reg, u32 val)
  166. {
  167. mtk_dma_rmw(pc, reg, val, 0);
  168. }
  169. static void mtk_cqdma_vdesc_free(struct virt_dma_desc *vd)
  170. {
  171. kfree(to_cqdma_vdesc(vd));
  172. }
  173. static int mtk_cqdma_poll_engine_done(struct mtk_cqdma_pchan *pc, bool atomic)
  174. {
  175. u32 status = 0;
  176. if (!atomic)
  177. return readl_poll_timeout(pc->base + MTK_CQDMA_EN,
  178. status,
  179. !(status & MTK_CQDMA_EN_BIT),
  180. MTK_CQDMA_USEC_POLL,
  181. MTK_CQDMA_TIMEOUT_POLL);
  182. return readl_poll_timeout_atomic(pc->base + MTK_CQDMA_EN,
  183. status,
  184. !(status & MTK_CQDMA_EN_BIT),
  185. MTK_CQDMA_USEC_POLL,
  186. MTK_CQDMA_TIMEOUT_POLL);
  187. }
  188. static int mtk_cqdma_hard_reset(struct mtk_cqdma_pchan *pc)
  189. {
  190. mtk_dma_set(pc, MTK_CQDMA_RESET, MTK_CQDMA_HARD_RST_BIT);
  191. mtk_dma_clr(pc, MTK_CQDMA_RESET, MTK_CQDMA_HARD_RST_BIT);
  192. return mtk_cqdma_poll_engine_done(pc, true);
  193. }
  194. static void mtk_cqdma_start(struct mtk_cqdma_pchan *pc,
  195. struct mtk_cqdma_vdesc *cvd)
  196. {
  197. /* wait for the previous transaction done */
  198. if (mtk_cqdma_poll_engine_done(pc, true) < 0)
  199. dev_err(cqdma2dev(to_cqdma_dev(cvd->ch)), "cqdma wait transaction timeout\n");
  200. /* warm reset the dma engine for the new transaction */
  201. mtk_dma_set(pc, MTK_CQDMA_RESET, MTK_CQDMA_WARM_RST_BIT);
  202. if (mtk_cqdma_poll_engine_done(pc, true) < 0)
  203. dev_err(cqdma2dev(to_cqdma_dev(cvd->ch)), "cqdma warm reset timeout\n");
  204. /* setup the source */
  205. mtk_dma_set(pc, MTK_CQDMA_SRC, cvd->src & MTK_CQDMA_ADDR_LIMIT);
  206. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  207. mtk_dma_set(pc, MTK_CQDMA_SRC2, cvd->src >> MTK_CQDMA_ADDR2_SHFIT);
  208. #else
  209. mtk_dma_set(pc, MTK_CQDMA_SRC2, 0);
  210. #endif
  211. /* setup the destination */
  212. mtk_dma_set(pc, MTK_CQDMA_DST, cvd->dest & MTK_CQDMA_ADDR_LIMIT);
  213. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  214. mtk_dma_set(pc, MTK_CQDMA_DST2, cvd->dest >> MTK_CQDMA_ADDR2_SHFIT);
  215. #else
  216. mtk_dma_set(pc, MTK_CQDMA_DST2, 0);
  217. #endif
  218. /* setup the length */
  219. mtk_dma_set(pc, MTK_CQDMA_LEN1, cvd->len);
  220. /* start dma engine */
  221. mtk_dma_set(pc, MTK_CQDMA_EN, MTK_CQDMA_EN_BIT);
  222. }
  223. static void mtk_cqdma_issue_vchan_pending(struct mtk_cqdma_vchan *cvc)
  224. {
  225. struct virt_dma_desc *vd, *vd2;
  226. struct mtk_cqdma_pchan *pc = cvc->pc;
  227. struct mtk_cqdma_vdesc *cvd;
  228. bool trigger_engine = false;
  229. lockdep_assert_held(&cvc->vc.lock);
  230. lockdep_assert_held(&pc->lock);
  231. list_for_each_entry_safe(vd, vd2, &cvc->vc.desc_issued, node) {
  232. /* need to trigger dma engine if PC's queue is empty */
  233. if (list_empty(&pc->queue))
  234. trigger_engine = true;
  235. cvd = to_cqdma_vdesc(vd);
  236. /* add VD into PC's queue */
  237. list_add_tail(&cvd->node, &pc->queue);
  238. /* start the dma engine */
  239. if (trigger_engine)
  240. mtk_cqdma_start(pc, cvd);
  241. /* remove VD from list desc_issued */
  242. list_del(&vd->node);
  243. }
  244. }
  245. /*
  246. * return true if this VC is active,
  247. * meaning that there are VDs under processing by the PC
  248. */
  249. static bool mtk_cqdma_is_vchan_active(struct mtk_cqdma_vchan *cvc)
  250. {
  251. struct mtk_cqdma_vdesc *cvd;
  252. list_for_each_entry(cvd, &cvc->pc->queue, node)
  253. if (cvc == to_cqdma_vchan(cvd->ch))
  254. return true;
  255. return false;
  256. }
  257. /*
  258. * return the pointer of the CVD that is just consumed by the PC
  259. */
  260. static struct mtk_cqdma_vdesc
  261. *mtk_cqdma_consume_work_queue(struct mtk_cqdma_pchan *pc)
  262. {
  263. struct mtk_cqdma_vchan *cvc;
  264. struct mtk_cqdma_vdesc *cvd, *ret = NULL;
  265. /* consume a CVD from PC's queue */
  266. cvd = list_first_entry_or_null(&pc->queue,
  267. struct mtk_cqdma_vdesc, node);
  268. if (unlikely(!cvd || !cvd->parent))
  269. return NULL;
  270. cvc = to_cqdma_vchan(cvd->ch);
  271. ret = cvd;
  272. /* update residue of the parent CVD */
  273. cvd->parent->residue -= cvd->len;
  274. /* delete CVD from PC's queue */
  275. list_del(&cvd->node);
  276. spin_lock(&cvc->vc.lock);
  277. /* check whether all the child CVDs completed */
  278. if (!cvd->parent->residue) {
  279. /* add the parent VD into list desc_completed */
  280. vchan_cookie_complete(&cvd->parent->vd);
  281. /* setup completion if this VC is under synchronization */
  282. if (cvc->issue_synchronize && !mtk_cqdma_is_vchan_active(cvc)) {
  283. complete(&cvc->issue_completion);
  284. cvc->issue_synchronize = false;
  285. }
  286. }
  287. spin_unlock(&cvc->vc.lock);
  288. /* start transaction for next CVD in the queue */
  289. cvd = list_first_entry_or_null(&pc->queue,
  290. struct mtk_cqdma_vdesc, node);
  291. if (cvd)
  292. mtk_cqdma_start(pc, cvd);
  293. return ret;
  294. }
  295. static void mtk_cqdma_tasklet_cb(struct tasklet_struct *t)
  296. {
  297. struct mtk_cqdma_pchan *pc = from_tasklet(pc, t, tasklet);
  298. struct mtk_cqdma_vdesc *cvd = NULL;
  299. unsigned long flags;
  300. spin_lock_irqsave(&pc->lock, flags);
  301. /* consume the queue */
  302. cvd = mtk_cqdma_consume_work_queue(pc);
  303. spin_unlock_irqrestore(&pc->lock, flags);
  304. /* submit the next CVD */
  305. if (cvd) {
  306. dma_run_dependencies(&cvd->vd.tx);
  307. /*
  308. * free child CVD after completion.
  309. * the parent CVD would be freed with desc_free by user.
  310. */
  311. if (cvd->parent != cvd)
  312. kfree(cvd);
  313. }
  314. /* re-enable interrupt before leaving tasklet */
  315. enable_irq(pc->irq);
  316. }
  317. static irqreturn_t mtk_cqdma_irq(int irq, void *devid)
  318. {
  319. struct mtk_cqdma_device *cqdma = devid;
  320. irqreturn_t ret = IRQ_NONE;
  321. bool schedule_tasklet = false;
  322. u32 i;
  323. /* clear interrupt flags for each PC */
  324. for (i = 0; i < cqdma->dma_channels; ++i, schedule_tasklet = false) {
  325. spin_lock(&cqdma->pc[i]->lock);
  326. if (mtk_dma_read(cqdma->pc[i],
  327. MTK_CQDMA_INT_FLAG) & MTK_CQDMA_INT_FLAG_BIT) {
  328. /* clear interrupt */
  329. mtk_dma_clr(cqdma->pc[i], MTK_CQDMA_INT_FLAG,
  330. MTK_CQDMA_INT_FLAG_BIT);
  331. schedule_tasklet = true;
  332. ret = IRQ_HANDLED;
  333. }
  334. spin_unlock(&cqdma->pc[i]->lock);
  335. if (schedule_tasklet) {
  336. /* disable interrupt */
  337. disable_irq_nosync(cqdma->pc[i]->irq);
  338. /* schedule the tasklet to handle the transactions */
  339. tasklet_schedule(&cqdma->pc[i]->tasklet);
  340. }
  341. }
  342. return ret;
  343. }
  344. static struct virt_dma_desc *mtk_cqdma_find_active_desc(struct dma_chan *c,
  345. dma_cookie_t cookie)
  346. {
  347. struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c);
  348. struct virt_dma_desc *vd;
  349. list_for_each_entry(vd, &cvc->pc->queue, node)
  350. if (vd->tx.cookie == cookie) {
  351. return vd;
  352. }
  353. list_for_each_entry(vd, &cvc->vc.desc_issued, node)
  354. if (vd->tx.cookie == cookie)
  355. return vd;
  356. return NULL;
  357. }
  358. static enum dma_status mtk_cqdma_tx_status(struct dma_chan *c,
  359. dma_cookie_t cookie,
  360. struct dma_tx_state *txstate)
  361. {
  362. struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c);
  363. struct mtk_cqdma_vdesc *cvd;
  364. struct virt_dma_desc *vd;
  365. enum dma_status ret;
  366. unsigned long flags;
  367. size_t bytes = 0;
  368. ret = dma_cookie_status(c, cookie, txstate);
  369. if (ret == DMA_COMPLETE || !txstate)
  370. return ret;
  371. spin_lock_irqsave(&cvc->pc->lock, flags);
  372. spin_lock(&cvc->vc.lock);
  373. vd = mtk_cqdma_find_active_desc(c, cookie);
  374. spin_unlock(&cvc->vc.lock);
  375. spin_unlock_irqrestore(&cvc->pc->lock, flags);
  376. if (vd) {
  377. cvd = to_cqdma_vdesc(vd);
  378. bytes = cvd->residue;
  379. }
  380. dma_set_residue(txstate, bytes);
  381. return ret;
  382. }
  383. static void mtk_cqdma_issue_pending(struct dma_chan *c)
  384. {
  385. struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c);
  386. unsigned long pc_flags;
  387. unsigned long vc_flags;
  388. /* acquire PC's lock before VS's lock for lock dependency in tasklet */
  389. spin_lock_irqsave(&cvc->pc->lock, pc_flags);
  390. spin_lock_irqsave(&cvc->vc.lock, vc_flags);
  391. if (vchan_issue_pending(&cvc->vc))
  392. mtk_cqdma_issue_vchan_pending(cvc);
  393. spin_unlock_irqrestore(&cvc->vc.lock, vc_flags);
  394. spin_unlock_irqrestore(&cvc->pc->lock, pc_flags);
  395. }
  396. static struct dma_async_tx_descriptor *
  397. mtk_cqdma_prep_dma_memcpy(struct dma_chan *c, dma_addr_t dest,
  398. dma_addr_t src, size_t len, unsigned long flags)
  399. {
  400. struct mtk_cqdma_vdesc **cvd;
  401. struct dma_async_tx_descriptor *tx = NULL, *prev_tx = NULL;
  402. size_t i, tlen, nr_vd;
  403. /*
  404. * In the case that trsanction length is larger than the
  405. * DMA engine supports, a single memcpy transaction needs
  406. * to be separated into several DMA transactions.
  407. * Each DMA transaction would be described by a CVD,
  408. * and the first one is referred as the parent CVD,
  409. * while the others are child CVDs.
  410. * The parent CVD's tx descriptor is the only tx descriptor
  411. * returned to the DMA user, and it should not be completed
  412. * until all the child CVDs completed.
  413. */
  414. nr_vd = DIV_ROUND_UP(len, MTK_CQDMA_MAX_LEN);
  415. cvd = kcalloc(nr_vd, sizeof(*cvd), GFP_NOWAIT);
  416. if (!cvd)
  417. return NULL;
  418. for (i = 0; i < nr_vd; ++i) {
  419. cvd[i] = kzalloc(sizeof(*cvd[i]), GFP_NOWAIT);
  420. if (!cvd[i]) {
  421. for (; i > 0; --i)
  422. kfree(cvd[i - 1]);
  423. return NULL;
  424. }
  425. /* setup dma channel */
  426. cvd[i]->ch = c;
  427. /* setup source, destination, and length */
  428. tlen = (len > MTK_CQDMA_MAX_LEN) ? MTK_CQDMA_MAX_LEN : len;
  429. cvd[i]->len = tlen;
  430. cvd[i]->src = src;
  431. cvd[i]->dest = dest;
  432. /* setup tx descriptor */
  433. tx = vchan_tx_prep(to_virt_chan(c), &cvd[i]->vd, flags);
  434. tx->next = NULL;
  435. if (!i) {
  436. cvd[0]->residue = len;
  437. } else {
  438. prev_tx->next = tx;
  439. cvd[i]->residue = tlen;
  440. }
  441. cvd[i]->parent = cvd[0];
  442. /* update the src, dest, len, prev_tx for the next CVD */
  443. src += tlen;
  444. dest += tlen;
  445. len -= tlen;
  446. prev_tx = tx;
  447. }
  448. return &cvd[0]->vd.tx;
  449. }
  450. static void mtk_cqdma_free_inactive_desc(struct dma_chan *c)
  451. {
  452. struct virt_dma_chan *vc = to_virt_chan(c);
  453. unsigned long flags;
  454. LIST_HEAD(head);
  455. /*
  456. * set desc_allocated, desc_submitted,
  457. * and desc_issued as the candicates to be freed
  458. */
  459. spin_lock_irqsave(&vc->lock, flags);
  460. list_splice_tail_init(&vc->desc_allocated, &head);
  461. list_splice_tail_init(&vc->desc_submitted, &head);
  462. list_splice_tail_init(&vc->desc_issued, &head);
  463. spin_unlock_irqrestore(&vc->lock, flags);
  464. /* free descriptor lists */
  465. vchan_dma_desc_free_list(vc, &head);
  466. }
  467. static void mtk_cqdma_free_active_desc(struct dma_chan *c)
  468. {
  469. struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c);
  470. bool sync_needed = false;
  471. unsigned long pc_flags;
  472. unsigned long vc_flags;
  473. /* acquire PC's lock first due to lock dependency in dma ISR */
  474. spin_lock_irqsave(&cvc->pc->lock, pc_flags);
  475. spin_lock_irqsave(&cvc->vc.lock, vc_flags);
  476. /* synchronization is required if this VC is active */
  477. if (mtk_cqdma_is_vchan_active(cvc)) {
  478. cvc->issue_synchronize = true;
  479. sync_needed = true;
  480. }
  481. spin_unlock_irqrestore(&cvc->vc.lock, vc_flags);
  482. spin_unlock_irqrestore(&cvc->pc->lock, pc_flags);
  483. /* waiting for the completion of this VC */
  484. if (sync_needed)
  485. wait_for_completion(&cvc->issue_completion);
  486. /* free all descriptors in list desc_completed */
  487. vchan_synchronize(&cvc->vc);
  488. WARN_ONCE(!list_empty(&cvc->vc.desc_completed),
  489. "Desc pending still in list desc_completed\n");
  490. }
  491. static int mtk_cqdma_terminate_all(struct dma_chan *c)
  492. {
  493. /* free descriptors not processed yet by hardware */
  494. mtk_cqdma_free_inactive_desc(c);
  495. /* free descriptors being processed by hardware */
  496. mtk_cqdma_free_active_desc(c);
  497. return 0;
  498. }
  499. static int mtk_cqdma_alloc_chan_resources(struct dma_chan *c)
  500. {
  501. struct mtk_cqdma_device *cqdma = to_cqdma_dev(c);
  502. struct mtk_cqdma_vchan *vc = to_cqdma_vchan(c);
  503. struct mtk_cqdma_pchan *pc = NULL;
  504. u32 i, min_refcnt = U32_MAX, refcnt;
  505. unsigned long flags;
  506. /* allocate PC with the minimum refcount */
  507. for (i = 0; i < cqdma->dma_channels; ++i) {
  508. refcnt = refcount_read(&cqdma->pc[i]->refcnt);
  509. if (refcnt < min_refcnt) {
  510. pc = cqdma->pc[i];
  511. min_refcnt = refcnt;
  512. }
  513. }
  514. if (!pc)
  515. return -ENOSPC;
  516. spin_lock_irqsave(&pc->lock, flags);
  517. if (!refcount_read(&pc->refcnt)) {
  518. /* allocate PC when the refcount is zero */
  519. mtk_cqdma_hard_reset(pc);
  520. /* enable interrupt for this PC */
  521. mtk_dma_set(pc, MTK_CQDMA_INT_EN, MTK_CQDMA_INT_EN_BIT);
  522. /*
  523. * refcount_inc would complain increment on 0; use-after-free.
  524. * Thus, we need to explicitly set it as 1 initially.
  525. */
  526. refcount_set(&pc->refcnt, 1);
  527. } else {
  528. refcount_inc(&pc->refcnt);
  529. }
  530. spin_unlock_irqrestore(&pc->lock, flags);
  531. vc->pc = pc;
  532. return 0;
  533. }
  534. static void mtk_cqdma_free_chan_resources(struct dma_chan *c)
  535. {
  536. struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c);
  537. unsigned long flags;
  538. /* free all descriptors in all lists on the VC */
  539. mtk_cqdma_terminate_all(c);
  540. spin_lock_irqsave(&cvc->pc->lock, flags);
  541. /* PC is not freed until there is no VC mapped to it */
  542. if (refcount_dec_and_test(&cvc->pc->refcnt)) {
  543. /* start the flush operation and stop the engine */
  544. mtk_dma_set(cvc->pc, MTK_CQDMA_FLUSH, MTK_CQDMA_FLUSH_BIT);
  545. /* wait for the completion of flush operation */
  546. if (mtk_cqdma_poll_engine_done(cvc->pc, true) < 0)
  547. dev_err(cqdma2dev(to_cqdma_dev(c)), "cqdma flush timeout\n");
  548. /* clear the flush bit and interrupt flag */
  549. mtk_dma_clr(cvc->pc, MTK_CQDMA_FLUSH, MTK_CQDMA_FLUSH_BIT);
  550. mtk_dma_clr(cvc->pc, MTK_CQDMA_INT_FLAG,
  551. MTK_CQDMA_INT_FLAG_BIT);
  552. /* disable interrupt for this PC */
  553. mtk_dma_clr(cvc->pc, MTK_CQDMA_INT_EN, MTK_CQDMA_INT_EN_BIT);
  554. }
  555. spin_unlock_irqrestore(&cvc->pc->lock, flags);
  556. }
  557. static int mtk_cqdma_hw_init(struct mtk_cqdma_device *cqdma)
  558. {
  559. unsigned long flags;
  560. int err;
  561. u32 i;
  562. pm_runtime_enable(cqdma2dev(cqdma));
  563. pm_runtime_get_sync(cqdma2dev(cqdma));
  564. err = clk_prepare_enable(cqdma->clk);
  565. if (err) {
  566. pm_runtime_put_sync(cqdma2dev(cqdma));
  567. pm_runtime_disable(cqdma2dev(cqdma));
  568. return err;
  569. }
  570. /* reset all PCs */
  571. for (i = 0; i < cqdma->dma_channels; ++i) {
  572. spin_lock_irqsave(&cqdma->pc[i]->lock, flags);
  573. if (mtk_cqdma_hard_reset(cqdma->pc[i]) < 0) {
  574. dev_err(cqdma2dev(cqdma), "cqdma hard reset timeout\n");
  575. spin_unlock_irqrestore(&cqdma->pc[i]->lock, flags);
  576. clk_disable_unprepare(cqdma->clk);
  577. pm_runtime_put_sync(cqdma2dev(cqdma));
  578. pm_runtime_disable(cqdma2dev(cqdma));
  579. return -EINVAL;
  580. }
  581. spin_unlock_irqrestore(&cqdma->pc[i]->lock, flags);
  582. }
  583. return 0;
  584. }
  585. static void mtk_cqdma_hw_deinit(struct mtk_cqdma_device *cqdma)
  586. {
  587. unsigned long flags;
  588. u32 i;
  589. /* reset all PCs */
  590. for (i = 0; i < cqdma->dma_channels; ++i) {
  591. spin_lock_irqsave(&cqdma->pc[i]->lock, flags);
  592. if (mtk_cqdma_hard_reset(cqdma->pc[i]) < 0)
  593. dev_err(cqdma2dev(cqdma), "cqdma hard reset timeout\n");
  594. spin_unlock_irqrestore(&cqdma->pc[i]->lock, flags);
  595. }
  596. clk_disable_unprepare(cqdma->clk);
  597. pm_runtime_put_sync(cqdma2dev(cqdma));
  598. pm_runtime_disable(cqdma2dev(cqdma));
  599. }
  600. static const struct of_device_id mtk_cqdma_match[] = {
  601. { .compatible = "mediatek,mt6765-cqdma" },
  602. { /* sentinel */ }
  603. };
  604. MODULE_DEVICE_TABLE(of, mtk_cqdma_match);
  605. static int mtk_cqdma_probe(struct platform_device *pdev)
  606. {
  607. struct mtk_cqdma_device *cqdma;
  608. struct mtk_cqdma_vchan *vc;
  609. struct dma_device *dd;
  610. int err;
  611. u32 i;
  612. cqdma = devm_kzalloc(&pdev->dev, sizeof(*cqdma), GFP_KERNEL);
  613. if (!cqdma)
  614. return -ENOMEM;
  615. dd = &cqdma->ddev;
  616. cqdma->clk = devm_clk_get(&pdev->dev, "cqdma");
  617. if (IS_ERR(cqdma->clk)) {
  618. dev_err(&pdev->dev, "No clock for %s\n",
  619. dev_name(&pdev->dev));
  620. return PTR_ERR(cqdma->clk);
  621. }
  622. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  623. dd->copy_align = MTK_CQDMA_ALIGN_SIZE;
  624. dd->device_alloc_chan_resources = mtk_cqdma_alloc_chan_resources;
  625. dd->device_free_chan_resources = mtk_cqdma_free_chan_resources;
  626. dd->device_tx_status = mtk_cqdma_tx_status;
  627. dd->device_issue_pending = mtk_cqdma_issue_pending;
  628. dd->device_prep_dma_memcpy = mtk_cqdma_prep_dma_memcpy;
  629. dd->device_terminate_all = mtk_cqdma_terminate_all;
  630. dd->src_addr_widths = MTK_CQDMA_DMA_BUSWIDTHS;
  631. dd->dst_addr_widths = MTK_CQDMA_DMA_BUSWIDTHS;
  632. dd->directions = BIT(DMA_MEM_TO_MEM);
  633. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  634. dd->dev = &pdev->dev;
  635. INIT_LIST_HEAD(&dd->channels);
  636. if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
  637. "dma-requests",
  638. &cqdma->dma_requests)) {
  639. dev_info(&pdev->dev,
  640. "Using %u as missing dma-requests property\n",
  641. MTK_CQDMA_NR_VCHANS);
  642. cqdma->dma_requests = MTK_CQDMA_NR_VCHANS;
  643. }
  644. if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
  645. "dma-channels",
  646. &cqdma->dma_channels)) {
  647. dev_info(&pdev->dev,
  648. "Using %u as missing dma-channels property\n",
  649. MTK_CQDMA_NR_PCHANS);
  650. cqdma->dma_channels = MTK_CQDMA_NR_PCHANS;
  651. }
  652. cqdma->pc = devm_kcalloc(&pdev->dev, cqdma->dma_channels,
  653. sizeof(*cqdma->pc), GFP_KERNEL);
  654. if (!cqdma->pc)
  655. return -ENOMEM;
  656. /* initialization for PCs */
  657. for (i = 0; i < cqdma->dma_channels; ++i) {
  658. cqdma->pc[i] = devm_kcalloc(&pdev->dev, 1,
  659. sizeof(**cqdma->pc), GFP_KERNEL);
  660. if (!cqdma->pc[i])
  661. return -ENOMEM;
  662. INIT_LIST_HEAD(&cqdma->pc[i]->queue);
  663. spin_lock_init(&cqdma->pc[i]->lock);
  664. refcount_set(&cqdma->pc[i]->refcnt, 0);
  665. cqdma->pc[i]->base = devm_platform_ioremap_resource(pdev, i);
  666. if (IS_ERR(cqdma->pc[i]->base))
  667. return PTR_ERR(cqdma->pc[i]->base);
  668. /* allocate IRQ resource */
  669. err = platform_get_irq(pdev, i);
  670. if (err < 0)
  671. return err;
  672. cqdma->pc[i]->irq = err;
  673. err = devm_request_irq(&pdev->dev, cqdma->pc[i]->irq,
  674. mtk_cqdma_irq, 0, dev_name(&pdev->dev),
  675. cqdma);
  676. if (err) {
  677. dev_err(&pdev->dev,
  678. "request_irq failed with err %d\n", err);
  679. return -EINVAL;
  680. }
  681. }
  682. /* allocate resource for VCs */
  683. cqdma->vc = devm_kcalloc(&pdev->dev, cqdma->dma_requests,
  684. sizeof(*cqdma->vc), GFP_KERNEL);
  685. if (!cqdma->vc)
  686. return -ENOMEM;
  687. for (i = 0; i < cqdma->dma_requests; i++) {
  688. vc = &cqdma->vc[i];
  689. vc->vc.desc_free = mtk_cqdma_vdesc_free;
  690. vchan_init(&vc->vc, dd);
  691. init_completion(&vc->issue_completion);
  692. }
  693. err = dma_async_device_register(dd);
  694. if (err)
  695. return err;
  696. err = of_dma_controller_register(pdev->dev.of_node,
  697. of_dma_xlate_by_chan_id, cqdma);
  698. if (err) {
  699. dev_err(&pdev->dev,
  700. "MediaTek CQDMA OF registration failed %d\n", err);
  701. goto err_unregister;
  702. }
  703. err = mtk_cqdma_hw_init(cqdma);
  704. if (err) {
  705. dev_err(&pdev->dev,
  706. "MediaTek CQDMA HW initialization failed %d\n", err);
  707. goto err_unregister;
  708. }
  709. platform_set_drvdata(pdev, cqdma);
  710. /* initialize tasklet for each PC */
  711. for (i = 0; i < cqdma->dma_channels; ++i)
  712. tasklet_setup(&cqdma->pc[i]->tasklet, mtk_cqdma_tasklet_cb);
  713. dev_info(&pdev->dev, "MediaTek CQDMA driver registered\n");
  714. return 0;
  715. err_unregister:
  716. dma_async_device_unregister(dd);
  717. return err;
  718. }
  719. static void mtk_cqdma_remove(struct platform_device *pdev)
  720. {
  721. struct mtk_cqdma_device *cqdma = platform_get_drvdata(pdev);
  722. struct mtk_cqdma_vchan *vc;
  723. unsigned long flags;
  724. int i;
  725. /* kill VC task */
  726. for (i = 0; i < cqdma->dma_requests; i++) {
  727. vc = &cqdma->vc[i];
  728. list_del(&vc->vc.chan.device_node);
  729. tasklet_kill(&vc->vc.task);
  730. }
  731. /* disable interrupt */
  732. for (i = 0; i < cqdma->dma_channels; i++) {
  733. spin_lock_irqsave(&cqdma->pc[i]->lock, flags);
  734. mtk_dma_clr(cqdma->pc[i], MTK_CQDMA_INT_EN,
  735. MTK_CQDMA_INT_EN_BIT);
  736. spin_unlock_irqrestore(&cqdma->pc[i]->lock, flags);
  737. /* Waits for any pending IRQ handlers to complete */
  738. synchronize_irq(cqdma->pc[i]->irq);
  739. tasklet_kill(&cqdma->pc[i]->tasklet);
  740. }
  741. /* disable hardware */
  742. mtk_cqdma_hw_deinit(cqdma);
  743. dma_async_device_unregister(&cqdma->ddev);
  744. of_dma_controller_free(pdev->dev.of_node);
  745. }
  746. static struct platform_driver mtk_cqdma_driver = {
  747. .probe = mtk_cqdma_probe,
  748. .remove_new = mtk_cqdma_remove,
  749. .driver = {
  750. .name = KBUILD_MODNAME,
  751. .of_match_table = mtk_cqdma_match,
  752. },
  753. };
  754. module_platform_driver(mtk_cqdma_driver);
  755. MODULE_DESCRIPTION("MediaTek CQDMA Controller Driver");
  756. MODULE_AUTHOR("Shun-Chih Yu <shun-chih.yu@mediatek.com>");
  757. MODULE_LICENSE("GPL v2");