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