1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2019 Intel Corporation 4 */ 5 6 #include <linux/string_helpers.h> 7 #include <linux/suspend.h> 8 9 #include "i915_drv.h" 10 #include "i915_irq.h" 11 #include "i915_params.h" 12 #include "intel_context.h" 13 #include "intel_engine_pm.h" 14 #include "intel_gt.h" 15 #include "intel_gt_clock_utils.h" 16 #include "intel_gt_pm.h" 17 #include "intel_gt_print.h" 18 #include "intel_gt_requests.h" 19 #include "intel_llc.h" 20 #include "intel_pm.h" 21 #include "intel_rc6.h" 22 #include "intel_rps.h" 23 #include "intel_wakeref.h" 24 #include "pxp/intel_pxp_pm.h" 25 26 #define I915_GT_SUSPEND_IDLE_TIMEOUT (HZ / 2) 27 28 static void user_forcewake(struct intel_gt *gt, bool suspend) 29 { 30 int count = atomic_read(>->user_wakeref); 31 32 /* Inside suspend/resume so single threaded, no races to worry about. */ 33 if (likely(!count)) 34 return; 35 36 intel_gt_pm_get(gt); 37 if (suspend) { 38 GEM_BUG_ON(count > atomic_read(>->wakeref.count)); 39 atomic_sub(count, >->wakeref.count); 40 } else { 41 atomic_add(count, >->wakeref.count); 42 } 43 intel_gt_pm_put(gt); 44 } 45 46 static void runtime_begin(struct intel_gt *gt) 47 { 48 local_irq_disable(); 49 write_seqcount_begin(>->stats.lock); 50 gt->stats.start = ktime_get(); 51 gt->stats.active = true; 52 write_seqcount_end(>->stats.lock); 53 local_irq_enable(); 54 } 55 56 static void runtime_end(struct intel_gt *gt) 57 { 58 local_irq_disable(); 59 write_seqcount_begin(>->stats.lock); 60 gt->stats.active = false; 61 gt->stats.total = 62 ktime_add(gt->stats.total, 63 ktime_sub(ktime_get(), gt->stats.start)); 64 write_seqcount_end(>->stats.lock); 65 local_irq_enable(); 66 } 67 68 static int __gt_unpark(struct intel_wakeref *wf) 69 { 70 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref); 71 struct drm_i915_private *i915 = gt->i915; 72 73 GT_TRACE(gt, "\n"); 74 75 /* 76 * It seems that the DMC likes to transition between the DC states a lot 77 * when there are no connected displays (no active power domains) during 78 * command submission. 79 * 80 * This activity has negative impact on the performance of the chip with 81 * huge latencies observed in the interrupt handler and elsewhere. 82 * 83 * Work around it by grabbing a GT IRQ power domain whilst there is any 84 * GT activity, preventing any DC state transitions. 85 */ 86 gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); 87 GEM_BUG_ON(!gt->awake); 88 89 intel_rc6_unpark(>->rc6); 90 intel_rps_unpark(>->rps); 91 i915_pmu_gt_unparked(i915); 92 intel_guc_busyness_unpark(gt); 93 94 intel_gt_unpark_requests(gt); 95 runtime_begin(gt); 96 97 return 0; 98 } 99 100 static int __gt_park(struct intel_wakeref *wf) 101 { 102 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref); 103 intel_wakeref_t wakeref = fetch_and_zero(>->awake); 104 struct drm_i915_private *i915 = gt->i915; 105 106 GT_TRACE(gt, "\n"); 107 108 runtime_end(gt); 109 intel_gt_park_requests(gt); 110 111 intel_guc_busyness_park(gt); 112 i915_vma_parked(gt); 113 i915_pmu_gt_parked(i915); 114 intel_rps_park(>->rps); 115 intel_rc6_park(>->rc6); 116 117 /* Everything switched off, flush any residual interrupt just in case */ 118 intel_synchronize_irq(i915); 119 120 /* Defer dropping the display power well for 100ms, it's slow! */ 121 GEM_BUG_ON(!wakeref); 122 intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref); 123 124 return 0; 125 } 126 127 static const struct intel_wakeref_ops wf_ops = { 128 .get = __gt_unpark, 129 .put = __gt_park, 130 }; 131 132 void intel_gt_pm_init_early(struct intel_gt *gt) 133 { 134 /* 135 * We access the runtime_pm structure via gt->i915 here rather than 136 * gt->uncore as we do elsewhere in the file because gt->uncore is not 137 * yet initialized for all tiles at this point in the driver startup. 138 * runtime_pm is per-device rather than per-tile, so this is still the 139 * correct structure. 140 */ 141 intel_wakeref_init(>->wakeref, >->i915->runtime_pm, &wf_ops); 142 seqcount_mutex_init(>->stats.lock, >->wakeref.mutex); 143 } 144 145 void intel_gt_pm_init(struct intel_gt *gt) 146 { 147 /* 148 * Enabling power-management should be "self-healing". If we cannot 149 * enable a feature, simply leave it disabled with a notice to the 150 * user. 151 */ 152 intel_rc6_init(>->rc6); 153 intel_rps_init(>->rps); 154 } 155 156 static bool reset_engines(struct intel_gt *gt) 157 { 158 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display) 159 return false; 160 161 return __intel_gt_reset(gt, ALL_ENGINES) == 0; 162 } 163 164 static void gt_sanitize(struct intel_gt *gt, bool force) 165 { 166 struct intel_engine_cs *engine; 167 enum intel_engine_id id; 168 intel_wakeref_t wakeref; 169 170 GT_TRACE(gt, "force:%s", str_yes_no(force)); 171 172 /* Use a raw wakeref to avoid calling intel_display_power_get early */ 173 wakeref = intel_runtime_pm_get(gt->uncore->rpm); 174 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL); 175 176 intel_gt_check_clock_frequency(gt); 177 178 /* 179 * As we have just resumed the machine and woken the device up from 180 * deep PCI sleep (presumably D3_cold), assume the HW has been reset 181 * back to defaults, recovering from whatever wedged state we left it 182 * in and so worth trying to use the device once more. 183 */ 184 if (intel_gt_is_wedged(gt)) 185 intel_gt_unset_wedged(gt); 186 187 /* For GuC mode, ensure submission is disabled before stopping ring */ 188 intel_uc_reset_prepare(>->uc); 189 190 for_each_engine(engine, gt, id) { 191 if (engine->reset.prepare) 192 engine->reset.prepare(engine); 193 194 if (engine->sanitize) 195 engine->sanitize(engine); 196 } 197 198 if (reset_engines(gt) || force) { 199 for_each_engine(engine, gt, id) 200 __intel_engine_reset(engine, false); 201 } 202 203 intel_uc_reset(>->uc, false); 204 205 for_each_engine(engine, gt, id) 206 if (engine->reset.finish) 207 engine->reset.finish(engine); 208 209 intel_rps_sanitize(>->rps); 210 211 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL); 212 intel_runtime_pm_put(gt->uncore->rpm, wakeref); 213 } 214 215 void intel_gt_pm_fini(struct intel_gt *gt) 216 { 217 intel_rc6_fini(>->rc6); 218 } 219 220 int intel_gt_resume(struct intel_gt *gt) 221 { 222 struct intel_engine_cs *engine; 223 enum intel_engine_id id; 224 int err; 225 226 err = intel_gt_has_unrecoverable_error(gt); 227 if (err) 228 return err; 229 230 GT_TRACE(gt, "\n"); 231 232 /* 233 * After resume, we may need to poke into the pinned kernel 234 * contexts to paper over any damage caused by the sudden suspend. 235 * Only the kernel contexts should remain pinned over suspend, 236 * allowing us to fixup the user contexts on their first pin. 237 */ 238 gt_sanitize(gt, true); 239 240 intel_gt_pm_get(gt); 241 242 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL); 243 intel_rc6_sanitize(>->rc6); 244 if (intel_gt_is_wedged(gt)) { 245 err = -EIO; 246 goto out_fw; 247 } 248 249 /* Only when the HW is re-initialised, can we replay the requests */ 250 err = intel_gt_init_hw(gt); 251 if (err) { 252 gt_probe_error(gt, "Failed to initialize GPU, declaring it wedged!\n"); 253 goto err_wedged; 254 } 255 256 intel_uc_reset_finish(>->uc); 257 258 intel_rps_enable(>->rps); 259 intel_llc_enable(>->llc); 260 261 for_each_engine(engine, gt, id) { 262 intel_engine_pm_get(engine); 263 264 engine->serial++; /* kernel context lost */ 265 err = intel_engine_resume(engine); 266 267 intel_engine_pm_put(engine); 268 if (err) { 269 gt_err(gt, "Failed to restart %s (%d)\n", 270 engine->name, err); 271 goto err_wedged; 272 } 273 } 274 275 intel_rc6_enable(>->rc6); 276 277 intel_uc_resume(>->uc); 278 279 user_forcewake(gt, false); 280 281 out_fw: 282 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL); 283 intel_gt_pm_put(gt); 284 return err; 285 286 err_wedged: 287 intel_gt_set_wedged(gt); 288 goto out_fw; 289 } 290 291 static void wait_for_suspend(struct intel_gt *gt) 292 { 293 if (!intel_gt_pm_is_awake(gt)) 294 return; 295 296 if (intel_gt_wait_for_idle(gt, I915_GT_SUSPEND_IDLE_TIMEOUT) == -ETIME) { 297 /* 298 * Forcibly cancel outstanding work and leave 299 * the gpu quiet. 300 */ 301 intel_gt_set_wedged(gt); 302 intel_gt_retire_requests(gt); 303 } 304 305 intel_gt_pm_wait_for_idle(gt); 306 } 307 308 void intel_gt_suspend_prepare(struct intel_gt *gt) 309 { 310 user_forcewake(gt, true); 311 wait_for_suspend(gt); 312 } 313 314 static suspend_state_t pm_suspend_target(void) 315 { 316 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP) 317 return pm_suspend_target_state; 318 #else 319 return PM_SUSPEND_TO_IDLE; 320 #endif 321 } 322 323 void intel_gt_suspend_late(struct intel_gt *gt) 324 { 325 intel_wakeref_t wakeref; 326 327 /* We expect to be idle already; but also want to be independent */ 328 wait_for_suspend(gt); 329 330 if (is_mock_gt(gt)) 331 return; 332 333 GEM_BUG_ON(gt->awake); 334 335 intel_uc_suspend(>->uc); 336 337 /* 338 * On disabling the device, we want to turn off HW access to memory 339 * that we no longer own. 340 * 341 * However, not all suspend-states disable the device. S0 (s2idle) 342 * is effectively runtime-suspend, the device is left powered on 343 * but needs to be put into a low power state. We need to keep 344 * powermanagement enabled, but we also retain system state and so 345 * it remains safe to keep on using our allocated memory. 346 */ 347 if (pm_suspend_target() == PM_SUSPEND_TO_IDLE) 348 return; 349 350 with_intel_runtime_pm(gt->uncore->rpm, wakeref) { 351 intel_rps_disable(>->rps); 352 intel_rc6_disable(>->rc6); 353 intel_llc_disable(>->llc); 354 } 355 356 gt_sanitize(gt, false); 357 358 GT_TRACE(gt, "\n"); 359 } 360 361 void intel_gt_runtime_suspend(struct intel_gt *gt) 362 { 363 intel_uc_runtime_suspend(>->uc); 364 365 GT_TRACE(gt, "\n"); 366 } 367 368 int intel_gt_runtime_resume(struct intel_gt *gt) 369 { 370 int ret; 371 372 GT_TRACE(gt, "\n"); 373 intel_gt_init_swizzling(gt); 374 intel_ggtt_restore_fences(gt->ggtt); 375 376 ret = intel_uc_runtime_resume(>->uc); 377 if (ret) 378 return ret; 379 380 return 0; 381 } 382 383 static ktime_t __intel_gt_get_awake_time(const struct intel_gt *gt) 384 { 385 ktime_t total = gt->stats.total; 386 387 if (gt->stats.active) 388 total = ktime_add(total, 389 ktime_sub(ktime_get(), gt->stats.start)); 390 391 return total; 392 } 393 394 ktime_t intel_gt_get_awake_time(const struct intel_gt *gt) 395 { 396 unsigned int seq; 397 ktime_t total; 398 399 do { 400 seq = read_seqcount_begin(>->stats.lock); 401 total = __intel_gt_get_awake_time(gt); 402 } while (read_seqcount_retry(>->stats.lock, seq)); 403 404 return total; 405 } 406 407 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 408 #include "selftest_gt_pm.c" 409 #endif 410