blob: 1b57b0058ad642e6d0d610dadbf7af235f517c71 [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;
Jerome Glisse721604a2012-01-05 22:11:05 -0500137
138 if (rdev->family < CHIP_CAYMAN) {
139 return;
140 }
141
142 if (radeon_bo_reserve(rbo, false)) {
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400143 dev_err(rdev->dev, "leaking bo va because we fail to reserve bo\n");
Jerome Glisse721604a2012-01-05 22:11:05 -0500144 return;
145 }
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400146 radeon_vm_bo_rmv(rdev, vm, rbo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500147 radeon_bo_unreserve(rbo);
148}
149
Christian König6c6f4782012-05-02 15:11:19 +0200150static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
151{
152 if (r == -EDEADLK) {
Christian König6c6f4782012-05-02 15:11:19 +0200153 r = radeon_gpu_reset(rdev);
154 if (!r)
155 r = -EAGAIN;
Christian König6c6f4782012-05-02 15:11:19 +0200156 }
157 return r;
158}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200159
160/*
161 * GEM ioctls.
162 */
163int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
164 struct drm_file *filp)
165{
166 struct radeon_device *rdev = dev->dev_private;
167 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000168 struct ttm_mem_type_manager *man;
Christian Königbf852792011-10-13 13:19:22 +0200169 unsigned i;
Dave Airlie53595332011-03-14 09:47:24 +1000170
171 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172
Dave Airlie7a50f012009-07-21 20:39:30 +1000173 args->vram_size = rdev->mc.real_vram_size;
Dave Airlie53595332011-03-14 09:47:24 +1000174 args->vram_visible = (u64)man->size << PAGE_SHIFT;
Michel Dänzer38e14922009-08-05 00:19:51 +0200175 if (rdev->stollen_vga_memory)
Jerome Glisse4c788672009-11-20 14:29:23 +0100176 args->vram_visible -= radeon_bo_size(rdev->stollen_vga_memory);
Dave Airlie38651672010-03-30 05:34:13 +0000177 args->vram_visible -= radeon_fbdev_total_size(rdev);
Christian König7b1f2482011-09-23 15:11:23 +0200178 args->gart_size = rdev->mc.gtt_size - 4096 - RADEON_IB_POOL_SIZE*64*1024;
Christian Königbf852792011-10-13 13:19:22 +0200179 for(i = 0; i < RADEON_NUM_RINGS; ++i)
Christian Könige32eb502011-10-23 12:56:27 +0200180 args->gart_size -= rdev->ring[i].ring_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200181 return 0;
182}
183
184int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
185 struct drm_file *filp)
186{
187 /* TODO: implement */
188 DRM_ERROR("unimplemented %s\n", __func__);
189 return -ENOSYS;
190}
191
192int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
193 struct drm_file *filp)
194{
195 /* TODO: implement */
196 DRM_ERROR("unimplemented %s\n", __func__);
197 return -ENOSYS;
198}
199
200int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
201 struct drm_file *filp)
202{
203 struct radeon_device *rdev = dev->dev_private;
204 struct drm_radeon_gem_create *args = data;
205 struct drm_gem_object *gobj;
206 uint32_t handle;
207 int r;
208
Jerome Glissedee53e72012-07-02 12:45:19 -0400209 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200210 /* create a gem object to contain this object in */
211 args->size = roundup(args->size, PAGE_SIZE);
212 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Jerome Glisse4c788672009-11-20 14:29:23 +0100213 args->initial_domain, false,
214 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200215 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400216 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200217 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200218 return r;
219 }
220 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000221 /* drop reference from allocate - handle holds it now */
222 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200223 if (r) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400224 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200225 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200226 return r;
227 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200228 args->handle = handle;
Jerome Glissedee53e72012-07-02 12:45:19 -0400229 up_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230 return 0;
231}
232
233int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
234 struct drm_file *filp)
235{
236 /* transition the BO to a domain -
237 * just validate the BO into a certain domain */
Jerome Glissedee53e72012-07-02 12:45:19 -0400238 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200239 struct drm_radeon_gem_set_domain *args = data;
240 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100241 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200242 int r;
243
244 /* for now if someone requests domain CPU -
245 * just make sure the buffer is finished with */
Jerome Glissedee53e72012-07-02 12:45:19 -0400246 down_read(&rdev->exclusive_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200247
248 /* just do a BO wait for now */
249 gobj = drm_gem_object_lookup(dev, filp, args->handle);
250 if (gobj == NULL) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400251 up_read(&rdev->exclusive_lock);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100252 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200253 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100254 robj = gem_to_radeon_bo(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200255
256 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
257
Luca Barbieribc9025b2010-02-09 05:49:12 +0000258 drm_gem_object_unreference_unlocked(gobj);
Jerome Glissedee53e72012-07-02 12:45:19 -0400259 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200260 r = radeon_gem_handle_lockup(robj->rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200261 return r;
262}
263
Dave Airlieff72145b2011-02-07 12:16:14 +1000264int radeon_mode_dumb_mmap(struct drm_file *filp,
265 struct drm_device *dev,
266 uint32_t handle, uint64_t *offset_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200267{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200268 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100269 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270
Dave Airlieff72145b2011-02-07 12:16:14 +1000271 gobj = drm_gem_object_lookup(dev, filp, handle);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200272 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100273 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200274 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100275 robj = gem_to_radeon_bo(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000276 *offset_p = radeon_bo_mmap_offset(robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000277 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100278 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200279}
280
Dave Airlieff72145b2011-02-07 12:16:14 +1000281int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
282 struct drm_file *filp)
283{
284 struct drm_radeon_gem_mmap *args = data;
285
286 return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
287}
288
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200289int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
290 struct drm_file *filp)
291{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400292 struct radeon_device *rdev = dev->dev_private;
Dave Airliecefb87e2009-08-16 21:05:45 +1000293 struct drm_radeon_gem_busy *args = data;
294 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100295 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000296 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000297 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000298
299 gobj = drm_gem_object_lookup(dev, filp, args->handle);
300 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100301 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000302 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100303 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100304 r = radeon_bo_wait(robj, &cur_placement, true);
Michel Dänzer9f844e52009-08-22 17:38:23 +0200305 switch (cur_placement) {
306 case TTM_PL_VRAM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000307 args->domain = RADEON_GEM_DOMAIN_VRAM;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200308 break;
309 case TTM_PL_TT:
Dave Airliecefb87e2009-08-16 21:05:45 +1000310 args->domain = RADEON_GEM_DOMAIN_GTT;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200311 break;
312 case TTM_PL_SYSTEM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000313 args->domain = RADEON_GEM_DOMAIN_CPU;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200314 default:
315 break;
316 }
Luca Barbieribc9025b2010-02-09 05:49:12 +0000317 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400318 r = radeon_gem_handle_lockup(rdev, r);
Dave Airliee3b24152009-08-21 09:47:45 +1000319 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200320}
321
322int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
323 struct drm_file *filp)
324{
Jerome Glisse1ef53252012-07-02 12:40:54 -0400325 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200326 struct drm_radeon_gem_wait_idle *args = data;
327 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100328 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200329 int r;
330
331 gobj = drm_gem_object_lookup(dev, filp, args->handle);
332 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100333 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200334 }
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100335 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100336 r = radeon_bo_wait(robj, NULL, false);
Jerome Glisse062b3892010-02-04 20:36:39 +0100337 /* callback hw specific functions if any */
Jerome Glisse1ef53252012-07-02 12:40:54 -0400338 if (rdev->asic->ioctl_wait_idle)
339 robj->rdev->asic->ioctl_wait_idle(rdev, robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000340 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse1ef53252012-07-02 12:40:54 -0400341 r = radeon_gem_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200342 return r;
343}
Dave Airliee024e112009-06-24 09:48:08 +1000344
345int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
346 struct drm_file *filp)
347{
348 struct drm_radeon_gem_set_tiling *args = data;
349 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100350 struct radeon_bo *robj;
Dave Airliee024e112009-06-24 09:48:08 +1000351 int r = 0;
352
353 DRM_DEBUG("%d \n", args->handle);
354 gobj = drm_gem_object_lookup(dev, filp, args->handle);
355 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100356 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100357 robj = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100358 r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000359 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000360 return r;
361}
362
363int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
364 struct drm_file *filp)
365{
366 struct drm_radeon_gem_get_tiling *args = data;
367 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100368 struct radeon_bo *rbo;
Dave Airliee024e112009-06-24 09:48:08 +1000369 int r = 0;
370
371 DRM_DEBUG("\n");
372 gobj = drm_gem_object_lookup(dev, filp, args->handle);
373 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100374 return -ENOENT;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100375 rbo = gem_to_radeon_bo(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100376 r = radeon_bo_reserve(rbo, false);
377 if (unlikely(r != 0))
Dave Airlie51f07b72009-12-16 13:10:43 +1000378 goto out;
Jerome Glisse4c788672009-11-20 14:29:23 +0100379 radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
380 radeon_bo_unreserve(rbo);
Dave Airlie51f07b72009-12-16 13:10:43 +1000381out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000382 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000383 return r;
384}
Dave Airlieff72145b2011-02-07 12:16:14 +1000385
Jerome Glisse721604a2012-01-05 22:11:05 -0500386int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
387 struct drm_file *filp)
388{
389 struct drm_radeon_gem_va *args = data;
390 struct drm_gem_object *gobj;
391 struct radeon_device *rdev = dev->dev_private;
392 struct radeon_fpriv *fpriv = filp->driver_priv;
393 struct radeon_bo *rbo;
394 struct radeon_bo_va *bo_va;
395 u32 invalid_flags;
396 int r = 0;
397
Alex Deucher67e915e2012-01-06 09:38:15 -0500398 if (!rdev->vm_manager.enabled) {
399 args->operation = RADEON_VA_RESULT_ERROR;
400 return -ENOTTY;
401 }
402
Jerome Glisse721604a2012-01-05 22:11:05 -0500403 /* !! DONT REMOVE !!
404 * We don't support vm_id yet, to be sure we don't have have broken
405 * userspace, reject anyone trying to use non 0 value thus moving
406 * forward we can use those fields without breaking existant userspace
407 */
408 if (args->vm_id) {
409 args->operation = RADEON_VA_RESULT_ERROR;
410 return -EINVAL;
411 }
412
413 if (args->offset < RADEON_VA_RESERVED_SIZE) {
414 dev_err(&dev->pdev->dev,
415 "offset 0x%lX is in reserved area 0x%X\n",
416 (unsigned long)args->offset,
417 RADEON_VA_RESERVED_SIZE);
418 args->operation = RADEON_VA_RESULT_ERROR;
419 return -EINVAL;
420 }
421
422 /* don't remove, we need to enforce userspace to set the snooped flag
423 * otherwise we will endup with broken userspace and we won't be able
424 * to enable this feature without adding new interface
425 */
426 invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
427 if ((args->flags & invalid_flags)) {
428 dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
429 args->flags, invalid_flags);
430 args->operation = RADEON_VA_RESULT_ERROR;
431 return -EINVAL;
432 }
433 if (!(args->flags & RADEON_VM_PAGE_SNOOPED)) {
434 dev_err(&dev->pdev->dev, "only supported snooped mapping for now\n");
435 args->operation = RADEON_VA_RESULT_ERROR;
436 return -EINVAL;
437 }
438
439 switch (args->operation) {
440 case RADEON_VA_MAP:
441 case RADEON_VA_UNMAP:
442 break;
443 default:
444 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
445 args->operation);
446 args->operation = RADEON_VA_RESULT_ERROR;
447 return -EINVAL;
448 }
449
450 gobj = drm_gem_object_lookup(dev, filp, args->handle);
451 if (gobj == NULL) {
452 args->operation = RADEON_VA_RESULT_ERROR;
453 return -ENOENT;
454 }
455 rbo = gem_to_radeon_bo(gobj);
456 r = radeon_bo_reserve(rbo, false);
457 if (r) {
458 args->operation = RADEON_VA_RESULT_ERROR;
459 drm_gem_object_unreference_unlocked(gobj);
460 return r;
461 }
462 switch (args->operation) {
463 case RADEON_VA_MAP:
464 bo_va = radeon_bo_va(rbo, &fpriv->vm);
465 if (bo_va) {
466 args->operation = RADEON_VA_RESULT_VA_EXIST;
467 args->offset = bo_va->soffset;
468 goto out;
469 }
470 r = radeon_vm_bo_add(rdev, &fpriv->vm, rbo,
471 args->offset, args->flags);
472 break;
473 case RADEON_VA_UNMAP:
474 r = radeon_vm_bo_rmv(rdev, &fpriv->vm, rbo);
475 break;
476 default:
477 break;
478 }
479 args->operation = RADEON_VA_RESULT_OK;
480 if (r) {
481 args->operation = RADEON_VA_RESULT_ERROR;
482 }
483out:
484 radeon_bo_unreserve(rbo);
485 drm_gem_object_unreference_unlocked(gobj);
486 return r;
487}
488
Dave Airlieff72145b2011-02-07 12:16:14 +1000489int radeon_mode_dumb_create(struct drm_file *file_priv,
490 struct drm_device *dev,
491 struct drm_mode_create_dumb *args)
492{
493 struct radeon_device *rdev = dev->dev_private;
494 struct drm_gem_object *gobj;
Dave Airliec87a8d82011-03-17 13:58:34 +1000495 uint32_t handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000496 int r;
497
498 args->pitch = radeon_align_pitch(rdev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
499 args->size = args->pitch * args->height;
500 args->size = ALIGN(args->size, PAGE_SIZE);
501
502 r = radeon_gem_object_create(rdev, args->size, 0,
503 RADEON_GEM_DOMAIN_VRAM,
504 false, ttm_bo_type_device,
505 &gobj);
506 if (r)
507 return -ENOMEM;
508
Dave Airliec87a8d82011-03-17 13:58:34 +1000509 r = drm_gem_handle_create(file_priv, gobj, &handle);
510 /* drop reference from allocate - handle holds it now */
511 drm_gem_object_unreference_unlocked(gobj);
Dave Airlieff72145b2011-02-07 12:16:14 +1000512 if (r) {
Dave Airlieff72145b2011-02-07 12:16:14 +1000513 return r;
514 }
Dave Airliec87a8d82011-03-17 13:58:34 +1000515 args->handle = handle;
Dave Airlieff72145b2011-02-07 12:16:14 +1000516 return 0;
517}
518
519int radeon_mode_dumb_destroy(struct drm_file *file_priv,
520 struct drm_device *dev,
521 uint32_t handle)
522{
523 return drm_gem_handle_delete(file_priv, handle);
524}