Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | /* * SPDX-License-Identifier: MIT * * Copyright © 2016 Intel Corporation */ #include "i915_drv.h" #include "i915_selftest.h" #include "mock_dmabuf.h" #include "selftests/mock_gem_device.h" static int igt_dmabuf_export(void *arg) { struct drm_i915_private *i915 = arg; struct drm_i915_gem_object *obj; struct dma_buf *dmabuf; obj = i915_gem_object_create_shmem(i915, PAGE_SIZE); if (IS_ERR(obj)) return PTR_ERR(obj); dmabuf = i915_gem_prime_export(&obj->base, 0); i915_gem_object_put(obj); if (IS_ERR(dmabuf)) { pr_err("i915_gem_prime_export failed with err=%d\n", (int)PTR_ERR(dmabuf)); return PTR_ERR(dmabuf); } dma_buf_put(dmabuf); return 0; } static int igt_dmabuf_import_self(void *arg) { struct drm_i915_private *i915 = arg; struct drm_i915_gem_object *obj; struct drm_gem_object *import; struct dma_buf *dmabuf; int err; obj = i915_gem_object_create_shmem(i915, PAGE_SIZE); if (IS_ERR(obj)) return PTR_ERR(obj); dmabuf = i915_gem_prime_export(&obj->base, 0); if (IS_ERR(dmabuf)) { pr_err("i915_gem_prime_export failed with err=%d\n", (int)PTR_ERR(dmabuf)); err = PTR_ERR(dmabuf); goto out; } import = i915_gem_prime_import(&i915->drm, dmabuf); if (IS_ERR(import)) { pr_err("i915_gem_prime_import failed with err=%d\n", (int)PTR_ERR(import)); err = PTR_ERR(import); goto out_dmabuf; } if (import != &obj->base) { pr_err("i915_gem_prime_import created a new object!\n"); err = -EINVAL; goto out_import; } err = 0; out_import: i915_gem_object_put(to_intel_bo(import)); out_dmabuf: dma_buf_put(dmabuf); out: i915_gem_object_put(obj); return err; } static int igt_dmabuf_import(void *arg) { struct drm_i915_private *i915 = arg; struct drm_i915_gem_object *obj; struct dma_buf *dmabuf; void *obj_map, *dma_map; u32 pattern[] = { 0, 0xaa, 0xcc, 0x55, 0xff }; int err, i; dmabuf = mock_dmabuf(1); if (IS_ERR(dmabuf)) return PTR_ERR(dmabuf); obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf)); if (IS_ERR(obj)) { pr_err("i915_gem_prime_import failed with err=%d\n", (int)PTR_ERR(obj)); err = PTR_ERR(obj); goto out_dmabuf; } if (obj->base.dev != &i915->drm) { pr_err("i915_gem_prime_import created a non-i915 object!\n"); err = -EINVAL; goto out_obj; } if (obj->base.size != PAGE_SIZE) { pr_err("i915_gem_prime_import is wrong size found %lld, expected %ld\n", (long long)obj->base.size, PAGE_SIZE); err = -EINVAL; goto out_obj; } dma_map = dma_buf_vmap(dmabuf); if (!dma_map) { pr_err("dma_buf_vmap failed\n"); err = -ENOMEM; goto out_obj; } if (0) { /* Can not yet map dmabuf */ obj_map = i915_gem_object_pin_map(obj, I915_MAP_WB); if (IS_ERR(obj_map)) { err = PTR_ERR(obj_map); pr_err("i915_gem_object_pin_map failed with err=%d\n", err); goto out_dma_map; } for (i = 0; i < ARRAY_SIZE(pattern); i++) { memset(dma_map, pattern[i], PAGE_SIZE); if (memchr_inv(obj_map, pattern[i], PAGE_SIZE)) { err = -EINVAL; pr_err("imported vmap not all set to %x!\n", pattern[i]); i915_gem_object_unpin_map(obj); goto out_dma_map; } } for (i = 0; i < ARRAY_SIZE(pattern); i++) { memset(obj_map, pattern[i], PAGE_SIZE); if (memchr_inv(dma_map, pattern[i], PAGE_SIZE)) { err = -EINVAL; pr_err("exported vmap not all set to %x!\n", pattern[i]); i915_gem_object_unpin_map(obj); goto out_dma_map; } } i915_gem_object_unpin_map(obj); } err = 0; out_dma_map: dma_buf_vunmap(dmabuf, dma_map); out_obj: i915_gem_object_put(obj); out_dmabuf: dma_buf_put(dmabuf); return err; } static int igt_dmabuf_import_ownership(void *arg) { struct drm_i915_private *i915 = arg; struct drm_i915_gem_object *obj; struct dma_buf *dmabuf; void *ptr; int err; dmabuf = mock_dmabuf(1); if (IS_ERR(dmabuf)) return PTR_ERR(dmabuf); ptr = dma_buf_vmap(dmabuf); if (!ptr) { pr_err("dma_buf_vmap failed\n"); err = -ENOMEM; goto err_dmabuf; } memset(ptr, 0xc5, PAGE_SIZE); dma_buf_vunmap(dmabuf, ptr); obj = to_intel_bo(i915_gem_prime_import(&i915->drm, dmabuf)); if (IS_ERR(obj)) { pr_err("i915_gem_prime_import failed with err=%d\n", (int)PTR_ERR(obj)); err = PTR_ERR(obj); goto err_dmabuf; } dma_buf_put(dmabuf); err = i915_gem_object_pin_pages(obj); if (err) { pr_err("i915_gem_object_pin_pages failed with err=%d\n", err); goto out_obj; } err = 0; i915_gem_object_unpin_pages(obj); out_obj: i915_gem_object_put(obj); return err; err_dmabuf: dma_buf_put(dmabuf); return err; } static int igt_dmabuf_export_vmap(void *arg) { struct drm_i915_private *i915 = arg; struct drm_i915_gem_object *obj; struct dma_buf *dmabuf; void *ptr; int err; obj = i915_gem_object_create_shmem(i915, PAGE_SIZE); if (IS_ERR(obj)) return PTR_ERR(obj); dmabuf = i915_gem_prime_export(&obj->base, 0); if (IS_ERR(dmabuf)) { pr_err("i915_gem_prime_export failed with err=%d\n", (int)PTR_ERR(dmabuf)); err = PTR_ERR(dmabuf); goto err_obj; } i915_gem_object_put(obj); ptr = dma_buf_vmap(dmabuf); if (!ptr) { pr_err("dma_buf_vmap failed\n"); err = -ENOMEM; goto out; } if (memchr_inv(ptr, 0, dmabuf->size)) { pr_err("Exported object not initialiased to zero!\n"); err = -EINVAL; goto out; } memset(ptr, 0xc5, dmabuf->size); err = 0; dma_buf_vunmap(dmabuf, ptr); out: dma_buf_put(dmabuf); return err; err_obj: i915_gem_object_put(obj); return err; } static int igt_dmabuf_export_kmap(void *arg) { struct drm_i915_private *i915 = arg; struct drm_i915_gem_object *obj; struct dma_buf *dmabuf; void *ptr; int err; obj = i915_gem_object_create_shmem(i915, 2 * PAGE_SIZE); if (IS_ERR(obj)) return PTR_ERR(obj); dmabuf = i915_gem_prime_export(&obj->base, 0); i915_gem_object_put(obj); if (IS_ERR(dmabuf)) { err = PTR_ERR(dmabuf); pr_err("i915_gem_prime_export failed with err=%d\n", err); return err; } ptr = dma_buf_kmap(dmabuf, 0); if (!ptr) { pr_err("dma_buf_kmap failed\n"); err = -ENOMEM; goto err; } if (memchr_inv(ptr, 0, PAGE_SIZE)) { dma_buf_kunmap(dmabuf, 0, ptr); pr_err("Exported page[0] not initialiased to zero!\n"); err = -EINVAL; goto err; } memset(ptr, 0xc5, PAGE_SIZE); dma_buf_kunmap(dmabuf, 0, ptr); ptr = i915_gem_object_pin_map(obj, I915_MAP_WB); if (IS_ERR(ptr)) { err = PTR_ERR(ptr); pr_err("i915_gem_object_pin_map failed with err=%d\n", err); goto err; } memset(ptr + PAGE_SIZE, 0xaa, PAGE_SIZE); i915_gem_object_flush_map(obj); i915_gem_object_unpin_map(obj); ptr = dma_buf_kmap(dmabuf, 1); if (!ptr) { pr_err("dma_buf_kmap failed\n"); err = -ENOMEM; goto err; } if (memchr_inv(ptr, 0xaa, PAGE_SIZE)) { dma_buf_kunmap(dmabuf, 1, ptr); pr_err("Exported page[1] not set to 0xaa!\n"); err = -EINVAL; goto err; } memset(ptr, 0xc5, PAGE_SIZE); dma_buf_kunmap(dmabuf, 1, ptr); ptr = dma_buf_kmap(dmabuf, 0); if (!ptr) { pr_err("dma_buf_kmap failed\n"); err = -ENOMEM; goto err; } if (memchr_inv(ptr, 0xc5, PAGE_SIZE)) { dma_buf_kunmap(dmabuf, 0, ptr); pr_err("Exported page[0] did not retain 0xc5!\n"); err = -EINVAL; goto err; } dma_buf_kunmap(dmabuf, 0, ptr); ptr = dma_buf_kmap(dmabuf, 2); if (ptr) { pr_err("Erroneously kmapped beyond the end of the object!\n"); dma_buf_kunmap(dmabuf, 2, ptr); err = -EINVAL; goto err; } ptr = dma_buf_kmap(dmabuf, -1); if (ptr) { pr_err("Erroneously kmapped before the start of the object!\n"); dma_buf_kunmap(dmabuf, -1, ptr); err = -EINVAL; goto err; } err = 0; err: dma_buf_put(dmabuf); return err; } int i915_gem_dmabuf_mock_selftests(void) { static const struct i915_subtest tests[] = { SUBTEST(igt_dmabuf_export), SUBTEST(igt_dmabuf_import_self), SUBTEST(igt_dmabuf_import), SUBTEST(igt_dmabuf_import_ownership), SUBTEST(igt_dmabuf_export_vmap), SUBTEST(igt_dmabuf_export_kmap), }; struct drm_i915_private *i915; int err; i915 = mock_gem_device(); if (!i915) return -ENOMEM; err = i915_subtests(tests, i915); drm_dev_put(&i915->drm); return err; } int i915_gem_dmabuf_live_selftests(struct drm_i915_private *i915) { static const struct i915_subtest tests[] = { SUBTEST(igt_dmabuf_export), }; return i915_subtests(tests, i915); } |