cdma_hw.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x Command DMA
  4. *
  5. * Copyright (c) 2010-2013, NVIDIA Corporation.
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/scatterlist.h>
  9. #include <linux/dma-mapping.h>
  10. #include "../cdma.h"
  11. #include "../channel.h"
  12. #include "../dev.h"
  13. #include "../debug.h"
  14. /*
  15. * Put the restart at the end of pushbuffer memory
  16. */
  17. static void push_buffer_init(struct push_buffer *pb)
  18. {
  19. *(u32 *)(pb->mapped + pb->size) = host1x_opcode_restart(0);
  20. }
  21. /*
  22. * Increment timedout buffer's syncpt via CPU.
  23. */
  24. static void cdma_timeout_cpu_incr(struct host1x_cdma *cdma, u32 getptr,
  25. u32 syncpt_incrs, u32 syncval, u32 nr_slots)
  26. {
  27. unsigned int i;
  28. for (i = 0; i < syncpt_incrs; i++)
  29. host1x_syncpt_incr(cdma->timeout.syncpt);
  30. /* after CPU incr, ensure shadow is up to date */
  31. host1x_syncpt_load(cdma->timeout.syncpt);
  32. }
  33. /*
  34. * Start channel DMA
  35. */
  36. static void cdma_start(struct host1x_cdma *cdma)
  37. {
  38. struct host1x_channel *ch = cdma_to_channel(cdma);
  39. u64 start, end;
  40. if (cdma->running)
  41. return;
  42. cdma->last_pos = cdma->push_buffer.pos;
  43. start = cdma->push_buffer.dma;
  44. end = cdma->push_buffer.size + 4;
  45. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  46. HOST1X_CHANNEL_DMACTRL);
  47. /* set base, put and end pointer */
  48. host1x_ch_writel(ch, lower_32_bits(start), HOST1X_CHANNEL_DMASTART);
  49. #if HOST1X_HW >= 6
  50. host1x_ch_writel(ch, upper_32_bits(start), HOST1X_CHANNEL_DMASTART_HI);
  51. #endif
  52. host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
  53. #if HOST1X_HW >= 6
  54. host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMAPUT_HI);
  55. #endif
  56. host1x_ch_writel(ch, lower_32_bits(end), HOST1X_CHANNEL_DMAEND);
  57. #if HOST1X_HW >= 6
  58. host1x_ch_writel(ch, upper_32_bits(end), HOST1X_CHANNEL_DMAEND_HI);
  59. #endif
  60. /* reset GET */
  61. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
  62. HOST1X_CHANNEL_DMACTRL_DMAGETRST |
  63. HOST1X_CHANNEL_DMACTRL_DMAINITGET,
  64. HOST1X_CHANNEL_DMACTRL);
  65. /* start the command DMA */
  66. host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
  67. cdma->running = true;
  68. }
  69. /*
  70. * Similar to cdma_start(), but rather than starting from an idle
  71. * state (where DMA GET is set to DMA PUT), on a timeout we restore
  72. * DMA GET from an explicit value (so DMA may again be pending).
  73. */
  74. static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
  75. {
  76. struct host1x *host1x = cdma_to_host1x(cdma);
  77. struct host1x_channel *ch = cdma_to_channel(cdma);
  78. u64 start, end;
  79. if (cdma->running)
  80. return;
  81. cdma->last_pos = cdma->push_buffer.pos;
  82. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  83. HOST1X_CHANNEL_DMACTRL);
  84. start = cdma->push_buffer.dma;
  85. end = cdma->push_buffer.size + 4;
  86. /* set base, end pointer (all of memory) */
  87. host1x_ch_writel(ch, lower_32_bits(start), HOST1X_CHANNEL_DMASTART);
  88. #if HOST1X_HW >= 6
  89. host1x_ch_writel(ch, upper_32_bits(start), HOST1X_CHANNEL_DMASTART_HI);
  90. #endif
  91. host1x_ch_writel(ch, lower_32_bits(end), HOST1X_CHANNEL_DMAEND);
  92. #if HOST1X_HW >= 6
  93. host1x_ch_writel(ch, upper_32_bits(end), HOST1X_CHANNEL_DMAEND_HI);
  94. #endif
  95. /* set GET, by loading the value in PUT (then reset GET) */
  96. host1x_ch_writel(ch, getptr, HOST1X_CHANNEL_DMAPUT);
  97. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
  98. HOST1X_CHANNEL_DMACTRL_DMAGETRST |
  99. HOST1X_CHANNEL_DMACTRL_DMAINITGET,
  100. HOST1X_CHANNEL_DMACTRL);
  101. dev_dbg(host1x->dev,
  102. "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n", __func__,
  103. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
  104. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
  105. cdma->last_pos);
  106. /* deassert GET reset and set PUT */
  107. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  108. HOST1X_CHANNEL_DMACTRL);
  109. host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
  110. /* start the command DMA */
  111. host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
  112. cdma->running = true;
  113. }
  114. /*
  115. * Kick channel DMA into action by writing its PUT offset (if it has changed)
  116. */
  117. static void cdma_flush(struct host1x_cdma *cdma)
  118. {
  119. struct host1x_channel *ch = cdma_to_channel(cdma);
  120. if (cdma->push_buffer.pos != cdma->last_pos) {
  121. host1x_ch_writel(ch, cdma->push_buffer.pos,
  122. HOST1X_CHANNEL_DMAPUT);
  123. cdma->last_pos = cdma->push_buffer.pos;
  124. }
  125. }
  126. static void cdma_stop(struct host1x_cdma *cdma)
  127. {
  128. struct host1x_channel *ch = cdma_to_channel(cdma);
  129. mutex_lock(&cdma->lock);
  130. if (cdma->running) {
  131. host1x_cdma_wait_locked(cdma, CDMA_EVENT_SYNC_QUEUE_EMPTY);
  132. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  133. HOST1X_CHANNEL_DMACTRL);
  134. cdma->running = false;
  135. }
  136. mutex_unlock(&cdma->lock);
  137. }
  138. static void cdma_hw_cmdproc_stop(struct host1x *host, struct host1x_channel *ch,
  139. bool stop)
  140. {
  141. #if HOST1X_HW >= 6
  142. host1x_ch_writel(ch, stop ? 0x1 : 0x0, HOST1X_CHANNEL_CMDPROC_STOP);
  143. #else
  144. u32 cmdproc_stop = host1x_sync_readl(host, HOST1X_SYNC_CMDPROC_STOP);
  145. if (stop)
  146. cmdproc_stop |= BIT(ch->id);
  147. else
  148. cmdproc_stop &= ~BIT(ch->id);
  149. host1x_sync_writel(host, cmdproc_stop, HOST1X_SYNC_CMDPROC_STOP);
  150. #endif
  151. }
  152. static void cdma_hw_teardown(struct host1x *host, struct host1x_channel *ch)
  153. {
  154. #if HOST1X_HW >= 6
  155. host1x_ch_writel(ch, 0x1, HOST1X_CHANNEL_TEARDOWN);
  156. #else
  157. host1x_sync_writel(host, BIT(ch->id), HOST1X_SYNC_CH_TEARDOWN);
  158. #endif
  159. }
  160. /*
  161. * Stops both channel's command processor and CDMA immediately.
  162. * Also, tears down the channel and resets corresponding module.
  163. */
  164. static void cdma_freeze(struct host1x_cdma *cdma)
  165. {
  166. struct host1x *host = cdma_to_host1x(cdma);
  167. struct host1x_channel *ch = cdma_to_channel(cdma);
  168. if (cdma->torndown && !cdma->running) {
  169. dev_warn(host->dev, "Already torn down\n");
  170. return;
  171. }
  172. dev_dbg(host->dev, "freezing channel (id %d)\n", ch->id);
  173. cdma_hw_cmdproc_stop(host, ch, true);
  174. dev_dbg(host->dev, "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n",
  175. __func__, host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
  176. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
  177. cdma->last_pos);
  178. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  179. HOST1X_CHANNEL_DMACTRL);
  180. cdma_hw_teardown(host, ch);
  181. cdma->running = false;
  182. cdma->torndown = true;
  183. }
  184. static void cdma_resume(struct host1x_cdma *cdma, u32 getptr)
  185. {
  186. struct host1x *host1x = cdma_to_host1x(cdma);
  187. struct host1x_channel *ch = cdma_to_channel(cdma);
  188. dev_dbg(host1x->dev,
  189. "resuming channel (id %u, DMAGET restart = 0x%x)\n",
  190. ch->id, getptr);
  191. cdma_hw_cmdproc_stop(host1x, ch, false);
  192. cdma->torndown = false;
  193. cdma_timeout_restart(cdma, getptr);
  194. }
  195. static void timeout_release_mlock(struct host1x_cdma *cdma)
  196. {
  197. #if HOST1X_HW >= 8
  198. /* Tegra186 and Tegra194 require a more complicated MLOCK release
  199. * sequence. Furthermore, those chips by default don't enforce MLOCKs,
  200. * so it turns out that if we don't /actually/ need MLOCKs, we can just
  201. * ignore them.
  202. *
  203. * As such, for now just implement this on Tegra234 where things are
  204. * stricter but also easy to implement.
  205. */
  206. struct host1x_channel *ch = cdma_to_channel(cdma);
  207. struct host1x *host1x = cdma_to_host1x(cdma);
  208. u32 offset;
  209. switch (ch->client->class) {
  210. case HOST1X_CLASS_VIC:
  211. offset = HOST1X_COMMON_VIC_MLOCK;
  212. break;
  213. case HOST1X_CLASS_NVDEC:
  214. offset = HOST1X_COMMON_NVDEC_MLOCK;
  215. break;
  216. default:
  217. WARN(1, "%s was not updated for class %u", __func__, ch->client->class);
  218. return;
  219. }
  220. host1x_common_writel(host1x, 0x0, offset);
  221. #endif
  222. }
  223. /*
  224. * If this timeout fires, it indicates the current sync_queue entry has
  225. * exceeded its TTL and the userctx should be timed out and remaining
  226. * submits already issued cleaned up (future submits return an error).
  227. */
  228. static void cdma_timeout_handler(struct work_struct *work)
  229. {
  230. u32 syncpt_val;
  231. struct host1x_cdma *cdma;
  232. struct host1x *host1x;
  233. struct host1x_channel *ch;
  234. cdma = container_of(to_delayed_work(work), struct host1x_cdma,
  235. timeout.wq);
  236. host1x = cdma_to_host1x(cdma);
  237. ch = cdma_to_channel(cdma);
  238. host1x_debug_dump(cdma_to_host1x(cdma));
  239. mutex_lock(&cdma->lock);
  240. if (!cdma->timeout.client) {
  241. dev_dbg(host1x->dev,
  242. "cdma_timeout: expired, but has no clientid\n");
  243. mutex_unlock(&cdma->lock);
  244. return;
  245. }
  246. /* stop processing to get a clean snapshot */
  247. cdma_hw_cmdproc_stop(host1x, ch, true);
  248. syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
  249. /* has buffer actually completed? */
  250. if ((s32)(syncpt_val - cdma->timeout.syncpt_val) >= 0) {
  251. dev_dbg(host1x->dev,
  252. "cdma_timeout: expired, but buffer had completed\n");
  253. /* restore */
  254. cdma_hw_cmdproc_stop(host1x, ch, false);
  255. mutex_unlock(&cdma->lock);
  256. return;
  257. }
  258. dev_warn(host1x->dev, "%s: timeout: %u (%s), HW thresh %d, done %d\n",
  259. __func__, cdma->timeout.syncpt->id, cdma->timeout.syncpt->name,
  260. syncpt_val, cdma->timeout.syncpt_val);
  261. /* stop HW, resetting channel/module */
  262. host1x_hw_cdma_freeze(host1x, cdma);
  263. /* release any held MLOCK */
  264. timeout_release_mlock(cdma);
  265. host1x_cdma_update_sync_queue(cdma, ch->dev);
  266. mutex_unlock(&cdma->lock);
  267. }
  268. /*
  269. * Init timeout resources
  270. */
  271. static int cdma_timeout_init(struct host1x_cdma *cdma)
  272. {
  273. INIT_DELAYED_WORK(&cdma->timeout.wq, cdma_timeout_handler);
  274. cdma->timeout.initialized = true;
  275. return 0;
  276. }
  277. /*
  278. * Clean up timeout resources
  279. */
  280. static void cdma_timeout_destroy(struct host1x_cdma *cdma)
  281. {
  282. if (cdma->timeout.initialized)
  283. cancel_delayed_work(&cdma->timeout.wq);
  284. cdma->timeout.initialized = false;
  285. }
  286. static const struct host1x_cdma_ops host1x_cdma_ops = {
  287. .start = cdma_start,
  288. .stop = cdma_stop,
  289. .flush = cdma_flush,
  290. .timeout_init = cdma_timeout_init,
  291. .timeout_destroy = cdma_timeout_destroy,
  292. .freeze = cdma_freeze,
  293. .resume = cdma_resume,
  294. .timeout_cpu_incr = cdma_timeout_cpu_incr,
  295. };
  296. static const struct host1x_pushbuffer_ops host1x_pushbuffer_ops = {
  297. .init = push_buffer_init,
  298. };