treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / selftests / i915_gem.c
blob78f36faf2bbe8c34dbdfb76012e65870d1e7aad1
1 /*
2 * SPDX-License-Identifier: MIT
4 * Copyright © 2018 Intel Corporation
5 */
7 #include <linux/random.h>
9 #include "gem/selftests/igt_gem_utils.h"
10 #include "gem/selftests/mock_context.h"
11 #include "gt/intel_gt.h"
12 #include "gt/intel_gt_pm.h"
14 #include "i915_selftest.h"
16 #include "igt_flush_test.h"
17 #include "mock_drm.h"
19 static int switch_to_context(struct i915_gem_context *ctx)
21 struct i915_gem_engines_iter it;
22 struct intel_context *ce;
23 int err = 0;
25 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
26 struct i915_request *rq;
28 rq = intel_context_create_request(ce);
29 if (IS_ERR(rq)) {
30 err = PTR_ERR(rq);
31 break;
34 i915_request_add(rq);
36 i915_gem_context_unlock_engines(ctx);
38 return err;
41 static void trash_stolen(struct drm_i915_private *i915)
43 struct i915_ggtt *ggtt = &i915->ggtt;
44 const u64 slot = ggtt->error_capture.start;
45 const resource_size_t size = resource_size(&i915->dsm);
46 unsigned long page;
47 u32 prng = 0x12345678;
49 /* XXX: fsck. needs some more thought... */
50 if (!i915_ggtt_has_aperture(ggtt))
51 return;
53 for (page = 0; page < size; page += PAGE_SIZE) {
54 const dma_addr_t dma = i915->dsm.start + page;
55 u32 __iomem *s;
56 int x;
58 ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
60 s = io_mapping_map_atomic_wc(&ggtt->iomap, slot);
61 for (x = 0; x < PAGE_SIZE / sizeof(u32); x++) {
62 prng = next_pseudo_random32(prng);
63 iowrite32(prng, &s[x]);
65 io_mapping_unmap_atomic(s);
68 ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
71 static void simulate_hibernate(struct drm_i915_private *i915)
73 intel_wakeref_t wakeref;
75 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
78 * As a final sting in the tail, invalidate stolen. Under a real S4,
79 * stolen is lost and needs to be refilled on resume. However, under
80 * CI we merely do S4-device testing (as full S4 is too unreliable
81 * for automated testing across a cluster), so to simulate the effect
82 * of stolen being trashed across S4, we trash it ourselves.
84 trash_stolen(i915);
86 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
89 static int pm_prepare(struct drm_i915_private *i915)
91 i915_gem_suspend(i915);
93 return 0;
96 static void pm_suspend(struct drm_i915_private *i915)
98 intel_wakeref_t wakeref;
100 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
101 i915_gem_suspend_gtt_mappings(i915);
102 i915_gem_suspend_late(i915);
106 static void pm_hibernate(struct drm_i915_private *i915)
108 intel_wakeref_t wakeref;
110 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
111 i915_gem_suspend_gtt_mappings(i915);
113 i915_gem_freeze(i915);
114 i915_gem_freeze_late(i915);
118 static void pm_resume(struct drm_i915_private *i915)
120 intel_wakeref_t wakeref;
123 * Both suspend and hibernate follow the same wakeup path and assume
124 * that runtime-pm just works.
126 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
127 i915_gem_restore_gtt_mappings(i915);
128 i915_gem_restore_fences(&i915->ggtt);
130 i915_gem_resume(i915);
134 static int igt_gem_suspend(void *arg)
136 struct drm_i915_private *i915 = arg;
137 struct i915_gem_context *ctx;
138 struct file *file;
139 int err;
141 file = mock_file(i915);
142 if (IS_ERR(file))
143 return PTR_ERR(file);
145 err = -ENOMEM;
146 ctx = live_context(i915, file);
147 if (!IS_ERR(ctx))
148 err = switch_to_context(ctx);
149 if (err)
150 goto out;
152 err = pm_prepare(i915);
153 if (err)
154 goto out;
156 pm_suspend(i915);
158 /* Here be dragons! Note that with S3RST any S3 may become S4! */
159 simulate_hibernate(i915);
161 pm_resume(i915);
163 err = switch_to_context(ctx);
164 out:
165 fput(file);
166 return err;
169 static int igt_gem_hibernate(void *arg)
171 struct drm_i915_private *i915 = arg;
172 struct i915_gem_context *ctx;
173 struct file *file;
174 int err;
176 file = mock_file(i915);
177 if (IS_ERR(file))
178 return PTR_ERR(file);
180 err = -ENOMEM;
181 ctx = live_context(i915, file);
182 if (!IS_ERR(ctx))
183 err = switch_to_context(ctx);
184 if (err)
185 goto out;
187 err = pm_prepare(i915);
188 if (err)
189 goto out;
191 pm_hibernate(i915);
193 /* Here be dragons! */
194 simulate_hibernate(i915);
196 pm_resume(i915);
198 err = switch_to_context(ctx);
199 out:
200 fput(file);
201 return err;
204 int i915_gem_live_selftests(struct drm_i915_private *i915)
206 static const struct i915_subtest tests[] = {
207 SUBTEST(igt_gem_suspend),
208 SUBTEST(igt_gem_hibernate),
211 if (intel_gt_is_wedged(&i915->gt))
212 return 0;
214 return i915_live_subtests(tests, i915);