hidma_ll.c 23 KB

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