blob: 2e26a517f2d6301e295f1a8f736f25072011b8c2 [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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 return 0;
88}
89
Christian König418aa0c2016-02-15 16:59:57 +010090void amdgpu_gem_force_release(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091{
Christian König418aa0c2016-02-15 16:59:57 +010092 struct drm_device *ddev = adev->ddev;
93 struct drm_file *file;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094
Christian König418aa0c2016-02-15 16:59:57 +010095 mutex_lock(&ddev->struct_mutex);
96
97 list_for_each_entry(file, &ddev->filelist, lhead) {
98 struct drm_gem_object *gobj;
99 int handle;
100
101 WARN_ONCE(1, "Still active user space clients!\n");
102 spin_lock(&file->table_lock);
103 idr_for_each_entry(&file->object_idr, gobj, handle) {
104 WARN_ONCE(1, "And also active allocations!\n");
105 drm_gem_object_unreference(gobj);
106 }
107 idr_destroy(&file->object_idr);
108 spin_unlock(&file->table_lock);
109 }
110
111 mutex_unlock(&ddev->struct_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112}
113
114/*
115 * Call from drm_gem_handle_create which appear in both new and open ioctl
116 * case.
117 */
118int amdgpu_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
119{
120 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
121 struct amdgpu_device *adev = rbo->adev;
122 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
123 struct amdgpu_vm *vm = &fpriv->vm;
124 struct amdgpu_bo_va *bo_va;
125 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126 r = amdgpu_bo_reserve(rbo, false);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800127 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129
130 bo_va = amdgpu_vm_bo_find(vm, rbo);
131 if (!bo_va) {
132 bo_va = amdgpu_vm_bo_add(adev, vm, rbo);
133 } else {
134 ++bo_va->ref_count;
135 }
136 amdgpu_bo_unreserve(rbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 return 0;
138}
139
140void amdgpu_gem_object_close(struct drm_gem_object *obj,
141 struct drm_file *file_priv)
142{
143 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
144 struct amdgpu_device *adev = rbo->adev;
145 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
146 struct amdgpu_vm *vm = &fpriv->vm;
147 struct amdgpu_bo_va *bo_va;
148 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149 r = amdgpu_bo_reserve(rbo, true);
150 if (r) {
151 dev_err(adev->dev, "leaking bo va because "
152 "we fail to reserve bo (%d)\n", r);
153 return;
154 }
155 bo_va = amdgpu_vm_bo_find(vm, rbo);
156 if (bo_va) {
157 if (--bo_va->ref_count == 0) {
158 amdgpu_vm_bo_rmv(adev, bo_va);
159 }
160 }
161 amdgpu_bo_unreserve(rbo);
162}
163
164static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r)
165{
166 if (r == -EDEADLK) {
167 r = amdgpu_gpu_reset(adev);
168 if (!r)
169 r = -EAGAIN;
170 }
171 return r;
172}
173
174/*
175 * GEM ioctls.
176 */
177int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
178 struct drm_file *filp)
179{
180 struct amdgpu_device *adev = dev->dev_private;
181 union drm_amdgpu_gem_create *args = data;
182 uint64_t size = args->in.bo_size;
183 struct drm_gem_object *gobj;
184 uint32_t handle;
185 bool kernel = false;
186 int r;
187
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188 /* create a gem object to contain this object in */
189 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
190 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
191 kernel = true;
192 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
193 size = size << AMDGPU_GDS_SHIFT;
194 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
195 size = size << AMDGPU_GWS_SHIFT;
196 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
197 size = size << AMDGPU_OA_SHIFT;
198 else {
199 r = -EINVAL;
200 goto error_unlock;
201 }
202 }
203 size = roundup(size, PAGE_SIZE);
204
205 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
206 (u32)(0xffffffff & args->in.domains),
207 args->in.domain_flags,
208 kernel, &gobj);
209 if (r)
210 goto error_unlock;
211
212 r = drm_gem_handle_create(filp, gobj, &handle);
213 /* drop reference from allocate - handle holds it now */
214 drm_gem_object_unreference_unlocked(gobj);
215 if (r)
216 goto error_unlock;
217
218 memset(args, 0, sizeof(*args));
219 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220 return 0;
221
222error_unlock:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400223 r = amdgpu_gem_handle_lockup(adev, r);
224 return r;
225}
226
227int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
228 struct drm_file *filp)
229{
230 struct amdgpu_device *adev = dev->dev_private;
231 struct drm_amdgpu_gem_userptr *args = data;
232 struct drm_gem_object *gobj;
233 struct amdgpu_bo *bo;
234 uint32_t handle;
235 int r;
236
237 if (offset_in_page(args->addr | args->size))
238 return -EINVAL;
239
240 /* reject unknown flag values */
241 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
242 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
243 AMDGPU_GEM_USERPTR_REGISTER))
244 return -EINVAL;
245
Christian König585116c2015-11-26 11:06:20 +0100246 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) && (
247 !(args->flags & AMDGPU_GEM_USERPTR_ANONONLY) ||
248 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER))) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400249
250 /* if we want to write to it we must require anonymous
251 memory and install a MMU notifier */
252 return -EACCES;
253 }
254
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255 /* 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);
Christian König1ea863f2015-12-18 22:13:12 +0100263 bo->prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
264 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
266 if (r)
267 goto release_object;
268
269 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
270 r = amdgpu_mn_register(bo, args->addr);
271 if (r)
272 goto release_object;
273 }
274
275 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
276 down_read(&current->mm->mmap_sem);
277 r = amdgpu_bo_reserve(bo, true);
278 if (r) {
279 up_read(&current->mm->mmap_sem);
280 goto release_object;
281 }
282
283 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
284 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
285 amdgpu_bo_unreserve(bo);
286 up_read(&current->mm->mmap_sem);
287 if (r)
288 goto release_object;
289 }
290
291 r = drm_gem_handle_create(filp, gobj, &handle);
292 /* drop reference from allocate - handle holds it now */
293 drm_gem_object_unreference_unlocked(gobj);
294 if (r)
295 goto handle_lockup;
296
297 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298 return 0;
299
300release_object:
301 drm_gem_object_unreference_unlocked(gobj);
302
303handle_lockup:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400304 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önigcc325d12016-02-08 11:08:35 +0100321 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200322 (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) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300430 if (args->data.data_size_bytes > sizeof(args->data.data)) {
431 r = -EINVAL;
432 goto unreserve;
433 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
435 if (!r)
436 r = amdgpu_bo_set_metadata(robj, args->data.data,
437 args->data.data_size_bytes,
438 args->data.flags);
439 }
440
Dan Carpenter0913eab2015-09-23 14:00:35 +0300441unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400442 amdgpu_bo_unreserve(robj);
443out:
444 drm_gem_object_unreference_unlocked(gobj);
445 return r;
446}
447
448/**
449 * amdgpu_gem_va_update_vm -update the bo_va in its VM
450 *
451 * @adev: amdgpu_device pointer
452 * @bo_va: bo_va to update
453 *
454 * Update the bo_va directly after setting it's address. Errors are not
455 * vital here, so they are not reported back to userspace.
456 */
457static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
monk.liu194a3362015-07-22 13:29:28 +0800458 struct amdgpu_bo_va *bo_va, uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459{
460 struct ttm_validate_buffer tv, *entry;
Christian König56467eb2015-12-11 15:16:32 +0100461 struct amdgpu_bo_list_entry vm_pd;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462 struct ww_acquire_ctx ticket;
Christian Königbf60efd2015-09-04 10:47:56 +0200463 struct list_head list, duplicates;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 unsigned domain;
465 int r;
466
467 INIT_LIST_HEAD(&list);
Christian Königbf60efd2015-09-04 10:47:56 +0200468 INIT_LIST_HEAD(&duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469
470 tv.bo = &bo_va->bo->tbo;
471 tv.shared = true;
472 list_add(&tv.head, &list);
473
Christian König56467eb2015-12-11 15:16:32 +0100474 amdgpu_vm_get_pd_bo(bo_va->vm, &list, &vm_pd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475
Christian Königbf60efd2015-09-04 10:47:56 +0200476 /* Provide duplicates to avoid -EALREADY */
477 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400478 if (r)
Christian König56467eb2015-12-11 15:16:32 +0100479 goto error_print;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480
Christian Königee1782c2015-12-11 21:01:23 +0100481 amdgpu_vm_get_pt_bos(bo_va->vm, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400482 list_for_each_entry(entry, &list, head) {
483 domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type);
484 /* if anything is swapped out don't swap it in here,
485 just abort and wait for the next CS */
486 if (domain == AMDGPU_GEM_DOMAIN_CPU)
487 goto error_unreserve;
488 }
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800489 list_for_each_entry(entry, &duplicates, head) {
490 domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type);
491 /* if anything is swapped out don't swap it in here,
492 just abort and wait for the next CS */
493 if (domain == AMDGPU_GEM_DOMAIN_CPU)
494 goto error_unreserve;
495 }
496
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800497 r = amdgpu_vm_update_page_directory(adev, bo_va->vm);
498 if (r)
499 goto error_unreserve;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501 r = amdgpu_vm_clear_freed(adev, bo_va->vm);
502 if (r)
Chunming Zhouf48b2652015-10-16 14:06:19 +0800503 goto error_unreserve;
monk.liu194a3362015-07-22 13:29:28 +0800504
505 if (operation == AMDGPU_VA_OP_MAP)
506 r = amdgpu_vm_bo_update(adev, bo_va, &bo_va->bo->tbo.mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508error_unreserve:
509 ttm_eu_backoff_reservation(&ticket, &list);
510
Christian König56467eb2015-12-11 15:16:32 +0100511error_print:
Christian König68fdd3d2015-06-16 14:50:02 +0200512 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
514}
515
516
517
518int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
519 struct drm_file *filp)
520{
Christian König34b5f6a2015-06-08 15:03:00 +0200521 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 struct drm_gem_object *gobj;
523 struct amdgpu_device *adev = dev->dev_private;
524 struct amdgpu_fpriv *fpriv = filp->driver_priv;
525 struct amdgpu_bo *rbo;
526 struct amdgpu_bo_va *bo_va;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800527 struct ttm_validate_buffer tv, tv_pd;
528 struct ww_acquire_ctx ticket;
529 struct list_head list, duplicates;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 uint32_t invalid_flags, va_flags = 0;
531 int r = 0;
532
Christian König34b5f6a2015-06-08 15:03:00 +0200533 if (!adev->vm_manager.enabled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 return -ENOTTY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535
Christian König34b5f6a2015-06-08 15:03:00 +0200536 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537 dev_err(&dev->pdev->dev,
538 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200539 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 return -EINVAL;
542 }
543
Christian Königfc220f62015-06-29 17:12:20 +0200544 invalid_flags = ~(AMDGPU_VM_DELAY_UPDATE | AMDGPU_VM_PAGE_READABLE |
545 AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_PAGE_EXECUTABLE);
Christian König34b5f6a2015-06-08 15:03:00 +0200546 if ((args->flags & invalid_flags)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200548 args->flags, invalid_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400549 return -EINVAL;
550 }
551
Christian König34b5f6a2015-06-08 15:03:00 +0200552 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 case AMDGPU_VA_OP_MAP:
554 case AMDGPU_VA_OP_UNMAP:
555 break;
556 default:
557 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200558 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559 return -EINVAL;
560 }
561
Christian König34b5f6a2015-06-08 15:03:00 +0200562 gobj = drm_gem_object_lookup(dev, filp, args->handle);
563 if (gobj == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400565 rbo = gem_to_amdgpu_bo(gobj);
Chunming Zhou49b02b12015-11-13 14:18:38 +0800566 INIT_LIST_HEAD(&list);
567 INIT_LIST_HEAD(&duplicates);
568 tv.bo = &rbo->tbo;
569 tv.shared = true;
570 list_add(&tv.head, &list);
571
572 if (args->operation == AMDGPU_VA_OP_MAP) {
573 tv_pd.bo = &fpriv->vm.page_directory->tbo;
574 tv_pd.shared = true;
575 list_add(&tv_pd.head, &list);
576 }
577 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 drm_gem_object_unreference_unlocked(gobj);
580 return r;
581 }
Christian König34b5f6a2015-06-08 15:03:00 +0200582
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 bo_va = amdgpu_vm_bo_find(&fpriv->vm, rbo);
584 if (!bo_va) {
Chunming Zhou49b02b12015-11-13 14:18:38 +0800585 ttm_eu_backoff_reservation(&ticket, &list);
586 drm_gem_object_unreference_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 return -ENOENT;
588 }
589
Christian König34b5f6a2015-06-08 15:03:00 +0200590 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 case AMDGPU_VA_OP_MAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200592 if (args->flags & AMDGPU_VM_PAGE_READABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 va_flags |= AMDGPU_PTE_READABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200594 if (args->flags & AMDGPU_VM_PAGE_WRITEABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 va_flags |= AMDGPU_PTE_WRITEABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200596 if (args->flags & AMDGPU_VM_PAGE_EXECUTABLE)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597 va_flags |= AMDGPU_PTE_EXECUTABLE;
Christian König34b5f6a2015-06-08 15:03:00 +0200598 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
599 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200600 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601 break;
602 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200603 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 break;
605 default:
606 break;
607 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800608 ttm_eu_backoff_reservation(&ticket, &list);
Christian Königfc220f62015-06-29 17:12:20 +0200609 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE))
monk.liu194a3362015-07-22 13:29:28 +0800610 amdgpu_gem_va_update_vm(adev, bo_va, args->operation);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800611
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400612 drm_gem_object_unreference_unlocked(gobj);
613 return r;
614}
615
616int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
617 struct drm_file *filp)
618{
619 struct drm_amdgpu_gem_op *args = data;
620 struct drm_gem_object *gobj;
621 struct amdgpu_bo *robj;
622 int r;
623
624 gobj = drm_gem_object_lookup(dev, filp, args->handle);
625 if (gobj == NULL) {
626 return -ENOENT;
627 }
628 robj = gem_to_amdgpu_bo(gobj);
629
630 r = amdgpu_bo_reserve(robj, false);
631 if (unlikely(r))
632 goto out;
633
634 switch (args->op) {
635 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
636 struct drm_amdgpu_gem_create_in info;
637 void __user *out = (void __user *)(long)args->value;
638
639 info.bo_size = robj->gem_base.size;
640 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Christian König1ea863f2015-12-18 22:13:12 +0100641 info.domains = robj->prefered_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200643 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 if (copy_to_user(out, &info, sizeof(info)))
645 r = -EFAULT;
646 break;
647 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200648 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christian Königcc325d12016-02-08 11:08:35 +0100649 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200651 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 break;
653 }
Christian König1ea863f2015-12-18 22:13:12 +0100654 robj->prefered_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
655 AMDGPU_GEM_DOMAIN_GTT |
656 AMDGPU_GEM_DOMAIN_CPU);
657 robj->allowed_domains = robj->prefered_domains;
658 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
659 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
660
Christian König4c28fb02015-08-28 17:27:54 +0200661 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662 break;
663 default:
Christian König4c28fb02015-08-28 17:27:54 +0200664 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665 r = -EINVAL;
666 }
667
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668out:
669 drm_gem_object_unreference_unlocked(gobj);
670 return r;
671}
672
673int amdgpu_mode_dumb_create(struct drm_file *file_priv,
674 struct drm_device *dev,
675 struct drm_mode_create_dumb *args)
676{
677 struct amdgpu_device *adev = dev->dev_private;
678 struct drm_gem_object *gobj;
679 uint32_t handle;
680 int r;
681
682 args->pitch = amdgpu_align_pitch(adev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300683 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 args->size = ALIGN(args->size, PAGE_SIZE);
685
686 r = amdgpu_gem_object_create(adev, args->size, 0,
687 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400688 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
689 ttm_bo_type_device,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 &gobj);
691 if (r)
692 return -ENOMEM;
693
694 r = drm_gem_handle_create(file_priv, gobj, &handle);
695 /* drop reference from allocate - handle holds it now */
696 drm_gem_object_unreference_unlocked(gobj);
697 if (r) {
698 return r;
699 }
700 args->handle = handle;
701 return 0;
702}
703
704#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100705static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
706{
707 struct drm_gem_object *gobj = ptr;
708 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
709 struct seq_file *m = data;
710
711 unsigned domain;
712 const char *placement;
713 unsigned pin_count;
714
715 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
716 switch (domain) {
717 case AMDGPU_GEM_DOMAIN_VRAM:
718 placement = "VRAM";
719 break;
720 case AMDGPU_GEM_DOMAIN_GTT:
721 placement = " GTT";
722 break;
723 case AMDGPU_GEM_DOMAIN_CPU:
724 default:
725 placement = " CPU";
726 break;
727 }
728 seq_printf(m, "\t0x%08x: %12ld byte %s @ 0x%010Lx",
729 id, amdgpu_bo_size(bo), placement,
730 amdgpu_bo_gpu_offset(bo));
731
732 pin_count = ACCESS_ONCE(bo->pin_count);
733 if (pin_count)
734 seq_printf(m, " pin count %d", pin_count);
735 seq_printf(m, "\n");
736
737 return 0;
738}
739
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400740static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
741{
742 struct drm_info_node *node = (struct drm_info_node *)m->private;
743 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100744 struct drm_file *file;
745 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746
Christian König7ea23562016-02-15 15:23:00 +0100747 r = mutex_lock_interruptible(&dev->struct_mutex);
748 if (r)
749 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400750
Christian König7ea23562016-02-15 15:23:00 +0100751 list_for_each_entry(file, &dev->filelist, lhead) {
752 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100753
Christian König7ea23562016-02-15 15:23:00 +0100754 /*
755 * Although we have a valid reference on file->pid, that does
756 * not guarantee that the task_struct who called get_pid() is
757 * still alive (e.g. get_pid(current) => fork() => exit()).
758 * Therefore, we need to protect this ->comm access using RCU.
759 */
760 rcu_read_lock();
761 task = pid_task(file->pid, PIDTYPE_PID);
762 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
763 task ? task->comm : "<unknown>");
764 rcu_read_unlock();
765
766 spin_lock(&file->table_lock);
767 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
768 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769 }
Christian König7ea23562016-02-15 15:23:00 +0100770
771 mutex_unlock(&dev->struct_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400772 return 0;
773}
774
775static struct drm_info_list amdgpu_debugfs_gem_list[] = {
776 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
777};
778#endif
779
780int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
781{
782#if defined(CONFIG_DEBUG_FS)
783 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
784#endif
785 return 0;
786}