hidma.c 25 KB

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