1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2018 Intel Corporation 5 */ 6 7 #include <linux/random.h> 8 9 #include "gem/selftests/igt_gem_utils.h" 10 #include "gem/selftests/mock_context.h" 11 #include "gt/intel_gt.h" 12 13 #include "i915_selftest.h" 14 15 #include "igt_flush_test.h" 16 #include "mock_drm.h" 17 18 static int switch_to_context(struct i915_gem_context *ctx) 19 { 20 struct i915_gem_engines_iter it; 21 struct intel_context *ce; 22 int err = 0; 23 24 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) { 25 struct i915_request *rq; 26 27 rq = intel_context_create_request(ce); 28 if (IS_ERR(rq)) { 29 err = PTR_ERR(rq); 30 break; 31 } 32 33 i915_request_add(rq); 34 } 35 i915_gem_context_unlock_engines(ctx); 36 37 return err; 38 } 39 40 static void trash_stolen(struct drm_i915_private *i915) 41 { 42 struct i915_ggtt *ggtt = &i915->ggtt; 43 const u64 slot = ggtt->error_capture.start; 44 const resource_size_t size = resource_size(&i915->dsm); 45 unsigned long page; 46 u32 prng = 0x12345678; 47 48 /* XXX: fsck. needs some more thought... */ 49 if (!i915_ggtt_has_aperture(ggtt)) 50 return; 51 52 for (page = 0; page < size; page += PAGE_SIZE) { 53 const dma_addr_t dma = i915->dsm.start + page; 54 u32 __iomem *s; 55 int x; 56 57 ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0); 58 59 s = io_mapping_map_atomic_wc(&ggtt->iomap, slot); 60 for (x = 0; x < PAGE_SIZE / sizeof(u32); x++) { 61 prng = next_pseudo_random32(prng); 62 iowrite32(prng, &s[x]); 63 } 64 io_mapping_unmap_atomic(s); 65 } 66 67 ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE); 68 } 69 70 static void simulate_hibernate(struct drm_i915_private *i915) 71 { 72 intel_wakeref_t wakeref; 73 74 wakeref = intel_runtime_pm_get(&i915->runtime_pm); 75 76 /* 77 * As a final sting in the tail, invalidate stolen. Under a real S4, 78 * stolen is lost and needs to be refilled on resume. However, under 79 * CI we merely do S4-device testing (as full S4 is too unreliable 80 * for automated testing across a cluster), so to simulate the effect 81 * of stolen being trashed across S4, we trash it ourselves. 82 */ 83 trash_stolen(i915); 84 85 intel_runtime_pm_put(&i915->runtime_pm, wakeref); 86 } 87 88 static int pm_prepare(struct drm_i915_private *i915) 89 { 90 i915_gem_suspend(i915); 91 92 return 0; 93 } 94 95 static void pm_suspend(struct drm_i915_private *i915) 96 { 97 intel_wakeref_t wakeref; 98 99 with_intel_runtime_pm(&i915->runtime_pm, wakeref) { 100 i915_gem_suspend_gtt_mappings(i915); 101 i915_gem_suspend_late(i915); 102 } 103 } 104 105 static void pm_hibernate(struct drm_i915_private *i915) 106 { 107 intel_wakeref_t wakeref; 108 109 with_intel_runtime_pm(&i915->runtime_pm, wakeref) { 110 i915_gem_suspend_gtt_mappings(i915); 111 112 i915_gem_freeze(i915); 113 i915_gem_freeze_late(i915); 114 } 115 } 116 117 static void pm_resume(struct drm_i915_private *i915) 118 { 119 intel_wakeref_t wakeref; 120 121 /* 122 * Both suspend and hibernate follow the same wakeup path and assume 123 * that runtime-pm just works. 124 */ 125 with_intel_runtime_pm(&i915->runtime_pm, wakeref) { 126 intel_gt_sanitize(&i915->gt, false); 127 128 i915_gem_restore_gtt_mappings(i915); 129 i915_gem_restore_fences(&i915->ggtt); 130 131 i915_gem_resume(i915); 132 } 133 } 134 135 static int igt_gem_suspend(void *arg) 136 { 137 struct drm_i915_private *i915 = arg; 138 struct i915_gem_context *ctx; 139 struct drm_file *file; 140 int err; 141 142 file = mock_file(i915); 143 if (IS_ERR(file)) 144 return PTR_ERR(file); 145 146 err = -ENOMEM; 147 ctx = live_context(i915, file); 148 if (!IS_ERR(ctx)) 149 err = switch_to_context(ctx); 150 if (err) 151 goto out; 152 153 err = pm_prepare(i915); 154 if (err) 155 goto out; 156 157 pm_suspend(i915); 158 159 /* Here be dragons! Note that with S3RST any S3 may become S4! */ 160 simulate_hibernate(i915); 161 162 pm_resume(i915); 163 164 err = switch_to_context(ctx); 165 out: 166 mock_file_free(i915, file); 167 return err; 168 } 169 170 static int igt_gem_hibernate(void *arg) 171 { 172 struct drm_i915_private *i915 = arg; 173 struct i915_gem_context *ctx; 174 struct drm_file *file; 175 int err; 176 177 file = mock_file(i915); 178 if (IS_ERR(file)) 179 return PTR_ERR(file); 180 181 err = -ENOMEM; 182 ctx = live_context(i915, file); 183 if (!IS_ERR(ctx)) 184 err = switch_to_context(ctx); 185 if (err) 186 goto out; 187 188 err = pm_prepare(i915); 189 if (err) 190 goto out; 191 192 pm_hibernate(i915); 193 194 /* Here be dragons! */ 195 simulate_hibernate(i915); 196 197 pm_resume(i915); 198 199 err = switch_to_context(ctx); 200 out: 201 mock_file_free(i915, file); 202 return err; 203 } 204 205 int i915_gem_live_selftests(struct drm_i915_private *i915) 206 { 207 static const struct i915_subtest tests[] = { 208 SUBTEST(igt_gem_suspend), 209 SUBTEST(igt_gem_hibernate), 210 }; 211 212 if (intel_gt_is_wedged(&i915->gt)) 213 return 0; 214 215 return i915_live_subtests(tests, i915); 216 } 217