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