vc4_gem.c 27 KB

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