blob: 44617dec8183373c5bd278502d54337f93b4ad77 [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) {
Christian König12f13842015-07-14 15:58:30 +020037 radeon_mn_unregister(robj);
Jerome Glisse4c788672009-11-20 14:29:23 +010038 radeon_bo_unref(&robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020039 }
40}
41
Alex Deucher391bfec2014-07-17 12:26:29 -040042int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size,
Jerome Glisse4c788672009-11-20 14:29:23 +010043 int alignment, int initial_domain,
Christian Königed5cb432014-07-21 13:27:27 +020044 u32 flags, bool kernel,
Jerome Glisse4c788672009-11-20 14:29:23 +010045 struct drm_gem_object **obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020046{
Jerome Glisse4c788672009-11-20 14:29:23 +010047 struct radeon_bo *robj;
Christian König6c0d1122012-10-23 15:53:18 +020048 unsigned long max_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020049 int r;
50
51 *obj = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020052 /* At least align on page size */
53 if (alignment < PAGE_SIZE) {
54 alignment = PAGE_SIZE;
55 }
Christian König6c0d1122012-10-23 15:53:18 +020056
Alex Deucher391bfec2014-07-17 12:26:29 -040057 /* Maximum bo size is the unpinned gtt size since we use the gtt to
58 * handle vram to system pool migrations.
59 */
60 max_size = rdev->mc.gtt_size - rdev->gart_pin_size;
Christian König6c0d1122012-10-23 15:53:18 +020061 if (size > max_size) {
Alex Deucher391bfec2014-07-17 12:26:29 -040062 DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
Michel Dänzer380670a2014-07-16 18:40:32 +090063 size >> 20, max_size >> 20);
Christian König6c0d1122012-10-23 15:53:18 +020064 return -ENOMEM;
65 }
66
Christian König0fe71582012-10-23 15:53:19 +020067retry:
Michel Dänzer02376d82014-07-17 19:01:08 +090068 r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain,
Maarten Lankhorst831b6962014-09-18 14:11:56 +020069 flags, NULL, NULL, &robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020070 if (r) {
Christian König0fe71582012-10-23 15:53:19 +020071 if (r != -ERESTARTSYS) {
72 if (initial_domain == RADEON_GEM_DOMAIN_VRAM) {
73 initial_domain |= RADEON_GEM_DOMAIN_GTT;
74 goto retry;
75 }
Alex Deucher391bfec2014-07-17 12:26:29 -040076 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
Dave Airlieecabd322009-12-15 10:39:48 +100077 size, initial_domain, alignment, r);
Christian König0fe71582012-10-23 15:53:19 +020078 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020079 return r;
80 }
Daniel Vetter441921d2011-02-18 17:59:16 +010081 *obj = &robj->gem_base;
Jerome Glisse409851f2013-04-25 22:29:27 -040082 robj->pid = task_pid_nr(current);
Daniel Vetter441921d2011-02-18 17:59:16 +010083
84 mutex_lock(&rdev->gem.mutex);
85 list_add_tail(&robj->list, &rdev->gem.objects);
86 mutex_unlock(&rdev->gem.mutex);
87
Jerome Glisse771fe6b2009-06-05 14:42:42 +020088 return 0;
89}
90
Rashika Kheria248a6c42014-01-06 20:58:45 +053091static int radeon_gem_set_domain(struct drm_gem_object *gobj,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020092 uint32_t rdomain, uint32_t wdomain)
93{
Jerome Glisse4c788672009-11-20 14:29:23 +010094 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020095 uint32_t domain;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +020096 long r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020097
98 /* FIXME: reeimplement */
Daniel Vetter7e4d15d2011-02-18 17:59:17 +010099 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200100 /* work out where to validate the buffer to */
101 domain = wdomain;
102 if (!domain) {
103 domain = rdomain;
104 }
105 if (!domain) {
106 /* Do nothings */
Joe Perches7ca85292017-02-28 04:55:52 -0800107 pr_warn("Set domain without domain !\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200108 return 0;
109 }
110 if (domain == RADEON_GEM_DOMAIN_CPU) {
111 /* Asking for cpu access wait for object idle */
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200112 r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
113 if (!r)
114 r = -EBUSY;
115
116 if (r < 0 && r != -EINTR) {
Joe Perches7ca85292017-02-28 04:55:52 -0800117 pr_err("Failed to wait for object: %li\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200118 return r;
119 }
120 }
Christopher James Halse Rogersede2e012017-04-03 13:35:23 +1000121 if (domain == RADEON_GEM_DOMAIN_VRAM && robj->prime_shared_count) {
122 /* A BO that is associated with a dma-buf cannot be sensibly migrated to VRAM */
123 return -EINVAL;
124 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200125 return 0;
126}
127
128int radeon_gem_init(struct radeon_device *rdev)
129{
130 INIT_LIST_HEAD(&rdev->gem.objects);
131 return 0;
132}
133
134void radeon_gem_fini(struct radeon_device *rdev)
135{
Jerome Glisse4c788672009-11-20 14:29:23 +0100136 radeon_bo_force_delete(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200137}
138
Jerome Glisse721604a2012-01-05 22:11:05 -0500139/*
140 * Call from drm_gem_handle_create which appear in both new and open ioctl
141 * case.
142 */
143int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
144{
Christian Könige971bd52012-09-11 16:10:04 +0200145 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
146 struct radeon_device *rdev = rbo->rdev;
147 struct radeon_fpriv *fpriv = file_priv->driver_priv;
148 struct radeon_vm *vm = &fpriv->vm;
149 struct radeon_bo_va *bo_va;
150 int r;
151
Alex Deucher544143f2015-01-28 14:36:26 -0500152 if ((rdev->family < CHIP_CAYMAN) ||
153 (!rdev->accel_working)) {
Christian Könige971bd52012-09-11 16:10:04 +0200154 return 0;
155 }
156
157 r = radeon_bo_reserve(rbo, false);
158 if (r) {
159 return r;
160 }
161
162 bo_va = radeon_vm_bo_find(vm, rbo);
163 if (!bo_va) {
164 bo_va = radeon_vm_bo_add(rdev, vm, rbo);
165 } else {
166 ++bo_va->ref_count;
167 }
168 radeon_bo_unreserve(rbo);
169
Jerome Glisse721604a2012-01-05 22:11:05 -0500170 return 0;
171}
172
173void radeon_gem_object_close(struct drm_gem_object *obj,
174 struct drm_file *file_priv)
175{
176 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
177 struct radeon_device *rdev = rbo->rdev;
178 struct radeon_fpriv *fpriv = file_priv->driver_priv;
179 struct radeon_vm *vm = &fpriv->vm;
Christian Könige971bd52012-09-11 16:10:04 +0200180 struct radeon_bo_va *bo_va;
Christian Königd59f7022012-09-11 16:10:02 +0200181 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500182
Alex Deucher544143f2015-01-28 14:36:26 -0500183 if ((rdev->family < CHIP_CAYMAN) ||
184 (!rdev->accel_working)) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500185 return;
186 }
187
Christian Königd59f7022012-09-11 16:10:02 +0200188 r = radeon_bo_reserve(rbo, true);
189 if (r) {
190 dev_err(rdev->dev, "leaking bo va because "
191 "we fail to reserve bo (%d)\n", r);
Jerome Glisse721604a2012-01-05 22:11:05 -0500192 return;
193 }
Christian Könige971bd52012-09-11 16:10:04 +0200194 bo_va = radeon_vm_bo_find(vm, rbo);
195 if (bo_va) {
196 if (--bo_va->ref_count == 0) {
197 radeon_vm_bo_rmv(rdev, bo_va);
198 }
199 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500200 radeon_bo_unreserve(rbo);
201}
202
Christian König6c6f4782012-05-02 15:11:19 +0200203static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
204{
205 if (r == -EDEADLK) {
Christian König6c6f4782012-05-02 15:11:19 +0200206 r = radeon_gpu_reset(rdev);
207 if (!r)
208 r = -EAGAIN;
Christian König6c6f4782012-05-02 15:11:19 +0200209 }
210 return r;
211}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200212
213/*
214 * GEM ioctls.
215 */
216int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
217 struct drm_file *filp)
218{
219 struct radeon_device *rdev = dev->dev_private;
220 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000221 struct ttm_mem_type_manager *man;
222
223 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200224
Michel Dänzer51964e92017-01-30 12:06:35 +0900225 args->vram_size = (u64)man->size << PAGE_SHIFT;
226 args->vram_visible = rdev->mc.visible_vram_size;
Alex Deucherccbe0062014-07-17 12:16:20 -0400227 args->vram_visible -= rdev->vram_pin_size;
228 args->gart_size = rdev->mc.gtt_size;
229 args->gart_size -= rdev->gart_pin_size;
230
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200231 return 0;
232}
233
234int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
235 struct drm_file *filp)
236{
237 /* TODO: implement */
238 DRM_ERROR("unimplemented %s\n", __func__);
239 return -ENOSYS;
240}
241
242int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
243 struct drm_file *filp)
244{
245 /* TODO: implement */
246 DRM_ERROR("unimplemented %s\n", __func__);
247 return -ENOSYS;
248}
249
250int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
251 struct drm_file *filp)
252{
253 struct radeon_device *rdev = dev->dev_private;
254 struct drm_radeon_gem_create *args = data;
255 struct drm_gem_object *gobj;
256 uint32_t handle;
257 int r;
258
Jerome Glissedee53e72012-07-02 12:45:19 -0400259 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200260 /* create a gem object to contain this object in */
261 args->size = roundup(args->size, PAGE_SIZE);
262 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Michel Dänzer02376d82014-07-17 19:01:08 +0900263 args->initial_domain, args->flags,
Christian Königed5cb432014-07-21 13:27:27 +0200264 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200265 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400266 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200267 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200268 return r;
269 }
270 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000271 /* drop reference from allocate - handle holds it now */
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300272 drm_gem_object_put_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400274 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200275 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200276 return r;
277 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278 args->handle = handle;
Jerome Glissedee53e72012-07-02 12:45:19 -0400279 up_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200280 return 0;
281}
282
Christian Königf72a113a2014-08-07 09:36:00 +0200283int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
284 struct drm_file *filp)
285{
Christian König19be5572017-04-12 14:24:39 +0200286 struct ttm_operation_ctx ctx = { true, false };
Christian Königf72a113a2014-08-07 09:36:00 +0200287 struct radeon_device *rdev = dev->dev_private;
288 struct drm_radeon_gem_userptr *args = data;
289 struct drm_gem_object *gobj;
290 struct radeon_bo *bo;
291 uint32_t handle;
292 int r;
293
294 if (offset_in_page(args->addr | args->size))
295 return -EINVAL;
296
Christian Königf72a113a2014-08-07 09:36:00 +0200297 /* reject unknown flag values */
Christian Königddd00e32014-08-07 09:36:01 +0200298 if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
Christian König341cb9e2014-08-07 09:36:03 +0200299 RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
300 RADEON_GEM_USERPTR_REGISTER))
Christian Königf72a113a2014-08-07 09:36:00 +0200301 return -EINVAL;
302
Christian Königbd645e42014-08-07 09:36:04 +0200303 if (args->flags & RADEON_GEM_USERPTR_READONLY) {
304 /* readonly pages not tested on older hardware */
305 if (rdev->family < CHIP_R600)
306 return -EINVAL;
307
308 } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
309 !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {
310
311 /* if we want to write to it we must require anonymous
312 memory and install a MMU notifier */
313 return -EACCES;
314 }
Christian Königf72a113a2014-08-07 09:36:00 +0200315
316 down_read(&rdev->exclusive_lock);
317
318 /* create a gem object to contain this object in */
319 r = radeon_gem_object_create(rdev, args->size, 0,
320 RADEON_GEM_DOMAIN_CPU, 0,
321 false, &gobj);
322 if (r)
323 goto handle_lockup;
324
325 bo = gem_to_radeon_bo(gobj);
326 r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
327 if (r)
328 goto release_object;
329
Christian König341cb9e2014-08-07 09:36:03 +0200330 if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
331 r = radeon_mn_register(bo, args->addr);
332 if (r)
333 goto release_object;
334 }
335
Christian König2a84a442014-08-07 09:36:02 +0200336 if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
337 down_read(&current->mm->mmap_sem);
338 r = radeon_bo_reserve(bo, true);
339 if (r) {
340 up_read(&current->mm->mmap_sem);
341 goto release_object;
342 }
343
344 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
Christian König19be5572017-04-12 14:24:39 +0200345 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
Christian König2a84a442014-08-07 09:36:02 +0200346 radeon_bo_unreserve(bo);
347 up_read(&current->mm->mmap_sem);
348 if (r)
349 goto release_object;
350 }
351
Christian Königf72a113a2014-08-07 09:36:00 +0200352 r = drm_gem_handle_create(filp, gobj, &handle);
353 /* drop reference from allocate - handle holds it now */
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300354 drm_gem_object_put_unlocked(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200355 if (r)
356 goto handle_lockup;
357
358 args->handle = handle;
359 up_read(&rdev->exclusive_lock);
360 return 0;
361
362release_object:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300363 drm_gem_object_put_unlocked(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200364
365handle_lockup:
366 up_read(&rdev->exclusive_lock);
367 r = radeon_gem_handle_lockup(rdev, r);
368
369 return r;
370}
371
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200372int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
373 struct drm_file *filp)
374{
375 /* transition the BO to a domain -
376 * just validate the BO into a certain domain */
Jerome Glissedee53e72012-07-02 12:45:19 -0400377 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200378 struct drm_radeon_gem_set_domain *args = data;
379 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100380 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200381 int r;
382
383 /* for now if someone requests domain CPU -
384 * just make sure the buffer is finished with */
Jerome Glissedee53e72012-07-02 12:45:19 -0400385 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386
387 /* just do a BO wait for now */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100388 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200389 if (gobj == NULL) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400390 up_read(&rdev->exclusive_lock);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100391 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200392 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100393 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200394
395 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
396
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300397 drm_gem_object_put_unlocked(gobj);
Jerome Glissedee53e72012-07-02 12:45:19 -0400398 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200399 r = radeon_gem_handle_lockup(robj->rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200400 return r;
401}
402
Dave Airlieda6b51d2014-12-24 13:11:17 +1000403int radeon_mode_dumb_mmap(struct drm_file *filp,
404 struct drm_device *dev,
405 uint32_t handle, uint64_t *offset_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200406{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200407 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100408 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200409
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100410 gobj = drm_gem_object_lookup(filp, handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200411 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100412 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200413 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100414 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200415 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300416 drm_gem_object_put_unlocked(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200417 return -EPERM;
418 }
Dave Airlieff72145b2011-02-07 12:16:14 +1000419 *offset_p = radeon_bo_mmap_offset(robj);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300420 drm_gem_object_put_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100421 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200422}
423
Dave Airlieff72145b2011-02-07 12:16:14 +1000424int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
425 struct drm_file *filp)
426{
427 struct drm_radeon_gem_mmap *args = data;
428
Dave Airlieda6b51d2014-12-24 13:11:17 +1000429 return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
Dave Airlieff72145b2011-02-07 12:16:14 +1000430}
431
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200432int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
433 struct drm_file *filp)
434{
Dave Airliecefb87e2009-08-16 21:05:45 +1000435 struct drm_radeon_gem_busy *args = data;
436 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100437 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000438 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000439 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000440
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100441 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliecefb87e2009-08-16 21:05:45 +1000442 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100443 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000444 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100445 robj = gem_to_radeon_bo(gobj);
Grigori Goronzy828202a2015-07-03 01:54:10 +0200446
447 r = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
448 if (r == 0)
449 r = -EBUSY;
450 else
451 r = 0;
452
Mark Rutland6aa7de02017-10-23 14:07:29 -0700453 cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
Marek Olšák0bc490a2014-03-02 00:56:19 +0100454 args->domain = radeon_mem_type_to_domain(cur_placement);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300455 drm_gem_object_put_unlocked(gobj);
Dave Airliee3b24152009-08-21 09:47:45 +1000456 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200457}
458
459int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
460 struct drm_file *filp)
461{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400462 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200463 struct drm_radeon_gem_wait_idle *args = data;
464 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100465 struct radeon_bo *robj;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200466 int r = 0;
Michel Dänzer404a6a52014-08-01 17:22:09 +0900467 uint32_t cur_placement = 0;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200468 long ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200469
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100470 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200471 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100472 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200473 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100474 robj = gem_to_radeon_bo(gobj);
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200475
476 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
477 if (ret == 0)
478 r = -EBUSY;
479 else if (ret < 0)
480 r = ret;
481
Michel Dänzer124764f2014-07-31 18:43:48 +0900482 /* Flush HDP cache via MMIO if necessary */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700483 cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
Michel Dänzer404a6a52014-08-01 17:22:09 +0900484 if (rdev->asic->mmio_hdp_flush &&
485 radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
Michel Dänzer124764f2014-07-31 18:43:48 +0900486 robj->rdev->asic->mmio_hdp_flush(rdev);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300487 drm_gem_object_put_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400488 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200489 return r;
490}
Dave Airliee024e112009-06-24 09:48:08 +1000491
492int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
493 struct drm_file *filp)
494{
495 struct drm_radeon_gem_set_tiling *args = data;
496 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100497 struct radeon_bo *robj;
Dave Airliee024e112009-06-24 09:48:08 +1000498 int r = 0;
499
500 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100501 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000502 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100503 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100504 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100505 r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300506 drm_gem_object_put_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000507 return r;
508}
509
510int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
511 struct drm_file *filp)
512{
513 struct drm_radeon_gem_get_tiling *args = data;
514 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100515 struct radeon_bo *rbo;
Dave Airliee024e112009-06-24 09:48:08 +1000516 int r = 0;
517
518 DRM_DEBUG("\n");
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100519 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000520 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100521 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100522 rbo = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100523 r = radeon_bo_reserve(rbo, false);
524 if (unlikely(r != 0))
Dave Airlie51f07b72009-12-16 13:10:43 +1000525 goto out;
Jerome Glisse4c788672009-11-20 14:29:23 +0100526 radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
527 radeon_bo_unreserve(rbo);
Dave Airlie51f07b72009-12-16 13:10:43 +1000528out:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300529 drm_gem_object_put_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000530 return r;
531}
Dave Airlieff72145b2011-02-07 12:16:14 +1000532
Christian König2f2624c2014-09-12 12:25:45 +0200533/**
534 * radeon_gem_va_update_vm -update the bo_va in its VM
535 *
536 * @rdev: radeon_device pointer
537 * @bo_va: bo_va to update
538 *
539 * Update the bo_va directly after setting it's address. Errors are not
540 * vital here, so they are not reported back to userspace.
541 */
542static void radeon_gem_va_update_vm(struct radeon_device *rdev,
543 struct radeon_bo_va *bo_va)
544{
545 struct ttm_validate_buffer tv, *entry;
Christian König1d0c0942014-11-27 14:48:42 +0100546 struct radeon_bo_list *vm_bos;
Christian König2f2624c2014-09-12 12:25:45 +0200547 struct ww_acquire_ctx ticket;
548 struct list_head list;
549 unsigned domain;
550 int r;
551
552 INIT_LIST_HEAD(&list);
553
554 tv.bo = &bo_va->bo->tbo;
Christian Königa9f34c702018-09-19 16:25:08 +0200555 tv.num_shared = 1;
Christian König2f2624c2014-09-12 12:25:45 +0200556 list_add(&tv.head, &list);
557
558 vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
559 if (!vm_bos)
560 return;
561
Christian Königaa350712014-12-03 15:46:48 +0100562 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
Christian König2f2624c2014-09-12 12:25:45 +0200563 if (r)
564 goto error_free;
565
566 list_for_each_entry(entry, &list, head) {
567 domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type);
568 /* if anything is swapped out don't swap it in here,
569 just abort and wait for the next CS */
570 if (domain == RADEON_GEM_DOMAIN_CPU)
571 goto error_unreserve;
572 }
573
574 mutex_lock(&bo_va->vm->mutex);
575 r = radeon_vm_clear_freed(rdev, bo_va->vm);
576 if (r)
577 goto error_unlock;
578
579 if (bo_va->it.start)
580 r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem);
581
582error_unlock:
583 mutex_unlock(&bo_va->vm->mutex);
584
585error_unreserve:
586 ttm_eu_backoff_reservation(&ticket, &list);
587
588error_free:
Michal Hocko20981052017-05-17 14:23:12 +0200589 kvfree(vm_bos);
Christian König2f2624c2014-09-12 12:25:45 +0200590
Christian Königad1a6222015-01-09 11:07:49 +0100591 if (r && r != -ERESTARTSYS)
Christian König2f2624c2014-09-12 12:25:45 +0200592 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
593}
594
Jerome Glisse721604a2012-01-05 22:11:05 -0500595int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
596 struct drm_file *filp)
597{
598 struct drm_radeon_gem_va *args = data;
599 struct drm_gem_object *gobj;
600 struct radeon_device *rdev = dev->dev_private;
601 struct radeon_fpriv *fpriv = filp->driver_priv;
602 struct radeon_bo *rbo;
603 struct radeon_bo_va *bo_va;
604 u32 invalid_flags;
605 int r = 0;
606
Alex Deucher67e915e2012-01-06 09:38:15 -0500607 if (!rdev->vm_manager.enabled) {
608 args->operation = RADEON_VA_RESULT_ERROR;
609 return -ENOTTY;
610 }
611
Jerome Glisse721604a2012-01-05 22:11:05 -0500612 /* !! DONT REMOVE !!
613 * We don't support vm_id yet, to be sure we don't have have broken
614 * userspace, reject anyone trying to use non 0 value thus moving
615 * forward we can use those fields without breaking existant userspace
616 */
617 if (args->vm_id) {
618 args->operation = RADEON_VA_RESULT_ERROR;
619 return -EINVAL;
620 }
621
622 if (args->offset < RADEON_VA_RESERVED_SIZE) {
623 dev_err(&dev->pdev->dev,
624 "offset 0x%lX is in reserved area 0x%X\n",
625 (unsigned long)args->offset,
626 RADEON_VA_RESERVED_SIZE);
627 args->operation = RADEON_VA_RESULT_ERROR;
628 return -EINVAL;
629 }
630
631 /* don't remove, we need to enforce userspace to set the snooped flag
632 * otherwise we will endup with broken userspace and we won't be able
633 * to enable this feature without adding new interface
634 */
635 invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
636 if ((args->flags & invalid_flags)) {
637 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
638 args->flags, invalid_flags);
639 args->operation = RADEON_VA_RESULT_ERROR;
640 return -EINVAL;
641 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500642
643 switch (args->operation) {
644 case RADEON_VA_MAP:
645 case RADEON_VA_UNMAP:
646 break;
647 default:
648 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
649 args->operation);
650 args->operation = RADEON_VA_RESULT_ERROR;
651 return -EINVAL;
652 }
653
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100654 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse721604a2012-01-05 22:11:05 -0500655 if (gobj == NULL) {
656 args->operation = RADEON_VA_RESULT_ERROR;
657 return -ENOENT;
658 }
659 rbo = gem_to_radeon_bo(gobj);
660 r = radeon_bo_reserve(rbo, false);
661 if (r) {
662 args->operation = RADEON_VA_RESULT_ERROR;
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300663 drm_gem_object_put_unlocked(gobj);
Jerome Glisse721604a2012-01-05 22:11:05 -0500664 return r;
665 }
Christian Könige971bd52012-09-11 16:10:04 +0200666 bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
667 if (!bo_va) {
668 args->operation = RADEON_VA_RESULT_ERROR;
Matthew Dawson186bac82016-01-25 10:34:12 -0500669 radeon_bo_unreserve(rbo);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300670 drm_gem_object_put_unlocked(gobj);
Christian Könige971bd52012-09-11 16:10:04 +0200671 return -ENOENT;
672 }
673
Jerome Glisse721604a2012-01-05 22:11:05 -0500674 switch (args->operation) {
675 case RADEON_VA_MAP:
Alex Deucher0aea5e42014-07-30 11:49:56 -0400676 if (bo_va->it.start) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500677 args->operation = RADEON_VA_RESULT_VA_EXIST;
Alex Deucher0aea5e42014-07-30 11:49:56 -0400678 args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
Christian König85761f62014-11-19 14:01:20 +0100679 radeon_bo_unreserve(rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500680 goto out;
681 }
Christian Könige971bd52012-09-11 16:10:04 +0200682 r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
Jerome Glisse721604a2012-01-05 22:11:05 -0500683 break;
684 case RADEON_VA_UNMAP:
Christian Könige971bd52012-09-11 16:10:04 +0200685 r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
Jerome Glisse721604a2012-01-05 22:11:05 -0500686 break;
687 default:
688 break;
689 }
Christian König2f2624c2014-09-12 12:25:45 +0200690 if (!r)
691 radeon_gem_va_update_vm(rdev, bo_va);
Jerome Glisse721604a2012-01-05 22:11:05 -0500692 args->operation = RADEON_VA_RESULT_OK;
693 if (r) {
694 args->operation = RADEON_VA_RESULT_ERROR;
695 }
696out:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300697 drm_gem_object_put_unlocked(gobj);
Jerome Glisse721604a2012-01-05 22:11:05 -0500698 return r;
699}
700
Marek Olšákbda72d52014-03-02 00:56:17 +0100701int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
702 struct drm_file *filp)
703{
704 struct drm_radeon_gem_op *args = data;
705 struct drm_gem_object *gobj;
706 struct radeon_bo *robj;
707 int r;
708
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100709 gobj = drm_gem_object_lookup(filp, args->handle);
Marek Olšákbda72d52014-03-02 00:56:17 +0100710 if (gobj == NULL) {
711 return -ENOENT;
712 }
713 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200714
715 r = -EPERM;
716 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
717 goto out;
718
Marek Olšákbda72d52014-03-02 00:56:17 +0100719 r = radeon_bo_reserve(robj, false);
720 if (unlikely(r))
721 goto out;
722
723 switch (args->op) {
724 case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
725 args->value = robj->initial_domain;
726 break;
727 case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
728 robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
729 RADEON_GEM_DOMAIN_GTT |
730 RADEON_GEM_DOMAIN_CPU);
731 break;
732 default:
733 r = -EINVAL;
734 }
735
736 radeon_bo_unreserve(robj);
737out:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300738 drm_gem_object_put_unlocked(gobj);
Marek Olšákbda72d52014-03-02 00:56:17 +0100739 return r;
740}
741
Dave Airlieff72145b2011-02-07 12:16:14 +1000742int radeon_mode_dumb_create(struct drm_file *file_priv,
743 struct drm_device *dev,
744 struct drm_mode_create_dumb *args)
745{
746 struct radeon_device *rdev = dev->dev_private;
747 struct drm_gem_object *gobj;
Dave Airliec87a8d82011-03-17 13:58:34 +1000748 uint32_t handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000749 int r;
750
Laurent Pinchart802aaf72016-10-18 01:41:18 +0300751 args->pitch = radeon_align_pitch(rdev, args->width,
752 DIV_ROUND_UP(args->bpp, 8), 0);
Dave Airlieff72145b2011-02-07 12:16:14 +1000753 args->size = args->pitch * args->height;
754 args->size = ALIGN(args->size, PAGE_SIZE);
755
756 r = radeon_gem_object_create(rdev, args->size, 0,
Michel Dänzer02376d82014-07-17 19:01:08 +0900757 RADEON_GEM_DOMAIN_VRAM, 0,
Christian Königed5cb432014-07-21 13:27:27 +0200758 false, &gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000759 if (r)
760 return -ENOMEM;
761
Dave Airliec87a8d82011-03-17 13:58:34 +1000762 r = drm_gem_handle_create(file_priv, gobj, &handle);
763 /* drop reference from allocate - handle holds it now */
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300764 drm_gem_object_put_unlocked(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000765 if (r) {
Dave Airlieff72145b2011-02-07 12:16:14 +1000766 return r;
767 }
Dave Airliec87a8d82011-03-17 13:58:34 +1000768 args->handle = handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000769 return 0;
770}
771
Jerome Glisse409851f2013-04-25 22:29:27 -0400772#if defined(CONFIG_DEBUG_FS)
773static int radeon_debugfs_gem_info(struct seq_file *m, void *data)
774{
775 struct drm_info_node *node = (struct drm_info_node *)m->private;
776 struct drm_device *dev = node->minor->dev;
777 struct radeon_device *rdev = dev->dev_private;
778 struct radeon_bo *rbo;
779 unsigned i = 0;
780
781 mutex_lock(&rdev->gem.mutex);
782 list_for_each_entry(rbo, &rdev->gem.objects, list) {
783 unsigned domain;
784 const char *placement;
785
786 domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
787 switch (domain) {
788 case RADEON_GEM_DOMAIN_VRAM:
789 placement = "VRAM";
790 break;
791 case RADEON_GEM_DOMAIN_GTT:
792 placement = " GTT";
793 break;
794 case RADEON_GEM_DOMAIN_CPU:
795 default:
796 placement = " CPU";
797 break;
798 }
799 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
800 i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
801 placement, (unsigned long)rbo->pid);
802 i++;
803 }
804 mutex_unlock(&rdev->gem.mutex);
805 return 0;
806}
807
808static struct drm_info_list radeon_debugfs_gem_list[] = {
809 {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
810};
811#endif
812
813int radeon_gem_debugfs_init(struct radeon_device *rdev)
814{
815#if defined(CONFIG_DEBUG_FS)
816 return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
817#endif
818 return 0;
819}