]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c
Merge tag 'gfs2-merge-window' of git://git.kernel.org:/pub/scm/linux/kernel/git/gfs2...
[linux.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 <[email protected]>
29  */
30
31 #include <drm/drmP.h>
32 #include "amdgpu.h"
33
34 static int amdgpu_bo_list_create(struct amdgpu_fpriv *fpriv,
35                                  struct amdgpu_bo_list **result,
36                                  int *id)
37 {
38         int r;
39
40         *result = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
41         if (!*result)
42                 return -ENOMEM;
43
44         mutex_lock(&fpriv->bo_list_lock);
45         r = idr_alloc(&fpriv->bo_list_handles, *result,
46                       1, 0, GFP_KERNEL);
47         if (r < 0) {
48                 mutex_unlock(&fpriv->bo_list_lock);
49                 kfree(*result);
50                 return r;
51         }
52         *id = r;
53
54         mutex_init(&(*result)->lock);
55         (*result)->num_entries = 0;
56         (*result)->array = NULL;
57
58         mutex_lock(&(*result)->lock);
59         mutex_unlock(&fpriv->bo_list_lock);
60
61         return 0;
62 }
63
64 static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
65 {
66         struct amdgpu_bo_list *list;
67
68         mutex_lock(&fpriv->bo_list_lock);
69         list = idr_find(&fpriv->bo_list_handles, id);
70         if (list) {
71                 mutex_lock(&list->lock);
72                 idr_remove(&fpriv->bo_list_handles, id);
73                 mutex_unlock(&list->lock);
74                 amdgpu_bo_list_free(list);
75         }
76         mutex_unlock(&fpriv->bo_list_lock);
77 }
78
79 static int amdgpu_bo_list_set(struct amdgpu_device *adev,
80                                      struct drm_file *filp,
81                                      struct amdgpu_bo_list *list,
82                                      struct drm_amdgpu_bo_list_entry *info,
83                                      unsigned num_entries)
84 {
85         struct amdgpu_bo_list_entry *array;
86         struct amdgpu_bo *gds_obj = adev->gds.gds_gfx_bo;
87         struct amdgpu_bo *gws_obj = adev->gds.gws_gfx_bo;
88         struct amdgpu_bo *oa_obj = adev->gds.oa_gfx_bo;
89
90         bool has_userptr = false;
91         unsigned i;
92
93         array = drm_malloc_ab(num_entries, sizeof(struct amdgpu_bo_list_entry));
94         if (!array)
95                 return -ENOMEM;
96         memset(array, 0, num_entries * sizeof(struct amdgpu_bo_list_entry));
97
98         for (i = 0; i < num_entries; ++i) {
99                 struct amdgpu_bo_list_entry *entry = &array[i];
100                 struct drm_gem_object *gobj;
101
102                 gobj = drm_gem_object_lookup(adev->ddev, filp, info[i].bo_handle);
103                 if (!gobj)
104                         goto error_free;
105
106                 entry->robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
107                 drm_gem_object_unreference_unlocked(gobj);
108                 entry->priority = info[i].bo_priority;
109                 entry->prefered_domains = entry->robj->initial_domain;
110                 entry->allowed_domains = entry->prefered_domains;
111                 if (entry->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
112                         entry->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
113                 if (amdgpu_ttm_tt_has_userptr(entry->robj->tbo.ttm)) {
114                         has_userptr = true;
115                         entry->prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
116                         entry->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
117                 }
118                 entry->tv.bo = &entry->robj->tbo;
119                 entry->tv.shared = true;
120
121                 if (entry->prefered_domains == AMDGPU_GEM_DOMAIN_GDS)
122                         gds_obj = entry->robj;
123                 if (entry->prefered_domains == AMDGPU_GEM_DOMAIN_GWS)
124                         gws_obj = entry->robj;
125                 if (entry->prefered_domains == AMDGPU_GEM_DOMAIN_OA)
126                         oa_obj = entry->robj;
127         }
128
129         for (i = 0; i < list->num_entries; ++i)
130                 amdgpu_bo_unref(&list->array[i].robj);
131
132         drm_free_large(list->array);
133
134         list->gds_obj = gds_obj;
135         list->gws_obj = gws_obj;
136         list->oa_obj = oa_obj;
137         list->has_userptr = has_userptr;
138         list->array = array;
139         list->num_entries = num_entries;
140
141         return 0;
142
143 error_free:
144         drm_free_large(array);
145         return -ENOENT;
146 }
147
148 struct amdgpu_bo_list *
149 amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
150 {
151         struct amdgpu_bo_list *result;
152
153         mutex_lock(&fpriv->bo_list_lock);
154         result = idr_find(&fpriv->bo_list_handles, id);
155         if (result)
156                 mutex_lock(&result->lock);
157         mutex_unlock(&fpriv->bo_list_lock);
158         return result;
159 }
160
161 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
162 {
163         mutex_unlock(&list->lock);
164 }
165
166 void amdgpu_bo_list_free(struct amdgpu_bo_list *list)
167 {
168         unsigned i;
169
170         for (i = 0; i < list->num_entries; ++i)
171                 amdgpu_bo_unref(&list->array[i].robj);
172
173         mutex_destroy(&list->lock);
174         drm_free_large(list->array);
175         kfree(list);
176 }
177
178 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
179                                 struct drm_file *filp)
180 {
181         const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
182
183         struct amdgpu_device *adev = dev->dev_private;
184         struct amdgpu_fpriv *fpriv = filp->driver_priv;
185         union drm_amdgpu_bo_list *args = data;
186         uint32_t handle = args->in.list_handle;
187         const void __user *uptr = (const void*)(long)args->in.bo_info_ptr;
188
189         struct drm_amdgpu_bo_list_entry *info;
190         struct amdgpu_bo_list *list;
191
192         int r;
193
194         info = drm_malloc_ab(args->in.bo_number,
195                              sizeof(struct drm_amdgpu_bo_list_entry));
196         if (!info)
197                 return -ENOMEM;
198
199         /* copy the handle array from userspace to a kernel buffer */
200         r = -EFAULT;
201         if (likely(info_size == args->in.bo_info_size)) {
202                 unsigned long bytes = args->in.bo_number *
203                         args->in.bo_info_size;
204
205                 if (copy_from_user(info, uptr, bytes))
206                         goto error_free;
207
208         } else {
209                 unsigned long bytes = min(args->in.bo_info_size, info_size);
210                 unsigned i;
211
212                 memset(info, 0, args->in.bo_number * info_size);
213                 for (i = 0; i < args->in.bo_number; ++i) {
214                         if (copy_from_user(&info[i], uptr, bytes))
215                                 goto error_free;
216                         
217                         uptr += args->in.bo_info_size;
218                 }
219         }
220
221         switch (args->in.operation) {
222         case AMDGPU_BO_LIST_OP_CREATE:
223                 r = amdgpu_bo_list_create(fpriv, &list, &handle);
224                 if (r) 
225                         goto error_free;
226
227                 r = amdgpu_bo_list_set(adev, filp, list, info,
228                                               args->in.bo_number);
229                 amdgpu_bo_list_put(list);
230                 if (r)
231                         goto error_free;
232
233                 break;
234                 
235         case AMDGPU_BO_LIST_OP_DESTROY:
236                 amdgpu_bo_list_destroy(fpriv, handle);
237                 handle = 0;
238                 break;
239
240         case AMDGPU_BO_LIST_OP_UPDATE:
241                 r = -ENOENT;
242                 list = amdgpu_bo_list_get(fpriv, handle);
243                 if (!list)
244                         goto error_free;
245
246                 r = amdgpu_bo_list_set(adev, filp, list, info,
247                                               args->in.bo_number);
248                 amdgpu_bo_list_put(list);
249                 if (r)
250                         goto error_free;
251
252                 break;
253
254         default:
255                 r = -EINVAL;
256                 goto error_free;
257         }
258
259         memset(args, 0, sizeof(*args));
260         args->out.list_handle = handle;
261         drm_free_large(info);
262
263         return 0;
264
265 error_free:
266         drm_free_large(info);
267         return r;
268 }
This page took 0.045417 seconds and 4 git commands to generate.