blob: 2f39fea10bd1f881a8b8c6ee167405dc6c1fdb8e [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;
118
119 r = amdgpu_bo_reserve(rbo, false);
120 if (r) {
121 return r;
122 }
123
124 bo_va = amdgpu_vm_bo_find(vm, rbo);
125 if (!bo_va) {
126 bo_va = amdgpu_vm_bo_add(adev, vm, rbo);
127 } else {
128 ++bo_va->ref_count;
129 }
130 amdgpu_bo_unreserve(rbo);
131
132 return 0;
133}
134
135void amdgpu_gem_object_close(struct drm_gem_object *obj,
136 struct drm_file *file_priv)
137{
138 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
139 struct amdgpu_device *adev = rbo->adev;
140 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
141 struct amdgpu_vm *vm = &fpriv->vm;
142 struct amdgpu_bo_va *bo_va;
143 int r;
144
145 r = amdgpu_bo_reserve(rbo, true);
146 if (r) {
147 dev_err(adev->dev, "leaking bo va because "
148 "we fail to reserve bo (%d)\n", r);
149 return;
150 }
151 bo_va = amdgpu_vm_bo_find(vm, rbo);
152 if (bo_va) {
153 if (--bo_va->ref_count == 0) {
154 amdgpu_vm_bo_rmv(adev, bo_va);
155 }
156 }
157 amdgpu_bo_unreserve(rbo);
158}
159
160static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r)
161{
162 if (r == -EDEADLK) {
163 r = amdgpu_gpu_reset(adev);
164 if (!r)
165 r = -EAGAIN;
166 }
167 return r;
168}
169
170/*
171 * GEM ioctls.
172 */
173int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
174 struct drm_file *filp)
175{
176 struct amdgpu_device *adev = dev->dev_private;
177 union drm_amdgpu_gem_create *args = data;
178 uint64_t size = args->in.bo_size;
179 struct drm_gem_object *gobj;
180 uint32_t handle;
181 bool kernel = false;
182 int r;
183
184 down_read(&adev->exclusive_lock);
185 /* create a gem object to contain this object in */
186 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
187 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
188 kernel = true;
189 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
190 size = size << AMDGPU_GDS_SHIFT;
191 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
192 size = size << AMDGPU_GWS_SHIFT;
193 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
194 size = size << AMDGPU_OA_SHIFT;
195 else {
196 r = -EINVAL;
197 goto error_unlock;
198 }
199 }
200 size = roundup(size, PAGE_SIZE);
201
202 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
203 (u32)(0xffffffff & args->in.domains),
204 args->in.domain_flags,
205 kernel, &gobj);
206 if (r)
207 goto error_unlock;
208
209 r = drm_gem_handle_create(filp, gobj, &handle);
210 /* drop reference from allocate - handle holds it now */
211 drm_gem_object_unreference_unlocked(gobj);
212 if (r)
213 goto error_unlock;
214
215 memset(args, 0, sizeof(*args));
216 args->out.handle = handle;
217 up_read(&adev->exclusive_lock);
218 return 0;
219
220error_unlock:
221 up_read(&adev->exclusive_lock);
222 r = amdgpu_gem_handle_lockup(adev, r);
223 return r;
224}
225
226int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
227 struct drm_file *filp)
228{
229 struct amdgpu_device *adev = dev->dev_private;
230 struct drm_amdgpu_gem_userptr *args = data;
231 struct drm_gem_object *gobj;
232 struct amdgpu_bo *bo;
233 uint32_t handle;
234 int r;
235
236 if (offset_in_page(args->addr | args->size))
237 return -EINVAL;
238
239 /* reject unknown flag values */
240 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
241 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
242 AMDGPU_GEM_USERPTR_REGISTER))
243 return -EINVAL;
244
245 if (!(args->flags & AMDGPU_GEM_USERPTR_ANONONLY) ||
246 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
247
248 /* if we want to write to it we must require anonymous
249 memory and install a MMU notifier */
250 return -EACCES;
251 }
252
253 down_read(&adev->exclusive_lock);
254
255 /* create a gem object to contain this object in */
256 r = amdgpu_gem_object_create(adev, args->size, 0,
257 AMDGPU_GEM_DOMAIN_CPU, 0,
258 0, &gobj);
259 if (r)
260 goto handle_lockup;
261
262 bo = gem_to_amdgpu_bo(gobj);
263 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
264 if (r)
265 goto release_object;
266
267 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
268 r = amdgpu_mn_register(bo, args->addr);
269 if (r)
270 goto release_object;
271 }
272
273 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
274 down_read(&current->mm->mmap_sem);
275 r = amdgpu_bo_reserve(bo, true);
276 if (r) {
277 up_read(&current->mm->mmap_sem);
278 goto release_object;
279 }
280
281 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
282 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
283 amdgpu_bo_unreserve(bo);
284 up_read(&current->mm->mmap_sem);
285 if (r)
286 goto release_object;
287 }
288
289 r = drm_gem_handle_create(filp, gobj, &handle);
290 /* drop reference from allocate - handle holds it now */
291 drm_gem_object_unreference_unlocked(gobj);
292 if (r)
293 goto handle_lockup;
294
295 args->handle = handle;
296 up_read(&adev->exclusive_lock);
297 return 0;
298
299release_object:
300 drm_gem_object_unreference_unlocked(gobj);
301
302handle_lockup:
303 up_read(&adev->exclusive_lock);
304 r = amdgpu_gem_handle_lockup(adev, r);
305
306 return r;
307}
308
309int amdgpu_mode_dumb_mmap(struct drm_file *filp,
310 struct drm_device *dev,
311 uint32_t handle, uint64_t *offset_p)
312{
313 struct drm_gem_object *gobj;
314 struct amdgpu_bo *robj;
315
316 gobj = drm_gem_object_lookup(dev, filp, handle);
317 if (gobj == NULL) {
318 return -ENOENT;
319 }
320 robj = gem_to_amdgpu_bo(gobj);
Christian König271c8122015-05-13 14:30:53 +0200321 if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm) ||
322 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400323 drm_gem_object_unreference_unlocked(gobj);
324 return -EPERM;
325 }
326 *offset_p = amdgpu_bo_mmap_offset(robj);
327 drm_gem_object_unreference_unlocked(gobj);
328 return 0;
329}
330
331int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
332 struct drm_file *filp)
333{
334 union drm_amdgpu_gem_mmap *args = data;
335 uint32_t handle = args->in.handle;
336 memset(args, 0, sizeof(*args));
337 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
338}
339
340/**
341 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
342 *
343 * @timeout_ns: timeout in ns
344 *
345 * Calculate the timeout in jiffies from an absolute timeout in ns.
346 */
347unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
348{
349 unsigned long timeout_jiffies;
350 ktime_t timeout;
351
352 /* clamp timeout if it's to large */
353 if (((int64_t)timeout_ns) < 0)
354 return MAX_SCHEDULE_TIMEOUT;
355
Christian König0f117702015-07-08 16:58:48 +0200356 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357 if (ktime_to_ns(timeout) < 0)
358 return 0;
359
360 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
361 /* clamp timeout to avoid unsigned-> signed overflow */
362 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
363 return MAX_SCHEDULE_TIMEOUT - 1;
364
365 return timeout_jiffies;
366}
367
368int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
369 struct drm_file *filp)
370{
371 struct amdgpu_device *adev = dev->dev_private;
372 union drm_amdgpu_gem_wait_idle *args = data;
373 struct drm_gem_object *gobj;
374 struct amdgpu_bo *robj;
375 uint32_t handle = args->in.handle;
376 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
377 int r = 0;
378 long ret;
379
380 gobj = drm_gem_object_lookup(dev, filp, handle);
381 if (gobj == NULL) {
382 return -ENOENT;
383 }
384 robj = gem_to_amdgpu_bo(gobj);
385 if (timeout == 0)
386 ret = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
387 else
388 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, timeout);
389
390 /* ret == 0 means not signaled,
391 * ret > 0 means signaled
392 * ret < 0 means interrupted before timeout
393 */
394 if (ret >= 0) {
395 memset(args, 0, sizeof(*args));
396 args->out.status = (ret == 0);
397 } else
398 r = ret;
399
400 drm_gem_object_unreference_unlocked(gobj);
401 r = amdgpu_gem_handle_lockup(adev, r);
402 return r;
403}
404
405int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
406 struct drm_file *filp)
407{
408 struct drm_amdgpu_gem_metadata *args = data;
409 struct drm_gem_object *gobj;
410 struct amdgpu_bo *robj;
411 int r = -1;
412
413 DRM_DEBUG("%d \n", args->handle);
414 gobj = drm_gem_object_lookup(dev, filp, args->handle);
415 if (gobj == NULL)
416 return -ENOENT;
417 robj = gem_to_amdgpu_bo(gobj);
418
419 r = amdgpu_bo_reserve(robj, false);
420 if (unlikely(r != 0))
421 goto out;
422
423 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
424 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
425 r = amdgpu_bo_get_metadata(robj, args->data.data,
426 sizeof(args->data.data),
427 &args->data.data_size_bytes,
428 &args->data.flags);
429 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
430 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
431 if (!r)
432 r = amdgpu_bo_set_metadata(robj, args->data.data,
433 args->data.data_size_bytes,
434 args->data.flags);
435 }
436
437 amdgpu_bo_unreserve(robj);
438out:
439 drm_gem_object_unreference_unlocked(gobj);
440 return r;
441}
442
443/**
444 * amdgpu_gem_va_update_vm -update the bo_va in its VM
445 *
446 * @adev: amdgpu_device pointer
447 * @bo_va: bo_va to update
448 *
449 * Update the bo_va directly after setting it's address. Errors are not
450 * vital here, so they are not reported back to userspace.
451 */
452static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
monk.liu194a3362015-07-22 13:29:28 +0800453 struct amdgpu_bo_va *bo_va, uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454{
455 struct ttm_validate_buffer tv, *entry;
456 struct amdgpu_bo_list_entry *vm_bos;
457 struct ww_acquire_ctx ticket;
Christian Königbf60efd2015-09-04 10:47:56 +0200458 struct list_head list, duplicates;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459 unsigned domain;
460 int r;
461
462 INIT_LIST_HEAD(&list);
Christian Königbf60efd2015-09-04 10:47:56 +0200463 INIT_LIST_HEAD(&duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464
465 tv.bo = &bo_va->bo->tbo;
466 tv.shared = true;
467 list_add(&tv.head, &list);
468
469 vm_bos = amdgpu_vm_get_bos(adev, bo_va->vm, &list);
470 if (!vm_bos)
471 return;
472
Christian Königbf60efd2015-09-04 10:47:56 +0200473 /* Provide duplicates to avoid -EALREADY */
474 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475 if (r)
476 goto error_free;
477
478 list_for_each_entry(entry, &list, head) {
479 domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type);
480 /* if anything is swapped out don't swap it in here,
481 just abort and wait for the next CS */
482 if (domain == AMDGPU_GEM_DOMAIN_CPU)
483 goto error_unreserve;
484 }
485
486 mutex_lock(&bo_va->vm->mutex);
487 r = amdgpu_vm_clear_freed(adev, bo_va->vm);
488 if (r)
489 goto error_unlock;
490
monk.liu194a3362015-07-22 13:29:28 +0800491
492 if (operation == AMDGPU_VA_OP_MAP)
493 r = amdgpu_vm_bo_update(adev, bo_va, &bo_va->bo->tbo.mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400494
495error_unlock:
496 mutex_unlock(&bo_va->vm->mutex);
497
498error_unreserve:
499 ttm_eu_backoff_reservation(&ticket, &list);
500
501error_free:
502 drm_free_large(vm_bos);
503
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;
519 uint32_t invalid_flags, va_flags = 0;
520 int r = 0;
521
Christian König34b5f6a2015-06-08 15:03:00 +0200522 if (!adev->vm_manager.enabled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 return -ENOTTY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524
Christian König34b5f6a2015-06-08 15:03:00 +0200525 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526 dev_err(&dev->pdev->dev,
527 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200528 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 return -EINVAL;
531 }
532
Christian Königfc220f62015-06-29 17:12:20 +0200533 invalid_flags = ~(AMDGPU_VM_DELAY_UPDATE | AMDGPU_VM_PAGE_READABLE |
534 AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_PAGE_EXECUTABLE);
Christian König34b5f6a2015-06-08 15:03:00 +0200535 if ((args->flags & invalid_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200537 args->flags, invalid_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400538 return -EINVAL;
539 }
540
Christian König34b5f6a2015-06-08 15:03:00 +0200541 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 case AMDGPU_VA_OP_MAP:
543 case AMDGPU_VA_OP_UNMAP:
544 break;
545 default:
546 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200547 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548 return -EINVAL;
549 }
550
Christian König34b5f6a2015-06-08 15:03:00 +0200551 gobj = drm_gem_object_lookup(dev, filp, args->handle);
552 if (gobj == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 return -ENOENT;
Christian König34b5f6a2015-06-08 15:03:00 +0200554
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555 rbo = gem_to_amdgpu_bo(gobj);
556 r = amdgpu_bo_reserve(rbo, false);
557 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400558 drm_gem_object_unreference_unlocked(gobj);
559 return r;
560 }
Christian König34b5f6a2015-06-08 15:03:00 +0200561
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 bo_va = amdgpu_vm_bo_find(&fpriv->vm, rbo);
563 if (!bo_va) {
Christian König34b5f6a2015-06-08 15:03:00 +0200564 amdgpu_bo_unreserve(rbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400565 return -ENOENT;
566 }
567
Christian König34b5f6a2015-06-08 15:03:00 +0200568 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569 case AMDGPU_VA_OP_MAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200570 if (args->flags & AMDGPU_VM_PAGE_READABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571 va_flags |= AMDGPU_PTE_READABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200572 if (args->flags & AMDGPU_VM_PAGE_WRITEABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 va_flags |= AMDGPU_PTE_WRITEABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200574 if (args->flags & AMDGPU_VM_PAGE_EXECUTABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400575 va_flags |= AMDGPU_PTE_EXECUTABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200576 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
577 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200578 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 break;
580 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200581 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 break;
583 default:
584 break;
585 }
586
Christian Königfc220f62015-06-29 17:12:20 +0200587 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE))
monk.liu194a3362015-07-22 13:29:28 +0800588 amdgpu_gem_va_update_vm(adev, bo_va, args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589
590 drm_gem_object_unreference_unlocked(gobj);
591 return r;
592}
593
594int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
595 struct drm_file *filp)
596{
597 struct drm_amdgpu_gem_op *args = data;
598 struct drm_gem_object *gobj;
599 struct amdgpu_bo *robj;
600 int r;
601
602 gobj = drm_gem_object_lookup(dev, filp, args->handle);
603 if (gobj == NULL) {
604 return -ENOENT;
605 }
606 robj = gem_to_amdgpu_bo(gobj);
607
608 r = amdgpu_bo_reserve(robj, false);
609 if (unlikely(r))
610 goto out;
611
612 switch (args->op) {
613 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
614 struct drm_amdgpu_gem_create_in info;
615 void __user *out = (void __user *)(long)args->value;
616
617 info.bo_size = robj->gem_base.size;
618 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
619 info.domains = robj->initial_domain;
620 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200621 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622 if (copy_to_user(out, &info, sizeof(info)))
623 r = -EFAULT;
624 break;
625 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200626 case AMDGPU_GEM_OP_SET_PLACEMENT:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm)) {
628 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200629 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 break;
631 }
632 robj->initial_domain = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
633 AMDGPU_GEM_DOMAIN_GTT |
634 AMDGPU_GEM_DOMAIN_CPU);
Christian König4c28fb02015-08-28 17:27:54 +0200635 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 break;
637 default:
Christian König4c28fb02015-08-28 17:27:54 +0200638 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 r = -EINVAL;
640 }
641
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642out:
643 drm_gem_object_unreference_unlocked(gobj);
644 return r;
645}
646
647int amdgpu_mode_dumb_create(struct drm_file *file_priv,
648 struct drm_device *dev,
649 struct drm_mode_create_dumb *args)
650{
651 struct amdgpu_device *adev = dev->dev_private;
652 struct drm_gem_object *gobj;
653 uint32_t handle;
654 int r;
655
656 args->pitch = amdgpu_align_pitch(adev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
657 args->size = args->pitch * args->height;
658 args->size = ALIGN(args->size, PAGE_SIZE);
659
660 r = amdgpu_gem_object_create(adev, args->size, 0,
661 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400662 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
663 ttm_bo_type_device,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 &gobj);
665 if (r)
666 return -ENOMEM;
667
668 r = drm_gem_handle_create(file_priv, gobj, &handle);
669 /* drop reference from allocate - handle holds it now */
670 drm_gem_object_unreference_unlocked(gobj);
671 if (r) {
672 return r;
673 }
674 args->handle = handle;
675 return 0;
676}
677
678#if defined(CONFIG_DEBUG_FS)
679static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
680{
681 struct drm_info_node *node = (struct drm_info_node *)m->private;
682 struct drm_device *dev = node->minor->dev;
683 struct amdgpu_device *adev = dev->dev_private;
684 struct amdgpu_bo *rbo;
685 unsigned i = 0;
686
687 mutex_lock(&adev->gem.mutex);
688 list_for_each_entry(rbo, &adev->gem.objects, list) {
689 unsigned domain;
690 const char *placement;
691
692 domain = amdgpu_mem_type_to_domain(rbo->tbo.mem.mem_type);
693 switch (domain) {
694 case AMDGPU_GEM_DOMAIN_VRAM:
695 placement = "VRAM";
696 break;
697 case AMDGPU_GEM_DOMAIN_GTT:
698 placement = " GTT";
699 break;
700 case AMDGPU_GEM_DOMAIN_CPU:
701 default:
702 placement = " CPU";
703 break;
704 }
705 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
706 i, amdgpu_bo_size(rbo) >> 10, amdgpu_bo_size(rbo) >> 20,
707 placement, (unsigned long)rbo->pid);
708 i++;
709 }
710 mutex_unlock(&adev->gem.mutex);
711 return 0;
712}
713
714static struct drm_info_list amdgpu_debugfs_gem_list[] = {
715 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
716};
717#endif
718
719int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
720{
721#if defined(CONFIG_DEBUG_FS)
722 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
723#endif
724 return 0;
725}