i915_gem_request.c 19 KB

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