channel_hw.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x Channel
  4. *
  5. * Copyright (c) 2010-2013, NVIDIA Corporation.
  6. */
  7. #include <linux/host1x.h>
  8. #include <linux/iommu.h>
  9. #include <linux/slab.h>
  10. #include <trace/events/host1x.h>
  11. #include "../channel.h"
  12. #include "../dev.h"
  13. #include "../intr.h"
  14. #include "../job.h"
  15. #define TRACE_MAX_LENGTH 128U
  16. static void trace_write_gather(struct host1x_cdma *cdma, struct host1x_bo *bo,
  17. u32 offset, u32 words)
  18. {
  19. struct device *dev = cdma_to_channel(cdma)->dev;
  20. void *mem = NULL;
  21. if (host1x_debug_trace_cmdbuf)
  22. mem = host1x_bo_mmap(bo);
  23. if (mem) {
  24. u32 i;
  25. /*
  26. * Write in batches of 128 as there seems to be a limit
  27. * of how much you can output to ftrace at once.
  28. */
  29. for (i = 0; i < words; i += TRACE_MAX_LENGTH) {
  30. u32 num_words = min(words - i, TRACE_MAX_LENGTH);
  31. offset += i * sizeof(u32);
  32. trace_host1x_cdma_push_gather(dev_name(dev), bo,
  33. num_words, offset,
  34. mem);
  35. }
  36. host1x_bo_munmap(bo, mem);
  37. }
  38. }
  39. static void submit_wait(struct host1x_job *job, u32 id, u32 threshold,
  40. u32 next_class)
  41. {
  42. struct host1x_cdma *cdma = &job->channel->cdma;
  43. #if HOST1X_HW >= 6
  44. u32 stream_id;
  45. /*
  46. * If a memory context has been set, use it. Otherwise
  47. * (if context isolation is disabled) use the engine's
  48. * firmware stream ID.
  49. */
  50. if (job->memory_context)
  51. stream_id = job->memory_context->stream_id;
  52. else
  53. stream_id = job->engine_fallback_streamid;
  54. host1x_cdma_push_wide(cdma,
  55. host1x_opcode_setclass(
  56. HOST1X_CLASS_HOST1X,
  57. HOST1X_UCLASS_LOAD_SYNCPT_PAYLOAD_32,
  58. /* WAIT_SYNCPT_32 is at SYNCPT_PAYLOAD_32+2 */
  59. BIT(0) | BIT(2)
  60. ),
  61. threshold,
  62. id,
  63. HOST1X_OPCODE_NOP
  64. );
  65. host1x_cdma_push_wide(&job->channel->cdma,
  66. host1x_opcode_setclass(job->class, 0, 0),
  67. host1x_opcode_setpayload(stream_id),
  68. host1x_opcode_setstreamid(job->engine_streamid_offset / 4),
  69. HOST1X_OPCODE_NOP);
  70. #elif HOST1X_HW >= 2
  71. host1x_cdma_push_wide(cdma,
  72. host1x_opcode_setclass(
  73. HOST1X_CLASS_HOST1X,
  74. HOST1X_UCLASS_LOAD_SYNCPT_PAYLOAD_32,
  75. /* WAIT_SYNCPT_32 is at SYNCPT_PAYLOAD_32+2 */
  76. BIT(0) | BIT(2)
  77. ),
  78. threshold,
  79. id,
  80. host1x_opcode_setclass(next_class, 0, 0)
  81. );
  82. #else
  83. /* TODO add waitchk or use waitbases or other mitigation */
  84. host1x_cdma_push(cdma,
  85. host1x_opcode_setclass(
  86. HOST1X_CLASS_HOST1X,
  87. host1x_uclass_wait_syncpt_r(),
  88. BIT(0)
  89. ),
  90. host1x_class_host_wait_syncpt(id, threshold)
  91. );
  92. host1x_cdma_push(cdma,
  93. host1x_opcode_setclass(next_class, 0, 0),
  94. HOST1X_OPCODE_NOP
  95. );
  96. #endif
  97. }
  98. static void submit_gathers(struct host1x_job *job, u32 job_syncpt_base)
  99. {
  100. struct host1x_cdma *cdma = &job->channel->cdma;
  101. #if HOST1X_HW < 6
  102. struct device *dev = job->channel->dev;
  103. #endif
  104. unsigned int i;
  105. u32 threshold;
  106. for (i = 0; i < job->num_cmds; i++) {
  107. struct host1x_job_cmd *cmd = &job->cmds[i];
  108. if (cmd->is_wait) {
  109. if (cmd->wait.relative)
  110. threshold = job_syncpt_base + cmd->wait.threshold;
  111. else
  112. threshold = cmd->wait.threshold;
  113. submit_wait(job, cmd->wait.id, threshold, cmd->wait.next_class);
  114. } else {
  115. struct host1x_job_gather *g = &cmd->gather;
  116. dma_addr_t addr = g->base + g->offset;
  117. u32 op2, op3;
  118. op2 = lower_32_bits(addr);
  119. op3 = upper_32_bits(addr);
  120. trace_write_gather(cdma, g->bo, g->offset, g->words);
  121. if (op3 != 0) {
  122. #if HOST1X_HW >= 6
  123. u32 op1 = host1x_opcode_gather_wide(g->words);
  124. u32 op4 = HOST1X_OPCODE_NOP;
  125. host1x_cdma_push_wide(cdma, op1, op2, op3, op4);
  126. #else
  127. dev_err(dev, "invalid gather for push buffer %pad\n",
  128. &addr);
  129. continue;
  130. #endif
  131. } else {
  132. u32 op1 = host1x_opcode_gather(g->words);
  133. host1x_cdma_push(cdma, op1, op2);
  134. }
  135. }
  136. }
  137. }
  138. static inline void synchronize_syncpt_base(struct host1x_job *job)
  139. {
  140. struct host1x_syncpt *sp = job->syncpt;
  141. unsigned int id;
  142. u32 value;
  143. value = host1x_syncpt_read_max(sp);
  144. id = sp->base->id;
  145. host1x_cdma_push(&job->channel->cdma,
  146. host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
  147. HOST1X_UCLASS_LOAD_SYNCPT_BASE, 1),
  148. HOST1X_UCLASS_LOAD_SYNCPT_BASE_BASE_INDX_F(id) |
  149. HOST1X_UCLASS_LOAD_SYNCPT_BASE_VALUE_F(value));
  150. }
  151. static void host1x_channel_set_streamid(struct host1x_channel *channel)
  152. {
  153. #if HOST1X_HW >= 6
  154. u32 stream_id;
  155. if (!tegra_dev_iommu_get_stream_id(channel->dev->parent, &stream_id))
  156. stream_id = TEGRA_STREAM_ID_BYPASS;
  157. host1x_ch_writel(channel, stream_id, HOST1X_CHANNEL_SMMU_STREAMID);
  158. #endif
  159. }
  160. static void host1x_enable_gather_filter(struct host1x_channel *ch)
  161. {
  162. #if HOST1X_HW >= 6
  163. struct host1x *host = dev_get_drvdata(ch->dev->parent);
  164. u32 val;
  165. if (!host->hv_regs)
  166. return;
  167. val = host1x_hypervisor_readl(
  168. host, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32));
  169. val |= BIT(ch->id % 32);
  170. host1x_hypervisor_writel(
  171. host, val, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32));
  172. #elif HOST1X_HW >= 4
  173. host1x_ch_writel(ch,
  174. HOST1X_CHANNEL_CHANNELCTRL_KERNEL_FILTER_GBUFFER(1),
  175. HOST1X_CHANNEL_CHANNELCTRL);
  176. #endif
  177. }
  178. static void channel_program_cdma(struct host1x_job *job)
  179. {
  180. struct host1x_cdma *cdma = &job->channel->cdma;
  181. struct host1x_syncpt *sp = job->syncpt;
  182. #if HOST1X_HW >= 6
  183. u32 fence;
  184. /* Enter engine class with invalid stream ID. */
  185. host1x_cdma_push_wide(cdma,
  186. host1x_opcode_acquire_mlock(job->class),
  187. host1x_opcode_setclass(job->class, 0, 0),
  188. host1x_opcode_setpayload(0),
  189. host1x_opcode_setstreamid(job->engine_streamid_offset / 4));
  190. /* Before switching stream ID to real stream ID, ensure engine is idle. */
  191. fence = host1x_syncpt_incr_max(sp, 1);
  192. host1x_cdma_push(&job->channel->cdma,
  193. host1x_opcode_nonincr(HOST1X_UCLASS_INCR_SYNCPT, 1),
  194. HOST1X_UCLASS_INCR_SYNCPT_INDX_F(job->syncpt->id) |
  195. HOST1X_UCLASS_INCR_SYNCPT_COND_F(4));
  196. submit_wait(job, job->syncpt->id, fence, job->class);
  197. /* Submit work. */
  198. job->syncpt_end = host1x_syncpt_incr_max(sp, job->syncpt_incrs);
  199. submit_gathers(job, job->syncpt_end - job->syncpt_incrs);
  200. /* Before releasing MLOCK, ensure engine is idle again. */
  201. fence = host1x_syncpt_incr_max(sp, 1);
  202. host1x_cdma_push(&job->channel->cdma,
  203. host1x_opcode_nonincr(HOST1X_UCLASS_INCR_SYNCPT, 1),
  204. HOST1X_UCLASS_INCR_SYNCPT_INDX_F(job->syncpt->id) |
  205. HOST1X_UCLASS_INCR_SYNCPT_COND_F(4));
  206. submit_wait(job, job->syncpt->id, fence, job->class);
  207. /* Release MLOCK. */
  208. host1x_cdma_push(cdma,
  209. HOST1X_OPCODE_NOP, host1x_opcode_release_mlock(job->class));
  210. #else
  211. if (job->serialize) {
  212. /*
  213. * Force serialization by inserting a host wait for the
  214. * previous job to finish before this one can commence.
  215. */
  216. host1x_cdma_push(cdma,
  217. host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
  218. host1x_uclass_wait_syncpt_r(), 1),
  219. host1x_class_host_wait_syncpt(job->syncpt->id,
  220. host1x_syncpt_read_max(sp)));
  221. }
  222. /* Synchronize base register to allow using it for relative waiting */
  223. if (sp->base)
  224. synchronize_syncpt_base(job);
  225. /* add a setclass for modules that require it */
  226. if (job->class)
  227. host1x_cdma_push(cdma,
  228. host1x_opcode_setclass(job->class, 0, 0),
  229. HOST1X_OPCODE_NOP);
  230. job->syncpt_end = host1x_syncpt_incr_max(sp, job->syncpt_incrs);
  231. submit_gathers(job, job->syncpt_end - job->syncpt_incrs);
  232. #endif
  233. }
  234. static void job_complete_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
  235. {
  236. struct host1x_job *job = container_of(cb, struct host1x_job, fence_cb);
  237. /* Schedules CDMA update. */
  238. host1x_cdma_update(&job->channel->cdma);
  239. }
  240. static int channel_submit(struct host1x_job *job)
  241. {
  242. struct host1x_channel *ch = job->channel;
  243. struct host1x_syncpt *sp = job->syncpt;
  244. u32 prev_max = 0;
  245. u32 syncval;
  246. int err;
  247. struct host1x *host = dev_get_drvdata(ch->dev->parent);
  248. trace_host1x_channel_submit(dev_name(ch->dev),
  249. job->num_cmds, job->num_relocs,
  250. job->syncpt->id, job->syncpt_incrs);
  251. /* before error checks, return current max */
  252. prev_max = job->syncpt_end = host1x_syncpt_read_max(sp);
  253. /* get submit lock */
  254. err = mutex_lock_interruptible(&ch->submitlock);
  255. if (err)
  256. return err;
  257. host1x_channel_set_streamid(ch);
  258. host1x_enable_gather_filter(ch);
  259. host1x_hw_syncpt_assign_to_channel(host, sp, ch);
  260. /* begin a CDMA submit */
  261. err = host1x_cdma_begin(&ch->cdma, job);
  262. if (err) {
  263. mutex_unlock(&ch->submitlock);
  264. return err;
  265. }
  266. channel_program_cdma(job);
  267. syncval = host1x_syncpt_read_max(sp);
  268. /*
  269. * Create fence before submitting job to HW to avoid job completing
  270. * before the fence is set up.
  271. */
  272. job->fence = host1x_fence_create(sp, syncval, true);
  273. if (WARN(IS_ERR(job->fence), "Failed to create submit complete fence")) {
  274. job->fence = NULL;
  275. } else {
  276. err = dma_fence_add_callback(job->fence, &job->fence_cb,
  277. job_complete_callback);
  278. }
  279. /* end CDMA submit & stash pinned hMems into sync queue */
  280. host1x_cdma_end(&ch->cdma, job);
  281. trace_host1x_channel_submitted(dev_name(ch->dev), prev_max, syncval);
  282. mutex_unlock(&ch->submitlock);
  283. if (err == -ENOENT)
  284. host1x_cdma_update(&ch->cdma);
  285. else
  286. WARN(err, "Failed to set submit complete interrupt");
  287. return 0;
  288. }
  289. static int host1x_channel_init(struct host1x_channel *ch, struct host1x *dev,
  290. unsigned int index)
  291. {
  292. #if HOST1X_HW < 6
  293. ch->regs = dev->regs + index * 0x4000;
  294. #else
  295. ch->regs = dev->regs + index * 0x100;
  296. #endif
  297. return 0;
  298. }
  299. static const struct host1x_channel_ops host1x_channel_ops = {
  300. .init = host1x_channel_init,
  301. .submit = channel_submit,
  302. };