xref: /linux/drivers/gpu/drm/i915/display/intel_frontbuffer.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 /*
2  * Copyright © 2014 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
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Daniel Vetter <daniel.vetter@ffwll.ch>
25  */
26 
27 /**
28  * DOC: frontbuffer tracking
29  *
30  * Many features require us to track changes to the currently active
31  * frontbuffer, especially rendering targeted at the frontbuffer.
32  *
33  * To be able to do so we track frontbuffers using a bitmask for all possible
34  * frontbuffer slots through intel_frontbuffer_track(). The functions in this
35  * file are then called when the contents of the frontbuffer are invalidated,
36  * when frontbuffer rendering has stopped again to flush out all the changes
37  * and when the frontbuffer is exchanged with a flip. Subsystems interested in
38  * frontbuffer changes (e.g. PSR, FBC, DRRS) should directly put their callbacks
39  * into the relevant places and filter for the frontbuffer slots that they are
40  * interested int.
41  *
42  * On a high level there are two types of powersaving features. The first one
43  * work like a special cache (FBC and PSR) and are interested when they should
44  * stop caching and when to restart caching. This is done by placing callbacks
45  * into the invalidate and the flush functions: At invalidate the caching must
46  * be stopped and at flush time it can be restarted. And maybe they need to know
47  * when the frontbuffer changes (e.g. when the hw doesn't initiate an invalidate
48  * and flush on its own) which can be achieved with placing callbacks into the
49  * flip functions.
50  *
51  * The other type of display power saving feature only cares about busyness
52  * (e.g. DRRS). In that case all three (invalidate, flush and flip) indicate
53  * busyness. There is no direct way to detect idleness. Instead an idle timer
54  * work delayed work should be started from the flush and flip functions and
55  * cancelled as soon as busyness is detected.
56  */
57 
58 #include "gem/i915_gem_object_frontbuffer.h"
59 #include "i915_active.h"
60 #include "i915_drv.h"
61 #include "intel_display_trace.h"
62 #include "intel_display_types.h"
63 #include "intel_dp.h"
64 #include "intel_drrs.h"
65 #include "intel_fbc.h"
66 #include "intel_frontbuffer.h"
67 #include "intel_psr.h"
68 #include "intel_tdf.h"
69 
70 /**
71  * frontbuffer_flush - flush frontbuffer
72  * @i915: i915 device
73  * @frontbuffer_bits: frontbuffer plane tracking bits
74  * @origin: which operation caused the flush
75  *
76  * This function gets called every time rendering on the given planes has
77  * completed and frontbuffer caching can be started again. Flushes will get
78  * delayed if they're blocked by some outstanding asynchronous rendering.
79  *
80  * Can be called without any locks held.
81  */
82 static void frontbuffer_flush(struct drm_i915_private *i915,
83 			      unsigned int frontbuffer_bits,
84 			      enum fb_op_origin origin)
85 {
86 	/* Delay flushing when rings are still busy.*/
87 	spin_lock(&i915->display.fb_tracking.lock);
88 	frontbuffer_bits &= ~i915->display.fb_tracking.busy_bits;
89 	spin_unlock(&i915->display.fb_tracking.lock);
90 
91 	if (!frontbuffer_bits)
92 		return;
93 
94 	trace_intel_frontbuffer_flush(i915, frontbuffer_bits, origin);
95 
96 	might_sleep();
97 	intel_td_flush(i915);
98 	intel_drrs_flush(i915, frontbuffer_bits);
99 	intel_psr_flush(i915, frontbuffer_bits, origin);
100 	intel_fbc_flush(i915, frontbuffer_bits, origin);
101 }
102 
103 /**
104  * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
105  * @i915: i915 device
106  * @frontbuffer_bits: frontbuffer plane tracking bits
107  *
108  * This function gets called after scheduling a flip on @obj. The actual
109  * frontbuffer flushing will be delayed until completion is signalled with
110  * intel_frontbuffer_flip_complete. If an invalidate happens in between this
111  * flush will be cancelled.
112  *
113  * Can be called without any locks held.
114  */
115 void intel_frontbuffer_flip_prepare(struct drm_i915_private *i915,
116 				    unsigned frontbuffer_bits)
117 {
118 	spin_lock(&i915->display.fb_tracking.lock);
119 	i915->display.fb_tracking.flip_bits |= frontbuffer_bits;
120 	/* Remove stale busy bits due to the old buffer. */
121 	i915->display.fb_tracking.busy_bits &= ~frontbuffer_bits;
122 	spin_unlock(&i915->display.fb_tracking.lock);
123 }
124 
125 /**
126  * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
127  * @i915: i915 device
128  * @frontbuffer_bits: frontbuffer plane tracking bits
129  *
130  * This function gets called after the flip has been latched and will complete
131  * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
132  *
133  * Can be called without any locks held.
134  */
135 void intel_frontbuffer_flip_complete(struct drm_i915_private *i915,
136 				     unsigned frontbuffer_bits)
137 {
138 	spin_lock(&i915->display.fb_tracking.lock);
139 	/* Mask any cancelled flips. */
140 	frontbuffer_bits &= i915->display.fb_tracking.flip_bits;
141 	i915->display.fb_tracking.flip_bits &= ~frontbuffer_bits;
142 	spin_unlock(&i915->display.fb_tracking.lock);
143 
144 	if (frontbuffer_bits)
145 		frontbuffer_flush(i915, frontbuffer_bits, ORIGIN_FLIP);
146 }
147 
148 /**
149  * intel_frontbuffer_flip - synchronous frontbuffer flip
150  * @i915: i915 device
151  * @frontbuffer_bits: frontbuffer plane tracking bits
152  *
153  * This function gets called after scheduling a flip on @obj. This is for
154  * synchronous plane updates which will happen on the next vblank and which will
155  * not get delayed by pending gpu rendering.
156  *
157  * Can be called without any locks held.
158  */
159 void intel_frontbuffer_flip(struct drm_i915_private *i915,
160 			    unsigned frontbuffer_bits)
161 {
162 	spin_lock(&i915->display.fb_tracking.lock);
163 	/* Remove stale busy bits due to the old buffer. */
164 	i915->display.fb_tracking.busy_bits &= ~frontbuffer_bits;
165 	spin_unlock(&i915->display.fb_tracking.lock);
166 
167 	frontbuffer_flush(i915, frontbuffer_bits, ORIGIN_FLIP);
168 }
169 
170 void __intel_fb_invalidate(struct intel_frontbuffer *front,
171 			   enum fb_op_origin origin,
172 			   unsigned int frontbuffer_bits)
173 {
174 	struct drm_i915_private *i915 = intel_bo_to_i915(front->obj);
175 
176 	if (origin == ORIGIN_CS) {
177 		spin_lock(&i915->display.fb_tracking.lock);
178 		i915->display.fb_tracking.busy_bits |= frontbuffer_bits;
179 		i915->display.fb_tracking.flip_bits &= ~frontbuffer_bits;
180 		spin_unlock(&i915->display.fb_tracking.lock);
181 	}
182 
183 	trace_intel_frontbuffer_invalidate(i915, frontbuffer_bits, origin);
184 
185 	might_sleep();
186 	intel_psr_invalidate(i915, frontbuffer_bits, origin);
187 	intel_drrs_invalidate(i915, frontbuffer_bits);
188 	intel_fbc_invalidate(i915, frontbuffer_bits, origin);
189 }
190 
191 void __intel_fb_flush(struct intel_frontbuffer *front,
192 		      enum fb_op_origin origin,
193 		      unsigned int frontbuffer_bits)
194 {
195 	struct drm_i915_private *i915 = intel_bo_to_i915(front->obj);
196 
197 	if (origin == ORIGIN_CS) {
198 		spin_lock(&i915->display.fb_tracking.lock);
199 		/* Filter out new bits since rendering started. */
200 		frontbuffer_bits &= i915->display.fb_tracking.busy_bits;
201 		i915->display.fb_tracking.busy_bits &= ~frontbuffer_bits;
202 		spin_unlock(&i915->display.fb_tracking.lock);
203 	}
204 
205 	if (frontbuffer_bits)
206 		frontbuffer_flush(i915, frontbuffer_bits, origin);
207 }
208 
209 static void intel_frontbuffer_flush_work(struct work_struct *work)
210 {
211 	struct intel_frontbuffer *front =
212 		container_of(work, struct intel_frontbuffer, flush_work);
213 
214 	i915_gem_object_flush_if_display(front->obj);
215 	intel_frontbuffer_flush(front, ORIGIN_DIRTYFB);
216 	intel_frontbuffer_put(front);
217 }
218 
219 /**
220  * intel_frontbuffer_queue_flush - queue flushing frontbuffer object
221  * @front: GEM object to flush
222  *
223  * This function is targeted for our dirty callback for queueing flush when
224  * dma fence is signales
225  */
226 void intel_frontbuffer_queue_flush(struct intel_frontbuffer *front)
227 {
228 	if (!front)
229 		return;
230 
231 	kref_get(&front->ref);
232 	if (!schedule_work(&front->flush_work))
233 		intel_frontbuffer_put(front);
234 }
235 
236 static int frontbuffer_active(struct i915_active *ref)
237 {
238 	struct intel_frontbuffer *front =
239 		container_of(ref, typeof(*front), write);
240 
241 	kref_get(&front->ref);
242 	return 0;
243 }
244 
245 static void frontbuffer_retire(struct i915_active *ref)
246 {
247 	struct intel_frontbuffer *front =
248 		container_of(ref, typeof(*front), write);
249 
250 	intel_frontbuffer_flush(front, ORIGIN_CS);
251 	intel_frontbuffer_put(front);
252 }
253 
254 static void frontbuffer_release(struct kref *ref)
255 	__releases(&intel_bo_to_i915(front->obj)->display.fb_tracking.lock)
256 {
257 	struct intel_frontbuffer *ret, *front =
258 		container_of(ref, typeof(*front), ref);
259 	struct drm_i915_gem_object *obj = front->obj;
260 
261 	drm_WARN_ON(&intel_bo_to_i915(obj)->drm, atomic_read(&front->bits));
262 
263 	i915_ggtt_clear_scanout(obj);
264 
265 	ret = i915_gem_object_set_frontbuffer(obj, NULL);
266 	drm_WARN_ON(&intel_bo_to_i915(obj)->drm, ret);
267 	spin_unlock(&intel_bo_to_i915(obj)->display.fb_tracking.lock);
268 
269 	i915_active_fini(&front->write);
270 	kfree_rcu(front, rcu);
271 }
272 
273 struct intel_frontbuffer *
274 intel_frontbuffer_get(struct drm_i915_gem_object *obj)
275 {
276 	struct drm_i915_private *i915 = intel_bo_to_i915(obj);
277 	struct intel_frontbuffer *front, *cur;
278 
279 	front = i915_gem_object_get_frontbuffer(obj);
280 	if (front)
281 		return front;
282 
283 	front = kmalloc(sizeof(*front), GFP_KERNEL);
284 	if (!front)
285 		return NULL;
286 
287 	front->obj = obj;
288 	kref_init(&front->ref);
289 	atomic_set(&front->bits, 0);
290 	i915_active_init(&front->write,
291 			 frontbuffer_active,
292 			 frontbuffer_retire,
293 			 I915_ACTIVE_RETIRE_SLEEPS);
294 	INIT_WORK(&front->flush_work, intel_frontbuffer_flush_work);
295 
296 	spin_lock(&i915->display.fb_tracking.lock);
297 	cur = i915_gem_object_set_frontbuffer(obj, front);
298 	spin_unlock(&i915->display.fb_tracking.lock);
299 	if (cur != front)
300 		kfree(front);
301 	return cur;
302 }
303 
304 void intel_frontbuffer_put(struct intel_frontbuffer *front)
305 {
306 	kref_put_lock(&front->ref,
307 		      frontbuffer_release,
308 		      &intel_bo_to_i915(front->obj)->display.fb_tracking.lock);
309 }
310 
311 /**
312  * intel_frontbuffer_track - update frontbuffer tracking
313  * @old: current buffer for the frontbuffer slots
314  * @new: new buffer for the frontbuffer slots
315  * @frontbuffer_bits: bitmask of frontbuffer slots
316  *
317  * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
318  * from @old and setting them in @new. Both @old and @new can be NULL.
319  */
320 void intel_frontbuffer_track(struct intel_frontbuffer *old,
321 			     struct intel_frontbuffer *new,
322 			     unsigned int frontbuffer_bits)
323 {
324 	/*
325 	 * Control of individual bits within the mask are guarded by
326 	 * the owning plane->mutex, i.e. we can never see concurrent
327 	 * manipulation of individual bits. But since the bitfield as a whole
328 	 * is updated using RMW, we need to use atomics in order to update
329 	 * the bits.
330 	 */
331 	BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
332 		     BITS_PER_TYPE(atomic_t));
333 	BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > 32);
334 	BUILD_BUG_ON(I915_MAX_PLANES > INTEL_FRONTBUFFER_BITS_PER_PIPE);
335 
336 	if (old) {
337 		drm_WARN_ON(&intel_bo_to_i915(old->obj)->drm,
338 			    !(atomic_read(&old->bits) & frontbuffer_bits));
339 		atomic_andnot(frontbuffer_bits, &old->bits);
340 	}
341 
342 	if (new) {
343 		drm_WARN_ON(&intel_bo_to_i915(new->obj)->drm,
344 			    atomic_read(&new->bits) & frontbuffer_bits);
345 		atomic_or(frontbuffer_bits, &new->bits);
346 	}
347 }
348