i915_request.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  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. */
  24. #include <linux/prefetch.h>
  25. #include <linux/dma-fence-array.h>
  26. #include <linux/sched.h>
  27. #include <linux/sched/clock.h>
  28. #include <linux/sched/signal.h>
  29. #include "i915_drv.h"
  30. static const char *i915_fence_get_driver_name(struct dma_fence *fence)
  31. {
  32. return "i915";
  33. }
  34. static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
  35. {
  36. /*
  37. * The timeline struct (as part of the ppgtt underneath a context)
  38. * may be freed when the request is no longer in use by the GPU.
  39. * We could extend the life of a context to beyond that of all
  40. * fences, possibly keeping the hw resource around indefinitely,
  41. * or we just give them a false name. Since
  42. * dma_fence_ops.get_timeline_name is a debug feature, the occasional
  43. * lie seems justifiable.
  44. */
  45. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  46. return "signaled";
  47. return to_request(fence)->timeline->name;
  48. }
  49. static bool i915_fence_signaled(struct dma_fence *fence)
  50. {
  51. return i915_request_completed(to_request(fence));
  52. }
  53. static bool i915_fence_enable_signaling(struct dma_fence *fence)
  54. {
  55. return intel_engine_enable_signaling(to_request(fence), true);
  56. }
  57. static signed long i915_fence_wait(struct dma_fence *fence,
  58. bool interruptible,
  59. signed long timeout)
  60. {
  61. return i915_request_wait(to_request(fence), interruptible, timeout);
  62. }
  63. static void i915_fence_release(struct dma_fence *fence)
  64. {
  65. struct i915_request *rq = to_request(fence);
  66. /*
  67. * The request is put onto a RCU freelist (i.e. the address
  68. * is immediately reused), mark the fences as being freed now.
  69. * Otherwise the debugobjects for the fences are only marked as
  70. * freed when the slab cache itself is freed, and so we would get
  71. * caught trying to reuse dead objects.
  72. */
  73. i915_sw_fence_fini(&rq->submit);
  74. kmem_cache_free(rq->i915->requests, rq);
  75. }
  76. const struct dma_fence_ops i915_fence_ops = {
  77. .get_driver_name = i915_fence_get_driver_name,
  78. .get_timeline_name = i915_fence_get_timeline_name,
  79. .enable_signaling = i915_fence_enable_signaling,
  80. .signaled = i915_fence_signaled,
  81. .wait = i915_fence_wait,
  82. .release = i915_fence_release,
  83. };
  84. static inline void
  85. i915_request_remove_from_client(struct i915_request *request)
  86. {
  87. struct drm_i915_file_private *file_priv;
  88. file_priv = request->file_priv;
  89. if (!file_priv)
  90. return;
  91. spin_lock(&file_priv->mm.lock);
  92. if (request->file_priv) {
  93. list_del(&request->client_link);
  94. request->file_priv = NULL;
  95. }
  96. spin_unlock(&file_priv->mm.lock);
  97. }
  98. static struct i915_dependency *
  99. i915_dependency_alloc(struct drm_i915_private *i915)
  100. {
  101. return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
  102. }
  103. static void
  104. i915_dependency_free(struct drm_i915_private *i915,
  105. struct i915_dependency *dep)
  106. {
  107. kmem_cache_free(i915->dependencies, dep);
  108. }
  109. static void
  110. __i915_sched_node_add_dependency(struct i915_sched_node *node,
  111. struct i915_sched_node *signal,
  112. struct i915_dependency *dep,
  113. unsigned long flags)
  114. {
  115. INIT_LIST_HEAD(&dep->dfs_link);
  116. list_add(&dep->wait_link, &signal->waiters_list);
  117. list_add(&dep->signal_link, &node->signalers_list);
  118. dep->signaler = signal;
  119. dep->flags = flags;
  120. }
  121. static int
  122. i915_sched_node_add_dependency(struct drm_i915_private *i915,
  123. struct i915_sched_node *node,
  124. struct i915_sched_node *signal)
  125. {
  126. struct i915_dependency *dep;
  127. dep = i915_dependency_alloc(i915);
  128. if (!dep)
  129. return -ENOMEM;
  130. __i915_sched_node_add_dependency(node, signal, dep,
  131. I915_DEPENDENCY_ALLOC);
  132. return 0;
  133. }
  134. static void
  135. i915_sched_node_fini(struct drm_i915_private *i915,
  136. struct i915_sched_node *node)
  137. {
  138. struct i915_dependency *dep, *tmp;
  139. GEM_BUG_ON(!list_empty(&node->link));
  140. /*
  141. * Everyone we depended upon (the fences we wait to be signaled)
  142. * should retire before us and remove themselves from our list.
  143. * However, retirement is run independently on each timeline and
  144. * so we may be called out-of-order.
  145. */
  146. list_for_each_entry_safe(dep, tmp, &node->signalers_list, signal_link) {
  147. GEM_BUG_ON(!i915_sched_node_signaled(dep->signaler));
  148. GEM_BUG_ON(!list_empty(&dep->dfs_link));
  149. list_del(&dep->wait_link);
  150. if (dep->flags & I915_DEPENDENCY_ALLOC)
  151. i915_dependency_free(i915, dep);
  152. }
  153. /* Remove ourselves from everyone who depends upon us */
  154. list_for_each_entry_safe(dep, tmp, &node->waiters_list, wait_link) {
  155. GEM_BUG_ON(dep->signaler != node);
  156. GEM_BUG_ON(!list_empty(&dep->dfs_link));
  157. list_del(&dep->signal_link);
  158. if (dep->flags & I915_DEPENDENCY_ALLOC)
  159. i915_dependency_free(i915, dep);
  160. }
  161. }
  162. static void
  163. i915_sched_node_init(struct i915_sched_node *node)
  164. {
  165. INIT_LIST_HEAD(&node->signalers_list);
  166. INIT_LIST_HEAD(&node->waiters_list);
  167. INIT_LIST_HEAD(&node->link);
  168. node->attr.priority = I915_PRIORITY_INVALID;
  169. }
  170. static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno)
  171. {
  172. struct intel_engine_cs *engine;
  173. struct i915_timeline *timeline;
  174. enum intel_engine_id id;
  175. int ret;
  176. /* Carefully retire all requests without writing to the rings */
  177. ret = i915_gem_wait_for_idle(i915,
  178. I915_WAIT_INTERRUPTIBLE |
  179. I915_WAIT_LOCKED,
  180. MAX_SCHEDULE_TIMEOUT);
  181. if (ret)
  182. return ret;
  183. GEM_BUG_ON(i915->gt.active_requests);
  184. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  185. for_each_engine(engine, i915, id) {
  186. GEM_TRACE("%s seqno %d (current %d) -> %d\n",
  187. engine->name,
  188. engine->timeline.seqno,
  189. intel_engine_get_seqno(engine),
  190. seqno);
  191. if (!i915_seqno_passed(seqno, engine->timeline.seqno)) {
  192. /* Flush any waiters before we reuse the seqno */
  193. intel_engine_disarm_breadcrumbs(engine);
  194. intel_engine_init_hangcheck(engine);
  195. GEM_BUG_ON(!list_empty(&engine->breadcrumbs.signals));
  196. }
  197. /* Check we are idle before we fiddle with hw state! */
  198. GEM_BUG_ON(!intel_engine_is_idle(engine));
  199. GEM_BUG_ON(i915_gem_active_isset(&engine->timeline.last_request));
  200. /* Finally reset hw state */
  201. intel_engine_init_global_seqno(engine, seqno);
  202. engine->timeline.seqno = seqno;
  203. }
  204. list_for_each_entry(timeline, &i915->gt.timelines, link)
  205. memset(timeline->global_sync, 0, sizeof(timeline->global_sync));
  206. i915->gt.request_serial = seqno;
  207. return 0;
  208. }
  209. int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
  210. {
  211. struct drm_i915_private *i915 = to_i915(dev);
  212. lockdep_assert_held(&i915->drm.struct_mutex);
  213. if (seqno == 0)
  214. return -EINVAL;
  215. /* HWS page needs to be set less than what we will inject to ring */
  216. return reset_all_global_seqno(i915, seqno - 1);
  217. }
  218. static int reserve_gt(struct drm_i915_private *i915)
  219. {
  220. int ret;
  221. /*
  222. * Reservation is fine until we may need to wrap around
  223. *
  224. * By incrementing the serial for every request, we know that no
  225. * individual engine may exceed that serial (as each is reset to 0
  226. * on any wrap). This protects even the most pessimistic of migrations
  227. * of every request from all engines onto just one.
  228. */
  229. while (unlikely(++i915->gt.request_serial == 0)) {
  230. ret = reset_all_global_seqno(i915, 0);
  231. if (ret) {
  232. i915->gt.request_serial--;
  233. return ret;
  234. }
  235. }
  236. if (!i915->gt.active_requests++)
  237. i915_gem_unpark(i915);
  238. return 0;
  239. }
  240. static void unreserve_gt(struct drm_i915_private *i915)
  241. {
  242. GEM_BUG_ON(!i915->gt.active_requests);
  243. if (!--i915->gt.active_requests)
  244. i915_gem_park(i915);
  245. }
  246. void i915_gem_retire_noop(struct i915_gem_active *active,
  247. struct i915_request *request)
  248. {
  249. /* Space left intentionally blank */
  250. }
  251. static void advance_ring(struct i915_request *request)
  252. {
  253. struct intel_ring *ring = request->ring;
  254. unsigned int tail;
  255. /*
  256. * We know the GPU must have read the request to have
  257. * sent us the seqno + interrupt, so use the position
  258. * of tail of the request to update the last known position
  259. * of the GPU head.
  260. *
  261. * Note this requires that we are always called in request
  262. * completion order.
  263. */
  264. GEM_BUG_ON(!list_is_first(&request->ring_link, &ring->request_list));
  265. if (list_is_last(&request->ring_link, &ring->request_list)) {
  266. /*
  267. * We may race here with execlists resubmitting this request
  268. * as we retire it. The resubmission will move the ring->tail
  269. * forwards (to request->wa_tail). We either read the
  270. * current value that was written to hw, or the value that
  271. * is just about to be. Either works, if we miss the last two
  272. * noops - they are safe to be replayed on a reset.
  273. */
  274. GEM_TRACE("marking %s as inactive\n", ring->timeline->name);
  275. tail = READ_ONCE(request->tail);
  276. list_del(&ring->active_link);
  277. } else {
  278. tail = request->postfix;
  279. }
  280. list_del_init(&request->ring_link);
  281. ring->head = tail;
  282. }
  283. static void free_capture_list(struct i915_request *request)
  284. {
  285. struct i915_capture_list *capture;
  286. capture = request->capture_list;
  287. while (capture) {
  288. struct i915_capture_list *next = capture->next;
  289. kfree(capture);
  290. capture = next;
  291. }
  292. }
  293. static void __retire_engine_request(struct intel_engine_cs *engine,
  294. struct i915_request *rq)
  295. {
  296. GEM_TRACE("%s(%s) fence %llx:%d, global=%d, current %d\n",
  297. __func__, engine->name,
  298. rq->fence.context, rq->fence.seqno,
  299. rq->global_seqno,
  300. intel_engine_get_seqno(engine));
  301. GEM_BUG_ON(!i915_request_completed(rq));
  302. local_irq_disable();
  303. spin_lock(&engine->timeline.lock);
  304. GEM_BUG_ON(!list_is_first(&rq->link, &engine->timeline.requests));
  305. list_del_init(&rq->link);
  306. spin_unlock(&engine->timeline.lock);
  307. spin_lock(&rq->lock);
  308. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
  309. dma_fence_signal_locked(&rq->fence);
  310. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags))
  311. intel_engine_cancel_signaling(rq);
  312. if (rq->waitboost) {
  313. GEM_BUG_ON(!atomic_read(&rq->i915->gt_pm.rps.num_waiters));
  314. atomic_dec(&rq->i915->gt_pm.rps.num_waiters);
  315. }
  316. spin_unlock(&rq->lock);
  317. local_irq_enable();
  318. /*
  319. * The backing object for the context is done after switching to the
  320. * *next* context. Therefore we cannot retire the previous context until
  321. * the next context has already started running. However, since we
  322. * cannot take the required locks at i915_request_submit() we
  323. * defer the unpinning of the active context to now, retirement of
  324. * the subsequent request.
  325. */
  326. if (engine->last_retired_context)
  327. intel_context_unpin(engine->last_retired_context);
  328. engine->last_retired_context = rq->hw_context;
  329. }
  330. static void __retire_engine_upto(struct intel_engine_cs *engine,
  331. struct i915_request *rq)
  332. {
  333. struct i915_request *tmp;
  334. if (list_empty(&rq->link))
  335. return;
  336. do {
  337. tmp = list_first_entry(&engine->timeline.requests,
  338. typeof(*tmp), link);
  339. GEM_BUG_ON(tmp->engine != engine);
  340. __retire_engine_request(engine, tmp);
  341. } while (tmp != rq);
  342. }
  343. static void i915_request_retire(struct i915_request *request)
  344. {
  345. struct i915_gem_active *active, *next;
  346. GEM_TRACE("%s fence %llx:%d, global=%d, current %d\n",
  347. request->engine->name,
  348. request->fence.context, request->fence.seqno,
  349. request->global_seqno,
  350. intel_engine_get_seqno(request->engine));
  351. lockdep_assert_held(&request->i915->drm.struct_mutex);
  352. GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
  353. GEM_BUG_ON(!i915_request_completed(request));
  354. trace_i915_request_retire(request);
  355. advance_ring(request);
  356. free_capture_list(request);
  357. /*
  358. * Walk through the active list, calling retire on each. This allows
  359. * objects to track their GPU activity and mark themselves as idle
  360. * when their *last* active request is completed (updating state
  361. * tracking lists for eviction, active references for GEM, etc).
  362. *
  363. * As the ->retire() may free the node, we decouple it first and
  364. * pass along the auxiliary information (to avoid dereferencing
  365. * the node after the callback).
  366. */
  367. list_for_each_entry_safe(active, next, &request->active_list, link) {
  368. /*
  369. * In microbenchmarks or focusing upon time inside the kernel,
  370. * we may spend an inordinate amount of time simply handling
  371. * the retirement of requests and processing their callbacks.
  372. * Of which, this loop itself is particularly hot due to the
  373. * cache misses when jumping around the list of i915_gem_active.
  374. * So we try to keep this loop as streamlined as possible and
  375. * also prefetch the next i915_gem_active to try and hide
  376. * the likely cache miss.
  377. */
  378. prefetchw(next);
  379. INIT_LIST_HEAD(&active->link);
  380. RCU_INIT_POINTER(active->request, NULL);
  381. active->retire(active, request);
  382. }
  383. i915_request_remove_from_client(request);
  384. /* Retirement decays the ban score as it is a sign of ctx progress */
  385. atomic_dec_if_positive(&request->gem_context->ban_score);
  386. intel_context_unpin(request->hw_context);
  387. __retire_engine_upto(request->engine, request);
  388. unreserve_gt(request->i915);
  389. i915_sched_node_fini(request->i915, &request->sched);
  390. i915_request_put(request);
  391. }
  392. void i915_request_retire_upto(struct i915_request *rq)
  393. {
  394. struct intel_ring *ring = rq->ring;
  395. struct i915_request *tmp;
  396. GEM_TRACE("%s fence %llx:%d, global=%d, current %d\n",
  397. rq->engine->name,
  398. rq->fence.context, rq->fence.seqno,
  399. rq->global_seqno,
  400. intel_engine_get_seqno(rq->engine));
  401. lockdep_assert_held(&rq->i915->drm.struct_mutex);
  402. GEM_BUG_ON(!i915_request_completed(rq));
  403. if (list_empty(&rq->ring_link))
  404. return;
  405. do {
  406. tmp = list_first_entry(&ring->request_list,
  407. typeof(*tmp), ring_link);
  408. i915_request_retire(tmp);
  409. } while (tmp != rq);
  410. }
  411. static u32 timeline_get_seqno(struct i915_timeline *tl)
  412. {
  413. return ++tl->seqno;
  414. }
  415. static void move_to_timeline(struct i915_request *request,
  416. struct i915_timeline *timeline)
  417. {
  418. GEM_BUG_ON(request->timeline == &request->engine->timeline);
  419. lockdep_assert_held(&request->engine->timeline.lock);
  420. spin_lock(&request->timeline->lock);
  421. list_move_tail(&request->link, &timeline->requests);
  422. spin_unlock(&request->timeline->lock);
  423. }
  424. void __i915_request_submit(struct i915_request *request)
  425. {
  426. struct intel_engine_cs *engine = request->engine;
  427. u32 seqno;
  428. GEM_TRACE("%s fence %llx:%d -> global=%d, current %d\n",
  429. engine->name,
  430. request->fence.context, request->fence.seqno,
  431. engine->timeline.seqno + 1,
  432. intel_engine_get_seqno(engine));
  433. GEM_BUG_ON(!irqs_disabled());
  434. lockdep_assert_held(&engine->timeline.lock);
  435. GEM_BUG_ON(request->global_seqno);
  436. seqno = timeline_get_seqno(&engine->timeline);
  437. GEM_BUG_ON(!seqno);
  438. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
  439. /* We may be recursing from the signal callback of another i915 fence */
  440. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  441. request->global_seqno = seqno;
  442. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  443. intel_engine_enable_signaling(request, false);
  444. spin_unlock(&request->lock);
  445. engine->emit_breadcrumb(request,
  446. request->ring->vaddr + request->postfix);
  447. /* Transfer from per-context onto the global per-engine timeline */
  448. move_to_timeline(request, &engine->timeline);
  449. trace_i915_request_execute(request);
  450. wake_up_all(&request->execute);
  451. }
  452. void i915_request_submit(struct i915_request *request)
  453. {
  454. struct intel_engine_cs *engine = request->engine;
  455. unsigned long flags;
  456. /* Will be called from irq-context when using foreign fences. */
  457. spin_lock_irqsave(&engine->timeline.lock, flags);
  458. __i915_request_submit(request);
  459. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  460. }
  461. void __i915_request_unsubmit(struct i915_request *request)
  462. {
  463. struct intel_engine_cs *engine = request->engine;
  464. GEM_TRACE("%s fence %llx:%d <- global=%d, current %d\n",
  465. engine->name,
  466. request->fence.context, request->fence.seqno,
  467. request->global_seqno,
  468. intel_engine_get_seqno(engine));
  469. GEM_BUG_ON(!irqs_disabled());
  470. lockdep_assert_held(&engine->timeline.lock);
  471. /*
  472. * Only unwind in reverse order, required so that the per-context list
  473. * is kept in seqno/ring order.
  474. */
  475. GEM_BUG_ON(!request->global_seqno);
  476. GEM_BUG_ON(request->global_seqno != engine->timeline.seqno);
  477. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine),
  478. request->global_seqno));
  479. engine->timeline.seqno--;
  480. /* We may be recursing from the signal callback of another i915 fence */
  481. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  482. request->global_seqno = 0;
  483. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  484. intel_engine_cancel_signaling(request);
  485. spin_unlock(&request->lock);
  486. /* Transfer back from the global per-engine timeline to per-context */
  487. move_to_timeline(request, request->timeline);
  488. /*
  489. * We don't need to wake_up any waiters on request->execute, they
  490. * will get woken by any other event or us re-adding this request
  491. * to the engine timeline (__i915_request_submit()). The waiters
  492. * should be quite adapt at finding that the request now has a new
  493. * global_seqno to the one they went to sleep on.
  494. */
  495. }
  496. void i915_request_unsubmit(struct i915_request *request)
  497. {
  498. struct intel_engine_cs *engine = request->engine;
  499. unsigned long flags;
  500. /* Will be called from irq-context when using foreign fences. */
  501. spin_lock_irqsave(&engine->timeline.lock, flags);
  502. __i915_request_unsubmit(request);
  503. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  504. }
  505. static int __i915_sw_fence_call
  506. submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  507. {
  508. struct i915_request *request =
  509. container_of(fence, typeof(*request), submit);
  510. switch (state) {
  511. case FENCE_COMPLETE:
  512. trace_i915_request_submit(request);
  513. /*
  514. * We need to serialize use of the submit_request() callback
  515. * with its hotplugging performed during an emergency
  516. * i915_gem_set_wedged(). We use the RCU mechanism to mark the
  517. * critical section in order to force i915_gem_set_wedged() to
  518. * wait until the submit_request() is completed before
  519. * proceeding.
  520. */
  521. rcu_read_lock();
  522. request->engine->submit_request(request);
  523. rcu_read_unlock();
  524. break;
  525. case FENCE_FREE:
  526. i915_request_put(request);
  527. break;
  528. }
  529. return NOTIFY_DONE;
  530. }
  531. /**
  532. * i915_request_alloc - allocate a request structure
  533. *
  534. * @engine: engine that we wish to issue the request on.
  535. * @ctx: context that the request will be associated with.
  536. *
  537. * Returns a pointer to the allocated request if successful,
  538. * or an error code if not.
  539. */
  540. struct i915_request *
  541. i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx)
  542. {
  543. struct drm_i915_private *i915 = engine->i915;
  544. struct i915_request *rq;
  545. struct intel_context *ce;
  546. int ret;
  547. lockdep_assert_held(&i915->drm.struct_mutex);
  548. /*
  549. * Preempt contexts are reserved for exclusive use to inject a
  550. * preemption context switch. They are never to be used for any trivial
  551. * request!
  552. */
  553. GEM_BUG_ON(ctx == i915->preempt_context);
  554. /*
  555. * ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  556. * EIO if the GPU is already wedged.
  557. */
  558. if (i915_terminally_wedged(&i915->gpu_error))
  559. return ERR_PTR(-EIO);
  560. /*
  561. * Pinning the contexts may generate requests in order to acquire
  562. * GGTT space, so do this first before we reserve a seqno for
  563. * ourselves.
  564. */
  565. ce = intel_context_pin(ctx, engine);
  566. if (IS_ERR(ce))
  567. return ERR_CAST(ce);
  568. ret = reserve_gt(i915);
  569. if (ret)
  570. goto err_unpin;
  571. ret = intel_ring_wait_for_space(ce->ring, MIN_SPACE_FOR_ADD_REQUEST);
  572. if (ret)
  573. goto err_unreserve;
  574. /* Move our oldest request to the slab-cache (if not in use!) */
  575. rq = list_first_entry(&ce->ring->request_list, typeof(*rq), ring_link);
  576. if (!list_is_last(&rq->ring_link, &ce->ring->request_list) &&
  577. i915_request_completed(rq))
  578. i915_request_retire(rq);
  579. /*
  580. * Beware: Dragons be flying overhead.
  581. *
  582. * We use RCU to look up requests in flight. The lookups may
  583. * race with the request being allocated from the slab freelist.
  584. * That is the request we are writing to here, may be in the process
  585. * of being read by __i915_gem_active_get_rcu(). As such,
  586. * we have to be very careful when overwriting the contents. During
  587. * the RCU lookup, we change chase the request->engine pointer,
  588. * read the request->global_seqno and increment the reference count.
  589. *
  590. * The reference count is incremented atomically. If it is zero,
  591. * the lookup knows the request is unallocated and complete. Otherwise,
  592. * it is either still in use, or has been reallocated and reset
  593. * with dma_fence_init(). This increment is safe for release as we
  594. * check that the request we have a reference to and matches the active
  595. * request.
  596. *
  597. * Before we increment the refcount, we chase the request->engine
  598. * pointer. We must not call kmem_cache_zalloc() or else we set
  599. * that pointer to NULL and cause a crash during the lookup. If
  600. * we see the request is completed (based on the value of the
  601. * old engine and seqno), the lookup is complete and reports NULL.
  602. * If we decide the request is not completed (new engine or seqno),
  603. * then we grab a reference and double check that it is still the
  604. * active request - which it won't be and restart the lookup.
  605. *
  606. * Do not use kmem_cache_zalloc() here!
  607. */
  608. rq = kmem_cache_alloc(i915->requests,
  609. GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  610. if (unlikely(!rq)) {
  611. /* Ratelimit ourselves to prevent oom from malicious clients */
  612. ret = i915_gem_wait_for_idle(i915,
  613. I915_WAIT_LOCKED |
  614. I915_WAIT_INTERRUPTIBLE,
  615. MAX_SCHEDULE_TIMEOUT);
  616. if (ret)
  617. goto err_unreserve;
  618. /*
  619. * We've forced the client to stall and catch up with whatever
  620. * backlog there might have been. As we are assuming that we
  621. * caused the mempressure, now is an opportune time to
  622. * recover as much memory from the request pool as is possible.
  623. * Having already penalized the client to stall, we spend
  624. * a little extra time to re-optimise page allocation.
  625. */
  626. kmem_cache_shrink(i915->requests);
  627. rcu_barrier(); /* Recover the TYPESAFE_BY_RCU pages */
  628. rq = kmem_cache_alloc(i915->requests, GFP_KERNEL);
  629. if (!rq) {
  630. ret = -ENOMEM;
  631. goto err_unreserve;
  632. }
  633. }
  634. INIT_LIST_HEAD(&rq->active_list);
  635. rq->i915 = i915;
  636. rq->engine = engine;
  637. rq->gem_context = ctx;
  638. rq->hw_context = ce;
  639. rq->ring = ce->ring;
  640. rq->timeline = ce->ring->timeline;
  641. GEM_BUG_ON(rq->timeline == &engine->timeline);
  642. spin_lock_init(&rq->lock);
  643. dma_fence_init(&rq->fence,
  644. &i915_fence_ops,
  645. &rq->lock,
  646. rq->timeline->fence_context,
  647. timeline_get_seqno(rq->timeline));
  648. /* We bump the ref for the fence chain */
  649. i915_sw_fence_init(&i915_request_get(rq)->submit, submit_notify);
  650. init_waitqueue_head(&rq->execute);
  651. i915_sched_node_init(&rq->sched);
  652. /* No zalloc, must clear what we need by hand */
  653. rq->global_seqno = 0;
  654. rq->signaling.wait.seqno = 0;
  655. rq->file_priv = NULL;
  656. rq->batch = NULL;
  657. rq->capture_list = NULL;
  658. rq->waitboost = false;
  659. /*
  660. * Reserve space in the ring buffer for all the commands required to
  661. * eventually emit this request. This is to guarantee that the
  662. * i915_request_add() call can't fail. Note that the reserve may need
  663. * to be redone if the request is not actually submitted straight
  664. * away, e.g. because a GPU scheduler has deferred it.
  665. */
  666. rq->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  667. GEM_BUG_ON(rq->reserved_space < engine->emit_breadcrumb_sz);
  668. /*
  669. * Record the position of the start of the request so that
  670. * should we detect the updated seqno part-way through the
  671. * GPU processing the request, we never over-estimate the
  672. * position of the head.
  673. */
  674. rq->head = rq->ring->emit;
  675. /* Unconditionally invalidate GPU caches and TLBs. */
  676. ret = engine->emit_flush(rq, EMIT_INVALIDATE);
  677. if (ret)
  678. goto err_unwind;
  679. ret = engine->request_alloc(rq);
  680. if (ret)
  681. goto err_unwind;
  682. /* Keep a second pin for the dual retirement along engine and ring */
  683. __intel_context_pin(ce);
  684. rq->infix = rq->ring->emit; /* end of header; start of user payload */
  685. /* Check that we didn't interrupt ourselves with a new request */
  686. GEM_BUG_ON(rq->timeline->seqno != rq->fence.seqno);
  687. return rq;
  688. err_unwind:
  689. ce->ring->emit = rq->head;
  690. /* Make sure we didn't add ourselves to external state before freeing */
  691. GEM_BUG_ON(!list_empty(&rq->active_list));
  692. GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
  693. GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
  694. kmem_cache_free(i915->requests, rq);
  695. err_unreserve:
  696. unreserve_gt(i915);
  697. err_unpin:
  698. intel_context_unpin(ce);
  699. return ERR_PTR(ret);
  700. }
  701. static int
  702. i915_request_await_request(struct i915_request *to, struct i915_request *from)
  703. {
  704. int ret;
  705. GEM_BUG_ON(to == from);
  706. GEM_BUG_ON(to->timeline == from->timeline);
  707. if (i915_request_completed(from))
  708. return 0;
  709. if (to->engine->schedule) {
  710. ret = i915_sched_node_add_dependency(to->i915,
  711. &to->sched,
  712. &from->sched);
  713. if (ret < 0)
  714. return ret;
  715. }
  716. if (to->engine == from->engine) {
  717. ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
  718. &from->submit,
  719. I915_FENCE_GFP);
  720. return ret < 0 ? ret : 0;
  721. }
  722. if (to->engine->semaphore.sync_to) {
  723. u32 seqno;
  724. GEM_BUG_ON(!from->engine->semaphore.signal);
  725. seqno = i915_request_global_seqno(from);
  726. if (!seqno)
  727. goto await_dma_fence;
  728. if (seqno <= to->timeline->global_sync[from->engine->id])
  729. return 0;
  730. trace_i915_gem_ring_sync_to(to, from);
  731. ret = to->engine->semaphore.sync_to(to, from);
  732. if (ret)
  733. return ret;
  734. to->timeline->global_sync[from->engine->id] = seqno;
  735. return 0;
  736. }
  737. await_dma_fence:
  738. ret = i915_sw_fence_await_dma_fence(&to->submit,
  739. &from->fence, 0,
  740. I915_FENCE_GFP);
  741. return ret < 0 ? ret : 0;
  742. }
  743. int
  744. i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
  745. {
  746. struct dma_fence **child = &fence;
  747. unsigned int nchild = 1;
  748. int ret;
  749. /*
  750. * Note that if the fence-array was created in signal-on-any mode,
  751. * we should *not* decompose it into its individual fences. However,
  752. * we don't currently store which mode the fence-array is operating
  753. * in. Fortunately, the only user of signal-on-any is private to
  754. * amdgpu and we should not see any incoming fence-array from
  755. * sync-file being in signal-on-any mode.
  756. */
  757. if (dma_fence_is_array(fence)) {
  758. struct dma_fence_array *array = to_dma_fence_array(fence);
  759. child = array->fences;
  760. nchild = array->num_fences;
  761. GEM_BUG_ON(!nchild);
  762. }
  763. do {
  764. fence = *child++;
  765. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  766. continue;
  767. /*
  768. * Requests on the same timeline are explicitly ordered, along
  769. * with their dependencies, by i915_request_add() which ensures
  770. * that requests are submitted in-order through each ring.
  771. */
  772. if (fence->context == rq->fence.context)
  773. continue;
  774. /* Squash repeated waits to the same timelines */
  775. if (fence->context != rq->i915->mm.unordered_timeline &&
  776. i915_timeline_sync_is_later(rq->timeline, fence))
  777. continue;
  778. if (dma_fence_is_i915(fence))
  779. ret = i915_request_await_request(rq, to_request(fence));
  780. else
  781. ret = i915_sw_fence_await_dma_fence(&rq->submit, fence,
  782. I915_FENCE_TIMEOUT,
  783. I915_FENCE_GFP);
  784. if (ret < 0)
  785. return ret;
  786. /* Record the latest fence used against each timeline */
  787. if (fence->context != rq->i915->mm.unordered_timeline)
  788. i915_timeline_sync_set(rq->timeline, fence);
  789. } while (--nchild);
  790. return 0;
  791. }
  792. /**
  793. * i915_request_await_object - set this request to (async) wait upon a bo
  794. * @to: request we are wishing to use
  795. * @obj: object which may be in use on another ring.
  796. * @write: whether the wait is on behalf of a writer
  797. *
  798. * This code is meant to abstract object synchronization with the GPU.
  799. * Conceptually we serialise writes between engines inside the GPU.
  800. * We only allow one engine to write into a buffer at any time, but
  801. * multiple readers. To ensure each has a coherent view of memory, we must:
  802. *
  803. * - If there is an outstanding write request to the object, the new
  804. * request must wait for it to complete (either CPU or in hw, requests
  805. * on the same ring will be naturally ordered).
  806. *
  807. * - If we are a write request (pending_write_domain is set), the new
  808. * request must wait for outstanding read requests to complete.
  809. *
  810. * Returns 0 if successful, else propagates up the lower layer error.
  811. */
  812. int
  813. i915_request_await_object(struct i915_request *to,
  814. struct drm_i915_gem_object *obj,
  815. bool write)
  816. {
  817. struct dma_fence *excl;
  818. int ret = 0;
  819. if (write) {
  820. struct dma_fence **shared;
  821. unsigned int count, i;
  822. ret = reservation_object_get_fences_rcu(obj->resv,
  823. &excl, &count, &shared);
  824. if (ret)
  825. return ret;
  826. for (i = 0; i < count; i++) {
  827. ret = i915_request_await_dma_fence(to, shared[i]);
  828. if (ret)
  829. break;
  830. dma_fence_put(shared[i]);
  831. }
  832. for (; i < count; i++)
  833. dma_fence_put(shared[i]);
  834. kfree(shared);
  835. } else {
  836. excl = reservation_object_get_excl_rcu(obj->resv);
  837. }
  838. if (excl) {
  839. if (ret == 0)
  840. ret = i915_request_await_dma_fence(to, excl);
  841. dma_fence_put(excl);
  842. }
  843. return ret;
  844. }
  845. void i915_request_skip(struct i915_request *rq, int error)
  846. {
  847. void *vaddr = rq->ring->vaddr;
  848. u32 head;
  849. GEM_BUG_ON(!IS_ERR_VALUE((long)error));
  850. dma_fence_set_error(&rq->fence, error);
  851. /*
  852. * As this request likely depends on state from the lost
  853. * context, clear out all the user operations leaving the
  854. * breadcrumb at the end (so we get the fence notifications).
  855. */
  856. head = rq->infix;
  857. if (rq->postfix < head) {
  858. memset(vaddr + head, 0, rq->ring->size - head);
  859. head = 0;
  860. }
  861. memset(vaddr + head, 0, rq->postfix - head);
  862. }
  863. /*
  864. * NB: This function is not allowed to fail. Doing so would mean the the
  865. * request is not being tracked for completion but the work itself is
  866. * going to happen on the hardware. This would be a Bad Thing(tm).
  867. */
  868. void i915_request_add(struct i915_request *request)
  869. {
  870. struct intel_engine_cs *engine = request->engine;
  871. struct i915_timeline *timeline = request->timeline;
  872. struct intel_ring *ring = request->ring;
  873. struct i915_request *prev;
  874. u32 *cs;
  875. GEM_TRACE("%s fence %llx:%d\n",
  876. engine->name, request->fence.context, request->fence.seqno);
  877. lockdep_assert_held(&request->i915->drm.struct_mutex);
  878. trace_i915_request_add(request);
  879. /*
  880. * Make sure that no request gazumped us - if it was allocated after
  881. * our i915_request_alloc() and called __i915_request_add() before
  882. * us, the timeline will hold its seqno which is later than ours.
  883. */
  884. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  885. /*
  886. * To ensure that this call will not fail, space for its emissions
  887. * should already have been reserved in the ring buffer. Let the ring
  888. * know that it is time to use that space up.
  889. */
  890. request->reserved_space = 0;
  891. engine->emit_flush(request, EMIT_FLUSH);
  892. /*
  893. * Record the position of the start of the breadcrumb so that
  894. * should we detect the updated seqno part-way through the
  895. * GPU processing the request, we never over-estimate the
  896. * position of the ring's HEAD.
  897. */
  898. cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
  899. GEM_BUG_ON(IS_ERR(cs));
  900. request->postfix = intel_ring_offset(request, cs);
  901. /*
  902. * Seal the request and mark it as pending execution. Note that
  903. * we may inspect this state, without holding any locks, during
  904. * hangcheck. Hence we apply the barrier to ensure that we do not
  905. * see a more recent value in the hws than we are tracking.
  906. */
  907. prev = i915_gem_active_raw(&timeline->last_request,
  908. &request->i915->drm.struct_mutex);
  909. if (prev && !i915_request_completed(prev)) {
  910. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  911. &request->submitq);
  912. if (engine->schedule)
  913. __i915_sched_node_add_dependency(&request->sched,
  914. &prev->sched,
  915. &request->dep,
  916. 0);
  917. }
  918. spin_lock_irq(&timeline->lock);
  919. list_add_tail(&request->link, &timeline->requests);
  920. spin_unlock_irq(&timeline->lock);
  921. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  922. i915_gem_active_set(&timeline->last_request, request);
  923. list_add_tail(&request->ring_link, &ring->request_list);
  924. if (list_is_first(&request->ring_link, &ring->request_list)) {
  925. GEM_TRACE("marking %s as active\n", ring->timeline->name);
  926. list_add(&ring->active_link, &request->i915->gt.active_rings);
  927. }
  928. request->emitted_jiffies = jiffies;
  929. /*
  930. * Let the backend know a new request has arrived that may need
  931. * to adjust the existing execution schedule due to a high priority
  932. * request - i.e. we may want to preempt the current request in order
  933. * to run a high priority dependency chain *before* we can execute this
  934. * request.
  935. *
  936. * This is called before the request is ready to run so that we can
  937. * decide whether to preempt the entire chain so that it is ready to
  938. * run at the earliest possible convenience.
  939. */
  940. local_bh_disable();
  941. rcu_read_lock(); /* RCU serialisation for set-wedged protection */
  942. if (engine->schedule)
  943. engine->schedule(request, &request->gem_context->sched);
  944. rcu_read_unlock();
  945. i915_sw_fence_commit(&request->submit);
  946. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  947. /*
  948. * In typical scenarios, we do not expect the previous request on
  949. * the timeline to be still tracked by timeline->last_request if it
  950. * has been completed. If the completed request is still here, that
  951. * implies that request retirement is a long way behind submission,
  952. * suggesting that we haven't been retiring frequently enough from
  953. * the combination of retire-before-alloc, waiters and the background
  954. * retirement worker. So if the last request on this timeline was
  955. * already completed, do a catch up pass, flushing the retirement queue
  956. * up to this client. Since we have now moved the heaviest operations
  957. * during retirement onto secondary workers, such as freeing objects
  958. * or contexts, retiring a bunch of requests is mostly list management
  959. * (and cache misses), and so we should not be overly penalizing this
  960. * client by performing excess work, though we may still performing
  961. * work on behalf of others -- but instead we should benefit from
  962. * improved resource management. (Well, that's the theory at least.)
  963. */
  964. if (prev && i915_request_completed(prev))
  965. i915_request_retire_upto(prev);
  966. }
  967. static unsigned long local_clock_us(unsigned int *cpu)
  968. {
  969. unsigned long t;
  970. /*
  971. * Cheaply and approximately convert from nanoseconds to microseconds.
  972. * The result and subsequent calculations are also defined in the same
  973. * approximate microseconds units. The principal source of timing
  974. * error here is from the simple truncation.
  975. *
  976. * Note that local_clock() is only defined wrt to the current CPU;
  977. * the comparisons are no longer valid if we switch CPUs. Instead of
  978. * blocking preemption for the entire busywait, we can detect the CPU
  979. * switch and use that as indicator of system load and a reason to
  980. * stop busywaiting, see busywait_stop().
  981. */
  982. *cpu = get_cpu();
  983. t = local_clock() >> 10;
  984. put_cpu();
  985. return t;
  986. }
  987. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  988. {
  989. unsigned int this_cpu;
  990. if (time_after(local_clock_us(&this_cpu), timeout))
  991. return true;
  992. return this_cpu != cpu;
  993. }
  994. static bool __i915_spin_request(const struct i915_request *rq,
  995. u32 seqno, int state, unsigned long timeout_us)
  996. {
  997. struct intel_engine_cs *engine = rq->engine;
  998. unsigned int irq, cpu;
  999. GEM_BUG_ON(!seqno);
  1000. /*
  1001. * Only wait for the request if we know it is likely to complete.
  1002. *
  1003. * We don't track the timestamps around requests, nor the average
  1004. * request length, so we do not have a good indicator that this
  1005. * request will complete within the timeout. What we do know is the
  1006. * order in which requests are executed by the engine and so we can
  1007. * tell if the request has started. If the request hasn't started yet,
  1008. * it is a fair assumption that it will not complete within our
  1009. * relatively short timeout.
  1010. */
  1011. if (!i915_seqno_passed(intel_engine_get_seqno(engine), seqno - 1))
  1012. return false;
  1013. /*
  1014. * When waiting for high frequency requests, e.g. during synchronous
  1015. * rendering split between the CPU and GPU, the finite amount of time
  1016. * required to set up the irq and wait upon it limits the response
  1017. * rate. By busywaiting on the request completion for a short while we
  1018. * can service the high frequency waits as quick as possible. However,
  1019. * if it is a slow request, we want to sleep as quickly as possible.
  1020. * The tradeoff between waiting and sleeping is roughly the time it
  1021. * takes to sleep on a request, on the order of a microsecond.
  1022. */
  1023. irq = READ_ONCE(engine->breadcrumbs.irq_count);
  1024. timeout_us += local_clock_us(&cpu);
  1025. do {
  1026. if (i915_seqno_passed(intel_engine_get_seqno(engine), seqno))
  1027. return seqno == i915_request_global_seqno(rq);
  1028. /*
  1029. * Seqno are meant to be ordered *before* the interrupt. If
  1030. * we see an interrupt without a corresponding seqno advance,
  1031. * assume we won't see one in the near future but require
  1032. * the engine->seqno_barrier() to fixup coherency.
  1033. */
  1034. if (READ_ONCE(engine->breadcrumbs.irq_count) != irq)
  1035. break;
  1036. if (signal_pending_state(state, current))
  1037. break;
  1038. if (busywait_stop(timeout_us, cpu))
  1039. break;
  1040. cpu_relax();
  1041. } while (!need_resched());
  1042. return false;
  1043. }
  1044. static bool __i915_wait_request_check_and_reset(struct i915_request *request)
  1045. {
  1046. struct i915_gpu_error *error = &request->i915->gpu_error;
  1047. if (likely(!i915_reset_handoff(error)))
  1048. return false;
  1049. __set_current_state(TASK_RUNNING);
  1050. i915_reset(request->i915, error->stalled_mask, error->reason);
  1051. return true;
  1052. }
  1053. /**
  1054. * i915_request_wait - wait until execution of request has finished
  1055. * @rq: the request to wait upon
  1056. * @flags: how to wait
  1057. * @timeout: how long to wait in jiffies
  1058. *
  1059. * i915_request_wait() waits for the request to be completed, for a
  1060. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  1061. * unbounded wait).
  1062. *
  1063. * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
  1064. * in via the flags, and vice versa if the struct_mutex is not held, the caller
  1065. * must not specify that the wait is locked.
  1066. *
  1067. * Returns the remaining time (in jiffies) if the request completed, which may
  1068. * be zero or -ETIME if the request is unfinished after the timeout expires.
  1069. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  1070. * pending before the request completes.
  1071. */
  1072. long i915_request_wait(struct i915_request *rq,
  1073. unsigned int flags,
  1074. long timeout)
  1075. {
  1076. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  1077. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1078. wait_queue_head_t *errq = &rq->i915->gpu_error.wait_queue;
  1079. DEFINE_WAIT_FUNC(reset, default_wake_function);
  1080. DEFINE_WAIT_FUNC(exec, default_wake_function);
  1081. struct intel_wait wait;
  1082. might_sleep();
  1083. #if IS_ENABLED(CONFIG_LOCKDEP)
  1084. GEM_BUG_ON(debug_locks &&
  1085. !!lockdep_is_held(&rq->i915->drm.struct_mutex) !=
  1086. !!(flags & I915_WAIT_LOCKED));
  1087. #endif
  1088. GEM_BUG_ON(timeout < 0);
  1089. if (i915_request_completed(rq))
  1090. return timeout;
  1091. if (!timeout)
  1092. return -ETIME;
  1093. trace_i915_request_wait_begin(rq, flags);
  1094. add_wait_queue(&rq->execute, &exec);
  1095. if (flags & I915_WAIT_LOCKED)
  1096. add_wait_queue(errq, &reset);
  1097. intel_wait_init(&wait);
  1098. restart:
  1099. do {
  1100. set_current_state(state);
  1101. if (intel_wait_update_request(&wait, rq))
  1102. break;
  1103. if (flags & I915_WAIT_LOCKED &&
  1104. __i915_wait_request_check_and_reset(rq))
  1105. continue;
  1106. if (signal_pending_state(state, current)) {
  1107. timeout = -ERESTARTSYS;
  1108. goto complete;
  1109. }
  1110. if (!timeout) {
  1111. timeout = -ETIME;
  1112. goto complete;
  1113. }
  1114. timeout = io_schedule_timeout(timeout);
  1115. } while (1);
  1116. GEM_BUG_ON(!intel_wait_has_seqno(&wait));
  1117. GEM_BUG_ON(!i915_sw_fence_signaled(&rq->submit));
  1118. /* Optimistic short spin before touching IRQs */
  1119. if (__i915_spin_request(rq, wait.seqno, state, 5))
  1120. goto complete;
  1121. set_current_state(state);
  1122. if (intel_engine_add_wait(rq->engine, &wait))
  1123. /*
  1124. * In order to check that we haven't missed the interrupt
  1125. * as we enabled it, we need to kick ourselves to do a
  1126. * coherent check on the seqno before we sleep.
  1127. */
  1128. goto wakeup;
  1129. if (flags & I915_WAIT_LOCKED)
  1130. __i915_wait_request_check_and_reset(rq);
  1131. for (;;) {
  1132. if (signal_pending_state(state, current)) {
  1133. timeout = -ERESTARTSYS;
  1134. break;
  1135. }
  1136. if (!timeout) {
  1137. timeout = -ETIME;
  1138. break;
  1139. }
  1140. timeout = io_schedule_timeout(timeout);
  1141. if (intel_wait_complete(&wait) &&
  1142. intel_wait_check_request(&wait, rq))
  1143. break;
  1144. set_current_state(state);
  1145. wakeup:
  1146. /*
  1147. * Carefully check if the request is complete, giving time
  1148. * for the seqno to be visible following the interrupt.
  1149. * We also have to check in case we are kicked by the GPU
  1150. * reset in order to drop the struct_mutex.
  1151. */
  1152. if (__i915_request_irq_complete(rq))
  1153. break;
  1154. /*
  1155. * If the GPU is hung, and we hold the lock, reset the GPU
  1156. * and then check for completion. On a full reset, the engine's
  1157. * HW seqno will be advanced passed us and we are complete.
  1158. * If we do a partial reset, we have to wait for the GPU to
  1159. * resume and update the breadcrumb.
  1160. *
  1161. * If we don't hold the mutex, we can just wait for the worker
  1162. * to come along and update the breadcrumb (either directly
  1163. * itself, or indirectly by recovering the GPU).
  1164. */
  1165. if (flags & I915_WAIT_LOCKED &&
  1166. __i915_wait_request_check_and_reset(rq))
  1167. continue;
  1168. /* Only spin if we know the GPU is processing this request */
  1169. if (__i915_spin_request(rq, wait.seqno, state, 2))
  1170. break;
  1171. if (!intel_wait_check_request(&wait, rq)) {
  1172. intel_engine_remove_wait(rq->engine, &wait);
  1173. goto restart;
  1174. }
  1175. }
  1176. intel_engine_remove_wait(rq->engine, &wait);
  1177. complete:
  1178. __set_current_state(TASK_RUNNING);
  1179. if (flags & I915_WAIT_LOCKED)
  1180. remove_wait_queue(errq, &reset);
  1181. remove_wait_queue(&rq->execute, &exec);
  1182. trace_i915_request_wait_end(rq);
  1183. return timeout;
  1184. }
  1185. static void ring_retire_requests(struct intel_ring *ring)
  1186. {
  1187. struct i915_request *request, *next;
  1188. list_for_each_entry_safe(request, next,
  1189. &ring->request_list, ring_link) {
  1190. if (!i915_request_completed(request))
  1191. break;
  1192. i915_request_retire(request);
  1193. }
  1194. }
  1195. void i915_retire_requests(struct drm_i915_private *i915)
  1196. {
  1197. struct intel_ring *ring, *tmp;
  1198. lockdep_assert_held(&i915->drm.struct_mutex);
  1199. if (!i915->gt.active_requests)
  1200. return;
  1201. list_for_each_entry_safe(ring, tmp, &i915->gt.active_rings, active_link)
  1202. ring_retire_requests(ring);
  1203. }
  1204. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1205. #include "selftests/mock_request.c"
  1206. #include "selftests/i915_request.c"
  1207. #endif