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"
38 int radeon_ttm_init(struct radeon_device *rdev);
39 void radeon_ttm_fini(struct radeon_device *rdev);
40 static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
43 * To exclude mutual BO access we rely on bo_reserve exclusion, as all
44 * function are calling it.
47 static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
51 bo = container_of(tbo, struct radeon_bo, tbo);
52 mutex_lock(&bo->rdev->gem.mutex);
53 list_del_init(&bo->list);
54 mutex_unlock(&bo->rdev->gem.mutex);
55 radeon_bo_clear_surface_reg(bo);
59 void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
63 rbo->placement.fpfn = 0;
64 rbo->placement.lpfn = 0;
65 rbo->placement.placement = rbo->placements;
66 rbo->placement.busy_placement = rbo->placements;
67 if (domain & RADEON_GEM_DOMAIN_VRAM)
68 rbo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
70 if (domain & RADEON_GEM_DOMAIN_GTT)
71 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
72 if (domain & RADEON_GEM_DOMAIN_CPU)
73 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
75 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
76 rbo->placement.num_placement = c;
77 rbo->placement.num_busy_placement = c;
80 int radeon_bo_create(struct radeon_device *rdev, struct drm_gem_object *gobj,
81 unsigned long size, bool kernel, u32 domain,
82 struct radeon_bo **bo_ptr)
85 enum ttm_bo_type type;
88 if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
89 rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
92 type = ttm_bo_type_kernel;
94 type = ttm_bo_type_device;
97 bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
102 bo->surface_reg = -1;
103 INIT_LIST_HEAD(&bo->list);
105 radeon_ttm_placement_from_domain(bo, domain);
106 /* Kernel allocation are uninterruptible */
107 r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
108 &bo->placement, 0, 0, !kernel, NULL, size,
109 &radeon_ttm_bo_destroy);
110 if (unlikely(r != 0)) {
111 if (r != -ERESTARTSYS)
113 "object_init failed for (%lu, 0x%08X)\n",
119 mutex_lock(&bo->rdev->gem.mutex);
120 list_add_tail(&bo->list, &rdev->gem.objects);
121 mutex_unlock(&bo->rdev->gem.mutex);
126 int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
137 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
141 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
145 radeon_bo_check_tiling(bo, 0, 0);
149 void radeon_bo_kunmap(struct radeon_bo *bo)
151 if (bo->kptr == NULL)
154 radeon_bo_check_tiling(bo, 0, 0);
155 ttm_bo_kunmap(&bo->kmap);
158 void radeon_bo_unref(struct radeon_bo **bo)
160 struct ttm_buffer_object *tbo;
170 int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
174 radeon_ttm_placement_from_domain(bo, domain);
178 *gpu_addr = radeon_bo_gpu_offset(bo);
181 radeon_ttm_placement_from_domain(bo, domain);
182 for (i = 0; i < bo->placement.num_placement; i++)
183 bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
184 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
185 if (likely(r == 0)) {
187 if (gpu_addr != NULL)
188 *gpu_addr = radeon_bo_gpu_offset(bo);
190 if (unlikely(r != 0))
191 dev_err(bo->rdev->dev, "%p pin failed\n", bo);
195 int radeon_bo_unpin(struct radeon_bo *bo)
199 if (!bo->pin_count) {
200 dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
206 for (i = 0; i < bo->placement.num_placement; i++)
207 bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
208 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
209 if (unlikely(r != 0))
210 dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
214 int radeon_bo_evict_vram(struct radeon_device *rdev)
216 if (rdev->flags & RADEON_IS_IGP) {
217 /* Useless to evict on IGP chips */
220 return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
223 void radeon_bo_force_delete(struct radeon_device *rdev)
225 struct radeon_bo *bo, *n;
226 struct drm_gem_object *gobj;
228 if (list_empty(&rdev->gem.objects)) {
231 dev_err(rdev->dev, "Userspace still has active objects !\n");
232 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
233 mutex_lock(&rdev->ddev->struct_mutex);
235 dev_err(rdev->dev, "%p %p %lu %lu force free\n",
236 gobj, bo, (unsigned long)gobj->size,
237 *((unsigned long *)&gobj->refcount));
238 mutex_lock(&bo->rdev->gem.mutex);
239 list_del_init(&bo->list);
240 mutex_unlock(&bo->rdev->gem.mutex);
241 radeon_bo_unref(&bo);
242 gobj->driver_private = NULL;
243 drm_gem_object_unreference(gobj);
244 mutex_unlock(&rdev->ddev->struct_mutex);
248 int radeon_bo_init(struct radeon_device *rdev)
250 /* Add an MTRR for the VRAM */
251 rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
252 MTRR_TYPE_WRCOMB, 1);
253 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
254 rdev->mc.mc_vram_size >> 20,
255 (unsigned long long)rdev->mc.aper_size >> 20);
256 DRM_INFO("RAM width %dbits %cDR\n",
257 rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
258 return radeon_ttm_init(rdev);
261 void radeon_bo_fini(struct radeon_device *rdev)
263 radeon_ttm_fini(rdev);
266 void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
267 struct list_head *head)
270 list_add(&lobj->list, head);
272 list_add_tail(&lobj->list, head);
276 int radeon_bo_list_reserve(struct list_head *head)
278 struct radeon_bo_list *lobj;
281 list_for_each_entry(lobj, head, list){
282 r = radeon_bo_reserve(lobj->bo, false);
283 if (unlikely(r != 0))
289 void radeon_bo_list_unreserve(struct list_head *head)
291 struct radeon_bo_list *lobj;
293 list_for_each_entry(lobj, head, list) {
294 /* only unreserve object we successfully reserved */
295 if (radeon_bo_is_reserved(lobj->bo))
296 radeon_bo_unreserve(lobj->bo);
300 int radeon_bo_list_validate(struct list_head *head, void *fence)
302 struct radeon_bo_list *lobj;
303 struct radeon_bo *bo;
304 struct radeon_fence *old_fence = NULL;
307 r = radeon_bo_list_reserve(head);
308 if (unlikely(r != 0)) {
311 list_for_each_entry(lobj, head, list) {
313 if (!bo->pin_count) {
315 radeon_ttm_placement_from_domain(bo,
318 radeon_ttm_placement_from_domain(bo,
321 r = ttm_bo_validate(&bo->tbo, &bo->placement,
326 lobj->gpu_offset = radeon_bo_gpu_offset(bo);
327 lobj->tiling_flags = bo->tiling_flags;
329 old_fence = (struct radeon_fence *)bo->tbo.sync_obj;
330 bo->tbo.sync_obj = radeon_fence_ref(fence);
331 bo->tbo.sync_obj_arg = NULL;
334 radeon_fence_unref(&old_fence);
340 void radeon_bo_list_unvalidate(struct list_head *head, void *fence)
342 struct radeon_bo_list *lobj;
343 struct radeon_fence *old_fence;
346 list_for_each_entry(lobj, head, list) {
347 old_fence = to_radeon_fence(lobj->bo->tbo.sync_obj);
348 if (old_fence == fence) {
349 lobj->bo->tbo.sync_obj = NULL;
350 radeon_fence_unref(&old_fence);
353 radeon_bo_list_unreserve(head);
356 int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
357 struct vm_area_struct *vma)
359 return ttm_fbdev_mmap(vma, &bo->tbo);
362 int radeon_bo_get_surface_reg(struct radeon_bo *bo)
364 struct radeon_device *rdev = bo->rdev;
365 struct radeon_surface_reg *reg;
366 struct radeon_bo *old_object;
370 BUG_ON(!atomic_read(&bo->tbo.reserved));
372 if (!bo->tiling_flags)
375 if (bo->surface_reg >= 0) {
376 reg = &rdev->surface_regs[bo->surface_reg];
382 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
384 reg = &rdev->surface_regs[i];
388 old_object = reg->bo;
389 if (old_object->pin_count == 0)
393 /* if we are all out */
394 if (i == RADEON_GEM_MAX_SURFACES) {
397 /* find someone with a surface reg and nuke their BO */
398 reg = &rdev->surface_regs[steal];
399 old_object = reg->bo;
400 /* blow away the mapping */
401 DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
402 ttm_bo_unmap_virtual(&old_object->tbo);
403 old_object->surface_reg = -1;
411 radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
412 bo->tbo.mem.mm_node->start << PAGE_SHIFT,
413 bo->tbo.num_pages << PAGE_SHIFT);
417 static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
419 struct radeon_device *rdev = bo->rdev;
420 struct radeon_surface_reg *reg;
422 if (bo->surface_reg == -1)
425 reg = &rdev->surface_regs[bo->surface_reg];
426 radeon_clear_surface_reg(rdev, bo->surface_reg);
429 bo->surface_reg = -1;
432 int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
433 uint32_t tiling_flags, uint32_t pitch)
437 r = radeon_bo_reserve(bo, false);
438 if (unlikely(r != 0))
440 bo->tiling_flags = tiling_flags;
442 radeon_bo_unreserve(bo);
446 void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
447 uint32_t *tiling_flags,
450 BUG_ON(!atomic_read(&bo->tbo.reserved));
452 *tiling_flags = bo->tiling_flags;
457 int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
460 BUG_ON(!atomic_read(&bo->tbo.reserved));
462 if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
466 radeon_bo_clear_surface_reg(bo);
470 if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
474 if (bo->surface_reg >= 0)
475 radeon_bo_clear_surface_reg(bo);
479 if ((bo->surface_reg >= 0) && !has_moved)
482 return radeon_bo_get_surface_reg(bo);
485 void radeon_bo_move_notify(struct ttm_buffer_object *bo,
486 struct ttm_mem_reg *mem)
488 struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
489 radeon_bo_check_tiling(rbo, 0, 1);
492 void radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
494 struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
495 radeon_bo_check_tiling(rbo, 0, 0);