vc4_gem.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * Copyright © 2014 Broadcom
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/device.h>
  27. #include <linux/io.h>
  28. #include <linux/sched/signal.h>
  29. #include "uapi/drm/vc4_drm.h"
  30. #include "vc4_drv.h"
  31. #include "vc4_regs.h"
  32. #include "vc4_trace.h"
  33. static void
  34. vc4_queue_hangcheck(struct drm_device *dev)
  35. {
  36. struct vc4_dev *vc4 = to_vc4_dev(dev);
  37. mod_timer(&vc4->hangcheck.timer,
  38. round_jiffies_up(jiffies + msecs_to_jiffies(100)));
  39. }
  40. struct vc4_hang_state {
  41. struct drm_vc4_get_hang_state user_state;
  42. u32 bo_count;
  43. struct drm_gem_object **bo;
  44. };
  45. static void
  46. vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)
  47. {
  48. unsigned int i;
  49. for (i = 0; i < state->user_state.bo_count; i++)
  50. drm_gem_object_put_unlocked(state->bo[i]);
  51. kfree(state);
  52. }
  53. int
  54. vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
  55. struct drm_file *file_priv)
  56. {
  57. struct drm_vc4_get_hang_state *get_state = data;
  58. struct drm_vc4_get_hang_state_bo *bo_state;
  59. struct vc4_hang_state *kernel_state;
  60. struct drm_vc4_get_hang_state *state;
  61. struct vc4_dev *vc4 = to_vc4_dev(dev);
  62. unsigned long irqflags;
  63. u32 i;
  64. int ret = 0;
  65. spin_lock_irqsave(&vc4->job_lock, irqflags);
  66. kernel_state = vc4->hang_state;
  67. if (!kernel_state) {
  68. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  69. return -ENOENT;
  70. }
  71. state = &kernel_state->user_state;
  72. /* If the user's array isn't big enough, just return the
  73. * required array size.
  74. */
  75. if (get_state->bo_count < state->bo_count) {
  76. get_state->bo_count = state->bo_count;
  77. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  78. return 0;
  79. }
  80. vc4->hang_state = NULL;
  81. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  82. /* Save the user's BO pointer, so we don't stomp it with the memcpy. */
  83. state->bo = get_state->bo;
  84. memcpy(get_state, state, sizeof(*state));
  85. bo_state = kcalloc(state->bo_count, sizeof(*bo_state), GFP_KERNEL);
  86. if (!bo_state) {
  87. ret = -ENOMEM;
  88. goto err_free;
  89. }
  90. for (i = 0; i < state->bo_count; i++) {
  91. struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]);
  92. u32 handle;
  93. ret = drm_gem_handle_create(file_priv, kernel_state->bo[i],
  94. &handle);
  95. if (ret) {
  96. state->bo_count = i;
  97. goto err_delete_handle;
  98. }
  99. bo_state[i].handle = handle;
  100. bo_state[i].paddr = vc4_bo->base.paddr;
  101. bo_state[i].size = vc4_bo->base.base.size;
  102. }
  103. if (copy_to_user(u64_to_user_ptr(get_state->bo),
  104. bo_state,
  105. state->bo_count * sizeof(*bo_state)))
  106. ret = -EFAULT;
  107. err_delete_handle:
  108. if (ret) {
  109. for (i = 0; i < state->bo_count; i++)
  110. drm_gem_handle_delete(file_priv, bo_state[i].handle);
  111. }
  112. err_free:
  113. vc4_free_hang_state(dev, kernel_state);
  114. kfree(bo_state);
  115. return ret;
  116. }
  117. static void
  118. vc4_save_hang_state(struct drm_device *dev)
  119. {
  120. struct vc4_dev *vc4 = to_vc4_dev(dev);
  121. struct drm_vc4_get_hang_state *state;
  122. struct vc4_hang_state *kernel_state;
  123. struct vc4_exec_info *exec[2];
  124. struct vc4_bo *bo;
  125. unsigned long irqflags;
  126. unsigned int i, j, unref_list_count, prev_idx;
  127. kernel_state = kcalloc(1, sizeof(*kernel_state), GFP_KERNEL);
  128. if (!kernel_state)
  129. return;
  130. state = &kernel_state->user_state;
  131. spin_lock_irqsave(&vc4->job_lock, irqflags);
  132. exec[0] = vc4_first_bin_job(vc4);
  133. exec[1] = vc4_first_render_job(vc4);
  134. if (!exec[0] && !exec[1]) {
  135. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  136. return;
  137. }
  138. /* Get the bos from both binner and renderer into hang state. */
  139. state->bo_count = 0;
  140. for (i = 0; i < 2; i++) {
  141. if (!exec[i])
  142. continue;
  143. unref_list_count = 0;
  144. list_for_each_entry(bo, &exec[i]->unref_list, unref_head)
  145. unref_list_count++;
  146. state->bo_count += exec[i]->bo_count + unref_list_count;
  147. }
  148. kernel_state->bo = kcalloc(state->bo_count,
  149. sizeof(*kernel_state->bo), GFP_ATOMIC);
  150. if (!kernel_state->bo) {
  151. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  152. return;
  153. }
  154. prev_idx = 0;
  155. for (i = 0; i < 2; i++) {
  156. if (!exec[i])
  157. continue;
  158. for (j = 0; j < exec[i]->bo_count; j++) {
  159. drm_gem_object_get(&exec[i]->bo[j]->base);
  160. kernel_state->bo[j + prev_idx] = &exec[i]->bo[j]->base;
  161. }
  162. list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
  163. drm_gem_object_get(&bo->base.base);
  164. kernel_state->bo[j + prev_idx] = &bo->base.base;
  165. j++;
  166. }
  167. prev_idx = j + 1;
  168. }
  169. if (exec[0])
  170. state->start_bin = exec[0]->ct0ca;
  171. if (exec[1])
  172. state->start_render = exec[1]->ct1ca;
  173. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  174. state->ct0ca = V3D_READ(V3D_CTNCA(0));
  175. state->ct0ea = V3D_READ(V3D_CTNEA(0));
  176. state->ct1ca = V3D_READ(V3D_CTNCA(1));
  177. state->ct1ea = V3D_READ(V3D_CTNEA(1));
  178. state->ct0cs = V3D_READ(V3D_CTNCS(0));
  179. state->ct1cs = V3D_READ(V3D_CTNCS(1));
  180. state->ct0ra0 = V3D_READ(V3D_CT00RA0);
  181. state->ct1ra0 = V3D_READ(V3D_CT01RA0);
  182. state->bpca = V3D_READ(V3D_BPCA);
  183. state->bpcs = V3D_READ(V3D_BPCS);
  184. state->bpoa = V3D_READ(V3D_BPOA);
  185. state->bpos = V3D_READ(V3D_BPOS);
  186. state->vpmbase = V3D_READ(V3D_VPMBASE);
  187. state->dbge = V3D_READ(V3D_DBGE);
  188. state->fdbgo = V3D_READ(V3D_FDBGO);
  189. state->fdbgb = V3D_READ(V3D_FDBGB);
  190. state->fdbgr = V3D_READ(V3D_FDBGR);
  191. state->fdbgs = V3D_READ(V3D_FDBGS);
  192. state->errstat = V3D_READ(V3D_ERRSTAT);
  193. spin_lock_irqsave(&vc4->job_lock, irqflags);
  194. if (vc4->hang_state) {
  195. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  196. vc4_free_hang_state(dev, kernel_state);
  197. } else {
  198. vc4->hang_state = kernel_state;
  199. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  200. }
  201. }
  202. static void
  203. vc4_reset(struct drm_device *dev)
  204. {
  205. struct vc4_dev *vc4 = to_vc4_dev(dev);
  206. DRM_INFO("Resetting GPU.\n");
  207. mutex_lock(&vc4->power_lock);
  208. if (vc4->power_refcount) {
  209. /* Power the device off and back on the by dropping the
  210. * reference on runtime PM.
  211. */
  212. pm_runtime_put_sync_suspend(&vc4->v3d->pdev->dev);
  213. pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  214. }
  215. mutex_unlock(&vc4->power_lock);
  216. vc4_irq_reset(dev);
  217. /* Rearm the hangcheck -- another job might have been waiting
  218. * for our hung one to get kicked off, and vc4_irq_reset()
  219. * would have started it.
  220. */
  221. vc4_queue_hangcheck(dev);
  222. }
  223. static void
  224. vc4_reset_work(struct work_struct *work)
  225. {
  226. struct vc4_dev *vc4 =
  227. container_of(work, struct vc4_dev, hangcheck.reset_work);
  228. vc4_save_hang_state(vc4->dev);
  229. vc4_reset(vc4->dev);
  230. }
  231. static void
  232. vc4_hangcheck_elapsed(unsigned long data)
  233. {
  234. struct drm_device *dev = (struct drm_device *)data;
  235. struct vc4_dev *vc4 = to_vc4_dev(dev);
  236. uint32_t ct0ca, ct1ca;
  237. unsigned long irqflags;
  238. struct vc4_exec_info *bin_exec, *render_exec;
  239. spin_lock_irqsave(&vc4->job_lock, irqflags);
  240. bin_exec = vc4_first_bin_job(vc4);
  241. render_exec = vc4_first_render_job(vc4);
  242. /* If idle, we can stop watching for hangs. */
  243. if (!bin_exec && !render_exec) {
  244. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  245. return;
  246. }
  247. ct0ca = V3D_READ(V3D_CTNCA(0));
  248. ct1ca = V3D_READ(V3D_CTNCA(1));
  249. /* If we've made any progress in execution, rearm the timer
  250. * and wait.
  251. */
  252. if ((bin_exec && ct0ca != bin_exec->last_ct0ca) ||
  253. (render_exec && ct1ca != render_exec->last_ct1ca)) {
  254. if (bin_exec)
  255. bin_exec->last_ct0ca = ct0ca;
  256. if (render_exec)
  257. render_exec->last_ct1ca = ct1ca;
  258. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  259. vc4_queue_hangcheck(dev);
  260. return;
  261. }
  262. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  263. /* We've gone too long with no progress, reset. This has to
  264. * be done from a work struct, since resetting can sleep and
  265. * this timer hook isn't allowed to.
  266. */
  267. schedule_work(&vc4->hangcheck.reset_work);
  268. }
  269. static void
  270. submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
  271. {
  272. struct vc4_dev *vc4 = to_vc4_dev(dev);
  273. /* Set the current and end address of the control list.
  274. * Writing the end register is what starts the job.
  275. */
  276. V3D_WRITE(V3D_CTNCA(thread), start);
  277. V3D_WRITE(V3D_CTNEA(thread), end);
  278. }
  279. int
  280. vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
  281. bool interruptible)
  282. {
  283. struct vc4_dev *vc4 = to_vc4_dev(dev);
  284. int ret = 0;
  285. unsigned long timeout_expire;
  286. DEFINE_WAIT(wait);
  287. if (vc4->finished_seqno >= seqno)
  288. return 0;
  289. if (timeout_ns == 0)
  290. return -ETIME;
  291. timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
  292. trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
  293. for (;;) {
  294. prepare_to_wait(&vc4->job_wait_queue, &wait,
  295. interruptible ? TASK_INTERRUPTIBLE :
  296. TASK_UNINTERRUPTIBLE);
  297. if (interruptible && signal_pending(current)) {
  298. ret = -ERESTARTSYS;
  299. break;
  300. }
  301. if (vc4->finished_seqno >= seqno)
  302. break;
  303. if (timeout_ns != ~0ull) {
  304. if (time_after_eq(jiffies, timeout_expire)) {
  305. ret = -ETIME;
  306. break;
  307. }
  308. schedule_timeout(timeout_expire - jiffies);
  309. } else {
  310. schedule();
  311. }
  312. }
  313. finish_wait(&vc4->job_wait_queue, &wait);
  314. trace_vc4_wait_for_seqno_end(dev, seqno);
  315. return ret;
  316. }
  317. static void
  318. vc4_flush_caches(struct drm_device *dev)
  319. {
  320. struct vc4_dev *vc4 = to_vc4_dev(dev);
  321. /* Flush the GPU L2 caches. These caches sit on top of system
  322. * L3 (the 128kb or so shared with the CPU), and are
  323. * non-allocating in the L3.
  324. */
  325. V3D_WRITE(V3D_L2CACTL,
  326. V3D_L2CACTL_L2CCLR);
  327. V3D_WRITE(V3D_SLCACTL,
  328. VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  329. VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
  330. VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
  331. VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
  332. }
  333. /* Sets the registers for the next job to be actually be executed in
  334. * the hardware.
  335. *
  336. * The job_lock should be held during this.
  337. */
  338. void
  339. vc4_submit_next_bin_job(struct drm_device *dev)
  340. {
  341. struct vc4_dev *vc4 = to_vc4_dev(dev);
  342. struct vc4_exec_info *exec;
  343. again:
  344. exec = vc4_first_bin_job(vc4);
  345. if (!exec)
  346. return;
  347. vc4_flush_caches(dev);
  348. /* Either put the job in the binner if it uses the binner, or
  349. * immediately move it to the to-be-rendered queue.
  350. */
  351. if (exec->ct0ca != exec->ct0ea) {
  352. submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
  353. } else {
  354. vc4_move_job_to_render(dev, exec);
  355. goto again;
  356. }
  357. }
  358. void
  359. vc4_submit_next_render_job(struct drm_device *dev)
  360. {
  361. struct vc4_dev *vc4 = to_vc4_dev(dev);
  362. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  363. if (!exec)
  364. return;
  365. submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
  366. }
  367. void
  368. vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
  369. {
  370. struct vc4_dev *vc4 = to_vc4_dev(dev);
  371. bool was_empty = list_empty(&vc4->render_job_list);
  372. list_move_tail(&exec->head, &vc4->render_job_list);
  373. if (was_empty)
  374. vc4_submit_next_render_job(dev);
  375. }
  376. static void
  377. vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  378. {
  379. struct vc4_bo *bo;
  380. unsigned i;
  381. for (i = 0; i < exec->bo_count; i++) {
  382. bo = to_vc4_bo(&exec->bo[i]->base);
  383. bo->seqno = seqno;
  384. reservation_object_add_shared_fence(bo->resv, exec->fence);
  385. }
  386. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  387. bo->seqno = seqno;
  388. }
  389. for (i = 0; i < exec->rcl_write_bo_count; i++) {
  390. bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
  391. bo->write_seqno = seqno;
  392. reservation_object_add_excl_fence(bo->resv, exec->fence);
  393. }
  394. }
  395. static void
  396. vc4_unlock_bo_reservations(struct drm_device *dev,
  397. struct vc4_exec_info *exec,
  398. struct ww_acquire_ctx *acquire_ctx)
  399. {
  400. int i;
  401. for (i = 0; i < exec->bo_count; i++) {
  402. struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
  403. ww_mutex_unlock(&bo->resv->lock);
  404. }
  405. ww_acquire_fini(acquire_ctx);
  406. }
  407. /* Takes the reservation lock on all the BOs being referenced, so that
  408. * at queue submit time we can update the reservations.
  409. *
  410. * We don't lock the RCL the tile alloc/state BOs, or overflow memory
  411. * (all of which are on exec->unref_list). They're entirely private
  412. * to vc4, so we don't attach dma-buf fences to them.
  413. */
  414. static int
  415. vc4_lock_bo_reservations(struct drm_device *dev,
  416. struct vc4_exec_info *exec,
  417. struct ww_acquire_ctx *acquire_ctx)
  418. {
  419. int contended_lock = -1;
  420. int i, ret;
  421. struct vc4_bo *bo;
  422. ww_acquire_init(acquire_ctx, &reservation_ww_class);
  423. retry:
  424. if (contended_lock != -1) {
  425. bo = to_vc4_bo(&exec->bo[contended_lock]->base);
  426. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  427. acquire_ctx);
  428. if (ret) {
  429. ww_acquire_done(acquire_ctx);
  430. return ret;
  431. }
  432. }
  433. for (i = 0; i < exec->bo_count; i++) {
  434. if (i == contended_lock)
  435. continue;
  436. bo = to_vc4_bo(&exec->bo[i]->base);
  437. ret = ww_mutex_lock_interruptible(&bo->resv->lock, acquire_ctx);
  438. if (ret) {
  439. int j;
  440. for (j = 0; j < i; j++) {
  441. bo = to_vc4_bo(&exec->bo[j]->base);
  442. ww_mutex_unlock(&bo->resv->lock);
  443. }
  444. if (contended_lock != -1 && contended_lock >= i) {
  445. bo = to_vc4_bo(&exec->bo[contended_lock]->base);
  446. ww_mutex_unlock(&bo->resv->lock);
  447. }
  448. if (ret == -EDEADLK) {
  449. contended_lock = i;
  450. goto retry;
  451. }
  452. ww_acquire_done(acquire_ctx);
  453. return ret;
  454. }
  455. }
  456. ww_acquire_done(acquire_ctx);
  457. /* Reserve space for our shared (read-only) fence references,
  458. * before we commit the CL to the hardware.
  459. */
  460. for (i = 0; i < exec->bo_count; i++) {
  461. bo = to_vc4_bo(&exec->bo[i]->base);
  462. ret = reservation_object_reserve_shared(bo->resv);
  463. if (ret) {
  464. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  465. return ret;
  466. }
  467. }
  468. return 0;
  469. }
  470. /* Queues a struct vc4_exec_info for execution. If no job is
  471. * currently executing, then submits it.
  472. *
  473. * Unlike most GPUs, our hardware only handles one command list at a
  474. * time. To queue multiple jobs at once, we'd need to edit the
  475. * previous command list to have a jump to the new one at the end, and
  476. * then bump the end address. That's a change for a later date,
  477. * though.
  478. */
  479. static int
  480. vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
  481. struct ww_acquire_ctx *acquire_ctx)
  482. {
  483. struct vc4_dev *vc4 = to_vc4_dev(dev);
  484. uint64_t seqno;
  485. unsigned long irqflags;
  486. struct vc4_fence *fence;
  487. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  488. if (!fence)
  489. return -ENOMEM;
  490. fence->dev = dev;
  491. spin_lock_irqsave(&vc4->job_lock, irqflags);
  492. seqno = ++vc4->emit_seqno;
  493. exec->seqno = seqno;
  494. dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
  495. vc4->dma_fence_context, exec->seqno);
  496. fence->seqno = exec->seqno;
  497. exec->fence = &fence->base;
  498. vc4_update_bo_seqnos(exec, seqno);
  499. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  500. list_add_tail(&exec->head, &vc4->bin_job_list);
  501. /* If no job was executing, kick ours off. Otherwise, it'll
  502. * get started when the previous job's flush done interrupt
  503. * occurs.
  504. */
  505. if (vc4_first_bin_job(vc4) == exec) {
  506. vc4_submit_next_bin_job(dev);
  507. vc4_queue_hangcheck(dev);
  508. }
  509. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  510. return 0;
  511. }
  512. /**
  513. * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
  514. * referenced by the job.
  515. * @dev: DRM device
  516. * @file_priv: DRM file for this fd
  517. * @exec: V3D job being set up
  518. *
  519. * The command validator needs to reference BOs by their index within
  520. * the submitted job's BO list. This does the validation of the job's
  521. * BO list and reference counting for the lifetime of the job.
  522. *
  523. * Note that this function doesn't need to unreference the BOs on
  524. * failure, because that will happen at vc4_complete_exec() time.
  525. */
  526. static int
  527. vc4_cl_lookup_bos(struct drm_device *dev,
  528. struct drm_file *file_priv,
  529. struct vc4_exec_info *exec)
  530. {
  531. struct drm_vc4_submit_cl *args = exec->args;
  532. uint32_t *handles;
  533. int ret = 0;
  534. int i;
  535. exec->bo_count = args->bo_handle_count;
  536. if (!exec->bo_count) {
  537. /* See comment on bo_index for why we have to check
  538. * this.
  539. */
  540. DRM_DEBUG("Rendering requires BOs to validate\n");
  541. return -EINVAL;
  542. }
  543. exec->bo = kvmalloc_array(exec->bo_count,
  544. sizeof(struct drm_gem_cma_object *),
  545. GFP_KERNEL | __GFP_ZERO);
  546. if (!exec->bo) {
  547. DRM_ERROR("Failed to allocate validated BO pointers\n");
  548. return -ENOMEM;
  549. }
  550. handles = kvmalloc_array(exec->bo_count, sizeof(uint32_t), GFP_KERNEL);
  551. if (!handles) {
  552. ret = -ENOMEM;
  553. DRM_ERROR("Failed to allocate incoming GEM handles\n");
  554. goto fail;
  555. }
  556. if (copy_from_user(handles, u64_to_user_ptr(args->bo_handles),
  557. exec->bo_count * sizeof(uint32_t))) {
  558. ret = -EFAULT;
  559. DRM_ERROR("Failed to copy in GEM handles\n");
  560. goto fail;
  561. }
  562. spin_lock(&file_priv->table_lock);
  563. for (i = 0; i < exec->bo_count; i++) {
  564. struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
  565. handles[i]);
  566. if (!bo) {
  567. DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
  568. i, handles[i]);
  569. ret = -EINVAL;
  570. spin_unlock(&file_priv->table_lock);
  571. goto fail;
  572. }
  573. drm_gem_object_get(bo);
  574. exec->bo[i] = (struct drm_gem_cma_object *)bo;
  575. }
  576. spin_unlock(&file_priv->table_lock);
  577. fail:
  578. kvfree(handles);
  579. return ret;
  580. }
  581. static int
  582. vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  583. {
  584. struct drm_vc4_submit_cl *args = exec->args;
  585. void *temp = NULL;
  586. void *bin;
  587. int ret = 0;
  588. uint32_t bin_offset = 0;
  589. uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
  590. 16);
  591. uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
  592. uint32_t exec_size = uniforms_offset + args->uniforms_size;
  593. uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
  594. args->shader_rec_count);
  595. struct vc4_bo *bo;
  596. if (shader_rec_offset < args->bin_cl_size ||
  597. uniforms_offset < shader_rec_offset ||
  598. exec_size < uniforms_offset ||
  599. args->shader_rec_count >= (UINT_MAX /
  600. sizeof(struct vc4_shader_state)) ||
  601. temp_size < exec_size) {
  602. DRM_DEBUG("overflow in exec arguments\n");
  603. ret = -EINVAL;
  604. goto fail;
  605. }
  606. /* Allocate space where we'll store the copied in user command lists
  607. * and shader records.
  608. *
  609. * We don't just copy directly into the BOs because we need to
  610. * read the contents back for validation, and I think the
  611. * bo->vaddr is uncached access.
  612. */
  613. temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
  614. if (!temp) {
  615. DRM_ERROR("Failed to allocate storage for copying "
  616. "in bin/render CLs.\n");
  617. ret = -ENOMEM;
  618. goto fail;
  619. }
  620. bin = temp + bin_offset;
  621. exec->shader_rec_u = temp + shader_rec_offset;
  622. exec->uniforms_u = temp + uniforms_offset;
  623. exec->shader_state = temp + exec_size;
  624. exec->shader_state_size = args->shader_rec_count;
  625. if (copy_from_user(bin,
  626. u64_to_user_ptr(args->bin_cl),
  627. args->bin_cl_size)) {
  628. ret = -EFAULT;
  629. goto fail;
  630. }
  631. if (copy_from_user(exec->shader_rec_u,
  632. u64_to_user_ptr(args->shader_rec),
  633. args->shader_rec_size)) {
  634. ret = -EFAULT;
  635. goto fail;
  636. }
  637. if (copy_from_user(exec->uniforms_u,
  638. u64_to_user_ptr(args->uniforms),
  639. args->uniforms_size)) {
  640. ret = -EFAULT;
  641. goto fail;
  642. }
  643. bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
  644. if (IS_ERR(bo)) {
  645. DRM_ERROR("Couldn't allocate BO for binning\n");
  646. ret = PTR_ERR(bo);
  647. goto fail;
  648. }
  649. exec->exec_bo = &bo->base;
  650. list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
  651. &exec->unref_list);
  652. exec->ct0ca = exec->exec_bo->paddr + bin_offset;
  653. exec->bin_u = bin;
  654. exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
  655. exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
  656. exec->shader_rec_size = args->shader_rec_size;
  657. exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
  658. exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
  659. exec->uniforms_size = args->uniforms_size;
  660. ret = vc4_validate_bin_cl(dev,
  661. exec->exec_bo->vaddr + bin_offset,
  662. bin,
  663. exec);
  664. if (ret)
  665. goto fail;
  666. ret = vc4_validate_shader_recs(dev, exec);
  667. if (ret)
  668. goto fail;
  669. /* Block waiting on any previous rendering into the CS's VBO,
  670. * IB, or textures, so that pixels are actually written by the
  671. * time we try to read them.
  672. */
  673. ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);
  674. fail:
  675. kvfree(temp);
  676. return ret;
  677. }
  678. static void
  679. vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
  680. {
  681. struct vc4_dev *vc4 = to_vc4_dev(dev);
  682. unsigned long irqflags;
  683. unsigned i;
  684. /* If we got force-completed because of GPU reset rather than
  685. * through our IRQ handler, signal the fence now.
  686. */
  687. if (exec->fence)
  688. dma_fence_signal(exec->fence);
  689. if (exec->bo) {
  690. for (i = 0; i < exec->bo_count; i++)
  691. drm_gem_object_put_unlocked(&exec->bo[i]->base);
  692. kvfree(exec->bo);
  693. }
  694. while (!list_empty(&exec->unref_list)) {
  695. struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  696. struct vc4_bo, unref_head);
  697. list_del(&bo->unref_head);
  698. drm_gem_object_put_unlocked(&bo->base.base);
  699. }
  700. /* Free up the allocation of any bin slots we used. */
  701. spin_lock_irqsave(&vc4->job_lock, irqflags);
  702. vc4->bin_alloc_used &= ~exec->bin_slots;
  703. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  704. mutex_lock(&vc4->power_lock);
  705. if (--vc4->power_refcount == 0) {
  706. pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
  707. pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
  708. }
  709. mutex_unlock(&vc4->power_lock);
  710. kfree(exec);
  711. }
  712. void
  713. vc4_job_handle_completed(struct vc4_dev *vc4)
  714. {
  715. unsigned long irqflags;
  716. struct vc4_seqno_cb *cb, *cb_temp;
  717. spin_lock_irqsave(&vc4->job_lock, irqflags);
  718. while (!list_empty(&vc4->job_done_list)) {
  719. struct vc4_exec_info *exec =
  720. list_first_entry(&vc4->job_done_list,
  721. struct vc4_exec_info, head);
  722. list_del(&exec->head);
  723. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  724. vc4_complete_exec(vc4->dev, exec);
  725. spin_lock_irqsave(&vc4->job_lock, irqflags);
  726. }
  727. list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  728. if (cb->seqno <= vc4->finished_seqno) {
  729. list_del_init(&cb->work.entry);
  730. schedule_work(&cb->work);
  731. }
  732. }
  733. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  734. }
  735. static void vc4_seqno_cb_work(struct work_struct *work)
  736. {
  737. struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  738. cb->func(cb);
  739. }
  740. int vc4_queue_seqno_cb(struct drm_device *dev,
  741. struct vc4_seqno_cb *cb, uint64_t seqno,
  742. void (*func)(struct vc4_seqno_cb *cb))
  743. {
  744. struct vc4_dev *vc4 = to_vc4_dev(dev);
  745. int ret = 0;
  746. unsigned long irqflags;
  747. cb->func = func;
  748. INIT_WORK(&cb->work, vc4_seqno_cb_work);
  749. spin_lock_irqsave(&vc4->job_lock, irqflags);
  750. if (seqno > vc4->finished_seqno) {
  751. cb->seqno = seqno;
  752. list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  753. } else {
  754. schedule_work(&cb->work);
  755. }
  756. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  757. return ret;
  758. }
  759. /* Scheduled when any job has been completed, this walks the list of
  760. * jobs that had completed and unrefs their BOs and frees their exec
  761. * structs.
  762. */
  763. static void
  764. vc4_job_done_work(struct work_struct *work)
  765. {
  766. struct vc4_dev *vc4 =
  767. container_of(work, struct vc4_dev, job_done_work);
  768. vc4_job_handle_completed(vc4);
  769. }
  770. static int
  771. vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  772. uint64_t seqno,
  773. uint64_t *timeout_ns)
  774. {
  775. unsigned long start = jiffies;
  776. int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  777. if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  778. uint64_t delta = jiffies_to_nsecs(jiffies - start);
  779. if (*timeout_ns >= delta)
  780. *timeout_ns -= delta;
  781. }
  782. return ret;
  783. }
  784. int
  785. vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  786. struct drm_file *file_priv)
  787. {
  788. struct drm_vc4_wait_seqno *args = data;
  789. return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  790. &args->timeout_ns);
  791. }
  792. int
  793. vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  794. struct drm_file *file_priv)
  795. {
  796. int ret;
  797. struct drm_vc4_wait_bo *args = data;
  798. struct drm_gem_object *gem_obj;
  799. struct vc4_bo *bo;
  800. if (args->pad != 0)
  801. return -EINVAL;
  802. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  803. if (!gem_obj) {
  804. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  805. return -EINVAL;
  806. }
  807. bo = to_vc4_bo(gem_obj);
  808. ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
  809. &args->timeout_ns);
  810. drm_gem_object_put_unlocked(gem_obj);
  811. return ret;
  812. }
  813. /**
  814. * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
  815. * @dev: DRM device
  816. * @data: ioctl argument
  817. * @file_priv: DRM file for this fd
  818. *
  819. * This is the main entrypoint for userspace to submit a 3D frame to
  820. * the GPU. Userspace provides the binner command list (if
  821. * applicable), and the kernel sets up the render command list to draw
  822. * to the framebuffer described in the ioctl, using the command lists
  823. * that the 3D engine's binner will produce.
  824. */
  825. int
  826. vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  827. struct drm_file *file_priv)
  828. {
  829. struct vc4_dev *vc4 = to_vc4_dev(dev);
  830. struct drm_vc4_submit_cl *args = data;
  831. struct vc4_exec_info *exec;
  832. struct ww_acquire_ctx acquire_ctx;
  833. int ret = 0;
  834. if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
  835. VC4_SUBMIT_CL_FIXED_RCL_ORDER |
  836. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
  837. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
  838. DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
  839. return -EINVAL;
  840. }
  841. exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  842. if (!exec) {
  843. DRM_ERROR("malloc failure on exec struct\n");
  844. return -ENOMEM;
  845. }
  846. mutex_lock(&vc4->power_lock);
  847. if (vc4->power_refcount++ == 0) {
  848. ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  849. if (ret < 0) {
  850. mutex_unlock(&vc4->power_lock);
  851. vc4->power_refcount--;
  852. kfree(exec);
  853. return ret;
  854. }
  855. }
  856. mutex_unlock(&vc4->power_lock);
  857. exec->args = args;
  858. INIT_LIST_HEAD(&exec->unref_list);
  859. ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  860. if (ret)
  861. goto fail;
  862. if (exec->args->bin_cl_size != 0) {
  863. ret = vc4_get_bcl(dev, exec);
  864. if (ret)
  865. goto fail;
  866. } else {
  867. exec->ct0ca = 0;
  868. exec->ct0ea = 0;
  869. }
  870. ret = vc4_get_rcl(dev, exec);
  871. if (ret)
  872. goto fail;
  873. ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
  874. if (ret)
  875. goto fail;
  876. /* Clear this out of the struct we'll be putting in the queue,
  877. * since it's part of our stack.
  878. */
  879. exec->args = NULL;
  880. ret = vc4_queue_submit(dev, exec, &acquire_ctx);
  881. if (ret)
  882. goto fail;
  883. /* Return the seqno for our job. */
  884. args->seqno = vc4->emit_seqno;
  885. return 0;
  886. fail:
  887. vc4_complete_exec(vc4->dev, exec);
  888. return ret;
  889. }
  890. void
  891. vc4_gem_init(struct drm_device *dev)
  892. {
  893. struct vc4_dev *vc4 = to_vc4_dev(dev);
  894. vc4->dma_fence_context = dma_fence_context_alloc(1);
  895. INIT_LIST_HEAD(&vc4->bin_job_list);
  896. INIT_LIST_HEAD(&vc4->render_job_list);
  897. INIT_LIST_HEAD(&vc4->job_done_list);
  898. INIT_LIST_HEAD(&vc4->seqno_cb_list);
  899. spin_lock_init(&vc4->job_lock);
  900. INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  901. setup_timer(&vc4->hangcheck.timer,
  902. vc4_hangcheck_elapsed,
  903. (unsigned long)dev);
  904. INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  905. mutex_init(&vc4->power_lock);
  906. }
  907. void
  908. vc4_gem_destroy(struct drm_device *dev)
  909. {
  910. struct vc4_dev *vc4 = to_vc4_dev(dev);
  911. /* Waiting for exec to finish would need to be done before
  912. * unregistering V3D.
  913. */
  914. WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  915. /* V3D should already have disabled its interrupt and cleared
  916. * the overflow allocation registers. Now free the object.
  917. */
  918. if (vc4->bin_bo) {
  919. drm_gem_object_put_unlocked(&vc4->bin_bo->base.base);
  920. vc4->bin_bo = NULL;
  921. }
  922. if (vc4->hang_state)
  923. vc4_free_hang_state(dev, vc4->hang_state);
  924. }