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