]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c
Merge tag 'for-6.6-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_bo_list.c
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 <[email protected]>
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
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
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
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
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                 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
116                 if (usermm) {
117                         if (usermm != current->mm) {
118                                 amdgpu_bo_unref(&bo);
119                                 r = -EPERM;
120                                 goto error_free;
121                         }
122                         entry = &array[--first_userptr];
123                 } else {
124                         entry = &array[last_entry++];
125                 }
126
127                 entry->priority = min(info[i].bo_priority,
128                                       AMDGPU_BO_LIST_MAX_PRIORITY);
129                 entry->bo = bo;
130
131                 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
132                         list->gds_obj = bo;
133                 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GWS)
134                         list->gws_obj = bo;
135                 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_OA)
136                         list->oa_obj = bo;
137
138                 total_size += amdgpu_bo_size(bo);
139                 trace_amdgpu_bo_list_set(list, bo);
140         }
141
142         list->first_userptr = first_userptr;
143         list->num_entries = num_entries;
144         sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
145              amdgpu_bo_list_entry_cmp, NULL);
146
147         trace_amdgpu_cs_bo_status(list->num_entries, total_size);
148
149         mutex_init(&list->bo_list_mutex);
150         *result = list;
151         return 0;
152
153 error_free:
154         for (i = 0; i < last_entry; ++i)
155                 amdgpu_bo_unref(&array[i].bo);
156         for (i = first_userptr; i < num_entries; ++i)
157                 amdgpu_bo_unref(&array[i].bo);
158         kvfree(list);
159         return r;
160
161 }
162
163 static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
164 {
165         struct amdgpu_bo_list *list;
166
167         mutex_lock(&fpriv->bo_list_lock);
168         list = idr_remove(&fpriv->bo_list_handles, id);
169         mutex_unlock(&fpriv->bo_list_lock);
170         if (list)
171                 kref_put(&list->refcount, amdgpu_bo_list_free);
172 }
173
174 int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
175                        struct amdgpu_bo_list **result)
176 {
177         rcu_read_lock();
178         *result = idr_find(&fpriv->bo_list_handles, id);
179
180         if (*result && kref_get_unless_zero(&(*result)->refcount)) {
181                 rcu_read_unlock();
182                 return 0;
183         }
184
185         rcu_read_unlock();
186         return -ENOENT;
187 }
188
189 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
190 {
191         kref_put(&list->refcount, amdgpu_bo_list_free);
192 }
193
194 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
195                                       struct drm_amdgpu_bo_list_entry **info_param)
196 {
197         const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
198         const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
199         struct drm_amdgpu_bo_list_entry *info;
200         int r;
201
202         info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
203         if (!info)
204                 return -ENOMEM;
205
206         /* copy the handle array from userspace to a kernel buffer */
207         r = -EFAULT;
208         if (likely(info_size == in->bo_info_size)) {
209                 unsigned long bytes = in->bo_number *
210                         in->bo_info_size;
211
212                 if (copy_from_user(info, uptr, bytes))
213                         goto error_free;
214
215         } else {
216                 unsigned long bytes = min(in->bo_info_size, info_size);
217                 unsigned i;
218
219                 memset(info, 0, in->bo_number * info_size);
220                 for (i = 0; i < in->bo_number; ++i) {
221                         if (copy_from_user(&info[i], uptr, bytes))
222                                 goto error_free;
223
224                         uptr += in->bo_info_size;
225                 }
226         }
227
228         *info_param = info;
229         return 0;
230
231 error_free:
232         kvfree(info);
233         return r;
234 }
235
236 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
237                                 struct drm_file *filp)
238 {
239         struct amdgpu_device *adev = drm_to_adev(dev);
240         struct amdgpu_fpriv *fpriv = filp->driver_priv;
241         union drm_amdgpu_bo_list *args = data;
242         uint32_t handle = args->in.list_handle;
243         struct drm_amdgpu_bo_list_entry *info = NULL;
244         struct amdgpu_bo_list *list, *old;
245         int r;
246
247         r = amdgpu_bo_create_list_entry_array(&args->in, &info);
248         if (r)
249                 return r;
250
251         switch (args->in.operation) {
252         case AMDGPU_BO_LIST_OP_CREATE:
253                 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
254                                           &list);
255                 if (r)
256                         goto error_free;
257
258                 mutex_lock(&fpriv->bo_list_lock);
259                 r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
260                 mutex_unlock(&fpriv->bo_list_lock);
261                 if (r < 0) {
262                         goto error_put_list;
263                 }
264
265                 handle = r;
266                 break;
267
268         case AMDGPU_BO_LIST_OP_DESTROY:
269                 amdgpu_bo_list_destroy(fpriv, handle);
270                 handle = 0;
271                 break;
272
273         case AMDGPU_BO_LIST_OP_UPDATE:
274                 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
275                                           &list);
276                 if (r)
277                         goto error_free;
278
279                 mutex_lock(&fpriv->bo_list_lock);
280                 old = idr_replace(&fpriv->bo_list_handles, list, handle);
281                 mutex_unlock(&fpriv->bo_list_lock);
282
283                 if (IS_ERR(old)) {
284                         r = PTR_ERR(old);
285                         goto error_put_list;
286                 }
287
288                 amdgpu_bo_list_put(old);
289                 break;
290
291         default:
292                 r = -EINVAL;
293                 goto error_free;
294         }
295
296         memset(args, 0, sizeof(*args));
297         args->out.list_handle = handle;
298         kvfree(info);
299
300         return 0;
301
302 error_put_list:
303         amdgpu_bo_list_put(list);
304
305 error_free:
306         kvfree(info);
307         return r;
308 }
This page took 0.047932 seconds and 4 git commands to generate.