hidma_ll.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Qualcomm Technologies HIDMA DMA engine low level code
  3. *
  4. * Copyright (c) 2015-2016, 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. #include <linux/dmaengine.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mm.h>
  19. #include <linux/highmem.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/delay.h>
  22. #include <linux/atomic.h>
  23. #include <linux/iopoll.h>
  24. #include <linux/kfifo.h>
  25. #include <linux/bitops.h>
  26. #include "hidma.h"
  27. #define HIDMA_EVRE_SIZE 16 /* each EVRE is 16 bytes */
  28. #define HIDMA_TRCA_CTRLSTS_REG 0x000
  29. #define HIDMA_TRCA_RING_LOW_REG 0x008
  30. #define HIDMA_TRCA_RING_HIGH_REG 0x00C
  31. #define HIDMA_TRCA_RING_LEN_REG 0x010
  32. #define HIDMA_TRCA_DOORBELL_REG 0x400
  33. #define HIDMA_EVCA_CTRLSTS_REG 0x000
  34. #define HIDMA_EVCA_INTCTRL_REG 0x004
  35. #define HIDMA_EVCA_RING_LOW_REG 0x008
  36. #define HIDMA_EVCA_RING_HIGH_REG 0x00C
  37. #define HIDMA_EVCA_RING_LEN_REG 0x010
  38. #define HIDMA_EVCA_WRITE_PTR_REG 0x020
  39. #define HIDMA_EVCA_DOORBELL_REG 0x400
  40. #define HIDMA_EVCA_IRQ_STAT_REG 0x100
  41. #define HIDMA_EVCA_IRQ_CLR_REG 0x108
  42. #define HIDMA_EVCA_IRQ_EN_REG 0x110
  43. #define HIDMA_EVRE_CFG_IDX 0
  44. #define HIDMA_EVRE_ERRINFO_BIT_POS 24
  45. #define HIDMA_EVRE_CODE_BIT_POS 28
  46. #define HIDMA_EVRE_ERRINFO_MASK GENMASK(3, 0)
  47. #define HIDMA_EVRE_CODE_MASK GENMASK(3, 0)
  48. #define HIDMA_CH_CONTROL_MASK GENMASK(7, 0)
  49. #define HIDMA_CH_STATE_MASK GENMASK(7, 0)
  50. #define HIDMA_CH_STATE_BIT_POS 0x8
  51. #define HIDMA_IRQ_EV_CH_EOB_IRQ_BIT_POS 0
  52. #define HIDMA_IRQ_EV_CH_WR_RESP_BIT_POS 1
  53. #define HIDMA_IRQ_TR_CH_TRE_RD_RSP_ER_BIT_POS 9
  54. #define HIDMA_IRQ_TR_CH_DATA_RD_ER_BIT_POS 10
  55. #define HIDMA_IRQ_TR_CH_DATA_WR_ER_BIT_POS 11
  56. #define HIDMA_IRQ_TR_CH_INVALID_TRE_BIT_POS 14
  57. #define ENABLE_IRQS (BIT(HIDMA_IRQ_EV_CH_EOB_IRQ_BIT_POS) | \
  58. BIT(HIDMA_IRQ_EV_CH_WR_RESP_BIT_POS) | \
  59. BIT(HIDMA_IRQ_TR_CH_TRE_RD_RSP_ER_BIT_POS) | \
  60. BIT(HIDMA_IRQ_TR_CH_DATA_RD_ER_BIT_POS) | \
  61. BIT(HIDMA_IRQ_TR_CH_DATA_WR_ER_BIT_POS) | \
  62. BIT(HIDMA_IRQ_TR_CH_INVALID_TRE_BIT_POS))
  63. #define HIDMA_INCREMENT_ITERATOR(iter, size, ring_size) \
  64. do { \
  65. iter += size; \
  66. if (iter >= ring_size) \
  67. iter -= ring_size; \
  68. } while (0)
  69. #define HIDMA_CH_STATE(val) \
  70. ((val >> HIDMA_CH_STATE_BIT_POS) & HIDMA_CH_STATE_MASK)
  71. #define HIDMA_ERR_INT_MASK \
  72. (BIT(HIDMA_IRQ_TR_CH_INVALID_TRE_BIT_POS) | \
  73. BIT(HIDMA_IRQ_TR_CH_TRE_RD_RSP_ER_BIT_POS) | \
  74. BIT(HIDMA_IRQ_EV_CH_WR_RESP_BIT_POS) | \
  75. BIT(HIDMA_IRQ_TR_CH_DATA_RD_ER_BIT_POS) | \
  76. BIT(HIDMA_IRQ_TR_CH_DATA_WR_ER_BIT_POS))
  77. enum ch_command {
  78. HIDMA_CH_DISABLE = 0,
  79. HIDMA_CH_ENABLE = 1,
  80. HIDMA_CH_SUSPEND = 2,
  81. HIDMA_CH_RESET = 9,
  82. };
  83. enum ch_state {
  84. HIDMA_CH_DISABLED = 0,
  85. HIDMA_CH_ENABLED = 1,
  86. HIDMA_CH_RUNNING = 2,
  87. HIDMA_CH_SUSPENDED = 3,
  88. HIDMA_CH_STOPPED = 4,
  89. };
  90. enum tre_type {
  91. HIDMA_TRE_MEMCPY = 3,
  92. };
  93. enum err_code {
  94. HIDMA_EVRE_STATUS_COMPLETE = 1,
  95. HIDMA_EVRE_STATUS_ERROR = 4,
  96. };
  97. static int hidma_is_chan_enabled(int state)
  98. {
  99. switch (state) {
  100. case HIDMA_CH_ENABLED:
  101. case HIDMA_CH_RUNNING:
  102. return true;
  103. default:
  104. return false;
  105. }
  106. }
  107. void hidma_ll_free(struct hidma_lldev *lldev, u32 tre_ch)
  108. {
  109. struct hidma_tre *tre;
  110. if (tre_ch >= lldev->nr_tres) {
  111. dev_err(lldev->dev, "invalid TRE number in free:%d", tre_ch);
  112. return;
  113. }
  114. tre = &lldev->trepool[tre_ch];
  115. if (atomic_read(&tre->allocated) != true) {
  116. dev_err(lldev->dev, "trying to free an unused TRE:%d", tre_ch);
  117. return;
  118. }
  119. atomic_set(&tre->allocated, 0);
  120. }
  121. int hidma_ll_request(struct hidma_lldev *lldev, u32 sig, const char *dev_name,
  122. void (*callback)(void *data), void *data, u32 *tre_ch)
  123. {
  124. unsigned int i;
  125. struct hidma_tre *tre;
  126. u32 *tre_local;
  127. if (!tre_ch || !lldev)
  128. return -EINVAL;
  129. /* need to have at least one empty spot in the queue */
  130. for (i = 0; i < lldev->nr_tres - 1; i++) {
  131. if (atomic_add_unless(&lldev->trepool[i].allocated, 1, 1))
  132. break;
  133. }
  134. if (i == (lldev->nr_tres - 1))
  135. return -ENOMEM;
  136. tre = &lldev->trepool[i];
  137. tre->dma_sig = sig;
  138. tre->dev_name = dev_name;
  139. tre->callback = callback;
  140. tre->data = data;
  141. tre->idx = i;
  142. tre->status = 0;
  143. tre->queued = 0;
  144. tre->err_code = 0;
  145. tre->err_info = 0;
  146. tre->lldev = lldev;
  147. tre_local = &tre->tre_local[0];
  148. tre_local[HIDMA_TRE_CFG_IDX] = HIDMA_TRE_MEMCPY;
  149. tre_local[HIDMA_TRE_CFG_IDX] |= (lldev->chidx & 0xFF) << 8;
  150. tre_local[HIDMA_TRE_CFG_IDX] |= BIT(16); /* set IEOB */
  151. *tre_ch = i;
  152. if (callback)
  153. callback(data);
  154. return 0;
  155. }
  156. /*
  157. * Multiple TREs may be queued and waiting in the pending queue.
  158. */
  159. static void hidma_ll_tre_complete(unsigned long arg)
  160. {
  161. struct hidma_lldev *lldev = (struct hidma_lldev *)arg;
  162. struct hidma_tre *tre;
  163. while (kfifo_out(&lldev->handoff_fifo, &tre, 1)) {
  164. /* call the user if it has been read by the hardware */
  165. if (tre->callback)
  166. tre->callback(tre->data);
  167. }
  168. }
  169. static int hidma_post_completed(struct hidma_lldev *lldev, int tre_iterator,
  170. u8 err_info, u8 err_code)
  171. {
  172. struct hidma_tre *tre;
  173. unsigned long flags;
  174. spin_lock_irqsave(&lldev->lock, flags);
  175. tre = lldev->pending_tre_list[tre_iterator / HIDMA_TRE_SIZE];
  176. if (!tre) {
  177. spin_unlock_irqrestore(&lldev->lock, flags);
  178. dev_warn(lldev->dev, "tre_index [%d] and tre out of sync\n",
  179. tre_iterator / HIDMA_TRE_SIZE);
  180. return -EINVAL;
  181. }
  182. lldev->pending_tre_list[tre->tre_index] = NULL;
  183. /*
  184. * Keep track of pending TREs that SW is expecting to receive
  185. * from HW. We got one now. Decrement our counter.
  186. */
  187. lldev->pending_tre_count--;
  188. if (lldev->pending_tre_count < 0) {
  189. dev_warn(lldev->dev, "tre count mismatch on completion");
  190. lldev->pending_tre_count = 0;
  191. }
  192. spin_unlock_irqrestore(&lldev->lock, flags);
  193. tre->err_info = err_info;
  194. tre->err_code = err_code;
  195. tre->queued = 0;
  196. kfifo_put(&lldev->handoff_fifo, tre);
  197. tasklet_schedule(&lldev->task);
  198. return 0;
  199. }
  200. /*
  201. * Called to handle the interrupt for the channel.
  202. * Return a positive number if TRE or EVRE were consumed on this run.
  203. * Return a positive number if there are pending TREs or EVREs.
  204. * Return 0 if there is nothing to consume or no pending TREs/EVREs found.
  205. */
  206. static int hidma_handle_tre_completion(struct hidma_lldev *lldev)
  207. {
  208. u32 evre_ring_size = lldev->evre_ring_size;
  209. u32 tre_ring_size = lldev->tre_ring_size;
  210. u32 err_info, err_code, evre_write_off;
  211. u32 tre_iterator, evre_iterator;
  212. u32 num_completed = 0;
  213. evre_write_off = readl_relaxed(lldev->evca + HIDMA_EVCA_WRITE_PTR_REG);
  214. tre_iterator = lldev->tre_processed_off;
  215. evre_iterator = lldev->evre_processed_off;
  216. if ((evre_write_off > evre_ring_size) ||
  217. (evre_write_off % HIDMA_EVRE_SIZE)) {
  218. dev_err(lldev->dev, "HW reports invalid EVRE write offset\n");
  219. return 0;
  220. }
  221. /*
  222. * By the time control reaches here the number of EVREs and TREs
  223. * may not match. Only consume the ones that hardware told us.
  224. */
  225. while ((evre_iterator != evre_write_off)) {
  226. u32 *current_evre = lldev->evre_ring + evre_iterator;
  227. u32 cfg;
  228. cfg = current_evre[HIDMA_EVRE_CFG_IDX];
  229. err_info = cfg >> HIDMA_EVRE_ERRINFO_BIT_POS;
  230. err_info &= HIDMA_EVRE_ERRINFO_MASK;
  231. err_code =
  232. (cfg >> HIDMA_EVRE_CODE_BIT_POS) & HIDMA_EVRE_CODE_MASK;
  233. if (hidma_post_completed(lldev, tre_iterator, err_info,
  234. err_code))
  235. break;
  236. HIDMA_INCREMENT_ITERATOR(tre_iterator, HIDMA_TRE_SIZE,
  237. tre_ring_size);
  238. HIDMA_INCREMENT_ITERATOR(evre_iterator, HIDMA_EVRE_SIZE,
  239. evre_ring_size);
  240. /*
  241. * Read the new event descriptor written by the HW.
  242. * As we are processing the delivered events, other events
  243. * get queued to the SW for processing.
  244. */
  245. evre_write_off =
  246. readl_relaxed(lldev->evca + HIDMA_EVCA_WRITE_PTR_REG);
  247. num_completed++;
  248. }
  249. if (num_completed) {
  250. u32 evre_read_off = (lldev->evre_processed_off +
  251. HIDMA_EVRE_SIZE * num_completed);
  252. u32 tre_read_off = (lldev->tre_processed_off +
  253. HIDMA_TRE_SIZE * num_completed);
  254. evre_read_off = evre_read_off % evre_ring_size;
  255. tre_read_off = tre_read_off % tre_ring_size;
  256. writel(evre_read_off, lldev->evca + HIDMA_EVCA_DOORBELL_REG);
  257. /* record the last processed tre offset */
  258. lldev->tre_processed_off = tre_read_off;
  259. lldev->evre_processed_off = evre_read_off;
  260. }
  261. return num_completed;
  262. }
  263. void hidma_cleanup_pending_tre(struct hidma_lldev *lldev, u8 err_info,
  264. u8 err_code)
  265. {
  266. u32 tre_iterator;
  267. u32 tre_ring_size = lldev->tre_ring_size;
  268. int num_completed = 0;
  269. u32 tre_read_off;
  270. tre_iterator = lldev->tre_processed_off;
  271. while (lldev->pending_tre_count) {
  272. if (hidma_post_completed(lldev, tre_iterator, err_info,
  273. err_code))
  274. break;
  275. HIDMA_INCREMENT_ITERATOR(tre_iterator, HIDMA_TRE_SIZE,
  276. tre_ring_size);
  277. num_completed++;
  278. }
  279. tre_read_off = (lldev->tre_processed_off +
  280. HIDMA_TRE_SIZE * num_completed);
  281. tre_read_off = tre_read_off % tre_ring_size;
  282. /* record the last processed tre offset */
  283. lldev->tre_processed_off = tre_read_off;
  284. }
  285. static int hidma_ll_reset(struct hidma_lldev *lldev)
  286. {
  287. u32 val;
  288. int ret;
  289. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  290. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  291. val |= HIDMA_CH_RESET << 16;
  292. writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  293. /*
  294. * Delay 10ms after reset to allow DMA logic to quiesce.
  295. * Do a polled read up to 1ms and 10ms maximum.
  296. */
  297. ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val,
  298. HIDMA_CH_STATE(val) == HIDMA_CH_DISABLED,
  299. 1000, 10000);
  300. if (ret) {
  301. dev_err(lldev->dev, "transfer channel did not reset\n");
  302. return ret;
  303. }
  304. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  305. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  306. val |= HIDMA_CH_RESET << 16;
  307. writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  308. /*
  309. * Delay 10ms after reset to allow DMA logic to quiesce.
  310. * Do a polled read up to 1ms and 10ms maximum.
  311. */
  312. ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val,
  313. HIDMA_CH_STATE(val) == HIDMA_CH_DISABLED,
  314. 1000, 10000);
  315. if (ret)
  316. return ret;
  317. lldev->trch_state = HIDMA_CH_DISABLED;
  318. lldev->evch_state = HIDMA_CH_DISABLED;
  319. return 0;
  320. }
  321. /*
  322. * Abort all transactions and perform a reset.
  323. */
  324. static void hidma_ll_abort(unsigned long arg)
  325. {
  326. struct hidma_lldev *lldev = (struct hidma_lldev *)arg;
  327. u8 err_code = HIDMA_EVRE_STATUS_ERROR;
  328. u8 err_info = 0xFF;
  329. int rc;
  330. hidma_cleanup_pending_tre(lldev, err_info, err_code);
  331. /* reset the channel for recovery */
  332. rc = hidma_ll_setup(lldev);
  333. if (rc) {
  334. dev_err(lldev->dev, "channel reinitialize failed after error\n");
  335. return;
  336. }
  337. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  338. }
  339. /*
  340. * The interrupt handler for HIDMA will try to consume as many pending
  341. * EVRE from the event queue as possible. Each EVRE has an associated
  342. * TRE that holds the user interface parameters. EVRE reports the
  343. * result of the transaction. Hardware guarantees ordering between EVREs
  344. * and TREs. We use last processed offset to figure out which TRE is
  345. * associated with which EVRE. If two TREs are consumed by HW, the EVREs
  346. * are in order in the event ring.
  347. *
  348. * This handler will do a one pass for consuming EVREs. Other EVREs may
  349. * be delivered while we are working. It will try to consume incoming
  350. * EVREs one more time and return.
  351. *
  352. * For unprocessed EVREs, hardware will trigger another interrupt until
  353. * all the interrupt bits are cleared.
  354. *
  355. * Hardware guarantees that by the time interrupt is observed, all data
  356. * transactions in flight are delivered to their respective places and
  357. * are visible to the CPU.
  358. *
  359. * On demand paging for IOMMU is only supported for PCIe via PRI
  360. * (Page Request Interface) not for HIDMA. All other hardware instances
  361. * including HIDMA work on pinned DMA addresses.
  362. *
  363. * HIDMA is not aware of IOMMU presence since it follows the DMA API. All
  364. * IOMMU latency will be built into the data movement time. By the time
  365. * interrupt happens, IOMMU lookups + data movement has already taken place.
  366. *
  367. * While the first read in a typical PCI endpoint ISR flushes all outstanding
  368. * requests traditionally to the destination, this concept does not apply
  369. * here for this HW.
  370. */
  371. irqreturn_t hidma_ll_inthandler(int chirq, void *arg)
  372. {
  373. struct hidma_lldev *lldev = arg;
  374. u32 status;
  375. u32 enable;
  376. u32 cause;
  377. /*
  378. * Fine tuned for this HW...
  379. *
  380. * This ISR has been designed for this particular hardware. Relaxed
  381. * read and write accessors are used for performance reasons due to
  382. * interrupt delivery guarantees. Do not copy this code blindly and
  383. * expect that to work.
  384. */
  385. status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  386. enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  387. cause = status & enable;
  388. while (cause) {
  389. if (cause & HIDMA_ERR_INT_MASK) {
  390. dev_err(lldev->dev, "error 0x%x, resetting...\n",
  391. cause);
  392. /* Clear out pending interrupts */
  393. writel(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  394. tasklet_schedule(&lldev->rst_task);
  395. goto out;
  396. }
  397. /*
  398. * Try to consume as many EVREs as possible.
  399. */
  400. hidma_handle_tre_completion(lldev);
  401. /* We consumed TREs or there are pending TREs or EVREs. */
  402. writel_relaxed(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  403. /*
  404. * Another interrupt might have arrived while we are
  405. * processing this one. Read the new cause.
  406. */
  407. status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  408. enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  409. cause = status & enable;
  410. }
  411. out:
  412. return IRQ_HANDLED;
  413. }
  414. int hidma_ll_enable(struct hidma_lldev *lldev)
  415. {
  416. u32 val;
  417. int ret;
  418. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  419. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  420. val |= HIDMA_CH_ENABLE << 16;
  421. writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  422. ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val,
  423. hidma_is_chan_enabled(HIDMA_CH_STATE(val)),
  424. 1000, 10000);
  425. if (ret) {
  426. dev_err(lldev->dev, "event channel did not get enabled\n");
  427. return ret;
  428. }
  429. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  430. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  431. val |= HIDMA_CH_ENABLE << 16;
  432. writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  433. ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val,
  434. hidma_is_chan_enabled(HIDMA_CH_STATE(val)),
  435. 1000, 10000);
  436. if (ret) {
  437. dev_err(lldev->dev, "transfer channel did not get enabled\n");
  438. return ret;
  439. }
  440. lldev->trch_state = HIDMA_CH_ENABLED;
  441. lldev->evch_state = HIDMA_CH_ENABLED;
  442. return 0;
  443. }
  444. void hidma_ll_start(struct hidma_lldev *lldev)
  445. {
  446. unsigned long irqflags;
  447. spin_lock_irqsave(&lldev->lock, irqflags);
  448. writel(lldev->tre_write_offset, lldev->trca + HIDMA_TRCA_DOORBELL_REG);
  449. spin_unlock_irqrestore(&lldev->lock, irqflags);
  450. }
  451. bool hidma_ll_isenabled(struct hidma_lldev *lldev)
  452. {
  453. u32 val;
  454. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  455. lldev->trch_state = HIDMA_CH_STATE(val);
  456. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  457. lldev->evch_state = HIDMA_CH_STATE(val);
  458. /* both channels have to be enabled before calling this function */
  459. if (hidma_is_chan_enabled(lldev->trch_state) &&
  460. hidma_is_chan_enabled(lldev->evch_state))
  461. return true;
  462. return false;
  463. }
  464. void hidma_ll_queue_request(struct hidma_lldev *lldev, u32 tre_ch)
  465. {
  466. struct hidma_tre *tre;
  467. unsigned long flags;
  468. tre = &lldev->trepool[tre_ch];
  469. /* copy the TRE into its location in the TRE ring */
  470. spin_lock_irqsave(&lldev->lock, flags);
  471. tre->tre_index = lldev->tre_write_offset / HIDMA_TRE_SIZE;
  472. lldev->pending_tre_list[tre->tre_index] = tre;
  473. memcpy(lldev->tre_ring + lldev->tre_write_offset,
  474. &tre->tre_local[0], HIDMA_TRE_SIZE);
  475. tre->err_code = 0;
  476. tre->err_info = 0;
  477. tre->queued = 1;
  478. lldev->pending_tre_count++;
  479. lldev->tre_write_offset = (lldev->tre_write_offset + HIDMA_TRE_SIZE)
  480. % lldev->tre_ring_size;
  481. spin_unlock_irqrestore(&lldev->lock, flags);
  482. }
  483. /*
  484. * Note that even though we stop this channel if there is a pending transaction
  485. * in flight it will complete and follow the callback. This request will
  486. * prevent further requests to be made.
  487. */
  488. int hidma_ll_disable(struct hidma_lldev *lldev)
  489. {
  490. u32 val;
  491. int ret;
  492. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  493. lldev->evch_state = HIDMA_CH_STATE(val);
  494. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  495. lldev->trch_state = HIDMA_CH_STATE(val);
  496. /* already suspended by this OS */
  497. if ((lldev->trch_state == HIDMA_CH_SUSPENDED) ||
  498. (lldev->evch_state == HIDMA_CH_SUSPENDED))
  499. return 0;
  500. /* already stopped by the manager */
  501. if ((lldev->trch_state == HIDMA_CH_STOPPED) ||
  502. (lldev->evch_state == HIDMA_CH_STOPPED))
  503. return 0;
  504. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  505. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  506. val |= HIDMA_CH_SUSPEND << 16;
  507. writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  508. /*
  509. * Start the wait right after the suspend is confirmed.
  510. * Do a polled read up to 1ms and 10ms maximum.
  511. */
  512. ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val,
  513. HIDMA_CH_STATE(val) == HIDMA_CH_SUSPENDED,
  514. 1000, 10000);
  515. if (ret)
  516. return ret;
  517. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  518. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  519. val |= HIDMA_CH_SUSPEND << 16;
  520. writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  521. /*
  522. * Start the wait right after the suspend is confirmed
  523. * Delay up to 10ms after reset to allow DMA logic to quiesce.
  524. */
  525. ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val,
  526. HIDMA_CH_STATE(val) == HIDMA_CH_SUSPENDED,
  527. 1000, 10000);
  528. if (ret)
  529. return ret;
  530. lldev->trch_state = HIDMA_CH_SUSPENDED;
  531. lldev->evch_state = HIDMA_CH_SUSPENDED;
  532. return 0;
  533. }
  534. void hidma_ll_set_transfer_params(struct hidma_lldev *lldev, u32 tre_ch,
  535. dma_addr_t src, dma_addr_t dest, u32 len,
  536. u32 flags)
  537. {
  538. struct hidma_tre *tre;
  539. u32 *tre_local;
  540. if (tre_ch >= lldev->nr_tres) {
  541. dev_err(lldev->dev, "invalid TRE number in transfer params:%d",
  542. tre_ch);
  543. return;
  544. }
  545. tre = &lldev->trepool[tre_ch];
  546. if (atomic_read(&tre->allocated) != true) {
  547. dev_err(lldev->dev, "trying to set params on an unused TRE:%d",
  548. tre_ch);
  549. return;
  550. }
  551. tre_local = &tre->tre_local[0];
  552. tre_local[HIDMA_TRE_LEN_IDX] = len;
  553. tre_local[HIDMA_TRE_SRC_LOW_IDX] = lower_32_bits(src);
  554. tre_local[HIDMA_TRE_SRC_HI_IDX] = upper_32_bits(src);
  555. tre_local[HIDMA_TRE_DEST_LOW_IDX] = lower_32_bits(dest);
  556. tre_local[HIDMA_TRE_DEST_HI_IDX] = upper_32_bits(dest);
  557. tre->int_flags = flags;
  558. }
  559. /*
  560. * Called during initialization and after an error condition
  561. * to restore hardware state.
  562. */
  563. int hidma_ll_setup(struct hidma_lldev *lldev)
  564. {
  565. int rc;
  566. u64 addr;
  567. u32 val;
  568. u32 nr_tres = lldev->nr_tres;
  569. lldev->pending_tre_count = 0;
  570. lldev->tre_processed_off = 0;
  571. lldev->evre_processed_off = 0;
  572. lldev->tre_write_offset = 0;
  573. /* disable interrupts */
  574. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  575. /* clear all pending interrupts */
  576. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  577. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  578. rc = hidma_ll_reset(lldev);
  579. if (rc)
  580. return rc;
  581. /*
  582. * Clear all pending interrupts again.
  583. * Otherwise, we observe reset complete interrupts.
  584. */
  585. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  586. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  587. /* disable interrupts again after reset */
  588. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  589. addr = lldev->tre_dma;
  590. writel(lower_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_LOW_REG);
  591. writel(upper_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_HIGH_REG);
  592. writel(lldev->tre_ring_size, lldev->trca + HIDMA_TRCA_RING_LEN_REG);
  593. addr = lldev->evre_dma;
  594. writel(lower_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_LOW_REG);
  595. writel(upper_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_HIGH_REG);
  596. writel(HIDMA_EVRE_SIZE * nr_tres,
  597. lldev->evca + HIDMA_EVCA_RING_LEN_REG);
  598. /* support IRQ only for now */
  599. val = readl(lldev->evca + HIDMA_EVCA_INTCTRL_REG);
  600. val &= ~0xF;
  601. val |= 0x1;
  602. writel(val, lldev->evca + HIDMA_EVCA_INTCTRL_REG);
  603. /* clear all pending interrupts and enable them */
  604. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  605. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  606. return hidma_ll_enable(lldev);
  607. }
  608. struct hidma_lldev *hidma_ll_init(struct device *dev, u32 nr_tres,
  609. void __iomem *trca, void __iomem *evca,
  610. u8 chidx)
  611. {
  612. u32 required_bytes;
  613. struct hidma_lldev *lldev;
  614. int rc;
  615. size_t sz;
  616. if (!trca || !evca || !dev || !nr_tres)
  617. return NULL;
  618. /* need at least four TREs */
  619. if (nr_tres < 4)
  620. return NULL;
  621. /* need an extra space */
  622. nr_tres += 1;
  623. lldev = devm_kzalloc(dev, sizeof(struct hidma_lldev), GFP_KERNEL);
  624. if (!lldev)
  625. return NULL;
  626. lldev->evca = evca;
  627. lldev->trca = trca;
  628. lldev->dev = dev;
  629. sz = sizeof(struct hidma_tre);
  630. lldev->trepool = devm_kcalloc(lldev->dev, nr_tres, sz, GFP_KERNEL);
  631. if (!lldev->trepool)
  632. return NULL;
  633. required_bytes = sizeof(lldev->pending_tre_list[0]);
  634. lldev->pending_tre_list = devm_kcalloc(dev, nr_tres, required_bytes,
  635. GFP_KERNEL);
  636. if (!lldev->pending_tre_list)
  637. return NULL;
  638. sz = (HIDMA_TRE_SIZE + 1) * nr_tres;
  639. lldev->tre_ring = dmam_alloc_coherent(dev, sz, &lldev->tre_dma,
  640. GFP_KERNEL);
  641. if (!lldev->tre_ring)
  642. return NULL;
  643. memset(lldev->tre_ring, 0, (HIDMA_TRE_SIZE + 1) * nr_tres);
  644. lldev->tre_ring_size = HIDMA_TRE_SIZE * nr_tres;
  645. lldev->nr_tres = nr_tres;
  646. /* the TRE ring has to be TRE_SIZE aligned */
  647. if (!IS_ALIGNED(lldev->tre_dma, HIDMA_TRE_SIZE)) {
  648. u8 tre_ring_shift;
  649. tre_ring_shift = lldev->tre_dma % HIDMA_TRE_SIZE;
  650. tre_ring_shift = HIDMA_TRE_SIZE - tre_ring_shift;
  651. lldev->tre_dma += tre_ring_shift;
  652. lldev->tre_ring += tre_ring_shift;
  653. }
  654. sz = (HIDMA_EVRE_SIZE + 1) * nr_tres;
  655. lldev->evre_ring = dmam_alloc_coherent(dev, sz, &lldev->evre_dma,
  656. GFP_KERNEL);
  657. if (!lldev->evre_ring)
  658. return NULL;
  659. memset(lldev->evre_ring, 0, (HIDMA_EVRE_SIZE + 1) * nr_tres);
  660. lldev->evre_ring_size = HIDMA_EVRE_SIZE * nr_tres;
  661. /* the EVRE ring has to be EVRE_SIZE aligned */
  662. if (!IS_ALIGNED(lldev->evre_dma, HIDMA_EVRE_SIZE)) {
  663. u8 evre_ring_shift;
  664. evre_ring_shift = lldev->evre_dma % HIDMA_EVRE_SIZE;
  665. evre_ring_shift = HIDMA_EVRE_SIZE - evre_ring_shift;
  666. lldev->evre_dma += evre_ring_shift;
  667. lldev->evre_ring += evre_ring_shift;
  668. }
  669. lldev->nr_tres = nr_tres;
  670. lldev->chidx = chidx;
  671. sz = nr_tres * sizeof(struct hidma_tre *);
  672. rc = kfifo_alloc(&lldev->handoff_fifo, sz, GFP_KERNEL);
  673. if (rc)
  674. return NULL;
  675. rc = hidma_ll_setup(lldev);
  676. if (rc)
  677. return NULL;
  678. spin_lock_init(&lldev->lock);
  679. tasklet_init(&lldev->rst_task, hidma_ll_abort, (unsigned long)lldev);
  680. tasklet_init(&lldev->task, hidma_ll_tre_complete, (unsigned long)lldev);
  681. lldev->initialized = 1;
  682. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  683. return lldev;
  684. }
  685. int hidma_ll_uninit(struct hidma_lldev *lldev)
  686. {
  687. u32 required_bytes;
  688. int rc = 0;
  689. u32 val;
  690. if (!lldev)
  691. return -ENODEV;
  692. if (!lldev->initialized)
  693. return 0;
  694. lldev->initialized = 0;
  695. required_bytes = sizeof(struct hidma_tre) * lldev->nr_tres;
  696. tasklet_kill(&lldev->task);
  697. tasklet_kill(&lldev->rst_task);
  698. memset(lldev->trepool, 0, required_bytes);
  699. lldev->trepool = NULL;
  700. lldev->pending_tre_count = 0;
  701. lldev->tre_write_offset = 0;
  702. rc = hidma_ll_reset(lldev);
  703. /*
  704. * Clear all pending interrupts again.
  705. * Otherwise, we observe reset complete interrupts.
  706. */
  707. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  708. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  709. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  710. return rc;
  711. }
  712. enum dma_status hidma_ll_status(struct hidma_lldev *lldev, u32 tre_ch)
  713. {
  714. enum dma_status ret = DMA_ERROR;
  715. struct hidma_tre *tre;
  716. unsigned long flags;
  717. u8 err_code;
  718. spin_lock_irqsave(&lldev->lock, flags);
  719. tre = &lldev->trepool[tre_ch];
  720. err_code = tre->err_code;
  721. if (err_code & HIDMA_EVRE_STATUS_COMPLETE)
  722. ret = DMA_COMPLETE;
  723. else if (err_code & HIDMA_EVRE_STATUS_ERROR)
  724. ret = DMA_ERROR;
  725. else
  726. ret = DMA_IN_PROGRESS;
  727. spin_unlock_irqrestore(&lldev->lock, flags);
  728. return ret;
  729. }