blob: cfad667ce78501dc62a7893726b2ccdb112ae2f5 [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 */
28#include "drmP.h"
29#include "drm.h"
30#include "radeon_drm.h"
31#include "radeon.h"
32
33int radeon_gem_object_init(struct drm_gem_object *obj)
34{
Daniel Vetter441921d2011-02-18 17:59:16 +010035 BUG();
36
Jerome Glisse771fe6b2009-06-05 14:42:42 +020037 return 0;
38}
39
40void radeon_gem_object_free(struct drm_gem_object *gobj)
41{
Daniel Vetter7e4d15d2011-02-18 17:59:17 +010042 struct radeon_bo *robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020043
Jerome Glisse771fe6b2009-06-05 14:42:42 +020044 if (robj) {
Alex Deucher40f5cf92012-05-10 18:33:13 -040045 if (robj->gem_base.import_attach)
46 drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
Jerome Glisse4c788672009-11-20 14:29:23 +010047 radeon_bo_unref(&robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020048 }
49}
50
51int radeon_gem_object_create(struct radeon_device *rdev, int size,
Jerome Glisse4c788672009-11-20 14:29:23 +010052 int alignment, int initial_domain,
53 bool discardable, bool kernel,
54 struct drm_gem_object **obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020055{
Jerome Glisse4c788672009-11-20 14:29:23 +010056 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020057 int r;
58
59 *obj = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020060 /* At least align on page size */
61 if (alignment < PAGE_SIZE) {
62 alignment = PAGE_SIZE;
63 }
Alex Deucher40f5cf92012-05-10 18:33:13 -040064 r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain, NULL, &robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020065 if (r) {
Dave Airlieecabd322009-12-15 10:39:48 +100066 if (r != -ERESTARTSYS)
67 DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n",
68 size, initial_domain, alignment, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020069 return r;
70 }
Daniel Vetter441921d2011-02-18 17:59:16 +010071 *obj = &robj->gem_base;
72
73 mutex_lock(&rdev->gem.mutex);
74 list_add_tail(&robj->list, &rdev->gem.objects);
75 mutex_unlock(&rdev->gem.mutex);
76
Jerome Glisse771fe6b2009-06-05 14:42:42 +020077 return 0;
78}
79
Jerome Glisse771fe6b2009-06-05 14:42:42 +020080int radeon_gem_set_domain(struct drm_gem_object *gobj,
81 uint32_t rdomain, uint32_t wdomain)
82{
Jerome Glisse4c788672009-11-20 14:29:23 +010083 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020084 uint32_t domain;
85 int r;
86
87 /* FIXME: reeimplement */
Daniel Vetter7e4d15d2011-02-18 17:59:17 +010088 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020089 /* work out where to validate the buffer to */
90 domain = wdomain;
91 if (!domain) {
92 domain = rdomain;
93 }
94 if (!domain) {
95 /* Do nothings */
Masanari Iidab6cafa22012-02-27 23:28:38 +090096 printk(KERN_WARNING "Set domain without domain !\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +020097 return 0;
98 }
99 if (domain == RADEON_GEM_DOMAIN_CPU) {
100 /* Asking for cpu access wait for object idle */
Jerome Glisse4c788672009-11-20 14:29:23 +0100101 r = radeon_bo_wait(robj, NULL, false);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200102 if (r) {
103 printk(KERN_ERR "Failed to wait for object !\n");
104 return r;
105 }
106 }
107 return 0;
108}
109
110int radeon_gem_init(struct radeon_device *rdev)
111{
112 INIT_LIST_HEAD(&rdev->gem.objects);
113 return 0;
114}
115
116void radeon_gem_fini(struct radeon_device *rdev)
117{
Jerome Glisse4c788672009-11-20 14:29:23 +0100118 radeon_bo_force_delete(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119}
120
Jerome Glisse721604a2012-01-05 22:11:05 -0500121/*
122 * Call from drm_gem_handle_create which appear in both new and open ioctl
123 * case.
124 */
125int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
126{
127 return 0;
128}
129
130void radeon_gem_object_close(struct drm_gem_object *obj,
131 struct drm_file *file_priv)
132{
133 struct radeon_bo *rbo = gem_to_radeon_bo(obj);
134 struct radeon_device *rdev = rbo->rdev;
135 struct radeon_fpriv *fpriv = file_priv->driver_priv;
136 struct radeon_vm *vm = &fpriv->vm;
Christian Königd59f7022012-09-11 16:10:02 +0200137 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500138
139 if (rdev->family < CHIP_CAYMAN) {
140 return;
141 }
142
Christian Königd59f7022012-09-11 16:10:02 +0200143 r = radeon_bo_reserve(rbo, true);
144 if (r) {
145 dev_err(rdev->dev, "leaking bo va because "
146 "we fail to reserve bo (%d)\n", r);
Jerome Glisse721604a2012-01-05 22:11:05 -0500147 return;
148 }
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400149 radeon_vm_bo_rmv(rdev, vm, rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500150 radeon_bo_unreserve(rbo);
151}
152
Christian König6c6f4782012-05-02 15:11:19 +0200153static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
154{
155 if (r == -EDEADLK) {
Christian König6c6f4782012-05-02 15:11:19 +0200156 r = radeon_gpu_reset(rdev);
157 if (!r)
158 r = -EAGAIN;
Christian König6c6f4782012-05-02 15:11:19 +0200159 }
160 return r;
161}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162
163/*
164 * GEM ioctls.
165 */
166int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
167 struct drm_file *filp)
168{
169 struct radeon_device *rdev = dev->dev_private;
170 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000171 struct ttm_mem_type_manager *man;
Christian Königbf852792011-10-13 13:19:22 +0200172 unsigned i;
Dave Airlie53595332011-03-14 09:47:24 +1000173
174 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175
Dave Airlie7a50f012009-07-21 20:39:30 +1000176 args->vram_size = rdev->mc.real_vram_size;
Dave Airlie53595332011-03-14 09:47:24 +1000177 args->vram_visible = (u64)man->size << PAGE_SHIFT;
Michel Dänzer38e14922009-08-05 00:19:51 +0200178 if (rdev->stollen_vga_memory)
Jerome Glisse4c788672009-11-20 14:29:23 +0100179 args->vram_visible -= radeon_bo_size(rdev->stollen_vga_memory);
Dave Airlie38651672010-03-30 05:34:13 +0000180 args->vram_visible -= radeon_fbdev_total_size(rdev);
Christian König7b1f2482011-09-23 15:11:23 +0200181 args->gart_size = rdev->mc.gtt_size - 4096 - RADEON_IB_POOL_SIZE*64*1024;
Christian Königbf852792011-10-13 13:19:22 +0200182 for(i = 0; i < RADEON_NUM_RINGS; ++i)
Christian Könige32eb502011-10-23 12:56:27 +0200183 args->gart_size -= rdev->ring[i].ring_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200184 return 0;
185}
186
187int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
188 struct drm_file *filp)
189{
190 /* TODO: implement */
191 DRM_ERROR("unimplemented %s\n", __func__);
192 return -ENOSYS;
193}
194
195int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
196 struct drm_file *filp)
197{
198 /* TODO: implement */
199 DRM_ERROR("unimplemented %s\n", __func__);
200 return -ENOSYS;
201}
202
203int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
204 struct drm_file *filp)
205{
206 struct radeon_device *rdev = dev->dev_private;
207 struct drm_radeon_gem_create *args = data;
208 struct drm_gem_object *gobj;
209 uint32_t handle;
210 int r;
211
Jerome Glissedee53e72012-07-02 12:45:19 -0400212 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200213 /* create a gem object to contain this object in */
214 args->size = roundup(args->size, PAGE_SIZE);
215 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Jerome Glisse4c788672009-11-20 14:29:23 +0100216 args->initial_domain, false,
217 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200218 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400219 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200220 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200221 return r;
222 }
223 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000224 /* drop reference from allocate - handle holds it now */
225 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200226 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400227 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200228 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200229 return r;
230 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200231 args->handle = handle;
Jerome Glissedee53e72012-07-02 12:45:19 -0400232 up_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200233 return 0;
234}
235
236int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
237 struct drm_file *filp)
238{
239 /* transition the BO to a domain -
240 * just validate the BO into a certain domain */
Jerome Glissedee53e72012-07-02 12:45:19 -0400241 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200242 struct drm_radeon_gem_set_domain *args = data;
243 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100244 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200245 int r;
246
247 /* for now if someone requests domain CPU -
248 * just make sure the buffer is finished with */
Jerome Glissedee53e72012-07-02 12:45:19 -0400249 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200250
251 /* just do a BO wait for now */
252 gobj = drm_gem_object_lookup(dev, filp, args->handle);
253 if (gobj == NULL) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400254 up_read(&rdev->exclusive_lock);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100255 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200256 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100257 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200258
259 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
260
Luca Barbieribc9025b2010-02-09 05:49:12 +0000261 drm_gem_object_unreference_unlocked(gobj);
Jerome Glissedee53e72012-07-02 12:45:19 -0400262 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200263 r = radeon_gem_handle_lockup(robj->rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200264 return r;
265}
266
Dave Airlieff72145b2011-02-07 12:16:14 +1000267int radeon_mode_dumb_mmap(struct drm_file *filp,
268 struct drm_device *dev,
269 uint32_t handle, uint64_t *offset_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200271 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100272 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273
Dave Airlieff72145b2011-02-07 12:16:14 +1000274 gobj = drm_gem_object_lookup(dev, filp, handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200275 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100276 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200277 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100278 robj = gem_to_radeon_bo(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000279 *offset_p = radeon_bo_mmap_offset(robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000280 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100281 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200282}
283
Dave Airlieff72145b2011-02-07 12:16:14 +1000284int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
285 struct drm_file *filp)
286{
287 struct drm_radeon_gem_mmap *args = data;
288
289 return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
290}
291
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200292int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
293 struct drm_file *filp)
294{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400295 struct radeon_device *rdev = dev->dev_private;
Dave Airliecefb87e2009-08-16 21:05:45 +1000296 struct drm_radeon_gem_busy *args = data;
297 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100298 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000299 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000300 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000301
302 gobj = drm_gem_object_lookup(dev, filp, args->handle);
303 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100304 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000305 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100306 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100307 r = radeon_bo_wait(robj, &cur_placement, true);
Michel Dänzer9f844e52009-08-22 17:38:23 +0200308 switch (cur_placement) {
309 case TTM_PL_VRAM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000310 args->domain = RADEON_GEM_DOMAIN_VRAM;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200311 break;
312 case TTM_PL_TT:
Dave Airliecefb87e2009-08-16 21:05:45 +1000313 args->domain = RADEON_GEM_DOMAIN_GTT;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200314 break;
315 case TTM_PL_SYSTEM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000316 args->domain = RADEON_GEM_DOMAIN_CPU;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200317 default:
318 break;
319 }
Luca Barbieribc9025b2010-02-09 05:49:12 +0000320 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400321 r = radeon_gem_handle_lockup(rdev, r);
Dave Airliee3b24152009-08-21 09:47:45 +1000322 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200323}
324
325int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
326 struct drm_file *filp)
327{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400328 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200329 struct drm_radeon_gem_wait_idle *args = data;
330 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100331 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200332 int r;
333
334 gobj = drm_gem_object_lookup(dev, filp, args->handle);
335 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100336 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200337 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100338 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100339 r = radeon_bo_wait(robj, NULL, false);
Jerome Glisse062b3892010-02-04 20:36:39 +0100340 /* callback hw specific functions if any */
Jerome Glisse1ef53252012-07-02 12:40:54 -0400341 if (rdev->asic->ioctl_wait_idle)
342 robj->rdev->asic->ioctl_wait_idle(rdev, robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000343 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400344 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200345 return r;
346}
Dave Airliee024e112009-06-24 09:48:08 +1000347
348int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
349 struct drm_file *filp)
350{
351 struct drm_radeon_gem_set_tiling *args = data;
352 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100353 struct radeon_bo *robj;
Dave Airliee024e112009-06-24 09:48:08 +1000354 int r = 0;
355
356 DRM_DEBUG("%d \n", args->handle);
357 gobj = drm_gem_object_lookup(dev, filp, args->handle);
358 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100359 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100360 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100361 r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000362 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000363 return r;
364}
365
366int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
367 struct drm_file *filp)
368{
369 struct drm_radeon_gem_get_tiling *args = data;
370 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100371 struct radeon_bo *rbo;
Dave Airliee024e112009-06-24 09:48:08 +1000372 int r = 0;
373
374 DRM_DEBUG("\n");
375 gobj = drm_gem_object_lookup(dev, filp, args->handle);
376 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100377 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100378 rbo = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100379 r = radeon_bo_reserve(rbo, false);
380 if (unlikely(r != 0))
Dave Airlie51f07b72009-12-16 13:10:43 +1000381 goto out;
Jerome Glisse4c788672009-11-20 14:29:23 +0100382 radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
383 radeon_bo_unreserve(rbo);
Dave Airlie51f07b72009-12-16 13:10:43 +1000384out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000385 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000386 return r;
387}
Dave Airlieff72145b2011-02-07 12:16:14 +1000388
Jerome Glisse721604a2012-01-05 22:11:05 -0500389int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
390 struct drm_file *filp)
391{
392 struct drm_radeon_gem_va *args = data;
393 struct drm_gem_object *gobj;
394 struct radeon_device *rdev = dev->dev_private;
395 struct radeon_fpriv *fpriv = filp->driver_priv;
396 struct radeon_bo *rbo;
397 struct radeon_bo_va *bo_va;
398 u32 invalid_flags;
399 int r = 0;
400
Alex Deucher67e915e2012-01-06 09:38:15 -0500401 if (!rdev->vm_manager.enabled) {
402 args->operation = RADEON_VA_RESULT_ERROR;
403 return -ENOTTY;
404 }
405
Jerome Glisse721604a2012-01-05 22:11:05 -0500406 /* !! DONT REMOVE !!
407 * We don't support vm_id yet, to be sure we don't have have broken
408 * userspace, reject anyone trying to use non 0 value thus moving
409 * forward we can use those fields without breaking existant userspace
410 */
411 if (args->vm_id) {
412 args->operation = RADEON_VA_RESULT_ERROR;
413 return -EINVAL;
414 }
415
416 if (args->offset < RADEON_VA_RESERVED_SIZE) {
417 dev_err(&dev->pdev->dev,
418 "offset 0x%lX is in reserved area 0x%X\n",
419 (unsigned long)args->offset,
420 RADEON_VA_RESERVED_SIZE);
421 args->operation = RADEON_VA_RESULT_ERROR;
422 return -EINVAL;
423 }
424
425 /* don't remove, we need to enforce userspace to set the snooped flag
426 * otherwise we will endup with broken userspace and we won't be able
427 * to enable this feature without adding new interface
428 */
429 invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
430 if ((args->flags & invalid_flags)) {
431 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
432 args->flags, invalid_flags);
433 args->operation = RADEON_VA_RESULT_ERROR;
434 return -EINVAL;
435 }
436 if (!(args->flags & RADEON_VM_PAGE_SNOOPED)) {
437 dev_err(&dev->pdev->dev, "only supported snooped mapping for now\n");
438 args->operation = RADEON_VA_RESULT_ERROR;
439 return -EINVAL;
440 }
441
442 switch (args->operation) {
443 case RADEON_VA_MAP:
444 case RADEON_VA_UNMAP:
445 break;
446 default:
447 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
448 args->operation);
449 args->operation = RADEON_VA_RESULT_ERROR;
450 return -EINVAL;
451 }
452
453 gobj = drm_gem_object_lookup(dev, filp, args->handle);
454 if (gobj == NULL) {
455 args->operation = RADEON_VA_RESULT_ERROR;
456 return -ENOENT;
457 }
458 rbo = gem_to_radeon_bo(gobj);
459 r = radeon_bo_reserve(rbo, false);
460 if (r) {
461 args->operation = RADEON_VA_RESULT_ERROR;
462 drm_gem_object_unreference_unlocked(gobj);
463 return r;
464 }
465 switch (args->operation) {
466 case RADEON_VA_MAP:
Christian König421ca7a2012-09-11 16:10:00 +0200467 bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500468 if (bo_va) {
469 args->operation = RADEON_VA_RESULT_VA_EXIST;
470 args->offset = bo_va->soffset;
471 goto out;
472 }
473 r = radeon_vm_bo_add(rdev, &fpriv->vm, rbo,
474 args->offset, args->flags);
475 break;
476 case RADEON_VA_UNMAP:
477 r = radeon_vm_bo_rmv(rdev, &fpriv->vm, rbo);
478 break;
479 default:
480 break;
481 }
482 args->operation = RADEON_VA_RESULT_OK;
483 if (r) {
484 args->operation = RADEON_VA_RESULT_ERROR;
485 }
486out:
487 radeon_bo_unreserve(rbo);
488 drm_gem_object_unreference_unlocked(gobj);
489 return r;
490}
491
Dave Airlieff72145b2011-02-07 12:16:14 +1000492int radeon_mode_dumb_create(struct drm_file *file_priv,
493 struct drm_device *dev,
494 struct drm_mode_create_dumb *args)
495{
496 struct radeon_device *rdev = dev->dev_private;
497 struct drm_gem_object *gobj;
Dave Airliec87a8d82011-03-17 13:58:34 +1000498 uint32_t handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000499 int r;
500
501 args->pitch = radeon_align_pitch(rdev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
502 args->size = args->pitch * args->height;
503 args->size = ALIGN(args->size, PAGE_SIZE);
504
505 r = radeon_gem_object_create(rdev, args->size, 0,
506 RADEON_GEM_DOMAIN_VRAM,
507 false, ttm_bo_type_device,
508 &gobj);
509 if (r)
510 return -ENOMEM;
511
Dave Airliec87a8d82011-03-17 13:58:34 +1000512 r = drm_gem_handle_create(file_priv, gobj, &handle);
513 /* drop reference from allocate - handle holds it now */
514 drm_gem_object_unreference_unlocked(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000515 if (r) {
Dave Airlieff72145b2011-02-07 12:16:14 +1000516 return r;
517 }
Dave Airliec87a8d82011-03-17 13:58:34 +1000518 args->handle = handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000519 return 0;
520}
521
522int radeon_mode_dumb_destroy(struct drm_file *file_priv,
523 struct drm_device *dev,
524 uint32_t handle)
525{
526 return drm_gem_handle_delete(file_priv, handle);
527}