vc4_gem.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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. bo = to_vc4_bo(&exec[i]->bo[j]->base);
  160. /* Retain BOs just in case they were marked purgeable.
  161. * This prevents the BO from being purged before
  162. * someone had a chance to dump the hang state.
  163. */
  164. WARN_ON(!refcount_read(&bo->usecnt));
  165. refcount_inc(&bo->usecnt);
  166. drm_gem_object_get(&exec[i]->bo[j]->base);
  167. kernel_state->bo[j + prev_idx] = &exec[i]->bo[j]->base;
  168. }
  169. list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
  170. /* No need to retain BOs coming from the ->unref_list
  171. * because they are naturally unpurgeable.
  172. */
  173. drm_gem_object_get(&bo->base.base);
  174. kernel_state->bo[j + prev_idx] = &bo->base.base;
  175. j++;
  176. }
  177. prev_idx = j + 1;
  178. }
  179. if (exec[0])
  180. state->start_bin = exec[0]->ct0ca;
  181. if (exec[1])
  182. state->start_render = exec[1]->ct1ca;
  183. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  184. state->ct0ca = V3D_READ(V3D_CTNCA(0));
  185. state->ct0ea = V3D_READ(V3D_CTNEA(0));
  186. state->ct1ca = V3D_READ(V3D_CTNCA(1));
  187. state->ct1ea = V3D_READ(V3D_CTNEA(1));
  188. state->ct0cs = V3D_READ(V3D_CTNCS(0));
  189. state->ct1cs = V3D_READ(V3D_CTNCS(1));
  190. state->ct0ra0 = V3D_READ(V3D_CT00RA0);
  191. state->ct1ra0 = V3D_READ(V3D_CT01RA0);
  192. state->bpca = V3D_READ(V3D_BPCA);
  193. state->bpcs = V3D_READ(V3D_BPCS);
  194. state->bpoa = V3D_READ(V3D_BPOA);
  195. state->bpos = V3D_READ(V3D_BPOS);
  196. state->vpmbase = V3D_READ(V3D_VPMBASE);
  197. state->dbge = V3D_READ(V3D_DBGE);
  198. state->fdbgo = V3D_READ(V3D_FDBGO);
  199. state->fdbgb = V3D_READ(V3D_FDBGB);
  200. state->fdbgr = V3D_READ(V3D_FDBGR);
  201. state->fdbgs = V3D_READ(V3D_FDBGS);
  202. state->errstat = V3D_READ(V3D_ERRSTAT);
  203. /* We need to turn purgeable BOs into unpurgeable ones so that
  204. * userspace has a chance to dump the hang state before the kernel
  205. * decides to purge those BOs.
  206. * Note that BO consistency at dump time cannot be guaranteed. For
  207. * example, if the owner of these BOs decides to re-use them or mark
  208. * them purgeable again there's nothing we can do to prevent it.
  209. */
  210. for (i = 0; i < kernel_state->user_state.bo_count; i++) {
  211. struct vc4_bo *bo = to_vc4_bo(kernel_state->bo[i]);
  212. if (bo->madv == __VC4_MADV_NOTSUPP)
  213. continue;
  214. mutex_lock(&bo->madv_lock);
  215. if (!WARN_ON(bo->madv == __VC4_MADV_PURGED))
  216. bo->madv = VC4_MADV_WILLNEED;
  217. refcount_dec(&bo->usecnt);
  218. mutex_unlock(&bo->madv_lock);
  219. }
  220. spin_lock_irqsave(&vc4->job_lock, irqflags);
  221. if (vc4->hang_state) {
  222. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  223. vc4_free_hang_state(dev, kernel_state);
  224. } else {
  225. vc4->hang_state = kernel_state;
  226. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  227. }
  228. }
  229. static void
  230. vc4_reset(struct drm_device *dev)
  231. {
  232. struct vc4_dev *vc4 = to_vc4_dev(dev);
  233. DRM_INFO("Resetting GPU.\n");
  234. mutex_lock(&vc4->power_lock);
  235. if (vc4->power_refcount) {
  236. /* Power the device off and back on the by dropping the
  237. * reference on runtime PM.
  238. */
  239. pm_runtime_put_sync_suspend(&vc4->v3d->pdev->dev);
  240. pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  241. }
  242. mutex_unlock(&vc4->power_lock);
  243. vc4_irq_reset(dev);
  244. /* Rearm the hangcheck -- another job might have been waiting
  245. * for our hung one to get kicked off, and vc4_irq_reset()
  246. * would have started it.
  247. */
  248. vc4_queue_hangcheck(dev);
  249. }
  250. static void
  251. vc4_reset_work(struct work_struct *work)
  252. {
  253. struct vc4_dev *vc4 =
  254. container_of(work, struct vc4_dev, hangcheck.reset_work);
  255. vc4_save_hang_state(vc4->dev);
  256. vc4_reset(vc4->dev);
  257. }
  258. static void
  259. vc4_hangcheck_elapsed(struct timer_list *t)
  260. {
  261. struct vc4_dev *vc4 = from_timer(vc4, t, hangcheck.timer);
  262. struct drm_device *dev = vc4->dev;
  263. uint32_t ct0ca, ct1ca;
  264. unsigned long irqflags;
  265. struct vc4_exec_info *bin_exec, *render_exec;
  266. spin_lock_irqsave(&vc4->job_lock, irqflags);
  267. bin_exec = vc4_first_bin_job(vc4);
  268. render_exec = vc4_first_render_job(vc4);
  269. /* If idle, we can stop watching for hangs. */
  270. if (!bin_exec && !render_exec) {
  271. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  272. return;
  273. }
  274. ct0ca = V3D_READ(V3D_CTNCA(0));
  275. ct1ca = V3D_READ(V3D_CTNCA(1));
  276. /* If we've made any progress in execution, rearm the timer
  277. * and wait.
  278. */
  279. if ((bin_exec && ct0ca != bin_exec->last_ct0ca) ||
  280. (render_exec && ct1ca != render_exec->last_ct1ca)) {
  281. if (bin_exec)
  282. bin_exec->last_ct0ca = ct0ca;
  283. if (render_exec)
  284. render_exec->last_ct1ca = ct1ca;
  285. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  286. vc4_queue_hangcheck(dev);
  287. return;
  288. }
  289. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  290. /* We've gone too long with no progress, reset. This has to
  291. * be done from a work struct, since resetting can sleep and
  292. * this timer hook isn't allowed to.
  293. */
  294. schedule_work(&vc4->hangcheck.reset_work);
  295. }
  296. static void
  297. submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
  298. {
  299. struct vc4_dev *vc4 = to_vc4_dev(dev);
  300. /* Set the current and end address of the control list.
  301. * Writing the end register is what starts the job.
  302. */
  303. V3D_WRITE(V3D_CTNCA(thread), start);
  304. V3D_WRITE(V3D_CTNEA(thread), end);
  305. }
  306. int
  307. vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
  308. bool interruptible)
  309. {
  310. struct vc4_dev *vc4 = to_vc4_dev(dev);
  311. int ret = 0;
  312. unsigned long timeout_expire;
  313. DEFINE_WAIT(wait);
  314. if (vc4->finished_seqno >= seqno)
  315. return 0;
  316. if (timeout_ns == 0)
  317. return -ETIME;
  318. timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
  319. trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
  320. for (;;) {
  321. prepare_to_wait(&vc4->job_wait_queue, &wait,
  322. interruptible ? TASK_INTERRUPTIBLE :
  323. TASK_UNINTERRUPTIBLE);
  324. if (interruptible && signal_pending(current)) {
  325. ret = -ERESTARTSYS;
  326. break;
  327. }
  328. if (vc4->finished_seqno >= seqno)
  329. break;
  330. if (timeout_ns != ~0ull) {
  331. if (time_after_eq(jiffies, timeout_expire)) {
  332. ret = -ETIME;
  333. break;
  334. }
  335. schedule_timeout(timeout_expire - jiffies);
  336. } else {
  337. schedule();
  338. }
  339. }
  340. finish_wait(&vc4->job_wait_queue, &wait);
  341. trace_vc4_wait_for_seqno_end(dev, seqno);
  342. return ret;
  343. }
  344. static void
  345. vc4_flush_caches(struct drm_device *dev)
  346. {
  347. struct vc4_dev *vc4 = to_vc4_dev(dev);
  348. /* Flush the GPU L2 caches. These caches sit on top of system
  349. * L3 (the 128kb or so shared with the CPU), and are
  350. * non-allocating in the L3.
  351. */
  352. V3D_WRITE(V3D_L2CACTL,
  353. V3D_L2CACTL_L2CCLR);
  354. V3D_WRITE(V3D_SLCACTL,
  355. VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  356. VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
  357. VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
  358. VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
  359. }
  360. /* Sets the registers for the next job to be actually be executed in
  361. * the hardware.
  362. *
  363. * The job_lock should be held during this.
  364. */
  365. void
  366. vc4_submit_next_bin_job(struct drm_device *dev)
  367. {
  368. struct vc4_dev *vc4 = to_vc4_dev(dev);
  369. struct vc4_exec_info *exec;
  370. again:
  371. exec = vc4_first_bin_job(vc4);
  372. if (!exec)
  373. return;
  374. vc4_flush_caches(dev);
  375. /* Either put the job in the binner if it uses the binner, or
  376. * immediately move it to the to-be-rendered queue.
  377. */
  378. if (exec->ct0ca != exec->ct0ea) {
  379. submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
  380. } else {
  381. vc4_move_job_to_render(dev, exec);
  382. goto again;
  383. }
  384. }
  385. void
  386. vc4_submit_next_render_job(struct drm_device *dev)
  387. {
  388. struct vc4_dev *vc4 = to_vc4_dev(dev);
  389. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  390. if (!exec)
  391. return;
  392. submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
  393. }
  394. void
  395. vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
  396. {
  397. struct vc4_dev *vc4 = to_vc4_dev(dev);
  398. bool was_empty = list_empty(&vc4->render_job_list);
  399. list_move_tail(&exec->head, &vc4->render_job_list);
  400. if (was_empty)
  401. vc4_submit_next_render_job(dev);
  402. }
  403. static void
  404. vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  405. {
  406. struct vc4_bo *bo;
  407. unsigned i;
  408. for (i = 0; i < exec->bo_count; i++) {
  409. bo = to_vc4_bo(&exec->bo[i]->base);
  410. bo->seqno = seqno;
  411. reservation_object_add_shared_fence(bo->resv, exec->fence);
  412. }
  413. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  414. bo->seqno = seqno;
  415. }
  416. for (i = 0; i < exec->rcl_write_bo_count; i++) {
  417. bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
  418. bo->write_seqno = seqno;
  419. reservation_object_add_excl_fence(bo->resv, exec->fence);
  420. }
  421. }
  422. static void
  423. vc4_unlock_bo_reservations(struct drm_device *dev,
  424. struct vc4_exec_info *exec,
  425. struct ww_acquire_ctx *acquire_ctx)
  426. {
  427. int i;
  428. for (i = 0; i < exec->bo_count; i++) {
  429. struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
  430. ww_mutex_unlock(&bo->resv->lock);
  431. }
  432. ww_acquire_fini(acquire_ctx);
  433. }
  434. /* Takes the reservation lock on all the BOs being referenced, so that
  435. * at queue submit time we can update the reservations.
  436. *
  437. * We don't lock the RCL the tile alloc/state BOs, or overflow memory
  438. * (all of which are on exec->unref_list). They're entirely private
  439. * to vc4, so we don't attach dma-buf fences to them.
  440. */
  441. static int
  442. vc4_lock_bo_reservations(struct drm_device *dev,
  443. struct vc4_exec_info *exec,
  444. struct ww_acquire_ctx *acquire_ctx)
  445. {
  446. int contended_lock = -1;
  447. int i, ret;
  448. struct vc4_bo *bo;
  449. ww_acquire_init(acquire_ctx, &reservation_ww_class);
  450. retry:
  451. if (contended_lock != -1) {
  452. bo = to_vc4_bo(&exec->bo[contended_lock]->base);
  453. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  454. acquire_ctx);
  455. if (ret) {
  456. ww_acquire_done(acquire_ctx);
  457. return ret;
  458. }
  459. }
  460. for (i = 0; i < exec->bo_count; i++) {
  461. if (i == contended_lock)
  462. continue;
  463. bo = to_vc4_bo(&exec->bo[i]->base);
  464. ret = ww_mutex_lock_interruptible(&bo->resv->lock, acquire_ctx);
  465. if (ret) {
  466. int j;
  467. for (j = 0; j < i; j++) {
  468. bo = to_vc4_bo(&exec->bo[j]->base);
  469. ww_mutex_unlock(&bo->resv->lock);
  470. }
  471. if (contended_lock != -1 && contended_lock >= i) {
  472. bo = to_vc4_bo(&exec->bo[contended_lock]->base);
  473. ww_mutex_unlock(&bo->resv->lock);
  474. }
  475. if (ret == -EDEADLK) {
  476. contended_lock = i;
  477. goto retry;
  478. }
  479. ww_acquire_done(acquire_ctx);
  480. return ret;
  481. }
  482. }
  483. ww_acquire_done(acquire_ctx);
  484. /* Reserve space for our shared (read-only) fence references,
  485. * before we commit the CL to the hardware.
  486. */
  487. for (i = 0; i < exec->bo_count; i++) {
  488. bo = to_vc4_bo(&exec->bo[i]->base);
  489. ret = reservation_object_reserve_shared(bo->resv);
  490. if (ret) {
  491. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  492. return ret;
  493. }
  494. }
  495. return 0;
  496. }
  497. /* Queues a struct vc4_exec_info for execution. If no job is
  498. * currently executing, then submits it.
  499. *
  500. * Unlike most GPUs, our hardware only handles one command list at a
  501. * time. To queue multiple jobs at once, we'd need to edit the
  502. * previous command list to have a jump to the new one at the end, and
  503. * then bump the end address. That's a change for a later date,
  504. * though.
  505. */
  506. static int
  507. vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
  508. struct ww_acquire_ctx *acquire_ctx)
  509. {
  510. struct vc4_dev *vc4 = to_vc4_dev(dev);
  511. uint64_t seqno;
  512. unsigned long irqflags;
  513. struct vc4_fence *fence;
  514. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  515. if (!fence)
  516. return -ENOMEM;
  517. fence->dev = dev;
  518. spin_lock_irqsave(&vc4->job_lock, irqflags);
  519. seqno = ++vc4->emit_seqno;
  520. exec->seqno = seqno;
  521. dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
  522. vc4->dma_fence_context, exec->seqno);
  523. fence->seqno = exec->seqno;
  524. exec->fence = &fence->base;
  525. vc4_update_bo_seqnos(exec, seqno);
  526. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  527. list_add_tail(&exec->head, &vc4->bin_job_list);
  528. /* If no job was executing, kick ours off. Otherwise, it'll
  529. * get started when the previous job's flush done interrupt
  530. * occurs.
  531. */
  532. if (vc4_first_bin_job(vc4) == exec) {
  533. vc4_submit_next_bin_job(dev);
  534. vc4_queue_hangcheck(dev);
  535. }
  536. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  537. return 0;
  538. }
  539. /**
  540. * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
  541. * referenced by the job.
  542. * @dev: DRM device
  543. * @file_priv: DRM file for this fd
  544. * @exec: V3D job being set up
  545. *
  546. * The command validator needs to reference BOs by their index within
  547. * the submitted job's BO list. This does the validation of the job's
  548. * BO list and reference counting for the lifetime of the job.
  549. */
  550. static int
  551. vc4_cl_lookup_bos(struct drm_device *dev,
  552. struct drm_file *file_priv,
  553. struct vc4_exec_info *exec)
  554. {
  555. struct drm_vc4_submit_cl *args = exec->args;
  556. uint32_t *handles;
  557. int ret = 0;
  558. int i;
  559. exec->bo_count = args->bo_handle_count;
  560. if (!exec->bo_count) {
  561. /* See comment on bo_index for why we have to check
  562. * this.
  563. */
  564. DRM_DEBUG("Rendering requires BOs to validate\n");
  565. return -EINVAL;
  566. }
  567. exec->bo = kvmalloc_array(exec->bo_count,
  568. sizeof(struct drm_gem_cma_object *),
  569. GFP_KERNEL | __GFP_ZERO);
  570. if (!exec->bo) {
  571. DRM_ERROR("Failed to allocate validated BO pointers\n");
  572. return -ENOMEM;
  573. }
  574. handles = kvmalloc_array(exec->bo_count, sizeof(uint32_t), GFP_KERNEL);
  575. if (!handles) {
  576. ret = -ENOMEM;
  577. DRM_ERROR("Failed to allocate incoming GEM handles\n");
  578. goto fail;
  579. }
  580. if (copy_from_user(handles, u64_to_user_ptr(args->bo_handles),
  581. exec->bo_count * sizeof(uint32_t))) {
  582. ret = -EFAULT;
  583. DRM_ERROR("Failed to copy in GEM handles\n");
  584. goto fail;
  585. }
  586. spin_lock(&file_priv->table_lock);
  587. for (i = 0; i < exec->bo_count; i++) {
  588. struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
  589. handles[i]);
  590. if (!bo) {
  591. DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
  592. i, handles[i]);
  593. ret = -EINVAL;
  594. break;
  595. }
  596. drm_gem_object_get(bo);
  597. exec->bo[i] = (struct drm_gem_cma_object *)bo;
  598. }
  599. spin_unlock(&file_priv->table_lock);
  600. if (ret)
  601. goto fail_put_bo;
  602. for (i = 0; i < exec->bo_count; i++) {
  603. ret = vc4_bo_inc_usecnt(to_vc4_bo(&exec->bo[i]->base));
  604. if (ret)
  605. goto fail_dec_usecnt;
  606. }
  607. kvfree(handles);
  608. return 0;
  609. fail_dec_usecnt:
  610. /* Decrease usecnt on acquired objects.
  611. * We cannot rely on vc4_complete_exec() to release resources here,
  612. * because vc4_complete_exec() has no information about which BO has
  613. * had its ->usecnt incremented.
  614. * To make things easier we just free everything explicitly and set
  615. * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
  616. * step.
  617. */
  618. for (i-- ; i >= 0; i--)
  619. vc4_bo_dec_usecnt(to_vc4_bo(&exec->bo[i]->base));
  620. fail_put_bo:
  621. /* Release any reference to acquired objects. */
  622. for (i = 0; i < exec->bo_count && exec->bo[i]; i++)
  623. drm_gem_object_put_unlocked(&exec->bo[i]->base);
  624. fail:
  625. kvfree(handles);
  626. kvfree(exec->bo);
  627. exec->bo = NULL;
  628. return ret;
  629. }
  630. static int
  631. vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  632. {
  633. struct drm_vc4_submit_cl *args = exec->args;
  634. void *temp = NULL;
  635. void *bin;
  636. int ret = 0;
  637. uint32_t bin_offset = 0;
  638. uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
  639. 16);
  640. uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
  641. uint32_t exec_size = uniforms_offset + args->uniforms_size;
  642. uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
  643. args->shader_rec_count);
  644. struct vc4_bo *bo;
  645. if (shader_rec_offset < args->bin_cl_size ||
  646. uniforms_offset < shader_rec_offset ||
  647. exec_size < uniforms_offset ||
  648. args->shader_rec_count >= (UINT_MAX /
  649. sizeof(struct vc4_shader_state)) ||
  650. temp_size < exec_size) {
  651. DRM_DEBUG("overflow in exec arguments\n");
  652. ret = -EINVAL;
  653. goto fail;
  654. }
  655. /* Allocate space where we'll store the copied in user command lists
  656. * and shader records.
  657. *
  658. * We don't just copy directly into the BOs because we need to
  659. * read the contents back for validation, and I think the
  660. * bo->vaddr is uncached access.
  661. */
  662. temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
  663. if (!temp) {
  664. DRM_ERROR("Failed to allocate storage for copying "
  665. "in bin/render CLs.\n");
  666. ret = -ENOMEM;
  667. goto fail;
  668. }
  669. bin = temp + bin_offset;
  670. exec->shader_rec_u = temp + shader_rec_offset;
  671. exec->uniforms_u = temp + uniforms_offset;
  672. exec->shader_state = temp + exec_size;
  673. exec->shader_state_size = args->shader_rec_count;
  674. if (copy_from_user(bin,
  675. u64_to_user_ptr(args->bin_cl),
  676. args->bin_cl_size)) {
  677. ret = -EFAULT;
  678. goto fail;
  679. }
  680. if (copy_from_user(exec->shader_rec_u,
  681. u64_to_user_ptr(args->shader_rec),
  682. args->shader_rec_size)) {
  683. ret = -EFAULT;
  684. goto fail;
  685. }
  686. if (copy_from_user(exec->uniforms_u,
  687. u64_to_user_ptr(args->uniforms),
  688. args->uniforms_size)) {
  689. ret = -EFAULT;
  690. goto fail;
  691. }
  692. bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
  693. if (IS_ERR(bo)) {
  694. DRM_ERROR("Couldn't allocate BO for binning\n");
  695. ret = PTR_ERR(bo);
  696. goto fail;
  697. }
  698. exec->exec_bo = &bo->base;
  699. list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
  700. &exec->unref_list);
  701. exec->ct0ca = exec->exec_bo->paddr + bin_offset;
  702. exec->bin_u = bin;
  703. exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
  704. exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
  705. exec->shader_rec_size = args->shader_rec_size;
  706. exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
  707. exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
  708. exec->uniforms_size = args->uniforms_size;
  709. ret = vc4_validate_bin_cl(dev,
  710. exec->exec_bo->vaddr + bin_offset,
  711. bin,
  712. exec);
  713. if (ret)
  714. goto fail;
  715. ret = vc4_validate_shader_recs(dev, exec);
  716. if (ret)
  717. goto fail;
  718. /* Block waiting on any previous rendering into the CS's VBO,
  719. * IB, or textures, so that pixels are actually written by the
  720. * time we try to read them.
  721. */
  722. ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);
  723. fail:
  724. kvfree(temp);
  725. return ret;
  726. }
  727. static void
  728. vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
  729. {
  730. struct vc4_dev *vc4 = to_vc4_dev(dev);
  731. unsigned long irqflags;
  732. unsigned i;
  733. /* If we got force-completed because of GPU reset rather than
  734. * through our IRQ handler, signal the fence now.
  735. */
  736. if (exec->fence) {
  737. dma_fence_signal(exec->fence);
  738. dma_fence_put(exec->fence);
  739. }
  740. if (exec->bo) {
  741. for (i = 0; i < exec->bo_count; i++) {
  742. struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
  743. vc4_bo_dec_usecnt(bo);
  744. drm_gem_object_put_unlocked(&exec->bo[i]->base);
  745. }
  746. kvfree(exec->bo);
  747. }
  748. while (!list_empty(&exec->unref_list)) {
  749. struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  750. struct vc4_bo, unref_head);
  751. list_del(&bo->unref_head);
  752. drm_gem_object_put_unlocked(&bo->base.base);
  753. }
  754. /* Free up the allocation of any bin slots we used. */
  755. spin_lock_irqsave(&vc4->job_lock, irqflags);
  756. vc4->bin_alloc_used &= ~exec->bin_slots;
  757. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  758. mutex_lock(&vc4->power_lock);
  759. if (--vc4->power_refcount == 0) {
  760. pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
  761. pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
  762. }
  763. mutex_unlock(&vc4->power_lock);
  764. kfree(exec);
  765. }
  766. void
  767. vc4_job_handle_completed(struct vc4_dev *vc4)
  768. {
  769. unsigned long irqflags;
  770. struct vc4_seqno_cb *cb, *cb_temp;
  771. spin_lock_irqsave(&vc4->job_lock, irqflags);
  772. while (!list_empty(&vc4->job_done_list)) {
  773. struct vc4_exec_info *exec =
  774. list_first_entry(&vc4->job_done_list,
  775. struct vc4_exec_info, head);
  776. list_del(&exec->head);
  777. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  778. vc4_complete_exec(vc4->dev, exec);
  779. spin_lock_irqsave(&vc4->job_lock, irqflags);
  780. }
  781. list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  782. if (cb->seqno <= vc4->finished_seqno) {
  783. list_del_init(&cb->work.entry);
  784. schedule_work(&cb->work);
  785. }
  786. }
  787. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  788. }
  789. static void vc4_seqno_cb_work(struct work_struct *work)
  790. {
  791. struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  792. cb->func(cb);
  793. }
  794. int vc4_queue_seqno_cb(struct drm_device *dev,
  795. struct vc4_seqno_cb *cb, uint64_t seqno,
  796. void (*func)(struct vc4_seqno_cb *cb))
  797. {
  798. struct vc4_dev *vc4 = to_vc4_dev(dev);
  799. int ret = 0;
  800. unsigned long irqflags;
  801. cb->func = func;
  802. INIT_WORK(&cb->work, vc4_seqno_cb_work);
  803. spin_lock_irqsave(&vc4->job_lock, irqflags);
  804. if (seqno > vc4->finished_seqno) {
  805. cb->seqno = seqno;
  806. list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  807. } else {
  808. schedule_work(&cb->work);
  809. }
  810. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  811. return ret;
  812. }
  813. /* Scheduled when any job has been completed, this walks the list of
  814. * jobs that had completed and unrefs their BOs and frees their exec
  815. * structs.
  816. */
  817. static void
  818. vc4_job_done_work(struct work_struct *work)
  819. {
  820. struct vc4_dev *vc4 =
  821. container_of(work, struct vc4_dev, job_done_work);
  822. vc4_job_handle_completed(vc4);
  823. }
  824. static int
  825. vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  826. uint64_t seqno,
  827. uint64_t *timeout_ns)
  828. {
  829. unsigned long start = jiffies;
  830. int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  831. if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  832. uint64_t delta = jiffies_to_nsecs(jiffies - start);
  833. if (*timeout_ns >= delta)
  834. *timeout_ns -= delta;
  835. }
  836. return ret;
  837. }
  838. int
  839. vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  840. struct drm_file *file_priv)
  841. {
  842. struct drm_vc4_wait_seqno *args = data;
  843. return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  844. &args->timeout_ns);
  845. }
  846. int
  847. vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  848. struct drm_file *file_priv)
  849. {
  850. int ret;
  851. struct drm_vc4_wait_bo *args = data;
  852. struct drm_gem_object *gem_obj;
  853. struct vc4_bo *bo;
  854. if (args->pad != 0)
  855. return -EINVAL;
  856. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  857. if (!gem_obj) {
  858. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  859. return -EINVAL;
  860. }
  861. bo = to_vc4_bo(gem_obj);
  862. ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
  863. &args->timeout_ns);
  864. drm_gem_object_put_unlocked(gem_obj);
  865. return ret;
  866. }
  867. /**
  868. * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
  869. * @dev: DRM device
  870. * @data: ioctl argument
  871. * @file_priv: DRM file for this fd
  872. *
  873. * This is the main entrypoint for userspace to submit a 3D frame to
  874. * the GPU. Userspace provides the binner command list (if
  875. * applicable), and the kernel sets up the render command list to draw
  876. * to the framebuffer described in the ioctl, using the command lists
  877. * that the 3D engine's binner will produce.
  878. */
  879. int
  880. vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  881. struct drm_file *file_priv)
  882. {
  883. struct vc4_dev *vc4 = to_vc4_dev(dev);
  884. struct drm_vc4_submit_cl *args = data;
  885. struct vc4_exec_info *exec;
  886. struct ww_acquire_ctx acquire_ctx;
  887. int ret = 0;
  888. if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
  889. VC4_SUBMIT_CL_FIXED_RCL_ORDER |
  890. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
  891. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
  892. DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
  893. return -EINVAL;
  894. }
  895. exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  896. if (!exec) {
  897. DRM_ERROR("malloc failure on exec struct\n");
  898. return -ENOMEM;
  899. }
  900. mutex_lock(&vc4->power_lock);
  901. if (vc4->power_refcount++ == 0) {
  902. ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  903. if (ret < 0) {
  904. mutex_unlock(&vc4->power_lock);
  905. vc4->power_refcount--;
  906. kfree(exec);
  907. return ret;
  908. }
  909. }
  910. mutex_unlock(&vc4->power_lock);
  911. exec->args = args;
  912. INIT_LIST_HEAD(&exec->unref_list);
  913. ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  914. if (ret)
  915. goto fail;
  916. if (exec->args->bin_cl_size != 0) {
  917. ret = vc4_get_bcl(dev, exec);
  918. if (ret)
  919. goto fail;
  920. } else {
  921. exec->ct0ca = 0;
  922. exec->ct0ea = 0;
  923. }
  924. ret = vc4_get_rcl(dev, exec);
  925. if (ret)
  926. goto fail;
  927. ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
  928. if (ret)
  929. goto fail;
  930. /* Clear this out of the struct we'll be putting in the queue,
  931. * since it's part of our stack.
  932. */
  933. exec->args = NULL;
  934. ret = vc4_queue_submit(dev, exec, &acquire_ctx);
  935. if (ret)
  936. goto fail;
  937. /* Return the seqno for our job. */
  938. args->seqno = vc4->emit_seqno;
  939. return 0;
  940. fail:
  941. vc4_complete_exec(vc4->dev, exec);
  942. return ret;
  943. }
  944. void
  945. vc4_gem_init(struct drm_device *dev)
  946. {
  947. struct vc4_dev *vc4 = to_vc4_dev(dev);
  948. vc4->dma_fence_context = dma_fence_context_alloc(1);
  949. INIT_LIST_HEAD(&vc4->bin_job_list);
  950. INIT_LIST_HEAD(&vc4->render_job_list);
  951. INIT_LIST_HEAD(&vc4->job_done_list);
  952. INIT_LIST_HEAD(&vc4->seqno_cb_list);
  953. spin_lock_init(&vc4->job_lock);
  954. INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  955. timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
  956. INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  957. mutex_init(&vc4->power_lock);
  958. INIT_LIST_HEAD(&vc4->purgeable.list);
  959. mutex_init(&vc4->purgeable.lock);
  960. }
  961. void
  962. vc4_gem_destroy(struct drm_device *dev)
  963. {
  964. struct vc4_dev *vc4 = to_vc4_dev(dev);
  965. /* Waiting for exec to finish would need to be done before
  966. * unregistering V3D.
  967. */
  968. WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  969. /* V3D should already have disabled its interrupt and cleared
  970. * the overflow allocation registers. Now free the object.
  971. */
  972. if (vc4->bin_bo) {
  973. drm_gem_object_put_unlocked(&vc4->bin_bo->base.base);
  974. vc4->bin_bo = NULL;
  975. }
  976. if (vc4->hang_state)
  977. vc4_free_hang_state(dev, vc4->hang_state);
  978. }
  979. int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
  980. struct drm_file *file_priv)
  981. {
  982. struct drm_vc4_gem_madvise *args = data;
  983. struct drm_gem_object *gem_obj;
  984. struct vc4_bo *bo;
  985. int ret;
  986. switch (args->madv) {
  987. case VC4_MADV_DONTNEED:
  988. case VC4_MADV_WILLNEED:
  989. break;
  990. default:
  991. return -EINVAL;
  992. }
  993. if (args->pad != 0)
  994. return -EINVAL;
  995. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  996. if (!gem_obj) {
  997. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  998. return -ENOENT;
  999. }
  1000. bo = to_vc4_bo(gem_obj);
  1001. /* Only BOs exposed to userspace can be purged. */
  1002. if (bo->madv == __VC4_MADV_NOTSUPP) {
  1003. DRM_DEBUG("madvise not supported on this BO\n");
  1004. ret = -EINVAL;
  1005. goto out_put_gem;
  1006. }
  1007. /* Not sure it's safe to purge imported BOs. Let's just assume it's
  1008. * not until proven otherwise.
  1009. */
  1010. if (gem_obj->import_attach) {
  1011. DRM_DEBUG("madvise not supported on imported BOs\n");
  1012. ret = -EINVAL;
  1013. goto out_put_gem;
  1014. }
  1015. mutex_lock(&bo->madv_lock);
  1016. if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED &&
  1017. !refcount_read(&bo->usecnt)) {
  1018. /* If the BO is about to be marked as purgeable, is not used
  1019. * and is not already purgeable or purged, add it to the
  1020. * purgeable list.
  1021. */
  1022. vc4_bo_add_to_purgeable_pool(bo);
  1023. } else if (args->madv == VC4_MADV_WILLNEED &&
  1024. bo->madv == VC4_MADV_DONTNEED &&
  1025. !refcount_read(&bo->usecnt)) {
  1026. /* The BO has not been purged yet, just remove it from
  1027. * the purgeable list.
  1028. */
  1029. vc4_bo_remove_from_purgeable_pool(bo);
  1030. }
  1031. /* Save the purged state. */
  1032. args->retained = bo->madv != __VC4_MADV_PURGED;
  1033. /* Update internal madv state only if the bo was not purged. */
  1034. if (bo->madv != __VC4_MADV_PURGED)
  1035. bo->madv = args->madv;
  1036. mutex_unlock(&bo->madv_lock);
  1037. ret = 0;
  1038. out_put_gem:
  1039. drm_gem_object_put_unlocked(gem_obj);
  1040. return ret;
  1041. }