vc4_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. /* Disable the binner's pre-loaded overflow memory address */
  344. V3D_WRITE(V3D_BPOA, 0);
  345. V3D_WRITE(V3D_BPOS, 0);
  346. /* Either put the job in the binner if it uses the binner, or
  347. * immediately move it to the to-be-rendered queue.
  348. */
  349. if (exec->ct0ca != exec->ct0ea) {
  350. submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
  351. } else {
  352. vc4_move_job_to_render(dev, exec);
  353. goto again;
  354. }
  355. }
  356. void
  357. vc4_submit_next_render_job(struct drm_device *dev)
  358. {
  359. struct vc4_dev *vc4 = to_vc4_dev(dev);
  360. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  361. if (!exec)
  362. return;
  363. submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
  364. }
  365. void
  366. vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
  367. {
  368. struct vc4_dev *vc4 = to_vc4_dev(dev);
  369. bool was_empty = list_empty(&vc4->render_job_list);
  370. list_move_tail(&exec->head, &vc4->render_job_list);
  371. if (was_empty)
  372. vc4_submit_next_render_job(dev);
  373. }
  374. static void
  375. vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  376. {
  377. struct vc4_bo *bo;
  378. unsigned i;
  379. for (i = 0; i < exec->bo_count; i++) {
  380. bo = to_vc4_bo(&exec->bo[i]->base);
  381. bo->seqno = seqno;
  382. }
  383. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  384. bo->seqno = seqno;
  385. }
  386. }
  387. /* Queues a struct vc4_exec_info for execution. If no job is
  388. * currently executing, then submits it.
  389. *
  390. * Unlike most GPUs, our hardware only handles one command list at a
  391. * time. To queue multiple jobs at once, we'd need to edit the
  392. * previous command list to have a jump to the new one at the end, and
  393. * then bump the end address. That's a change for a later date,
  394. * though.
  395. */
  396. static void
  397. vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec)
  398. {
  399. struct vc4_dev *vc4 = to_vc4_dev(dev);
  400. uint64_t seqno;
  401. unsigned long irqflags;
  402. spin_lock_irqsave(&vc4->job_lock, irqflags);
  403. seqno = ++vc4->emit_seqno;
  404. exec->seqno = seqno;
  405. vc4_update_bo_seqnos(exec, seqno);
  406. list_add_tail(&exec->head, &vc4->bin_job_list);
  407. /* If no job was executing, kick ours off. Otherwise, it'll
  408. * get started when the previous job's flush done interrupt
  409. * occurs.
  410. */
  411. if (vc4_first_bin_job(vc4) == exec) {
  412. vc4_submit_next_bin_job(dev);
  413. vc4_queue_hangcheck(dev);
  414. }
  415. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  416. }
  417. /**
  418. * Looks up a bunch of GEM handles for BOs and stores the array for
  419. * use in the command validator that actually writes relocated
  420. * addresses pointing to them.
  421. */
  422. static int
  423. vc4_cl_lookup_bos(struct drm_device *dev,
  424. struct drm_file *file_priv,
  425. struct vc4_exec_info *exec)
  426. {
  427. struct drm_vc4_submit_cl *args = exec->args;
  428. uint32_t *handles;
  429. int ret = 0;
  430. int i;
  431. exec->bo_count = args->bo_handle_count;
  432. if (!exec->bo_count) {
  433. /* See comment on bo_index for why we have to check
  434. * this.
  435. */
  436. DRM_ERROR("Rendering requires BOs to validate\n");
  437. return -EINVAL;
  438. }
  439. exec->bo = kcalloc(exec->bo_count, sizeof(struct drm_gem_cma_object *),
  440. GFP_KERNEL);
  441. if (!exec->bo) {
  442. DRM_ERROR("Failed to allocate validated BO pointers\n");
  443. return -ENOMEM;
  444. }
  445. handles = drm_malloc_ab(exec->bo_count, sizeof(uint32_t));
  446. if (!handles) {
  447. DRM_ERROR("Failed to allocate incoming GEM handles\n");
  448. goto fail;
  449. }
  450. ret = copy_from_user(handles,
  451. (void __user *)(uintptr_t)args->bo_handles,
  452. exec->bo_count * sizeof(uint32_t));
  453. if (ret) {
  454. DRM_ERROR("Failed to copy in GEM handles\n");
  455. goto fail;
  456. }
  457. spin_lock(&file_priv->table_lock);
  458. for (i = 0; i < exec->bo_count; i++) {
  459. struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
  460. handles[i]);
  461. if (!bo) {
  462. DRM_ERROR("Failed to look up GEM BO %d: %d\n",
  463. i, handles[i]);
  464. ret = -EINVAL;
  465. spin_unlock(&file_priv->table_lock);
  466. goto fail;
  467. }
  468. drm_gem_object_reference(bo);
  469. exec->bo[i] = (struct drm_gem_cma_object *)bo;
  470. }
  471. spin_unlock(&file_priv->table_lock);
  472. fail:
  473. kfree(handles);
  474. return 0;
  475. }
  476. static int
  477. vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  478. {
  479. struct drm_vc4_submit_cl *args = exec->args;
  480. void *temp = NULL;
  481. void *bin;
  482. int ret = 0;
  483. uint32_t bin_offset = 0;
  484. uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
  485. 16);
  486. uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
  487. uint32_t exec_size = uniforms_offset + args->uniforms_size;
  488. uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
  489. args->shader_rec_count);
  490. struct vc4_bo *bo;
  491. if (uniforms_offset < shader_rec_offset ||
  492. exec_size < uniforms_offset ||
  493. args->shader_rec_count >= (UINT_MAX /
  494. sizeof(struct vc4_shader_state)) ||
  495. temp_size < exec_size) {
  496. DRM_ERROR("overflow in exec arguments\n");
  497. goto fail;
  498. }
  499. /* Allocate space where we'll store the copied in user command lists
  500. * and shader records.
  501. *
  502. * We don't just copy directly into the BOs because we need to
  503. * read the contents back for validation, and I think the
  504. * bo->vaddr is uncached access.
  505. */
  506. temp = kmalloc(temp_size, GFP_KERNEL);
  507. if (!temp) {
  508. DRM_ERROR("Failed to allocate storage for copying "
  509. "in bin/render CLs.\n");
  510. ret = -ENOMEM;
  511. goto fail;
  512. }
  513. bin = temp + bin_offset;
  514. exec->shader_rec_u = temp + shader_rec_offset;
  515. exec->uniforms_u = temp + uniforms_offset;
  516. exec->shader_state = temp + exec_size;
  517. exec->shader_state_size = args->shader_rec_count;
  518. if (copy_from_user(bin,
  519. (void __user *)(uintptr_t)args->bin_cl,
  520. args->bin_cl_size)) {
  521. ret = -EFAULT;
  522. goto fail;
  523. }
  524. if (copy_from_user(exec->shader_rec_u,
  525. (void __user *)(uintptr_t)args->shader_rec,
  526. args->shader_rec_size)) {
  527. ret = -EFAULT;
  528. goto fail;
  529. }
  530. if (copy_from_user(exec->uniforms_u,
  531. (void __user *)(uintptr_t)args->uniforms,
  532. args->uniforms_size)) {
  533. ret = -EFAULT;
  534. goto fail;
  535. }
  536. bo = vc4_bo_create(dev, exec_size, true);
  537. if (IS_ERR(bo)) {
  538. DRM_ERROR("Couldn't allocate BO for binning\n");
  539. ret = PTR_ERR(bo);
  540. goto fail;
  541. }
  542. exec->exec_bo = &bo->base;
  543. list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
  544. &exec->unref_list);
  545. exec->ct0ca = exec->exec_bo->paddr + bin_offset;
  546. exec->bin_u = bin;
  547. exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
  548. exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
  549. exec->shader_rec_size = args->shader_rec_size;
  550. exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
  551. exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
  552. exec->uniforms_size = args->uniforms_size;
  553. ret = vc4_validate_bin_cl(dev,
  554. exec->exec_bo->vaddr + bin_offset,
  555. bin,
  556. exec);
  557. if (ret)
  558. goto fail;
  559. ret = vc4_validate_shader_recs(dev, exec);
  560. fail:
  561. kfree(temp);
  562. return ret;
  563. }
  564. static void
  565. vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
  566. {
  567. struct vc4_dev *vc4 = to_vc4_dev(dev);
  568. unsigned i;
  569. if (exec->bo) {
  570. for (i = 0; i < exec->bo_count; i++)
  571. drm_gem_object_unreference_unlocked(&exec->bo[i]->base);
  572. kfree(exec->bo);
  573. }
  574. while (!list_empty(&exec->unref_list)) {
  575. struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  576. struct vc4_bo, unref_head);
  577. list_del(&bo->unref_head);
  578. drm_gem_object_unreference_unlocked(&bo->base.base);
  579. }
  580. mutex_lock(&vc4->power_lock);
  581. if (--vc4->power_refcount == 0)
  582. pm_runtime_put(&vc4->v3d->pdev->dev);
  583. mutex_unlock(&vc4->power_lock);
  584. kfree(exec);
  585. }
  586. void
  587. vc4_job_handle_completed(struct vc4_dev *vc4)
  588. {
  589. unsigned long irqflags;
  590. struct vc4_seqno_cb *cb, *cb_temp;
  591. spin_lock_irqsave(&vc4->job_lock, irqflags);
  592. while (!list_empty(&vc4->job_done_list)) {
  593. struct vc4_exec_info *exec =
  594. list_first_entry(&vc4->job_done_list,
  595. struct vc4_exec_info, head);
  596. list_del(&exec->head);
  597. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  598. vc4_complete_exec(vc4->dev, exec);
  599. spin_lock_irqsave(&vc4->job_lock, irqflags);
  600. }
  601. list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  602. if (cb->seqno <= vc4->finished_seqno) {
  603. list_del_init(&cb->work.entry);
  604. schedule_work(&cb->work);
  605. }
  606. }
  607. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  608. }
  609. static void vc4_seqno_cb_work(struct work_struct *work)
  610. {
  611. struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  612. cb->func(cb);
  613. }
  614. int vc4_queue_seqno_cb(struct drm_device *dev,
  615. struct vc4_seqno_cb *cb, uint64_t seqno,
  616. void (*func)(struct vc4_seqno_cb *cb))
  617. {
  618. struct vc4_dev *vc4 = to_vc4_dev(dev);
  619. int ret = 0;
  620. unsigned long irqflags;
  621. cb->func = func;
  622. INIT_WORK(&cb->work, vc4_seqno_cb_work);
  623. spin_lock_irqsave(&vc4->job_lock, irqflags);
  624. if (seqno > vc4->finished_seqno) {
  625. cb->seqno = seqno;
  626. list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  627. } else {
  628. schedule_work(&cb->work);
  629. }
  630. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  631. return ret;
  632. }
  633. /* Scheduled when any job has been completed, this walks the list of
  634. * jobs that had completed and unrefs their BOs and frees their exec
  635. * structs.
  636. */
  637. static void
  638. vc4_job_done_work(struct work_struct *work)
  639. {
  640. struct vc4_dev *vc4 =
  641. container_of(work, struct vc4_dev, job_done_work);
  642. vc4_job_handle_completed(vc4);
  643. }
  644. static int
  645. vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  646. uint64_t seqno,
  647. uint64_t *timeout_ns)
  648. {
  649. unsigned long start = jiffies;
  650. int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  651. if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  652. uint64_t delta = jiffies_to_nsecs(jiffies - start);
  653. if (*timeout_ns >= delta)
  654. *timeout_ns -= delta;
  655. }
  656. return ret;
  657. }
  658. int
  659. vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  660. struct drm_file *file_priv)
  661. {
  662. struct drm_vc4_wait_seqno *args = data;
  663. return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  664. &args->timeout_ns);
  665. }
  666. int
  667. vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  668. struct drm_file *file_priv)
  669. {
  670. int ret;
  671. struct drm_vc4_wait_bo *args = data;
  672. struct drm_gem_object *gem_obj;
  673. struct vc4_bo *bo;
  674. if (args->pad != 0)
  675. return -EINVAL;
  676. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  677. if (!gem_obj) {
  678. DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
  679. return -EINVAL;
  680. }
  681. bo = to_vc4_bo(gem_obj);
  682. ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
  683. &args->timeout_ns);
  684. drm_gem_object_unreference_unlocked(gem_obj);
  685. return ret;
  686. }
  687. /**
  688. * Submits a command list to the VC4.
  689. *
  690. * This is what is called batchbuffer emitting on other hardware.
  691. */
  692. int
  693. vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  694. struct drm_file *file_priv)
  695. {
  696. struct vc4_dev *vc4 = to_vc4_dev(dev);
  697. struct drm_vc4_submit_cl *args = data;
  698. struct vc4_exec_info *exec;
  699. int ret = 0;
  700. if ((args->flags & ~VC4_SUBMIT_CL_USE_CLEAR_COLOR) != 0) {
  701. DRM_ERROR("Unknown flags: 0x%02x\n", args->flags);
  702. return -EINVAL;
  703. }
  704. exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  705. if (!exec) {
  706. DRM_ERROR("malloc failure on exec struct\n");
  707. return -ENOMEM;
  708. }
  709. mutex_lock(&vc4->power_lock);
  710. if (vc4->power_refcount++ == 0)
  711. ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  712. mutex_unlock(&vc4->power_lock);
  713. if (ret < 0) {
  714. kfree(exec);
  715. return ret;
  716. }
  717. exec->args = args;
  718. INIT_LIST_HEAD(&exec->unref_list);
  719. ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  720. if (ret)
  721. goto fail;
  722. if (exec->args->bin_cl_size != 0) {
  723. ret = vc4_get_bcl(dev, exec);
  724. if (ret)
  725. goto fail;
  726. } else {
  727. exec->ct0ca = 0;
  728. exec->ct0ea = 0;
  729. }
  730. ret = vc4_get_rcl(dev, exec);
  731. if (ret)
  732. goto fail;
  733. /* Clear this out of the struct we'll be putting in the queue,
  734. * since it's part of our stack.
  735. */
  736. exec->args = NULL;
  737. vc4_queue_submit(dev, exec);
  738. /* Return the seqno for our job. */
  739. args->seqno = vc4->emit_seqno;
  740. return 0;
  741. fail:
  742. vc4_complete_exec(vc4->dev, exec);
  743. return ret;
  744. }
  745. void
  746. vc4_gem_init(struct drm_device *dev)
  747. {
  748. struct vc4_dev *vc4 = to_vc4_dev(dev);
  749. INIT_LIST_HEAD(&vc4->bin_job_list);
  750. INIT_LIST_HEAD(&vc4->render_job_list);
  751. INIT_LIST_HEAD(&vc4->job_done_list);
  752. INIT_LIST_HEAD(&vc4->seqno_cb_list);
  753. spin_lock_init(&vc4->job_lock);
  754. INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  755. setup_timer(&vc4->hangcheck.timer,
  756. vc4_hangcheck_elapsed,
  757. (unsigned long)dev);
  758. INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  759. mutex_init(&vc4->power_lock);
  760. }
  761. void
  762. vc4_gem_destroy(struct drm_device *dev)
  763. {
  764. struct vc4_dev *vc4 = to_vc4_dev(dev);
  765. /* Waiting for exec to finish would need to be done before
  766. * unregistering V3D.
  767. */
  768. WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  769. /* V3D should already have disabled its interrupt and cleared
  770. * the overflow allocation registers. Now free the object.
  771. */
  772. if (vc4->overflow_mem) {
  773. drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base);
  774. vc4->overflow_mem = NULL;
  775. }
  776. vc4_bo_cache_destroy(dev);
  777. if (vc4->hang_state)
  778. vc4_free_hang_state(dev, vc4->hang_state);
  779. }