blob: 501becc47c0cd65291e84f5fad2b77600ae3fbc9 [file] [log] [blame]
Chris Wilson0daf0112017-02-13 17:15:19 +00001/*
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
Chris Wilsond1b48c12017-08-16 09:52:08 +010043 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
44 INIT_LIST_HEAD(&ctx->handles_list);
Chris Wilson4ff4b442017-06-16 15:05:16 +010045
Chris Wilson829a0af2017-06-20 12:05:45 +010046 ret = ida_simple_get(&i915->contexts.hw_ida,
Chris Wilson0daf0112017-02-13 17:15:19 +000047 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
48 if (ret < 0)
Chris Wilsond1b48c12017-08-16 09:52:08 +010049 goto err_handles;
Chris Wilson0daf0112017-02-13 17:15:19 +000050 ctx->hw_id = ret;
51
52 if (name) {
53 ctx->name = kstrdup(name, GFP_KERNEL);
54 if (!ctx->name)
55 goto err_put;
56
57 ctx->ppgtt = mock_ppgtt(i915, name);
58 if (!ctx->ppgtt)
59 goto err_put;
60 }
61
62 return ctx;
63
Chris Wilsond1b48c12017-08-16 09:52:08 +010064err_handles:
Chris Wilson0daf0112017-02-13 17:15:19 +000065 kfree(ctx);
66 return NULL;
67
68err_put:
69 i915_gem_context_set_closed(ctx);
70 i915_gem_context_put(ctx);
71 return NULL;
72}
73
74void mock_context_close(struct i915_gem_context *ctx)
75{
Chris Wilson9c1477e2017-10-12 13:57:26 +010076 context_close(ctx);
Chris Wilson0daf0112017-02-13 17:15:19 +000077}
Chris Wilson5f09a9c2017-06-20 12:05:46 +010078
79void mock_init_contexts(struct drm_i915_private *i915)
80{
81 INIT_LIST_HEAD(&i915->contexts.list);
82 ida_init(&i915->contexts.hw_ida);
83
84 INIT_WORK(&i915->contexts.free_work, contexts_free_worker);
85 init_llist_head(&i915->contexts.free_list);
86}
Chris Wilson79f0f472017-07-21 13:32:34 +010087
88struct i915_gem_context *
89live_context(struct drm_i915_private *i915, struct drm_file *file)
90{
91 lockdep_assert_held(&i915->drm.struct_mutex);
92
93 return i915_gem_create_context(i915, file->driver_priv);
94}
Chris Wilson8ec21a72018-02-05 15:24:29 +000095
96struct i915_gem_context *
97kernel_context(struct drm_i915_private *i915)
98{
99 return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
100}
101
102void kernel_context_close(struct i915_gem_context *ctx)
103{
104 context_close(ctx);
105}