vc4_gem.c 27 KB

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