hidma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 <[email protected]>. Hardware description
  22. * (defines, structures and comments) was taken from MPC5121 DMA driver
  23. * written by Hongjun Chen <[email protected]>.
  24. *
  25. * Approved as OSADL project by a majority of OSADL members and funded
  26. * by OSADL membership fees in 2009; for details see www.osadl.org.
  27. *
  28. * This program is free software; you can redistribute it and/or modify it
  29. * under the terms of the GNU General Public License as published by the Free
  30. * Software Foundation; either version 2 of the License, or (at your option)
  31. * any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful, but WITHOUT
  34. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  35. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  36. * more details.
  37. *
  38. * The full GNU General Public License is included in this distribution in the
  39. * file called COPYING.
  40. */
  41. /* Linux Foundation elects GPLv2 license only. */
  42. #include <linux/dmaengine.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/list.h>
  45. #include <linux/module.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/slab.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/of_dma.h>
  50. #include <linux/of_device.h>
  51. #include <linux/property.h>
  52. #include <linux/delay.h>
  53. #include <linux/acpi.h>
  54. #include <linux/irq.h>
  55. #include <linux/atomic.h>
  56. #include <linux/pm_runtime.h>
  57. #include <linux/msi.h>
  58. #include "../dmaengine.h"
  59. #include "hidma.h"
  60. /*
  61. * Default idle time is 2 seconds. This parameter can
  62. * be overridden by changing the following
  63. * /sys/bus/platform/devices/QCOM8061:<xy>/power/autosuspend_delay_ms
  64. * during kernel boot.
  65. */
  66. #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
  67. #define HIDMA_ERR_INFO_SW 0xFF
  68. #define HIDMA_ERR_CODE_UNEXPECTED_TERMINATE 0x0
  69. #define HIDMA_NR_DEFAULT_DESC 10
  70. #define HIDMA_MSI_INTS 11
  71. static inline struct hidma_dev *to_hidma_dev(struct dma_device *dmadev)
  72. {
  73. return container_of(dmadev, struct hidma_dev, ddev);
  74. }
  75. static inline
  76. struct hidma_dev *to_hidma_dev_from_lldev(struct hidma_lldev **_lldevp)
  77. {
  78. return container_of(_lldevp, struct hidma_dev, lldev);
  79. }
  80. static inline struct hidma_chan *to_hidma_chan(struct dma_chan *dmach)
  81. {
  82. return container_of(dmach, struct hidma_chan, chan);
  83. }
  84. static void hidma_free(struct hidma_dev *dmadev)
  85. {
  86. INIT_LIST_HEAD(&dmadev->ddev.channels);
  87. }
  88. static unsigned int nr_desc_prm;
  89. module_param(nr_desc_prm, uint, 0644);
  90. MODULE_PARM_DESC(nr_desc_prm, "number of descriptors (default: 0)");
  91. enum hidma_cap {
  92. HIDMA_MSI_CAP = 1,
  93. HIDMA_IDENTITY_CAP,
  94. };
  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(struct tasklet_struct *t)
  188. {
  189. struct hidma_dev *dmadev = from_tasklet(dmadev, t, task);
  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. u64 byte_pattern, fill_pattern;
  367. /* Get free descriptor */
  368. spin_lock_irqsave(&mchan->lock, irqflags);
  369. if (!list_empty(&mchan->free)) {
  370. mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
  371. list_del(&mdesc->node);
  372. }
  373. spin_unlock_irqrestore(&mchan->lock, irqflags);
  374. if (!mdesc)
  375. return NULL;
  376. byte_pattern = (char)value;
  377. fill_pattern = (byte_pattern << 56) |
  378. (byte_pattern << 48) |
  379. (byte_pattern << 40) |
  380. (byte_pattern << 32) |
  381. (byte_pattern << 24) |
  382. (byte_pattern << 16) |
  383. (byte_pattern << 8) |
  384. byte_pattern;
  385. mdesc->desc.flags = flags;
  386. hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
  387. fill_pattern, dest, len, flags,
  388. HIDMA_TRE_MEMSET);
  389. /* Place descriptor in prepared list */
  390. spin_lock_irqsave(&mchan->lock, irqflags);
  391. list_add_tail(&mdesc->node, &mchan->prepared);
  392. spin_unlock_irqrestore(&mchan->lock, irqflags);
  393. return &mdesc->desc;
  394. }
  395. static int hidma_terminate_channel(struct dma_chan *chan)
  396. {
  397. struct hidma_chan *mchan = to_hidma_chan(chan);
  398. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  399. struct hidma_desc *tmp, *mdesc;
  400. unsigned long irqflags;
  401. LIST_HEAD(list);
  402. int rc;
  403. pm_runtime_get_sync(dmadev->ddev.dev);
  404. /* give completed requests a chance to finish */
  405. hidma_process_completed(mchan);
  406. spin_lock_irqsave(&mchan->lock, irqflags);
  407. mchan->last_success = 0;
  408. list_splice_init(&mchan->active, &list);
  409. list_splice_init(&mchan->prepared, &list);
  410. list_splice_init(&mchan->completed, &list);
  411. list_splice_init(&mchan->queued, &list);
  412. spin_unlock_irqrestore(&mchan->lock, irqflags);
  413. /* this suspends the existing transfer */
  414. rc = hidma_ll_disable(dmadev->lldev);
  415. if (rc) {
  416. dev_err(dmadev->ddev.dev, "channel did not pause\n");
  417. goto out;
  418. }
  419. /* return all user requests */
  420. list_for_each_entry_safe(mdesc, tmp, &list, node) {
  421. struct dma_async_tx_descriptor *txd = &mdesc->desc;
  422. dma_descriptor_unmap(txd);
  423. dmaengine_desc_get_callback_invoke(txd, NULL);
  424. dma_run_dependencies(txd);
  425. /* move myself to free_list */
  426. list_move(&mdesc->node, &mchan->free);
  427. }
  428. rc = hidma_ll_enable(dmadev->lldev);
  429. out:
  430. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  431. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  432. return rc;
  433. }
  434. static int hidma_terminate_all(struct dma_chan *chan)
  435. {
  436. struct hidma_chan *mchan = to_hidma_chan(chan);
  437. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  438. int rc;
  439. rc = hidma_terminate_channel(chan);
  440. if (rc)
  441. return rc;
  442. /* reinitialize the hardware */
  443. pm_runtime_get_sync(dmadev->ddev.dev);
  444. rc = hidma_ll_setup(dmadev->lldev);
  445. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  446. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  447. return rc;
  448. }
  449. static void hidma_free_chan_resources(struct dma_chan *dmach)
  450. {
  451. struct hidma_chan *mchan = to_hidma_chan(dmach);
  452. struct hidma_dev *mdma = mchan->dmadev;
  453. struct hidma_desc *mdesc, *tmp;
  454. unsigned long irqflags;
  455. LIST_HEAD(descs);
  456. /* terminate running transactions and free descriptors */
  457. hidma_terminate_channel(dmach);
  458. spin_lock_irqsave(&mchan->lock, irqflags);
  459. /* Move data */
  460. list_splice_tail_init(&mchan->free, &descs);
  461. /* Free descriptors */
  462. list_for_each_entry_safe(mdesc, tmp, &descs, node) {
  463. hidma_ll_free(mdma->lldev, mdesc->tre_ch);
  464. list_del(&mdesc->node);
  465. kfree(mdesc);
  466. }
  467. mchan->allocated = false;
  468. spin_unlock_irqrestore(&mchan->lock, irqflags);
  469. }
  470. static int hidma_pause(struct dma_chan *chan)
  471. {
  472. struct hidma_chan *mchan;
  473. struct hidma_dev *dmadev;
  474. mchan = to_hidma_chan(chan);
  475. dmadev = to_hidma_dev(mchan->chan.device);
  476. if (!mchan->paused) {
  477. pm_runtime_get_sync(dmadev->ddev.dev);
  478. if (hidma_ll_disable(dmadev->lldev))
  479. dev_warn(dmadev->ddev.dev, "channel did not stop\n");
  480. mchan->paused = true;
  481. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  482. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  483. }
  484. return 0;
  485. }
  486. static int hidma_resume(struct dma_chan *chan)
  487. {
  488. struct hidma_chan *mchan;
  489. struct hidma_dev *dmadev;
  490. int rc = 0;
  491. mchan = to_hidma_chan(chan);
  492. dmadev = to_hidma_dev(mchan->chan.device);
  493. if (mchan->paused) {
  494. pm_runtime_get_sync(dmadev->ddev.dev);
  495. rc = hidma_ll_enable(dmadev->lldev);
  496. if (!rc)
  497. mchan->paused = false;
  498. else
  499. dev_err(dmadev->ddev.dev,
  500. "failed to resume the channel");
  501. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  502. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  503. }
  504. return rc;
  505. }
  506. static irqreturn_t hidma_chirq_handler(int chirq, void *arg)
  507. {
  508. struct hidma_lldev *lldev = arg;
  509. /*
  510. * All interrupts are request driven.
  511. * HW doesn't send an interrupt by itself.
  512. */
  513. return hidma_ll_inthandler(chirq, lldev);
  514. }
  515. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  516. static irqreturn_t hidma_chirq_handler_msi(int chirq, void *arg)
  517. {
  518. struct hidma_lldev **lldevp = arg;
  519. struct hidma_dev *dmadev = to_hidma_dev_from_lldev(lldevp);
  520. return hidma_ll_inthandler_msi(chirq, *lldevp,
  521. 1 << (chirq - dmadev->msi_virqbase));
  522. }
  523. #endif
  524. static ssize_t hidma_show_values(struct device *dev,
  525. struct device_attribute *attr, char *buf)
  526. {
  527. struct hidma_dev *mdev = dev_get_drvdata(dev);
  528. buf[0] = 0;
  529. if (strcmp(attr->attr.name, "chid") == 0)
  530. sprintf(buf, "%d\n", mdev->chidx);
  531. return strlen(buf);
  532. }
  533. static inline void hidma_sysfs_uninit(struct hidma_dev *dev)
  534. {
  535. device_remove_file(dev->ddev.dev, dev->chid_attrs);
  536. }
  537. static struct device_attribute*
  538. hidma_create_sysfs_entry(struct hidma_dev *dev, char *name, int mode)
  539. {
  540. struct device_attribute *attrs;
  541. char *name_copy;
  542. attrs = devm_kmalloc(dev->ddev.dev, sizeof(struct device_attribute),
  543. GFP_KERNEL);
  544. if (!attrs)
  545. return NULL;
  546. name_copy = devm_kstrdup(dev->ddev.dev, name, GFP_KERNEL);
  547. if (!name_copy)
  548. return NULL;
  549. attrs->attr.name = name_copy;
  550. attrs->attr.mode = mode;
  551. attrs->show = hidma_show_values;
  552. sysfs_attr_init(&attrs->attr);
  553. return attrs;
  554. }
  555. static int hidma_sysfs_init(struct hidma_dev *dev)
  556. {
  557. dev->chid_attrs = hidma_create_sysfs_entry(dev, "chid", S_IRUGO);
  558. if (!dev->chid_attrs)
  559. return -ENOMEM;
  560. return device_create_file(dev->ddev.dev, dev->chid_attrs);
  561. }
  562. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  563. static void hidma_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  564. {
  565. struct device *dev = msi_desc_to_dev(desc);
  566. struct hidma_dev *dmadev = dev_get_drvdata(dev);
  567. if (!desc->msi_index) {
  568. writel(msg->address_lo, dmadev->dev_evca + 0x118);
  569. writel(msg->address_hi, dmadev->dev_evca + 0x11C);
  570. writel(msg->data, dmadev->dev_evca + 0x120);
  571. }
  572. }
  573. #endif
  574. static void hidma_free_msis(struct hidma_dev *dmadev)
  575. {
  576. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  577. struct device *dev = dmadev->ddev.dev;
  578. int i, virq;
  579. for (i = 0; i < HIDMA_MSI_INTS; i++) {
  580. virq = msi_get_virq(dev, i);
  581. if (virq)
  582. devm_free_irq(dev, virq, &dmadev->lldev);
  583. }
  584. platform_msi_domain_free_irqs(dev);
  585. #endif
  586. }
  587. static int hidma_request_msi(struct hidma_dev *dmadev,
  588. struct platform_device *pdev)
  589. {
  590. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  591. int rc, i, virq;
  592. rc = platform_msi_domain_alloc_irqs(&pdev->dev, HIDMA_MSI_INTS,
  593. hidma_write_msi_msg);
  594. if (rc)
  595. return rc;
  596. for (i = 0; i < HIDMA_MSI_INTS; i++) {
  597. virq = msi_get_virq(&pdev->dev, i);
  598. rc = devm_request_irq(&pdev->dev, virq,
  599. hidma_chirq_handler_msi,
  600. 0, "qcom-hidma-msi",
  601. &dmadev->lldev);
  602. if (rc)
  603. break;
  604. if (!i)
  605. dmadev->msi_virqbase = virq;
  606. }
  607. if (rc) {
  608. /* free allocated MSI interrupts above */
  609. for (--i; i >= 0; i--) {
  610. virq = msi_get_virq(&pdev->dev, i);
  611. devm_free_irq(&pdev->dev, virq, &dmadev->lldev);
  612. }
  613. dev_warn(&pdev->dev,
  614. "failed to request MSI irq, falling back to wired IRQ\n");
  615. } else {
  616. /* Add callback to free MSIs on teardown */
  617. hidma_ll_setup_irq(dmadev->lldev, true);
  618. }
  619. return rc;
  620. #else
  621. return -EINVAL;
  622. #endif
  623. }
  624. static bool hidma_test_capability(struct device *dev, enum hidma_cap test_cap)
  625. {
  626. enum hidma_cap cap;
  627. cap = (enum hidma_cap) device_get_match_data(dev);
  628. return cap ? ((cap & test_cap) > 0) : 0;
  629. }
  630. static int hidma_probe(struct platform_device *pdev)
  631. {
  632. struct hidma_dev *dmadev;
  633. struct resource *trca_resource;
  634. struct resource *evca_resource;
  635. int chirq;
  636. void __iomem *evca;
  637. void __iomem *trca;
  638. int rc;
  639. bool msi;
  640. pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
  641. pm_runtime_use_autosuspend(&pdev->dev);
  642. pm_runtime_set_active(&pdev->dev);
  643. pm_runtime_enable(&pdev->dev);
  644. trca_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  645. trca = devm_ioremap_resource(&pdev->dev, trca_resource);
  646. if (IS_ERR(trca)) {
  647. rc = -ENOMEM;
  648. goto bailout;
  649. }
  650. evca_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  651. evca = devm_ioremap_resource(&pdev->dev, evca_resource);
  652. if (IS_ERR(evca)) {
  653. rc = -ENOMEM;
  654. goto bailout;
  655. }
  656. /*
  657. * This driver only handles the channel IRQs.
  658. * Common IRQ is handled by the management driver.
  659. */
  660. chirq = platform_get_irq(pdev, 0);
  661. if (chirq < 0) {
  662. rc = -ENODEV;
  663. goto bailout;
  664. }
  665. dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
  666. if (!dmadev) {
  667. rc = -ENOMEM;
  668. goto bailout;
  669. }
  670. INIT_LIST_HEAD(&dmadev->ddev.channels);
  671. spin_lock_init(&dmadev->lock);
  672. dmadev->ddev.dev = &pdev->dev;
  673. pm_runtime_get_sync(dmadev->ddev.dev);
  674. dma_cap_set(DMA_MEMCPY, dmadev->ddev.cap_mask);
  675. dma_cap_set(DMA_MEMSET, dmadev->ddev.cap_mask);
  676. if (WARN_ON(!pdev->dev.dma_mask)) {
  677. rc = -ENXIO;
  678. goto dmafree;
  679. }
  680. dmadev->dev_evca = evca;
  681. dmadev->evca_resource = evca_resource;
  682. dmadev->dev_trca = trca;
  683. dmadev->trca_resource = trca_resource;
  684. dmadev->ddev.device_prep_dma_memcpy = hidma_prep_dma_memcpy;
  685. dmadev->ddev.device_prep_dma_memset = hidma_prep_dma_memset;
  686. dmadev->ddev.device_alloc_chan_resources = hidma_alloc_chan_resources;
  687. dmadev->ddev.device_free_chan_resources = hidma_free_chan_resources;
  688. dmadev->ddev.device_tx_status = hidma_tx_status;
  689. dmadev->ddev.device_issue_pending = hidma_issue_pending;
  690. dmadev->ddev.device_pause = hidma_pause;
  691. dmadev->ddev.device_resume = hidma_resume;
  692. dmadev->ddev.device_terminate_all = hidma_terminate_all;
  693. dmadev->ddev.copy_align = 8;
  694. /*
  695. * Determine the MSI capability of the platform. Old HW doesn't
  696. * support MSI.
  697. */
  698. msi = hidma_test_capability(&pdev->dev, HIDMA_MSI_CAP);
  699. device_property_read_u32(&pdev->dev, "desc-count",
  700. &dmadev->nr_descriptors);
  701. if (nr_desc_prm) {
  702. dev_info(&pdev->dev, "overriding number of descriptors as %d\n",
  703. nr_desc_prm);
  704. dmadev->nr_descriptors = nr_desc_prm;
  705. }
  706. if (!dmadev->nr_descriptors)
  707. dmadev->nr_descriptors = HIDMA_NR_DEFAULT_DESC;
  708. if (hidma_test_capability(&pdev->dev, HIDMA_IDENTITY_CAP))
  709. dmadev->chidx = readl(dmadev->dev_trca + 0x40);
  710. else
  711. dmadev->chidx = readl(dmadev->dev_trca + 0x28);
  712. /* Set DMA mask to 64 bits. */
  713. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  714. if (rc) {
  715. dev_warn(&pdev->dev, "unable to set coherent mask to 64");
  716. goto dmafree;
  717. }
  718. dmadev->lldev = hidma_ll_init(dmadev->ddev.dev,
  719. dmadev->nr_descriptors, dmadev->dev_trca,
  720. dmadev->dev_evca, dmadev->chidx);
  721. if (!dmadev->lldev) {
  722. rc = -EPROBE_DEFER;
  723. goto dmafree;
  724. }
  725. platform_set_drvdata(pdev, dmadev);
  726. if (msi)
  727. rc = hidma_request_msi(dmadev, pdev);
  728. if (!msi || rc) {
  729. hidma_ll_setup_irq(dmadev->lldev, false);
  730. rc = devm_request_irq(&pdev->dev, chirq, hidma_chirq_handler,
  731. 0, "qcom-hidma", dmadev->lldev);
  732. if (rc)
  733. goto uninit;
  734. }
  735. INIT_LIST_HEAD(&dmadev->ddev.channels);
  736. rc = hidma_chan_init(dmadev, 0);
  737. if (rc)
  738. goto uninit;
  739. rc = dma_async_device_register(&dmadev->ddev);
  740. if (rc)
  741. goto uninit;
  742. dmadev->irq = chirq;
  743. tasklet_setup(&dmadev->task, hidma_issue_task);
  744. hidma_debug_init(dmadev);
  745. hidma_sysfs_init(dmadev);
  746. dev_info(&pdev->dev, "HI-DMA engine driver registration complete\n");
  747. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  748. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  749. return 0;
  750. uninit:
  751. if (msi)
  752. hidma_free_msis(dmadev);
  753. hidma_ll_uninit(dmadev->lldev);
  754. dmafree:
  755. if (dmadev)
  756. hidma_free(dmadev);
  757. bailout:
  758. pm_runtime_put_sync(&pdev->dev);
  759. pm_runtime_disable(&pdev->dev);
  760. return rc;
  761. }
  762. static void hidma_shutdown(struct platform_device *pdev)
  763. {
  764. struct hidma_dev *dmadev = platform_get_drvdata(pdev);
  765. dev_info(dmadev->ddev.dev, "HI-DMA engine shutdown\n");
  766. pm_runtime_get_sync(dmadev->ddev.dev);
  767. if (hidma_ll_disable(dmadev->lldev))
  768. dev_warn(dmadev->ddev.dev, "channel did not stop\n");
  769. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  770. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  771. }
  772. static int hidma_remove(struct platform_device *pdev)
  773. {
  774. struct hidma_dev *dmadev = platform_get_drvdata(pdev);
  775. pm_runtime_get_sync(dmadev->ddev.dev);
  776. dma_async_device_unregister(&dmadev->ddev);
  777. if (!dmadev->lldev->msi_support)
  778. devm_free_irq(dmadev->ddev.dev, dmadev->irq, dmadev->lldev);
  779. else
  780. hidma_free_msis(dmadev);
  781. tasklet_kill(&dmadev->task);
  782. hidma_sysfs_uninit(dmadev);
  783. hidma_debug_uninit(dmadev);
  784. hidma_ll_uninit(dmadev->lldev);
  785. hidma_free(dmadev);
  786. dev_info(&pdev->dev, "HI-DMA engine removed\n");
  787. pm_runtime_put_sync_suspend(&pdev->dev);
  788. pm_runtime_disable(&pdev->dev);
  789. return 0;
  790. }
  791. #if IS_ENABLED(CONFIG_ACPI)
  792. static const struct acpi_device_id hidma_acpi_ids[] = {
  793. {"QCOM8061"},
  794. {"QCOM8062", HIDMA_MSI_CAP},
  795. {"QCOM8063", (HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP)},
  796. {},
  797. };
  798. MODULE_DEVICE_TABLE(acpi, hidma_acpi_ids);
  799. #endif
  800. static const struct of_device_id hidma_match[] = {
  801. {.compatible = "qcom,hidma-1.0",},
  802. {.compatible = "qcom,hidma-1.1", .data = (void *)(HIDMA_MSI_CAP),},
  803. {.compatible = "qcom,hidma-1.2",
  804. .data = (void *)(HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP),},
  805. {},
  806. };
  807. MODULE_DEVICE_TABLE(of, hidma_match);
  808. static struct platform_driver hidma_driver = {
  809. .probe = hidma_probe,
  810. .remove = hidma_remove,
  811. .shutdown = hidma_shutdown,
  812. .driver = {
  813. .name = "hidma",
  814. .of_match_table = hidma_match,
  815. .acpi_match_table = ACPI_PTR(hidma_acpi_ids),
  816. },
  817. };
  818. module_platform_driver(hidma_driver);
  819. MODULE_LICENSE("GPL v2");