1 /* $OpenBSD: radeon_gart.c,v 1.4 2014/02/09 23:57:04 jsg Exp $ */ 2 /* 3 * Copyright 2008 Advanced Micro Devices, Inc. 4 * Copyright 2008 Red Hat Inc. 5 * Copyright 2009 Jerome Glisse. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: Dave Airlie 26 * Alex Deucher 27 * Jerome Glisse 28 */ 29 #include <dev/pci/drm/drmP.h> 30 #include <dev/pci/drm/radeon_drm.h> 31 #include "radeon.h" 32 #include "radeon_reg.h" 33 34 /* 35 * GART 36 * The GART (Graphics Aperture Remapping Table) is an aperture 37 * in the GPU's address space. System pages can be mapped into 38 * the aperture and look like contiguous pages from the GPU's 39 * perspective. A page table maps the pages in the aperture 40 * to the actual backing pages in system memory. 41 * 42 * Radeon GPUs support both an internal GART, as described above, 43 * and AGP. AGP works similarly, but the GART table is configured 44 * and maintained by the northbridge rather than the driver. 45 * Radeon hw has a separate AGP aperture that is programmed to 46 * point to the AGP aperture provided by the northbridge and the 47 * requests are passed through to the northbridge aperture. 48 * Both AGP and internal GART can be used at the same time, however 49 * that is not currently supported by the driver. 50 * 51 * This file handles the common internal GART management. 52 */ 53 54 /* 55 * Common GART table functions. 56 */ 57 /** 58 * radeon_gart_table_ram_alloc - allocate system ram for gart page table 59 * 60 * @rdev: radeon_device pointer 61 * 62 * Allocate system memory for GART page table 63 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the 64 * gart table to be in system memory. 65 * Returns 0 for success, -ENOMEM for failure. 66 */ 67 int radeon_gart_table_ram_alloc(struct radeon_device *rdev) 68 { 69 struct drm_dmamem *dmah; 70 int flags = 0; 71 72 #if defined(__amd64__) || defined(__i386__) 73 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 || 74 rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) { 75 flags |= BUS_DMA_NOCACHE; 76 } 77 #endif 78 dmah = drm_dmamem_alloc(rdev->dmat, rdev->gart.table_size, 0, 79 1, rdev->gart.table_size, flags, 0); 80 if (dmah == NULL) { 81 return -ENOMEM; 82 } 83 84 rdev->gart.dmah = dmah; 85 rdev->gart.table_addr = dmah->map->dm_segs[0].ds_addr; 86 rdev->gart.ptr = dmah->kva; 87 memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size); 88 return 0; 89 } 90 91 /** 92 * radeon_gart_table_ram_free - free system ram for gart page table 93 * 94 * @rdev: radeon_device pointer 95 * 96 * Free system memory for GART page table 97 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the 98 * gart table to be in system memory. 99 */ 100 void radeon_gart_table_ram_free(struct radeon_device *rdev) 101 { 102 if (rdev->gart.ptr == NULL) { 103 return; 104 } 105 drm_dmamem_free(rdev->dmat, rdev->gart.dmah); 106 rdev->gart.ptr = NULL; 107 rdev->gart.table_addr = 0; 108 } 109 110 /** 111 * radeon_gart_table_vram_alloc - allocate vram for gart page table 112 * 113 * @rdev: radeon_device pointer 114 * 115 * Allocate video memory for GART page table 116 * (pcie r4xx, r5xx+). These asics require the 117 * gart table to be in video memory. 118 * Returns 0 for success, error for failure. 119 */ 120 int radeon_gart_table_vram_alloc(struct radeon_device *rdev) 121 { 122 int r; 123 124 if (rdev->gart.robj == NULL) { 125 r = radeon_bo_create(rdev, rdev->gart.table_size, 126 PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM, 127 NULL, &rdev->gart.robj); 128 if (r) { 129 return r; 130 } 131 } 132 return 0; 133 } 134 135 /** 136 * radeon_gart_table_vram_pin - pin gart page table in vram 137 * 138 * @rdev: radeon_device pointer 139 * 140 * Pin the GART page table in vram so it will not be moved 141 * by the memory manager (pcie r4xx, r5xx+). These asics require the 142 * gart table to be in video memory. 143 * Returns 0 for success, error for failure. 144 */ 145 int radeon_gart_table_vram_pin(struct radeon_device *rdev) 146 { 147 uint64_t gpu_addr; 148 int r; 149 150 r = radeon_bo_reserve(rdev->gart.robj, false); 151 if (unlikely(r != 0)) 152 return r; 153 r = radeon_bo_pin(rdev->gart.robj, 154 RADEON_GEM_DOMAIN_VRAM, &gpu_addr); 155 if (r) { 156 radeon_bo_unreserve(rdev->gart.robj); 157 return r; 158 } 159 r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr); 160 if (r) 161 radeon_bo_unpin(rdev->gart.robj); 162 radeon_bo_unreserve(rdev->gart.robj); 163 rdev->gart.table_addr = gpu_addr; 164 return r; 165 } 166 167 /** 168 * radeon_gart_table_vram_unpin - unpin gart page table in vram 169 * 170 * @rdev: radeon_device pointer 171 * 172 * Unpin the GART page table in vram (pcie r4xx, r5xx+). 173 * These asics require the gart table to be in video memory. 174 */ 175 void radeon_gart_table_vram_unpin(struct radeon_device *rdev) 176 { 177 int r; 178 179 if (rdev->gart.robj == NULL) { 180 return; 181 } 182 r = radeon_bo_reserve(rdev->gart.robj, false); 183 if (likely(r == 0)) { 184 radeon_bo_kunmap(rdev->gart.robj); 185 radeon_bo_unpin(rdev->gart.robj); 186 radeon_bo_unreserve(rdev->gart.robj); 187 rdev->gart.ptr = NULL; 188 } 189 } 190 191 /** 192 * radeon_gart_table_vram_free - free gart page table vram 193 * 194 * @rdev: radeon_device pointer 195 * 196 * Free the video memory used for the GART page table 197 * (pcie r4xx, r5xx+). These asics require the gart table to 198 * be in video memory. 199 */ 200 void radeon_gart_table_vram_free(struct radeon_device *rdev) 201 { 202 if (rdev->gart.robj == NULL) { 203 return; 204 } 205 radeon_gart_table_vram_unpin(rdev); 206 radeon_bo_unref(&rdev->gart.robj); 207 } 208 209 /* 210 * Common gart functions. 211 */ 212 /** 213 * radeon_gart_unbind - unbind pages from the gart page table 214 * 215 * @rdev: radeon_device pointer 216 * @offset: offset into the GPU's gart aperture 217 * @pages: number of pages to unbind 218 * 219 * Unbinds the requested pages from the gart page table and 220 * replaces them with the dummy page (all asics). 221 */ 222 void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset, 223 int pages) 224 { 225 unsigned t; 226 unsigned p; 227 int i, j; 228 u64 page_base; 229 230 if (!rdev->gart.ready) { 231 WARN(1, "trying to unbind memory from uninitialized GART !\n"); 232 return; 233 } 234 t = offset / RADEON_GPU_PAGE_SIZE; 235 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); 236 for (i = 0; i < pages; i++, p++) { 237 if (rdev->gart.pages[p]) { 238 rdev->gart.pages[p] = NULL; 239 rdev->gart.pages_addr[p] = rdev->dummy_page.addr; 240 page_base = rdev->gart.pages_addr[p]; 241 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { 242 if (rdev->gart.ptr) { 243 radeon_gart_set_page(rdev, t, page_base); 244 } 245 page_base += RADEON_GPU_PAGE_SIZE; 246 } 247 } 248 } 249 DRM_MEMORYBARRIER(); 250 radeon_gart_tlb_flush(rdev); 251 } 252 253 /** 254 * radeon_gart_bind - bind pages into the gart page table 255 * 256 * @rdev: radeon_device pointer 257 * @offset: offset into the GPU's gart aperture 258 * @pages: number of pages to bind 259 * @pagelist: pages to bind 260 * @dma_addr: DMA addresses of pages 261 * 262 * Binds the requested pages to the gart page table 263 * (all asics). 264 * Returns 0 for success, -EINVAL for failure. 265 */ 266 int radeon_gart_bind(struct radeon_device *rdev, unsigned offset, 267 int pages, struct vm_page **pagelist, bus_addr_t *dma_addr) 268 { 269 unsigned t; 270 unsigned p; 271 uint64_t page_base; 272 int i, j; 273 274 if (!rdev->gart.ready) { 275 WARN(1, "trying to bind memory to uninitialized GART !\n"); 276 return -EINVAL; 277 } 278 t = offset / RADEON_GPU_PAGE_SIZE; 279 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); 280 281 for (i = 0; i < pages; i++, p++) { 282 rdev->gart.pages_addr[p] = dma_addr[i]; 283 rdev->gart.pages[p] = pagelist[i]; 284 if (rdev->gart.ptr) { 285 page_base = rdev->gart.pages_addr[p]; 286 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { 287 radeon_gart_set_page(rdev, t, page_base); 288 page_base += RADEON_GPU_PAGE_SIZE; 289 } 290 } 291 } 292 DRM_MEMORYBARRIER(); 293 radeon_gart_tlb_flush(rdev); 294 return 0; 295 } 296 297 /** 298 * radeon_gart_restore - bind all pages in the gart page table 299 * 300 * @rdev: radeon_device pointer 301 * 302 * Binds all pages in the gart page table (all asics). 303 * Used to rebuild the gart table on device startup or resume. 304 */ 305 void radeon_gart_restore(struct radeon_device *rdev) 306 { 307 int i, j, t; 308 u64 page_base; 309 310 if (!rdev->gart.ptr) { 311 return; 312 } 313 for (i = 0, t = 0; i < rdev->gart.num_cpu_pages; i++) { 314 page_base = rdev->gart.pages_addr[i]; 315 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { 316 radeon_gart_set_page(rdev, t, page_base); 317 page_base += RADEON_GPU_PAGE_SIZE; 318 } 319 } 320 DRM_MEMORYBARRIER(); 321 radeon_gart_tlb_flush(rdev); 322 } 323 324 /** 325 * radeon_gart_init - init the driver info for managing the gart 326 * 327 * @rdev: radeon_device pointer 328 * 329 * Allocate the dummy page and init the gart driver info (all asics). 330 * Returns 0 for success, error for failure. 331 */ 332 int radeon_gart_init(struct radeon_device *rdev) 333 { 334 int r, i; 335 336 if (rdev->gart.pages) { 337 return 0; 338 } 339 /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */ 340 if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) { 341 DRM_ERROR("Page size is smaller than GPU page size!\n"); 342 return -EINVAL; 343 } 344 r = radeon_dummy_page_init(rdev); 345 if (r) 346 return r; 347 /* Compute table size */ 348 rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE; 349 rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE; 350 #ifdef DRMDEBUG 351 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n", 352 rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages); 353 #endif 354 /* Allocate pages table */ 355 rdev->gart.pages = vzalloc(sizeof(void *) * rdev->gart.num_cpu_pages); 356 if (rdev->gart.pages == NULL) { 357 radeon_gart_fini(rdev); 358 return -ENOMEM; 359 } 360 rdev->gart.pages_addr = vzalloc(sizeof(bus_addr_t) * 361 rdev->gart.num_cpu_pages); 362 if (rdev->gart.pages_addr == NULL) { 363 radeon_gart_fini(rdev); 364 return -ENOMEM; 365 } 366 /* set GART entry to point to the dummy page by default */ 367 for (i = 0; i < rdev->gart.num_cpu_pages; i++) { 368 rdev->gart.pages_addr[i] = rdev->dummy_page.addr; 369 } 370 return 0; 371 } 372 373 /** 374 * radeon_gart_fini - tear down the driver info for managing the gart 375 * 376 * @rdev: radeon_device pointer 377 * 378 * Tear down the gart driver info and free the dummy page (all asics). 379 */ 380 void radeon_gart_fini(struct radeon_device *rdev) 381 { 382 if (rdev->gart.pages && rdev->gart.pages_addr && rdev->gart.ready) { 383 /* unbind pages */ 384 radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages); 385 } 386 rdev->gart.ready = false; 387 vfree(rdev->gart.pages); 388 vfree(rdev->gart.pages_addr); 389 rdev->gart.pages = NULL; 390 rdev->gart.pages_addr = NULL; 391 392 radeon_dummy_page_fini(rdev); 393 } 394 395 /* 396 * GPUVM 397 * GPUVM is similar to the legacy gart on older asics, however 398 * rather than there being a single global gart table 399 * for the entire GPU, there are multiple VM page tables active 400 * at any given time. The VM page tables can contain a mix 401 * vram pages and system memory pages and system memory pages 402 * can be mapped as snooped (cached system pages) or unsnooped 403 * (uncached system pages). 404 * Each VM has an ID associated with it and there is a page table 405 * associated with each VMID. When execting a command buffer, 406 * the kernel tells the the ring what VMID to use for that command 407 * buffer. VMIDs are allocated dynamically as commands are submitted. 408 * The userspace drivers maintain their own address space and the kernel 409 * sets up their pages tables accordingly when they submit their 410 * command buffers and a VMID is assigned. 411 * Cayman/Trinity support up to 8 active VMs at any given time; 412 * SI supports 16. 413 */ 414 415 /* 416 * vm helpers 417 * 418 * TODO bind a default page at vm initialization for default address 419 */ 420 421 /** 422 * radeon_vm_num_pde - return the number of page directory entries 423 * 424 * @rdev: radeon_device pointer 425 * 426 * Calculate the number of page directory entries (cayman+). 427 */ 428 static unsigned radeon_vm_num_pdes(struct radeon_device *rdev) 429 { 430 return rdev->vm_manager.max_pfn >> RADEON_VM_BLOCK_SIZE; 431 } 432 433 /** 434 * radeon_vm_directory_size - returns the size of the page directory in bytes 435 * 436 * @rdev: radeon_device pointer 437 * 438 * Calculate the size of the page directory in bytes (cayman+). 439 */ 440 static unsigned radeon_vm_directory_size(struct radeon_device *rdev) 441 { 442 return RADEON_GPU_PAGE_ALIGN(radeon_vm_num_pdes(rdev) * 8); 443 } 444 445 /** 446 * radeon_vm_manager_init - init the vm manager 447 * 448 * @rdev: radeon_device pointer 449 * 450 * Init the vm manager (cayman+). 451 * Returns 0 for success, error for failure. 452 */ 453 int radeon_vm_manager_init(struct radeon_device *rdev) 454 { 455 struct radeon_vm *vm; 456 struct radeon_bo_va *bo_va; 457 int r; 458 unsigned size; 459 460 if (!rdev->vm_manager.enabled) { 461 /* allocate enough for 2 full VM pts */ 462 size = radeon_vm_directory_size(rdev); 463 size += rdev->vm_manager.max_pfn * 8; 464 size *= 2; 465 r = radeon_sa_bo_manager_init(rdev, &rdev->vm_manager.sa_manager, 466 RADEON_GPU_PAGE_ALIGN(size), 467 RADEON_GPU_PAGE_SIZE, 468 RADEON_GEM_DOMAIN_VRAM); 469 if (r) { 470 dev_err(rdev->dev, "failed to allocate vm bo (%dKB)\n", 471 (rdev->vm_manager.max_pfn * 8) >> 10); 472 return r; 473 } 474 475 r = radeon_asic_vm_init(rdev); 476 if (r) 477 return r; 478 479 rdev->vm_manager.enabled = true; 480 481 r = radeon_sa_bo_manager_start(rdev, &rdev->vm_manager.sa_manager); 482 if (r) 483 return r; 484 } 485 486 /* restore page table */ 487 list_for_each_entry(vm, &rdev->vm_manager.lru_vm, list) { 488 if (vm->page_directory == NULL) 489 continue; 490 491 list_for_each_entry(bo_va, &vm->va, vm_list) { 492 bo_va->valid = false; 493 } 494 } 495 return 0; 496 } 497 498 /** 499 * radeon_vm_free_pt - free the page table for a specific vm 500 * 501 * @rdev: radeon_device pointer 502 * @vm: vm to unbind 503 * 504 * Free the page table of a specific vm (cayman+). 505 * 506 * Global and local rwlock must be lock! 507 */ 508 static void radeon_vm_free_pt(struct radeon_device *rdev, 509 struct radeon_vm *vm) 510 { 511 struct radeon_bo_va *bo_va; 512 int i; 513 514 if (!vm->page_directory) 515 return; 516 517 list_del_init(&vm->list); 518 radeon_sa_bo_free(rdev, &vm->page_directory, vm->fence); 519 520 list_for_each_entry(bo_va, &vm->va, vm_list) { 521 bo_va->valid = false; 522 } 523 524 if (vm->page_tables == NULL) 525 return; 526 527 for (i = 0; i < radeon_vm_num_pdes(rdev); i++) 528 radeon_sa_bo_free(rdev, &vm->page_tables[i], vm->fence); 529 530 kfree(vm->page_tables); 531 } 532 533 /** 534 * radeon_vm_manager_fini - tear down the vm manager 535 * 536 * @rdev: radeon_device pointer 537 * 538 * Tear down the VM manager (cayman+). 539 */ 540 void radeon_vm_manager_fini(struct radeon_device *rdev) 541 { 542 struct radeon_vm *vm, *tmp; 543 int i; 544 545 if (!rdev->vm_manager.enabled) 546 return; 547 548 rw_enter_write(&rdev->vm_manager.lock); 549 /* free all allocated page tables */ 550 list_for_each_entry_safe(vm, tmp, &rdev->vm_manager.lru_vm, list) { 551 rw_enter_write(&vm->rwlock); 552 radeon_vm_free_pt(rdev, vm); 553 rw_exit_write(&vm->rwlock); 554 } 555 for (i = 0; i < RADEON_NUM_VM; ++i) { 556 radeon_fence_unref(&rdev->vm_manager.active[i]); 557 } 558 radeon_asic_vm_fini(rdev); 559 rw_exit_write(&rdev->vm_manager.lock); 560 561 radeon_sa_bo_manager_suspend(rdev, &rdev->vm_manager.sa_manager); 562 radeon_sa_bo_manager_fini(rdev, &rdev->vm_manager.sa_manager); 563 rdev->vm_manager.enabled = false; 564 } 565 566 /** 567 * radeon_vm_evict - evict page table to make room for new one 568 * 569 * @rdev: radeon_device pointer 570 * @vm: VM we want to allocate something for 571 * 572 * Evict a VM from the lru, making sure that it isn't @vm. (cayman+). 573 * Returns 0 for success, -ENOMEM for failure. 574 * 575 * Global and local rwlock must be locked! 576 */ 577 static int radeon_vm_evict(struct radeon_device *rdev, struct radeon_vm *vm) 578 { 579 struct radeon_vm *vm_evict; 580 581 if (list_empty(&rdev->vm_manager.lru_vm)) 582 return -ENOMEM; 583 584 vm_evict = list_first_entry(&rdev->vm_manager.lru_vm, 585 struct radeon_vm, list); 586 if (vm_evict == vm) 587 return -ENOMEM; 588 589 rw_enter_write(&vm_evict->rwlock); 590 radeon_vm_free_pt(rdev, vm_evict); 591 rw_exit_write(&vm_evict->rwlock); 592 return 0; 593 } 594 595 /** 596 * radeon_vm_alloc_pt - allocates a page table for a VM 597 * 598 * @rdev: radeon_device pointer 599 * @vm: vm to bind 600 * 601 * Allocate a page table for the requested vm (cayman+). 602 * Returns 0 for success, error for failure. 603 * 604 * Global and local rwlock must be locked! 605 */ 606 int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm) 607 { 608 unsigned pd_size, pts_size; 609 u64 *pd_addr; 610 int r; 611 612 if (vm == NULL) { 613 return -EINVAL; 614 } 615 616 if (vm->page_directory != NULL) { 617 return 0; 618 } 619 620 retry: 621 pd_size = RADEON_GPU_PAGE_ALIGN(radeon_vm_directory_size(rdev)); 622 r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager, 623 &vm->page_directory, pd_size, 624 RADEON_GPU_PAGE_SIZE, false); 625 if (r == -ENOMEM) { 626 r = radeon_vm_evict(rdev, vm); 627 if (r) 628 return r; 629 goto retry; 630 631 } else if (r) { 632 return r; 633 } 634 635 vm->pd_gpu_addr = radeon_sa_bo_gpu_addr(vm->page_directory); 636 637 /* Initially clear the page directory */ 638 pd_addr = radeon_sa_bo_cpu_addr(vm->page_directory); 639 memset(pd_addr, 0, pd_size); 640 641 pts_size = radeon_vm_num_pdes(rdev) * sizeof(struct radeon_sa_bo *); 642 vm->page_tables = kzalloc(pts_size, GFP_KERNEL); 643 644 if (vm->page_tables == NULL) { 645 DRM_ERROR("Cannot allocate memory for page table array\n"); 646 radeon_sa_bo_free(rdev, &vm->page_directory, vm->fence); 647 return -ENOMEM; 648 } 649 650 return 0; 651 } 652 653 /** 654 * radeon_vm_add_to_lru - add VMs page table to LRU list 655 * 656 * @rdev: radeon_device pointer 657 * @vm: vm to add to LRU 658 * 659 * Add the allocated page table to the LRU list (cayman+). 660 * 661 * Global rwlock must be locked! 662 */ 663 void radeon_vm_add_to_lru(struct radeon_device *rdev, struct radeon_vm *vm) 664 { 665 list_del_init(&vm->list); 666 list_add_tail(&vm->list, &rdev->vm_manager.lru_vm); 667 } 668 669 /** 670 * radeon_vm_grab_id - allocate the next free VMID 671 * 672 * @rdev: radeon_device pointer 673 * @vm: vm to allocate id for 674 * @ring: ring we want to submit job to 675 * 676 * Allocate an id for the vm (cayman+). 677 * Returns the fence we need to sync to (if any). 678 * 679 * Global and local rwlock must be locked! 680 */ 681 struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev, 682 struct radeon_vm *vm, int ring) 683 { 684 struct radeon_fence *best[RADEON_NUM_RINGS] = {}; 685 unsigned choices[2] = {}; 686 unsigned i; 687 688 /* check if the id is still valid */ 689 if (vm->fence && vm->fence == rdev->vm_manager.active[vm->id]) 690 return NULL; 691 692 /* we definately need to flush */ 693 radeon_fence_unref(&vm->last_flush); 694 695 /* skip over VMID 0, since it is the system VM */ 696 for (i = 1; i < rdev->vm_manager.nvm; ++i) { 697 struct radeon_fence *fence = rdev->vm_manager.active[i]; 698 699 if (fence == NULL) { 700 /* found a free one */ 701 vm->id = i; 702 return NULL; 703 } 704 705 if (radeon_fence_is_earlier(fence, best[fence->ring])) { 706 best[fence->ring] = fence; 707 choices[fence->ring == ring ? 0 : 1] = i; 708 } 709 } 710 711 for (i = 0; i < 2; ++i) { 712 if (choices[i]) { 713 vm->id = choices[i]; 714 return rdev->vm_manager.active[choices[i]]; 715 } 716 } 717 718 /* should never happen */ 719 BUG(); 720 return NULL; 721 } 722 723 /** 724 * radeon_vm_fence - remember fence for vm 725 * 726 * @rdev: radeon_device pointer 727 * @vm: vm we want to fence 728 * @fence: fence to remember 729 * 730 * Fence the vm (cayman+). 731 * Set the fence used to protect page table and id. 732 * 733 * Global and local rwlock must be locked! 734 */ 735 void radeon_vm_fence(struct radeon_device *rdev, 736 struct radeon_vm *vm, 737 struct radeon_fence *fence) 738 { 739 radeon_fence_unref(&rdev->vm_manager.active[vm->id]); 740 rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence); 741 742 radeon_fence_unref(&vm->fence); 743 vm->fence = radeon_fence_ref(fence); 744 } 745 746 /** 747 * radeon_vm_bo_find - find the bo_va for a specific vm & bo 748 * 749 * @vm: requested vm 750 * @bo: requested buffer object 751 * 752 * Find @bo inside the requested vm (cayman+). 753 * Search inside the @bos vm list for the requested vm 754 * Returns the found bo_va or NULL if none is found 755 * 756 * Object has to be reserved! 757 */ 758 struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm, 759 struct radeon_bo *bo) 760 { 761 struct radeon_bo_va *bo_va; 762 763 list_for_each_entry(bo_va, &bo->va, bo_list) { 764 if (bo_va->vm == vm) { 765 return bo_va; 766 } 767 } 768 return NULL; 769 } 770 771 /** 772 * radeon_vm_bo_add - add a bo to a specific vm 773 * 774 * @rdev: radeon_device pointer 775 * @vm: requested vm 776 * @bo: radeon buffer object 777 * 778 * Add @bo into the requested vm (cayman+). 779 * Add @bo to the list of bos associated with the vm 780 * Returns newly added bo_va or NULL for failure 781 * 782 * Object has to be reserved! 783 */ 784 struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev, 785 struct radeon_vm *vm, 786 struct radeon_bo *bo) 787 { 788 struct radeon_bo_va *bo_va; 789 790 bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL); 791 if (bo_va == NULL) { 792 return NULL; 793 } 794 bo_va->vm = vm; 795 bo_va->bo = bo; 796 bo_va->soffset = 0; 797 bo_va->eoffset = 0; 798 bo_va->flags = 0; 799 bo_va->valid = false; 800 bo_va->ref_count = 1; 801 INIT_LIST_HEAD(&bo_va->bo_list); 802 INIT_LIST_HEAD(&bo_va->vm_list); 803 804 rw_enter_write(&vm->rwlock); 805 list_add(&bo_va->vm_list, &vm->va); 806 list_add_tail(&bo_va->bo_list, &bo->va); 807 rw_exit_write(&vm->rwlock); 808 809 return bo_va; 810 } 811 812 /** 813 * radeon_vm_bo_set_addr - set bos virtual address inside a vm 814 * 815 * @rdev: radeon_device pointer 816 * @bo_va: bo_va to store the address 817 * @soffset: requested offset of the buffer in the VM address space 818 * @flags: attributes of pages (read/write/valid/etc.) 819 * 820 * Set offset of @bo_va (cayman+). 821 * Validate and set the offset requested within the vm address space. 822 * Returns 0 for success, error for failure. 823 * 824 * Object has to be reserved! 825 */ 826 int radeon_vm_bo_set_addr(struct radeon_device *rdev, 827 struct radeon_bo_va *bo_va, 828 uint64_t soffset, 829 uint32_t flags) 830 { 831 uint64_t size = radeon_bo_size(bo_va->bo); 832 uint64_t eoffset, last_offset = 0; 833 struct radeon_vm *vm = bo_va->vm; 834 struct radeon_bo_va *tmp; 835 struct list_head *head; 836 unsigned last_pfn; 837 838 if (soffset) { 839 /* make sure object fit at this offset */ 840 eoffset = soffset + size; 841 if (soffset >= eoffset) { 842 return -EINVAL; 843 } 844 845 last_pfn = eoffset / RADEON_GPU_PAGE_SIZE; 846 if (last_pfn > rdev->vm_manager.max_pfn) { 847 dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n", 848 last_pfn, rdev->vm_manager.max_pfn); 849 return -EINVAL; 850 } 851 852 } else { 853 eoffset = last_pfn = 0; 854 } 855 856 rw_enter_write(&vm->rwlock); 857 head = &vm->va; 858 last_offset = 0; 859 list_for_each_entry(tmp, &vm->va, vm_list) { 860 if (bo_va == tmp) { 861 /* skip over currently modified bo */ 862 continue; 863 } 864 865 if (soffset >= last_offset && eoffset <= tmp->soffset) { 866 /* bo can be added before this one */ 867 break; 868 } 869 if (eoffset > tmp->soffset && soffset < tmp->eoffset) { 870 /* bo and tmp overlap, invalid offset */ 871 dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n", 872 bo_va->bo, (unsigned)bo_va->soffset, tmp->bo, 873 (unsigned)tmp->soffset, (unsigned)tmp->eoffset); 874 rw_exit_write(&vm->rwlock); 875 return -EINVAL; 876 } 877 last_offset = tmp->eoffset; 878 head = &tmp->vm_list; 879 } 880 881 bo_va->soffset = soffset; 882 bo_va->eoffset = eoffset; 883 bo_va->flags = flags; 884 bo_va->valid = false; 885 list_move(&bo_va->vm_list, head); 886 887 rw_exit_write(&vm->rwlock); 888 return 0; 889 } 890 891 /** 892 * radeon_vm_map_gart - get the physical address of a gart page 893 * 894 * @rdev: radeon_device pointer 895 * @addr: the unmapped addr 896 * 897 * Look up the physical address of the page that the pte resolves 898 * to (cayman+). 899 * Returns the physical address of the page. 900 */ 901 uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr) 902 { 903 uint64_t result; 904 905 /* page table offset */ 906 result = rdev->gart.pages_addr[addr >> PAGE_SHIFT]; 907 908 /* in case cpu page size != gpu page size*/ 909 result |= addr & (~PAGE_MASK); 910 911 return result; 912 } 913 914 /** 915 * radeon_vm_update_pdes - make sure that page directory is valid 916 * 917 * @rdev: radeon_device pointer 918 * @vm: requested vm 919 * @start: start of GPU address range 920 * @end: end of GPU address range 921 * 922 * Allocates new page tables if necessary 923 * and updates the page directory (cayman+). 924 * Returns 0 for success, error for failure. 925 * 926 * Global and local rwlock must be locked! 927 */ 928 static int radeon_vm_update_pdes(struct radeon_device *rdev, 929 struct radeon_vm *vm, 930 uint64_t start, uint64_t end) 931 { 932 static const uint32_t incr = RADEON_VM_PTE_COUNT * 8; 933 934 uint64_t last_pde = ~0, last_pt = ~0; 935 unsigned count = 0; 936 uint64_t pt_idx; 937 int r; 938 939 start = (start / RADEON_GPU_PAGE_SIZE) >> RADEON_VM_BLOCK_SIZE; 940 end = (end / RADEON_GPU_PAGE_SIZE) >> RADEON_VM_BLOCK_SIZE; 941 942 /* walk over the address space and update the page directory */ 943 for (pt_idx = start; pt_idx <= end; ++pt_idx) { 944 uint64_t pde, pt; 945 946 if (vm->page_tables[pt_idx]) 947 continue; 948 949 retry: 950 r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager, 951 &vm->page_tables[pt_idx], 952 RADEON_VM_PTE_COUNT * 8, 953 RADEON_GPU_PAGE_SIZE, false); 954 955 if (r == -ENOMEM) { 956 r = radeon_vm_evict(rdev, vm); 957 if (r) 958 return r; 959 goto retry; 960 } else if (r) { 961 return r; 962 } 963 964 pde = vm->pd_gpu_addr + pt_idx * 8; 965 966 pt = radeon_sa_bo_gpu_addr(vm->page_tables[pt_idx]); 967 968 if (((last_pde + 8 * count) != pde) || 969 ((last_pt + incr * count) != pt)) { 970 971 if (count) { 972 radeon_asic_vm_set_page(rdev, last_pde, 973 last_pt, count, incr, 974 RADEON_VM_PAGE_VALID); 975 } 976 977 count = 1; 978 last_pde = pde; 979 last_pt = pt; 980 } else { 981 ++count; 982 } 983 } 984 985 if (count) { 986 radeon_asic_vm_set_page(rdev, last_pde, last_pt, count, 987 incr, RADEON_VM_PAGE_VALID); 988 989 } 990 991 return 0; 992 } 993 994 /** 995 * radeon_vm_update_ptes - make sure that page tables are valid 996 * 997 * @rdev: radeon_device pointer 998 * @vm: requested vm 999 * @start: start of GPU address range 1000 * @end: end of GPU address range 1001 * @dst: destination address to map to 1002 * @flags: mapping flags 1003 * 1004 * Update the page tables in the range @start - @end (cayman+). 1005 * 1006 * Global and local rwlock must be locked! 1007 */ 1008 static void radeon_vm_update_ptes(struct radeon_device *rdev, 1009 struct radeon_vm *vm, 1010 uint64_t start, uint64_t end, 1011 uint64_t dst, uint32_t flags) 1012 { 1013 static const uint64_t mask = RADEON_VM_PTE_COUNT - 1; 1014 1015 uint64_t last_pte = ~0, last_dst = ~0; 1016 unsigned count = 0; 1017 uint64_t addr; 1018 1019 start = start / RADEON_GPU_PAGE_SIZE; 1020 end = end / RADEON_GPU_PAGE_SIZE; 1021 1022 /* walk over the address space and update the page tables */ 1023 for (addr = start; addr < end; ) { 1024 uint64_t pt_idx = addr >> RADEON_VM_BLOCK_SIZE; 1025 unsigned nptes; 1026 uint64_t pte; 1027 1028 if ((addr & ~mask) == (end & ~mask)) 1029 nptes = end - addr; 1030 else 1031 nptes = RADEON_VM_PTE_COUNT - (addr & mask); 1032 1033 pte = radeon_sa_bo_gpu_addr(vm->page_tables[pt_idx]); 1034 pte += (addr & mask) * 8; 1035 1036 if ((last_pte + 8 * count) != pte) { 1037 1038 if (count) { 1039 radeon_asic_vm_set_page(rdev, last_pte, 1040 last_dst, count, 1041 RADEON_GPU_PAGE_SIZE, 1042 flags); 1043 } 1044 1045 count = nptes; 1046 last_pte = pte; 1047 last_dst = dst; 1048 } else { 1049 count += nptes; 1050 } 1051 1052 addr += nptes; 1053 dst += nptes * RADEON_GPU_PAGE_SIZE; 1054 } 1055 1056 if (count) { 1057 radeon_asic_vm_set_page(rdev, last_pte, last_dst, count, 1058 RADEON_GPU_PAGE_SIZE, flags); 1059 } 1060 } 1061 1062 /** 1063 * radeon_vm_bo_update_pte - map a bo into the vm page table 1064 * 1065 * @rdev: radeon_device pointer 1066 * @vm: requested vm 1067 * @bo: radeon buffer object 1068 * @mem: ttm mem 1069 * 1070 * Fill in the page table entries for @bo (cayman+). 1071 * Returns 0 for success, -EINVAL for failure. 1072 * 1073 * Object have to be reserved & global and local rwlock must be locked! 1074 */ 1075 int radeon_vm_bo_update_pte(struct radeon_device *rdev, 1076 struct radeon_vm *vm, 1077 struct radeon_bo *bo, 1078 struct ttm_mem_reg *mem) 1079 { 1080 unsigned ridx = rdev->asic->vm.pt_ring_index; 1081 struct radeon_ring *ring = &rdev->ring[ridx]; 1082 struct radeon_semaphore *sem = NULL; 1083 struct radeon_bo_va *bo_va; 1084 unsigned nptes, npdes, ndw; 1085 uint64_t addr; 1086 int r; 1087 1088 /* nothing to do if vm isn't bound */ 1089 if (vm->page_directory == NULL) 1090 return 0; 1091 1092 bo_va = radeon_vm_bo_find(vm, bo); 1093 if (bo_va == NULL) { 1094 dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm); 1095 return -EINVAL; 1096 } 1097 1098 if (!bo_va->soffset) { 1099 dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n", 1100 bo, vm); 1101 return -EINVAL; 1102 } 1103 1104 if ((bo_va->valid && mem) || (!bo_va->valid && mem == NULL)) 1105 return 0; 1106 1107 bo_va->flags &= ~RADEON_VM_PAGE_VALID; 1108 bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM; 1109 if (mem) { 1110 addr = mem->start << PAGE_SHIFT; 1111 if (mem->mem_type != TTM_PL_SYSTEM) { 1112 bo_va->flags |= RADEON_VM_PAGE_VALID; 1113 bo_va->valid = true; 1114 } 1115 if (mem->mem_type == TTM_PL_TT) { 1116 bo_va->flags |= RADEON_VM_PAGE_SYSTEM; 1117 } else { 1118 addr += rdev->vm_manager.vram_base_offset; 1119 } 1120 } else { 1121 addr = 0; 1122 bo_va->valid = false; 1123 } 1124 1125 if (vm->fence && radeon_fence_signaled(vm->fence)) { 1126 radeon_fence_unref(&vm->fence); 1127 } 1128 1129 if (vm->fence && vm->fence->ring != ridx) { 1130 r = radeon_semaphore_create(rdev, &sem); 1131 if (r) { 1132 return r; 1133 } 1134 } 1135 1136 nptes = radeon_bo_ngpu_pages(bo); 1137 1138 /* assume two extra pdes in case the mapping overlaps the borders */ 1139 npdes = (nptes >> RADEON_VM_BLOCK_SIZE) + 2; 1140 1141 /* estimate number of dw needed */ 1142 /* semaphore, fence and padding */ 1143 ndw = 32; 1144 1145 if (RADEON_VM_BLOCK_SIZE > 11) 1146 /* reserve space for one header for every 2k dwords */ 1147 ndw += (nptes >> 11) * 4; 1148 else 1149 /* reserve space for one header for 1150 every (1 << BLOCK_SIZE) entries */ 1151 ndw += (nptes >> RADEON_VM_BLOCK_SIZE) * 4; 1152 1153 /* reserve space for pte addresses */ 1154 ndw += nptes * 2; 1155 1156 /* reserve space for one header for every 2k dwords */ 1157 ndw += (npdes >> 11) * 4; 1158 1159 /* reserve space for pde addresses */ 1160 ndw += npdes * 2; 1161 1162 r = radeon_ring_lock(rdev, ring, ndw); 1163 if (r) { 1164 return r; 1165 } 1166 1167 if (sem && radeon_fence_need_sync(vm->fence, ridx)) { 1168 radeon_semaphore_sync_rings(rdev, sem, vm->fence->ring, ridx); 1169 radeon_fence_note_sync(vm->fence, ridx); 1170 } 1171 1172 r = radeon_vm_update_pdes(rdev, vm, bo_va->soffset, bo_va->eoffset); 1173 if (r) { 1174 radeon_ring_unlock_undo(rdev, ring); 1175 return r; 1176 } 1177 1178 radeon_vm_update_ptes(rdev, vm, bo_va->soffset, bo_va->eoffset, 1179 addr, bo_va->flags); 1180 1181 radeon_fence_unref(&vm->fence); 1182 r = radeon_fence_emit(rdev, &vm->fence, ridx); 1183 if (r) { 1184 radeon_ring_unlock_undo(rdev, ring); 1185 return r; 1186 } 1187 radeon_ring_unlock_commit(rdev, ring); 1188 radeon_semaphore_free(rdev, &sem, vm->fence); 1189 radeon_fence_unref(&vm->last_flush); 1190 1191 return 0; 1192 } 1193 1194 /** 1195 * radeon_vm_bo_rmv - remove a bo to a specific vm 1196 * 1197 * @rdev: radeon_device pointer 1198 * @bo_va: requested bo_va 1199 * 1200 * Remove @bo_va->bo from the requested vm (cayman+). 1201 * Remove @bo_va->bo from the list of bos associated with the bo_va->vm and 1202 * remove the ptes for @bo_va in the page table. 1203 * Returns 0 for success. 1204 * 1205 * Object have to be reserved! 1206 */ 1207 int radeon_vm_bo_rmv(struct radeon_device *rdev, 1208 struct radeon_bo_va *bo_va) 1209 { 1210 int r = 0; 1211 1212 rw_enter_write(&rdev->vm_manager.lock); 1213 rw_enter_write(&bo_va->vm->rwlock); 1214 if (bo_va->soffset) { 1215 r = radeon_vm_bo_update_pte(rdev, bo_va->vm, bo_va->bo, NULL); 1216 } 1217 rw_exit_write(&rdev->vm_manager.lock); 1218 list_del(&bo_va->vm_list); 1219 rw_exit_write(&bo_va->vm->rwlock); 1220 list_del(&bo_va->bo_list); 1221 1222 kfree(bo_va); 1223 return r; 1224 } 1225 1226 /** 1227 * radeon_vm_bo_invalidate - mark the bo as invalid 1228 * 1229 * @rdev: radeon_device pointer 1230 * @vm: requested vm 1231 * @bo: radeon buffer object 1232 * 1233 * Mark @bo as invalid (cayman+). 1234 */ 1235 void radeon_vm_bo_invalidate(struct radeon_device *rdev, 1236 struct radeon_bo *bo) 1237 { 1238 struct radeon_bo_va *bo_va; 1239 1240 list_for_each_entry(bo_va, &bo->va, bo_list) { 1241 bo_va->valid = false; 1242 } 1243 } 1244 1245 /** 1246 * radeon_vm_init - initialize a vm instance 1247 * 1248 * @rdev: radeon_device pointer 1249 * @vm: requested vm 1250 * 1251 * Init @vm fields (cayman+). 1252 */ 1253 void radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm) 1254 { 1255 vm->id = 0; 1256 vm->fence = NULL; 1257 rw_init(&vm->rwlock, "vmlk"); 1258 INIT_LIST_HEAD(&vm->list); 1259 INIT_LIST_HEAD(&vm->va); 1260 } 1261 1262 /** 1263 * radeon_vm_fini - tear down a vm instance 1264 * 1265 * @rdev: radeon_device pointer 1266 * @vm: requested vm 1267 * 1268 * Tear down @vm (cayman+). 1269 * Unbind the VM and remove all bos from the vm bo list 1270 */ 1271 void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm) 1272 { 1273 struct radeon_bo_va *bo_va, *tmp; 1274 int r; 1275 1276 rw_enter_write(&rdev->vm_manager.lock); 1277 rw_enter_write(&vm->rwlock); 1278 radeon_vm_free_pt(rdev, vm); 1279 rw_exit_write(&rdev->vm_manager.lock); 1280 1281 if (!list_empty(&vm->va)) { 1282 dev_err(rdev->dev, "still active bo inside vm\n"); 1283 } 1284 list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) { 1285 list_del_init(&bo_va->vm_list); 1286 r = radeon_bo_reserve(bo_va->bo, false); 1287 if (!r) { 1288 list_del_init(&bo_va->bo_list); 1289 radeon_bo_unreserve(bo_va->bo); 1290 kfree(bo_va); 1291 } 1292 } 1293 radeon_fence_unref(&vm->fence); 1294 radeon_fence_unref(&vm->last_flush); 1295 rw_exit_write(&vm->rwlock); 1296 } 1297