2 * SPDX-License-Identifier: MIT
4 * Copyright © 2018 Intel Corporation
7 #include <linux/random.h>
9 #include "gem/selftests/igt_gem_utils.h"
10 #include "gem/selftests/mock_context.h"
11 #include "gt/intel_gt.h"
13 #include "i915_selftest.h"
15 #include "igt_flush_test.h"
18 static int switch_to_context(struct drm_i915_private *i915,
19 struct i915_gem_context *ctx)
21 struct intel_engine_cs *engine;
22 enum intel_engine_id id;
24 for_each_engine(engine, i915, id) {
25 struct i915_request *rq;
27 rq = igt_request_alloc(ctx, engine);
37 static void trash_stolen(struct drm_i915_private *i915)
39 struct i915_ggtt *ggtt = &i915->ggtt;
40 const u64 slot = ggtt->error_capture.start;
41 const resource_size_t size = resource_size(&i915->dsm);
43 u32 prng = 0x12345678;
45 for (page = 0; page < size; page += PAGE_SIZE) {
46 const dma_addr_t dma = i915->dsm.start + page;
50 ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
52 s = io_mapping_map_atomic_wc(&ggtt->iomap, slot);
53 for (x = 0; x < PAGE_SIZE / sizeof(u32); x++) {
54 prng = next_pseudo_random32(prng);
55 iowrite32(prng, &s[x]);
57 io_mapping_unmap_atomic(s);
60 ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
63 static void simulate_hibernate(struct drm_i915_private *i915)
65 intel_wakeref_t wakeref;
67 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
70 * As a final sting in the tail, invalidate stolen. Under a real S4,
71 * stolen is lost and needs to be refilled on resume. However, under
72 * CI we merely do S4-device testing (as full S4 is too unreliable
73 * for automated testing across a cluster), so to simulate the effect
74 * of stolen being trashed across S4, we trash it ourselves.
78 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
81 static int pm_prepare(struct drm_i915_private *i915)
83 i915_gem_suspend(i915);
88 static void pm_suspend(struct drm_i915_private *i915)
90 intel_wakeref_t wakeref;
92 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
93 i915_gem_suspend_gtt_mappings(i915);
94 i915_gem_suspend_late(i915);
98 static void pm_hibernate(struct drm_i915_private *i915)
100 intel_wakeref_t wakeref;
102 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
103 i915_gem_suspend_gtt_mappings(i915);
105 i915_gem_freeze(i915);
106 i915_gem_freeze_late(i915);
110 static void pm_resume(struct drm_i915_private *i915)
112 intel_wakeref_t wakeref;
115 * Both suspend and hibernate follow the same wakeup path and assume
116 * that runtime-pm just works.
118 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
119 intel_gt_sanitize(&i915->gt, false);
120 i915_gem_sanitize(i915);
122 i915_gem_restore_gtt_mappings(i915);
123 i915_gem_restore_fences(i915);
125 i915_gem_resume(i915);
129 static int igt_gem_suspend(void *arg)
131 struct drm_i915_private *i915 = arg;
132 struct i915_gem_context *ctx;
133 struct drm_file *file;
136 file = mock_file(i915);
138 return PTR_ERR(file);
141 mutex_lock(&i915->drm.struct_mutex);
142 ctx = live_context(i915, file);
144 err = switch_to_context(i915, ctx);
145 mutex_unlock(&i915->drm.struct_mutex);
149 err = pm_prepare(i915);
155 /* Here be dragons! Note that with S3RST any S3 may become S4! */
156 simulate_hibernate(i915);
160 mutex_lock(&i915->drm.struct_mutex);
161 err = switch_to_context(i915, ctx);
162 mutex_unlock(&i915->drm.struct_mutex);
164 mock_file_free(i915, file);
168 static int igt_gem_hibernate(void *arg)
170 struct drm_i915_private *i915 = arg;
171 struct i915_gem_context *ctx;
172 struct drm_file *file;
175 file = mock_file(i915);
177 return PTR_ERR(file);
180 mutex_lock(&i915->drm.struct_mutex);
181 ctx = live_context(i915, file);
183 err = switch_to_context(i915, ctx);
184 mutex_unlock(&i915->drm.struct_mutex);
188 err = pm_prepare(i915);
194 /* Here be dragons! */
195 simulate_hibernate(i915);
199 mutex_lock(&i915->drm.struct_mutex);
200 err = switch_to_context(i915, ctx);
201 mutex_unlock(&i915->drm.struct_mutex);
203 mock_file_free(i915, file);
207 int i915_gem_live_selftests(struct drm_i915_private *i915)
209 static const struct i915_subtest tests[] = {
210 SUBTEST(igt_gem_suspend),
211 SUBTEST(igt_gem_hibernate),
214 if (intel_gt_is_wedged(&i915->gt))
217 return i915_live_subtests(tests, i915);