vc4_gem.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /*
  2. * Copyright © 2014 Broadcom
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/device.h>
  27. #include <linux/io.h>
  28. #include <linux/sched/signal.h>
  29. #include <linux/dma-fence-array.h>
  30. #include <drm/drm_syncobj.h>
  31. #include "uapi/drm/vc4_drm.h"
  32. #include "vc4_drv.h"
  33. #include "vc4_regs.h"
  34. #include "vc4_trace.h"
  35. static void
  36. vc4_queue_hangcheck(struct drm_device *dev)
  37. {
  38. struct vc4_dev *vc4 = to_vc4_dev(dev);
  39. mod_timer(&vc4->hangcheck.timer,
  40. round_jiffies_up(jiffies + msecs_to_jiffies(100)));
  41. }
  42. struct vc4_hang_state {
  43. struct drm_vc4_get_hang_state user_state;
  44. u32 bo_count;
  45. struct drm_gem_object **bo;
  46. };
  47. static void
  48. vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)
  49. {
  50. unsigned int i;
  51. for (i = 0; i < state->user_state.bo_count; i++)
  52. drm_gem_object_put(state->bo[i]);
  53. kfree(state);
  54. }
  55. int
  56. vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
  57. struct drm_file *file_priv)
  58. {
  59. struct drm_vc4_get_hang_state *get_state = data;
  60. struct drm_vc4_get_hang_state_bo *bo_state;
  61. struct vc4_hang_state *kernel_state;
  62. struct drm_vc4_get_hang_state *state;
  63. struct vc4_dev *vc4 = to_vc4_dev(dev);
  64. unsigned long irqflags;
  65. u32 i;
  66. int ret = 0;
  67. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  68. return -ENODEV;
  69. if (!vc4->v3d) {
  70. DRM_DEBUG("VC4_GET_HANG_STATE with no VC4 V3D probed\n");
  71. return -ENODEV;
  72. }
  73. spin_lock_irqsave(&vc4->job_lock, irqflags);
  74. kernel_state = vc4->hang_state;
  75. if (!kernel_state) {
  76. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  77. return -ENOENT;
  78. }
  79. state = &kernel_state->user_state;
  80. /* If the user's array isn't big enough, just return the
  81. * required array size.
  82. */
  83. if (get_state->bo_count < state->bo_count) {
  84. get_state->bo_count = state->bo_count;
  85. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  86. return 0;
  87. }
  88. vc4->hang_state = NULL;
  89. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  90. /* Save the user's BO pointer, so we don't stomp it with the memcpy. */
  91. state->bo = get_state->bo;
  92. memcpy(get_state, state, sizeof(*state));
  93. bo_state = kcalloc(state->bo_count, sizeof(*bo_state), GFP_KERNEL);
  94. if (!bo_state) {
  95. ret = -ENOMEM;
  96. goto err_free;
  97. }
  98. for (i = 0; i < state->bo_count; i++) {
  99. struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]);
  100. u32 handle;
  101. ret = drm_gem_handle_create(file_priv, kernel_state->bo[i],
  102. &handle);
  103. if (ret) {
  104. state->bo_count = i;
  105. goto err_delete_handle;
  106. }
  107. bo_state[i].handle = handle;
  108. bo_state[i].paddr = vc4_bo->base.dma_addr;
  109. bo_state[i].size = vc4_bo->base.base.size;
  110. }
  111. if (copy_to_user(u64_to_user_ptr(get_state->bo),
  112. bo_state,
  113. state->bo_count * sizeof(*bo_state)))
  114. ret = -EFAULT;
  115. err_delete_handle:
  116. if (ret) {
  117. for (i = 0; i < state->bo_count; i++)
  118. drm_gem_handle_delete(file_priv, bo_state[i].handle);
  119. }
  120. err_free:
  121. vc4_free_hang_state(dev, kernel_state);
  122. kfree(bo_state);
  123. return ret;
  124. }
  125. static void
  126. vc4_save_hang_state(struct drm_device *dev)
  127. {
  128. struct vc4_dev *vc4 = to_vc4_dev(dev);
  129. struct drm_vc4_get_hang_state *state;
  130. struct vc4_hang_state *kernel_state;
  131. struct vc4_exec_info *exec[2];
  132. struct vc4_bo *bo;
  133. unsigned long irqflags;
  134. unsigned int i, j, k, unref_list_count;
  135. kernel_state = kcalloc(1, sizeof(*kernel_state), GFP_KERNEL);
  136. if (!kernel_state)
  137. return;
  138. state = &kernel_state->user_state;
  139. spin_lock_irqsave(&vc4->job_lock, irqflags);
  140. exec[0] = vc4_first_bin_job(vc4);
  141. exec[1] = vc4_first_render_job(vc4);
  142. if (!exec[0] && !exec[1]) {
  143. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  144. return;
  145. }
  146. /* Get the bos from both binner and renderer into hang state. */
  147. state->bo_count = 0;
  148. for (i = 0; i < 2; i++) {
  149. if (!exec[i])
  150. continue;
  151. unref_list_count = 0;
  152. list_for_each_entry(bo, &exec[i]->unref_list, unref_head)
  153. unref_list_count++;
  154. state->bo_count += exec[i]->bo_count + unref_list_count;
  155. }
  156. kernel_state->bo = kcalloc(state->bo_count,
  157. sizeof(*kernel_state->bo), GFP_ATOMIC);
  158. if (!kernel_state->bo) {
  159. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  160. return;
  161. }
  162. k = 0;
  163. for (i = 0; i < 2; i++) {
  164. if (!exec[i])
  165. continue;
  166. for (j = 0; j < exec[i]->bo_count; j++) {
  167. bo = to_vc4_bo(exec[i]->bo[j]);
  168. /* Retain BOs just in case they were marked purgeable.
  169. * This prevents the BO from being purged before
  170. * someone had a chance to dump the hang state.
  171. */
  172. WARN_ON(!refcount_read(&bo->usecnt));
  173. refcount_inc(&bo->usecnt);
  174. drm_gem_object_get(exec[i]->bo[j]);
  175. kernel_state->bo[k++] = exec[i]->bo[j];
  176. }
  177. list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
  178. /* No need to retain BOs coming from the ->unref_list
  179. * because they are naturally unpurgeable.
  180. */
  181. drm_gem_object_get(&bo->base.base);
  182. kernel_state->bo[k++] = &bo->base.base;
  183. }
  184. }
  185. WARN_ON_ONCE(k != state->bo_count);
  186. if (exec[0])
  187. state->start_bin = exec[0]->ct0ca;
  188. if (exec[1])
  189. state->start_render = exec[1]->ct1ca;
  190. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  191. state->ct0ca = V3D_READ(V3D_CTNCA(0));
  192. state->ct0ea = V3D_READ(V3D_CTNEA(0));
  193. state->ct1ca = V3D_READ(V3D_CTNCA(1));
  194. state->ct1ea = V3D_READ(V3D_CTNEA(1));
  195. state->ct0cs = V3D_READ(V3D_CTNCS(0));
  196. state->ct1cs = V3D_READ(V3D_CTNCS(1));
  197. state->ct0ra0 = V3D_READ(V3D_CT00RA0);
  198. state->ct1ra0 = V3D_READ(V3D_CT01RA0);
  199. state->bpca = V3D_READ(V3D_BPCA);
  200. state->bpcs = V3D_READ(V3D_BPCS);
  201. state->bpoa = V3D_READ(V3D_BPOA);
  202. state->bpos = V3D_READ(V3D_BPOS);
  203. state->vpmbase = V3D_READ(V3D_VPMBASE);
  204. state->dbge = V3D_READ(V3D_DBGE);
  205. state->fdbgo = V3D_READ(V3D_FDBGO);
  206. state->fdbgb = V3D_READ(V3D_FDBGB);
  207. state->fdbgr = V3D_READ(V3D_FDBGR);
  208. state->fdbgs = V3D_READ(V3D_FDBGS);
  209. state->errstat = V3D_READ(V3D_ERRSTAT);
  210. /* We need to turn purgeable BOs into unpurgeable ones so that
  211. * userspace has a chance to dump the hang state before the kernel
  212. * decides to purge those BOs.
  213. * Note that BO consistency at dump time cannot be guaranteed. For
  214. * example, if the owner of these BOs decides to re-use them or mark
  215. * them purgeable again there's nothing we can do to prevent it.
  216. */
  217. for (i = 0; i < kernel_state->user_state.bo_count; i++) {
  218. struct vc4_bo *bo = to_vc4_bo(kernel_state->bo[i]);
  219. if (bo->madv == __VC4_MADV_NOTSUPP)
  220. continue;
  221. mutex_lock(&bo->madv_lock);
  222. if (!WARN_ON(bo->madv == __VC4_MADV_PURGED))
  223. bo->madv = VC4_MADV_WILLNEED;
  224. refcount_dec(&bo->usecnt);
  225. mutex_unlock(&bo->madv_lock);
  226. }
  227. spin_lock_irqsave(&vc4->job_lock, irqflags);
  228. if (vc4->hang_state) {
  229. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  230. vc4_free_hang_state(dev, kernel_state);
  231. } else {
  232. vc4->hang_state = kernel_state;
  233. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  234. }
  235. }
  236. static void
  237. vc4_reset(struct drm_device *dev)
  238. {
  239. struct vc4_dev *vc4 = to_vc4_dev(dev);
  240. DRM_INFO("Resetting GPU.\n");
  241. mutex_lock(&vc4->power_lock);
  242. if (vc4->power_refcount) {
  243. /* Power the device off and back on the by dropping the
  244. * reference on runtime PM.
  245. */
  246. pm_runtime_put_sync_suspend(&vc4->v3d->pdev->dev);
  247. pm_runtime_get_sync(&vc4->v3d->pdev->dev);
  248. }
  249. mutex_unlock(&vc4->power_lock);
  250. vc4_irq_reset(dev);
  251. /* Rearm the hangcheck -- another job might have been waiting
  252. * for our hung one to get kicked off, and vc4_irq_reset()
  253. * would have started it.
  254. */
  255. vc4_queue_hangcheck(dev);
  256. }
  257. static void
  258. vc4_reset_work(struct work_struct *work)
  259. {
  260. struct vc4_dev *vc4 =
  261. container_of(work, struct vc4_dev, hangcheck.reset_work);
  262. vc4_save_hang_state(&vc4->base);
  263. vc4_reset(&vc4->base);
  264. }
  265. static void
  266. vc4_hangcheck_elapsed(struct timer_list *t)
  267. {
  268. struct vc4_dev *vc4 = from_timer(vc4, t, hangcheck.timer);
  269. struct drm_device *dev = &vc4->base;
  270. uint32_t ct0ca, ct1ca;
  271. unsigned long irqflags;
  272. struct vc4_exec_info *bin_exec, *render_exec;
  273. spin_lock_irqsave(&vc4->job_lock, irqflags);
  274. bin_exec = vc4_first_bin_job(vc4);
  275. render_exec = vc4_first_render_job(vc4);
  276. /* If idle, we can stop watching for hangs. */
  277. if (!bin_exec && !render_exec) {
  278. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  279. return;
  280. }
  281. ct0ca = V3D_READ(V3D_CTNCA(0));
  282. ct1ca = V3D_READ(V3D_CTNCA(1));
  283. /* If we've made any progress in execution, rearm the timer
  284. * and wait.
  285. */
  286. if ((bin_exec && ct0ca != bin_exec->last_ct0ca) ||
  287. (render_exec && ct1ca != render_exec->last_ct1ca)) {
  288. if (bin_exec)
  289. bin_exec->last_ct0ca = ct0ca;
  290. if (render_exec)
  291. render_exec->last_ct1ca = ct1ca;
  292. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  293. vc4_queue_hangcheck(dev);
  294. return;
  295. }
  296. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  297. /* We've gone too long with no progress, reset. This has to
  298. * be done from a work struct, since resetting can sleep and
  299. * this timer hook isn't allowed to.
  300. */
  301. schedule_work(&vc4->hangcheck.reset_work);
  302. }
  303. static void
  304. submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
  305. {
  306. struct vc4_dev *vc4 = to_vc4_dev(dev);
  307. /* Set the current and end address of the control list.
  308. * Writing the end register is what starts the job.
  309. */
  310. V3D_WRITE(V3D_CTNCA(thread), start);
  311. V3D_WRITE(V3D_CTNEA(thread), end);
  312. }
  313. int
  314. vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
  315. bool interruptible)
  316. {
  317. struct vc4_dev *vc4 = to_vc4_dev(dev);
  318. int ret = 0;
  319. unsigned long timeout_expire;
  320. DEFINE_WAIT(wait);
  321. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  322. return -ENODEV;
  323. if (vc4->finished_seqno >= seqno)
  324. return 0;
  325. if (timeout_ns == 0)
  326. return -ETIME;
  327. timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
  328. trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
  329. for (;;) {
  330. prepare_to_wait(&vc4->job_wait_queue, &wait,
  331. interruptible ? TASK_INTERRUPTIBLE :
  332. TASK_UNINTERRUPTIBLE);
  333. if (interruptible && signal_pending(current)) {
  334. ret = -ERESTARTSYS;
  335. break;
  336. }
  337. if (vc4->finished_seqno >= seqno)
  338. break;
  339. if (timeout_ns != ~0ull) {
  340. if (time_after_eq(jiffies, timeout_expire)) {
  341. ret = -ETIME;
  342. break;
  343. }
  344. schedule_timeout(timeout_expire - jiffies);
  345. } else {
  346. schedule();
  347. }
  348. }
  349. finish_wait(&vc4->job_wait_queue, &wait);
  350. trace_vc4_wait_for_seqno_end(dev, seqno);
  351. return ret;
  352. }
  353. static void
  354. vc4_flush_caches(struct drm_device *dev)
  355. {
  356. struct vc4_dev *vc4 = to_vc4_dev(dev);
  357. /* Flush the GPU L2 caches. These caches sit on top of system
  358. * L3 (the 128kb or so shared with the CPU), and are
  359. * non-allocating in the L3.
  360. */
  361. V3D_WRITE(V3D_L2CACTL,
  362. V3D_L2CACTL_L2CCLR);
  363. V3D_WRITE(V3D_SLCACTL,
  364. VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  365. VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
  366. VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
  367. VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
  368. }
  369. static void
  370. vc4_flush_texture_caches(struct drm_device *dev)
  371. {
  372. struct vc4_dev *vc4 = to_vc4_dev(dev);
  373. V3D_WRITE(V3D_L2CACTL,
  374. V3D_L2CACTL_L2CCLR);
  375. V3D_WRITE(V3D_SLCACTL,
  376. VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  377. VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC));
  378. }
  379. /* Sets the registers for the next job to be actually be executed in
  380. * the hardware.
  381. *
  382. * The job_lock should be held during this.
  383. */
  384. void
  385. vc4_submit_next_bin_job(struct drm_device *dev)
  386. {
  387. struct vc4_dev *vc4 = to_vc4_dev(dev);
  388. struct vc4_exec_info *exec;
  389. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  390. return;
  391. again:
  392. exec = vc4_first_bin_job(vc4);
  393. if (!exec)
  394. return;
  395. vc4_flush_caches(dev);
  396. /* Only start the perfmon if it was not already started by a previous
  397. * job.
  398. */
  399. if (exec->perfmon && vc4->active_perfmon != exec->perfmon)
  400. vc4_perfmon_start(vc4, exec->perfmon);
  401. /* Either put the job in the binner if it uses the binner, or
  402. * immediately move it to the to-be-rendered queue.
  403. */
  404. if (exec->ct0ca != exec->ct0ea) {
  405. trace_vc4_submit_cl(dev, false, exec->seqno, exec->ct0ca,
  406. exec->ct0ea);
  407. submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
  408. } else {
  409. struct vc4_exec_info *next;
  410. vc4_move_job_to_render(dev, exec);
  411. next = vc4_first_bin_job(vc4);
  412. /* We can't start the next bin job if the previous job had a
  413. * different perfmon instance attached to it. The same goes
  414. * if one of them had a perfmon attached to it and the other
  415. * one doesn't.
  416. */
  417. if (next && next->perfmon == exec->perfmon)
  418. goto again;
  419. }
  420. }
  421. void
  422. vc4_submit_next_render_job(struct drm_device *dev)
  423. {
  424. struct vc4_dev *vc4 = to_vc4_dev(dev);
  425. struct vc4_exec_info *exec = vc4_first_render_job(vc4);
  426. if (!exec)
  427. return;
  428. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  429. return;
  430. /* A previous RCL may have written to one of our textures, and
  431. * our full cache flush at bin time may have occurred before
  432. * that RCL completed. Flush the texture cache now, but not
  433. * the instructions or uniforms (since we don't write those
  434. * from an RCL).
  435. */
  436. vc4_flush_texture_caches(dev);
  437. trace_vc4_submit_cl(dev, true, exec->seqno, exec->ct1ca, exec->ct1ea);
  438. submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
  439. }
  440. void
  441. vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
  442. {
  443. struct vc4_dev *vc4 = to_vc4_dev(dev);
  444. bool was_empty = list_empty(&vc4->render_job_list);
  445. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  446. return;
  447. list_move_tail(&exec->head, &vc4->render_job_list);
  448. if (was_empty)
  449. vc4_submit_next_render_job(dev);
  450. }
  451. static void
  452. vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  453. {
  454. struct vc4_bo *bo;
  455. unsigned i;
  456. for (i = 0; i < exec->bo_count; i++) {
  457. bo = to_vc4_bo(exec->bo[i]);
  458. bo->seqno = seqno;
  459. dma_resv_add_fence(bo->base.base.resv, exec->fence,
  460. DMA_RESV_USAGE_READ);
  461. }
  462. list_for_each_entry(bo, &exec->unref_list, unref_head) {
  463. bo->seqno = seqno;
  464. }
  465. for (i = 0; i < exec->rcl_write_bo_count; i++) {
  466. bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
  467. bo->write_seqno = seqno;
  468. dma_resv_add_fence(bo->base.base.resv, exec->fence,
  469. DMA_RESV_USAGE_WRITE);
  470. }
  471. }
  472. static void
  473. vc4_unlock_bo_reservations(struct drm_device *dev,
  474. struct vc4_exec_info *exec,
  475. struct ww_acquire_ctx *acquire_ctx)
  476. {
  477. int i;
  478. for (i = 0; i < exec->bo_count; i++)
  479. dma_resv_unlock(exec->bo[i]->resv);
  480. ww_acquire_fini(acquire_ctx);
  481. }
  482. /* Takes the reservation lock on all the BOs being referenced, so that
  483. * at queue submit time we can update the reservations.
  484. *
  485. * We don't lock the RCL the tile alloc/state BOs, or overflow memory
  486. * (all of which are on exec->unref_list). They're entirely private
  487. * to vc4, so we don't attach dma-buf fences to them.
  488. */
  489. static int
  490. vc4_lock_bo_reservations(struct drm_device *dev,
  491. struct vc4_exec_info *exec,
  492. struct ww_acquire_ctx *acquire_ctx)
  493. {
  494. int contended_lock = -1;
  495. int i, ret;
  496. struct drm_gem_object *bo;
  497. ww_acquire_init(acquire_ctx, &reservation_ww_class);
  498. retry:
  499. if (contended_lock != -1) {
  500. bo = exec->bo[contended_lock];
  501. ret = dma_resv_lock_slow_interruptible(bo->resv, acquire_ctx);
  502. if (ret) {
  503. ww_acquire_done(acquire_ctx);
  504. return ret;
  505. }
  506. }
  507. for (i = 0; i < exec->bo_count; i++) {
  508. if (i == contended_lock)
  509. continue;
  510. bo = exec->bo[i];
  511. ret = dma_resv_lock_interruptible(bo->resv, acquire_ctx);
  512. if (ret) {
  513. int j;
  514. for (j = 0; j < i; j++) {
  515. bo = exec->bo[j];
  516. dma_resv_unlock(bo->resv);
  517. }
  518. if (contended_lock != -1 && contended_lock >= i) {
  519. bo = exec->bo[contended_lock];
  520. dma_resv_unlock(bo->resv);
  521. }
  522. if (ret == -EDEADLK) {
  523. contended_lock = i;
  524. goto retry;
  525. }
  526. ww_acquire_done(acquire_ctx);
  527. return ret;
  528. }
  529. }
  530. ww_acquire_done(acquire_ctx);
  531. /* Reserve space for our shared (read-only) fence references,
  532. * before we commit the CL to the hardware.
  533. */
  534. for (i = 0; i < exec->bo_count; i++) {
  535. bo = exec->bo[i];
  536. ret = dma_resv_reserve_fences(bo->resv, 1);
  537. if (ret) {
  538. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  539. return ret;
  540. }
  541. }
  542. return 0;
  543. }
  544. /* Queues a struct vc4_exec_info for execution. If no job is
  545. * currently executing, then submits it.
  546. *
  547. * Unlike most GPUs, our hardware only handles one command list at a
  548. * time. To queue multiple jobs at once, we'd need to edit the
  549. * previous command list to have a jump to the new one at the end, and
  550. * then bump the end address. That's a change for a later date,
  551. * though.
  552. */
  553. static int
  554. vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
  555. struct ww_acquire_ctx *acquire_ctx,
  556. struct drm_syncobj *out_sync)
  557. {
  558. struct vc4_dev *vc4 = to_vc4_dev(dev);
  559. struct vc4_exec_info *renderjob;
  560. uint64_t seqno;
  561. unsigned long irqflags;
  562. struct vc4_fence *fence;
  563. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  564. if (!fence)
  565. return -ENOMEM;
  566. fence->dev = dev;
  567. spin_lock_irqsave(&vc4->job_lock, irqflags);
  568. seqno = ++vc4->emit_seqno;
  569. exec->seqno = seqno;
  570. dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
  571. vc4->dma_fence_context, exec->seqno);
  572. fence->seqno = exec->seqno;
  573. exec->fence = &fence->base;
  574. if (out_sync)
  575. drm_syncobj_replace_fence(out_sync, exec->fence);
  576. vc4_update_bo_seqnos(exec, seqno);
  577. vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
  578. list_add_tail(&exec->head, &vc4->bin_job_list);
  579. /* If no bin job was executing and if the render job (if any) has the
  580. * same perfmon as our job attached to it (or if both jobs don't have
  581. * perfmon activated), then kick ours off. Otherwise, it'll get
  582. * started when the previous job's flush/render done interrupt occurs.
  583. */
  584. renderjob = vc4_first_render_job(vc4);
  585. if (vc4_first_bin_job(vc4) == exec &&
  586. (!renderjob || renderjob->perfmon == exec->perfmon)) {
  587. vc4_submit_next_bin_job(dev);
  588. vc4_queue_hangcheck(dev);
  589. }
  590. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  591. return 0;
  592. }
  593. /**
  594. * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
  595. * referenced by the job.
  596. * @dev: DRM device
  597. * @file_priv: DRM file for this fd
  598. * @exec: V3D job being set up
  599. *
  600. * The command validator needs to reference BOs by their index within
  601. * the submitted job's BO list. This does the validation of the job's
  602. * BO list and reference counting for the lifetime of the job.
  603. */
  604. static int
  605. vc4_cl_lookup_bos(struct drm_device *dev,
  606. struct drm_file *file_priv,
  607. struct vc4_exec_info *exec)
  608. {
  609. struct drm_vc4_submit_cl *args = exec->args;
  610. int ret = 0;
  611. int i;
  612. exec->bo_count = args->bo_handle_count;
  613. if (!exec->bo_count) {
  614. /* See comment on bo_index for why we have to check
  615. * this.
  616. */
  617. DRM_DEBUG("Rendering requires BOs to validate\n");
  618. return -EINVAL;
  619. }
  620. ret = drm_gem_objects_lookup(file_priv, u64_to_user_ptr(args->bo_handles),
  621. exec->bo_count, &exec->bo);
  622. if (ret)
  623. goto fail_put_bo;
  624. for (i = 0; i < exec->bo_count; i++) {
  625. ret = vc4_bo_inc_usecnt(to_vc4_bo(exec->bo[i]));
  626. if (ret)
  627. goto fail_dec_usecnt;
  628. }
  629. return 0;
  630. fail_dec_usecnt:
  631. /* Decrease usecnt on acquired objects.
  632. * We cannot rely on vc4_complete_exec() to release resources here,
  633. * because vc4_complete_exec() has no information about which BO has
  634. * had its ->usecnt incremented.
  635. * To make things easier we just free everything explicitly and set
  636. * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
  637. * step.
  638. */
  639. for (i-- ; i >= 0; i--)
  640. vc4_bo_dec_usecnt(to_vc4_bo(exec->bo[i]));
  641. fail_put_bo:
  642. /* Release any reference to acquired objects. */
  643. for (i = 0; i < exec->bo_count && exec->bo[i]; i++)
  644. drm_gem_object_put(exec->bo[i]);
  645. kvfree(exec->bo);
  646. exec->bo = NULL;
  647. return ret;
  648. }
  649. static int
  650. vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  651. {
  652. struct drm_vc4_submit_cl *args = exec->args;
  653. struct vc4_dev *vc4 = to_vc4_dev(dev);
  654. void *temp = NULL;
  655. void *bin;
  656. int ret = 0;
  657. uint32_t bin_offset = 0;
  658. uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
  659. 16);
  660. uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
  661. uint32_t exec_size = uniforms_offset + args->uniforms_size;
  662. uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
  663. args->shader_rec_count);
  664. struct vc4_bo *bo;
  665. if (shader_rec_offset < args->bin_cl_size ||
  666. uniforms_offset < shader_rec_offset ||
  667. exec_size < uniforms_offset ||
  668. args->shader_rec_count >= (UINT_MAX /
  669. sizeof(struct vc4_shader_state)) ||
  670. temp_size < exec_size) {
  671. DRM_DEBUG("overflow in exec arguments\n");
  672. ret = -EINVAL;
  673. goto fail;
  674. }
  675. /* Allocate space where we'll store the copied in user command lists
  676. * and shader records.
  677. *
  678. * We don't just copy directly into the BOs because we need to
  679. * read the contents back for validation, and I think the
  680. * bo->vaddr is uncached access.
  681. */
  682. temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
  683. if (!temp) {
  684. drm_err(dev, "Failed to allocate storage for copying "
  685. "in bin/render CLs.\n");
  686. ret = -ENOMEM;
  687. goto fail;
  688. }
  689. bin = temp + bin_offset;
  690. exec->shader_rec_u = temp + shader_rec_offset;
  691. exec->uniforms_u = temp + uniforms_offset;
  692. exec->shader_state = temp + exec_size;
  693. exec->shader_state_size = args->shader_rec_count;
  694. if (copy_from_user(bin,
  695. u64_to_user_ptr(args->bin_cl),
  696. args->bin_cl_size)) {
  697. ret = -EFAULT;
  698. goto fail;
  699. }
  700. if (copy_from_user(exec->shader_rec_u,
  701. u64_to_user_ptr(args->shader_rec),
  702. args->shader_rec_size)) {
  703. ret = -EFAULT;
  704. goto fail;
  705. }
  706. if (copy_from_user(exec->uniforms_u,
  707. u64_to_user_ptr(args->uniforms),
  708. args->uniforms_size)) {
  709. ret = -EFAULT;
  710. goto fail;
  711. }
  712. bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
  713. if (IS_ERR(bo)) {
  714. drm_err(dev, "Couldn't allocate BO for binning\n");
  715. ret = PTR_ERR(bo);
  716. goto fail;
  717. }
  718. exec->exec_bo = &bo->base;
  719. list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
  720. &exec->unref_list);
  721. exec->ct0ca = exec->exec_bo->dma_addr + bin_offset;
  722. exec->bin_u = bin;
  723. exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
  724. exec->shader_rec_p = exec->exec_bo->dma_addr + shader_rec_offset;
  725. exec->shader_rec_size = args->shader_rec_size;
  726. exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
  727. exec->uniforms_p = exec->exec_bo->dma_addr + uniforms_offset;
  728. exec->uniforms_size = args->uniforms_size;
  729. ret = vc4_validate_bin_cl(dev,
  730. exec->exec_bo->vaddr + bin_offset,
  731. bin,
  732. exec);
  733. if (ret)
  734. goto fail;
  735. ret = vc4_validate_shader_recs(dev, exec);
  736. if (ret)
  737. goto fail;
  738. if (exec->found_tile_binning_mode_config_packet) {
  739. ret = vc4_v3d_bin_bo_get(vc4, &exec->bin_bo_used);
  740. if (ret)
  741. goto fail;
  742. }
  743. /* Block waiting on any previous rendering into the CS's VBO,
  744. * IB, or textures, so that pixels are actually written by the
  745. * time we try to read them.
  746. */
  747. ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);
  748. fail:
  749. kvfree(temp);
  750. return ret;
  751. }
  752. static void
  753. vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
  754. {
  755. struct vc4_dev *vc4 = to_vc4_dev(dev);
  756. unsigned long irqflags;
  757. unsigned i;
  758. /* If we got force-completed because of GPU reset rather than
  759. * through our IRQ handler, signal the fence now.
  760. */
  761. if (exec->fence) {
  762. dma_fence_signal(exec->fence);
  763. dma_fence_put(exec->fence);
  764. }
  765. if (exec->bo) {
  766. for (i = 0; i < exec->bo_count; i++) {
  767. struct vc4_bo *bo = to_vc4_bo(exec->bo[i]);
  768. vc4_bo_dec_usecnt(bo);
  769. drm_gem_object_put(exec->bo[i]);
  770. }
  771. kvfree(exec->bo);
  772. }
  773. while (!list_empty(&exec->unref_list)) {
  774. struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  775. struct vc4_bo, unref_head);
  776. list_del(&bo->unref_head);
  777. drm_gem_object_put(&bo->base.base);
  778. }
  779. /* Free up the allocation of any bin slots we used. */
  780. spin_lock_irqsave(&vc4->job_lock, irqflags);
  781. vc4->bin_alloc_used &= ~exec->bin_slots;
  782. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  783. /* Release the reference on the binner BO if needed. */
  784. if (exec->bin_bo_used)
  785. vc4_v3d_bin_bo_put(vc4);
  786. /* Release the reference we had on the perf monitor. */
  787. vc4_perfmon_put(exec->perfmon);
  788. vc4_v3d_pm_put(vc4);
  789. kfree(exec);
  790. }
  791. void
  792. vc4_job_handle_completed(struct vc4_dev *vc4)
  793. {
  794. unsigned long irqflags;
  795. struct vc4_seqno_cb *cb, *cb_temp;
  796. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  797. return;
  798. spin_lock_irqsave(&vc4->job_lock, irqflags);
  799. while (!list_empty(&vc4->job_done_list)) {
  800. struct vc4_exec_info *exec =
  801. list_first_entry(&vc4->job_done_list,
  802. struct vc4_exec_info, head);
  803. list_del(&exec->head);
  804. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  805. vc4_complete_exec(&vc4->base, exec);
  806. spin_lock_irqsave(&vc4->job_lock, irqflags);
  807. }
  808. list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  809. if (cb->seqno <= vc4->finished_seqno) {
  810. list_del_init(&cb->work.entry);
  811. schedule_work(&cb->work);
  812. }
  813. }
  814. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  815. }
  816. static void vc4_seqno_cb_work(struct work_struct *work)
  817. {
  818. struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  819. cb->func(cb);
  820. }
  821. int vc4_queue_seqno_cb(struct drm_device *dev,
  822. struct vc4_seqno_cb *cb, uint64_t seqno,
  823. void (*func)(struct vc4_seqno_cb *cb))
  824. {
  825. struct vc4_dev *vc4 = to_vc4_dev(dev);
  826. unsigned long irqflags;
  827. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  828. return -ENODEV;
  829. cb->func = func;
  830. INIT_WORK(&cb->work, vc4_seqno_cb_work);
  831. spin_lock_irqsave(&vc4->job_lock, irqflags);
  832. if (seqno > vc4->finished_seqno) {
  833. cb->seqno = seqno;
  834. list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  835. } else {
  836. schedule_work(&cb->work);
  837. }
  838. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  839. return 0;
  840. }
  841. /* Scheduled when any job has been completed, this walks the list of
  842. * jobs that had completed and unrefs their BOs and frees their exec
  843. * structs.
  844. */
  845. static void
  846. vc4_job_done_work(struct work_struct *work)
  847. {
  848. struct vc4_dev *vc4 =
  849. container_of(work, struct vc4_dev, job_done_work);
  850. vc4_job_handle_completed(vc4);
  851. }
  852. static int
  853. vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  854. uint64_t seqno,
  855. uint64_t *timeout_ns)
  856. {
  857. unsigned long start = jiffies;
  858. int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  859. if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  860. uint64_t delta = jiffies_to_nsecs(jiffies - start);
  861. if (*timeout_ns >= delta)
  862. *timeout_ns -= delta;
  863. }
  864. return ret;
  865. }
  866. int
  867. vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  868. struct drm_file *file_priv)
  869. {
  870. struct vc4_dev *vc4 = to_vc4_dev(dev);
  871. struct drm_vc4_wait_seqno *args = data;
  872. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  873. return -ENODEV;
  874. return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  875. &args->timeout_ns);
  876. }
  877. int
  878. vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  879. struct drm_file *file_priv)
  880. {
  881. struct vc4_dev *vc4 = to_vc4_dev(dev);
  882. int ret;
  883. struct drm_vc4_wait_bo *args = data;
  884. struct drm_gem_object *gem_obj;
  885. struct vc4_bo *bo;
  886. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  887. return -ENODEV;
  888. if (args->pad != 0)
  889. return -EINVAL;
  890. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  891. if (!gem_obj) {
  892. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  893. return -EINVAL;
  894. }
  895. bo = to_vc4_bo(gem_obj);
  896. ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
  897. &args->timeout_ns);
  898. drm_gem_object_put(gem_obj);
  899. return ret;
  900. }
  901. /**
  902. * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
  903. * @dev: DRM device
  904. * @data: ioctl argument
  905. * @file_priv: DRM file for this fd
  906. *
  907. * This is the main entrypoint for userspace to submit a 3D frame to
  908. * the GPU. Userspace provides the binner command list (if
  909. * applicable), and the kernel sets up the render command list to draw
  910. * to the framebuffer described in the ioctl, using the command lists
  911. * that the 3D engine's binner will produce.
  912. */
  913. int
  914. vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  915. struct drm_file *file_priv)
  916. {
  917. struct vc4_dev *vc4 = to_vc4_dev(dev);
  918. struct vc4_file *vc4file = file_priv->driver_priv;
  919. struct drm_vc4_submit_cl *args = data;
  920. struct drm_syncobj *out_sync = NULL;
  921. struct vc4_exec_info *exec;
  922. struct ww_acquire_ctx acquire_ctx;
  923. struct dma_fence *in_fence;
  924. int ret = 0;
  925. trace_vc4_submit_cl_ioctl(dev, args->bin_cl_size,
  926. args->shader_rec_size,
  927. args->bo_handle_count);
  928. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  929. return -ENODEV;
  930. if (!vc4->v3d) {
  931. DRM_DEBUG("VC4_SUBMIT_CL with no VC4 V3D probed\n");
  932. return -ENODEV;
  933. }
  934. if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
  935. VC4_SUBMIT_CL_FIXED_RCL_ORDER |
  936. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
  937. VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
  938. DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
  939. return -EINVAL;
  940. }
  941. if (args->pad2 != 0) {
  942. DRM_DEBUG("Invalid pad: 0x%08x\n", args->pad2);
  943. return -EINVAL;
  944. }
  945. exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  946. if (!exec)
  947. return -ENOMEM;
  948. exec->dev = vc4;
  949. ret = vc4_v3d_pm_get(vc4);
  950. if (ret) {
  951. kfree(exec);
  952. return ret;
  953. }
  954. exec->args = args;
  955. INIT_LIST_HEAD(&exec->unref_list);
  956. ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  957. if (ret)
  958. goto fail;
  959. if (args->perfmonid) {
  960. exec->perfmon = vc4_perfmon_find(vc4file,
  961. args->perfmonid);
  962. if (!exec->perfmon) {
  963. ret = -ENOENT;
  964. goto fail;
  965. }
  966. }
  967. if (args->in_sync) {
  968. ret = drm_syncobj_find_fence(file_priv, args->in_sync,
  969. 0, 0, &in_fence);
  970. if (ret)
  971. goto fail;
  972. /* When the fence (or fence array) is exclusively from our
  973. * context we can skip the wait since jobs are executed in
  974. * order of their submission through this ioctl and this can
  975. * only have fences from a prior job.
  976. */
  977. if (!dma_fence_match_context(in_fence,
  978. vc4->dma_fence_context)) {
  979. ret = dma_fence_wait(in_fence, true);
  980. if (ret) {
  981. dma_fence_put(in_fence);
  982. goto fail;
  983. }
  984. }
  985. dma_fence_put(in_fence);
  986. }
  987. if (exec->args->bin_cl_size != 0) {
  988. ret = vc4_get_bcl(dev, exec);
  989. if (ret)
  990. goto fail;
  991. } else {
  992. exec->ct0ca = 0;
  993. exec->ct0ea = 0;
  994. }
  995. ret = vc4_get_rcl(dev, exec);
  996. if (ret)
  997. goto fail;
  998. ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
  999. if (ret)
  1000. goto fail;
  1001. if (args->out_sync) {
  1002. out_sync = drm_syncobj_find(file_priv, args->out_sync);
  1003. if (!out_sync) {
  1004. ret = -EINVAL;
  1005. goto fail;
  1006. }
  1007. /* We replace the fence in out_sync in vc4_queue_submit since
  1008. * the render job could execute immediately after that call.
  1009. * If it finishes before our ioctl processing resumes the
  1010. * render job fence could already have been freed.
  1011. */
  1012. }
  1013. /* Clear this out of the struct we'll be putting in the queue,
  1014. * since it's part of our stack.
  1015. */
  1016. exec->args = NULL;
  1017. ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
  1018. /* The syncobj isn't part of the exec data and we need to free our
  1019. * reference even if job submission failed.
  1020. */
  1021. if (out_sync)
  1022. drm_syncobj_put(out_sync);
  1023. if (ret)
  1024. goto fail;
  1025. /* Return the seqno for our job. */
  1026. args->seqno = vc4->emit_seqno;
  1027. return 0;
  1028. fail:
  1029. vc4_complete_exec(&vc4->base, exec);
  1030. return ret;
  1031. }
  1032. static void vc4_gem_destroy(struct drm_device *dev, void *unused);
  1033. int vc4_gem_init(struct drm_device *dev)
  1034. {
  1035. struct vc4_dev *vc4 = to_vc4_dev(dev);
  1036. int ret;
  1037. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  1038. return -ENODEV;
  1039. vc4->dma_fence_context = dma_fence_context_alloc(1);
  1040. INIT_LIST_HEAD(&vc4->bin_job_list);
  1041. INIT_LIST_HEAD(&vc4->render_job_list);
  1042. INIT_LIST_HEAD(&vc4->job_done_list);
  1043. INIT_LIST_HEAD(&vc4->seqno_cb_list);
  1044. spin_lock_init(&vc4->job_lock);
  1045. INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  1046. timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
  1047. INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  1048. ret = drmm_mutex_init(dev, &vc4->power_lock);
  1049. if (ret)
  1050. return ret;
  1051. INIT_LIST_HEAD(&vc4->purgeable.list);
  1052. ret = drmm_mutex_init(dev, &vc4->purgeable.lock);
  1053. if (ret)
  1054. return ret;
  1055. return drmm_add_action_or_reset(dev, vc4_gem_destroy, NULL);
  1056. }
  1057. static void vc4_gem_destroy(struct drm_device *dev, void *unused)
  1058. {
  1059. struct vc4_dev *vc4 = to_vc4_dev(dev);
  1060. /* Waiting for exec to finish would need to be done before
  1061. * unregistering V3D.
  1062. */
  1063. WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  1064. /* V3D should already have disabled its interrupt and cleared
  1065. * the overflow allocation registers. Now free the object.
  1066. */
  1067. if (vc4->bin_bo) {
  1068. drm_gem_object_put(&vc4->bin_bo->base.base);
  1069. vc4->bin_bo = NULL;
  1070. }
  1071. if (vc4->hang_state)
  1072. vc4_free_hang_state(dev, vc4->hang_state);
  1073. }
  1074. int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
  1075. struct drm_file *file_priv)
  1076. {
  1077. struct vc4_dev *vc4 = to_vc4_dev(dev);
  1078. struct drm_vc4_gem_madvise *args = data;
  1079. struct drm_gem_object *gem_obj;
  1080. struct vc4_bo *bo;
  1081. int ret;
  1082. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  1083. return -ENODEV;
  1084. switch (args->madv) {
  1085. case VC4_MADV_DONTNEED:
  1086. case VC4_MADV_WILLNEED:
  1087. break;
  1088. default:
  1089. return -EINVAL;
  1090. }
  1091. if (args->pad != 0)
  1092. return -EINVAL;
  1093. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  1094. if (!gem_obj) {
  1095. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  1096. return -ENOENT;
  1097. }
  1098. bo = to_vc4_bo(gem_obj);
  1099. /* Only BOs exposed to userspace can be purged. */
  1100. if (bo->madv == __VC4_MADV_NOTSUPP) {
  1101. DRM_DEBUG("madvise not supported on this BO\n");
  1102. ret = -EINVAL;
  1103. goto out_put_gem;
  1104. }
  1105. /* Not sure it's safe to purge imported BOs. Let's just assume it's
  1106. * not until proven otherwise.
  1107. */
  1108. if (gem_obj->import_attach) {
  1109. DRM_DEBUG("madvise not supported on imported BOs\n");
  1110. ret = -EINVAL;
  1111. goto out_put_gem;
  1112. }
  1113. mutex_lock(&bo->madv_lock);
  1114. if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED &&
  1115. !refcount_read(&bo->usecnt)) {
  1116. /* If the BO is about to be marked as purgeable, is not used
  1117. * and is not already purgeable or purged, add it to the
  1118. * purgeable list.
  1119. */
  1120. vc4_bo_add_to_purgeable_pool(bo);
  1121. } else if (args->madv == VC4_MADV_WILLNEED &&
  1122. bo->madv == VC4_MADV_DONTNEED &&
  1123. !refcount_read(&bo->usecnt)) {
  1124. /* The BO has not been purged yet, just remove it from
  1125. * the purgeable list.
  1126. */
  1127. vc4_bo_remove_from_purgeable_pool(bo);
  1128. }
  1129. /* Save the purged state. */
  1130. args->retained = bo->madv != __VC4_MADV_PURGED;
  1131. /* Update internal madv state only if the bo was not purged. */
  1132. if (bo->madv != __VC4_MADV_PURGED)
  1133. bo->madv = args->madv;
  1134. mutex_unlock(&bo->madv_lock);
  1135. ret = 0;
  1136. out_put_gem:
  1137. drm_gem_object_put(gem_obj);
  1138. return ret;
  1139. }