blob: 152d9ab135b1d4a6dddd0a92dd75a6eb15b10f7d [file] [log] [blame]
Chris Wilson3f51b7e12018-08-30 14:48:06 +01001/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2018 Intel Corporation
5 */
6
7#include <linux/random.h>
8
Chris Wilson10be98a2019-05-28 10:29:49 +01009#include "gem/selftests/igt_gem_utils.h"
10#include "gem/selftests/mock_context.h"
Chris Wilson29d88082021-01-23 14:55:43 +000011#include "gem/i915_gem_pm.h"
Chris Wilsoncb823ed2019-07-12 20:29:53 +010012#include "gt/intel_gt.h"
Chris Wilsone26b6d42019-12-22 12:07:52 +000013#include "gt/intel_gt_pm.h"
Chris Wilson3f51b7e12018-08-30 14:48:06 +010014
Chris Wilson10be98a2019-05-28 10:29:49 +010015#include "i915_selftest.h"
16
Chris Wilson3f51b7e12018-08-30 14:48:06 +010017#include "igt_flush_test.h"
Chris Wilson10be98a2019-05-28 10:29:49 +010018#include "mock_drm.h"
Chris Wilson3f51b7e12018-08-30 14:48:06 +010019
Chris Wilsonc31c9e82019-10-22 14:02:21 +010020static int switch_to_context(struct i915_gem_context *ctx)
Chris Wilson3f51b7e12018-08-30 14:48:06 +010021{
Chris Wilsonc31c9e82019-10-22 14:02:21 +010022 struct i915_gem_engines_iter it;
23 struct intel_context *ce;
Chris Wilsone16302c2019-10-22 23:33:16 +010024 int err = 0;
Chris Wilson3f51b7e12018-08-30 14:48:06 +010025
Chris Wilsonc31c9e82019-10-22 14:02:21 +010026 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
Chris Wilson3f51b7e12018-08-30 14:48:06 +010027 struct i915_request *rq;
28
Chris Wilsonc31c9e82019-10-22 14:02:21 +010029 rq = intel_context_create_request(ce);
Chris Wilsone16302c2019-10-22 23:33:16 +010030 if (IS_ERR(rq)) {
31 err = PTR_ERR(rq);
32 break;
33 }
Chris Wilson3f51b7e12018-08-30 14:48:06 +010034
35 i915_request_add(rq);
36 }
Chris Wilsone16302c2019-10-22 23:33:16 +010037 i915_gem_context_unlock_engines(ctx);
Chris Wilson3f51b7e12018-08-30 14:48:06 +010038
Chris Wilsone16302c2019-10-22 23:33:16 +010039 return err;
Chris Wilson3f51b7e12018-08-30 14:48:06 +010040}
41
42static void trash_stolen(struct drm_i915_private *i915)
43{
44 struct i915_ggtt *ggtt = &i915->ggtt;
45 const u64 slot = ggtt->error_capture.start;
46 const resource_size_t size = resource_size(&i915->dsm);
47 unsigned long page;
48 u32 prng = 0x12345678;
49
Matthew Aulde60f7bb2019-10-29 09:58:56 +000050 /* XXX: fsck. needs some more thought... */
51 if (!i915_ggtt_has_aperture(ggtt))
52 return;
53
Chris Wilson3f51b7e12018-08-30 14:48:06 +010054 for (page = 0; page < size; page += PAGE_SIZE) {
55 const dma_addr_t dma = i915->dsm.start + page;
56 u32 __iomem *s;
57 int x;
58
59 ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
60
61 s = io_mapping_map_atomic_wc(&ggtt->iomap, slot);
62 for (x = 0; x < PAGE_SIZE / sizeof(u32); x++) {
63 prng = next_pseudo_random32(prng);
64 iowrite32(prng, &s[x]);
65 }
66 io_mapping_unmap_atomic(s);
67 }
68
69 ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
70}
71
72static void simulate_hibernate(struct drm_i915_private *i915)
73{
Chris Wilsonc9d08cc2019-01-14 14:21:22 +000074 intel_wakeref_t wakeref;
75
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -070076 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
Chris Wilson3f51b7e12018-08-30 14:48:06 +010077
78 /*
79 * As a final sting in the tail, invalidate stolen. Under a real S4,
80 * stolen is lost and needs to be refilled on resume. However, under
81 * CI we merely do S4-device testing (as full S4 is too unreliable
82 * for automated testing across a cluster), so to simulate the effect
83 * of stolen being trashed across S4, we trash it ourselves.
84 */
85 trash_stolen(i915);
86
Daniele Ceraolo Spuriod858d562019-06-13 16:21:54 -070087 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
Chris Wilson3f51b7e12018-08-30 14:48:06 +010088}
89
Hsin-Yi Wang5b117052021-04-20 16:08:53 +030090static int igt_pm_prepare(struct drm_i915_private *i915)
Chris Wilson3f51b7e12018-08-30 14:48:06 +010091{
Chris Wilson5861b012019-03-08 09:36:54 +000092 i915_gem_suspend(i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +010093
Chris Wilson5861b012019-03-08 09:36:54 +000094 return 0;
Chris Wilson3f51b7e12018-08-30 14:48:06 +010095}
96
Hsin-Yi Wang5b117052021-04-20 16:08:53 +030097static void igt_pm_suspend(struct drm_i915_private *i915)
Chris Wilson3f51b7e12018-08-30 14:48:06 +010098{
Chris Wilsonc9d08cc2019-01-14 14:21:22 +000099 intel_wakeref_t wakeref;
100
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -0700101 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilsone9862092020-01-30 18:17:09 +0000102 i915_ggtt_suspend(&i915->ggtt);
Chris Wilsond4225a52019-01-14 14:21:23 +0000103 i915_gem_suspend_late(i915);
104 }
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100105}
106
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300107static void igt_pm_hibernate(struct drm_i915_private *i915)
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100108{
Chris Wilsonc9d08cc2019-01-14 14:21:22 +0000109 intel_wakeref_t wakeref;
110
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -0700111 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilsone9862092020-01-30 18:17:09 +0000112 i915_ggtt_suspend(&i915->ggtt);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100113
Chris Wilsond4225a52019-01-14 14:21:23 +0000114 i915_gem_freeze(i915);
115 i915_gem_freeze_late(i915);
116 }
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100117}
118
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300119static void igt_pm_resume(struct drm_i915_private *i915)
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100120{
Chris Wilsonc9d08cc2019-01-14 14:21:22 +0000121 intel_wakeref_t wakeref;
122
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100123 /*
124 * Both suspend and hibernate follow the same wakeup path and assume
125 * that runtime-pm just works.
126 */
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -0700127 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilsone9862092020-01-30 18:17:09 +0000128 i915_ggtt_resume(&i915->ggtt);
Chris Wilsond4225a52019-01-14 14:21:23 +0000129 i915_gem_resume(i915);
130 }
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100131}
132
133static int igt_gem_suspend(void *arg)
134{
135 struct drm_i915_private *i915 = arg;
136 struct i915_gem_context *ctx;
Chris Wilsona8c9a7f2019-11-07 21:39:29 +0000137 struct file *file;
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100138 int err;
139
140 file = mock_file(i915);
141 if (IS_ERR(file))
142 return PTR_ERR(file);
143
144 err = -ENOMEM;
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100145 ctx = live_context(i915, file);
146 if (!IS_ERR(ctx))
Chris Wilsonc31c9e82019-10-22 14:02:21 +0100147 err = switch_to_context(ctx);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100148 if (err)
149 goto out;
150
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300151 err = igt_pm_prepare(i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100152 if (err)
153 goto out;
154
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300155 igt_pm_suspend(i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100156
157 /* Here be dragons! Note that with S3RST any S3 may become S4! */
158 simulate_hibernate(i915);
159
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300160 igt_pm_resume(i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100161
Chris Wilsonc31c9e82019-10-22 14:02:21 +0100162 err = switch_to_context(ctx);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100163out:
Chris Wilsona8c9a7f2019-11-07 21:39:29 +0000164 fput(file);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100165 return err;
166}
167
168static int igt_gem_hibernate(void *arg)
169{
170 struct drm_i915_private *i915 = arg;
171 struct i915_gem_context *ctx;
Chris Wilsona8c9a7f2019-11-07 21:39:29 +0000172 struct file *file;
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100173 int err;
174
175 file = mock_file(i915);
176 if (IS_ERR(file))
177 return PTR_ERR(file);
178
179 err = -ENOMEM;
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100180 ctx = live_context(i915, file);
181 if (!IS_ERR(ctx))
Chris Wilsonc31c9e82019-10-22 14:02:21 +0100182 err = switch_to_context(ctx);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100183 if (err)
184 goto out;
185
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300186 err = igt_pm_prepare(i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100187 if (err)
188 goto out;
189
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300190 igt_pm_hibernate(i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100191
192 /* Here be dragons! */
193 simulate_hibernate(i915);
194
Hsin-Yi Wang5b117052021-04-20 16:08:53 +0300195 igt_pm_resume(i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100196
Chris Wilsonc31c9e82019-10-22 14:02:21 +0100197 err = switch_to_context(ctx);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100198out:
Chris Wilsona8c9a7f2019-11-07 21:39:29 +0000199 fput(file);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100200 return err;
201}
202
Maarten Lankhorst80f0b672020-08-19 16:08:45 +0200203static int igt_gem_ww_ctx(void *arg)
204{
205 struct drm_i915_private *i915 = arg;
206 struct drm_i915_gem_object *obj, *obj2;
207 struct i915_gem_ww_ctx ww;
208 int err = 0;
209
210 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
211 if (IS_ERR(obj))
212 return PTR_ERR(obj);
213
214 obj2 = i915_gem_object_create_internal(i915, PAGE_SIZE);
Dan Carpenter352ded42020-12-03 11:45:17 +0300215 if (IS_ERR(obj2)) {
216 err = PTR_ERR(obj2);
Maarten Lankhorst80f0b672020-08-19 16:08:45 +0200217 goto put1;
218 }
219
220 i915_gem_ww_ctx_init(&ww, true);
221retry:
222 /* Lock the objects, twice for good measure (-EALREADY handling) */
223 err = i915_gem_object_lock(obj, &ww);
224 if (!err)
225 err = i915_gem_object_lock_interruptible(obj, &ww);
226 if (!err)
227 err = i915_gem_object_lock_interruptible(obj2, &ww);
228 if (!err)
229 err = i915_gem_object_lock(obj2, &ww);
230
231 if (err == -EDEADLK) {
232 err = i915_gem_ww_ctx_backoff(&ww);
233 if (!err)
234 goto retry;
235 }
236 i915_gem_ww_ctx_fini(&ww);
237 i915_gem_object_put(obj2);
238put1:
239 i915_gem_object_put(obj);
240 return err;
241}
242
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100243int i915_gem_live_selftests(struct drm_i915_private *i915)
244{
245 static const struct i915_subtest tests[] = {
246 SUBTEST(igt_gem_suspend),
247 SUBTEST(igt_gem_hibernate),
Maarten Lankhorst80f0b672020-08-19 16:08:45 +0200248 SUBTEST(igt_gem_ww_ctx),
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100249 };
250
Chris Wilsoncb823ed2019-07-12 20:29:53 +0100251 if (intel_gt_is_wedged(&i915->gt))
Chris Wilson1ab494c2019-04-13 13:58:20 +0100252 return 0;
253
Chris Wilson63251682019-07-03 10:17:12 +0100254 return i915_live_subtests(tests, i915);
Chris Wilson3f51b7e12018-08-30 14:48:06 +0100255}