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" 66*71f41f3eSFrançois Tigeot #include "intel_frontbuffer.h" 672c9916cdSFrançois Tigeot #include "i915_drv.h" 682c9916cdSFrançois Tigeot 69*71f41f3eSFrançois Tigeot void __intel_fb_obj_invalidate(struct drm_i915_gem_object *obj, 70*71f41f3eSFrançois Tigeot enum fb_op_origin origin, 71*71f41f3eSFrançois Tigeot unsigned int frontbuffer_bits) 722c9916cdSFrançois Tigeot { 73*71f41f3eSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 742c9916cdSFrançois Tigeot 75a05eeebfSFrançois Tigeot if (origin == ORIGIN_CS) { 76*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE); 77*71f41f3eSFrançois Tigeot dev_priv->fb_tracking.busy_bits |= frontbuffer_bits; 78*71f41f3eSFrançois Tigeot dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits; 79*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE); 802c9916cdSFrançois Tigeot } 812c9916cdSFrançois Tigeot 82*71f41f3eSFrançois Tigeot intel_psr_invalidate(dev_priv, frontbuffer_bits); 83*71f41f3eSFrançois Tigeot intel_edp_drrs_invalidate(dev_priv, frontbuffer_bits); 84*71f41f3eSFranç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 89*71f41f3eSFranç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 */ 99*71f41f3eSFranç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.*/ 104*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE); 1052c9916cdSFrançois Tigeot frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits; 106*71f41f3eSFranç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 111*71f41f3eSFrançois Tigeot intel_edp_drrs_flush(dev_priv, frontbuffer_bits); 112*71f41f3eSFranç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 116*71f41f3eSFrançois Tigeot void __intel_fb_obj_flush(struct drm_i915_gem_object *obj, 117*71f41f3eSFrançois Tigeot bool retire, 118*71f41f3eSFrançois Tigeot enum fb_op_origin origin, 119*71f41f3eSFrançois Tigeot unsigned int frontbuffer_bits) 1202c9916cdSFrançois Tigeot { 121*71f41f3eSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 1222c9916cdSFrançois Tigeot 1232c9916cdSFrançois Tigeot if (retire) { 124*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE); 1252c9916cdSFrançois Tigeot /* Filter out new bits since rendering started. */ 1262c9916cdSFrançois Tigeot frontbuffer_bits &= dev_priv->fb_tracking.busy_bits; 1272c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 128*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE); 1292c9916cdSFrançois Tigeot } 1302c9916cdSFrançois Tigeot 131*71f41f3eSFrançois Tigeot if (frontbuffer_bits) 132*71f41f3eSFrançois Tigeot intel_frontbuffer_flush(dev_priv, frontbuffer_bits, origin); 1332c9916cdSFrançois Tigeot } 1342c9916cdSFrançois Tigeot 1352c9916cdSFrançois Tigeot /** 1362c9916cdSFrançois Tigeot * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip 137*71f41f3eSFrançois Tigeot * @dev_priv: i915 device 1382c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 1392c9916cdSFrançois Tigeot * 1402c9916cdSFrançois Tigeot * This function gets called after scheduling a flip on @obj. The actual 1412c9916cdSFrançois Tigeot * frontbuffer flushing will be delayed until completion is signalled with 1422c9916cdSFrançois Tigeot * intel_frontbuffer_flip_complete. If an invalidate happens in between this 1432c9916cdSFrançois Tigeot * flush will be cancelled. 1442c9916cdSFrançois Tigeot * 1452c9916cdSFrançois Tigeot * Can be called without any locks held. 1462c9916cdSFrançois Tigeot */ 147*71f41f3eSFrançois Tigeot void intel_frontbuffer_flip_prepare(struct drm_i915_private *dev_priv, 1482c9916cdSFrançois Tigeot unsigned frontbuffer_bits) 1492c9916cdSFrançois Tigeot { 150*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE); 1512c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits |= frontbuffer_bits; 1522c9916cdSFrançois Tigeot /* Remove stale busy bits due to the old buffer. */ 1532c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 154*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE); 15519c468b4SFrançois Tigeot 156*71f41f3eSFrançois Tigeot intel_psr_single_frame_update(dev_priv, frontbuffer_bits); 1572c9916cdSFrançois Tigeot } 1582c9916cdSFrançois Tigeot 1592c9916cdSFrançois Tigeot /** 1602c9916cdSFrançois Tigeot * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip 161*71f41f3eSFrançois Tigeot * @dev_priv: i915 device 1622c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 1632c9916cdSFrançois Tigeot * 1642c9916cdSFrançois Tigeot * This function gets called after the flip has been latched and will complete 1652c9916cdSFrançois Tigeot * on the next vblank. It will execute the flush if it hasn't been cancelled yet. 1662c9916cdSFrançois Tigeot * 1672c9916cdSFrançois Tigeot * Can be called without any locks held. 1682c9916cdSFrançois Tigeot */ 169*71f41f3eSFrançois Tigeot void intel_frontbuffer_flip_complete(struct drm_i915_private *dev_priv, 1702c9916cdSFrançois Tigeot unsigned frontbuffer_bits) 1712c9916cdSFrançois Tigeot { 172*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE); 1732c9916cdSFrançois Tigeot /* Mask any cancelled flips. */ 1742c9916cdSFrançois Tigeot frontbuffer_bits &= dev_priv->fb_tracking.flip_bits; 1752c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits; 176*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE); 1772c9916cdSFrançois Tigeot 178*71f41f3eSFrançois Tigeot if (frontbuffer_bits) 179*71f41f3eSFrançois Tigeot intel_frontbuffer_flush(dev_priv, 180*71f41f3eSFrançois Tigeot frontbuffer_bits, ORIGIN_FLIP); 181a05eeebfSFrançois Tigeot } 182a05eeebfSFrançois Tigeot 183a05eeebfSFrançois Tigeot /** 184a05eeebfSFrançois Tigeot * intel_frontbuffer_flip - synchronous frontbuffer flip 185*71f41f3eSFrançois Tigeot * @dev_priv: i915 device 186a05eeebfSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 187a05eeebfSFrançois Tigeot * 188a05eeebfSFrançois Tigeot * This function gets called after scheduling a flip on @obj. This is for 189a05eeebfSFrançois Tigeot * synchronous plane updates which will happen on the next vblank and which will 190a05eeebfSFrançois Tigeot * not get delayed by pending gpu rendering. 191a05eeebfSFrançois Tigeot * 192a05eeebfSFrançois Tigeot * Can be called without any locks held. 193a05eeebfSFrançois Tigeot */ 194*71f41f3eSFrançois Tigeot void intel_frontbuffer_flip(struct drm_i915_private *dev_priv, 195a05eeebfSFrançois Tigeot unsigned frontbuffer_bits) 196a05eeebfSFrançois Tigeot { 197*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_EXCLUSIVE); 198a05eeebfSFrançois Tigeot /* Remove stale busy bits due to the old buffer. */ 199a05eeebfSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 200*71f41f3eSFrançois Tigeot lockmgr(&dev_priv->fb_tracking.lock, LK_RELEASE); 201a05eeebfSFrançois Tigeot 202*71f41f3eSFrançois Tigeot intel_frontbuffer_flush(dev_priv, frontbuffer_bits, ORIGIN_FLIP); 2032c9916cdSFrançois Tigeot } 204