xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/i915/display/intel_frontbuffer.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2014 Intel Corporation
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Authors:
24*4882a593Smuzhiyun  *	Daniel Vetter <daniel.vetter@ffwll.ch>
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /**
28*4882a593Smuzhiyun  * DOC: frontbuffer tracking
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * Many features require us to track changes to the currently active
31*4882a593Smuzhiyun  * frontbuffer, especially rendering targeted at the frontbuffer.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * To be able to do so we track frontbuffers using a bitmask for all possible
34*4882a593Smuzhiyun  * frontbuffer slots through intel_frontbuffer_track(). The functions in this
35*4882a593Smuzhiyun  * file are then called when the contents of the frontbuffer are invalidated,
36*4882a593Smuzhiyun  * when frontbuffer rendering has stopped again to flush out all the changes
37*4882a593Smuzhiyun  * and when the frontbuffer is exchanged with a flip. Subsystems interested in
38*4882a593Smuzhiyun  * frontbuffer changes (e.g. PSR, FBC, DRRS) should directly put their callbacks
39*4882a593Smuzhiyun  * into the relevant places and filter for the frontbuffer slots that they are
40*4882a593Smuzhiyun  * interested int.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * On a high level there are two types of powersaving features. The first one
43*4882a593Smuzhiyun  * work like a special cache (FBC and PSR) and are interested when they should
44*4882a593Smuzhiyun  * stop caching and when to restart caching. This is done by placing callbacks
45*4882a593Smuzhiyun  * into the invalidate and the flush functions: At invalidate the caching must
46*4882a593Smuzhiyun  * be stopped and at flush time it can be restarted. And maybe they need to know
47*4882a593Smuzhiyun  * when the frontbuffer changes (e.g. when the hw doesn't initiate an invalidate
48*4882a593Smuzhiyun  * and flush on its own) which can be achieved with placing callbacks into the
49*4882a593Smuzhiyun  * flip functions.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * The other type of display power saving feature only cares about busyness
52*4882a593Smuzhiyun  * (e.g. DRRS). In that case all three (invalidate, flush and flip) indicate
53*4882a593Smuzhiyun  * busyness. There is no direct way to detect idleness. Instead an idle timer
54*4882a593Smuzhiyun  * work delayed work should be started from the flush and flip functions and
55*4882a593Smuzhiyun  * cancelled as soon as busyness is detected.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #include "display/intel_dp.h"
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #include "i915_drv.h"
61*4882a593Smuzhiyun #include "intel_display_types.h"
62*4882a593Smuzhiyun #include "intel_fbc.h"
63*4882a593Smuzhiyun #include "intel_frontbuffer.h"
64*4882a593Smuzhiyun #include "intel_psr.h"
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /**
67*4882a593Smuzhiyun  * frontbuffer_flush - flush frontbuffer
68*4882a593Smuzhiyun  * @i915: i915 device
69*4882a593Smuzhiyun  * @frontbuffer_bits: frontbuffer plane tracking bits
70*4882a593Smuzhiyun  * @origin: which operation caused the flush
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * This function gets called every time rendering on the given planes has
73*4882a593Smuzhiyun  * completed and frontbuffer caching can be started again. Flushes will get
74*4882a593Smuzhiyun  * delayed if they're blocked by some outstanding asynchronous rendering.
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * Can be called without any locks held.
77*4882a593Smuzhiyun  */
frontbuffer_flush(struct drm_i915_private * i915,unsigned int frontbuffer_bits,enum fb_op_origin origin)78*4882a593Smuzhiyun static void frontbuffer_flush(struct drm_i915_private *i915,
79*4882a593Smuzhiyun 			      unsigned int frontbuffer_bits,
80*4882a593Smuzhiyun 			      enum fb_op_origin origin)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	/* Delay flushing when rings are still busy.*/
83*4882a593Smuzhiyun 	spin_lock(&i915->fb_tracking.lock);
84*4882a593Smuzhiyun 	frontbuffer_bits &= ~i915->fb_tracking.busy_bits;
85*4882a593Smuzhiyun 	spin_unlock(&i915->fb_tracking.lock);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (!frontbuffer_bits)
88*4882a593Smuzhiyun 		return;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	might_sleep();
91*4882a593Smuzhiyun 	intel_edp_drrs_flush(i915, frontbuffer_bits);
92*4882a593Smuzhiyun 	intel_psr_flush(i915, frontbuffer_bits, origin);
93*4882a593Smuzhiyun 	intel_fbc_flush(i915, frontbuffer_bits, origin);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /**
97*4882a593Smuzhiyun  * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
98*4882a593Smuzhiyun  * @i915: i915 device
99*4882a593Smuzhiyun  * @frontbuffer_bits: frontbuffer plane tracking bits
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * This function gets called after scheduling a flip on @obj. The actual
102*4882a593Smuzhiyun  * frontbuffer flushing will be delayed until completion is signalled with
103*4882a593Smuzhiyun  * intel_frontbuffer_flip_complete. If an invalidate happens in between this
104*4882a593Smuzhiyun  * flush will be cancelled.
105*4882a593Smuzhiyun  *
106*4882a593Smuzhiyun  * Can be called without any locks held.
107*4882a593Smuzhiyun  */
intel_frontbuffer_flip_prepare(struct drm_i915_private * i915,unsigned frontbuffer_bits)108*4882a593Smuzhiyun void intel_frontbuffer_flip_prepare(struct drm_i915_private *i915,
109*4882a593Smuzhiyun 				    unsigned frontbuffer_bits)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	spin_lock(&i915->fb_tracking.lock);
112*4882a593Smuzhiyun 	i915->fb_tracking.flip_bits |= frontbuffer_bits;
113*4882a593Smuzhiyun 	/* Remove stale busy bits due to the old buffer. */
114*4882a593Smuzhiyun 	i915->fb_tracking.busy_bits &= ~frontbuffer_bits;
115*4882a593Smuzhiyun 	spin_unlock(&i915->fb_tracking.lock);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun  * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
120*4882a593Smuzhiyun  * @i915: i915 device
121*4882a593Smuzhiyun  * @frontbuffer_bits: frontbuffer plane tracking bits
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * This function gets called after the flip has been latched and will complete
124*4882a593Smuzhiyun  * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * Can be called without any locks held.
127*4882a593Smuzhiyun  */
intel_frontbuffer_flip_complete(struct drm_i915_private * i915,unsigned frontbuffer_bits)128*4882a593Smuzhiyun void intel_frontbuffer_flip_complete(struct drm_i915_private *i915,
129*4882a593Smuzhiyun 				     unsigned frontbuffer_bits)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	spin_lock(&i915->fb_tracking.lock);
132*4882a593Smuzhiyun 	/* Mask any cancelled flips. */
133*4882a593Smuzhiyun 	frontbuffer_bits &= i915->fb_tracking.flip_bits;
134*4882a593Smuzhiyun 	i915->fb_tracking.flip_bits &= ~frontbuffer_bits;
135*4882a593Smuzhiyun 	spin_unlock(&i915->fb_tracking.lock);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (frontbuffer_bits)
138*4882a593Smuzhiyun 		frontbuffer_flush(i915, frontbuffer_bits, ORIGIN_FLIP);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /**
142*4882a593Smuzhiyun  * intel_frontbuffer_flip - synchronous frontbuffer flip
143*4882a593Smuzhiyun  * @i915: i915 device
144*4882a593Smuzhiyun  * @frontbuffer_bits: frontbuffer plane tracking bits
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * This function gets called after scheduling a flip on @obj. This is for
147*4882a593Smuzhiyun  * synchronous plane updates which will happen on the next vblank and which will
148*4882a593Smuzhiyun  * not get delayed by pending gpu rendering.
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * Can be called without any locks held.
151*4882a593Smuzhiyun  */
intel_frontbuffer_flip(struct drm_i915_private * i915,unsigned frontbuffer_bits)152*4882a593Smuzhiyun void intel_frontbuffer_flip(struct drm_i915_private *i915,
153*4882a593Smuzhiyun 			    unsigned frontbuffer_bits)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	spin_lock(&i915->fb_tracking.lock);
156*4882a593Smuzhiyun 	/* Remove stale busy bits due to the old buffer. */
157*4882a593Smuzhiyun 	i915->fb_tracking.busy_bits &= ~frontbuffer_bits;
158*4882a593Smuzhiyun 	spin_unlock(&i915->fb_tracking.lock);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	frontbuffer_flush(i915, frontbuffer_bits, ORIGIN_FLIP);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
__intel_fb_invalidate(struct intel_frontbuffer * front,enum fb_op_origin origin,unsigned int frontbuffer_bits)163*4882a593Smuzhiyun void __intel_fb_invalidate(struct intel_frontbuffer *front,
164*4882a593Smuzhiyun 			   enum fb_op_origin origin,
165*4882a593Smuzhiyun 			   unsigned int frontbuffer_bits)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct drm_i915_private *i915 = to_i915(front->obj->base.dev);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (origin == ORIGIN_CS) {
170*4882a593Smuzhiyun 		spin_lock(&i915->fb_tracking.lock);
171*4882a593Smuzhiyun 		i915->fb_tracking.busy_bits |= frontbuffer_bits;
172*4882a593Smuzhiyun 		i915->fb_tracking.flip_bits &= ~frontbuffer_bits;
173*4882a593Smuzhiyun 		spin_unlock(&i915->fb_tracking.lock);
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	might_sleep();
177*4882a593Smuzhiyun 	intel_psr_invalidate(i915, frontbuffer_bits, origin);
178*4882a593Smuzhiyun 	intel_edp_drrs_invalidate(i915, frontbuffer_bits);
179*4882a593Smuzhiyun 	intel_fbc_invalidate(i915, frontbuffer_bits, origin);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
__intel_fb_flush(struct intel_frontbuffer * front,enum fb_op_origin origin,unsigned int frontbuffer_bits)182*4882a593Smuzhiyun void __intel_fb_flush(struct intel_frontbuffer *front,
183*4882a593Smuzhiyun 		      enum fb_op_origin origin,
184*4882a593Smuzhiyun 		      unsigned int frontbuffer_bits)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct drm_i915_private *i915 = to_i915(front->obj->base.dev);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (origin == ORIGIN_CS) {
189*4882a593Smuzhiyun 		spin_lock(&i915->fb_tracking.lock);
190*4882a593Smuzhiyun 		/* Filter out new bits since rendering started. */
191*4882a593Smuzhiyun 		frontbuffer_bits &= i915->fb_tracking.busy_bits;
192*4882a593Smuzhiyun 		i915->fb_tracking.busy_bits &= ~frontbuffer_bits;
193*4882a593Smuzhiyun 		spin_unlock(&i915->fb_tracking.lock);
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	if (frontbuffer_bits)
197*4882a593Smuzhiyun 		frontbuffer_flush(i915, frontbuffer_bits, origin);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
frontbuffer_active(struct i915_active * ref)200*4882a593Smuzhiyun static int frontbuffer_active(struct i915_active *ref)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct intel_frontbuffer *front =
203*4882a593Smuzhiyun 		container_of(ref, typeof(*front), write);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	kref_get(&front->ref);
206*4882a593Smuzhiyun 	return 0;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun __i915_active_call
frontbuffer_retire(struct i915_active * ref)210*4882a593Smuzhiyun static void frontbuffer_retire(struct i915_active *ref)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct intel_frontbuffer *front =
213*4882a593Smuzhiyun 		container_of(ref, typeof(*front), write);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	intel_frontbuffer_flush(front, ORIGIN_CS);
216*4882a593Smuzhiyun 	intel_frontbuffer_put(front);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun 
frontbuffer_release(struct kref * ref)219*4882a593Smuzhiyun static void frontbuffer_release(struct kref *ref)
220*4882a593Smuzhiyun 	__releases(&to_i915(front->obj->base.dev)->fb_tracking.lock)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	struct intel_frontbuffer *front =
223*4882a593Smuzhiyun 		container_of(ref, typeof(*front), ref);
224*4882a593Smuzhiyun 	struct drm_i915_gem_object *obj = front->obj;
225*4882a593Smuzhiyun 	struct i915_vma *vma;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	spin_lock(&obj->vma.lock);
228*4882a593Smuzhiyun 	for_each_ggtt_vma(vma, obj)
229*4882a593Smuzhiyun 		vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
230*4882a593Smuzhiyun 	spin_unlock(&obj->vma.lock);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	RCU_INIT_POINTER(obj->frontbuffer, NULL);
233*4882a593Smuzhiyun 	spin_unlock(&to_i915(obj->base.dev)->fb_tracking.lock);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	i915_active_fini(&front->write);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	i915_gem_object_put(obj);
238*4882a593Smuzhiyun 	kfree_rcu(front, rcu);
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun struct intel_frontbuffer *
intel_frontbuffer_get(struct drm_i915_gem_object * obj)242*4882a593Smuzhiyun intel_frontbuffer_get(struct drm_i915_gem_object *obj)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
245*4882a593Smuzhiyun 	struct intel_frontbuffer *front;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	front = __intel_frontbuffer_get(obj);
248*4882a593Smuzhiyun 	if (front)
249*4882a593Smuzhiyun 		return front;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	front = kmalloc(sizeof(*front), GFP_KERNEL);
252*4882a593Smuzhiyun 	if (!front)
253*4882a593Smuzhiyun 		return NULL;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	front->obj = obj;
256*4882a593Smuzhiyun 	kref_init(&front->ref);
257*4882a593Smuzhiyun 	atomic_set(&front->bits, 0);
258*4882a593Smuzhiyun 	i915_active_init(&front->write,
259*4882a593Smuzhiyun 			 frontbuffer_active,
260*4882a593Smuzhiyun 			 i915_active_may_sleep(frontbuffer_retire));
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	spin_lock(&i915->fb_tracking.lock);
263*4882a593Smuzhiyun 	if (rcu_access_pointer(obj->frontbuffer)) {
264*4882a593Smuzhiyun 		kfree(front);
265*4882a593Smuzhiyun 		front = rcu_dereference_protected(obj->frontbuffer, true);
266*4882a593Smuzhiyun 		kref_get(&front->ref);
267*4882a593Smuzhiyun 	} else {
268*4882a593Smuzhiyun 		i915_gem_object_get(obj);
269*4882a593Smuzhiyun 		rcu_assign_pointer(obj->frontbuffer, front);
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 	spin_unlock(&i915->fb_tracking.lock);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return front;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
intel_frontbuffer_put(struct intel_frontbuffer * front)276*4882a593Smuzhiyun void intel_frontbuffer_put(struct intel_frontbuffer *front)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	kref_put_lock(&front->ref,
279*4882a593Smuzhiyun 		      frontbuffer_release,
280*4882a593Smuzhiyun 		      &to_i915(front->obj->base.dev)->fb_tracking.lock);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /**
284*4882a593Smuzhiyun  * intel_frontbuffer_track - update frontbuffer tracking
285*4882a593Smuzhiyun  * @old: current buffer for the frontbuffer slots
286*4882a593Smuzhiyun  * @new: new buffer for the frontbuffer slots
287*4882a593Smuzhiyun  * @frontbuffer_bits: bitmask of frontbuffer slots
288*4882a593Smuzhiyun  *
289*4882a593Smuzhiyun  * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
290*4882a593Smuzhiyun  * from @old and setting them in @new. Both @old and @new can be NULL.
291*4882a593Smuzhiyun  */
intel_frontbuffer_track(struct intel_frontbuffer * old,struct intel_frontbuffer * new,unsigned int frontbuffer_bits)292*4882a593Smuzhiyun void intel_frontbuffer_track(struct intel_frontbuffer *old,
293*4882a593Smuzhiyun 			     struct intel_frontbuffer *new,
294*4882a593Smuzhiyun 			     unsigned int frontbuffer_bits)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	/*
297*4882a593Smuzhiyun 	 * Control of individual bits within the mask are guarded by
298*4882a593Smuzhiyun 	 * the owning plane->mutex, i.e. we can never see concurrent
299*4882a593Smuzhiyun 	 * manipulation of individual bits. But since the bitfield as a whole
300*4882a593Smuzhiyun 	 * is updated using RMW, we need to use atomics in order to update
301*4882a593Smuzhiyun 	 * the bits.
302*4882a593Smuzhiyun 	 */
303*4882a593Smuzhiyun 	BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
304*4882a593Smuzhiyun 		     BITS_PER_TYPE(atomic_t));
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if (old) {
307*4882a593Smuzhiyun 		drm_WARN_ON(old->obj->base.dev,
308*4882a593Smuzhiyun 			    !(atomic_read(&old->bits) & frontbuffer_bits));
309*4882a593Smuzhiyun 		atomic_andnot(frontbuffer_bits, &old->bits);
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	if (new) {
313*4882a593Smuzhiyun 		drm_WARN_ON(new->obj->base.dev,
314*4882a593Smuzhiyun 			    atomic_read(&new->bits) & frontbuffer_bits);
315*4882a593Smuzhiyun 		atomic_or(frontbuffer_bits, &new->bits);
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun }
318