1 /* 2 * Copyright 2014 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #ifndef KFD_PRIV_H_INCLUDED 24 #define KFD_PRIV_H_INCLUDED 25 26 #include <linux/hashtable.h> 27 #include <linux/mmu_notifier.h> 28 #include <linux/mutex.h> 29 #include <linux/types.h> 30 #include <linux/atomic.h> 31 #include <linux/workqueue.h> 32 #include <linux/spinlock.h> 33 #include <linux/kfd_ioctl.h> 34 #include <linux/idr.h> 35 #include <linux/kfifo.h> 36 #include <linux/seq_file.h> 37 #include <linux/kref.h> 38 #include <kgd_kfd_interface.h> 39 40 #include "amd_shared.h" 41 42 #define KFD_MAX_RING_ENTRY_SIZE 8 43 44 #define KFD_SYSFS_FILE_MODE 0444 45 46 /* GPU ID hash width in bits */ 47 #define KFD_GPU_ID_HASH_WIDTH 16 48 49 /* Use upper bits of mmap offset to store KFD driver specific information. 50 * BITS[63:62] - Encode MMAP type 51 * BITS[61:46] - Encode gpu_id. To identify to which GPU the offset belongs to 52 * BITS[45:0] - MMAP offset value 53 * 54 * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these 55 * defines are w.r.t to PAGE_SIZE 56 */ 57 #define KFD_MMAP_TYPE_SHIFT (62 - PAGE_SHIFT) 58 #define KFD_MMAP_TYPE_MASK (0x3ULL << KFD_MMAP_TYPE_SHIFT) 59 #define KFD_MMAP_TYPE_DOORBELL (0x3ULL << KFD_MMAP_TYPE_SHIFT) 60 #define KFD_MMAP_TYPE_EVENTS (0x2ULL << KFD_MMAP_TYPE_SHIFT) 61 #define KFD_MMAP_TYPE_RESERVED_MEM (0x1ULL << KFD_MMAP_TYPE_SHIFT) 62 63 #define KFD_MMAP_GPU_ID_SHIFT (46 - PAGE_SHIFT) 64 #define KFD_MMAP_GPU_ID_MASK (((1ULL << KFD_GPU_ID_HASH_WIDTH) - 1) \ 65 << KFD_MMAP_GPU_ID_SHIFT) 66 #define KFD_MMAP_GPU_ID(gpu_id) ((((uint64_t)gpu_id) << KFD_MMAP_GPU_ID_SHIFT)\ 67 & KFD_MMAP_GPU_ID_MASK) 68 #define KFD_MMAP_GPU_ID_GET(offset) ((offset & KFD_MMAP_GPU_ID_MASK) \ 69 >> KFD_MMAP_GPU_ID_SHIFT) 70 71 #define KFD_MMAP_OFFSET_VALUE_MASK (0x3FFFFFFFFFFFULL >> PAGE_SHIFT) 72 #define KFD_MMAP_OFFSET_VALUE_GET(offset) (offset & KFD_MMAP_OFFSET_VALUE_MASK) 73 74 /* 75 * When working with cp scheduler we should assign the HIQ manually or via 76 * the amdgpu driver to a fixed hqd slot, here are the fixed HIQ hqd slot 77 * definitions for Kaveri. In Kaveri only the first ME queues participates 78 * in the cp scheduling taking that in mind we set the HIQ slot in the 79 * second ME. 80 */ 81 #define KFD_CIK_HIQ_PIPE 4 82 #define KFD_CIK_HIQ_QUEUE 0 83 84 /* Macro for allocating structures */ 85 #define kfd_alloc_struct(ptr_to_struct) \ 86 ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL)) 87 88 #define KFD_MAX_NUM_OF_PROCESSES 512 89 #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024 90 91 /* 92 * Size of the per-process TBA+TMA buffer: 2 pages 93 * 94 * The first page is the TBA used for the CWSR ISA code. The second 95 * page is used as TMA for daisy changing a user-mode trap handler. 96 */ 97 #define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2) 98 #define KFD_CWSR_TMA_OFFSET PAGE_SIZE 99 100 #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \ 101 (KFD_MAX_NUM_OF_PROCESSES * \ 102 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) 103 104 #define KFD_KERNEL_QUEUE_SIZE 2048 105 106 /* 107 * 512 = 0x200 108 * The doorbell index distance between SDMA RLC (2*i) and (2*i+1) in the 109 * same SDMA engine on SOC15, which has 8-byte doorbells for SDMA. 110 * 512 8-byte doorbell distance (i.e. one page away) ensures that SDMA RLC 111 * (2*i+1) doorbells (in terms of the lower 12 bit address) lie exactly in 112 * the OFFSET and SIZE set in registers like BIF_SDMA0_DOORBELL_RANGE. 113 */ 114 #define KFD_QUEUE_DOORBELL_MIRROR_OFFSET 512 115 116 117 /* 118 * Kernel module parameter to specify maximum number of supported queues per 119 * device 120 */ 121 extern int max_num_of_queues_per_device; 122 123 124 /* Kernel module parameter to specify the scheduling policy */ 125 extern int sched_policy; 126 127 /* 128 * Kernel module parameter to specify the maximum process 129 * number per HW scheduler 130 */ 131 extern int hws_max_conc_proc; 132 133 extern int cwsr_enable; 134 135 /* 136 * Kernel module parameter to specify whether to send sigterm to HSA process on 137 * unhandled exception 138 */ 139 extern int send_sigterm; 140 141 /* 142 * This kernel module is used to simulate large bar machine on non-large bar 143 * enabled machines. 144 */ 145 extern int debug_largebar; 146 147 /* 148 * Ignore CRAT table during KFD initialization, can be used to work around 149 * broken CRAT tables on some AMD systems 150 */ 151 extern int ignore_crat; 152 153 /* 154 * Set sh_mem_config.retry_disable on Vega10 155 */ 156 extern int noretry; 157 158 /* 159 * Halt if HWS hang is detected 160 */ 161 extern int halt_if_hws_hang; 162 163 enum cache_policy { 164 cache_policy_coherent, 165 cache_policy_noncoherent 166 }; 167 168 #define KFD_IS_SOC15(chip) ((chip) >= CHIP_VEGA10) 169 170 struct kfd_event_interrupt_class { 171 bool (*interrupt_isr)(struct kfd_dev *dev, 172 const uint32_t *ih_ring_entry, uint32_t *patched_ihre, 173 bool *patched_flag); 174 void (*interrupt_wq)(struct kfd_dev *dev, 175 const uint32_t *ih_ring_entry); 176 }; 177 178 struct kfd_device_info { 179 enum amd_asic_type asic_family; 180 const struct kfd_event_interrupt_class *event_interrupt_class; 181 unsigned int max_pasid_bits; 182 unsigned int max_no_of_hqd; 183 unsigned int doorbell_size; 184 size_t ih_ring_entry_size; 185 uint8_t num_of_watch_points; 186 uint16_t mqd_size_aligned; 187 bool supports_cwsr; 188 bool needs_iommu_device; 189 bool needs_pci_atomics; 190 unsigned int num_sdma_engines; 191 unsigned int num_sdma_queues_per_engine; 192 }; 193 194 struct kfd_mem_obj { 195 uint32_t range_start; 196 uint32_t range_end; 197 uint64_t gpu_addr; 198 uint32_t *cpu_ptr; 199 void *gtt_mem; 200 }; 201 202 struct kfd_vmid_info { 203 uint32_t first_vmid_kfd; 204 uint32_t last_vmid_kfd; 205 uint32_t vmid_num_kfd; 206 }; 207 208 struct kfd_dev { 209 struct kgd_dev *kgd; 210 211 const struct kfd_device_info *device_info; 212 struct pci_dev *pdev; 213 214 unsigned int id; /* topology stub index */ 215 216 phys_addr_t doorbell_base; /* Start of actual doorbells used by 217 * KFD. It is aligned for mapping 218 * into user mode 219 */ 220 size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell 221 * to HW doorbell, GFX reserved some 222 * at the start) 223 */ 224 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells 225 * page used by kernel queue 226 */ 227 228 struct kgd2kfd_shared_resources shared_resources; 229 struct kfd_vmid_info vm_info; 230 231 const struct kfd2kgd_calls *kfd2kgd; 232 struct mutex doorbell_mutex; 233 DECLARE_BITMAP(doorbell_available_index, 234 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS); 235 236 void *gtt_mem; 237 uint64_t gtt_start_gpu_addr; 238 void *gtt_start_cpu_ptr; 239 void *gtt_sa_bitmap; 240 struct mutex gtt_sa_lock; 241 unsigned int gtt_sa_chunk_size; 242 unsigned int gtt_sa_num_of_chunks; 243 244 /* Interrupts */ 245 struct kfifo ih_fifo; 246 struct workqueue_struct *ih_wq; 247 struct work_struct interrupt_work; 248 spinlock_t interrupt_lock; 249 250 /* QCM Device instance */ 251 struct device_queue_manager *dqm; 252 253 bool init_complete; 254 /* 255 * Interrupts of interest to KFD are copied 256 * from the HW ring into a SW ring. 257 */ 258 bool interrupts_active; 259 260 /* Debug manager */ 261 struct kfd_dbgmgr *dbgmgr; 262 263 /* Firmware versions */ 264 uint16_t mec_fw_version; 265 uint16_t sdma_fw_version; 266 267 /* Maximum process number mapped to HW scheduler */ 268 unsigned int max_proc_per_quantum; 269 270 /* CWSR */ 271 bool cwsr_enabled; 272 const void *cwsr_isa; 273 unsigned int cwsr_isa_size; 274 275 /* xGMI */ 276 uint64_t hive_id; 277 278 bool pci_atomic_requested; 279 280 /* SRAM ECC flag */ 281 atomic_t sram_ecc_flag; 282 }; 283 284 enum kfd_mempool { 285 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1, 286 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2, 287 KFD_MEMPOOL_FRAMEBUFFER = 3, 288 }; 289 290 /* Character device interface */ 291 int kfd_chardev_init(void); 292 void kfd_chardev_exit(void); 293 struct device *kfd_chardev(void); 294 295 /** 296 * enum kfd_unmap_queues_filter 297 * 298 * @KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE: Preempts single queue. 299 * 300 * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the 301 * running queues list. 302 * 303 * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to 304 * specific process. 305 * 306 */ 307 enum kfd_unmap_queues_filter { 308 KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE, 309 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 310 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 311 KFD_UNMAP_QUEUES_FILTER_BY_PASID 312 }; 313 314 /** 315 * enum kfd_queue_type 316 * 317 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type. 318 * 319 * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type. 320 * 321 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type. 322 * 323 * @KFD_QUEUE_TYPE_DIQ: DIQ queue type. 324 */ 325 enum kfd_queue_type { 326 KFD_QUEUE_TYPE_COMPUTE, 327 KFD_QUEUE_TYPE_SDMA, 328 KFD_QUEUE_TYPE_HIQ, 329 KFD_QUEUE_TYPE_DIQ 330 }; 331 332 enum kfd_queue_format { 333 KFD_QUEUE_FORMAT_PM4, 334 KFD_QUEUE_FORMAT_AQL 335 }; 336 337 /** 338 * struct queue_properties 339 * 340 * @type: The queue type. 341 * 342 * @queue_id: Queue identifier. 343 * 344 * @queue_address: Queue ring buffer address. 345 * 346 * @queue_size: Queue ring buffer size. 347 * 348 * @priority: Defines the queue priority relative to other queues in the 349 * process. 350 * This is just an indication and HW scheduling may override the priority as 351 * necessary while keeping the relative prioritization. 352 * the priority granularity is from 0 to f which f is the highest priority. 353 * currently all queues are initialized with the highest priority. 354 * 355 * @queue_percent: This field is partially implemented and currently a zero in 356 * this field defines that the queue is non active. 357 * 358 * @read_ptr: User space address which points to the number of dwords the 359 * cp read from the ring buffer. This field updates automatically by the H/W. 360 * 361 * @write_ptr: Defines the number of dwords written to the ring buffer. 362 * 363 * @doorbell_ptr: This field aim is to notify the H/W of new packet written to 364 * the queue ring buffer. This field should be similar to write_ptr and the 365 * user should update this field after he updated the write_ptr. 366 * 367 * @doorbell_off: The doorbell offset in the doorbell pci-bar. 368 * 369 * @is_interop: Defines if this is a interop queue. Interop queue means that 370 * the queue can access both graphics and compute resources. 371 * 372 * @is_evicted: Defines if the queue is evicted. Only active queues 373 * are evicted, rendering them inactive. 374 * 375 * @is_active: Defines if the queue is active or not. @is_active and 376 * @is_evicted are protected by the DQM lock. 377 * 378 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid 379 * of the queue. 380 * 381 * This structure represents the queue properties for each queue no matter if 382 * it's user mode or kernel mode queue. 383 * 384 */ 385 struct queue_properties { 386 enum kfd_queue_type type; 387 enum kfd_queue_format format; 388 unsigned int queue_id; 389 uint64_t queue_address; 390 uint64_t queue_size; 391 uint32_t priority; 392 uint32_t queue_percent; 393 uint32_t *read_ptr; 394 uint32_t *write_ptr; 395 void __iomem *doorbell_ptr; 396 uint32_t doorbell_off; 397 bool is_interop; 398 bool is_evicted; 399 bool is_active; 400 /* Not relevant for user mode queues in cp scheduling */ 401 unsigned int vmid; 402 /* Relevant only for sdma queues*/ 403 uint32_t sdma_engine_id; 404 uint32_t sdma_queue_id; 405 uint32_t sdma_vm_addr; 406 /* Relevant only for VI */ 407 uint64_t eop_ring_buffer_address; 408 uint32_t eop_ring_buffer_size; 409 uint64_t ctx_save_restore_area_address; 410 uint32_t ctx_save_restore_area_size; 411 uint32_t ctl_stack_size; 412 uint64_t tba_addr; 413 uint64_t tma_addr; 414 /* Relevant for CU */ 415 uint32_t cu_mask_count; /* Must be a multiple of 32 */ 416 uint32_t *cu_mask; 417 }; 418 419 /** 420 * struct queue 421 * 422 * @list: Queue linked list. 423 * 424 * @mqd: The queue MQD. 425 * 426 * @mqd_mem_obj: The MQD local gpu memory object. 427 * 428 * @gart_mqd_addr: The MQD gart mc address. 429 * 430 * @properties: The queue properties. 431 * 432 * @mec: Used only in no cp scheduling mode and identifies to micro engine id 433 * that the queue should be execute on. 434 * 435 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe 436 * id. 437 * 438 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot. 439 * 440 * @process: The kfd process that created this queue. 441 * 442 * @device: The kfd device that created this queue. 443 * 444 * This structure represents user mode compute queues. 445 * It contains all the necessary data to handle such queues. 446 * 447 */ 448 449 struct queue { 450 struct list_head list; 451 void *mqd; 452 struct kfd_mem_obj *mqd_mem_obj; 453 uint64_t gart_mqd_addr; 454 struct queue_properties properties; 455 456 uint32_t mec; 457 uint32_t pipe; 458 uint32_t queue; 459 460 unsigned int sdma_id; 461 unsigned int doorbell_id; 462 463 struct kfd_process *process; 464 struct kfd_dev *device; 465 }; 466 467 /* 468 * Please read the kfd_mqd_manager.h description. 469 */ 470 enum KFD_MQD_TYPE { 471 KFD_MQD_TYPE_COMPUTE = 0, /* for no cp scheduling */ 472 KFD_MQD_TYPE_HIQ, /* for hiq */ 473 KFD_MQD_TYPE_CP, /* for cp queues and diq */ 474 KFD_MQD_TYPE_SDMA, /* for sdma queues */ 475 KFD_MQD_TYPE_MAX 476 }; 477 478 struct scheduling_resources { 479 unsigned int vmid_mask; 480 enum kfd_queue_type type; 481 uint64_t queue_mask; 482 uint64_t gws_mask; 483 uint32_t oac_mask; 484 uint32_t gds_heap_base; 485 uint32_t gds_heap_size; 486 }; 487 488 struct process_queue_manager { 489 /* data */ 490 struct kfd_process *process; 491 struct list_head queues; 492 unsigned long *queue_slot_bitmap; 493 }; 494 495 struct qcm_process_device { 496 /* The Device Queue Manager that owns this data */ 497 struct device_queue_manager *dqm; 498 struct process_queue_manager *pqm; 499 /* Queues list */ 500 struct list_head queues_list; 501 struct list_head priv_queue_list; 502 503 unsigned int queue_count; 504 unsigned int vmid; 505 bool is_debug; 506 unsigned int evicted; /* eviction counter, 0=active */ 507 508 /* This flag tells if we should reset all wavefronts on 509 * process termination 510 */ 511 bool reset_wavefronts; 512 513 /* 514 * All the memory management data should be here too 515 */ 516 uint64_t gds_context_area; 517 /* Contains page table flags such as AMDGPU_PTE_VALID since gfx9 */ 518 uint64_t page_table_base; 519 uint32_t sh_mem_config; 520 uint32_t sh_mem_bases; 521 uint32_t sh_mem_ape1_base; 522 uint32_t sh_mem_ape1_limit; 523 uint32_t gds_size; 524 uint32_t num_gws; 525 uint32_t num_oac; 526 uint32_t sh_hidden_private_base; 527 528 /* CWSR memory */ 529 void *cwsr_kaddr; 530 uint64_t cwsr_base; 531 uint64_t tba_addr; 532 uint64_t tma_addr; 533 534 /* IB memory */ 535 uint64_t ib_base; 536 void *ib_kaddr; 537 538 /* doorbell resources per process per device */ 539 unsigned long *doorbell_bitmap; 540 }; 541 542 /* KFD Memory Eviction */ 543 544 /* Approx. wait time before attempting to restore evicted BOs */ 545 #define PROCESS_RESTORE_TIME_MS 100 546 /* Approx. back off time if restore fails due to lack of memory */ 547 #define PROCESS_BACK_OFF_TIME_MS 100 548 /* Approx. time before evicting the process again */ 549 #define PROCESS_ACTIVE_TIME_MS 10 550 551 /* 8 byte handle containing GPU ID in the most significant 4 bytes and 552 * idr_handle in the least significant 4 bytes 553 */ 554 #define MAKE_HANDLE(gpu_id, idr_handle) \ 555 (((uint64_t)(gpu_id) << 32) + idr_handle) 556 #define GET_GPU_ID(handle) (handle >> 32) 557 #define GET_IDR_HANDLE(handle) (handle & 0xFFFFFFFF) 558 559 enum kfd_pdd_bound { 560 PDD_UNBOUND = 0, 561 PDD_BOUND, 562 PDD_BOUND_SUSPENDED, 563 }; 564 565 /* Data that is per-process-per device. */ 566 struct kfd_process_device { 567 /* 568 * List of all per-device data for a process. 569 * Starts from kfd_process.per_device_data. 570 */ 571 struct list_head per_device_list; 572 573 /* The device that owns this data. */ 574 struct kfd_dev *dev; 575 576 /* The process that owns this kfd_process_device. */ 577 struct kfd_process *process; 578 579 /* per-process-per device QCM data structure */ 580 struct qcm_process_device qpd; 581 582 /*Apertures*/ 583 uint64_t lds_base; 584 uint64_t lds_limit; 585 uint64_t gpuvm_base; 586 uint64_t gpuvm_limit; 587 uint64_t scratch_base; 588 uint64_t scratch_limit; 589 590 /* VM context for GPUVM allocations */ 591 struct file *drm_file; 592 void *vm; 593 594 /* GPUVM allocations storage */ 595 struct idr alloc_idr; 596 597 /* Flag used to tell the pdd has dequeued from the dqm. 598 * This is used to prevent dev->dqm->ops.process_termination() from 599 * being called twice when it is already called in IOMMU callback 600 * function. 601 */ 602 bool already_dequeued; 603 604 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */ 605 enum kfd_pdd_bound bound; 606 }; 607 608 #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd) 609 610 /* Process data */ 611 struct kfd_process { 612 /* 613 * kfd_process are stored in an mm_struct*->kfd_process* 614 * hash table (kfd_processes in kfd_process.c) 615 */ 616 struct hlist_node kfd_processes; 617 618 /* 619 * Opaque pointer to mm_struct. We don't hold a reference to 620 * it so it should never be dereferenced from here. This is 621 * only used for looking up processes by their mm. 622 */ 623 void *mm; 624 625 struct kref ref; 626 struct work_struct release_work; 627 628 struct mutex mutex; 629 630 /* 631 * In any process, the thread that started main() is the lead 632 * thread and outlives the rest. 633 * It is here because amd_iommu_bind_pasid wants a task_struct. 634 * It can also be used for safely getting a reference to the 635 * mm_struct of the process. 636 */ 637 struct task_struct *lead_thread; 638 639 /* We want to receive a notification when the mm_struct is destroyed */ 640 struct mmu_notifier mmu_notifier; 641 642 /* Use for delayed freeing of kfd_process structure */ 643 struct rcu_head rcu; 644 645 unsigned int pasid; 646 unsigned int doorbell_index; 647 648 /* 649 * List of kfd_process_device structures, 650 * one for each device the process is using. 651 */ 652 struct list_head per_device_data; 653 654 struct process_queue_manager pqm; 655 656 /*Is the user space process 32 bit?*/ 657 bool is_32bit_user_mode; 658 659 /* Event-related data */ 660 struct mutex event_mutex; 661 /* Event ID allocator and lookup */ 662 struct idr event_idr; 663 /* Event page */ 664 struct kfd_signal_page *signal_page; 665 size_t signal_mapped_size; 666 size_t signal_event_count; 667 bool signal_event_limit_reached; 668 669 /* Information used for memory eviction */ 670 void *kgd_process_info; 671 /* Eviction fence that is attached to all the BOs of this process. The 672 * fence will be triggered during eviction and new one will be created 673 * during restore 674 */ 675 struct dma_fence *ef; 676 677 /* Work items for evicting and restoring BOs */ 678 struct delayed_work eviction_work; 679 struct delayed_work restore_work; 680 /* seqno of the last scheduled eviction */ 681 unsigned int last_eviction_seqno; 682 /* Approx. the last timestamp (in jiffies) when the process was 683 * restored after an eviction 684 */ 685 unsigned long last_restore_timestamp; 686 }; 687 688 #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */ 689 extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); 690 extern struct srcu_struct kfd_processes_srcu; 691 692 /** 693 * Ioctl function type. 694 * 695 * \param filep pointer to file structure. 696 * \param p amdkfd process pointer. 697 * \param data pointer to arg that was copied from user. 698 */ 699 typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p, 700 void *data); 701 702 struct amdkfd_ioctl_desc { 703 unsigned int cmd; 704 int flags; 705 amdkfd_ioctl_t *func; 706 unsigned int cmd_drv; 707 const char *name; 708 }; 709 bool kfd_dev_is_large_bar(struct kfd_dev *dev); 710 711 int kfd_process_create_wq(void); 712 void kfd_process_destroy_wq(void); 713 struct kfd_process *kfd_create_process(struct file *filep); 714 struct kfd_process *kfd_get_process(const struct task_struct *); 715 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid); 716 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm); 717 void kfd_unref_process(struct kfd_process *p); 718 int kfd_process_evict_queues(struct kfd_process *p); 719 int kfd_process_restore_queues(struct kfd_process *p); 720 void kfd_suspend_all_processes(void); 721 int kfd_resume_all_processes(void); 722 723 int kfd_process_device_init_vm(struct kfd_process_device *pdd, 724 struct file *drm_file); 725 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, 726 struct kfd_process *p); 727 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, 728 struct kfd_process *p); 729 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, 730 struct kfd_process *p); 731 732 int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process, 733 struct vm_area_struct *vma); 734 735 /* KFD process API for creating and translating handles */ 736 int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd, 737 void *mem); 738 void *kfd_process_device_translate_handle(struct kfd_process_device *p, 739 int handle); 740 void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd, 741 int handle); 742 743 /* Process device data iterator */ 744 struct kfd_process_device *kfd_get_first_process_device_data( 745 struct kfd_process *p); 746 struct kfd_process_device *kfd_get_next_process_device_data( 747 struct kfd_process *p, 748 struct kfd_process_device *pdd); 749 bool kfd_has_process_device_data(struct kfd_process *p); 750 751 /* PASIDs */ 752 int kfd_pasid_init(void); 753 void kfd_pasid_exit(void); 754 bool kfd_set_pasid_limit(unsigned int new_limit); 755 unsigned int kfd_get_pasid_limit(void); 756 unsigned int kfd_pasid_alloc(void); 757 void kfd_pasid_free(unsigned int pasid); 758 759 /* Doorbells */ 760 size_t kfd_doorbell_process_slice(struct kfd_dev *kfd); 761 int kfd_doorbell_init(struct kfd_dev *kfd); 762 void kfd_doorbell_fini(struct kfd_dev *kfd); 763 int kfd_doorbell_mmap(struct kfd_dev *dev, struct kfd_process *process, 764 struct vm_area_struct *vma); 765 void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd, 766 unsigned int *doorbell_off); 767 void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr); 768 u32 read_kernel_doorbell(u32 __iomem *db); 769 void write_kernel_doorbell(void __iomem *db, u32 value); 770 void write_kernel_doorbell64(void __iomem *db, u64 value); 771 unsigned int kfd_doorbell_id_to_offset(struct kfd_dev *kfd, 772 struct kfd_process *process, 773 unsigned int doorbell_id); 774 phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev, 775 struct kfd_process *process); 776 int kfd_alloc_process_doorbells(struct kfd_process *process); 777 void kfd_free_process_doorbells(struct kfd_process *process); 778 779 /* GTT Sub-Allocator */ 780 781 int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, 782 struct kfd_mem_obj **mem_obj); 783 784 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj); 785 786 extern struct device *kfd_device; 787 788 /* Topology */ 789 int kfd_topology_init(void); 790 void kfd_topology_shutdown(void); 791 int kfd_topology_add_device(struct kfd_dev *gpu); 792 int kfd_topology_remove_device(struct kfd_dev *gpu); 793 struct kfd_topology_device *kfd_topology_device_by_proximity_domain( 794 uint32_t proximity_domain); 795 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id); 796 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id); 797 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev); 798 struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd); 799 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev); 800 int kfd_numa_node_to_apic_id(int numa_node_id); 801 802 /* Interrupts */ 803 int kfd_interrupt_init(struct kfd_dev *dev); 804 void kfd_interrupt_exit(struct kfd_dev *dev); 805 bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry); 806 bool interrupt_is_wanted(struct kfd_dev *dev, 807 const uint32_t *ih_ring_entry, 808 uint32_t *patched_ihre, bool *flag); 809 810 /* amdkfd Apertures */ 811 int kfd_init_apertures(struct kfd_process *process); 812 813 /* Queue Context Management */ 814 int init_queue(struct queue **q, const struct queue_properties *properties); 815 void uninit_queue(struct queue *q); 816 void print_queue_properties(struct queue_properties *q); 817 void print_queue(struct queue *q); 818 819 struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type, 820 struct kfd_dev *dev); 821 struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type, 822 struct kfd_dev *dev); 823 struct mqd_manager *mqd_manager_init_cik_hawaii(enum KFD_MQD_TYPE type, 824 struct kfd_dev *dev); 825 struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type, 826 struct kfd_dev *dev); 827 struct mqd_manager *mqd_manager_init_vi_tonga(enum KFD_MQD_TYPE type, 828 struct kfd_dev *dev); 829 struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type, 830 struct kfd_dev *dev); 831 struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev); 832 void device_queue_manager_uninit(struct device_queue_manager *dqm); 833 struct kernel_queue *kernel_queue_init(struct kfd_dev *dev, 834 enum kfd_queue_type type); 835 void kernel_queue_uninit(struct kernel_queue *kq); 836 int kfd_process_vm_fault(struct device_queue_manager *dqm, unsigned int pasid); 837 838 /* Process Queue Manager */ 839 struct process_queue_node { 840 struct queue *q; 841 struct kernel_queue *kq; 842 struct list_head process_queue_list; 843 }; 844 845 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd); 846 void kfd_process_dequeue_from_all_devices(struct kfd_process *p); 847 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p); 848 void pqm_uninit(struct process_queue_manager *pqm); 849 int pqm_create_queue(struct process_queue_manager *pqm, 850 struct kfd_dev *dev, 851 struct file *f, 852 struct queue_properties *properties, 853 unsigned int *qid); 854 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid); 855 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid, 856 struct queue_properties *p); 857 int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid, 858 struct queue_properties *p); 859 struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm, 860 unsigned int qid); 861 int pqm_get_wave_state(struct process_queue_manager *pqm, 862 unsigned int qid, 863 void __user *ctl_stack, 864 u32 *ctl_stack_used_size, 865 u32 *save_area_used_size); 866 867 int amdkfd_fence_wait_timeout(unsigned int *fence_addr, 868 unsigned int fence_value, 869 unsigned int timeout_ms); 870 871 /* Packet Manager */ 872 873 #define KFD_FENCE_COMPLETED (100) 874 #define KFD_FENCE_INIT (10) 875 876 struct packet_manager { 877 struct device_queue_manager *dqm; 878 struct kernel_queue *priv_queue; 879 struct mutex lock; 880 bool allocated; 881 struct kfd_mem_obj *ib_buffer_obj; 882 unsigned int ib_size_bytes; 883 884 const struct packet_manager_funcs *pmf; 885 }; 886 887 struct packet_manager_funcs { 888 /* Support ASIC-specific packet formats for PM4 packets */ 889 int (*map_process)(struct packet_manager *pm, uint32_t *buffer, 890 struct qcm_process_device *qpd); 891 int (*runlist)(struct packet_manager *pm, uint32_t *buffer, 892 uint64_t ib, size_t ib_size_in_dwords, bool chain); 893 int (*set_resources)(struct packet_manager *pm, uint32_t *buffer, 894 struct scheduling_resources *res); 895 int (*map_queues)(struct packet_manager *pm, uint32_t *buffer, 896 struct queue *q, bool is_static); 897 int (*unmap_queues)(struct packet_manager *pm, uint32_t *buffer, 898 enum kfd_queue_type type, 899 enum kfd_unmap_queues_filter mode, 900 uint32_t filter_param, bool reset, 901 unsigned int sdma_engine); 902 int (*query_status)(struct packet_manager *pm, uint32_t *buffer, 903 uint64_t fence_address, uint32_t fence_value); 904 int (*release_mem)(uint64_t gpu_addr, uint32_t *buffer); 905 906 /* Packet sizes */ 907 int map_process_size; 908 int runlist_size; 909 int set_resources_size; 910 int map_queues_size; 911 int unmap_queues_size; 912 int query_status_size; 913 int release_mem_size; 914 }; 915 916 extern const struct packet_manager_funcs kfd_vi_pm_funcs; 917 extern const struct packet_manager_funcs kfd_v9_pm_funcs; 918 919 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm); 920 void pm_uninit(struct packet_manager *pm); 921 int pm_send_set_resources(struct packet_manager *pm, 922 struct scheduling_resources *res); 923 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues); 924 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address, 925 uint32_t fence_value); 926 927 int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type, 928 enum kfd_unmap_queues_filter mode, 929 uint32_t filter_param, bool reset, 930 unsigned int sdma_engine); 931 932 void pm_release_ib(struct packet_manager *pm); 933 934 /* Following PM funcs can be shared among VI and AI */ 935 unsigned int pm_build_pm4_header(unsigned int opcode, size_t packet_size); 936 int pm_set_resources_vi(struct packet_manager *pm, uint32_t *buffer, 937 struct scheduling_resources *res); 938 939 uint64_t kfd_get_number_elems(struct kfd_dev *kfd); 940 941 /* Events */ 942 extern const struct kfd_event_interrupt_class event_interrupt_class_cik; 943 extern const struct kfd_event_interrupt_class event_interrupt_class_v9; 944 945 extern const struct kfd_device_global_init_class device_global_init_class_cik; 946 947 void kfd_event_init_process(struct kfd_process *p); 948 void kfd_event_free_process(struct kfd_process *p); 949 int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma); 950 int kfd_wait_on_events(struct kfd_process *p, 951 uint32_t num_events, void __user *data, 952 bool all, uint32_t user_timeout_ms, 953 uint32_t *wait_result); 954 void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id, 955 uint32_t valid_id_bits); 956 void kfd_signal_iommu_event(struct kfd_dev *dev, 957 unsigned int pasid, unsigned long address, 958 bool is_write_requested, bool is_execute_requested); 959 void kfd_signal_hw_exception_event(unsigned int pasid); 960 int kfd_set_event(struct kfd_process *p, uint32_t event_id); 961 int kfd_reset_event(struct kfd_process *p, uint32_t event_id); 962 int kfd_event_page_set(struct kfd_process *p, void *kernel_address, 963 uint64_t size); 964 int kfd_event_create(struct file *devkfd, struct kfd_process *p, 965 uint32_t event_type, bool auto_reset, uint32_t node_id, 966 uint32_t *event_id, uint32_t *event_trigger_data, 967 uint64_t *event_page_offset, uint32_t *event_slot_index); 968 int kfd_event_destroy(struct kfd_process *p, uint32_t event_id); 969 970 void kfd_signal_vm_fault_event(struct kfd_dev *dev, unsigned int pasid, 971 struct kfd_vm_fault_info *info); 972 973 void kfd_signal_reset_event(struct kfd_dev *dev); 974 975 void kfd_flush_tlb(struct kfd_process_device *pdd); 976 977 int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p); 978 979 bool kfd_is_locked(void); 980 981 /* Debugfs */ 982 #if defined(CONFIG_DEBUG_FS) 983 984 void kfd_debugfs_init(void); 985 void kfd_debugfs_fini(void); 986 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data); 987 int pqm_debugfs_mqds(struct seq_file *m, void *data); 988 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data); 989 int dqm_debugfs_hqds(struct seq_file *m, void *data); 990 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data); 991 int pm_debugfs_runlist(struct seq_file *m, void *data); 992 993 int kfd_debugfs_hang_hws(struct kfd_dev *dev); 994 int pm_debugfs_hang_hws(struct packet_manager *pm); 995 int dqm_debugfs_execute_queues(struct device_queue_manager *dqm); 996 997 #else 998 999 static inline void kfd_debugfs_init(void) {} 1000 static inline void kfd_debugfs_fini(void) {} 1001 1002 #endif 1003 1004 #endif 1005