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