1 /*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26 /*
27 * Authors:
28 * Christian König <deathsimple@vodafone.de>
29 */
30
31 #include <linux/sort.h>
32 #include <linux/uaccess.h>
33
34 #include "amdgpu.h"
35 #include "amdgpu_trace.h"
36
37 #define AMDGPU_BO_LIST_MAX_PRIORITY 32u
38 #define AMDGPU_BO_LIST_NUM_BUCKETS (AMDGPU_BO_LIST_MAX_PRIORITY + 1)
39
amdgpu_bo_list_free_rcu(struct rcu_head * rcu)40 static void amdgpu_bo_list_free_rcu(struct rcu_head *rcu)
41 {
42 struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list,
43 rhead);
44 mutex_destroy(&list->bo_list_mutex);
45 kvfree(list);
46 }
47
amdgpu_bo_list_free(struct kref * ref)48 static void amdgpu_bo_list_free(struct kref *ref)
49 {
50 struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,
51 refcount);
52 struct amdgpu_bo_list_entry *e;
53
54 amdgpu_bo_list_for_each_entry(e, list)
55 amdgpu_bo_unref(&e->bo);
56 call_rcu(&list->rhead, amdgpu_bo_list_free_rcu);
57 }
58
amdgpu_bo_list_entry_cmp(const void * _a,const void * _b)59 static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
60 {
61 const struct amdgpu_bo_list_entry *a = _a, *b = _b;
62
63 if (a->priority > b->priority)
64 return 1;
65 if (a->priority < b->priority)
66 return -1;
67 return 0;
68 }
69
amdgpu_bo_list_create(struct amdgpu_device * adev,struct drm_file * filp,struct drm_amdgpu_bo_list_entry * info,size_t num_entries,struct amdgpu_bo_list ** result)70 int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
71 struct drm_amdgpu_bo_list_entry *info,
72 size_t num_entries, struct amdgpu_bo_list **result)
73 {
74 unsigned last_entry = 0, first_userptr = num_entries;
75 struct amdgpu_bo_list_entry *array;
76 struct amdgpu_bo_list *list;
77 uint64_t total_size = 0;
78 size_t size;
79 unsigned i;
80 int r;
81
82 if (num_entries > (SIZE_MAX - sizeof(struct amdgpu_bo_list))
83 / sizeof(struct amdgpu_bo_list_entry))
84 return -EINVAL;
85
86 size = sizeof(struct amdgpu_bo_list);
87 size += num_entries * sizeof(struct amdgpu_bo_list_entry);
88 list = kvmalloc(size, GFP_KERNEL);
89 if (!list)
90 return -ENOMEM;
91
92 kref_init(&list->refcount);
93 list->gds_obj = NULL;
94 list->gws_obj = NULL;
95 list->oa_obj = NULL;
96
97 array = amdgpu_bo_list_array_entry(list, 0);
98 memset(array, 0, num_entries * sizeof(struct amdgpu_bo_list_entry));
99
100 for (i = 0; i < num_entries; ++i) {
101 struct amdgpu_bo_list_entry *entry;
102 struct drm_gem_object *gobj;
103 struct amdgpu_bo *bo;
104 struct mm_struct *usermm;
105
106 gobj = drm_gem_object_lookup(filp, info[i].bo_handle);
107 if (!gobj) {
108 r = -ENOENT;
109 goto error_free;
110 }
111
112 bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
113 drm_gem_object_put(gobj);
114
115 #ifdef notyet
116 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
117 if (usermm) {
118 if (usermm != current->mm) {
119 amdgpu_bo_unref(&bo);
120 r = -EPERM;
121 goto error_free;
122 }
123 entry = &array[--first_userptr];
124 } else {
125 entry = &array[last_entry++];
126 }
127 #else
128 entry = &array[last_entry++];
129 #endif
130
131 entry->priority = min(info[i].bo_priority,
132 AMDGPU_BO_LIST_MAX_PRIORITY);
133 entry->bo = bo;
134
135 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
136 list->gds_obj = bo;
137 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GWS)
138 list->gws_obj = bo;
139 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_OA)
140 list->oa_obj = bo;
141
142 total_size += amdgpu_bo_size(bo);
143 trace_amdgpu_bo_list_set(list, bo);
144 }
145
146 list->first_userptr = first_userptr;
147 list->num_entries = num_entries;
148 sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
149 amdgpu_bo_list_entry_cmp, NULL);
150
151 trace_amdgpu_cs_bo_status(list->num_entries, total_size);
152
153 rw_init(&list->bo_list_mutex, "agbl");
154 *result = list;
155 return 0;
156
157 error_free:
158 for (i = 0; i < last_entry; ++i)
159 amdgpu_bo_unref(&array[i].bo);
160 for (i = first_userptr; i < num_entries; ++i)
161 amdgpu_bo_unref(&array[i].bo);
162 kvfree(list);
163 return r;
164
165 }
166
amdgpu_bo_list_destroy(struct amdgpu_fpriv * fpriv,int id)167 static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
168 {
169 struct amdgpu_bo_list *list;
170
171 mutex_lock(&fpriv->bo_list_lock);
172 list = idr_remove(&fpriv->bo_list_handles, id);
173 mutex_unlock(&fpriv->bo_list_lock);
174 if (list)
175 kref_put(&list->refcount, amdgpu_bo_list_free);
176 }
177
amdgpu_bo_list_get(struct amdgpu_fpriv * fpriv,int id,struct amdgpu_bo_list ** result)178 int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
179 struct amdgpu_bo_list **result)
180 {
181 rcu_read_lock();
182 *result = idr_find(&fpriv->bo_list_handles, id);
183
184 if (*result && kref_get_unless_zero(&(*result)->refcount)) {
185 rcu_read_unlock();
186 return 0;
187 }
188
189 rcu_read_unlock();
190 *result = NULL;
191 return -ENOENT;
192 }
193
amdgpu_bo_list_put(struct amdgpu_bo_list * list)194 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
195 {
196 kref_put(&list->refcount, amdgpu_bo_list_free);
197 }
198
amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in * in,struct drm_amdgpu_bo_list_entry ** info_param)199 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
200 struct drm_amdgpu_bo_list_entry **info_param)
201 {
202 const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
203 const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
204 struct drm_amdgpu_bo_list_entry *info;
205 int r;
206
207 info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
208 if (!info)
209 return -ENOMEM;
210
211 /* copy the handle array from userspace to a kernel buffer */
212 r = -EFAULT;
213 if (likely(info_size == in->bo_info_size)) {
214 unsigned long bytes = in->bo_number *
215 in->bo_info_size;
216
217 if (copy_from_user(info, uptr, bytes))
218 goto error_free;
219
220 } else {
221 unsigned long bytes = min(in->bo_info_size, info_size);
222 unsigned i;
223
224 memset(info, 0, in->bo_number * info_size);
225 for (i = 0; i < in->bo_number; ++i) {
226 if (copy_from_user(&info[i], uptr, bytes))
227 goto error_free;
228
229 uptr += in->bo_info_size;
230 }
231 }
232
233 *info_param = info;
234 return 0;
235
236 error_free:
237 kvfree(info);
238 return r;
239 }
240
amdgpu_bo_list_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)241 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
242 struct drm_file *filp)
243 {
244 struct amdgpu_device *adev = drm_to_adev(dev);
245 struct amdgpu_fpriv *fpriv = filp->driver_priv;
246 union drm_amdgpu_bo_list *args = data;
247 uint32_t handle = args->in.list_handle;
248 struct drm_amdgpu_bo_list_entry *info = NULL;
249 struct amdgpu_bo_list *list, *old;
250 int r;
251
252 r = amdgpu_bo_create_list_entry_array(&args->in, &info);
253 if (r)
254 return r;
255
256 switch (args->in.operation) {
257 case AMDGPU_BO_LIST_OP_CREATE:
258 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
259 &list);
260 if (r)
261 goto error_free;
262
263 mutex_lock(&fpriv->bo_list_lock);
264 r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
265 mutex_unlock(&fpriv->bo_list_lock);
266 if (r < 0) {
267 goto error_put_list;
268 }
269
270 handle = r;
271 break;
272
273 case AMDGPU_BO_LIST_OP_DESTROY:
274 amdgpu_bo_list_destroy(fpriv, handle);
275 handle = 0;
276 break;
277
278 case AMDGPU_BO_LIST_OP_UPDATE:
279 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
280 &list);
281 if (r)
282 goto error_free;
283
284 mutex_lock(&fpriv->bo_list_lock);
285 old = idr_replace(&fpriv->bo_list_handles, list, handle);
286 mutex_unlock(&fpriv->bo_list_lock);
287
288 if (IS_ERR(old)) {
289 r = PTR_ERR(old);
290 goto error_put_list;
291 }
292
293 amdgpu_bo_list_put(old);
294 break;
295
296 default:
297 r = -EINVAL;
298 goto error_free;
299 }
300
301 memset(args, 0, sizeof(*args));
302 args->out.list_handle = handle;
303 kvfree(info);
304
305 return 0;
306
307 error_put_list:
308 amdgpu_bo_list_put(list);
309
310 error_free:
311 kvfree(info);
312 return r;
313 }
314