hidma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * Qualcomm Technologies HIDMA DMA engine interface
  3. *
  4. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. /*
  16. * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
  17. * Copyright (C) Semihalf 2009
  18. * Copyright (C) Ilya Yanok, Emcraft Systems 2010
  19. * Copyright (C) Alexander Popov, Promcontroller 2014
  20. *
  21. * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
  22. * (defines, structures and comments) was taken from MPC5121 DMA driver
  23. * written by Hongjun Chen <hong-jun.chen@freescale.com>.
  24. *
  25. * Approved as OSADL project by a majority of OSADL members and funded
  26. * by OSADL membership fees in 2009; for details see www.osadl.org.
  27. *
  28. * This program is free software; you can redistribute it and/or modify it
  29. * under the terms of the GNU General Public License as published by the Free
  30. * Software Foundation; either version 2 of the License, or (at your option)
  31. * any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful, but WITHOUT
  34. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  35. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  36. * more details.
  37. *
  38. * The full GNU General Public License is included in this distribution in the
  39. * file called COPYING.
  40. */
  41. /* Linux Foundation elects GPLv2 license only. */
  42. #include <linux/dmaengine.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/list.h>
  45. #include <linux/module.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/slab.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/of_dma.h>
  50. #include <linux/of_device.h>
  51. #include <linux/property.h>
  52. #include <linux/delay.h>
  53. #include <linux/acpi.h>
  54. #include <linux/irq.h>
  55. #include <linux/atomic.h>
  56. #include <linux/pm_runtime.h>
  57. #include <linux/msi.h>
  58. #include "../dmaengine.h"
  59. #include "hidma.h"
  60. /*
  61. * Default idle time is 2 seconds. This parameter can
  62. * be overridden by changing the following
  63. * /sys/bus/platform/devices/QCOM8061:<xy>/power/autosuspend_delay_ms
  64. * during kernel boot.
  65. */
  66. #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
  67. #define HIDMA_ERR_INFO_SW 0xFF
  68. #define HIDMA_ERR_CODE_UNEXPECTED_TERMINATE 0x0
  69. #define HIDMA_NR_DEFAULT_DESC 10
  70. #define HIDMA_MSI_INTS 11
  71. static inline struct hidma_dev *to_hidma_dev(struct dma_device *dmadev)
  72. {
  73. return container_of(dmadev, struct hidma_dev, ddev);
  74. }
  75. static inline
  76. struct hidma_dev *to_hidma_dev_from_lldev(struct hidma_lldev **_lldevp)
  77. {
  78. return container_of(_lldevp, struct hidma_dev, lldev);
  79. }
  80. static inline struct hidma_chan *to_hidma_chan(struct dma_chan *dmach)
  81. {
  82. return container_of(dmach, struct hidma_chan, chan);
  83. }
  84. static inline
  85. struct hidma_desc *to_hidma_desc(struct dma_async_tx_descriptor *t)
  86. {
  87. return container_of(t, struct hidma_desc, desc);
  88. }
  89. static void hidma_free(struct hidma_dev *dmadev)
  90. {
  91. INIT_LIST_HEAD(&dmadev->ddev.channels);
  92. }
  93. static unsigned int nr_desc_prm;
  94. module_param(nr_desc_prm, uint, 0644);
  95. MODULE_PARM_DESC(nr_desc_prm, "number of descriptors (default: 0)");
  96. enum hidma_cap {
  97. HIDMA_MSI_CAP = 1,
  98. HIDMA_IDENTITY_CAP,
  99. };
  100. /* process completed descriptors */
  101. static void hidma_process_completed(struct hidma_chan *mchan)
  102. {
  103. struct dma_device *ddev = mchan->chan.device;
  104. struct hidma_dev *mdma = to_hidma_dev(ddev);
  105. struct dma_async_tx_descriptor *desc;
  106. dma_cookie_t last_cookie;
  107. struct hidma_desc *mdesc;
  108. struct hidma_desc *next;
  109. unsigned long irqflags;
  110. struct list_head list;
  111. INIT_LIST_HEAD(&list);
  112. /* Get all completed descriptors */
  113. spin_lock_irqsave(&mchan->lock, irqflags);
  114. list_splice_tail_init(&mchan->completed, &list);
  115. spin_unlock_irqrestore(&mchan->lock, irqflags);
  116. /* Execute callbacks and run dependencies */
  117. list_for_each_entry_safe(mdesc, next, &list, node) {
  118. enum dma_status llstat;
  119. struct dmaengine_desc_callback cb;
  120. struct dmaengine_result result;
  121. desc = &mdesc->desc;
  122. last_cookie = desc->cookie;
  123. spin_lock_irqsave(&mchan->lock, irqflags);
  124. dma_cookie_complete(desc);
  125. spin_unlock_irqrestore(&mchan->lock, irqflags);
  126. llstat = hidma_ll_status(mdma->lldev, mdesc->tre_ch);
  127. dmaengine_desc_get_callback(desc, &cb);
  128. dma_run_dependencies(desc);
  129. spin_lock_irqsave(&mchan->lock, irqflags);
  130. list_move(&mdesc->node, &mchan->free);
  131. if (llstat == DMA_COMPLETE) {
  132. mchan->last_success = last_cookie;
  133. result.result = DMA_TRANS_NOERROR;
  134. } else
  135. result.result = DMA_TRANS_ABORTED;
  136. spin_unlock_irqrestore(&mchan->lock, irqflags);
  137. dmaengine_desc_callback_invoke(&cb, &result);
  138. }
  139. }
  140. /*
  141. * Called once for each submitted descriptor.
  142. * PM is locked once for each descriptor that is currently
  143. * in execution.
  144. */
  145. static void hidma_callback(void *data)
  146. {
  147. struct hidma_desc *mdesc = data;
  148. struct hidma_chan *mchan = to_hidma_chan(mdesc->desc.chan);
  149. struct dma_device *ddev = mchan->chan.device;
  150. struct hidma_dev *dmadev = to_hidma_dev(ddev);
  151. unsigned long irqflags;
  152. bool queued = false;
  153. spin_lock_irqsave(&mchan->lock, irqflags);
  154. if (mdesc->node.next) {
  155. /* Delete from the active list, add to completed list */
  156. list_move_tail(&mdesc->node, &mchan->completed);
  157. queued = true;
  158. /* calculate the next running descriptor */
  159. mchan->running = list_first_entry(&mchan->active,
  160. struct hidma_desc, node);
  161. }
  162. spin_unlock_irqrestore(&mchan->lock, irqflags);
  163. hidma_process_completed(mchan);
  164. if (queued) {
  165. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  166. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  167. }
  168. }
  169. static int hidma_chan_init(struct hidma_dev *dmadev, u32 dma_sig)
  170. {
  171. struct hidma_chan *mchan;
  172. struct dma_device *ddev;
  173. mchan = devm_kzalloc(dmadev->ddev.dev, sizeof(*mchan), GFP_KERNEL);
  174. if (!mchan)
  175. return -ENOMEM;
  176. ddev = &dmadev->ddev;
  177. mchan->dma_sig = dma_sig;
  178. mchan->dmadev = dmadev;
  179. mchan->chan.device = ddev;
  180. dma_cookie_init(&mchan->chan);
  181. INIT_LIST_HEAD(&mchan->free);
  182. INIT_LIST_HEAD(&mchan->prepared);
  183. INIT_LIST_HEAD(&mchan->active);
  184. INIT_LIST_HEAD(&mchan->completed);
  185. INIT_LIST_HEAD(&mchan->queued);
  186. spin_lock_init(&mchan->lock);
  187. list_add_tail(&mchan->chan.device_node, &ddev->channels);
  188. dmadev->ddev.chancnt++;
  189. return 0;
  190. }
  191. static void hidma_issue_task(unsigned long arg)
  192. {
  193. struct hidma_dev *dmadev = (struct hidma_dev *)arg;
  194. pm_runtime_get_sync(dmadev->ddev.dev);
  195. hidma_ll_start(dmadev->lldev);
  196. }
  197. static void hidma_issue_pending(struct dma_chan *dmach)
  198. {
  199. struct hidma_chan *mchan = to_hidma_chan(dmach);
  200. struct hidma_dev *dmadev = mchan->dmadev;
  201. unsigned long flags;
  202. struct hidma_desc *qdesc, *next;
  203. int status;
  204. spin_lock_irqsave(&mchan->lock, flags);
  205. list_for_each_entry_safe(qdesc, next, &mchan->queued, node) {
  206. hidma_ll_queue_request(dmadev->lldev, qdesc->tre_ch);
  207. list_move_tail(&qdesc->node, &mchan->active);
  208. }
  209. if (!mchan->running) {
  210. struct hidma_desc *desc = list_first_entry(&mchan->active,
  211. struct hidma_desc,
  212. node);
  213. mchan->running = desc;
  214. }
  215. spin_unlock_irqrestore(&mchan->lock, flags);
  216. /* PM will be released in hidma_callback function. */
  217. status = pm_runtime_get(dmadev->ddev.dev);
  218. if (status < 0)
  219. tasklet_schedule(&dmadev->task);
  220. else
  221. hidma_ll_start(dmadev->lldev);
  222. }
  223. static inline bool hidma_txn_is_success(dma_cookie_t cookie,
  224. dma_cookie_t last_success, dma_cookie_t last_used)
  225. {
  226. if (last_success <= last_used) {
  227. if ((cookie <= last_success) || (cookie > last_used))
  228. return true;
  229. } else {
  230. if ((cookie <= last_success) && (cookie > last_used))
  231. return true;
  232. }
  233. return false;
  234. }
  235. static enum dma_status hidma_tx_status(struct dma_chan *dmach,
  236. dma_cookie_t cookie,
  237. struct dma_tx_state *txstate)
  238. {
  239. struct hidma_chan *mchan = to_hidma_chan(dmach);
  240. enum dma_status ret;
  241. ret = dma_cookie_status(dmach, cookie, txstate);
  242. if (ret == DMA_COMPLETE) {
  243. bool is_success;
  244. is_success = hidma_txn_is_success(cookie, mchan->last_success,
  245. dmach->cookie);
  246. return is_success ? ret : DMA_ERROR;
  247. }
  248. if (mchan->paused && (ret == DMA_IN_PROGRESS)) {
  249. unsigned long flags;
  250. dma_cookie_t runcookie;
  251. spin_lock_irqsave(&mchan->lock, flags);
  252. if (mchan->running)
  253. runcookie = mchan->running->desc.cookie;
  254. else
  255. runcookie = -EINVAL;
  256. if (runcookie == cookie)
  257. ret = DMA_PAUSED;
  258. spin_unlock_irqrestore(&mchan->lock, flags);
  259. }
  260. return ret;
  261. }
  262. /*
  263. * Submit descriptor to hardware.
  264. * Lock the PM for each descriptor we are sending.
  265. */
  266. static dma_cookie_t hidma_tx_submit(struct dma_async_tx_descriptor *txd)
  267. {
  268. struct hidma_chan *mchan = to_hidma_chan(txd->chan);
  269. struct hidma_dev *dmadev = mchan->dmadev;
  270. struct hidma_desc *mdesc;
  271. unsigned long irqflags;
  272. dma_cookie_t cookie;
  273. pm_runtime_get_sync(dmadev->ddev.dev);
  274. if (!hidma_ll_isenabled(dmadev->lldev)) {
  275. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  276. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  277. return -ENODEV;
  278. }
  279. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  280. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  281. mdesc = container_of(txd, struct hidma_desc, desc);
  282. spin_lock_irqsave(&mchan->lock, irqflags);
  283. /* Move descriptor to queued */
  284. list_move_tail(&mdesc->node, &mchan->queued);
  285. /* Update cookie */
  286. cookie = dma_cookie_assign(txd);
  287. spin_unlock_irqrestore(&mchan->lock, irqflags);
  288. return cookie;
  289. }
  290. static int hidma_alloc_chan_resources(struct dma_chan *dmach)
  291. {
  292. struct hidma_chan *mchan = to_hidma_chan(dmach);
  293. struct hidma_dev *dmadev = mchan->dmadev;
  294. struct hidma_desc *mdesc, *tmp;
  295. unsigned long irqflags;
  296. LIST_HEAD(descs);
  297. unsigned int i;
  298. int rc = 0;
  299. if (mchan->allocated)
  300. return 0;
  301. /* Alloc descriptors for this channel */
  302. for (i = 0; i < dmadev->nr_descriptors; i++) {
  303. mdesc = kzalloc(sizeof(struct hidma_desc), GFP_NOWAIT);
  304. if (!mdesc) {
  305. rc = -ENOMEM;
  306. break;
  307. }
  308. dma_async_tx_descriptor_init(&mdesc->desc, dmach);
  309. mdesc->desc.tx_submit = hidma_tx_submit;
  310. rc = hidma_ll_request(dmadev->lldev, mchan->dma_sig,
  311. "DMA engine", hidma_callback, mdesc,
  312. &mdesc->tre_ch);
  313. if (rc) {
  314. dev_err(dmach->device->dev,
  315. "channel alloc failed at %u\n", i);
  316. kfree(mdesc);
  317. break;
  318. }
  319. list_add_tail(&mdesc->node, &descs);
  320. }
  321. if (rc) {
  322. /* return the allocated descriptors */
  323. list_for_each_entry_safe(mdesc, tmp, &descs, node) {
  324. hidma_ll_free(dmadev->lldev, mdesc->tre_ch);
  325. kfree(mdesc);
  326. }
  327. return rc;
  328. }
  329. spin_lock_irqsave(&mchan->lock, irqflags);
  330. list_splice_tail_init(&descs, &mchan->free);
  331. mchan->allocated = true;
  332. spin_unlock_irqrestore(&mchan->lock, irqflags);
  333. return 1;
  334. }
  335. static struct dma_async_tx_descriptor *
  336. hidma_prep_dma_memcpy(struct dma_chan *dmach, dma_addr_t dest, dma_addr_t src,
  337. size_t len, unsigned long flags)
  338. {
  339. struct hidma_chan *mchan = to_hidma_chan(dmach);
  340. struct hidma_desc *mdesc = NULL;
  341. struct hidma_dev *mdma = mchan->dmadev;
  342. unsigned long irqflags;
  343. /* Get free descriptor */
  344. spin_lock_irqsave(&mchan->lock, irqflags);
  345. if (!list_empty(&mchan->free)) {
  346. mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
  347. list_del(&mdesc->node);
  348. }
  349. spin_unlock_irqrestore(&mchan->lock, irqflags);
  350. if (!mdesc)
  351. return NULL;
  352. hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
  353. src, dest, len, flags,
  354. HIDMA_TRE_MEMCPY);
  355. /* Place descriptor in prepared list */
  356. spin_lock_irqsave(&mchan->lock, irqflags);
  357. list_add_tail(&mdesc->node, &mchan->prepared);
  358. spin_unlock_irqrestore(&mchan->lock, irqflags);
  359. return &mdesc->desc;
  360. }
  361. static struct dma_async_tx_descriptor *
  362. hidma_prep_dma_memset(struct dma_chan *dmach, dma_addr_t dest, int value,
  363. size_t len, unsigned long flags)
  364. {
  365. struct hidma_chan *mchan = to_hidma_chan(dmach);
  366. struct hidma_desc *mdesc = NULL;
  367. struct hidma_dev *mdma = mchan->dmadev;
  368. unsigned long irqflags;
  369. /* Get free descriptor */
  370. spin_lock_irqsave(&mchan->lock, irqflags);
  371. if (!list_empty(&mchan->free)) {
  372. mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
  373. list_del(&mdesc->node);
  374. }
  375. spin_unlock_irqrestore(&mchan->lock, irqflags);
  376. if (!mdesc)
  377. return NULL;
  378. hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
  379. value, dest, len, flags,
  380. HIDMA_TRE_MEMSET);
  381. /* Place descriptor in prepared list */
  382. spin_lock_irqsave(&mchan->lock, irqflags);
  383. list_add_tail(&mdesc->node, &mchan->prepared);
  384. spin_unlock_irqrestore(&mchan->lock, irqflags);
  385. return &mdesc->desc;
  386. }
  387. static int hidma_terminate_channel(struct dma_chan *chan)
  388. {
  389. struct hidma_chan *mchan = to_hidma_chan(chan);
  390. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  391. struct hidma_desc *tmp, *mdesc;
  392. unsigned long irqflags;
  393. LIST_HEAD(list);
  394. int rc;
  395. pm_runtime_get_sync(dmadev->ddev.dev);
  396. /* give completed requests a chance to finish */
  397. hidma_process_completed(mchan);
  398. spin_lock_irqsave(&mchan->lock, irqflags);
  399. mchan->last_success = 0;
  400. list_splice_init(&mchan->active, &list);
  401. list_splice_init(&mchan->prepared, &list);
  402. list_splice_init(&mchan->completed, &list);
  403. list_splice_init(&mchan->queued, &list);
  404. spin_unlock_irqrestore(&mchan->lock, irqflags);
  405. /* this suspends the existing transfer */
  406. rc = hidma_ll_disable(dmadev->lldev);
  407. if (rc) {
  408. dev_err(dmadev->ddev.dev, "channel did not pause\n");
  409. goto out;
  410. }
  411. /* return all user requests */
  412. list_for_each_entry_safe(mdesc, tmp, &list, node) {
  413. struct dma_async_tx_descriptor *txd = &mdesc->desc;
  414. dma_descriptor_unmap(txd);
  415. dmaengine_desc_get_callback_invoke(txd, NULL);
  416. dma_run_dependencies(txd);
  417. /* move myself to free_list */
  418. list_move(&mdesc->node, &mchan->free);
  419. }
  420. rc = hidma_ll_enable(dmadev->lldev);
  421. out:
  422. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  423. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  424. return rc;
  425. }
  426. static int hidma_terminate_all(struct dma_chan *chan)
  427. {
  428. struct hidma_chan *mchan = to_hidma_chan(chan);
  429. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  430. int rc;
  431. rc = hidma_terminate_channel(chan);
  432. if (rc)
  433. return rc;
  434. /* reinitialize the hardware */
  435. pm_runtime_get_sync(dmadev->ddev.dev);
  436. rc = hidma_ll_setup(dmadev->lldev);
  437. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  438. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  439. return rc;
  440. }
  441. static void hidma_free_chan_resources(struct dma_chan *dmach)
  442. {
  443. struct hidma_chan *mchan = to_hidma_chan(dmach);
  444. struct hidma_dev *mdma = mchan->dmadev;
  445. struct hidma_desc *mdesc, *tmp;
  446. unsigned long irqflags;
  447. LIST_HEAD(descs);
  448. /* terminate running transactions and free descriptors */
  449. hidma_terminate_channel(dmach);
  450. spin_lock_irqsave(&mchan->lock, irqflags);
  451. /* Move data */
  452. list_splice_tail_init(&mchan->free, &descs);
  453. /* Free descriptors */
  454. list_for_each_entry_safe(mdesc, tmp, &descs, node) {
  455. hidma_ll_free(mdma->lldev, mdesc->tre_ch);
  456. list_del(&mdesc->node);
  457. kfree(mdesc);
  458. }
  459. mchan->allocated = 0;
  460. spin_unlock_irqrestore(&mchan->lock, irqflags);
  461. }
  462. static int hidma_pause(struct dma_chan *chan)
  463. {
  464. struct hidma_chan *mchan;
  465. struct hidma_dev *dmadev;
  466. mchan = to_hidma_chan(chan);
  467. dmadev = to_hidma_dev(mchan->chan.device);
  468. if (!mchan->paused) {
  469. pm_runtime_get_sync(dmadev->ddev.dev);
  470. if (hidma_ll_disable(dmadev->lldev))
  471. dev_warn(dmadev->ddev.dev, "channel did not stop\n");
  472. mchan->paused = true;
  473. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  474. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  475. }
  476. return 0;
  477. }
  478. static int hidma_resume(struct dma_chan *chan)
  479. {
  480. struct hidma_chan *mchan;
  481. struct hidma_dev *dmadev;
  482. int rc = 0;
  483. mchan = to_hidma_chan(chan);
  484. dmadev = to_hidma_dev(mchan->chan.device);
  485. if (mchan->paused) {
  486. pm_runtime_get_sync(dmadev->ddev.dev);
  487. rc = hidma_ll_enable(dmadev->lldev);
  488. if (!rc)
  489. mchan->paused = false;
  490. else
  491. dev_err(dmadev->ddev.dev,
  492. "failed to resume the channel");
  493. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  494. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  495. }
  496. return rc;
  497. }
  498. static irqreturn_t hidma_chirq_handler(int chirq, void *arg)
  499. {
  500. struct hidma_lldev *lldev = arg;
  501. /*
  502. * All interrupts are request driven.
  503. * HW doesn't send an interrupt by itself.
  504. */
  505. return hidma_ll_inthandler(chirq, lldev);
  506. }
  507. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  508. static irqreturn_t hidma_chirq_handler_msi(int chirq, void *arg)
  509. {
  510. struct hidma_lldev **lldevp = arg;
  511. struct hidma_dev *dmadev = to_hidma_dev_from_lldev(lldevp);
  512. return hidma_ll_inthandler_msi(chirq, *lldevp,
  513. 1 << (chirq - dmadev->msi_virqbase));
  514. }
  515. #endif
  516. static ssize_t hidma_show_values(struct device *dev,
  517. struct device_attribute *attr, char *buf)
  518. {
  519. struct hidma_dev *mdev = dev_get_drvdata(dev);
  520. buf[0] = 0;
  521. if (strcmp(attr->attr.name, "chid") == 0)
  522. sprintf(buf, "%d\n", mdev->chidx);
  523. return strlen(buf);
  524. }
  525. static inline void hidma_sysfs_uninit(struct hidma_dev *dev)
  526. {
  527. device_remove_file(dev->ddev.dev, dev->chid_attrs);
  528. }
  529. static struct device_attribute*
  530. hidma_create_sysfs_entry(struct hidma_dev *dev, char *name, int mode)
  531. {
  532. struct device_attribute *attrs;
  533. char *name_copy;
  534. attrs = devm_kmalloc(dev->ddev.dev, sizeof(struct device_attribute),
  535. GFP_KERNEL);
  536. if (!attrs)
  537. return NULL;
  538. name_copy = devm_kstrdup(dev->ddev.dev, name, GFP_KERNEL);
  539. if (!name_copy)
  540. return NULL;
  541. attrs->attr.name = name_copy;
  542. attrs->attr.mode = mode;
  543. attrs->show = hidma_show_values;
  544. sysfs_attr_init(&attrs->attr);
  545. return attrs;
  546. }
  547. static int hidma_sysfs_init(struct hidma_dev *dev)
  548. {
  549. dev->chid_attrs = hidma_create_sysfs_entry(dev, "chid", S_IRUGO);
  550. if (!dev->chid_attrs)
  551. return -ENOMEM;
  552. return device_create_file(dev->ddev.dev, dev->chid_attrs);
  553. }
  554. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  555. static void hidma_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  556. {
  557. struct device *dev = msi_desc_to_dev(desc);
  558. struct hidma_dev *dmadev = dev_get_drvdata(dev);
  559. if (!desc->platform.msi_index) {
  560. writel(msg->address_lo, dmadev->dev_evca + 0x118);
  561. writel(msg->address_hi, dmadev->dev_evca + 0x11C);
  562. writel(msg->data, dmadev->dev_evca + 0x120);
  563. }
  564. }
  565. #endif
  566. static void hidma_free_msis(struct hidma_dev *dmadev)
  567. {
  568. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  569. struct device *dev = dmadev->ddev.dev;
  570. struct msi_desc *desc;
  571. /* free allocated MSI interrupts above */
  572. for_each_msi_entry(desc, dev)
  573. devm_free_irq(dev, desc->irq, &dmadev->lldev);
  574. platform_msi_domain_free_irqs(dev);
  575. #endif
  576. }
  577. static int hidma_request_msi(struct hidma_dev *dmadev,
  578. struct platform_device *pdev)
  579. {
  580. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  581. int rc;
  582. struct msi_desc *desc;
  583. struct msi_desc *failed_desc = NULL;
  584. rc = platform_msi_domain_alloc_irqs(&pdev->dev, HIDMA_MSI_INTS,
  585. hidma_write_msi_msg);
  586. if (rc)
  587. return rc;
  588. for_each_msi_entry(desc, &pdev->dev) {
  589. if (!desc->platform.msi_index)
  590. dmadev->msi_virqbase = desc->irq;
  591. rc = devm_request_irq(&pdev->dev, desc->irq,
  592. hidma_chirq_handler_msi,
  593. 0, "qcom-hidma-msi",
  594. &dmadev->lldev);
  595. if (rc) {
  596. failed_desc = desc;
  597. break;
  598. }
  599. }
  600. if (rc) {
  601. /* free allocated MSI interrupts above */
  602. for_each_msi_entry(desc, &pdev->dev) {
  603. if (desc == failed_desc)
  604. break;
  605. devm_free_irq(&pdev->dev, desc->irq,
  606. &dmadev->lldev);
  607. }
  608. } else {
  609. /* Add callback to free MSIs on teardown */
  610. hidma_ll_setup_irq(dmadev->lldev, true);
  611. }
  612. if (rc)
  613. dev_warn(&pdev->dev,
  614. "failed to request MSI irq, falling back to wired IRQ\n");
  615. return rc;
  616. #else
  617. return -EINVAL;
  618. #endif
  619. }
  620. static bool hidma_test_capability(struct device *dev, enum hidma_cap test_cap)
  621. {
  622. enum hidma_cap cap;
  623. cap = (enum hidma_cap) device_get_match_data(dev);
  624. return cap ? ((cap & test_cap) > 0) : 0;
  625. }
  626. static int hidma_probe(struct platform_device *pdev)
  627. {
  628. struct hidma_dev *dmadev;
  629. struct resource *trca_resource;
  630. struct resource *evca_resource;
  631. int chirq;
  632. void __iomem *evca;
  633. void __iomem *trca;
  634. int rc;
  635. bool msi;
  636. pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
  637. pm_runtime_use_autosuspend(&pdev->dev);
  638. pm_runtime_set_active(&pdev->dev);
  639. pm_runtime_enable(&pdev->dev);
  640. trca_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  641. trca = devm_ioremap_resource(&pdev->dev, trca_resource);
  642. if (IS_ERR(trca)) {
  643. rc = -ENOMEM;
  644. goto bailout;
  645. }
  646. evca_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  647. evca = devm_ioremap_resource(&pdev->dev, evca_resource);
  648. if (IS_ERR(evca)) {
  649. rc = -ENOMEM;
  650. goto bailout;
  651. }
  652. /*
  653. * This driver only handles the channel IRQs.
  654. * Common IRQ is handled by the management driver.
  655. */
  656. chirq = platform_get_irq(pdev, 0);
  657. if (chirq < 0) {
  658. rc = -ENODEV;
  659. goto bailout;
  660. }
  661. dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
  662. if (!dmadev) {
  663. rc = -ENOMEM;
  664. goto bailout;
  665. }
  666. INIT_LIST_HEAD(&dmadev->ddev.channels);
  667. spin_lock_init(&dmadev->lock);
  668. dmadev->ddev.dev = &pdev->dev;
  669. pm_runtime_get_sync(dmadev->ddev.dev);
  670. dma_cap_set(DMA_MEMCPY, dmadev->ddev.cap_mask);
  671. dma_cap_set(DMA_MEMSET, dmadev->ddev.cap_mask);
  672. if (WARN_ON(!pdev->dev.dma_mask)) {
  673. rc = -ENXIO;
  674. goto dmafree;
  675. }
  676. dmadev->dev_evca = evca;
  677. dmadev->evca_resource = evca_resource;
  678. dmadev->dev_trca = trca;
  679. dmadev->trca_resource = trca_resource;
  680. dmadev->ddev.device_prep_dma_memcpy = hidma_prep_dma_memcpy;
  681. dmadev->ddev.device_prep_dma_memset = hidma_prep_dma_memset;
  682. dmadev->ddev.device_alloc_chan_resources = hidma_alloc_chan_resources;
  683. dmadev->ddev.device_free_chan_resources = hidma_free_chan_resources;
  684. dmadev->ddev.device_tx_status = hidma_tx_status;
  685. dmadev->ddev.device_issue_pending = hidma_issue_pending;
  686. dmadev->ddev.device_pause = hidma_pause;
  687. dmadev->ddev.device_resume = hidma_resume;
  688. dmadev->ddev.device_terminate_all = hidma_terminate_all;
  689. dmadev->ddev.copy_align = 8;
  690. /*
  691. * Determine the MSI capability of the platform. Old HW doesn't
  692. * support MSI.
  693. */
  694. msi = hidma_test_capability(&pdev->dev, HIDMA_MSI_CAP);
  695. device_property_read_u32(&pdev->dev, "desc-count",
  696. &dmadev->nr_descriptors);
  697. if (nr_desc_prm) {
  698. dev_info(&pdev->dev, "overriding number of descriptors as %d\n",
  699. nr_desc_prm);
  700. dmadev->nr_descriptors = nr_desc_prm;
  701. }
  702. if (!dmadev->nr_descriptors)
  703. dmadev->nr_descriptors = HIDMA_NR_DEFAULT_DESC;
  704. if (hidma_test_capability(&pdev->dev, HIDMA_IDENTITY_CAP))
  705. dmadev->chidx = readl(dmadev->dev_trca + 0x40);
  706. else
  707. dmadev->chidx = readl(dmadev->dev_trca + 0x28);
  708. /* Set DMA mask to 64 bits. */
  709. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  710. if (rc) {
  711. dev_warn(&pdev->dev, "unable to set coherent mask to 64");
  712. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  713. if (rc)
  714. goto dmafree;
  715. }
  716. dmadev->lldev = hidma_ll_init(dmadev->ddev.dev,
  717. dmadev->nr_descriptors, dmadev->dev_trca,
  718. dmadev->dev_evca, dmadev->chidx);
  719. if (!dmadev->lldev) {
  720. rc = -EPROBE_DEFER;
  721. goto dmafree;
  722. }
  723. platform_set_drvdata(pdev, dmadev);
  724. if (msi)
  725. rc = hidma_request_msi(dmadev, pdev);
  726. if (!msi || rc) {
  727. hidma_ll_setup_irq(dmadev->lldev, false);
  728. rc = devm_request_irq(&pdev->dev, chirq, hidma_chirq_handler,
  729. 0, "qcom-hidma", dmadev->lldev);
  730. if (rc)
  731. goto uninit;
  732. }
  733. INIT_LIST_HEAD(&dmadev->ddev.channels);
  734. rc = hidma_chan_init(dmadev, 0);
  735. if (rc)
  736. goto uninit;
  737. rc = dma_async_device_register(&dmadev->ddev);
  738. if (rc)
  739. goto uninit;
  740. dmadev->irq = chirq;
  741. tasklet_init(&dmadev->task, hidma_issue_task, (unsigned long)dmadev);
  742. hidma_debug_init(dmadev);
  743. hidma_sysfs_init(dmadev);
  744. dev_info(&pdev->dev, "HI-DMA engine driver registration complete\n");
  745. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  746. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  747. return 0;
  748. uninit:
  749. if (msi)
  750. hidma_free_msis(dmadev);
  751. hidma_debug_uninit(dmadev);
  752. hidma_ll_uninit(dmadev->lldev);
  753. dmafree:
  754. if (dmadev)
  755. hidma_free(dmadev);
  756. bailout:
  757. pm_runtime_put_sync(&pdev->dev);
  758. pm_runtime_disable(&pdev->dev);
  759. return rc;
  760. }
  761. static void hidma_shutdown(struct platform_device *pdev)
  762. {
  763. struct hidma_dev *dmadev = platform_get_drvdata(pdev);
  764. dev_info(dmadev->ddev.dev, "HI-DMA engine shutdown\n");
  765. pm_runtime_get_sync(dmadev->ddev.dev);
  766. if (hidma_ll_disable(dmadev->lldev))
  767. dev_warn(dmadev->ddev.dev, "channel did not stop\n");
  768. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  769. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  770. }
  771. static int hidma_remove(struct platform_device *pdev)
  772. {
  773. struct hidma_dev *dmadev = platform_get_drvdata(pdev);
  774. pm_runtime_get_sync(dmadev->ddev.dev);
  775. dma_async_device_unregister(&dmadev->ddev);
  776. if (!dmadev->lldev->msi_support)
  777. devm_free_irq(dmadev->ddev.dev, dmadev->irq, dmadev->lldev);
  778. else
  779. hidma_free_msis(dmadev);
  780. tasklet_kill(&dmadev->task);
  781. hidma_sysfs_uninit(dmadev);
  782. hidma_debug_uninit(dmadev);
  783. hidma_ll_uninit(dmadev->lldev);
  784. hidma_free(dmadev);
  785. dev_info(&pdev->dev, "HI-DMA engine removed\n");
  786. pm_runtime_put_sync_suspend(&pdev->dev);
  787. pm_runtime_disable(&pdev->dev);
  788. return 0;
  789. }
  790. #if IS_ENABLED(CONFIG_ACPI)
  791. static const struct acpi_device_id hidma_acpi_ids[] = {
  792. {"QCOM8061"},
  793. {"QCOM8062", HIDMA_MSI_CAP},
  794. {"QCOM8063", (HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP)},
  795. {},
  796. };
  797. MODULE_DEVICE_TABLE(acpi, hidma_acpi_ids);
  798. #endif
  799. static const struct of_device_id hidma_match[] = {
  800. {.compatible = "qcom,hidma-1.0",},
  801. {.compatible = "qcom,hidma-1.1", .data = (void *)(HIDMA_MSI_CAP),},
  802. {.compatible = "qcom,hidma-1.2",
  803. .data = (void *)(HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP),},
  804. {},
  805. };
  806. MODULE_DEVICE_TABLE(of, hidma_match);
  807. static struct platform_driver hidma_driver = {
  808. .probe = hidma_probe,
  809. .remove = hidma_remove,
  810. .shutdown = hidma_shutdown,
  811. .driver = {
  812. .name = "hidma",
  813. .of_match_table = hidma_match,
  814. .acpi_match_table = ACPI_PTR(hidma_acpi_ids),
  815. },
  816. };
  817. module_platform_driver(hidma_driver);
  818. MODULE_LICENSE("GPL v2");