hidma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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/mod_devicetable.h>
  46. #include <linux/module.h>
  47. #include <linux/platform_device.h>
  48. #include <linux/slab.h>
  49. #include <linux/spinlock.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 void hidma_free(struct hidma_dev *dmadev)
  84. {
  85. INIT_LIST_HEAD(&dmadev->ddev.channels);
  86. }
  87. static unsigned int nr_desc_prm;
  88. module_param(nr_desc_prm, uint, 0644);
  89. MODULE_PARM_DESC(nr_desc_prm, "number of descriptors (default: 0)");
  90. enum hidma_cap {
  91. HIDMA_MSI_CAP = 1,
  92. HIDMA_IDENTITY_CAP,
  93. };
  94. /* process completed descriptors */
  95. static void hidma_process_completed(struct hidma_chan *mchan)
  96. {
  97. struct dma_device *ddev = mchan->chan.device;
  98. struct hidma_dev *mdma = to_hidma_dev(ddev);
  99. struct dma_async_tx_descriptor *desc;
  100. dma_cookie_t last_cookie;
  101. struct hidma_desc *mdesc;
  102. struct hidma_desc *next;
  103. unsigned long irqflags;
  104. struct list_head list;
  105. INIT_LIST_HEAD(&list);
  106. /* Get all completed descriptors */
  107. spin_lock_irqsave(&mchan->lock, irqflags);
  108. list_splice_tail_init(&mchan->completed, &list);
  109. spin_unlock_irqrestore(&mchan->lock, irqflags);
  110. /* Execute callbacks and run dependencies */
  111. list_for_each_entry_safe(mdesc, next, &list, node) {
  112. enum dma_status llstat;
  113. struct dmaengine_desc_callback cb;
  114. struct dmaengine_result result;
  115. desc = &mdesc->desc;
  116. last_cookie = desc->cookie;
  117. llstat = hidma_ll_status(mdma->lldev, mdesc->tre_ch);
  118. spin_lock_irqsave(&mchan->lock, irqflags);
  119. if (llstat == DMA_COMPLETE) {
  120. mchan->last_success = last_cookie;
  121. result.result = DMA_TRANS_NOERROR;
  122. } else {
  123. result.result = DMA_TRANS_ABORTED;
  124. }
  125. dma_cookie_complete(desc);
  126. spin_unlock_irqrestore(&mchan->lock, irqflags);
  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. 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. return 0;
  184. }
  185. static void hidma_issue_task(struct tasklet_struct *t)
  186. {
  187. struct hidma_dev *dmadev = from_tasklet(dmadev, t, task);
  188. pm_runtime_get_sync(dmadev->ddev.dev);
  189. hidma_ll_start(dmadev->lldev);
  190. }
  191. static void hidma_issue_pending(struct dma_chan *dmach)
  192. {
  193. struct hidma_chan *mchan = to_hidma_chan(dmach);
  194. struct hidma_dev *dmadev = mchan->dmadev;
  195. unsigned long flags;
  196. struct hidma_desc *qdesc, *next;
  197. int status;
  198. spin_lock_irqsave(&mchan->lock, flags);
  199. list_for_each_entry_safe(qdesc, next, &mchan->queued, node) {
  200. hidma_ll_queue_request(dmadev->lldev, qdesc->tre_ch);
  201. list_move_tail(&qdesc->node, &mchan->active);
  202. }
  203. if (!mchan->running) {
  204. struct hidma_desc *desc = list_first_entry(&mchan->active,
  205. struct hidma_desc,
  206. node);
  207. mchan->running = desc;
  208. }
  209. spin_unlock_irqrestore(&mchan->lock, flags);
  210. /* PM will be released in hidma_callback function. */
  211. status = pm_runtime_get(dmadev->ddev.dev);
  212. if (status < 0)
  213. tasklet_schedule(&dmadev->task);
  214. else
  215. hidma_ll_start(dmadev->lldev);
  216. }
  217. static inline bool hidma_txn_is_success(dma_cookie_t cookie,
  218. dma_cookie_t last_success, dma_cookie_t last_used)
  219. {
  220. if (last_success <= last_used) {
  221. if ((cookie <= last_success) || (cookie > last_used))
  222. return true;
  223. } else {
  224. if ((cookie <= last_success) && (cookie > last_used))
  225. return true;
  226. }
  227. return false;
  228. }
  229. static enum dma_status hidma_tx_status(struct dma_chan *dmach,
  230. dma_cookie_t cookie,
  231. struct dma_tx_state *txstate)
  232. {
  233. struct hidma_chan *mchan = to_hidma_chan(dmach);
  234. enum dma_status ret;
  235. ret = dma_cookie_status(dmach, cookie, txstate);
  236. if (ret == DMA_COMPLETE) {
  237. bool is_success;
  238. is_success = hidma_txn_is_success(cookie, mchan->last_success,
  239. dmach->cookie);
  240. return is_success ? ret : DMA_ERROR;
  241. }
  242. if (mchan->paused && (ret == DMA_IN_PROGRESS)) {
  243. unsigned long flags;
  244. dma_cookie_t runcookie;
  245. spin_lock_irqsave(&mchan->lock, flags);
  246. if (mchan->running)
  247. runcookie = mchan->running->desc.cookie;
  248. else
  249. runcookie = -EINVAL;
  250. if (runcookie == cookie)
  251. ret = DMA_PAUSED;
  252. spin_unlock_irqrestore(&mchan->lock, flags);
  253. }
  254. return ret;
  255. }
  256. /*
  257. * Submit descriptor to hardware.
  258. * Lock the PM for each descriptor we are sending.
  259. */
  260. static dma_cookie_t hidma_tx_submit(struct dma_async_tx_descriptor *txd)
  261. {
  262. struct hidma_chan *mchan = to_hidma_chan(txd->chan);
  263. struct hidma_dev *dmadev = mchan->dmadev;
  264. struct hidma_desc *mdesc;
  265. unsigned long irqflags;
  266. dma_cookie_t cookie;
  267. pm_runtime_get_sync(dmadev->ddev.dev);
  268. if (!hidma_ll_isenabled(dmadev->lldev)) {
  269. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  270. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  271. return -ENODEV;
  272. }
  273. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  274. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  275. mdesc = container_of(txd, struct hidma_desc, desc);
  276. spin_lock_irqsave(&mchan->lock, irqflags);
  277. /* Move descriptor to queued */
  278. list_move_tail(&mdesc->node, &mchan->queued);
  279. /* Update cookie */
  280. cookie = dma_cookie_assign(txd);
  281. spin_unlock_irqrestore(&mchan->lock, irqflags);
  282. return cookie;
  283. }
  284. static int hidma_alloc_chan_resources(struct dma_chan *dmach)
  285. {
  286. struct hidma_chan *mchan = to_hidma_chan(dmach);
  287. struct hidma_dev *dmadev = mchan->dmadev;
  288. struct hidma_desc *mdesc, *tmp;
  289. unsigned long irqflags;
  290. LIST_HEAD(descs);
  291. unsigned int i;
  292. int rc = 0;
  293. if (mchan->allocated)
  294. return 0;
  295. /* Alloc descriptors for this channel */
  296. for (i = 0; i < dmadev->nr_descriptors; i++) {
  297. mdesc = kzalloc(sizeof(struct hidma_desc), GFP_NOWAIT);
  298. if (!mdesc) {
  299. rc = -ENOMEM;
  300. break;
  301. }
  302. dma_async_tx_descriptor_init(&mdesc->desc, dmach);
  303. mdesc->desc.tx_submit = hidma_tx_submit;
  304. rc = hidma_ll_request(dmadev->lldev, mchan->dma_sig,
  305. "DMA engine", hidma_callback, mdesc,
  306. &mdesc->tre_ch);
  307. if (rc) {
  308. dev_err(dmach->device->dev,
  309. "channel alloc failed at %u\n", i);
  310. kfree(mdesc);
  311. break;
  312. }
  313. list_add_tail(&mdesc->node, &descs);
  314. }
  315. if (rc) {
  316. /* return the allocated descriptors */
  317. list_for_each_entry_safe(mdesc, tmp, &descs, node) {
  318. hidma_ll_free(dmadev->lldev, mdesc->tre_ch);
  319. kfree(mdesc);
  320. }
  321. return rc;
  322. }
  323. spin_lock_irqsave(&mchan->lock, irqflags);
  324. list_splice_tail_init(&descs, &mchan->free);
  325. mchan->allocated = true;
  326. spin_unlock_irqrestore(&mchan->lock, irqflags);
  327. return 1;
  328. }
  329. static struct dma_async_tx_descriptor *
  330. hidma_prep_dma_memcpy(struct dma_chan *dmach, dma_addr_t dest, dma_addr_t src,
  331. size_t len, unsigned long flags)
  332. {
  333. struct hidma_chan *mchan = to_hidma_chan(dmach);
  334. struct hidma_desc *mdesc = NULL;
  335. struct hidma_dev *mdma = mchan->dmadev;
  336. unsigned long irqflags;
  337. /* Get free descriptor */
  338. spin_lock_irqsave(&mchan->lock, irqflags);
  339. if (!list_empty(&mchan->free)) {
  340. mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
  341. list_del(&mdesc->node);
  342. }
  343. spin_unlock_irqrestore(&mchan->lock, irqflags);
  344. if (!mdesc)
  345. return NULL;
  346. mdesc->desc.flags = flags;
  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. u64 byte_pattern, fill_pattern;
  365. /* Get free descriptor */
  366. spin_lock_irqsave(&mchan->lock, irqflags);
  367. if (!list_empty(&mchan->free)) {
  368. mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
  369. list_del(&mdesc->node);
  370. }
  371. spin_unlock_irqrestore(&mchan->lock, irqflags);
  372. if (!mdesc)
  373. return NULL;
  374. byte_pattern = (char)value;
  375. fill_pattern = (byte_pattern << 56) |
  376. (byte_pattern << 48) |
  377. (byte_pattern << 40) |
  378. (byte_pattern << 32) |
  379. (byte_pattern << 24) |
  380. (byte_pattern << 16) |
  381. (byte_pattern << 8) |
  382. byte_pattern;
  383. mdesc->desc.flags = flags;
  384. hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
  385. fill_pattern, dest, len, flags,
  386. HIDMA_TRE_MEMSET);
  387. /* Place descriptor in prepared list */
  388. spin_lock_irqsave(&mchan->lock, irqflags);
  389. list_add_tail(&mdesc->node, &mchan->prepared);
  390. spin_unlock_irqrestore(&mchan->lock, irqflags);
  391. return &mdesc->desc;
  392. }
  393. static int hidma_terminate_channel(struct dma_chan *chan)
  394. {
  395. struct hidma_chan *mchan = to_hidma_chan(chan);
  396. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  397. struct hidma_desc *tmp, *mdesc;
  398. unsigned long irqflags;
  399. LIST_HEAD(list);
  400. int rc;
  401. pm_runtime_get_sync(dmadev->ddev.dev);
  402. /* give completed requests a chance to finish */
  403. hidma_process_completed(mchan);
  404. spin_lock_irqsave(&mchan->lock, irqflags);
  405. mchan->last_success = 0;
  406. list_splice_init(&mchan->active, &list);
  407. list_splice_init(&mchan->prepared, &list);
  408. list_splice_init(&mchan->completed, &list);
  409. list_splice_init(&mchan->queued, &list);
  410. spin_unlock_irqrestore(&mchan->lock, irqflags);
  411. /* this suspends the existing transfer */
  412. rc = hidma_ll_disable(dmadev->lldev);
  413. if (rc) {
  414. dev_err(dmadev->ddev.dev, "channel did not pause\n");
  415. goto out;
  416. }
  417. /* return all user requests */
  418. list_for_each_entry_safe(mdesc, tmp, &list, node) {
  419. struct dma_async_tx_descriptor *txd = &mdesc->desc;
  420. dma_descriptor_unmap(txd);
  421. dmaengine_desc_get_callback_invoke(txd, NULL);
  422. dma_run_dependencies(txd);
  423. /* move myself to free_list */
  424. list_move(&mdesc->node, &mchan->free);
  425. }
  426. rc = hidma_ll_enable(dmadev->lldev);
  427. out:
  428. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  429. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  430. return rc;
  431. }
  432. static int hidma_terminate_all(struct dma_chan *chan)
  433. {
  434. struct hidma_chan *mchan = to_hidma_chan(chan);
  435. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  436. int rc;
  437. rc = hidma_terminate_channel(chan);
  438. if (rc)
  439. return rc;
  440. /* reinitialize the hardware */
  441. pm_runtime_get_sync(dmadev->ddev.dev);
  442. rc = hidma_ll_setup(dmadev->lldev);
  443. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  444. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  445. return rc;
  446. }
  447. static void hidma_free_chan_resources(struct dma_chan *dmach)
  448. {
  449. struct hidma_chan *mchan = to_hidma_chan(dmach);
  450. struct hidma_dev *mdma = mchan->dmadev;
  451. struct hidma_desc *mdesc, *tmp;
  452. unsigned long irqflags;
  453. LIST_HEAD(descs);
  454. /* terminate running transactions and free descriptors */
  455. hidma_terminate_channel(dmach);
  456. spin_lock_irqsave(&mchan->lock, irqflags);
  457. /* Move data */
  458. list_splice_tail_init(&mchan->free, &descs);
  459. /* Free descriptors */
  460. list_for_each_entry_safe(mdesc, tmp, &descs, node) {
  461. hidma_ll_free(mdma->lldev, mdesc->tre_ch);
  462. list_del(&mdesc->node);
  463. kfree(mdesc);
  464. }
  465. mchan->allocated = false;
  466. spin_unlock_irqrestore(&mchan->lock, irqflags);
  467. }
  468. static int hidma_pause(struct dma_chan *chan)
  469. {
  470. struct hidma_chan *mchan;
  471. struct hidma_dev *dmadev;
  472. mchan = to_hidma_chan(chan);
  473. dmadev = to_hidma_dev(mchan->chan.device);
  474. if (!mchan->paused) {
  475. pm_runtime_get_sync(dmadev->ddev.dev);
  476. if (hidma_ll_disable(dmadev->lldev))
  477. dev_warn(dmadev->ddev.dev, "channel did not stop\n");
  478. mchan->paused = true;
  479. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  480. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  481. }
  482. return 0;
  483. }
  484. static int hidma_resume(struct dma_chan *chan)
  485. {
  486. struct hidma_chan *mchan;
  487. struct hidma_dev *dmadev;
  488. int rc = 0;
  489. mchan = to_hidma_chan(chan);
  490. dmadev = to_hidma_dev(mchan->chan.device);
  491. if (mchan->paused) {
  492. pm_runtime_get_sync(dmadev->ddev.dev);
  493. rc = hidma_ll_enable(dmadev->lldev);
  494. if (!rc)
  495. mchan->paused = false;
  496. else
  497. dev_err(dmadev->ddev.dev,
  498. "failed to resume the channel");
  499. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  500. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  501. }
  502. return rc;
  503. }
  504. static irqreturn_t hidma_chirq_handler(int chirq, void *arg)
  505. {
  506. struct hidma_lldev *lldev = arg;
  507. /*
  508. * All interrupts are request driven.
  509. * HW doesn't send an interrupt by itself.
  510. */
  511. return hidma_ll_inthandler(chirq, lldev);
  512. }
  513. #ifdef CONFIG_GENERIC_MSI_IRQ
  514. static irqreturn_t hidma_chirq_handler_msi(int chirq, void *arg)
  515. {
  516. struct hidma_lldev **lldevp = arg;
  517. struct hidma_dev *dmadev = to_hidma_dev_from_lldev(lldevp);
  518. return hidma_ll_inthandler_msi(chirq, *lldevp,
  519. 1 << (chirq - dmadev->msi_virqbase));
  520. }
  521. #endif
  522. static ssize_t hidma_show_values(struct device *dev,
  523. struct device_attribute *attr, char *buf)
  524. {
  525. struct hidma_dev *mdev = dev_get_drvdata(dev);
  526. buf[0] = 0;
  527. if (strcmp(attr->attr.name, "chid") == 0)
  528. sprintf(buf, "%d\n", mdev->chidx);
  529. return strlen(buf);
  530. }
  531. static inline void hidma_sysfs_uninit(struct hidma_dev *dev)
  532. {
  533. device_remove_file(dev->ddev.dev, dev->chid_attrs);
  534. }
  535. static struct device_attribute*
  536. hidma_create_sysfs_entry(struct hidma_dev *dev, char *name, int mode)
  537. {
  538. struct device_attribute *attrs;
  539. char *name_copy;
  540. attrs = devm_kmalloc(dev->ddev.dev, sizeof(struct device_attribute),
  541. GFP_KERNEL);
  542. if (!attrs)
  543. return NULL;
  544. name_copy = devm_kstrdup(dev->ddev.dev, name, GFP_KERNEL);
  545. if (!name_copy)
  546. return NULL;
  547. attrs->attr.name = name_copy;
  548. attrs->attr.mode = mode;
  549. attrs->show = hidma_show_values;
  550. sysfs_attr_init(&attrs->attr);
  551. return attrs;
  552. }
  553. static int hidma_sysfs_init(struct hidma_dev *dev)
  554. {
  555. dev->chid_attrs = hidma_create_sysfs_entry(dev, "chid", S_IRUGO);
  556. if (!dev->chid_attrs)
  557. return -ENOMEM;
  558. return device_create_file(dev->ddev.dev, dev->chid_attrs);
  559. }
  560. #ifdef CONFIG_GENERIC_MSI_IRQ
  561. static void hidma_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  562. {
  563. struct device *dev = msi_desc_to_dev(desc);
  564. struct hidma_dev *dmadev = dev_get_drvdata(dev);
  565. if (!desc->msi_index) {
  566. writel(msg->address_lo, dmadev->dev_evca + 0x118);
  567. writel(msg->address_hi, dmadev->dev_evca + 0x11C);
  568. writel(msg->data, dmadev->dev_evca + 0x120);
  569. }
  570. }
  571. #endif
  572. static void hidma_free_msis(struct hidma_dev *dmadev)
  573. {
  574. #ifdef CONFIG_GENERIC_MSI_IRQ
  575. struct device *dev = dmadev->ddev.dev;
  576. int i, virq;
  577. for (i = 0; i < HIDMA_MSI_INTS; i++) {
  578. virq = msi_get_virq(dev, i);
  579. if (virq)
  580. devm_free_irq(dev, virq, &dmadev->lldev);
  581. }
  582. platform_device_msi_free_irqs_all(dev);
  583. #endif
  584. }
  585. static int hidma_request_msi(struct hidma_dev *dmadev,
  586. struct platform_device *pdev)
  587. {
  588. #ifdef CONFIG_GENERIC_MSI_IRQ
  589. int rc, i, virq;
  590. rc = platform_device_msi_init_and_alloc_irqs(&pdev->dev, HIDMA_MSI_INTS,
  591. hidma_write_msi_msg);
  592. if (rc)
  593. return rc;
  594. for (i = 0; i < HIDMA_MSI_INTS; i++) {
  595. virq = msi_get_virq(&pdev->dev, i);
  596. rc = devm_request_irq(&pdev->dev, virq,
  597. hidma_chirq_handler_msi,
  598. 0, "qcom-hidma-msi",
  599. &dmadev->lldev);
  600. if (rc)
  601. break;
  602. if (!i)
  603. dmadev->msi_virqbase = virq;
  604. }
  605. if (rc) {
  606. /* free allocated MSI interrupts above */
  607. for (--i; i >= 0; i--) {
  608. virq = msi_get_virq(&pdev->dev, i);
  609. devm_free_irq(&pdev->dev, virq, &dmadev->lldev);
  610. }
  611. dev_warn(&pdev->dev,
  612. "failed to request MSI irq, falling back to wired IRQ\n");
  613. } else {
  614. /* Add callback to free MSIs on teardown */
  615. hidma_ll_setup_irq(dmadev->lldev, true);
  616. }
  617. return rc;
  618. #else
  619. return -EINVAL;
  620. #endif
  621. }
  622. static bool hidma_test_capability(struct device *dev, enum hidma_cap test_cap)
  623. {
  624. enum hidma_cap cap;
  625. cap = (uintptr_t) device_get_match_data(dev);
  626. return cap ? ((cap & test_cap) > 0) : 0;
  627. }
  628. static int hidma_probe(struct platform_device *pdev)
  629. {
  630. struct hidma_dev *dmadev;
  631. struct resource *trca_resource;
  632. struct resource *evca_resource;
  633. int chirq;
  634. void __iomem *evca;
  635. void __iomem *trca;
  636. int rc;
  637. bool msi;
  638. pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
  639. pm_runtime_use_autosuspend(&pdev->dev);
  640. pm_runtime_set_active(&pdev->dev);
  641. pm_runtime_enable(&pdev->dev);
  642. trca = devm_platform_get_and_ioremap_resource(pdev, 0, &trca_resource);
  643. if (IS_ERR(trca)) {
  644. rc = PTR_ERR(trca);
  645. goto bailout;
  646. }
  647. evca = devm_platform_get_and_ioremap_resource(pdev, 1, &evca_resource);
  648. if (IS_ERR(evca)) {
  649. rc = PTR_ERR(evca);
  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 = chirq;
  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. goto dmafree;
  713. }
  714. dmadev->lldev = hidma_ll_init(dmadev->ddev.dev,
  715. dmadev->nr_descriptors, dmadev->dev_trca,
  716. dmadev->dev_evca, dmadev->chidx);
  717. if (!dmadev->lldev) {
  718. rc = -EPROBE_DEFER;
  719. goto dmafree;
  720. }
  721. platform_set_drvdata(pdev, dmadev);
  722. if (msi)
  723. rc = hidma_request_msi(dmadev, pdev);
  724. if (!msi || rc) {
  725. hidma_ll_setup_irq(dmadev->lldev, false);
  726. rc = devm_request_irq(&pdev->dev, chirq, hidma_chirq_handler,
  727. 0, "qcom-hidma", dmadev->lldev);
  728. if (rc)
  729. goto uninit;
  730. }
  731. INIT_LIST_HEAD(&dmadev->ddev.channels);
  732. rc = hidma_chan_init(dmadev, 0);
  733. if (rc)
  734. goto uninit;
  735. rc = dma_async_device_register(&dmadev->ddev);
  736. if (rc)
  737. goto uninit;
  738. dmadev->irq = chirq;
  739. tasklet_setup(&dmadev->task, hidma_issue_task);
  740. hidma_debug_init(dmadev);
  741. hidma_sysfs_init(dmadev);
  742. dev_info(&pdev->dev, "HI-DMA engine driver registration complete\n");
  743. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  744. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  745. return 0;
  746. uninit:
  747. if (msi)
  748. hidma_free_msis(dmadev);
  749. hidma_ll_uninit(dmadev->lldev);
  750. dmafree:
  751. if (dmadev)
  752. hidma_free(dmadev);
  753. bailout:
  754. pm_runtime_put_sync(&pdev->dev);
  755. pm_runtime_disable(&pdev->dev);
  756. return rc;
  757. }
  758. static void hidma_shutdown(struct platform_device *pdev)
  759. {
  760. struct hidma_dev *dmadev = platform_get_drvdata(pdev);
  761. dev_info(dmadev->ddev.dev, "HI-DMA engine shutdown\n");
  762. pm_runtime_get_sync(dmadev->ddev.dev);
  763. if (hidma_ll_disable(dmadev->lldev))
  764. dev_warn(dmadev->ddev.dev, "channel did not stop\n");
  765. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  766. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  767. }
  768. static void hidma_remove(struct platform_device *pdev)
  769. {
  770. struct hidma_dev *dmadev = platform_get_drvdata(pdev);
  771. pm_runtime_get_sync(dmadev->ddev.dev);
  772. dma_async_device_unregister(&dmadev->ddev);
  773. if (!dmadev->lldev->msi_support)
  774. devm_free_irq(dmadev->ddev.dev, dmadev->irq, dmadev->lldev);
  775. else
  776. hidma_free_msis(dmadev);
  777. tasklet_kill(&dmadev->task);
  778. hidma_sysfs_uninit(dmadev);
  779. hidma_debug_uninit(dmadev);
  780. hidma_ll_uninit(dmadev->lldev);
  781. hidma_free(dmadev);
  782. dev_info(&pdev->dev, "HI-DMA engine removed\n");
  783. pm_runtime_put_sync_suspend(&pdev->dev);
  784. pm_runtime_disable(&pdev->dev);
  785. }
  786. #if IS_ENABLED(CONFIG_ACPI)
  787. static const struct acpi_device_id hidma_acpi_ids[] = {
  788. {"QCOM8061"},
  789. {"QCOM8062", HIDMA_MSI_CAP},
  790. {"QCOM8063", (HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP)},
  791. {},
  792. };
  793. MODULE_DEVICE_TABLE(acpi, hidma_acpi_ids);
  794. #endif
  795. static struct platform_driver hidma_driver = {
  796. .probe = hidma_probe,
  797. .remove_new = hidma_remove,
  798. .shutdown = hidma_shutdown,
  799. .driver = {
  800. .name = "hidma",
  801. .acpi_match_table = ACPI_PTR(hidma_acpi_ids),
  802. },
  803. };
  804. module_platform_driver(hidma_driver);
  805. MODULE_DESCRIPTION("Qualcomm Technologies HIDMA Channel support");
  806. MODULE_LICENSE("GPL v2");