GNU Linux-libre 4.19.304-gnu1
[releases.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 <deathsimple@vodafone.de>
29  */
30
31 #include <drm/drmP.h>
32 #include "amdgpu.h"
33 #include "amdgpu_trace.h"
34
35 #define AMDGPU_BO_LIST_MAX_PRIORITY     32u
36 #define AMDGPU_BO_LIST_NUM_BUCKETS      (AMDGPU_BO_LIST_MAX_PRIORITY + 1)
37
38 static void amdgpu_bo_list_free_rcu(struct rcu_head *rcu)
39 {
40         struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list,
41                                                    rhead);
42
43         kvfree(list);
44 }
45
46 static void amdgpu_bo_list_free(struct kref *ref)
47 {
48         struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,
49                                                    refcount);
50         struct amdgpu_bo_list_entry *e;
51
52         amdgpu_bo_list_for_each_entry(e, list)
53                 amdgpu_bo_unref(&e->robj);
54
55         call_rcu(&list->rhead, amdgpu_bo_list_free_rcu);
56 }
57
58 int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
59                           struct drm_amdgpu_bo_list_entry *info,
60                           size_t num_entries, struct amdgpu_bo_list **result)
61 {
62         unsigned last_entry = 0, first_userptr = num_entries;
63         struct amdgpu_bo_list_entry *array;
64         struct amdgpu_bo_list *list;
65         uint64_t total_size = 0;
66         size_t size;
67         unsigned i;
68         int r;
69
70         if (num_entries > (SIZE_MAX - sizeof(struct amdgpu_bo_list))
71                                 / sizeof(struct amdgpu_bo_list_entry))
72                 return -EINVAL;
73
74         size = sizeof(struct amdgpu_bo_list);
75         size += num_entries * sizeof(struct amdgpu_bo_list_entry);
76         list = kvmalloc(size, GFP_KERNEL);
77         if (!list)
78                 return -ENOMEM;
79
80         kref_init(&list->refcount);
81         list->gds_obj = adev->gds.gds_gfx_bo;
82         list->gws_obj = adev->gds.gws_gfx_bo;
83         list->oa_obj = adev->gds.oa_gfx_bo;
84
85         array = amdgpu_bo_list_array_entry(list, 0);
86         memset(array, 0, num_entries * sizeof(struct amdgpu_bo_list_entry));
87
88         for (i = 0; i < num_entries; ++i) {
89                 struct amdgpu_bo_list_entry *entry;
90                 struct drm_gem_object *gobj;
91                 struct amdgpu_bo *bo;
92                 struct mm_struct *usermm;
93
94                 gobj = drm_gem_object_lookup(filp, info[i].bo_handle);
95                 if (!gobj) {
96                         r = -ENOENT;
97                         goto error_free;
98                 }
99
100                 bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
101                 drm_gem_object_put_unlocked(gobj);
102
103                 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
104                 if (usermm) {
105                         if (usermm != current->mm) {
106                                 amdgpu_bo_unref(&bo);
107                                 r = -EPERM;
108                                 goto error_free;
109                         }
110                         entry = &array[--first_userptr];
111                 } else {
112                         entry = &array[last_entry++];
113                 }
114
115                 entry->robj = bo;
116                 entry->priority = min(info[i].bo_priority,
117                                       AMDGPU_BO_LIST_MAX_PRIORITY);
118                 entry->tv.bo = &entry->robj->tbo;
119                 entry->tv.shared = !entry->robj->prime_shared_count;
120
121                 if (entry->robj->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
122                         list->gds_obj = entry->robj;
123                 if (entry->robj->preferred_domains == AMDGPU_GEM_DOMAIN_GWS)
124                         list->gws_obj = entry->robj;
125                 if (entry->robj->preferred_domains == AMDGPU_GEM_DOMAIN_OA)
126                         list->oa_obj = entry->robj;
127
128                 total_size += amdgpu_bo_size(entry->robj);
129                 trace_amdgpu_bo_list_set(list, entry->robj);
130         }
131
132         list->first_userptr = first_userptr;
133         list->num_entries = num_entries;
134
135         trace_amdgpu_cs_bo_status(list->num_entries, total_size);
136
137         *result = list;
138         return 0;
139
140 error_free:
141         while (i--)
142                 amdgpu_bo_unref(&array[i].robj);
143         kvfree(list);
144         return r;
145
146 }
147
148 static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
149 {
150         struct amdgpu_bo_list *list;
151
152         mutex_lock(&fpriv->bo_list_lock);
153         list = idr_remove(&fpriv->bo_list_handles, id);
154         mutex_unlock(&fpriv->bo_list_lock);
155         if (list)
156                 kref_put(&list->refcount, amdgpu_bo_list_free);
157 }
158
159 int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
160                        struct amdgpu_bo_list **result)
161 {
162         rcu_read_lock();
163         *result = idr_find(&fpriv->bo_list_handles, id);
164
165         if (*result && kref_get_unless_zero(&(*result)->refcount)) {
166                 rcu_read_unlock();
167                 return 0;
168         }
169
170         rcu_read_unlock();
171         *result = NULL;
172         return -ENOENT;
173 }
174
175 void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
176                              struct list_head *validated)
177 {
178         /* This is based on the bucket sort with O(n) time complexity.
179          * An item with priority "i" is added to bucket[i]. The lists are then
180          * concatenated in descending order.
181          */
182         struct list_head bucket[AMDGPU_BO_LIST_NUM_BUCKETS];
183         struct amdgpu_bo_list_entry *e;
184         unsigned i;
185
186         for (i = 0; i < AMDGPU_BO_LIST_NUM_BUCKETS; i++)
187                 INIT_LIST_HEAD(&bucket[i]);
188
189         /* Since buffers which appear sooner in the relocation list are
190          * likely to be used more often than buffers which appear later
191          * in the list, the sort mustn't change the ordering of buffers
192          * with the same priority, i.e. it must be stable.
193          */
194         amdgpu_bo_list_for_each_entry(e, list) {
195                 unsigned priority = e->priority;
196
197                 if (!e->robj->parent)
198                         list_add_tail(&e->tv.head, &bucket[priority]);
199
200                 e->user_pages = NULL;
201         }
202
203         /* Connect the sorted buckets in the output list. */
204         for (i = 0; i < AMDGPU_BO_LIST_NUM_BUCKETS; i++)
205                 list_splice(&bucket[i], validated);
206 }
207
208 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
209 {
210         kref_put(&list->refcount, amdgpu_bo_list_free);
211 }
212
213 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
214                                       struct drm_amdgpu_bo_list_entry **info_param)
215 {
216         const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
217         const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
218         struct drm_amdgpu_bo_list_entry *info;
219         int r;
220
221         info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
222         if (!info)
223                 return -ENOMEM;
224
225         /* copy the handle array from userspace to a kernel buffer */
226         r = -EFAULT;
227         if (likely(info_size == in->bo_info_size)) {
228                 unsigned long bytes = in->bo_number *
229                         in->bo_info_size;
230
231                 if (copy_from_user(info, uptr, bytes))
232                         goto error_free;
233
234         } else {
235                 unsigned long bytes = min(in->bo_info_size, info_size);
236                 unsigned i;
237
238                 memset(info, 0, in->bo_number * info_size);
239                 for (i = 0; i < in->bo_number; ++i) {
240                         if (copy_from_user(&info[i], uptr, bytes))
241                                 goto error_free;
242
243                         uptr += in->bo_info_size;
244                 }
245         }
246
247         *info_param = info;
248         return 0;
249
250 error_free:
251         kvfree(info);
252         return r;
253 }
254
255 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
256                                 struct drm_file *filp)
257 {
258         struct amdgpu_device *adev = dev->dev_private;
259         struct amdgpu_fpriv *fpriv = filp->driver_priv;
260         union drm_amdgpu_bo_list *args = data;
261         uint32_t handle = args->in.list_handle;
262         struct drm_amdgpu_bo_list_entry *info = NULL;
263         struct amdgpu_bo_list *list, *old;
264         int r;
265
266         r = amdgpu_bo_create_list_entry_array(&args->in, &info);
267         if (r)
268                 return r;
269
270         switch (args->in.operation) {
271         case AMDGPU_BO_LIST_OP_CREATE:
272                 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
273                                           &list);
274                 if (r)
275                         goto error_free;
276
277                 mutex_lock(&fpriv->bo_list_lock);
278                 r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
279                 mutex_unlock(&fpriv->bo_list_lock);
280                 if (r < 0) {
281                         goto error_put_list;
282                 }
283
284                 handle = r;
285                 break;
286
287         case AMDGPU_BO_LIST_OP_DESTROY:
288                 amdgpu_bo_list_destroy(fpriv, handle);
289                 handle = 0;
290                 break;
291
292         case AMDGPU_BO_LIST_OP_UPDATE:
293                 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
294                                           &list);
295                 if (r)
296                         goto error_free;
297
298                 mutex_lock(&fpriv->bo_list_lock);
299                 old = idr_replace(&fpriv->bo_list_handles, list, handle);
300                 mutex_unlock(&fpriv->bo_list_lock);
301
302                 if (IS_ERR(old)) {
303                         r = PTR_ERR(old);
304                         goto error_put_list;
305                 }
306
307                 amdgpu_bo_list_put(old);
308                 break;
309
310         default:
311                 r = -EINVAL;
312                 goto error_free;
313         }
314
315         memset(args, 0, sizeof(*args));
316         args->out.list_handle = handle;
317         kvfree(info);
318
319         return 0;
320
321 error_put_list:
322         amdgpu_bo_list_put(list);
323
324 error_free:
325         kvfree(info);
326         return r;
327 }