linux/drivers/gpu/drm/i915/selftests/mock_context.c
<<
>>
Prefs
   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
  28struct i915_gem_context *
  29mock_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
  43        ctx->hw_contexts = RB_ROOT;
  44        spin_lock_init(&ctx->hw_contexts_lock);
  45
  46        INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
  47        INIT_LIST_HEAD(&ctx->handles_list);
  48        INIT_LIST_HEAD(&ctx->hw_id_link);
  49        INIT_LIST_HEAD(&ctx->active_engines);
  50        mutex_init(&ctx->mutex);
  51
  52        ret = i915_gem_context_pin_hw_id(ctx);
  53        if (ret < 0)
  54                goto err_handles;
  55
  56        if (name) {
  57                struct i915_hw_ppgtt *ppgtt;
  58
  59                ctx->name = kstrdup(name, GFP_KERNEL);
  60                if (!ctx->name)
  61                        goto err_put;
  62
  63                ppgtt = mock_ppgtt(i915, name);
  64                if (!ppgtt)
  65                        goto err_put;
  66
  67                __set_ppgtt(ctx, ppgtt);
  68        }
  69
  70        return ctx;
  71
  72err_handles:
  73        kfree(ctx);
  74        return NULL;
  75
  76err_put:
  77        i915_gem_context_set_closed(ctx);
  78        i915_gem_context_put(ctx);
  79        return NULL;
  80}
  81
  82void mock_context_close(struct i915_gem_context *ctx)
  83{
  84        context_close(ctx);
  85}
  86
  87void mock_init_contexts(struct drm_i915_private *i915)
  88{
  89        init_contexts(i915);
  90}
  91
  92struct i915_gem_context *
  93live_context(struct drm_i915_private *i915, struct drm_file *file)
  94{
  95        struct i915_gem_context *ctx;
  96        int err;
  97
  98        lockdep_assert_held(&i915->drm.struct_mutex);
  99
 100        ctx = i915_gem_create_context(i915, 0);
 101        if (IS_ERR(ctx))
 102                return ctx;
 103
 104        err = gem_context_register(ctx, file->driver_priv);
 105        if (err < 0)
 106                goto err_ctx;
 107
 108        return ctx;
 109
 110err_ctx:
 111        context_close(ctx);
 112        return ERR_PTR(err);
 113}
 114
 115struct i915_gem_context *
 116kernel_context(struct drm_i915_private *i915)
 117{
 118        return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
 119}
 120
 121void kernel_context_close(struct i915_gem_context *ctx)
 122{
 123        context_close(ctx);
 124}
 125