blob: fe48f229043e33720c26bcdd40949d7a8fd41b00 [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);
Jerome Glisse4c788672009-11-20 14:29:23 +010039 radeon_bo_unref(&robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020040 }
41}
42
Alex Deucher391bfec2014-07-17 12:26:29 -040043int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size,
Jerome Glisse4c788672009-11-20 14:29:23 +010044 int alignment, int initial_domain,
Christian Königed5cb432014-07-21 13:27:27 +020045 u32 flags, bool kernel,
Jerome Glisse4c788672009-11-20 14:29:23 +010046 struct drm_gem_object **obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020047{
Jerome Glisse4c788672009-11-20 14:29:23 +010048 struct radeon_bo *robj;
Christian König6c0d1122012-10-23 15:53:18 +020049 unsigned long max_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020050 int r;
51
52 *obj = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020053 /* At least align on page size */
54 if (alignment < PAGE_SIZE) {
55 alignment = PAGE_SIZE;
56 }
Christian König6c0d1122012-10-23 15:53:18 +020057
Alex Deucher391bfec2014-07-17 12:26:29 -040058 /* Maximum bo size is the unpinned gtt size since we use the gtt to
59 * handle vram to system pool migrations.
60 */
61 max_size = rdev->mc.gtt_size - rdev->gart_pin_size;
Christian König6c0d1122012-10-23 15:53:18 +020062 if (size > max_size) {
Alex Deucher391bfec2014-07-17 12:26:29 -040063 DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
Michel Dänzer380670a2014-07-16 18:40:32 +090064 size >> 20, max_size >> 20);
Christian König6c0d1122012-10-23 15:53:18 +020065 return -ENOMEM;
66 }
67
Christian König0fe71582012-10-23 15:53:19 +020068retry:
Michel Dänzer02376d82014-07-17 19:01:08 +090069 r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain,
Maarten Lankhorst831b6962014-09-18 14:11:56 +020070 flags, NULL, NULL, &robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020071 if (r) {
Christian König0fe71582012-10-23 15:53:19 +020072 if (r != -ERESTARTSYS) {
73 if (initial_domain == RADEON_GEM_DOMAIN_VRAM) {
74 initial_domain |= RADEON_GEM_DOMAIN_GTT;
75 goto retry;
76 }
Alex Deucher391bfec2014-07-17 12:26:29 -040077 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
Dave Airlieecabd322009-12-15 10:39:48 +100078 size, initial_domain, alignment, r);
Christian König0fe71582012-10-23 15:53:19 +020079 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020080 return r;
81 }
Daniel Vetter441921d2011-02-18 17:59:16 +010082 *obj = &robj->gem_base;
Jerome Glisse409851f2013-04-25 22:29:27 -040083 robj->pid = task_pid_nr(current);
Daniel Vetter441921d2011-02-18 17:59:16 +010084
85 mutex_lock(&rdev->gem.mutex);
86 list_add_tail(&robj->list, &rdev->gem.objects);
87 mutex_unlock(&rdev->gem.mutex);
88
Jerome Glisse771fe6b2009-06-05 14:42:42 +020089 return 0;
90}
91
Rashika Kheria248a6c42014-01-06 20:58:45 +053092static int radeon_gem_set_domain(struct drm_gem_object *gobj,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020093 uint32_t rdomain, uint32_t wdomain)
94{
Jerome Glisse4c788672009-11-20 14:29:23 +010095 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020096 uint32_t domain;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +020097 long r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020098
99 /* FIXME: reeimplement */
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100100 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200101 /* work out where to validate the buffer to */
102 domain = wdomain;
103 if (!domain) {
104 domain = rdomain;
105 }
106 if (!domain) {
107 /* Do nothings */
Masanari Iidab6cafa22012-02-27 23:28:38 +0900108 printk(KERN_WARNING "Set domain without domain !\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200109 return 0;
110 }
111 if (domain == RADEON_GEM_DOMAIN_CPU) {
112 /* Asking for cpu access wait for object idle */
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200113 r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
114 if (!r)
115 r = -EBUSY;
116
117 if (r < 0 && r != -EINTR) {
118 printk(KERN_ERR "Failed to wait for object: %li\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119 return r;
120 }
121 }
122 return 0;
123}
124
125int radeon_gem_init(struct radeon_device *rdev)
126{
127 INIT_LIST_HEAD(&rdev->gem.objects);
128 return 0;
129}
130
131void radeon_gem_fini(struct radeon_device *rdev)
132{
Jerome Glisse4c788672009-11-20 14:29:23 +0100133 radeon_bo_force_delete(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200134}
135
Jerome Glisse721604a2012-01-05 22:11:05 -0500136/*
137 * Call from drm_gem_handle_create which appear in both new and open ioctl
138 * case.
139 */
140int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
141{
Christian Könige971bd52012-09-11 16:10:04 +0200142 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
143 struct radeon_device *rdev = rbo->rdev;
144 struct radeon_fpriv *fpriv = file_priv->driver_priv;
145 struct radeon_vm *vm = &fpriv->vm;
146 struct radeon_bo_va *bo_va;
147 int r;
148
149 if (rdev->family < CHIP_CAYMAN) {
150 return 0;
151 }
152
153 r = radeon_bo_reserve(rbo, false);
154 if (r) {
155 return r;
156 }
157
158 bo_va = radeon_vm_bo_find(vm, rbo);
159 if (!bo_va) {
160 bo_va = radeon_vm_bo_add(rdev, vm, rbo);
161 } else {
162 ++bo_va->ref_count;
163 }
164 radeon_bo_unreserve(rbo);
165
Jerome Glisse721604a2012-01-05 22:11:05 -0500166 return 0;
167}
168
169void radeon_gem_object_close(struct drm_gem_object *obj,
170 struct drm_file *file_priv)
171{
172 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
173 struct radeon_device *rdev = rbo->rdev;
174 struct radeon_fpriv *fpriv = file_priv->driver_priv;
175 struct radeon_vm *vm = &fpriv->vm;
Christian Könige971bd52012-09-11 16:10:04 +0200176 struct radeon_bo_va *bo_va;
Christian Königd59f7022012-09-11 16:10:02 +0200177 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500178
179 if (rdev->family < CHIP_CAYMAN) {
180 return;
181 }
182
Christian Königd59f7022012-09-11 16:10:02 +0200183 r = radeon_bo_reserve(rbo, true);
184 if (r) {
185 dev_err(rdev->dev, "leaking bo va because "
186 "we fail to reserve bo (%d)\n", r);
Jerome Glisse721604a2012-01-05 22:11:05 -0500187 return;
188 }
Christian Könige971bd52012-09-11 16:10:04 +0200189 bo_va = radeon_vm_bo_find(vm, rbo);
190 if (bo_va) {
191 if (--bo_va->ref_count == 0) {
192 radeon_vm_bo_rmv(rdev, bo_va);
193 }
194 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500195 radeon_bo_unreserve(rbo);
196}
197
Christian König6c6f4782012-05-02 15:11:19 +0200198static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
199{
200 if (r == -EDEADLK) {
Christian König6c6f4782012-05-02 15:11:19 +0200201 r = radeon_gpu_reset(rdev);
202 if (!r)
203 r = -EAGAIN;
Christian König6c6f4782012-05-02 15:11:19 +0200204 }
205 return r;
206}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200207
208/*
209 * GEM ioctls.
210 */
211int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
212 struct drm_file *filp)
213{
214 struct radeon_device *rdev = dev->dev_private;
215 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000216 struct ttm_mem_type_manager *man;
217
218 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200219
Dave Airlie7a50f012009-07-21 20:39:30 +1000220 args->vram_size = rdev->mc.real_vram_size;
Dave Airlie53595332011-03-14 09:47:24 +1000221 args->vram_visible = (u64)man->size << PAGE_SHIFT;
Alex Deucherccbe0062014-07-17 12:16:20 -0400222 args->vram_visible -= rdev->vram_pin_size;
223 args->gart_size = rdev->mc.gtt_size;
224 args->gart_size -= rdev->gart_pin_size;
225
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200226 return 0;
227}
228
229int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
230 struct drm_file *filp)
231{
232 /* TODO: implement */
233 DRM_ERROR("unimplemented %s\n", __func__);
234 return -ENOSYS;
235}
236
237int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
238 struct drm_file *filp)
239{
240 /* TODO: implement */
241 DRM_ERROR("unimplemented %s\n", __func__);
242 return -ENOSYS;
243}
244
245int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
246 struct drm_file *filp)
247{
248 struct radeon_device *rdev = dev->dev_private;
249 struct drm_radeon_gem_create *args = data;
250 struct drm_gem_object *gobj;
251 uint32_t handle;
252 int r;
253
Jerome Glissedee53e72012-07-02 12:45:19 -0400254 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200255 /* create a gem object to contain this object in */
256 args->size = roundup(args->size, PAGE_SIZE);
257 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Michel Dänzer02376d82014-07-17 19:01:08 +0900258 args->initial_domain, args->flags,
Christian Königed5cb432014-07-21 13:27:27 +0200259 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200260 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400261 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200262 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200263 return r;
264 }
265 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000266 /* drop reference from allocate - handle holds it now */
267 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200268 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400269 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200270 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200271 return r;
272 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273 args->handle = handle;
Jerome Glissedee53e72012-07-02 12:45:19 -0400274 up_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200275 return 0;
276}
277
Christian Königf72a113a2014-08-07 09:36:00 +0200278int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
279 struct drm_file *filp)
280{
281 struct radeon_device *rdev = dev->dev_private;
282 struct drm_radeon_gem_userptr *args = data;
283 struct drm_gem_object *gobj;
284 struct radeon_bo *bo;
285 uint32_t handle;
286 int r;
287
288 if (offset_in_page(args->addr | args->size))
289 return -EINVAL;
290
Christian Königf72a113a2014-08-07 09:36:00 +0200291 /* reject unknown flag values */
Christian Königddd00e32014-08-07 09:36:01 +0200292 if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
Christian König341cb9e2014-08-07 09:36:03 +0200293 RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
294 RADEON_GEM_USERPTR_REGISTER))
Christian Königf72a113a2014-08-07 09:36:00 +0200295 return -EINVAL;
296
Christian Königbd645e42014-08-07 09:36:04 +0200297 if (args->flags & RADEON_GEM_USERPTR_READONLY) {
298 /* readonly pages not tested on older hardware */
299 if (rdev->family < CHIP_R600)
300 return -EINVAL;
301
302 } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
303 !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {
304
305 /* if we want to write to it we must require anonymous
306 memory and install a MMU notifier */
307 return -EACCES;
308 }
Christian Königf72a113a2014-08-07 09:36:00 +0200309
310 down_read(&rdev->exclusive_lock);
311
312 /* create a gem object to contain this object in */
313 r = radeon_gem_object_create(rdev, args->size, 0,
314 RADEON_GEM_DOMAIN_CPU, 0,
315 false, &gobj);
316 if (r)
317 goto handle_lockup;
318
319 bo = gem_to_radeon_bo(gobj);
320 r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
321 if (r)
322 goto release_object;
323
Christian König341cb9e2014-08-07 09:36:03 +0200324 if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
325 r = radeon_mn_register(bo, args->addr);
326 if (r)
327 goto release_object;
328 }
329
Christian König2a84a442014-08-07 09:36:02 +0200330 if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
331 down_read(&current->mm->mmap_sem);
332 r = radeon_bo_reserve(bo, true);
333 if (r) {
334 up_read(&current->mm->mmap_sem);
335 goto release_object;
336 }
337
338 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
339 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
340 radeon_bo_unreserve(bo);
341 up_read(&current->mm->mmap_sem);
342 if (r)
343 goto release_object;
344 }
345
Christian Königf72a113a2014-08-07 09:36:00 +0200346 r = drm_gem_handle_create(filp, gobj, &handle);
347 /* drop reference from allocate - handle holds it now */
348 drm_gem_object_unreference_unlocked(gobj);
349 if (r)
350 goto handle_lockup;
351
352 args->handle = handle;
353 up_read(&rdev->exclusive_lock);
354 return 0;
355
356release_object:
357 drm_gem_object_unreference_unlocked(gobj);
358
359handle_lockup:
360 up_read(&rdev->exclusive_lock);
361 r = radeon_gem_handle_lockup(rdev, r);
362
363 return r;
364}
365
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200366int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
367 struct drm_file *filp)
368{
369 /* transition the BO to a domain -
370 * just validate the BO into a certain domain */
Jerome Glissedee53e72012-07-02 12:45:19 -0400371 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200372 struct drm_radeon_gem_set_domain *args = data;
373 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100374 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200375 int r;
376
377 /* for now if someone requests domain CPU -
378 * just make sure the buffer is finished with */
Jerome Glissedee53e72012-07-02 12:45:19 -0400379 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200380
381 /* just do a BO wait for now */
382 gobj = drm_gem_object_lookup(dev, filp, args->handle);
383 if (gobj == NULL) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400384 up_read(&rdev->exclusive_lock);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100385 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100387 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200388
389 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
390
Luca Barbieribc9025b2010-02-09 05:49:12 +0000391 drm_gem_object_unreference_unlocked(gobj);
Jerome Glissedee53e72012-07-02 12:45:19 -0400392 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200393 r = radeon_gem_handle_lockup(robj->rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200394 return r;
395}
396
Thomas Hellstrom355a7012014-11-20 09:56:25 +0100397static int radeon_mode_mmap(struct drm_file *filp,
398 struct drm_device *dev,
399 uint32_t handle, bool dumb,
400 uint64_t *offset_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200401{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200402 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100403 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200404
Dave Airlieff72145b2011-02-07 12:16:14 +1000405 gobj = drm_gem_object_lookup(dev, filp, handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200406 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100407 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200408 }
Thomas Hellstrom355a7012014-11-20 09:56:25 +0100409
410 /*
411 * We don't allow dumb mmaps on objects created using another
412 * interface.
413 */
414 WARN_ONCE(dumb && !(gobj->dumb || gobj->import_attach),
415 "Illegal dumb map of GPU buffer.\n");
416
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100417 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200418 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
419 drm_gem_object_unreference_unlocked(gobj);
420 return -EPERM;
421 }
Dave Airlieff72145b2011-02-07 12:16:14 +1000422 *offset_p = radeon_bo_mmap_offset(robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000423 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100424 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200425}
426
Thomas Hellstrom355a7012014-11-20 09:56:25 +0100427int radeon_mode_dumb_mmap(struct drm_file *filp,
428 struct drm_device *dev,
429 uint32_t handle, uint64_t *offset_p)
430{
431 return radeon_mode_mmap(filp, dev, handle, true, offset_p);
432}
433
Dave Airlieff72145b2011-02-07 12:16:14 +1000434int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
435 struct drm_file *filp)
436{
437 struct drm_radeon_gem_mmap *args = data;
438
Thomas Hellstrom355a7012014-11-20 09:56:25 +0100439 return radeon_mode_mmap(filp, dev, args->handle, false,
440 &args->addr_ptr);
Dave Airlieff72145b2011-02-07 12:16:14 +1000441}
442
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200443int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
444 struct drm_file *filp)
445{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400446 struct radeon_device *rdev = dev->dev_private;
Dave Airliecefb87e2009-08-16 21:05:45 +1000447 struct drm_radeon_gem_busy *args = data;
448 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100449 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000450 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000451 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000452
453 gobj = drm_gem_object_lookup(dev, filp, args->handle);
454 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100455 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000456 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100457 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100458 r = radeon_bo_wait(robj, &cur_placement, true);
Marek Olšák0bc490a2014-03-02 00:56:19 +0100459 args->domain = radeon_mem_type_to_domain(cur_placement);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000460 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400461 r = radeon_gem_handle_lockup(rdev, r);
Dave Airliee3b24152009-08-21 09:47:45 +1000462 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200463}
464
465int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
466 struct drm_file *filp)
467{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400468 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200469 struct drm_radeon_gem_wait_idle *args = data;
470 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100471 struct radeon_bo *robj;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200472 int r = 0;
Michel Dänzer404a6a52014-08-01 17:22:09 +0900473 uint32_t cur_placement = 0;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200474 long ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200475
476 gobj = drm_gem_object_lookup(dev, filp, args->handle);
477 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100478 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200479 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100480 robj = gem_to_radeon_bo(gobj);
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200481
482 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
483 if (ret == 0)
484 r = -EBUSY;
485 else if (ret < 0)
486 r = ret;
487
Michel Dänzer124764f2014-07-31 18:43:48 +0900488 /* Flush HDP cache via MMIO if necessary */
Michel Dänzer404a6a52014-08-01 17:22:09 +0900489 if (rdev->asic->mmio_hdp_flush &&
490 radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
Michel Dänzer124764f2014-07-31 18:43:48 +0900491 robj->rdev->asic->mmio_hdp_flush(rdev);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000492 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400493 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200494 return r;
495}
Dave Airliee024e112009-06-24 09:48:08 +1000496
497int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
498 struct drm_file *filp)
499{
500 struct drm_radeon_gem_set_tiling *args = data;
501 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100502 struct radeon_bo *robj;
Dave Airliee024e112009-06-24 09:48:08 +1000503 int r = 0;
504
505 DRM_DEBUG("%d \n", args->handle);
506 gobj = drm_gem_object_lookup(dev, filp, args->handle);
507 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100508 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100509 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100510 r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000511 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000512 return r;
513}
514
515int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
516 struct drm_file *filp)
517{
518 struct drm_radeon_gem_get_tiling *args = data;
519 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100520 struct radeon_bo *rbo;
Dave Airliee024e112009-06-24 09:48:08 +1000521 int r = 0;
522
523 DRM_DEBUG("\n");
524 gobj = drm_gem_object_lookup(dev, filp, args->handle);
525 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100526 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100527 rbo = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100528 r = radeon_bo_reserve(rbo, false);
529 if (unlikely(r != 0))
Dave Airlie51f07b72009-12-16 13:10:43 +1000530 goto out;
Jerome Glisse4c788672009-11-20 14:29:23 +0100531 radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
532 radeon_bo_unreserve(rbo);
Dave Airlie51f07b72009-12-16 13:10:43 +1000533out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000534 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000535 return r;
536}
Dave Airlieff72145b2011-02-07 12:16:14 +1000537
Christian König2f2624c2014-09-12 12:25:45 +0200538/**
539 * radeon_gem_va_update_vm -update the bo_va in its VM
540 *
541 * @rdev: radeon_device pointer
542 * @bo_va: bo_va to update
543 *
544 * Update the bo_va directly after setting it's address. Errors are not
545 * vital here, so they are not reported back to userspace.
546 */
547static void radeon_gem_va_update_vm(struct radeon_device *rdev,
548 struct radeon_bo_va *bo_va)
549{
550 struct ttm_validate_buffer tv, *entry;
Christian König1d0c0942014-11-27 14:48:42 +0100551 struct radeon_bo_list *vm_bos;
Christian König2f2624c2014-09-12 12:25:45 +0200552 struct ww_acquire_ctx ticket;
553 struct list_head list;
554 unsigned domain;
555 int r;
556
557 INIT_LIST_HEAD(&list);
558
559 tv.bo = &bo_va->bo->tbo;
560 tv.shared = true;
561 list_add(&tv.head, &list);
562
563 vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
564 if (!vm_bos)
565 return;
566
Christian Königaa350712014-12-03 15:46:48 +0100567 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
Christian König2f2624c2014-09-12 12:25:45 +0200568 if (r)
569 goto error_free;
570
571 list_for_each_entry(entry, &list, head) {
572 domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type);
573 /* if anything is swapped out don't swap it in here,
574 just abort and wait for the next CS */
575 if (domain == RADEON_GEM_DOMAIN_CPU)
576 goto error_unreserve;
577 }
578
579 mutex_lock(&bo_va->vm->mutex);
580 r = radeon_vm_clear_freed(rdev, bo_va->vm);
581 if (r)
582 goto error_unlock;
583
584 if (bo_va->it.start)
585 r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem);
586
587error_unlock:
588 mutex_unlock(&bo_va->vm->mutex);
589
590error_unreserve:
591 ttm_eu_backoff_reservation(&ticket, &list);
592
593error_free:
594 drm_free_large(vm_bos);
595
596 if (r)
597 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
598}
599
Jerome Glisse721604a2012-01-05 22:11:05 -0500600int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
601 struct drm_file *filp)
602{
603 struct drm_radeon_gem_va *args = data;
604 struct drm_gem_object *gobj;
605 struct radeon_device *rdev = dev->dev_private;
606 struct radeon_fpriv *fpriv = filp->driver_priv;
607 struct radeon_bo *rbo;
608 struct radeon_bo_va *bo_va;
609 u32 invalid_flags;
610 int r = 0;
611
Alex Deucher67e915e2012-01-06 09:38:15 -0500612 if (!rdev->vm_manager.enabled) {
613 args->operation = RADEON_VA_RESULT_ERROR;
614 return -ENOTTY;
615 }
616
Jerome Glisse721604a2012-01-05 22:11:05 -0500617 /* !! DONT REMOVE !!
618 * We don't support vm_id yet, to be sure we don't have have broken
619 * userspace, reject anyone trying to use non 0 value thus moving
620 * forward we can use those fields without breaking existant userspace
621 */
622 if (args->vm_id) {
623 args->operation = RADEON_VA_RESULT_ERROR;
624 return -EINVAL;
625 }
626
627 if (args->offset < RADEON_VA_RESERVED_SIZE) {
628 dev_err(&dev->pdev->dev,
629 "offset 0x%lX is in reserved area 0x%X\n",
630 (unsigned long)args->offset,
631 RADEON_VA_RESERVED_SIZE);
632 args->operation = RADEON_VA_RESULT_ERROR;
633 return -EINVAL;
634 }
635
636 /* don't remove, we need to enforce userspace to set the snooped flag
637 * otherwise we will endup with broken userspace and we won't be able
638 * to enable this feature without adding new interface
639 */
640 invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
641 if ((args->flags & invalid_flags)) {
642 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
643 args->flags, invalid_flags);
644 args->operation = RADEON_VA_RESULT_ERROR;
645 return -EINVAL;
646 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500647
648 switch (args->operation) {
649 case RADEON_VA_MAP:
650 case RADEON_VA_UNMAP:
651 break;
652 default:
653 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
654 args->operation);
655 args->operation = RADEON_VA_RESULT_ERROR;
656 return -EINVAL;
657 }
658
659 gobj = drm_gem_object_lookup(dev, filp, args->handle);
660 if (gobj == NULL) {
661 args->operation = RADEON_VA_RESULT_ERROR;
662 return -ENOENT;
663 }
664 rbo = gem_to_radeon_bo(gobj);
665 r = radeon_bo_reserve(rbo, false);
666 if (r) {
667 args->operation = RADEON_VA_RESULT_ERROR;
668 drm_gem_object_unreference_unlocked(gobj);
669 return r;
670 }
Christian Könige971bd52012-09-11 16:10:04 +0200671 bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
672 if (!bo_va) {
673 args->operation = RADEON_VA_RESULT_ERROR;
674 drm_gem_object_unreference_unlocked(gobj);
675 return -ENOENT;
676 }
677
Jerome Glisse721604a2012-01-05 22:11:05 -0500678 switch (args->operation) {
679 case RADEON_VA_MAP:
Alex Deucher0aea5e42014-07-30 11:49:56 -0400680 if (bo_va->it.start) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500681 args->operation = RADEON_VA_RESULT_VA_EXIST;
Alex Deucher0aea5e42014-07-30 11:49:56 -0400682 args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
Christian König85761f62014-11-19 14:01:20 +0100683 radeon_bo_unreserve(rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500684 goto out;
685 }
Christian Könige971bd52012-09-11 16:10:04 +0200686 r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
Jerome Glisse721604a2012-01-05 22:11:05 -0500687 break;
688 case RADEON_VA_UNMAP:
Christian Könige971bd52012-09-11 16:10:04 +0200689 r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
Jerome Glisse721604a2012-01-05 22:11:05 -0500690 break;
691 default:
692 break;
693 }
Christian König2f2624c2014-09-12 12:25:45 +0200694 if (!r)
695 radeon_gem_va_update_vm(rdev, bo_va);
Jerome Glisse721604a2012-01-05 22:11:05 -0500696 args->operation = RADEON_VA_RESULT_OK;
697 if (r) {
698 args->operation = RADEON_VA_RESULT_ERROR;
699 }
700out:
Jerome Glisse721604a2012-01-05 22:11:05 -0500701 drm_gem_object_unreference_unlocked(gobj);
702 return r;
703}
704
Marek Olšákbda72d52014-03-02 00:56:17 +0100705int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
706 struct drm_file *filp)
707{
708 struct drm_radeon_gem_op *args = data;
709 struct drm_gem_object *gobj;
710 struct radeon_bo *robj;
711 int r;
712
713 gobj = drm_gem_object_lookup(dev, filp, args->handle);
714 if (gobj == NULL) {
715 return -ENOENT;
716 }
717 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200718
719 r = -EPERM;
720 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
721 goto out;
722
Marek Olšákbda72d52014-03-02 00:56:17 +0100723 r = radeon_bo_reserve(robj, false);
724 if (unlikely(r))
725 goto out;
726
727 switch (args->op) {
728 case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
729 args->value = robj->initial_domain;
730 break;
731 case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
732 robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
733 RADEON_GEM_DOMAIN_GTT |
734 RADEON_GEM_DOMAIN_CPU);
735 break;
736 default:
737 r = -EINVAL;
738 }
739
740 radeon_bo_unreserve(robj);
741out:
742 drm_gem_object_unreference_unlocked(gobj);
743 return r;
744}
745
Dave Airlieff72145b2011-02-07 12:16:14 +1000746int radeon_mode_dumb_create(struct drm_file *file_priv,
747 struct drm_device *dev,
748 struct drm_mode_create_dumb *args)
749{
750 struct radeon_device *rdev = dev->dev_private;
751 struct drm_gem_object *gobj;
Dave Airliec87a8d82011-03-17 13:58:34 +1000752 uint32_t handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000753 int r;
754
755 args->pitch = radeon_align_pitch(rdev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
756 args->size = args->pitch * args->height;
757 args->size = ALIGN(args->size, PAGE_SIZE);
758
759 r = radeon_gem_object_create(rdev, args->size, 0,
Michel Dänzer02376d82014-07-17 19:01:08 +0900760 RADEON_GEM_DOMAIN_VRAM, 0,
Christian Königed5cb432014-07-21 13:27:27 +0200761 false, &gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000762 if (r)
763 return -ENOMEM;
764
Dave Airliec87a8d82011-03-17 13:58:34 +1000765 r = drm_gem_handle_create(file_priv, gobj, &handle);
Thomas Hellstrom355a7012014-11-20 09:56:25 +0100766 gobj->dumb = true;
Dave Airliec87a8d82011-03-17 13:58:34 +1000767 /* drop reference from allocate - handle holds it now */
768 drm_gem_object_unreference_unlocked(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000769 if (r) {
Dave Airlieff72145b2011-02-07 12:16:14 +1000770 return r;
771 }
Dave Airliec87a8d82011-03-17 13:58:34 +1000772 args->handle = handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000773 return 0;
774}
775
Jerome Glisse409851f2013-04-25 22:29:27 -0400776#if defined(CONFIG_DEBUG_FS)
777static int radeon_debugfs_gem_info(struct seq_file *m, void *data)
778{
779 struct drm_info_node *node = (struct drm_info_node *)m->private;
780 struct drm_device *dev = node->minor->dev;
781 struct radeon_device *rdev = dev->dev_private;
782 struct radeon_bo *rbo;
783 unsigned i = 0;
784
785 mutex_lock(&rdev->gem.mutex);
786 list_for_each_entry(rbo, &rdev->gem.objects, list) {
787 unsigned domain;
788 const char *placement;
789
790 domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
791 switch (domain) {
792 case RADEON_GEM_DOMAIN_VRAM:
793 placement = "VRAM";
794 break;
795 case RADEON_GEM_DOMAIN_GTT:
796 placement = " GTT";
797 break;
798 case RADEON_GEM_DOMAIN_CPU:
799 default:
800 placement = " CPU";
801 break;
802 }
803 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
804 i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
805 placement, (unsigned long)rbo->pid);
806 i++;
807 }
808 mutex_unlock(&rdev->gem.mutex);
809 return 0;
810}
811
812static struct drm_info_list radeon_debugfs_gem_list[] = {
813 {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
814};
815#endif
816
817int radeon_gem_debugfs_init(struct radeon_device *rdev)
818{
819#if defined(CONFIG_DEBUG_FS)
820 return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
821#endif
822 return 0;
823}