2 * Copyright 2009 Jerome Glisse.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32 #include <linux/list.h>
34 #include "radeon_drm.h"
37 struct radeon_object {
38 struct ttm_buffer_object tobj;
39 struct list_head list;
40 struct radeon_device *rdev;
41 struct drm_gem_object *gobj;
42 struct ttm_bo_kmap_obj kmap;
49 int radeon_ttm_init(struct radeon_device *rdev);
50 void radeon_ttm_fini(struct radeon_device *rdev);
53 * To exclude mutual BO access we rely on bo_reserve exclusion, as all
54 * function are calling it.
57 static int radeon_object_reserve(struct radeon_object *robj, bool interruptible)
59 return ttm_bo_reserve(&robj->tobj, interruptible, false, false, 0);
62 static void radeon_object_unreserve(struct radeon_object *robj)
64 ttm_bo_unreserve(&robj->tobj);
67 static void radeon_ttm_object_object_destroy(struct ttm_buffer_object *tobj)
69 struct radeon_object *robj;
71 robj = container_of(tobj, struct radeon_object, tobj);
72 list_del_init(&robj->list);
76 static inline void radeon_object_gpu_addr(struct radeon_object *robj)
78 /* Default gpu address */
79 robj->gpu_addr = 0xFFFFFFFFFFFFFFFFULL;
80 if (robj->tobj.mem.mm_node == NULL) {
83 robj->gpu_addr = ((u64)robj->tobj.mem.mm_node->start) << PAGE_SHIFT;
84 switch (robj->tobj.mem.mem_type) {
86 robj->gpu_addr += (u64)robj->rdev->mc.vram_location;
89 robj->gpu_addr += (u64)robj->rdev->mc.gtt_location;
92 DRM_ERROR("Unknown placement %d\n", robj->tobj.mem.mem_type);
93 robj->gpu_addr = 0xFFFFFFFFFFFFFFFFULL;
98 static inline uint32_t radeon_object_flags_from_domain(uint32_t domain)
101 if (domain & RADEON_GEM_DOMAIN_VRAM) {
102 flags |= TTM_PL_FLAG_VRAM;
104 if (domain & RADEON_GEM_DOMAIN_GTT) {
105 flags |= TTM_PL_FLAG_TT;
107 if (domain & RADEON_GEM_DOMAIN_CPU) {
108 flags |= TTM_PL_FLAG_SYSTEM;
111 flags |= TTM_PL_FLAG_SYSTEM;
116 int radeon_object_create(struct radeon_device *rdev,
117 struct drm_gem_object *gobj,
122 struct radeon_object **robj_ptr)
124 struct radeon_object *robj;
125 enum ttm_bo_type type;
129 if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
130 rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
133 type = ttm_bo_type_kernel;
135 type = ttm_bo_type_device;
138 robj = kzalloc(sizeof(struct radeon_object), GFP_KERNEL);
144 INIT_LIST_HEAD(&robj->list);
146 flags = radeon_object_flags_from_domain(domain);
147 r = ttm_buffer_object_init(&rdev->mman.bdev, &robj->tobj, size, type, flags,
148 0, 0, false, NULL, size,
149 &radeon_ttm_object_object_destroy);
150 if (unlikely(r != 0)) {
151 /* ttm call radeon_ttm_object_object_destroy if error happen */
152 DRM_ERROR("Failed to allocate TTM object (%ld, 0x%08X, %u)\n",
158 list_add_tail(&robj->list, &rdev->gem.objects);
163 int radeon_object_kmap(struct radeon_object *robj, void **ptr)
167 spin_lock(&robj->tobj.lock);
172 spin_unlock(&robj->tobj.lock);
175 spin_unlock(&robj->tobj.lock);
176 r = ttm_bo_kmap(&robj->tobj, 0, robj->tobj.num_pages, &robj->kmap);
180 spin_lock(&robj->tobj.lock);
181 robj->kptr = ttm_kmap_obj_virtual(&robj->kmap, &robj->is_iomem);
182 spin_unlock(&robj->tobj.lock);
189 void radeon_object_kunmap(struct radeon_object *robj)
191 spin_lock(&robj->tobj.lock);
192 if (robj->kptr == NULL) {
193 spin_unlock(&robj->tobj.lock);
197 spin_unlock(&robj->tobj.lock);
198 ttm_bo_kunmap(&robj->kmap);
201 void radeon_object_unref(struct radeon_object **robj)
203 struct ttm_buffer_object *tobj;
205 if ((*robj) == NULL) {
208 tobj = &((*robj)->tobj);
215 int radeon_object_mmap(struct radeon_object *robj, uint64_t *offset)
217 *offset = robj->tobj.addr_space_offset;
221 int radeon_object_pin(struct radeon_object *robj, uint32_t domain,
228 flags = radeon_object_flags_from_domain(domain);
229 spin_lock(&robj->tobj.lock);
230 if (robj->pin_count) {
232 if (gpu_addr != NULL) {
233 *gpu_addr = robj->gpu_addr;
235 spin_unlock(&robj->tobj.lock);
238 spin_unlock(&robj->tobj.lock);
239 r = radeon_object_reserve(robj, false);
240 if (unlikely(r != 0)) {
241 DRM_ERROR("radeon: failed to reserve object for pinning it.\n");
244 tmp = robj->tobj.mem.placement;
245 ttm_flag_masked(&tmp, flags, TTM_PL_MASK_MEM);
246 robj->tobj.proposed_placement = tmp | TTM_PL_FLAG_NO_EVICT | TTM_PL_MASK_CACHING;
247 r = ttm_buffer_object_validate(&robj->tobj,
248 robj->tobj.proposed_placement,
250 radeon_object_gpu_addr(robj);
251 if (gpu_addr != NULL) {
252 *gpu_addr = robj->gpu_addr;
255 if (unlikely(r != 0)) {
256 DRM_ERROR("radeon: failed to pin object.\n");
258 radeon_object_unreserve(robj);
262 void radeon_object_unpin(struct radeon_object *robj)
267 spin_lock(&robj->tobj.lock);
268 if (!robj->pin_count) {
269 spin_unlock(&robj->tobj.lock);
270 printk(KERN_WARNING "Unpin not necessary for %p !\n", robj);
274 if (robj->pin_count) {
275 spin_unlock(&robj->tobj.lock);
278 spin_unlock(&robj->tobj.lock);
279 r = radeon_object_reserve(robj, false);
280 if (unlikely(r != 0)) {
281 DRM_ERROR("radeon: failed to reserve object for unpinning it.\n");
284 flags = robj->tobj.mem.placement;
285 robj->tobj.proposed_placement = flags & ~TTM_PL_FLAG_NO_EVICT;
286 r = ttm_buffer_object_validate(&robj->tobj,
287 robj->tobj.proposed_placement,
289 if (unlikely(r != 0)) {
290 DRM_ERROR("radeon: failed to unpin buffer.\n");
292 radeon_object_unreserve(robj);
295 int radeon_object_wait(struct radeon_object *robj)
299 /* FIXME: should use block reservation instead */
300 r = radeon_object_reserve(robj, true);
301 if (unlikely(r != 0)) {
302 DRM_ERROR("radeon: failed to reserve object for waiting.\n");
305 spin_lock(&robj->tobj.lock);
306 if (robj->tobj.sync_obj) {
307 r = ttm_bo_wait(&robj->tobj, true, false, false);
309 spin_unlock(&robj->tobj.lock);
310 radeon_object_unreserve(robj);
314 int radeon_object_evict_vram(struct radeon_device *rdev)
316 if (rdev->flags & RADEON_IS_IGP) {
317 /* Useless to evict on IGP chips */
320 return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
323 void radeon_object_force_delete(struct radeon_device *rdev)
325 struct radeon_object *robj, *n;
326 struct drm_gem_object *gobj;
328 if (list_empty(&rdev->gem.objects)) {
331 DRM_ERROR("Userspace still has active objects !\n");
332 list_for_each_entry_safe(robj, n, &rdev->gem.objects, list) {
333 mutex_lock(&rdev->ddev->struct_mutex);
335 DRM_ERROR("Force free for (%p,%p,%lu,%lu)\n",
336 gobj, robj, (unsigned long)gobj->size,
337 *((unsigned long *)&gobj->refcount));
338 list_del_init(&robj->list);
339 radeon_object_unref(&robj);
340 gobj->driver_private = NULL;
341 drm_gem_object_unreference(gobj);
342 mutex_unlock(&rdev->ddev->struct_mutex);
346 int radeon_object_init(struct radeon_device *rdev)
348 return radeon_ttm_init(rdev);
351 void radeon_object_fini(struct radeon_device *rdev)
353 radeon_ttm_fini(rdev);
356 void radeon_object_list_add_object(struct radeon_object_list *lobj,
357 struct list_head *head)
360 list_add(&lobj->list, head);
362 list_add_tail(&lobj->list, head);
366 int radeon_object_list_reserve(struct list_head *head)
368 struct radeon_object_list *lobj;
372 list_for_each(i, head) {
373 lobj = list_entry(i, struct radeon_object_list, list);
374 if (!lobj->robj->pin_count) {
375 r = radeon_object_reserve(lobj->robj, true);
376 if (unlikely(r != 0)) {
377 DRM_ERROR("radeon: failed to reserve object.\n");
386 void radeon_object_list_unreserve(struct list_head *head)
388 struct radeon_object_list *lobj;
391 list_for_each(i, head) {
392 lobj = list_entry(i, struct radeon_object_list, list);
393 if (!lobj->robj->pin_count) {
394 radeon_object_unreserve(lobj->robj);
400 int radeon_object_list_validate(struct list_head *head, void *fence)
402 struct radeon_object_list *lobj;
403 struct radeon_object *robj;
404 struct radeon_fence *old_fence = NULL;
409 r = radeon_object_list_reserve(head);
410 if (unlikely(r != 0)) {
411 radeon_object_list_unreserve(head);
414 list_for_each(i, head) {
415 lobj = list_entry(i, struct radeon_object_list, list);
418 flags = radeon_object_flags_from_domain(lobj->wdomain);
419 flags |= TTM_PL_FLAG_TT;
421 flags = radeon_object_flags_from_domain(lobj->rdomain);
422 flags |= TTM_PL_FLAG_TT;
423 flags |= TTM_PL_FLAG_VRAM;
425 if (!robj->pin_count) {
426 robj->tobj.proposed_placement = flags | TTM_PL_MASK_CACHING;
427 r = ttm_buffer_object_validate(&robj->tobj,
428 robj->tobj.proposed_placement,
431 radeon_object_list_unreserve(head);
432 DRM_ERROR("radeon: failed to validate.\n");
435 radeon_object_gpu_addr(robj);
437 lobj->gpu_offset = robj->gpu_addr;
439 old_fence = (struct radeon_fence *)robj->tobj.sync_obj;
440 robj->tobj.sync_obj = radeon_fence_ref(fence);
441 robj->tobj.sync_obj_arg = NULL;
444 radeon_fence_unref(&old_fence);
450 void radeon_object_list_unvalidate(struct list_head *head)
452 struct radeon_object_list *lobj;
453 struct radeon_fence *old_fence = NULL;
456 list_for_each(i, head) {
457 lobj = list_entry(i, struct radeon_object_list, list);
458 old_fence = (struct radeon_fence *)lobj->robj->tobj.sync_obj;
459 lobj->robj->tobj.sync_obj = NULL;
461 radeon_fence_unref(&old_fence);
464 radeon_object_list_unreserve(head);
467 void radeon_object_list_clean(struct list_head *head)
469 radeon_object_list_unreserve(head);
472 int radeon_object_fbdev_mmap(struct radeon_object *robj,
473 struct vm_area_struct *vma)
475 return ttm_fbdev_mmap(vma, &robj->tobj);
478 unsigned long radeon_object_size(struct radeon_object *robj)
480 return robj->tobj.num_pages << PAGE_SHIFT;