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> 33 #include <drm/drmP.h> 34 #include "radeon_drm.h" 35 #include "radeon.h" 36 37 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); 41 42 /* 43 * To exclude mutual BO access we rely on bo_reserve exclusion, as all 44 * function are calling it. 45 */ 46 47 static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo) 48 { 49 struct radeon_bo *bo; 50 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); 56 kfree(bo); 57 } 58 59 void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain) 60 { 61 u32 c = 0; 62 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 | 69 TTM_PL_FLAG_VRAM; 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; 74 rbo->placement.num_placement = c; 75 rbo->placement.num_busy_placement = c; 76 } 77 78 int radeon_bo_create(struct radeon_device *rdev, struct drm_gem_object *gobj, 79 unsigned long size, bool kernel, u32 domain, 80 struct radeon_bo **bo_ptr) 81 { 82 struct radeon_bo *bo; 83 enum ttm_bo_type type; 84 int r; 85 86 if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) { 87 rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping; 88 } 89 if (kernel) { 90 type = ttm_bo_type_kernel; 91 } else { 92 type = ttm_bo_type_device; 93 } 94 *bo_ptr = NULL; 95 bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL); 96 if (bo == NULL) 97 return -ENOMEM; 98 bo->rdev = rdev; 99 bo->gobj = gobj; 100 bo->surface_reg = -1; 101 INIT_LIST_HEAD(&bo->list); 102 103 radeon_ttm_placement_from_domain(bo, domain); 104 /* Kernel allocation are uninterruptible */ 105 r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type, 106 &bo->placement, 0, 0, !kernel, NULL, size, 107 &radeon_ttm_bo_destroy); 108 if (unlikely(r != 0)) { 109 if (r != -ERESTARTSYS) 110 dev_err(rdev->dev, 111 "object_init failed for (%lu, 0x%08X)\n", 112 size, domain); 113 return r; 114 } 115 *bo_ptr = bo; 116 if (gobj) { 117 mutex_lock(&bo->rdev->gem.mutex); 118 list_add_tail(&bo->list, &rdev->gem.objects); 119 mutex_unlock(&bo->rdev->gem.mutex); 120 } 121 return 0; 122 } 123 124 int radeon_bo_kmap(struct radeon_bo *bo, void **ptr) 125 { 126 bool is_iomem; 127 int r; 128 129 if (bo->kptr) { 130 if (ptr) { 131 *ptr = bo->kptr; 132 } 133 return 0; 134 } 135 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap); 136 if (r) { 137 return r; 138 } 139 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); 140 if (ptr) { 141 *ptr = bo->kptr; 142 } 143 radeon_bo_check_tiling(bo, 0, 0); 144 return 0; 145 } 146 147 void radeon_bo_kunmap(struct radeon_bo *bo) 148 { 149 if (bo->kptr == NULL) 150 return; 151 bo->kptr = NULL; 152 radeon_bo_check_tiling(bo, 0, 0); 153 ttm_bo_kunmap(&bo->kmap); 154 } 155 156 void radeon_bo_unref(struct radeon_bo **bo) 157 { 158 struct ttm_buffer_object *tbo; 159 160 if ((*bo) == NULL) 161 return; 162 tbo = &((*bo)->tbo); 163 ttm_bo_unref(&tbo); 164 if (tbo == NULL) 165 *bo = NULL; 166 } 167 168 int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr) 169 { 170 int r, i; 171 172 radeon_ttm_placement_from_domain(bo, domain); 173 if (bo->pin_count) { 174 bo->pin_count++; 175 if (gpu_addr) 176 *gpu_addr = radeon_bo_gpu_offset(bo); 177 return 0; 178 } 179 radeon_ttm_placement_from_domain(bo, domain); 180 for (i = 0; i < bo->placement.num_placement; i++) 181 bo->placements[i] |= TTM_PL_FLAG_NO_EVICT; 182 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); 183 if (likely(r == 0)) { 184 bo->pin_count = 1; 185 if (gpu_addr != NULL) 186 *gpu_addr = radeon_bo_gpu_offset(bo); 187 } 188 if (unlikely(r != 0)) 189 dev_err(bo->rdev->dev, "%p pin failed\n", bo); 190 return r; 191 } 192 193 int radeon_bo_unpin(struct radeon_bo *bo) 194 { 195 int r, i; 196 197 if (!bo->pin_count) { 198 dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo); 199 return 0; 200 } 201 bo->pin_count--; 202 if (bo->pin_count) 203 return 0; 204 for (i = 0; i < bo->placement.num_placement; i++) 205 bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT; 206 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); 207 if (unlikely(r != 0)) 208 dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo); 209 return r; 210 } 211 212 int radeon_bo_evict_vram(struct radeon_device *rdev) 213 { 214 if (rdev->flags & RADEON_IS_IGP) { 215 /* Useless to evict on IGP chips */ 216 return 0; 217 } 218 return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM); 219 } 220 221 void radeon_bo_force_delete(struct radeon_device *rdev) 222 { 223 struct radeon_bo *bo, *n; 224 struct drm_gem_object *gobj; 225 226 if (list_empty(&rdev->gem.objects)) { 227 return; 228 } 229 dev_err(rdev->dev, "Userspace still has active objects !\n"); 230 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) { 231 mutex_lock(&rdev->ddev->struct_mutex); 232 gobj = bo->gobj; 233 dev_err(rdev->dev, "%p %p %lu %lu force free\n", 234 gobj, bo, (unsigned long)gobj->size, 235 *((unsigned long *)&gobj->refcount)); 236 mutex_lock(&bo->rdev->gem.mutex); 237 list_del_init(&bo->list); 238 mutex_unlock(&bo->rdev->gem.mutex); 239 radeon_bo_unref(&bo); 240 gobj->driver_private = NULL; 241 drm_gem_object_unreference(gobj); 242 mutex_unlock(&rdev->ddev->struct_mutex); 243 } 244 } 245 246 int radeon_bo_init(struct radeon_device *rdev) 247 { 248 /* Add an MTRR for the VRAM */ 249 rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size, 250 MTRR_TYPE_WRCOMB, 1); 251 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n", 252 rdev->mc.mc_vram_size >> 20, 253 (unsigned long long)rdev->mc.aper_size >> 20); 254 DRM_INFO("RAM width %dbits %cDR\n", 255 rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S'); 256 return radeon_ttm_init(rdev); 257 } 258 259 void radeon_bo_fini(struct radeon_device *rdev) 260 { 261 radeon_ttm_fini(rdev); 262 } 263 264 void radeon_bo_list_add_object(struct radeon_bo_list *lobj, 265 struct list_head *head) 266 { 267 if (lobj->wdomain) { 268 list_add(&lobj->list, head); 269 } else { 270 list_add_tail(&lobj->list, head); 271 } 272 } 273 274 int radeon_bo_list_reserve(struct list_head *head) 275 { 276 struct radeon_bo_list *lobj; 277 int r; 278 279 list_for_each_entry(lobj, head, list){ 280 r = radeon_bo_reserve(lobj->bo, false); 281 if (unlikely(r != 0)) 282 return r; 283 } 284 return 0; 285 } 286 287 void radeon_bo_list_unreserve(struct list_head *head) 288 { 289 struct radeon_bo_list *lobj; 290 291 list_for_each_entry(lobj, head, list) { 292 /* only unreserve object we successfully reserved */ 293 if (radeon_bo_is_reserved(lobj->bo)) 294 radeon_bo_unreserve(lobj->bo); 295 } 296 } 297 298 int radeon_bo_list_validate(struct list_head *head, void *fence) 299 { 300 struct radeon_bo_list *lobj; 301 struct radeon_bo *bo; 302 struct radeon_fence *old_fence = NULL; 303 int r; 304 305 r = radeon_bo_list_reserve(head); 306 if (unlikely(r != 0)) { 307 return r; 308 } 309 list_for_each_entry(lobj, head, list) { 310 bo = lobj->bo; 311 if (!bo->pin_count) { 312 if (lobj->wdomain) { 313 radeon_ttm_placement_from_domain(bo, 314 lobj->wdomain); 315 } else { 316 radeon_ttm_placement_from_domain(bo, 317 lobj->rdomain); 318 } 319 r = ttm_bo_validate(&bo->tbo, &bo->placement, 320 true, false); 321 if (unlikely(r)) 322 return r; 323 } 324 lobj->gpu_offset = radeon_bo_gpu_offset(bo); 325 lobj->tiling_flags = bo->tiling_flags; 326 if (fence) { 327 old_fence = (struct radeon_fence *)bo->tbo.sync_obj; 328 bo->tbo.sync_obj = radeon_fence_ref(fence); 329 bo->tbo.sync_obj_arg = NULL; 330 } 331 if (old_fence) { 332 radeon_fence_unref(&old_fence); 333 } 334 } 335 return 0; 336 } 337 338 void radeon_bo_list_unvalidate(struct list_head *head, void *fence) 339 { 340 struct radeon_bo_list *lobj; 341 struct radeon_fence *old_fence; 342 343 if (fence) 344 list_for_each_entry(lobj, head, list) { 345 old_fence = to_radeon_fence(lobj->bo->tbo.sync_obj); 346 if (old_fence == fence) { 347 lobj->bo->tbo.sync_obj = NULL; 348 radeon_fence_unref(&old_fence); 349 } 350 } 351 radeon_bo_list_unreserve(head); 352 } 353 354 int radeon_bo_fbdev_mmap(struct radeon_bo *bo, 355 struct vm_area_struct *vma) 356 { 357 return ttm_fbdev_mmap(vma, &bo->tbo); 358 } 359 360 int radeon_bo_get_surface_reg(struct radeon_bo *bo) 361 { 362 struct radeon_device *rdev = bo->rdev; 363 struct radeon_surface_reg *reg; 364 struct radeon_bo *old_object; 365 int steal; 366 int i; 367 368 BUG_ON(!atomic_read(&bo->tbo.reserved)); 369 370 if (!bo->tiling_flags) 371 return 0; 372 373 if (bo->surface_reg >= 0) { 374 reg = &rdev->surface_regs[bo->surface_reg]; 375 i = bo->surface_reg; 376 goto out; 377 } 378 379 steal = -1; 380 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) { 381 382 reg = &rdev->surface_regs[i]; 383 if (!reg->bo) 384 break; 385 386 old_object = reg->bo; 387 if (old_object->pin_count == 0) 388 steal = i; 389 } 390 391 /* if we are all out */ 392 if (i == RADEON_GEM_MAX_SURFACES) { 393 if (steal == -1) 394 return -ENOMEM; 395 /* find someone with a surface reg and nuke their BO */ 396 reg = &rdev->surface_regs[steal]; 397 old_object = reg->bo; 398 /* blow away the mapping */ 399 DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object); 400 ttm_bo_unmap_virtual(&old_object->tbo); 401 old_object->surface_reg = -1; 402 i = steal; 403 } 404 405 bo->surface_reg = i; 406 reg->bo = bo; 407 408 out: 409 radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch, 410 bo->tbo.mem.mm_node->start << PAGE_SHIFT, 411 bo->tbo.num_pages << PAGE_SHIFT); 412 return 0; 413 } 414 415 static void radeon_bo_clear_surface_reg(struct radeon_bo *bo) 416 { 417 struct radeon_device *rdev = bo->rdev; 418 struct radeon_surface_reg *reg; 419 420 if (bo->surface_reg == -1) 421 return; 422 423 reg = &rdev->surface_regs[bo->surface_reg]; 424 radeon_clear_surface_reg(rdev, bo->surface_reg); 425 426 reg->bo = NULL; 427 bo->surface_reg = -1; 428 } 429 430 int radeon_bo_set_tiling_flags(struct radeon_bo *bo, 431 uint32_t tiling_flags, uint32_t pitch) 432 { 433 int r; 434 435 r = radeon_bo_reserve(bo, false); 436 if (unlikely(r != 0)) 437 return r; 438 bo->tiling_flags = tiling_flags; 439 bo->pitch = pitch; 440 radeon_bo_unreserve(bo); 441 return 0; 442 } 443 444 void radeon_bo_get_tiling_flags(struct radeon_bo *bo, 445 uint32_t *tiling_flags, 446 uint32_t *pitch) 447 { 448 BUG_ON(!atomic_read(&bo->tbo.reserved)); 449 if (tiling_flags) 450 *tiling_flags = bo->tiling_flags; 451 if (pitch) 452 *pitch = bo->pitch; 453 } 454 455 int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved, 456 bool force_drop) 457 { 458 BUG_ON(!atomic_read(&bo->tbo.reserved)); 459 460 if (!(bo->tiling_flags & RADEON_TILING_SURFACE)) 461 return 0; 462 463 if (force_drop) { 464 radeon_bo_clear_surface_reg(bo); 465 return 0; 466 } 467 468 if (bo->tbo.mem.mem_type != TTM_PL_VRAM) { 469 if (!has_moved) 470 return 0; 471 472 if (bo->surface_reg >= 0) 473 radeon_bo_clear_surface_reg(bo); 474 return 0; 475 } 476 477 if ((bo->surface_reg >= 0) && !has_moved) 478 return 0; 479 480 return radeon_bo_get_surface_reg(bo); 481 } 482 483 void radeon_bo_move_notify(struct ttm_buffer_object *bo, 484 struct ttm_mem_reg *mem) 485 { 486 struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo); 487 radeon_bo_check_tiling(rbo, 0, 1); 488 } 489 490 void radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo) 491 { 492 struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo); 493 radeon_bo_check_tiling(rbo, 0, 0); 494 } 495