1 /* $NetBSD: i915_gem.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $ */
2
3 /*
4 * SPDX-License-Identifier: MIT
5 *
6 * Copyright © 2018 Intel Corporation
7 */
8
9 #include <sys/cdefs.h>
10 __KERNEL_RCSID(0, "$NetBSD: i915_gem.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $");
11
12 #include <linux/random.h>
13
14 #include "gem/selftests/igt_gem_utils.h"
15 #include "gem/selftests/mock_context.h"
16 #include "gt/intel_gt.h"
17 #include "gt/intel_gt_pm.h"
18
19 #include "i915_selftest.h"
20
21 #include "igt_flush_test.h"
22 #include "mock_drm.h"
23
switch_to_context(struct i915_gem_context * ctx)24 static int switch_to_context(struct i915_gem_context *ctx)
25 {
26 struct i915_gem_engines_iter it;
27 struct intel_context *ce;
28 int err = 0;
29
30 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
31 struct i915_request *rq;
32
33 rq = intel_context_create_request(ce);
34 if (IS_ERR(rq)) {
35 err = PTR_ERR(rq);
36 break;
37 }
38
39 i915_request_add(rq);
40 }
41 i915_gem_context_unlock_engines(ctx);
42
43 return err;
44 }
45
trash_stolen(struct drm_i915_private * i915)46 static void trash_stolen(struct drm_i915_private *i915)
47 {
48 struct i915_ggtt *ggtt = &i915->ggtt;
49 const u64 slot = ggtt->error_capture.start;
50 const resource_size_t size = resource_size(&i915->dsm);
51 unsigned long page;
52 u32 prng = 0x12345678;
53
54 /* XXX: fsck. needs some more thought... */
55 if (!i915_ggtt_has_aperture(ggtt))
56 return;
57
58 for (page = 0; page < size; page += PAGE_SIZE) {
59 const dma_addr_t dma = i915->dsm.start + page;
60 u32 __iomem *s;
61 int x;
62
63 ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
64
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]);
69 }
70 io_mapping_unmap_atomic(s);
71 }
72
73 ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
74 }
75
simulate_hibernate(struct drm_i915_private * i915)76 static void simulate_hibernate(struct drm_i915_private *i915)
77 {
78 intel_wakeref_t wakeref;
79
80 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
81
82 /*
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.
88 */
89 trash_stolen(i915);
90
91 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
92 }
93
pm_prepare(struct drm_i915_private * i915)94 static int pm_prepare(struct drm_i915_private *i915)
95 {
96 i915_gem_suspend(i915);
97
98 return 0;
99 }
100
pm_suspend(struct drm_i915_private * i915)101 static void pm_suspend(struct drm_i915_private *i915)
102 {
103 intel_wakeref_t wakeref;
104
105 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
106 i915_gem_suspend_gtt_mappings(i915);
107 i915_gem_suspend_late(i915);
108 }
109 }
110
pm_hibernate(struct drm_i915_private * i915)111 static void pm_hibernate(struct drm_i915_private *i915)
112 {
113 intel_wakeref_t wakeref;
114
115 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
116 i915_gem_suspend_gtt_mappings(i915);
117
118 i915_gem_freeze(i915);
119 i915_gem_freeze_late(i915);
120 }
121 }
122
pm_resume(struct drm_i915_private * i915)123 static void pm_resume(struct drm_i915_private *i915)
124 {
125 intel_wakeref_t wakeref;
126
127 /*
128 * Both suspend and hibernate follow the same wakeup path and assume
129 * that runtime-pm just works.
130 */
131 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
132 i915_gem_restore_gtt_mappings(i915);
133 i915_gem_restore_fences(&i915->ggtt);
134
135 i915_gem_resume(i915);
136 }
137 }
138
igt_gem_suspend(void * arg)139 static int igt_gem_suspend(void *arg)
140 {
141 struct drm_i915_private *i915 = arg;
142 struct i915_gem_context *ctx;
143 struct file *file;
144 int err;
145
146 file = mock_file(i915);
147 if (IS_ERR(file))
148 return PTR_ERR(file);
149
150 err = -ENOMEM;
151 ctx = live_context(i915, file);
152 if (!IS_ERR(ctx))
153 err = switch_to_context(ctx);
154 if (err)
155 goto out;
156
157 err = pm_prepare(i915);
158 if (err)
159 goto out;
160
161 pm_suspend(i915);
162
163 /* Here be dragons! Note that with S3RST any S3 may become S4! */
164 simulate_hibernate(i915);
165
166 pm_resume(i915);
167
168 err = switch_to_context(ctx);
169 out:
170 fput(file);
171 return err;
172 }
173
igt_gem_hibernate(void * arg)174 static int igt_gem_hibernate(void *arg)
175 {
176 struct drm_i915_private *i915 = arg;
177 struct i915_gem_context *ctx;
178 struct file *file;
179 int err;
180
181 file = mock_file(i915);
182 if (IS_ERR(file))
183 return PTR_ERR(file);
184
185 err = -ENOMEM;
186 ctx = live_context(i915, file);
187 if (!IS_ERR(ctx))
188 err = switch_to_context(ctx);
189 if (err)
190 goto out;
191
192 err = pm_prepare(i915);
193 if (err)
194 goto out;
195
196 pm_hibernate(i915);
197
198 /* Here be dragons! */
199 simulate_hibernate(i915);
200
201 pm_resume(i915);
202
203 err = switch_to_context(ctx);
204 out:
205 fput(file);
206 return err;
207 }
208
i915_gem_live_selftests(struct drm_i915_private * i915)209 int i915_gem_live_selftests(struct drm_i915_private *i915)
210 {
211 static const struct i915_subtest tests[] = {
212 SUBTEST(igt_gem_suspend),
213 SUBTEST(igt_gem_hibernate),
214 };
215
216 if (intel_gt_is_wedged(&i915->gt))
217 return 0;
218
219 return i915_live_subtests(tests, i915);
220 }
221