2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
30 #include "radeon_drm.h"
33 int radeon_gem_object_init(struct drm_gem_object
*obj
)
35 /* we do nothings here */
39 void radeon_gem_object_free(struct drm_gem_object
*gobj
)
41 struct radeon_object
*robj
= gobj
->driver_private
;
43 gobj
->driver_private
= NULL
;
45 radeon_object_unref(&robj
);
49 int radeon_gem_object_create(struct radeon_device
*rdev
, int size
,
50 int alignment
, int initial_domain
,
51 bool discardable
, bool kernel
,
53 struct drm_gem_object
**obj
)
55 struct drm_gem_object
*gobj
;
56 struct radeon_object
*robj
;
60 gobj
= drm_gem_object_alloc(rdev
->ddev
, size
);
64 /* At least align on page size */
65 if (alignment
< PAGE_SIZE
) {
66 alignment
= PAGE_SIZE
;
68 r
= radeon_object_create(rdev
, gobj
, size
, kernel
, initial_domain
,
69 interruptible
, &robj
);
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
);
78 gobj
->driver_private
= robj
;
83 int radeon_gem_object_pin(struct drm_gem_object
*obj
, uint32_t pin_domain
,
86 struct radeon_object
*robj
= obj
->driver_private
;
90 case RADEON_GEM_DOMAIN_VRAM
:
91 flags
= TTM_PL_FLAG_VRAM
;
93 case RADEON_GEM_DOMAIN_GTT
:
94 flags
= TTM_PL_FLAG_TT
;
97 flags
= TTM_PL_FLAG_SYSTEM
;
100 return radeon_object_pin(robj
, flags
, gpu_addr
);
103 void radeon_gem_object_unpin(struct drm_gem_object
*obj
)
105 struct radeon_object
*robj
= obj
->driver_private
;
106 radeon_object_unpin(robj
);
109 int radeon_gem_set_domain(struct drm_gem_object
*gobj
,
110 uint32_t rdomain
, uint32_t wdomain
)
112 struct radeon_object
*robj
;
116 /* FIXME: reeimplement */
117 robj
= gobj
->driver_private
;
118 /* work out where to validate the buffer to */
125 printk(KERN_WARNING
"Set domain withou domain !\n");
128 if (domain
== RADEON_GEM_DOMAIN_CPU
) {
129 /* Asking for cpu access wait for object idle */
130 r
= radeon_object_wait(robj
);
132 printk(KERN_ERR
"Failed to wait for object !\n");
139 int radeon_gem_init(struct radeon_device
*rdev
)
141 INIT_LIST_HEAD(&rdev
->gem
.objects
);
145 void radeon_gem_fini(struct radeon_device
*rdev
)
147 radeon_object_force_delete(rdev
);
154 int radeon_gem_info_ioctl(struct drm_device
*dev
, void *data
,
155 struct drm_file
*filp
)
157 struct radeon_device
*rdev
= dev
->dev_private
;
158 struct drm_radeon_gem_info
*args
= data
;
160 args
->vram_size
= rdev
->mc
.real_vram_size
;
161 /* FIXME: report somethings that makes sense */
162 args
->vram_visible
= rdev
->mc
.real_vram_size
- (4 * 1024 * 1024);
163 args
->gart_size
= rdev
->mc
.gtt_size
;
167 int radeon_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
168 struct drm_file
*filp
)
170 /* TODO: implement */
171 DRM_ERROR("unimplemented %s\n", __func__
);
175 int radeon_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
176 struct drm_file
*filp
)
178 /* TODO: implement */
179 DRM_ERROR("unimplemented %s\n", __func__
);
183 int radeon_gem_create_ioctl(struct drm_device
*dev
, void *data
,
184 struct drm_file
*filp
)
186 struct radeon_device
*rdev
= dev
->dev_private
;
187 struct drm_radeon_gem_create
*args
= data
;
188 struct drm_gem_object
*gobj
;
192 /* create a gem object to contain this object in */
193 args
->size
= roundup(args
->size
, PAGE_SIZE
);
194 r
= radeon_gem_object_create(rdev
, args
->size
, args
->alignment
,
195 args
->initial_domain
, false,
200 r
= drm_gem_handle_create(filp
, gobj
, &handle
);
202 mutex_lock(&dev
->struct_mutex
);
203 drm_gem_object_unreference(gobj
);
204 mutex_unlock(&dev
->struct_mutex
);
207 mutex_lock(&dev
->struct_mutex
);
208 drm_gem_object_handle_unreference(gobj
);
209 mutex_unlock(&dev
->struct_mutex
);
210 args
->handle
= handle
;
214 int radeon_gem_set_domain_ioctl(struct drm_device
*dev
, void *data
,
215 struct drm_file
*filp
)
217 /* transition the BO to a domain -
218 * just validate the BO into a certain domain */
219 struct drm_radeon_gem_set_domain
*args
= data
;
220 struct drm_gem_object
*gobj
;
221 struct radeon_object
*robj
;
224 /* for now if someone requests domain CPU -
225 * just make sure the buffer is finished with */
227 /* just do a BO wait for now */
228 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
232 robj
= gobj
->driver_private
;
234 r
= radeon_gem_set_domain(gobj
, args
->read_domains
, args
->write_domain
);
236 mutex_lock(&dev
->struct_mutex
);
237 drm_gem_object_unreference(gobj
);
238 mutex_unlock(&dev
->struct_mutex
);
242 int radeon_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
243 struct drm_file
*filp
)
245 struct drm_radeon_gem_mmap
*args
= data
;
246 struct drm_gem_object
*gobj
;
247 struct radeon_object
*robj
;
250 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
254 robj
= gobj
->driver_private
;
255 r
= radeon_object_mmap(robj
, &args
->addr_ptr
);
256 mutex_lock(&dev
->struct_mutex
);
257 drm_gem_object_unreference(gobj
);
258 mutex_unlock(&dev
->struct_mutex
);
262 int radeon_gem_busy_ioctl(struct drm_device
*dev
, void *data
,
263 struct drm_file
*filp
)
265 struct drm_radeon_gem_busy
*args
= data
;
266 struct drm_gem_object
*gobj
;
267 struct radeon_object
*robj
;
269 uint32_t cur_placement
;
271 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
275 robj
= gobj
->driver_private
;
276 r
= radeon_object_busy_domain(robj
, &cur_placement
);
277 switch (cur_placement
) {
279 args
->domain
= RADEON_GEM_DOMAIN_VRAM
;
282 args
->domain
= RADEON_GEM_DOMAIN_GTT
;
285 args
->domain
= RADEON_GEM_DOMAIN_CPU
;
289 mutex_lock(&dev
->struct_mutex
);
290 drm_gem_object_unreference(gobj
);
291 mutex_unlock(&dev
->struct_mutex
);
295 int radeon_gem_wait_idle_ioctl(struct drm_device
*dev
, void *data
,
296 struct drm_file
*filp
)
298 struct drm_radeon_gem_wait_idle
*args
= data
;
299 struct drm_gem_object
*gobj
;
300 struct radeon_object
*robj
;
303 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
307 robj
= gobj
->driver_private
;
308 r
= radeon_object_wait(robj
);
309 mutex_lock(&dev
->struct_mutex
);
310 drm_gem_object_unreference(gobj
);
311 mutex_unlock(&dev
->struct_mutex
);
315 int radeon_gem_set_tiling_ioctl(struct drm_device
*dev
, void *data
,
316 struct drm_file
*filp
)
318 struct drm_radeon_gem_set_tiling
*args
= data
;
319 struct drm_gem_object
*gobj
;
320 struct radeon_object
*robj
;
323 DRM_DEBUG("%d \n", args
->handle
);
324 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
327 robj
= gobj
->driver_private
;
328 radeon_object_set_tiling_flags(robj
, args
->tiling_flags
, args
->pitch
);
329 mutex_lock(&dev
->struct_mutex
);
330 drm_gem_object_unreference(gobj
);
331 mutex_unlock(&dev
->struct_mutex
);
335 int radeon_gem_get_tiling_ioctl(struct drm_device
*dev
, void *data
,
336 struct drm_file
*filp
)
338 struct drm_radeon_gem_get_tiling
*args
= data
;
339 struct drm_gem_object
*gobj
;
340 struct radeon_object
*robj
;
344 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
347 robj
= gobj
->driver_private
;
348 radeon_object_get_tiling_flags(robj
, &args
->tiling_flags
,
350 mutex_lock(&dev
->struct_mutex
);
351 drm_gem_object_unreference(gobj
);
352 mutex_unlock(&dev
->struct_mutex
);