Chris Wilson | e3c7a1c | 2017-02-13 17:15:45 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2016 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/prime_numbers.h> |
| 26 | |
| 27 | #include "../i915_selftest.h" |
| 28 | |
| 29 | #include "mock_gem_device.h" |
| 30 | #include "mock_context.h" |
| 31 | |
| 32 | static bool assert_vma(struct i915_vma *vma, |
| 33 | struct drm_i915_gem_object *obj, |
| 34 | struct i915_gem_context *ctx) |
| 35 | { |
| 36 | bool ok = true; |
| 37 | |
| 38 | if (vma->vm != &ctx->ppgtt->base) { |
| 39 | pr_err("VMA created with wrong VM\n"); |
| 40 | ok = false; |
| 41 | } |
| 42 | |
| 43 | if (vma->size != obj->base.size) { |
| 44 | pr_err("VMA created with wrong size, found %llu, expected %zu\n", |
| 45 | vma->size, obj->base.size); |
| 46 | ok = false; |
| 47 | } |
| 48 | |
| 49 | if (vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) { |
| 50 | pr_err("VMA created with wrong type [%d]\n", |
| 51 | vma->ggtt_view.type); |
| 52 | ok = false; |
| 53 | } |
| 54 | |
| 55 | return ok; |
| 56 | } |
| 57 | |
| 58 | static struct i915_vma * |
| 59 | checked_vma_instance(struct drm_i915_gem_object *obj, |
| 60 | struct i915_address_space *vm, |
| 61 | struct i915_ggtt_view *view) |
| 62 | { |
| 63 | struct i915_vma *vma; |
| 64 | bool ok = true; |
| 65 | |
| 66 | vma = i915_vma_instance(obj, vm, view); |
| 67 | if (IS_ERR(vma)) |
| 68 | return vma; |
| 69 | |
| 70 | /* Manual checks, will be reinforced by i915_vma_compare! */ |
| 71 | if (vma->vm != vm) { |
| 72 | pr_err("VMA's vm [%p] does not match request [%p]\n", |
| 73 | vma->vm, vm); |
| 74 | ok = false; |
| 75 | } |
| 76 | |
| 77 | if (i915_is_ggtt(vm) != i915_vma_is_ggtt(vma)) { |
| 78 | pr_err("VMA ggtt status [%d] does not match parent [%d]\n", |
| 79 | i915_vma_is_ggtt(vma), i915_is_ggtt(vm)); |
| 80 | ok = false; |
| 81 | } |
| 82 | |
| 83 | if (i915_vma_compare(vma, vm, view)) { |
| 84 | pr_err("i915_vma_compare failed with create parmaters!\n"); |
| 85 | return ERR_PTR(-EINVAL); |
| 86 | } |
| 87 | |
| 88 | if (i915_vma_compare(vma, vma->vm, |
| 89 | i915_vma_is_ggtt(vma) ? &vma->ggtt_view : NULL)) { |
| 90 | pr_err("i915_vma_compare failed with itself\n"); |
| 91 | return ERR_PTR(-EINVAL); |
| 92 | } |
| 93 | |
| 94 | if (!ok) { |
| 95 | pr_err("i915_vma_compare failed to detect the difference!\n"); |
| 96 | return ERR_PTR(-EINVAL); |
| 97 | } |
| 98 | |
| 99 | return vma; |
| 100 | } |
| 101 | |
| 102 | static int create_vmas(struct drm_i915_private *i915, |
| 103 | struct list_head *objects, |
| 104 | struct list_head *contexts) |
| 105 | { |
| 106 | struct drm_i915_gem_object *obj; |
| 107 | struct i915_gem_context *ctx; |
| 108 | int pinned; |
| 109 | |
| 110 | list_for_each_entry(obj, objects, st_link) { |
| 111 | for (pinned = 0; pinned <= 1; pinned++) { |
| 112 | list_for_each_entry(ctx, contexts, link) { |
| 113 | struct i915_address_space *vm = |
| 114 | &ctx->ppgtt->base; |
| 115 | struct i915_vma *vma; |
| 116 | int err; |
| 117 | |
| 118 | vma = checked_vma_instance(obj, vm, NULL); |
| 119 | if (IS_ERR(vma)) |
| 120 | return PTR_ERR(vma); |
| 121 | |
| 122 | if (!assert_vma(vma, obj, ctx)) { |
| 123 | pr_err("VMA lookup/create failed\n"); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | if (!pinned) { |
| 128 | err = i915_vma_pin(vma, 0, 0, PIN_USER); |
| 129 | if (err) { |
| 130 | pr_err("Failed to pin VMA\n"); |
| 131 | return err; |
| 132 | } |
| 133 | } else { |
| 134 | i915_vma_unpin(vma); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int igt_vma_create(void *arg) |
| 144 | { |
| 145 | struct drm_i915_private *i915 = arg; |
| 146 | struct drm_i915_gem_object *obj, *on; |
| 147 | struct i915_gem_context *ctx, *cn; |
| 148 | unsigned long num_obj, num_ctx; |
| 149 | unsigned long no, nc; |
| 150 | IGT_TIMEOUT(end_time); |
| 151 | LIST_HEAD(contexts); |
| 152 | LIST_HEAD(objects); |
| 153 | int err; |
| 154 | |
| 155 | /* Exercise creating many vma amonst many objections, checking the |
| 156 | * vma creation and lookup routines. |
| 157 | */ |
| 158 | |
| 159 | no = 0; |
| 160 | for_each_prime_number(num_obj, ULONG_MAX - 1) { |
| 161 | for (; no < num_obj; no++) { |
| 162 | obj = i915_gem_object_create_internal(i915, PAGE_SIZE); |
| 163 | if (IS_ERR(obj)) |
| 164 | goto out; |
| 165 | |
| 166 | list_add(&obj->st_link, &objects); |
| 167 | } |
| 168 | |
| 169 | nc = 0; |
| 170 | for_each_prime_number(num_ctx, MAX_CONTEXT_HW_ID) { |
| 171 | for (; nc < num_ctx; nc++) { |
| 172 | ctx = mock_context(i915, "mock"); |
| 173 | if (!ctx) |
| 174 | goto out; |
| 175 | |
| 176 | list_move(&ctx->link, &contexts); |
| 177 | } |
| 178 | |
| 179 | err = create_vmas(i915, &objects, &contexts); |
| 180 | if (err) |
| 181 | goto out; |
| 182 | |
| 183 | if (igt_timeout(end_time, |
| 184 | "%s timed out: after %lu objects in %lu contexts\n", |
| 185 | __func__, no, nc)) |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | list_for_each_entry_safe(ctx, cn, &contexts, link) |
| 190 | mock_context_close(ctx); |
| 191 | } |
| 192 | |
| 193 | end: |
| 194 | /* Final pass to lookup all created contexts */ |
| 195 | err = create_vmas(i915, &objects, &contexts); |
| 196 | out: |
| 197 | list_for_each_entry_safe(ctx, cn, &contexts, link) |
| 198 | mock_context_close(ctx); |
| 199 | |
| 200 | list_for_each_entry_safe(obj, on, &objects, st_link) |
| 201 | i915_gem_object_put(obj); |
| 202 | return err; |
| 203 | } |
| 204 | |
| 205 | int i915_vma_mock_selftests(void) |
| 206 | { |
| 207 | static const struct i915_subtest tests[] = { |
| 208 | SUBTEST(igt_vma_create), |
| 209 | }; |
| 210 | struct drm_i915_private *i915; |
| 211 | int err; |
| 212 | |
| 213 | i915 = mock_gem_device(); |
| 214 | if (!i915) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | mutex_lock(&i915->drm.struct_mutex); |
| 218 | err = i915_subtests(tests, i915); |
| 219 | mutex_unlock(&i915->drm.struct_mutex); |
| 220 | |
| 221 | drm_dev_unref(&i915->drm); |
| 222 | return err; |
| 223 | } |
| 224 | |