Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +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 "mock_context.h" |
| 26 | #include "mock_gtt.h" |
| 27 | |
| 28 | struct i915_gem_context * |
| 29 | mock_context(struct drm_i915_private *i915, |
| 30 | const char *name) |
| 31 | { |
| 32 | struct i915_gem_context *ctx; |
| 33 | int ret; |
| 34 | |
| 35 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 36 | if (!ctx) |
| 37 | return NULL; |
| 38 | |
| 39 | kref_init(&ctx->ref); |
| 40 | INIT_LIST_HEAD(&ctx->link); |
| 41 | ctx->i915 = i915; |
| 42 | |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 43 | ctx->vma_lut.ht_bits = VMA_HT_BITS; |
| 44 | ctx->vma_lut.ht_size = BIT(VMA_HT_BITS); |
| 45 | ctx->vma_lut.ht = kcalloc(ctx->vma_lut.ht_size, |
| 46 | sizeof(*ctx->vma_lut.ht), |
| 47 | GFP_KERNEL); |
| 48 | if (!ctx->vma_lut.ht) |
| 49 | goto err_free; |
| 50 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame^] | 51 | ret = ida_simple_get(&i915->contexts.hw_ida, |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 52 | 0, MAX_CONTEXT_HW_ID, GFP_KERNEL); |
| 53 | if (ret < 0) |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 54 | goto err_vma_ht; |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 55 | ctx->hw_id = ret; |
| 56 | |
| 57 | if (name) { |
| 58 | ctx->name = kstrdup(name, GFP_KERNEL); |
| 59 | if (!ctx->name) |
| 60 | goto err_put; |
| 61 | |
| 62 | ctx->ppgtt = mock_ppgtt(i915, name); |
| 63 | if (!ctx->ppgtt) |
| 64 | goto err_put; |
| 65 | } |
| 66 | |
| 67 | return ctx; |
| 68 | |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 69 | err_vma_ht: |
| 70 | kvfree(ctx->vma_lut.ht); |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 71 | err_free: |
| 72 | kfree(ctx); |
| 73 | return NULL; |
| 74 | |
| 75 | err_put: |
| 76 | i915_gem_context_set_closed(ctx); |
| 77 | i915_gem_context_put(ctx); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | void mock_context_close(struct i915_gem_context *ctx) |
| 82 | { |
| 83 | i915_gem_context_set_closed(ctx); |
| 84 | |
| 85 | i915_ppgtt_close(&ctx->ppgtt->base); |
| 86 | |
| 87 | i915_gem_context_put(ctx); |
| 88 | } |