|
| 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 "mock_context.h" |
| 12 | +#include "igt_flush_test.h" |
| 13 | + |
| 14 | +static int switch_to_context(struct drm_i915_private *i915, |
| 15 | + struct i915_gem_context *ctx) |
| 16 | +{ |
| 17 | + struct intel_engine_cs *engine; |
| 18 | + enum intel_engine_id id; |
| 19 | + int err = 0; |
| 20 | + |
| 21 | + intel_runtime_pm_get(i915); |
| 22 | + |
| 23 | + for_each_engine(engine, i915, id) { |
| 24 | + struct i915_request *rq; |
| 25 | + |
| 26 | + rq = i915_request_alloc(engine, ctx); |
| 27 | + if (IS_ERR(rq)) { |
| 28 | + err = PTR_ERR(rq); |
| 29 | + break; |
| 30 | + } |
| 31 | + |
| 32 | + i915_request_add(rq); |
| 33 | + } |
| 34 | + |
| 35 | + intel_runtime_pm_put(i915); |
| 36 | + |
| 37 | + return err; |
| 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 | +{ |
| 68 | + intel_runtime_pm_get(i915); |
| 69 | + |
| 70 | + /* |
| 71 | + * As a final sting in the tail, invalidate stolen. Under a real S4, |
| 72 | + * stolen is lost and needs to be refilled on resume. However, under |
| 73 | + * CI we merely do S4-device testing (as full S4 is too unreliable |
| 74 | + * for automated testing across a cluster), so to simulate the effect |
| 75 | + * of stolen being trashed across S4, we trash it ourselves. |
| 76 | + */ |
| 77 | + trash_stolen(i915); |
| 78 | + |
| 79 | + intel_runtime_pm_put(i915); |
| 80 | +} |
| 81 | + |
| 82 | +static int pm_prepare(struct drm_i915_private *i915) |
| 83 | +{ |
| 84 | + int err = 0; |
| 85 | + |
| 86 | + if (i915_gem_suspend(i915)) { |
| 87 | + pr_err("i915_gem_suspend failed\n"); |
| 88 | + err = -EINVAL; |
| 89 | + } |
| 90 | + |
| 91 | + return err; |
| 92 | +} |
| 93 | + |
| 94 | +static void pm_suspend(struct drm_i915_private *i915) |
| 95 | +{ |
| 96 | + intel_runtime_pm_get(i915); |
| 97 | + |
| 98 | + i915_gem_suspend_gtt_mappings(i915); |
| 99 | + i915_gem_suspend_late(i915); |
| 100 | + |
| 101 | + intel_runtime_pm_put(i915); |
| 102 | +} |
| 103 | + |
| 104 | +static void pm_hibernate(struct drm_i915_private *i915) |
| 105 | +{ |
| 106 | + intel_runtime_pm_get(i915); |
| 107 | + |
| 108 | + i915_gem_suspend_gtt_mappings(i915); |
| 109 | + |
| 110 | + i915_gem_freeze(i915); |
| 111 | + i915_gem_freeze_late(i915); |
| 112 | + |
| 113 | + intel_runtime_pm_put(i915); |
| 114 | +} |
| 115 | + |
| 116 | +static void pm_resume(struct drm_i915_private *i915) |
| 117 | +{ |
| 118 | + /* |
| 119 | + * Both suspend and hibernate follow the same wakeup path and assume |
| 120 | + * that runtime-pm just works. |
| 121 | + */ |
| 122 | + intel_runtime_pm_get(i915); |
| 123 | + |
| 124 | + intel_engines_sanitize(i915); |
| 125 | + i915_gem_sanitize(i915); |
| 126 | + i915_gem_resume(i915); |
| 127 | + |
| 128 | + intel_runtime_pm_put(i915); |
| 129 | +} |
| 130 | + |
| 131 | +static int igt_gem_suspend(void *arg) |
| 132 | +{ |
| 133 | + struct drm_i915_private *i915 = arg; |
| 134 | + struct i915_gem_context *ctx; |
| 135 | + struct drm_file *file; |
| 136 | + int err; |
| 137 | + |
| 138 | + file = mock_file(i915); |
| 139 | + if (IS_ERR(file)) |
| 140 | + return PTR_ERR(file); |
| 141 | + |
| 142 | + err = -ENOMEM; |
| 143 | + mutex_lock(&i915->drm.struct_mutex); |
| 144 | + ctx = live_context(i915, file); |
| 145 | + if (!IS_ERR(ctx)) |
| 146 | + err = switch_to_context(i915, ctx); |
| 147 | + mutex_unlock(&i915->drm.struct_mutex); |
| 148 | + if (err) |
| 149 | + goto out; |
| 150 | + |
| 151 | + err = pm_prepare(i915); |
| 152 | + if (err) |
| 153 | + goto out; |
| 154 | + |
| 155 | + pm_suspend(i915); |
| 156 | + |
| 157 | + /* Here be dragons! Note that with S3RST any S3 may become S4! */ |
| 158 | + simulate_hibernate(i915); |
| 159 | + |
| 160 | + pm_resume(i915); |
| 161 | + |
| 162 | + mutex_lock(&i915->drm.struct_mutex); |
| 163 | + err = switch_to_context(i915, ctx); |
| 164 | + if (igt_flush_test(i915, I915_WAIT_LOCKED)) |
| 165 | + err = -EIO; |
| 166 | + mutex_unlock(&i915->drm.struct_mutex); |
| 167 | +out: |
| 168 | + mock_file_free(i915, file); |
| 169 | + return err; |
| 170 | +} |
| 171 | + |
| 172 | +static int igt_gem_hibernate(void *arg) |
| 173 | +{ |
| 174 | + struct drm_i915_private *i915 = arg; |
| 175 | + struct i915_gem_context *ctx; |
| 176 | + struct drm_file *file; |
| 177 | + int err; |
| 178 | + |
| 179 | + file = mock_file(i915); |
| 180 | + if (IS_ERR(file)) |
| 181 | + return PTR_ERR(file); |
| 182 | + |
| 183 | + err = -ENOMEM; |
| 184 | + mutex_lock(&i915->drm.struct_mutex); |
| 185 | + ctx = live_context(i915, file); |
| 186 | + if (!IS_ERR(ctx)) |
| 187 | + err = switch_to_context(i915, ctx); |
| 188 | + mutex_unlock(&i915->drm.struct_mutex); |
| 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 | + mutex_lock(&i915->drm.struct_mutex); |
| 204 | + err = switch_to_context(i915, ctx); |
| 205 | + if (igt_flush_test(i915, I915_WAIT_LOCKED)) |
| 206 | + err = -EIO; |
| 207 | + mutex_unlock(&i915->drm.struct_mutex); |
| 208 | +out: |
| 209 | + mock_file_free(i915, file); |
| 210 | + return err; |
| 211 | +} |
| 212 | + |
| 213 | +int i915_gem_live_selftests(struct drm_i915_private *i915) |
| 214 | +{ |
| 215 | + static const struct i915_subtest tests[] = { |
| 216 | + SUBTEST(igt_gem_suspend), |
| 217 | + SUBTEST(igt_gem_hibernate), |
| 218 | + }; |
| 219 | + |
| 220 | + return i915_subtests(tests, i915); |
| 221 | +} |
0 commit comments