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
33 #include "amdgpu_trace.h"
35 static int amdgpu_bo_list_create(struct amdgpu_fpriv *fpriv,
36 struct amdgpu_bo_list **result,
41 *result = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
45 mutex_lock(&fpriv->bo_list_lock);
46 r = idr_alloc(&fpriv->bo_list_handles, *result,
49 mutex_unlock(&fpriv->bo_list_lock);
55 mutex_init(&(*result)->lock);
56 (*result)->num_entries = 0;
57 (*result)->array = NULL;
59 mutex_lock(&(*result)->lock);
60 mutex_unlock(&fpriv->bo_list_lock);
65 struct amdgpu_bo_list *
66 amdgpu_bo_list_clone(struct amdgpu_bo_list *list)
68 struct amdgpu_bo_list *result;
71 result = kmalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
75 result->array = drm_calloc_large(list->num_entries,
76 sizeof(struct amdgpu_bo_list_entry));
82 mutex_init(&result->lock);
83 result->gds_obj = list->gds_obj;
84 result->gws_obj = list->gws_obj;
85 result->oa_obj = list->oa_obj;
86 result->has_userptr = list->has_userptr;
87 result->num_entries = list->num_entries;
89 memcpy(result->array, list->array, list->num_entries *
90 sizeof(struct amdgpu_bo_list_entry));
92 for (i = 0; i < result->num_entries; ++i)
93 amdgpu_bo_ref(result->array[i].robj);
98 static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
100 struct amdgpu_bo_list *list;
102 mutex_lock(&fpriv->bo_list_lock);
103 list = idr_find(&fpriv->bo_list_handles, id);
105 mutex_lock(&list->lock);
106 idr_remove(&fpriv->bo_list_handles, id);
107 mutex_unlock(&list->lock);
108 amdgpu_bo_list_free(list);
110 mutex_unlock(&fpriv->bo_list_lock);
113 static int amdgpu_bo_list_set(struct amdgpu_device *adev,
114 struct drm_file *filp,
115 struct amdgpu_bo_list *list,
116 struct drm_amdgpu_bo_list_entry *info,
117 unsigned num_entries)
119 struct amdgpu_bo_list_entry *array;
120 struct amdgpu_bo *gds_obj = adev->gds.gds_gfx_bo;
121 struct amdgpu_bo *gws_obj = adev->gds.gws_gfx_bo;
122 struct amdgpu_bo *oa_obj = adev->gds.oa_gfx_bo;
124 bool has_userptr = false;
127 array = drm_malloc_ab(num_entries, sizeof(struct amdgpu_bo_list_entry));
130 memset(array, 0, num_entries * sizeof(struct amdgpu_bo_list_entry));
132 for (i = 0; i < num_entries; ++i) {
133 struct amdgpu_bo_list_entry *entry = &array[i];
134 struct drm_gem_object *gobj;
136 gobj = drm_gem_object_lookup(adev->ddev, filp, info[i].bo_handle);
140 entry->robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
141 drm_gem_object_unreference_unlocked(gobj);
142 entry->priority = info[i].bo_priority;
143 entry->prefered_domains = entry->robj->initial_domain;
144 entry->allowed_domains = entry->prefered_domains;
145 if (entry->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
146 entry->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
147 if (amdgpu_ttm_tt_has_userptr(entry->robj->tbo.ttm)) {
149 entry->prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
150 entry->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
152 entry->tv.bo = &entry->robj->tbo;
153 entry->tv.shared = true;
155 if (entry->prefered_domains == AMDGPU_GEM_DOMAIN_GDS)
156 gds_obj = entry->robj;
157 if (entry->prefered_domains == AMDGPU_GEM_DOMAIN_GWS)
158 gws_obj = entry->robj;
159 if (entry->prefered_domains == AMDGPU_GEM_DOMAIN_OA)
160 oa_obj = entry->robj;
162 trace_amdgpu_bo_list_set(list, entry->robj);
165 for (i = 0; i < list->num_entries; ++i)
166 amdgpu_bo_unref(&list->array[i].robj);
168 drm_free_large(list->array);
170 list->gds_obj = gds_obj;
171 list->gws_obj = gws_obj;
172 list->oa_obj = oa_obj;
173 list->has_userptr = has_userptr;
175 list->num_entries = num_entries;
180 drm_free_large(array);
184 struct amdgpu_bo_list *
185 amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
187 struct amdgpu_bo_list *result;
189 mutex_lock(&fpriv->bo_list_lock);
190 result = idr_find(&fpriv->bo_list_handles, id);
192 mutex_lock(&result->lock);
193 mutex_unlock(&fpriv->bo_list_lock);
197 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
199 mutex_unlock(&list->lock);
202 void amdgpu_bo_list_free(struct amdgpu_bo_list *list)
206 for (i = 0; i < list->num_entries; ++i)
207 amdgpu_bo_unref(&list->array[i].robj);
209 mutex_destroy(&list->lock);
210 drm_free_large(list->array);
214 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
215 struct drm_file *filp)
217 const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
219 struct amdgpu_device *adev = dev->dev_private;
220 struct amdgpu_fpriv *fpriv = filp->driver_priv;
221 union drm_amdgpu_bo_list *args = data;
222 uint32_t handle = args->in.list_handle;
223 const void __user *uptr = (const void*)(long)args->in.bo_info_ptr;
225 struct drm_amdgpu_bo_list_entry *info;
226 struct amdgpu_bo_list *list;
230 info = drm_malloc_ab(args->in.bo_number,
231 sizeof(struct drm_amdgpu_bo_list_entry));
235 /* copy the handle array from userspace to a kernel buffer */
237 if (likely(info_size == args->in.bo_info_size)) {
238 unsigned long bytes = args->in.bo_number *
239 args->in.bo_info_size;
241 if (copy_from_user(info, uptr, bytes))
245 unsigned long bytes = min(args->in.bo_info_size, info_size);
248 memset(info, 0, args->in.bo_number * info_size);
249 for (i = 0; i < args->in.bo_number; ++i) {
250 if (copy_from_user(&info[i], uptr, bytes))
253 uptr += args->in.bo_info_size;
257 switch (args->in.operation) {
258 case AMDGPU_BO_LIST_OP_CREATE:
259 r = amdgpu_bo_list_create(fpriv, &list, &handle);
263 r = amdgpu_bo_list_set(adev, filp, list, info,
265 amdgpu_bo_list_put(list);
271 case AMDGPU_BO_LIST_OP_DESTROY:
272 amdgpu_bo_list_destroy(fpriv, handle);
276 case AMDGPU_BO_LIST_OP_UPDATE:
278 list = amdgpu_bo_list_get(fpriv, handle);
282 r = amdgpu_bo_list_set(adev, filp, list, info,
284 amdgpu_bo_list_put(list);
295 memset(args, 0, sizeof(*args));
296 args->out.list_handle = handle;
297 drm_free_large(info);
302 drm_free_large(info);