v3d_sched.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2018 Broadcom */
  3. /**
  4. * DOC: Broadcom V3D scheduling
  5. *
  6. * The shared DRM GPU scheduler is used to coordinate submitting jobs
  7. * to the hardware. Each DRM fd (roughly a client process) gets its
  8. * own scheduler entity, which will process jobs in order. The GPU
  9. * scheduler will round-robin between clients to submit the next job.
  10. *
  11. * For simplicity, and in order to keep latency low for interactive
  12. * jobs when bulk background jobs are queued up, we submit a new job
  13. * to the HW only when it has completed the last one, instead of
  14. * filling up the CT[01]Q FIFOs with jobs. Similarly, we use
  15. * drm_sched_job_add_dependency() to manage the dependency between bin and
  16. * render, instead of having the clients submit jobs using the HW's
  17. * semaphores to interlock between them.
  18. */
  19. #include <linux/kthread.h>
  20. #include "v3d_drv.h"
  21. #include "v3d_regs.h"
  22. #include "v3d_trace.h"
  23. static struct v3d_job *
  24. to_v3d_job(struct drm_sched_job *sched_job)
  25. {
  26. return container_of(sched_job, struct v3d_job, base);
  27. }
  28. static struct v3d_bin_job *
  29. to_bin_job(struct drm_sched_job *sched_job)
  30. {
  31. return container_of(sched_job, struct v3d_bin_job, base.base);
  32. }
  33. static struct v3d_render_job *
  34. to_render_job(struct drm_sched_job *sched_job)
  35. {
  36. return container_of(sched_job, struct v3d_render_job, base.base);
  37. }
  38. static struct v3d_tfu_job *
  39. to_tfu_job(struct drm_sched_job *sched_job)
  40. {
  41. return container_of(sched_job, struct v3d_tfu_job, base.base);
  42. }
  43. static struct v3d_csd_job *
  44. to_csd_job(struct drm_sched_job *sched_job)
  45. {
  46. return container_of(sched_job, struct v3d_csd_job, base.base);
  47. }
  48. static void
  49. v3d_sched_job_free(struct drm_sched_job *sched_job)
  50. {
  51. struct v3d_job *job = to_v3d_job(sched_job);
  52. v3d_job_cleanup(job);
  53. }
  54. static void
  55. v3d_switch_perfmon(struct v3d_dev *v3d, struct v3d_job *job)
  56. {
  57. if (job->perfmon != v3d->active_perfmon)
  58. v3d_perfmon_stop(v3d, v3d->active_perfmon, true);
  59. if (job->perfmon && v3d->active_perfmon != job->perfmon)
  60. v3d_perfmon_start(v3d, job->perfmon);
  61. }
  62. static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
  63. {
  64. struct v3d_bin_job *job = to_bin_job(sched_job);
  65. struct v3d_dev *v3d = job->base.v3d;
  66. struct drm_device *dev = &v3d->drm;
  67. struct dma_fence *fence;
  68. unsigned long irqflags;
  69. if (unlikely(job->base.base.s_fence->finished.error))
  70. return NULL;
  71. /* Lock required around bin_job update vs
  72. * v3d_overflow_mem_work().
  73. */
  74. spin_lock_irqsave(&v3d->job_lock, irqflags);
  75. v3d->bin_job = job;
  76. /* Clear out the overflow allocation, so we don't
  77. * reuse the overflow attached to a previous job.
  78. */
  79. V3D_CORE_WRITE(0, V3D_PTB_BPOS, 0);
  80. spin_unlock_irqrestore(&v3d->job_lock, irqflags);
  81. v3d_invalidate_caches(v3d);
  82. fence = v3d_fence_create(v3d, V3D_BIN);
  83. if (IS_ERR(fence))
  84. return NULL;
  85. if (job->base.irq_fence)
  86. dma_fence_put(job->base.irq_fence);
  87. job->base.irq_fence = dma_fence_get(fence);
  88. trace_v3d_submit_cl(dev, false, to_v3d_fence(fence)->seqno,
  89. job->start, job->end);
  90. v3d_switch_perfmon(v3d, &job->base);
  91. /* Set the current and end address of the control list.
  92. * Writing the end register is what starts the job.
  93. */
  94. if (job->qma) {
  95. V3D_CORE_WRITE(0, V3D_CLE_CT0QMA, job->qma);
  96. V3D_CORE_WRITE(0, V3D_CLE_CT0QMS, job->qms);
  97. }
  98. if (job->qts) {
  99. V3D_CORE_WRITE(0, V3D_CLE_CT0QTS,
  100. V3D_CLE_CT0QTS_ENABLE |
  101. job->qts);
  102. }
  103. V3D_CORE_WRITE(0, V3D_CLE_CT0QBA, job->start);
  104. V3D_CORE_WRITE(0, V3D_CLE_CT0QEA, job->end);
  105. return fence;
  106. }
  107. static struct dma_fence *v3d_render_job_run(struct drm_sched_job *sched_job)
  108. {
  109. struct v3d_render_job *job = to_render_job(sched_job);
  110. struct v3d_dev *v3d = job->base.v3d;
  111. struct drm_device *dev = &v3d->drm;
  112. struct dma_fence *fence;
  113. if (unlikely(job->base.base.s_fence->finished.error))
  114. return NULL;
  115. v3d->render_job = job;
  116. /* Can we avoid this flush? We need to be careful of
  117. * scheduling, though -- imagine job0 rendering to texture and
  118. * job1 reading, and them being executed as bin0, bin1,
  119. * render0, render1, so that render1's flush at bin time
  120. * wasn't enough.
  121. */
  122. v3d_invalidate_caches(v3d);
  123. fence = v3d_fence_create(v3d, V3D_RENDER);
  124. if (IS_ERR(fence))
  125. return NULL;
  126. if (job->base.irq_fence)
  127. dma_fence_put(job->base.irq_fence);
  128. job->base.irq_fence = dma_fence_get(fence);
  129. trace_v3d_submit_cl(dev, true, to_v3d_fence(fence)->seqno,
  130. job->start, job->end);
  131. v3d_switch_perfmon(v3d, &job->base);
  132. /* XXX: Set the QCFG */
  133. /* Set the current and end address of the control list.
  134. * Writing the end register is what starts the job.
  135. */
  136. V3D_CORE_WRITE(0, V3D_CLE_CT1QBA, job->start);
  137. V3D_CORE_WRITE(0, V3D_CLE_CT1QEA, job->end);
  138. return fence;
  139. }
  140. static struct dma_fence *
  141. v3d_tfu_job_run(struct drm_sched_job *sched_job)
  142. {
  143. struct v3d_tfu_job *job = to_tfu_job(sched_job);
  144. struct v3d_dev *v3d = job->base.v3d;
  145. struct drm_device *dev = &v3d->drm;
  146. struct dma_fence *fence;
  147. fence = v3d_fence_create(v3d, V3D_TFU);
  148. if (IS_ERR(fence))
  149. return NULL;
  150. v3d->tfu_job = job;
  151. if (job->base.irq_fence)
  152. dma_fence_put(job->base.irq_fence);
  153. job->base.irq_fence = dma_fence_get(fence);
  154. trace_v3d_submit_tfu(dev, to_v3d_fence(fence)->seqno);
  155. V3D_WRITE(V3D_TFU_IIA, job->args.iia);
  156. V3D_WRITE(V3D_TFU_IIS, job->args.iis);
  157. V3D_WRITE(V3D_TFU_ICA, job->args.ica);
  158. V3D_WRITE(V3D_TFU_IUA, job->args.iua);
  159. V3D_WRITE(V3D_TFU_IOA, job->args.ioa);
  160. V3D_WRITE(V3D_TFU_IOS, job->args.ios);
  161. V3D_WRITE(V3D_TFU_COEF0, job->args.coef[0]);
  162. if (job->args.coef[0] & V3D_TFU_COEF0_USECOEF) {
  163. V3D_WRITE(V3D_TFU_COEF1, job->args.coef[1]);
  164. V3D_WRITE(V3D_TFU_COEF2, job->args.coef[2]);
  165. V3D_WRITE(V3D_TFU_COEF3, job->args.coef[3]);
  166. }
  167. /* ICFG kicks off the job. */
  168. V3D_WRITE(V3D_TFU_ICFG, job->args.icfg | V3D_TFU_ICFG_IOC);
  169. return fence;
  170. }
  171. static struct dma_fence *
  172. v3d_csd_job_run(struct drm_sched_job *sched_job)
  173. {
  174. struct v3d_csd_job *job = to_csd_job(sched_job);
  175. struct v3d_dev *v3d = job->base.v3d;
  176. struct drm_device *dev = &v3d->drm;
  177. struct dma_fence *fence;
  178. int i;
  179. v3d->csd_job = job;
  180. v3d_invalidate_caches(v3d);
  181. fence = v3d_fence_create(v3d, V3D_CSD);
  182. if (IS_ERR(fence))
  183. return NULL;
  184. if (job->base.irq_fence)
  185. dma_fence_put(job->base.irq_fence);
  186. job->base.irq_fence = dma_fence_get(fence);
  187. trace_v3d_submit_csd(dev, to_v3d_fence(fence)->seqno);
  188. v3d_switch_perfmon(v3d, &job->base);
  189. for (i = 1; i <= 6; i++)
  190. V3D_CORE_WRITE(0, V3D_CSD_QUEUED_CFG0 + 4 * i, job->args.cfg[i]);
  191. /* CFG0 write kicks off the job. */
  192. V3D_CORE_WRITE(0, V3D_CSD_QUEUED_CFG0, job->args.cfg[0]);
  193. return fence;
  194. }
  195. static struct dma_fence *
  196. v3d_cache_clean_job_run(struct drm_sched_job *sched_job)
  197. {
  198. struct v3d_job *job = to_v3d_job(sched_job);
  199. struct v3d_dev *v3d = job->v3d;
  200. v3d_clean_caches(v3d);
  201. return NULL;
  202. }
  203. static enum drm_gpu_sched_stat
  204. v3d_gpu_reset_for_timeout(struct v3d_dev *v3d, struct drm_sched_job *sched_job)
  205. {
  206. enum v3d_queue q;
  207. mutex_lock(&v3d->reset_lock);
  208. /* block scheduler */
  209. for (q = 0; q < V3D_MAX_QUEUES; q++)
  210. drm_sched_stop(&v3d->queue[q].sched, sched_job);
  211. if (sched_job)
  212. drm_sched_increase_karma(sched_job);
  213. /* get the GPU back into the init state */
  214. v3d_reset(v3d);
  215. for (q = 0; q < V3D_MAX_QUEUES; q++)
  216. drm_sched_resubmit_jobs(&v3d->queue[q].sched);
  217. /* Unblock schedulers and restart their jobs. */
  218. for (q = 0; q < V3D_MAX_QUEUES; q++) {
  219. drm_sched_start(&v3d->queue[q].sched, true);
  220. }
  221. mutex_unlock(&v3d->reset_lock);
  222. return DRM_GPU_SCHED_STAT_NOMINAL;
  223. }
  224. /* If the current address or return address have changed, then the GPU
  225. * has probably made progress and we should delay the reset. This
  226. * could fail if the GPU got in an infinite loop in the CL, but that
  227. * is pretty unlikely outside of an i-g-t testcase.
  228. */
  229. static enum drm_gpu_sched_stat
  230. v3d_cl_job_timedout(struct drm_sched_job *sched_job, enum v3d_queue q,
  231. u32 *timedout_ctca, u32 *timedout_ctra)
  232. {
  233. struct v3d_job *job = to_v3d_job(sched_job);
  234. struct v3d_dev *v3d = job->v3d;
  235. u32 ctca = V3D_CORE_READ(0, V3D_CLE_CTNCA(q));
  236. u32 ctra = V3D_CORE_READ(0, V3D_CLE_CTNRA(q));
  237. if (*timedout_ctca != ctca || *timedout_ctra != ctra) {
  238. *timedout_ctca = ctca;
  239. *timedout_ctra = ctra;
  240. return DRM_GPU_SCHED_STAT_NOMINAL;
  241. }
  242. return v3d_gpu_reset_for_timeout(v3d, sched_job);
  243. }
  244. static enum drm_gpu_sched_stat
  245. v3d_bin_job_timedout(struct drm_sched_job *sched_job)
  246. {
  247. struct v3d_bin_job *job = to_bin_job(sched_job);
  248. return v3d_cl_job_timedout(sched_job, V3D_BIN,
  249. &job->timedout_ctca, &job->timedout_ctra);
  250. }
  251. static enum drm_gpu_sched_stat
  252. v3d_render_job_timedout(struct drm_sched_job *sched_job)
  253. {
  254. struct v3d_render_job *job = to_render_job(sched_job);
  255. return v3d_cl_job_timedout(sched_job, V3D_RENDER,
  256. &job->timedout_ctca, &job->timedout_ctra);
  257. }
  258. static enum drm_gpu_sched_stat
  259. v3d_generic_job_timedout(struct drm_sched_job *sched_job)
  260. {
  261. struct v3d_job *job = to_v3d_job(sched_job);
  262. return v3d_gpu_reset_for_timeout(job->v3d, sched_job);
  263. }
  264. static enum drm_gpu_sched_stat
  265. v3d_csd_job_timedout(struct drm_sched_job *sched_job)
  266. {
  267. struct v3d_csd_job *job = to_csd_job(sched_job);
  268. struct v3d_dev *v3d = job->base.v3d;
  269. u32 batches = V3D_CORE_READ(0, V3D_CSD_CURRENT_CFG4);
  270. /* If we've made progress, skip reset and let the timer get
  271. * rearmed.
  272. */
  273. if (job->timedout_batches != batches) {
  274. job->timedout_batches = batches;
  275. return DRM_GPU_SCHED_STAT_NOMINAL;
  276. }
  277. return v3d_gpu_reset_for_timeout(v3d, sched_job);
  278. }
  279. static const struct drm_sched_backend_ops v3d_bin_sched_ops = {
  280. .run_job = v3d_bin_job_run,
  281. .timedout_job = v3d_bin_job_timedout,
  282. .free_job = v3d_sched_job_free,
  283. };
  284. static const struct drm_sched_backend_ops v3d_render_sched_ops = {
  285. .run_job = v3d_render_job_run,
  286. .timedout_job = v3d_render_job_timedout,
  287. .free_job = v3d_sched_job_free,
  288. };
  289. static const struct drm_sched_backend_ops v3d_tfu_sched_ops = {
  290. .run_job = v3d_tfu_job_run,
  291. .timedout_job = v3d_generic_job_timedout,
  292. .free_job = v3d_sched_job_free,
  293. };
  294. static const struct drm_sched_backend_ops v3d_csd_sched_ops = {
  295. .run_job = v3d_csd_job_run,
  296. .timedout_job = v3d_csd_job_timedout,
  297. .free_job = v3d_sched_job_free
  298. };
  299. static const struct drm_sched_backend_ops v3d_cache_clean_sched_ops = {
  300. .run_job = v3d_cache_clean_job_run,
  301. .timedout_job = v3d_generic_job_timedout,
  302. .free_job = v3d_sched_job_free
  303. };
  304. int
  305. v3d_sched_init(struct v3d_dev *v3d)
  306. {
  307. int hw_jobs_limit = 1;
  308. int job_hang_limit = 0;
  309. int hang_limit_ms = 500;
  310. int ret;
  311. ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
  312. &v3d_bin_sched_ops,
  313. hw_jobs_limit, job_hang_limit,
  314. msecs_to_jiffies(hang_limit_ms), NULL,
  315. NULL, "v3d_bin", v3d->drm.dev);
  316. if (ret)
  317. return ret;
  318. ret = drm_sched_init(&v3d->queue[V3D_RENDER].sched,
  319. &v3d_render_sched_ops,
  320. hw_jobs_limit, job_hang_limit,
  321. msecs_to_jiffies(hang_limit_ms), NULL,
  322. NULL, "v3d_render", v3d->drm.dev);
  323. if (ret)
  324. goto fail;
  325. ret = drm_sched_init(&v3d->queue[V3D_TFU].sched,
  326. &v3d_tfu_sched_ops,
  327. hw_jobs_limit, job_hang_limit,
  328. msecs_to_jiffies(hang_limit_ms), NULL,
  329. NULL, "v3d_tfu", v3d->drm.dev);
  330. if (ret)
  331. goto fail;
  332. if (v3d_has_csd(v3d)) {
  333. ret = drm_sched_init(&v3d->queue[V3D_CSD].sched,
  334. &v3d_csd_sched_ops,
  335. hw_jobs_limit, job_hang_limit,
  336. msecs_to_jiffies(hang_limit_ms), NULL,
  337. NULL, "v3d_csd", v3d->drm.dev);
  338. if (ret)
  339. goto fail;
  340. ret = drm_sched_init(&v3d->queue[V3D_CACHE_CLEAN].sched,
  341. &v3d_cache_clean_sched_ops,
  342. hw_jobs_limit, job_hang_limit,
  343. msecs_to_jiffies(hang_limit_ms), NULL,
  344. NULL, "v3d_cache_clean", v3d->drm.dev);
  345. if (ret)
  346. goto fail;
  347. }
  348. return 0;
  349. fail:
  350. v3d_sched_fini(v3d);
  351. return ret;
  352. }
  353. void
  354. v3d_sched_fini(struct v3d_dev *v3d)
  355. {
  356. enum v3d_queue q;
  357. for (q = 0; q < V3D_MAX_QUEUES; q++) {
  358. if (v3d->queue[q].sched.ready)
  359. drm_sched_fini(&v3d->queue[q].sched);
  360. }
  361. }