cdma_hw.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_bytes) = 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. u32 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->phys, getptr);
  52. getptr = (getptr + 8) & (pb->size_bytes - 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.phys, HOST1X_CHANNEL_DMASTART);
  69. host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
  70. host1x_ch_writel(ch, cdma->push_buffer.phys +
  71. cdma->push_buffer.size_bytes + 4,
  72. HOST1X_CHANNEL_DMAEND);
  73. /* reset GET */
  74. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
  75. HOST1X_CHANNEL_DMACTRL_DMAGETRST |
  76. HOST1X_CHANNEL_DMACTRL_DMAINITGET,
  77. HOST1X_CHANNEL_DMACTRL);
  78. /* start the command DMA */
  79. host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
  80. cdma->running = true;
  81. }
  82. /*
  83. * Similar to cdma_start(), but rather than starting from an idle
  84. * state (where DMA GET is set to DMA PUT), on a timeout we restore
  85. * DMA GET from an explicit value (so DMA may again be pending).
  86. */
  87. static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
  88. {
  89. struct host1x *host1x = cdma_to_host1x(cdma);
  90. struct host1x_channel *ch = cdma_to_channel(cdma);
  91. if (cdma->running)
  92. return;
  93. cdma->last_pos = cdma->push_buffer.pos;
  94. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  95. HOST1X_CHANNEL_DMACTRL);
  96. /* set base, end pointer (all of memory) */
  97. host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
  98. host1x_ch_writel(ch, cdma->push_buffer.phys +
  99. cdma->push_buffer.size_bytes,
  100. HOST1X_CHANNEL_DMAEND);
  101. /* set GET, by loading the value in PUT (then reset GET) */
  102. host1x_ch_writel(ch, getptr, HOST1X_CHANNEL_DMAPUT);
  103. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP |
  104. HOST1X_CHANNEL_DMACTRL_DMAGETRST |
  105. HOST1X_CHANNEL_DMACTRL_DMAINITGET,
  106. HOST1X_CHANNEL_DMACTRL);
  107. dev_dbg(host1x->dev,
  108. "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n", __func__,
  109. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
  110. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
  111. cdma->last_pos);
  112. /* deassert GET reset and set PUT */
  113. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  114. HOST1X_CHANNEL_DMACTRL);
  115. host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
  116. /* start the command DMA */
  117. host1x_ch_writel(ch, 0, HOST1X_CHANNEL_DMACTRL);
  118. cdma->running = true;
  119. }
  120. /*
  121. * Kick channel DMA into action by writing its PUT offset (if it has changed)
  122. */
  123. static void cdma_flush(struct host1x_cdma *cdma)
  124. {
  125. struct host1x_channel *ch = cdma_to_channel(cdma);
  126. if (cdma->push_buffer.pos != cdma->last_pos) {
  127. host1x_ch_writel(ch, cdma->push_buffer.pos,
  128. HOST1X_CHANNEL_DMAPUT);
  129. cdma->last_pos = cdma->push_buffer.pos;
  130. }
  131. }
  132. static void cdma_stop(struct host1x_cdma *cdma)
  133. {
  134. struct host1x_channel *ch = cdma_to_channel(cdma);
  135. mutex_lock(&cdma->lock);
  136. if (cdma->running) {
  137. host1x_cdma_wait_locked(cdma, CDMA_EVENT_SYNC_QUEUE_EMPTY);
  138. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  139. HOST1X_CHANNEL_DMACTRL);
  140. cdma->running = false;
  141. }
  142. mutex_unlock(&cdma->lock);
  143. }
  144. /*
  145. * Stops both channel's command processor and CDMA immediately.
  146. * Also, tears down the channel and resets corresponding module.
  147. */
  148. static void cdma_freeze(struct host1x_cdma *cdma)
  149. {
  150. struct host1x *host = cdma_to_host1x(cdma);
  151. struct host1x_channel *ch = cdma_to_channel(cdma);
  152. u32 cmdproc_stop;
  153. if (cdma->torndown && !cdma->running) {
  154. dev_warn(host->dev, "Already torn down\n");
  155. return;
  156. }
  157. dev_dbg(host->dev, "freezing channel (id %d)\n", ch->id);
  158. cmdproc_stop = host1x_sync_readl(host, HOST1X_SYNC_CMDPROC_STOP);
  159. cmdproc_stop |= BIT(ch->id);
  160. host1x_sync_writel(host, cmdproc_stop, HOST1X_SYNC_CMDPROC_STOP);
  161. dev_dbg(host->dev, "%s: DMA GET 0x%x, PUT HW 0x%x / shadow 0x%x\n",
  162. __func__, host1x_ch_readl(ch, HOST1X_CHANNEL_DMAGET),
  163. host1x_ch_readl(ch, HOST1X_CHANNEL_DMAPUT),
  164. cdma->last_pos);
  165. host1x_ch_writel(ch, HOST1X_CHANNEL_DMACTRL_DMASTOP,
  166. HOST1X_CHANNEL_DMACTRL);
  167. host1x_sync_writel(host, BIT(ch->id), HOST1X_SYNC_CH_TEARDOWN);
  168. cdma->running = false;
  169. cdma->torndown = true;
  170. }
  171. static void cdma_resume(struct host1x_cdma *cdma, u32 getptr)
  172. {
  173. struct host1x *host1x = cdma_to_host1x(cdma);
  174. struct host1x_channel *ch = cdma_to_channel(cdma);
  175. u32 cmdproc_stop;
  176. dev_dbg(host1x->dev,
  177. "resuming channel (id %d, DMAGET restart = 0x%x)\n",
  178. ch->id, getptr);
  179. cmdproc_stop = host1x_sync_readl(host1x, HOST1X_SYNC_CMDPROC_STOP);
  180. cmdproc_stop &= ~(BIT(ch->id));
  181. host1x_sync_writel(host1x, cmdproc_stop, HOST1X_SYNC_CMDPROC_STOP);
  182. cdma->torndown = false;
  183. cdma_timeout_restart(cdma, getptr);
  184. }
  185. /*
  186. * If this timeout fires, it indicates the current sync_queue entry has
  187. * exceeded its TTL and the userctx should be timed out and remaining
  188. * submits already issued cleaned up (future submits return an error).
  189. */
  190. static void cdma_timeout_handler(struct work_struct *work)
  191. {
  192. struct host1x_cdma *cdma;
  193. struct host1x *host1x;
  194. struct host1x_channel *ch;
  195. u32 syncpt_val;
  196. u32 prev_cmdproc, cmdproc_stop;
  197. cdma = container_of(to_delayed_work(work), struct host1x_cdma,
  198. timeout.wq);
  199. host1x = cdma_to_host1x(cdma);
  200. ch = cdma_to_channel(cdma);
  201. host1x_debug_dump(cdma_to_host1x(cdma));
  202. mutex_lock(&cdma->lock);
  203. if (!cdma->timeout.client) {
  204. dev_dbg(host1x->dev,
  205. "cdma_timeout: expired, but has no clientid\n");
  206. mutex_unlock(&cdma->lock);
  207. return;
  208. }
  209. /* stop processing to get a clean snapshot */
  210. prev_cmdproc = host1x_sync_readl(host1x, HOST1X_SYNC_CMDPROC_STOP);
  211. cmdproc_stop = prev_cmdproc | BIT(ch->id);
  212. host1x_sync_writel(host1x, cmdproc_stop, HOST1X_SYNC_CMDPROC_STOP);
  213. dev_dbg(host1x->dev, "cdma_timeout: cmdproc was 0x%x is 0x%x\n",
  214. prev_cmdproc, cmdproc_stop);
  215. syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
  216. /* has buffer actually completed? */
  217. if ((s32)(syncpt_val - cdma->timeout.syncpt_val) >= 0) {
  218. dev_dbg(host1x->dev,
  219. "cdma_timeout: expired, but buffer had completed\n");
  220. /* restore */
  221. cmdproc_stop = prev_cmdproc & ~(BIT(ch->id));
  222. host1x_sync_writel(host1x, cmdproc_stop,
  223. HOST1X_SYNC_CMDPROC_STOP);
  224. mutex_unlock(&cdma->lock);
  225. return;
  226. }
  227. dev_warn(host1x->dev, "%s: timeout: %d (%s), HW thresh %d, done %d\n",
  228. __func__, cdma->timeout.syncpt->id, cdma->timeout.syncpt->name,
  229. syncpt_val, cdma->timeout.syncpt_val);
  230. /* stop HW, resetting channel/module */
  231. host1x_hw_cdma_freeze(host1x, cdma);
  232. host1x_cdma_update_sync_queue(cdma, ch->dev);
  233. mutex_unlock(&cdma->lock);
  234. }
  235. /*
  236. * Init timeout resources
  237. */
  238. static int cdma_timeout_init(struct host1x_cdma *cdma, u32 syncpt_id)
  239. {
  240. INIT_DELAYED_WORK(&cdma->timeout.wq, cdma_timeout_handler);
  241. cdma->timeout.initialized = true;
  242. return 0;
  243. }
  244. /*
  245. * Clean up timeout resources
  246. */
  247. static void cdma_timeout_destroy(struct host1x_cdma *cdma)
  248. {
  249. if (cdma->timeout.initialized)
  250. cancel_delayed_work(&cdma->timeout.wq);
  251. cdma->timeout.initialized = false;
  252. }
  253. static const struct host1x_cdma_ops host1x_cdma_ops = {
  254. .start = cdma_start,
  255. .stop = cdma_stop,
  256. .flush = cdma_flush,
  257. .timeout_init = cdma_timeout_init,
  258. .timeout_destroy = cdma_timeout_destroy,
  259. .freeze = cdma_freeze,
  260. .resume = cdma_resume,
  261. .timeout_cpu_incr = cdma_timeout_cpu_incr,
  262. };
  263. static const struct host1x_pushbuffer_ops host1x_pushbuffer_ops = {
  264. .init = push_buffer_init,
  265. };