blob: 3c22656aa1bf3dbda87c62c1502f104fdb9c6556 [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
142void amdgpu_gem_object_close(struct drm_gem_object *obj,
143 struct drm_file *file_priv)
144{
Christian Königb5a5ec52016-03-08 17:47:46 +0100145 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200146 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
148 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb5a5ec52016-03-08 17:47:46 +0100149
150 struct amdgpu_bo_list_entry vm_pd;
151 struct list_head list, duplicates;
152 struct ttm_validate_buffer tv;
153 struct ww_acquire_ctx ticket;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154 struct amdgpu_bo_va *bo_va;
155 int r;
Christian Königb5a5ec52016-03-08 17:47:46 +0100156
157 INIT_LIST_HEAD(&list);
158 INIT_LIST_HEAD(&duplicates);
159
160 tv.bo = &bo->tbo;
161 tv.shared = true;
162 list_add(&tv.head, &list);
163
164 amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
165
Christian König35264f62016-03-17 17:14:10 +0100166 r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167 if (r) {
168 dev_err(adev->dev, "leaking bo va because "
169 "we fail to reserve bo (%d)\n", r);
170 return;
171 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100172 bo_va = amdgpu_vm_bo_find(vm, bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173 if (bo_va) {
174 if (--bo_va->ref_count == 0) {
175 amdgpu_vm_bo_rmv(adev, bo_va);
176 }
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
181static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r)
182{
183 if (r == -EDEADLK) {
184 r = amdgpu_gpu_reset(adev);
185 if (!r)
186 r = -EAGAIN;
187 }
188 return r;
189}
190
191/*
192 * GEM ioctls.
193 */
194int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
195 struct drm_file *filp)
196{
197 struct amdgpu_device *adev = dev->dev_private;
198 union drm_amdgpu_gem_create *args = data;
199 uint64_t size = args->in.bo_size;
200 struct drm_gem_object *gobj;
201 uint32_t handle;
202 bool kernel = false;
203 int r;
204
Alex Deucher834e0f82017-03-08 17:40:17 -0500205 /* reject invalid gem flags */
206 if (args->in.domain_flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
207 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
208 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
209 AMDGPU_GEM_CREATE_VRAM_CLEARED|
210 AMDGPU_GEM_CREATE_SHADOW |
211 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) {
212 r = -EINVAL;
213 goto error_unlock;
214 }
215 /* reject invalid gem domains */
216 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
217 AMDGPU_GEM_DOMAIN_GTT |
218 AMDGPU_GEM_DOMAIN_VRAM |
219 AMDGPU_GEM_DOMAIN_GDS |
220 AMDGPU_GEM_DOMAIN_GWS |
221 AMDGPU_GEM_DOMAIN_OA)) {
222 r = -EINVAL;
223 goto error_unlock;
224 }
225
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226 /* create a gem object to contain this object in */
227 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
228 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
229 kernel = true;
230 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
231 size = size << AMDGPU_GDS_SHIFT;
232 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
233 size = size << AMDGPU_GWS_SHIFT;
234 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
235 size = size << AMDGPU_OA_SHIFT;
236 else {
237 r = -EINVAL;
238 goto error_unlock;
239 }
240 }
241 size = roundup(size, PAGE_SIZE);
242
243 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
244 (u32)(0xffffffff & args->in.domains),
245 args->in.domain_flags,
246 kernel, &gobj);
247 if (r)
248 goto error_unlock;
249
250 r = drm_gem_handle_create(filp, gobj, &handle);
251 /* drop reference from allocate - handle holds it now */
252 drm_gem_object_unreference_unlocked(gobj);
253 if (r)
254 goto error_unlock;
255
256 memset(args, 0, sizeof(*args));
257 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400258 return 0;
259
260error_unlock:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400261 r = amdgpu_gem_handle_lockup(adev, r);
262 return r;
263}
264
265int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
266 struct drm_file *filp)
267{
268 struct amdgpu_device *adev = dev->dev_private;
269 struct drm_amdgpu_gem_userptr *args = data;
270 struct drm_gem_object *gobj;
271 struct amdgpu_bo *bo;
272 uint32_t handle;
273 int r;
274
275 if (offset_in_page(args->addr | args->size))
276 return -EINVAL;
277
278 /* reject unknown flag values */
279 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
280 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
281 AMDGPU_GEM_USERPTR_REGISTER))
282 return -EINVAL;
283
Christian König358c2582016-03-11 15:29:27 +0100284 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
285 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286
Christian König358c2582016-03-11 15:29:27 +0100287 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400288 return -EACCES;
289 }
290
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400291 /* create a gem object to contain this object in */
292 r = amdgpu_gem_object_create(adev, args->size, 0,
293 AMDGPU_GEM_DOMAIN_CPU, 0,
294 0, &gobj);
295 if (r)
296 goto handle_lockup;
297
298 bo = gem_to_amdgpu_bo(gobj);
Christian König1ea863f2015-12-18 22:13:12 +0100299 bo->prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
300 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400301 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
302 if (r)
303 goto release_object;
304
305 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
306 r = amdgpu_mn_register(bo, args->addr);
307 if (r)
308 goto release_object;
309 }
310
311 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
312 down_read(&current->mm->mmap_sem);
Christian König2f568db2016-02-23 12:36:59 +0100313
314 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
315 bo->tbo.ttm->pages);
316 if (r)
317 goto unlock_mmap_sem;
318
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400319 r = amdgpu_bo_reserve(bo, true);
Christian König2f568db2016-02-23 12:36:59 +0100320 if (r)
321 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400322
323 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
324 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
325 amdgpu_bo_unreserve(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400326 if (r)
Christian König2f568db2016-02-23 12:36:59 +0100327 goto free_pages;
328
329 up_read(&current->mm->mmap_sem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400330 }
331
332 r = drm_gem_handle_create(filp, gobj, &handle);
333 /* drop reference from allocate - handle holds it now */
334 drm_gem_object_unreference_unlocked(gobj);
335 if (r)
336 goto handle_lockup;
337
338 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339 return 0;
340
Christian König2f568db2016-02-23 12:36:59 +0100341free_pages:
342 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages, false);
343
344unlock_mmap_sem:
345 up_read(&current->mm->mmap_sem);
346
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347release_object:
348 drm_gem_object_unreference_unlocked(gobj);
349
350handle_lockup:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400351 r = amdgpu_gem_handle_lockup(adev, r);
352
353 return r;
354}
355
356int amdgpu_mode_dumb_mmap(struct drm_file *filp,
357 struct drm_device *dev,
358 uint32_t handle, uint64_t *offset_p)
359{
360 struct drm_gem_object *gobj;
361 struct amdgpu_bo *robj;
362
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100363 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400364 if (gobj == NULL) {
365 return -ENOENT;
366 }
367 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100368 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200369 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400370 drm_gem_object_unreference_unlocked(gobj);
371 return -EPERM;
372 }
373 *offset_p = amdgpu_bo_mmap_offset(robj);
374 drm_gem_object_unreference_unlocked(gobj);
375 return 0;
376}
377
378int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
379 struct drm_file *filp)
380{
381 union drm_amdgpu_gem_mmap *args = data;
382 uint32_t handle = args->in.handle;
383 memset(args, 0, sizeof(*args));
384 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
385}
386
387/**
388 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
389 *
390 * @timeout_ns: timeout in ns
391 *
392 * Calculate the timeout in jiffies from an absolute timeout in ns.
393 */
394unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
395{
396 unsigned long timeout_jiffies;
397 ktime_t timeout;
398
399 /* clamp timeout if it's to large */
400 if (((int64_t)timeout_ns) < 0)
401 return MAX_SCHEDULE_TIMEOUT;
402
Christian König0f117702015-07-08 16:58:48 +0200403 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400404 if (ktime_to_ns(timeout) < 0)
405 return 0;
406
407 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
408 /* clamp timeout to avoid unsigned-> signed overflow */
409 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
410 return MAX_SCHEDULE_TIMEOUT - 1;
411
412 return timeout_jiffies;
413}
414
415int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
416 struct drm_file *filp)
417{
418 struct amdgpu_device *adev = dev->dev_private;
419 union drm_amdgpu_gem_wait_idle *args = data;
420 struct drm_gem_object *gobj;
421 struct amdgpu_bo *robj;
422 uint32_t handle = args->in.handle;
423 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
424 int r = 0;
425 long ret;
426
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100427 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428 if (gobj == NULL) {
429 return -ENOENT;
430 }
431 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100432 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
433 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434
435 /* ret == 0 means not signaled,
436 * ret > 0 means signaled
437 * ret < 0 means interrupted before timeout
438 */
439 if (ret >= 0) {
440 memset(args, 0, sizeof(*args));
441 args->out.status = (ret == 0);
442 } else
443 r = ret;
444
445 drm_gem_object_unreference_unlocked(gobj);
446 r = amdgpu_gem_handle_lockup(adev, r);
447 return r;
448}
449
450int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
451 struct drm_file *filp)
452{
453 struct drm_amdgpu_gem_metadata *args = data;
454 struct drm_gem_object *gobj;
455 struct amdgpu_bo *robj;
456 int r = -1;
457
458 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100459 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 if (gobj == NULL)
461 return -ENOENT;
462 robj = gem_to_amdgpu_bo(gobj);
463
464 r = amdgpu_bo_reserve(robj, false);
465 if (unlikely(r != 0))
466 goto out;
467
468 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
469 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
470 r = amdgpu_bo_get_metadata(robj, args->data.data,
471 sizeof(args->data.data),
472 &args->data.data_size_bytes,
473 &args->data.flags);
474 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300475 if (args->data.data_size_bytes > sizeof(args->data.data)) {
476 r = -EINVAL;
477 goto unreserve;
478 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
480 if (!r)
481 r = amdgpu_bo_set_metadata(robj, args->data.data,
482 args->data.data_size_bytes,
483 args->data.flags);
484 }
485
Dan Carpenter0913eab2015-09-23 14:00:35 +0300486unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400487 amdgpu_bo_unreserve(robj);
488out:
489 drm_gem_object_unreference_unlocked(gobj);
490 return r;
491}
492
Christian Königf7da30d2016-09-28 12:03:04 +0200493static int amdgpu_gem_va_check(void *param, struct amdgpu_bo *bo)
494{
Christian Königf7da30d2016-09-28 12:03:04 +0200495 /* if anything is swapped out don't swap it in here,
496 just abort and wait for the next CS */
Nicolai Hähnle3e19e692016-12-12 11:53:11 +0100497 if (!amdgpu_bo_gpu_accessible(bo))
498 return -ERESTARTSYS;
Christian Königf7da30d2016-09-28 12:03:04 +0200499
Nicolai Hähnle3e19e692016-12-12 11:53:11 +0100500 if (bo->shadow && !amdgpu_bo_gpu_accessible(bo->shadow))
501 return -ERESTARTSYS;
502
503 return 0;
Christian Königf7da30d2016-09-28 12:03:04 +0200504}
505
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506/**
507 * amdgpu_gem_va_update_vm -update the bo_va in its VM
508 *
509 * @adev: amdgpu_device pointer
510 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100511 * @list: validation list
512 * @operation: map or unmap
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100514 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515 * vital here, so they are not reported back to userspace.
516 */
517static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königf7da30d2016-09-28 12:03:04 +0200518 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100519 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200520 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521{
Christian König2ffdaaf2017-01-27 15:58:43 +0100522 struct ttm_validate_buffer *entry;
523 int r = -ERESTARTSYS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524
Christian König2ffdaaf2017-01-27 15:58:43 +0100525 list_for_each_entry(entry, list, head) {
Nicolai Hähnled1144b82016-12-12 12:09:12 +0100526 struct amdgpu_bo *bo =
527 container_of(entry->bo, struct amdgpu_bo, tbo);
Samuel Pitoiseta73effa2017-02-09 23:28:24 +0100528 if (amdgpu_gem_va_check(NULL, bo))
Christian König2ffdaaf2017-01-27 15:58:43 +0100529 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 }
Nicolai Hähnled1144b82016-12-12 12:09:12 +0100531
Christian Königf7da30d2016-09-28 12:03:04 +0200532 r = amdgpu_vm_validate_pt_bos(adev, bo_va->vm, amdgpu_gem_va_check,
533 NULL);
534 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100535 goto error;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800536
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800537 r = amdgpu_vm_update_page_directory(adev, bo_va->vm);
538 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100539 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 r = amdgpu_vm_clear_freed(adev, bo_va->vm);
542 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100543 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800544
545 if (operation == AMDGPU_VA_OP_MAP)
Flora Cui05dcb5c2016-09-22 11:34:47 +0800546 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547
Christian König2ffdaaf2017-01-27 15:58:43 +0100548error:
Christian König68fdd3d2015-06-16 14:50:02 +0200549 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
551}
552
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
554 struct drm_file *filp)
555{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800556 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
557 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
558 AMDGPU_VM_PAGE_EXECUTABLE;
559 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
560 AMDGPU_VM_PAGE_PRT;
561
Christian König34b5f6a2015-06-08 15:03:00 +0200562 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 struct drm_gem_object *gobj;
564 struct amdgpu_device *adev = dev->dev_private;
565 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200566 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400567 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200568 struct amdgpu_bo_list_entry vm_pd;
569 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800570 struct ww_acquire_ctx ticket;
Christian Königd7d29552017-01-30 10:24:13 +0100571 struct list_head list;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800572 uint64_t va_flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 int r = 0;
574
Christian König34b5f6a2015-06-08 15:03:00 +0200575 if (!adev->vm_manager.enabled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576 return -ENOTTY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577
Christian König34b5f6a2015-06-08 15:03:00 +0200578 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 dev_err(&dev->pdev->dev,
580 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200581 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 return -EINVAL;
584 }
585
Junwei Zhangb85891b2017-01-16 13:59:01 +0800586 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
587 dev_err(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
588 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589 return -EINVAL;
590 }
591
Christian König34b5f6a2015-06-08 15:03:00 +0200592 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 case AMDGPU_VA_OP_MAP:
594 case AMDGPU_VA_OP_UNMAP:
595 break;
596 default:
597 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200598 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 return -EINVAL;
600 }
601
Chunming Zhou49b02b12015-11-13 14:18:38 +0800602 INIT_LIST_HEAD(&list);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800603 if (!(args->flags & AMDGPU_VM_PAGE_PRT)) {
604 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 }
628 } else {
629 bo_va = fpriv->prt_va;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 }
631
Christian König34b5f6a2015-06-08 15:03:00 +0200632 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633 case AMDGPU_VA_OP_MAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200634 if (args->flags & AMDGPU_VM_PAGE_READABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635 va_flags |= AMDGPU_PTE_READABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200636 if (args->flags & AMDGPU_VM_PAGE_WRITEABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 va_flags |= AMDGPU_PTE_WRITEABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200638 if (args->flags & AMDGPU_VM_PAGE_EXECUTABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 va_flags |= AMDGPU_PTE_EXECUTABLE;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800640 if (args->flags & AMDGPU_VM_PAGE_PRT)
641 va_flags |= AMDGPU_PTE_PRT;
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;
649 default:
650 break;
651 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800652 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian König2ffdaaf2017-01-27 15:58:43 +0100653 amdgpu_gem_va_update_vm(adev, bo_va, &list, args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800654
655error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100656 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800657
Junwei Zhangb85891b2017-01-16 13:59:01 +0800658error_unref:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 drm_gem_object_unreference_unlocked(gobj);
660 return r;
661}
662
663int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
664 struct drm_file *filp)
665{
666 struct drm_amdgpu_gem_op *args = data;
667 struct drm_gem_object *gobj;
668 struct amdgpu_bo *robj;
669 int r;
670
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100671 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 if (gobj == NULL) {
673 return -ENOENT;
674 }
675 robj = gem_to_amdgpu_bo(gobj);
676
677 r = amdgpu_bo_reserve(robj, false);
678 if (unlikely(r))
679 goto out;
680
681 switch (args->op) {
682 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
683 struct drm_amdgpu_gem_create_in info;
684 void __user *out = (void __user *)(long)args->value;
685
686 info.bo_size = robj->gem_base.size;
687 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Christian König1ea863f2015-12-18 22:13:12 +0100688 info.domains = robj->prefered_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200690 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400691 if (copy_to_user(out, &info, sizeof(info)))
692 r = -EFAULT;
693 break;
694 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200695 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christian Königcc325d12016-02-08 11:08:35 +0100696 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200698 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 break;
700 }
Christian König1ea863f2015-12-18 22:13:12 +0100701 robj->prefered_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
702 AMDGPU_GEM_DOMAIN_GTT |
703 AMDGPU_GEM_DOMAIN_CPU);
704 robj->allowed_domains = robj->prefered_domains;
705 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
706 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
707
Christian König4c28fb02015-08-28 17:27:54 +0200708 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 break;
710 default:
Christian König4c28fb02015-08-28 17:27:54 +0200711 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712 r = -EINVAL;
713 }
714
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715out:
716 drm_gem_object_unreference_unlocked(gobj);
717 return r;
718}
719
720int amdgpu_mode_dumb_create(struct drm_file *file_priv,
721 struct drm_device *dev,
722 struct drm_mode_create_dumb *args)
723{
724 struct amdgpu_device *adev = dev->dev_private;
725 struct drm_gem_object *gobj;
726 uint32_t handle;
727 int r;
728
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300729 args->pitch = amdgpu_align_pitch(adev, args->width,
730 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300731 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 args->size = ALIGN(args->size, PAGE_SIZE);
733
734 r = amdgpu_gem_object_create(adev, args->size, 0,
735 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400736 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
737 ttm_bo_type_device,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738 &gobj);
739 if (r)
740 return -ENOMEM;
741
742 r = drm_gem_handle_create(file_priv, gobj, &handle);
743 /* drop reference from allocate - handle holds it now */
744 drm_gem_object_unreference_unlocked(gobj);
745 if (r) {
746 return r;
747 }
748 args->handle = handle;
749 return 0;
750}
751
752#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100753static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
754{
755 struct drm_gem_object *gobj = ptr;
756 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
757 struct seq_file *m = data;
758
759 unsigned domain;
760 const char *placement;
761 unsigned pin_count;
762
763 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
764 switch (domain) {
765 case AMDGPU_GEM_DOMAIN_VRAM:
766 placement = "VRAM";
767 break;
768 case AMDGPU_GEM_DOMAIN_GTT:
769 placement = " GTT";
770 break;
771 case AMDGPU_GEM_DOMAIN_CPU:
772 default:
773 placement = " CPU";
774 break;
775 }
776 seq_printf(m, "\t0x%08x: %12ld byte %s @ 0x%010Lx",
777 id, amdgpu_bo_size(bo), placement,
778 amdgpu_bo_gpu_offset(bo));
779
780 pin_count = ACCESS_ONCE(bo->pin_count);
781 if (pin_count)
782 seq_printf(m, " pin count %d", pin_count);
783 seq_printf(m, "\n");
784
785 return 0;
786}
787
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400788static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
789{
790 struct drm_info_node *node = (struct drm_info_node *)m->private;
791 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100792 struct drm_file *file;
793 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200795 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100796 if (r)
797 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798
Christian König7ea23562016-02-15 15:23:00 +0100799 list_for_each_entry(file, &dev->filelist, lhead) {
800 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100801
Christian König7ea23562016-02-15 15:23:00 +0100802 /*
803 * Although we have a valid reference on file->pid, that does
804 * not guarantee that the task_struct who called get_pid() is
805 * still alive (e.g. get_pid(current) => fork() => exit()).
806 * Therefore, we need to protect this ->comm access using RCU.
807 */
808 rcu_read_lock();
809 task = pid_task(file->pid, PIDTYPE_PID);
810 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
811 task ? task->comm : "<unknown>");
812 rcu_read_unlock();
813
814 spin_lock(&file->table_lock);
815 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
816 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400817 }
Christian König7ea23562016-02-15 15:23:00 +0100818
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200819 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820 return 0;
821}
822
Nils Wallménius06ab6832016-05-02 12:46:15 -0400823static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400824 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
825};
826#endif
827
828int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
829{
830#if defined(CONFIG_DEBUG_FS)
831 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
832#endif
833 return 0;
834}