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