1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot * Copyright 2008 Advanced Micro Devices, Inc.
3926deccbSFrançois Tigeot * Copyright 2008 Red Hat Inc.
4926deccbSFrançois Tigeot * Copyright 2009 Jerome Glisse.
5926deccbSFrançois Tigeot *
6926deccbSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
7926deccbSFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
8926deccbSFrançois Tigeot * to deal in the Software without restriction, including without limitation
9926deccbSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10926deccbSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
11926deccbSFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
12926deccbSFrançois Tigeot *
13926deccbSFrançois Tigeot * The above copyright notice and this permission notice shall be included in
14926deccbSFrançois Tigeot * all copies or substantial portions of the Software.
15926deccbSFrançois Tigeot *
16926deccbSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17926deccbSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18926deccbSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19926deccbSFrançois Tigeot * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20926deccbSFrançois Tigeot * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21926deccbSFrançois Tigeot * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22926deccbSFrançois Tigeot * OTHER DEALINGS IN THE SOFTWARE.
23926deccbSFrançois Tigeot *
24926deccbSFrançois Tigeot * Authors: Dave Airlie
25926deccbSFrançois Tigeot * Alex Deucher
26926deccbSFrançois Tigeot * Jerome Glisse
27926deccbSFrançois Tigeot */
28926deccbSFrançois Tigeot #include <drm/drmP.h>
2983b4b9b9SFrançois Tigeot #include <drm/radeon_drm.h>
30926deccbSFrançois Tigeot #include "radeon.h"
31926deccbSFrançois Tigeot #include "radeon_gem.h"
32926deccbSFrançois Tigeot
radeon_gem_object_free(struct drm_gem_object * gobj)33926deccbSFrançois Tigeot void radeon_gem_object_free(struct drm_gem_object *gobj)
34926deccbSFrançois Tigeot {
35926deccbSFrançois Tigeot struct radeon_bo *robj = gem_to_radeon_bo(gobj);
36926deccbSFrançois Tigeot
37926deccbSFrançois Tigeot if (robj) {
38926deccbSFrançois Tigeot #ifdef DUMBBELL_WIP
39926deccbSFrançois Tigeot if (robj->gem_base.import_attach)
40926deccbSFrançois Tigeot drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
41926deccbSFrançois Tigeot #endif /* DUMBBELL_WIP */
42c59a5c48SFrançois Tigeot radeon_mn_unregister(robj);
43926deccbSFrançois Tigeot radeon_bo_unref(&robj);
44926deccbSFrançois Tigeot }
45926deccbSFrançois Tigeot }
46926deccbSFrançois Tigeot
radeon_gem_object_create(struct radeon_device * rdev,unsigned long size,int alignment,int initial_domain,u32 flags,bool kernel,struct drm_gem_object ** obj)47c6f73aabSFrançois Tigeot int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size,
48926deccbSFrançois Tigeot int alignment, int initial_domain,
49c6f73aabSFrançois Tigeot u32 flags, bool kernel,
50926deccbSFrançois Tigeot struct drm_gem_object **obj)
51926deccbSFrançois Tigeot {
52926deccbSFrançois Tigeot struct radeon_bo *robj;
53926deccbSFrançois Tigeot unsigned long max_size;
54926deccbSFrançois Tigeot int r;
55926deccbSFrançois Tigeot
56926deccbSFrançois Tigeot *obj = NULL;
57926deccbSFrançois Tigeot /* At least align on page size */
58926deccbSFrançois Tigeot if (alignment < PAGE_SIZE) {
59926deccbSFrançois Tigeot alignment = PAGE_SIZE;
60926deccbSFrançois Tigeot }
61926deccbSFrançois Tigeot
62c6f73aabSFrançois Tigeot /* Maximum bo size is the unpinned gtt size since we use the gtt to
63c6f73aabSFrançois Tigeot * handle vram to system pool migrations.
64c6f73aabSFrançois Tigeot */
65c6f73aabSFrançois Tigeot max_size = rdev->mc.gtt_size - rdev->gart_pin_size;
66926deccbSFrançois Tigeot if (size > max_size) {
67c6f73aabSFrançois Tigeot DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
68c6f73aabSFrançois Tigeot size >> 20, max_size >> 20);
69926deccbSFrançois Tigeot return -ENOMEM;
70926deccbSFrançois Tigeot }
71926deccbSFrançois Tigeot
72926deccbSFrançois Tigeot retry:
73c6f73aabSFrançois Tigeot r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain,
747dcf36dcSFrançois Tigeot flags, NULL, NULL, &robj);
75926deccbSFrançois Tigeot if (r) {
76797013cfSFrançois Tigeot if (r != -ERESTARTSYS) {
77926deccbSFrançois Tigeot if (initial_domain == RADEON_GEM_DOMAIN_VRAM) {
78926deccbSFrançois Tigeot initial_domain |= RADEON_GEM_DOMAIN_GTT;
79926deccbSFrançois Tigeot goto retry;
80926deccbSFrançois Tigeot }
81c6f73aabSFrançois Tigeot DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
82926deccbSFrançois Tigeot size, initial_domain, alignment, r);
83926deccbSFrançois Tigeot }
84926deccbSFrançois Tigeot return r;
85926deccbSFrançois Tigeot }
86926deccbSFrançois Tigeot *obj = &robj->gem_base;
87f43cf1b1SMichael Neumann robj->pid = curproc ? curproc->p_pid : 0;
88926deccbSFrançois Tigeot
89fefad7a7SFrançois Tigeot mutex_lock(&rdev->gem.mutex);
90926deccbSFrançois Tigeot list_add_tail(&robj->list, &rdev->gem.objects);
91fefad7a7SFrançois Tigeot mutex_unlock(&rdev->gem.mutex);
92926deccbSFrançois Tigeot
93926deccbSFrançois Tigeot return 0;
94926deccbSFrançois Tigeot }
95926deccbSFrançois Tigeot
radeon_gem_set_domain(struct drm_gem_object * gobj,uint32_t rdomain,uint32_t wdomain)96926deccbSFrançois Tigeot static int radeon_gem_set_domain(struct drm_gem_object *gobj,
97926deccbSFrançois Tigeot uint32_t rdomain, uint32_t wdomain)
98926deccbSFrançois Tigeot {
99926deccbSFrançois Tigeot struct radeon_bo *robj;
100926deccbSFrançois Tigeot uint32_t domain;
1011cfef1a5SFrançois Tigeot long r;
102926deccbSFrançois Tigeot
103926deccbSFrançois Tigeot /* FIXME: reeimplement */
104926deccbSFrançois Tigeot robj = gem_to_radeon_bo(gobj);
105926deccbSFrançois Tigeot /* work out where to validate the buffer to */
106926deccbSFrançois Tigeot domain = wdomain;
107926deccbSFrançois Tigeot if (!domain) {
108926deccbSFrançois Tigeot domain = rdomain;
109926deccbSFrançois Tigeot }
110926deccbSFrançois Tigeot if (!domain) {
111926deccbSFrançois Tigeot /* Do nothings */
112a85cb24fSFrançois Tigeot pr_warn("Set domain without domain !\n");
113926deccbSFrançois Tigeot return 0;
114926deccbSFrançois Tigeot }
115926deccbSFrançois Tigeot if (domain == RADEON_GEM_DOMAIN_CPU) {
116926deccbSFrançois Tigeot /* Asking for cpu access wait for object idle */
1171cfef1a5SFrançois Tigeot r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
1181cfef1a5SFrançois Tigeot if (!r)
1191cfef1a5SFrançois Tigeot r = -EBUSY;
1201cfef1a5SFrançois Tigeot
1211cfef1a5SFrançois Tigeot if (r < 0 && r != -EINTR) {
122a85cb24fSFrançois Tigeot pr_err("Failed to wait for object: %li\n", r);
123926deccbSFrançois Tigeot return r;
124926deccbSFrançois Tigeot }
125926deccbSFrançois Tigeot }
126a85cb24fSFrançois Tigeot if (domain == RADEON_GEM_DOMAIN_VRAM && robj->prime_shared_count) {
127a85cb24fSFrançois Tigeot /* A BO that is associated with a dma-buf cannot be sensibly migrated to VRAM */
128a85cb24fSFrançois Tigeot return -EINVAL;
129a85cb24fSFrançois Tigeot }
130926deccbSFrançois Tigeot return 0;
131926deccbSFrançois Tigeot }
132926deccbSFrançois Tigeot
radeon_gem_init(struct radeon_device * rdev)133926deccbSFrançois Tigeot int radeon_gem_init(struct radeon_device *rdev)
134926deccbSFrançois Tigeot {
135926deccbSFrançois Tigeot INIT_LIST_HEAD(&rdev->gem.objects);
136926deccbSFrançois Tigeot return 0;
137926deccbSFrançois Tigeot }
138926deccbSFrançois Tigeot
radeon_gem_fini(struct radeon_device * rdev)139926deccbSFrançois Tigeot void radeon_gem_fini(struct radeon_device *rdev)
140926deccbSFrançois Tigeot {
141926deccbSFrançois Tigeot radeon_bo_force_delete(rdev);
142926deccbSFrançois Tigeot }
143926deccbSFrançois Tigeot
144926deccbSFrançois Tigeot /*
145926deccbSFrançois Tigeot * Call from drm_gem_handle_create which appear in both new and open ioctl
146926deccbSFrançois Tigeot * case.
147926deccbSFrançois Tigeot */
radeon_gem_object_open(struct drm_gem_object * obj,struct drm_file * file_priv)148926deccbSFrançois Tigeot int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
149926deccbSFrançois Tigeot {
150926deccbSFrançois Tigeot struct radeon_bo *rbo = gem_to_radeon_bo(obj);
151926deccbSFrançois Tigeot struct radeon_device *rdev = rbo->rdev;
152926deccbSFrançois Tigeot struct radeon_fpriv *fpriv = file_priv->driver_priv;
153926deccbSFrançois Tigeot struct radeon_vm *vm = &fpriv->vm;
154926deccbSFrançois Tigeot struct radeon_bo_va *bo_va;
155926deccbSFrançois Tigeot int r;
156926deccbSFrançois Tigeot
1577dcf36dcSFrançois Tigeot if ((rdev->family < CHIP_CAYMAN) ||
1587dcf36dcSFrançois Tigeot (!rdev->accel_working)) {
159926deccbSFrançois Tigeot return 0;
160926deccbSFrançois Tigeot }
161926deccbSFrançois Tigeot
162926deccbSFrançois Tigeot r = radeon_bo_reserve(rbo, false);
163926deccbSFrançois Tigeot if (r) {
164926deccbSFrançois Tigeot return r;
165926deccbSFrançois Tigeot }
166926deccbSFrançois Tigeot
167926deccbSFrançois Tigeot bo_va = radeon_vm_bo_find(vm, rbo);
168926deccbSFrançois Tigeot if (!bo_va) {
169926deccbSFrançois Tigeot bo_va = radeon_vm_bo_add(rdev, vm, rbo);
170926deccbSFrançois Tigeot } else {
171926deccbSFrançois Tigeot ++bo_va->ref_count;
172926deccbSFrançois Tigeot }
173926deccbSFrançois Tigeot radeon_bo_unreserve(rbo);
174926deccbSFrançois Tigeot
175926deccbSFrançois Tigeot return 0;
176926deccbSFrançois Tigeot }
177926deccbSFrançois Tigeot
radeon_gem_object_close(struct drm_gem_object * obj,struct drm_file * file_priv)178926deccbSFrançois Tigeot void radeon_gem_object_close(struct drm_gem_object *obj,
179926deccbSFrançois Tigeot struct drm_file *file_priv)
180926deccbSFrançois Tigeot {
181926deccbSFrançois Tigeot struct radeon_bo *rbo = gem_to_radeon_bo(obj);
182926deccbSFrançois Tigeot struct radeon_device *rdev = rbo->rdev;
183926deccbSFrançois Tigeot struct radeon_fpriv *fpriv = file_priv->driver_priv;
184926deccbSFrançois Tigeot struct radeon_vm *vm = &fpriv->vm;
185926deccbSFrançois Tigeot struct radeon_bo_va *bo_va;
186926deccbSFrançois Tigeot int r;
187926deccbSFrançois Tigeot
1887dcf36dcSFrançois Tigeot if ((rdev->family < CHIP_CAYMAN) ||
1897dcf36dcSFrançois Tigeot (!rdev->accel_working)) {
190926deccbSFrançois Tigeot return;
191926deccbSFrançois Tigeot }
192926deccbSFrançois Tigeot
193926deccbSFrançois Tigeot r = radeon_bo_reserve(rbo, true);
194926deccbSFrançois Tigeot if (r) {
195926deccbSFrançois Tigeot dev_err(rdev->dev, "leaking bo va because "
196926deccbSFrançois Tigeot "we fail to reserve bo (%d)\n", r);
197926deccbSFrançois Tigeot return;
198926deccbSFrançois Tigeot }
199926deccbSFrançois Tigeot bo_va = radeon_vm_bo_find(vm, rbo);
200926deccbSFrançois Tigeot if (bo_va) {
201926deccbSFrançois Tigeot if (--bo_va->ref_count == 0) {
202926deccbSFrançois Tigeot radeon_vm_bo_rmv(rdev, bo_va);
203926deccbSFrançois Tigeot }
204926deccbSFrançois Tigeot }
205926deccbSFrançois Tigeot radeon_bo_unreserve(rbo);
206926deccbSFrançois Tigeot }
207926deccbSFrançois Tigeot
radeon_gem_handle_lockup(struct radeon_device * rdev,int r)208926deccbSFrançois Tigeot static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
209926deccbSFrançois Tigeot {
210926deccbSFrançois Tigeot if (r == -EDEADLK) {
211926deccbSFrançois Tigeot r = radeon_gpu_reset(rdev);
212926deccbSFrançois Tigeot if (!r)
213926deccbSFrançois Tigeot r = -EAGAIN;
214926deccbSFrançois Tigeot }
215926deccbSFrançois Tigeot return r;
216926deccbSFrançois Tigeot }
217926deccbSFrançois Tigeot
218926deccbSFrançois Tigeot /*
219926deccbSFrançois Tigeot * GEM ioctls.
220926deccbSFrançois Tigeot */
radeon_gem_info_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)221926deccbSFrançois Tigeot int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
222926deccbSFrançois Tigeot struct drm_file *filp)
223926deccbSFrançois Tigeot {
224926deccbSFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
225926deccbSFrançois Tigeot struct drm_radeon_gem_info *args = data;
226926deccbSFrançois Tigeot struct ttm_mem_type_manager *man;
227926deccbSFrançois Tigeot
228926deccbSFrançois Tigeot man = &rdev->mman.bdev.man[TTM_PL_VRAM];
229926deccbSFrançois Tigeot
2304be47400SFrançois Tigeot args->vram_size = (u64)man->size << PAGE_SHIFT;
2314be47400SFrançois Tigeot args->vram_visible = rdev->mc.visible_vram_size;
232c6f73aabSFrançois Tigeot args->vram_visible -= rdev->vram_pin_size;
233c6f73aabSFrançois Tigeot args->gart_size = rdev->mc.gtt_size;
234c6f73aabSFrançois Tigeot args->gart_size -= rdev->gart_pin_size;
235c6f73aabSFrançois Tigeot
236926deccbSFrançois Tigeot return 0;
237926deccbSFrançois Tigeot }
238926deccbSFrançois Tigeot
radeon_gem_pread_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)239926deccbSFrançois Tigeot int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
240926deccbSFrançois Tigeot struct drm_file *filp)
241926deccbSFrançois Tigeot {
242926deccbSFrançois Tigeot /* TODO: implement */
243926deccbSFrançois Tigeot DRM_ERROR("unimplemented %s\n", __func__);
244926deccbSFrançois Tigeot return -ENOSYS;
245926deccbSFrançois Tigeot }
246926deccbSFrançois Tigeot
radeon_gem_pwrite_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)247926deccbSFrançois Tigeot int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
248926deccbSFrançois Tigeot struct drm_file *filp)
249926deccbSFrançois Tigeot {
250926deccbSFrançois Tigeot /* TODO: implement */
251926deccbSFrançois Tigeot DRM_ERROR("unimplemented %s\n", __func__);
252926deccbSFrançois Tigeot return -ENOSYS;
253926deccbSFrançois Tigeot }
254926deccbSFrançois Tigeot
radeon_gem_create_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)255926deccbSFrançois Tigeot int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
256926deccbSFrançois Tigeot struct drm_file *filp)
257926deccbSFrançois Tigeot {
258926deccbSFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
259926deccbSFrançois Tigeot struct drm_radeon_gem_create *args = data;
260926deccbSFrançois Tigeot struct drm_gem_object *gobj;
261926deccbSFrançois Tigeot uint32_t handle;
262926deccbSFrançois Tigeot int r;
263926deccbSFrançois Tigeot
264d78d3a22SFrançois Tigeot down_read(&rdev->exclusive_lock);
265926deccbSFrançois Tigeot /* create a gem object to contain this object in */
266926deccbSFrançois Tigeot args->size = roundup(args->size, PAGE_SIZE);
267926deccbSFrançois Tigeot r = radeon_gem_object_create(rdev, args->size, args->alignment,
268c6f73aabSFrançois Tigeot args->initial_domain, args->flags,
269926deccbSFrançois Tigeot false, &gobj);
270926deccbSFrançois Tigeot if (r) {
27163c3939fSImre Vadasz if (r == -ERESTARTSYS)
27263c3939fSImre Vadasz r = -EINTR;
273d78d3a22SFrançois Tigeot up_read(&rdev->exclusive_lock);
274926deccbSFrançois Tigeot r = radeon_gem_handle_lockup(rdev, r);
275926deccbSFrançois Tigeot return r;
276926deccbSFrançois Tigeot }
277926deccbSFrançois Tigeot r = drm_gem_handle_create(filp, gobj, &handle);
278926deccbSFrançois Tigeot /* drop reference from allocate - handle holds it now */
279*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
280926deccbSFrançois Tigeot if (r) {
281d78d3a22SFrançois Tigeot up_read(&rdev->exclusive_lock);
282926deccbSFrançois Tigeot r = radeon_gem_handle_lockup(rdev, r);
283926deccbSFrançois Tigeot return r;
284926deccbSFrançois Tigeot }
285926deccbSFrançois Tigeot args->handle = handle;
286d78d3a22SFrançois Tigeot up_read(&rdev->exclusive_lock);
287926deccbSFrançois Tigeot return 0;
288926deccbSFrançois Tigeot }
289926deccbSFrançois Tigeot
2901cfef1a5SFrançois Tigeot #if 0
2911cfef1a5SFrançois Tigeot int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
2921cfef1a5SFrançois Tigeot struct drm_file *filp)
2931cfef1a5SFrançois Tigeot {
2941cfef1a5SFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
2951cfef1a5SFrançois Tigeot struct drm_radeon_gem_userptr *args = data;
2961cfef1a5SFrançois Tigeot struct drm_gem_object *gobj;
2971cfef1a5SFrançois Tigeot struct radeon_bo *bo;
2981cfef1a5SFrançois Tigeot uint32_t handle;
2991cfef1a5SFrançois Tigeot int r;
3001cfef1a5SFrançois Tigeot
3011cfef1a5SFrançois Tigeot if (offset_in_page(args->addr | args->size))
3021cfef1a5SFrançois Tigeot return -EINVAL;
3031cfef1a5SFrançois Tigeot
3041cfef1a5SFrançois Tigeot /* reject unknown flag values */
3051cfef1a5SFrançois Tigeot if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
3061cfef1a5SFrançois Tigeot RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
3071cfef1a5SFrançois Tigeot RADEON_GEM_USERPTR_REGISTER))
3081cfef1a5SFrançois Tigeot return -EINVAL;
3091cfef1a5SFrançois Tigeot
3101cfef1a5SFrançois Tigeot if (args->flags & RADEON_GEM_USERPTR_READONLY) {
3111cfef1a5SFrançois Tigeot /* readonly pages not tested on older hardware */
3121cfef1a5SFrançois Tigeot if (rdev->family < CHIP_R600)
3131cfef1a5SFrançois Tigeot return -EINVAL;
3141cfef1a5SFrançois Tigeot
3151cfef1a5SFrançois Tigeot } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
3161cfef1a5SFrançois Tigeot !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {
3171cfef1a5SFrançois Tigeot
3181cfef1a5SFrançois Tigeot /* if we want to write to it we must require anonymous
3191cfef1a5SFrançois Tigeot memory and install a MMU notifier */
3201cfef1a5SFrançois Tigeot return -EACCES;
3211cfef1a5SFrançois Tigeot }
3221cfef1a5SFrançois Tigeot
3231cfef1a5SFrançois Tigeot down_read(&rdev->exclusive_lock);
3241cfef1a5SFrançois Tigeot
3251cfef1a5SFrançois Tigeot /* create a gem object to contain this object in */
3261cfef1a5SFrançois Tigeot r = radeon_gem_object_create(rdev, args->size, 0,
3271cfef1a5SFrançois Tigeot RADEON_GEM_DOMAIN_CPU, 0,
3281cfef1a5SFrançois Tigeot false, &gobj);
3291cfef1a5SFrançois Tigeot if (r)
3301cfef1a5SFrançois Tigeot goto handle_lockup;
3311cfef1a5SFrançois Tigeot
3321cfef1a5SFrançois Tigeot bo = gem_to_radeon_bo(gobj);
3331cfef1a5SFrançois Tigeot r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
3341cfef1a5SFrançois Tigeot if (r)
3351cfef1a5SFrançois Tigeot goto release_object;
3361cfef1a5SFrançois Tigeot
3371cfef1a5SFrançois Tigeot if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
3381cfef1a5SFrançois Tigeot r = radeon_mn_register(bo, args->addr);
3391cfef1a5SFrançois Tigeot if (r)
3401cfef1a5SFrançois Tigeot goto release_object;
3411cfef1a5SFrançois Tigeot }
3421cfef1a5SFrançois Tigeot
3431cfef1a5SFrançois Tigeot if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
3441cfef1a5SFrançois Tigeot down_read(¤t->mm->mmap_sem);
3451cfef1a5SFrançois Tigeot r = radeon_bo_reserve(bo, true);
3461cfef1a5SFrançois Tigeot if (r) {
3471cfef1a5SFrançois Tigeot up_read(¤t->mm->mmap_sem);
3481cfef1a5SFrançois Tigeot goto release_object;
3491cfef1a5SFrançois Tigeot }
3501cfef1a5SFrançois Tigeot
3511cfef1a5SFrançois Tigeot radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
3521cfef1a5SFrançois Tigeot r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
3531cfef1a5SFrançois Tigeot radeon_bo_unreserve(bo);
3541cfef1a5SFrançois Tigeot up_read(¤t->mm->mmap_sem);
3551cfef1a5SFrançois Tigeot if (r)
3561cfef1a5SFrançois Tigeot goto release_object;
3571cfef1a5SFrançois Tigeot }
3581cfef1a5SFrançois Tigeot
3591cfef1a5SFrançois Tigeot r = drm_gem_handle_create(filp, gobj, &handle);
3601cfef1a5SFrançois Tigeot /* drop reference from allocate - handle holds it now */
361*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
3621cfef1a5SFrançois Tigeot if (r)
3631cfef1a5SFrançois Tigeot goto handle_lockup;
3641cfef1a5SFrançois Tigeot
3651cfef1a5SFrançois Tigeot args->handle = handle;
3661cfef1a5SFrançois Tigeot up_read(&rdev->exclusive_lock);
3671cfef1a5SFrançois Tigeot return 0;
3681cfef1a5SFrançois Tigeot
3691cfef1a5SFrançois Tigeot release_object:
370*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
3711cfef1a5SFrançois Tigeot
3721cfef1a5SFrançois Tigeot handle_lockup:
3731cfef1a5SFrançois Tigeot up_read(&rdev->exclusive_lock);
3741cfef1a5SFrançois Tigeot r = radeon_gem_handle_lockup(rdev, r);
3751cfef1a5SFrançois Tigeot
3761cfef1a5SFrançois Tigeot return r;
3771cfef1a5SFrançois Tigeot }
3781cfef1a5SFrançois Tigeot #endif
3791cfef1a5SFrançois Tigeot
radeon_gem_set_domain_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)380926deccbSFrançois Tigeot int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
381926deccbSFrançois Tigeot struct drm_file *filp)
382926deccbSFrançois Tigeot {
383926deccbSFrançois Tigeot /* transition the BO to a domain -
384926deccbSFrançois Tigeot * just validate the BO into a certain domain */
385926deccbSFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
386926deccbSFrançois Tigeot struct drm_radeon_gem_set_domain *args = data;
387926deccbSFrançois Tigeot struct drm_gem_object *gobj;
388926deccbSFrançois Tigeot struct radeon_bo *robj;
389926deccbSFrançois Tigeot int r;
390926deccbSFrançois Tigeot
391926deccbSFrançois Tigeot /* for now if someone requests domain CPU -
392926deccbSFrançois Tigeot * just make sure the buffer is finished with */
393d78d3a22SFrançois Tigeot down_read(&rdev->exclusive_lock);
394926deccbSFrançois Tigeot
395926deccbSFrançois Tigeot /* just do a BO wait for now */
3968621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, args->handle);
397926deccbSFrançois Tigeot if (gobj == NULL) {
398d78d3a22SFrançois Tigeot up_read(&rdev->exclusive_lock);
399926deccbSFrançois Tigeot return -ENOENT;
400926deccbSFrançois Tigeot }
401926deccbSFrançois Tigeot robj = gem_to_radeon_bo(gobj);
402926deccbSFrançois Tigeot
403926deccbSFrançois Tigeot r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
404926deccbSFrançois Tigeot
405*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
406d78d3a22SFrançois Tigeot up_read(&rdev->exclusive_lock);
407926deccbSFrançois Tigeot r = radeon_gem_handle_lockup(robj->rdev, r);
408926deccbSFrançois Tigeot return r;
409926deccbSFrançois Tigeot }
410926deccbSFrançois Tigeot
radeon_mode_dumb_mmap(struct drm_file * filp,struct drm_device * dev,uint32_t handle,uint64_t * offset_p)411926deccbSFrançois Tigeot int radeon_mode_dumb_mmap(struct drm_file *filp,
412926deccbSFrançois Tigeot struct drm_device *dev,
413926deccbSFrançois Tigeot uint32_t handle, uint64_t *offset_p)
414926deccbSFrançois Tigeot {
415926deccbSFrançois Tigeot struct drm_gem_object *gobj;
416926deccbSFrançois Tigeot struct radeon_bo *robj;
417926deccbSFrançois Tigeot
4188621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, handle);
419926deccbSFrançois Tigeot if (gobj == NULL) {
420926deccbSFrançois Tigeot return -ENOENT;
421926deccbSFrançois Tigeot }
422926deccbSFrançois Tigeot robj = gem_to_radeon_bo(gobj);
423d78d3a22SFrançois Tigeot #if 0
424d78d3a22SFrançois Tigeot if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
425*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
426d78d3a22SFrançois Tigeot return -EPERM;
427d78d3a22SFrançois Tigeot }
428d78d3a22SFrançois Tigeot #endif
429926deccbSFrançois Tigeot *offset_p = radeon_bo_mmap_offset(robj);
430*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
431926deccbSFrançois Tigeot return 0;
432926deccbSFrançois Tigeot }
433926deccbSFrançois Tigeot
radeon_gem_mmap_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)434926deccbSFrançois Tigeot int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
435926deccbSFrançois Tigeot struct drm_file *filp)
436926deccbSFrançois Tigeot {
437926deccbSFrançois Tigeot struct drm_radeon_gem_mmap *args = data;
438926deccbSFrançois Tigeot
439f77dbd6cSFrançois Tigeot return radeon_mode_dumb_mmap(filp, dev, args->handle, (uint64_t *)&args->addr_ptr);
440926deccbSFrançois Tigeot }
441926deccbSFrançois Tigeot
radeon_gem_busy_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)442926deccbSFrançois Tigeot int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
443926deccbSFrançois Tigeot struct drm_file *filp)
444926deccbSFrançois Tigeot {
445926deccbSFrançois Tigeot struct drm_radeon_gem_busy *args = data;
446926deccbSFrançois Tigeot struct drm_gem_object *gobj;
447926deccbSFrançois Tigeot struct radeon_bo *robj;
448926deccbSFrançois Tigeot int r;
449926deccbSFrançois Tigeot uint32_t cur_placement = 0;
450926deccbSFrançois Tigeot
4518621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, args->handle);
452926deccbSFrançois Tigeot if (gobj == NULL) {
453926deccbSFrançois Tigeot return -ENOENT;
454926deccbSFrançois Tigeot }
455926deccbSFrançois Tigeot robj = gem_to_radeon_bo(gobj);
456c59a5c48SFrançois Tigeot
457c59a5c48SFrançois Tigeot r = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
458c59a5c48SFrançois Tigeot if (r == 0)
459c59a5c48SFrançois Tigeot r = -EBUSY;
460c59a5c48SFrançois Tigeot else
461c59a5c48SFrançois Tigeot r = 0;
462c59a5c48SFrançois Tigeot
463*3f2dd94aSFrançois Tigeot cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
464c6f73aabSFrançois Tigeot args->domain = radeon_mem_type_to_domain(cur_placement);
465*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
466926deccbSFrançois Tigeot return r;
467926deccbSFrançois Tigeot }
468926deccbSFrançois Tigeot
radeon_gem_wait_idle_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)469926deccbSFrançois Tigeot int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
470926deccbSFrançois Tigeot struct drm_file *filp)
471926deccbSFrançois Tigeot {
472926deccbSFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
473926deccbSFrançois Tigeot struct drm_radeon_gem_wait_idle *args = data;
474926deccbSFrançois Tigeot struct drm_gem_object *gobj;
475926deccbSFrançois Tigeot struct radeon_bo *robj;
4761cfef1a5SFrançois Tigeot int r = 0;
477c6f73aabSFrançois Tigeot uint32_t cur_placement = 0;
4781cfef1a5SFrançois Tigeot long ret;
479926deccbSFrançois Tigeot
4808621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, args->handle);
481926deccbSFrançois Tigeot if (gobj == NULL) {
482926deccbSFrançois Tigeot return -ENOENT;
483926deccbSFrançois Tigeot }
484926deccbSFrançois Tigeot robj = gem_to_radeon_bo(gobj);
4851cfef1a5SFrançois Tigeot
4861cfef1a5SFrançois Tigeot ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
4871cfef1a5SFrançois Tigeot if (ret == 0)
4881cfef1a5SFrançois Tigeot r = -EBUSY;
4891cfef1a5SFrançois Tigeot else if (ret < 0)
4901cfef1a5SFrançois Tigeot r = ret;
4911cfef1a5SFrançois Tigeot
492c6f73aabSFrançois Tigeot /* Flush HDP cache via MMIO if necessary */
493*3f2dd94aSFrançois Tigeot cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
494c6f73aabSFrançois Tigeot if (rdev->asic->mmio_hdp_flush &&
495c6f73aabSFrançois Tigeot radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
496c6f73aabSFrançois Tigeot robj->rdev->asic->mmio_hdp_flush(rdev);
497*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
49863c3939fSImre Vadasz if (r == -ERESTARTSYS)
49963c3939fSImre Vadasz r = -EINTR;
500926deccbSFrançois Tigeot r = radeon_gem_handle_lockup(rdev, r);
501926deccbSFrançois Tigeot return r;
502926deccbSFrançois Tigeot }
503926deccbSFrançois Tigeot
radeon_gem_set_tiling_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)504926deccbSFrançois Tigeot int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
505926deccbSFrançois Tigeot struct drm_file *filp)
506926deccbSFrançois Tigeot {
507926deccbSFrançois Tigeot struct drm_radeon_gem_set_tiling *args = data;
508926deccbSFrançois Tigeot struct drm_gem_object *gobj;
509926deccbSFrançois Tigeot struct radeon_bo *robj;
510926deccbSFrançois Tigeot int r = 0;
511926deccbSFrançois Tigeot
512926deccbSFrançois Tigeot DRM_DEBUG("%d \n", args->handle);
5138621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, args->handle);
514926deccbSFrançois Tigeot if (gobj == NULL)
515926deccbSFrançois Tigeot return -ENOENT;
516926deccbSFrançois Tigeot robj = gem_to_radeon_bo(gobj);
517926deccbSFrançois Tigeot r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
518*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
519926deccbSFrançois Tigeot return r;
520926deccbSFrançois Tigeot }
521926deccbSFrançois Tigeot
radeon_gem_get_tiling_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)522926deccbSFrançois Tigeot int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
523926deccbSFrançois Tigeot struct drm_file *filp)
524926deccbSFrançois Tigeot {
525926deccbSFrançois Tigeot struct drm_radeon_gem_get_tiling *args = data;
526926deccbSFrançois Tigeot struct drm_gem_object *gobj;
527926deccbSFrançois Tigeot struct radeon_bo *rbo;
528926deccbSFrançois Tigeot int r = 0;
529926deccbSFrançois Tigeot
530926deccbSFrançois Tigeot DRM_DEBUG("\n");
5318621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, args->handle);
532926deccbSFrançois Tigeot if (gobj == NULL)
533926deccbSFrançois Tigeot return -ENOENT;
534926deccbSFrançois Tigeot rbo = gem_to_radeon_bo(gobj);
535926deccbSFrançois Tigeot r = radeon_bo_reserve(rbo, false);
536926deccbSFrançois Tigeot if (unlikely(r != 0))
537926deccbSFrançois Tigeot goto out;
538926deccbSFrançois Tigeot radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
539926deccbSFrançois Tigeot radeon_bo_unreserve(rbo);
540926deccbSFrançois Tigeot out:
541*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
542926deccbSFrançois Tigeot return r;
543926deccbSFrançois Tigeot }
544926deccbSFrançois Tigeot
5457dcf36dcSFrançois Tigeot /**
5467dcf36dcSFrançois Tigeot * radeon_gem_va_update_vm -update the bo_va in its VM
5477dcf36dcSFrançois Tigeot *
5487dcf36dcSFrançois Tigeot * @rdev: radeon_device pointer
5497dcf36dcSFrançois Tigeot * @bo_va: bo_va to update
5507dcf36dcSFrançois Tigeot *
5517dcf36dcSFrançois Tigeot * Update the bo_va directly after setting it's address. Errors are not
5527dcf36dcSFrançois Tigeot * vital here, so they are not reported back to userspace.
5537dcf36dcSFrançois Tigeot */
radeon_gem_va_update_vm(struct radeon_device * rdev,struct radeon_bo_va * bo_va)5547dcf36dcSFrançois Tigeot static void radeon_gem_va_update_vm(struct radeon_device *rdev,
5557dcf36dcSFrançois Tigeot struct radeon_bo_va *bo_va)
5567dcf36dcSFrançois Tigeot {
5577dcf36dcSFrançois Tigeot struct ttm_validate_buffer tv, *entry;
5587dcf36dcSFrançois Tigeot struct radeon_bo_list *vm_bos;
5597dcf36dcSFrançois Tigeot struct ww_acquire_ctx ticket;
5607dcf36dcSFrançois Tigeot struct list_head list;
5617dcf36dcSFrançois Tigeot unsigned domain;
5627dcf36dcSFrançois Tigeot int r;
5637dcf36dcSFrançois Tigeot
5647dcf36dcSFrançois Tigeot INIT_LIST_HEAD(&list);
5657dcf36dcSFrançois Tigeot
5667dcf36dcSFrançois Tigeot tv.bo = &bo_va->bo->tbo;
5677dcf36dcSFrançois Tigeot tv.shared = true;
5687dcf36dcSFrançois Tigeot list_add(&tv.head, &list);
5697dcf36dcSFrançois Tigeot
5707dcf36dcSFrançois Tigeot vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
5717dcf36dcSFrançois Tigeot if (!vm_bos)
5727dcf36dcSFrançois Tigeot return;
5737dcf36dcSFrançois Tigeot
5747dcf36dcSFrançois Tigeot r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
5757dcf36dcSFrançois Tigeot if (r)
5767dcf36dcSFrançois Tigeot goto error_free;
5777dcf36dcSFrançois Tigeot
5787dcf36dcSFrançois Tigeot list_for_each_entry(entry, &list, head) {
5797dcf36dcSFrançois Tigeot domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type);
5807dcf36dcSFrançois Tigeot /* if anything is swapped out don't swap it in here,
5817dcf36dcSFrançois Tigeot just abort and wait for the next CS */
5827dcf36dcSFrançois Tigeot if (domain == RADEON_GEM_DOMAIN_CPU)
5837dcf36dcSFrançois Tigeot goto error_unreserve;
5847dcf36dcSFrançois Tigeot }
5857dcf36dcSFrançois Tigeot
5867dcf36dcSFrançois Tigeot mutex_lock(&bo_va->vm->mutex);
5877dcf36dcSFrançois Tigeot r = radeon_vm_clear_freed(rdev, bo_va->vm);
5887dcf36dcSFrançois Tigeot if (r)
5897dcf36dcSFrançois Tigeot goto error_unlock;
5907dcf36dcSFrançois Tigeot
5917dcf36dcSFrançois Tigeot if (bo_va->it.start)
5927dcf36dcSFrançois Tigeot r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem);
5937dcf36dcSFrançois Tigeot
5947dcf36dcSFrançois Tigeot error_unlock:
5957dcf36dcSFrançois Tigeot mutex_unlock(&bo_va->vm->mutex);
5967dcf36dcSFrançois Tigeot
5977dcf36dcSFrançois Tigeot error_unreserve:
5987dcf36dcSFrançois Tigeot ttm_eu_backoff_reservation(&ticket, &list);
5997dcf36dcSFrançois Tigeot
6007dcf36dcSFrançois Tigeot error_free:
601*3f2dd94aSFrançois Tigeot kvfree(vm_bos);
6027dcf36dcSFrançois Tigeot
6037dcf36dcSFrançois Tigeot if (r && r != -ERESTARTSYS)
6047dcf36dcSFrançois Tigeot DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
6057dcf36dcSFrançois Tigeot }
6067dcf36dcSFrançois Tigeot
radeon_gem_va_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)607926deccbSFrançois Tigeot int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
608926deccbSFrançois Tigeot struct drm_file *filp)
609926deccbSFrançois Tigeot {
610926deccbSFrançois Tigeot struct drm_radeon_gem_va *args = data;
611926deccbSFrançois Tigeot struct drm_gem_object *gobj;
612926deccbSFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
613926deccbSFrançois Tigeot struct radeon_fpriv *fpriv = filp->driver_priv;
614926deccbSFrançois Tigeot struct radeon_bo *rbo;
615926deccbSFrançois Tigeot struct radeon_bo_va *bo_va;
616926deccbSFrançois Tigeot u32 invalid_flags;
617926deccbSFrançois Tigeot int r = 0;
618926deccbSFrançois Tigeot
619926deccbSFrançois Tigeot if (!rdev->vm_manager.enabled) {
620926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
621926deccbSFrançois Tigeot return -ENOTTY;
622926deccbSFrançois Tigeot }
623926deccbSFrançois Tigeot
624926deccbSFrançois Tigeot /* !! DONT REMOVE !!
625926deccbSFrançois Tigeot * We don't support vm_id yet, to be sure we don't have have broken
626926deccbSFrançois Tigeot * userspace, reject anyone trying to use non 0 value thus moving
627926deccbSFrançois Tigeot * forward we can use those fields without breaking existant userspace
628926deccbSFrançois Tigeot */
629926deccbSFrançois Tigeot if (args->vm_id) {
630926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
631926deccbSFrançois Tigeot return -EINVAL;
632926deccbSFrançois Tigeot }
633926deccbSFrançois Tigeot
634926deccbSFrançois Tigeot if (args->offset < RADEON_VA_RESERVED_SIZE) {
635fb572d17SFrançois Tigeot dev_err(&dev->pdev->dev,
636926deccbSFrançois Tigeot "offset 0x%lX is in reserved area 0x%X\n",
637926deccbSFrançois Tigeot (unsigned long)args->offset,
638926deccbSFrançois Tigeot RADEON_VA_RESERVED_SIZE);
639926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
640926deccbSFrançois Tigeot return -EINVAL;
641926deccbSFrançois Tigeot }
642926deccbSFrançois Tigeot
643926deccbSFrançois Tigeot /* don't remove, we need to enforce userspace to set the snooped flag
644926deccbSFrançois Tigeot * otherwise we will endup with broken userspace and we won't be able
645926deccbSFrançois Tigeot * to enable this feature without adding new interface
646926deccbSFrançois Tigeot */
647926deccbSFrançois Tigeot invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
648926deccbSFrançois Tigeot if ((args->flags & invalid_flags)) {
649fb572d17SFrançois Tigeot dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
650926deccbSFrançois Tigeot args->flags, invalid_flags);
651926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
652926deccbSFrançois Tigeot return -EINVAL;
653926deccbSFrançois Tigeot }
654926deccbSFrançois Tigeot
655926deccbSFrançois Tigeot switch (args->operation) {
656926deccbSFrançois Tigeot case RADEON_VA_MAP:
657926deccbSFrançois Tigeot case RADEON_VA_UNMAP:
658926deccbSFrançois Tigeot break;
659926deccbSFrançois Tigeot default:
660fb572d17SFrançois Tigeot dev_err(&dev->pdev->dev, "unsupported operation %d\n",
661926deccbSFrançois Tigeot args->operation);
662926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
663926deccbSFrançois Tigeot return -EINVAL;
664926deccbSFrançois Tigeot }
665926deccbSFrançois Tigeot
6668621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, args->handle);
667926deccbSFrançois Tigeot if (gobj == NULL) {
668926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
669926deccbSFrançois Tigeot return -ENOENT;
670926deccbSFrançois Tigeot }
671926deccbSFrançois Tigeot rbo = gem_to_radeon_bo(gobj);
672926deccbSFrançois Tigeot r = radeon_bo_reserve(rbo, false);
673926deccbSFrançois Tigeot if (r) {
674926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
675*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
676926deccbSFrançois Tigeot return r;
677926deccbSFrançois Tigeot }
678926deccbSFrançois Tigeot bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
679926deccbSFrançois Tigeot if (!bo_va) {
680926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
681d78d3a22SFrançois Tigeot radeon_bo_unreserve(rbo);
682*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
683926deccbSFrançois Tigeot return -ENOENT;
684926deccbSFrançois Tigeot }
685926deccbSFrançois Tigeot
686926deccbSFrançois Tigeot switch (args->operation) {
687926deccbSFrançois Tigeot case RADEON_VA_MAP:
6881cfef1a5SFrançois Tigeot if (bo_va->it.start) {
689926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_VA_EXIST;
6901cfef1a5SFrançois Tigeot args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
6911cfef1a5SFrançois Tigeot radeon_bo_unreserve(rbo);
692926deccbSFrançois Tigeot goto out;
693926deccbSFrançois Tigeot }
694926deccbSFrançois Tigeot r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
695926deccbSFrançois Tigeot break;
696926deccbSFrançois Tigeot case RADEON_VA_UNMAP:
697926deccbSFrançois Tigeot r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
698926deccbSFrançois Tigeot break;
699926deccbSFrançois Tigeot default:
700926deccbSFrançois Tigeot break;
701926deccbSFrançois Tigeot }
7027dcf36dcSFrançois Tigeot if (!r)
7037dcf36dcSFrançois Tigeot radeon_gem_va_update_vm(rdev, bo_va);
704926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_OK;
705926deccbSFrançois Tigeot if (r) {
706926deccbSFrançois Tigeot args->operation = RADEON_VA_RESULT_ERROR;
707926deccbSFrançois Tigeot }
708926deccbSFrançois Tigeot out:
709*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
710926deccbSFrançois Tigeot return r;
711926deccbSFrançois Tigeot }
712926deccbSFrançois Tigeot
radeon_gem_op_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)713c6f73aabSFrançois Tigeot int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
714c6f73aabSFrançois Tigeot struct drm_file *filp)
715c6f73aabSFrançois Tigeot {
716c6f73aabSFrançois Tigeot struct drm_radeon_gem_op *args = data;
717c6f73aabSFrançois Tigeot struct drm_gem_object *gobj;
718c6f73aabSFrançois Tigeot struct radeon_bo *robj;
719c6f73aabSFrançois Tigeot int r;
720c6f73aabSFrançois Tigeot
7218621f407SFrançois Tigeot gobj = drm_gem_object_lookup(filp, args->handle);
722c6f73aabSFrançois Tigeot if (gobj == NULL) {
723c6f73aabSFrançois Tigeot return -ENOENT;
724c6f73aabSFrançois Tigeot }
725c6f73aabSFrançois Tigeot robj = gem_to_radeon_bo(gobj);
726d78d3a22SFrançois Tigeot
727d78d3a22SFrançois Tigeot r = -EPERM;
728d78d3a22SFrançois Tigeot #if 0
729d78d3a22SFrançois Tigeot if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
730d78d3a22SFrançois Tigeot goto out;
731d78d3a22SFrançois Tigeot #endif
732d78d3a22SFrançois Tigeot
733c6f73aabSFrançois Tigeot r = radeon_bo_reserve(robj, false);
734c6f73aabSFrançois Tigeot if (unlikely(r))
735c6f73aabSFrançois Tigeot goto out;
736c6f73aabSFrançois Tigeot
737c6f73aabSFrançois Tigeot switch (args->op) {
738c6f73aabSFrançois Tigeot case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
739c6f73aabSFrançois Tigeot args->value = robj->initial_domain;
740c6f73aabSFrançois Tigeot break;
741c6f73aabSFrançois Tigeot case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
742c6f73aabSFrançois Tigeot robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
743c6f73aabSFrançois Tigeot RADEON_GEM_DOMAIN_GTT |
744c6f73aabSFrançois Tigeot RADEON_GEM_DOMAIN_CPU);
745c6f73aabSFrançois Tigeot break;
746c6f73aabSFrançois Tigeot default:
747c6f73aabSFrançois Tigeot r = -EINVAL;
748c6f73aabSFrançois Tigeot }
749c6f73aabSFrançois Tigeot
750c6f73aabSFrançois Tigeot radeon_bo_unreserve(robj);
751c6f73aabSFrançois Tigeot out:
752*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
753c6f73aabSFrançois Tigeot return r;
754c6f73aabSFrançois Tigeot }
755c6f73aabSFrançois Tigeot
radeon_mode_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)756926deccbSFrançois Tigeot int radeon_mode_dumb_create(struct drm_file *file_priv,
757926deccbSFrançois Tigeot struct drm_device *dev,
758926deccbSFrançois Tigeot struct drm_mode_create_dumb *args)
759926deccbSFrançois Tigeot {
760926deccbSFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
761926deccbSFrançois Tigeot struct drm_gem_object *gobj;
762926deccbSFrançois Tigeot uint32_t handle;
763926deccbSFrançois Tigeot int r;
764926deccbSFrançois Tigeot
765a85cb24fSFrançois Tigeot args->pitch = radeon_align_pitch(rdev, args->width,
766a85cb24fSFrançois Tigeot DIV_ROUND_UP(args->bpp, 8), 0);
767926deccbSFrançois Tigeot args->size = args->pitch * args->height;
768c4ef309bSzrj args->size = ALIGN(args->size, PAGE_SIZE);
769926deccbSFrançois Tigeot
770926deccbSFrançois Tigeot r = radeon_gem_object_create(rdev, args->size, 0,
771c6f73aabSFrançois Tigeot RADEON_GEM_DOMAIN_VRAM, 0,
772c6f73aabSFrançois Tigeot false, &gobj);
773926deccbSFrançois Tigeot if (r)
774926deccbSFrançois Tigeot return -ENOMEM;
775926deccbSFrançois Tigeot
776926deccbSFrançois Tigeot r = drm_gem_handle_create(file_priv, gobj, &handle);
777926deccbSFrançois Tigeot /* drop reference from allocate - handle holds it now */
778*3f2dd94aSFrançois Tigeot drm_gem_object_put_unlocked(gobj);
779926deccbSFrançois Tigeot if (r) {
780926deccbSFrançois Tigeot return r;
781926deccbSFrançois Tigeot }
782926deccbSFrançois Tigeot args->handle = handle;
783926deccbSFrançois Tigeot return 0;
784926deccbSFrançois Tigeot }
785926deccbSFrançois Tigeot
786f43cf1b1SMichael Neumann #if defined(CONFIG_DEBUG_FS)
radeon_debugfs_gem_info(struct seq_file * m,void * data)787f43cf1b1SMichael Neumann static int radeon_debugfs_gem_info(struct seq_file *m, void *data)
788f43cf1b1SMichael Neumann {
789f43cf1b1SMichael Neumann struct drm_info_node *node = (struct drm_info_node *)m->private;
790f43cf1b1SMichael Neumann struct drm_device *dev = node->minor->dev;
791f43cf1b1SMichael Neumann struct radeon_device *rdev = dev->dev_private;
792f43cf1b1SMichael Neumann struct radeon_bo *rbo;
793f43cf1b1SMichael Neumann unsigned i = 0;
794f43cf1b1SMichael Neumann
795f43cf1b1SMichael Neumann mutex_lock(&rdev->gem.mutex);
796f43cf1b1SMichael Neumann list_for_each_entry(rbo, &rdev->gem.objects, list) {
797f43cf1b1SMichael Neumann unsigned domain;
798f43cf1b1SMichael Neumann const char *placement;
799f43cf1b1SMichael Neumann
800f43cf1b1SMichael Neumann domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
801f43cf1b1SMichael Neumann switch (domain) {
802f43cf1b1SMichael Neumann case RADEON_GEM_DOMAIN_VRAM:
803f43cf1b1SMichael Neumann placement = "VRAM";
804f43cf1b1SMichael Neumann break;
805f43cf1b1SMichael Neumann case RADEON_GEM_DOMAIN_GTT:
806f43cf1b1SMichael Neumann placement = " GTT";
807f43cf1b1SMichael Neumann break;
808f43cf1b1SMichael Neumann case RADEON_GEM_DOMAIN_CPU:
809f43cf1b1SMichael Neumann default:
810f43cf1b1SMichael Neumann placement = " CPU";
811f43cf1b1SMichael Neumann break;
812f43cf1b1SMichael Neumann }
813f43cf1b1SMichael Neumann seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
814f43cf1b1SMichael Neumann i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
815f43cf1b1SMichael Neumann placement, (unsigned long)rbo->pid);
816f43cf1b1SMichael Neumann i++;
817f43cf1b1SMichael Neumann }
818f43cf1b1SMichael Neumann mutex_unlock(&rdev->gem.mutex);
819f43cf1b1SMichael Neumann return 0;
820f43cf1b1SMichael Neumann }
821f43cf1b1SMichael Neumann
822f43cf1b1SMichael Neumann static struct drm_info_list radeon_debugfs_gem_list[] = {
823f43cf1b1SMichael Neumann {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
824f43cf1b1SMichael Neumann };
825f43cf1b1SMichael Neumann #endif
826f43cf1b1SMichael Neumann
radeon_gem_debugfs_init(struct radeon_device * rdev)827f43cf1b1SMichael Neumann int radeon_gem_debugfs_init(struct radeon_device *rdev)
828f43cf1b1SMichael Neumann {
829f43cf1b1SMichael Neumann #if defined(CONFIG_DEBUG_FS)
830f43cf1b1SMichael Neumann return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
831f43cf1b1SMichael Neumann #endif
832f43cf1b1SMichael Neumann return 0;
833f43cf1b1SMichael Neumann }
834