blob: 6cfae2079e192899b2a6abb6deb0697aa104458b [file] [log] [blame]
Chris Wilson05235c52016-07-20 09:21:08 +01001/*
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
25#ifndef I915_GEM_REQUEST_H
26#define I915_GEM_REQUEST_H
27
Chris Wilson04769652016-07-20 09:21:11 +010028#include <linux/fence.h>
29
30#include "i915_gem.h"
31
Chris Wilson05235c52016-07-20 09:21:08 +010032/**
33 * Request queue structure.
34 *
35 * The request queue allows us to note sequence numbers that have been emitted
36 * and may be associated with active buffers to be retired.
37 *
38 * By keeping this list, we can avoid having to do questionable sequence
39 * number comparisons on buffer last_read|write_seqno. It also allows an
40 * emission time to be associated with the request for tracking how far ahead
41 * of the GPU the submission is.
42 *
Chris Wilson04769652016-07-20 09:21:11 +010043 * The requests are reference counted.
Chris Wilson05235c52016-07-20 09:21:08 +010044 */
45struct drm_i915_gem_request {
Chris Wilson04769652016-07-20 09:21:11 +010046 struct fence fence;
47 spinlock_t lock;
Chris Wilson05235c52016-07-20 09:21:08 +010048
49 /** On Which ring this request was generated */
50 struct drm_i915_private *i915;
51
52 /**
53 * Context and ring buffer related to this request
54 * Contexts are refcounted, so when this request is associated with a
55 * context, we must increment the context's refcount, to guarantee that
56 * it persists while any request is linked to it. Requests themselves
57 * are also refcounted, so the request will only be freed when the last
58 * reference to it is dismissed, and the code in
59 * i915_gem_request_free() will then decrement the refcount on the
60 * context.
61 */
62 struct i915_gem_context *ctx;
63 struct intel_engine_cs *engine;
Chris Wilson7e37f882016-08-02 22:50:21 +010064 struct intel_ring *ring;
Chris Wilson05235c52016-07-20 09:21:08 +010065 struct intel_signal_node signaling;
66
67 /** GEM sequence number associated with the previous request,
68 * when the HWS breadcrumb is equal to this the GPU is processing
69 * this request.
70 */
71 u32 previous_seqno;
72
Chris Wilson05235c52016-07-20 09:21:08 +010073 /** Position in the ringbuffer of the start of the request */
74 u32 head;
75
76 /**
77 * Position in the ringbuffer of the start of the postfix.
78 * This is required to calculate the maximum available ringbuffer
79 * space without overwriting the postfix.
80 */
81 u32 postfix;
82
83 /** Position in the ringbuffer of the end of the whole request */
84 u32 tail;
85
86 /** Preallocate space in the ringbuffer for the emitting the request */
87 u32 reserved_space;
88
89 /**
90 * Context related to the previous request.
91 * As the contexts are accessed by the hardware until the switch is
92 * completed to a new context, the hardware may still be writing
93 * to the context object after the breadcrumb is visible. We must
94 * not unpin/unbind/prune that object whilst still active and so
95 * we keep the previous context pinned until the following (this)
96 * request is retired.
97 */
98 struct i915_gem_context *previous_context;
99
100 /** Batch buffer related to this request if any (used for
101 * error state dump only).
102 */
103 struct drm_i915_gem_object *batch_obj;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100104 struct list_head active_list;
Chris Wilson05235c52016-07-20 09:21:08 +0100105
106 /** Time at which this request was emitted, in jiffies. */
107 unsigned long emitted_jiffies;
108
Chris Wilsonefdf7c02016-08-04 07:52:33 +0100109 /** engine->request_list entry for this request */
110 struct list_head link;
Chris Wilson05235c52016-07-20 09:21:08 +0100111
112 struct drm_i915_file_private *file_priv;
113 /** file_priv list entry for this request */
114 struct list_head client_list;
115
116 /** process identifier submitting this request */
117 struct pid *pid;
118
119 /**
120 * The ELSP only accepts two elements at a time, so we queue
121 * context/tail pairs on a given queue (ring->execlist_queue) until the
122 * hardware is available. The queue serves a double purpose: we also use
123 * it to keep track of the up to 2 contexts currently in the hardware
124 * (usually one in execution and the other queued up by the GPU): We
125 * only remove elements from the head of the queue when the hardware
126 * informs us that an element has been completed.
127 *
128 * All accesses to the queue are mediated by a spinlock
129 * (ring->execlist_lock).
130 */
131
132 /** Execlist link in the submission queue.*/
133 struct list_head execlist_link;
134
135 /** Execlists no. of times this request has been sent to the ELSP */
136 int elsp_submitted;
137
138 /** Execlists context hardware id. */
139 unsigned int ctx_hw_id;
140};
141
Chris Wilson04769652016-07-20 09:21:11 +0100142extern const struct fence_ops i915_fence_ops;
143
144static inline bool fence_is_i915(struct fence *fence)
145{
146 return fence->ops == &i915_fence_ops;
147}
148
Chris Wilson05235c52016-07-20 09:21:08 +0100149struct drm_i915_gem_request * __must_check
150i915_gem_request_alloc(struct intel_engine_cs *engine,
151 struct i915_gem_context *ctx);
Chris Wilson05235c52016-07-20 09:21:08 +0100152int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
153 struct drm_file *file);
154void i915_gem_request_retire_upto(struct drm_i915_gem_request *req);
155
156static inline u32
157i915_gem_request_get_seqno(struct drm_i915_gem_request *req)
158{
Chris Wilson04769652016-07-20 09:21:11 +0100159 return req ? req->fence.seqno : 0;
Chris Wilson05235c52016-07-20 09:21:08 +0100160}
161
162static inline struct intel_engine_cs *
163i915_gem_request_get_engine(struct drm_i915_gem_request *req)
164{
165 return req ? req->engine : NULL;
166}
167
168static inline struct drm_i915_gem_request *
Chris Wilson04769652016-07-20 09:21:11 +0100169to_request(struct fence *fence)
170{
171 /* We assume that NULL fence/request are interoperable */
172 BUILD_BUG_ON(offsetof(struct drm_i915_gem_request, fence) != 0);
173 GEM_BUG_ON(fence && !fence_is_i915(fence));
174 return container_of(fence, struct drm_i915_gem_request, fence);
175}
176
177static inline struct drm_i915_gem_request *
Chris Wilsone8a261e2016-07-20 13:31:49 +0100178i915_gem_request_get(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100179{
Chris Wilson04769652016-07-20 09:21:11 +0100180 return to_request(fence_get(&req->fence));
Chris Wilson05235c52016-07-20 09:21:08 +0100181}
182
183static inline void
Chris Wilsone8a261e2016-07-20 13:31:49 +0100184i915_gem_request_put(struct drm_i915_gem_request *req)
Chris Wilson05235c52016-07-20 09:21:08 +0100185{
Chris Wilson04769652016-07-20 09:21:11 +0100186 fence_put(&req->fence);
Chris Wilson05235c52016-07-20 09:21:08 +0100187}
188
189static inline void i915_gem_request_assign(struct drm_i915_gem_request **pdst,
190 struct drm_i915_gem_request *src)
191{
192 if (src)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100193 i915_gem_request_get(src);
Chris Wilson05235c52016-07-20 09:21:08 +0100194
195 if (*pdst)
Chris Wilsone8a261e2016-07-20 13:31:49 +0100196 i915_gem_request_put(*pdst);
Chris Wilson05235c52016-07-20 09:21:08 +0100197
198 *pdst = src;
199}
200
201void __i915_add_request(struct drm_i915_gem_request *req,
202 struct drm_i915_gem_object *batch_obj,
203 bool flush_caches);
204#define i915_add_request(req) \
205 __i915_add_request(req, NULL, true)
206#define i915_add_request_no_flush(req) \
207 __i915_add_request(req, NULL, false)
208
209struct intel_rps_client;
Chris Wilson42df2712016-07-20 09:21:12 +0100210#define NO_WAITBOOST ERR_PTR(-1)
211#define IS_RPS_CLIENT(p) (!IS_ERR(p))
212#define IS_RPS_USER(p) (!IS_ERR_OR_NULL(p))
Chris Wilson05235c52016-07-20 09:21:08 +0100213
214int __i915_wait_request(struct drm_i915_gem_request *req,
215 bool interruptible,
216 s64 *timeout,
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100217 struct intel_rps_client *rps)
218 __attribute__((nonnull(1)));
219
220int __must_check
221i915_wait_request(struct drm_i915_gem_request *req)
222 __attribute__((nonnull));
Chris Wilson05235c52016-07-20 09:21:08 +0100223
224static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine);
225
226/**
227 * Returns true if seq1 is later than seq2.
228 */
229static inline bool i915_seqno_passed(u32 seq1, u32 seq2)
230{
231 return (s32)(seq1 - seq2) >= 0;
232}
233
234static inline bool
235i915_gem_request_started(const struct drm_i915_gem_request *req)
236{
237 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
238 req->previous_seqno);
239}
240
241static inline bool
242i915_gem_request_completed(const struct drm_i915_gem_request *req)
243{
244 return i915_seqno_passed(intel_engine_get_seqno(req->engine),
Chris Wilson04769652016-07-20 09:21:11 +0100245 req->fence.seqno);
Chris Wilson05235c52016-07-20 09:21:08 +0100246}
247
248bool __i915_spin_request(const struct drm_i915_gem_request *request,
249 int state, unsigned long timeout_us);
250static inline bool i915_spin_request(const struct drm_i915_gem_request *request,
251 int state, unsigned long timeout_us)
252{
253 return (i915_gem_request_started(request) &&
254 __i915_spin_request(request, state, timeout_us));
255}
256
Chris Wilson381f3712016-08-04 07:52:29 +0100257/* We treat requests as fences. This is not be to confused with our
258 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
259 * We use the fences to synchronize access from the CPU with activity on the
260 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
261 * is reading them. We also track fences at a higher level to provide
262 * implicit synchronisation around GEM objects, e.g. set-domain will wait
263 * for outstanding GPU rendering before marking the object ready for CPU
264 * access, or a pageflip will wait until the GPU is complete before showing
265 * the frame on the scanout.
266 *
267 * In order to use a fence, the object must track the fence it needs to
268 * serialise with. For example, GEM objects want to track both read and
269 * write access so that we can perform concurrent read operations between
270 * the CPU and GPU engines, as well as waiting for all rendering to
271 * complete, or waiting for the last GPU user of a "fence register". The
272 * object then embeds a #i915_gem_active to track the most recent (in
273 * retirement order) request relevant for the desired mode of access.
274 * The #i915_gem_active is updated with i915_gem_active_set() to track the
275 * most recent fence request, typically this is done as part of
276 * i915_vma_move_to_active().
277 *
278 * When the #i915_gem_active completes (is retired), it will
279 * signal its completion to the owner through a callback as well as mark
280 * itself as idle (i915_gem_active.request == NULL). The owner
281 * can then perform any action, such as delayed freeing of an active
282 * resource including itself.
283 */
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100284struct i915_gem_active;
285
286typedef void (*i915_gem_retire_fn)(struct i915_gem_active *,
287 struct drm_i915_gem_request *);
288
Chris Wilson381f3712016-08-04 07:52:29 +0100289struct i915_gem_active {
290 struct drm_i915_gem_request *request;
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100291 struct list_head link;
292 i915_gem_retire_fn retire;
Chris Wilson381f3712016-08-04 07:52:29 +0100293};
294
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100295void i915_gem_retire_noop(struct i915_gem_active *,
296 struct drm_i915_gem_request *request);
297
298/**
299 * init_request_active - prepares the activity tracker for use
300 * @active - the active tracker
301 * @func - a callback when then the tracker is retired (becomes idle),
302 * can be NULL
303 *
304 * init_request_active() prepares the embedded @active struct for use as
305 * an activity tracker, that is for tracking the last known active request
306 * associated with it. When the last request becomes idle, when it is retired
307 * after completion, the optional callback @func is invoked.
308 */
309static inline void
310init_request_active(struct i915_gem_active *active,
311 i915_gem_retire_fn retire)
312{
313 INIT_LIST_HEAD(&active->link);
314 active->retire = retire ?: i915_gem_retire_noop;
315}
316
Chris Wilson27c01aa2016-08-04 07:52:30 +0100317/**
318 * i915_gem_active_set - updates the tracker to watch the current request
319 * @active - the active tracker
320 * @request - the request to watch
321 *
322 * i915_gem_active_set() watches the given @request for completion. Whilst
323 * that @request is busy, the @active reports busy. When that @request is
324 * retired, the @active tracker is updated to report idle.
325 */
Chris Wilson381f3712016-08-04 07:52:29 +0100326static inline void
327i915_gem_active_set(struct i915_gem_active *active,
328 struct drm_i915_gem_request *request)
329{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100330 list_move(&active->link, &request->active_list);
331 active->request = request;
Chris Wilson381f3712016-08-04 07:52:29 +0100332}
333
Chris Wilsond72d9082016-08-04 07:52:31 +0100334static inline struct drm_i915_gem_request *
335__i915_gem_active_peek(const struct i915_gem_active *active)
336{
337 return active->request;
338}
339
Chris Wilson27c01aa2016-08-04 07:52:30 +0100340/**
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100341 * i915_gem_active_peek - report the active request being monitored
Chris Wilson27c01aa2016-08-04 07:52:30 +0100342 * @active - the active tracker
343 *
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100344 * i915_gem_active_peek() returns the current request being tracked if
345 * still active, or NULL. It does not obtain a reference on the request
346 * for the caller, so the caller must hold struct_mutex.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100347 */
348static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100349i915_gem_active_peek(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100350{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100351 struct drm_i915_gem_request *request;
352
353 request = active->request;
354 if (!request || i915_gem_request_completed(request))
355 return NULL;
356
357 return request;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100358}
359
360/**
361 * i915_gem_active_get - return a reference to the active request
362 * @active - the active tracker
363 *
364 * i915_gem_active_get() returns a reference to the active request, or NULL
365 * if the active tracker is idle. The caller must hold struct_mutex.
366 */
367static inline struct drm_i915_gem_request *
Chris Wilsond72d9082016-08-04 07:52:31 +0100368i915_gem_active_get(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100369{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100370 return i915_gem_request_get(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100371}
372
373/**
374 * i915_gem_active_isset - report whether the active tracker is assigned
375 * @active - the active tracker
376 *
377 * i915_gem_active_isset() returns true if the active tracker is currently
378 * assigned to a request. Due to the lazy retiring, that request may be idle
379 * and this may report stale information.
380 */
381static inline bool
382i915_gem_active_isset(const struct i915_gem_active *active)
383{
384 return active->request;
385}
386
387/**
388 * i915_gem_active_is_idle - report whether the active tracker is idle
389 * @active - the active tracker
390 *
391 * i915_gem_active_is_idle() returns true if the active tracker is currently
392 * unassigned or if the request is complete (but not yet retired). Requires
393 * the caller to hold struct_mutex (but that can be relaxed if desired).
394 */
395static inline bool
Chris Wilsond72d9082016-08-04 07:52:31 +0100396i915_gem_active_is_idle(const struct i915_gem_active *active,
397 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100398{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100399 return !i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100400}
401
402/**
403 * i915_gem_active_wait - waits until the request is completed
404 * @active - the active request on which to wait
405 *
406 * i915_gem_active_wait() waits until the request is completed before
407 * returning. Note that it does not guarantee that the request is
408 * retired first, see i915_gem_active_retire().
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100409 *
410 * i915_gem_active_wait() returns immediately if the active
411 * request is already complete.
Chris Wilson27c01aa2016-08-04 07:52:30 +0100412 */
413static inline int __must_check
Chris Wilsond72d9082016-08-04 07:52:31 +0100414i915_gem_active_wait(const struct i915_gem_active *active, struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100415{
416 struct drm_i915_gem_request *request;
417
Chris Wilsond72d9082016-08-04 07:52:31 +0100418 request = i915_gem_active_peek(active, mutex);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100419 if (!request)
420 return 0;
421
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100422 return __i915_wait_request(request, true, NULL, NULL);
Chris Wilson27c01aa2016-08-04 07:52:30 +0100423}
424
425/**
426 * i915_gem_active_retire - waits until the request is retired
427 * @active - the active request on which to wait
428 *
429 * i915_gem_active_retire() waits until the request is completed,
430 * and then ensures that at least the retirement handler for this
431 * @active tracker is called before returning. If the @active
432 * tracker is idle, the function returns immediately.
433 */
434static inline int __must_check
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100435i915_gem_active_retire(struct i915_gem_active *active,
Chris Wilsond72d9082016-08-04 07:52:31 +0100436 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100437{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100438 struct drm_i915_gem_request *request;
439 int ret;
440
441 request = active->request;
442 if (!request)
443 return 0;
444
445 ret = __i915_wait_request(request, true, NULL, NULL);
446 if (ret)
447 return ret;
448
449 list_del_init(&active->link);
450 active->request = NULL;
451 active->retire(active, request);
452
453 return 0;
Chris Wilson27c01aa2016-08-04 07:52:30 +0100454}
455
456/* Convenience functions for peeking at state inside active's request whilst
457 * guarded by the struct_mutex.
458 */
459
460static inline uint32_t
Chris Wilsond72d9082016-08-04 07:52:31 +0100461i915_gem_active_get_seqno(const struct i915_gem_active *active,
462 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100463{
Chris Wilsond72d9082016-08-04 07:52:31 +0100464 return i915_gem_request_get_seqno(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100465}
466
467static inline struct intel_engine_cs *
Chris Wilsond72d9082016-08-04 07:52:31 +0100468i915_gem_active_get_engine(const struct i915_gem_active *active,
469 struct mutex *mutex)
Chris Wilson27c01aa2016-08-04 07:52:30 +0100470{
Chris Wilsond72d9082016-08-04 07:52:31 +0100471 return i915_gem_request_get_engine(i915_gem_active_peek(active, mutex));
Chris Wilson27c01aa2016-08-04 07:52:30 +0100472}
473
Chris Wilson381f3712016-08-04 07:52:29 +0100474#define for_each_active(mask, idx) \
475 for (; mask ? idx = ffs(mask) - 1, 1 : 0; mask &= ~BIT(idx))
476
Chris Wilson05235c52016-07-20 09:21:08 +0100477#endif /* I915_GEM_REQUEST_H */