blob: 94cb91cf93eb8b0b6652b6a271ca56063e0ab912 [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
222static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r)
223{
224 if (r == -EDEADLK) {
225 r = amdgpu_gpu_reset(adev);
226 if (!r)
227 r = -EAGAIN;
228 }
229 return r;
230}
231
232/*
233 * GEM ioctls.
234 */
235int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
236 struct drm_file *filp)
237{
238 struct amdgpu_device *adev = dev->dev_private;
239 union drm_amdgpu_gem_create *args = data;
240 uint64_t size = args->in.bo_size;
241 struct drm_gem_object *gobj;
242 uint32_t handle;
243 bool kernel = false;
244 int r;
245
Alex Deucher834e0f82017-03-08 17:40:17 -0500246 /* reject invalid gem flags */
247 if (args->in.domain_flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
248 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
249 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
250 AMDGPU_GEM_CREATE_VRAM_CLEARED|
251 AMDGPU_GEM_CREATE_SHADOW |
252 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) {
253 r = -EINVAL;
254 goto error_unlock;
255 }
256 /* reject invalid gem domains */
257 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
258 AMDGPU_GEM_DOMAIN_GTT |
259 AMDGPU_GEM_DOMAIN_VRAM |
260 AMDGPU_GEM_DOMAIN_GDS |
261 AMDGPU_GEM_DOMAIN_GWS |
262 AMDGPU_GEM_DOMAIN_OA)) {
263 r = -EINVAL;
264 goto error_unlock;
265 }
266
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267 /* create a gem object to contain this object in */
268 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
269 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
270 kernel = true;
271 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
272 size = size << AMDGPU_GDS_SHIFT;
273 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
274 size = size << AMDGPU_GWS_SHIFT;
275 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
276 size = size << AMDGPU_OA_SHIFT;
277 else {
278 r = -EINVAL;
279 goto error_unlock;
280 }
281 }
282 size = roundup(size, PAGE_SIZE);
283
284 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
285 (u32)(0xffffffff & args->in.domains),
286 args->in.domain_flags,
287 kernel, &gobj);
288 if (r)
289 goto error_unlock;
290
291 r = drm_gem_handle_create(filp, gobj, &handle);
292 /* drop reference from allocate - handle holds it now */
293 drm_gem_object_unreference_unlocked(gobj);
294 if (r)
295 goto error_unlock;
296
297 memset(args, 0, sizeof(*args));
298 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299 return 0;
300
301error_unlock:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400302 r = amdgpu_gem_handle_lockup(adev, r);
303 return r;
304}
305
306int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
307 struct drm_file *filp)
308{
309 struct amdgpu_device *adev = dev->dev_private;
310 struct drm_amdgpu_gem_userptr *args = data;
311 struct drm_gem_object *gobj;
312 struct amdgpu_bo *bo;
313 uint32_t handle;
314 int r;
315
316 if (offset_in_page(args->addr | args->size))
317 return -EINVAL;
318
319 /* reject unknown flag values */
320 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
321 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
322 AMDGPU_GEM_USERPTR_REGISTER))
323 return -EINVAL;
324
Christian König358c2582016-03-11 15:29:27 +0100325 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
326 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400327
Christian König358c2582016-03-11 15:29:27 +0100328 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400329 return -EACCES;
330 }
331
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332 /* create a gem object to contain this object in */
333 r = amdgpu_gem_object_create(adev, args->size, 0,
334 AMDGPU_GEM_DOMAIN_CPU, 0,
335 0, &gobj);
336 if (r)
337 goto handle_lockup;
338
339 bo = gem_to_amdgpu_bo(gobj);
Christian König1ea863f2015-12-18 22:13:12 +0100340 bo->prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
341 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
343 if (r)
344 goto release_object;
345
346 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
347 r = amdgpu_mn_register(bo, args->addr);
348 if (r)
349 goto release_object;
350 }
351
352 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
353 down_read(&current->mm->mmap_sem);
Christian König2f568db2016-02-23 12:36:59 +0100354
355 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
356 bo->tbo.ttm->pages);
357 if (r)
358 goto unlock_mmap_sem;
359
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400360 r = amdgpu_bo_reserve(bo, true);
Christian König2f568db2016-02-23 12:36:59 +0100361 if (r)
362 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400363
364 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
365 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
366 amdgpu_bo_unreserve(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400367 if (r)
Christian König2f568db2016-02-23 12:36:59 +0100368 goto free_pages;
369
370 up_read(&current->mm->mmap_sem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400371 }
372
373 r = drm_gem_handle_create(filp, gobj, &handle);
374 /* drop reference from allocate - handle holds it now */
375 drm_gem_object_unreference_unlocked(gobj);
376 if (r)
377 goto handle_lockup;
378
379 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380 return 0;
381
Christian König2f568db2016-02-23 12:36:59 +0100382free_pages:
383 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages, false);
384
385unlock_mmap_sem:
386 up_read(&current->mm->mmap_sem);
387
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388release_object:
389 drm_gem_object_unreference_unlocked(gobj);
390
391handle_lockup:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400392 r = amdgpu_gem_handle_lockup(adev, r);
393
394 return r;
395}
396
397int amdgpu_mode_dumb_mmap(struct drm_file *filp,
398 struct drm_device *dev,
399 uint32_t handle, uint64_t *offset_p)
400{
401 struct drm_gem_object *gobj;
402 struct amdgpu_bo *robj;
403
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100404 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405 if (gobj == NULL) {
406 return -ENOENT;
407 }
408 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100409 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200410 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400411 drm_gem_object_unreference_unlocked(gobj);
412 return -EPERM;
413 }
414 *offset_p = amdgpu_bo_mmap_offset(robj);
415 drm_gem_object_unreference_unlocked(gobj);
416 return 0;
417}
418
419int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
420 struct drm_file *filp)
421{
422 union drm_amdgpu_gem_mmap *args = data;
423 uint32_t handle = args->in.handle;
424 memset(args, 0, sizeof(*args));
425 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
426}
427
428/**
429 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
430 *
431 * @timeout_ns: timeout in ns
432 *
433 * Calculate the timeout in jiffies from an absolute timeout in ns.
434 */
435unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
436{
437 unsigned long timeout_jiffies;
438 ktime_t timeout;
439
440 /* clamp timeout if it's to large */
441 if (((int64_t)timeout_ns) < 0)
442 return MAX_SCHEDULE_TIMEOUT;
443
Christian König0f117702015-07-08 16:58:48 +0200444 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445 if (ktime_to_ns(timeout) < 0)
446 return 0;
447
448 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
449 /* clamp timeout to avoid unsigned-> signed overflow */
450 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
451 return MAX_SCHEDULE_TIMEOUT - 1;
452
453 return timeout_jiffies;
454}
455
456int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
457 struct drm_file *filp)
458{
459 struct amdgpu_device *adev = dev->dev_private;
460 union drm_amdgpu_gem_wait_idle *args = data;
461 struct drm_gem_object *gobj;
462 struct amdgpu_bo *robj;
463 uint32_t handle = args->in.handle;
464 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
465 int r = 0;
466 long ret;
467
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100468 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469 if (gobj == NULL) {
470 return -ENOENT;
471 }
472 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100473 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
474 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475
476 /* ret == 0 means not signaled,
477 * ret > 0 means signaled
478 * ret < 0 means interrupted before timeout
479 */
480 if (ret >= 0) {
481 memset(args, 0, sizeof(*args));
482 args->out.status = (ret == 0);
483 } else
484 r = ret;
485
486 drm_gem_object_unreference_unlocked(gobj);
487 r = amdgpu_gem_handle_lockup(adev, r);
488 return r;
489}
490
491int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
492 struct drm_file *filp)
493{
494 struct drm_amdgpu_gem_metadata *args = data;
495 struct drm_gem_object *gobj;
496 struct amdgpu_bo *robj;
497 int r = -1;
498
499 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100500 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501 if (gobj == NULL)
502 return -ENOENT;
503 robj = gem_to_amdgpu_bo(gobj);
504
505 r = amdgpu_bo_reserve(robj, false);
506 if (unlikely(r != 0))
507 goto out;
508
509 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
510 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
511 r = amdgpu_bo_get_metadata(robj, args->data.data,
512 sizeof(args->data.data),
513 &args->data.data_size_bytes,
514 &args->data.flags);
515 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300516 if (args->data.data_size_bytes > sizeof(args->data.data)) {
517 r = -EINVAL;
518 goto unreserve;
519 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
521 if (!r)
522 r = amdgpu_bo_set_metadata(robj, args->data.data,
523 args->data.data_size_bytes,
524 args->data.flags);
525 }
526
Dan Carpenter0913eab2015-09-23 14:00:35 +0300527unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528 amdgpu_bo_unreserve(robj);
529out:
530 drm_gem_object_unreference_unlocked(gobj);
531 return r;
532}
533
534/**
535 * amdgpu_gem_va_update_vm -update the bo_va in its VM
536 *
537 * @adev: amdgpu_device pointer
Christian Königdc54d3d2017-03-13 10:13:38 +0100538 * @vm: vm to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100540 * @list: validation list
Christian Königdc54d3d2017-03-13 10:13:38 +0100541 * @operation: map, unmap or clear
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100543 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 * vital here, so they are not reported back to userspace.
545 */
546static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königdc54d3d2017-03-13 10:13:38 +0100547 struct amdgpu_vm *vm,
Christian Königf7da30d2016-09-28 12:03:04 +0200548 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100549 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200550 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551{
Christian König2ffdaaf2017-01-27 15:58:43 +0100552 int r = -ERESTARTSYS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553
Christian König5a0f3b52017-04-21 10:05:56 +0200554 if (!amdgpu_gem_vm_ready(adev, vm, list))
Christian König2ffdaaf2017-01-27 15:58:43 +0100555 goto error;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800556
Christian König194d2162016-10-12 15:13:52 +0200557 r = amdgpu_vm_update_directories(adev, vm);
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800558 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100559 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100561 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100563 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800564
Christian König80f95c52017-03-13 10:13:39 +0100565 if (operation == AMDGPU_VA_OP_MAP ||
566 operation == AMDGPU_VA_OP_REPLACE)
Flora Cui05dcb5c2016-09-22 11:34:47 +0800567 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400568
Christian König2ffdaaf2017-01-27 15:58:43 +0100569error:
Christian König68fdd3d2015-06-16 14:50:02 +0200570 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
572}
573
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
575 struct drm_file *filp)
576{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800577 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
578 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
Alex Xie66e02bc2017-02-14 12:04:52 -0500579 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800580 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
581 AMDGPU_VM_PAGE_PRT;
582
Christian König34b5f6a2015-06-08 15:03:00 +0200583 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 struct drm_gem_object *gobj;
585 struct amdgpu_device *adev = dev->dev_private;
586 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200587 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200589 struct amdgpu_bo_list_entry vm_pd;
590 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800591 struct ww_acquire_ctx ticket;
Christian Königd7d29552017-01-30 10:24:13 +0100592 struct list_head list;
Alex Xie54635452017-02-14 12:22:57 -0500593 uint64_t va_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 int r = 0;
595
Christian König34b5f6a2015-06-08 15:03:00 +0200596 if (!adev->vm_manager.enabled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597 return -ENOTTY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598
Christian König34b5f6a2015-06-08 15:03:00 +0200599 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600 dev_err(&dev->pdev->dev,
601 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200602 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 return -EINVAL;
605 }
606
Junwei Zhangb85891b2017-01-16 13:59:01 +0800607 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
608 dev_err(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
609 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610 return -EINVAL;
611 }
612
Christian König34b5f6a2015-06-08 15:03:00 +0200613 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 case AMDGPU_VA_OP_MAP:
615 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100616 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100617 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 break;
619 default:
620 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200621 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622 return -EINVAL;
623 }
624
Chunming Zhou49b02b12015-11-13 14:18:38 +0800625 INIT_LIST_HEAD(&list);
Christian Königdc54d3d2017-03-13 10:13:38 +0100626 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
627 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800628 gobj = drm_gem_object_lookup(filp, args->handle);
629 if (gobj == NULL)
630 return -ENOENT;
631 abo = gem_to_amdgpu_bo(gobj);
632 tv.bo = &abo->tbo;
633 tv.shared = false;
634 list_add(&tv.head, &list);
635 } else {
636 gobj = NULL;
637 abo = NULL;
638 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800639
Christian Königb88c8792016-09-28 16:33:01 +0200640 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100641
Christian Königd7d29552017-01-30 10:24:13 +0100642 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800643 if (r)
644 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200645
Junwei Zhangb85891b2017-01-16 13:59:01 +0800646 if (abo) {
647 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
648 if (!bo_va) {
649 r = -ENOENT;
650 goto error_backoff;
651 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100652 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800653 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100654 } else {
655 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 }
657
Christian König34b5f6a2015-06-08 15:03:00 +0200658 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 case AMDGPU_VA_OP_MAP:
Christian König663e4572017-03-13 10:13:37 +0100660 r = amdgpu_vm_alloc_pts(adev, bo_va->vm, args->va_address,
661 args->map_size);
662 if (r)
663 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500664
Christian König663e4572017-03-13 10:13:37 +0100665 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200666 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
667 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200668 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400669 break;
670 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200671 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100673
674 case AMDGPU_VA_OP_CLEAR:
675 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
676 args->va_address,
677 args->map_size);
678 break;
Christian König80f95c52017-03-13 10:13:39 +0100679 case AMDGPU_VA_OP_REPLACE:
680 r = amdgpu_vm_alloc_pts(adev, bo_va->vm, args->va_address,
681 args->map_size);
682 if (r)
683 goto error_backoff;
684
685 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
686 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
687 args->offset_in_bo, args->map_size,
688 va_flags);
689 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 default:
691 break;
692 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800693 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100694 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
695 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800696
697error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100698 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800699
Junwei Zhangb85891b2017-01-16 13:59:01 +0800700error_unref:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 drm_gem_object_unreference_unlocked(gobj);
702 return r;
703}
704
705int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
706 struct drm_file *filp)
707{
708 struct drm_amdgpu_gem_op *args = data;
709 struct drm_gem_object *gobj;
710 struct amdgpu_bo *robj;
711 int r;
712
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100713 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400714 if (gobj == NULL) {
715 return -ENOENT;
716 }
717 robj = gem_to_amdgpu_bo(gobj);
718
719 r = amdgpu_bo_reserve(robj, false);
720 if (unlikely(r))
721 goto out;
722
723 switch (args->op) {
724 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
725 struct drm_amdgpu_gem_create_in info;
Alex Xieec2c4672017-04-05 16:33:00 -0400726 void __user *out = (void __user *)(uintptr_t)args->value;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727
728 info.bo_size = robj->gem_base.size;
729 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Christian König1ea863f2015-12-18 22:13:12 +0100730 info.domains = robj->prefered_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200732 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400733 if (copy_to_user(out, &info, sizeof(info)))
734 r = -EFAULT;
735 break;
736 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200737 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000738 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
739 r = -EINVAL;
740 amdgpu_bo_unreserve(robj);
741 break;
742 }
Christian Königcc325d12016-02-08 11:08:35 +0100743 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200745 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746 break;
747 }
Christian König1ea863f2015-12-18 22:13:12 +0100748 robj->prefered_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
749 AMDGPU_GEM_DOMAIN_GTT |
750 AMDGPU_GEM_DOMAIN_CPU);
751 robj->allowed_domains = robj->prefered_domains;
752 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
753 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
754
Christian König4c28fb02015-08-28 17:27:54 +0200755 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756 break;
757 default:
Christian König4c28fb02015-08-28 17:27:54 +0200758 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759 r = -EINVAL;
760 }
761
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762out:
763 drm_gem_object_unreference_unlocked(gobj);
764 return r;
765}
766
767int amdgpu_mode_dumb_create(struct drm_file *file_priv,
768 struct drm_device *dev,
769 struct drm_mode_create_dumb *args)
770{
771 struct amdgpu_device *adev = dev->dev_private;
772 struct drm_gem_object *gobj;
773 uint32_t handle;
774 int r;
775
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300776 args->pitch = amdgpu_align_pitch(adev, args->width,
777 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300778 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779 args->size = ALIGN(args->size, PAGE_SIZE);
780
781 r = amdgpu_gem_object_create(adev, args->size, 0,
782 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400783 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
784 ttm_bo_type_device,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785 &gobj);
786 if (r)
787 return -ENOMEM;
788
789 r = drm_gem_handle_create(file_priv, gobj, &handle);
790 /* drop reference from allocate - handle holds it now */
791 drm_gem_object_unreference_unlocked(gobj);
792 if (r) {
793 return r;
794 }
795 args->handle = handle;
796 return 0;
797}
798
799#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100800static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
801{
802 struct drm_gem_object *gobj = ptr;
803 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
804 struct seq_file *m = data;
805
806 unsigned domain;
807 const char *placement;
808 unsigned pin_count;
809
810 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
811 switch (domain) {
812 case AMDGPU_GEM_DOMAIN_VRAM:
813 placement = "VRAM";
814 break;
815 case AMDGPU_GEM_DOMAIN_GTT:
816 placement = " GTT";
817 break;
818 case AMDGPU_GEM_DOMAIN_CPU:
819 default:
820 placement = " CPU";
821 break;
822 }
823 seq_printf(m, "\t0x%08x: %12ld byte %s @ 0x%010Lx",
824 id, amdgpu_bo_size(bo), placement,
825 amdgpu_bo_gpu_offset(bo));
826
827 pin_count = ACCESS_ONCE(bo->pin_count);
828 if (pin_count)
829 seq_printf(m, " pin count %d", pin_count);
830 seq_printf(m, "\n");
831
832 return 0;
833}
834
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400835static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
836{
837 struct drm_info_node *node = (struct drm_info_node *)m->private;
838 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100839 struct drm_file *file;
840 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200842 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100843 if (r)
844 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400845
Christian König7ea23562016-02-15 15:23:00 +0100846 list_for_each_entry(file, &dev->filelist, lhead) {
847 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100848
Christian König7ea23562016-02-15 15:23:00 +0100849 /*
850 * Although we have a valid reference on file->pid, that does
851 * not guarantee that the task_struct who called get_pid() is
852 * still alive (e.g. get_pid(current) => fork() => exit()).
853 * Therefore, we need to protect this ->comm access using RCU.
854 */
855 rcu_read_lock();
856 task = pid_task(file->pid, PIDTYPE_PID);
857 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
858 task ? task->comm : "<unknown>");
859 rcu_read_unlock();
860
861 spin_lock(&file->table_lock);
862 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
863 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400864 }
Christian König7ea23562016-02-15 15:23:00 +0100865
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200866 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400867 return 0;
868}
869
Nils Wallménius06ab6832016-05-02 12:46:15 -0400870static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400871 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
872};
873#endif
874
875int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
876{
877#if defined(CONFIG_DEBUG_FS)
878 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
879#endif
880 return 0;
881}