vc4_gem.c 35 KB

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