1d38ceaf9SAlex Deucher /*
2d38ceaf9SAlex Deucher * Copyright 2015 Advanced Micro Devices, Inc.
3d38ceaf9SAlex Deucher * All Rights Reserved.
4d38ceaf9SAlex Deucher *
5d38ceaf9SAlex Deucher * Permission is hereby granted, free of charge, to any person obtaining a
6d38ceaf9SAlex Deucher * copy of this software and associated documentation files (the
7d38ceaf9SAlex Deucher * "Software"), to deal in the Software without restriction, including
8d38ceaf9SAlex Deucher * without limitation the rights to use, copy, modify, merge, publish,
9d38ceaf9SAlex Deucher * distribute, sub license, and/or sell copies of the Software, and to
10d38ceaf9SAlex Deucher * permit persons to whom the Software is furnished to do so, subject to
11d38ceaf9SAlex Deucher * the following conditions:
12d38ceaf9SAlex Deucher *
13d38ceaf9SAlex Deucher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14d38ceaf9SAlex Deucher * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15d38ceaf9SAlex Deucher * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16d38ceaf9SAlex Deucher * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17d38ceaf9SAlex Deucher * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18d38ceaf9SAlex Deucher * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19d38ceaf9SAlex Deucher * USE OR OTHER DEALINGS IN THE SOFTWARE.
20d38ceaf9SAlex Deucher *
21d38ceaf9SAlex Deucher * The above copyright notice and this permission notice (including the
22d38ceaf9SAlex Deucher * next paragraph) shall be included in all copies or substantial portions
23d38ceaf9SAlex Deucher * of the Software.
24d38ceaf9SAlex Deucher *
25d38ceaf9SAlex Deucher */
26d38ceaf9SAlex Deucher /*
27d38ceaf9SAlex Deucher * Authors:
28d38ceaf9SAlex Deucher * Christian König <deathsimple@vodafone.de>
29d38ceaf9SAlex Deucher */
30d38ceaf9SAlex Deucher
31ca6c1e21SChristian König #include <linux/sort.h>
32fdf2f6c5SSam Ravnborg #include <linux/uaccess.h>
33fdf2f6c5SSam Ravnborg
34d38ceaf9SAlex Deucher #include "amdgpu.h"
35ec74407aSChristian König #include "amdgpu_trace.h"
36d38ceaf9SAlex Deucher
37636ce25cSChristian König #define AMDGPU_BO_LIST_MAX_PRIORITY 32u
38636ce25cSChristian König #define AMDGPU_BO_LIST_NUM_BUCKETS (AMDGPU_BO_LIST_MAX_PRIORITY + 1)
39636ce25cSChristian König
amdgpu_bo_list_free_rcu(struct rcu_head * rcu)40920990cbSChristian König static void amdgpu_bo_list_free_rcu(struct rcu_head *rcu)
41920990cbSChristian König {
42920990cbSChristian König struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list,
43920990cbSChristian König rhead);
4490af0ca0SLuben Tuikov mutex_destroy(&list->bo_list_mutex);
45920990cbSChristian König kvfree(list);
46920990cbSChristian König }
47920990cbSChristian König
amdgpu_bo_list_free(struct kref * ref)48920990cbSChristian König static void amdgpu_bo_list_free(struct kref *ref)
495ac55629SAlex Xie {
505ac55629SAlex Xie struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,
515ac55629SAlex Xie refcount);
5239f7f69aSChristian König struct amdgpu_bo_list_entry *e;
535ac55629SAlex Xie
54ca6c1e21SChristian König amdgpu_bo_list_for_each_entry(e, list)
55ca6c1e21SChristian König amdgpu_bo_unref(&e->bo);
56ca6c1e21SChristian König call_rcu(&list->rhead, amdgpu_bo_list_free_rcu);
57e83dfe4dSChristian König }
585ac55629SAlex Xie
amdgpu_bo_list_entry_cmp(const void * _a,const void * _b)59ca6c1e21SChristian König static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
60ca6c1e21SChristian König {
61ca6c1e21SChristian König const struct amdgpu_bo_list_entry *a = _a, *b = _b;
62ca6c1e21SChristian König
63ca6c1e21SChristian König if (a->priority > b->priority)
64ca6c1e21SChristian König return 1;
65ca6c1e21SChristian König if (a->priority < b->priority)
66ca6c1e21SChristian König return -1;
67ca6c1e21SChristian König return 0;
685ac55629SAlex Xie }
695ac55629SAlex Xie
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)70920990cbSChristian König int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
7199eea4dfSAlex Xie struct drm_amdgpu_bo_list_entry *info,
72335aea75SArnd Bergmann size_t num_entries, struct amdgpu_bo_list **result)
73d38ceaf9SAlex Deucher {
74211dff55SChristian König unsigned last_entry = 0, first_userptr = num_entries;
75920990cbSChristian König struct amdgpu_bo_list_entry *array;
76920990cbSChristian König struct amdgpu_bo_list *list;
77275105ceSChristian König uint64_t total_size = 0;
78920990cbSChristian König size_t size;
79d38ceaf9SAlex Deucher unsigned i;
80cc325d19SChristian König int r;
81d38ceaf9SAlex Deucher
82ff30e9e8SDan Carpenter if (num_entries > (SIZE_MAX - sizeof(struct amdgpu_bo_list))
83ff30e9e8SDan Carpenter / sizeof(struct amdgpu_bo_list_entry))
84920990cbSChristian König return -EINVAL;
85920990cbSChristian König
86920990cbSChristian König size = sizeof(struct amdgpu_bo_list);
87920990cbSChristian König size += num_entries * sizeof(struct amdgpu_bo_list_entry);
88920990cbSChristian König list = kvmalloc(size, GFP_KERNEL);
89920990cbSChristian König if (!list)
90d38ceaf9SAlex Deucher return -ENOMEM;
91920990cbSChristian König
92920990cbSChristian König kref_init(&list->refcount);
93dca29491SChristian König list->gds_obj = NULL;
94dca29491SChristian König list->gws_obj = NULL;
95dca29491SChristian König list->oa_obj = NULL;
96920990cbSChristian König
97920990cbSChristian König array = amdgpu_bo_list_array_entry(list, 0);
98d38ceaf9SAlex Deucher memset(array, 0, num_entries * sizeof(struct amdgpu_bo_list_entry));
99d38ceaf9SAlex Deucher
100d38ceaf9SAlex Deucher for (i = 0; i < num_entries; ++i) {
101211dff55SChristian König struct amdgpu_bo_list_entry *entry;
102d38ceaf9SAlex Deucher struct drm_gem_object *gobj;
103211dff55SChristian König struct amdgpu_bo *bo;
104cc325d19SChristian König struct mm_struct *usermm;
105d38ceaf9SAlex Deucher
106a8ad0bd8SChris Wilson gobj = drm_gem_object_lookup(filp, info[i].bo_handle);
107cc325d19SChristian König if (!gobj) {
108cc325d19SChristian König r = -ENOENT;
109d38ceaf9SAlex Deucher goto error_free;
110cc325d19SChristian König }
111d38ceaf9SAlex Deucher
112211dff55SChristian König bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
113e07ddb0cSEmil Velikov drm_gem_object_put(gobj);
114211dff55SChristian König
115211dff55SChristian König usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
116cc325d19SChristian König if (usermm) {
117cc325d19SChristian König if (usermm != current->mm) {
118211dff55SChristian König amdgpu_bo_unref(&bo);
119cc325d19SChristian König r = -EPERM;
120cc325d19SChristian König goto error_free;
121cc325d19SChristian König }
122211dff55SChristian König entry = &array[--first_userptr];
123211dff55SChristian König } else {
124211dff55SChristian König entry = &array[last_entry++];
125cc325d19SChristian König }
126211dff55SChristian König
127211dff55SChristian König entry->priority = min(info[i].bo_priority,
128211dff55SChristian König AMDGPU_BO_LIST_MAX_PRIORITY);
129ca6c1e21SChristian König entry->bo = bo;
130d38ceaf9SAlex Deucher
131e83dfe4dSChristian König if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
132e83dfe4dSChristian König list->gds_obj = bo;
133e83dfe4dSChristian König if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GWS)
134e83dfe4dSChristian König list->gws_obj = bo;
135e83dfe4dSChristian König if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_OA)
136e83dfe4dSChristian König list->oa_obj = bo;
137ec74407aSChristian König
138e83dfe4dSChristian König total_size += amdgpu_bo_size(bo);
139e83dfe4dSChristian König trace_amdgpu_bo_list_set(list, bo);
140d38ceaf9SAlex Deucher }
141d38ceaf9SAlex Deucher
142211dff55SChristian König list->first_userptr = first_userptr;
143d38ceaf9SAlex Deucher list->num_entries = num_entries;
144ca6c1e21SChristian König sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
145ca6c1e21SChristian König amdgpu_bo_list_entry_cmp, NULL);
146d38ceaf9SAlex Deucher
14715da301dSDavid Mao trace_amdgpu_cs_bo_status(list->num_entries, total_size);
148920990cbSChristian König
14990af0ca0SLuben Tuikov mutex_init(&list->bo_list_mutex);
150920990cbSChristian König *result = list;
151d38ceaf9SAlex Deucher return 0;
152d38ceaf9SAlex Deucher
153d38ceaf9SAlex Deucher error_free:
154ca6c1e21SChristian König for (i = 0; i < last_entry; ++i)
155ca6c1e21SChristian König amdgpu_bo_unref(&array[i].bo);
156ca6c1e21SChristian König for (i = first_userptr; i < num_entries; ++i)
157ca6c1e21SChristian König amdgpu_bo_unref(&array[i].bo);
158920990cbSChristian König kvfree(list);
159cc325d19SChristian König return r;
160920990cbSChristian König
161920990cbSChristian König }
162920990cbSChristian König
amdgpu_bo_list_destroy(struct amdgpu_fpriv * fpriv,int id)163920990cbSChristian König static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
164920990cbSChristian König {
165920990cbSChristian König struct amdgpu_bo_list *list;
166920990cbSChristian König
167920990cbSChristian König mutex_lock(&fpriv->bo_list_lock);
168920990cbSChristian König list = idr_remove(&fpriv->bo_list_handles, id);
169920990cbSChristian König mutex_unlock(&fpriv->bo_list_lock);
170920990cbSChristian König if (list)
171920990cbSChristian König kref_put(&list->refcount, amdgpu_bo_list_free);
172d38ceaf9SAlex Deucher }
173d38ceaf9SAlex Deucher
amdgpu_bo_list_get(struct amdgpu_fpriv * fpriv,int id,struct amdgpu_bo_list ** result)17452c054caSChristian König int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
17552c054caSChristian König struct amdgpu_bo_list **result)
176d38ceaf9SAlex Deucher {
1775ac55629SAlex Xie rcu_read_lock();
17852c054caSChristian König *result = idr_find(&fpriv->bo_list_handles, id);
1795ac55629SAlex Xie
18052c054caSChristian König if (*result && kref_get_unless_zero(&(*result)->refcount)) {
181564f0458SAlex Xie rcu_read_unlock();
18252c054caSChristian König return 0;
183564f0458SAlex Xie }
1845ac55629SAlex Xie
18552c054caSChristian König rcu_read_unlock();
186*df9142a3SChristian König *result = NULL;
18752c054caSChristian König return -ENOENT;
188d38ceaf9SAlex Deucher }
189d38ceaf9SAlex Deucher
amdgpu_bo_list_put(struct amdgpu_bo_list * list)190d38ceaf9SAlex Deucher void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
191d38ceaf9SAlex Deucher {
192920990cbSChristian König kref_put(&list->refcount, amdgpu_bo_list_free);
193d38ceaf9SAlex Deucher }
194d38ceaf9SAlex Deucher
amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in * in,struct drm_amdgpu_bo_list_entry ** info_param)195964d0fbfSAndrey Grodzovsky int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
196964d0fbfSAndrey Grodzovsky struct drm_amdgpu_bo_list_entry **info_param)
197d38ceaf9SAlex Deucher {
198964d0fbfSAndrey Grodzovsky const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
199d38ceaf9SAlex Deucher const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
200d38ceaf9SAlex Deucher struct drm_amdgpu_bo_list_entry *info;
201d38ceaf9SAlex Deucher int r;
202d38ceaf9SAlex Deucher
203964d0fbfSAndrey Grodzovsky info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
204d38ceaf9SAlex Deucher if (!info)
205d38ceaf9SAlex Deucher return -ENOMEM;
206d38ceaf9SAlex Deucher
207d38ceaf9SAlex Deucher /* copy the handle array from userspace to a kernel buffer */
208d38ceaf9SAlex Deucher r = -EFAULT;
209964d0fbfSAndrey Grodzovsky if (likely(info_size == in->bo_info_size)) {
210964d0fbfSAndrey Grodzovsky unsigned long bytes = in->bo_number *
211964d0fbfSAndrey Grodzovsky in->bo_info_size;
212d38ceaf9SAlex Deucher
213d38ceaf9SAlex Deucher if (copy_from_user(info, uptr, bytes))
214d38ceaf9SAlex Deucher goto error_free;
215d38ceaf9SAlex Deucher
216d38ceaf9SAlex Deucher } else {
217964d0fbfSAndrey Grodzovsky unsigned long bytes = min(in->bo_info_size, info_size);
218d38ceaf9SAlex Deucher unsigned i;
219d38ceaf9SAlex Deucher
220964d0fbfSAndrey Grodzovsky memset(info, 0, in->bo_number * info_size);
221964d0fbfSAndrey Grodzovsky for (i = 0; i < in->bo_number; ++i) {
222d38ceaf9SAlex Deucher if (copy_from_user(&info[i], uptr, bytes))
223d38ceaf9SAlex Deucher goto error_free;
224d38ceaf9SAlex Deucher
225964d0fbfSAndrey Grodzovsky uptr += in->bo_info_size;
226d38ceaf9SAlex Deucher }
227d38ceaf9SAlex Deucher }
228d38ceaf9SAlex Deucher
229964d0fbfSAndrey Grodzovsky *info_param = info;
230964d0fbfSAndrey Grodzovsky return 0;
231964d0fbfSAndrey Grodzovsky
232964d0fbfSAndrey Grodzovsky error_free:
233964d0fbfSAndrey Grodzovsky kvfree(info);
234964d0fbfSAndrey Grodzovsky return r;
235964d0fbfSAndrey Grodzovsky }
236964d0fbfSAndrey Grodzovsky
amdgpu_bo_list_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)237964d0fbfSAndrey Grodzovsky int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
238964d0fbfSAndrey Grodzovsky struct drm_file *filp)
239964d0fbfSAndrey Grodzovsky {
2401348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(dev);
241964d0fbfSAndrey Grodzovsky struct amdgpu_fpriv *fpriv = filp->driver_priv;
242964d0fbfSAndrey Grodzovsky union drm_amdgpu_bo_list *args = data;
243964d0fbfSAndrey Grodzovsky uint32_t handle = args->in.list_handle;
244964d0fbfSAndrey Grodzovsky struct drm_amdgpu_bo_list_entry *info = NULL;
24581c6dabcSChristian König struct amdgpu_bo_list *list, *old;
246964d0fbfSAndrey Grodzovsky int r;
247964d0fbfSAndrey Grodzovsky
248964d0fbfSAndrey Grodzovsky r = amdgpu_bo_create_list_entry_array(&args->in, &info);
249964d0fbfSAndrey Grodzovsky if (r)
250083164dbSNirmoy Das return r;
251964d0fbfSAndrey Grodzovsky
252d38ceaf9SAlex Deucher switch (args->in.operation) {
253d38ceaf9SAlex Deucher case AMDGPU_BO_LIST_OP_CREATE:
25499eea4dfSAlex Xie r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
255964d0fbfSAndrey Grodzovsky &list);
256d38ceaf9SAlex Deucher if (r)
257d38ceaf9SAlex Deucher goto error_free;
258964d0fbfSAndrey Grodzovsky
259964d0fbfSAndrey Grodzovsky mutex_lock(&fpriv->bo_list_lock);
260964d0fbfSAndrey Grodzovsky r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
261964d0fbfSAndrey Grodzovsky mutex_unlock(&fpriv->bo_list_lock);
262964d0fbfSAndrey Grodzovsky if (r < 0) {
263083164dbSNirmoy Das goto error_put_list;
264964d0fbfSAndrey Grodzovsky }
265964d0fbfSAndrey Grodzovsky
266964d0fbfSAndrey Grodzovsky handle = r;
267d38ceaf9SAlex Deucher break;
268d38ceaf9SAlex Deucher
269d38ceaf9SAlex Deucher case AMDGPU_BO_LIST_OP_DESTROY:
270d38ceaf9SAlex Deucher amdgpu_bo_list_destroy(fpriv, handle);
271d38ceaf9SAlex Deucher handle = 0;
272d38ceaf9SAlex Deucher break;
273d38ceaf9SAlex Deucher
274d38ceaf9SAlex Deucher case AMDGPU_BO_LIST_OP_UPDATE:
27581c6dabcSChristian König r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
27681c6dabcSChristian König &list);
27752c054caSChristian König if (r)
278d38ceaf9SAlex Deucher goto error_free;
279d38ceaf9SAlex Deucher
28081c6dabcSChristian König mutex_lock(&fpriv->bo_list_lock);
28181c6dabcSChristian König old = idr_replace(&fpriv->bo_list_handles, list, handle);
28281c6dabcSChristian König mutex_unlock(&fpriv->bo_list_lock);
28381c6dabcSChristian König
28481c6dabcSChristian König if (IS_ERR(old)) {
28581c6dabcSChristian König r = PTR_ERR(old);
286083164dbSNirmoy Das goto error_put_list;
28781c6dabcSChristian König }
288d38ceaf9SAlex Deucher
28981c6dabcSChristian König amdgpu_bo_list_put(old);
290d38ceaf9SAlex Deucher break;
291d38ceaf9SAlex Deucher
292d38ceaf9SAlex Deucher default:
293d38ceaf9SAlex Deucher r = -EINVAL;
294d38ceaf9SAlex Deucher goto error_free;
295d38ceaf9SAlex Deucher }
296d38ceaf9SAlex Deucher
297d38ceaf9SAlex Deucher memset(args, 0, sizeof(*args));
298d38ceaf9SAlex Deucher args->out.list_handle = handle;
2992098105eSMichal Hocko kvfree(info);
300d38ceaf9SAlex Deucher
301d38ceaf9SAlex Deucher return 0;
302d38ceaf9SAlex Deucher
303083164dbSNirmoy Das error_put_list:
304083164dbSNirmoy Das amdgpu_bo_list_put(list);
305083164dbSNirmoy Das
306d38ceaf9SAlex Deucher error_free:
3072098105eSMichal Hocko kvfree(info);
308d38ceaf9SAlex Deucher return r;
309d38ceaf9SAlex Deucher }
310