blob: 96683f5b2b1b722db08de97550aa8cdf99444a1c [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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 */
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/radeon_drm.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
31
Jerome Glisse771fe6b2009-06-05 14:42:42 +020032void radeon_gem_object_free(struct drm_gem_object *gobj)
33{
Daniel Vetter7e4d15d2011-02-18 17:59:17 +010034 struct radeon_bo *robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020035
Jerome Glisse771fe6b2009-06-05 14:42:42 +020036 if (robj) {
Alex Deucher40f5cf92012-05-10 18:33:13 -040037 if (robj->gem_base.import_attach)
38 drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
Christian König12f13842015-07-14 15:58:30 +020039 radeon_mn_unregister(robj);
Jerome Glisse4c788672009-11-20 14:29:23 +010040 radeon_bo_unref(&robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020041 }
42}
43
Alex Deucher391bfec2014-07-17 12:26:29 -040044int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size,
Jerome Glisse4c788672009-11-20 14:29:23 +010045 int alignment, int initial_domain,
Christian Königed5cb432014-07-21 13:27:27 +020046 u32 flags, bool kernel,
Jerome Glisse4c788672009-11-20 14:29:23 +010047 struct drm_gem_object **obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020048{
Jerome Glisse4c788672009-11-20 14:29:23 +010049 struct radeon_bo *robj;
Christian König6c0d1122012-10-23 15:53:18 +020050 unsigned long max_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020051 int r;
52
53 *obj = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020054 /* At least align on page size */
55 if (alignment < PAGE_SIZE) {
56 alignment = PAGE_SIZE;
57 }
Christian König6c0d1122012-10-23 15:53:18 +020058
Alex Deucher391bfec2014-07-17 12:26:29 -040059 /* Maximum bo size is the unpinned gtt size since we use the gtt to
60 * handle vram to system pool migrations.
61 */
62 max_size = rdev->mc.gtt_size - rdev->gart_pin_size;
Christian König6c0d1122012-10-23 15:53:18 +020063 if (size > max_size) {
Alex Deucher391bfec2014-07-17 12:26:29 -040064 DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
Michel Dänzer380670a2014-07-16 18:40:32 +090065 size >> 20, max_size >> 20);
Christian König6c0d1122012-10-23 15:53:18 +020066 return -ENOMEM;
67 }
68
Christian König0fe71582012-10-23 15:53:19 +020069retry:
Michel Dänzer02376d82014-07-17 19:01:08 +090070 r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain,
Maarten Lankhorst831b6962014-09-18 14:11:56 +020071 flags, NULL, NULL, &robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020072 if (r) {
Christian König0fe71582012-10-23 15:53:19 +020073 if (r != -ERESTARTSYS) {
74 if (initial_domain == RADEON_GEM_DOMAIN_VRAM) {
75 initial_domain |= RADEON_GEM_DOMAIN_GTT;
76 goto retry;
77 }
Alex Deucher391bfec2014-07-17 12:26:29 -040078 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
Dave Airlieecabd322009-12-15 10:39:48 +100079 size, initial_domain, alignment, r);
Christian König0fe71582012-10-23 15:53:19 +020080 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020081 return r;
82 }
Daniel Vetter441921d2011-02-18 17:59:16 +010083 *obj = &robj->gem_base;
Jerome Glisse409851f2013-04-25 22:29:27 -040084 robj->pid = task_pid_nr(current);
Daniel Vetter441921d2011-02-18 17:59:16 +010085
86 mutex_lock(&rdev->gem.mutex);
87 list_add_tail(&robj->list, &rdev->gem.objects);
88 mutex_unlock(&rdev->gem.mutex);
89
Jerome Glisse771fe6b2009-06-05 14:42:42 +020090 return 0;
91}
92
Rashika Kheria248a6c42014-01-06 20:58:45 +053093static int radeon_gem_set_domain(struct drm_gem_object *gobj,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020094 uint32_t rdomain, uint32_t wdomain)
95{
Jerome Glisse4c788672009-11-20 14:29:23 +010096 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020097 uint32_t domain;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +020098 long r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020099
100 /* FIXME: reeimplement */
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100101 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200102 /* work out where to validate the buffer to */
103 domain = wdomain;
104 if (!domain) {
105 domain = rdomain;
106 }
107 if (!domain) {
108 /* Do nothings */
Masanari Iidab6cafa22012-02-27 23:28:38 +0900109 printk(KERN_WARNING "Set domain without domain !\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200110 return 0;
111 }
112 if (domain == RADEON_GEM_DOMAIN_CPU) {
113 /* Asking for cpu access wait for object idle */
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200114 r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
115 if (!r)
116 r = -EBUSY;
117
118 if (r < 0 && r != -EINTR) {
119 printk(KERN_ERR "Failed to wait for object: %li\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200120 return r;
121 }
122 }
123 return 0;
124}
125
126int radeon_gem_init(struct radeon_device *rdev)
127{
128 INIT_LIST_HEAD(&rdev->gem.objects);
129 return 0;
130}
131
132void radeon_gem_fini(struct radeon_device *rdev)
133{
Jerome Glisse4c788672009-11-20 14:29:23 +0100134 radeon_bo_force_delete(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200135}
136
Jerome Glisse721604a2012-01-05 22:11:05 -0500137/*
138 * Call from drm_gem_handle_create which appear in both new and open ioctl
139 * case.
140 */
141int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
142{
Christian Könige971bd52012-09-11 16:10:04 +0200143 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
144 struct radeon_device *rdev = rbo->rdev;
145 struct radeon_fpriv *fpriv = file_priv->driver_priv;
146 struct radeon_vm *vm = &fpriv->vm;
147 struct radeon_bo_va *bo_va;
148 int r;
149
Alex Deucher544143f2015-01-28 14:36:26 -0500150 if ((rdev->family < CHIP_CAYMAN) ||
151 (!rdev->accel_working)) {
Christian Könige971bd52012-09-11 16:10:04 +0200152 return 0;
153 }
154
155 r = radeon_bo_reserve(rbo, false);
156 if (r) {
157 return r;
158 }
159
160 bo_va = radeon_vm_bo_find(vm, rbo);
161 if (!bo_va) {
162 bo_va = radeon_vm_bo_add(rdev, vm, rbo);
163 } else {
164 ++bo_va->ref_count;
165 }
166 radeon_bo_unreserve(rbo);
167
Jerome Glisse721604a2012-01-05 22:11:05 -0500168 return 0;
169}
170
171void radeon_gem_object_close(struct drm_gem_object *obj,
172 struct drm_file *file_priv)
173{
174 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
175 struct radeon_device *rdev = rbo->rdev;
176 struct radeon_fpriv *fpriv = file_priv->driver_priv;
177 struct radeon_vm *vm = &fpriv->vm;
Christian Könige971bd52012-09-11 16:10:04 +0200178 struct radeon_bo_va *bo_va;
Christian Königd59f7022012-09-11 16:10:02 +0200179 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500180
Alex Deucher544143f2015-01-28 14:36:26 -0500181 if ((rdev->family < CHIP_CAYMAN) ||
182 (!rdev->accel_working)) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500183 return;
184 }
185
Christian Königd59f7022012-09-11 16:10:02 +0200186 r = radeon_bo_reserve(rbo, true);
187 if (r) {
188 dev_err(rdev->dev, "leaking bo va because "
189 "we fail to reserve bo (%d)\n", r);
Jerome Glisse721604a2012-01-05 22:11:05 -0500190 return;
191 }
Christian Könige971bd52012-09-11 16:10:04 +0200192 bo_va = radeon_vm_bo_find(vm, rbo);
193 if (bo_va) {
194 if (--bo_va->ref_count == 0) {
195 radeon_vm_bo_rmv(rdev, bo_va);
196 }
197 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500198 radeon_bo_unreserve(rbo);
199}
200
Christian König6c6f4782012-05-02 15:11:19 +0200201static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
202{
203 if (r == -EDEADLK) {
Christian König6c6f4782012-05-02 15:11:19 +0200204 r = radeon_gpu_reset(rdev);
205 if (!r)
206 r = -EAGAIN;
Christian König6c6f4782012-05-02 15:11:19 +0200207 }
208 return r;
209}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200210
211/*
212 * GEM ioctls.
213 */
214int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
215 struct drm_file *filp)
216{
217 struct radeon_device *rdev = dev->dev_private;
218 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000219 struct ttm_mem_type_manager *man;
220
221 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222
Michel Dänzer51964e92017-01-30 12:06:35 +0900223 args->vram_size = (u64)man->size << PAGE_SHIFT;
224 args->vram_visible = rdev->mc.visible_vram_size;
Alex Deucherccbe0062014-07-17 12:16:20 -0400225 args->vram_visible -= rdev->vram_pin_size;
226 args->gart_size = rdev->mc.gtt_size;
227 args->gart_size -= rdev->gart_pin_size;
228
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200229 return 0;
230}
231
232int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
233 struct drm_file *filp)
234{
235 /* TODO: implement */
236 DRM_ERROR("unimplemented %s\n", __func__);
237 return -ENOSYS;
238}
239
240int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
241 struct drm_file *filp)
242{
243 /* TODO: implement */
244 DRM_ERROR("unimplemented %s\n", __func__);
245 return -ENOSYS;
246}
247
248int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
249 struct drm_file *filp)
250{
251 struct radeon_device *rdev = dev->dev_private;
252 struct drm_radeon_gem_create *args = data;
253 struct drm_gem_object *gobj;
254 uint32_t handle;
255 int r;
256
Jerome Glissedee53e72012-07-02 12:45:19 -0400257 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200258 /* create a gem object to contain this object in */
259 args->size = roundup(args->size, PAGE_SIZE);
260 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Michel Dänzer02376d82014-07-17 19:01:08 +0900261 args->initial_domain, args->flags,
Christian Königed5cb432014-07-21 13:27:27 +0200262 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200263 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400264 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200265 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200266 return r;
267 }
268 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000269 /* drop reference from allocate - handle holds it now */
270 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200271 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400272 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200273 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200274 return r;
275 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200276 args->handle = handle;
Jerome Glissedee53e72012-07-02 12:45:19 -0400277 up_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278 return 0;
279}
280
Christian Königf72a113a2014-08-07 09:36:00 +0200281int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
282 struct drm_file *filp)
283{
284 struct radeon_device *rdev = dev->dev_private;
285 struct drm_radeon_gem_userptr *args = data;
286 struct drm_gem_object *gobj;
287 struct radeon_bo *bo;
288 uint32_t handle;
289 int r;
290
291 if (offset_in_page(args->addr | args->size))
292 return -EINVAL;
293
Christian Königf72a113a2014-08-07 09:36:00 +0200294 /* reject unknown flag values */
Christian Königddd00e32014-08-07 09:36:01 +0200295 if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
Christian König341cb9e2014-08-07 09:36:03 +0200296 RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
297 RADEON_GEM_USERPTR_REGISTER))
Christian Königf72a113a2014-08-07 09:36:00 +0200298 return -EINVAL;
299
Christian Königbd645e42014-08-07 09:36:04 +0200300 if (args->flags & RADEON_GEM_USERPTR_READONLY) {
301 /* readonly pages not tested on older hardware */
302 if (rdev->family < CHIP_R600)
303 return -EINVAL;
304
305 } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
306 !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {
307
308 /* if we want to write to it we must require anonymous
309 memory and install a MMU notifier */
310 return -EACCES;
311 }
Christian Königf72a113a2014-08-07 09:36:00 +0200312
313 down_read(&rdev->exclusive_lock);
314
315 /* create a gem object to contain this object in */
316 r = radeon_gem_object_create(rdev, args->size, 0,
317 RADEON_GEM_DOMAIN_CPU, 0,
318 false, &gobj);
319 if (r)
320 goto handle_lockup;
321
322 bo = gem_to_radeon_bo(gobj);
323 r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
324 if (r)
325 goto release_object;
326
Christian König341cb9e2014-08-07 09:36:03 +0200327 if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
328 r = radeon_mn_register(bo, args->addr);
329 if (r)
330 goto release_object;
331 }
332
Christian König2a84a442014-08-07 09:36:02 +0200333 if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
334 down_read(&current->mm->mmap_sem);
335 r = radeon_bo_reserve(bo, true);
336 if (r) {
337 up_read(&current->mm->mmap_sem);
338 goto release_object;
339 }
340
341 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
342 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
343 radeon_bo_unreserve(bo);
344 up_read(&current->mm->mmap_sem);
345 if (r)
346 goto release_object;
347 }
348
Christian Königf72a113a2014-08-07 09:36:00 +0200349 r = drm_gem_handle_create(filp, gobj, &handle);
350 /* drop reference from allocate - handle holds it now */
351 drm_gem_object_unreference_unlocked(gobj);
352 if (r)
353 goto handle_lockup;
354
355 args->handle = handle;
356 up_read(&rdev->exclusive_lock);
357 return 0;
358
359release_object:
360 drm_gem_object_unreference_unlocked(gobj);
361
362handle_lockup:
363 up_read(&rdev->exclusive_lock);
364 r = radeon_gem_handle_lockup(rdev, r);
365
366 return r;
367}
368
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200369int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
370 struct drm_file *filp)
371{
372 /* transition the BO to a domain -
373 * just validate the BO into a certain domain */
Jerome Glissedee53e72012-07-02 12:45:19 -0400374 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200375 struct drm_radeon_gem_set_domain *args = data;
376 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100377 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200378 int r;
379
380 /* for now if someone requests domain CPU -
381 * just make sure the buffer is finished with */
Jerome Glissedee53e72012-07-02 12:45:19 -0400382 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200383
384 /* just do a BO wait for now */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100385 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386 if (gobj == NULL) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400387 up_read(&rdev->exclusive_lock);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100388 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200389 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100390 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200391
392 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
393
Luca Barbieribc9025b2010-02-09 05:49:12 +0000394 drm_gem_object_unreference_unlocked(gobj);
Jerome Glissedee53e72012-07-02 12:45:19 -0400395 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200396 r = radeon_gem_handle_lockup(robj->rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200397 return r;
398}
399
Dave Airlieda6b51d2014-12-24 13:11:17 +1000400int radeon_mode_dumb_mmap(struct drm_file *filp,
401 struct drm_device *dev,
402 uint32_t handle, uint64_t *offset_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200403{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200404 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100405 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200406
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100407 gobj = drm_gem_object_lookup(filp, handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200408 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100409 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200410 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100411 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200412 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
413 drm_gem_object_unreference_unlocked(gobj);
414 return -EPERM;
415 }
Dave Airlieff72145b2011-02-07 12:16:14 +1000416 *offset_p = radeon_bo_mmap_offset(robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000417 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100418 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200419}
420
Dave Airlieff72145b2011-02-07 12:16:14 +1000421int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
422 struct drm_file *filp)
423{
424 struct drm_radeon_gem_mmap *args = data;
425
Dave Airlieda6b51d2014-12-24 13:11:17 +1000426 return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
Dave Airlieff72145b2011-02-07 12:16:14 +1000427}
428
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200429int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
430 struct drm_file *filp)
431{
Dave Airliecefb87e2009-08-16 21:05:45 +1000432 struct drm_radeon_gem_busy *args = data;
433 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100434 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000435 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000436 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000437
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100438 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliecefb87e2009-08-16 21:05:45 +1000439 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100440 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000441 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100442 robj = gem_to_radeon_bo(gobj);
Grigori Goronzy828202a2015-07-03 01:54:10 +0200443
444 r = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
445 if (r == 0)
446 r = -EBUSY;
447 else
448 r = 0;
449
450 cur_placement = ACCESS_ONCE(robj->tbo.mem.mem_type);
Marek Olšák0bc490a2014-03-02 00:56:19 +0100451 args->domain = radeon_mem_type_to_domain(cur_placement);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000452 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee3b24152009-08-21 09:47:45 +1000453 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200454}
455
456int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
457 struct drm_file *filp)
458{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400459 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200460 struct drm_radeon_gem_wait_idle *args = data;
461 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100462 struct radeon_bo *robj;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200463 int r = 0;
Michel Dänzer404a6a52014-08-01 17:22:09 +0900464 uint32_t cur_placement = 0;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200465 long ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200466
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100467 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200468 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100469 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200470 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100471 robj = gem_to_radeon_bo(gobj);
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200472
473 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
474 if (ret == 0)
475 r = -EBUSY;
476 else if (ret < 0)
477 r = ret;
478
Michel Dänzer124764f2014-07-31 18:43:48 +0900479 /* Flush HDP cache via MMIO if necessary */
Grigori Goronzy54e03982015-07-03 01:54:11 +0200480 cur_placement = ACCESS_ONCE(robj->tbo.mem.mem_type);
Michel Dänzer404a6a52014-08-01 17:22:09 +0900481 if (rdev->asic->mmio_hdp_flush &&
482 radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
Michel Dänzer124764f2014-07-31 18:43:48 +0900483 robj->rdev->asic->mmio_hdp_flush(rdev);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000484 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400485 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200486 return r;
487}
Dave Airliee024e112009-06-24 09:48:08 +1000488
489int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
490 struct drm_file *filp)
491{
492 struct drm_radeon_gem_set_tiling *args = data;
493 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100494 struct radeon_bo *robj;
Dave Airliee024e112009-06-24 09:48:08 +1000495 int r = 0;
496
497 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100498 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000499 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100500 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100501 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100502 r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000503 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000504 return r;
505}
506
507int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
508 struct drm_file *filp)
509{
510 struct drm_radeon_gem_get_tiling *args = data;
511 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100512 struct radeon_bo *rbo;
Dave Airliee024e112009-06-24 09:48:08 +1000513 int r = 0;
514
515 DRM_DEBUG("\n");
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100516 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000517 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100518 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100519 rbo = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100520 r = radeon_bo_reserve(rbo, false);
521 if (unlikely(r != 0))
Dave Airlie51f07b72009-12-16 13:10:43 +1000522 goto out;
Jerome Glisse4c788672009-11-20 14:29:23 +0100523 radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
524 radeon_bo_unreserve(rbo);
Dave Airlie51f07b72009-12-16 13:10:43 +1000525out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000526 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000527 return r;
528}
Dave Airlieff72145b2011-02-07 12:16:14 +1000529
Christian König2f2624c2014-09-12 12:25:45 +0200530/**
531 * radeon_gem_va_update_vm -update the bo_va in its VM
532 *
533 * @rdev: radeon_device pointer
534 * @bo_va: bo_va to update
535 *
536 * Update the bo_va directly after setting it's address. Errors are not
537 * vital here, so they are not reported back to userspace.
538 */
539static void radeon_gem_va_update_vm(struct radeon_device *rdev,
540 struct radeon_bo_va *bo_va)
541{
542 struct ttm_validate_buffer tv, *entry;
Christian König1d0c0942014-11-27 14:48:42 +0100543 struct radeon_bo_list *vm_bos;
Christian König2f2624c2014-09-12 12:25:45 +0200544 struct ww_acquire_ctx ticket;
545 struct list_head list;
546 unsigned domain;
547 int r;
548
549 INIT_LIST_HEAD(&list);
550
551 tv.bo = &bo_va->bo->tbo;
552 tv.shared = true;
553 list_add(&tv.head, &list);
554
555 vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
556 if (!vm_bos)
557 return;
558
Christian Königaa350712014-12-03 15:46:48 +0100559 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
Christian König2f2624c2014-09-12 12:25:45 +0200560 if (r)
561 goto error_free;
562
563 list_for_each_entry(entry, &list, head) {
564 domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type);
565 /* if anything is swapped out don't swap it in here,
566 just abort and wait for the next CS */
567 if (domain == RADEON_GEM_DOMAIN_CPU)
568 goto error_unreserve;
569 }
570
571 mutex_lock(&bo_va->vm->mutex);
572 r = radeon_vm_clear_freed(rdev, bo_va->vm);
573 if (r)
574 goto error_unlock;
575
576 if (bo_va->it.start)
577 r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem);
578
579error_unlock:
580 mutex_unlock(&bo_va->vm->mutex);
581
582error_unreserve:
583 ttm_eu_backoff_reservation(&ticket, &list);
584
585error_free:
586 drm_free_large(vm_bos);
587
Christian Königad1a6222015-01-09 11:07:49 +0100588 if (r && r != -ERESTARTSYS)
Christian König2f2624c2014-09-12 12:25:45 +0200589 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
590}
591
Jerome Glisse721604a2012-01-05 22:11:05 -0500592int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
593 struct drm_file *filp)
594{
595 struct drm_radeon_gem_va *args = data;
596 struct drm_gem_object *gobj;
597 struct radeon_device *rdev = dev->dev_private;
598 struct radeon_fpriv *fpriv = filp->driver_priv;
599 struct radeon_bo *rbo;
600 struct radeon_bo_va *bo_va;
601 u32 invalid_flags;
602 int r = 0;
603
Alex Deucher67e915e2012-01-06 09:38:15 -0500604 if (!rdev->vm_manager.enabled) {
605 args->operation = RADEON_VA_RESULT_ERROR;
606 return -ENOTTY;
607 }
608
Jerome Glisse721604a2012-01-05 22:11:05 -0500609 /* !! DONT REMOVE !!
610 * We don't support vm_id yet, to be sure we don't have have broken
611 * userspace, reject anyone trying to use non 0 value thus moving
612 * forward we can use those fields without breaking existant userspace
613 */
614 if (args->vm_id) {
615 args->operation = RADEON_VA_RESULT_ERROR;
616 return -EINVAL;
617 }
618
619 if (args->offset < RADEON_VA_RESERVED_SIZE) {
620 dev_err(&dev->pdev->dev,
621 "offset 0x%lX is in reserved area 0x%X\n",
622 (unsigned long)args->offset,
623 RADEON_VA_RESERVED_SIZE);
624 args->operation = RADEON_VA_RESULT_ERROR;
625 return -EINVAL;
626 }
627
628 /* don't remove, we need to enforce userspace to set the snooped flag
629 * otherwise we will endup with broken userspace and we won't be able
630 * to enable this feature without adding new interface
631 */
632 invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
633 if ((args->flags & invalid_flags)) {
634 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
635 args->flags, invalid_flags);
636 args->operation = RADEON_VA_RESULT_ERROR;
637 return -EINVAL;
638 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500639
640 switch (args->operation) {
641 case RADEON_VA_MAP:
642 case RADEON_VA_UNMAP:
643 break;
644 default:
645 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
646 args->operation);
647 args->operation = RADEON_VA_RESULT_ERROR;
648 return -EINVAL;
649 }
650
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100651 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse721604a2012-01-05 22:11:05 -0500652 if (gobj == NULL) {
653 args->operation = RADEON_VA_RESULT_ERROR;
654 return -ENOENT;
655 }
656 rbo = gem_to_radeon_bo(gobj);
657 r = radeon_bo_reserve(rbo, false);
658 if (r) {
659 args->operation = RADEON_VA_RESULT_ERROR;
660 drm_gem_object_unreference_unlocked(gobj);
661 return r;
662 }
Christian Könige971bd52012-09-11 16:10:04 +0200663 bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
664 if (!bo_va) {
665 args->operation = RADEON_VA_RESULT_ERROR;
Matthew Dawson186bac82016-01-25 10:34:12 -0500666 radeon_bo_unreserve(rbo);
Christian Könige971bd52012-09-11 16:10:04 +0200667 drm_gem_object_unreference_unlocked(gobj);
668 return -ENOENT;
669 }
670
Jerome Glisse721604a2012-01-05 22:11:05 -0500671 switch (args->operation) {
672 case RADEON_VA_MAP:
Alex Deucher0aea5e42014-07-30 11:49:56 -0400673 if (bo_va->it.start) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500674 args->operation = RADEON_VA_RESULT_VA_EXIST;
Alex Deucher0aea5e42014-07-30 11:49:56 -0400675 args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
Christian König85761f62014-11-19 14:01:20 +0100676 radeon_bo_unreserve(rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500677 goto out;
678 }
Christian Könige971bd52012-09-11 16:10:04 +0200679 r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
Jerome Glisse721604a2012-01-05 22:11:05 -0500680 break;
681 case RADEON_VA_UNMAP:
Christian Könige971bd52012-09-11 16:10:04 +0200682 r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
Jerome Glisse721604a2012-01-05 22:11:05 -0500683 break;
684 default:
685 break;
686 }
Christian König2f2624c2014-09-12 12:25:45 +0200687 if (!r)
688 radeon_gem_va_update_vm(rdev, bo_va);
Jerome Glisse721604a2012-01-05 22:11:05 -0500689 args->operation = RADEON_VA_RESULT_OK;
690 if (r) {
691 args->operation = RADEON_VA_RESULT_ERROR;
692 }
693out:
Jerome Glisse721604a2012-01-05 22:11:05 -0500694 drm_gem_object_unreference_unlocked(gobj);
695 return r;
696}
697
Marek Olšákbda72d52014-03-02 00:56:17 +0100698int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
699 struct drm_file *filp)
700{
701 struct drm_radeon_gem_op *args = data;
702 struct drm_gem_object *gobj;
703 struct radeon_bo *robj;
704 int r;
705
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100706 gobj = drm_gem_object_lookup(filp, args->handle);
Marek Olšákbda72d52014-03-02 00:56:17 +0100707 if (gobj == NULL) {
708 return -ENOENT;
709 }
710 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200711
712 r = -EPERM;
713 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
714 goto out;
715
Marek Olšákbda72d52014-03-02 00:56:17 +0100716 r = radeon_bo_reserve(robj, false);
717 if (unlikely(r))
718 goto out;
719
720 switch (args->op) {
721 case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
722 args->value = robj->initial_domain;
723 break;
724 case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
725 robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
726 RADEON_GEM_DOMAIN_GTT |
727 RADEON_GEM_DOMAIN_CPU);
728 break;
729 default:
730 r = -EINVAL;
731 }
732
733 radeon_bo_unreserve(robj);
734out:
735 drm_gem_object_unreference_unlocked(gobj);
736 return r;
737}
738
Dave Airlieff72145b2011-02-07 12:16:14 +1000739int radeon_mode_dumb_create(struct drm_file *file_priv,
740 struct drm_device *dev,
741 struct drm_mode_create_dumb *args)
742{
743 struct radeon_device *rdev = dev->dev_private;
744 struct drm_gem_object *gobj;
Dave Airliec87a8d82011-03-17 13:58:34 +1000745 uint32_t handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000746 int r;
747
Laurent Pinchart802aaf72016-10-18 01:41:18 +0300748 args->pitch = radeon_align_pitch(rdev, args->width,
749 DIV_ROUND_UP(args->bpp, 8), 0);
Dave Airlieff72145b2011-02-07 12:16:14 +1000750 args->size = args->pitch * args->height;
751 args->size = ALIGN(args->size, PAGE_SIZE);
752
753 r = radeon_gem_object_create(rdev, args->size, 0,
Michel Dänzer02376d82014-07-17 19:01:08 +0900754 RADEON_GEM_DOMAIN_VRAM, 0,
Christian Königed5cb432014-07-21 13:27:27 +0200755 false, &gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000756 if (r)
757 return -ENOMEM;
758
Dave Airliec87a8d82011-03-17 13:58:34 +1000759 r = drm_gem_handle_create(file_priv, gobj, &handle);
760 /* drop reference from allocate - handle holds it now */
761 drm_gem_object_unreference_unlocked(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000762 if (r) {
Dave Airlieff72145b2011-02-07 12:16:14 +1000763 return r;
764 }
Dave Airliec87a8d82011-03-17 13:58:34 +1000765 args->handle = handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000766 return 0;
767}
768
Jerome Glisse409851f2013-04-25 22:29:27 -0400769#if defined(CONFIG_DEBUG_FS)
770static int radeon_debugfs_gem_info(struct seq_file *m, void *data)
771{
772 struct drm_info_node *node = (struct drm_info_node *)m->private;
773 struct drm_device *dev = node->minor->dev;
774 struct radeon_device *rdev = dev->dev_private;
775 struct radeon_bo *rbo;
776 unsigned i = 0;
777
778 mutex_lock(&rdev->gem.mutex);
779 list_for_each_entry(rbo, &rdev->gem.objects, list) {
780 unsigned domain;
781 const char *placement;
782
783 domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
784 switch (domain) {
785 case RADEON_GEM_DOMAIN_VRAM:
786 placement = "VRAM";
787 break;
788 case RADEON_GEM_DOMAIN_GTT:
789 placement = " GTT";
790 break;
791 case RADEON_GEM_DOMAIN_CPU:
792 default:
793 placement = " CPU";
794 break;
795 }
796 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
797 i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
798 placement, (unsigned long)rbo->pid);
799 i++;
800 }
801 mutex_unlock(&rdev->gem.mutex);
802 return 0;
803}
804
805static struct drm_info_list radeon_debugfs_gem_list[] = {
806 {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
807};
808#endif
809
810int radeon_gem_debugfs_init(struct radeon_device *rdev)
811{
812#if defined(CONFIG_DEBUG_FS)
813 return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
814#endif
815 return 0;
816}