cdma_hw.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Tegra host1x Command DMA
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/dma-mapping.h>
  21. #include "../cdma.h"
  22. #include "../channel.h"
  23. #include "../dev.h"
  24. #include "../debug.h"
  25. /*
  26. * Put the restart at the end of pushbuffer memory
  27. */
  28. static void push_buffer_init(struct push_buffer *pb)
  29. {
  30. *(u32 *)(pb->mapped + pb->size) = host1x_opcode_restart(0);
  31. }
  32. /*
  33. * Increment timedout buffer's syncpt via CPU.
  34. */
  35. static void cdma_timeout_cpu_incr(struct host1x_cdma *cdma, u32 getptr,
  36. u32 syncpt_incrs, u32 syncval, u32 nr_slots)
  37. {
  38. struct host1x *host1x = cdma_to_host1x(cdma);
  39. struct push_buffer *pb = &cdma->push_buffer;
  40. unsigned int i;
  41. for (i = 0; i < syncpt_incrs; i++)
  42. host1x_syncpt_incr(cdma->timeout.syncpt);
  43. /* after CPU incr, ensure shadow is up to date */
  44. host1x_syncpt_load(cdma->timeout.syncpt);
  45. /* NOP all the PB slots */
  46. while (nr_slots--) {
  47. u32 *p = (u32 *)(pb->mapped + getptr);
  48. *(p++) = HOST1X_OPCODE_NOP;
  49. *(p++) = HOST1X_OPCODE_NOP;
  50. dev_dbg(host1x->dev, "%s: NOP at %pad+%#x\n", __func__,
  51. &pb->dma, getptr);
  52. getptr = (getptr + 8) & (pb->size - 1);
  53. }
  54. wmb();
  55. }
  56. /*
  57. * Start channel DMA
  58. */
  59. static void cdma_start(struct host1x_cdma *cdma)
  60. {
  61. struct host1x_channel *ch = cdma_to_channel(cdma);
  62. if (cdma->running)
  63. return;
  64. cdma->last_pos = cdma->push_buffer.pos;
  65. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  66. HOST1X_CHANNEL_DMACTRL);
  67. /* set base, put and end pointer */
  68. host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
  69. host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
  70. host1x_ch_writel(ch, cdma->push_buffer.dma + cdma->push_buffer.size + 4,
  71. HOST1X_CHANNEL_DMAEND);
  72. /* reset GET */
  73. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
  74. HOST1X_CHANNEL_DMACTRL_DMAGETRST |
  75. HOST1X_CHANNEL_DMACTRL_DMAINITGET,
  76. HOST1X_CHANNEL_DMACTRL);
  77. /* start the command DMA */
  78. host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
  79. cdma->running = true;
  80. }
  81. /*
  82. * Similar to cdma_start(), but rather than starting from an idle
  83. * state (where DMA GET is set to DMA PUT), on a timeout we restore
  84. * DMA GET from an explicit value (so DMA may again be pending).
  85. */
  86. static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
  87. {
  88. struct host1x *host1x = cdma_to_host1x(cdma);
  89. struct host1x_channel *ch = cdma_to_channel(cdma);
  90. if (cdma->running)
  91. return;
  92. cdma->last_pos = cdma->push_buffer.pos;
  93. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  94. HOST1X_CHANNEL_DMACTRL);
  95. /* set base, end pointer (all of memory) */
  96. host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
  97. host1x_ch_writel(ch, cdma->push_buffer.dma + cdma->push_buffer.size,
  98. HOST1X_CHANNEL_DMAEND);
  99. /* set GET, by loading the value in PUT (then reset GET) */
  100. host1x_ch_writel(ch, getptr, HOST1X_CHANNEL_DMAPUT);
  101. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
  102. HOST1X_CHANNEL_DMACTRL_DMAGETRST |
  103. HOST1X_CHANNEL_DMACTRL_DMAINITGET,
  104. HOST1X_CHANNEL_DMACTRL);
  105. dev_dbg(host1x->dev,
  106. "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n", __func__,
  107. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
  108. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
  109. cdma->last_pos);
  110. /* deassert GET reset and set PUT */
  111. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  112. HOST1X_CHANNEL_DMACTRL);
  113. host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
  114. /* start the command DMA */
  115. host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
  116. cdma->running = true;
  117. }
  118. /*
  119. * Kick channel DMA into action by writing its PUT offset (if it has changed)
  120. */
  121. static void cdma_flush(struct host1x_cdma *cdma)
  122. {
  123. struct host1x_channel *ch = cdma_to_channel(cdma);
  124. if (cdma->push_buffer.pos != cdma->last_pos) {
  125. host1x_ch_writel(ch, cdma->push_buffer.pos,
  126. HOST1X_CHANNEL_DMAPUT);
  127. cdma->last_pos = cdma->push_buffer.pos;
  128. }
  129. }
  130. static void cdma_stop(struct host1x_cdma *cdma)
  131. {
  132. struct host1x_channel *ch = cdma_to_channel(cdma);
  133. mutex_lock(&cdma->lock);
  134. if (cdma->running) {
  135. host1x_cdma_wait_locked(cdma, CDMA_EVENT_SYNC_QUEUE_EMPTY);
  136. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  137. HOST1X_CHANNEL_DMACTRL);
  138. cdma->running = false;
  139. }
  140. mutex_unlock(&cdma->lock);
  141. }
  142. static void cdma_hw_cmdproc_stop(struct host1x *host, struct host1x_channel *ch,
  143. bool stop)
  144. {
  145. #if HOST1X_HW >= 6
  146. host1x_ch_writel(ch, stop ? 0x1 : 0x0, HOST1X_CHANNEL_CMDPROC_STOP);
  147. #else
  148. u32 cmdproc_stop = host1x_sync_readl(host, HOST1X_SYNC_CMDPROC_STOP);
  149. if (stop)
  150. cmdproc_stop |= BIT(ch->id);
  151. else
  152. cmdproc_stop &= ~BIT(ch->id);
  153. host1x_sync_writel(host, cmdproc_stop, HOST1X_SYNC_CMDPROC_STOP);
  154. #endif
  155. }
  156. static void cdma_hw_teardown(struct host1x *host, struct host1x_channel *ch)
  157. {
  158. #if HOST1X_HW >= 6
  159. host1x_ch_writel(ch, 0x1, HOST1X_CHANNEL_TEARDOWN);
  160. #else
  161. host1x_sync_writel(host, BIT(ch->id), HOST1X_SYNC_CH_TEARDOWN);
  162. #endif
  163. }
  164. /*
  165. * Stops both channel's command processor and CDMA immediately.
  166. * Also, tears down the channel and resets corresponding module.
  167. */
  168. static void cdma_freeze(struct host1x_cdma *cdma)
  169. {
  170. struct host1x *host = cdma_to_host1x(cdma);
  171. struct host1x_channel *ch = cdma_to_channel(cdma);
  172. if (cdma->torndown && !cdma->running) {
  173. dev_warn(host->dev, "Already torn down\n");
  174. return;
  175. }
  176. dev_dbg(host->dev, "freezing channel (id %d)\n", ch->id);
  177. cdma_hw_cmdproc_stop(host, ch, true);
  178. dev_dbg(host->dev, "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n",
  179. __func__, host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
  180. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
  181. cdma->last_pos);
  182. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  183. HOST1X_CHANNEL_DMACTRL);
  184. cdma_hw_teardown(host, ch);
  185. cdma->running = false;
  186. cdma->torndown = true;
  187. }
  188. static void cdma_resume(struct host1x_cdma *cdma, u32 getptr)
  189. {
  190. struct host1x *host1x = cdma_to_host1x(cdma);
  191. struct host1x_channel *ch = cdma_to_channel(cdma);
  192. dev_dbg(host1x->dev,
  193. "resuming channel (id %u, DMAGET restart = 0x%x)\n",
  194. ch->id, getptr);
  195. cdma_hw_cmdproc_stop(host1x, ch, false);
  196. cdma->torndown = false;
  197. cdma_timeout_restart(cdma, getptr);
  198. }
  199. /*
  200. * If this timeout fires, it indicates the current sync_queue entry has
  201. * exceeded its TTL and the userctx should be timed out and remaining
  202. * submits already issued cleaned up (future submits return an error).
  203. */
  204. static void cdma_timeout_handler(struct work_struct *work)
  205. {
  206. u32 syncpt_val;
  207. struct host1x_cdma *cdma;
  208. struct host1x *host1x;
  209. struct host1x_channel *ch;
  210. cdma = container_of(to_delayed_work(work), struct host1x_cdma,
  211. timeout.wq);
  212. host1x = cdma_to_host1x(cdma);
  213. ch = cdma_to_channel(cdma);
  214. host1x_debug_dump(cdma_to_host1x(cdma));
  215. mutex_lock(&cdma->lock);
  216. if (!cdma->timeout.client) {
  217. dev_dbg(host1x->dev,
  218. "cdma_timeout: expired, but has no clientid\n");
  219. mutex_unlock(&cdma->lock);
  220. return;
  221. }
  222. /* stop processing to get a clean snapshot */
  223. cdma_hw_cmdproc_stop(host1x, ch, true);
  224. syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
  225. /* has buffer actually completed? */
  226. if ((s32)(syncpt_val - cdma->timeout.syncpt_val) >= 0) {
  227. dev_dbg(host1x->dev,
  228. "cdma_timeout: expired, but buffer had completed\n");
  229. /* restore */
  230. cdma_hw_cmdproc_stop(host1x, ch, false);
  231. mutex_unlock(&cdma->lock);
  232. return;
  233. }
  234. dev_warn(host1x->dev, "%s: timeout: %u (%s), HW thresh %d, done %d\n",
  235. __func__, cdma->timeout.syncpt->id, cdma->timeout.syncpt->name,
  236. syncpt_val, cdma->timeout.syncpt_val);
  237. /* stop HW, resetting channel/module */
  238. host1x_hw_cdma_freeze(host1x, cdma);
  239. host1x_cdma_update_sync_queue(cdma, ch->dev);
  240. mutex_unlock(&cdma->lock);
  241. }
  242. /*
  243. * Init timeout resources
  244. */
  245. static int cdma_timeout_init(struct host1x_cdma *cdma, unsigned int syncpt)
  246. {
  247. INIT_DELAYED_WORK(&cdma->timeout.wq, cdma_timeout_handler);
  248. cdma->timeout.initialized = true;
  249. return 0;
  250. }
  251. /*
  252. * Clean up timeout resources
  253. */
  254. static void cdma_timeout_destroy(struct host1x_cdma *cdma)
  255. {
  256. if (cdma->timeout.initialized)
  257. cancel_delayed_work(&cdma->timeout.wq);
  258. cdma->timeout.initialized = false;
  259. }
  260. static const struct host1x_cdma_ops host1x_cdma_ops = {
  261. .start = cdma_start,
  262. .stop = cdma_stop,
  263. .flush = cdma_flush,
  264. .timeout_init = cdma_timeout_init,
  265. .timeout_destroy = cdma_timeout_destroy,
  266. .freeze = cdma_freeze,
  267. .resume = cdma_resume,
  268. .timeout_cpu_incr = cdma_timeout_cpu_incr,
  269. };
  270. static const struct host1x_pushbuffer_ops host1x_pushbuffer_ops = {
  271. .init = push_buffer_init,
  272. };