]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
drm/amd/powerplay: force clock levels for smu11
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_gem.c
1 /*
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 <linux/pagemap.h>
30 #include <drm/drmP.h>
31 #include <drm/amdgpu_drm.h>
32 #include "amdgpu.h"
33 #include "amdgpu_display.h"
34
35 void amdgpu_gem_object_free(struct drm_gem_object *gobj)
36 {
37         struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
38
39         if (robj) {
40                 amdgpu_mn_unregister(robj);
41                 amdgpu_bo_unref(&robj);
42         }
43 }
44
45 int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
46                              int alignment, u32 initial_domain,
47                              u64 flags, enum ttm_bo_type type,
48                              struct reservation_object *resv,
49                              struct drm_gem_object **obj)
50 {
51         struct amdgpu_bo *bo;
52         struct amdgpu_bo_param bp;
53         int r;
54
55         memset(&bp, 0, sizeof(bp));
56         *obj = NULL;
57
58         bp.size = size;
59         bp.byte_align = alignment;
60         bp.type = type;
61         bp.resv = resv;
62         bp.preferred_domain = initial_domain;
63 retry:
64         bp.flags = flags;
65         bp.domain = initial_domain;
66         r = amdgpu_bo_create(adev, &bp, &bo);
67         if (r) {
68                 if (r != -ERESTARTSYS) {
69                         if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
70                                 flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
71                                 goto retry;
72                         }
73
74                         if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
75                                 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
76                                 goto retry;
77                         }
78                         DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
79                                   size, initial_domain, alignment, r);
80                 }
81                 return r;
82         }
83         *obj = &bo->gem_base;
84
85         return 0;
86 }
87
88 void amdgpu_gem_force_release(struct amdgpu_device *adev)
89 {
90         struct drm_device *ddev = adev->ddev;
91         struct drm_file *file;
92
93         mutex_lock(&ddev->filelist_mutex);
94
95         list_for_each_entry(file, &ddev->filelist, lhead) {
96                 struct drm_gem_object *gobj;
97                 int handle;
98
99                 WARN_ONCE(1, "Still active user space clients!\n");
100                 spin_lock(&file->table_lock);
101                 idr_for_each_entry(&file->object_idr, gobj, handle) {
102                         WARN_ONCE(1, "And also active allocations!\n");
103                         drm_gem_object_put_unlocked(gobj);
104                 }
105                 idr_destroy(&file->object_idr);
106                 spin_unlock(&file->table_lock);
107         }
108
109         mutex_unlock(&ddev->filelist_mutex);
110 }
111
112 /*
113  * Call from drm_gem_handle_create which appear in both new and open ioctl
114  * case.
115  */
116 int amdgpu_gem_object_open(struct drm_gem_object *obj,
117                            struct drm_file *file_priv)
118 {
119         struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
120         struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
121         struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
122         struct amdgpu_vm *vm = &fpriv->vm;
123         struct amdgpu_bo_va *bo_va;
124         struct mm_struct *mm;
125         int r;
126
127         mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
128         if (mm && mm != current->mm)
129                 return -EPERM;
130
131         if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
132             abo->tbo.resv != vm->root.base.bo->tbo.resv)
133                 return -EPERM;
134
135         r = amdgpu_bo_reserve(abo, false);
136         if (r)
137                 return r;
138
139         bo_va = amdgpu_vm_bo_find(vm, abo);
140         if (!bo_va) {
141                 bo_va = amdgpu_vm_bo_add(adev, vm, abo);
142         } else {
143                 ++bo_va->ref_count;
144         }
145         amdgpu_bo_unreserve(abo);
146         return 0;
147 }
148
149 void amdgpu_gem_object_close(struct drm_gem_object *obj,
150                              struct drm_file *file_priv)
151 {
152         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
153         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
154         struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
155         struct amdgpu_vm *vm = &fpriv->vm;
156
157         struct amdgpu_bo_list_entry vm_pd;
158         struct list_head list, duplicates;
159         struct ttm_validate_buffer tv;
160         struct ww_acquire_ctx ticket;
161         struct amdgpu_bo_va *bo_va;
162         int r;
163
164         INIT_LIST_HEAD(&list);
165         INIT_LIST_HEAD(&duplicates);
166
167         tv.bo = &bo->tbo;
168         tv.num_shared = 1;
169         list_add(&tv.head, &list);
170
171         amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
172
173         r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
174         if (r) {
175                 dev_err(adev->dev, "leaking bo va because "
176                         "we fail to reserve bo (%d)\n", r);
177                 return;
178         }
179         bo_va = amdgpu_vm_bo_find(vm, bo);
180         if (bo_va && --bo_va->ref_count == 0) {
181                 amdgpu_vm_bo_rmv(adev, bo_va);
182
183                 if (amdgpu_vm_ready(vm)) {
184                         struct dma_fence *fence = NULL;
185
186                         r = amdgpu_vm_clear_freed(adev, vm, &fence);
187                         if (unlikely(r)) {
188                                 dev_err(adev->dev, "failed to clear page "
189                                         "tables on GEM object close (%d)\n", r);
190                         }
191
192                         if (fence) {
193                                 amdgpu_bo_fence(bo, fence, true);
194                                 dma_fence_put(fence);
195                         }
196                 }
197         }
198         ttm_eu_backoff_reservation(&ticket, &list);
199 }
200
201 /*
202  * GEM ioctls.
203  */
204 int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
205                             struct drm_file *filp)
206 {
207         struct amdgpu_device *adev = dev->dev_private;
208         struct amdgpu_fpriv *fpriv = filp->driver_priv;
209         struct amdgpu_vm *vm = &fpriv->vm;
210         union drm_amdgpu_gem_create *args = data;
211         uint64_t flags = args->in.domain_flags;
212         uint64_t size = args->in.bo_size;
213         struct reservation_object *resv = NULL;
214         struct drm_gem_object *gobj;
215         uint32_t handle;
216         int r;
217
218         /* reject invalid gem flags */
219         if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
220                       AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
221                       AMDGPU_GEM_CREATE_CPU_GTT_USWC |
222                       AMDGPU_GEM_CREATE_VRAM_CLEARED |
223                       AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
224                       AMDGPU_GEM_CREATE_EXPLICIT_SYNC))
225
226                 return -EINVAL;
227
228         /* reject invalid gem domains */
229         if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
230                 return -EINVAL;
231
232         /* create a gem object to contain this object in */
233         if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
234             AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
235                 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
236                         /* if gds bo is created from user space, it must be
237                          * passed to bo list
238                          */
239                         DRM_ERROR("GDS bo cannot be per-vm-bo\n");
240                         return -EINVAL;
241                 }
242                 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
243         }
244
245         if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
246                 r = amdgpu_bo_reserve(vm->root.base.bo, false);
247                 if (r)
248                         return r;
249
250                 resv = vm->root.base.bo->tbo.resv;
251         }
252
253         r = amdgpu_gem_object_create(adev, size, args->in.alignment,
254                                      (u32)(0xffffffff & args->in.domains),
255                                      flags, ttm_bo_type_device, resv, &gobj);
256         if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
257                 if (!r) {
258                         struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
259
260                         abo->parent = amdgpu_bo_ref(vm->root.base.bo);
261                 }
262                 amdgpu_bo_unreserve(vm->root.base.bo);
263         }
264         if (r)
265                 return r;
266
267         r = drm_gem_handle_create(filp, gobj, &handle);
268         /* drop reference from allocate - handle holds it now */
269         drm_gem_object_put_unlocked(gobj);
270         if (r)
271                 return r;
272
273         memset(args, 0, sizeof(*args));
274         args->out.handle = handle;
275         return 0;
276 }
277
278 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
279                              struct drm_file *filp)
280 {
281         struct ttm_operation_ctx ctx = { true, false };
282         struct amdgpu_device *adev = dev->dev_private;
283         struct drm_amdgpu_gem_userptr *args = data;
284         struct drm_gem_object *gobj;
285         struct amdgpu_bo *bo;
286         uint32_t handle;
287         int r;
288
289         if (offset_in_page(args->addr | args->size))
290                 return -EINVAL;
291
292         /* reject unknown flag values */
293         if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
294             AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
295             AMDGPU_GEM_USERPTR_REGISTER))
296                 return -EINVAL;
297
298         if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
299              !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
300
301                 /* if we want to write to it we must install a MMU notifier */
302                 return -EACCES;
303         }
304
305         /* create a gem object to contain this object in */
306         r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
307                                      0, ttm_bo_type_device, NULL, &gobj);
308         if (r)
309                 return r;
310
311         bo = gem_to_amdgpu_bo(gobj);
312         bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
313         bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
314         r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
315         if (r)
316                 goto release_object;
317
318         if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
319                 r = amdgpu_mn_register(bo, args->addr);
320                 if (r)
321                         goto release_object;
322         }
323
324         if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
325                 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
326                                                  bo->tbo.ttm->pages);
327                 if (r)
328                         goto release_object;
329
330                 r = amdgpu_bo_reserve(bo, true);
331                 if (r)
332                         goto user_pages_done;
333
334                 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
335                 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
336                 amdgpu_bo_unreserve(bo);
337                 if (r)
338                         goto user_pages_done;
339         }
340
341         r = drm_gem_handle_create(filp, gobj, &handle);
342         if (r)
343                 goto user_pages_done;
344
345         args->handle = handle;
346
347 user_pages_done:
348         if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE)
349                 amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm);
350
351 release_object:
352         drm_gem_object_put_unlocked(gobj);
353
354         return r;
355 }
356
357 int amdgpu_mode_dumb_mmap(struct drm_file *filp,
358                           struct drm_device *dev,
359                           uint32_t handle, uint64_t *offset_p)
360 {
361         struct drm_gem_object *gobj;
362         struct amdgpu_bo *robj;
363
364         gobj = drm_gem_object_lookup(filp, handle);
365         if (gobj == NULL) {
366                 return -ENOENT;
367         }
368         robj = gem_to_amdgpu_bo(gobj);
369         if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
370             (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
371                 drm_gem_object_put_unlocked(gobj);
372                 return -EPERM;
373         }
374         *offset_p = amdgpu_bo_mmap_offset(robj);
375         drm_gem_object_put_unlocked(gobj);
376         return 0;
377 }
378
379 int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
380                           struct drm_file *filp)
381 {
382         union drm_amdgpu_gem_mmap *args = data;
383         uint32_t handle = args->in.handle;
384         memset(args, 0, sizeof(*args));
385         return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
386 }
387
388 /**
389  * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
390  *
391  * @timeout_ns: timeout in ns
392  *
393  * Calculate the timeout in jiffies from an absolute timeout in ns.
394  */
395 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
396 {
397         unsigned long timeout_jiffies;
398         ktime_t timeout;
399
400         /* clamp timeout if it's to large */
401         if (((int64_t)timeout_ns) < 0)
402                 return MAX_SCHEDULE_TIMEOUT;
403
404         timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
405         if (ktime_to_ns(timeout) < 0)
406                 return 0;
407
408         timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
409         /*  clamp timeout to avoid unsigned-> signed overflow */
410         if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
411                 return MAX_SCHEDULE_TIMEOUT - 1;
412
413         return timeout_jiffies;
414 }
415
416 int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
417                               struct drm_file *filp)
418 {
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
427         gobj = drm_gem_object_lookup(filp, handle);
428         if (gobj == NULL) {
429                 return -ENOENT;
430         }
431         robj = gem_to_amdgpu_bo(gobj);
432         ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
433                                                   timeout);
434
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_put_unlocked(gobj);
446         return r;
447 }
448
449 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
450                                 struct drm_file *filp)
451 {
452         struct drm_amdgpu_gem_metadata *args = data;
453         struct drm_gem_object *gobj;
454         struct amdgpu_bo *robj;
455         int r = -1;
456
457         DRM_DEBUG("%d \n", args->handle);
458         gobj = drm_gem_object_lookup(filp, args->handle);
459         if (gobj == NULL)
460                 return -ENOENT;
461         robj = gem_to_amdgpu_bo(gobj);
462
463         r = amdgpu_bo_reserve(robj, false);
464         if (unlikely(r != 0))
465                 goto out;
466
467         if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
468                 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
469                 r = amdgpu_bo_get_metadata(robj, args->data.data,
470                                            sizeof(args->data.data),
471                                            &args->data.data_size_bytes,
472                                            &args->data.flags);
473         } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
474                 if (args->data.data_size_bytes > sizeof(args->data.data)) {
475                         r = -EINVAL;
476                         goto unreserve;
477                 }
478                 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
479                 if (!r)
480                         r = amdgpu_bo_set_metadata(robj, args->data.data,
481                                                    args->data.data_size_bytes,
482                                                    args->data.flags);
483         }
484
485 unreserve:
486         amdgpu_bo_unreserve(robj);
487 out:
488         drm_gem_object_put_unlocked(gobj);
489         return r;
490 }
491
492 /**
493  * amdgpu_gem_va_update_vm -update the bo_va in its VM
494  *
495  * @adev: amdgpu_device pointer
496  * @vm: vm to update
497  * @bo_va: bo_va to update
498  * @operation: map, unmap or clear
499  *
500  * Update the bo_va directly after setting its address. Errors are not
501  * vital here, so they are not reported back to userspace.
502  */
503 static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
504                                     struct amdgpu_vm *vm,
505                                     struct amdgpu_bo_va *bo_va,
506                                     uint32_t operation)
507 {
508         int r;
509
510         if (!amdgpu_vm_ready(vm))
511                 return;
512
513         r = amdgpu_vm_clear_freed(adev, vm, NULL);
514         if (r)
515                 goto error;
516
517         if (operation == AMDGPU_VA_OP_MAP ||
518             operation == AMDGPU_VA_OP_REPLACE) {
519                 r = amdgpu_vm_bo_update(adev, bo_va, false);
520                 if (r)
521                         goto error;
522         }
523
524         r = amdgpu_vm_update_directories(adev, vm);
525
526 error:
527         if (r && r != -ERESTARTSYS)
528                 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
529 }
530
531 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
532                           struct drm_file *filp)
533 {
534         const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
535                 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
536                 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
537         const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
538                 AMDGPU_VM_PAGE_PRT;
539
540         struct drm_amdgpu_gem_va *args = data;
541         struct drm_gem_object *gobj;
542         struct amdgpu_device *adev = dev->dev_private;
543         struct amdgpu_fpriv *fpriv = filp->driver_priv;
544         struct amdgpu_bo *abo;
545         struct amdgpu_bo_va *bo_va;
546         struct amdgpu_bo_list_entry vm_pd;
547         struct ttm_validate_buffer tv;
548         struct ww_acquire_ctx ticket;
549         struct list_head list, duplicates;
550         uint64_t va_flags;
551         int r = 0;
552
553         if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
554                 dev_dbg(&dev->pdev->dev,
555                         "va_address 0x%LX is in reserved area 0x%LX\n",
556                         args->va_address, AMDGPU_VA_RESERVED_SIZE);
557                 return -EINVAL;
558         }
559
560         if (args->va_address >= AMDGPU_GMC_HOLE_START &&
561             args->va_address < AMDGPU_GMC_HOLE_END) {
562                 dev_dbg(&dev->pdev->dev,
563                         "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
564                         args->va_address, AMDGPU_GMC_HOLE_START,
565                         AMDGPU_GMC_HOLE_END);
566                 return -EINVAL;
567         }
568
569         args->va_address &= AMDGPU_GMC_HOLE_MASK;
570
571         if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
572                 dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
573                         args->flags);
574                 return -EINVAL;
575         }
576
577         switch (args->operation) {
578         case AMDGPU_VA_OP_MAP:
579         case AMDGPU_VA_OP_UNMAP:
580         case AMDGPU_VA_OP_CLEAR:
581         case AMDGPU_VA_OP_REPLACE:
582                 break;
583         default:
584                 dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
585                         args->operation);
586                 return -EINVAL;
587         }
588
589         INIT_LIST_HEAD(&list);
590         INIT_LIST_HEAD(&duplicates);
591         if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
592             !(args->flags & AMDGPU_VM_PAGE_PRT)) {
593                 gobj = drm_gem_object_lookup(filp, args->handle);
594                 if (gobj == NULL)
595                         return -ENOENT;
596                 abo = gem_to_amdgpu_bo(gobj);
597                 tv.bo = &abo->tbo;
598                 if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
599                         tv.num_shared = 1;
600                 else
601                         tv.num_shared = 0;
602                 list_add(&tv.head, &list);
603         } else {
604                 gobj = NULL;
605                 abo = NULL;
606         }
607
608         amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
609
610         r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
611         if (r)
612                 goto error_unref;
613
614         if (abo) {
615                 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
616                 if (!bo_va) {
617                         r = -ENOENT;
618                         goto error_backoff;
619                 }
620         } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
621                 bo_va = fpriv->prt_va;
622         } else {
623                 bo_va = NULL;
624         }
625
626         switch (args->operation) {
627         case AMDGPU_VA_OP_MAP:
628                 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
629                                         args->map_size);
630                 if (r)
631                         goto error_backoff;
632
633                 va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
634                 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
635                                      args->offset_in_bo, args->map_size,
636                                      va_flags);
637                 break;
638         case AMDGPU_VA_OP_UNMAP:
639                 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
640                 break;
641
642         case AMDGPU_VA_OP_CLEAR:
643                 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
644                                                 args->va_address,
645                                                 args->map_size);
646                 break;
647         case AMDGPU_VA_OP_REPLACE:
648                 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
649                                         args->map_size);
650                 if (r)
651                         goto error_backoff;
652
653                 va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
654                 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
655                                              args->offset_in_bo, args->map_size,
656                                              va_flags);
657                 break;
658         default:
659                 break;
660         }
661         if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
662                 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
663                                         args->operation);
664
665 error_backoff:
666         ttm_eu_backoff_reservation(&ticket, &list);
667
668 error_unref:
669         drm_gem_object_put_unlocked(gobj);
670         return r;
671 }
672
673 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
674                         struct drm_file *filp)
675 {
676         struct amdgpu_device *adev = dev->dev_private;
677         struct drm_amdgpu_gem_op *args = data;
678         struct drm_gem_object *gobj;
679         struct amdgpu_bo *robj;
680         int r;
681
682         gobj = drm_gem_object_lookup(filp, args->handle);
683         if (gobj == NULL) {
684                 return -ENOENT;
685         }
686         robj = gem_to_amdgpu_bo(gobj);
687
688         r = amdgpu_bo_reserve(robj, false);
689         if (unlikely(r))
690                 goto out;
691
692         switch (args->op) {
693         case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
694                 struct drm_amdgpu_gem_create_in info;
695                 void __user *out = u64_to_user_ptr(args->value);
696
697                 info.bo_size = robj->gem_base.size;
698                 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
699                 info.domains = robj->preferred_domains;
700                 info.domain_flags = robj->flags;
701                 amdgpu_bo_unreserve(robj);
702                 if (copy_to_user(out, &info, sizeof(info)))
703                         r = -EFAULT;
704                 break;
705         }
706         case AMDGPU_GEM_OP_SET_PLACEMENT:
707                 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
708                         r = -EINVAL;
709                         amdgpu_bo_unreserve(robj);
710                         break;
711                 }
712                 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
713                         r = -EPERM;
714                         amdgpu_bo_unreserve(robj);
715                         break;
716                 }
717                 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
718                                                         AMDGPU_GEM_DOMAIN_GTT |
719                                                         AMDGPU_GEM_DOMAIN_CPU);
720                 robj->allowed_domains = robj->preferred_domains;
721                 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
722                         robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
723
724                 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
725                         amdgpu_vm_bo_invalidate(adev, robj, true);
726
727                 amdgpu_bo_unreserve(robj);
728                 break;
729         default:
730                 amdgpu_bo_unreserve(robj);
731                 r = -EINVAL;
732         }
733
734 out:
735         drm_gem_object_put_unlocked(gobj);
736         return r;
737 }
738
739 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
740                             struct drm_device *dev,
741                             struct drm_mode_create_dumb *args)
742 {
743         struct amdgpu_device *adev = dev->dev_private;
744         struct drm_gem_object *gobj;
745         uint32_t handle;
746         u32 domain;
747         int r;
748
749         args->pitch = amdgpu_align_pitch(adev, args->width,
750                                          DIV_ROUND_UP(args->bpp, 8), 0);
751         args->size = (u64)args->pitch * args->height;
752         args->size = ALIGN(args->size, PAGE_SIZE);
753         domain = amdgpu_bo_get_preferred_pin_domain(adev,
754                                 amdgpu_display_supported_domains(adev));
755         r = amdgpu_gem_object_create(adev, args->size, 0, domain,
756                                      AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
757                                      ttm_bo_type_device, NULL, &gobj);
758         if (r)
759                 return -ENOMEM;
760
761         r = drm_gem_handle_create(file_priv, gobj, &handle);
762         /* drop reference from allocate - handle holds it now */
763         drm_gem_object_put_unlocked(gobj);
764         if (r) {
765                 return r;
766         }
767         args->handle = handle;
768         return 0;
769 }
770
771 #if defined(CONFIG_DEBUG_FS)
772
773 #define amdgpu_debugfs_gem_bo_print_flag(m, bo, flag)   \
774         if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \
775                 seq_printf((m), " " #flag);             \
776         }
777
778 static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
779 {
780         struct drm_gem_object *gobj = ptr;
781         struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
782         struct seq_file *m = data;
783
784         struct dma_buf_attachment *attachment;
785         struct dma_buf *dma_buf;
786         unsigned domain;
787         const char *placement;
788         unsigned pin_count;
789
790         domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
791         switch (domain) {
792         case AMDGPU_GEM_DOMAIN_VRAM:
793                 placement = "VRAM";
794                 break;
795         case AMDGPU_GEM_DOMAIN_GTT:
796                 placement = " GTT";
797                 break;
798         case AMDGPU_GEM_DOMAIN_CPU:
799         default:
800                 placement = " CPU";
801                 break;
802         }
803         seq_printf(m, "\t0x%08x: %12ld byte %s",
804                    id, amdgpu_bo_size(bo), placement);
805
806         pin_count = READ_ONCE(bo->pin_count);
807         if (pin_count)
808                 seq_printf(m, " pin count %d", pin_count);
809
810         dma_buf = READ_ONCE(bo->gem_base.dma_buf);
811         attachment = READ_ONCE(bo->gem_base.import_attach);
812
813         if (attachment)
814                 seq_printf(m, " imported from %p", dma_buf);
815         else if (dma_buf)
816                 seq_printf(m, " exported as %p", dma_buf);
817
818         amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED);
819         amdgpu_debugfs_gem_bo_print_flag(m, bo, NO_CPU_ACCESS);
820         amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_GTT_USWC);
821         amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CLEARED);
822         amdgpu_debugfs_gem_bo_print_flag(m, bo, SHADOW);
823         amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CONTIGUOUS);
824         amdgpu_debugfs_gem_bo_print_flag(m, bo, VM_ALWAYS_VALID);
825         amdgpu_debugfs_gem_bo_print_flag(m, bo, EXPLICIT_SYNC);
826
827         seq_printf(m, "\n");
828
829         return 0;
830 }
831
832 static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
833 {
834         struct drm_info_node *node = (struct drm_info_node *)m->private;
835         struct drm_device *dev = node->minor->dev;
836         struct drm_file *file;
837         int r;
838
839         r = mutex_lock_interruptible(&dev->filelist_mutex);
840         if (r)
841                 return r;
842
843         list_for_each_entry(file, &dev->filelist, lhead) {
844                 struct task_struct *task;
845
846                 /*
847                  * Although we have a valid reference on file->pid, that does
848                  * not guarantee that the task_struct who called get_pid() is
849                  * still alive (e.g. get_pid(current) => fork() => exit()).
850                  * Therefore, we need to protect this ->comm access using RCU.
851                  */
852                 rcu_read_lock();
853                 task = pid_task(file->pid, PIDTYPE_PID);
854                 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
855                            task ? task->comm : "<unknown>");
856                 rcu_read_unlock();
857
858                 spin_lock(&file->table_lock);
859                 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
860                 spin_unlock(&file->table_lock);
861         }
862
863         mutex_unlock(&dev->filelist_mutex);
864         return 0;
865 }
866
867 static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
868         {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
869 };
870 #endif
871
872 int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
873 {
874 #if defined(CONFIG_DEBUG_FS)
875         return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
876 #endif
877         return 0;
878 }
This page took 0.087207 seconds and 4 git commands to generate.