i915_gem_request.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 "i915_drv.h"
  26. static const char *i915_fence_get_driver_name(struct fence *fence)
  27. {
  28. return "i915";
  29. }
  30. static const char *i915_fence_get_timeline_name(struct fence *fence)
  31. {
  32. /* Timelines are bound by eviction to a VM. However, since
  33. * we only have a global seqno at the moment, we only have
  34. * a single timeline. Note that each timeline will have
  35. * multiple execution contexts (fence contexts) as we allow
  36. * engines within a single timeline to execute in parallel.
  37. */
  38. return "global";
  39. }
  40. static bool i915_fence_signaled(struct fence *fence)
  41. {
  42. return i915_gem_request_completed(to_request(fence));
  43. }
  44. static bool i915_fence_enable_signaling(struct fence *fence)
  45. {
  46. if (i915_fence_signaled(fence))
  47. return false;
  48. intel_engine_enable_signaling(to_request(fence));
  49. return true;
  50. }
  51. static signed long i915_fence_wait(struct fence *fence,
  52. bool interruptible,
  53. signed long timeout_jiffies)
  54. {
  55. s64 timeout_ns, *timeout;
  56. int ret;
  57. if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT) {
  58. timeout_ns = jiffies_to_nsecs(timeout_jiffies);
  59. timeout = &timeout_ns;
  60. } else {
  61. timeout = NULL;
  62. }
  63. ret = i915_wait_request(to_request(fence),
  64. interruptible, timeout,
  65. NO_WAITBOOST);
  66. if (ret == -ETIME)
  67. return 0;
  68. if (ret < 0)
  69. return ret;
  70. if (timeout_jiffies != MAX_SCHEDULE_TIMEOUT)
  71. timeout_jiffies = nsecs_to_jiffies(timeout_ns);
  72. return timeout_jiffies;
  73. }
  74. static void i915_fence_value_str(struct fence *fence, char *str, int size)
  75. {
  76. snprintf(str, size, "%u", fence->seqno);
  77. }
  78. static void i915_fence_timeline_value_str(struct fence *fence, char *str,
  79. int size)
  80. {
  81. snprintf(str, size, "%u",
  82. intel_engine_get_seqno(to_request(fence)->engine));
  83. }
  84. static void i915_fence_release(struct fence *fence)
  85. {
  86. struct drm_i915_gem_request *req = to_request(fence);
  87. kmem_cache_free(req->i915->requests, req);
  88. }
  89. const struct fence_ops i915_fence_ops = {
  90. .get_driver_name = i915_fence_get_driver_name,
  91. .get_timeline_name = i915_fence_get_timeline_name,
  92. .enable_signaling = i915_fence_enable_signaling,
  93. .signaled = i915_fence_signaled,
  94. .wait = i915_fence_wait,
  95. .release = i915_fence_release,
  96. .fence_value_str = i915_fence_value_str,
  97. .timeline_value_str = i915_fence_timeline_value_str,
  98. };
  99. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  100. struct drm_file *file)
  101. {
  102. struct drm_i915_private *dev_private;
  103. struct drm_i915_file_private *file_priv;
  104. WARN_ON(!req || !file || req->file_priv);
  105. if (!req || !file)
  106. return -EINVAL;
  107. if (req->file_priv)
  108. return -EINVAL;
  109. dev_private = req->i915;
  110. file_priv = file->driver_priv;
  111. spin_lock(&file_priv->mm.lock);
  112. req->file_priv = file_priv;
  113. list_add_tail(&req->client_list, &file_priv->mm.request_list);
  114. spin_unlock(&file_priv->mm.lock);
  115. req->pid = get_pid(task_pid(current));
  116. return 0;
  117. }
  118. static inline void
  119. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  120. {
  121. struct drm_i915_file_private *file_priv = request->file_priv;
  122. if (!file_priv)
  123. return;
  124. spin_lock(&file_priv->mm.lock);
  125. list_del(&request->client_list);
  126. request->file_priv = NULL;
  127. spin_unlock(&file_priv->mm.lock);
  128. put_pid(request->pid);
  129. request->pid = NULL;
  130. }
  131. void i915_gem_retire_noop(struct i915_gem_active *active,
  132. struct drm_i915_gem_request *request)
  133. {
  134. /* Space left intentionally blank */
  135. }
  136. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  137. {
  138. struct i915_gem_active *active, *next;
  139. trace_i915_gem_request_retire(request);
  140. list_del_init(&request->link);
  141. /* We know the GPU must have read the request to have
  142. * sent us the seqno + interrupt, so use the position
  143. * of tail of the request to update the last known position
  144. * of the GPU head.
  145. *
  146. * Note this requires that we are always called in request
  147. * completion order.
  148. */
  149. list_del(&request->ring_link);
  150. request->ring->last_retired_head = request->postfix;
  151. /* Walk through the active list, calling retire on each. This allows
  152. * objects to track their GPU activity and mark themselves as idle
  153. * when their *last* active request is completed (updating state
  154. * tracking lists for eviction, active references for GEM, etc).
  155. *
  156. * As the ->retire() may free the node, we decouple it first and
  157. * pass along the auxiliary information (to avoid dereferencing
  158. * the node after the callback).
  159. */
  160. list_for_each_entry_safe(active, next, &request->active_list, link) {
  161. /* In microbenchmarks or focusing upon time inside the kernel,
  162. * we may spend an inordinate amount of time simply handling
  163. * the retirement of requests and processing their callbacks.
  164. * Of which, this loop itself is particularly hot due to the
  165. * cache misses when jumping around the list of i915_gem_active.
  166. * So we try to keep this loop as streamlined as possible and
  167. * also prefetch the next i915_gem_active to try and hide
  168. * the likely cache miss.
  169. */
  170. prefetchw(next);
  171. INIT_LIST_HEAD(&active->link);
  172. active->request = NULL;
  173. active->retire(active, request);
  174. }
  175. i915_gem_request_remove_from_client(request);
  176. if (request->previous_context) {
  177. if (i915.enable_execlists)
  178. intel_lr_context_unpin(request->previous_context,
  179. request->engine);
  180. }
  181. i915_gem_context_put(request->ctx);
  182. i915_gem_request_put(request);
  183. }
  184. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
  185. {
  186. struct intel_engine_cs *engine = req->engine;
  187. struct drm_i915_gem_request *tmp;
  188. lockdep_assert_held(&req->i915->drm.struct_mutex);
  189. if (list_empty(&req->link))
  190. return;
  191. do {
  192. tmp = list_first_entry(&engine->request_list,
  193. typeof(*tmp), link);
  194. i915_gem_request_retire(tmp);
  195. } while (tmp != req);
  196. }
  197. static int i915_gem_check_wedge(unsigned int reset_counter, bool interruptible)
  198. {
  199. if (__i915_terminally_wedged(reset_counter))
  200. return -EIO;
  201. if (__i915_reset_in_progress(reset_counter)) {
  202. /* Non-interruptible callers can't handle -EAGAIN, hence return
  203. * -EIO unconditionally for these.
  204. */
  205. if (!interruptible)
  206. return -EIO;
  207. return -EAGAIN;
  208. }
  209. return 0;
  210. }
  211. static int i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
  212. {
  213. struct intel_engine_cs *engine;
  214. int ret;
  215. /* Carefully retire all requests without writing to the rings */
  216. for_each_engine(engine, dev_priv) {
  217. ret = intel_engine_idle(engine);
  218. if (ret)
  219. return ret;
  220. }
  221. i915_gem_retire_requests(dev_priv);
  222. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  223. if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) {
  224. while (intel_kick_waiters(dev_priv) ||
  225. intel_kick_signalers(dev_priv))
  226. yield();
  227. }
  228. /* Finally reset hw state */
  229. for_each_engine(engine, dev_priv)
  230. intel_engine_init_seqno(engine, seqno);
  231. return 0;
  232. }
  233. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  234. {
  235. struct drm_i915_private *dev_priv = to_i915(dev);
  236. int ret;
  237. if (seqno == 0)
  238. return -EINVAL;
  239. /* HWS page needs to be set less than what we
  240. * will inject to ring
  241. */
  242. ret = i915_gem_init_seqno(dev_priv, seqno - 1);
  243. if (ret)
  244. return ret;
  245. dev_priv->next_seqno = seqno;
  246. return 0;
  247. }
  248. static int i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
  249. {
  250. /* reserve 0 for non-seqno */
  251. if (unlikely(dev_priv->next_seqno == 0)) {
  252. int ret;
  253. ret = i915_gem_init_seqno(dev_priv, 0);
  254. if (ret)
  255. return ret;
  256. dev_priv->next_seqno = 1;
  257. }
  258. *seqno = dev_priv->next_seqno++;
  259. return 0;
  260. }
  261. /**
  262. * i915_gem_request_alloc - allocate a request structure
  263. *
  264. * @engine: engine that we wish to issue the request on.
  265. * @ctx: context that the request will be associated with.
  266. * This can be NULL if the request is not directly related to
  267. * any specific user context, in which case this function will
  268. * choose an appropriate context to use.
  269. *
  270. * Returns a pointer to the allocated request if successful,
  271. * or an error code if not.
  272. */
  273. struct drm_i915_gem_request *
  274. i915_gem_request_alloc(struct intel_engine_cs *engine,
  275. struct i915_gem_context *ctx)
  276. {
  277. struct drm_i915_private *dev_priv = engine->i915;
  278. unsigned int reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  279. struct drm_i915_gem_request *req;
  280. u32 seqno;
  281. int ret;
  282. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  283. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  284. * and restart.
  285. */
  286. ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
  287. if (ret)
  288. return ERR_PTR(ret);
  289. /* Move the oldest request to the slab-cache (if not in use!) */
  290. req = list_first_entry_or_null(&engine->request_list,
  291. typeof(*req), link);
  292. if (req && i915_gem_request_completed(req))
  293. i915_gem_request_retire(req);
  294. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  295. if (!req)
  296. return ERR_PTR(-ENOMEM);
  297. ret = i915_gem_get_seqno(dev_priv, &seqno);
  298. if (ret)
  299. goto err;
  300. spin_lock_init(&req->lock);
  301. fence_init(&req->fence,
  302. &i915_fence_ops,
  303. &req->lock,
  304. engine->fence_context,
  305. seqno);
  306. INIT_LIST_HEAD(&req->active_list);
  307. req->i915 = dev_priv;
  308. req->engine = engine;
  309. req->ctx = i915_gem_context_get(ctx);
  310. /*
  311. * Reserve space in the ring buffer for all the commands required to
  312. * eventually emit this request. This is to guarantee that the
  313. * i915_add_request() call can't fail. Note that the reserve may need
  314. * to be redone if the request is not actually submitted straight
  315. * away, e.g. because a GPU scheduler has deferred it.
  316. */
  317. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  318. if (i915.enable_execlists)
  319. ret = intel_logical_ring_alloc_request_extras(req);
  320. else
  321. ret = intel_ring_alloc_request_extras(req);
  322. if (ret)
  323. goto err_ctx;
  324. return req;
  325. err_ctx:
  326. i915_gem_context_put(ctx);
  327. err:
  328. kmem_cache_free(dev_priv->requests, req);
  329. return ERR_PTR(ret);
  330. }
  331. static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
  332. {
  333. struct drm_i915_private *dev_priv = engine->i915;
  334. dev_priv->gt.active_engines |= intel_engine_flag(engine);
  335. if (dev_priv->gt.awake)
  336. return;
  337. intel_runtime_pm_get_noresume(dev_priv);
  338. dev_priv->gt.awake = true;
  339. intel_enable_gt_powersave(dev_priv);
  340. i915_update_gfx_val(dev_priv);
  341. if (INTEL_GEN(dev_priv) >= 6)
  342. gen6_rps_busy(dev_priv);
  343. queue_delayed_work(dev_priv->wq,
  344. &dev_priv->gt.retire_work,
  345. round_jiffies_up_relative(HZ));
  346. }
  347. /*
  348. * NB: This function is not allowed to fail. Doing so would mean the the
  349. * request is not being tracked for completion but the work itself is
  350. * going to happen on the hardware. This would be a Bad Thing(tm).
  351. */
  352. void __i915_add_request(struct drm_i915_gem_request *request,
  353. struct drm_i915_gem_object *obj,
  354. bool flush_caches)
  355. {
  356. struct intel_engine_cs *engine;
  357. struct intel_ring *ring;
  358. u32 request_start;
  359. u32 reserved_tail;
  360. int ret;
  361. if (WARN_ON(!request))
  362. return;
  363. engine = request->engine;
  364. ring = request->ring;
  365. /*
  366. * To ensure that this call will not fail, space for its emissions
  367. * should already have been reserved in the ring buffer. Let the ring
  368. * know that it is time to use that space up.
  369. */
  370. request_start = ring->tail;
  371. reserved_tail = request->reserved_space;
  372. request->reserved_space = 0;
  373. /*
  374. * Emit any outstanding flushes - execbuf can fail to emit the flush
  375. * after having emitted the batchbuffer command. Hence we need to fix
  376. * things up similar to emitting the lazy request. The difference here
  377. * is that the flush _must_ happen before the next request, no matter
  378. * what.
  379. */
  380. if (flush_caches) {
  381. ret = engine->emit_flush(request, EMIT_FLUSH);
  382. /* Not allowed to fail! */
  383. WARN(ret, "engine->emit_flush() failed: %d!\n", ret);
  384. }
  385. trace_i915_gem_request_add(request);
  386. request->head = request_start;
  387. /* Whilst this request exists, batch_obj will be on the
  388. * active_list, and so will hold the active reference. Only when this
  389. * request is retired will the the batch_obj be moved onto the
  390. * inactive_list and lose its active reference. Hence we do not need
  391. * to explicitly hold another reference here.
  392. */
  393. request->batch_obj = obj;
  394. /* Seal the request and mark it as pending execution. Note that
  395. * we may inspect this state, without holding any locks, during
  396. * hangcheck. Hence we apply the barrier to ensure that we do not
  397. * see a more recent value in the hws than we are tracking.
  398. */
  399. request->emitted_jiffies = jiffies;
  400. request->previous_seqno = engine->last_submitted_seqno;
  401. smp_store_mb(engine->last_submitted_seqno, request->fence.seqno);
  402. list_add_tail(&request->link, &engine->request_list);
  403. list_add_tail(&request->ring_link, &ring->request_list);
  404. /* Record the position of the start of the request so that
  405. * should we detect the updated seqno part-way through the
  406. * GPU processing the request, we never over-estimate the
  407. * position of the head.
  408. */
  409. request->postfix = ring->tail;
  410. /* Not allowed to fail! */
  411. ret = engine->emit_request(request);
  412. WARN(ret, "(%s)->emit_request failed: %d!\n", engine->name, ret);
  413. /* Sanity check that the reserved size was large enough. */
  414. ret = ring->tail - request_start;
  415. if (ret < 0)
  416. ret += ring->size;
  417. WARN_ONCE(ret > reserved_tail,
  418. "Not enough space reserved (%d bytes) "
  419. "for adding the request (%d bytes)\n",
  420. reserved_tail, ret);
  421. i915_gem_mark_busy(engine);
  422. engine->submit_request(request);
  423. }
  424. static unsigned long local_clock_us(unsigned int *cpu)
  425. {
  426. unsigned long t;
  427. /* Cheaply and approximately convert from nanoseconds to microseconds.
  428. * The result and subsequent calculations are also defined in the same
  429. * approximate microseconds units. The principal source of timing
  430. * error here is from the simple truncation.
  431. *
  432. * Note that local_clock() is only defined wrt to the current CPU;
  433. * the comparisons are no longer valid if we switch CPUs. Instead of
  434. * blocking preemption for the entire busywait, we can detect the CPU
  435. * switch and use that as indicator of system load and a reason to
  436. * stop busywaiting, see busywait_stop().
  437. */
  438. *cpu = get_cpu();
  439. t = local_clock() >> 10;
  440. put_cpu();
  441. return t;
  442. }
  443. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  444. {
  445. unsigned int this_cpu;
  446. if (time_after(local_clock_us(&this_cpu), timeout))
  447. return true;
  448. return this_cpu != cpu;
  449. }
  450. bool __i915_spin_request(const struct drm_i915_gem_request *req,
  451. int state, unsigned long timeout_us)
  452. {
  453. unsigned int cpu;
  454. /* When waiting for high frequency requests, e.g. during synchronous
  455. * rendering split between the CPU and GPU, the finite amount of time
  456. * required to set up the irq and wait upon it limits the response
  457. * rate. By busywaiting on the request completion for a short while we
  458. * can service the high frequency waits as quick as possible. However,
  459. * if it is a slow request, we want to sleep as quickly as possible.
  460. * The tradeoff between waiting and sleeping is roughly the time it
  461. * takes to sleep on a request, on the order of a microsecond.
  462. */
  463. timeout_us += local_clock_us(&cpu);
  464. do {
  465. if (i915_gem_request_completed(req))
  466. return true;
  467. if (signal_pending_state(state, current))
  468. break;
  469. if (busywait_stop(timeout_us, cpu))
  470. break;
  471. cpu_relax_lowlatency();
  472. } while (!need_resched());
  473. return false;
  474. }
  475. /**
  476. * i915_wait_request - wait until execution of request has finished
  477. * @req: duh!
  478. * @interruptible: do an interruptible wait (normally yes)
  479. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  480. * @rps: client to charge for RPS boosting
  481. *
  482. * Note: It is of utmost importance that the passed in seqno and reset_counter
  483. * values have been read by the caller in an smp safe manner. Where read-side
  484. * locks are involved, it is sufficient to read the reset_counter before
  485. * unlocking the lock that protects the seqno. For lockless tricks, the
  486. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  487. * inserted.
  488. *
  489. * Returns 0 if the request was found within the alloted time. Else returns the
  490. * errno with remaining time filled in timeout argument.
  491. */
  492. int i915_wait_request(struct drm_i915_gem_request *req,
  493. bool interruptible,
  494. s64 *timeout,
  495. struct intel_rps_client *rps)
  496. {
  497. int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  498. DEFINE_WAIT(reset);
  499. struct intel_wait wait;
  500. unsigned long timeout_remain;
  501. int ret = 0;
  502. might_sleep();
  503. if (i915_gem_request_completed(req))
  504. return 0;
  505. timeout_remain = MAX_SCHEDULE_TIMEOUT;
  506. if (timeout) {
  507. if (WARN_ON(*timeout < 0))
  508. return -EINVAL;
  509. if (*timeout == 0)
  510. return -ETIME;
  511. /* Record current time in case interrupted, or wedged */
  512. timeout_remain = nsecs_to_jiffies_timeout(*timeout);
  513. *timeout += ktime_get_raw_ns();
  514. }
  515. trace_i915_gem_request_wait_begin(req);
  516. /* This client is about to stall waiting for the GPU. In many cases
  517. * this is undesirable and limits the throughput of the system, as
  518. * many clients cannot continue processing user input/output whilst
  519. * blocked. RPS autotuning may take tens of milliseconds to respond
  520. * to the GPU load and thus incurs additional latency for the client.
  521. * We can circumvent that by promoting the GPU frequency to maximum
  522. * before we wait. This makes the GPU throttle up much more quickly
  523. * (good for benchmarks and user experience, e.g. window animations),
  524. * but at a cost of spending more power processing the workload
  525. * (bad for battery). Not all clients even want their results
  526. * immediately and for them we should just let the GPU select its own
  527. * frequency to maximise efficiency. To prevent a single client from
  528. * forcing the clocks too high for the whole system, we only allow
  529. * each client to waitboost once in a busy period.
  530. */
  531. if (IS_RPS_CLIENT(rps) && INTEL_GEN(req->i915) >= 6)
  532. gen6_rps_boost(req->i915, rps, req->emitted_jiffies);
  533. /* Optimistic spin for the next ~jiffie before touching IRQs */
  534. if (i915_spin_request(req, state, 5))
  535. goto complete;
  536. set_current_state(state);
  537. add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  538. intel_wait_init(&wait, req->fence.seqno);
  539. if (intel_engine_add_wait(req->engine, &wait))
  540. /* In order to check that we haven't missed the interrupt
  541. * as we enabled it, we need to kick ourselves to do a
  542. * coherent check on the seqno before we sleep.
  543. */
  544. goto wakeup;
  545. for (;;) {
  546. if (signal_pending_state(state, current)) {
  547. ret = -ERESTARTSYS;
  548. break;
  549. }
  550. timeout_remain = io_schedule_timeout(timeout_remain);
  551. if (timeout_remain == 0) {
  552. ret = -ETIME;
  553. break;
  554. }
  555. if (intel_wait_complete(&wait))
  556. break;
  557. set_current_state(state);
  558. wakeup:
  559. /* Carefully check if the request is complete, giving time
  560. * for the seqno to be visible following the interrupt.
  561. * We also have to check in case we are kicked by the GPU
  562. * reset in order to drop the struct_mutex.
  563. */
  564. if (__i915_request_irq_complete(req))
  565. break;
  566. /* Only spin if we know the GPU is processing this request */
  567. if (i915_spin_request(req, state, 2))
  568. break;
  569. }
  570. remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  571. intel_engine_remove_wait(req->engine, &wait);
  572. __set_current_state(TASK_RUNNING);
  573. complete:
  574. trace_i915_gem_request_wait_end(req);
  575. if (timeout) {
  576. *timeout -= ktime_get_raw_ns();
  577. if (*timeout < 0)
  578. *timeout = 0;
  579. /*
  580. * Apparently ktime isn't accurate enough and occasionally has a
  581. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  582. * things up to make the test happy. We allow up to 1 jiffy.
  583. *
  584. * This is a regrssion from the timespec->ktime conversion.
  585. */
  586. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  587. *timeout = 0;
  588. }
  589. if (IS_RPS_USER(rps) &&
  590. req->fence.seqno == req->engine->last_submitted_seqno) {
  591. /* The GPU is now idle and this client has stalled.
  592. * Since no other client has submitted a request in the
  593. * meantime, assume that this client is the only one
  594. * supplying work to the GPU but is unable to keep that
  595. * work supplied because it is waiting. Since the GPU is
  596. * then never kept fully busy, RPS autoclocking will
  597. * keep the clocks relatively low, causing further delays.
  598. * Compensate by giving the synchronous client credit for
  599. * a waitboost next time.
  600. */
  601. spin_lock(&req->i915->rps.client_lock);
  602. list_del_init(&rps->link);
  603. spin_unlock(&req->i915->rps.client_lock);
  604. }
  605. return ret;
  606. }
  607. static void engine_retire_requests(struct intel_engine_cs *engine)
  608. {
  609. struct drm_i915_gem_request *request, *next;
  610. list_for_each_entry_safe(request, next, &engine->request_list, link) {
  611. if (!i915_gem_request_completed(request))
  612. break;
  613. i915_gem_request_retire(request);
  614. }
  615. }
  616. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  617. {
  618. struct intel_engine_cs *engine;
  619. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  620. if (dev_priv->gt.active_engines == 0)
  621. return;
  622. GEM_BUG_ON(!dev_priv->gt.awake);
  623. for_each_engine(engine, dev_priv) {
  624. engine_retire_requests(engine);
  625. if (list_empty(&engine->request_list))
  626. dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
  627. }
  628. if (dev_priv->gt.active_engines == 0)
  629. queue_delayed_work(dev_priv->wq,
  630. &dev_priv->gt.idle_work,
  631. msecs_to_jiffies(100));
  632. }