vc4_gem.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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 <linux/dma-fence-array.h>
  30. #include "uapi/drm/vc4_drm.h"
  31. #include "vc4_drv.h"
  32. #include "vc4_regs.h"
  33. #include "vc4_trace.h"
  34. static void
  35. vc4_queue_hangcheck(struct drm_device *dev)
  36. {
  37. struct vc4_dev *vc4 = to_vc4_dev(dev);
  38. mod_timer(&vc4->hangcheck.timer,
  39. round_jiffies_up(jiffies + msecs_to_jiffies(100)));
  40. }
  41. struct vc4_hang_state {
  42. struct drm_vc4_get_hang_state user_state;
  43. u32 bo_count;
  44. struct drm_gem_object **bo;
  45. };
  46. static void
  47. vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)
  48. {
  49. unsigned int i;
  50. for (i = 0; i < state->user_state.bo_count; i++)
  51. drm_gem_object_put_unlocked(state->bo[i]);
  52. kfree(state);
  53. }
  54. int
  55. vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
  56. struct drm_file *file_priv)
  57. {
  58. struct drm_vc4_get_hang_state *get_state = data;
  59. struct drm_vc4_get_hang_state_bo *bo_state;
  60. struct vc4_hang_state *kernel_state;
  61. struct drm_vc4_get_hang_state *state;
  62. struct vc4_dev *vc4 = to_vc4_dev(dev);
  63. unsigned long irqflags;
  64. u32 i;
  65. int ret = 0;
  66. spin_lock_irqsave(&vc4->job_lock, irqflags);
  67. kernel_state = vc4->hang_state;
  68. if (!kernel_state) {
  69. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  70. return -ENOENT;
  71. }
  72. state = &kernel_state->user_state;
  73. /* If the user's array isn't big enough, just return the
  74. * required array size.
  75. */
  76. if (get_state->bo_count < state->bo_count) {
  77. get_state->bo_count = state->bo_count;
  78. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  79. return 0;
  80. }
  81. vc4->hang_state = NULL;
  82. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  83. /* Save the user's BO pointer, so we don't stomp it with the memcpy. */
  84. state->bo = get_state->bo;
  85. memcpy(get_state, state, sizeof(*state));
  86. bo_state = kcalloc(state->bo_count, sizeof(*bo_state), GFP_KERNEL);
  87. if (!bo_state) {
  88. ret = -ENOMEM;
  89. goto err_free;
  90. }
  91. for (i = 0; i < state->bo_count; i++) {
  92. struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]);
  93. u32 handle;
  94. ret = drm_gem_handle_create(file_priv, kernel_state->bo[i],
  95. &handle);
  96. if (ret) {
  97. state->bo_count = i;
  98. goto err_delete_handle;
  99. }
  100. bo_state[i].handle = handle;
  101. bo_state[i].paddr = vc4_bo->base.paddr;
  102. bo_state[i].size = vc4_bo->base.base.size;
  103. }
  104. if (copy_to_user(u64_to_user_ptr(get_state->bo),
  105. bo_state,
  106. state->bo_count * sizeof(*bo_state)))
  107. ret = -EFAULT;
  108. err_delete_handle:
  109. if (ret) {
  110. for (i = 0; i < state->bo_count; i++)
  111. drm_gem_handle_delete(file_priv, bo_state[i].handle);
  112. }
  113. err_free:
  114. vc4_free_hang_state(dev, kernel_state);
  115. kfree(bo_state);
  116. return ret;
  117. }
  118. static void
  119. vc4_save_hang_state(struct drm_device *dev)
  120. {
  121. struct vc4_dev *vc4 = to_vc4_dev(dev);
  122. struct drm_vc4_get_hang_state *state;
  123. struct vc4_hang_state *kernel_state;
  124. struct vc4_exec_info *exec[2];
  125. struct vc4_bo *bo;
  126. unsigned long irqflags;
  127. unsigned int i, j, k, unref_list_count;
  128. kernel_state = kcalloc(1, sizeof(*kernel_state), GFP_KERNEL);
  129. if (!kernel_state)
  130. return;
  131. state = &kernel_state->user_state;
  132. spin_lock_irqsave(&vc4->job_lock, irqflags);
  133. exec[0] = vc4_first_bin_job(vc4);
  134. exec[1] = vc4_first_render_job(vc4);
  135. if (!exec[0] && !exec[1]) {
  136. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  137. return;
  138. }
  139. /* Get the bos from both binner and renderer into hang state. */
  140. state->bo_count = 0;
  141. for (i = 0; i < 2; i++) {
  142. if (!exec[i])
  143. continue;
  144. unref_list_count = 0;
  145. list_for_each_entry(bo, &exec[i]->unref_list, unref_head)
  146. unref_list_count++;
  147. state->bo_count += exec[i]->bo_count + unref_list_count;
  148. }
  149. kernel_state->bo = kcalloc(state->bo_count,
  150. sizeof(*kernel_state->bo), GFP_ATOMIC);
  151. if (!kernel_state->bo) {
  152. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  153. return;
  154. }
  155. k = 0;
  156. for (i = 0; i < 2; i++) {
  157. if (!exec[i])
  158. continue;
  159. for (j = 0; j < exec[i]->bo_count; j++) {
  160. bo = to_vc4_bo(&exec[i]->bo[j]->base);
  161. /* Retain BOs just in case they were marked purgeable.
  162. * This prevents the BO from being purged before
  163. * someone had a chance to dump the hang state.
  164. */
  165. WARN_ON(!refcount_read(&bo->usecnt));
  166. refcount_inc(&bo->usecnt);
  167. drm_gem_object_get(&exec[i]->bo[j]->base);
  168. kernel_state->bo[k++] = &exec[i]->bo[j]->base;
  169. }
  170. list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
  171. /* No need to retain BOs coming from the ->unref_list
  172. * because they are naturally unpurgeable.
  173. */
  174. drm_gem_object_get(&bo->base.base);
  175. kernel_state->bo[k++] = &bo->base.base;
  176. }
  177. }
  178. WARN_ON_ONCE(k != state->bo_count);
  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. static void
  361. vc4_flush_texture_caches(struct drm_device *dev)
  362. {
  363. struct vc4_dev *vc4 = to_vc4_dev(dev);
  364. V3D_WRITE(V3D_L2CACTL,
  365. V3D_L2CACTL_L2CCLR);
  366. V3D_WRITE(V3D_SLCACTL,
  367. VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  368. VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC));
  369. }
  370. /* Sets the registers for the next job to be actually be executed in
  371. * the hardware.
  372. *
  373. * The job_lock should be held during this.
  374. */
  375. void
  376. vc4_submit_next_bin_job(struct drm_device *dev)
  377. {
  378. struct vc4_dev *vc4 = to_vc4_dev(dev);
  379. struct vc4_exec_info *exec;
  380. again:
  381. exec = vc4_first_bin_job(vc4);
  382. if (!exec)
  383. return;
  384. vc4_flush_caches(dev);
  385. /* Only start the perfmon if it was not already started by a previous
  386. * job.
  387. */
  388. if (exec->perfmon && vc4->active_perfmon != exec->perfmon)
  389. vc4_perfmon_start(vc4, exec->perfmon);
  390. /* Either put the job in the binner if it uses the binner, or
  391. * immediately move it to the to-be-rendered queue.
  392. */
  393. if (exec->ct0ca != exec->ct0ea) {
  394. submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
  395. } else {
  396. struct vc4_exec_info *next;
  397. vc4_move_job_to_render(dev, exec);
  398. next = vc4_first_bin_job(vc4);
  399. /* We can't start the next bin job if the previous job had a
  400. * different perfmon instance attached to it. The same goes
  401. * if one of them had a perfmon attached to it and the other
  402. * one doesn't.
  403. */
  404. if (next && next->perfmon == exec->perfmon)
  405. goto again;
  406. }
  407. }
  408. void
  409. vc4_submit_next_render_job(struct drm_device *dev)
  410. {
  411. struct vc4_dev *vc4 = to_vc4_dev(dev);
  412. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  413. if (!exec)
  414. return;
  415. /* A previous RCL may have written to one of our textures, and
  416. * our full cache flush at bin time may have occurred before
  417. * that RCL completed. Flush the texture cache now, but not
  418. * the instructions or uniforms (since we don't write those
  419. * from an RCL).
  420. */
  421. vc4_flush_texture_caches(dev);
  422. submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
  423. }
  424. void
  425. vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
  426. {
  427. struct vc4_dev *vc4 = to_vc4_dev(dev);
  428. bool was_empty = list_empty(&vc4->render_job_list);
  429. list_move_tail(&exec->head, &vc4->render_job_list);
  430. if (was_empty)
  431. vc4_submit_next_render_job(dev);
  432. }
  433. static void
  434. vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  435. {
  436. struct vc4_bo *bo;
  437. unsigned i;
  438. for (i = 0; i < exec->bo_count; i++) {
  439. bo = to_vc4_bo(&exec->bo[i]->base);
  440. bo->seqno = seqno;
  441. reservation_object_add_shared_fence(bo->resv, exec->fence);
  442. }
  443. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  444. bo->seqno = seqno;
  445. }
  446. for (i = 0; i < exec->rcl_write_bo_count; i++) {
  447. bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
  448. bo->write_seqno = seqno;
  449. reservation_object_add_excl_fence(bo->resv, exec->fence);
  450. }
  451. }
  452. static void
  453. vc4_unlock_bo_reservations(struct drm_device *dev,
  454. struct vc4_exec_info *exec,
  455. struct ww_acquire_ctx *acquire_ctx)
  456. {
  457. int i;
  458. for (i = 0; i < exec->bo_count; i++) {
  459. struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
  460. ww_mutex_unlock(&bo->resv->lock);
  461. }
  462. ww_acquire_fini(acquire_ctx);
  463. }
  464. /* Takes the reservation lock on all the BOs being referenced, so that
  465. * at queue submit time we can update the reservations.
  466. *
  467. * We don't lock the RCL the tile alloc/state BOs, or overflow memory
  468. * (all of which are on exec->unref_list). They're entirely private
  469. * to vc4, so we don't attach dma-buf fences to them.
  470. */
  471. static int
  472. vc4_lock_bo_reservations(struct drm_device *dev,
  473. struct vc4_exec_info *exec,
  474. struct ww_acquire_ctx *acquire_ctx)
  475. {
  476. int contended_lock = -1;
  477. int i, ret;
  478. struct vc4_bo *bo;
  479. ww_acquire_init(acquire_ctx, &reservation_ww_class);
  480. retry:
  481. if (contended_lock != -1) {
  482. bo = to_vc4_bo(&exec->bo[contended_lock]->base);
  483. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  484. acquire_ctx);
  485. if (ret) {
  486. ww_acquire_done(acquire_ctx);
  487. return ret;
  488. }
  489. }
  490. for (i = 0; i < exec->bo_count; i++) {
  491. if (i == contended_lock)
  492. continue;
  493. bo = to_vc4_bo(&exec->bo[i]->base);
  494. ret = ww_mutex_lock_interruptible(&bo->resv->lock, acquire_ctx);
  495. if (ret) {
  496. int j;
  497. for (j = 0; j < i; j++) {
  498. bo = to_vc4_bo(&exec->bo[j]->base);
  499. ww_mutex_unlock(&bo->resv->lock);
  500. }
  501. if (contended_lock != -1 && contended_lock >= i) {
  502. bo = to_vc4_bo(&exec->bo[contended_lock]->base);
  503. ww_mutex_unlock(&bo->resv->lock);
  504. }
  505. if (ret == -EDEADLK) {
  506. contended_lock = i;
  507. goto retry;
  508. }
  509. ww_acquire_done(acquire_ctx);
  510. return ret;
  511. }
  512. }
  513. ww_acquire_done(acquire_ctx);
  514. /* Reserve space for our shared (read-only) fence references,
  515. * before we commit the CL to the hardware.
  516. */
  517. for (i = 0; i < exec->bo_count; i++) {
  518. bo = to_vc4_bo(&exec->bo[i]->base);
  519. ret = reservation_object_reserve_shared(bo->resv);
  520. if (ret) {
  521. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  522. return ret;
  523. }
  524. }
  525. return 0;
  526. }
  527. /* Queues a struct vc4_exec_info for execution. If no job is
  528. * currently executing, then submits it.
  529. *
  530. * Unlike most GPUs, our hardware only handles one command list at a
  531. * time. To queue multiple jobs at once, we'd need to edit the
  532. * previous command list to have a jump to the new one at the end, and
  533. * then bump the end address. That's a change for a later date,
  534. * though.
  535. */
  536. static int
  537. vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
  538. struct ww_acquire_ctx *acquire_ctx,
  539. struct drm_syncobj *out_sync)
  540. {
  541. struct vc4_dev *vc4 = to_vc4_dev(dev);
  542. struct vc4_exec_info *renderjob;
  543. uint64_t seqno;
  544. unsigned long irqflags;
  545. struct vc4_fence *fence;
  546. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  547. if (!fence)
  548. return -ENOMEM;
  549. fence->dev = dev;
  550. spin_lock_irqsave(&vc4->job_lock, irqflags);
  551. seqno = ++vc4->emit_seqno;
  552. exec->seqno = seqno;
  553. dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
  554. vc4->dma_fence_context, exec->seqno);
  555. fence->seqno = exec->seqno;
  556. exec->fence = &fence->base;
  557. if (out_sync)
  558. drm_syncobj_replace_fence(out_sync, exec->fence);
  559. vc4_update_bo_seqnos(exec, seqno);
  560. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  561. list_add_tail(&exec->head, &vc4->bin_job_list);
  562. /* If no bin job was executing and if the render job (if any) has the
  563. * same perfmon as our job attached to it (or if both jobs don't have
  564. * perfmon activated), then kick ours off. Otherwise, it'll get
  565. * started when the previous job's flush/render done interrupt occurs.
  566. */
  567. renderjob = vc4_first_render_job(vc4);
  568. if (vc4_first_bin_job(vc4) == exec &&
  569. (!renderjob || renderjob->perfmon == exec->perfmon)) {
  570. vc4_submit_next_bin_job(dev);
  571. vc4_queue_hangcheck(dev);
  572. }
  573. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  574. return 0;
  575. }
  576. /**
  577. * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
  578. * referenced by the job.
  579. * @dev: DRM device
  580. * @file_priv: DRM file for this fd
  581. * @exec: V3D job being set up
  582. *
  583. * The command validator needs to reference BOs by their index within
  584. * the submitted job's BO list. This does the validation of the job's
  585. * BO list and reference counting for the lifetime of the job.
  586. */
  587. static int
  588. vc4_cl_lookup_bos(struct drm_device *dev,
  589. struct drm_file *file_priv,
  590. struct vc4_exec_info *exec)
  591. {
  592. struct drm_vc4_submit_cl *args = exec->args;
  593. uint32_t *handles;
  594. int ret = 0;
  595. int i;
  596. exec->bo_count = args->bo_handle_count;
  597. if (!exec->bo_count) {
  598. /* See comment on bo_index for why we have to check
  599. * this.
  600. */
  601. DRM_DEBUG("Rendering requires BOs to validate\n");
  602. return -EINVAL;
  603. }
  604. exec->bo = kvmalloc_array(exec->bo_count,
  605. sizeof(struct drm_gem_cma_object *),
  606. GFP_KERNEL | __GFP_ZERO);
  607. if (!exec->bo) {
  608. DRM_ERROR("Failed to allocate validated BO pointers\n");
  609. return -ENOMEM;
  610. }
  611. handles = kvmalloc_array(exec->bo_count, sizeof(uint32_t), GFP_KERNEL);
  612. if (!handles) {
  613. ret = -ENOMEM;
  614. DRM_ERROR("Failed to allocate incoming GEM handles\n");
  615. goto fail;
  616. }
  617. if (copy_from_user(handles, u64_to_user_ptr(args->bo_handles),
  618. exec->bo_count * sizeof(uint32_t))) {
  619. ret = -EFAULT;
  620. DRM_ERROR("Failed to copy in GEM handles\n");
  621. goto fail;
  622. }
  623. spin_lock(&file_priv->table_lock);
  624. for (i = 0; i < exec->bo_count; i++) {
  625. struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
  626. handles[i]);
  627. if (!bo) {
  628. DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
  629. i, handles[i]);
  630. ret = -EINVAL;
  631. break;
  632. }
  633. drm_gem_object_get(bo);
  634. exec->bo[i] = (struct drm_gem_cma_object *)bo;
  635. }
  636. spin_unlock(&file_priv->table_lock);
  637. if (ret)
  638. goto fail_put_bo;
  639. for (i = 0; i < exec->bo_count; i++) {
  640. ret = vc4_bo_inc_usecnt(to_vc4_bo(&exec->bo[i]->base));
  641. if (ret)
  642. goto fail_dec_usecnt;
  643. }
  644. kvfree(handles);
  645. return 0;
  646. fail_dec_usecnt:
  647. /* Decrease usecnt on acquired objects.
  648. * We cannot rely on vc4_complete_exec() to release resources here,
  649. * because vc4_complete_exec() has no information about which BO has
  650. * had its ->usecnt incremented.
  651. * To make things easier we just free everything explicitly and set
  652. * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
  653. * step.
  654. */
  655. for (i-- ; i >= 0; i--)
  656. vc4_bo_dec_usecnt(to_vc4_bo(&exec->bo[i]->base));
  657. fail_put_bo:
  658. /* Release any reference to acquired objects. */
  659. for (i = 0; i < exec->bo_count && exec->bo[i]; i++)
  660. drm_gem_object_put_unlocked(&exec->bo[i]->base);
  661. fail:
  662. kvfree(handles);
  663. kvfree(exec->bo);
  664. exec->bo = NULL;
  665. return ret;
  666. }
  667. static int
  668. vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  669. {
  670. struct drm_vc4_submit_cl *args = exec->args;
  671. void *temp = NULL;
  672. void *bin;
  673. int ret = 0;
  674. uint32_t bin_offset = 0;
  675. uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
  676. 16);
  677. uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
  678. uint32_t exec_size = uniforms_offset + args->uniforms_size;
  679. uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
  680. args->shader_rec_count);
  681. struct vc4_bo *bo;
  682. if (shader_rec_offset < args->bin_cl_size ||
  683. uniforms_offset < shader_rec_offset ||
  684. exec_size < uniforms_offset ||
  685. args->shader_rec_count >= (UINT_MAX /
  686. sizeof(struct vc4_shader_state)) ||
  687. temp_size < exec_size) {
  688. DRM_DEBUG("overflow in exec arguments\n");
  689. ret = -EINVAL;
  690. goto fail;
  691. }
  692. /* Allocate space where we'll store the copied in user command lists
  693. * and shader records.
  694. *
  695. * We don't just copy directly into the BOs because we need to
  696. * read the contents back for validation, and I think the
  697. * bo->vaddr is uncached access.
  698. */
  699. temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
  700. if (!temp) {
  701. DRM_ERROR("Failed to allocate storage for copying "
  702. "in bin/render CLs.\n");
  703. ret = -ENOMEM;
  704. goto fail;
  705. }
  706. bin = temp + bin_offset;
  707. exec->shader_rec_u = temp + shader_rec_offset;
  708. exec->uniforms_u = temp + uniforms_offset;
  709. exec->shader_state = temp + exec_size;
  710. exec->shader_state_size = args->shader_rec_count;
  711. if (copy_from_user(bin,
  712. u64_to_user_ptr(args->bin_cl),
  713. args->bin_cl_size)) {
  714. ret = -EFAULT;
  715. goto fail;
  716. }
  717. if (copy_from_user(exec->shader_rec_u,
  718. u64_to_user_ptr(args->shader_rec),
  719. args->shader_rec_size)) {
  720. ret = -EFAULT;
  721. goto fail;
  722. }
  723. if (copy_from_user(exec->uniforms_u,
  724. u64_to_user_ptr(args->uniforms),
  725. args->uniforms_size)) {
  726. ret = -EFAULT;
  727. goto fail;
  728. }
  729. bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
  730. if (IS_ERR(bo)) {
  731. DRM_ERROR("Couldn't allocate BO for binning\n");
  732. ret = PTR_ERR(bo);
  733. goto fail;
  734. }
  735. exec->exec_bo = &bo->base;
  736. list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
  737. &exec->unref_list);
  738. exec->ct0ca = exec->exec_bo->paddr + bin_offset;
  739. exec->bin_u = bin;
  740. exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
  741. exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
  742. exec->shader_rec_size = args->shader_rec_size;
  743. exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
  744. exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
  745. exec->uniforms_size = args->uniforms_size;
  746. ret = vc4_validate_bin_cl(dev,
  747. exec->exec_bo->vaddr + bin_offset,
  748. bin,
  749. exec);
  750. if (ret)
  751. goto fail;
  752. ret = vc4_validate_shader_recs(dev, exec);
  753. if (ret)
  754. goto fail;
  755. /* Block waiting on any previous rendering into the CS's VBO,
  756. * IB, or textures, so that pixels are actually written by the
  757. * time we try to read them.
  758. */
  759. ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);
  760. fail:
  761. kvfree(temp);
  762. return ret;
  763. }
  764. static void
  765. vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
  766. {
  767. struct vc4_dev *vc4 = to_vc4_dev(dev);
  768. unsigned long irqflags;
  769. unsigned i;
  770. /* If we got force-completed because of GPU reset rather than
  771. * through our IRQ handler, signal the fence now.
  772. */
  773. if (exec->fence) {
  774. dma_fence_signal(exec->fence);
  775. dma_fence_put(exec->fence);
  776. }
  777. if (exec->bo) {
  778. for (i = 0; i < exec->bo_count; i++) {
  779. struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
  780. vc4_bo_dec_usecnt(bo);
  781. drm_gem_object_put_unlocked(&exec->bo[i]->base);
  782. }
  783. kvfree(exec->bo);
  784. }
  785. while (!list_empty(&exec->unref_list)) {
  786. struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  787. struct vc4_bo, unref_head);
  788. list_del(&bo->unref_head);
  789. drm_gem_object_put_unlocked(&bo->base.base);
  790. }
  791. /* Free up the allocation of any bin slots we used. */
  792. spin_lock_irqsave(&vc4->job_lock, irqflags);
  793. vc4->bin_alloc_used &= ~exec->bin_slots;
  794. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  795. /* Release the reference we had on the perf monitor. */
  796. vc4_perfmon_put(exec->perfmon);
  797. mutex_lock(&vc4->power_lock);
  798. if (--vc4->power_refcount == 0) {
  799. pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
  800. pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
  801. }
  802. mutex_unlock(&vc4->power_lock);
  803. kfree(exec);
  804. }
  805. void
  806. vc4_job_handle_completed(struct vc4_dev *vc4)
  807. {
  808. unsigned long irqflags;
  809. struct vc4_seqno_cb *cb, *cb_temp;
  810. spin_lock_irqsave(&vc4->job_lock, irqflags);
  811. while (!list_empty(&vc4->job_done_list)) {
  812. struct vc4_exec_info *exec =
  813. list_first_entry(&vc4->job_done_list,
  814. struct vc4_exec_info, head);
  815. list_del(&exec->head);
  816. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  817. vc4_complete_exec(vc4->dev, exec);
  818. spin_lock_irqsave(&vc4->job_lock, irqflags);
  819. }
  820. list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  821. if (cb->seqno <= vc4->finished_seqno) {
  822. list_del_init(&cb->work.entry);
  823. schedule_work(&cb->work);
  824. }
  825. }
  826. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  827. }
  828. static void vc4_seqno_cb_work(struct work_struct *work)
  829. {
  830. struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  831. cb->func(cb);
  832. }
  833. int vc4_queue_seqno_cb(struct drm_device *dev,
  834. struct vc4_seqno_cb *cb, uint64_t seqno,
  835. void (*func)(struct vc4_seqno_cb *cb))
  836. {
  837. struct vc4_dev *vc4 = to_vc4_dev(dev);
  838. int ret = 0;
  839. unsigned long irqflags;
  840. cb->func = func;
  841. INIT_WORK(&cb->work, vc4_seqno_cb_work);
  842. spin_lock_irqsave(&vc4->job_lock, irqflags);
  843. if (seqno > vc4->finished_seqno) {
  844. cb->seqno = seqno;
  845. list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  846. } else {
  847. schedule_work(&cb->work);
  848. }
  849. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  850. return ret;
  851. }
  852. /* Scheduled when any job has been completed, this walks the list of
  853. * jobs that had completed and unrefs their BOs and frees their exec
  854. * structs.
  855. */
  856. static void
  857. vc4_job_done_work(struct work_struct *work)
  858. {
  859. struct vc4_dev *vc4 =
  860. container_of(work, struct vc4_dev, job_done_work);
  861. vc4_job_handle_completed(vc4);
  862. }
  863. static int
  864. vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  865. uint64_t seqno,
  866. uint64_t *timeout_ns)
  867. {
  868. unsigned long start = jiffies;
  869. int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  870. if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  871. uint64_t delta = jiffies_to_nsecs(jiffies - start);
  872. if (*timeout_ns >= delta)
  873. *timeout_ns -= delta;
  874. }
  875. return ret;
  876. }
  877. int
  878. vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  879. struct drm_file *file_priv)
  880. {
  881. struct drm_vc4_wait_seqno *args = data;
  882. return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  883. &args->timeout_ns);
  884. }
  885. int
  886. vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  887. struct drm_file *file_priv)
  888. {
  889. int ret;
  890. struct drm_vc4_wait_bo *args = data;
  891. struct drm_gem_object *gem_obj;
  892. struct vc4_bo *bo;
  893. if (args->pad != 0)
  894. return -EINVAL;
  895. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  896. if (!gem_obj) {
  897. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  898. return -EINVAL;
  899. }
  900. bo = to_vc4_bo(gem_obj);
  901. ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
  902. &args->timeout_ns);
  903. drm_gem_object_put_unlocked(gem_obj);
  904. return ret;
  905. }
  906. /**
  907. * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
  908. * @dev: DRM device
  909. * @data: ioctl argument
  910. * @file_priv: DRM file for this fd
  911. *
  912. * This is the main entrypoint for userspace to submit a 3D frame to
  913. * the GPU. Userspace provides the binner command list (if
  914. * applicable), and the kernel sets up the render command list to draw
  915. * to the framebuffer described in the ioctl, using the command lists
  916. * that the 3D engine's binner will produce.
  917. */
  918. int
  919. vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  920. struct drm_file *file_priv)
  921. {
  922. struct vc4_dev *vc4 = to_vc4_dev(dev);
  923. struct vc4_file *vc4file = file_priv->driver_priv;
  924. struct drm_vc4_submit_cl *args = data;
  925. struct drm_syncobj *out_sync = NULL;
  926. struct vc4_exec_info *exec;
  927. struct ww_acquire_ctx acquire_ctx;
  928. struct dma_fence *in_fence;
  929. int ret = 0;
  930. if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
  931. VC4_SUBMIT_CL_FIXED_RCL_ORDER |
  932. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
  933. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
  934. DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
  935. return -EINVAL;
  936. }
  937. if (args->pad2 != 0) {
  938. DRM_DEBUG("Invalid pad: 0x%08x\n", args->pad2);
  939. return -EINVAL;
  940. }
  941. exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  942. if (!exec) {
  943. DRM_ERROR("malloc failure on exec struct\n");
  944. return -ENOMEM;
  945. }
  946. mutex_lock(&vc4->power_lock);
  947. if (vc4->power_refcount++ == 0) {
  948. ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  949. if (ret < 0) {
  950. mutex_unlock(&vc4->power_lock);
  951. vc4->power_refcount--;
  952. kfree(exec);
  953. return ret;
  954. }
  955. }
  956. mutex_unlock(&vc4->power_lock);
  957. exec->args = args;
  958. INIT_LIST_HEAD(&exec->unref_list);
  959. ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  960. if (ret)
  961. goto fail;
  962. if (args->perfmonid) {
  963. exec->perfmon = vc4_perfmon_find(vc4file,
  964. args->perfmonid);
  965. if (!exec->perfmon) {
  966. ret = -ENOENT;
  967. goto fail;
  968. }
  969. }
  970. if (args->in_sync) {
  971. ret = drm_syncobj_find_fence(file_priv, args->in_sync,
  972. &in_fence);
  973. if (ret)
  974. goto fail;
  975. /* When the fence (or fence array) is exclusively from our
  976. * context we can skip the wait since jobs are executed in
  977. * order of their submission through this ioctl and this can
  978. * only have fences from a prior job.
  979. */
  980. if (!dma_fence_match_context(in_fence,
  981. vc4->dma_fence_context)) {
  982. ret = dma_fence_wait(in_fence, true);
  983. if (ret) {
  984. dma_fence_put(in_fence);
  985. goto fail;
  986. }
  987. }
  988. dma_fence_put(in_fence);
  989. }
  990. if (exec->args->bin_cl_size != 0) {
  991. ret = vc4_get_bcl(dev, exec);
  992. if (ret)
  993. goto fail;
  994. } else {
  995. exec->ct0ca = 0;
  996. exec->ct0ea = 0;
  997. }
  998. ret = vc4_get_rcl(dev, exec);
  999. if (ret)
  1000. goto fail;
  1001. ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
  1002. if (ret)
  1003. goto fail;
  1004. if (args->out_sync) {
  1005. out_sync = drm_syncobj_find(file_priv, args->out_sync);
  1006. if (!out_sync) {
  1007. ret = -EINVAL;
  1008. goto fail;
  1009. }
  1010. /* We replace the fence in out_sync in vc4_queue_submit since
  1011. * the render job could execute immediately after that call.
  1012. * If it finishes before our ioctl processing resumes the
  1013. * render job fence could already have been freed.
  1014. */
  1015. }
  1016. /* Clear this out of the struct we'll be putting in the queue,
  1017. * since it's part of our stack.
  1018. */
  1019. exec->args = NULL;
  1020. ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
  1021. /* The syncobj isn't part of the exec data and we need to free our
  1022. * reference even if job submission failed.
  1023. */
  1024. if (out_sync)
  1025. drm_syncobj_put(out_sync);
  1026. if (ret)
  1027. goto fail;
  1028. /* Return the seqno for our job. */
  1029. args->seqno = vc4->emit_seqno;
  1030. return 0;
  1031. fail:
  1032. vc4_complete_exec(vc4->dev, exec);
  1033. return ret;
  1034. }
  1035. void
  1036. vc4_gem_init(struct drm_device *dev)
  1037. {
  1038. struct vc4_dev *vc4 = to_vc4_dev(dev);
  1039. vc4->dma_fence_context = dma_fence_context_alloc(1);
  1040. INIT_LIST_HEAD(&vc4->bin_job_list);
  1041. INIT_LIST_HEAD(&vc4->render_job_list);
  1042. INIT_LIST_HEAD(&vc4->job_done_list);
  1043. INIT_LIST_HEAD(&vc4->seqno_cb_list);
  1044. spin_lock_init(&vc4->job_lock);
  1045. INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  1046. timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
  1047. INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  1048. mutex_init(&vc4->power_lock);
  1049. INIT_LIST_HEAD(&vc4->purgeable.list);
  1050. mutex_init(&vc4->purgeable.lock);
  1051. }
  1052. void
  1053. vc4_gem_destroy(struct drm_device *dev)
  1054. {
  1055. struct vc4_dev *vc4 = to_vc4_dev(dev);
  1056. /* Waiting for exec to finish would need to be done before
  1057. * unregistering V3D.
  1058. */
  1059. WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  1060. /* V3D should already have disabled its interrupt and cleared
  1061. * the overflow allocation registers. Now free the object.
  1062. */
  1063. if (vc4->bin_bo) {
  1064. drm_gem_object_put_unlocked(&vc4->bin_bo->base.base);
  1065. vc4->bin_bo = NULL;
  1066. }
  1067. if (vc4->hang_state)
  1068. vc4_free_hang_state(dev, vc4->hang_state);
  1069. }
  1070. int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
  1071. struct drm_file *file_priv)
  1072. {
  1073. struct drm_vc4_gem_madvise *args = data;
  1074. struct drm_gem_object *gem_obj;
  1075. struct vc4_bo *bo;
  1076. int ret;
  1077. switch (args->madv) {
  1078. case VC4_MADV_DONTNEED:
  1079. case VC4_MADV_WILLNEED:
  1080. break;
  1081. default:
  1082. return -EINVAL;
  1083. }
  1084. if (args->pad != 0)
  1085. return -EINVAL;
  1086. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  1087. if (!gem_obj) {
  1088. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  1089. return -ENOENT;
  1090. }
  1091. bo = to_vc4_bo(gem_obj);
  1092. /* Only BOs exposed to userspace can be purged. */
  1093. if (bo->madv == __VC4_MADV_NOTSUPP) {
  1094. DRM_DEBUG("madvise not supported on this BO\n");
  1095. ret = -EINVAL;
  1096. goto out_put_gem;
  1097. }
  1098. /* Not sure it's safe to purge imported BOs. Let's just assume it's
  1099. * not until proven otherwise.
  1100. */
  1101. if (gem_obj->import_attach) {
  1102. DRM_DEBUG("madvise not supported on imported BOs\n");
  1103. ret = -EINVAL;
  1104. goto out_put_gem;
  1105. }
  1106. mutex_lock(&bo->madv_lock);
  1107. if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED &&
  1108. !refcount_read(&bo->usecnt)) {
  1109. /* If the BO is about to be marked as purgeable, is not used
  1110. * and is not already purgeable or purged, add it to the
  1111. * purgeable list.
  1112. */
  1113. vc4_bo_add_to_purgeable_pool(bo);
  1114. } else if (args->madv == VC4_MADV_WILLNEED &&
  1115. bo->madv == VC4_MADV_DONTNEED &&
  1116. !refcount_read(&bo->usecnt)) {
  1117. /* The BO has not been purged yet, just remove it from
  1118. * the purgeable list.
  1119. */
  1120. vc4_bo_remove_from_purgeable_pool(bo);
  1121. }
  1122. /* Save the purged state. */
  1123. args->retained = bo->madv != __VC4_MADV_PURGED;
  1124. /* Update internal madv state only if the bo was not purged. */
  1125. if (bo->madv != __VC4_MADV_PURGED)
  1126. bo->madv = args->madv;
  1127. mutex_unlock(&bo->madv_lock);
  1128. ret = 0;
  1129. out_put_gem:
  1130. drm_gem_object_put_unlocked(gem_obj);
  1131. return ret;
  1132. }