blob: 9b1b6bdd48410bbfab9fa0dc8fc66a25c68d3559 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <linux/ktime.h>
Stephen Rothwell568d7c72016-03-17 15:30:49 +110029#include <linux/pagemap.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030#include <drm/drmP.h>
31#include <drm/amdgpu_drm.h>
32#include "amdgpu.h"
33
34void amdgpu_gem_object_free(struct drm_gem_object *gobj)
35{
36 struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
37
38 if (robj) {
39 if (robj->gem_base.import_attach)
40 drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
Christian König9298e522015-06-03 21:31:20 +020041 amdgpu_mn_unregister(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040042 amdgpu_bo_unref(&robj);
43 }
44}
45
46int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
47 int alignment, u32 initial_domain,
48 u64 flags, bool kernel,
49 struct drm_gem_object **obj)
50{
51 struct amdgpu_bo *robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040052 int r;
53
54 *obj = NULL;
55 /* At least align on page size */
56 if (alignment < PAGE_SIZE) {
57 alignment = PAGE_SIZE;
58 }
59
Alex Deucherd38ceaf2015-04-20 16:55:21 -040060retry:
Christian König72d76682015-09-03 17:34:59 +020061 r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain,
Yong Zhao2046d462017-07-20 18:49:09 -040062 flags, NULL, NULL, 0, &robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063 if (r) {
64 if (r != -ERESTARTSYS) {
65 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
66 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
67 goto retry;
68 }
69 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
70 size, initial_domain, alignment, r);
71 }
72 return r;
73 }
74 *obj = &robj->gem_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076 return 0;
77}
78
Christian König418aa0c2016-02-15 16:59:57 +010079void amdgpu_gem_force_release(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080{
Christian König418aa0c2016-02-15 16:59:57 +010081 struct drm_device *ddev = adev->ddev;
82 struct drm_file *file;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083
Daniel Vetter1d2ac402016-04-26 19:29:41 +020084 mutex_lock(&ddev->filelist_mutex);
Christian König418aa0c2016-02-15 16:59:57 +010085
86 list_for_each_entry(file, &ddev->filelist, lhead) {
87 struct drm_gem_object *gobj;
88 int handle;
89
90 WARN_ONCE(1, "Still active user space clients!\n");
91 spin_lock(&file->table_lock);
92 idr_for_each_entry(&file->object_idr, gobj, handle) {
93 WARN_ONCE(1, "And also active allocations!\n");
Cihangir Akturkf62facc2017-08-03 14:58:16 +030094 drm_gem_object_put_unlocked(gobj);
Christian König418aa0c2016-02-15 16:59:57 +010095 }
96 idr_destroy(&file->object_idr);
97 spin_unlock(&file->table_lock);
98 }
99
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200100 mutex_unlock(&ddev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101}
102
103/*
104 * Call from drm_gem_handle_create which appear in both new and open ioctl
105 * case.
106 */
Christian Königa7d64de2016-09-15 14:58:48 +0200107int amdgpu_gem_object_open(struct drm_gem_object *obj,
108 struct drm_file *file_priv)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109{
Christian König765e7fb2016-09-15 15:06:50 +0200110 struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200111 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
113 struct amdgpu_vm *vm = &fpriv->vm;
114 struct amdgpu_bo_va *bo_va;
115 int r;
Christian König765e7fb2016-09-15 15:06:50 +0200116 r = amdgpu_bo_reserve(abo, false);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800117 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119
Christian König765e7fb2016-09-15 15:06:50 +0200120 bo_va = amdgpu_vm_bo_find(vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121 if (!bo_va) {
Christian König765e7fb2016-09-15 15:06:50 +0200122 bo_va = amdgpu_vm_bo_add(adev, vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 } else {
124 ++bo_va->ref_count;
125 }
Christian König765e7fb2016-09-15 15:06:50 +0200126 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 return 0;
128}
129
130void amdgpu_gem_object_close(struct drm_gem_object *obj,
131 struct drm_file *file_priv)
132{
Christian Königb5a5ec52016-03-08 17:47:46 +0100133 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200134 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
136 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb5a5ec52016-03-08 17:47:46 +0100137
138 struct amdgpu_bo_list_entry vm_pd;
Christian König5a0f3b52017-04-21 10:05:56 +0200139 struct list_head list;
Christian Königb5a5ec52016-03-08 17:47:46 +0100140 struct ttm_validate_buffer tv;
141 struct ww_acquire_ctx ticket;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 struct amdgpu_bo_va *bo_va;
143 int r;
Christian Königb5a5ec52016-03-08 17:47:46 +0100144
145 INIT_LIST_HEAD(&list);
Christian Königb5a5ec52016-03-08 17:47:46 +0100146
147 tv.bo = &bo->tbo;
148 tv.shared = true;
149 list_add(&tv.head, &list);
150
151 amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
152
Christian König5a0f3b52017-04-21 10:05:56 +0200153 r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154 if (r) {
155 dev_err(adev->dev, "leaking bo va because "
156 "we fail to reserve bo (%d)\n", r);
157 return;
158 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100159 bo_va = amdgpu_vm_bo_find(vm, bo);
Christian König5a0f3b52017-04-21 10:05:56 +0200160 if (bo_va && --bo_va->ref_count == 0) {
161 amdgpu_vm_bo_rmv(adev, bo_va);
162
Christian König34d7be52017-08-24 12:32:55 +0200163 if (amdgpu_vm_ready(adev, vm)) {
Christian König5a0f3b52017-04-21 10:05:56 +0200164 struct dma_fence *fence = NULL;
Nicolai Hähnle23e05632017-03-23 19:34:11 +0100165
166 r = amdgpu_vm_clear_freed(adev, vm, &fence);
167 if (unlikely(r)) {
168 dev_err(adev->dev, "failed to clear page "
169 "tables on GEM object close (%d)\n", r);
170 }
171
172 if (fence) {
173 amdgpu_bo_fence(bo, fence, true);
174 dma_fence_put(fence);
175 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 }
177 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100178 ttm_eu_backoff_reservation(&ticket, &list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400179}
180
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181/*
182 * GEM ioctls.
183 */
184int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
185 struct drm_file *filp)
186{
187 struct amdgpu_device *adev = dev->dev_private;
188 union drm_amdgpu_gem_create *args = data;
189 uint64_t size = args->in.bo_size;
190 struct drm_gem_object *gobj;
191 uint32_t handle;
192 bool kernel = false;
193 int r;
194
Alex Deucher834e0f82017-03-08 17:40:17 -0500195 /* reject invalid gem flags */
196 if (args->in.domain_flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
197 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
198 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
Christian Königc0573af2017-08-09 15:33:49 +0200199 AMDGPU_GEM_CREATE_VRAM_CLEARED))
Christian Königa022c542017-05-08 15:14:54 +0200200 return -EINVAL;
201
Alex Deucher834e0f82017-03-08 17:40:17 -0500202 /* reject invalid gem domains */
203 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
204 AMDGPU_GEM_DOMAIN_GTT |
205 AMDGPU_GEM_DOMAIN_VRAM |
206 AMDGPU_GEM_DOMAIN_GDS |
207 AMDGPU_GEM_DOMAIN_GWS |
Christian Königa022c542017-05-08 15:14:54 +0200208 AMDGPU_GEM_DOMAIN_OA))
209 return -EINVAL;
Alex Deucher834e0f82017-03-08 17:40:17 -0500210
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211 /* create a gem object to contain this object in */
212 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
213 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
214 kernel = true;
215 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
216 size = size << AMDGPU_GDS_SHIFT;
217 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
218 size = size << AMDGPU_GWS_SHIFT;
219 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
220 size = size << AMDGPU_OA_SHIFT;
Christian Königa022c542017-05-08 15:14:54 +0200221 else
222 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400223 }
224 size = roundup(size, PAGE_SIZE);
225
226 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
227 (u32)(0xffffffff & args->in.domains),
228 args->in.domain_flags,
229 kernel, &gobj);
230 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200231 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232
233 r = drm_gem_handle_create(filp, gobj, &handle);
234 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300235 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200237 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238
239 memset(args, 0, sizeof(*args));
240 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242}
243
244int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
245 struct drm_file *filp)
246{
247 struct amdgpu_device *adev = dev->dev_private;
248 struct drm_amdgpu_gem_userptr *args = data;
249 struct drm_gem_object *gobj;
250 struct amdgpu_bo *bo;
251 uint32_t handle;
252 int r;
253
254 if (offset_in_page(args->addr | args->size))
255 return -EINVAL;
256
257 /* reject unknown flag values */
258 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
259 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
260 AMDGPU_GEM_USERPTR_REGISTER))
261 return -EINVAL;
262
Christian König358c2582016-03-11 15:29:27 +0100263 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
264 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265
Christian König358c2582016-03-11 15:29:27 +0100266 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267 return -EACCES;
268 }
269
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270 /* create a gem object to contain this object in */
271 r = amdgpu_gem_object_create(adev, args->size, 0,
272 AMDGPU_GEM_DOMAIN_CPU, 0,
273 0, &gobj);
274 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200275 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400276
277 bo = gem_to_amdgpu_bo(gobj);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400278 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
Christian König1ea863f2015-12-18 22:13:12 +0100279 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400280 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
281 if (r)
282 goto release_object;
283
284 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
285 r = amdgpu_mn_register(bo, args->addr);
286 if (r)
287 goto release_object;
288 }
289
290 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
291 down_read(&current->mm->mmap_sem);
Christian König2f568db2016-02-23 12:36:59 +0100292
293 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
294 bo->tbo.ttm->pages);
295 if (r)
296 goto unlock_mmap_sem;
297
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298 r = amdgpu_bo_reserve(bo, true);
Christian König2f568db2016-02-23 12:36:59 +0100299 if (r)
300 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400301
302 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
303 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
304 amdgpu_bo_unreserve(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305 if (r)
Christian König2f568db2016-02-23 12:36:59 +0100306 goto free_pages;
307
308 up_read(&current->mm->mmap_sem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400309 }
310
311 r = drm_gem_handle_create(filp, gobj, &handle);
312 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300313 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400314 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200315 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400316
317 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400318 return 0;
319
Christian König2f568db2016-02-23 12:36:59 +0100320free_pages:
321 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages, false);
322
323unlock_mmap_sem:
324 up_read(&current->mm->mmap_sem);
325
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400326release_object:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300327 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400328
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400329 return r;
330}
331
332int amdgpu_mode_dumb_mmap(struct drm_file *filp,
333 struct drm_device *dev,
334 uint32_t handle, uint64_t *offset_p)
335{
336 struct drm_gem_object *gobj;
337 struct amdgpu_bo *robj;
338
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100339 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400340 if (gobj == NULL) {
341 return -ENOENT;
342 }
343 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100344 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200345 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300346 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347 return -EPERM;
348 }
349 *offset_p = amdgpu_bo_mmap_offset(robj);
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300350 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400351 return 0;
352}
353
354int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
355 struct drm_file *filp)
356{
357 union drm_amdgpu_gem_mmap *args = data;
358 uint32_t handle = args->in.handle;
359 memset(args, 0, sizeof(*args));
360 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
361}
362
363/**
364 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
365 *
366 * @timeout_ns: timeout in ns
367 *
368 * Calculate the timeout in jiffies from an absolute timeout in ns.
369 */
370unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
371{
372 unsigned long timeout_jiffies;
373 ktime_t timeout;
374
375 /* clamp timeout if it's to large */
376 if (((int64_t)timeout_ns) < 0)
377 return MAX_SCHEDULE_TIMEOUT;
378
Christian König0f117702015-07-08 16:58:48 +0200379 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380 if (ktime_to_ns(timeout) < 0)
381 return 0;
382
383 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
384 /* clamp timeout to avoid unsigned-> signed overflow */
385 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
386 return MAX_SCHEDULE_TIMEOUT - 1;
387
388 return timeout_jiffies;
389}
390
391int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
392 struct drm_file *filp)
393{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400394 union drm_amdgpu_gem_wait_idle *args = data;
395 struct drm_gem_object *gobj;
396 struct amdgpu_bo *robj;
397 uint32_t handle = args->in.handle;
398 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
399 int r = 0;
400 long ret;
401
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100402 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400403 if (gobj == NULL) {
404 return -ENOENT;
405 }
406 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100407 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
408 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409
410 /* ret == 0 means not signaled,
411 * ret > 0 means signaled
412 * ret < 0 means interrupted before timeout
413 */
414 if (ret >= 0) {
415 memset(args, 0, sizeof(*args));
416 args->out.status = (ret == 0);
417 } else
418 r = ret;
419
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300420 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400421 return r;
422}
423
424int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
425 struct drm_file *filp)
426{
427 struct drm_amdgpu_gem_metadata *args = data;
428 struct drm_gem_object *gobj;
429 struct amdgpu_bo *robj;
430 int r = -1;
431
432 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100433 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 if (gobj == NULL)
435 return -ENOENT;
436 robj = gem_to_amdgpu_bo(gobj);
437
438 r = amdgpu_bo_reserve(robj, false);
439 if (unlikely(r != 0))
440 goto out;
441
442 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
443 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
444 r = amdgpu_bo_get_metadata(robj, args->data.data,
445 sizeof(args->data.data),
446 &args->data.data_size_bytes,
447 &args->data.flags);
448 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300449 if (args->data.data_size_bytes > sizeof(args->data.data)) {
450 r = -EINVAL;
451 goto unreserve;
452 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
454 if (!r)
455 r = amdgpu_bo_set_metadata(robj, args->data.data,
456 args->data.data_size_bytes,
457 args->data.flags);
458 }
459
Dan Carpenter0913eab2015-09-23 14:00:35 +0300460unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400461 amdgpu_bo_unreserve(robj);
462out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300463 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 return r;
465}
466
467/**
468 * amdgpu_gem_va_update_vm -update the bo_va in its VM
469 *
470 * @adev: amdgpu_device pointer
Christian Königdc54d3d2017-03-13 10:13:38 +0100471 * @vm: vm to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100473 * @list: validation list
Christian Königdc54d3d2017-03-13 10:13:38 +0100474 * @operation: map, unmap or clear
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100476 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400477 * vital here, so they are not reported back to userspace.
478 */
479static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königdc54d3d2017-03-13 10:13:38 +0100480 struct amdgpu_vm *vm,
Christian Königf7da30d2016-09-28 12:03:04 +0200481 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100482 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200483 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400484{
Christian König2ffdaaf2017-01-27 15:58:43 +0100485 int r = -ERESTARTSYS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486
Christian König34d7be52017-08-24 12:32:55 +0200487 if (!amdgpu_vm_ready(adev, vm))
Christian König2ffdaaf2017-01-27 15:58:43 +0100488 goto error;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800489
Christian König194d2162016-10-12 15:13:52 +0200490 r = amdgpu_vm_update_directories(adev, vm);
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800491 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100492 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100494 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400495 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100496 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800497
Christian König80f95c52017-03-13 10:13:39 +0100498 if (operation == AMDGPU_VA_OP_MAP ||
499 operation == AMDGPU_VA_OP_REPLACE)
Flora Cui05dcb5c2016-09-22 11:34:47 +0800500 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501
Christian König2ffdaaf2017-01-27 15:58:43 +0100502error:
Christian König68fdd3d2015-06-16 14:50:02 +0200503 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
505}
506
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
508 struct drm_file *filp)
509{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800510 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
511 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
Alex Xie66e02bc2017-02-14 12:04:52 -0500512 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800513 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
514 AMDGPU_VM_PAGE_PRT;
515
Christian König34b5f6a2015-06-08 15:03:00 +0200516 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517 struct drm_gem_object *gobj;
518 struct amdgpu_device *adev = dev->dev_private;
519 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200520 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200522 struct amdgpu_bo_list_entry vm_pd;
523 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800524 struct ww_acquire_ctx ticket;
Christian Königd7d29552017-01-30 10:24:13 +0100525 struct list_head list;
Alex Xie54635452017-02-14 12:22:57 -0500526 uint64_t va_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527 int r = 0;
528
Christian König34b5f6a2015-06-08 15:03:00 +0200529 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 dev_err(&dev->pdev->dev,
531 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200532 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 return -EINVAL;
535 }
536
Junwei Zhangb85891b2017-01-16 13:59:01 +0800537 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
538 dev_err(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
539 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540 return -EINVAL;
541 }
542
Christian König34b5f6a2015-06-08 15:03:00 +0200543 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 case AMDGPU_VA_OP_MAP:
545 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100546 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100547 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548 break;
549 default:
550 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200551 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400552 return -EINVAL;
553 }
Chunming Zhouf1892132017-05-15 16:48:27 +0800554 if ((args->operation == AMDGPU_VA_OP_MAP) ||
555 (args->operation == AMDGPU_VA_OP_REPLACE)) {
556 if (amdgpu_kms_vram_lost(adev, fpriv))
557 return -ENODEV;
558 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559
Chunming Zhou49b02b12015-11-13 14:18:38 +0800560 INIT_LIST_HEAD(&list);
Christian Königdc54d3d2017-03-13 10:13:38 +0100561 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
562 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800563 gobj = drm_gem_object_lookup(filp, args->handle);
564 if (gobj == NULL)
565 return -ENOENT;
566 abo = gem_to_amdgpu_bo(gobj);
567 tv.bo = &abo->tbo;
568 tv.shared = false;
569 list_add(&tv.head, &list);
570 } else {
571 gobj = NULL;
572 abo = NULL;
573 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800574
Christian Königb88c8792016-09-28 16:33:01 +0200575 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100576
Christian Königd7d29552017-01-30 10:24:13 +0100577 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800578 if (r)
579 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200580
Junwei Zhangb85891b2017-01-16 13:59:01 +0800581 if (abo) {
582 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
583 if (!bo_va) {
584 r = -ENOENT;
585 goto error_backoff;
586 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100587 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800588 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100589 } else {
590 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 }
592
Christian König34b5f6a2015-06-08 15:03:00 +0200593 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 case AMDGPU_VA_OP_MAP:
Christian Königec681542017-08-01 10:51:43 +0200595 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König663e4572017-03-13 10:13:37 +0100596 args->map_size);
597 if (r)
598 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500599
Christian König663e4572017-03-13 10:13:37 +0100600 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200601 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
602 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200603 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 break;
605 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200606 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400607 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100608
609 case AMDGPU_VA_OP_CLEAR:
610 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
611 args->va_address,
612 args->map_size);
613 break;
Christian König80f95c52017-03-13 10:13:39 +0100614 case AMDGPU_VA_OP_REPLACE:
Christian Königec681542017-08-01 10:51:43 +0200615 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König80f95c52017-03-13 10:13:39 +0100616 args->map_size);
617 if (r)
618 goto error_backoff;
619
620 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
621 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
622 args->offset_in_bo, args->map_size,
623 va_flags);
624 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 default:
626 break;
627 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800628 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100629 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
630 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800631
632error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100633 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800634
Junwei Zhangb85891b2017-01-16 13:59:01 +0800635error_unref:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300636 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 return r;
638}
639
640int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
641 struct drm_file *filp)
642{
643 struct drm_amdgpu_gem_op *args = data;
644 struct drm_gem_object *gobj;
645 struct amdgpu_bo *robj;
646 int r;
647
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100648 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 if (gobj == NULL) {
650 return -ENOENT;
651 }
652 robj = gem_to_amdgpu_bo(gobj);
653
654 r = amdgpu_bo_reserve(robj, false);
655 if (unlikely(r))
656 goto out;
657
658 switch (args->op) {
659 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
660 struct drm_amdgpu_gem_create_in info;
Christian König7ecc2452017-07-26 17:02:52 +0200661 void __user *out = u64_to_user_ptr(args->value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662
663 info.bo_size = robj->gem_base.size;
664 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Kent Russell6d7d9c52017-08-08 07:58:01 -0400665 info.domains = robj->preferred_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200667 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668 if (copy_to_user(out, &info, sizeof(info)))
669 r = -EFAULT;
670 break;
671 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200672 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000673 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
674 r = -EINVAL;
675 amdgpu_bo_unreserve(robj);
676 break;
677 }
Christian Königcc325d12016-02-08 11:08:35 +0100678 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400679 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200680 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 break;
682 }
Kent Russell6d7d9c52017-08-08 07:58:01 -0400683 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
Christian König1ea863f2015-12-18 22:13:12 +0100684 AMDGPU_GEM_DOMAIN_GTT |
685 AMDGPU_GEM_DOMAIN_CPU);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400686 robj->allowed_domains = robj->preferred_domains;
Christian König1ea863f2015-12-18 22:13:12 +0100687 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
688 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
689
Christian König4c28fb02015-08-28 17:27:54 +0200690 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400691 break;
692 default:
Christian König4c28fb02015-08-28 17:27:54 +0200693 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 r = -EINVAL;
695 }
696
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300698 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 return r;
700}
701
702int amdgpu_mode_dumb_create(struct drm_file *file_priv,
703 struct drm_device *dev,
704 struct drm_mode_create_dumb *args)
705{
706 struct amdgpu_device *adev = dev->dev_private;
707 struct drm_gem_object *gobj;
708 uint32_t handle;
709 int r;
710
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300711 args->pitch = amdgpu_align_pitch(adev, args->width,
712 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300713 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400714 args->size = ALIGN(args->size, PAGE_SIZE);
715
716 r = amdgpu_gem_object_create(adev, args->size, 0,
717 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400718 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
719 ttm_bo_type_device,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 &gobj);
721 if (r)
722 return -ENOMEM;
723
724 r = drm_gem_handle_create(file_priv, gobj, &handle);
725 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300726 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727 if (r) {
728 return r;
729 }
730 args->handle = handle;
731 return 0;
732}
733
734#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100735static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
736{
737 struct drm_gem_object *gobj = ptr;
738 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
739 struct seq_file *m = data;
740
741 unsigned domain;
742 const char *placement;
743 unsigned pin_count;
Christian Königb8e0e6e2017-06-26 15:19:30 +0200744 uint64_t offset;
Christian König7ea23562016-02-15 15:23:00 +0100745
746 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
747 switch (domain) {
748 case AMDGPU_GEM_DOMAIN_VRAM:
749 placement = "VRAM";
750 break;
751 case AMDGPU_GEM_DOMAIN_GTT:
752 placement = " GTT";
753 break;
754 case AMDGPU_GEM_DOMAIN_CPU:
755 default:
756 placement = " CPU";
757 break;
758 }
Christian Königb8e0e6e2017-06-26 15:19:30 +0200759 seq_printf(m, "\t0x%08x: %12ld byte %s",
760 id, amdgpu_bo_size(bo), placement);
761
762 offset = ACCESS_ONCE(bo->tbo.mem.start);
763 if (offset != AMDGPU_BO_INVALID_OFFSET)
764 seq_printf(m, " @ 0x%010Lx", offset);
Christian König7ea23562016-02-15 15:23:00 +0100765
766 pin_count = ACCESS_ONCE(bo->pin_count);
767 if (pin_count)
768 seq_printf(m, " pin count %d", pin_count);
769 seq_printf(m, "\n");
770
771 return 0;
772}
773
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400774static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
775{
776 struct drm_info_node *node = (struct drm_info_node *)m->private;
777 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100778 struct drm_file *file;
779 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200781 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100782 if (r)
783 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784
Christian König7ea23562016-02-15 15:23:00 +0100785 list_for_each_entry(file, &dev->filelist, lhead) {
786 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100787
Christian König7ea23562016-02-15 15:23:00 +0100788 /*
789 * Although we have a valid reference on file->pid, that does
790 * not guarantee that the task_struct who called get_pid() is
791 * still alive (e.g. get_pid(current) => fork() => exit()).
792 * Therefore, we need to protect this ->comm access using RCU.
793 */
794 rcu_read_lock();
795 task = pid_task(file->pid, PIDTYPE_PID);
796 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
797 task ? task->comm : "<unknown>");
798 rcu_read_unlock();
799
800 spin_lock(&file->table_lock);
801 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
802 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400803 }
Christian König7ea23562016-02-15 15:23:00 +0100804
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200805 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400806 return 0;
807}
808
Nils Wallménius06ab6832016-05-02 12:46:15 -0400809static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400810 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
811};
812#endif
813
814int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
815{
816#if defined(CONFIG_DEBUG_FS)
817 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
818#endif
819 return 0;
820}