blob: 574bf7e6b1186fed318cf10f05ff9ed313d671e1 [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 */
Joe Perches7ca85292017-02-28 04:55:52 -0800109 pr_warn("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) {
Joe Perches7ca85292017-02-28 04:55:52 -0800119 pr_err("Failed to wait for object: %li\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200120 return r;
121 }
122 }
Christopher James Halse Rogersede2e012017-04-03 13:35:23 +1000123 if (domain == RADEON_GEM_DOMAIN_VRAM && robj->prime_shared_count) {
124 /* A BO that is associated with a dma-buf cannot be sensibly migrated to VRAM */
125 return -EINVAL;
126 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200127 return 0;
128}
129
130int radeon_gem_init(struct radeon_device *rdev)
131{
132 INIT_LIST_HEAD(&rdev->gem.objects);
133 return 0;
134}
135
136void radeon_gem_fini(struct radeon_device *rdev)
137{
Jerome Glisse4c788672009-11-20 14:29:23 +0100138 radeon_bo_force_delete(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200139}
140
Jerome Glisse721604a2012-01-05 22:11:05 -0500141/*
142 * Call from drm_gem_handle_create which appear in both new and open ioctl
143 * case.
144 */
145int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
146{
Christian Könige971bd52012-09-11 16:10:04 +0200147 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
148 struct radeon_device *rdev = rbo->rdev;
149 struct radeon_fpriv *fpriv = file_priv->driver_priv;
150 struct radeon_vm *vm = &fpriv->vm;
151 struct radeon_bo_va *bo_va;
152 int r;
153
Alex Deucher544143f2015-01-28 14:36:26 -0500154 if ((rdev->family < CHIP_CAYMAN) ||
155 (!rdev->accel_working)) {
Christian Könige971bd52012-09-11 16:10:04 +0200156 return 0;
157 }
158
159 r = radeon_bo_reserve(rbo, false);
160 if (r) {
161 return r;
162 }
163
164 bo_va = radeon_vm_bo_find(vm, rbo);
165 if (!bo_va) {
166 bo_va = radeon_vm_bo_add(rdev, vm, rbo);
167 } else {
168 ++bo_va->ref_count;
169 }
170 radeon_bo_unreserve(rbo);
171
Jerome Glisse721604a2012-01-05 22:11:05 -0500172 return 0;
173}
174
175void radeon_gem_object_close(struct drm_gem_object *obj,
176 struct drm_file *file_priv)
177{
178 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
179 struct radeon_device *rdev = rbo->rdev;
180 struct radeon_fpriv *fpriv = file_priv->driver_priv;
181 struct radeon_vm *vm = &fpriv->vm;
Christian Könige971bd52012-09-11 16:10:04 +0200182 struct radeon_bo_va *bo_va;
Christian Königd59f7022012-09-11 16:10:02 +0200183 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500184
Alex Deucher544143f2015-01-28 14:36:26 -0500185 if ((rdev->family < CHIP_CAYMAN) ||
186 (!rdev->accel_working)) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500187 return;
188 }
189
Christian Königd59f7022012-09-11 16:10:02 +0200190 r = radeon_bo_reserve(rbo, true);
191 if (r) {
192 dev_err(rdev->dev, "leaking bo va because "
193 "we fail to reserve bo (%d)\n", r);
Jerome Glisse721604a2012-01-05 22:11:05 -0500194 return;
195 }
Christian Könige971bd52012-09-11 16:10:04 +0200196 bo_va = radeon_vm_bo_find(vm, rbo);
197 if (bo_va) {
198 if (--bo_va->ref_count == 0) {
199 radeon_vm_bo_rmv(rdev, bo_va);
200 }
201 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500202 radeon_bo_unreserve(rbo);
203}
204
Christian König6c6f4782012-05-02 15:11:19 +0200205static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
206{
207 if (r == -EDEADLK) {
Christian König6c6f4782012-05-02 15:11:19 +0200208 r = radeon_gpu_reset(rdev);
209 if (!r)
210 r = -EAGAIN;
Christian König6c6f4782012-05-02 15:11:19 +0200211 }
212 return r;
213}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200214
215/*
216 * GEM ioctls.
217 */
218int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
219 struct drm_file *filp)
220{
221 struct radeon_device *rdev = dev->dev_private;
222 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000223 struct ttm_mem_type_manager *man;
224
225 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200226
Michel Dänzer51964e92017-01-30 12:06:35 +0900227 args->vram_size = (u64)man->size << PAGE_SHIFT;
228 args->vram_visible = rdev->mc.visible_vram_size;
Alex Deucherccbe0062014-07-17 12:16:20 -0400229 args->vram_visible -= rdev->vram_pin_size;
230 args->gart_size = rdev->mc.gtt_size;
231 args->gart_size -= rdev->gart_pin_size;
232
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200233 return 0;
234}
235
236int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
237 struct drm_file *filp)
238{
239 /* TODO: implement */
240 DRM_ERROR("unimplemented %s\n", __func__);
241 return -ENOSYS;
242}
243
244int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
245 struct drm_file *filp)
246{
247 /* TODO: implement */
248 DRM_ERROR("unimplemented %s\n", __func__);
249 return -ENOSYS;
250}
251
252int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
253 struct drm_file *filp)
254{
255 struct radeon_device *rdev = dev->dev_private;
256 struct drm_radeon_gem_create *args = data;
257 struct drm_gem_object *gobj;
258 uint32_t handle;
259 int r;
260
Jerome Glissedee53e72012-07-02 12:45:19 -0400261 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200262 /* create a gem object to contain this object in */
263 args->size = roundup(args->size, PAGE_SIZE);
264 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Michel Dänzer02376d82014-07-17 19:01:08 +0900265 args->initial_domain, args->flags,
Christian Königed5cb432014-07-21 13:27:27 +0200266 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200267 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400268 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200269 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270 return r;
271 }
272 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000273 /* drop reference from allocate - handle holds it now */
274 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200275 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400276 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200277 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278 return r;
279 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200280 args->handle = handle;
Jerome Glissedee53e72012-07-02 12:45:19 -0400281 up_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200282 return 0;
283}
284
Christian Königf72a113a2014-08-07 09:36:00 +0200285int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
286 struct drm_file *filp)
287{
288 struct radeon_device *rdev = dev->dev_private;
289 struct drm_radeon_gem_userptr *args = data;
290 struct drm_gem_object *gobj;
291 struct radeon_bo *bo;
292 uint32_t handle;
293 int r;
294
295 if (offset_in_page(args->addr | args->size))
296 return -EINVAL;
297
Christian Königf72a113a2014-08-07 09:36:00 +0200298 /* reject unknown flag values */
Christian Königddd00e32014-08-07 09:36:01 +0200299 if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
Christian König341cb9e2014-08-07 09:36:03 +0200300 RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
301 RADEON_GEM_USERPTR_REGISTER))
Christian Königf72a113a2014-08-07 09:36:00 +0200302 return -EINVAL;
303
Christian Königbd645e42014-08-07 09:36:04 +0200304 if (args->flags & RADEON_GEM_USERPTR_READONLY) {
305 /* readonly pages not tested on older hardware */
306 if (rdev->family < CHIP_R600)
307 return -EINVAL;
308
309 } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
310 !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {
311
312 /* if we want to write to it we must require anonymous
313 memory and install a MMU notifier */
314 return -EACCES;
315 }
Christian Königf72a113a2014-08-07 09:36:00 +0200316
317 down_read(&rdev->exclusive_lock);
318
319 /* create a gem object to contain this object in */
320 r = radeon_gem_object_create(rdev, args->size, 0,
321 RADEON_GEM_DOMAIN_CPU, 0,
322 false, &gobj);
323 if (r)
324 goto handle_lockup;
325
326 bo = gem_to_radeon_bo(gobj);
327 r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
328 if (r)
329 goto release_object;
330
Christian König341cb9e2014-08-07 09:36:03 +0200331 if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
332 r = radeon_mn_register(bo, args->addr);
333 if (r)
334 goto release_object;
335 }
336
Christian König2a84a442014-08-07 09:36:02 +0200337 if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
338 down_read(&current->mm->mmap_sem);
339 r = radeon_bo_reserve(bo, true);
340 if (r) {
341 up_read(&current->mm->mmap_sem);
342 goto release_object;
343 }
344
345 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
346 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
347 radeon_bo_unreserve(bo);
348 up_read(&current->mm->mmap_sem);
349 if (r)
350 goto release_object;
351 }
352
Christian Königf72a113a2014-08-07 09:36:00 +0200353 r = drm_gem_handle_create(filp, gobj, &handle);
354 /* drop reference from allocate - handle holds it now */
355 drm_gem_object_unreference_unlocked(gobj);
356 if (r)
357 goto handle_lockup;
358
359 args->handle = handle;
360 up_read(&rdev->exclusive_lock);
361 return 0;
362
363release_object:
364 drm_gem_object_unreference_unlocked(gobj);
365
366handle_lockup:
367 up_read(&rdev->exclusive_lock);
368 r = radeon_gem_handle_lockup(rdev, r);
369
370 return r;
371}
372
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200373int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
374 struct drm_file *filp)
375{
376 /* transition the BO to a domain -
377 * just validate the BO into a certain domain */
Jerome Glissedee53e72012-07-02 12:45:19 -0400378 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200379 struct drm_radeon_gem_set_domain *args = data;
380 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100381 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200382 int r;
383
384 /* for now if someone requests domain CPU -
385 * just make sure the buffer is finished with */
Jerome Glissedee53e72012-07-02 12:45:19 -0400386 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200387
388 /* just do a BO wait for now */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100389 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200390 if (gobj == NULL) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400391 up_read(&rdev->exclusive_lock);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100392 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200393 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100394 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200395
396 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
397
Luca Barbieribc9025b2010-02-09 05:49:12 +0000398 drm_gem_object_unreference_unlocked(gobj);
Jerome Glissedee53e72012-07-02 12:45:19 -0400399 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200400 r = radeon_gem_handle_lockup(robj->rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200401 return r;
402}
403
Dave Airlieda6b51d2014-12-24 13:11:17 +1000404int radeon_mode_dumb_mmap(struct drm_file *filp,
405 struct drm_device *dev,
406 uint32_t handle, uint64_t *offset_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200407{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200408 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100409 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200410
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100411 gobj = drm_gem_object_lookup(filp, handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200412 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100413 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200414 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100415 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200416 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
417 drm_gem_object_unreference_unlocked(gobj);
418 return -EPERM;
419 }
Dave Airlieff72145b2011-02-07 12:16:14 +1000420 *offset_p = radeon_bo_mmap_offset(robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000421 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100422 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200423}
424
Dave Airlieff72145b2011-02-07 12:16:14 +1000425int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
426 struct drm_file *filp)
427{
428 struct drm_radeon_gem_mmap *args = data;
429
Dave Airlieda6b51d2014-12-24 13:11:17 +1000430 return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
Dave Airlieff72145b2011-02-07 12:16:14 +1000431}
432
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200433int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
434 struct drm_file *filp)
435{
Dave Airliecefb87e2009-08-16 21:05:45 +1000436 struct drm_radeon_gem_busy *args = data;
437 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100438 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000439 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000440 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000441
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100442 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliecefb87e2009-08-16 21:05:45 +1000443 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100444 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000445 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100446 robj = gem_to_radeon_bo(gobj);
Grigori Goronzy828202a2015-07-03 01:54:10 +0200447
448 r = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
449 if (r == 0)
450 r = -EBUSY;
451 else
452 r = 0;
453
454 cur_placement = ACCESS_ONCE(robj->tbo.mem.mem_type);
Marek Olšák0bc490a2014-03-02 00:56:19 +0100455 args->domain = radeon_mem_type_to_domain(cur_placement);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000456 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee3b24152009-08-21 09:47:45 +1000457 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200458}
459
460int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
461 struct drm_file *filp)
462{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400463 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200464 struct drm_radeon_gem_wait_idle *args = data;
465 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100466 struct radeon_bo *robj;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200467 int r = 0;
Michel Dänzer404a6a52014-08-01 17:22:09 +0900468 uint32_t cur_placement = 0;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200469 long ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200470
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100471 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200472 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100473 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200474 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100475 robj = gem_to_radeon_bo(gobj);
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200476
477 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
478 if (ret == 0)
479 r = -EBUSY;
480 else if (ret < 0)
481 r = ret;
482
Michel Dänzer124764f2014-07-31 18:43:48 +0900483 /* Flush HDP cache via MMIO if necessary */
Grigori Goronzy54e03982015-07-03 01:54:11 +0200484 cur_placement = ACCESS_ONCE(robj->tbo.mem.mem_type);
Michel Dänzer404a6a52014-08-01 17:22:09 +0900485 if (rdev->asic->mmio_hdp_flush &&
486 radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
Michel Dänzer124764f2014-07-31 18:43:48 +0900487 robj->rdev->asic->mmio_hdp_flush(rdev);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000488 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400489 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200490 return r;
491}
Dave Airliee024e112009-06-24 09:48:08 +1000492
493int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
494 struct drm_file *filp)
495{
496 struct drm_radeon_gem_set_tiling *args = data;
497 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100498 struct radeon_bo *robj;
Dave Airliee024e112009-06-24 09:48:08 +1000499 int r = 0;
500
501 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100502 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000503 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100504 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100505 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100506 r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000507 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000508 return r;
509}
510
511int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
512 struct drm_file *filp)
513{
514 struct drm_radeon_gem_get_tiling *args = data;
515 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100516 struct radeon_bo *rbo;
Dave Airliee024e112009-06-24 09:48:08 +1000517 int r = 0;
518
519 DRM_DEBUG("\n");
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100520 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000521 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100522 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100523 rbo = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100524 r = radeon_bo_reserve(rbo, false);
525 if (unlikely(r != 0))
Dave Airlie51f07b72009-12-16 13:10:43 +1000526 goto out;
Jerome Glisse4c788672009-11-20 14:29:23 +0100527 radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
528 radeon_bo_unreserve(rbo);
Dave Airlie51f07b72009-12-16 13:10:43 +1000529out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000530 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000531 return r;
532}
Dave Airlieff72145b2011-02-07 12:16:14 +1000533
Christian König2f2624c2014-09-12 12:25:45 +0200534/**
535 * radeon_gem_va_update_vm -update the bo_va in its VM
536 *
537 * @rdev: radeon_device pointer
538 * @bo_va: bo_va to update
539 *
540 * Update the bo_va directly after setting it's address. Errors are not
541 * vital here, so they are not reported back to userspace.
542 */
543static void radeon_gem_va_update_vm(struct radeon_device *rdev,
544 struct radeon_bo_va *bo_va)
545{
546 struct ttm_validate_buffer tv, *entry;
Christian König1d0c0942014-11-27 14:48:42 +0100547 struct radeon_bo_list *vm_bos;
Christian König2f2624c2014-09-12 12:25:45 +0200548 struct ww_acquire_ctx ticket;
549 struct list_head list;
550 unsigned domain;
551 int r;
552
553 INIT_LIST_HEAD(&list);
554
555 tv.bo = &bo_va->bo->tbo;
556 tv.shared = true;
557 list_add(&tv.head, &list);
558
559 vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
560 if (!vm_bos)
561 return;
562
Christian Königaa350712014-12-03 15:46:48 +0100563 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
Christian König2f2624c2014-09-12 12:25:45 +0200564 if (r)
565 goto error_free;
566
567 list_for_each_entry(entry, &list, head) {
568 domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type);
569 /* if anything is swapped out don't swap it in here,
570 just abort and wait for the next CS */
571 if (domain == RADEON_GEM_DOMAIN_CPU)
572 goto error_unreserve;
573 }
574
575 mutex_lock(&bo_va->vm->mutex);
576 r = radeon_vm_clear_freed(rdev, bo_va->vm);
577 if (r)
578 goto error_unlock;
579
580 if (bo_va->it.start)
581 r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem);
582
583error_unlock:
584 mutex_unlock(&bo_va->vm->mutex);
585
586error_unreserve:
587 ttm_eu_backoff_reservation(&ticket, &list);
588
589error_free:
Michal Hocko20981052017-05-17 14:23:12 +0200590 kvfree(vm_bos);
Christian König2f2624c2014-09-12 12:25:45 +0200591
Christian Königad1a6222015-01-09 11:07:49 +0100592 if (r && r != -ERESTARTSYS)
Christian König2f2624c2014-09-12 12:25:45 +0200593 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
594}
595
Jerome Glisse721604a2012-01-05 22:11:05 -0500596int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
597 struct drm_file *filp)
598{
599 struct drm_radeon_gem_va *args = data;
600 struct drm_gem_object *gobj;
601 struct radeon_device *rdev = dev->dev_private;
602 struct radeon_fpriv *fpriv = filp->driver_priv;
603 struct radeon_bo *rbo;
604 struct radeon_bo_va *bo_va;
605 u32 invalid_flags;
606 int r = 0;
607
Alex Deucher67e915e2012-01-06 09:38:15 -0500608 if (!rdev->vm_manager.enabled) {
609 args->operation = RADEON_VA_RESULT_ERROR;
610 return -ENOTTY;
611 }
612
Jerome Glisse721604a2012-01-05 22:11:05 -0500613 /* !! DONT REMOVE !!
614 * We don't support vm_id yet, to be sure we don't have have broken
615 * userspace, reject anyone trying to use non 0 value thus moving
616 * forward we can use those fields without breaking existant userspace
617 */
618 if (args->vm_id) {
619 args->operation = RADEON_VA_RESULT_ERROR;
620 return -EINVAL;
621 }
622
623 if (args->offset < RADEON_VA_RESERVED_SIZE) {
624 dev_err(&dev->pdev->dev,
625 "offset 0x%lX is in reserved area 0x%X\n",
626 (unsigned long)args->offset,
627 RADEON_VA_RESERVED_SIZE);
628 args->operation = RADEON_VA_RESULT_ERROR;
629 return -EINVAL;
630 }
631
632 /* don't remove, we need to enforce userspace to set the snooped flag
633 * otherwise we will endup with broken userspace and we won't be able
634 * to enable this feature without adding new interface
635 */
636 invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
637 if ((args->flags & invalid_flags)) {
638 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
639 args->flags, invalid_flags);
640 args->operation = RADEON_VA_RESULT_ERROR;
641 return -EINVAL;
642 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500643
644 switch (args->operation) {
645 case RADEON_VA_MAP:
646 case RADEON_VA_UNMAP:
647 break;
648 default:
649 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
650 args->operation);
651 args->operation = RADEON_VA_RESULT_ERROR;
652 return -EINVAL;
653 }
654
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100655 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse721604a2012-01-05 22:11:05 -0500656 if (gobj == NULL) {
657 args->operation = RADEON_VA_RESULT_ERROR;
658 return -ENOENT;
659 }
660 rbo = gem_to_radeon_bo(gobj);
661 r = radeon_bo_reserve(rbo, false);
662 if (r) {
663 args->operation = RADEON_VA_RESULT_ERROR;
664 drm_gem_object_unreference_unlocked(gobj);
665 return r;
666 }
Christian Könige971bd52012-09-11 16:10:04 +0200667 bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
668 if (!bo_va) {
669 args->operation = RADEON_VA_RESULT_ERROR;
Matthew Dawson186bac82016-01-25 10:34:12 -0500670 radeon_bo_unreserve(rbo);
Christian Könige971bd52012-09-11 16:10:04 +0200671 drm_gem_object_unreference_unlocked(gobj);
672 return -ENOENT;
673 }
674
Jerome Glisse721604a2012-01-05 22:11:05 -0500675 switch (args->operation) {
676 case RADEON_VA_MAP:
Alex Deucher0aea5e42014-07-30 11:49:56 -0400677 if (bo_va->it.start) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500678 args->operation = RADEON_VA_RESULT_VA_EXIST;
Alex Deucher0aea5e42014-07-30 11:49:56 -0400679 args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
Christian König85761f62014-11-19 14:01:20 +0100680 radeon_bo_unreserve(rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500681 goto out;
682 }
Christian Könige971bd52012-09-11 16:10:04 +0200683 r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
Jerome Glisse721604a2012-01-05 22:11:05 -0500684 break;
685 case RADEON_VA_UNMAP:
Christian Könige971bd52012-09-11 16:10:04 +0200686 r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
Jerome Glisse721604a2012-01-05 22:11:05 -0500687 break;
688 default:
689 break;
690 }
Christian König2f2624c2014-09-12 12:25:45 +0200691 if (!r)
692 radeon_gem_va_update_vm(rdev, bo_va);
Jerome Glisse721604a2012-01-05 22:11:05 -0500693 args->operation = RADEON_VA_RESULT_OK;
694 if (r) {
695 args->operation = RADEON_VA_RESULT_ERROR;
696 }
697out:
Jerome Glisse721604a2012-01-05 22:11:05 -0500698 drm_gem_object_unreference_unlocked(gobj);
699 return r;
700}
701
Marek Olšákbda72d52014-03-02 00:56:17 +0100702int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
703 struct drm_file *filp)
704{
705 struct drm_radeon_gem_op *args = data;
706 struct drm_gem_object *gobj;
707 struct radeon_bo *robj;
708 int r;
709
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100710 gobj = drm_gem_object_lookup(filp, args->handle);
Marek Olšákbda72d52014-03-02 00:56:17 +0100711 if (gobj == NULL) {
712 return -ENOENT;
713 }
714 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200715
716 r = -EPERM;
717 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
718 goto out;
719
Marek Olšákbda72d52014-03-02 00:56:17 +0100720 r = radeon_bo_reserve(robj, false);
721 if (unlikely(r))
722 goto out;
723
724 switch (args->op) {
725 case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
726 args->value = robj->initial_domain;
727 break;
728 case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
729 robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
730 RADEON_GEM_DOMAIN_GTT |
731 RADEON_GEM_DOMAIN_CPU);
732 break;
733 default:
734 r = -EINVAL;
735 }
736
737 radeon_bo_unreserve(robj);
738out:
739 drm_gem_object_unreference_unlocked(gobj);
740 return r;
741}
742
Dave Airlieff72145b2011-02-07 12:16:14 +1000743int radeon_mode_dumb_create(struct drm_file *file_priv,
744 struct drm_device *dev,
745 struct drm_mode_create_dumb *args)
746{
747 struct radeon_device *rdev = dev->dev_private;
748 struct drm_gem_object *gobj;
Dave Airliec87a8d82011-03-17 13:58:34 +1000749 uint32_t handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000750 int r;
751
Laurent Pinchart802aaf72016-10-18 01:41:18 +0300752 args->pitch = radeon_align_pitch(rdev, args->width,
753 DIV_ROUND_UP(args->bpp, 8), 0);
Dave Airlieff72145b2011-02-07 12:16:14 +1000754 args->size = args->pitch * args->height;
755 args->size = ALIGN(args->size, PAGE_SIZE);
756
757 r = radeon_gem_object_create(rdev, args->size, 0,
Michel Dänzer02376d82014-07-17 19:01:08 +0900758 RADEON_GEM_DOMAIN_VRAM, 0,
Christian Königed5cb432014-07-21 13:27:27 +0200759 false, &gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000760 if (r)
761 return -ENOMEM;
762
Dave Airliec87a8d82011-03-17 13:58:34 +1000763 r = drm_gem_handle_create(file_priv, gobj, &handle);
764 /* drop reference from allocate - handle holds it now */
765 drm_gem_object_unreference_unlocked(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000766 if (r) {
Dave Airlieff72145b2011-02-07 12:16:14 +1000767 return r;
768 }
Dave Airliec87a8d82011-03-17 13:58:34 +1000769 args->handle = handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000770 return 0;
771}
772
Jerome Glisse409851f2013-04-25 22:29:27 -0400773#if defined(CONFIG_DEBUG_FS)
774static int radeon_debugfs_gem_info(struct seq_file *m, void *data)
775{
776 struct drm_info_node *node = (struct drm_info_node *)m->private;
777 struct drm_device *dev = node->minor->dev;
778 struct radeon_device *rdev = dev->dev_private;
779 struct radeon_bo *rbo;
780 unsigned i = 0;
781
782 mutex_lock(&rdev->gem.mutex);
783 list_for_each_entry(rbo, &rdev->gem.objects, list) {
784 unsigned domain;
785 const char *placement;
786
787 domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
788 switch (domain) {
789 case RADEON_GEM_DOMAIN_VRAM:
790 placement = "VRAM";
791 break;
792 case RADEON_GEM_DOMAIN_GTT:
793 placement = " GTT";
794 break;
795 case RADEON_GEM_DOMAIN_CPU:
796 default:
797 placement = " CPU";
798 break;
799 }
800 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
801 i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
802 placement, (unsigned long)rbo->pid);
803 i++;
804 }
805 mutex_unlock(&rdev->gem.mutex);
806 return 0;
807}
808
809static struct drm_info_list radeon_debugfs_gem_list[] = {
810 {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
811};
812#endif
813
814int radeon_gem_debugfs_init(struct radeon_device *rdev)
815{
816#if defined(CONFIG_DEBUG_FS)
817 return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
818#endif
819 return 0;
820}