blob: 9c4f895a026efec01e215aa85b7365f20b9efb6a [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{
41 struct radeon_object *robj = gobj->driver_private;
42
43 gobj->driver_private = NULL;
44 if (robj) {
45 radeon_object_unref(&robj);
46 }
47}
48
49int radeon_gem_object_create(struct radeon_device *rdev, int size,
50 int alignment, int initial_domain,
51 bool discardable, bool kernel,
52 bool interruptible,
53 struct drm_gem_object **obj)
54{
55 struct drm_gem_object *gobj;
56 struct radeon_object *robj;
57 int r;
58
59 *obj = NULL;
60 gobj = drm_gem_object_alloc(rdev->ddev, size);
61 if (!gobj) {
62 return -ENOMEM;
63 }
64 /* At least align on page size */
65 if (alignment < PAGE_SIZE) {
66 alignment = PAGE_SIZE;
67 }
68 r = radeon_object_create(rdev, gobj, size, kernel, initial_domain,
69 interruptible, &robj);
70 if (r) {
71 DRM_ERROR("Failed to allocate GEM object (%d, %d, %u)\n",
72 size, initial_domain, alignment);
73 mutex_lock(&rdev->ddev->struct_mutex);
74 drm_gem_object_unreference(gobj);
75 mutex_unlock(&rdev->ddev->struct_mutex);
76 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{
86 struct radeon_object *robj = obj->driver_private;
87 uint32_t flags;
88
89 switch (pin_domain) {
90 case RADEON_GEM_DOMAIN_VRAM:
91 flags = TTM_PL_FLAG_VRAM;
92 break;
93 case RADEON_GEM_DOMAIN_GTT:
94 flags = TTM_PL_FLAG_TT;
95 break;
96 default:
97 flags = TTM_PL_FLAG_SYSTEM;
98 break;
99 }
100 return radeon_object_pin(robj, flags, gpu_addr);
101}
102
103void radeon_gem_object_unpin(struct drm_gem_object *obj)
104{
105 struct radeon_object *robj = obj->driver_private;
106 radeon_object_unpin(robj);
107}
108
109int radeon_gem_set_domain(struct drm_gem_object *gobj,
110 uint32_t rdomain, uint32_t wdomain)
111{
112 struct radeon_object *robj;
113 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 */
130 r = radeon_object_wait(robj);
131 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{
147 radeon_object_force_delete(rdev);
148}
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;
159
Dave Airlie7a50f012009-07-21 20:39:30 +1000160 args->vram_size = rdev->mc.real_vram_size;
Michel Dänzer38e14922009-08-05 00:19:51 +0200161 args->vram_visible = rdev->mc.real_vram_size;
162 if (rdev->stollen_vga_memory)
163 args->vram_visible -= radeon_object_size(rdev->stollen_vga_memory);
164 if (rdev->fbdev_robj)
165 args->vram_visible -= radeon_object_size(rdev->fbdev_robj);
166 args->gart_size = rdev->mc.gtt_size - rdev->cp.ring_size - 4096 -
167 RADEON_IB_POOL_SIZE*64*1024;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168 return 0;
169}
170
171int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
172 struct drm_file *filp)
173{
174 /* TODO: implement */
175 DRM_ERROR("unimplemented %s\n", __func__);
176 return -ENOSYS;
177}
178
179int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
180 struct drm_file *filp)
181{
182 /* TODO: implement */
183 DRM_ERROR("unimplemented %s\n", __func__);
184 return -ENOSYS;
185}
186
187int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
188 struct drm_file *filp)
189{
190 struct radeon_device *rdev = dev->dev_private;
191 struct drm_radeon_gem_create *args = data;
192 struct drm_gem_object *gobj;
193 uint32_t handle;
194 int r;
195
196 /* create a gem object to contain this object in */
197 args->size = roundup(args->size, PAGE_SIZE);
198 r = radeon_gem_object_create(rdev, args->size, args->alignment,
199 args->initial_domain, false,
200 false, true, &gobj);
201 if (r) {
202 return r;
203 }
204 r = drm_gem_handle_create(filp, gobj, &handle);
205 if (r) {
206 mutex_lock(&dev->struct_mutex);
207 drm_gem_object_unreference(gobj);
208 mutex_unlock(&dev->struct_mutex);
209 return r;
210 }
211 mutex_lock(&dev->struct_mutex);
212 drm_gem_object_handle_unreference(gobj);
213 mutex_unlock(&dev->struct_mutex);
214 args->handle = handle;
215 return 0;
216}
217
218int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
219 struct drm_file *filp)
220{
221 /* transition the BO to a domain -
222 * just validate the BO into a certain domain */
223 struct drm_radeon_gem_set_domain *args = data;
224 struct drm_gem_object *gobj;
225 struct radeon_object *robj;
226 int r;
227
228 /* for now if someone requests domain CPU -
229 * just make sure the buffer is finished with */
230
231 /* just do a BO wait for now */
232 gobj = drm_gem_object_lookup(dev, filp, args->handle);
233 if (gobj == NULL) {
234 return -EINVAL;
235 }
236 robj = gobj->driver_private;
237
238 r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
239
240 mutex_lock(&dev->struct_mutex);
241 drm_gem_object_unreference(gobj);
242 mutex_unlock(&dev->struct_mutex);
243 return r;
244}
245
246int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
247 struct drm_file *filp)
248{
249 struct drm_radeon_gem_mmap *args = data;
250 struct drm_gem_object *gobj;
251 struct radeon_object *robj;
252 int r;
253
254 gobj = drm_gem_object_lookup(dev, filp, args->handle);
255 if (gobj == NULL) {
256 return -EINVAL;
257 }
258 robj = gobj->driver_private;
259 r = radeon_object_mmap(robj, &args->addr_ptr);
260 mutex_lock(&dev->struct_mutex);
261 drm_gem_object_unreference(gobj);
262 mutex_unlock(&dev->struct_mutex);
263 return r;
264}
265
266int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
267 struct drm_file *filp)
268{
Dave Airliecefb87e2009-08-16 21:05:45 +1000269 struct drm_radeon_gem_busy *args = data;
270 struct drm_gem_object *gobj;
271 struct radeon_object *robj;
272 int r;
273 uint32_t cur_placement;
274
275 gobj = drm_gem_object_lookup(dev, filp, args->handle);
276 if (gobj == NULL) {
277 return -EINVAL;
278 }
279 robj = gobj->driver_private;
280 r = radeon_object_busy_domain(robj, &cur_placement);
Michel Dänzer9f844e52009-08-22 17:38:23 +0200281 switch (cur_placement) {
282 case TTM_PL_VRAM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000283 args->domain = RADEON_GEM_DOMAIN_VRAM;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200284 break;
285 case TTM_PL_TT:
Dave Airliecefb87e2009-08-16 21:05:45 +1000286 args->domain = RADEON_GEM_DOMAIN_GTT;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200287 break;
288 case TTM_PL_SYSTEM:
Dave Airliecefb87e2009-08-16 21:05:45 +1000289 args->domain = RADEON_GEM_DOMAIN_CPU;
Michel Dänzer9f844e52009-08-22 17:38:23 +0200290 default:
291 break;
292 }
Dave Airliecefb87e2009-08-16 21:05:45 +1000293 mutex_lock(&dev->struct_mutex);
294 drm_gem_object_unreference(gobj);
295 mutex_unlock(&dev->struct_mutex);
Dave Airliee3b24152009-08-21 09:47:45 +1000296 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200297}
298
299int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
300 struct drm_file *filp)
301{
302 struct drm_radeon_gem_wait_idle *args = data;
303 struct drm_gem_object *gobj;
304 struct radeon_object *robj;
305 int r;
306
307 gobj = drm_gem_object_lookup(dev, filp, args->handle);
308 if (gobj == NULL) {
309 return -EINVAL;
310 }
311 robj = gobj->driver_private;
312 r = radeon_object_wait(robj);
313 mutex_lock(&dev->struct_mutex);
314 drm_gem_object_unreference(gobj);
315 mutex_unlock(&dev->struct_mutex);
316 return r;
317}
Dave Airliee024e112009-06-24 09:48:08 +1000318
319int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
320 struct drm_file *filp)
321{
322 struct drm_radeon_gem_set_tiling *args = data;
323 struct drm_gem_object *gobj;
324 struct radeon_object *robj;
325 int r = 0;
326
327 DRM_DEBUG("%d \n", args->handle);
328 gobj = drm_gem_object_lookup(dev, filp, args->handle);
329 if (gobj == NULL)
330 return -EINVAL;
331 robj = gobj->driver_private;
332 radeon_object_set_tiling_flags(robj, args->tiling_flags, args->pitch);
333 mutex_lock(&dev->struct_mutex);
334 drm_gem_object_unreference(gobj);
335 mutex_unlock(&dev->struct_mutex);
336 return r;
337}
338
339int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
340 struct drm_file *filp)
341{
342 struct drm_radeon_gem_get_tiling *args = data;
343 struct drm_gem_object *gobj;
344 struct radeon_object *robj;
345 int r = 0;
346
347 DRM_DEBUG("\n");
348 gobj = drm_gem_object_lookup(dev, filp, args->handle);
349 if (gobj == NULL)
350 return -EINVAL;
351 robj = gobj->driver_private;
352 radeon_object_get_tiling_flags(robj, &args->tiling_flags,
353 &args->pitch);
354 mutex_lock(&dev->struct_mutex);
355 drm_gem_object_unreference(gobj);
356 mutex_unlock(&dev->struct_mutex);
357 return r;
358}