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
)
40 void radeon_gem_object_free(struct drm_gem_object
*gobj
)
42 struct radeon_bo
*robj
= gem_to_radeon_bo(gobj
);
45 radeon_bo_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
,
52 struct drm_gem_object
**obj
)
54 struct radeon_bo
*robj
;
58 /* At least align on page size */
59 if (alignment
< PAGE_SIZE
) {
60 alignment
= PAGE_SIZE
;
62 r
= radeon_bo_create(rdev
, size
, alignment
, kernel
, initial_domain
, &robj
);
64 if (r
!= -ERESTARTSYS
)
65 DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n",
66 size
, initial_domain
, alignment
, r
);
69 *obj
= &robj
->gem_base
;
71 mutex_lock(&rdev
->gem
.mutex
);
72 list_add_tail(&robj
->list
, &rdev
->gem
.objects
);
73 mutex_unlock(&rdev
->gem
.mutex
);
78 int radeon_gem_object_pin(struct drm_gem_object
*obj
, uint32_t pin_domain
,
81 struct radeon_bo
*robj
= gem_to_radeon_bo(obj
);
84 r
= radeon_bo_reserve(robj
, false);
87 r
= radeon_bo_pin(robj
, pin_domain
, gpu_addr
);
88 radeon_bo_unreserve(robj
);
92 void radeon_gem_object_unpin(struct drm_gem_object
*obj
)
94 struct radeon_bo
*robj
= gem_to_radeon_bo(obj
);
97 r
= radeon_bo_reserve(robj
, false);
99 radeon_bo_unpin(robj
);
100 radeon_bo_unreserve(robj
);
104 int radeon_gem_set_domain(struct drm_gem_object
*gobj
,
105 uint32_t rdomain
, uint32_t wdomain
)
107 struct radeon_bo
*robj
;
111 /* FIXME: reeimplement */
112 robj
= gem_to_radeon_bo(gobj
);
113 /* work out where to validate the buffer to */
120 printk(KERN_WARNING
"Set domain withou domain !\n");
123 if (domain
== RADEON_GEM_DOMAIN_CPU
) {
124 /* Asking for cpu access wait for object idle */
125 r
= radeon_bo_wait(robj
, NULL
, false, TTM_USAGE_READWRITE
);
127 printk(KERN_ERR
"Failed to wait for object !\n");
134 int radeon_gem_init(struct radeon_device
*rdev
)
136 INIT_LIST_HEAD(&rdev
->gem
.objects
);
140 void radeon_gem_fini(struct radeon_device
*rdev
)
142 radeon_bo_force_delete(rdev
);
149 int radeon_gem_info_ioctl(struct drm_device
*dev
, void *data
,
150 struct drm_file
*filp
)
152 struct radeon_device
*rdev
= dev
->dev_private
;
153 struct drm_radeon_gem_info
*args
= data
;
154 struct ttm_mem_type_manager
*man
;
156 man
= &rdev
->mman
.bdev
.man
[TTM_PL_VRAM
];
158 args
->vram_size
= rdev
->mc
.real_vram_size
;
159 args
->vram_visible
= (u64
)man
->size
<< PAGE_SHIFT
;
160 if (rdev
->stollen_vga_memory
)
161 args
->vram_visible
-= radeon_bo_size(rdev
->stollen_vga_memory
);
162 args
->vram_visible
-= radeon_fbdev_total_size(rdev
);
163 args
->gart_size
= rdev
->mc
.gtt_size
- rdev
->cp
.ring_size
- 4096 -
164 RADEON_IB_POOL_SIZE
*64*1024;
168 int radeon_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
169 struct drm_file
*filp
)
171 /* TODO: implement */
172 DRM_ERROR("unimplemented %s\n", __func__
);
176 int radeon_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
177 struct drm_file
*filp
)
179 /* TODO: implement */
180 DRM_ERROR("unimplemented %s\n", __func__
);
184 int radeon_gem_create_ioctl(struct drm_device
*dev
, void *data
,
185 struct drm_file
*filp
)
187 struct radeon_device
*rdev
= dev
->dev_private
;
188 struct drm_radeon_gem_create
*args
= data
;
189 struct drm_gem_object
*gobj
;
193 /* create a gem object to contain this object in */
194 args
->size
= roundup(args
->size
, PAGE_SIZE
);
195 r
= radeon_gem_object_create(rdev
, args
->size
, args
->alignment
,
196 args
->initial_domain
, false,
201 r
= drm_gem_handle_create(filp
, gobj
, &handle
);
202 /* drop reference from allocate - handle holds it now */
203 drm_gem_object_unreference_unlocked(gobj
);
207 args
->handle
= handle
;
211 int radeon_gem_set_domain_ioctl(struct drm_device
*dev
, void *data
,
212 struct drm_file
*filp
)
214 /* transition the BO to a domain -
215 * just validate the BO into a certain domain */
216 struct drm_radeon_gem_set_domain
*args
= data
;
217 struct drm_gem_object
*gobj
;
218 struct radeon_bo
*robj
;
221 /* for now if someone requests domain CPU -
222 * just make sure the buffer is finished with */
224 /* just do a BO wait for now */
225 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
229 robj
= gem_to_radeon_bo(gobj
);
231 r
= radeon_gem_set_domain(gobj
, args
->read_domains
, args
->write_domain
);
233 drm_gem_object_unreference_unlocked(gobj
);
237 int radeon_mode_dumb_mmap(struct drm_file
*filp
,
238 struct drm_device
*dev
,
239 uint32_t handle
, uint64_t *offset_p
)
241 struct drm_gem_object
*gobj
;
242 struct radeon_bo
*robj
;
244 gobj
= drm_gem_object_lookup(dev
, filp
, handle
);
248 robj
= gem_to_radeon_bo(gobj
);
249 *offset_p
= radeon_bo_mmap_offset(robj
);
250 drm_gem_object_unreference_unlocked(gobj
);
254 int radeon_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
255 struct drm_file
*filp
)
257 struct drm_radeon_gem_mmap
*args
= data
;
259 return radeon_mode_dumb_mmap(filp
, dev
, args
->handle
, &args
->addr_ptr
);
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_bo
*robj
;
269 uint32_t cur_placement
= 0;
271 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
275 robj
= gem_to_radeon_bo(gobj
);
276 r
= radeon_bo_wait(robj
, &cur_placement
, true, TTM_USAGE_READWRITE
);
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 drm_gem_object_unreference_unlocked(gobj
);
293 int radeon_gem_wait_idle_ioctl(struct drm_device
*dev
, void *data
,
294 struct drm_file
*filp
)
296 struct drm_radeon_gem_wait_idle
*args
= data
;
297 struct drm_gem_object
*gobj
;
298 struct radeon_bo
*robj
;
301 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
305 robj
= gem_to_radeon_bo(gobj
);
306 r
= radeon_bo_wait(robj
, NULL
, false, TTM_USAGE_READWRITE
);
307 /* callback hw specific functions if any */
308 if (robj
->rdev
->asic
->ioctl_wait_idle
)
309 robj
->rdev
->asic
->ioctl_wait_idle(robj
->rdev
, robj
);
310 drm_gem_object_unreference_unlocked(gobj
);
314 int radeon_gem_wait_ioctl(struct drm_device
*dev
, void *data
,
315 struct drm_file
*filp
)
317 struct drm_radeon_gem_wait
*args
= data
;
318 struct drm_gem_object
*gobj
;
319 struct radeon_bo
*robj
;
320 bool no_wait
= (args
->flags
& RADEON_GEM_NO_WAIT
) != 0;
321 enum ttm_buffer_usage usage
= 0;
324 if (args
->flags
& RADEON_GEM_USAGE_READ
)
325 usage
|= TTM_USAGE_READ
;
326 if (args
->flags
& RADEON_GEM_USAGE_WRITE
)
327 usage
|= TTM_USAGE_WRITE
;
329 usage
= TTM_USAGE_READWRITE
;
331 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
335 robj
= gem_to_radeon_bo(gobj
);
336 r
= radeon_bo_wait(robj
, NULL
, no_wait
, usage
);
337 /* callback hw specific functions if any */
338 if (!no_wait
&& robj
->rdev
->asic
->ioctl_wait_idle
)
339 robj
->rdev
->asic
->ioctl_wait_idle(robj
->rdev
, robj
);
340 drm_gem_object_unreference_unlocked(gobj
);
344 int radeon_gem_set_tiling_ioctl(struct drm_device
*dev
, void *data
,
345 struct drm_file
*filp
)
347 struct drm_radeon_gem_set_tiling
*args
= data
;
348 struct drm_gem_object
*gobj
;
349 struct radeon_bo
*robj
;
352 DRM_DEBUG("%d \n", args
->handle
);
353 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
356 robj
= gem_to_radeon_bo(gobj
);
357 r
= radeon_bo_set_tiling_flags(robj
, args
->tiling_flags
, args
->pitch
);
358 drm_gem_object_unreference_unlocked(gobj
);
362 int radeon_gem_get_tiling_ioctl(struct drm_device
*dev
, void *data
,
363 struct drm_file
*filp
)
365 struct drm_radeon_gem_get_tiling
*args
= data
;
366 struct drm_gem_object
*gobj
;
367 struct radeon_bo
*rbo
;
371 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
374 rbo
= gem_to_radeon_bo(gobj
);
375 r
= radeon_bo_reserve(rbo
, false);
376 if (unlikely(r
!= 0))
378 radeon_bo_get_tiling_flags(rbo
, &args
->tiling_flags
, &args
->pitch
);
379 radeon_bo_unreserve(rbo
);
381 drm_gem_object_unreference_unlocked(gobj
);
385 int radeon_mode_dumb_create(struct drm_file
*file_priv
,
386 struct drm_device
*dev
,
387 struct drm_mode_create_dumb
*args
)
389 struct radeon_device
*rdev
= dev
->dev_private
;
390 struct drm_gem_object
*gobj
;
394 args
->pitch
= radeon_align_pitch(rdev
, args
->width
, args
->bpp
, 0) * ((args
->bpp
+ 1) / 8);
395 args
->size
= args
->pitch
* args
->height
;
396 args
->size
= ALIGN(args
->size
, PAGE_SIZE
);
398 r
= radeon_gem_object_create(rdev
, args
->size
, 0,
399 RADEON_GEM_DOMAIN_VRAM
,
400 false, ttm_bo_type_device
,
405 r
= drm_gem_handle_create(file_priv
, gobj
, &handle
);
406 /* drop reference from allocate - handle holds it now */
407 drm_gem_object_unreference_unlocked(gobj
);
411 args
->handle
= handle
;
415 int radeon_mode_dumb_destroy(struct drm_file
*file_priv
,
416 struct drm_device
*dev
,
419 return drm_gem_handle_delete(file_priv
, handle
);