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" 662c9916cdSFrançois Tigeot #include "i915_drv.h" 672c9916cdSFrançois Tigeot 682c9916cdSFrançois Tigeot /** 692c9916cdSFrançois Tigeot * intel_fb_obj_invalidate - invalidate frontbuffer object 702c9916cdSFrançois Tigeot * @obj: GEM object to invalidate 71477eb7f9SFrançois Tigeot * @origin: which operation caused the invalidation 722c9916cdSFrançois Tigeot * 732c9916cdSFrançois Tigeot * This function gets called every time rendering on the given object starts and 742c9916cdSFrançois Tigeot * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must 75*a05eeebfSFrançois Tigeot * be invalidated. For ORIGIN_CS any subsequent invalidation will be delayed 762c9916cdSFrançois Tigeot * until the rendering completes or a flip on this frontbuffer plane is 772c9916cdSFrançois Tigeot * scheduled. 782c9916cdSFrançois Tigeot */ 792c9916cdSFrançois Tigeot void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj, 80477eb7f9SFrançois Tigeot enum fb_op_origin origin) 812c9916cdSFrançois Tigeot { 822c9916cdSFrançois Tigeot struct drm_device *dev = obj->base.dev; 83*a05eeebfSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 842c9916cdSFrançois Tigeot 852c9916cdSFrançois Tigeot WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 862c9916cdSFrançois Tigeot 872c9916cdSFrançois Tigeot if (!obj->frontbuffer_bits) 882c9916cdSFrançois Tigeot return; 892c9916cdSFrançois Tigeot 90*a05eeebfSFrançois Tigeot if (origin == ORIGIN_CS) { 912c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 922c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits 932c9916cdSFrançois Tigeot |= obj->frontbuffer_bits; 942c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits 952c9916cdSFrançois Tigeot &= ~obj->frontbuffer_bits; 962c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 972c9916cdSFrançois Tigeot } 982c9916cdSFrançois Tigeot 992c9916cdSFrançois Tigeot intel_psr_invalidate(dev, obj->frontbuffer_bits); 1002c9916cdSFrançois Tigeot intel_edp_drrs_invalidate(dev, obj->frontbuffer_bits); 101477eb7f9SFrançois Tigeot intel_fbc_invalidate(dev_priv, obj->frontbuffer_bits, origin); 1022c9916cdSFrançois Tigeot } 1032c9916cdSFrançois Tigeot 1042c9916cdSFrançois Tigeot /** 1052c9916cdSFrançois Tigeot * intel_frontbuffer_flush - flush frontbuffer 1062c9916cdSFrançois Tigeot * @dev: DRM device 1072c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 108*a05eeebfSFrançois Tigeot * @origin: which operation caused the flush 1092c9916cdSFrançois Tigeot * 1102c9916cdSFrançois Tigeot * This function gets called every time rendering on the given planes has 1112c9916cdSFrançois Tigeot * completed and frontbuffer caching can be started again. Flushes will get 1122c9916cdSFrançois Tigeot * delayed if they're blocked by some outstanding asynchronous rendering. 1132c9916cdSFrançois Tigeot * 1142c9916cdSFrançois Tigeot * Can be called without any locks held. 1152c9916cdSFrançois Tigeot */ 116*a05eeebfSFrançois Tigeot static void intel_frontbuffer_flush(struct drm_device *dev, 117*a05eeebfSFrançois Tigeot unsigned frontbuffer_bits, 118*a05eeebfSFrançois Tigeot enum fb_op_origin origin) 1192c9916cdSFrançois Tigeot { 120*a05eeebfSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 1212c9916cdSFrançois Tigeot 1222c9916cdSFrançois Tigeot /* Delay flushing when rings are still busy.*/ 1232c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 1242c9916cdSFrançois Tigeot frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits; 1252c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 1262c9916cdSFrançois Tigeot 127*a05eeebfSFrançois Tigeot if (!frontbuffer_bits) 128*a05eeebfSFrançois Tigeot return; 1292c9916cdSFrançois Tigeot 1302c9916cdSFrançois Tigeot intel_edp_drrs_flush(dev, frontbuffer_bits); 131*a05eeebfSFrançois Tigeot intel_psr_flush(dev, frontbuffer_bits, origin); 132*a05eeebfSFrançois Tigeot intel_fbc_flush(dev_priv, frontbuffer_bits, origin); 1332c9916cdSFrançois Tigeot } 1342c9916cdSFrançois Tigeot 1352c9916cdSFrançois Tigeot /** 1362c9916cdSFrançois Tigeot * intel_fb_obj_flush - flush frontbuffer object 1372c9916cdSFrançois Tigeot * @obj: GEM object to flush 1382c9916cdSFrançois Tigeot * @retire: set when retiring asynchronous rendering 139*a05eeebfSFrançois Tigeot * @origin: which operation caused the flush 1402c9916cdSFrançois Tigeot * 1412c9916cdSFrançois Tigeot * This function gets called every time rendering on the given object has 1422c9916cdSFrançois Tigeot * completed and frontbuffer caching can be started again. If @retire is true 1432c9916cdSFrançois Tigeot * then any delayed flushes will be unblocked. 1442c9916cdSFrançois Tigeot */ 1452c9916cdSFrançois Tigeot void intel_fb_obj_flush(struct drm_i915_gem_object *obj, 146*a05eeebfSFrançois Tigeot bool retire, enum fb_op_origin origin) 1472c9916cdSFrançois Tigeot { 1482c9916cdSFrançois Tigeot struct drm_device *dev = obj->base.dev; 149*a05eeebfSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 1502c9916cdSFrançois Tigeot unsigned frontbuffer_bits; 1512c9916cdSFrançois Tigeot 1522c9916cdSFrançois Tigeot WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 1532c9916cdSFrançois Tigeot 1542c9916cdSFrançois Tigeot if (!obj->frontbuffer_bits) 1552c9916cdSFrançois Tigeot return; 1562c9916cdSFrançois Tigeot 1572c9916cdSFrançois Tigeot frontbuffer_bits = obj->frontbuffer_bits; 1582c9916cdSFrançois Tigeot 1592c9916cdSFrançois Tigeot if (retire) { 1602c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 1612c9916cdSFrançois Tigeot /* Filter out new bits since rendering started. */ 1622c9916cdSFrançois Tigeot frontbuffer_bits &= dev_priv->fb_tracking.busy_bits; 1632c9916cdSFrançois Tigeot 1642c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 1652c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 1662c9916cdSFrançois Tigeot } 1672c9916cdSFrançois Tigeot 168*a05eeebfSFrançois Tigeot intel_frontbuffer_flush(dev, frontbuffer_bits, origin); 1692c9916cdSFrançois Tigeot } 1702c9916cdSFrançois Tigeot 1712c9916cdSFrançois Tigeot /** 1722c9916cdSFrançois Tigeot * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip 1732c9916cdSFrançois Tigeot * @dev: DRM device 1742c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 1752c9916cdSFrançois Tigeot * 1762c9916cdSFrançois Tigeot * This function gets called after scheduling a flip on @obj. The actual 1772c9916cdSFrançois Tigeot * frontbuffer flushing will be delayed until completion is signalled with 1782c9916cdSFrançois Tigeot * intel_frontbuffer_flip_complete. If an invalidate happens in between this 1792c9916cdSFrançois Tigeot * flush will be cancelled. 1802c9916cdSFrançois Tigeot * 1812c9916cdSFrançois Tigeot * Can be called without any locks held. 1822c9916cdSFrançois Tigeot */ 1832c9916cdSFrançois Tigeot void intel_frontbuffer_flip_prepare(struct drm_device *dev, 1842c9916cdSFrançois Tigeot unsigned frontbuffer_bits) 1852c9916cdSFrançois Tigeot { 186*a05eeebfSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 1872c9916cdSFrançois Tigeot 1882c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 1892c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits |= frontbuffer_bits; 1902c9916cdSFrançois Tigeot /* Remove stale busy bits due to the old buffer. */ 1912c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 1922c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 19319c468b4SFrançois Tigeot 194*a05eeebfSFrançois Tigeot intel_psr_single_frame_update(dev, frontbuffer_bits); 1952c9916cdSFrançois Tigeot } 1962c9916cdSFrançois Tigeot 1972c9916cdSFrançois Tigeot /** 1982c9916cdSFrançois Tigeot * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip 1992c9916cdSFrançois Tigeot * @dev: DRM device 2002c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 2012c9916cdSFrançois Tigeot * 2022c9916cdSFrançois Tigeot * This function gets called after the flip has been latched and will complete 2032c9916cdSFrançois Tigeot * on the next vblank. It will execute the flush if it hasn't been cancelled yet. 2042c9916cdSFrançois Tigeot * 2052c9916cdSFrançois Tigeot * Can be called without any locks held. 2062c9916cdSFrançois Tigeot */ 2072c9916cdSFrançois Tigeot void intel_frontbuffer_flip_complete(struct drm_device *dev, 2082c9916cdSFrançois Tigeot unsigned frontbuffer_bits) 2092c9916cdSFrançois Tigeot { 210*a05eeebfSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 2112c9916cdSFrançois Tigeot 2122c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 2132c9916cdSFrançois Tigeot /* Mask any cancelled flips. */ 2142c9916cdSFrançois Tigeot frontbuffer_bits &= dev_priv->fb_tracking.flip_bits; 2152c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits; 2162c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 2172c9916cdSFrançois Tigeot 218*a05eeebfSFrançois Tigeot intel_frontbuffer_flush(dev, frontbuffer_bits, ORIGIN_FLIP); 219*a05eeebfSFrançois Tigeot } 220*a05eeebfSFrançois Tigeot 221*a05eeebfSFrançois Tigeot /** 222*a05eeebfSFrançois Tigeot * intel_frontbuffer_flip - synchronous frontbuffer flip 223*a05eeebfSFrançois Tigeot * @dev: DRM device 224*a05eeebfSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 225*a05eeebfSFrançois Tigeot * 226*a05eeebfSFrançois Tigeot * This function gets called after scheduling a flip on @obj. This is for 227*a05eeebfSFrançois Tigeot * synchronous plane updates which will happen on the next vblank and which will 228*a05eeebfSFrançois Tigeot * not get delayed by pending gpu rendering. 229*a05eeebfSFrançois Tigeot * 230*a05eeebfSFrançois Tigeot * Can be called without any locks held. 231*a05eeebfSFrançois Tigeot */ 232*a05eeebfSFrançois Tigeot void intel_frontbuffer_flip(struct drm_device *dev, 233*a05eeebfSFrançois Tigeot unsigned frontbuffer_bits) 234*a05eeebfSFrançois Tigeot { 235*a05eeebfSFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 236*a05eeebfSFrançois Tigeot 237*a05eeebfSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 238*a05eeebfSFrançois Tigeot /* Remove stale busy bits due to the old buffer. */ 239*a05eeebfSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 240*a05eeebfSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 241*a05eeebfSFrançois Tigeot 242*a05eeebfSFrançois Tigeot intel_frontbuffer_flush(dev, frontbuffer_bits, ORIGIN_FLIP); 2432c9916cdSFrançois Tigeot } 244