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 unsigned i;
79 int r;
80
81 list = kvzalloc(struct_size(list, entries, num_entries), GFP_KERNEL);
82 if (!list)
83 return -ENOMEM;
84
85 kref_init(&list->refcount);
86
87 list->num_entries = num_entries;
88 array = list->entries;
89
90 for (i = 0; i < num_entries; ++i) {
91 struct amdgpu_bo_list_entry *entry;
92 struct drm_gem_object *gobj;
93 struct amdgpu_bo *bo;
94 struct mm_struct *usermm;
95
96 gobj = drm_gem_object_lookup(filp, info[i].bo_handle);
97 if (!gobj) {
98 r = -ENOENT;
99 goto error_free;
100 }
101
102 bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
103 drm_gem_object_put(gobj);
104
105 #ifdef notyet
106 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
107 if (usermm) {
108 if (usermm != current->mm) {
109 amdgpu_bo_unref(&bo);
110 r = -EPERM;
111 goto error_free;
112 }
113 entry = &array[--first_userptr];
114 } else {
115 entry = &array[last_entry++];
116 }
117 #else
118 entry = &array[last_entry++];
119 #endif
120
121 entry->priority = min(info[i].bo_priority,
122 AMDGPU_BO_LIST_MAX_PRIORITY);
123 entry->bo = bo;
124
125 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
126 list->gds_obj = bo;
127 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GWS)
128 list->gws_obj = bo;
129 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_OA)
130 list->oa_obj = bo;
131
132 total_size += amdgpu_bo_size(bo);
133 trace_amdgpu_bo_list_set(list, bo);
134 }
135
136 list->first_userptr = first_userptr;
137 sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
138 amdgpu_bo_list_entry_cmp, NULL);
139
140 trace_amdgpu_cs_bo_status(list->num_entries, total_size);
141
142 rw_init(&list->bo_list_mutex, "agbl");
143 *result = list;
144 return 0;
145
146 error_free:
147 for (i = 0; i < last_entry; ++i)
148 amdgpu_bo_unref(&array[i].bo);
149 for (i = first_userptr; i < num_entries; ++i)
150 amdgpu_bo_unref(&array[i].bo);
151 kvfree(list);
152 return r;
153
154 }
155
amdgpu_bo_list_destroy(struct amdgpu_fpriv * fpriv,int id)156 static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
157 {
158 struct amdgpu_bo_list *list;
159
160 mutex_lock(&fpriv->bo_list_lock);
161 list = idr_remove(&fpriv->bo_list_handles, id);
162 mutex_unlock(&fpriv->bo_list_lock);
163 if (list)
164 kref_put(&list->refcount, amdgpu_bo_list_free);
165 }
166
amdgpu_bo_list_get(struct amdgpu_fpriv * fpriv,int id,struct amdgpu_bo_list ** result)167 int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
168 struct amdgpu_bo_list **result)
169 {
170 rcu_read_lock();
171 *result = idr_find(&fpriv->bo_list_handles, id);
172
173 if (*result && kref_get_unless_zero(&(*result)->refcount)) {
174 rcu_read_unlock();
175 return 0;
176 }
177
178 rcu_read_unlock();
179 *result = NULL;
180 return -ENOENT;
181 }
182
amdgpu_bo_list_put(struct amdgpu_bo_list * list)183 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
184 {
185 kref_put(&list->refcount, amdgpu_bo_list_free);
186 }
187
amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in * in,struct drm_amdgpu_bo_list_entry ** info_param)188 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
189 struct drm_amdgpu_bo_list_entry **info_param)
190 {
191 const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
192 const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
193 struct drm_amdgpu_bo_list_entry *info;
194 int r;
195
196 info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
197 if (!info)
198 return -ENOMEM;
199
200 /* copy the handle array from userspace to a kernel buffer */
201 r = -EFAULT;
202 if (likely(info_size == in->bo_info_size)) {
203 unsigned long bytes = in->bo_number *
204 in->bo_info_size;
205
206 if (copy_from_user(info, uptr, bytes))
207 goto error_free;
208
209 } else {
210 unsigned long bytes = min(in->bo_info_size, info_size);
211 unsigned i;
212
213 memset(info, 0, in->bo_number * info_size);
214 for (i = 0; i < in->bo_number; ++i) {
215 if (copy_from_user(&info[i], uptr, bytes))
216 goto error_free;
217
218 uptr += in->bo_info_size;
219 }
220 }
221
222 *info_param = info;
223 return 0;
224
225 error_free:
226 kvfree(info);
227 return r;
228 }
229
amdgpu_bo_list_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)230 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
231 struct drm_file *filp)
232 {
233 struct amdgpu_device *adev = drm_to_adev(dev);
234 struct amdgpu_fpriv *fpriv = filp->driver_priv;
235 union drm_amdgpu_bo_list *args = data;
236 uint32_t handle = args->in.list_handle;
237 struct drm_amdgpu_bo_list_entry *info = NULL;
238 struct amdgpu_bo_list *list, *old;
239 int r;
240
241 r = amdgpu_bo_create_list_entry_array(&args->in, &info);
242 if (r)
243 return r;
244
245 switch (args->in.operation) {
246 case AMDGPU_BO_LIST_OP_CREATE:
247 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
248 &list);
249 if (r)
250 goto error_free;
251
252 mutex_lock(&fpriv->bo_list_lock);
253 r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
254 mutex_unlock(&fpriv->bo_list_lock);
255 if (r < 0) {
256 goto error_put_list;
257 }
258
259 handle = r;
260 break;
261
262 case AMDGPU_BO_LIST_OP_DESTROY:
263 amdgpu_bo_list_destroy(fpriv, handle);
264 handle = 0;
265 break;
266
267 case AMDGPU_BO_LIST_OP_UPDATE:
268 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
269 &list);
270 if (r)
271 goto error_free;
272
273 mutex_lock(&fpriv->bo_list_lock);
274 old = idr_replace(&fpriv->bo_list_handles, list, handle);
275 mutex_unlock(&fpriv->bo_list_lock);
276
277 if (IS_ERR(old)) {
278 r = PTR_ERR(old);
279 goto error_put_list;
280 }
281
282 amdgpu_bo_list_put(old);
283 break;
284
285 default:
286 r = -EINVAL;
287 goto error_free;
288 }
289
290 memset(args, 0, sizeof(*args));
291 args->out.list_handle = handle;
292 kvfree(info);
293
294 return 0;
295
296 error_put_list:
297 amdgpu_bo_list_put(list);
298
299 error_free:
300 kvfree(info);
301 return r;
302 }
303