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