blob: e63cfb7fa3900aedf93b69d3c021b54ff88f0175 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: monk liu <monk.liu@amd.com>
23 */
24
25#include <drm/drmP.h>
26#include "amdgpu.h"
27
28static void amdgpu_ctx_do_release(struct kref *ref)
29{
30 struct amdgpu_ctx *ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031
32 ctx = container_of(ref, struct amdgpu_ctx, refcount);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040033 kfree(ctx);
34}
35
Alex Deucher0b492a42015-08-16 22:48:26 -040036int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
37 uint32_t *id)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040038{
39 int r;
40 struct amdgpu_ctx *ctx;
41 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
42
43 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
44 if (!ctx)
45 return -ENOMEM;
46
Marek Olšák0147ee02015-05-05 20:52:00 +020047 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040048 r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
49 if (r < 0) {
Marek Olšák0147ee02015-05-05 20:52:00 +020050 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040051 kfree(ctx);
52 return r;
53 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -040054 *id = (uint32_t)r;
55
56 memset(ctx, 0, sizeof(*ctx));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057 kref_init(&ctx->refcount);
Marek Olšák0147ee02015-05-05 20:52:00 +020058 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059
60 return 0;
61}
62
63int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
64{
Alex Deucherd38ceaf2015-04-20 16:55:21 -040065 struct amdgpu_ctx *ctx;
66 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
67
Marek Olšák0147ee02015-05-05 20:52:00 +020068 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069 ctx = idr_find(&mgr->ctx_handles, id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040070 if (ctx) {
Alex Deucher0b492a42015-08-16 22:48:26 -040071 idr_remove(&mgr->ctx_handles, id);
Marek Olšákf11358d2015-05-05 00:56:45 +020072 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Marek Olšák0147ee02015-05-05 20:52:00 +020073 mutex_unlock(&mgr->lock);
Marek Olšákf11358d2015-05-05 00:56:45 +020074 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 }
Marek Olšák0147ee02015-05-05 20:52:00 +020076 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 return -EINVAL;
78}
79
Marek Olšákd94aed52015-05-05 21:13:49 +020080static int amdgpu_ctx_query(struct amdgpu_device *adev,
81 struct amdgpu_fpriv *fpriv, uint32_t id,
82 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083{
84 struct amdgpu_ctx *ctx;
85 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +020086 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087
Marek Olšák0147ee02015-05-05 20:52:00 +020088 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +020090 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +020091 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +020092 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093 }
Marek Olšákd94aed52015-05-05 21:13:49 +020094
95 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -040096 out->state.flags = 0x0;
97 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +020098
99 /* determine if a GPU reset has occured since the last call */
100 reset_counter = atomic_read(&adev->gpu_reset_counter);
101 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
102 if (ctx->reset_counter == reset_counter)
103 out->state.reset_status = AMDGPU_CTX_NO_RESET;
104 else
105 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
106 ctx->reset_counter = reset_counter;
107
Marek Olšák0147ee02015-05-05 20:52:00 +0200108 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200109 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110}
111
112void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
113{
114 struct idr *idp;
115 struct amdgpu_ctx *ctx;
116 uint32_t id;
117 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
118 idp = &mgr->ctx_handles;
119
120 idr_for_each_entry(idp,ctx,id) {
121 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
Alex Deucher0b492a42015-08-16 22:48:26 -0400122 DRM_ERROR("ctx %p is still alive\n", ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 }
124
Marek Olšák0147ee02015-05-05 20:52:00 +0200125 mutex_destroy(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126}
127
128int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200129 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130{
131 int r;
132 uint32_t id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133
134 union drm_amdgpu_ctx *args = data;
135 struct amdgpu_device *adev = dev->dev_private;
136 struct amdgpu_fpriv *fpriv = filp->driver_priv;
137
138 r = 0;
139 id = args->in.ctx_id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140
141 switch (args->in.op) {
142 case AMDGPU_CTX_OP_ALLOC_CTX:
Alex Deucher0b492a42015-08-16 22:48:26 -0400143 r = amdgpu_ctx_alloc(adev, fpriv, &id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 args->out.alloc.ctx_id = id;
145 break;
146 case AMDGPU_CTX_OP_FREE_CTX:
147 r = amdgpu_ctx_free(adev, fpriv, id);
148 break;
149 case AMDGPU_CTX_OP_QUERY_STATE:
Marek Olšákd94aed52015-05-05 21:13:49 +0200150 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151 break;
152 default:
153 return -EINVAL;
154 }
155
156 return r;
157}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800158
159struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
160{
161 struct amdgpu_ctx *ctx;
162 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
163
164 mutex_lock(&mgr->lock);
165 ctx = idr_find(&mgr->ctx_handles, id);
166 if (ctx)
167 kref_get(&ctx->refcount);
168 mutex_unlock(&mgr->lock);
169 return ctx;
170}
171
172int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
173{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800174 if (ctx == NULL)
175 return -EINVAL;
176
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800177 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800178 return 0;
179}