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 drm_i915_private *i915,
19 			     struct i915_gem_context *ctx)
20 {
21 	struct intel_engine_cs *engine;
22 	enum intel_engine_id id;
23 
24 	for_each_engine(engine, i915, id) {
25 		struct i915_request *rq;
26 
27 		rq = igt_request_alloc(ctx, engine);
28 		if (IS_ERR(rq))
29 			return PTR_ERR(rq);
30 
31 		i915_request_add(rq);
32 	}
33 
34 	return 0;
35 }
36 
37 static void trash_stolen(struct drm_i915_private *i915)
38 {
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);
42 	unsigned long page;
43 	u32 prng = 0x12345678;
44 
45 	for (page = 0; page < size; page += PAGE_SIZE) {
46 		const dma_addr_t dma = i915->dsm.start + page;
47 		u32 __iomem *s;
48 		int x;
49 
50 		ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
51 
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]);
56 		}
57 		io_mapping_unmap_atomic(s);
58 	}
59 
60 	ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
61 }
62 
63 static void simulate_hibernate(struct drm_i915_private *i915)
64 {
65 	intel_wakeref_t wakeref;
66 
67 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
68 
69 	/*
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.
75 	 */
76 	trash_stolen(i915);
77 
78 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
79 }
80 
81 static int pm_prepare(struct drm_i915_private *i915)
82 {
83 	i915_gem_suspend(i915);
84 
85 	return 0;
86 }
87 
88 static void pm_suspend(struct drm_i915_private *i915)
89 {
90 	intel_wakeref_t wakeref;
91 
92 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
93 		i915_gem_suspend_gtt_mappings(i915);
94 		i915_gem_suspend_late(i915);
95 	}
96 }
97 
98 static void pm_hibernate(struct drm_i915_private *i915)
99 {
100 	intel_wakeref_t wakeref;
101 
102 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
103 		i915_gem_suspend_gtt_mappings(i915);
104 
105 		i915_gem_freeze(i915);
106 		i915_gem_freeze_late(i915);
107 	}
108 }
109 
110 static void pm_resume(struct drm_i915_private *i915)
111 {
112 	intel_wakeref_t wakeref;
113 
114 	/*
115 	 * Both suspend and hibernate follow the same wakeup path and assume
116 	 * that runtime-pm just works.
117 	 */
118 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
119 		intel_gt_sanitize(&i915->gt, false);
120 		i915_gem_sanitize(i915);
121 		i915_gem_resume(i915);
122 	}
123 }
124 
125 static int igt_gem_suspend(void *arg)
126 {
127 	struct drm_i915_private *i915 = arg;
128 	struct i915_gem_context *ctx;
129 	struct drm_file *file;
130 	int err;
131 
132 	file = mock_file(i915);
133 	if (IS_ERR(file))
134 		return PTR_ERR(file);
135 
136 	err = -ENOMEM;
137 	mutex_lock(&i915->drm.struct_mutex);
138 	ctx = live_context(i915, file);
139 	if (!IS_ERR(ctx))
140 		err = switch_to_context(i915, ctx);
141 	mutex_unlock(&i915->drm.struct_mutex);
142 	if (err)
143 		goto out;
144 
145 	err = pm_prepare(i915);
146 	if (err)
147 		goto out;
148 
149 	pm_suspend(i915);
150 
151 	/* Here be dragons! Note that with S3RST any S3 may become S4! */
152 	simulate_hibernate(i915);
153 
154 	pm_resume(i915);
155 
156 	mutex_lock(&i915->drm.struct_mutex);
157 	err = switch_to_context(i915, ctx);
158 	mutex_unlock(&i915->drm.struct_mutex);
159 out:
160 	mock_file_free(i915, file);
161 	return err;
162 }
163 
164 static int igt_gem_hibernate(void *arg)
165 {
166 	struct drm_i915_private *i915 = arg;
167 	struct i915_gem_context *ctx;
168 	struct drm_file *file;
169 	int err;
170 
171 	file = mock_file(i915);
172 	if (IS_ERR(file))
173 		return PTR_ERR(file);
174 
175 	err = -ENOMEM;
176 	mutex_lock(&i915->drm.struct_mutex);
177 	ctx = live_context(i915, file);
178 	if (!IS_ERR(ctx))
179 		err = switch_to_context(i915, ctx);
180 	mutex_unlock(&i915->drm.struct_mutex);
181 	if (err)
182 		goto out;
183 
184 	err = pm_prepare(i915);
185 	if (err)
186 		goto out;
187 
188 	pm_hibernate(i915);
189 
190 	/* Here be dragons! */
191 	simulate_hibernate(i915);
192 
193 	pm_resume(i915);
194 
195 	mutex_lock(&i915->drm.struct_mutex);
196 	err = switch_to_context(i915, ctx);
197 	mutex_unlock(&i915->drm.struct_mutex);
198 out:
199 	mock_file_free(i915, file);
200 	return err;
201 }
202 
203 int i915_gem_live_selftests(struct drm_i915_private *i915)
204 {
205 	static const struct i915_subtest tests[] = {
206 		SUBTEST(igt_gem_suspend),
207 		SUBTEST(igt_gem_hibernate),
208 	};
209 
210 	if (intel_gt_is_wedged(&i915->gt))
211 		return 0;
212 
213 	return i915_live_subtests(tests, i915);
214 }
215