blob: d061e638f12ce477657a5fcc17975f393eb47719 [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 */
Sam Ravnborgf9183122019-06-08 10:02:40 +020028
29#include <drm/drm_debugfs.h>
30#include <drm/drm_device.h>
31#include <drm/drm_file.h>
32#include <drm/drm_pci.h>
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/radeon_drm.h>
Sam Ravnborgf9183122019-06-08 10:02:40 +020034
Jerome Glisse771fe6b2009-06-05 14:42:42 +020035#include "radeon.h"
36
Jerome Glisse771fe6b2009-06-05 14:42:42 +020037void radeon_gem_object_free(struct drm_gem_object *gobj)
38{
Daniel Vetter7e4d15d2011-02-18 17:59:17 +010039 struct radeon_bo *robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020040
Jerome Glisse771fe6b2009-06-05 14:42:42 +020041 if (robj) {
Christian König12f13842015-07-14 15:58:30 +020042 radeon_mn_unregister(robj);
Jerome Glisse4c788672009-11-20 14:29:23 +010043 radeon_bo_unref(&robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020044 }
45}
46
Alex Deucher391bfec2014-07-17 12:26:29 -040047int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size,
Jerome Glisse4c788672009-11-20 14:29:23 +010048 int alignment, int initial_domain,
Christian Königed5cb432014-07-21 13:27:27 +020049 u32 flags, bool kernel,
Jerome Glisse4c788672009-11-20 14:29:23 +010050 struct drm_gem_object **obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020051{
Jerome Glisse4c788672009-11-20 14:29:23 +010052 struct radeon_bo *robj;
Christian König6c0d1122012-10-23 15:53:18 +020053 unsigned long max_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020054 int r;
55
56 *obj = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020057 /* At least align on page size */
58 if (alignment < PAGE_SIZE) {
59 alignment = PAGE_SIZE;
60 }
Christian König6c0d1122012-10-23 15:53:18 +020061
Alex Deucher391bfec2014-07-17 12:26:29 -040062 /* Maximum bo size is the unpinned gtt size since we use the gtt to
63 * handle vram to system pool migrations.
64 */
65 max_size = rdev->mc.gtt_size - rdev->gart_pin_size;
Christian König6c0d1122012-10-23 15:53:18 +020066 if (size > max_size) {
Alex Deucher391bfec2014-07-17 12:26:29 -040067 DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
Michel Dänzer380670a2014-07-16 18:40:32 +090068 size >> 20, max_size >> 20);
Christian König6c0d1122012-10-23 15:53:18 +020069 return -ENOMEM;
70 }
71
Christian König0fe71582012-10-23 15:53:19 +020072retry:
Michel Dänzer02376d82014-07-17 19:01:08 +090073 r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain,
Maarten Lankhorst831b6962014-09-18 14:11:56 +020074 flags, NULL, NULL, &robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020075 if (r) {
Christian König0fe71582012-10-23 15:53:19 +020076 if (r != -ERESTARTSYS) {
77 if (initial_domain == RADEON_GEM_DOMAIN_VRAM) {
78 initial_domain |= RADEON_GEM_DOMAIN_GTT;
79 goto retry;
80 }
Alex Deucher391bfec2014-07-17 12:26:29 -040081 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
Dave Airlieecabd322009-12-15 10:39:48 +100082 size, initial_domain, alignment, r);
Christian König0fe71582012-10-23 15:53:19 +020083 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020084 return r;
85 }
Daniel Vetter441921d2011-02-18 17:59:16 +010086 *obj = &robj->gem_base;
Jerome Glisse409851f2013-04-25 22:29:27 -040087 robj->pid = task_pid_nr(current);
Daniel Vetter441921d2011-02-18 17:59:16 +010088
89 mutex_lock(&rdev->gem.mutex);
90 list_add_tail(&robj->list, &rdev->gem.objects);
91 mutex_unlock(&rdev->gem.mutex);
92
Jerome Glisse771fe6b2009-06-05 14:42:42 +020093 return 0;
94}
95
Rashika Kheria248a6c42014-01-06 20:58:45 +053096static int radeon_gem_set_domain(struct drm_gem_object *gobj,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020097 uint32_t rdomain, uint32_t wdomain)
98{
Jerome Glisse4c788672009-11-20 14:29:23 +010099 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200100 uint32_t domain;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200101 long r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200102
103 /* FIXME: reeimplement */
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100104 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200105 /* work out where to validate the buffer to */
106 domain = wdomain;
107 if (!domain) {
108 domain = rdomain;
109 }
110 if (!domain) {
111 /* Do nothings */
Joe Perches7ca85292017-02-28 04:55:52 -0800112 pr_warn("Set domain without domain !\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200113 return 0;
114 }
115 if (domain == RADEON_GEM_DOMAIN_CPU) {
116 /* Asking for cpu access wait for object idle */
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200117 r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
118 if (!r)
119 r = -EBUSY;
120
121 if (r < 0 && r != -EINTR) {
Joe Perches7ca85292017-02-28 04:55:52 -0800122 pr_err("Failed to wait for object: %li\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200123 return r;
124 }
125 }
Christopher James Halse Rogersede2e012017-04-03 13:35:23 +1000126 if (domain == RADEON_GEM_DOMAIN_VRAM && robj->prime_shared_count) {
127 /* A BO that is associated with a dma-buf cannot be sensibly migrated to VRAM */
128 return -EINVAL;
129 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200130 return 0;
131}
132
133int radeon_gem_init(struct radeon_device *rdev)
134{
135 INIT_LIST_HEAD(&rdev->gem.objects);
136 return 0;
137}
138
139void radeon_gem_fini(struct radeon_device *rdev)
140{
Jerome Glisse4c788672009-11-20 14:29:23 +0100141 radeon_bo_force_delete(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142}
143
Jerome Glisse721604a2012-01-05 22:11:05 -0500144/*
145 * Call from drm_gem_handle_create which appear in both new and open ioctl
146 * case.
147 */
148int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
149{
Christian Könige971bd52012-09-11 16:10:04 +0200150 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
151 struct radeon_device *rdev = rbo->rdev;
152 struct radeon_fpriv *fpriv = file_priv->driver_priv;
153 struct radeon_vm *vm = &fpriv->vm;
154 struct radeon_bo_va *bo_va;
155 int r;
156
Alex Deucher544143f2015-01-28 14:36:26 -0500157 if ((rdev->family < CHIP_CAYMAN) ||
158 (!rdev->accel_working)) {
Christian Könige971bd52012-09-11 16:10:04 +0200159 return 0;
160 }
161
162 r = radeon_bo_reserve(rbo, false);
163 if (r) {
164 return r;
165 }
166
167 bo_va = radeon_vm_bo_find(vm, rbo);
168 if (!bo_va) {
169 bo_va = radeon_vm_bo_add(rdev, vm, rbo);
170 } else {
171 ++bo_va->ref_count;
172 }
173 radeon_bo_unreserve(rbo);
174
Jerome Glisse721604a2012-01-05 22:11:05 -0500175 return 0;
176}
177
178void radeon_gem_object_close(struct drm_gem_object *obj,
179 struct drm_file *file_priv)
180{
181 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
182 struct radeon_device *rdev = rbo->rdev;
183 struct radeon_fpriv *fpriv = file_priv->driver_priv;
184 struct radeon_vm *vm = &fpriv->vm;
Christian Könige971bd52012-09-11 16:10:04 +0200185 struct radeon_bo_va *bo_va;
Christian Königd59f7022012-09-11 16:10:02 +0200186 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500187
Alex Deucher544143f2015-01-28 14:36:26 -0500188 if ((rdev->family < CHIP_CAYMAN) ||
189 (!rdev->accel_working)) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500190 return;
191 }
192
Christian Königd59f7022012-09-11 16:10:02 +0200193 r = radeon_bo_reserve(rbo, true);
194 if (r) {
195 dev_err(rdev->dev, "leaking bo va because "
196 "we fail to reserve bo (%d)\n", r);
Jerome Glisse721604a2012-01-05 22:11:05 -0500197 return;
198 }
Christian Könige971bd52012-09-11 16:10:04 +0200199 bo_va = radeon_vm_bo_find(vm, rbo);
200 if (bo_va) {
201 if (--bo_va->ref_count == 0) {
202 radeon_vm_bo_rmv(rdev, bo_va);
203 }
204 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500205 radeon_bo_unreserve(rbo);
206}
207
Christian König6c6f4782012-05-02 15:11:19 +0200208static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
209{
210 if (r == -EDEADLK) {
Christian König6c6f4782012-05-02 15:11:19 +0200211 r = radeon_gpu_reset(rdev);
212 if (!r)
213 r = -EAGAIN;
Christian König6c6f4782012-05-02 15:11:19 +0200214 }
215 return r;
216}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200217
218/*
219 * GEM ioctls.
220 */
221int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
222 struct drm_file *filp)
223{
224 struct radeon_device *rdev = dev->dev_private;
225 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000226 struct ttm_mem_type_manager *man;
227
228 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200229
Michel Dänzer51964e92017-01-30 12:06:35 +0900230 args->vram_size = (u64)man->size << PAGE_SHIFT;
231 args->vram_visible = rdev->mc.visible_vram_size;
Alex Deucherccbe0062014-07-17 12:16:20 -0400232 args->vram_visible -= rdev->vram_pin_size;
233 args->gart_size = rdev->mc.gtt_size;
234 args->gart_size -= rdev->gart_pin_size;
235
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200236 return 0;
237}
238
239int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
240 struct drm_file *filp)
241{
242 /* TODO: implement */
243 DRM_ERROR("unimplemented %s\n", __func__);
244 return -ENOSYS;
245}
246
247int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
248 struct drm_file *filp)
249{
250 /* TODO: implement */
251 DRM_ERROR("unimplemented %s\n", __func__);
252 return -ENOSYS;
253}
254
255int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
256 struct drm_file *filp)
257{
258 struct radeon_device *rdev = dev->dev_private;
259 struct drm_radeon_gem_create *args = data;
260 struct drm_gem_object *gobj;
261 uint32_t handle;
262 int r;
263
Jerome Glissedee53e72012-07-02 12:45:19 -0400264 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200265 /* create a gem object to contain this object in */
266 args->size = roundup(args->size, PAGE_SIZE);
267 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Michel Dänzer02376d82014-07-17 19:01:08 +0900268 args->initial_domain, args->flags,
Christian Königed5cb432014-07-21 13:27:27 +0200269 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400271 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200272 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273 return r;
274 }
275 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000276 /* drop reference from allocate - handle holds it now */
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300277 drm_gem_object_put_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400279 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200280 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200281 return r;
282 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200283 args->handle = handle;
Jerome Glissedee53e72012-07-02 12:45:19 -0400284 up_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200285 return 0;
286}
287
Christian Königf72a113a2014-08-07 09:36:00 +0200288int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
289 struct drm_file *filp)
290{
Christian König19be5572017-04-12 14:24:39 +0200291 struct ttm_operation_ctx ctx = { true, false };
Christian Königf72a113a2014-08-07 09:36:00 +0200292 struct radeon_device *rdev = dev->dev_private;
293 struct drm_radeon_gem_userptr *args = data;
294 struct drm_gem_object *gobj;
295 struct radeon_bo *bo;
296 uint32_t handle;
297 int r;
298
299 if (offset_in_page(args->addr | args->size))
300 return -EINVAL;
301
Christian Königf72a113a2014-08-07 09:36:00 +0200302 /* reject unknown flag values */
Christian Königddd00e32014-08-07 09:36:01 +0200303 if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
Christian König341cb9e2014-08-07 09:36:03 +0200304 RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
305 RADEON_GEM_USERPTR_REGISTER))
Christian Königf72a113a2014-08-07 09:36:00 +0200306 return -EINVAL;
307
Christian Königbd645e42014-08-07 09:36:04 +0200308 if (args->flags & RADEON_GEM_USERPTR_READONLY) {
309 /* readonly pages not tested on older hardware */
310 if (rdev->family < CHIP_R600)
311 return -EINVAL;
312
313 } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
314 !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {
315
316 /* if we want to write to it we must require anonymous
317 memory and install a MMU notifier */
318 return -EACCES;
319 }
Christian Königf72a113a2014-08-07 09:36:00 +0200320
321 down_read(&rdev->exclusive_lock);
322
323 /* create a gem object to contain this object in */
324 r = radeon_gem_object_create(rdev, args->size, 0,
325 RADEON_GEM_DOMAIN_CPU, 0,
326 false, &gobj);
327 if (r)
328 goto handle_lockup;
329
330 bo = gem_to_radeon_bo(gobj);
331 r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
332 if (r)
333 goto release_object;
334
Christian König341cb9e2014-08-07 09:36:03 +0200335 if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
336 r = radeon_mn_register(bo, args->addr);
337 if (r)
338 goto release_object;
339 }
340
Christian König2a84a442014-08-07 09:36:02 +0200341 if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
342 down_read(&current->mm->mmap_sem);
343 r = radeon_bo_reserve(bo, true);
344 if (r) {
345 up_read(&current->mm->mmap_sem);
346 goto release_object;
347 }
348
349 radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
Christian König19be5572017-04-12 14:24:39 +0200350 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
Christian König2a84a442014-08-07 09:36:02 +0200351 radeon_bo_unreserve(bo);
352 up_read(&current->mm->mmap_sem);
353 if (r)
354 goto release_object;
355 }
356
Christian Königf72a113a2014-08-07 09:36:00 +0200357 r = drm_gem_handle_create(filp, gobj, &handle);
358 /* drop reference from allocate - handle holds it now */
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300359 drm_gem_object_put_unlocked(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200360 if (r)
361 goto handle_lockup;
362
363 args->handle = handle;
364 up_read(&rdev->exclusive_lock);
365 return 0;
366
367release_object:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300368 drm_gem_object_put_unlocked(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200369
370handle_lockup:
371 up_read(&rdev->exclusive_lock);
372 r = radeon_gem_handle_lockup(rdev, r);
373
374 return r;
375}
376
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200377int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
378 struct drm_file *filp)
379{
380 /* transition the BO to a domain -
381 * just validate the BO into a certain domain */
Jerome Glissedee53e72012-07-02 12:45:19 -0400382 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200383 struct drm_radeon_gem_set_domain *args = data;
384 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100385 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386 int r;
387
388 /* for now if someone requests domain CPU -
389 * just make sure the buffer is finished with */
Jerome Glissedee53e72012-07-02 12:45:19 -0400390 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200391
392 /* just do a BO wait for now */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100393 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200394 if (gobj == NULL) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400395 up_read(&rdev->exclusive_lock);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100396 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200397 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100398 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200399
400 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
401
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300402 drm_gem_object_put_unlocked(gobj);
Jerome Glissedee53e72012-07-02 12:45:19 -0400403 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200404 r = radeon_gem_handle_lockup(robj->rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200405 return r;
406}
407
Dave Airlieda6b51d2014-12-24 13:11:17 +1000408int radeon_mode_dumb_mmap(struct drm_file *filp,
409 struct drm_device *dev,
410 uint32_t handle, uint64_t *offset_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200411{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200412 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100413 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200414
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100415 gobj = drm_gem_object_lookup(filp, handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200416 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100417 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200418 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100419 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200420 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300421 drm_gem_object_put_unlocked(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200422 return -EPERM;
423 }
Dave Airlieff72145b2011-02-07 12:16:14 +1000424 *offset_p = radeon_bo_mmap_offset(robj);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300425 drm_gem_object_put_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100426 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200427}
428
Dave Airlieff72145b2011-02-07 12:16:14 +1000429int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
430 struct drm_file *filp)
431{
432 struct drm_radeon_gem_mmap *args = data;
433
Dave Airlieda6b51d2014-12-24 13:11:17 +1000434 return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
Dave Airlieff72145b2011-02-07 12:16:14 +1000435}
436
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200437int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
438 struct drm_file *filp)
439{
Dave Airliecefb87e2009-08-16 21:05:45 +1000440 struct drm_radeon_gem_busy *args = data;
441 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100442 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000443 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000444 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000445
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100446 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliecefb87e2009-08-16 21:05:45 +1000447 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100448 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000449 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100450 robj = gem_to_radeon_bo(gobj);
Grigori Goronzy828202a2015-07-03 01:54:10 +0200451
452 r = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
453 if (r == 0)
454 r = -EBUSY;
455 else
456 r = 0;
457
Mark Rutland6aa7de02017-10-23 14:07:29 -0700458 cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
Marek Olšák0bc490a2014-03-02 00:56:19 +0100459 args->domain = radeon_mem_type_to_domain(cur_placement);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300460 drm_gem_object_put_unlocked(gobj);
Dave Airliee3b24152009-08-21 09:47:45 +1000461 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200462}
463
464int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
465 struct drm_file *filp)
466{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400467 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200468 struct drm_radeon_gem_wait_idle *args = data;
469 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100470 struct radeon_bo *robj;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200471 int r = 0;
Michel Dänzer404a6a52014-08-01 17:22:09 +0900472 uint32_t cur_placement = 0;
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200473 long ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200474
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100475 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200476 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100477 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200478 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100479 robj = gem_to_radeon_bo(gobj);
Maarten Lankhorst39e7f6f2014-05-14 15:40:49 +0200480
481 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
482 if (ret == 0)
483 r = -EBUSY;
484 else if (ret < 0)
485 r = ret;
486
Michel Dänzer124764f2014-07-31 18:43:48 +0900487 /* Flush HDP cache via MMIO if necessary */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700488 cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
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);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300492 drm_gem_object_put_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);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100506 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000507 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);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300511 drm_gem_object_put_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");
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100524 gobj = drm_gem_object_lookup(filp, args->handle);
Dave Airliee024e112009-06-24 09:48:08 +1000525 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:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300534 drm_gem_object_put_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;
Christian Königa9f34c702018-09-19 16:25:08 +0200560 tv.num_shared = 1;
Christian König2f2624c2014-09-12 12:25:45 +0200561 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:
Michal Hocko20981052017-05-17 14:23:12 +0200594 kvfree(vm_bos);
Christian König2f2624c2014-09-12 12:25:45 +0200595
Christian Königad1a6222015-01-09 11:07:49 +0100596 if (r && r != -ERESTARTSYS)
Christian König2f2624c2014-09-12 12:25:45 +0200597 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
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100659 gobj = drm_gem_object_lookup(filp, args->handle);
Jerome Glisse721604a2012-01-05 22:11:05 -0500660 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;
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300668 drm_gem_object_put_unlocked(gobj);
Jerome Glisse721604a2012-01-05 22:11:05 -0500669 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;
Matthew Dawson186bac82016-01-25 10:34:12 -0500674 radeon_bo_unreserve(rbo);
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300675 drm_gem_object_put_unlocked(gobj);
Christian Könige971bd52012-09-11 16:10:04 +0200676 return -ENOENT;
677 }
678
Jerome Glisse721604a2012-01-05 22:11:05 -0500679 switch (args->operation) {
680 case RADEON_VA_MAP:
Alex Deucher0aea5e42014-07-30 11:49:56 -0400681 if (bo_va->it.start) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500682 args->operation = RADEON_VA_RESULT_VA_EXIST;
Alex Deucher0aea5e42014-07-30 11:49:56 -0400683 args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
Christian König85761f62014-11-19 14:01:20 +0100684 radeon_bo_unreserve(rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500685 goto out;
686 }
Christian Könige971bd52012-09-11 16:10:04 +0200687 r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
Jerome Glisse721604a2012-01-05 22:11:05 -0500688 break;
689 case RADEON_VA_UNMAP:
Christian Könige971bd52012-09-11 16:10:04 +0200690 r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
Jerome Glisse721604a2012-01-05 22:11:05 -0500691 break;
692 default:
693 break;
694 }
Christian König2f2624c2014-09-12 12:25:45 +0200695 if (!r)
696 radeon_gem_va_update_vm(rdev, bo_va);
Jerome Glisse721604a2012-01-05 22:11:05 -0500697 args->operation = RADEON_VA_RESULT_OK;
698 if (r) {
699 args->operation = RADEON_VA_RESULT_ERROR;
700 }
701out:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300702 drm_gem_object_put_unlocked(gobj);
Jerome Glisse721604a2012-01-05 22:11:05 -0500703 return r;
704}
705
Marek Olšákbda72d52014-03-02 00:56:17 +0100706int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
707 struct drm_file *filp)
708{
709 struct drm_radeon_gem_op *args = data;
710 struct drm_gem_object *gobj;
711 struct radeon_bo *robj;
712 int r;
713
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100714 gobj = drm_gem_object_lookup(filp, args->handle);
Marek Olšákbda72d52014-03-02 00:56:17 +0100715 if (gobj == NULL) {
716 return -ENOENT;
717 }
718 robj = gem_to_radeon_bo(gobj);
Christian Königf72a113a2014-08-07 09:36:00 +0200719
720 r = -EPERM;
721 if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
722 goto out;
723
Marek Olšákbda72d52014-03-02 00:56:17 +0100724 r = radeon_bo_reserve(robj, false);
725 if (unlikely(r))
726 goto out;
727
728 switch (args->op) {
729 case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
730 args->value = robj->initial_domain;
731 break;
732 case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
733 robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
734 RADEON_GEM_DOMAIN_GTT |
735 RADEON_GEM_DOMAIN_CPU);
736 break;
737 default:
738 r = -EINVAL;
739 }
740
741 radeon_bo_unreserve(robj);
742out:
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300743 drm_gem_object_put_unlocked(gobj);
Marek Olšákbda72d52014-03-02 00:56:17 +0100744 return r;
745}
746
Dave Airlieff72145b2011-02-07 12:16:14 +1000747int radeon_mode_dumb_create(struct drm_file *file_priv,
748 struct drm_device *dev,
749 struct drm_mode_create_dumb *args)
750{
751 struct radeon_device *rdev = dev->dev_private;
752 struct drm_gem_object *gobj;
Dave Airliec87a8d82011-03-17 13:58:34 +1000753 uint32_t handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000754 int r;
755
Laurent Pinchart802aaf72016-10-18 01:41:18 +0300756 args->pitch = radeon_align_pitch(rdev, args->width,
757 DIV_ROUND_UP(args->bpp, 8), 0);
Dave Airlieff72145b2011-02-07 12:16:14 +1000758 args->size = args->pitch * args->height;
759 args->size = ALIGN(args->size, PAGE_SIZE);
760
761 r = radeon_gem_object_create(rdev, args->size, 0,
Michel Dänzer02376d82014-07-17 19:01:08 +0900762 RADEON_GEM_DOMAIN_VRAM, 0,
Christian Königed5cb432014-07-21 13:27:27 +0200763 false, &gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000764 if (r)
765 return -ENOMEM;
766
Dave Airliec87a8d82011-03-17 13:58:34 +1000767 r = drm_gem_handle_create(file_priv, gobj, &handle);
768 /* drop reference from allocate - handle holds it now */
Cihangir Akturk07f65bb2017-08-03 14:58:35 +0300769 drm_gem_object_put_unlocked(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000770 if (r) {
Dave Airlieff72145b2011-02-07 12:16:14 +1000771 return r;
772 }
Dave Airliec87a8d82011-03-17 13:58:34 +1000773 args->handle = handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000774 return 0;
775}
776
Jerome Glisse409851f2013-04-25 22:29:27 -0400777#if defined(CONFIG_DEBUG_FS)
778static int radeon_debugfs_gem_info(struct seq_file *m, void *data)
779{
780 struct drm_info_node *node = (struct drm_info_node *)m->private;
781 struct drm_device *dev = node->minor->dev;
782 struct radeon_device *rdev = dev->dev_private;
783 struct radeon_bo *rbo;
784 unsigned i = 0;
785
786 mutex_lock(&rdev->gem.mutex);
787 list_for_each_entry(rbo, &rdev->gem.objects, list) {
788 unsigned domain;
789 const char *placement;
790
791 domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
792 switch (domain) {
793 case RADEON_GEM_DOMAIN_VRAM:
794 placement = "VRAM";
795 break;
796 case RADEON_GEM_DOMAIN_GTT:
797 placement = " GTT";
798 break;
799 case RADEON_GEM_DOMAIN_CPU:
800 default:
801 placement = " CPU";
802 break;
803 }
804 seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
805 i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
806 placement, (unsigned long)rbo->pid);
807 i++;
808 }
809 mutex_unlock(&rdev->gem.mutex);
810 return 0;
811}
812
813static struct drm_info_list radeon_debugfs_gem_list[] = {
814 {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
815};
816#endif
817
818int radeon_gem_debugfs_init(struct radeon_device *rdev)
819{
820#if defined(CONFIG_DEBUG_FS)
821 return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
822#endif
823 return 0;
824}