2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
31 #include <linux/sort.h>
32 #include <linux/uaccess.h>
35 #include "amdgpu_trace.h"
37 #define AMDGPU_BO_LIST_MAX_PRIORITY 32u
38 #define AMDGPU_BO_LIST_NUM_BUCKETS (AMDGPU_BO_LIST_MAX_PRIORITY + 1)
40 static void amdgpu_bo_list_free_rcu(struct rcu_head *rcu)
42 struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list,
44 mutex_destroy(&list->bo_list_mutex);
48 static void amdgpu_bo_list_free(struct kref *ref)
50 struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,
52 struct amdgpu_bo_list_entry *e;
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);
59 static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
61 const struct amdgpu_bo_list_entry *a = _a, *b = _b;
63 if (a->priority > b->priority)
65 if (a->priority < b->priority)
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)
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;
81 list = kvzalloc(struct_size(list, entries, num_entries), GFP_KERNEL);
85 kref_init(&list->refcount);
87 list->num_entries = num_entries;
88 array = list->entries;
90 for (i = 0; i < num_entries; ++i) {
91 struct amdgpu_bo_list_entry *entry;
92 struct drm_gem_object *gobj;
94 struct mm_struct *usermm;
96 gobj = drm_gem_object_lookup(filp, info[i].bo_handle);
102 bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
103 drm_gem_object_put(gobj);
105 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
107 if (usermm != current->mm) {
108 amdgpu_bo_unref(&bo);
112 entry = &array[--first_userptr];
114 entry = &array[last_entry++];
117 entry->priority = min(info[i].bo_priority,
118 AMDGPU_BO_LIST_MAX_PRIORITY);
121 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
123 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GWS)
125 if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_OA)
128 total_size += amdgpu_bo_size(bo);
129 trace_amdgpu_bo_list_set(list, bo);
132 list->first_userptr = first_userptr;
133 sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
134 amdgpu_bo_list_entry_cmp, NULL);
136 trace_amdgpu_cs_bo_status(list->num_entries, total_size);
138 mutex_init(&list->bo_list_mutex);
143 for (i = 0; i < last_entry; ++i)
144 amdgpu_bo_unref(&array[i].bo);
145 for (i = first_userptr; i < num_entries; ++i)
146 amdgpu_bo_unref(&array[i].bo);
152 static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
154 struct amdgpu_bo_list *list;
156 mutex_lock(&fpriv->bo_list_lock);
157 list = idr_remove(&fpriv->bo_list_handles, id);
158 mutex_unlock(&fpriv->bo_list_lock);
160 kref_put(&list->refcount, amdgpu_bo_list_free);
163 int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
164 struct amdgpu_bo_list **result)
167 *result = idr_find(&fpriv->bo_list_handles, id);
169 if (*result && kref_get_unless_zero(&(*result)->refcount)) {
179 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
181 kref_put(&list->refcount, amdgpu_bo_list_free);
184 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
185 struct drm_amdgpu_bo_list_entry **info_param)
187 const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
188 const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
189 struct drm_amdgpu_bo_list_entry *info;
192 info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
196 /* copy the handle array from userspace to a kernel buffer */
198 if (likely(info_size == in->bo_info_size)) {
199 unsigned long bytes = in->bo_number *
202 if (copy_from_user(info, uptr, bytes))
206 unsigned long bytes = min(in->bo_info_size, info_size);
209 memset(info, 0, in->bo_number * info_size);
210 for (i = 0; i < in->bo_number; ++i) {
211 if (copy_from_user(&info[i], uptr, bytes))
214 uptr += in->bo_info_size;
226 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
227 struct drm_file *filp)
229 struct amdgpu_device *adev = drm_to_adev(dev);
230 struct amdgpu_fpriv *fpriv = filp->driver_priv;
231 union drm_amdgpu_bo_list *args = data;
232 uint32_t handle = args->in.list_handle;
233 struct drm_amdgpu_bo_list_entry *info = NULL;
234 struct amdgpu_bo_list *list, *old;
237 r = amdgpu_bo_create_list_entry_array(&args->in, &info);
241 switch (args->in.operation) {
242 case AMDGPU_BO_LIST_OP_CREATE:
243 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
248 mutex_lock(&fpriv->bo_list_lock);
249 r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
250 mutex_unlock(&fpriv->bo_list_lock);
258 case AMDGPU_BO_LIST_OP_DESTROY:
259 amdgpu_bo_list_destroy(fpriv, handle);
263 case AMDGPU_BO_LIST_OP_UPDATE:
264 r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
269 mutex_lock(&fpriv->bo_list_lock);
270 old = idr_replace(&fpriv->bo_list_handles, list, handle);
271 mutex_unlock(&fpriv->bo_list_lock);
278 amdgpu_bo_list_put(old);
286 memset(args, 0, sizeof(*args));
287 args->out.list_handle = handle;
293 amdgpu_bo_list_put(list);