vc4_gem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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/device.h>
  26. #include <linux/io.h>
  27. #include "uapi/drm/vc4_drm.h"
  28. #include "vc4_drv.h"
  29. #include "vc4_regs.h"
  30. #include "vc4_trace.h"
  31. static void
  32. vc4_queue_hangcheck(struct drm_device *dev)
  33. {
  34. struct vc4_dev *vc4 = to_vc4_dev(dev);
  35. mod_timer(&vc4->hangcheck.timer,
  36. round_jiffies_up(jiffies + msecs_to_jiffies(100)));
  37. }
  38. struct vc4_hang_state {
  39. struct drm_vc4_get_hang_state user_state;
  40. u32 bo_count;
  41. struct drm_gem_object **bo;
  42. };
  43. static void
  44. vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)
  45. {
  46. unsigned int i;
  47. mutex_lock(&dev->struct_mutex);
  48. for (i = 0; i < state->user_state.bo_count; i++)
  49. drm_gem_object_unreference(state->bo[i]);
  50. mutex_unlock(&dev->struct_mutex);
  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;
  120. struct vc4_bo *bo;
  121. unsigned long irqflags;
  122. unsigned int i, unref_list_count;
  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 = vc4_first_job(vc4);
  129. if (!exec) {
  130. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  131. return;
  132. }
  133. unref_list_count = 0;
  134. list_for_each_entry(bo, &exec->unref_list, unref_head)
  135. unref_list_count++;
  136. state->bo_count = exec->bo_count + unref_list_count;
  137. kernel_state->bo = kcalloc(state->bo_count, sizeof(*kernel_state->bo),
  138. GFP_ATOMIC);
  139. if (!kernel_state->bo) {
  140. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  141. return;
  142. }
  143. for (i = 0; i < exec->bo_count; i++) {
  144. drm_gem_object_reference(&exec->bo[i]->base);
  145. kernel_state->bo[i] = &exec->bo[i]->base;
  146. }
  147. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  148. drm_gem_object_reference(&bo->base.base);
  149. kernel_state->bo[i] = &bo->base.base;
  150. i++;
  151. }
  152. state->start_bin = exec->ct0ca;
  153. state->start_render = exec->ct1ca;
  154. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  155. state->ct0ca = V3D_READ(V3D_CTNCA(0));
  156. state->ct0ea = V3D_READ(V3D_CTNEA(0));
  157. state->ct1ca = V3D_READ(V3D_CTNCA(1));
  158. state->ct1ea = V3D_READ(V3D_CTNEA(1));
  159. state->ct0cs = V3D_READ(V3D_CTNCS(0));
  160. state->ct1cs = V3D_READ(V3D_CTNCS(1));
  161. state->ct0ra0 = V3D_READ(V3D_CT00RA0);
  162. state->ct1ra0 = V3D_READ(V3D_CT01RA0);
  163. state->bpca = V3D_READ(V3D_BPCA);
  164. state->bpcs = V3D_READ(V3D_BPCS);
  165. state->bpoa = V3D_READ(V3D_BPOA);
  166. state->bpos = V3D_READ(V3D_BPOS);
  167. state->vpmbase = V3D_READ(V3D_VPMBASE);
  168. state->dbge = V3D_READ(V3D_DBGE);
  169. state->fdbgo = V3D_READ(V3D_FDBGO);
  170. state->fdbgb = V3D_READ(V3D_FDBGB);
  171. state->fdbgr = V3D_READ(V3D_FDBGR);
  172. state->fdbgs = V3D_READ(V3D_FDBGS);
  173. state->errstat = V3D_READ(V3D_ERRSTAT);
  174. spin_lock_irqsave(&vc4->job_lock, irqflags);
  175. if (vc4->hang_state) {
  176. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  177. vc4_free_hang_state(dev, kernel_state);
  178. } else {
  179. vc4->hang_state = kernel_state;
  180. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  181. }
  182. }
  183. static void
  184. vc4_reset(struct drm_device *dev)
  185. {
  186. struct vc4_dev *vc4 = to_vc4_dev(dev);
  187. DRM_INFO("Resetting GPU.\n");
  188. vc4_v3d_set_power(vc4, false);
  189. vc4_v3d_set_power(vc4, true);
  190. vc4_irq_reset(dev);
  191. /* Rearm the hangcheck -- another job might have been waiting
  192. * for our hung one to get kicked off, and vc4_irq_reset()
  193. * would have started it.
  194. */
  195. vc4_queue_hangcheck(dev);
  196. }
  197. static void
  198. vc4_reset_work(struct work_struct *work)
  199. {
  200. struct vc4_dev *vc4 =
  201. container_of(work, struct vc4_dev, hangcheck.reset_work);
  202. vc4_save_hang_state(vc4->dev);
  203. vc4_reset(vc4->dev);
  204. }
  205. static void
  206. vc4_hangcheck_elapsed(unsigned long data)
  207. {
  208. struct drm_device *dev = (struct drm_device *)data;
  209. struct vc4_dev *vc4 = to_vc4_dev(dev);
  210. uint32_t ct0ca, ct1ca;
  211. unsigned long irqflags;
  212. struct vc4_exec_info *exec;
  213. spin_lock_irqsave(&vc4->job_lock, irqflags);
  214. exec = vc4_first_job(vc4);
  215. /* If idle, we can stop watching for hangs. */
  216. if (!exec) {
  217. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  218. return;
  219. }
  220. ct0ca = V3D_READ(V3D_CTNCA(0));
  221. ct1ca = V3D_READ(V3D_CTNCA(1));
  222. /* If we've made any progress in execution, rearm the timer
  223. * and wait.
  224. */
  225. if (ct0ca != exec->last_ct0ca || ct1ca != exec->last_ct1ca) {
  226. exec->last_ct0ca = ct0ca;
  227. exec->last_ct1ca = ct1ca;
  228. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  229. vc4_queue_hangcheck(dev);
  230. return;
  231. }
  232. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  233. /* We've gone too long with no progress, reset. This has to
  234. * be done from a work struct, since resetting can sleep and
  235. * this timer hook isn't allowed to.
  236. */
  237. schedule_work(&vc4->hangcheck.reset_work);
  238. }
  239. static void
  240. submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
  241. {
  242. struct vc4_dev *vc4 = to_vc4_dev(dev);
  243. /* Set the current and end address of the control list.
  244. * Writing the end register is what starts the job.
  245. */
  246. V3D_WRITE(V3D_CTNCA(thread), start);
  247. V3D_WRITE(V3D_CTNEA(thread), end);
  248. }
  249. int
  250. vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
  251. bool interruptible)
  252. {
  253. struct vc4_dev *vc4 = to_vc4_dev(dev);
  254. int ret = 0;
  255. unsigned long timeout_expire;
  256. DEFINE_WAIT(wait);
  257. if (vc4->finished_seqno >= seqno)
  258. return 0;
  259. if (timeout_ns == 0)
  260. return -ETIME;
  261. timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
  262. trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
  263. for (;;) {
  264. prepare_to_wait(&vc4->job_wait_queue, &wait,
  265. interruptible ? TASK_INTERRUPTIBLE :
  266. TASK_UNINTERRUPTIBLE);
  267. if (interruptible && signal_pending(current)) {
  268. ret = -ERESTARTSYS;
  269. break;
  270. }
  271. if (vc4->finished_seqno >= seqno)
  272. break;
  273. if (timeout_ns != ~0ull) {
  274. if (time_after_eq(jiffies, timeout_expire)) {
  275. ret = -ETIME;
  276. break;
  277. }
  278. schedule_timeout(timeout_expire - jiffies);
  279. } else {
  280. schedule();
  281. }
  282. }
  283. finish_wait(&vc4->job_wait_queue, &wait);
  284. trace_vc4_wait_for_seqno_end(dev, seqno);
  285. return ret;
  286. }
  287. static void
  288. vc4_flush_caches(struct drm_device *dev)
  289. {
  290. struct vc4_dev *vc4 = to_vc4_dev(dev);
  291. /* Flush the GPU L2 caches. These caches sit on top of system
  292. * L3 (the 128kb or so shared with the CPU), and are
  293. * non-allocating in the L3.
  294. */
  295. V3D_WRITE(V3D_L2CACTL,
  296. V3D_L2CACTL_L2CCLR);
  297. V3D_WRITE(V3D_SLCACTL,
  298. VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  299. VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
  300. VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
  301. VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
  302. }
  303. /* Sets the registers for the next job to be actually be executed in
  304. * the hardware.
  305. *
  306. * The job_lock should be held during this.
  307. */
  308. void
  309. vc4_submit_next_job(struct drm_device *dev)
  310. {
  311. struct vc4_dev *vc4 = to_vc4_dev(dev);
  312. struct vc4_exec_info *exec = vc4_first_job(vc4);
  313. if (!exec)
  314. return;
  315. vc4_flush_caches(dev);
  316. /* Disable the binner's pre-loaded overflow memory address */
  317. V3D_WRITE(V3D_BPOA, 0);
  318. V3D_WRITE(V3D_BPOS, 0);
  319. if (exec->ct0ca != exec->ct0ea)
  320. submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
  321. submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
  322. }
  323. static void
  324. vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  325. {
  326. struct vc4_bo *bo;
  327. unsigned i;
  328. for (i = 0; i < exec->bo_count; i++) {
  329. bo = to_vc4_bo(&exec->bo[i]->base);
  330. bo->seqno = seqno;
  331. }
  332. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  333. bo->seqno = seqno;
  334. }
  335. }
  336. /* Queues a struct vc4_exec_info for execution. If no job is
  337. * currently executing, then submits it.
  338. *
  339. * Unlike most GPUs, our hardware only handles one command list at a
  340. * time. To queue multiple jobs at once, we'd need to edit the
  341. * previous command list to have a jump to the new one at the end, and
  342. * then bump the end address. That's a change for a later date,
  343. * though.
  344. */
  345. static void
  346. vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec)
  347. {
  348. struct vc4_dev *vc4 = to_vc4_dev(dev);
  349. uint64_t seqno;
  350. unsigned long irqflags;
  351. spin_lock_irqsave(&vc4->job_lock, irqflags);
  352. seqno = ++vc4->emit_seqno;
  353. exec->seqno = seqno;
  354. vc4_update_bo_seqnos(exec, seqno);
  355. list_add_tail(&exec->head, &vc4->job_list);
  356. /* If no job was executing, kick ours off. Otherwise, it'll
  357. * get started when the previous job's frame done interrupt
  358. * occurs.
  359. */
  360. if (vc4_first_job(vc4) == exec) {
  361. vc4_submit_next_job(dev);
  362. vc4_queue_hangcheck(dev);
  363. }
  364. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  365. }
  366. /**
  367. * Looks up a bunch of GEM handles for BOs and stores the array for
  368. * use in the command validator that actually writes relocated
  369. * addresses pointing to them.
  370. */
  371. static int
  372. vc4_cl_lookup_bos(struct drm_device *dev,
  373. struct drm_file *file_priv,
  374. struct vc4_exec_info *exec)
  375. {
  376. struct drm_vc4_submit_cl *args = exec->args;
  377. uint32_t *handles;
  378. int ret = 0;
  379. int i;
  380. exec->bo_count = args->bo_handle_count;
  381. if (!exec->bo_count) {
  382. /* See comment on bo_index for why we have to check
  383. * this.
  384. */
  385. DRM_ERROR("Rendering requires BOs to validate\n");
  386. return -EINVAL;
  387. }
  388. exec->bo = kcalloc(exec->bo_count, sizeof(struct drm_gem_cma_object *),
  389. GFP_KERNEL);
  390. if (!exec->bo) {
  391. DRM_ERROR("Failed to allocate validated BO pointers\n");
  392. return -ENOMEM;
  393. }
  394. handles = drm_malloc_ab(exec->bo_count, sizeof(uint32_t));
  395. if (!handles) {
  396. DRM_ERROR("Failed to allocate incoming GEM handles\n");
  397. goto fail;
  398. }
  399. ret = copy_from_user(handles,
  400. (void __user *)(uintptr_t)args->bo_handles,
  401. exec->bo_count * sizeof(uint32_t));
  402. if (ret) {
  403. DRM_ERROR("Failed to copy in GEM handles\n");
  404. goto fail;
  405. }
  406. spin_lock(&file_priv->table_lock);
  407. for (i = 0; i < exec->bo_count; i++) {
  408. struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
  409. handles[i]);
  410. if (!bo) {
  411. DRM_ERROR("Failed to look up GEM BO %d: %d\n",
  412. i, handles[i]);
  413. ret = -EINVAL;
  414. spin_unlock(&file_priv->table_lock);
  415. goto fail;
  416. }
  417. drm_gem_object_reference(bo);
  418. exec->bo[i] = (struct drm_gem_cma_object *)bo;
  419. }
  420. spin_unlock(&file_priv->table_lock);
  421. fail:
  422. kfree(handles);
  423. return 0;
  424. }
  425. static int
  426. vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  427. {
  428. struct drm_vc4_submit_cl *args = exec->args;
  429. void *temp = NULL;
  430. void *bin;
  431. int ret = 0;
  432. uint32_t bin_offset = 0;
  433. uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
  434. 16);
  435. uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
  436. uint32_t exec_size = uniforms_offset + args->uniforms_size;
  437. uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
  438. args->shader_rec_count);
  439. struct vc4_bo *bo;
  440. if (uniforms_offset < shader_rec_offset ||
  441. exec_size < uniforms_offset ||
  442. args->shader_rec_count >= (UINT_MAX /
  443. sizeof(struct vc4_shader_state)) ||
  444. temp_size < exec_size) {
  445. DRM_ERROR("overflow in exec arguments\n");
  446. goto fail;
  447. }
  448. /* Allocate space where we'll store the copied in user command lists
  449. * and shader records.
  450. *
  451. * We don't just copy directly into the BOs because we need to
  452. * read the contents back for validation, and I think the
  453. * bo->vaddr is uncached access.
  454. */
  455. temp = kmalloc(temp_size, GFP_KERNEL);
  456. if (!temp) {
  457. DRM_ERROR("Failed to allocate storage for copying "
  458. "in bin/render CLs.\n");
  459. ret = -ENOMEM;
  460. goto fail;
  461. }
  462. bin = temp + bin_offset;
  463. exec->shader_rec_u = temp + shader_rec_offset;
  464. exec->uniforms_u = temp + uniforms_offset;
  465. exec->shader_state = temp + exec_size;
  466. exec->shader_state_size = args->shader_rec_count;
  467. if (copy_from_user(bin,
  468. (void __user *)(uintptr_t)args->bin_cl,
  469. args->bin_cl_size)) {
  470. ret = -EFAULT;
  471. goto fail;
  472. }
  473. if (copy_from_user(exec->shader_rec_u,
  474. (void __user *)(uintptr_t)args->shader_rec,
  475. args->shader_rec_size)) {
  476. ret = -EFAULT;
  477. goto fail;
  478. }
  479. if (copy_from_user(exec->uniforms_u,
  480. (void __user *)(uintptr_t)args->uniforms,
  481. args->uniforms_size)) {
  482. ret = -EFAULT;
  483. goto fail;
  484. }
  485. bo = vc4_bo_create(dev, exec_size, true);
  486. if (IS_ERR(bo)) {
  487. DRM_ERROR("Couldn't allocate BO for binning\n");
  488. ret = PTR_ERR(bo);
  489. goto fail;
  490. }
  491. exec->exec_bo = &bo->base;
  492. list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
  493. &exec->unref_list);
  494. exec->ct0ca = exec->exec_bo->paddr + bin_offset;
  495. exec->bin_u = bin;
  496. exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
  497. exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
  498. exec->shader_rec_size = args->shader_rec_size;
  499. exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
  500. exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
  501. exec->uniforms_size = args->uniforms_size;
  502. ret = vc4_validate_bin_cl(dev,
  503. exec->exec_bo->vaddr + bin_offset,
  504. bin,
  505. exec);
  506. if (ret)
  507. goto fail;
  508. ret = vc4_validate_shader_recs(dev, exec);
  509. fail:
  510. kfree(temp);
  511. return ret;
  512. }
  513. static void
  514. vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
  515. {
  516. unsigned i;
  517. /* Need the struct lock for drm_gem_object_unreference(). */
  518. mutex_lock(&dev->struct_mutex);
  519. if (exec->bo) {
  520. for (i = 0; i < exec->bo_count; i++)
  521. drm_gem_object_unreference(&exec->bo[i]->base);
  522. kfree(exec->bo);
  523. }
  524. while (!list_empty(&exec->unref_list)) {
  525. struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  526. struct vc4_bo, unref_head);
  527. list_del(&bo->unref_head);
  528. drm_gem_object_unreference(&bo->base.base);
  529. }
  530. mutex_unlock(&dev->struct_mutex);
  531. kfree(exec);
  532. }
  533. void
  534. vc4_job_handle_completed(struct vc4_dev *vc4)
  535. {
  536. unsigned long irqflags;
  537. struct vc4_seqno_cb *cb, *cb_temp;
  538. spin_lock_irqsave(&vc4->job_lock, irqflags);
  539. while (!list_empty(&vc4->job_done_list)) {
  540. struct vc4_exec_info *exec =
  541. list_first_entry(&vc4->job_done_list,
  542. struct vc4_exec_info, head);
  543. list_del(&exec->head);
  544. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  545. vc4_complete_exec(vc4->dev, exec);
  546. spin_lock_irqsave(&vc4->job_lock, irqflags);
  547. }
  548. list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  549. if (cb->seqno <= vc4->finished_seqno) {
  550. list_del_init(&cb->work.entry);
  551. schedule_work(&cb->work);
  552. }
  553. }
  554. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  555. }
  556. static void vc4_seqno_cb_work(struct work_struct *work)
  557. {
  558. struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  559. cb->func(cb);
  560. }
  561. int vc4_queue_seqno_cb(struct drm_device *dev,
  562. struct vc4_seqno_cb *cb, uint64_t seqno,
  563. void (*func)(struct vc4_seqno_cb *cb))
  564. {
  565. struct vc4_dev *vc4 = to_vc4_dev(dev);
  566. int ret = 0;
  567. unsigned long irqflags;
  568. cb->func = func;
  569. INIT_WORK(&cb->work, vc4_seqno_cb_work);
  570. spin_lock_irqsave(&vc4->job_lock, irqflags);
  571. if (seqno > vc4->finished_seqno) {
  572. cb->seqno = seqno;
  573. list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  574. } else {
  575. schedule_work(&cb->work);
  576. }
  577. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  578. return ret;
  579. }
  580. /* Scheduled when any job has been completed, this walks the list of
  581. * jobs that had completed and unrefs their BOs and frees their exec
  582. * structs.
  583. */
  584. static void
  585. vc4_job_done_work(struct work_struct *work)
  586. {
  587. struct vc4_dev *vc4 =
  588. container_of(work, struct vc4_dev, job_done_work);
  589. vc4_job_handle_completed(vc4);
  590. }
  591. static int
  592. vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  593. uint64_t seqno,
  594. uint64_t *timeout_ns)
  595. {
  596. unsigned long start = jiffies;
  597. int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  598. if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  599. uint64_t delta = jiffies_to_nsecs(jiffies - start);
  600. if (*timeout_ns >= delta)
  601. *timeout_ns -= delta;
  602. }
  603. return ret;
  604. }
  605. int
  606. vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  607. struct drm_file *file_priv)
  608. {
  609. struct drm_vc4_wait_seqno *args = data;
  610. return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  611. &args->timeout_ns);
  612. }
  613. int
  614. vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  615. struct drm_file *file_priv)
  616. {
  617. int ret;
  618. struct drm_vc4_wait_bo *args = data;
  619. struct drm_gem_object *gem_obj;
  620. struct vc4_bo *bo;
  621. if (args->pad != 0)
  622. return -EINVAL;
  623. gem_obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  624. if (!gem_obj) {
  625. DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
  626. return -EINVAL;
  627. }
  628. bo = to_vc4_bo(gem_obj);
  629. ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
  630. &args->timeout_ns);
  631. drm_gem_object_unreference_unlocked(gem_obj);
  632. return ret;
  633. }
  634. /**
  635. * Submits a command list to the VC4.
  636. *
  637. * This is what is called batchbuffer emitting on other hardware.
  638. */
  639. int
  640. vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  641. struct drm_file *file_priv)
  642. {
  643. struct vc4_dev *vc4 = to_vc4_dev(dev);
  644. struct drm_vc4_submit_cl *args = data;
  645. struct vc4_exec_info *exec;
  646. int ret;
  647. if ((args->flags & ~VC4_SUBMIT_CL_USE_CLEAR_COLOR) != 0) {
  648. DRM_ERROR("Unknown flags: 0x%02x\n", args->flags);
  649. return -EINVAL;
  650. }
  651. exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  652. if (!exec) {
  653. DRM_ERROR("malloc failure on exec struct\n");
  654. return -ENOMEM;
  655. }
  656. exec->args = args;
  657. INIT_LIST_HEAD(&exec->unref_list);
  658. ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  659. if (ret)
  660. goto fail;
  661. if (exec->args->bin_cl_size != 0) {
  662. ret = vc4_get_bcl(dev, exec);
  663. if (ret)
  664. goto fail;
  665. } else {
  666. exec->ct0ca = 0;
  667. exec->ct0ea = 0;
  668. }
  669. ret = vc4_get_rcl(dev, exec);
  670. if (ret)
  671. goto fail;
  672. /* Clear this out of the struct we'll be putting in the queue,
  673. * since it's part of our stack.
  674. */
  675. exec->args = NULL;
  676. vc4_queue_submit(dev, exec);
  677. /* Return the seqno for our job. */
  678. args->seqno = vc4->emit_seqno;
  679. return 0;
  680. fail:
  681. vc4_complete_exec(vc4->dev, exec);
  682. return ret;
  683. }
  684. void
  685. vc4_gem_init(struct drm_device *dev)
  686. {
  687. struct vc4_dev *vc4 = to_vc4_dev(dev);
  688. INIT_LIST_HEAD(&vc4->job_list);
  689. INIT_LIST_HEAD(&vc4->job_done_list);
  690. INIT_LIST_HEAD(&vc4->seqno_cb_list);
  691. spin_lock_init(&vc4->job_lock);
  692. INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  693. setup_timer(&vc4->hangcheck.timer,
  694. vc4_hangcheck_elapsed,
  695. (unsigned long)dev);
  696. INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  697. }
  698. void
  699. vc4_gem_destroy(struct drm_device *dev)
  700. {
  701. struct vc4_dev *vc4 = to_vc4_dev(dev);
  702. /* Waiting for exec to finish would need to be done before
  703. * unregistering V3D.
  704. */
  705. WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  706. /* V3D should already have disabled its interrupt and cleared
  707. * the overflow allocation registers. Now free the object.
  708. */
  709. if (vc4->overflow_mem) {
  710. drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base);
  711. vc4->overflow_mem = NULL;
  712. }
  713. vc4_bo_cache_destroy(dev);
  714. if (vc4->hang_state)
  715. vc4_free_hang_state(dev, vc4->hang_state);
  716. }