hidma_ll.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. * The interrupt handler for HIDMA will try to consume as many pending
  323. * EVRE from the event queue as possible. Each EVRE has an associated
  324. * TRE that holds the user interface parameters. EVRE reports the
  325. * result of the transaction. Hardware guarantees ordering between EVREs
  326. * and TREs. We use last processed offset to figure out which TRE is
  327. * associated with which EVRE. If two TREs are consumed by HW, the EVREs
  328. * are in order in the event ring.
  329. *
  330. * This handler will do a one pass for consuming EVREs. Other EVREs may
  331. * be delivered while we are working. It will try to consume incoming
  332. * EVREs one more time and return.
  333. *
  334. * For unprocessed EVREs, hardware will trigger another interrupt until
  335. * all the interrupt bits are cleared.
  336. *
  337. * Hardware guarantees that by the time interrupt is observed, all data
  338. * transactions in flight are delivered to their respective places and
  339. * are visible to the CPU.
  340. *
  341. * On demand paging for IOMMU is only supported for PCIe via PRI
  342. * (Page Request Interface) not for HIDMA. All other hardware instances
  343. * including HIDMA work on pinned DMA addresses.
  344. *
  345. * HIDMA is not aware of IOMMU presence since it follows the DMA API. All
  346. * IOMMU latency will be built into the data movement time. By the time
  347. * interrupt happens, IOMMU lookups + data movement has already taken place.
  348. *
  349. * While the first read in a typical PCI endpoint ISR flushes all outstanding
  350. * requests traditionally to the destination, this concept does not apply
  351. * here for this HW.
  352. */
  353. irqreturn_t hidma_ll_inthandler(int chirq, void *arg)
  354. {
  355. struct hidma_lldev *lldev = arg;
  356. u32 status;
  357. u32 enable;
  358. u32 cause;
  359. /*
  360. * Fine tuned for this HW...
  361. *
  362. * This ISR has been designed for this particular hardware. Relaxed
  363. * read and write accessors are used for performance reasons due to
  364. * interrupt delivery guarantees. Do not copy this code blindly and
  365. * expect that to work.
  366. */
  367. status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  368. enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  369. cause = status & enable;
  370. while (cause) {
  371. if (cause & HIDMA_ERR_INT_MASK) {
  372. dev_err(lldev->dev, "error 0x%x, disabling...\n",
  373. cause);
  374. /* Clear out pending interrupts */
  375. writel(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  376. /* No further submissions. */
  377. hidma_ll_disable(lldev);
  378. /* Driver completes the txn and intimates the client.*/
  379. hidma_cleanup_pending_tre(lldev, 0xFF,
  380. HIDMA_EVRE_STATUS_ERROR);
  381. goto out;
  382. }
  383. /*
  384. * Try to consume as many EVREs as possible.
  385. */
  386. hidma_handle_tre_completion(lldev);
  387. /* We consumed TREs or there are pending TREs or EVREs. */
  388. writel_relaxed(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  389. /*
  390. * Another interrupt might have arrived while we are
  391. * processing this one. Read the new cause.
  392. */
  393. status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  394. enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  395. cause = status & enable;
  396. }
  397. out:
  398. return IRQ_HANDLED;
  399. }
  400. int hidma_ll_enable(struct hidma_lldev *lldev)
  401. {
  402. u32 val;
  403. int ret;
  404. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  405. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  406. val |= HIDMA_CH_ENABLE << 16;
  407. writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  408. ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val,
  409. hidma_is_chan_enabled(HIDMA_CH_STATE(val)),
  410. 1000, 10000);
  411. if (ret) {
  412. dev_err(lldev->dev, "event channel did not get enabled\n");
  413. return ret;
  414. }
  415. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  416. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  417. val |= HIDMA_CH_ENABLE << 16;
  418. writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  419. ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val,
  420. hidma_is_chan_enabled(HIDMA_CH_STATE(val)),
  421. 1000, 10000);
  422. if (ret) {
  423. dev_err(lldev->dev, "transfer channel did not get enabled\n");
  424. return ret;
  425. }
  426. lldev->trch_state = HIDMA_CH_ENABLED;
  427. lldev->evch_state = HIDMA_CH_ENABLED;
  428. return 0;
  429. }
  430. void hidma_ll_start(struct hidma_lldev *lldev)
  431. {
  432. unsigned long irqflags;
  433. spin_lock_irqsave(&lldev->lock, irqflags);
  434. writel(lldev->tre_write_offset, lldev->trca + HIDMA_TRCA_DOORBELL_REG);
  435. spin_unlock_irqrestore(&lldev->lock, irqflags);
  436. }
  437. bool hidma_ll_isenabled(struct hidma_lldev *lldev)
  438. {
  439. u32 val;
  440. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  441. lldev->trch_state = HIDMA_CH_STATE(val);
  442. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  443. lldev->evch_state = HIDMA_CH_STATE(val);
  444. /* both channels have to be enabled before calling this function */
  445. if (hidma_is_chan_enabled(lldev->trch_state) &&
  446. hidma_is_chan_enabled(lldev->evch_state))
  447. return true;
  448. return false;
  449. }
  450. void hidma_ll_queue_request(struct hidma_lldev *lldev, u32 tre_ch)
  451. {
  452. struct hidma_tre *tre;
  453. unsigned long flags;
  454. tre = &lldev->trepool[tre_ch];
  455. /* copy the TRE into its location in the TRE ring */
  456. spin_lock_irqsave(&lldev->lock, flags);
  457. tre->tre_index = lldev->tre_write_offset / HIDMA_TRE_SIZE;
  458. lldev->pending_tre_list[tre->tre_index] = tre;
  459. memcpy(lldev->tre_ring + lldev->tre_write_offset,
  460. &tre->tre_local[0], HIDMA_TRE_SIZE);
  461. tre->err_code = 0;
  462. tre->err_info = 0;
  463. tre->queued = 1;
  464. lldev->pending_tre_count++;
  465. lldev->tre_write_offset = (lldev->tre_write_offset + HIDMA_TRE_SIZE)
  466. % lldev->tre_ring_size;
  467. spin_unlock_irqrestore(&lldev->lock, flags);
  468. }
  469. /*
  470. * Note that even though we stop this channel if there is a pending transaction
  471. * in flight it will complete and follow the callback. This request will
  472. * prevent further requests to be made.
  473. */
  474. int hidma_ll_disable(struct hidma_lldev *lldev)
  475. {
  476. u32 val;
  477. int ret;
  478. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  479. lldev->evch_state = HIDMA_CH_STATE(val);
  480. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  481. lldev->trch_state = HIDMA_CH_STATE(val);
  482. /* already suspended by this OS */
  483. if ((lldev->trch_state == HIDMA_CH_SUSPENDED) ||
  484. (lldev->evch_state == HIDMA_CH_SUSPENDED))
  485. return 0;
  486. /* already stopped by the manager */
  487. if ((lldev->trch_state == HIDMA_CH_STOPPED) ||
  488. (lldev->evch_state == HIDMA_CH_STOPPED))
  489. return 0;
  490. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  491. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  492. val |= HIDMA_CH_SUSPEND << 16;
  493. writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  494. /*
  495. * Start the wait right after the suspend is confirmed.
  496. * Do a polled read up to 1ms and 10ms maximum.
  497. */
  498. ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val,
  499. HIDMA_CH_STATE(val) == HIDMA_CH_SUSPENDED,
  500. 1000, 10000);
  501. if (ret)
  502. return ret;
  503. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  504. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  505. val |= HIDMA_CH_SUSPEND << 16;
  506. writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  507. /*
  508. * Start the wait right after the suspend is confirmed
  509. * Delay up to 10ms after reset to allow DMA logic to quiesce.
  510. */
  511. ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val,
  512. HIDMA_CH_STATE(val) == HIDMA_CH_SUSPENDED,
  513. 1000, 10000);
  514. if (ret)
  515. return ret;
  516. lldev->trch_state = HIDMA_CH_SUSPENDED;
  517. lldev->evch_state = HIDMA_CH_SUSPENDED;
  518. return 0;
  519. }
  520. void hidma_ll_set_transfer_params(struct hidma_lldev *lldev, u32 tre_ch,
  521. dma_addr_t src, dma_addr_t dest, u32 len,
  522. u32 flags)
  523. {
  524. struct hidma_tre *tre;
  525. u32 *tre_local;
  526. if (tre_ch >= lldev->nr_tres) {
  527. dev_err(lldev->dev, "invalid TRE number in transfer params:%d",
  528. tre_ch);
  529. return;
  530. }
  531. tre = &lldev->trepool[tre_ch];
  532. if (atomic_read(&tre->allocated) != true) {
  533. dev_err(lldev->dev, "trying to set params on an unused TRE:%d",
  534. tre_ch);
  535. return;
  536. }
  537. tre_local = &tre->tre_local[0];
  538. tre_local[HIDMA_TRE_LEN_IDX] = len;
  539. tre_local[HIDMA_TRE_SRC_LOW_IDX] = lower_32_bits(src);
  540. tre_local[HIDMA_TRE_SRC_HI_IDX] = upper_32_bits(src);
  541. tre_local[HIDMA_TRE_DEST_LOW_IDX] = lower_32_bits(dest);
  542. tre_local[HIDMA_TRE_DEST_HI_IDX] = upper_32_bits(dest);
  543. tre->int_flags = flags;
  544. }
  545. /*
  546. * Called during initialization and after an error condition
  547. * to restore hardware state.
  548. */
  549. int hidma_ll_setup(struct hidma_lldev *lldev)
  550. {
  551. int rc;
  552. u64 addr;
  553. u32 val;
  554. u32 nr_tres = lldev->nr_tres;
  555. lldev->pending_tre_count = 0;
  556. lldev->tre_processed_off = 0;
  557. lldev->evre_processed_off = 0;
  558. lldev->tre_write_offset = 0;
  559. /* disable interrupts */
  560. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  561. /* clear all pending interrupts */
  562. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  563. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  564. rc = hidma_ll_reset(lldev);
  565. if (rc)
  566. return rc;
  567. /*
  568. * Clear all pending interrupts again.
  569. * Otherwise, we observe reset complete interrupts.
  570. */
  571. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  572. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  573. /* disable interrupts again after reset */
  574. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  575. addr = lldev->tre_dma;
  576. writel(lower_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_LOW_REG);
  577. writel(upper_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_HIGH_REG);
  578. writel(lldev->tre_ring_size, lldev->trca + HIDMA_TRCA_RING_LEN_REG);
  579. addr = lldev->evre_dma;
  580. writel(lower_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_LOW_REG);
  581. writel(upper_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_HIGH_REG);
  582. writel(HIDMA_EVRE_SIZE * nr_tres,
  583. lldev->evca + HIDMA_EVCA_RING_LEN_REG);
  584. /* support IRQ only for now */
  585. val = readl(lldev->evca + HIDMA_EVCA_INTCTRL_REG);
  586. val &= ~0xF;
  587. val |= 0x1;
  588. writel(val, lldev->evca + HIDMA_EVCA_INTCTRL_REG);
  589. /* clear all pending interrupts and enable them */
  590. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  591. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  592. return hidma_ll_enable(lldev);
  593. }
  594. struct hidma_lldev *hidma_ll_init(struct device *dev, u32 nr_tres,
  595. void __iomem *trca, void __iomem *evca,
  596. u8 chidx)
  597. {
  598. u32 required_bytes;
  599. struct hidma_lldev *lldev;
  600. int rc;
  601. size_t sz;
  602. if (!trca || !evca || !dev || !nr_tres)
  603. return NULL;
  604. /* need at least four TREs */
  605. if (nr_tres < 4)
  606. return NULL;
  607. /* need an extra space */
  608. nr_tres += 1;
  609. lldev = devm_kzalloc(dev, sizeof(struct hidma_lldev), GFP_KERNEL);
  610. if (!lldev)
  611. return NULL;
  612. lldev->evca = evca;
  613. lldev->trca = trca;
  614. lldev->dev = dev;
  615. sz = sizeof(struct hidma_tre);
  616. lldev->trepool = devm_kcalloc(lldev->dev, nr_tres, sz, GFP_KERNEL);
  617. if (!lldev->trepool)
  618. return NULL;
  619. required_bytes = sizeof(lldev->pending_tre_list[0]);
  620. lldev->pending_tre_list = devm_kcalloc(dev, nr_tres, required_bytes,
  621. GFP_KERNEL);
  622. if (!lldev->pending_tre_list)
  623. return NULL;
  624. sz = (HIDMA_TRE_SIZE + 1) * nr_tres;
  625. lldev->tre_ring = dmam_alloc_coherent(dev, sz, &lldev->tre_dma,
  626. GFP_KERNEL);
  627. if (!lldev->tre_ring)
  628. return NULL;
  629. memset(lldev->tre_ring, 0, (HIDMA_TRE_SIZE + 1) * nr_tres);
  630. lldev->tre_ring_size = HIDMA_TRE_SIZE * nr_tres;
  631. lldev->nr_tres = nr_tres;
  632. /* the TRE ring has to be TRE_SIZE aligned */
  633. if (!IS_ALIGNED(lldev->tre_dma, HIDMA_TRE_SIZE)) {
  634. u8 tre_ring_shift;
  635. tre_ring_shift = lldev->tre_dma % HIDMA_TRE_SIZE;
  636. tre_ring_shift = HIDMA_TRE_SIZE - tre_ring_shift;
  637. lldev->tre_dma += tre_ring_shift;
  638. lldev->tre_ring += tre_ring_shift;
  639. }
  640. sz = (HIDMA_EVRE_SIZE + 1) * nr_tres;
  641. lldev->evre_ring = dmam_alloc_coherent(dev, sz, &lldev->evre_dma,
  642. GFP_KERNEL);
  643. if (!lldev->evre_ring)
  644. return NULL;
  645. memset(lldev->evre_ring, 0, (HIDMA_EVRE_SIZE + 1) * nr_tres);
  646. lldev->evre_ring_size = HIDMA_EVRE_SIZE * nr_tres;
  647. /* the EVRE ring has to be EVRE_SIZE aligned */
  648. if (!IS_ALIGNED(lldev->evre_dma, HIDMA_EVRE_SIZE)) {
  649. u8 evre_ring_shift;
  650. evre_ring_shift = lldev->evre_dma % HIDMA_EVRE_SIZE;
  651. evre_ring_shift = HIDMA_EVRE_SIZE - evre_ring_shift;
  652. lldev->evre_dma += evre_ring_shift;
  653. lldev->evre_ring += evre_ring_shift;
  654. }
  655. lldev->nr_tres = nr_tres;
  656. lldev->chidx = chidx;
  657. sz = nr_tres * sizeof(struct hidma_tre *);
  658. rc = kfifo_alloc(&lldev->handoff_fifo, sz, GFP_KERNEL);
  659. if (rc)
  660. return NULL;
  661. rc = hidma_ll_setup(lldev);
  662. if (rc)
  663. return NULL;
  664. spin_lock_init(&lldev->lock);
  665. tasklet_init(&lldev->task, hidma_ll_tre_complete, (unsigned long)lldev);
  666. lldev->initialized = 1;
  667. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  668. return lldev;
  669. }
  670. int hidma_ll_uninit(struct hidma_lldev *lldev)
  671. {
  672. u32 required_bytes;
  673. int rc = 0;
  674. u32 val;
  675. if (!lldev)
  676. return -ENODEV;
  677. if (!lldev->initialized)
  678. return 0;
  679. lldev->initialized = 0;
  680. required_bytes = sizeof(struct hidma_tre) * lldev->nr_tres;
  681. tasklet_kill(&lldev->task);
  682. memset(lldev->trepool, 0, required_bytes);
  683. lldev->trepool = NULL;
  684. lldev->pending_tre_count = 0;
  685. lldev->tre_write_offset = 0;
  686. rc = hidma_ll_reset(lldev);
  687. /*
  688. * Clear all pending interrupts again.
  689. * Otherwise, we observe reset complete interrupts.
  690. */
  691. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  692. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  693. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  694. return rc;
  695. }
  696. enum dma_status hidma_ll_status(struct hidma_lldev *lldev, u32 tre_ch)
  697. {
  698. enum dma_status ret = DMA_ERROR;
  699. struct hidma_tre *tre;
  700. unsigned long flags;
  701. u8 err_code;
  702. spin_lock_irqsave(&lldev->lock, flags);
  703. tre = &lldev->trepool[tre_ch];
  704. err_code = tre->err_code;
  705. if (err_code & HIDMA_EVRE_STATUS_COMPLETE)
  706. ret = DMA_COMPLETE;
  707. else if (err_code & HIDMA_EVRE_STATUS_ERROR)
  708. ret = DMA_ERROR;
  709. else
  710. ret = DMA_IN_PROGRESS;
  711. spin_unlock_irqrestore(&lldev->lock, flags);
  712. return ret;
  713. }