blob: 8dd843da799faf0d397683d8c51efc6a4077c1b5 [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);
40 amdgpu_bo_unref(&robj);
41 }
42}
43
44int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
45 int alignment, u32 initial_domain,
46 u64 flags, bool kernel,
47 struct drm_gem_object **obj)
48{
49 struct amdgpu_bo *robj;
50 unsigned long max_size;
51 int r;
52
53 *obj = NULL;
54 /* At least align on page size */
55 if (alignment < PAGE_SIZE) {
56 alignment = PAGE_SIZE;
57 }
58
59 if (!(initial_domain & (AMDGPU_GEM_DOMAIN_GDS | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA))) {
60 /* Maximum bo size is the unpinned gtt size since we use the gtt to
61 * handle vram to system pool migrations.
62 */
63 max_size = adev->mc.gtt_size - adev->gart_pin_size;
64 if (size > max_size) {
65 DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
66 size >> 20, max_size >> 20);
67 return -ENOMEM;
68 }
69 }
70retry:
71 r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain, flags, NULL, &robj);
72 if (r) {
73 if (r != -ERESTARTSYS) {
74 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
75 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
76 goto retry;
77 }
78 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
79 size, initial_domain, alignment, r);
80 }
81 return r;
82 }
83 *obj = &robj->gem_base;
84 robj->pid = task_pid_nr(current);
85
86 mutex_lock(&adev->gem.mutex);
87 list_add_tail(&robj->list, &adev->gem.objects);
88 mutex_unlock(&adev->gem.mutex);
89
90 return 0;
91}
92
93int amdgpu_gem_init(struct amdgpu_device *adev)
94{
95 INIT_LIST_HEAD(&adev->gem.objects);
96 return 0;
97}
98
99void amdgpu_gem_fini(struct amdgpu_device *adev)
100{
101 amdgpu_bo_force_delete(adev);
102}
103
104/*
105 * Call from drm_gem_handle_create which appear in both new and open ioctl
106 * case.
107 */
108int amdgpu_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
109{
110 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
111 struct amdgpu_device *adev = rbo->adev;
112 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
113 struct amdgpu_vm *vm = &fpriv->vm;
114 struct amdgpu_bo_va *bo_va;
115 int r;
116
117 r = amdgpu_bo_reserve(rbo, false);
118 if (r) {
119 return r;
120 }
121
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);
129
130 return 0;
131}
132
133void amdgpu_gem_object_close(struct drm_gem_object *obj,
134 struct drm_file *file_priv)
135{
136 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
137 struct amdgpu_device *adev = rbo->adev;
138 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
139 struct amdgpu_vm *vm = &fpriv->vm;
140 struct amdgpu_bo_va *bo_va;
141 int r;
142
143 r = amdgpu_bo_reserve(rbo, true);
144 if (r) {
145 dev_err(adev->dev, "leaking bo va because "
146 "we fail to reserve bo (%d)\n", r);
147 return;
148 }
149 bo_va = amdgpu_vm_bo_find(vm, rbo);
150 if (bo_va) {
151 if (--bo_va->ref_count == 0) {
152 amdgpu_vm_bo_rmv(adev, bo_va);
153 }
154 }
155 amdgpu_bo_unreserve(rbo);
156}
157
158static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r)
159{
160 if (r == -EDEADLK) {
161 r = amdgpu_gpu_reset(adev);
162 if (!r)
163 r = -EAGAIN;
164 }
165 return r;
166}
167
168/*
169 * GEM ioctls.
170 */
171int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
172 struct drm_file *filp)
173{
174 struct amdgpu_device *adev = dev->dev_private;
175 union drm_amdgpu_gem_create *args = data;
176 uint64_t size = args->in.bo_size;
177 struct drm_gem_object *gobj;
178 uint32_t handle;
179 bool kernel = false;
180 int r;
181
182 down_read(&adev->exclusive_lock);
183 /* create a gem object to contain this object in */
184 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
185 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
186 kernel = true;
187 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
188 size = size << AMDGPU_GDS_SHIFT;
189 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
190 size = size << AMDGPU_GWS_SHIFT;
191 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
192 size = size << AMDGPU_OA_SHIFT;
193 else {
194 r = -EINVAL;
195 goto error_unlock;
196 }
197 }
198 size = roundup(size, PAGE_SIZE);
199
200 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
201 (u32)(0xffffffff & args->in.domains),
202 args->in.domain_flags,
203 kernel, &gobj);
204 if (r)
205 goto error_unlock;
206
207 r = drm_gem_handle_create(filp, gobj, &handle);
208 /* drop reference from allocate - handle holds it now */
209 drm_gem_object_unreference_unlocked(gobj);
210 if (r)
211 goto error_unlock;
212
213 memset(args, 0, sizeof(*args));
214 args->out.handle = handle;
215 up_read(&adev->exclusive_lock);
216 return 0;
217
218error_unlock:
219 up_read(&adev->exclusive_lock);
220 r = amdgpu_gem_handle_lockup(adev, r);
221 return r;
222}
223
224int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
225 struct drm_file *filp)
226{
227 struct amdgpu_device *adev = dev->dev_private;
228 struct drm_amdgpu_gem_userptr *args = data;
229 struct drm_gem_object *gobj;
230 struct amdgpu_bo *bo;
231 uint32_t handle;
232 int r;
233
234 if (offset_in_page(args->addr | args->size))
235 return -EINVAL;
236
237 /* reject unknown flag values */
238 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
239 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
240 AMDGPU_GEM_USERPTR_REGISTER))
241 return -EINVAL;
242
243 if (!(args->flags & AMDGPU_GEM_USERPTR_ANONONLY) ||
244 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
245
246 /* if we want to write to it we must require anonymous
247 memory and install a MMU notifier */
248 return -EACCES;
249 }
250
251 down_read(&adev->exclusive_lock);
252
253 /* create a gem object to contain this object in */
254 r = amdgpu_gem_object_create(adev, args->size, 0,
255 AMDGPU_GEM_DOMAIN_CPU, 0,
256 0, &gobj);
257 if (r)
258 goto handle_lockup;
259
260 bo = gem_to_amdgpu_bo(gobj);
261 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
262 if (r)
263 goto release_object;
264
265 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
266 r = amdgpu_mn_register(bo, args->addr);
267 if (r)
268 goto release_object;
269 }
270
271 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
272 down_read(&current->mm->mmap_sem);
273 r = amdgpu_bo_reserve(bo, true);
274 if (r) {
275 up_read(&current->mm->mmap_sem);
276 goto release_object;
277 }
278
279 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
280 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
281 amdgpu_bo_unreserve(bo);
282 up_read(&current->mm->mmap_sem);
283 if (r)
284 goto release_object;
285 }
286
287 r = drm_gem_handle_create(filp, gobj, &handle);
288 /* drop reference from allocate - handle holds it now */
289 drm_gem_object_unreference_unlocked(gobj);
290 if (r)
291 goto handle_lockup;
292
293 args->handle = handle;
294 up_read(&adev->exclusive_lock);
295 return 0;
296
297release_object:
298 drm_gem_object_unreference_unlocked(gobj);
299
300handle_lockup:
301 up_read(&adev->exclusive_lock);
302 r = amdgpu_gem_handle_lockup(adev, r);
303
304 return r;
305}
306
307int amdgpu_mode_dumb_mmap(struct drm_file *filp,
308 struct drm_device *dev,
309 uint32_t handle, uint64_t *offset_p)
310{
311 struct drm_gem_object *gobj;
312 struct amdgpu_bo *robj;
313
314 gobj = drm_gem_object_lookup(dev, filp, handle);
315 if (gobj == NULL) {
316 return -ENOENT;
317 }
318 robj = gem_to_amdgpu_bo(gobj);
Christian König271c8122015-05-13 14:30:53 +0200319 if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm) ||
320 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400321 drm_gem_object_unreference_unlocked(gobj);
322 return -EPERM;
323 }
324 *offset_p = amdgpu_bo_mmap_offset(robj);
325 drm_gem_object_unreference_unlocked(gobj);
326 return 0;
327}
328
329int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
330 struct drm_file *filp)
331{
332 union drm_amdgpu_gem_mmap *args = data;
333 uint32_t handle = args->in.handle;
334 memset(args, 0, sizeof(*args));
335 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
336}
337
338/**
339 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
340 *
341 * @timeout_ns: timeout in ns
342 *
343 * Calculate the timeout in jiffies from an absolute timeout in ns.
344 */
345unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
346{
347 unsigned long timeout_jiffies;
348 ktime_t timeout;
349
350 /* clamp timeout if it's to large */
351 if (((int64_t)timeout_ns) < 0)
352 return MAX_SCHEDULE_TIMEOUT;
353
354 timeout = ktime_sub_ns(ktime_get(), timeout_ns);
355 if (ktime_to_ns(timeout) < 0)
356 return 0;
357
358 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
359 /* clamp timeout to avoid unsigned-> signed overflow */
360 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
361 return MAX_SCHEDULE_TIMEOUT - 1;
362
363 return timeout_jiffies;
364}
365
366int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
367 struct drm_file *filp)
368{
369 struct amdgpu_device *adev = dev->dev_private;
370 union drm_amdgpu_gem_wait_idle *args = data;
371 struct drm_gem_object *gobj;
372 struct amdgpu_bo *robj;
373 uint32_t handle = args->in.handle;
374 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
375 int r = 0;
376 long ret;
377
378 gobj = drm_gem_object_lookup(dev, filp, handle);
379 if (gobj == NULL) {
380 return -ENOENT;
381 }
382 robj = gem_to_amdgpu_bo(gobj);
383 if (timeout == 0)
384 ret = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
385 else
386 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, timeout);
387
388 /* ret == 0 means not signaled,
389 * ret > 0 means signaled
390 * ret < 0 means interrupted before timeout
391 */
392 if (ret >= 0) {
393 memset(args, 0, sizeof(*args));
394 args->out.status = (ret == 0);
395 } else
396 r = ret;
397
398 drm_gem_object_unreference_unlocked(gobj);
399 r = amdgpu_gem_handle_lockup(adev, r);
400 return r;
401}
402
403int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
404 struct drm_file *filp)
405{
406 struct drm_amdgpu_gem_metadata *args = data;
407 struct drm_gem_object *gobj;
408 struct amdgpu_bo *robj;
409 int r = -1;
410
411 DRM_DEBUG("%d \n", args->handle);
412 gobj = drm_gem_object_lookup(dev, filp, args->handle);
413 if (gobj == NULL)
414 return -ENOENT;
415 robj = gem_to_amdgpu_bo(gobj);
416
417 r = amdgpu_bo_reserve(robj, false);
418 if (unlikely(r != 0))
419 goto out;
420
421 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
422 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
423 r = amdgpu_bo_get_metadata(robj, args->data.data,
424 sizeof(args->data.data),
425 &args->data.data_size_bytes,
426 &args->data.flags);
427 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
428 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
429 if (!r)
430 r = amdgpu_bo_set_metadata(robj, args->data.data,
431 args->data.data_size_bytes,
432 args->data.flags);
433 }
434
435 amdgpu_bo_unreserve(robj);
436out:
437 drm_gem_object_unreference_unlocked(gobj);
438 return r;
439}
440
441/**
442 * amdgpu_gem_va_update_vm -update the bo_va in its VM
443 *
444 * @adev: amdgpu_device pointer
445 * @bo_va: bo_va to update
446 *
447 * Update the bo_va directly after setting it's address. Errors are not
448 * vital here, so they are not reported back to userspace.
449 */
450static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
451 struct amdgpu_bo_va *bo_va)
452{
453 struct ttm_validate_buffer tv, *entry;
454 struct amdgpu_bo_list_entry *vm_bos;
455 struct ww_acquire_ctx ticket;
456 struct list_head list;
457 unsigned domain;
458 int r;
459
460 INIT_LIST_HEAD(&list);
461
462 tv.bo = &bo_va->bo->tbo;
463 tv.shared = true;
464 list_add(&tv.head, &list);
465
466 vm_bos = amdgpu_vm_get_bos(adev, bo_va->vm, &list);
467 if (!vm_bos)
468 return;
469
470 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
471 if (r)
472 goto error_free;
473
474 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 }
481
482 mutex_lock(&bo_va->vm->mutex);
483 r = amdgpu_vm_clear_freed(adev, bo_va->vm);
484 if (r)
485 goto error_unlock;
486
487 r = amdgpu_vm_bo_update(adev, bo_va, &bo_va->bo->tbo.mem);
488
489error_unlock:
490 mutex_unlock(&bo_va->vm->mutex);
491
492error_unreserve:
493 ttm_eu_backoff_reservation(&ticket, &list);
494
495error_free:
496 drm_free_large(vm_bos);
497
498 if (r)
499 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
500}
501
502
503
504int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
505 struct drm_file *filp)
506{
507 union drm_amdgpu_gem_va *args = data;
508 struct drm_gem_object *gobj;
509 struct amdgpu_device *adev = dev->dev_private;
510 struct amdgpu_fpriv *fpriv = filp->driver_priv;
511 struct amdgpu_bo *rbo;
512 struct amdgpu_bo_va *bo_va;
513 uint32_t invalid_flags, va_flags = 0;
514 int r = 0;
515
516 if (!adev->vm_manager.enabled) {
517 memset(args, 0, sizeof(*args));
518 args->out.result = AMDGPU_VA_RESULT_ERROR;
519 return -ENOTTY;
520 }
521
522 if (args->in.va_address < AMDGPU_VA_RESERVED_SIZE) {
523 dev_err(&dev->pdev->dev,
524 "va_address 0x%lX is in reserved area 0x%X\n",
525 (unsigned long)args->in.va_address,
526 AMDGPU_VA_RESERVED_SIZE);
527 memset(args, 0, sizeof(*args));
528 args->out.result = AMDGPU_VA_RESULT_ERROR;
529 return -EINVAL;
530 }
531
532 invalid_flags = ~(AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
533 AMDGPU_VM_PAGE_EXECUTABLE);
534 if ((args->in.flags & invalid_flags)) {
535 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
536 args->in.flags, invalid_flags);
537 memset(args, 0, sizeof(*args));
538 args->out.result = AMDGPU_VA_RESULT_ERROR;
539 return -EINVAL;
540 }
541
542 switch (args->in.operation) {
543 case AMDGPU_VA_OP_MAP:
544 case AMDGPU_VA_OP_UNMAP:
545 break;
546 default:
547 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
548 args->in.operation);
549 memset(args, 0, sizeof(*args));
550 args->out.result = AMDGPU_VA_RESULT_ERROR;
551 return -EINVAL;
552 }
553
554 gobj = drm_gem_object_lookup(dev, filp, args->in.handle);
555 if (gobj == NULL) {
556 memset(args, 0, sizeof(*args));
557 args->out.result = AMDGPU_VA_RESULT_ERROR;
558 return -ENOENT;
559 }
560 rbo = gem_to_amdgpu_bo(gobj);
561 r = amdgpu_bo_reserve(rbo, false);
562 if (r) {
563 if (r != -ERESTARTSYS) {
564 memset(args, 0, sizeof(*args));
565 args->out.result = AMDGPU_VA_RESULT_ERROR;
566 }
567 drm_gem_object_unreference_unlocked(gobj);
568 return r;
569 }
570 bo_va = amdgpu_vm_bo_find(&fpriv->vm, rbo);
571 if (!bo_va) {
572 memset(args, 0, sizeof(*args));
573 args->out.result = AMDGPU_VA_RESULT_ERROR;
574 drm_gem_object_unreference_unlocked(gobj);
575 return -ENOENT;
576 }
577
578 switch (args->in.operation) {
579 case AMDGPU_VA_OP_MAP:
580 if (args->in.flags & AMDGPU_VM_PAGE_READABLE)
581 va_flags |= AMDGPU_PTE_READABLE;
582 if (args->in.flags & AMDGPU_VM_PAGE_WRITEABLE)
583 va_flags |= AMDGPU_PTE_WRITEABLE;
584 if (args->in.flags & AMDGPU_VM_PAGE_EXECUTABLE)
585 va_flags |= AMDGPU_PTE_EXECUTABLE;
586 r = amdgpu_vm_bo_map(adev, bo_va, args->in.va_address, 0,
587 amdgpu_bo_size(bo_va->bo), va_flags);
588 break;
589 case AMDGPU_VA_OP_UNMAP:
590 r = amdgpu_vm_bo_unmap(adev, bo_va, args->in.va_address);
591 break;
592 default:
593 break;
594 }
595
596 if (!r) {
597 amdgpu_gem_va_update_vm(adev, bo_va);
598 memset(args, 0, sizeof(*args));
599 args->out.result = AMDGPU_VA_RESULT_OK;
600 } else {
601 memset(args, 0, sizeof(*args));
602 args->out.result = AMDGPU_VA_RESULT_ERROR;
603 }
604
605 drm_gem_object_unreference_unlocked(gobj);
606 return r;
607}
608
609int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
610 struct drm_file *filp)
611{
612 struct drm_amdgpu_gem_op *args = data;
613 struct drm_gem_object *gobj;
614 struct amdgpu_bo *robj;
615 int r;
616
617 gobj = drm_gem_object_lookup(dev, filp, args->handle);
618 if (gobj == NULL) {
619 return -ENOENT;
620 }
621 robj = gem_to_amdgpu_bo(gobj);
622
623 r = amdgpu_bo_reserve(robj, false);
624 if (unlikely(r))
625 goto out;
626
627 switch (args->op) {
628 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
629 struct drm_amdgpu_gem_create_in info;
630 void __user *out = (void __user *)(long)args->value;
631
632 info.bo_size = robj->gem_base.size;
633 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
634 info.domains = robj->initial_domain;
635 info.domain_flags = robj->flags;
636 if (copy_to_user(out, &info, sizeof(info)))
637 r = -EFAULT;
638 break;
639 }
640 case AMDGPU_GEM_OP_SET_INITIAL_DOMAIN:
641 if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm)) {
642 r = -EPERM;
643 break;
644 }
645 robj->initial_domain = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
646 AMDGPU_GEM_DOMAIN_GTT |
647 AMDGPU_GEM_DOMAIN_CPU);
648 break;
649 default:
650 r = -EINVAL;
651 }
652
653 amdgpu_bo_unreserve(robj);
654out:
655 drm_gem_object_unreference_unlocked(gobj);
656 return r;
657}
658
659int amdgpu_mode_dumb_create(struct drm_file *file_priv,
660 struct drm_device *dev,
661 struct drm_mode_create_dumb *args)
662{
663 struct amdgpu_device *adev = dev->dev_private;
664 struct drm_gem_object *gobj;
665 uint32_t handle;
666 int r;
667
668 args->pitch = amdgpu_align_pitch(adev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
669 args->size = args->pitch * args->height;
670 args->size = ALIGN(args->size, PAGE_SIZE);
671
672 r = amdgpu_gem_object_create(adev, args->size, 0,
673 AMDGPU_GEM_DOMAIN_VRAM,
674 0, ttm_bo_type_device,
675 &gobj);
676 if (r)
677 return -ENOMEM;
678
679 r = drm_gem_handle_create(file_priv, gobj, &handle);
680 /* drop reference from allocate - handle holds it now */
681 drm_gem_object_unreference_unlocked(gobj);
682 if (r) {
683 return r;
684 }
685 args->handle = handle;
686 return 0;
687}
688
689#if defined(CONFIG_DEBUG_FS)
690static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
691{
692 struct drm_info_node *node = (struct drm_info_node *)m->private;
693 struct drm_device *dev = node->minor->dev;
694 struct amdgpu_device *adev = dev->dev_private;
695 struct amdgpu_bo *rbo;
696 unsigned i = 0;
697
698 mutex_lock(&adev->gem.mutex);
699 list_for_each_entry(rbo, &adev->gem.objects, list) {
700 unsigned domain;
701 const char *placement;
702
703 domain = amdgpu_mem_type_to_domain(rbo->tbo.mem.mem_type);
704 switch (domain) {
705 case AMDGPU_GEM_DOMAIN_VRAM:
706 placement = "VRAM";
707 break;
708 case AMDGPU_GEM_DOMAIN_GTT:
709 placement = " GTT";
710 break;
711 case AMDGPU_GEM_DOMAIN_CPU:
712 default:
713 placement = " CPU";
714 break;
715 }
716 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
717 i, amdgpu_bo_size(rbo) >> 10, amdgpu_bo_size(rbo) >> 20,
718 placement, (unsigned long)rbo->pid);
719 i++;
720 }
721 mutex_unlock(&adev->gem.mutex);
722 return 0;
723}
724
725static struct drm_info_list amdgpu_debugfs_gem_list[] = {
726 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
727};
728#endif
729
730int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
731{
732#if defined(CONFIG_DEBUG_FS)
733 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
734#endif
735 return 0;
736}