vc4_gem.c 33 KB

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