vc4_gem.c 23 KB

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