blob: b1a611e79c947707e2b091dca34b380c2c3bdffc [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>
29#include <drm/drmP.h>
30#include <drm/amdgpu_drm.h>
31#include "amdgpu.h"
32
33void amdgpu_gem_object_free(struct drm_gem_object *gobj)
34{
35 struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
36
37 if (robj) {
38 if (robj->gem_base.import_attach)
39 drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
Christian König9298e522015-06-03 21:31:20 +020040 amdgpu_mn_unregister(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040041 amdgpu_bo_unref(&robj);
42 }
43}
44
45int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
46 int alignment, u32 initial_domain,
47 u64 flags, bool kernel,
48 struct drm_gem_object **obj)
49{
50 struct amdgpu_bo *robj;
51 unsigned long max_size;
52 int r;
53
54 *obj = NULL;
55 /* At least align on page size */
56 if (alignment < PAGE_SIZE) {
57 alignment = PAGE_SIZE;
58 }
59
60 if (!(initial_domain & (AMDGPU_GEM_DOMAIN_GDS | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA))) {
61 /* Maximum bo size is the unpinned gtt size since we use the gtt to
62 * handle vram to system pool migrations.
63 */
64 max_size = adev->mc.gtt_size - adev->gart_pin_size;
65 if (size > max_size) {
66 DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
67 size >> 20, max_size >> 20);
68 return -ENOMEM;
69 }
70 }
71retry:
Christian König72d76682015-09-03 17:34:59 +020072 r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain,
73 flags, NULL, NULL, &robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 if (r) {
75 if (r != -ERESTARTSYS) {
76 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
77 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
78 goto retry;
79 }
80 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
81 size, initial_domain, alignment, r);
82 }
83 return r;
84 }
85 *obj = &robj->gem_base;
86 robj->pid = task_pid_nr(current);
87
88 mutex_lock(&adev->gem.mutex);
89 list_add_tail(&robj->list, &adev->gem.objects);
90 mutex_unlock(&adev->gem.mutex);
91
92 return 0;
93}
94
95int amdgpu_gem_init(struct amdgpu_device *adev)
96{
97 INIT_LIST_HEAD(&adev->gem.objects);
98 return 0;
99}
100
101void amdgpu_gem_fini(struct amdgpu_device *adev)
102{
103 amdgpu_bo_force_delete(adev);
104}
105
106/*
107 * Call from drm_gem_handle_create which appear in both new and open ioctl
108 * case.
109 */
110int amdgpu_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
111{
112 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
113 struct amdgpu_device *adev = rbo->adev;
114 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
115 struct amdgpu_vm *vm = &fpriv->vm;
116 struct amdgpu_bo_va *bo_va;
117 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 r = amdgpu_bo_reserve(rbo, false);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800119 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121
122 bo_va = amdgpu_vm_bo_find(vm, rbo);
123 if (!bo_va) {
124 bo_va = amdgpu_vm_bo_add(adev, vm, rbo);
125 } else {
126 ++bo_va->ref_count;
127 }
128 amdgpu_bo_unreserve(rbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129 return 0;
130}
131
132void amdgpu_gem_object_close(struct drm_gem_object *obj,
133 struct drm_file *file_priv)
134{
135 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
136 struct amdgpu_device *adev = rbo->adev;
137 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
138 struct amdgpu_vm *vm = &fpriv->vm;
139 struct amdgpu_bo_va *bo_va;
140 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141 r = amdgpu_bo_reserve(rbo, true);
142 if (r) {
143 dev_err(adev->dev, "leaking bo va because "
144 "we fail to reserve bo (%d)\n", r);
145 return;
146 }
147 bo_va = amdgpu_vm_bo_find(vm, rbo);
148 if (bo_va) {
149 if (--bo_va->ref_count == 0) {
150 amdgpu_vm_bo_rmv(adev, bo_va);
151 }
152 }
153 amdgpu_bo_unreserve(rbo);
154}
155
156static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r)
157{
158 if (r == -EDEADLK) {
159 r = amdgpu_gpu_reset(adev);
160 if (!r)
161 r = -EAGAIN;
162 }
163 return r;
164}
165
166/*
167 * GEM ioctls.
168 */
169int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
170 struct drm_file *filp)
171{
172 struct amdgpu_device *adev = dev->dev_private;
173 union drm_amdgpu_gem_create *args = data;
174 uint64_t size = args->in.bo_size;
175 struct drm_gem_object *gobj;
176 uint32_t handle;
177 bool kernel = false;
178 int r;
179
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180 /* create a gem object to contain this object in */
181 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
182 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
183 kernel = true;
184 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
185 size = size << AMDGPU_GDS_SHIFT;
186 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
187 size = size << AMDGPU_GWS_SHIFT;
188 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
189 size = size << AMDGPU_OA_SHIFT;
190 else {
191 r = -EINVAL;
192 goto error_unlock;
193 }
194 }
195 size = roundup(size, PAGE_SIZE);
196
197 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
198 (u32)(0xffffffff & args->in.domains),
199 args->in.domain_flags,
200 kernel, &gobj);
201 if (r)
202 goto error_unlock;
203
204 r = drm_gem_handle_create(filp, gobj, &handle);
205 /* drop reference from allocate - handle holds it now */
206 drm_gem_object_unreference_unlocked(gobj);
207 if (r)
208 goto error_unlock;
209
210 memset(args, 0, sizeof(*args));
211 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 return 0;
213
214error_unlock:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215 r = amdgpu_gem_handle_lockup(adev, r);
216 return r;
217}
218
219int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
220 struct drm_file *filp)
221{
222 struct amdgpu_device *adev = dev->dev_private;
223 struct drm_amdgpu_gem_userptr *args = data;
224 struct drm_gem_object *gobj;
225 struct amdgpu_bo *bo;
226 uint32_t handle;
227 int r;
228
229 if (offset_in_page(args->addr | args->size))
230 return -EINVAL;
231
232 /* reject unknown flag values */
233 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
234 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
235 AMDGPU_GEM_USERPTR_REGISTER))
236 return -EINVAL;
237
Christian König585116c2015-11-26 11:06:20 +0100238 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) && (
239 !(args->flags & AMDGPU_GEM_USERPTR_ANONONLY) ||
240 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER))) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241
242 /* if we want to write to it we must require anonymous
243 memory and install a MMU notifier */
244 return -EACCES;
245 }
246
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400247 /* create a gem object to contain this object in */
248 r = amdgpu_gem_object_create(adev, args->size, 0,
249 AMDGPU_GEM_DOMAIN_CPU, 0,
250 0, &gobj);
251 if (r)
252 goto handle_lockup;
253
254 bo = gem_to_amdgpu_bo(gobj);
Christian König1ea863f2015-12-18 22:13:12 +0100255 bo->prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
256 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
258 if (r)
259 goto release_object;
260
261 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
262 r = amdgpu_mn_register(bo, args->addr);
263 if (r)
264 goto release_object;
265 }
266
267 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
268 down_read(&current->mm->mmap_sem);
269 r = amdgpu_bo_reserve(bo, true);
270 if (r) {
271 up_read(&current->mm->mmap_sem);
272 goto release_object;
273 }
274
275 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
276 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
277 amdgpu_bo_unreserve(bo);
278 up_read(&current->mm->mmap_sem);
279 if (r)
280 goto release_object;
281 }
282
283 r = drm_gem_handle_create(filp, gobj, &handle);
284 /* drop reference from allocate - handle holds it now */
285 drm_gem_object_unreference_unlocked(gobj);
286 if (r)
287 goto handle_lockup;
288
289 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290 return 0;
291
292release_object:
293 drm_gem_object_unreference_unlocked(gobj);
294
295handle_lockup:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 r = amdgpu_gem_handle_lockup(adev, r);
297
298 return r;
299}
300
301int amdgpu_mode_dumb_mmap(struct drm_file *filp,
302 struct drm_device *dev,
303 uint32_t handle, uint64_t *offset_p)
304{
305 struct drm_gem_object *gobj;
306 struct amdgpu_bo *robj;
307
308 gobj = drm_gem_object_lookup(dev, filp, handle);
309 if (gobj == NULL) {
310 return -ENOENT;
311 }
312 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100313 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200314 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315 drm_gem_object_unreference_unlocked(gobj);
316 return -EPERM;
317 }
318 *offset_p = amdgpu_bo_mmap_offset(robj);
319 drm_gem_object_unreference_unlocked(gobj);
320 return 0;
321}
322
323int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
324 struct drm_file *filp)
325{
326 union drm_amdgpu_gem_mmap *args = data;
327 uint32_t handle = args->in.handle;
328 memset(args, 0, sizeof(*args));
329 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
330}
331
332/**
333 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
334 *
335 * @timeout_ns: timeout in ns
336 *
337 * Calculate the timeout in jiffies from an absolute timeout in ns.
338 */
339unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
340{
341 unsigned long timeout_jiffies;
342 ktime_t timeout;
343
344 /* clamp timeout if it's to large */
345 if (((int64_t)timeout_ns) < 0)
346 return MAX_SCHEDULE_TIMEOUT;
347
Christian König0f117702015-07-08 16:58:48 +0200348 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400349 if (ktime_to_ns(timeout) < 0)
350 return 0;
351
352 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
353 /* clamp timeout to avoid unsigned-> signed overflow */
354 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
355 return MAX_SCHEDULE_TIMEOUT - 1;
356
357 return timeout_jiffies;
358}
359
360int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
361 struct drm_file *filp)
362{
363 struct amdgpu_device *adev = dev->dev_private;
364 union drm_amdgpu_gem_wait_idle *args = data;
365 struct drm_gem_object *gobj;
366 struct amdgpu_bo *robj;
367 uint32_t handle = args->in.handle;
368 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
369 int r = 0;
370 long ret;
371
372 gobj = drm_gem_object_lookup(dev, filp, handle);
373 if (gobj == NULL) {
374 return -ENOENT;
375 }
376 robj = gem_to_amdgpu_bo(gobj);
377 if (timeout == 0)
378 ret = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
379 else
380 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, timeout);
381
382 /* ret == 0 means not signaled,
383 * ret > 0 means signaled
384 * ret < 0 means interrupted before timeout
385 */
386 if (ret >= 0) {
387 memset(args, 0, sizeof(*args));
388 args->out.status = (ret == 0);
389 } else
390 r = ret;
391
392 drm_gem_object_unreference_unlocked(gobj);
393 r = amdgpu_gem_handle_lockup(adev, r);
394 return r;
395}
396
397int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
398 struct drm_file *filp)
399{
400 struct drm_amdgpu_gem_metadata *args = data;
401 struct drm_gem_object *gobj;
402 struct amdgpu_bo *robj;
403 int r = -1;
404
405 DRM_DEBUG("%d \n", args->handle);
406 gobj = drm_gem_object_lookup(dev, filp, args->handle);
407 if (gobj == NULL)
408 return -ENOENT;
409 robj = gem_to_amdgpu_bo(gobj);
410
411 r = amdgpu_bo_reserve(robj, false);
412 if (unlikely(r != 0))
413 goto out;
414
415 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
416 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
417 r = amdgpu_bo_get_metadata(robj, args->data.data,
418 sizeof(args->data.data),
419 &args->data.data_size_bytes,
420 &args->data.flags);
421 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300422 if (args->data.data_size_bytes > sizeof(args->data.data)) {
423 r = -EINVAL;
424 goto unreserve;
425 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400426 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
427 if (!r)
428 r = amdgpu_bo_set_metadata(robj, args->data.data,
429 args->data.data_size_bytes,
430 args->data.flags);
431 }
432
Dan Carpenter0913eab2015-09-23 14:00:35 +0300433unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 amdgpu_bo_unreserve(robj);
435out:
436 drm_gem_object_unreference_unlocked(gobj);
437 return r;
438}
439
440/**
441 * amdgpu_gem_va_update_vm -update the bo_va in its VM
442 *
443 * @adev: amdgpu_device pointer
444 * @bo_va: bo_va to update
445 *
446 * Update the bo_va directly after setting it's address. Errors are not
447 * vital here, so they are not reported back to userspace.
448 */
449static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
monk.liu194a3362015-07-22 13:29:28 +0800450 struct amdgpu_bo_va *bo_va, uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451{
452 struct ttm_validate_buffer tv, *entry;
Christian König56467eb2015-12-11 15:16:32 +0100453 struct amdgpu_bo_list_entry vm_pd;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454 struct ww_acquire_ctx ticket;
Christian Königbf60efd2015-09-04 10:47:56 +0200455 struct list_head list, duplicates;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400456 unsigned domain;
457 int r;
458
459 INIT_LIST_HEAD(&list);
Christian Königbf60efd2015-09-04 10:47:56 +0200460 INIT_LIST_HEAD(&duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400461
462 tv.bo = &bo_va->bo->tbo;
463 tv.shared = true;
464 list_add(&tv.head, &list);
465
Christian König56467eb2015-12-11 15:16:32 +0100466 amdgpu_vm_get_pd_bo(bo_va->vm, &list, &vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400467
Christian Königbf60efd2015-09-04 10:47:56 +0200468 /* Provide duplicates to avoid -EALREADY */
469 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470 if (r)
Christian König56467eb2015-12-11 15:16:32 +0100471 goto error_print;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472
Christian Königee1782c2015-12-11 21:01:23 +0100473 amdgpu_vm_get_pt_bos(bo_va->vm, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400474 list_for_each_entry(entry, &list, head) {
475 domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type);
476 /* if anything is swapped out don't swap it in here,
477 just abort and wait for the next CS */
478 if (domain == AMDGPU_GEM_DOMAIN_CPU)
479 goto error_unreserve;
480 }
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800481 list_for_each_entry(entry, &duplicates, head) {
482 domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type);
483 /* if anything is swapped out don't swap it in here,
484 just abort and wait for the next CS */
485 if (domain == AMDGPU_GEM_DOMAIN_CPU)
486 goto error_unreserve;
487 }
488
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800489 r = amdgpu_vm_update_page_directory(adev, bo_va->vm);
490 if (r)
491 goto error_unreserve;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400492
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493 r = amdgpu_vm_clear_freed(adev, bo_va->vm);
494 if (r)
Chunming Zhouf48b2652015-10-16 14:06:19 +0800495 goto error_unreserve;
monk.liu194a3362015-07-22 13:29:28 +0800496
497 if (operation == AMDGPU_VA_OP_MAP)
498 r = amdgpu_vm_bo_update(adev, bo_va, &bo_va->bo->tbo.mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500error_unreserve:
501 ttm_eu_backoff_reservation(&ticket, &list);
502
Christian König56467eb2015-12-11 15:16:32 +0100503error_print:
Christian König68fdd3d2015-06-16 14:50:02 +0200504 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
506}
507
508
509
510int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
511 struct drm_file *filp)
512{
Christian König34b5f6a2015-06-08 15:03:00 +0200513 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400514 struct drm_gem_object *gobj;
515 struct amdgpu_device *adev = dev->dev_private;
516 struct amdgpu_fpriv *fpriv = filp->driver_priv;
517 struct amdgpu_bo *rbo;
518 struct amdgpu_bo_va *bo_va;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800519 struct ttm_validate_buffer tv, tv_pd;
520 struct ww_acquire_ctx ticket;
521 struct list_head list, duplicates;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 uint32_t invalid_flags, va_flags = 0;
523 int r = 0;
524
Christian König34b5f6a2015-06-08 15:03:00 +0200525 if (!adev->vm_manager.enabled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526 return -ENOTTY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527
Christian König34b5f6a2015-06-08 15:03:00 +0200528 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 dev_err(&dev->pdev->dev,
530 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200531 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400532 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533 return -EINVAL;
534 }
535
Christian Königfc220f62015-06-29 17:12:20 +0200536 invalid_flags = ~(AMDGPU_VM_DELAY_UPDATE | AMDGPU_VM_PAGE_READABLE |
537 AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_PAGE_EXECUTABLE);
Christian König34b5f6a2015-06-08 15:03:00 +0200538 if ((args->flags & invalid_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200540 args->flags, invalid_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 return -EINVAL;
542 }
543
Christian König34b5f6a2015-06-08 15:03:00 +0200544 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400545 case AMDGPU_VA_OP_MAP:
546 case AMDGPU_VA_OP_UNMAP:
547 break;
548 default:
549 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200550 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551 return -EINVAL;
552 }
553
Christian König34b5f6a2015-06-08 15:03:00 +0200554 gobj = drm_gem_object_lookup(dev, filp, args->handle);
555 if (gobj == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557 rbo = gem_to_amdgpu_bo(gobj);
Chunming Zhou49b02b12015-11-13 14:18:38 +0800558 INIT_LIST_HEAD(&list);
559 INIT_LIST_HEAD(&duplicates);
560 tv.bo = &rbo->tbo;
561 tv.shared = true;
562 list_add(&tv.head, &list);
563
564 if (args->operation == AMDGPU_VA_OP_MAP) {
565 tv_pd.bo = &fpriv->vm.page_directory->tbo;
566 tv_pd.shared = true;
567 list_add(&tv_pd.head, &list);
568 }
569 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571 drm_gem_object_unreference_unlocked(gobj);
572 return r;
573 }
Christian König34b5f6a2015-06-08 15:03:00 +0200574
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400575 bo_va = amdgpu_vm_bo_find(&fpriv->vm, rbo);
576 if (!bo_va) {
Chunming Zhou49b02b12015-11-13 14:18:38 +0800577 ttm_eu_backoff_reservation(&ticket, &list);
578 drm_gem_object_unreference_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 return -ENOENT;
580 }
581
Christian König34b5f6a2015-06-08 15:03:00 +0200582 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 case AMDGPU_VA_OP_MAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200584 if (args->flags & AMDGPU_VM_PAGE_READABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585 va_flags |= AMDGPU_PTE_READABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200586 if (args->flags & AMDGPU_VM_PAGE_WRITEABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 va_flags |= AMDGPU_PTE_WRITEABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200588 if (args->flags & AMDGPU_VM_PAGE_EXECUTABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589 va_flags |= AMDGPU_PTE_EXECUTABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200590 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
591 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200592 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 break;
594 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200595 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400596 break;
597 default:
598 break;
599 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800600 ttm_eu_backoff_reservation(&ticket, &list);
Christian Königfc220f62015-06-29 17:12:20 +0200601 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE))
monk.liu194a3362015-07-22 13:29:28 +0800602 amdgpu_gem_va_update_vm(adev, bo_va, args->operation);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800603
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 drm_gem_object_unreference_unlocked(gobj);
605 return r;
606}
607
608int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
609 struct drm_file *filp)
610{
611 struct drm_amdgpu_gem_op *args = data;
612 struct drm_gem_object *gobj;
613 struct amdgpu_bo *robj;
614 int r;
615
616 gobj = drm_gem_object_lookup(dev, filp, args->handle);
617 if (gobj == NULL) {
618 return -ENOENT;
619 }
620 robj = gem_to_amdgpu_bo(gobj);
621
622 r = amdgpu_bo_reserve(robj, false);
623 if (unlikely(r))
624 goto out;
625
626 switch (args->op) {
627 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
628 struct drm_amdgpu_gem_create_in info;
629 void __user *out = (void __user *)(long)args->value;
630
631 info.bo_size = robj->gem_base.size;
632 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Christian König1ea863f2015-12-18 22:13:12 +0100633 info.domains = robj->prefered_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200635 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 if (copy_to_user(out, &info, sizeof(info)))
637 r = -EFAULT;
638 break;
639 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200640 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christian Königcc325d12016-02-08 11:08:35 +0100641 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200643 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 break;
645 }
Christian König1ea863f2015-12-18 22:13:12 +0100646 robj->prefered_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
647 AMDGPU_GEM_DOMAIN_GTT |
648 AMDGPU_GEM_DOMAIN_CPU);
649 robj->allowed_domains = robj->prefered_domains;
650 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
651 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
652
Christian König4c28fb02015-08-28 17:27:54 +0200653 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654 break;
655 default:
Christian König4c28fb02015-08-28 17:27:54 +0200656 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400657 r = -EINVAL;
658 }
659
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660out:
661 drm_gem_object_unreference_unlocked(gobj);
662 return r;
663}
664
665int amdgpu_mode_dumb_create(struct drm_file *file_priv,
666 struct drm_device *dev,
667 struct drm_mode_create_dumb *args)
668{
669 struct amdgpu_device *adev = dev->dev_private;
670 struct drm_gem_object *gobj;
671 uint32_t handle;
672 int r;
673
674 args->pitch = amdgpu_align_pitch(adev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300675 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 args->size = ALIGN(args->size, PAGE_SIZE);
677
678 r = amdgpu_gem_object_create(adev, args->size, 0,
679 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400680 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
681 ttm_bo_type_device,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 &gobj);
683 if (r)
684 return -ENOMEM;
685
686 r = drm_gem_handle_create(file_priv, gobj, &handle);
687 /* drop reference from allocate - handle holds it now */
688 drm_gem_object_unreference_unlocked(gobj);
689 if (r) {
690 return r;
691 }
692 args->handle = handle;
693 return 0;
694}
695
696#if defined(CONFIG_DEBUG_FS)
697static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
698{
699 struct drm_info_node *node = (struct drm_info_node *)m->private;
700 struct drm_device *dev = node->minor->dev;
701 struct amdgpu_device *adev = dev->dev_private;
702 struct amdgpu_bo *rbo;
703 unsigned i = 0;
704
705 mutex_lock(&adev->gem.mutex);
706 list_for_each_entry(rbo, &adev->gem.objects, list) {
707 unsigned domain;
708 const char *placement;
709
710 domain = amdgpu_mem_type_to_domain(rbo->tbo.mem.mem_type);
711 switch (domain) {
712 case AMDGPU_GEM_DOMAIN_VRAM:
713 placement = "VRAM";
714 break;
715 case AMDGPU_GEM_DOMAIN_GTT:
716 placement = " GTT";
717 break;
718 case AMDGPU_GEM_DOMAIN_CPU:
719 default:
720 placement = " CPU";
721 break;
722 }
723 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
724 i, amdgpu_bo_size(rbo) >> 10, amdgpu_bo_size(rbo) >> 20,
725 placement, (unsigned long)rbo->pid);
726 i++;
727 }
728 mutex_unlock(&adev->gem.mutex);
729 return 0;
730}
731
732static struct drm_info_list amdgpu_debugfs_gem_list[] = {
733 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
734};
735#endif
736
737int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
738{
739#if defined(CONFIG_DEBUG_FS)
740 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
741#endif
742 return 0;
743}