2 * SPDX-License-Identifier: MIT
4 * Copyright © 2018 Intel Corporation
7 #include <linux/prandom.h>
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"
16 #include "i915_selftest.h"
18 #include "igt_flush_test.h"
21 static int switch_to_context(struct i915_gem_context *ctx)
23 struct i915_gem_engines_iter it;
24 struct intel_context *ce;
27 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
28 struct i915_request *rq;
30 rq = intel_context_create_request(ce);
38 i915_gem_context_unlock_engines(ctx);
43 static void trash_stolen(struct drm_i915_private *i915)
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(&i915->dsm.stolen);
49 u32 prng = 0x12345678;
51 /* XXX: fsck. needs some more thought... */
52 if (!i915_ggtt_has_aperture(ggtt))
55 for (page = 0; page < size; page += PAGE_SIZE) {
56 const dma_addr_t dma = i915->dsm.stolen.start + page;
60 ggtt->vm.insert_page(&ggtt->vm, dma, slot,
61 i915_gem_get_pat_index(i915,
65 s = io_mapping_map_atomic_wc(&ggtt->iomap, slot);
66 for (x = 0; x < PAGE_SIZE / sizeof(u32); x++) {
67 prng = next_pseudo_random32(prng);
68 iowrite32(prng, &s[x]);
70 io_mapping_unmap_atomic(s);
73 ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
76 static void simulate_hibernate(struct drm_i915_private *i915)
78 intel_wakeref_t wakeref;
80 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
83 * As a final sting in the tail, invalidate stolen. Under a real S4,
84 * stolen is lost and needs to be refilled on resume. However, under
85 * CI we merely do S4-device testing (as full S4 is too unreliable
86 * for automated testing across a cluster), so to simulate the effect
87 * of stolen being trashed across S4, we trash it ourselves.
91 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
94 static int igt_pm_prepare(struct drm_i915_private *i915)
96 i915_gem_suspend(i915);
101 static void igt_pm_suspend(struct drm_i915_private *i915)
103 intel_wakeref_t wakeref;
105 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
106 i915_ggtt_suspend(to_gt(i915)->ggtt);
107 i915_gem_suspend_late(i915);
111 static void igt_pm_hibernate(struct drm_i915_private *i915)
113 intel_wakeref_t wakeref;
115 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
116 i915_ggtt_suspend(to_gt(i915)->ggtt);
118 i915_gem_freeze(i915);
119 i915_gem_freeze_late(i915);
123 static void igt_pm_resume(struct drm_i915_private *i915)
125 intel_wakeref_t wakeref;
128 * Both suspend and hibernate follow the same wakeup path and assume
129 * that runtime-pm just works.
131 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
132 i915_ggtt_resume(to_gt(i915)->ggtt);
133 if (GRAPHICS_VER(i915) >= 8)
134 setup_private_pat(to_gt(i915));
135 i915_gem_resume(i915);
139 static int igt_gem_suspend(void *arg)
141 struct drm_i915_private *i915 = arg;
142 struct i915_gem_context *ctx;
146 file = mock_file(i915);
148 return PTR_ERR(file);
151 ctx = live_context(i915, file);
153 err = switch_to_context(ctx);
157 err = igt_pm_prepare(i915);
161 igt_pm_suspend(i915);
163 /* Here be dragons! Note that with S3RST any S3 may become S4! */
164 simulate_hibernate(i915);
168 err = switch_to_context(ctx);
174 static int igt_gem_hibernate(void *arg)
176 struct drm_i915_private *i915 = arg;
177 struct i915_gem_context *ctx;
181 file = mock_file(i915);
183 return PTR_ERR(file);
186 ctx = live_context(i915, file);
188 err = switch_to_context(ctx);
192 err = igt_pm_prepare(i915);
196 igt_pm_hibernate(i915);
198 /* Here be dragons! */
199 simulate_hibernate(i915);
203 err = switch_to_context(ctx);
209 static int igt_gem_ww_ctx(void *arg)
211 struct drm_i915_private *i915 = arg;
212 struct drm_i915_gem_object *obj, *obj2;
213 struct i915_gem_ww_ctx ww;
216 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
220 obj2 = i915_gem_object_create_internal(i915, PAGE_SIZE);
226 i915_gem_ww_ctx_init(&ww, true);
228 /* Lock the objects, twice for good measure (-EALREADY handling) */
229 err = i915_gem_object_lock(obj, &ww);
231 err = i915_gem_object_lock_interruptible(obj, &ww);
233 err = i915_gem_object_lock_interruptible(obj2, &ww);
235 err = i915_gem_object_lock(obj2, &ww);
237 if (err == -EDEADLK) {
238 err = i915_gem_ww_ctx_backoff(&ww);
242 i915_gem_ww_ctx_fini(&ww);
243 i915_gem_object_put(obj2);
245 i915_gem_object_put(obj);
249 int i915_gem_live_selftests(struct drm_i915_private *i915)
251 static const struct i915_subtest tests[] = {
252 SUBTEST(igt_gem_suspend),
253 SUBTEST(igt_gem_hibernate),
254 SUBTEST(igt_gem_ww_ctx),
257 if (intel_gt_is_wedged(to_gt(i915)))
260 return i915_live_subtests(tests, i915);