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 static void intel_increase_pllclock(struct drm_device *dev, 692c9916cdSFrançois Tigeot enum i915_pipe pipe) 702c9916cdSFrançois Tigeot { 712c9916cdSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 722c9916cdSFrançois Tigeot int dpll_reg = DPLL(pipe); 732c9916cdSFrançois Tigeot int dpll; 742c9916cdSFrançois Tigeot 752c9916cdSFrançois Tigeot if (!HAS_GMCH_DISPLAY(dev)) 762c9916cdSFrançois Tigeot return; 772c9916cdSFrançois Tigeot 782c9916cdSFrançois Tigeot if (!dev_priv->lvds_downclock_avail) 792c9916cdSFrançois Tigeot return; 802c9916cdSFrançois Tigeot 812c9916cdSFrançois Tigeot dpll = I915_READ(dpll_reg); 822c9916cdSFrançois Tigeot if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) { 832c9916cdSFrançois Tigeot DRM_DEBUG_DRIVER("upclocking LVDS\n"); 842c9916cdSFrançois Tigeot 852c9916cdSFrançois Tigeot assert_panel_unlocked(dev_priv, pipe); 862c9916cdSFrançois Tigeot 872c9916cdSFrançois Tigeot dpll &= ~DISPLAY_RATE_SELECT_FPA1; 882c9916cdSFrançois Tigeot I915_WRITE(dpll_reg, dpll); 892c9916cdSFrançois Tigeot intel_wait_for_vblank(dev, pipe); 902c9916cdSFrançois Tigeot 912c9916cdSFrançois Tigeot dpll = I915_READ(dpll_reg); 922c9916cdSFrançois Tigeot if (dpll & DISPLAY_RATE_SELECT_FPA1) 932c9916cdSFrançois Tigeot DRM_DEBUG_DRIVER("failed to upclock LVDS!\n"); 942c9916cdSFrançois Tigeot } 952c9916cdSFrançois Tigeot } 962c9916cdSFrançois Tigeot 972c9916cdSFrançois Tigeot /** 982c9916cdSFrançois Tigeot * intel_mark_fb_busy - mark given planes as busy 992c9916cdSFrançois Tigeot * @dev: DRM device 1002c9916cdSFrançois Tigeot * @frontbuffer_bits: bits for the affected planes 1012c9916cdSFrançois Tigeot * @ring: optional ring for asynchronous commands 1022c9916cdSFrançois Tigeot * 1032c9916cdSFrançois Tigeot * This function gets called every time the screen contents change. It can be 1042c9916cdSFrançois Tigeot * used to keep e.g. the update rate at the nominal refresh rate with DRRS. 1052c9916cdSFrançois Tigeot */ 1062c9916cdSFrançois Tigeot static void intel_mark_fb_busy(struct drm_device *dev, 1072c9916cdSFrançois Tigeot unsigned frontbuffer_bits, 1082c9916cdSFrançois Tigeot struct intel_engine_cs *ring) 1092c9916cdSFrançois Tigeot { 1102c9916cdSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1112c9916cdSFrançois Tigeot enum i915_pipe pipe; 1122c9916cdSFrançois Tigeot 1132c9916cdSFrançois Tigeot for_each_pipe(dev_priv, pipe) { 1142c9916cdSFrançois Tigeot if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe))) 1152c9916cdSFrançois Tigeot continue; 1162c9916cdSFrançois Tigeot 1172c9916cdSFrançois Tigeot intel_increase_pllclock(dev, pipe); 1182c9916cdSFrançois Tigeot } 1192c9916cdSFrançois Tigeot } 1202c9916cdSFrançois Tigeot 1212c9916cdSFrançois Tigeot /** 1222c9916cdSFrançois Tigeot * intel_fb_obj_invalidate - invalidate frontbuffer object 1232c9916cdSFrançois Tigeot * @obj: GEM object to invalidate 1242c9916cdSFrançois Tigeot * @ring: set for asynchronous rendering 125477eb7f9SFrançois Tigeot * @origin: which operation caused the invalidation 1262c9916cdSFrançois Tigeot * 1272c9916cdSFrançois Tigeot * This function gets called every time rendering on the given object starts and 1282c9916cdSFrançois Tigeot * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must 1292c9916cdSFrançois Tigeot * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed 1302c9916cdSFrançois Tigeot * until the rendering completes or a flip on this frontbuffer plane is 1312c9916cdSFrançois Tigeot * scheduled. 1322c9916cdSFrançois Tigeot */ 1332c9916cdSFrançois Tigeot void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj, 134477eb7f9SFrançois Tigeot struct intel_engine_cs *ring, 135477eb7f9SFrançois Tigeot enum fb_op_origin origin) 1362c9916cdSFrançois Tigeot { 1372c9916cdSFrançois Tigeot struct drm_device *dev = obj->base.dev; 1382c9916cdSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1392c9916cdSFrançois Tigeot 1402c9916cdSFrançois Tigeot WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 1412c9916cdSFrançois Tigeot 1422c9916cdSFrançois Tigeot if (!obj->frontbuffer_bits) 1432c9916cdSFrançois Tigeot return; 1442c9916cdSFrançois Tigeot 1452c9916cdSFrançois Tigeot if (ring) { 1462c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 1472c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits 1482c9916cdSFrançois Tigeot |= obj->frontbuffer_bits; 1492c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits 1502c9916cdSFrançois Tigeot &= ~obj->frontbuffer_bits; 1512c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 1522c9916cdSFrançois Tigeot } 1532c9916cdSFrançois Tigeot 1542c9916cdSFrançois Tigeot intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring); 1552c9916cdSFrançois Tigeot 1562c9916cdSFrançois Tigeot intel_psr_invalidate(dev, obj->frontbuffer_bits); 1572c9916cdSFrançois Tigeot intel_edp_drrs_invalidate(dev, obj->frontbuffer_bits); 158477eb7f9SFrançois Tigeot intel_fbc_invalidate(dev_priv, obj->frontbuffer_bits, origin); 1592c9916cdSFrançois Tigeot } 1602c9916cdSFrançois Tigeot 1612c9916cdSFrançois Tigeot /** 1622c9916cdSFrançois Tigeot * intel_frontbuffer_flush - flush frontbuffer 1632c9916cdSFrançois Tigeot * @dev: DRM device 1642c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 1652c9916cdSFrançois Tigeot * 1662c9916cdSFrançois Tigeot * This function gets called every time rendering on the given planes has 1672c9916cdSFrançois Tigeot * completed and frontbuffer caching can be started again. Flushes will get 1682c9916cdSFrançois Tigeot * delayed if they're blocked by some outstanding asynchronous rendering. 1692c9916cdSFrançois Tigeot * 1702c9916cdSFrançois Tigeot * Can be called without any locks held. 1712c9916cdSFrançois Tigeot */ 1722c9916cdSFrançois Tigeot void intel_frontbuffer_flush(struct drm_device *dev, 1732c9916cdSFrançois Tigeot unsigned frontbuffer_bits) 1742c9916cdSFrançois Tigeot { 1752c9916cdSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1762c9916cdSFrançois Tigeot 1772c9916cdSFrançois Tigeot /* Delay flushing when rings are still busy.*/ 1782c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 1792c9916cdSFrançois Tigeot frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits; 1802c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 1812c9916cdSFrançois Tigeot 1822c9916cdSFrançois Tigeot intel_mark_fb_busy(dev, frontbuffer_bits, NULL); 1832c9916cdSFrançois Tigeot 1842c9916cdSFrançois Tigeot intel_edp_drrs_flush(dev, frontbuffer_bits); 1852c9916cdSFrançois Tigeot intel_psr_flush(dev, frontbuffer_bits); 186477eb7f9SFrançois Tigeot intel_fbc_flush(dev_priv, frontbuffer_bits); 1872c9916cdSFrançois Tigeot } 1882c9916cdSFrançois Tigeot 1892c9916cdSFrançois Tigeot /** 1902c9916cdSFrançois Tigeot * intel_fb_obj_flush - flush frontbuffer object 1912c9916cdSFrançois Tigeot * @obj: GEM object to flush 1922c9916cdSFrançois Tigeot * @retire: set when retiring asynchronous rendering 1932c9916cdSFrançois Tigeot * 1942c9916cdSFrançois Tigeot * This function gets called every time rendering on the given object has 1952c9916cdSFrançois Tigeot * completed and frontbuffer caching can be started again. If @retire is true 1962c9916cdSFrançois Tigeot * then any delayed flushes will be unblocked. 1972c9916cdSFrançois Tigeot */ 1982c9916cdSFrançois Tigeot void intel_fb_obj_flush(struct drm_i915_gem_object *obj, 1992c9916cdSFrançois Tigeot bool retire) 2002c9916cdSFrançois Tigeot { 2012c9916cdSFrançois Tigeot struct drm_device *dev = obj->base.dev; 2022c9916cdSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2032c9916cdSFrançois Tigeot unsigned frontbuffer_bits; 2042c9916cdSFrançois Tigeot 2052c9916cdSFrançois Tigeot WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 2062c9916cdSFrançois Tigeot 2072c9916cdSFrançois Tigeot if (!obj->frontbuffer_bits) 2082c9916cdSFrançois Tigeot return; 2092c9916cdSFrançois Tigeot 2102c9916cdSFrançois Tigeot frontbuffer_bits = obj->frontbuffer_bits; 2112c9916cdSFrançois Tigeot 2122c9916cdSFrançois Tigeot if (retire) { 2132c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 2142c9916cdSFrançois Tigeot /* Filter out new bits since rendering started. */ 2152c9916cdSFrançois Tigeot frontbuffer_bits &= dev_priv->fb_tracking.busy_bits; 2162c9916cdSFrançois Tigeot 2172c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 2182c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 2192c9916cdSFrançois Tigeot } 2202c9916cdSFrançois Tigeot 2212c9916cdSFrançois Tigeot intel_frontbuffer_flush(dev, frontbuffer_bits); 2222c9916cdSFrançois Tigeot } 2232c9916cdSFrançois Tigeot 2242c9916cdSFrançois Tigeot /** 2252c9916cdSFrançois Tigeot * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip 2262c9916cdSFrançois Tigeot * @dev: DRM device 2272c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 2282c9916cdSFrançois Tigeot * 2292c9916cdSFrançois Tigeot * This function gets called after scheduling a flip on @obj. The actual 2302c9916cdSFrançois Tigeot * frontbuffer flushing will be delayed until completion is signalled with 2312c9916cdSFrançois Tigeot * intel_frontbuffer_flip_complete. If an invalidate happens in between this 2322c9916cdSFrançois Tigeot * flush will be cancelled. 2332c9916cdSFrançois Tigeot * 2342c9916cdSFrançois Tigeot * Can be called without any locks held. 2352c9916cdSFrançois Tigeot */ 2362c9916cdSFrançois Tigeot void intel_frontbuffer_flip_prepare(struct drm_device *dev, 2372c9916cdSFrançois Tigeot unsigned frontbuffer_bits) 2382c9916cdSFrançois Tigeot { 2392c9916cdSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2402c9916cdSFrançois Tigeot 2412c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 2422c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits |= frontbuffer_bits; 2432c9916cdSFrançois Tigeot /* Remove stale busy bits due to the old buffer. */ 2442c9916cdSFrançois Tigeot dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits; 2452c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 246*19c468b4SFrançois Tigeot 247*19c468b4SFrançois Tigeot intel_psr_single_frame_update(dev); 2482c9916cdSFrançois Tigeot } 2492c9916cdSFrançois Tigeot 2502c9916cdSFrançois Tigeot /** 2512c9916cdSFrançois Tigeot * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip 2522c9916cdSFrançois Tigeot * @dev: DRM device 2532c9916cdSFrançois Tigeot * @frontbuffer_bits: frontbuffer plane tracking bits 2542c9916cdSFrançois Tigeot * 2552c9916cdSFrançois Tigeot * This function gets called after the flip has been latched and will complete 2562c9916cdSFrançois Tigeot * on the next vblank. It will execute the flush if it hasn't been cancelled yet. 2572c9916cdSFrançois Tigeot * 2582c9916cdSFrançois Tigeot * Can be called without any locks held. 2592c9916cdSFrançois Tigeot */ 2602c9916cdSFrançois Tigeot void intel_frontbuffer_flip_complete(struct drm_device *dev, 2612c9916cdSFrançois Tigeot unsigned frontbuffer_bits) 2622c9916cdSFrançois Tigeot { 2632c9916cdSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2642c9916cdSFrançois Tigeot 2652c9916cdSFrançois Tigeot mutex_lock(&dev_priv->fb_tracking.lock); 2662c9916cdSFrançois Tigeot /* Mask any cancelled flips. */ 2672c9916cdSFrançois Tigeot frontbuffer_bits &= dev_priv->fb_tracking.flip_bits; 2682c9916cdSFrançois Tigeot dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits; 2692c9916cdSFrançois Tigeot mutex_unlock(&dev_priv->fb_tracking.lock); 2702c9916cdSFrançois Tigeot 2712c9916cdSFrançois Tigeot intel_frontbuffer_flush(dev, frontbuffer_bits); 2722c9916cdSFrançois Tigeot } 273