| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright © 2018 Intel Corporation |
| 5 | */ |
| 6 | |
| 7 | #include <linux/prandom.h> |
| 8 | |
| 9 | #include "gem/i915_gem_internal.h" |
| 10 | #include "gem/i915_gem_pm.h" |
| 11 | #include "gem/selftests/igt_gem_utils.h" |
| 12 | #include "gem/selftests/mock_context.h" |
| 13 | #include "gt/intel_gt.h" |
| 14 | #include "gt/intel_gt_pm.h" |
| 15 | |
| 16 | #include "i915_selftest.h" |
| 17 | |
| 18 | #include "igt_flush_test.h" |
| 19 | #include "mock_drm.h" |
| 20 | |
| 21 | static int switch_to_context(struct i915_gem_context *ctx) |
| 22 | { |
| 23 | struct i915_gem_engines_iter it; |
| 24 | struct intel_context *ce; |
| 25 | int err = 0; |
| 26 | |
| 27 | for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) { |
| 28 | struct i915_request *rq; |
| 29 | |
| 30 | rq = intel_context_create_request(ce); |
| 31 | if (IS_ERR(ptr: rq)) { |
| 32 | err = PTR_ERR(ptr: rq); |
| 33 | break; |
| 34 | } |
| 35 | |
| 36 | i915_request_add(rq); |
| 37 | } |
| 38 | i915_gem_context_unlock_engines(ctx); |
| 39 | |
| 40 | return err; |
| 41 | } |
| 42 | |
| 43 | static void trash_stolen(struct drm_i915_private *i915) |
| 44 | { |
| 45 | struct i915_ggtt *ggtt = to_gt(i915)->ggtt; |
| 46 | const u64 slot = ggtt->error_capture.start; |
| 47 | const resource_size_t size = resource_size(res: &i915->dsm.stolen); |
| 48 | struct rnd_state prng; |
| 49 | unsigned long page; |
| 50 | |
| 51 | /* XXX: fsck. needs some more thought... */ |
| 52 | if (!i915_ggtt_has_aperture(ggtt)) |
| 53 | return; |
| 54 | |
| 55 | prandom_seed_state(state: &prng, seed: 0x12345678); |
| 56 | |
| 57 | for (page = 0; page < size; page += PAGE_SIZE) { |
| 58 | const dma_addr_t dma = i915->dsm.stolen.start + page; |
| 59 | u32 __iomem *s; |
| 60 | int x; |
| 61 | |
| 62 | ggtt->vm.insert_page(&ggtt->vm, dma, slot, |
| 63 | i915_gem_get_pat_index(i915, |
| 64 | level: I915_CACHE_NONE), |
| 65 | 0); |
| 66 | |
| 67 | s = io_mapping_map_atomic_wc(mapping: &ggtt->iomap, offset: slot); |
| 68 | for (x = 0; x < PAGE_SIZE / sizeof(u32); x++) { |
| 69 | iowrite32(prandom_u32_state(state: &prng), &s[x]); |
| 70 | } |
| 71 | io_mapping_unmap_atomic(vaddr: s); |
| 72 | } |
| 73 | |
| 74 | ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE); |
| 75 | } |
| 76 | |
| 77 | static void simulate_hibernate(struct drm_i915_private *i915) |
| 78 | { |
| 79 | intel_wakeref_t wakeref; |
| 80 | |
| 81 | wakeref = intel_runtime_pm_get(rpm: &i915->runtime_pm); |
| 82 | |
| 83 | /* |
| 84 | * As a final string in the tail, invalidate stolen. Under a real S4, |
| 85 | * stolen is lost and needs to be refilled on resume. However, under |
| 86 | * CI we merely do S4-device testing (as full S4 is too unreliable |
| 87 | * for automated testing across a cluster), so to simulate the effect |
| 88 | * of stolen being trashed across S4, we trash it ourselves. |
| 89 | */ |
| 90 | trash_stolen(i915); |
| 91 | |
| 92 | intel_runtime_pm_put(rpm: &i915->runtime_pm, wref: wakeref); |
| 93 | } |
| 94 | |
| 95 | static int igt_pm_prepare(struct drm_i915_private *i915) |
| 96 | { |
| 97 | i915_gem_suspend(i915); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static void igt_pm_suspend(struct drm_i915_private *i915) |
| 103 | { |
| 104 | intel_wakeref_t wakeref; |
| 105 | |
| 106 | with_intel_runtime_pm(&i915->runtime_pm, wakeref) { |
| 107 | i915_ggtt_suspend(gtt: to_gt(i915)->ggtt); |
| 108 | i915_gem_suspend_late(i915); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static void igt_pm_hibernate(struct drm_i915_private *i915) |
| 113 | { |
| 114 | intel_wakeref_t wakeref; |
| 115 | |
| 116 | with_intel_runtime_pm(&i915->runtime_pm, wakeref) { |
| 117 | i915_ggtt_suspend(gtt: to_gt(i915)->ggtt); |
| 118 | |
| 119 | i915_gem_freeze(i915); |
| 120 | i915_gem_freeze_late(i915); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static void igt_pm_resume(struct drm_i915_private *i915) |
| 125 | { |
| 126 | intel_wakeref_t wakeref; |
| 127 | |
| 128 | /* |
| 129 | * Both suspend and hibernate follow the same wakeup path and assume |
| 130 | * that runtime-pm just works. |
| 131 | */ |
| 132 | with_intel_runtime_pm(&i915->runtime_pm, wakeref) { |
| 133 | i915_ggtt_resume(ggtt: to_gt(i915)->ggtt); |
| 134 | if (GRAPHICS_VER(i915) >= 8) |
| 135 | setup_private_pat(to_gt(i915)); |
| 136 | i915_gem_resume(i915); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static int igt_gem_suspend(void *arg) |
| 141 | { |
| 142 | struct drm_i915_private *i915 = arg; |
| 143 | struct i915_gem_context *ctx; |
| 144 | struct file *file; |
| 145 | int err; |
| 146 | |
| 147 | file = mock_file(i915); |
| 148 | if (IS_ERR(ptr: file)) |
| 149 | return PTR_ERR(ptr: file); |
| 150 | |
| 151 | err = -ENOMEM; |
| 152 | ctx = live_context(i915, file); |
| 153 | if (!IS_ERR(ptr: ctx)) |
| 154 | err = switch_to_context(ctx); |
| 155 | if (err) |
| 156 | goto out; |
| 157 | |
| 158 | err = igt_pm_prepare(i915); |
| 159 | if (err) |
| 160 | goto out; |
| 161 | |
| 162 | igt_pm_suspend(i915); |
| 163 | |
| 164 | /* Here be dragons! Note that with S3RST any S3 may become S4! */ |
| 165 | simulate_hibernate(i915); |
| 166 | |
| 167 | igt_pm_resume(i915); |
| 168 | |
| 169 | err = switch_to_context(ctx); |
| 170 | out: |
| 171 | fput(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 file *file; |
| 180 | int err; |
| 181 | |
| 182 | file = mock_file(i915); |
| 183 | if (IS_ERR(ptr: file)) |
| 184 | return PTR_ERR(ptr: file); |
| 185 | |
| 186 | err = -ENOMEM; |
| 187 | ctx = live_context(i915, file); |
| 188 | if (!IS_ERR(ptr: ctx)) |
| 189 | err = switch_to_context(ctx); |
| 190 | if (err) |
| 191 | goto out; |
| 192 | |
| 193 | err = igt_pm_prepare(i915); |
| 194 | if (err) |
| 195 | goto out; |
| 196 | |
| 197 | igt_pm_hibernate(i915); |
| 198 | |
| 199 | /* Here be dragons! */ |
| 200 | simulate_hibernate(i915); |
| 201 | |
| 202 | igt_pm_resume(i915); |
| 203 | |
| 204 | err = switch_to_context(ctx); |
| 205 | out: |
| 206 | fput(file); |
| 207 | return err; |
| 208 | } |
| 209 | |
| 210 | static int igt_gem_ww_ctx(void *arg) |
| 211 | { |
| 212 | struct drm_i915_private *i915 = arg; |
| 213 | struct drm_i915_gem_object *obj, *obj2; |
| 214 | struct i915_gem_ww_ctx ww; |
| 215 | int err = 0; |
| 216 | |
| 217 | obj = i915_gem_object_create_internal(i915, PAGE_SIZE); |
| 218 | if (IS_ERR(ptr: obj)) |
| 219 | return PTR_ERR(ptr: obj); |
| 220 | |
| 221 | obj2 = i915_gem_object_create_internal(i915, PAGE_SIZE); |
| 222 | if (IS_ERR(ptr: obj2)) { |
| 223 | err = PTR_ERR(ptr: obj2); |
| 224 | goto put1; |
| 225 | } |
| 226 | |
| 227 | i915_gem_ww_ctx_init(ctx: &ww, intr: true); |
| 228 | retry: |
| 229 | /* Lock the objects, twice for good measure (-EALREADY handling) */ |
| 230 | err = i915_gem_object_lock(obj, ww: &ww); |
| 231 | if (!err) |
| 232 | err = i915_gem_object_lock_interruptible(obj, ww: &ww); |
| 233 | if (!err) |
| 234 | err = i915_gem_object_lock_interruptible(obj: obj2, ww: &ww); |
| 235 | if (!err) |
| 236 | err = i915_gem_object_lock(obj: obj2, ww: &ww); |
| 237 | |
| 238 | if (err == -EDEADLK) { |
| 239 | err = i915_gem_ww_ctx_backoff(ctx: &ww); |
| 240 | if (!err) |
| 241 | goto retry; |
| 242 | } |
| 243 | i915_gem_ww_ctx_fini(ctx: &ww); |
| 244 | i915_gem_object_put(obj: obj2); |
| 245 | put1: |
| 246 | i915_gem_object_put(obj); |
| 247 | return err; |
| 248 | } |
| 249 | |
| 250 | int i915_gem_live_selftests(struct drm_i915_private *i915) |
| 251 | { |
| 252 | static const struct i915_subtest tests[] = { |
| 253 | SUBTEST(igt_gem_suspend), |
| 254 | SUBTEST(igt_gem_hibernate), |
| 255 | SUBTEST(igt_gem_ww_ctx), |
| 256 | }; |
| 257 | |
| 258 | if (intel_gt_is_wedged(gt: to_gt(i915))) |
| 259 | return 0; |
| 260 | |
| 261 | return i915_live_subtests(tests, i915); |
| 262 | } |
| 263 | |