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 33 #include <linux/dma-mapping.h> 34 #include <linux/pagemap.h> 35 #include <linux/pci.h> 36 #include <linux/seq_file.h> 37 #include <linux/slab.h> 38 #include <linux/swap.h> 39 #include <linux/swiotlb.h> 40 41 #include <drm/drm_agpsupport.h> 42 #include <drm/drm_debugfs.h> 43 #include <drm/drm_device.h> 44 #include <drm/drm_file.h> 45 #include <drm/drm_prime.h> 46 #include <drm/radeon_drm.h> 47 #include <drm/ttm/ttm_bo_api.h> 48 #include <drm/ttm/ttm_bo_driver.h> 49 #include <drm/ttm/ttm_module.h> 50 #include <drm/ttm/ttm_page_alloc.h> 51 #include <drm/ttm/ttm_placement.h> 52 53 #include "radeon_reg.h" 54 #include "radeon.h" 55 56 #ifdef __amd64__ 57 #include "efifb.h" 58 #endif 59 60 #if NEFIFB > 0 61 #include <machine/efifbvar.h> 62 #endif 63 64 #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT) 65 66 static int radeon_ttm_debugfs_init(struct radeon_device *rdev); 67 static void radeon_ttm_debugfs_fini(struct radeon_device *rdev); 68 69 static struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev) 70 { 71 struct radeon_mman *mman; 72 struct radeon_device *rdev; 73 74 mman = container_of(bdev, struct radeon_mman, bdev); 75 rdev = container_of(mman, struct radeon_device, mman); 76 return rdev; 77 } 78 79 static int radeon_init_mem_type(struct ttm_bo_device *bdev, uint32_t type, 80 struct ttm_mem_type_manager *man) 81 { 82 struct radeon_device *rdev; 83 84 rdev = radeon_get_rdev(bdev); 85 86 switch (type) { 87 case TTM_PL_SYSTEM: 88 /* System memory */ 89 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; 90 man->available_caching = TTM_PL_MASK_CACHING; 91 man->default_caching = TTM_PL_FLAG_CACHED; 92 break; 93 case TTM_PL_TT: 94 man->func = &ttm_bo_manager_func; 95 man->gpu_offset = rdev->mc.gtt_start; 96 man->available_caching = TTM_PL_MASK_CACHING; 97 man->default_caching = TTM_PL_FLAG_CACHED; 98 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA; 99 #if IS_ENABLED(CONFIG_AGP) 100 if (rdev->flags & RADEON_IS_AGP) { 101 if (!rdev->ddev->agp) { 102 DRM_ERROR("AGP is not enabled for memory type %u\n", 103 (unsigned)type); 104 return -EINVAL; 105 } 106 if (!rdev->ddev->agp->cant_use_aperture) 107 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; 108 man->available_caching = TTM_PL_FLAG_UNCACHED | 109 TTM_PL_FLAG_WC; 110 man->default_caching = TTM_PL_FLAG_WC; 111 } 112 #endif 113 break; 114 case TTM_PL_VRAM: 115 /* "On-card" video ram */ 116 man->func = &ttm_bo_manager_func; 117 man->gpu_offset = rdev->mc.vram_start; 118 man->flags = TTM_MEMTYPE_FLAG_FIXED | 119 TTM_MEMTYPE_FLAG_MAPPABLE; 120 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC; 121 man->default_caching = TTM_PL_FLAG_WC; 122 break; 123 default: 124 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type); 125 return -EINVAL; 126 } 127 return 0; 128 } 129 130 static void radeon_evict_flags(struct ttm_buffer_object *bo, 131 struct ttm_placement *placement) 132 { 133 static const struct ttm_place placements = { 134 .fpfn = 0, 135 .lpfn = 0, 136 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM 137 }; 138 139 struct radeon_bo *rbo; 140 141 if (!radeon_ttm_bo_is_radeon_bo(bo)) { 142 placement->placement = &placements; 143 placement->busy_placement = &placements; 144 placement->num_placement = 1; 145 placement->num_busy_placement = 1; 146 return; 147 } 148 rbo = container_of(bo, struct radeon_bo, tbo); 149 switch (bo->mem.mem_type) { 150 case TTM_PL_VRAM: 151 if (rbo->rdev->ring[radeon_copy_ring_index(rbo->rdev)].ready == false) 152 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU); 153 else if (rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size && 154 bo->mem.start < (rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT)) { 155 unsigned fpfn = rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT; 156 int i; 157 158 /* Try evicting to the CPU inaccessible part of VRAM 159 * first, but only set GTT as busy placement, so this 160 * BO will be evicted to GTT rather than causing other 161 * BOs to be evicted from VRAM 162 */ 163 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM | 164 RADEON_GEM_DOMAIN_GTT); 165 rbo->placement.num_busy_placement = 0; 166 for (i = 0; i < rbo->placement.num_placement; i++) { 167 if (rbo->placements[i].flags & TTM_PL_FLAG_VRAM) { 168 if (rbo->placements[i].fpfn < fpfn) 169 rbo->placements[i].fpfn = fpfn; 170 } else { 171 rbo->placement.busy_placement = 172 &rbo->placements[i]; 173 rbo->placement.num_busy_placement = 1; 174 } 175 } 176 } else 177 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT); 178 break; 179 case TTM_PL_TT: 180 default: 181 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU); 182 } 183 *placement = rbo->placement; 184 } 185 186 static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp) 187 { 188 struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo); 189 190 if (radeon_ttm_tt_has_userptr(bo->ttm)) 191 return -EPERM; 192 return drm_vma_node_verify_access(&rbo->tbo.base.vma_node, filp); 193 } 194 195 static void radeon_move_null(struct ttm_buffer_object *bo, 196 struct ttm_mem_reg *new_mem) 197 { 198 struct ttm_mem_reg *old_mem = &bo->mem; 199 200 BUG_ON(old_mem->mm_node != NULL); 201 *old_mem = *new_mem; 202 new_mem->mm_node = NULL; 203 } 204 205 static int radeon_move_blit(struct ttm_buffer_object *bo, 206 bool evict, bool no_wait_gpu, 207 struct ttm_mem_reg *new_mem, 208 struct ttm_mem_reg *old_mem) 209 { 210 struct radeon_device *rdev; 211 uint64_t old_start, new_start; 212 struct radeon_fence *fence; 213 unsigned num_pages; 214 int r, ridx; 215 216 rdev = radeon_get_rdev(bo->bdev); 217 ridx = radeon_copy_ring_index(rdev); 218 old_start = (u64)old_mem->start << PAGE_SHIFT; 219 new_start = (u64)new_mem->start << PAGE_SHIFT; 220 221 switch (old_mem->mem_type) { 222 case TTM_PL_VRAM: 223 old_start += rdev->mc.vram_start; 224 break; 225 case TTM_PL_TT: 226 old_start += rdev->mc.gtt_start; 227 break; 228 default: 229 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type); 230 return -EINVAL; 231 } 232 switch (new_mem->mem_type) { 233 case TTM_PL_VRAM: 234 new_start += rdev->mc.vram_start; 235 break; 236 case TTM_PL_TT: 237 new_start += rdev->mc.gtt_start; 238 break; 239 default: 240 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type); 241 return -EINVAL; 242 } 243 if (!rdev->ring[ridx].ready) { 244 DRM_ERROR("Trying to move memory with ring turned off.\n"); 245 return -EINVAL; 246 } 247 248 BUILD_BUG_ON((PAGE_SIZE % RADEON_GPU_PAGE_SIZE) != 0); 249 250 num_pages = new_mem->num_pages * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); 251 fence = radeon_copy(rdev, old_start, new_start, num_pages, bo->base.resv); 252 if (IS_ERR(fence)) 253 return PTR_ERR(fence); 254 255 r = ttm_bo_move_accel_cleanup(bo, &fence->base, evict, new_mem); 256 radeon_fence_unref(&fence); 257 return r; 258 } 259 260 static int radeon_move_vram_ram(struct ttm_buffer_object *bo, 261 bool evict, bool interruptible, 262 bool no_wait_gpu, 263 struct ttm_mem_reg *new_mem) 264 { 265 struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu }; 266 struct ttm_mem_reg *old_mem = &bo->mem; 267 struct ttm_mem_reg tmp_mem; 268 struct ttm_place placements; 269 struct ttm_placement placement; 270 int r; 271 272 tmp_mem = *new_mem; 273 tmp_mem.mm_node = NULL; 274 placement.num_placement = 1; 275 placement.placement = &placements; 276 placement.num_busy_placement = 1; 277 placement.busy_placement = &placements; 278 placements.fpfn = 0; 279 placements.lpfn = 0; 280 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT; 281 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, &ctx); 282 if (unlikely(r)) { 283 return r; 284 } 285 286 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement); 287 if (unlikely(r)) { 288 goto out_cleanup; 289 } 290 291 r = ttm_tt_bind(bo->ttm, &tmp_mem, &ctx); 292 if (unlikely(r)) { 293 goto out_cleanup; 294 } 295 r = radeon_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem); 296 if (unlikely(r)) { 297 goto out_cleanup; 298 } 299 r = ttm_bo_move_ttm(bo, &ctx, new_mem); 300 out_cleanup: 301 ttm_bo_mem_put(bo, &tmp_mem); 302 return r; 303 } 304 305 static int radeon_move_ram_vram(struct ttm_buffer_object *bo, 306 bool evict, bool interruptible, 307 bool no_wait_gpu, 308 struct ttm_mem_reg *new_mem) 309 { 310 struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu }; 311 struct ttm_mem_reg *old_mem = &bo->mem; 312 struct ttm_mem_reg tmp_mem; 313 struct ttm_placement placement; 314 struct ttm_place placements; 315 int r; 316 317 tmp_mem = *new_mem; 318 tmp_mem.mm_node = NULL; 319 placement.num_placement = 1; 320 placement.placement = &placements; 321 placement.num_busy_placement = 1; 322 placement.busy_placement = &placements; 323 placements.fpfn = 0; 324 placements.lpfn = 0; 325 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT; 326 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, &ctx); 327 if (unlikely(r)) { 328 return r; 329 } 330 r = ttm_bo_move_ttm(bo, &ctx, &tmp_mem); 331 if (unlikely(r)) { 332 goto out_cleanup; 333 } 334 r = radeon_move_blit(bo, true, no_wait_gpu, new_mem, old_mem); 335 if (unlikely(r)) { 336 goto out_cleanup; 337 } 338 out_cleanup: 339 ttm_bo_mem_put(bo, &tmp_mem); 340 return r; 341 } 342 343 static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict, 344 struct ttm_operation_ctx *ctx, 345 struct ttm_mem_reg *new_mem) 346 { 347 struct radeon_device *rdev; 348 struct radeon_bo *rbo; 349 struct ttm_mem_reg *old_mem = &bo->mem; 350 int r; 351 352 r = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu); 353 if (r) 354 return r; 355 356 /* Can't move a pinned BO */ 357 rbo = container_of(bo, struct radeon_bo, tbo); 358 if (WARN_ON_ONCE(rbo->pin_count > 0)) 359 return -EINVAL; 360 361 rdev = radeon_get_rdev(bo->bdev); 362 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { 363 radeon_move_null(bo, new_mem); 364 return 0; 365 } 366 if ((old_mem->mem_type == TTM_PL_TT && 367 new_mem->mem_type == TTM_PL_SYSTEM) || 368 (old_mem->mem_type == TTM_PL_SYSTEM && 369 new_mem->mem_type == TTM_PL_TT)) { 370 /* bind is enough */ 371 radeon_move_null(bo, new_mem); 372 return 0; 373 } 374 if (!rdev->ring[radeon_copy_ring_index(rdev)].ready || 375 rdev->asic->copy.copy == NULL) { 376 /* use memcpy */ 377 goto memcpy; 378 } 379 380 if (old_mem->mem_type == TTM_PL_VRAM && 381 new_mem->mem_type == TTM_PL_SYSTEM) { 382 r = radeon_move_vram_ram(bo, evict, ctx->interruptible, 383 ctx->no_wait_gpu, new_mem); 384 } else if (old_mem->mem_type == TTM_PL_SYSTEM && 385 new_mem->mem_type == TTM_PL_VRAM) { 386 r = radeon_move_ram_vram(bo, evict, ctx->interruptible, 387 ctx->no_wait_gpu, new_mem); 388 } else { 389 r = radeon_move_blit(bo, evict, ctx->no_wait_gpu, 390 new_mem, old_mem); 391 } 392 393 if (r) { 394 memcpy: 395 r = ttm_bo_move_memcpy(bo, ctx, new_mem); 396 if (r) { 397 return r; 398 } 399 } 400 401 /* update statistics */ 402 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &rdev->num_bytes_moved); 403 return 0; 404 } 405 406 static int radeon_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) 407 { 408 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 409 struct radeon_device *rdev = radeon_get_rdev(bdev); 410 411 mem->bus.addr = NULL; 412 mem->bus.offset = 0; 413 mem->bus.size = mem->num_pages << PAGE_SHIFT; 414 mem->bus.base = 0; 415 mem->bus.is_iomem = false; 416 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) 417 return -EINVAL; 418 switch (mem->mem_type) { 419 case TTM_PL_SYSTEM: 420 /* system memory */ 421 return 0; 422 case TTM_PL_TT: 423 #if IS_ENABLED(CONFIG_AGP) 424 if (rdev->flags & RADEON_IS_AGP) { 425 /* RADEON_IS_AGP is set only if AGP is active */ 426 mem->bus.offset = mem->start << PAGE_SHIFT; 427 mem->bus.base = rdev->mc.agp_base; 428 mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture; 429 } 430 #endif 431 break; 432 case TTM_PL_VRAM: 433 mem->bus.offset = mem->start << PAGE_SHIFT; 434 /* check if it's visible */ 435 if ((mem->bus.offset + mem->bus.size) > rdev->mc.visible_vram_size) 436 return -EINVAL; 437 mem->bus.base = rdev->mc.aper_base; 438 mem->bus.is_iomem = true; 439 #ifdef __alpha__ 440 /* 441 * Alpha: use bus.addr to hold the ioremap() return, 442 * so we can modify bus.base below. 443 */ 444 if (mem->placement & TTM_PL_FLAG_WC) 445 mem->bus.addr = 446 ioremap_wc(mem->bus.base + mem->bus.offset, 447 mem->bus.size); 448 else 449 mem->bus.addr = 450 ioremap(mem->bus.base + mem->bus.offset, 451 mem->bus.size); 452 if (!mem->bus.addr) 453 return -ENOMEM; 454 455 /* 456 * Alpha: Use just the bus offset plus 457 * the hose/domain memory base for bus.base. 458 * It then can be used to build PTEs for VRAM 459 * access, as done in ttm_bo_vm_fault(). 460 */ 461 mem->bus.base = (mem->bus.base & 0x0ffffffffUL) + 462 rdev->ddev->hose->dense_mem_base; 463 #endif 464 break; 465 default: 466 return -EINVAL; 467 } 468 return 0; 469 } 470 471 static void radeon_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) 472 { 473 } 474 475 /* 476 * TTM backend functions. 477 */ 478 struct radeon_ttm_tt { 479 struct ttm_dma_tt ttm; 480 struct radeon_device *rdev; 481 u64 offset; 482 483 uint64_t userptr; 484 struct mm_struct *usermm; 485 uint32_t userflags; 486 }; 487 488 /* prepare the sg table with the user pages */ 489 static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm) 490 { 491 STUB(); 492 return -ENOSYS; 493 #ifdef notyet 494 struct radeon_device *rdev = radeon_get_rdev(ttm->bdev); 495 struct radeon_ttm_tt *gtt = (void *)ttm; 496 unsigned pinned = 0, nents; 497 int r; 498 499 int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY); 500 enum dma_data_direction direction = write ? 501 DMA_BIDIRECTIONAL : DMA_TO_DEVICE; 502 503 if (current->mm != gtt->usermm) 504 return -EPERM; 505 506 if (gtt->userflags & RADEON_GEM_USERPTR_ANONONLY) { 507 /* check that we only pin down anonymous memory 508 to prevent problems with writeback */ 509 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE; 510 struct vm_area_struct *vma; 511 vma = find_vma(gtt->usermm, gtt->userptr); 512 if (!vma || vma->vm_file || vma->vm_end < end) 513 return -EPERM; 514 } 515 516 do { 517 unsigned num_pages = ttm->num_pages - pinned; 518 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE; 519 struct vm_page **pages = ttm->pages + pinned; 520 521 r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0, 522 pages, NULL); 523 if (r < 0) 524 goto release_pages; 525 526 pinned += r; 527 528 } while (pinned < ttm->num_pages); 529 530 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0, 531 ttm->num_pages << PAGE_SHIFT, 532 GFP_KERNEL); 533 if (r) 534 goto release_sg; 535 536 r = -ENOMEM; 537 nents = dma_map_sg(rdev->dev, ttm->sg->sgl, ttm->sg->nents, direction); 538 if (nents == 0) 539 goto release_sg; 540 541 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, 542 gtt->ttm.dma_address, ttm->num_pages); 543 544 return 0; 545 546 release_sg: 547 kfree(ttm->sg); 548 549 release_pages: 550 release_pages(ttm->pages, pinned); 551 return r; 552 #endif 553 } 554 555 static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm) 556 { 557 STUB(); 558 #ifdef notyet 559 struct radeon_device *rdev = radeon_get_rdev(ttm->bdev); 560 struct radeon_ttm_tt *gtt = (void *)ttm; 561 struct sg_page_iter sg_iter; 562 563 int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY); 564 enum dma_data_direction direction = write ? 565 DMA_BIDIRECTIONAL : DMA_TO_DEVICE; 566 567 /* double check that we don't free the table twice */ 568 if (!ttm->sg->sgl) 569 return; 570 571 /* free the sg table and pages again */ 572 dma_unmap_sg(rdev->dev, ttm->sg->sgl, ttm->sg->nents, direction); 573 574 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) { 575 struct vm_page *page = sg_page_iter_page(&sg_iter); 576 if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY)) 577 set_page_dirty(page); 578 579 mark_page_accessed(page); 580 put_page(page); 581 } 582 583 sg_free_table(ttm->sg); 584 #endif 585 } 586 587 static int radeon_ttm_backend_bind(struct ttm_tt *ttm, 588 struct ttm_mem_reg *bo_mem) 589 { 590 struct radeon_ttm_tt *gtt = (void*)ttm; 591 uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ | 592 RADEON_GART_PAGE_WRITE; 593 int r; 594 595 if (gtt->userptr) { 596 radeon_ttm_tt_pin_userptr(ttm); 597 flags &= ~RADEON_GART_PAGE_WRITE; 598 } 599 600 gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT); 601 if (!ttm->num_pages) { 602 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n", 603 ttm->num_pages, bo_mem, ttm); 604 } 605 if (ttm->caching_state == tt_cached) 606 flags |= RADEON_GART_PAGE_SNOOP; 607 r = radeon_gart_bind(gtt->rdev, gtt->offset, ttm->num_pages, 608 ttm->pages, gtt->ttm.dma_address, flags); 609 if (r) { 610 DRM_ERROR("failed to bind %lu pages at 0x%08X\n", 611 ttm->num_pages, (unsigned)gtt->offset); 612 return r; 613 } 614 return 0; 615 } 616 617 static int radeon_ttm_backend_unbind(struct ttm_tt *ttm) 618 { 619 struct radeon_ttm_tt *gtt = (void *)ttm; 620 621 radeon_gart_unbind(gtt->rdev, gtt->offset, ttm->num_pages); 622 623 if (gtt->userptr) 624 radeon_ttm_tt_unpin_userptr(ttm); 625 626 return 0; 627 } 628 629 static void radeon_ttm_backend_destroy(struct ttm_tt *ttm) 630 { 631 struct radeon_ttm_tt *gtt = (void *)ttm; 632 633 ttm_dma_tt_fini(>t->ttm); 634 kfree(gtt); 635 } 636 637 static struct ttm_backend_func radeon_backend_func = { 638 .bind = &radeon_ttm_backend_bind, 639 .unbind = &radeon_ttm_backend_unbind, 640 .destroy = &radeon_ttm_backend_destroy, 641 }; 642 643 static struct ttm_tt *radeon_ttm_tt_create(struct ttm_buffer_object *bo, 644 uint32_t page_flags) 645 { 646 struct radeon_device *rdev; 647 struct radeon_ttm_tt *gtt; 648 649 rdev = radeon_get_rdev(bo->bdev); 650 #if IS_ENABLED(CONFIG_AGP) 651 if (rdev->flags & RADEON_IS_AGP) { 652 return ttm_agp_tt_create(bo, rdev->ddev->agp, 653 page_flags); 654 } 655 #endif 656 657 gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL); 658 if (gtt == NULL) { 659 return NULL; 660 } 661 gtt->ttm.ttm.func = &radeon_backend_func; 662 gtt->rdev = rdev; 663 if (ttm_dma_tt_init(>t->ttm, bo, page_flags)) { 664 kfree(gtt); 665 return NULL; 666 } 667 return >t->ttm.ttm; 668 } 669 670 static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct ttm_tt *ttm) 671 { 672 if (!ttm || ttm->func != &radeon_backend_func) 673 return NULL; 674 return (struct radeon_ttm_tt *)ttm; 675 } 676 677 static int radeon_ttm_tt_populate(struct ttm_tt *ttm, 678 struct ttm_operation_ctx *ctx) 679 { 680 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); 681 struct radeon_device *rdev; 682 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); 683 684 if (gtt && gtt->userptr) { 685 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL); 686 if (!ttm->sg) 687 return -ENOMEM; 688 689 ttm->page_flags |= TTM_PAGE_FLAG_SG; 690 ttm->state = tt_unbound; 691 return 0; 692 } 693 694 if (slave && ttm->sg) { 695 #ifdef notyet 696 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, 697 gtt->ttm.dma_address, ttm->num_pages); 698 #endif 699 ttm->state = tt_unbound; 700 return 0; 701 } 702 703 rdev = radeon_get_rdev(ttm->bdev); 704 #if IS_ENABLED(CONFIG_AGP) 705 if (rdev->flags & RADEON_IS_AGP) { 706 return ttm_agp_tt_populate(ttm, ctx); 707 } 708 #endif 709 710 #ifdef CONFIG_SWIOTLB 711 if (rdev->need_swiotlb && swiotlb_nr_tbl()) { 712 return ttm_dma_populate(>t->ttm, rdev->dev, ctx); 713 } 714 #endif 715 716 return ttm_populate_and_map_pages(rdev->dev, >t->ttm, ctx); 717 } 718 719 static void radeon_ttm_tt_unpopulate(struct ttm_tt *ttm) 720 { 721 struct radeon_device *rdev; 722 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); 723 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); 724 725 if (gtt && gtt->userptr) { 726 kfree(ttm->sg); 727 ttm->page_flags &= ~TTM_PAGE_FLAG_SG; 728 return; 729 } 730 731 if (slave) 732 return; 733 734 rdev = radeon_get_rdev(ttm->bdev); 735 #if IS_ENABLED(CONFIG_AGP) 736 if (rdev->flags & RADEON_IS_AGP) { 737 ttm_agp_tt_unpopulate(ttm); 738 return; 739 } 740 #endif 741 742 #ifdef CONFIG_SWIOTLB 743 if (rdev->need_swiotlb && swiotlb_nr_tbl()) { 744 ttm_dma_unpopulate(>t->ttm, rdev->dev); 745 return; 746 } 747 #endif 748 749 ttm_unmap_and_unpopulate_pages(rdev->dev, >t->ttm); 750 } 751 752 int radeon_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr, 753 uint32_t flags) 754 { 755 STUB(); 756 return -ENOSYS; 757 #if 0 758 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); 759 760 if (gtt == NULL) 761 return -EINVAL; 762 763 gtt->userptr = addr; 764 gtt->usermm = current->mm; 765 gtt->userflags = flags; 766 return 0; 767 #endif 768 } 769 770 bool radeon_ttm_tt_has_userptr(struct ttm_tt *ttm) 771 { 772 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); 773 774 if (gtt == NULL) 775 return false; 776 777 return !!gtt->userptr; 778 } 779 780 bool radeon_ttm_tt_is_readonly(struct ttm_tt *ttm) 781 { 782 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); 783 784 if (gtt == NULL) 785 return false; 786 787 return !!(gtt->userflags & RADEON_GEM_USERPTR_READONLY); 788 } 789 790 static struct ttm_bo_driver radeon_bo_driver = { 791 .ttm_tt_create = &radeon_ttm_tt_create, 792 .ttm_tt_populate = &radeon_ttm_tt_populate, 793 .ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate, 794 .init_mem_type = &radeon_init_mem_type, 795 .eviction_valuable = ttm_bo_eviction_valuable, 796 .evict_flags = &radeon_evict_flags, 797 .move = &radeon_bo_move, 798 .verify_access = &radeon_verify_access, 799 .move_notify = &radeon_bo_move_notify, 800 .fault_reserve_notify = &radeon_bo_fault_reserve_notify, 801 .io_mem_reserve = &radeon_ttm_io_mem_reserve, 802 .io_mem_free = &radeon_ttm_io_mem_free, 803 }; 804 805 int radeon_ttm_init(struct radeon_device *rdev) 806 { 807 int r; 808 unsigned long stolen_size = 0; 809 810 #if NEFIFB > 0 811 stolen_size = efifb_stolen(); 812 #endif 813 if (stolen_size == 0) 814 stolen_size = 256 * 1024; 815 816 /* No others user of address space so set it to 0 */ 817 #ifdef notyet 818 r = ttm_bo_device_init(&rdev->mman.bdev, 819 &radeon_bo_driver, 820 rdev->ddev->anon_inode->i_mapping, 821 rdev->ddev->vma_offset_manager, 822 dma_addressing_limited(&rdev->pdev->dev)); 823 #else 824 r = ttm_bo_device_init(&rdev->mman.bdev, 825 &radeon_bo_driver, 826 /*rdev->ddev->anon_inode->i_mapping*/ NULL, 827 rdev->ddev->vma_offset_manager, 828 dma_addressing_limited(&rdev->pdev->dev)); 829 #endif 830 if (r) { 831 DRM_ERROR("failed initializing buffer object driver(%d).\n", r); 832 return r; 833 } 834 rdev->mman.bdev.iot = rdev->iot; 835 rdev->mman.bdev.memt = rdev->memt; 836 rdev->mman.bdev.dmat = rdev->dmat; 837 rdev->mman.initialized = true; 838 r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_VRAM, 839 rdev->mc.real_vram_size >> PAGE_SHIFT); 840 if (r) { 841 DRM_ERROR("Failed initializing VRAM heap.\n"); 842 return r; 843 } 844 /* Change the size here instead of the init above so only lpfn is affected */ 845 radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size); 846 847 #ifdef __sparc64__ 848 r = radeon_bo_create(rdev, rdev->fb_offset, PAGE_SIZE, true, 849 RADEON_GEM_DOMAIN_VRAM, 0, NULL, 850 NULL, &rdev->stolen_vga_memory); 851 #else 852 r = radeon_bo_create(rdev, stolen_size, PAGE_SIZE, true, 853 RADEON_GEM_DOMAIN_VRAM, 0, NULL, 854 NULL, &rdev->stolen_vga_memory); 855 #endif 856 if (r) { 857 return r; 858 } 859 r = radeon_bo_reserve(rdev->stolen_vga_memory, false); 860 if (r) 861 return r; 862 r = radeon_bo_pin(rdev->stolen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL); 863 radeon_bo_unreserve(rdev->stolen_vga_memory); 864 if (r) { 865 radeon_bo_unref(&rdev->stolen_vga_memory); 866 return r; 867 } 868 DRM_INFO("radeon: %uM of VRAM memory ready\n", 869 (unsigned) (rdev->mc.real_vram_size / (1024 * 1024))); 870 r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_TT, 871 rdev->mc.gtt_size >> PAGE_SHIFT); 872 if (r) { 873 DRM_ERROR("Failed initializing GTT heap.\n"); 874 return r; 875 } 876 DRM_INFO("radeon: %uM of GTT memory ready.\n", 877 (unsigned)(rdev->mc.gtt_size / (1024 * 1024))); 878 879 r = radeon_ttm_debugfs_init(rdev); 880 if (r) { 881 DRM_ERROR("Failed to init debugfs\n"); 882 return r; 883 } 884 return 0; 885 } 886 887 void radeon_ttm_fini(struct radeon_device *rdev) 888 { 889 int r; 890 891 if (!rdev->mman.initialized) 892 return; 893 radeon_ttm_debugfs_fini(rdev); 894 if (rdev->stolen_vga_memory) { 895 r = radeon_bo_reserve(rdev->stolen_vga_memory, false); 896 if (r == 0) { 897 radeon_bo_unpin(rdev->stolen_vga_memory); 898 radeon_bo_unreserve(rdev->stolen_vga_memory); 899 } 900 radeon_bo_unref(&rdev->stolen_vga_memory); 901 } 902 ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_VRAM); 903 ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_TT); 904 ttm_bo_device_release(&rdev->mman.bdev); 905 radeon_gart_fini(rdev); 906 rdev->mman.initialized = false; 907 DRM_INFO("radeon: ttm finalized\n"); 908 } 909 910 /* this should only be called at bootup or when userspace 911 * isn't running */ 912 void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size) 913 { 914 struct ttm_mem_type_manager *man; 915 916 if (!rdev->mman.initialized) 917 return; 918 919 man = &rdev->mman.bdev.man[TTM_PL_VRAM]; 920 /* this just adjusts TTM size idea, which sets lpfn to the correct value */ 921 man->size = size >> PAGE_SHIFT; 922 } 923 924 #ifdef __linux__ 925 926 static vm_fault_t radeon_ttm_fault(struct vm_fault *vmf) 927 { 928 struct ttm_buffer_object *bo; 929 struct radeon_device *rdev; 930 vm_fault_t ret; 931 932 bo = (struct ttm_buffer_object *)vmf->vma->vm_private_data; 933 if (bo == NULL) 934 return VM_FAULT_NOPAGE; 935 936 rdev = radeon_get_rdev(bo->bdev); 937 down_read(&rdev->pm.mclk_lock); 938 ret = ttm_bo_vm_fault(vmf); 939 up_read(&rdev->pm.mclk_lock); 940 return ret; 941 } 942 943 static struct vm_operations_struct radeon_ttm_vm_ops = { 944 .fault = radeon_ttm_fault, 945 .open = ttm_bo_vm_open, 946 .close = ttm_bo_vm_close, 947 .access = ttm_bo_vm_access 948 }; 949 950 int radeon_mmap(struct file *filp, struct vm_area_struct *vma) 951 { 952 int r; 953 struct drm_file *file_priv = filp->private_data; 954 struct radeon_device *rdev = file_priv->minor->dev->dev_private; 955 956 if (rdev == NULL) 957 return -EINVAL; 958 959 r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev); 960 if (unlikely(r != 0)) 961 return r; 962 963 vma->vm_ops = &radeon_ttm_vm_ops; 964 return 0; 965 } 966 #else 967 968 static struct uvm_pagerops radeon_ttm_vm_ops; 969 static const struct uvm_pagerops *ttm_vm_ops = NULL; 970 971 static int 972 radeon_ttm_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, vm_page_t *pps, 973 int npages, int centeridx, vm_fault_t fault_type, 974 vm_prot_t access_type, int flags) 975 { 976 struct drm_gem_object *bo; 977 struct radeon_device *rdev; 978 int r; 979 980 bo = (struct drm_gem_object *)ufi->entry->object.uvm_obj; 981 rdev = bo->dev->dev_private; 982 down_read(&rdev->pm.mclk_lock); 983 r = ttm_vm_ops->pgo_fault(ufi, vaddr, pps, npages, centeridx, 984 fault_type, access_type, flags); 985 up_read(&rdev->pm.mclk_lock); 986 return r; 987 } 988 989 struct uvm_object * 990 radeon_mmap(struct file *filp, vm_prot_t accessprot, voff_t off, 991 vsize_t size) 992 { 993 struct drm_file *file_priv = (void *)filp; 994 struct radeon_device *rdev = file_priv->minor->dev->dev_private; 995 struct uvm_object *uobj; 996 997 if (rdev == NULL) 998 return NULL; 999 1000 if (unlikely(off < DRM_FILE_PAGE_OFFSET)) 1001 return NULL; 1002 1003 uobj = ttm_bo_mmap(filp, off, size, &rdev->mman.bdev); 1004 if (unlikely(uobj == NULL)) { 1005 return NULL; 1006 } 1007 if (unlikely(ttm_vm_ops == NULL)) { 1008 ttm_vm_ops = uobj->pgops; 1009 radeon_ttm_vm_ops = *ttm_vm_ops; 1010 radeon_ttm_vm_ops.pgo_fault = &radeon_ttm_fault; 1011 } 1012 uobj->pgops = &radeon_ttm_vm_ops; 1013 return uobj; 1014 } 1015 #endif 1016 1017 #if defined(CONFIG_DEBUG_FS) 1018 1019 static int radeon_mm_dump_table(struct seq_file *m, void *data) 1020 { 1021 struct drm_info_node *node = (struct drm_info_node *)m->private; 1022 unsigned ttm_pl = *(int*)node->info_ent->data; 1023 struct drm_device *dev = node->minor->dev; 1024 struct radeon_device *rdev = dev->dev_private; 1025 struct ttm_mem_type_manager *man = &rdev->mman.bdev.man[ttm_pl]; 1026 struct drm_printer p = drm_seq_file_printer(m); 1027 1028 man->func->debug(man, &p); 1029 return 0; 1030 } 1031 1032 1033 static int ttm_pl_vram = TTM_PL_VRAM; 1034 static int ttm_pl_tt = TTM_PL_TT; 1035 1036 static struct drm_info_list radeon_ttm_debugfs_list[] = { 1037 {"radeon_vram_mm", radeon_mm_dump_table, 0, &ttm_pl_vram}, 1038 {"radeon_gtt_mm", radeon_mm_dump_table, 0, &ttm_pl_tt}, 1039 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL}, 1040 #ifdef CONFIG_SWIOTLB 1041 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL} 1042 #endif 1043 }; 1044 1045 static int radeon_ttm_vram_open(struct inode *inode, struct file *filep) 1046 { 1047 struct radeon_device *rdev = inode->i_private; 1048 i_size_write(inode, rdev->mc.mc_vram_size); 1049 filep->private_data = inode->i_private; 1050 return 0; 1051 } 1052 1053 static ssize_t radeon_ttm_vram_read(struct file *f, char __user *buf, 1054 size_t size, loff_t *pos) 1055 { 1056 struct radeon_device *rdev = f->private_data; 1057 ssize_t result = 0; 1058 int r; 1059 1060 if (size & 0x3 || *pos & 0x3) 1061 return -EINVAL; 1062 1063 while (size) { 1064 unsigned long flags; 1065 uint32_t value; 1066 1067 if (*pos >= rdev->mc.mc_vram_size) 1068 return result; 1069 1070 spin_lock_irqsave(&rdev->mmio_idx_lock, flags); 1071 WREG32(RADEON_MM_INDEX, ((uint32_t)*pos) | 0x80000000); 1072 if (rdev->family >= CHIP_CEDAR) 1073 WREG32(EVERGREEN_MM_INDEX_HI, *pos >> 31); 1074 value = RREG32(RADEON_MM_DATA); 1075 spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags); 1076 1077 r = put_user(value, (uint32_t *)buf); 1078 if (r) 1079 return r; 1080 1081 result += 4; 1082 buf += 4; 1083 *pos += 4; 1084 size -= 4; 1085 } 1086 1087 return result; 1088 } 1089 1090 static const struct file_operations radeon_ttm_vram_fops = { 1091 .owner = THIS_MODULE, 1092 .open = radeon_ttm_vram_open, 1093 .read = radeon_ttm_vram_read, 1094 .llseek = default_llseek 1095 }; 1096 1097 static int radeon_ttm_gtt_open(struct inode *inode, struct file *filep) 1098 { 1099 struct radeon_device *rdev = inode->i_private; 1100 i_size_write(inode, rdev->mc.gtt_size); 1101 filep->private_data = inode->i_private; 1102 return 0; 1103 } 1104 1105 static ssize_t radeon_ttm_gtt_read(struct file *f, char __user *buf, 1106 size_t size, loff_t *pos) 1107 { 1108 struct radeon_device *rdev = f->private_data; 1109 ssize_t result = 0; 1110 int r; 1111 1112 while (size) { 1113 loff_t p = *pos / PAGE_SIZE; 1114 unsigned off = *pos & PAGE_MASK; 1115 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off); 1116 struct vm_page *page; 1117 void *ptr; 1118 1119 if (p >= rdev->gart.num_cpu_pages) 1120 return result; 1121 1122 page = rdev->gart.pages[p]; 1123 if (page) { 1124 ptr = kmap(page); 1125 ptr += off; 1126 1127 r = copy_to_user(buf, ptr, cur_size); 1128 kunmap(ptr); 1129 } else 1130 r = clear_user(buf, cur_size); 1131 1132 if (r) 1133 return -EFAULT; 1134 1135 result += cur_size; 1136 buf += cur_size; 1137 *pos += cur_size; 1138 size -= cur_size; 1139 } 1140 1141 return result; 1142 } 1143 1144 static const struct file_operations radeon_ttm_gtt_fops = { 1145 .owner = THIS_MODULE, 1146 .open = radeon_ttm_gtt_open, 1147 .read = radeon_ttm_gtt_read, 1148 .llseek = default_llseek 1149 }; 1150 1151 #endif 1152 1153 static int radeon_ttm_debugfs_init(struct radeon_device *rdev) 1154 { 1155 #if defined(CONFIG_DEBUG_FS) 1156 unsigned count; 1157 1158 struct drm_minor *minor = rdev->ddev->primary; 1159 struct dentry *root = minor->debugfs_root; 1160 1161 rdev->mman.vram = debugfs_create_file("radeon_vram", S_IFREG | S_IRUGO, 1162 root, rdev, 1163 &radeon_ttm_vram_fops); 1164 1165 rdev->mman.gtt = debugfs_create_file("radeon_gtt", S_IFREG | S_IRUGO, 1166 root, rdev, &radeon_ttm_gtt_fops); 1167 1168 count = ARRAY_SIZE(radeon_ttm_debugfs_list); 1169 1170 #ifdef CONFIG_SWIOTLB 1171 if (!(rdev->need_swiotlb && swiotlb_nr_tbl())) 1172 --count; 1173 #endif 1174 1175 return radeon_debugfs_add_files(rdev, radeon_ttm_debugfs_list, count); 1176 #else 1177 1178 return 0; 1179 #endif 1180 } 1181 1182 static void radeon_ttm_debugfs_fini(struct radeon_device *rdev) 1183 { 1184 #if defined(CONFIG_DEBUG_FS) 1185 1186 debugfs_remove(rdev->mman.vram); 1187 rdev->mman.vram = NULL; 1188 1189 debugfs_remove(rdev->mman.gtt); 1190 rdev->mman.gtt = NULL; 1191 #endif 1192 } 1193