mtk-cqdma.c 24 KB

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