Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Jerome Glisse. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 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: |
| 12 | * |
| 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. |
| 20 | * |
| 21 | * The above copyright notice and this permission notice (including the |
| 22 | * next paragraph) shall be included in all copies or substantial portions |
| 23 | * of the Software. |
| 24 | * |
| 25 | */ |
| 26 | /* |
| 27 | * Authors: |
| 28 | * Jerome Glisse <glisse@freedesktop.org> |
| 29 | * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com> |
| 30 | * Dave Airlie |
| 31 | */ |
| 32 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 34 | #include <drm/drmP.h> |
| 35 | #include "radeon_drm.h" |
| 36 | #include "radeon.h" |
Dave Airlie | 99ee7fa | 2010-11-23 11:47:49 +1000 | [diff] [blame^] | 37 | #include "radeon_trace.h" |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 38 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 39 | |
| 40 | int radeon_ttm_init(struct radeon_device *rdev); |
| 41 | void radeon_ttm_fini(struct radeon_device *rdev); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 42 | static void radeon_bo_clear_surface_reg(struct radeon_bo *bo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * To exclude mutual BO access we rely on bo_reserve exclusion, as all |
| 46 | * function are calling it. |
| 47 | */ |
| 48 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 49 | static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 50 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 51 | struct radeon_bo *bo; |
| 52 | |
| 53 | bo = container_of(tbo, struct radeon_bo, tbo); |
| 54 | mutex_lock(&bo->rdev->gem.mutex); |
| 55 | list_del_init(&bo->list); |
| 56 | mutex_unlock(&bo->rdev->gem.mutex); |
| 57 | radeon_bo_clear_surface_reg(bo); |
| 58 | kfree(bo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 61 | bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo) |
| 62 | { |
| 63 | if (bo->destroy == &radeon_ttm_bo_destroy) |
| 64 | return true; |
| 65 | return false; |
| 66 | } |
| 67 | |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 68 | void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain) |
| 69 | { |
| 70 | u32 c = 0; |
| 71 | |
| 72 | rbo->placement.fpfn = 0; |
Jerome Glisse | c919b37 | 2010-08-10 17:41:31 -0400 | [diff] [blame] | 73 | rbo->placement.lpfn = rbo->rdev->mc.active_vram_size >> PAGE_SHIFT; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 74 | rbo->placement.placement = rbo->placements; |
| 75 | rbo->placement.busy_placement = rbo->placements; |
| 76 | if (domain & RADEON_GEM_DOMAIN_VRAM) |
| 77 | rbo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | |
| 78 | TTM_PL_FLAG_VRAM; |
| 79 | if (domain & RADEON_GEM_DOMAIN_GTT) |
| 80 | rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT; |
| 81 | if (domain & RADEON_GEM_DOMAIN_CPU) |
| 82 | rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM; |
Jerome Glisse | 9fb03e6 | 2009-12-11 15:13:22 +0100 | [diff] [blame] | 83 | if (!c) |
| 84 | rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 85 | rbo->placement.num_placement = c; |
| 86 | rbo->placement.num_busy_placement = c; |
| 87 | } |
| 88 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 89 | int radeon_bo_create(struct radeon_device *rdev, struct drm_gem_object *gobj, |
Alex Deucher | 268b251 | 2010-11-17 19:00:26 -0500 | [diff] [blame] | 90 | unsigned long size, int byte_align, bool kernel, u32 domain, |
| 91 | struct radeon_bo **bo_ptr) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 92 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 93 | struct radeon_bo *bo; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 94 | enum ttm_bo_type type; |
Alex Deucher | 268b251 | 2010-11-17 19:00:26 -0500 | [diff] [blame] | 95 | int page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 96 | int r; |
| 97 | |
| 98 | if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) { |
| 99 | rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping; |
| 100 | } |
| 101 | if (kernel) { |
| 102 | type = ttm_bo_type_kernel; |
| 103 | } else { |
| 104 | type = ttm_bo_type_device; |
| 105 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 106 | *bo_ptr = NULL; |
Michel Dänzer | 2b66b50 | 2010-11-09 11:50:05 +0100 | [diff] [blame] | 107 | |
| 108 | retry: |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 109 | bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL); |
| 110 | if (bo == NULL) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 111 | return -ENOMEM; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 112 | bo->rdev = rdev; |
| 113 | bo->gobj = gobj; |
| 114 | bo->surface_reg = -1; |
| 115 | INIT_LIST_HEAD(&bo->list); |
Jerome Glisse | 1fb107f | 2009-12-10 17:16:28 +0100 | [diff] [blame] | 116 | radeon_ttm_placement_from_domain(bo, domain); |
Thomas Hellstrom | 5cc6fba | 2009-12-07 18:36:19 +0100 | [diff] [blame] | 117 | /* Kernel allocation are uninterruptible */ |
Matthew Garrett | 5876dd2 | 2010-04-26 15:52:20 -0400 | [diff] [blame] | 118 | mutex_lock(&rdev->vram_mutex); |
Jerome Glisse | 1fb107f | 2009-12-10 17:16:28 +0100 | [diff] [blame] | 119 | r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type, |
Alex Deucher | 268b251 | 2010-11-17 19:00:26 -0500 | [diff] [blame] | 120 | &bo->placement, page_align, 0, !kernel, NULL, size, |
Jerome Glisse | 1fb107f | 2009-12-10 17:16:28 +0100 | [diff] [blame] | 121 | &radeon_ttm_bo_destroy); |
Matthew Garrett | 5876dd2 | 2010-04-26 15:52:20 -0400 | [diff] [blame] | 122 | mutex_unlock(&rdev->vram_mutex); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 123 | if (unlikely(r != 0)) { |
Michel Dänzer | e376573 | 2010-07-08 12:43:28 +1000 | [diff] [blame] | 124 | if (r != -ERESTARTSYS) { |
| 125 | if (domain == RADEON_GEM_DOMAIN_VRAM) { |
| 126 | domain |= RADEON_GEM_DOMAIN_GTT; |
| 127 | goto retry; |
| 128 | } |
Thomas Hellstrom | 5cc6fba | 2009-12-07 18:36:19 +0100 | [diff] [blame] | 129 | dev_err(rdev->dev, |
Jerome Glisse | 1fb107f | 2009-12-10 17:16:28 +0100 | [diff] [blame] | 130 | "object_init failed for (%lu, 0x%08X)\n", |
| 131 | size, domain); |
Michel Dänzer | e376573 | 2010-07-08 12:43:28 +1000 | [diff] [blame] | 132 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 133 | return r; |
| 134 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 135 | *bo_ptr = bo; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 136 | if (gobj) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 137 | mutex_lock(&bo->rdev->gem.mutex); |
| 138 | list_add_tail(&bo->list, &rdev->gem.objects); |
| 139 | mutex_unlock(&bo->rdev->gem.mutex); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 140 | } |
Dave Airlie | 99ee7fa | 2010-11-23 11:47:49 +1000 | [diff] [blame^] | 141 | trace_radeon_bo_create(bo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 145 | int radeon_bo_kmap(struct radeon_bo *bo, void **ptr) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 146 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 147 | bool is_iomem; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 148 | int r; |
| 149 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 150 | if (bo->kptr) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 151 | if (ptr) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 152 | *ptr = bo->kptr; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 153 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 154 | return 0; |
| 155 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 156 | r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 157 | if (r) { |
| 158 | return r; |
| 159 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 160 | bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 161 | if (ptr) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 162 | *ptr = bo->kptr; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 163 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 164 | radeon_bo_check_tiling(bo, 0, 0); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 168 | void radeon_bo_kunmap(struct radeon_bo *bo) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 169 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 170 | if (bo->kptr == NULL) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 171 | return; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 172 | bo->kptr = NULL; |
| 173 | radeon_bo_check_tiling(bo, 0, 0); |
| 174 | ttm_bo_kunmap(&bo->kmap); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 177 | void radeon_bo_unref(struct radeon_bo **bo) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 178 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 179 | struct ttm_buffer_object *tbo; |
Dave Airlie | f4b7fb9 | 2010-04-29 18:37:59 +1000 | [diff] [blame] | 180 | struct radeon_device *rdev; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 181 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 182 | if ((*bo) == NULL) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 183 | return; |
Dave Airlie | f4b7fb9 | 2010-04-29 18:37:59 +1000 | [diff] [blame] | 184 | rdev = (*bo)->rdev; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 185 | tbo = &((*bo)->tbo); |
Dave Airlie | f4b7fb9 | 2010-04-29 18:37:59 +1000 | [diff] [blame] | 186 | mutex_lock(&rdev->vram_mutex); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 187 | ttm_bo_unref(&tbo); |
Dave Airlie | f4b7fb9 | 2010-04-29 18:37:59 +1000 | [diff] [blame] | 188 | mutex_unlock(&rdev->vram_mutex); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 189 | if (tbo == NULL) |
| 190 | *bo = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 193 | int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 194 | { |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 195 | int r, i; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 196 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 197 | if (bo->pin_count) { |
| 198 | bo->pin_count++; |
| 199 | if (gpu_addr) |
| 200 | *gpu_addr = radeon_bo_gpu_offset(bo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 201 | return 0; |
| 202 | } |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 203 | radeon_ttm_placement_from_domain(bo, domain); |
Michel Dänzer | 3ca82da | 2010-03-26 19:18:55 +0000 | [diff] [blame] | 204 | if (domain == RADEON_GEM_DOMAIN_VRAM) { |
| 205 | /* force to pin into visible video ram */ |
| 206 | bo->placement.lpfn = bo->rdev->mc.visible_vram_size >> PAGE_SHIFT; |
| 207 | } |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 208 | for (i = 0; i < bo->placement.num_placement; i++) |
| 209 | bo->placements[i] |= TTM_PL_FLAG_NO_EVICT; |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 210 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 211 | if (likely(r == 0)) { |
| 212 | bo->pin_count = 1; |
| 213 | if (gpu_addr != NULL) |
| 214 | *gpu_addr = radeon_bo_gpu_offset(bo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 215 | } |
Thomas Hellstrom | 5cc6fba | 2009-12-07 18:36:19 +0100 | [diff] [blame] | 216 | if (unlikely(r != 0)) |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 217 | dev_err(bo->rdev->dev, "%p pin failed\n", bo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 218 | return r; |
| 219 | } |
| 220 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 221 | int radeon_bo_unpin(struct radeon_bo *bo) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 222 | { |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 223 | int r, i; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 224 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 225 | if (!bo->pin_count) { |
| 226 | dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo); |
| 227 | return 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 228 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 229 | bo->pin_count--; |
| 230 | if (bo->pin_count) |
| 231 | return 0; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 232 | for (i = 0; i < bo->placement.num_placement; i++) |
| 233 | bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT; |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 234 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false); |
Thomas Hellstrom | 5cc6fba | 2009-12-07 18:36:19 +0100 | [diff] [blame] | 235 | if (unlikely(r != 0)) |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 236 | dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo); |
Thomas Hellstrom | 5cc6fba | 2009-12-07 18:36:19 +0100 | [diff] [blame] | 237 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 240 | int radeon_bo_evict_vram(struct radeon_device *rdev) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 241 | { |
Dave Airlie | d796d84 | 2010-01-25 13:08:08 +1000 | [diff] [blame] | 242 | /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */ |
| 243 | if (0 && (rdev->flags & RADEON_IS_IGP)) { |
Alex Deucher | 06b6476 | 2010-01-05 11:27:29 -0500 | [diff] [blame] | 244 | if (rdev->mc.igp_sideport_enabled == false) |
| 245 | /* Useless to evict on IGP chips */ |
| 246 | return 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 247 | } |
| 248 | return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM); |
| 249 | } |
| 250 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 251 | void radeon_bo_force_delete(struct radeon_device *rdev) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 252 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 253 | struct radeon_bo *bo, *n; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 254 | struct drm_gem_object *gobj; |
| 255 | |
| 256 | if (list_empty(&rdev->gem.objects)) { |
| 257 | return; |
| 258 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 259 | dev_err(rdev->dev, "Userspace still has active objects !\n"); |
| 260 | list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 261 | mutex_lock(&rdev->ddev->struct_mutex); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 262 | gobj = bo->gobj; |
| 263 | dev_err(rdev->dev, "%p %p %lu %lu force free\n", |
| 264 | gobj, bo, (unsigned long)gobj->size, |
| 265 | *((unsigned long *)&gobj->refcount)); |
| 266 | mutex_lock(&bo->rdev->gem.mutex); |
| 267 | list_del_init(&bo->list); |
| 268 | mutex_unlock(&bo->rdev->gem.mutex); |
| 269 | radeon_bo_unref(&bo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 270 | gobj->driver_private = NULL; |
| 271 | drm_gem_object_unreference(gobj); |
| 272 | mutex_unlock(&rdev->ddev->struct_mutex); |
| 273 | } |
| 274 | } |
| 275 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 276 | int radeon_bo_init(struct radeon_device *rdev) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 277 | { |
Jerome Glisse | a4d6827 | 2009-09-11 13:00:43 +0200 | [diff] [blame] | 278 | /* Add an MTRR for the VRAM */ |
| 279 | rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size, |
| 280 | MTRR_TYPE_WRCOMB, 1); |
| 281 | DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n", |
| 282 | rdev->mc.mc_vram_size >> 20, |
| 283 | (unsigned long long)rdev->mc.aper_size >> 20); |
| 284 | DRM_INFO("RAM width %dbits %cDR\n", |
| 285 | rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S'); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 286 | return radeon_ttm_init(rdev); |
| 287 | } |
| 288 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 289 | void radeon_bo_fini(struct radeon_device *rdev) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 290 | { |
| 291 | radeon_ttm_fini(rdev); |
| 292 | } |
| 293 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 294 | void radeon_bo_list_add_object(struct radeon_bo_list *lobj, |
| 295 | struct list_head *head) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 296 | { |
| 297 | if (lobj->wdomain) { |
Thomas Hellstrom | 147666f | 2010-11-17 12:38:32 +0000 | [diff] [blame] | 298 | list_add(&lobj->tv.head, head); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 299 | } else { |
Thomas Hellstrom | 147666f | 2010-11-17 12:38:32 +0000 | [diff] [blame] | 300 | list_add_tail(&lobj->tv.head, head); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Jerome Glisse | 6cb8e1f | 2010-02-15 21:36:33 +0100 | [diff] [blame] | 304 | int radeon_bo_list_validate(struct list_head *head) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 305 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 306 | struct radeon_bo_list *lobj; |
| 307 | struct radeon_bo *bo; |
Michel Dänzer | e376573 | 2010-07-08 12:43:28 +1000 | [diff] [blame] | 308 | u32 domain; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 309 | int r; |
| 310 | |
Thomas Hellstrom | 147666f | 2010-11-17 12:38:32 +0000 | [diff] [blame] | 311 | r = ttm_eu_reserve_buffers(head); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 312 | if (unlikely(r != 0)) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 313 | return r; |
| 314 | } |
Thomas Hellstrom | 147666f | 2010-11-17 12:38:32 +0000 | [diff] [blame] | 315 | list_for_each_entry(lobj, head, tv.head) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 316 | bo = lobj->bo; |
| 317 | if (!bo->pin_count) { |
Michel Dänzer | e376573 | 2010-07-08 12:43:28 +1000 | [diff] [blame] | 318 | domain = lobj->wdomain ? lobj->wdomain : lobj->rdomain; |
| 319 | |
| 320 | retry: |
| 321 | radeon_ttm_placement_from_domain(bo, domain); |
Jerome Glisse | 1fb107f | 2009-12-10 17:16:28 +0100 | [diff] [blame] | 322 | r = ttm_bo_validate(&bo->tbo, &bo->placement, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 323 | true, false, false); |
Michel Dänzer | e376573 | 2010-07-08 12:43:28 +1000 | [diff] [blame] | 324 | if (unlikely(r)) { |
| 325 | if (r != -ERESTARTSYS && domain == RADEON_GEM_DOMAIN_VRAM) { |
| 326 | domain |= RADEON_GEM_DOMAIN_GTT; |
| 327 | goto retry; |
| 328 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 329 | return r; |
Michel Dänzer | e376573 | 2010-07-08 12:43:28 +1000 | [diff] [blame] | 330 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 331 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 332 | lobj->gpu_offset = radeon_bo_gpu_offset(bo); |
| 333 | lobj->tiling_flags = bo->tiling_flags; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 338 | int radeon_bo_fbdev_mmap(struct radeon_bo *bo, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 339 | struct vm_area_struct *vma) |
| 340 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 341 | return ttm_fbdev_mmap(vma, &bo->tbo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Dave Airlie | 550e2d9 | 2009-12-09 14:15:38 +1000 | [diff] [blame] | 344 | int radeon_bo_get_surface_reg(struct radeon_bo *bo) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 345 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 346 | struct radeon_device *rdev = bo->rdev; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 347 | struct radeon_surface_reg *reg; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 348 | struct radeon_bo *old_object; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 349 | int steal; |
| 350 | int i; |
| 351 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 352 | BUG_ON(!atomic_read(&bo->tbo.reserved)); |
| 353 | |
| 354 | if (!bo->tiling_flags) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 355 | return 0; |
| 356 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 357 | if (bo->surface_reg >= 0) { |
| 358 | reg = &rdev->surface_regs[bo->surface_reg]; |
| 359 | i = bo->surface_reg; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 360 | goto out; |
| 361 | } |
| 362 | |
| 363 | steal = -1; |
| 364 | for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) { |
| 365 | |
| 366 | reg = &rdev->surface_regs[i]; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 367 | if (!reg->bo) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 368 | break; |
| 369 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 370 | old_object = reg->bo; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 371 | if (old_object->pin_count == 0) |
| 372 | steal = i; |
| 373 | } |
| 374 | |
| 375 | /* if we are all out */ |
| 376 | if (i == RADEON_GEM_MAX_SURFACES) { |
| 377 | if (steal == -1) |
| 378 | return -ENOMEM; |
| 379 | /* find someone with a surface reg and nuke their BO */ |
| 380 | reg = &rdev->surface_regs[steal]; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 381 | old_object = reg->bo; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 382 | /* blow away the mapping */ |
| 383 | DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 384 | ttm_bo_unmap_virtual(&old_object->tbo); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 385 | old_object->surface_reg = -1; |
| 386 | i = steal; |
| 387 | } |
| 388 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 389 | bo->surface_reg = i; |
| 390 | reg->bo = bo; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 391 | |
| 392 | out: |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 393 | radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch, |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 394 | bo->tbo.mem.start << PAGE_SHIFT, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 395 | bo->tbo.num_pages << PAGE_SHIFT); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 399 | static void radeon_bo_clear_surface_reg(struct radeon_bo *bo) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 400 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 401 | struct radeon_device *rdev = bo->rdev; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 402 | struct radeon_surface_reg *reg; |
| 403 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 404 | if (bo->surface_reg == -1) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 405 | return; |
| 406 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 407 | reg = &rdev->surface_regs[bo->surface_reg]; |
| 408 | radeon_clear_surface_reg(rdev, bo->surface_reg); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 409 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 410 | reg->bo = NULL; |
| 411 | bo->surface_reg = -1; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 412 | } |
| 413 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 414 | int radeon_bo_set_tiling_flags(struct radeon_bo *bo, |
| 415 | uint32_t tiling_flags, uint32_t pitch) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 416 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 417 | int r; |
| 418 | |
| 419 | r = radeon_bo_reserve(bo, false); |
| 420 | if (unlikely(r != 0)) |
| 421 | return r; |
| 422 | bo->tiling_flags = tiling_flags; |
| 423 | bo->pitch = pitch; |
| 424 | radeon_bo_unreserve(bo); |
| 425 | return 0; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 426 | } |
| 427 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 428 | void radeon_bo_get_tiling_flags(struct radeon_bo *bo, |
| 429 | uint32_t *tiling_flags, |
| 430 | uint32_t *pitch) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 431 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 432 | BUG_ON(!atomic_read(&bo->tbo.reserved)); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 433 | if (tiling_flags) |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 434 | *tiling_flags = bo->tiling_flags; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 435 | if (pitch) |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 436 | *pitch = bo->pitch; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 437 | } |
| 438 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 439 | int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved, |
| 440 | bool force_drop) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 441 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 442 | BUG_ON(!atomic_read(&bo->tbo.reserved)); |
| 443 | |
| 444 | if (!(bo->tiling_flags & RADEON_TILING_SURFACE)) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 445 | return 0; |
| 446 | |
| 447 | if (force_drop) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 448 | radeon_bo_clear_surface_reg(bo); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 452 | if (bo->tbo.mem.mem_type != TTM_PL_VRAM) { |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 453 | if (!has_moved) |
| 454 | return 0; |
| 455 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 456 | if (bo->surface_reg >= 0) |
| 457 | radeon_bo_clear_surface_reg(bo); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 461 | if ((bo->surface_reg >= 0) && !has_moved) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 462 | return 0; |
| 463 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 464 | return radeon_bo_get_surface_reg(bo); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | void radeon_bo_move_notify(struct ttm_buffer_object *bo, |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 468 | struct ttm_mem_reg *mem) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 469 | { |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 470 | struct radeon_bo *rbo; |
| 471 | if (!radeon_ttm_bo_is_radeon_bo(bo)) |
| 472 | return; |
| 473 | rbo = container_of(bo, struct radeon_bo, tbo); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 474 | radeon_bo_check_tiling(rbo, 0, 1); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 475 | } |
| 476 | |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 477 | int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo) |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 478 | { |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 479 | struct radeon_device *rdev; |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 480 | struct radeon_bo *rbo; |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 481 | unsigned long offset, size; |
| 482 | int r; |
| 483 | |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 484 | if (!radeon_ttm_bo_is_radeon_bo(bo)) |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 485 | return 0; |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 486 | rbo = container_of(bo, struct radeon_bo, tbo); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 487 | radeon_bo_check_tiling(rbo, 0, 0); |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 488 | rdev = rbo->rdev; |
| 489 | if (bo->mem.mem_type == TTM_PL_VRAM) { |
| 490 | size = bo->mem.num_pages << PAGE_SHIFT; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 491 | offset = bo->mem.start << PAGE_SHIFT; |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 492 | if ((offset + size) > rdev->mc.visible_vram_size) { |
| 493 | /* hurrah the memory is not visible ! */ |
| 494 | radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM); |
| 495 | rbo->placement.lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT; |
| 496 | r = ttm_bo_validate(bo, &rbo->placement, false, true, false); |
| 497 | if (unlikely(r != 0)) |
| 498 | return r; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 499 | offset = bo->mem.start << PAGE_SHIFT; |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 500 | /* this should not happen */ |
| 501 | if ((offset + size) > rdev->mc.visible_vram_size) |
| 502 | return -EINVAL; |
| 503 | } |
| 504 | } |
| 505 | return 0; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 506 | } |