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