xref: /linux/drivers/gpu/drm/i915/display/intel_frontbuffer.c (revision 390db60f8e2bd21fae544917eb3a8618265c058c)
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 <drm/drm_gem.h>
59 
60 #include "i915_active.h"
61 #include "i915_vma.h"
62 #include "intel_bo.h"
63 #include "intel_display_trace.h"
64 #include "intel_display_types.h"
65 #include "intel_dp.h"
66 #include "intel_drrs.h"
67 #include "intel_fbc.h"
68 #include "intel_frontbuffer.h"
69 #include "intel_psr.h"
70 #include "intel_tdf.h"
71 
72 /**
73  * frontbuffer_flush - flush frontbuffer
74  * @display: display device
75  * @frontbuffer_bits: frontbuffer plane tracking bits
76  * @origin: which operation caused the flush
77  *
78  * This function gets called every time rendering on the given planes has
79  * completed and frontbuffer caching can be started again. Flushes will get
80  * delayed if they're blocked by some outstanding asynchronous rendering.
81  *
82  * Can be called without any locks held.
83  */
frontbuffer_flush(struct intel_display * display,unsigned int frontbuffer_bits,enum fb_op_origin origin)84 static void frontbuffer_flush(struct intel_display *display,
85 			      unsigned int frontbuffer_bits,
86 			      enum fb_op_origin origin)
87 {
88 	/* Delay flushing when rings are still busy.*/
89 	spin_lock(&display->fb_tracking.lock);
90 	frontbuffer_bits &= ~display->fb_tracking.busy_bits;
91 	spin_unlock(&display->fb_tracking.lock);
92 
93 	if (!frontbuffer_bits)
94 		return;
95 
96 	trace_intel_frontbuffer_flush(display, frontbuffer_bits, origin);
97 
98 	might_sleep();
99 	intel_td_flush(display);
100 	intel_drrs_flush(display, frontbuffer_bits);
101 	intel_psr_flush(display, frontbuffer_bits, origin);
102 	intel_fbc_flush(display, frontbuffer_bits, origin);
103 }
104 
105 /**
106  * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
107  * @display: display device
108  * @frontbuffer_bits: frontbuffer plane tracking bits
109  *
110  * This function gets called after scheduling a flip on @obj. The actual
111  * frontbuffer flushing will be delayed until completion is signalled with
112  * intel_frontbuffer_flip_complete. If an invalidate happens in between this
113  * flush will be cancelled.
114  *
115  * Can be called without any locks held.
116  */
intel_frontbuffer_flip_prepare(struct intel_display * display,unsigned frontbuffer_bits)117 void intel_frontbuffer_flip_prepare(struct intel_display *display,
118 				    unsigned frontbuffer_bits)
119 {
120 	spin_lock(&display->fb_tracking.lock);
121 	display->fb_tracking.flip_bits |= frontbuffer_bits;
122 	/* Remove stale busy bits due to the old buffer. */
123 	display->fb_tracking.busy_bits &= ~frontbuffer_bits;
124 	spin_unlock(&display->fb_tracking.lock);
125 }
126 
127 /**
128  * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
129  * @display: display device
130  * @frontbuffer_bits: frontbuffer plane tracking bits
131  *
132  * This function gets called after the flip has been latched and will complete
133  * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
134  *
135  * Can be called without any locks held.
136  */
intel_frontbuffer_flip_complete(struct intel_display * display,unsigned frontbuffer_bits)137 void intel_frontbuffer_flip_complete(struct intel_display *display,
138 				     unsigned frontbuffer_bits)
139 {
140 	spin_lock(&display->fb_tracking.lock);
141 	/* Mask any cancelled flips. */
142 	frontbuffer_bits &= display->fb_tracking.flip_bits;
143 	display->fb_tracking.flip_bits &= ~frontbuffer_bits;
144 	spin_unlock(&display->fb_tracking.lock);
145 
146 	if (frontbuffer_bits)
147 		frontbuffer_flush(display, frontbuffer_bits, ORIGIN_FLIP);
148 }
149 
150 /**
151  * intel_frontbuffer_flip - synchronous frontbuffer flip
152  * @display: display device
153  * @frontbuffer_bits: frontbuffer plane tracking bits
154  *
155  * This function gets called after scheduling a flip on @obj. This is for
156  * synchronous plane updates which will happen on the next vblank and which will
157  * not get delayed by pending gpu rendering.
158  *
159  * Can be called without any locks held.
160  */
intel_frontbuffer_flip(struct intel_display * display,unsigned frontbuffer_bits)161 void intel_frontbuffer_flip(struct intel_display *display,
162 			    unsigned frontbuffer_bits)
163 {
164 	spin_lock(&display->fb_tracking.lock);
165 	/* Remove stale busy bits due to the old buffer. */
166 	display->fb_tracking.busy_bits &= ~frontbuffer_bits;
167 	spin_unlock(&display->fb_tracking.lock);
168 
169 	frontbuffer_flush(display, frontbuffer_bits, ORIGIN_FLIP);
170 }
171 
__intel_fb_invalidate(struct intel_frontbuffer * front,enum fb_op_origin origin,unsigned int frontbuffer_bits)172 void __intel_fb_invalidate(struct intel_frontbuffer *front,
173 			   enum fb_op_origin origin,
174 			   unsigned int frontbuffer_bits)
175 {
176 	struct intel_display *display = to_intel_display(front->obj->dev);
177 
178 	if (origin == ORIGIN_CS) {
179 		spin_lock(&display->fb_tracking.lock);
180 		display->fb_tracking.busy_bits |= frontbuffer_bits;
181 		display->fb_tracking.flip_bits &= ~frontbuffer_bits;
182 		spin_unlock(&display->fb_tracking.lock);
183 	}
184 
185 	trace_intel_frontbuffer_invalidate(display, frontbuffer_bits, origin);
186 
187 	might_sleep();
188 	intel_psr_invalidate(display, frontbuffer_bits, origin);
189 	intel_drrs_invalidate(display, frontbuffer_bits);
190 	intel_fbc_invalidate(display, frontbuffer_bits, origin);
191 }
192 
__intel_fb_flush(struct intel_frontbuffer * front,enum fb_op_origin origin,unsigned int frontbuffer_bits)193 void __intel_fb_flush(struct intel_frontbuffer *front,
194 		      enum fb_op_origin origin,
195 		      unsigned int frontbuffer_bits)
196 {
197 	struct intel_display *display = to_intel_display(front->obj->dev);
198 
199 	if (origin == ORIGIN_CS) {
200 		spin_lock(&display->fb_tracking.lock);
201 		/* Filter out new bits since rendering started. */
202 		frontbuffer_bits &= display->fb_tracking.busy_bits;
203 		display->fb_tracking.busy_bits &= ~frontbuffer_bits;
204 		spin_unlock(&display->fb_tracking.lock);
205 	}
206 
207 	if (frontbuffer_bits)
208 		frontbuffer_flush(display, frontbuffer_bits, origin);
209 }
210 
intel_frontbuffer_flush_work(struct work_struct * work)211 static void intel_frontbuffer_flush_work(struct work_struct *work)
212 {
213 	struct intel_frontbuffer *front =
214 		container_of(work, struct intel_frontbuffer, flush_work);
215 
216 	intel_bo_flush_if_display(front->obj);
217 	intel_frontbuffer_flush(front, ORIGIN_DIRTYFB);
218 	intel_frontbuffer_put(front);
219 }
220 
221 /**
222  * intel_frontbuffer_queue_flush - queue flushing frontbuffer object
223  * @front: GEM object to flush
224  *
225  * This function is targeted for our dirty callback for queueing flush when
226  * dma fence is signals
227  */
intel_frontbuffer_queue_flush(struct intel_frontbuffer * front)228 void intel_frontbuffer_queue_flush(struct intel_frontbuffer *front)
229 {
230 	if (!front)
231 		return;
232 
233 	kref_get(&front->ref);
234 	if (!schedule_work(&front->flush_work))
235 		intel_frontbuffer_put(front);
236 }
237 
frontbuffer_active(struct i915_active * ref)238 static int frontbuffer_active(struct i915_active *ref)
239 {
240 	struct intel_frontbuffer *front =
241 		container_of(ref, typeof(*front), write);
242 
243 	kref_get(&front->ref);
244 	return 0;
245 }
246 
frontbuffer_retire(struct i915_active * ref)247 static void frontbuffer_retire(struct i915_active *ref)
248 {
249 	struct intel_frontbuffer *front =
250 		container_of(ref, typeof(*front), write);
251 
252 	intel_frontbuffer_flush(front, ORIGIN_CS);
253 	intel_frontbuffer_put(front);
254 }
255 
frontbuffer_release(struct kref * ref)256 static void frontbuffer_release(struct kref *ref)
257 	__releases(&to_intel_display(front->obj->dev)->fb_tracking.lock)
258 {
259 	struct intel_frontbuffer *ret, *front =
260 		container_of(ref, typeof(*front), ref);
261 	struct drm_gem_object *obj = front->obj;
262 	struct intel_display *display = to_intel_display(obj->dev);
263 
264 	drm_WARN_ON(display->drm, atomic_read(&front->bits));
265 
266 	i915_ggtt_clear_scanout(to_intel_bo(obj));
267 
268 	ret = intel_bo_set_frontbuffer(obj, NULL);
269 	drm_WARN_ON(display->drm, ret);
270 	spin_unlock(&display->fb_tracking.lock);
271 
272 	i915_active_fini(&front->write);
273 
274 	drm_gem_object_put(obj);
275 	kfree_rcu(front, rcu);
276 }
277 
278 struct intel_frontbuffer *
intel_frontbuffer_get(struct drm_gem_object * obj)279 intel_frontbuffer_get(struct drm_gem_object *obj)
280 {
281 	struct intel_display *display = to_intel_display(obj->dev);
282 	struct intel_frontbuffer *front, *cur;
283 
284 	front = intel_bo_get_frontbuffer(obj);
285 	if (front)
286 		return front;
287 
288 	front = kmalloc(sizeof(*front), GFP_KERNEL);
289 	if (!front)
290 		return NULL;
291 
292 	drm_gem_object_get(obj);
293 
294 	front->obj = obj;
295 	kref_init(&front->ref);
296 	atomic_set(&front->bits, 0);
297 	i915_active_init(&front->write,
298 			 frontbuffer_active,
299 			 frontbuffer_retire,
300 			 I915_ACTIVE_RETIRE_SLEEPS);
301 	INIT_WORK(&front->flush_work, intel_frontbuffer_flush_work);
302 
303 	spin_lock(&display->fb_tracking.lock);
304 	cur = intel_bo_set_frontbuffer(obj, front);
305 	spin_unlock(&display->fb_tracking.lock);
306 
307 	if (cur != front) {
308 		drm_gem_object_put(obj);
309 		kfree(front);
310 	}
311 
312 	return cur;
313 }
314 
intel_frontbuffer_put(struct intel_frontbuffer * front)315 void intel_frontbuffer_put(struct intel_frontbuffer *front)
316 {
317 	kref_put_lock(&front->ref,
318 		      frontbuffer_release,
319 		      &to_intel_display(front->obj->dev)->fb_tracking.lock);
320 }
321 
322 /**
323  * intel_frontbuffer_track - update frontbuffer tracking
324  * @old: current buffer for the frontbuffer slots
325  * @new: new buffer for the frontbuffer slots
326  * @frontbuffer_bits: bitmask of frontbuffer slots
327  *
328  * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
329  * from @old and setting them in @new. Both @old and @new can be NULL.
330  */
intel_frontbuffer_track(struct intel_frontbuffer * old,struct intel_frontbuffer * new,unsigned int frontbuffer_bits)331 void intel_frontbuffer_track(struct intel_frontbuffer *old,
332 			     struct intel_frontbuffer *new,
333 			     unsigned int frontbuffer_bits)
334 {
335 	/*
336 	 * Control of individual bits within the mask are guarded by
337 	 * the owning plane->mutex, i.e. we can never see concurrent
338 	 * manipulation of individual bits. But since the bitfield as a whole
339 	 * is updated using RMW, we need to use atomics in order to update
340 	 * the bits.
341 	 */
342 	BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
343 		     BITS_PER_TYPE(atomic_t));
344 	BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > 32);
345 	BUILD_BUG_ON(I915_MAX_PLANES > INTEL_FRONTBUFFER_BITS_PER_PIPE);
346 
347 	if (old) {
348 		struct intel_display *display = to_intel_display(old->obj->dev);
349 
350 		drm_WARN_ON(display->drm,
351 			    !(atomic_read(&old->bits) & frontbuffer_bits));
352 		atomic_andnot(frontbuffer_bits, &old->bits);
353 	}
354 
355 	if (new) {
356 		struct intel_display *display = to_intel_display(new->obj->dev);
357 
358 		drm_WARN_ON(display->drm,
359 			    atomic_read(&new->bits) & frontbuffer_bits);
360 		atomic_or(frontbuffer_bits, &new->bits);
361 	}
362 }
363