hidma.c 25 KB

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