Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | static void amdgpu_ctx_do_release(struct kref *ref) |
| 29 | { |
| 30 | struct amdgpu_ctx *ctx; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 31 | |
| 32 | ctx = container_of(ref, struct amdgpu_ctx, refcount); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 33 | kfree(ctx); |
| 34 | } |
| 35 | |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 36 | int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, |
| 37 | uint32_t *id) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 38 | { |
| 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šák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 47 | mutex_lock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 48 | r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL); |
| 49 | if (r < 0) { |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 50 | mutex_unlock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 51 | kfree(ctx); |
| 52 | return r; |
| 53 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 54 | *id = (uint32_t)r; |
| 55 | |
| 56 | memset(ctx, 0, sizeof(*ctx)); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 57 | kref_init(&ctx->refcount); |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 58 | mutex_unlock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id) |
| 64 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 65 | struct amdgpu_ctx *ctx; |
| 66 | struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; |
| 67 | |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 68 | mutex_lock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 69 | ctx = idr_find(&mgr->ctx_handles, id); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 70 | if (ctx) { |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 71 | idr_remove(&mgr->ctx_handles, id); |
Marek Olšák | f11358d | 2015-05-05 00:56:45 +0200 | [diff] [blame] | 72 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 73 | mutex_unlock(&mgr->lock); |
Marek Olšák | f11358d | 2015-05-05 00:56:45 +0200 | [diff] [blame] | 74 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 75 | } |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 76 | mutex_unlock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 77 | return -EINVAL; |
| 78 | } |
| 79 | |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 80 | static int amdgpu_ctx_query(struct amdgpu_device *adev, |
| 81 | struct amdgpu_fpriv *fpriv, uint32_t id, |
| 82 | union drm_amdgpu_ctx_out *out) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 83 | { |
| 84 | struct amdgpu_ctx *ctx; |
| 85 | struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 86 | unsigned reset_counter; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 87 | |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 88 | mutex_lock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 89 | ctx = idr_find(&mgr->ctx_handles, id); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 90 | if (!ctx) { |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 91 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 92 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 93 | } |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 94 | |
| 95 | /* TODO: these two are always zero */ |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 96 | out->state.flags = 0x0; |
| 97 | out->state.hangs = 0x0; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 98 | |
| 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šák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 108 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 109 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void 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 Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 122 | DRM_ERROR("ctx %p is still alive\n", ctx); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 125 | mutex_destroy(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 129 | struct drm_file *filp) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 130 | { |
| 131 | int r; |
| 132 | uint32_t id; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 133 | |
| 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 Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 140 | |
| 141 | switch (args->in.op) { |
| 142 | case AMDGPU_CTX_OP_ALLOC_CTX: |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 143 | r = amdgpu_ctx_alloc(adev, fpriv, &id); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 144 | 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šák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 150 | r = amdgpu_ctx_query(adev, fpriv, id, &args->out); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 151 | break; |
| 152 | default: |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
| 156 | return r; |
| 157 | } |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 158 | |
| 159 | struct 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 | |
| 172 | int amdgpu_ctx_put(struct amdgpu_ctx *ctx) |
| 173 | { |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 174 | if (ctx == NULL) |
| 175 | return -EINVAL; |
| 176 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 177 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 178 | return 0; |
| 179 | } |