blob: 1fe95dfe48c9a130fae104da04a3af4b964614a2 [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{
35 /* we do nothings here */
36 return 0;
37}
38
39void radeon_gem_object_free(struct drm_gem_object *gobj)
40{
Jerome Glisse4c788672009-11-20 14:29:23 +010041 struct radeon_bo *robj = gobj->driver_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020042
43 gobj->driver_private = NULL;
44 if (robj) {
Jerome Glisse4c788672009-11-20 14:29:23 +010045 radeon_bo_unref(&robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020046 }
Daniel Vetterfd632aa2010-04-09 19:05:05 +000047
48 drm_gem_object_release(gobj);
49 kfree(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020050}
51
52int radeon_gem_object_create(struct radeon_device *rdev, int size,
Jerome Glisse4c788672009-11-20 14:29:23 +010053 int alignment, int initial_domain,
54 bool discardable, bool kernel,
55 struct drm_gem_object **obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020056{
57 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +010058 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020059 int r;
60
61 *obj = NULL;
62 gobj = drm_gem_object_alloc(rdev->ddev, size);
63 if (!gobj) {
64 return -ENOMEM;
65 }
66 /* At least align on page size */
67 if (alignment < PAGE_SIZE) {
68 alignment = PAGE_SIZE;
69 }
Alex Deucher268b2512010-11-17 19:00:26 -050070 r = radeon_bo_create(rdev, gobj, size, alignment, kernel, initial_domain, &robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020071 if (r) {
Dave Airlieecabd322009-12-15 10:39:48 +100072 if (r != -ERESTARTSYS)
73 DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n",
74 size, initial_domain, alignment, r);
Luca Barbieribc9025b2010-02-09 05:49:12 +000075 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020076 return r;
77 }
78 gobj->driver_private = robj;
79 *obj = gobj;
80 return 0;
81}
82
83int radeon_gem_object_pin(struct drm_gem_object *obj, uint32_t pin_domain,
84 uint64_t *gpu_addr)
85{
Jerome Glisse4c788672009-11-20 14:29:23 +010086 struct radeon_bo *robj = obj->driver_private;
87 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020088
Jerome Glisse4c788672009-11-20 14:29:23 +010089 r = radeon_bo_reserve(robj, false);
90 if (unlikely(r != 0))
91 return r;
92 r = radeon_bo_pin(robj, pin_domain, gpu_addr);
93 radeon_bo_unreserve(robj);
94 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020095}
96
97void radeon_gem_object_unpin(struct drm_gem_object *obj)
98{
Jerome Glisse4c788672009-11-20 14:29:23 +010099 struct radeon_bo *robj = obj->driver_private;
100 int r;
101
102 r = radeon_bo_reserve(robj, false);
103 if (likely(r == 0)) {
104 radeon_bo_unpin(robj);
105 radeon_bo_unreserve(robj);
106 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200107}
108
109int radeon_gem_set_domain(struct drm_gem_object *gobj,
110 uint32_t rdomain, uint32_t wdomain)
111{
Jerome Glisse4c788672009-11-20 14:29:23 +0100112 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200113 uint32_t domain;
114 int r;
115
116 /* FIXME: reeimplement */
117 robj = gobj->driver_private;
118 /* work out where to validate the buffer to */
119 domain = wdomain;
120 if (!domain) {
121 domain = rdomain;
122 }
123 if (!domain) {
124 /* Do nothings */
125 printk(KERN_WARNING "Set domain withou domain !\n");
126 return 0;
127 }
128 if (domain == RADEON_GEM_DOMAIN_CPU) {
129 /* Asking for cpu access wait for object idle */
Jerome Glisse4c788672009-11-20 14:29:23 +0100130 r = radeon_bo_wait(robj, NULL, false);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200131 if (r) {
132 printk(KERN_ERR "Failed to wait for object !\n");
133 return r;
134 }
135 }
136 return 0;
137}
138
139int radeon_gem_init(struct radeon_device *rdev)
140{
141 INIT_LIST_HEAD(&rdev->gem.objects);
142 return 0;
143}
144
145void radeon_gem_fini(struct radeon_device *rdev)
146{
Jerome Glisse4c788672009-11-20 14:29:23 +0100147 radeon_bo_force_delete(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200148}
149
150
151/*
152 * GEM ioctls.
153 */
154int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
155 struct drm_file *filp)
156{
157 struct radeon_device *rdev = dev->dev_private;
158 struct drm_radeon_gem_info *args = data;
Dave Airlie53595332011-03-14 09:47:24 +1000159 struct ttm_mem_type_manager *man;
160
161 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162
Dave Airlie7a50f012009-07-21 20:39:30 +1000163 args->vram_size = rdev->mc.real_vram_size;
Dave Airlie53595332011-03-14 09:47:24 +1000164 args->vram_visible = (u64)man->size << PAGE_SHIFT;
Michel Dänzer38e14922009-08-05 00:19:51 +0200165 if (rdev->stollen_vga_memory)
Jerome Glisse4c788672009-11-20 14:29:23 +0100166 args->vram_visible -= radeon_bo_size(rdev->stollen_vga_memory);
Dave Airlie38651672010-03-30 05:34:13 +0000167 args->vram_visible -= radeon_fbdev_total_size(rdev);
Michel Dänzer38e14922009-08-05 00:19:51 +0200168 args->gart_size = rdev->mc.gtt_size - rdev->cp.ring_size - 4096 -
169 RADEON_IB_POOL_SIZE*64*1024;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200170 return 0;
171}
172
173int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
174 struct drm_file *filp)
175{
176 /* TODO: implement */
177 DRM_ERROR("unimplemented %s\n", __func__);
178 return -ENOSYS;
179}
180
181int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
182 struct drm_file *filp)
183{
184 /* TODO: implement */
185 DRM_ERROR("unimplemented %s\n", __func__);
186 return -ENOSYS;
187}
188
189int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
190 struct drm_file *filp)
191{
192 struct radeon_device *rdev = dev->dev_private;
193 struct drm_radeon_gem_create *args = data;
194 struct drm_gem_object *gobj;
195 uint32_t handle;
196 int r;
197
198 /* create a gem object to contain this object in */
199 args->size = roundup(args->size, PAGE_SIZE);
200 r = radeon_gem_object_create(rdev, args->size, args->alignment,
Jerome Glisse4c788672009-11-20 14:29:23 +0100201 args->initial_domain, false,
202 false, &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200203 if (r) {
204 return r;
205 }
206 r = drm_gem_handle_create(filp, gobj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000207 /* drop reference from allocate - handle holds it now */
208 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200209 if (r) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200210 return r;
211 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200212 args->handle = handle;
213 return 0;
214}
215
216int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
217 struct drm_file *filp)
218{
219 /* transition the BO to a domain -
220 * just validate the BO into a certain domain */
221 struct drm_radeon_gem_set_domain *args = data;
222 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100223 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200224 int r;
225
226 /* for now if someone requests domain CPU -
227 * just make sure the buffer is finished with */
228
229 /* just do a BO wait for now */
230 gobj = drm_gem_object_lookup(dev, filp, args->handle);
231 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100232 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200233 }
234 robj = gobj->driver_private;
235
236 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
237
Luca Barbieribc9025b2010-02-09 05:49:12 +0000238 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200239 return r;
240}
241
242int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
243 struct drm_file *filp)
244{
245 struct drm_radeon_gem_mmap *args = data;
246 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100247 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200248
249 gobj = drm_gem_object_lookup(dev, filp, args->handle);
250 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100251 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200252 }
253 robj = gobj->driver_private;
Jerome Glisse4c788672009-11-20 14:29:23 +0100254 args->addr_ptr = radeon_bo_mmap_offset(robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000255 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100256 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200257}
258
259int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
260 struct drm_file *filp)
261{
Dave Airliecefb87e2009-08-16 21:05:45 +1000262 struct drm_radeon_gem_busy *args = data;
263 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100264 struct radeon_bo *robj;
Dave Airliecefb87e2009-08-16 21:05:45 +1000265 int r;
Dave Airlie4361e522009-12-10 15:59:32 +1000266 uint32_t cur_placement = 0;
Dave Airliecefb87e2009-08-16 21:05:45 +1000267
268 gobj = drm_gem_object_lookup(dev, filp, args->handle);
269 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100270 return -ENOENT;
Dave Airliecefb87e2009-08-16 21:05:45 +1000271 }
272 robj = gobj->driver_private;
Jerome Glisse4c788672009-11-20 14:29:23 +0100273 r = radeon_bo_wait(robj, &cur_placement, true);
Michel Dänzer9f844e52009-08-22 17:38:23 +0200274 switch (cur_placement) {
275 case TTM_PL_VRAM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000276 args->domain = RADEON_GEM_DOMAIN_VRAM;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200277 break;
278 case TTM_PL_TT:
Dave Airliecefb87e2009-08-16 21:05:45 +1000279 args->domain = RADEON_GEM_DOMAIN_GTT;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200280 break;
281 case TTM_PL_SYSTEM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000282 args->domain = RADEON_GEM_DOMAIN_CPU;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200283 default:
284 break;
285 }
Luca Barbieribc9025b2010-02-09 05:49:12 +0000286 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee3b24152009-08-21 09:47:45 +1000287 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200288}
289
290int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
291 struct drm_file *filp)
292{
293 struct drm_radeon_gem_wait_idle *args = data;
294 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100295 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296 int r;
297
298 gobj = drm_gem_object_lookup(dev, filp, args->handle);
299 if (gobj == NULL) {
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100300 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200301 }
302 robj = gobj->driver_private;
Jerome Glisse4c788672009-11-20 14:29:23 +0100303 r = radeon_bo_wait(robj, NULL, false);
Jerome Glisse062b3892010-02-04 20:36:39 +0100304 /* callback hw specific functions if any */
305 if (robj->rdev->asic->ioctl_wait_idle)
306 robj->rdev->asic->ioctl_wait_idle(robj->rdev, robj);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000307 drm_gem_object_unreference_unlocked(gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200308 return r;
309}
Dave Airliee024e112009-06-24 09:48:08 +1000310
311int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
312 struct drm_file *filp)
313{
314 struct drm_radeon_gem_set_tiling *args = data;
315 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100316 struct radeon_bo *robj;
Dave Airliee024e112009-06-24 09:48:08 +1000317 int r = 0;
318
319 DRM_DEBUG("%d \n", args->handle);
320 gobj = drm_gem_object_lookup(dev, filp, args->handle);
321 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100322 return -ENOENT;
Dave Airliee024e112009-06-24 09:48:08 +1000323 robj = gobj->driver_private;
Jerome Glisse4c788672009-11-20 14:29:23 +0100324 r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000325 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000326 return r;
327}
328
329int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
330 struct drm_file *filp)
331{
332 struct drm_radeon_gem_get_tiling *args = data;
333 struct drm_gem_object *gobj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100334 struct radeon_bo *rbo;
Dave Airliee024e112009-06-24 09:48:08 +1000335 int r = 0;
336
337 DRM_DEBUG("\n");
338 gobj = drm_gem_object_lookup(dev, filp, args->handle);
339 if (gobj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100340 return -ENOENT;
Jerome Glisse4c788672009-11-20 14:29:23 +0100341 rbo = gobj->driver_private;
342 r = radeon_bo_reserve(rbo, false);
343 if (unlikely(r != 0))
Dave Airlie51f07b72009-12-16 13:10:43 +1000344 goto out;
Jerome Glisse4c788672009-11-20 14:29:23 +0100345 radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
346 radeon_bo_unreserve(rbo);
Dave Airlie51f07b72009-12-16 13:10:43 +1000347out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000348 drm_gem_object_unreference_unlocked(gobj);
Dave Airliee024e112009-06-24 09:48:08 +1000349 return r;
350}