hidma_ll.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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, u8 err_info,
  170. u8 err_code)
  171. {
  172. struct hidma_tre *tre;
  173. unsigned long flags;
  174. u32 tre_iterator;
  175. spin_lock_irqsave(&lldev->lock, flags);
  176. tre_iterator = lldev->tre_processed_off;
  177. tre = lldev->pending_tre_list[tre_iterator / HIDMA_TRE_SIZE];
  178. if (!tre) {
  179. spin_unlock_irqrestore(&lldev->lock, flags);
  180. dev_warn(lldev->dev, "tre_index [%d] and tre out of sync\n",
  181. tre_iterator / HIDMA_TRE_SIZE);
  182. return -EINVAL;
  183. }
  184. lldev->pending_tre_list[tre->tre_index] = NULL;
  185. /*
  186. * Keep track of pending TREs that SW is expecting to receive
  187. * from HW. We got one now. Decrement our counter.
  188. */
  189. if (atomic_dec_return(&lldev->pending_tre_count) < 0) {
  190. dev_warn(lldev->dev, "tre count mismatch on completion");
  191. atomic_set(&lldev->pending_tre_count, 0);
  192. }
  193. HIDMA_INCREMENT_ITERATOR(tre_iterator, HIDMA_TRE_SIZE,
  194. lldev->tre_ring_size);
  195. lldev->tre_processed_off = tre_iterator;
  196. spin_unlock_irqrestore(&lldev->lock, flags);
  197. tre->err_info = err_info;
  198. tre->err_code = err_code;
  199. tre->queued = 0;
  200. kfifo_put(&lldev->handoff_fifo, tre);
  201. tasklet_schedule(&lldev->task);
  202. return 0;
  203. }
  204. /*
  205. * Called to handle the interrupt for the channel.
  206. * Return a positive number if TRE or EVRE were consumed on this run.
  207. * Return a positive number if there are pending TREs or EVREs.
  208. * Return 0 if there is nothing to consume or no pending TREs/EVREs found.
  209. */
  210. static int hidma_handle_tre_completion(struct hidma_lldev *lldev)
  211. {
  212. u32 evre_ring_size = lldev->evre_ring_size;
  213. u32 err_info, err_code, evre_write_off;
  214. u32 evre_iterator;
  215. u32 num_completed = 0;
  216. evre_write_off = readl_relaxed(lldev->evca + HIDMA_EVCA_WRITE_PTR_REG);
  217. evre_iterator = lldev->evre_processed_off;
  218. if ((evre_write_off > evre_ring_size) ||
  219. (evre_write_off % HIDMA_EVRE_SIZE)) {
  220. dev_err(lldev->dev, "HW reports invalid EVRE write offset\n");
  221. return 0;
  222. }
  223. /*
  224. * By the time control reaches here the number of EVREs and TREs
  225. * may not match. Only consume the ones that hardware told us.
  226. */
  227. while ((evre_iterator != evre_write_off)) {
  228. u32 *current_evre = lldev->evre_ring + evre_iterator;
  229. u32 cfg;
  230. cfg = current_evre[HIDMA_EVRE_CFG_IDX];
  231. err_info = cfg >> HIDMA_EVRE_ERRINFO_BIT_POS;
  232. err_info &= HIDMA_EVRE_ERRINFO_MASK;
  233. err_code =
  234. (cfg >> HIDMA_EVRE_CODE_BIT_POS) & HIDMA_EVRE_CODE_MASK;
  235. if (hidma_post_completed(lldev, err_info, err_code))
  236. break;
  237. HIDMA_INCREMENT_ITERATOR(evre_iterator, HIDMA_EVRE_SIZE,
  238. evre_ring_size);
  239. /*
  240. * Read the new event descriptor written by the HW.
  241. * As we are processing the delivered events, other events
  242. * get queued to the SW for processing.
  243. */
  244. evre_write_off =
  245. readl_relaxed(lldev->evca + HIDMA_EVCA_WRITE_PTR_REG);
  246. num_completed++;
  247. /*
  248. * An error interrupt might have arrived while we are processing
  249. * the completed interrupt.
  250. */
  251. if (!hidma_ll_isenabled(lldev))
  252. break;
  253. }
  254. if (num_completed) {
  255. u32 evre_read_off = (lldev->evre_processed_off +
  256. HIDMA_EVRE_SIZE * num_completed);
  257. evre_read_off = evre_read_off % evre_ring_size;
  258. writel(evre_read_off, lldev->evca + HIDMA_EVCA_DOORBELL_REG);
  259. /* record the last processed tre offset */
  260. lldev->evre_processed_off = evre_read_off;
  261. }
  262. return num_completed;
  263. }
  264. void hidma_cleanup_pending_tre(struct hidma_lldev *lldev, u8 err_info,
  265. u8 err_code)
  266. {
  267. while (atomic_read(&lldev->pending_tre_count)) {
  268. if (hidma_post_completed(lldev, err_info, err_code))
  269. break;
  270. }
  271. }
  272. static int hidma_ll_reset(struct hidma_lldev *lldev)
  273. {
  274. u32 val;
  275. int ret;
  276. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  277. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  278. val |= HIDMA_CH_RESET << 16;
  279. writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  280. /*
  281. * Delay 10ms after reset to allow DMA logic to quiesce.
  282. * Do a polled read up to 1ms and 10ms maximum.
  283. */
  284. ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val,
  285. HIDMA_CH_STATE(val) == HIDMA_CH_DISABLED,
  286. 1000, 10000);
  287. if (ret) {
  288. dev_err(lldev->dev, "transfer channel did not reset\n");
  289. return ret;
  290. }
  291. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  292. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  293. val |= HIDMA_CH_RESET << 16;
  294. writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  295. /*
  296. * Delay 10ms after reset to allow DMA logic to quiesce.
  297. * Do a polled read up to 1ms and 10ms maximum.
  298. */
  299. ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val,
  300. HIDMA_CH_STATE(val) == HIDMA_CH_DISABLED,
  301. 1000, 10000);
  302. if (ret)
  303. return ret;
  304. lldev->trch_state = HIDMA_CH_DISABLED;
  305. lldev->evch_state = HIDMA_CH_DISABLED;
  306. return 0;
  307. }
  308. /*
  309. * The interrupt handler for HIDMA will try to consume as many pending
  310. * EVRE from the event queue as possible. Each EVRE has an associated
  311. * TRE that holds the user interface parameters. EVRE reports the
  312. * result of the transaction. Hardware guarantees ordering between EVREs
  313. * and TREs. We use last processed offset to figure out which TRE is
  314. * associated with which EVRE. If two TREs are consumed by HW, the EVREs
  315. * are in order in the event ring.
  316. *
  317. * This handler will do a one pass for consuming EVREs. Other EVREs may
  318. * be delivered while we are working. It will try to consume incoming
  319. * EVREs one more time and return.
  320. *
  321. * For unprocessed EVREs, hardware will trigger another interrupt until
  322. * all the interrupt bits are cleared.
  323. *
  324. * Hardware guarantees that by the time interrupt is observed, all data
  325. * transactions in flight are delivered to their respective places and
  326. * are visible to the CPU.
  327. *
  328. * On demand paging for IOMMU is only supported for PCIe via PRI
  329. * (Page Request Interface) not for HIDMA. All other hardware instances
  330. * including HIDMA work on pinned DMA addresses.
  331. *
  332. * HIDMA is not aware of IOMMU presence since it follows the DMA API. All
  333. * IOMMU latency will be built into the data movement time. By the time
  334. * interrupt happens, IOMMU lookups + data movement has already taken place.
  335. *
  336. * While the first read in a typical PCI endpoint ISR flushes all outstanding
  337. * requests traditionally to the destination, this concept does not apply
  338. * here for this HW.
  339. */
  340. static void hidma_ll_int_handler_internal(struct hidma_lldev *lldev, int cause)
  341. {
  342. if (cause & HIDMA_ERR_INT_MASK) {
  343. dev_err(lldev->dev, "error 0x%x, disabling...\n",
  344. cause);
  345. /* Clear out pending interrupts */
  346. writel(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  347. /* No further submissions. */
  348. hidma_ll_disable(lldev);
  349. /* Driver completes the txn and intimates the client.*/
  350. hidma_cleanup_pending_tre(lldev, 0xFF,
  351. HIDMA_EVRE_STATUS_ERROR);
  352. return;
  353. }
  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. /* We consumed TREs or there are pending TREs or EVREs. */
  366. writel_relaxed(cause, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  367. }
  368. irqreturn_t hidma_ll_inthandler(int chirq, void *arg)
  369. {
  370. struct hidma_lldev *lldev = arg;
  371. u32 status;
  372. u32 enable;
  373. u32 cause;
  374. status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  375. enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  376. cause = status & enable;
  377. while (cause) {
  378. hidma_ll_int_handler_internal(lldev, cause);
  379. /*
  380. * Another interrupt might have arrived while we are
  381. * processing this one. Read the new cause.
  382. */
  383. status = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  384. enable = readl_relaxed(lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  385. cause = status & enable;
  386. }
  387. return IRQ_HANDLED;
  388. }
  389. irqreturn_t hidma_ll_inthandler_msi(int chirq, void *arg, int cause)
  390. {
  391. struct hidma_lldev *lldev = arg;
  392. hidma_ll_int_handler_internal(lldev, cause);
  393. return IRQ_HANDLED;
  394. }
  395. int hidma_ll_enable(struct hidma_lldev *lldev)
  396. {
  397. u32 val;
  398. int ret;
  399. val = readl(lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  400. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  401. val |= HIDMA_CH_ENABLE << 16;
  402. writel(val, lldev->evca + HIDMA_EVCA_CTRLSTS_REG);
  403. ret = readl_poll_timeout(lldev->evca + HIDMA_EVCA_CTRLSTS_REG, val,
  404. hidma_is_chan_enabled(HIDMA_CH_STATE(val)),
  405. 1000, 10000);
  406. if (ret) {
  407. dev_err(lldev->dev, "event channel did not get enabled\n");
  408. return ret;
  409. }
  410. val = readl(lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  411. val &= ~(HIDMA_CH_CONTROL_MASK << 16);
  412. val |= HIDMA_CH_ENABLE << 16;
  413. writel(val, lldev->trca + HIDMA_TRCA_CTRLSTS_REG);
  414. ret = readl_poll_timeout(lldev->trca + HIDMA_TRCA_CTRLSTS_REG, val,
  415. hidma_is_chan_enabled(HIDMA_CH_STATE(val)),
  416. 1000, 10000);
  417. if (ret) {
  418. dev_err(lldev->dev, "transfer channel did not get enabled\n");
  419. return ret;
  420. }
  421. lldev->trch_state = HIDMA_CH_ENABLED;
  422. lldev->evch_state = HIDMA_CH_ENABLED;
  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. return 0;
  505. }
  506. void hidma_ll_set_transfer_params(struct hidma_lldev *lldev, u32 tre_ch,
  507. dma_addr_t src, dma_addr_t dest, u32 len,
  508. u32 flags)
  509. {
  510. struct hidma_tre *tre;
  511. u32 *tre_local;
  512. if (tre_ch >= lldev->nr_tres) {
  513. dev_err(lldev->dev, "invalid TRE number in transfer params:%d",
  514. tre_ch);
  515. return;
  516. }
  517. tre = &lldev->trepool[tre_ch];
  518. if (atomic_read(&tre->allocated) != true) {
  519. dev_err(lldev->dev, "trying to set params on an unused TRE:%d",
  520. tre_ch);
  521. return;
  522. }
  523. tre_local = &tre->tre_local[0];
  524. tre_local[HIDMA_TRE_LEN_IDX] = len;
  525. tre_local[HIDMA_TRE_SRC_LOW_IDX] = lower_32_bits(src);
  526. tre_local[HIDMA_TRE_SRC_HI_IDX] = upper_32_bits(src);
  527. tre_local[HIDMA_TRE_DEST_LOW_IDX] = lower_32_bits(dest);
  528. tre_local[HIDMA_TRE_DEST_HI_IDX] = upper_32_bits(dest);
  529. tre->int_flags = flags;
  530. }
  531. /*
  532. * Called during initialization and after an error condition
  533. * to restore hardware state.
  534. */
  535. int hidma_ll_setup(struct hidma_lldev *lldev)
  536. {
  537. int rc;
  538. u64 addr;
  539. u32 val;
  540. u32 nr_tres = lldev->nr_tres;
  541. atomic_set(&lldev->pending_tre_count, 0);
  542. lldev->tre_processed_off = 0;
  543. lldev->evre_processed_off = 0;
  544. lldev->tre_write_offset = 0;
  545. /* disable interrupts */
  546. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  547. /* clear all pending interrupts */
  548. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  549. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  550. rc = hidma_ll_reset(lldev);
  551. if (rc)
  552. return rc;
  553. /*
  554. * Clear all pending interrupts again.
  555. * Otherwise, we observe reset complete interrupts.
  556. */
  557. val = readl(lldev->evca + HIDMA_EVCA_IRQ_STAT_REG);
  558. writel(val, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  559. /* disable interrupts again after reset */
  560. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  561. addr = lldev->tre_dma;
  562. writel(lower_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_LOW_REG);
  563. writel(upper_32_bits(addr), lldev->trca + HIDMA_TRCA_RING_HIGH_REG);
  564. writel(lldev->tre_ring_size, lldev->trca + HIDMA_TRCA_RING_LEN_REG);
  565. addr = lldev->evre_dma;
  566. writel(lower_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_LOW_REG);
  567. writel(upper_32_bits(addr), lldev->evca + HIDMA_EVCA_RING_HIGH_REG);
  568. writel(HIDMA_EVRE_SIZE * nr_tres,
  569. lldev->evca + HIDMA_EVCA_RING_LEN_REG);
  570. /* configure interrupts */
  571. hidma_ll_setup_irq(lldev, lldev->msi_support);
  572. rc = hidma_ll_enable(lldev);
  573. if (rc)
  574. return rc;
  575. return rc;
  576. }
  577. void hidma_ll_setup_irq(struct hidma_lldev *lldev, bool msi)
  578. {
  579. u32 val;
  580. lldev->msi_support = msi;
  581. /* disable interrupts again after reset */
  582. writel(0, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  583. writel(0, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  584. /* support IRQ by default */
  585. val = readl(lldev->evca + HIDMA_EVCA_INTCTRL_REG);
  586. val &= ~0xF;
  587. if (!lldev->msi_support)
  588. val = val | 0x1;
  589. writel(val, lldev->evca + HIDMA_EVCA_INTCTRL_REG);
  590. /* clear all pending interrupts and enable them */
  591. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_CLR_REG);
  592. writel(ENABLE_IRQS, lldev->evca + HIDMA_EVCA_IRQ_EN_REG);
  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. atomic_set(&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. }