blob: 92e9248ea8b1d640f04a6bfb0ada0bc5e58b2998 [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;
52 unsigned long max_size;
53 int r;
54
55 *obj = NULL;
56 /* At least align on page size */
57 if (alignment < PAGE_SIZE) {
58 alignment = PAGE_SIZE;
59 }
60
61 if (!(initial_domain & (AMDGPU_GEM_DOMAIN_GDS | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA))) {
62 /* Maximum bo size is the unpinned gtt size since we use the gtt to
63 * handle vram to system pool migrations.
64 */
65 max_size = adev->mc.gtt_size - adev->gart_pin_size;
66 if (size > max_size) {
67 DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
68 size >> 20, max_size >> 20);
69 return -ENOMEM;
70 }
71 }
72retry:
Christian König72d76682015-09-03 17:34:59 +020073 r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain,
74 flags, NULL, NULL, &robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 if (r) {
76 if (r != -ERESTARTSYS) {
77 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
78 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
79 goto retry;
80 }
81 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
82 size, initial_domain, alignment, r);
83 }
84 return r;
85 }
86 *obj = &robj->gem_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 return 0;
89}
90
Christian König418aa0c2016-02-15 16:59:57 +010091void amdgpu_gem_force_release(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092{
Christian König418aa0c2016-02-15 16:59:57 +010093 struct drm_device *ddev = adev->ddev;
94 struct drm_file *file;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095
Daniel Vetter1d2ac402016-04-26 19:29:41 +020096 mutex_lock(&ddev->filelist_mutex);
Christian König418aa0c2016-02-15 16:59:57 +010097
98 list_for_each_entry(file, &ddev->filelist, lhead) {
99 struct drm_gem_object *gobj;
100 int handle;
101
102 WARN_ONCE(1, "Still active user space clients!\n");
103 spin_lock(&file->table_lock);
104 idr_for_each_entry(&file->object_idr, gobj, handle) {
105 WARN_ONCE(1, "And also active allocations!\n");
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200106 drm_gem_object_unreference_unlocked(gobj);
Christian König418aa0c2016-02-15 16:59:57 +0100107 }
108 idr_destroy(&file->object_idr);
109 spin_unlock(&file->table_lock);
110 }
111
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200112 mutex_unlock(&ddev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113}
114
115/*
116 * Call from drm_gem_handle_create which appear in both new and open ioctl
117 * case.
118 */
Christian Königa7d64de2016-09-15 14:58:48 +0200119int amdgpu_gem_object_open(struct drm_gem_object *obj,
120 struct drm_file *file_priv)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121{
Christian König765e7fb2016-09-15 15:06:50 +0200122 struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200123 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
125 struct amdgpu_vm *vm = &fpriv->vm;
126 struct amdgpu_bo_va *bo_va;
127 int r;
Christian König765e7fb2016-09-15 15:06:50 +0200128 r = amdgpu_bo_reserve(abo, false);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800129 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131
Christian König765e7fb2016-09-15 15:06:50 +0200132 bo_va = amdgpu_vm_bo_find(vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 if (!bo_va) {
Christian König765e7fb2016-09-15 15:06:50 +0200134 bo_va = amdgpu_vm_bo_add(adev, vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 } else {
136 ++bo_va->ref_count;
137 }
Christian König765e7fb2016-09-15 15:06:50 +0200138 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400139 return 0;
140}
141
Christian König5a0f3b52017-04-21 10:05:56 +0200142static int amdgpu_gem_vm_check(void *param, struct amdgpu_bo *bo)
143{
144 /* if anything is swapped out don't swap it in here,
145 just abort and wait for the next CS */
146 if (!amdgpu_bo_gpu_accessible(bo))
147 return -ERESTARTSYS;
148
149 if (bo->shadow && !amdgpu_bo_gpu_accessible(bo->shadow))
150 return -ERESTARTSYS;
151
152 return 0;
153}
154
155static bool amdgpu_gem_vm_ready(struct amdgpu_device *adev,
156 struct amdgpu_vm *vm,
157 struct list_head *list)
158{
159 struct ttm_validate_buffer *entry;
160
161 list_for_each_entry(entry, list, head) {
162 struct amdgpu_bo *bo =
163 container_of(entry->bo, struct amdgpu_bo, tbo);
164 if (amdgpu_gem_vm_check(NULL, bo))
165 return false;
166 }
167
168 return !amdgpu_vm_validate_pt_bos(adev, vm, amdgpu_gem_vm_check, NULL);
169}
170
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171void amdgpu_gem_object_close(struct drm_gem_object *obj,
172 struct drm_file *file_priv)
173{
Christian Königb5a5ec52016-03-08 17:47:46 +0100174 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200175 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
177 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb5a5ec52016-03-08 17:47:46 +0100178
179 struct amdgpu_bo_list_entry vm_pd;
Christian König5a0f3b52017-04-21 10:05:56 +0200180 struct list_head list;
Christian Königb5a5ec52016-03-08 17:47:46 +0100181 struct ttm_validate_buffer tv;
182 struct ww_acquire_ctx ticket;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400183 struct amdgpu_bo_va *bo_va;
184 int r;
Christian Königb5a5ec52016-03-08 17:47:46 +0100185
186 INIT_LIST_HEAD(&list);
Christian Königb5a5ec52016-03-08 17:47:46 +0100187
188 tv.bo = &bo->tbo;
189 tv.shared = true;
190 list_add(&tv.head, &list);
191
192 amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
193
Christian König5a0f3b52017-04-21 10:05:56 +0200194 r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195 if (r) {
196 dev_err(adev->dev, "leaking bo va because "
197 "we fail to reserve bo (%d)\n", r);
198 return;
199 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100200 bo_va = amdgpu_vm_bo_find(vm, bo);
Christian König5a0f3b52017-04-21 10:05:56 +0200201 if (bo_va && --bo_va->ref_count == 0) {
202 amdgpu_vm_bo_rmv(adev, bo_va);
203
204 if (amdgpu_gem_vm_ready(adev, vm, &list)) {
205 struct dma_fence *fence = NULL;
Nicolai Hähnle23e05632017-03-23 19:34:11 +0100206
207 r = amdgpu_vm_clear_freed(adev, vm, &fence);
208 if (unlikely(r)) {
209 dev_err(adev->dev, "failed to clear page "
210 "tables on GEM object close (%d)\n", r);
211 }
212
213 if (fence) {
214 amdgpu_bo_fence(bo, fence, true);
215 dma_fence_put(fence);
216 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400217 }
218 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100219 ttm_eu_backoff_reservation(&ticket, &list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220}
221
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400222/*
223 * GEM ioctls.
224 */
225int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
226 struct drm_file *filp)
227{
228 struct amdgpu_device *adev = dev->dev_private;
229 union drm_amdgpu_gem_create *args = data;
230 uint64_t size = args->in.bo_size;
231 struct drm_gem_object *gobj;
232 uint32_t handle;
233 bool kernel = false;
234 int r;
235
Alex Deucher834e0f82017-03-08 17:40:17 -0500236 /* reject invalid gem flags */
237 if (args->in.domain_flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
238 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
239 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
240 AMDGPU_GEM_CREATE_VRAM_CLEARED|
241 AMDGPU_GEM_CREATE_SHADOW |
Christian Königa022c542017-05-08 15:14:54 +0200242 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS))
243 return -EINVAL;
244
Alex Deucher834e0f82017-03-08 17:40:17 -0500245 /* reject invalid gem domains */
246 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
247 AMDGPU_GEM_DOMAIN_GTT |
248 AMDGPU_GEM_DOMAIN_VRAM |
249 AMDGPU_GEM_DOMAIN_GDS |
250 AMDGPU_GEM_DOMAIN_GWS |
Christian Königa022c542017-05-08 15:14:54 +0200251 AMDGPU_GEM_DOMAIN_OA))
252 return -EINVAL;
Alex Deucher834e0f82017-03-08 17:40:17 -0500253
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254 /* create a gem object to contain this object in */
255 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
256 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
257 kernel = true;
258 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
259 size = size << AMDGPU_GDS_SHIFT;
260 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
261 size = size << AMDGPU_GWS_SHIFT;
262 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
263 size = size << AMDGPU_OA_SHIFT;
Christian Königa022c542017-05-08 15:14:54 +0200264 else
265 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 }
267 size = roundup(size, PAGE_SIZE);
268
269 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
270 (u32)(0xffffffff & args->in.domains),
271 args->in.domain_flags,
272 kernel, &gobj);
273 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200274 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400275
276 r = drm_gem_handle_create(filp, gobj, &handle);
277 /* drop reference from allocate - handle holds it now */
278 drm_gem_object_unreference_unlocked(gobj);
279 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200280 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281
282 memset(args, 0, sizeof(*args));
283 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285}
286
287int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
288 struct drm_file *filp)
289{
290 struct amdgpu_device *adev = dev->dev_private;
291 struct drm_amdgpu_gem_userptr *args = data;
292 struct drm_gem_object *gobj;
293 struct amdgpu_bo *bo;
294 uint32_t handle;
295 int r;
296
297 if (offset_in_page(args->addr | args->size))
298 return -EINVAL;
299
300 /* reject unknown flag values */
301 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
302 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
303 AMDGPU_GEM_USERPTR_REGISTER))
304 return -EINVAL;
305
Christian König358c2582016-03-11 15:29:27 +0100306 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
307 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400308
Christian König358c2582016-03-11 15:29:27 +0100309 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400310 return -EACCES;
311 }
312
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400313 /* create a gem object to contain this object in */
314 r = amdgpu_gem_object_create(adev, args->size, 0,
315 AMDGPU_GEM_DOMAIN_CPU, 0,
316 0, &gobj);
317 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200318 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400319
320 bo = gem_to_amdgpu_bo(gobj);
Christian König1ea863f2015-12-18 22:13:12 +0100321 bo->prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
322 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400323 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
324 if (r)
325 goto release_object;
326
327 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
328 r = amdgpu_mn_register(bo, args->addr);
329 if (r)
330 goto release_object;
331 }
332
333 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
334 down_read(&current->mm->mmap_sem);
Christian König2f568db2016-02-23 12:36:59 +0100335
336 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
337 bo->tbo.ttm->pages);
338 if (r)
339 goto unlock_mmap_sem;
340
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400341 r = amdgpu_bo_reserve(bo, true);
Christian König2f568db2016-02-23 12:36:59 +0100342 if (r)
343 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400344
345 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
346 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
347 amdgpu_bo_unreserve(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 if (r)
Christian König2f568db2016-02-23 12:36:59 +0100349 goto free_pages;
350
351 up_read(&current->mm->mmap_sem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400352 }
353
354 r = drm_gem_handle_create(filp, gobj, &handle);
355 /* drop reference from allocate - handle holds it now */
356 drm_gem_object_unreference_unlocked(gobj);
357 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200358 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400359
360 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400361 return 0;
362
Christian König2f568db2016-02-23 12:36:59 +0100363free_pages:
364 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages, false);
365
366unlock_mmap_sem:
367 up_read(&current->mm->mmap_sem);
368
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400369release_object:
370 drm_gem_object_unreference_unlocked(gobj);
371
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400372 return r;
373}
374
375int amdgpu_mode_dumb_mmap(struct drm_file *filp,
376 struct drm_device *dev,
377 uint32_t handle, uint64_t *offset_p)
378{
379 struct drm_gem_object *gobj;
380 struct amdgpu_bo *robj;
381
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100382 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400383 if (gobj == NULL) {
384 return -ENOENT;
385 }
386 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100387 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200388 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389 drm_gem_object_unreference_unlocked(gobj);
390 return -EPERM;
391 }
392 *offset_p = amdgpu_bo_mmap_offset(robj);
393 drm_gem_object_unreference_unlocked(gobj);
394 return 0;
395}
396
397int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
398 struct drm_file *filp)
399{
400 union drm_amdgpu_gem_mmap *args = data;
401 uint32_t handle = args->in.handle;
402 memset(args, 0, sizeof(*args));
403 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
404}
405
406/**
407 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
408 *
409 * @timeout_ns: timeout in ns
410 *
411 * Calculate the timeout in jiffies from an absolute timeout in ns.
412 */
413unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
414{
415 unsigned long timeout_jiffies;
416 ktime_t timeout;
417
418 /* clamp timeout if it's to large */
419 if (((int64_t)timeout_ns) < 0)
420 return MAX_SCHEDULE_TIMEOUT;
421
Christian König0f117702015-07-08 16:58:48 +0200422 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400423 if (ktime_to_ns(timeout) < 0)
424 return 0;
425
426 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
427 /* clamp timeout to avoid unsigned-> signed overflow */
428 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
429 return MAX_SCHEDULE_TIMEOUT - 1;
430
431 return timeout_jiffies;
432}
433
434int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
435 struct drm_file *filp)
436{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437 union drm_amdgpu_gem_wait_idle *args = data;
438 struct drm_gem_object *gobj;
439 struct amdgpu_bo *robj;
440 uint32_t handle = args->in.handle;
441 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
442 int r = 0;
443 long ret;
444
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100445 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400446 if (gobj == NULL) {
447 return -ENOENT;
448 }
449 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100450 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
451 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452
453 /* ret == 0 means not signaled,
454 * ret > 0 means signaled
455 * ret < 0 means interrupted before timeout
456 */
457 if (ret >= 0) {
458 memset(args, 0, sizeof(*args));
459 args->out.status = (ret == 0);
460 } else
461 r = ret;
462
463 drm_gem_object_unreference_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 return r;
465}
466
467int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
468 struct drm_file *filp)
469{
470 struct drm_amdgpu_gem_metadata *args = data;
471 struct drm_gem_object *gobj;
472 struct amdgpu_bo *robj;
473 int r = -1;
474
475 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100476 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400477 if (gobj == NULL)
478 return -ENOENT;
479 robj = gem_to_amdgpu_bo(gobj);
480
481 r = amdgpu_bo_reserve(robj, false);
482 if (unlikely(r != 0))
483 goto out;
484
485 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
486 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
487 r = amdgpu_bo_get_metadata(robj, args->data.data,
488 sizeof(args->data.data),
489 &args->data.data_size_bytes,
490 &args->data.flags);
491 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300492 if (args->data.data_size_bytes > sizeof(args->data.data)) {
493 r = -EINVAL;
494 goto unreserve;
495 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
497 if (!r)
498 r = amdgpu_bo_set_metadata(robj, args->data.data,
499 args->data.data_size_bytes,
500 args->data.flags);
501 }
502
Dan Carpenter0913eab2015-09-23 14:00:35 +0300503unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 amdgpu_bo_unreserve(robj);
505out:
506 drm_gem_object_unreference_unlocked(gobj);
507 return r;
508}
509
510/**
511 * amdgpu_gem_va_update_vm -update the bo_va in its VM
512 *
513 * @adev: amdgpu_device pointer
Christian Königdc54d3d2017-03-13 10:13:38 +0100514 * @vm: vm to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100516 * @list: validation list
Christian Königdc54d3d2017-03-13 10:13:38 +0100517 * @operation: map, unmap or clear
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100519 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520 * vital here, so they are not reported back to userspace.
521 */
522static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königdc54d3d2017-03-13 10:13:38 +0100523 struct amdgpu_vm *vm,
Christian Königf7da30d2016-09-28 12:03:04 +0200524 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100525 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200526 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527{
Christian König2ffdaaf2017-01-27 15:58:43 +0100528 int r = -ERESTARTSYS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529
Christian König5a0f3b52017-04-21 10:05:56 +0200530 if (!amdgpu_gem_vm_ready(adev, vm, list))
Christian König2ffdaaf2017-01-27 15:58:43 +0100531 goto error;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800532
Christian König194d2162016-10-12 15:13:52 +0200533 r = amdgpu_vm_update_directories(adev, vm);
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800534 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100535 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100537 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400538 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100539 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800540
Christian König80f95c52017-03-13 10:13:39 +0100541 if (operation == AMDGPU_VA_OP_MAP ||
542 operation == AMDGPU_VA_OP_REPLACE)
Flora Cui05dcb5c2016-09-22 11:34:47 +0800543 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544
Christian König2ffdaaf2017-01-27 15:58:43 +0100545error:
Christian König68fdd3d2015-06-16 14:50:02 +0200546 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
548}
549
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
551 struct drm_file *filp)
552{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800553 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
554 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
Alex Xie66e02bc2017-02-14 12:04:52 -0500555 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800556 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
557 AMDGPU_VM_PAGE_PRT;
558
Christian König34b5f6a2015-06-08 15:03:00 +0200559 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 struct drm_gem_object *gobj;
561 struct amdgpu_device *adev = dev->dev_private;
562 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200563 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200565 struct amdgpu_bo_list_entry vm_pd;
566 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800567 struct ww_acquire_ctx ticket;
Christian Königd7d29552017-01-30 10:24:13 +0100568 struct list_head list;
Alex Xie54635452017-02-14 12:22:57 -0500569 uint64_t va_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 int r = 0;
571
Christian König34b5f6a2015-06-08 15:03:00 +0200572 if (!adev->vm_manager.enabled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 return -ENOTTY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574
Christian König34b5f6a2015-06-08 15:03:00 +0200575 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576 dev_err(&dev->pdev->dev,
577 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200578 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 return -EINVAL;
581 }
582
Junwei Zhangb85891b2017-01-16 13:59:01 +0800583 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
584 dev_err(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
585 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 return -EINVAL;
587 }
588
Christian König34b5f6a2015-06-08 15:03:00 +0200589 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400590 case AMDGPU_VA_OP_MAP:
591 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100592 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100593 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 break;
595 default:
596 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200597 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598 return -EINVAL;
599 }
600
Chunming Zhou49b02b12015-11-13 14:18:38 +0800601 INIT_LIST_HEAD(&list);
Christian Königdc54d3d2017-03-13 10:13:38 +0100602 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
603 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800604 gobj = drm_gem_object_lookup(filp, args->handle);
605 if (gobj == NULL)
606 return -ENOENT;
607 abo = gem_to_amdgpu_bo(gobj);
608 tv.bo = &abo->tbo;
609 tv.shared = false;
610 list_add(&tv.head, &list);
611 } else {
612 gobj = NULL;
613 abo = NULL;
614 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800615
Christian Königb88c8792016-09-28 16:33:01 +0200616 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100617
Christian Königd7d29552017-01-30 10:24:13 +0100618 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800619 if (r)
620 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200621
Junwei Zhangb85891b2017-01-16 13:59:01 +0800622 if (abo) {
623 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
624 if (!bo_va) {
625 r = -ENOENT;
626 goto error_backoff;
627 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100628 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800629 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100630 } else {
631 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400632 }
633
Christian König34b5f6a2015-06-08 15:03:00 +0200634 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635 case AMDGPU_VA_OP_MAP:
Christian König663e4572017-03-13 10:13:37 +0100636 r = amdgpu_vm_alloc_pts(adev, bo_va->vm, args->va_address,
637 args->map_size);
638 if (r)
639 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500640
Christian König663e4572017-03-13 10:13:37 +0100641 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200642 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
643 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200644 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 break;
646 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200647 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100649
650 case AMDGPU_VA_OP_CLEAR:
651 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
652 args->va_address,
653 args->map_size);
654 break;
Christian König80f95c52017-03-13 10:13:39 +0100655 case AMDGPU_VA_OP_REPLACE:
656 r = amdgpu_vm_alloc_pts(adev, bo_va->vm, args->va_address,
657 args->map_size);
658 if (r)
659 goto error_backoff;
660
661 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
662 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
663 args->offset_in_bo, args->map_size,
664 va_flags);
665 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666 default:
667 break;
668 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800669 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100670 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
671 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800672
673error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100674 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800675
Junwei Zhangb85891b2017-01-16 13:59:01 +0800676error_unref:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 drm_gem_object_unreference_unlocked(gobj);
678 return r;
679}
680
681int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
682 struct drm_file *filp)
683{
684 struct drm_amdgpu_gem_op *args = data;
685 struct drm_gem_object *gobj;
686 struct amdgpu_bo *robj;
687 int r;
688
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100689 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 if (gobj == NULL) {
691 return -ENOENT;
692 }
693 robj = gem_to_amdgpu_bo(gobj);
694
695 r = amdgpu_bo_reserve(robj, false);
696 if (unlikely(r))
697 goto out;
698
699 switch (args->op) {
700 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
701 struct drm_amdgpu_gem_create_in info;
Alex Xieec2c4672017-04-05 16:33:00 -0400702 void __user *out = (void __user *)(uintptr_t)args->value;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703
704 info.bo_size = robj->gem_base.size;
705 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Christian König1ea863f2015-12-18 22:13:12 +0100706 info.domains = robj->prefered_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200708 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 if (copy_to_user(out, &info, sizeof(info)))
710 r = -EFAULT;
711 break;
712 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200713 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000714 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
715 r = -EINVAL;
716 amdgpu_bo_unreserve(robj);
717 break;
718 }
Christian Königcc325d12016-02-08 11:08:35 +0100719 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200721 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722 break;
723 }
Christian König1ea863f2015-12-18 22:13:12 +0100724 robj->prefered_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
725 AMDGPU_GEM_DOMAIN_GTT |
726 AMDGPU_GEM_DOMAIN_CPU);
727 robj->allowed_domains = robj->prefered_domains;
728 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
729 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
730
Christian König4c28fb02015-08-28 17:27:54 +0200731 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 break;
733 default:
Christian König4c28fb02015-08-28 17:27:54 +0200734 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400735 r = -EINVAL;
736 }
737
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738out:
739 drm_gem_object_unreference_unlocked(gobj);
740 return r;
741}
742
743int amdgpu_mode_dumb_create(struct drm_file *file_priv,
744 struct drm_device *dev,
745 struct drm_mode_create_dumb *args)
746{
747 struct amdgpu_device *adev = dev->dev_private;
748 struct drm_gem_object *gobj;
749 uint32_t handle;
750 int r;
751
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300752 args->pitch = amdgpu_align_pitch(adev, args->width,
753 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300754 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400755 args->size = ALIGN(args->size, PAGE_SIZE);
756
757 r = amdgpu_gem_object_create(adev, args->size, 0,
758 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400759 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
760 ttm_bo_type_device,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 &gobj);
762 if (r)
763 return -ENOMEM;
764
765 r = drm_gem_handle_create(file_priv, gobj, &handle);
766 /* drop reference from allocate - handle holds it now */
767 drm_gem_object_unreference_unlocked(gobj);
768 if (r) {
769 return r;
770 }
771 args->handle = handle;
772 return 0;
773}
774
775#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100776static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
777{
778 struct drm_gem_object *gobj = ptr;
779 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
780 struct seq_file *m = data;
781
782 unsigned domain;
783 const char *placement;
784 unsigned pin_count;
785
786 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
787 switch (domain) {
788 case AMDGPU_GEM_DOMAIN_VRAM:
789 placement = "VRAM";
790 break;
791 case AMDGPU_GEM_DOMAIN_GTT:
792 placement = " GTT";
793 break;
794 case AMDGPU_GEM_DOMAIN_CPU:
795 default:
796 placement = " CPU";
797 break;
798 }
799 seq_printf(m, "\t0x%08x: %12ld byte %s @ 0x%010Lx",
800 id, amdgpu_bo_size(bo), placement,
801 amdgpu_bo_gpu_offset(bo));
802
803 pin_count = ACCESS_ONCE(bo->pin_count);
804 if (pin_count)
805 seq_printf(m, " pin count %d", pin_count);
806 seq_printf(m, "\n");
807
808 return 0;
809}
810
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400811static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
812{
813 struct drm_info_node *node = (struct drm_info_node *)m->private;
814 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100815 struct drm_file *file;
816 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400817
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200818 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100819 if (r)
820 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400821
Christian König7ea23562016-02-15 15:23:00 +0100822 list_for_each_entry(file, &dev->filelist, lhead) {
823 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100824
Christian König7ea23562016-02-15 15:23:00 +0100825 /*
826 * Although we have a valid reference on file->pid, that does
827 * not guarantee that the task_struct who called get_pid() is
828 * still alive (e.g. get_pid(current) => fork() => exit()).
829 * Therefore, we need to protect this ->comm access using RCU.
830 */
831 rcu_read_lock();
832 task = pid_task(file->pid, PIDTYPE_PID);
833 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
834 task ? task->comm : "<unknown>");
835 rcu_read_unlock();
836
837 spin_lock(&file->table_lock);
838 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
839 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840 }
Christian König7ea23562016-02-15 15:23:00 +0100841
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200842 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400843 return 0;
844}
845
Nils Wallménius06ab6832016-05-02 12:46:15 -0400846static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400847 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
848};
849#endif
850
851int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
852{
853#if defined(CONFIG_DEBUG_FS)
854 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
855#endif
856 return 0;
857}