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