vc4_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. mutex_lock(&dev->struct_mutex);
  49. for (i = 0; i < state->user_state.bo_count; i++)
  50. drm_gem_object_unreference(state->bo[i]);
  51. mutex_unlock(&dev->struct_mutex);
  52. kfree(state);
  53. }
  54. int
  55. vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
  56. struct drm_file *file_priv)
  57. {
  58. struct drm_vc4_get_hang_state *get_state = data;
  59. struct drm_vc4_get_hang_state_bo *bo_state;
  60. struct vc4_hang_state *kernel_state;
  61. struct drm_vc4_get_hang_state *state;
  62. struct vc4_dev *vc4 = to_vc4_dev(dev);
  63. unsigned long irqflags;
  64. u32 i;
  65. int ret = 0;
  66. spin_lock_irqsave(&vc4->job_lock, irqflags);
  67. kernel_state = vc4->hang_state;
  68. if (!kernel_state) {
  69. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  70. return -ENOENT;
  71. }
  72. state = &kernel_state->user_state;
  73. /* If the user's array isn't big enough, just return the
  74. * required array size.
  75. */
  76. if (get_state->bo_count < state->bo_count) {
  77. get_state->bo_count = state->bo_count;
  78. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  79. return 0;
  80. }
  81. vc4->hang_state = NULL;
  82. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  83. /* Save the user's BO pointer, so we don't stomp it with the memcpy. */
  84. state->bo = get_state->bo;
  85. memcpy(get_state, state, sizeof(*state));
  86. bo_state = kcalloc(state->bo_count, sizeof(*bo_state), GFP_KERNEL);
  87. if (!bo_state) {
  88. ret = -ENOMEM;
  89. goto err_free;
  90. }
  91. for (i = 0; i < state->bo_count; i++) {
  92. struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]);
  93. u32 handle;
  94. ret = drm_gem_handle_create(file_priv, kernel_state->bo[i],
  95. &handle);
  96. if (ret) {
  97. state->bo_count = i - 1;
  98. goto err;
  99. }
  100. bo_state[i].handle = handle;
  101. bo_state[i].paddr = vc4_bo->base.paddr;
  102. bo_state[i].size = vc4_bo->base.base.size;
  103. }
  104. if (copy_to_user((void __user *)(uintptr_t)get_state->bo,
  105. bo_state,
  106. state->bo_count * sizeof(*bo_state)))
  107. ret = -EFAULT;
  108. kfree(bo_state);
  109. err_free:
  110. vc4_free_hang_state(dev, kernel_state);
  111. err:
  112. return ret;
  113. }
  114. static void
  115. vc4_save_hang_state(struct drm_device *dev)
  116. {
  117. struct vc4_dev *vc4 = to_vc4_dev(dev);
  118. struct drm_vc4_get_hang_state *state;
  119. struct vc4_hang_state *kernel_state;
  120. struct vc4_exec_info *exec[2];
  121. struct vc4_bo *bo;
  122. unsigned long irqflags;
  123. unsigned int i, j, unref_list_count, prev_idx;
  124. kernel_state = kcalloc(1, sizeof(*kernel_state), GFP_KERNEL);
  125. if (!kernel_state)
  126. return;
  127. state = &kernel_state->user_state;
  128. spin_lock_irqsave(&vc4->job_lock, irqflags);
  129. exec[0] = vc4_first_bin_job(vc4);
  130. exec[1] = vc4_first_render_job(vc4);
  131. if (!exec[0] && !exec[1]) {
  132. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  133. return;
  134. }
  135. /* Get the bos from both binner and renderer into hang state. */
  136. state->bo_count = 0;
  137. for (i = 0; i < 2; i++) {
  138. if (!exec[i])
  139. continue;
  140. unref_list_count = 0;
  141. list_for_each_entry(bo, &exec[i]->unref_list, unref_head)
  142. unref_list_count++;
  143. state->bo_count += exec[i]->bo_count + unref_list_count;
  144. }
  145. kernel_state->bo = kcalloc(state->bo_count,
  146. sizeof(*kernel_state->bo), GFP_ATOMIC);
  147. if (!kernel_state->bo) {
  148. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  149. return;
  150. }
  151. prev_idx = 0;
  152. for (i = 0; i < 2; i++) {
  153. if (!exec[i])
  154. continue;
  155. for (j = 0; j < exec[i]->bo_count; j++) {
  156. drm_gem_object_reference(&exec[i]->bo[j]->base);
  157. kernel_state->bo[j + prev_idx] = &exec[i]->bo[j]->base;
  158. }
  159. list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
  160. drm_gem_object_reference(&bo->base.base);
  161. kernel_state->bo[j + prev_idx] = &bo->base.base;
  162. j++;
  163. }
  164. prev_idx = j + 1;
  165. }
  166. if (exec[0])
  167. state->start_bin = exec[0]->ct0ca;
  168. if (exec[1])
  169. state->start_render = exec[1]->ct1ca;
  170. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  171. state->ct0ca = V3D_READ(V3D_CTNCA(0));
  172. state->ct0ea = V3D_READ(V3D_CTNEA(0));
  173. state->ct1ca = V3D_READ(V3D_CTNCA(1));
  174. state->ct1ea = V3D_READ(V3D_CTNEA(1));
  175. state->ct0cs = V3D_READ(V3D_CTNCS(0));
  176. state->ct1cs = V3D_READ(V3D_CTNCS(1));
  177. state->ct0ra0 = V3D_READ(V3D_CT00RA0);
  178. state->ct1ra0 = V3D_READ(V3D_CT01RA0);
  179. state->bpca = V3D_READ(V3D_BPCA);
  180. state->bpcs = V3D_READ(V3D_BPCS);
  181. state->bpoa = V3D_READ(V3D_BPOA);
  182. state->bpos = V3D_READ(V3D_BPOS);
  183. state->vpmbase = V3D_READ(V3D_VPMBASE);
  184. state->dbge = V3D_READ(V3D_DBGE);
  185. state->fdbgo = V3D_READ(V3D_FDBGO);
  186. state->fdbgb = V3D_READ(V3D_FDBGB);
  187. state->fdbgr = V3D_READ(V3D_FDBGR);
  188. state->fdbgs = V3D_READ(V3D_FDBGS);
  189. state->errstat = V3D_READ(V3D_ERRSTAT);
  190. spin_lock_irqsave(&vc4->job_lock, irqflags);
  191. if (vc4->hang_state) {
  192. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  193. vc4_free_hang_state(dev, kernel_state);
  194. } else {
  195. vc4->hang_state = kernel_state;
  196. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  197. }
  198. }
  199. static void
  200. vc4_reset(struct drm_device *dev)
  201. {
  202. struct vc4_dev *vc4 = to_vc4_dev(dev);
  203. DRM_INFO("Resetting GPU.\n");
  204. mutex_lock(&vc4->power_lock);
  205. if (vc4->power_refcount) {
  206. /* Power the device off and back on the by dropping the
  207. * reference on runtime PM.
  208. */
  209. pm_runtime_put_sync_suspend(&vc4->v3d->pdev->dev);
  210. pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  211. }
  212. mutex_unlock(&vc4->power_lock);
  213. vc4_irq_reset(dev);
  214. /* Rearm the hangcheck -- another job might have been waiting
  215. * for our hung one to get kicked off, and vc4_irq_reset()
  216. * would have started it.
  217. */
  218. vc4_queue_hangcheck(dev);
  219. }
  220. static void
  221. vc4_reset_work(struct work_struct *work)
  222. {
  223. struct vc4_dev *vc4 =
  224. container_of(work, struct vc4_dev, hangcheck.reset_work);
  225. vc4_save_hang_state(vc4->dev);
  226. vc4_reset(vc4->dev);
  227. }
  228. static void
  229. vc4_hangcheck_elapsed(unsigned long data)
  230. {
  231. struct drm_device *dev = (struct drm_device *)data;
  232. struct vc4_dev *vc4 = to_vc4_dev(dev);
  233. uint32_t ct0ca, ct1ca;
  234. unsigned long irqflags;
  235. struct vc4_exec_info *bin_exec, *render_exec;
  236. spin_lock_irqsave(&vc4->job_lock, irqflags);
  237. bin_exec = vc4_first_bin_job(vc4);
  238. render_exec = vc4_first_render_job(vc4);
  239. /* If idle, we can stop watching for hangs. */
  240. if (!bin_exec && !render_exec) {
  241. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  242. return;
  243. }
  244. ct0ca = V3D_READ(V3D_CTNCA(0));
  245. ct1ca = V3D_READ(V3D_CTNCA(1));
  246. /* If we've made any progress in execution, rearm the timer
  247. * and wait.
  248. */
  249. if ((bin_exec && ct0ca != bin_exec->last_ct0ca) ||
  250. (render_exec && ct1ca != render_exec->last_ct1ca)) {
  251. if (bin_exec)
  252. bin_exec->last_ct0ca = ct0ca;
  253. if (render_exec)
  254. render_exec->last_ct1ca = ct1ca;
  255. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  256. vc4_queue_hangcheck(dev);
  257. return;
  258. }
  259. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  260. /* We've gone too long with no progress, reset. This has to
  261. * be done from a work struct, since resetting can sleep and
  262. * this timer hook isn't allowed to.
  263. */
  264. schedule_work(&vc4->hangcheck.reset_work);
  265. }
  266. static void
  267. submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
  268. {
  269. struct vc4_dev *vc4 = to_vc4_dev(dev);
  270. /* Set the current and end address of the control list.
  271. * Writing the end register is what starts the job.
  272. */
  273. V3D_WRITE(V3D_CTNCA(thread), start);
  274. V3D_WRITE(V3D_CTNEA(thread), end);
  275. }
  276. int
  277. vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
  278. bool interruptible)
  279. {
  280. struct vc4_dev *vc4 = to_vc4_dev(dev);
  281. int ret = 0;
  282. unsigned long timeout_expire;
  283. DEFINE_WAIT(wait);
  284. if (vc4->finished_seqno >= seqno)
  285. return 0;
  286. if (timeout_ns == 0)
  287. return -ETIME;
  288. timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
  289. trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
  290. for (;;) {
  291. prepare_to_wait(&vc4->job_wait_queue, &wait,
  292. interruptible ? TASK_INTERRUPTIBLE :
  293. TASK_UNINTERRUPTIBLE);
  294. if (interruptible && signal_pending(current)) {
  295. ret = -ERESTARTSYS;
  296. break;
  297. }
  298. if (vc4->finished_seqno >= seqno)
  299. break;
  300. if (timeout_ns != ~0ull) {
  301. if (time_after_eq(jiffies, timeout_expire)) {
  302. ret = -ETIME;
  303. break;
  304. }
  305. schedule_timeout(timeout_expire - jiffies);
  306. } else {
  307. schedule();
  308. }
  309. }
  310. finish_wait(&vc4->job_wait_queue, &wait);
  311. trace_vc4_wait_for_seqno_end(dev, seqno);
  312. return ret;
  313. }
  314. static void
  315. vc4_flush_caches(struct drm_device *dev)
  316. {
  317. struct vc4_dev *vc4 = to_vc4_dev(dev);
  318. /* Flush the GPU L2 caches. These caches sit on top of system
  319. * L3 (the 128kb or so shared with the CPU), and are
  320. * non-allocating in the L3.
  321. */
  322. V3D_WRITE(V3D_L2CACTL,
  323. V3D_L2CACTL_L2CCLR);
  324. V3D_WRITE(V3D_SLCACTL,
  325. VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  326. VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
  327. VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
  328. VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
  329. }
  330. /* Sets the registers for the next job to be actually be executed in
  331. * the hardware.
  332. *
  333. * The job_lock should be held during this.
  334. */
  335. void
  336. vc4_submit_next_bin_job(struct drm_device *dev)
  337. {
  338. struct vc4_dev *vc4 = to_vc4_dev(dev);
  339. struct vc4_exec_info *exec;
  340. again:
  341. exec = vc4_first_bin_job(vc4);
  342. if (!exec)
  343. return;
  344. vc4_flush_caches(dev);
  345. /* Disable the binner's pre-loaded overflow memory address */
  346. V3D_WRITE(V3D_BPOA, 0);
  347. V3D_WRITE(V3D_BPOS, 0);
  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. }
  385. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  386. bo->seqno = seqno;
  387. }
  388. }
  389. /* Queues a struct vc4_exec_info for execution. If no job is
  390. * currently executing, then submits it.
  391. *
  392. * Unlike most GPUs, our hardware only handles one command list at a
  393. * time. To queue multiple jobs at once, we'd need to edit the
  394. * previous command list to have a jump to the new one at the end, and
  395. * then bump the end address. That's a change for a later date,
  396. * though.
  397. */
  398. static void
  399. vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec)
  400. {
  401. struct vc4_dev *vc4 = to_vc4_dev(dev);
  402. uint64_t seqno;
  403. unsigned long irqflags;
  404. spin_lock_irqsave(&vc4->job_lock, irqflags);
  405. seqno = ++vc4->emit_seqno;
  406. exec->seqno = seqno;
  407. vc4_update_bo_seqnos(exec, seqno);
  408. list_add_tail(&exec->head, &vc4->bin_job_list);
  409. /* If no job was executing, kick ours off. Otherwise, it'll
  410. * get started when the previous job's flush done interrupt
  411. * occurs.
  412. */
  413. if (vc4_first_bin_job(vc4) == exec) {
  414. vc4_submit_next_bin_job(dev);
  415. vc4_queue_hangcheck(dev);
  416. }
  417. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  418. }
  419. /**
  420. * Looks up a bunch of GEM handles for BOs and stores the array for
  421. * use in the command validator that actually writes relocated
  422. * addresses pointing to them.
  423. */
  424. static int
  425. vc4_cl_lookup_bos(struct drm_device *dev,
  426. struct drm_file *file_priv,
  427. struct vc4_exec_info *exec)
  428. {
  429. struct drm_vc4_submit_cl *args = exec->args;
  430. uint32_t *handles;
  431. int ret = 0;
  432. int i;
  433. exec->bo_count = args->bo_handle_count;
  434. if (!exec->bo_count) {
  435. /* See comment on bo_index for why we have to check
  436. * this.
  437. */
  438. DRM_ERROR("Rendering requires BOs to validate\n");
  439. return -EINVAL;
  440. }
  441. exec->bo = kcalloc(exec->bo_count, sizeof(struct drm_gem_cma_object *),
  442. GFP_KERNEL);
  443. if (!exec->bo) {
  444. DRM_ERROR("Failed to allocate validated BO pointers\n");
  445. return -ENOMEM;
  446. }
  447. handles = drm_malloc_ab(exec->bo_count, sizeof(uint32_t));
  448. if (!handles) {
  449. DRM_ERROR("Failed to allocate incoming GEM handles\n");
  450. goto fail;
  451. }
  452. ret = copy_from_user(handles,
  453. (void __user *)(uintptr_t)args->bo_handles,
  454. exec->bo_count * sizeof(uint32_t));
  455. if (ret) {
  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. kfree(handles);
  476. return 0;
  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 = kmalloc(temp_size, GFP_KERNEL);
  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. fail:
  563. kfree(temp);
  564. return ret;
  565. }
  566. static void
  567. vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
  568. {
  569. struct vc4_dev *vc4 = to_vc4_dev(dev);
  570. unsigned i;
  571. /* Need the struct lock for drm_gem_object_unreference(). */
  572. mutex_lock(&dev->struct_mutex);
  573. if (exec->bo) {
  574. for (i = 0; i < exec->bo_count; i++)
  575. drm_gem_object_unreference(&exec->bo[i]->base);
  576. kfree(exec->bo);
  577. }
  578. while (!list_empty(&exec->unref_list)) {
  579. struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  580. struct vc4_bo, unref_head);
  581. list_del(&bo->unref_head);
  582. drm_gem_object_unreference(&bo->base.base);
  583. }
  584. mutex_unlock(&dev->struct_mutex);
  585. mutex_lock(&vc4->power_lock);
  586. if (--vc4->power_refcount == 0)
  587. pm_runtime_put(&vc4->v3d->pdev->dev);
  588. mutex_unlock(&vc4->power_lock);
  589. kfree(exec);
  590. }
  591. void
  592. vc4_job_handle_completed(struct vc4_dev *vc4)
  593. {
  594. unsigned long irqflags;
  595. struct vc4_seqno_cb *cb, *cb_temp;
  596. spin_lock_irqsave(&vc4->job_lock, irqflags);
  597. while (!list_empty(&vc4->job_done_list)) {
  598. struct vc4_exec_info *exec =
  599. list_first_entry(&vc4->job_done_list,
  600. struct vc4_exec_info, head);
  601. list_del(&exec->head);
  602. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  603. vc4_complete_exec(vc4->dev, exec);
  604. spin_lock_irqsave(&vc4->job_lock, irqflags);
  605. }
  606. list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  607. if (cb->seqno <= vc4->finished_seqno) {
  608. list_del_init(&cb->work.entry);
  609. schedule_work(&cb->work);
  610. }
  611. }
  612. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  613. }
  614. static void vc4_seqno_cb_work(struct work_struct *work)
  615. {
  616. struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  617. cb->func(cb);
  618. }
  619. int vc4_queue_seqno_cb(struct drm_device *dev,
  620. struct vc4_seqno_cb *cb, uint64_t seqno,
  621. void (*func)(struct vc4_seqno_cb *cb))
  622. {
  623. struct vc4_dev *vc4 = to_vc4_dev(dev);
  624. int ret = 0;
  625. unsigned long irqflags;
  626. cb->func = func;
  627. INIT_WORK(&cb->work, vc4_seqno_cb_work);
  628. spin_lock_irqsave(&vc4->job_lock, irqflags);
  629. if (seqno > vc4->finished_seqno) {
  630. cb->seqno = seqno;
  631. list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  632. } else {
  633. schedule_work(&cb->work);
  634. }
  635. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  636. return ret;
  637. }
  638. /* Scheduled when any job has been completed, this walks the list of
  639. * jobs that had completed and unrefs their BOs and frees their exec
  640. * structs.
  641. */
  642. static void
  643. vc4_job_done_work(struct work_struct *work)
  644. {
  645. struct vc4_dev *vc4 =
  646. container_of(work, struct vc4_dev, job_done_work);
  647. vc4_job_handle_completed(vc4);
  648. }
  649. static int
  650. vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  651. uint64_t seqno,
  652. uint64_t *timeout_ns)
  653. {
  654. unsigned long start = jiffies;
  655. int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  656. if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  657. uint64_t delta = jiffies_to_nsecs(jiffies - start);
  658. if (*timeout_ns >= delta)
  659. *timeout_ns -= delta;
  660. }
  661. return ret;
  662. }
  663. int
  664. vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  665. struct drm_file *file_priv)
  666. {
  667. struct drm_vc4_wait_seqno *args = data;
  668. return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  669. &args->timeout_ns);
  670. }
  671. int
  672. vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  673. struct drm_file *file_priv)
  674. {
  675. int ret;
  676. struct drm_vc4_wait_bo *args = data;
  677. struct drm_gem_object *gem_obj;
  678. struct vc4_bo *bo;
  679. if (args->pad != 0)
  680. return -EINVAL;
  681. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  682. if (!gem_obj) {
  683. DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
  684. return -EINVAL;
  685. }
  686. bo = to_vc4_bo(gem_obj);
  687. ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
  688. &args->timeout_ns);
  689. drm_gem_object_unreference_unlocked(gem_obj);
  690. return ret;
  691. }
  692. /**
  693. * Submits a command list to the VC4.
  694. *
  695. * This is what is called batchbuffer emitting on other hardware.
  696. */
  697. int
  698. vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  699. struct drm_file *file_priv)
  700. {
  701. struct vc4_dev *vc4 = to_vc4_dev(dev);
  702. struct drm_vc4_submit_cl *args = data;
  703. struct vc4_exec_info *exec;
  704. int ret = 0;
  705. if ((args->flags & ~VC4_SUBMIT_CL_USE_CLEAR_COLOR) != 0) {
  706. DRM_ERROR("Unknown flags: 0x%02x\n", args->flags);
  707. return -EINVAL;
  708. }
  709. exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  710. if (!exec) {
  711. DRM_ERROR("malloc failure on exec struct\n");
  712. return -ENOMEM;
  713. }
  714. mutex_lock(&vc4->power_lock);
  715. if (vc4->power_refcount++ == 0)
  716. ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  717. mutex_unlock(&vc4->power_lock);
  718. if (ret < 0) {
  719. kfree(exec);
  720. return ret;
  721. }
  722. exec->args = args;
  723. INIT_LIST_HEAD(&exec->unref_list);
  724. ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  725. if (ret)
  726. goto fail;
  727. if (exec->args->bin_cl_size != 0) {
  728. ret = vc4_get_bcl(dev, exec);
  729. if (ret)
  730. goto fail;
  731. } else {
  732. exec->ct0ca = 0;
  733. exec->ct0ea = 0;
  734. }
  735. ret = vc4_get_rcl(dev, exec);
  736. if (ret)
  737. goto fail;
  738. /* Clear this out of the struct we'll be putting in the queue,
  739. * since it's part of our stack.
  740. */
  741. exec->args = NULL;
  742. vc4_queue_submit(dev, exec);
  743. /* Return the seqno for our job. */
  744. args->seqno = vc4->emit_seqno;
  745. return 0;
  746. fail:
  747. vc4_complete_exec(vc4->dev, exec);
  748. return ret;
  749. }
  750. void
  751. vc4_gem_init(struct drm_device *dev)
  752. {
  753. struct vc4_dev *vc4 = to_vc4_dev(dev);
  754. INIT_LIST_HEAD(&vc4->bin_job_list);
  755. INIT_LIST_HEAD(&vc4->render_job_list);
  756. INIT_LIST_HEAD(&vc4->job_done_list);
  757. INIT_LIST_HEAD(&vc4->seqno_cb_list);
  758. spin_lock_init(&vc4->job_lock);
  759. INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  760. setup_timer(&vc4->hangcheck.timer,
  761. vc4_hangcheck_elapsed,
  762. (unsigned long)dev);
  763. INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  764. mutex_init(&vc4->power_lock);
  765. }
  766. void
  767. vc4_gem_destroy(struct drm_device *dev)
  768. {
  769. struct vc4_dev *vc4 = to_vc4_dev(dev);
  770. /* Waiting for exec to finish would need to be done before
  771. * unregistering V3D.
  772. */
  773. WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  774. /* V3D should already have disabled its interrupt and cleared
  775. * the overflow allocation registers. Now free the object.
  776. */
  777. if (vc4->overflow_mem) {
  778. drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base);
  779. vc4->overflow_mem = NULL;
  780. }
  781. vc4_bo_cache_destroy(dev);
  782. if (vc4->hang_state)
  783. vc4_free_hang_state(dev, vc4->hang_state);
  784. }