xref: /dflybsd-src/sys/dev/drm/i915/intel_frontbuffer.c (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
12c9916cdSFrançois Tigeot /*
22c9916cdSFrançois Tigeot  * Copyright © 2014 Intel Corporation
32c9916cdSFrançois Tigeot  *
42c9916cdSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
52c9916cdSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
62c9916cdSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
72c9916cdSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
82c9916cdSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
92c9916cdSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
102c9916cdSFrançois Tigeot  *
112c9916cdSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
122c9916cdSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
132c9916cdSFrançois Tigeot  * Software.
142c9916cdSFrançois Tigeot  *
152c9916cdSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162c9916cdSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172c9916cdSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
182c9916cdSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192c9916cdSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
202c9916cdSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
212c9916cdSFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
222c9916cdSFrançois Tigeot  *
232c9916cdSFrançois Tigeot  * Authors:
242c9916cdSFrançois Tigeot  *	Daniel Vetter <daniel.vetter@ffwll.ch>
252c9916cdSFrançois Tigeot  */
262c9916cdSFrançois Tigeot 
272c9916cdSFrançois Tigeot /**
282c9916cdSFrançois Tigeot  * DOC: frontbuffer tracking
292c9916cdSFrançois Tigeot  *
302c9916cdSFrançois Tigeot  * Many features require us to track changes to the currently active
312c9916cdSFrançois Tigeot  * frontbuffer, especially rendering targeted at the frontbuffer.
322c9916cdSFrançois Tigeot  *
332c9916cdSFrançois Tigeot  * To be able to do so GEM tracks frontbuffers using a bitmask for all possible
342c9916cdSFrançois Tigeot  * frontbuffer slots through i915_gem_track_fb(). The function in this file are
352c9916cdSFrançois Tigeot  * then called when the contents of the frontbuffer are invalidated, when
362c9916cdSFrançois Tigeot  * frontbuffer rendering has stopped again to flush out all the changes and when
372c9916cdSFrançois Tigeot  * the frontbuffer is exchanged with a flip. Subsystems interested in
382c9916cdSFrançois Tigeot  * frontbuffer changes (e.g. PSR, FBC, DRRS) should directly put their callbacks
392c9916cdSFrançois Tigeot  * into the relevant places and filter for the frontbuffer slots that they are
402c9916cdSFrançois Tigeot  * interested int.
412c9916cdSFrançois Tigeot  *
422c9916cdSFrançois Tigeot  * On a high level there are two types of powersaving features. The first one
432c9916cdSFrançois Tigeot  * work like a special cache (FBC and PSR) and are interested when they should
442c9916cdSFrançois Tigeot  * stop caching and when to restart caching. This is done by placing callbacks
452c9916cdSFrançois Tigeot  * into the invalidate and the flush functions: At invalidate the caching must
462c9916cdSFrançois Tigeot  * be stopped and at flush time it can be restarted. And maybe they need to know
472c9916cdSFrançois Tigeot  * when the frontbuffer changes (e.g. when the hw doesn't initiate an invalidate
482c9916cdSFrançois Tigeot  * and flush on its own) which can be achieved with placing callbacks into the
492c9916cdSFrançois Tigeot  * flip functions.
502c9916cdSFrançois Tigeot  *
512c9916cdSFrançois Tigeot  * The other type of display power saving feature only cares about busyness
522c9916cdSFrançois Tigeot  * (e.g. DRRS). In that case all three (invalidate, flush and flip) indicate
532c9916cdSFrançois Tigeot  * busyness. There is no direct way to detect idleness. Instead an idle timer
542c9916cdSFrançois Tigeot  * work delayed work should be started from the flush and flip functions and
552c9916cdSFrançois Tigeot  * cancelled as soon as busyness is detected.
562c9916cdSFrançois Tigeot  *
572c9916cdSFrançois Tigeot  * Note that there's also an older frontbuffer activity tracking scheme which
582c9916cdSFrançois Tigeot  * just tracks general activity. This is done by the various mark_busy and
592c9916cdSFrançois Tigeot  * mark_idle functions. For display power management features using these
602c9916cdSFrançois Tigeot  * functions is deprecated and should be avoided.
612c9916cdSFrançois Tigeot  */
622c9916cdSFrançois Tigeot 
632c9916cdSFrançois Tigeot #include <drm/drmP.h>
642c9916cdSFrançois Tigeot 
652c9916cdSFrançois Tigeot #include "intel_drv.h"
6671f41f3eSFrançois Tigeot #include "intel_frontbuffer.h"
672c9916cdSFrançois Tigeot #include "i915_drv.h"
682c9916cdSFrançois Tigeot 
__intel_fb_obj_invalidate(struct drm_i915_gem_object * obj,enum fb_op_origin origin,unsigned int frontbuffer_bits)6971f41f3eSFrançois Tigeot void __intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
7071f41f3eSFrançois Tigeot 			       enum fb_op_origin origin,
7171f41f3eSFrançois Tigeot 			       unsigned int frontbuffer_bits)
722c9916cdSFrançois Tigeot {
7371f41f3eSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
742c9916cdSFrançois Tigeot 
75a05eeebfSFrançois Tigeot 	if (origin == ORIGIN_CS) {
7671f41f3eSFrançois Tigeot 		lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE);
7771f41f3eSFrançois Tigeot 		dev_priv->fb_tracking.busy_bits |= frontbuffer_bits;
7871f41f3eSFrançois Tigeot 		dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
7971f41f3eSFrançois Tigeot 		lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE);
802c9916cdSFrançois Tigeot 	}
812c9916cdSFrançois Tigeot 
8271f41f3eSFrançois Tigeot 	intel_psr_invalidate(dev_priv, frontbuffer_bits);
8371f41f3eSFrançois Tigeot 	intel_edp_drrs_invalidate(dev_priv, frontbuffer_bits);
8471f41f3eSFrançois Tigeot 	intel_fbc_invalidate(dev_priv, frontbuffer_bits, origin);
852c9916cdSFrançois Tigeot }
862c9916cdSFrançois Tigeot 
872c9916cdSFrançois Tigeot /**
882c9916cdSFrançois Tigeot  * intel_frontbuffer_flush - flush frontbuffer
8971f41f3eSFrançois Tigeot  * @dev_priv: i915 device
902c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
91a05eeebfSFrançois Tigeot  * @origin: which operation caused the flush
922c9916cdSFrançois Tigeot  *
932c9916cdSFrançois Tigeot  * This function gets called every time rendering on the given planes has
942c9916cdSFrançois Tigeot  * completed and frontbuffer caching can be started again. Flushes will get
952c9916cdSFrançois Tigeot  * delayed if they're blocked by some outstanding asynchronous rendering.
962c9916cdSFrançois Tigeot  *
972c9916cdSFrançois Tigeot  * Can be called without any locks held.
982c9916cdSFrançois Tigeot  */
intel_frontbuffer_flush(struct drm_i915_private * dev_priv,unsigned frontbuffer_bits,enum fb_op_origin origin)9971f41f3eSFrançois Tigeot static void intel_frontbuffer_flush(struct drm_i915_private *dev_priv,
100a05eeebfSFrançois Tigeot 				    unsigned frontbuffer_bits,
101a05eeebfSFrançois Tigeot 				    enum fb_op_origin origin)
1022c9916cdSFrançois Tigeot {
1032c9916cdSFrançois Tigeot 	/* Delay flushing when rings are still busy.*/
10471f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE);
1052c9916cdSFrançois Tigeot 	frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
10671f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE);
1072c9916cdSFrançois Tigeot 
108a05eeebfSFrançois Tigeot 	if (!frontbuffer_bits)
109a05eeebfSFrançois Tigeot 		return;
1102c9916cdSFrançois Tigeot 
11171f41f3eSFrançois Tigeot 	intel_edp_drrs_flush(dev_priv, frontbuffer_bits);
11271f41f3eSFrançois Tigeot 	intel_psr_flush(dev_priv, frontbuffer_bits, origin);
113a05eeebfSFrançois Tigeot 	intel_fbc_flush(dev_priv, frontbuffer_bits, origin);
1142c9916cdSFrançois Tigeot }
1152c9916cdSFrançois Tigeot 
__intel_fb_obj_flush(struct drm_i915_gem_object * obj,enum fb_op_origin origin,unsigned int frontbuffer_bits)11671f41f3eSFrançois Tigeot void __intel_fb_obj_flush(struct drm_i915_gem_object *obj,
11771f41f3eSFrançois Tigeot 			  enum fb_op_origin origin,
11871f41f3eSFrançois Tigeot 			  unsigned int frontbuffer_bits)
1192c9916cdSFrançois Tigeot {
12071f41f3eSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
1212c9916cdSFrançois Tigeot 
122*a85cb24fSFrançois Tigeot 	if (origin == ORIGIN_CS) {
12371f41f3eSFrançois Tigeot 		lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE);
1242c9916cdSFrançois Tigeot 		/* Filter out new bits since rendering started. */
1252c9916cdSFrançois Tigeot 		frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
1262c9916cdSFrançois Tigeot 		dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
12771f41f3eSFrançois Tigeot 		lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE);
1282c9916cdSFrançois Tigeot 	}
1292c9916cdSFrançois Tigeot 
13071f41f3eSFrançois Tigeot 	if (frontbuffer_bits)
13171f41f3eSFrançois Tigeot 		intel_frontbuffer_flush(dev_priv, frontbuffer_bits, origin);
1322c9916cdSFrançois Tigeot }
1332c9916cdSFrançois Tigeot 
1342c9916cdSFrançois Tigeot /**
1352c9916cdSFrançois Tigeot  * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
13671f41f3eSFrançois Tigeot  * @dev_priv: i915 device
1372c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
1382c9916cdSFrançois Tigeot  *
1392c9916cdSFrançois Tigeot  * This function gets called after scheduling a flip on @obj. The actual
1402c9916cdSFrançois Tigeot  * frontbuffer flushing will be delayed until completion is signalled with
1412c9916cdSFrançois Tigeot  * intel_frontbuffer_flip_complete. If an invalidate happens in between this
1422c9916cdSFrançois Tigeot  * flush will be cancelled.
1432c9916cdSFrançois Tigeot  *
1442c9916cdSFrançois Tigeot  * Can be called without any locks held.
1452c9916cdSFrançois Tigeot  */
intel_frontbuffer_flip_prepare(struct drm_i915_private * dev_priv,unsigned frontbuffer_bits)14671f41f3eSFrançois Tigeot void intel_frontbuffer_flip_prepare(struct drm_i915_private *dev_priv,
1472c9916cdSFrançois Tigeot 				    unsigned frontbuffer_bits)
1482c9916cdSFrançois Tigeot {
14971f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE);
1502c9916cdSFrançois Tigeot 	dev_priv->fb_tracking.flip_bits |= frontbuffer_bits;
1512c9916cdSFrançois Tigeot 	/* Remove stale busy bits due to the old buffer. */
1522c9916cdSFrançois Tigeot 	dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
15371f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE);
15419c468b4SFrançois Tigeot 
15571f41f3eSFrançois Tigeot 	intel_psr_single_frame_update(dev_priv, frontbuffer_bits);
1562c9916cdSFrançois Tigeot }
1572c9916cdSFrançois Tigeot 
1582c9916cdSFrançois Tigeot /**
1592c9916cdSFrançois Tigeot  * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
16071f41f3eSFrançois Tigeot  * @dev_priv: i915 device
1612c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
1622c9916cdSFrançois Tigeot  *
1632c9916cdSFrançois Tigeot  * This function gets called after the flip has been latched and will complete
1642c9916cdSFrançois Tigeot  * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
1652c9916cdSFrançois Tigeot  *
1662c9916cdSFrançois Tigeot  * Can be called without any locks held.
1672c9916cdSFrançois Tigeot  */
intel_frontbuffer_flip_complete(struct drm_i915_private * dev_priv,unsigned frontbuffer_bits)16871f41f3eSFrançois Tigeot void intel_frontbuffer_flip_complete(struct drm_i915_private *dev_priv,
1692c9916cdSFrançois Tigeot 				     unsigned frontbuffer_bits)
1702c9916cdSFrançois Tigeot {
17171f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE);
1722c9916cdSFrançois Tigeot 	/* Mask any cancelled flips. */
1732c9916cdSFrançois Tigeot 	frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
1742c9916cdSFrançois Tigeot 	dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
17571f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE);
1762c9916cdSFrançois Tigeot 
17771f41f3eSFrançois Tigeot 	if (frontbuffer_bits)
17871f41f3eSFrançois Tigeot 		intel_frontbuffer_flush(dev_priv,
17971f41f3eSFrançois Tigeot 					frontbuffer_bits, ORIGIN_FLIP);
180a05eeebfSFrançois Tigeot }
181a05eeebfSFrançois Tigeot 
182a05eeebfSFrançois Tigeot /**
183a05eeebfSFrançois Tigeot  * intel_frontbuffer_flip - synchronous frontbuffer flip
18471f41f3eSFrançois Tigeot  * @dev_priv: i915 device
185a05eeebfSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
186a05eeebfSFrançois Tigeot  *
187a05eeebfSFrançois Tigeot  * This function gets called after scheduling a flip on @obj. This is for
188a05eeebfSFrançois Tigeot  * synchronous plane updates which will happen on the next vblank and which will
189a05eeebfSFrançois Tigeot  * not get delayed by pending gpu rendering.
190a05eeebfSFrançois Tigeot  *
191a05eeebfSFrançois Tigeot  * Can be called without any locks held.
192a05eeebfSFrançois Tigeot  */
intel_frontbuffer_flip(struct drm_i915_private * dev_priv,unsigned frontbuffer_bits)19371f41f3eSFrançois Tigeot void intel_frontbuffer_flip(struct drm_i915_private *dev_priv,
194a05eeebfSFrançois Tigeot 			    unsigned frontbuffer_bits)
195a05eeebfSFrançois Tigeot {
19671f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE);
197a05eeebfSFrançois Tigeot 	/* Remove stale busy bits due to the old buffer. */
198a05eeebfSFrançois Tigeot 	dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
19971f41f3eSFrançois Tigeot 	lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE);
200a05eeebfSFrançois Tigeot 
20171f41f3eSFrançois Tigeot 	intel_frontbuffer_flush(dev_priv, frontbuffer_bits, ORIGIN_FLIP);
2022c9916cdSFrançois Tigeot }
203