i915_gem_request.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. return 0;
  116. }
  117. static inline void
  118. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  119. {
  120. struct drm_i915_file_private *file_priv = request->file_priv;
  121. if (!file_priv)
  122. return;
  123. spin_lock(&file_priv->mm.lock);
  124. list_del(&request->client_list);
  125. request->file_priv = NULL;
  126. spin_unlock(&file_priv->mm.lock);
  127. }
  128. void i915_gem_retire_noop(struct i915_gem_active *active,
  129. struct drm_i915_gem_request *request)
  130. {
  131. /* Space left intentionally blank */
  132. }
  133. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  134. {
  135. struct i915_gem_active *active, *next;
  136. trace_i915_gem_request_retire(request);
  137. list_del(&request->link);
  138. /* We know the GPU must have read the request to have
  139. * sent us the seqno + interrupt, so use the position
  140. * of tail of the request to update the last known position
  141. * of the GPU head.
  142. *
  143. * Note this requires that we are always called in request
  144. * completion order.
  145. */
  146. list_del(&request->ring_link);
  147. request->ring->last_retired_head = request->postfix;
  148. /* Walk through the active list, calling retire on each. This allows
  149. * objects to track their GPU activity and mark themselves as idle
  150. * when their *last* active request is completed (updating state
  151. * tracking lists for eviction, active references for GEM, etc).
  152. *
  153. * As the ->retire() may free the node, we decouple it first and
  154. * pass along the auxiliary information (to avoid dereferencing
  155. * the node after the callback).
  156. */
  157. list_for_each_entry_safe(active, next, &request->active_list, link) {
  158. /* In microbenchmarks or focusing upon time inside the kernel,
  159. * we may spend an inordinate amount of time simply handling
  160. * the retirement of requests and processing their callbacks.
  161. * Of which, this loop itself is particularly hot due to the
  162. * cache misses when jumping around the list of i915_gem_active.
  163. * So we try to keep this loop as streamlined as possible and
  164. * also prefetch the next i915_gem_active to try and hide
  165. * the likely cache miss.
  166. */
  167. prefetchw(next);
  168. INIT_LIST_HEAD(&active->link);
  169. RCU_INIT_POINTER(active->request, NULL);
  170. active->retire(active, request);
  171. }
  172. i915_gem_request_remove_from_client(request);
  173. if (request->previous_context) {
  174. if (i915.enable_execlists)
  175. intel_lr_context_unpin(request->previous_context,
  176. request->engine);
  177. }
  178. i915_gem_context_put(request->ctx);
  179. i915_gem_request_put(request);
  180. }
  181. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
  182. {
  183. struct intel_engine_cs *engine = req->engine;
  184. struct drm_i915_gem_request *tmp;
  185. lockdep_assert_held(&req->i915->drm.struct_mutex);
  186. GEM_BUG_ON(list_empty(&req->link));
  187. do {
  188. tmp = list_first_entry(&engine->request_list,
  189. typeof(*tmp), link);
  190. i915_gem_request_retire(tmp);
  191. } while (tmp != req);
  192. }
  193. static int i915_gem_check_wedge(struct drm_i915_private *dev_priv)
  194. {
  195. struct i915_gpu_error *error = &dev_priv->gpu_error;
  196. if (i915_terminally_wedged(error))
  197. return -EIO;
  198. if (i915_reset_in_progress(error)) {
  199. /* Non-interruptible callers can't handle -EAGAIN, hence return
  200. * -EIO unconditionally for these.
  201. */
  202. if (!dev_priv->mm.interruptible)
  203. return -EIO;
  204. return -EAGAIN;
  205. }
  206. return 0;
  207. }
  208. static int i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
  209. {
  210. struct intel_engine_cs *engine;
  211. int ret;
  212. /* Carefully retire all requests without writing to the rings */
  213. for_each_engine(engine, dev_priv) {
  214. ret = intel_engine_idle(engine,
  215. I915_WAIT_INTERRUPTIBLE |
  216. I915_WAIT_LOCKED);
  217. if (ret)
  218. return ret;
  219. }
  220. i915_gem_retire_requests(dev_priv);
  221. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  222. if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) {
  223. while (intel_kick_waiters(dev_priv) ||
  224. intel_kick_signalers(dev_priv))
  225. yield();
  226. }
  227. /* Finally reset hw state */
  228. for_each_engine(engine, dev_priv)
  229. intel_engine_init_seqno(engine, seqno);
  230. return 0;
  231. }
  232. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  233. {
  234. struct drm_i915_private *dev_priv = to_i915(dev);
  235. int ret;
  236. if (seqno == 0)
  237. return -EINVAL;
  238. /* HWS page needs to be set less than what we
  239. * will inject to ring
  240. */
  241. ret = i915_gem_init_seqno(dev_priv, seqno - 1);
  242. if (ret)
  243. return ret;
  244. dev_priv->next_seqno = seqno;
  245. return 0;
  246. }
  247. static int i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
  248. {
  249. /* reserve 0 for non-seqno */
  250. if (unlikely(dev_priv->next_seqno == 0)) {
  251. int ret;
  252. ret = i915_gem_init_seqno(dev_priv, 0);
  253. if (ret)
  254. return ret;
  255. dev_priv->next_seqno = 1;
  256. }
  257. *seqno = dev_priv->next_seqno++;
  258. return 0;
  259. }
  260. static int __i915_sw_fence_call
  261. submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  262. {
  263. struct drm_i915_gem_request *request =
  264. container_of(fence, typeof(*request), submit);
  265. /* Will be called from irq-context when using foreign DMA fences */
  266. switch (state) {
  267. case FENCE_COMPLETE:
  268. request->engine->last_submitted_seqno = request->fence.seqno;
  269. request->engine->submit_request(request);
  270. break;
  271. case FENCE_FREE:
  272. break;
  273. }
  274. return NOTIFY_DONE;
  275. }
  276. /**
  277. * i915_gem_request_alloc - allocate a request structure
  278. *
  279. * @engine: engine that we wish to issue the request on.
  280. * @ctx: context that the request will be associated with.
  281. * This can be NULL if the request is not directly related to
  282. * any specific user context, in which case this function will
  283. * choose an appropriate context to use.
  284. *
  285. * Returns a pointer to the allocated request if successful,
  286. * or an error code if not.
  287. */
  288. struct drm_i915_gem_request *
  289. i915_gem_request_alloc(struct intel_engine_cs *engine,
  290. struct i915_gem_context *ctx)
  291. {
  292. struct drm_i915_private *dev_priv = engine->i915;
  293. struct drm_i915_gem_request *req;
  294. u32 seqno;
  295. int ret;
  296. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  297. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  298. * and restart.
  299. */
  300. ret = i915_gem_check_wedge(dev_priv);
  301. if (ret)
  302. return ERR_PTR(ret);
  303. /* Move the oldest request to the slab-cache (if not in use!) */
  304. req = list_first_entry_or_null(&engine->request_list,
  305. typeof(*req), link);
  306. if (req && i915_gem_request_completed(req))
  307. i915_gem_request_retire(req);
  308. /* Beware: Dragons be flying overhead.
  309. *
  310. * We use RCU to look up requests in flight. The lookups may
  311. * race with the request being allocated from the slab freelist.
  312. * That is the request we are writing to here, may be in the process
  313. * of being read by __i915_gem_active_get_rcu(). As such,
  314. * we have to be very careful when overwriting the contents. During
  315. * the RCU lookup, we change chase the request->engine pointer,
  316. * read the request->fence.seqno and increment the reference count.
  317. *
  318. * The reference count is incremented atomically. If it is zero,
  319. * the lookup knows the request is unallocated and complete. Otherwise,
  320. * it is either still in use, or has been reallocated and reset
  321. * with fence_init(). This increment is safe for release as we check
  322. * that the request we have a reference to and matches the active
  323. * request.
  324. *
  325. * Before we increment the refcount, we chase the request->engine
  326. * pointer. We must not call kmem_cache_zalloc() or else we set
  327. * that pointer to NULL and cause a crash during the lookup. If
  328. * we see the request is completed (based on the value of the
  329. * old engine and seqno), the lookup is complete and reports NULL.
  330. * If we decide the request is not completed (new engine or seqno),
  331. * then we grab a reference and double check that it is still the
  332. * active request - which it won't be and restart the lookup.
  333. *
  334. * Do not use kmem_cache_zalloc() here!
  335. */
  336. req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
  337. if (!req)
  338. return ERR_PTR(-ENOMEM);
  339. ret = i915_gem_get_seqno(dev_priv, &seqno);
  340. if (ret)
  341. goto err;
  342. spin_lock_init(&req->lock);
  343. fence_init(&req->fence,
  344. &i915_fence_ops,
  345. &req->lock,
  346. engine->fence_context,
  347. seqno);
  348. i915_sw_fence_init(&req->submit, submit_notify);
  349. INIT_LIST_HEAD(&req->active_list);
  350. req->i915 = dev_priv;
  351. req->engine = engine;
  352. req->ctx = i915_gem_context_get(ctx);
  353. /* No zalloc, must clear what we need by hand */
  354. req->previous_context = NULL;
  355. req->file_priv = NULL;
  356. req->batch = NULL;
  357. /*
  358. * Reserve space in the ring buffer for all the commands required to
  359. * eventually emit this request. This is to guarantee that the
  360. * i915_add_request() call can't fail. Note that the reserve may need
  361. * to be redone if the request is not actually submitted straight
  362. * away, e.g. because a GPU scheduler has deferred it.
  363. */
  364. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  365. if (i915.enable_execlists)
  366. ret = intel_logical_ring_alloc_request_extras(req);
  367. else
  368. ret = intel_ring_alloc_request_extras(req);
  369. if (ret)
  370. goto err_ctx;
  371. /* Record the position of the start of the request so that
  372. * should we detect the updated seqno part-way through the
  373. * GPU processing the request, we never over-estimate the
  374. * position of the head.
  375. */
  376. req->head = req->ring->tail;
  377. return req;
  378. err_ctx:
  379. i915_gem_context_put(ctx);
  380. err:
  381. kmem_cache_free(dev_priv->requests, req);
  382. return ERR_PTR(ret);
  383. }
  384. static int
  385. i915_gem_request_await_request(struct drm_i915_gem_request *to,
  386. struct drm_i915_gem_request *from)
  387. {
  388. int idx, ret;
  389. GEM_BUG_ON(to == from);
  390. if (to->engine == from->engine)
  391. return 0;
  392. idx = intel_engine_sync_index(from->engine, to->engine);
  393. if (from->fence.seqno <= from->engine->semaphore.sync_seqno[idx])
  394. return 0;
  395. trace_i915_gem_ring_sync_to(to, from);
  396. if (!i915.semaphores) {
  397. if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
  398. ret = i915_sw_fence_await_dma_fence(&to->submit,
  399. &from->fence, 0,
  400. GFP_KERNEL);
  401. if (ret < 0)
  402. return ret;
  403. }
  404. } else {
  405. ret = to->engine->semaphore.sync_to(to, from);
  406. if (ret)
  407. return ret;
  408. }
  409. from->engine->semaphore.sync_seqno[idx] = from->fence.seqno;
  410. return 0;
  411. }
  412. /**
  413. * i915_gem_request_await_object - set this request to (async) wait upon a bo
  414. *
  415. * @to: request we are wishing to use
  416. * @obj: object which may be in use on another ring.
  417. *
  418. * This code is meant to abstract object synchronization with the GPU.
  419. * Conceptually we serialise writes between engines inside the GPU.
  420. * We only allow one engine to write into a buffer at any time, but
  421. * multiple readers. To ensure each has a coherent view of memory, we must:
  422. *
  423. * - If there is an outstanding write request to the object, the new
  424. * request must wait for it to complete (either CPU or in hw, requests
  425. * on the same ring will be naturally ordered).
  426. *
  427. * - If we are a write request (pending_write_domain is set), the new
  428. * request must wait for outstanding read requests to complete.
  429. *
  430. * Returns 0 if successful, else propagates up the lower layer error.
  431. */
  432. int
  433. i915_gem_request_await_object(struct drm_i915_gem_request *to,
  434. struct drm_i915_gem_object *obj,
  435. bool write)
  436. {
  437. struct i915_gem_active *active;
  438. unsigned long active_mask;
  439. int idx;
  440. if (write) {
  441. active_mask = i915_gem_object_get_active(obj);
  442. active = obj->last_read;
  443. } else {
  444. active_mask = 1;
  445. active = &obj->last_write;
  446. }
  447. for_each_active(active_mask, idx) {
  448. struct drm_i915_gem_request *request;
  449. int ret;
  450. request = i915_gem_active_peek(&active[idx],
  451. &obj->base.dev->struct_mutex);
  452. if (!request)
  453. continue;
  454. ret = i915_gem_request_await_request(to, request);
  455. if (ret)
  456. return ret;
  457. }
  458. return 0;
  459. }
  460. static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
  461. {
  462. struct drm_i915_private *dev_priv = engine->i915;
  463. dev_priv->gt.active_engines |= intel_engine_flag(engine);
  464. if (dev_priv->gt.awake)
  465. return;
  466. intel_runtime_pm_get_noresume(dev_priv);
  467. dev_priv->gt.awake = true;
  468. intel_enable_gt_powersave(dev_priv);
  469. i915_update_gfx_val(dev_priv);
  470. if (INTEL_GEN(dev_priv) >= 6)
  471. gen6_rps_busy(dev_priv);
  472. queue_delayed_work(dev_priv->wq,
  473. &dev_priv->gt.retire_work,
  474. round_jiffies_up_relative(HZ));
  475. }
  476. /*
  477. * NB: This function is not allowed to fail. Doing so would mean the the
  478. * request is not being tracked for completion but the work itself is
  479. * going to happen on the hardware. This would be a Bad Thing(tm).
  480. */
  481. void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
  482. {
  483. struct intel_engine_cs *engine = request->engine;
  484. struct intel_ring *ring = request->ring;
  485. struct drm_i915_gem_request *prev;
  486. u32 request_start;
  487. u32 reserved_tail;
  488. int ret;
  489. trace_i915_gem_request_add(request);
  490. /*
  491. * To ensure that this call will not fail, space for its emissions
  492. * should already have been reserved in the ring buffer. Let the ring
  493. * know that it is time to use that space up.
  494. */
  495. request_start = ring->tail;
  496. reserved_tail = request->reserved_space;
  497. request->reserved_space = 0;
  498. /*
  499. * Emit any outstanding flushes - execbuf can fail to emit the flush
  500. * after having emitted the batchbuffer command. Hence we need to fix
  501. * things up similar to emitting the lazy request. The difference here
  502. * is that the flush _must_ happen before the next request, no matter
  503. * what.
  504. */
  505. if (flush_caches) {
  506. ret = engine->emit_flush(request, EMIT_FLUSH);
  507. /* Not allowed to fail! */
  508. WARN(ret, "engine->emit_flush() failed: %d!\n", ret);
  509. }
  510. /* Record the position of the start of the breadcrumb so that
  511. * should we detect the updated seqno part-way through the
  512. * GPU processing the request, we never over-estimate the
  513. * position of the ring's HEAD.
  514. */
  515. request->postfix = ring->tail;
  516. /* Not allowed to fail! */
  517. ret = engine->emit_request(request);
  518. WARN(ret, "(%s)->emit_request failed: %d!\n", engine->name, ret);
  519. /* Sanity check that the reserved size was large enough. */
  520. ret = ring->tail - request_start;
  521. if (ret < 0)
  522. ret += ring->size;
  523. WARN_ONCE(ret > reserved_tail,
  524. "Not enough space reserved (%d bytes) "
  525. "for adding the request (%d bytes)\n",
  526. reserved_tail, ret);
  527. /* Seal the request and mark it as pending execution. Note that
  528. * we may inspect this state, without holding any locks, during
  529. * hangcheck. Hence we apply the barrier to ensure that we do not
  530. * see a more recent value in the hws than we are tracking.
  531. */
  532. prev = i915_gem_active_raw(&engine->last_request,
  533. &request->i915->drm.struct_mutex);
  534. if (prev)
  535. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  536. &request->submitq);
  537. request->emitted_jiffies = jiffies;
  538. request->previous_seqno = engine->last_pending_seqno;
  539. engine->last_pending_seqno = request->fence.seqno;
  540. i915_gem_active_set(&engine->last_request, request);
  541. list_add_tail(&request->link, &engine->request_list);
  542. list_add_tail(&request->ring_link, &ring->request_list);
  543. i915_gem_mark_busy(engine);
  544. local_bh_disable();
  545. i915_sw_fence_commit(&request->submit);
  546. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  547. }
  548. static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  549. {
  550. unsigned long flags;
  551. spin_lock_irqsave(&q->lock, flags);
  552. if (list_empty(&wait->task_list))
  553. __add_wait_queue(q, wait);
  554. spin_unlock_irqrestore(&q->lock, flags);
  555. }
  556. static unsigned long local_clock_us(unsigned int *cpu)
  557. {
  558. unsigned long t;
  559. /* Cheaply and approximately convert from nanoseconds to microseconds.
  560. * The result and subsequent calculations are also defined in the same
  561. * approximate microseconds units. The principal source of timing
  562. * error here is from the simple truncation.
  563. *
  564. * Note that local_clock() is only defined wrt to the current CPU;
  565. * the comparisons are no longer valid if we switch CPUs. Instead of
  566. * blocking preemption for the entire busywait, we can detect the CPU
  567. * switch and use that as indicator of system load and a reason to
  568. * stop busywaiting, see busywait_stop().
  569. */
  570. *cpu = get_cpu();
  571. t = local_clock() >> 10;
  572. put_cpu();
  573. return t;
  574. }
  575. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  576. {
  577. unsigned int this_cpu;
  578. if (time_after(local_clock_us(&this_cpu), timeout))
  579. return true;
  580. return this_cpu != cpu;
  581. }
  582. bool __i915_spin_request(const struct drm_i915_gem_request *req,
  583. int state, unsigned long timeout_us)
  584. {
  585. unsigned int cpu;
  586. /* When waiting for high frequency requests, e.g. during synchronous
  587. * rendering split between the CPU and GPU, the finite amount of time
  588. * required to set up the irq and wait upon it limits the response
  589. * rate. By busywaiting on the request completion for a short while we
  590. * can service the high frequency waits as quick as possible. However,
  591. * if it is a slow request, we want to sleep as quickly as possible.
  592. * The tradeoff between waiting and sleeping is roughly the time it
  593. * takes to sleep on a request, on the order of a microsecond.
  594. */
  595. timeout_us += local_clock_us(&cpu);
  596. do {
  597. if (i915_gem_request_completed(req))
  598. return true;
  599. if (signal_pending_state(state, current))
  600. break;
  601. if (busywait_stop(timeout_us, cpu))
  602. break;
  603. cpu_relax_lowlatency();
  604. } while (!need_resched());
  605. return false;
  606. }
  607. /**
  608. * i915_wait_request - wait until execution of request has finished
  609. * @req: duh!
  610. * @flags: how to wait
  611. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  612. * @rps: client to charge for RPS boosting
  613. *
  614. * Note: It is of utmost importance that the passed in seqno and reset_counter
  615. * values have been read by the caller in an smp safe manner. Where read-side
  616. * locks are involved, it is sufficient to read the reset_counter before
  617. * unlocking the lock that protects the seqno. For lockless tricks, the
  618. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  619. * inserted.
  620. *
  621. * Returns 0 if the request was found within the alloted time. Else returns the
  622. * errno with remaining time filled in timeout argument.
  623. */
  624. int i915_wait_request(struct drm_i915_gem_request *req,
  625. unsigned int flags,
  626. s64 *timeout,
  627. struct intel_rps_client *rps)
  628. {
  629. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  630. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  631. DEFINE_WAIT(reset);
  632. struct intel_wait wait;
  633. unsigned long timeout_remain;
  634. int ret = 0;
  635. might_sleep();
  636. #if IS_ENABLED(CONFIG_LOCKDEP)
  637. GEM_BUG_ON(!!lockdep_is_held(&req->i915->drm.struct_mutex) !=
  638. !!(flags & I915_WAIT_LOCKED));
  639. #endif
  640. if (i915_gem_request_completed(req))
  641. return 0;
  642. timeout_remain = MAX_SCHEDULE_TIMEOUT;
  643. if (timeout) {
  644. if (WARN_ON(*timeout < 0))
  645. return -EINVAL;
  646. if (*timeout == 0)
  647. return -ETIME;
  648. /* Record current time in case interrupted, or wedged */
  649. timeout_remain = nsecs_to_jiffies_timeout(*timeout);
  650. *timeout += ktime_get_raw_ns();
  651. }
  652. trace_i915_gem_request_wait_begin(req);
  653. /* This client is about to stall waiting for the GPU. In many cases
  654. * this is undesirable and limits the throughput of the system, as
  655. * many clients cannot continue processing user input/output whilst
  656. * blocked. RPS autotuning may take tens of milliseconds to respond
  657. * to the GPU load and thus incurs additional latency for the client.
  658. * We can circumvent that by promoting the GPU frequency to maximum
  659. * before we wait. This makes the GPU throttle up much more quickly
  660. * (good for benchmarks and user experience, e.g. window animations),
  661. * but at a cost of spending more power processing the workload
  662. * (bad for battery). Not all clients even want their results
  663. * immediately and for them we should just let the GPU select its own
  664. * frequency to maximise efficiency. To prevent a single client from
  665. * forcing the clocks too high for the whole system, we only allow
  666. * each client to waitboost once in a busy period.
  667. */
  668. if (IS_RPS_CLIENT(rps) && INTEL_GEN(req->i915) >= 6)
  669. gen6_rps_boost(req->i915, rps, req->emitted_jiffies);
  670. /* Optimistic short spin before touching IRQs */
  671. if (i915_spin_request(req, state, 5))
  672. goto complete;
  673. set_current_state(state);
  674. if (flags & I915_WAIT_LOCKED)
  675. add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  676. intel_wait_init(&wait, req->fence.seqno);
  677. if (intel_engine_add_wait(req->engine, &wait))
  678. /* In order to check that we haven't missed the interrupt
  679. * as we enabled it, we need to kick ourselves to do a
  680. * coherent check on the seqno before we sleep.
  681. */
  682. goto wakeup;
  683. for (;;) {
  684. if (signal_pending_state(state, current)) {
  685. ret = -ERESTARTSYS;
  686. break;
  687. }
  688. timeout_remain = io_schedule_timeout(timeout_remain);
  689. if (timeout_remain == 0) {
  690. ret = -ETIME;
  691. break;
  692. }
  693. if (intel_wait_complete(&wait))
  694. break;
  695. set_current_state(state);
  696. wakeup:
  697. /* Carefully check if the request is complete, giving time
  698. * for the seqno to be visible following the interrupt.
  699. * We also have to check in case we are kicked by the GPU
  700. * reset in order to drop the struct_mutex.
  701. */
  702. if (__i915_request_irq_complete(req))
  703. break;
  704. /* If the GPU is hung, and we hold the lock, reset the GPU
  705. * and then check for completion. On a full reset, the engine's
  706. * HW seqno will be advanced passed us and we are complete.
  707. * If we do a partial reset, we have to wait for the GPU to
  708. * resume and update the breadcrumb.
  709. *
  710. * If we don't hold the mutex, we can just wait for the worker
  711. * to come along and update the breadcrumb (either directly
  712. * itself, or indirectly by recovering the GPU).
  713. */
  714. if (flags & I915_WAIT_LOCKED &&
  715. i915_reset_in_progress(&req->i915->gpu_error)) {
  716. __set_current_state(TASK_RUNNING);
  717. i915_reset(req->i915);
  718. reset_wait_queue(&req->i915->gpu_error.wait_queue,
  719. &reset);
  720. continue;
  721. }
  722. /* Only spin if we know the GPU is processing this request */
  723. if (i915_spin_request(req, state, 2))
  724. break;
  725. }
  726. intel_engine_remove_wait(req->engine, &wait);
  727. if (flags & I915_WAIT_LOCKED)
  728. remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  729. __set_current_state(TASK_RUNNING);
  730. complete:
  731. trace_i915_gem_request_wait_end(req);
  732. if (timeout) {
  733. *timeout -= ktime_get_raw_ns();
  734. if (*timeout < 0)
  735. *timeout = 0;
  736. /*
  737. * Apparently ktime isn't accurate enough and occasionally has a
  738. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  739. * things up to make the test happy. We allow up to 1 jiffy.
  740. *
  741. * This is a regrssion from the timespec->ktime conversion.
  742. */
  743. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  744. *timeout = 0;
  745. }
  746. if (IS_RPS_USER(rps) &&
  747. req->fence.seqno == req->engine->last_submitted_seqno) {
  748. /* The GPU is now idle and this client has stalled.
  749. * Since no other client has submitted a request in the
  750. * meantime, assume that this client is the only one
  751. * supplying work to the GPU but is unable to keep that
  752. * work supplied because it is waiting. Since the GPU is
  753. * then never kept fully busy, RPS autoclocking will
  754. * keep the clocks relatively low, causing further delays.
  755. * Compensate by giving the synchronous client credit for
  756. * a waitboost next time.
  757. */
  758. spin_lock(&req->i915->rps.client_lock);
  759. list_del_init(&rps->link);
  760. spin_unlock(&req->i915->rps.client_lock);
  761. }
  762. return ret;
  763. }
  764. static bool engine_retire_requests(struct intel_engine_cs *engine)
  765. {
  766. struct drm_i915_gem_request *request, *next;
  767. list_for_each_entry_safe(request, next, &engine->request_list, link) {
  768. if (!i915_gem_request_completed(request))
  769. return false;
  770. i915_gem_request_retire(request);
  771. }
  772. return true;
  773. }
  774. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  775. {
  776. struct intel_engine_cs *engine;
  777. unsigned int tmp;
  778. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  779. if (dev_priv->gt.active_engines == 0)
  780. return;
  781. GEM_BUG_ON(!dev_priv->gt.awake);
  782. for_each_engine_masked(engine, dev_priv, dev_priv->gt.active_engines, tmp)
  783. if (engine_retire_requests(engine))
  784. dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
  785. if (dev_priv->gt.active_engines == 0)
  786. queue_delayed_work(dev_priv->wq,
  787. &dev_priv->gt.idle_work,
  788. msecs_to_jiffies(100));
  789. }