channel_hw.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 sid = 0x7f;
  155. #ifdef CONFIG_IOMMU_API
  156. struct iommu_fwspec *spec = dev_iommu_fwspec_get(channel->dev->parent);
  157. if (spec)
  158. sid = spec->ids[0] & 0xffff;
  159. #endif
  160. host1x_ch_writel(channel, sid, HOST1X_CHANNEL_SMMU_STREAMID);
  161. #endif
  162. }
  163. static void host1x_enable_gather_filter(struct host1x_channel *ch)
  164. {
  165. #if HOST1X_HW >= 6
  166. struct host1x *host = dev_get_drvdata(ch->dev->parent);
  167. u32 val;
  168. if (!host->hv_regs)
  169. return;
  170. val = host1x_hypervisor_readl(
  171. host, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32));
  172. val |= BIT(ch->id % 32);
  173. host1x_hypervisor_writel(
  174. host, val, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32));
  175. #elif HOST1X_HW >= 4
  176. host1x_ch_writel(ch,
  177. HOST1X_CHANNEL_CHANNELCTRL_KERNEL_FILTER_GBUFFER(1),
  178. HOST1X_CHANNEL_CHANNELCTRL);
  179. #endif
  180. }
  181. static void channel_program_cdma(struct host1x_job *job)
  182. {
  183. struct host1x_cdma *cdma = &job->channel->cdma;
  184. struct host1x_syncpt *sp = job->syncpt;
  185. #if HOST1X_HW >= 6
  186. u32 fence;
  187. /* Enter engine class with invalid stream ID. */
  188. host1x_cdma_push_wide(cdma,
  189. host1x_opcode_acquire_mlock(job->class),
  190. host1x_opcode_setclass(job->class, 0, 0),
  191. host1x_opcode_setpayload(0),
  192. host1x_opcode_setstreamid(job->engine_streamid_offset / 4));
  193. /* Before switching stream ID to real stream ID, ensure engine is idle. */
  194. fence = host1x_syncpt_incr_max(sp, 1);
  195. host1x_cdma_push(&job->channel->cdma,
  196. host1x_opcode_nonincr(HOST1X_UCLASS_INCR_SYNCPT, 1),
  197. HOST1X_UCLASS_INCR_SYNCPT_INDX_F(job->syncpt->id) |
  198. HOST1X_UCLASS_INCR_SYNCPT_COND_F(4));
  199. submit_wait(job, job->syncpt->id, fence, job->class);
  200. /* Submit work. */
  201. job->syncpt_end = host1x_syncpt_incr_max(sp, job->syncpt_incrs);
  202. submit_gathers(job, job->syncpt_end - job->syncpt_incrs);
  203. /* Before releasing MLOCK, ensure engine is idle again. */
  204. fence = host1x_syncpt_incr_max(sp, 1);
  205. host1x_cdma_push(&job->channel->cdma,
  206. host1x_opcode_nonincr(HOST1X_UCLASS_INCR_SYNCPT, 1),
  207. HOST1X_UCLASS_INCR_SYNCPT_INDX_F(job->syncpt->id) |
  208. HOST1X_UCLASS_INCR_SYNCPT_COND_F(4));
  209. submit_wait(job, job->syncpt->id, fence, job->class);
  210. /* Release MLOCK. */
  211. host1x_cdma_push(cdma,
  212. HOST1X_OPCODE_NOP, host1x_opcode_release_mlock(job->class));
  213. #else
  214. if (job->serialize) {
  215. /*
  216. * Force serialization by inserting a host wait for the
  217. * previous job to finish before this one can commence.
  218. */
  219. host1x_cdma_push(cdma,
  220. host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
  221. host1x_uclass_wait_syncpt_r(), 1),
  222. host1x_class_host_wait_syncpt(job->syncpt->id,
  223. host1x_syncpt_read_max(sp)));
  224. }
  225. /* Synchronize base register to allow using it for relative waiting */
  226. if (sp->base)
  227. synchronize_syncpt_base(job);
  228. /* add a setclass for modules that require it */
  229. if (job->class)
  230. host1x_cdma_push(cdma,
  231. host1x_opcode_setclass(job->class, 0, 0),
  232. HOST1X_OPCODE_NOP);
  233. job->syncpt_end = host1x_syncpt_incr_max(sp, job->syncpt_incrs);
  234. submit_gathers(job, job->syncpt_end - job->syncpt_incrs);
  235. #endif
  236. }
  237. static int channel_submit(struct host1x_job *job)
  238. {
  239. struct host1x_channel *ch = job->channel;
  240. struct host1x_syncpt *sp = job->syncpt;
  241. u32 prev_max = 0;
  242. u32 syncval;
  243. int err;
  244. struct host1x_waitlist *completed_waiter = NULL;
  245. struct host1x *host = dev_get_drvdata(ch->dev->parent);
  246. trace_host1x_channel_submit(dev_name(ch->dev),
  247. job->num_cmds, job->num_relocs,
  248. job->syncpt->id, job->syncpt_incrs);
  249. /* before error checks, return current max */
  250. prev_max = job->syncpt_end = host1x_syncpt_read_max(sp);
  251. /* get submit lock */
  252. err = mutex_lock_interruptible(&ch->submitlock);
  253. if (err)
  254. goto error;
  255. completed_waiter = kzalloc(sizeof(*completed_waiter), GFP_KERNEL);
  256. if (!completed_waiter) {
  257. mutex_unlock(&ch->submitlock);
  258. err = -ENOMEM;
  259. goto error;
  260. }
  261. host1x_channel_set_streamid(ch);
  262. host1x_enable_gather_filter(ch);
  263. host1x_hw_syncpt_assign_to_channel(host, sp, ch);
  264. /* begin a CDMA submit */
  265. err = host1x_cdma_begin(&ch->cdma, job);
  266. if (err) {
  267. mutex_unlock(&ch->submitlock);
  268. goto error;
  269. }
  270. channel_program_cdma(job);
  271. syncval = host1x_syncpt_read_max(sp);
  272. /* end CDMA submit & stash pinned hMems into sync queue */
  273. host1x_cdma_end(&ch->cdma, job);
  274. trace_host1x_channel_submitted(dev_name(ch->dev), prev_max, syncval);
  275. /* schedule a submit complete interrupt */
  276. err = host1x_intr_add_action(host, sp, syncval,
  277. HOST1X_INTR_ACTION_SUBMIT_COMPLETE, ch,
  278. completed_waiter, &job->waiter);
  279. completed_waiter = NULL;
  280. WARN(err, "Failed to set submit complete interrupt");
  281. mutex_unlock(&ch->submitlock);
  282. return 0;
  283. error:
  284. kfree(completed_waiter);
  285. return err;
  286. }
  287. static int host1x_channel_init(struct host1x_channel *ch, struct host1x *dev,
  288. unsigned int index)
  289. {
  290. #if HOST1X_HW < 6
  291. ch->regs = dev->regs + index * 0x4000;
  292. #else
  293. ch->regs = dev->regs + index * 0x100;
  294. #endif
  295. return 0;
  296. }
  297. static const struct host1x_channel_ops host1x_channel_ops = {
  298. .init = host1x_channel_init,
  299. .submit = channel_submit,
  300. };