1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 /* 3 * Copyright 2014-2022 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24 #ifndef KFD_PRIV_H_INCLUDED 25 #define KFD_PRIV_H_INCLUDED 26 27 #include <linux/hashtable.h> 28 #include <linux/mmu_notifier.h> 29 #include <linux/memremap.h> 30 #include <linux/mutex.h> 31 #include <linux/types.h> 32 #include <linux/atomic.h> 33 #include <linux/workqueue.h> 34 #include <linux/spinlock.h> 35 #include <linux/iosys-map.h> 36 #include <uapi/linux/kfd_ioctl.h> 37 #include <linux/idr.h> 38 #include <linux/kfifo.h> 39 #include <linux/seq_file.h> 40 #include <linux/kref.h> 41 #include <linux/sysfs.h> 42 #include <linux/device_cgroup.h> 43 #include <drm/drm_file.h> 44 #include <drm/drm_drv.h> 45 #include <drm/drm_device.h> 46 #include <drm/drm_ioctl.h> 47 #include <kgd_kfd_interface.h> 48 #include <linux/swap.h> 49 50 #include "amd_shared.h" 51 #include "amdgpu.h" 52 53 #define KFD_MAX_RING_ENTRY_SIZE 8 54 55 #define KFD_SYSFS_FILE_MODE 0444 56 57 /* GPU ID hash width in bits */ 58 #define KFD_GPU_ID_HASH_WIDTH 16 59 60 /* Use upper bits of mmap offset to store KFD driver specific information. 61 * BITS[63:62] - Encode MMAP type 62 * BITS[61:46] - Encode gpu_id. To identify to which GPU the offset belongs to 63 * BITS[45:0] - MMAP offset value 64 * 65 * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these 66 * defines are w.r.t to PAGE_SIZE 67 */ 68 #define KFD_MMAP_TYPE_SHIFT 62 69 #define KFD_MMAP_TYPE_MASK (0x3ULL << KFD_MMAP_TYPE_SHIFT) 70 #define KFD_MMAP_TYPE_DOORBELL (0x3ULL << KFD_MMAP_TYPE_SHIFT) 71 #define KFD_MMAP_TYPE_EVENTS (0x2ULL << KFD_MMAP_TYPE_SHIFT) 72 #define KFD_MMAP_TYPE_RESERVED_MEM (0x1ULL << KFD_MMAP_TYPE_SHIFT) 73 #define KFD_MMAP_TYPE_MMIO (0x0ULL << KFD_MMAP_TYPE_SHIFT) 74 75 #define KFD_MMAP_GPU_ID_SHIFT 46 76 #define KFD_MMAP_GPU_ID_MASK (((1ULL << KFD_GPU_ID_HASH_WIDTH) - 1) \ 77 << KFD_MMAP_GPU_ID_SHIFT) 78 #define KFD_MMAP_GPU_ID(gpu_id) ((((uint64_t)gpu_id) << KFD_MMAP_GPU_ID_SHIFT)\ 79 & KFD_MMAP_GPU_ID_MASK) 80 #define KFD_MMAP_GET_GPU_ID(offset) ((offset & KFD_MMAP_GPU_ID_MASK) \ 81 >> KFD_MMAP_GPU_ID_SHIFT) 82 83 /* 84 * When working with cp scheduler we should assign the HIQ manually or via 85 * the amdgpu driver to a fixed hqd slot, here are the fixed HIQ hqd slot 86 * definitions for Kaveri. In Kaveri only the first ME queues participates 87 * in the cp scheduling taking that in mind we set the HIQ slot in the 88 * second ME. 89 */ 90 #define KFD_CIK_HIQ_PIPE 4 91 #define KFD_CIK_HIQ_QUEUE 0 92 93 /* Macro for allocating structures */ 94 #define kfd_alloc_struct(ptr_to_struct) \ 95 ((typeof(ptr_to_struct)) kzalloc_obj(*ptr_to_struct)) 96 97 #define KFD_MAX_NUM_OF_PROCESSES 512 98 #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024 99 100 /* 101 * Size of the per-process TBA+TMA buffer: 2 pages 102 * 103 * The first chunk is the TBA used for the CWSR ISA code. The second 104 * chunk is used as TMA for user-mode trap handler setup in daisy-chain mode. 105 */ 106 #define KFD_CWSR_TBA_TMA_SIZE (AMDGPU_GPU_PAGE_SIZE * 2) 107 #define KFD_CWSR_TMA_OFFSET (AMDGPU_GPU_PAGE_SIZE + 2048) 108 109 #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \ 110 (KFD_MAX_NUM_OF_PROCESSES * \ 111 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) 112 113 #define KFD_KERNEL_QUEUE_SIZE 2048 114 115 /* KFD_UNMAP_LATENCY_MS is the timeout CP waiting for SDMA preemption. One XCC 116 * can be associated to 2 SDMA engines. queue_preemption_timeout_ms is the time 117 * driver waiting for CP returning the UNMAP_QUEUE fence. Thus the math is 118 * queue_preemption_timeout_ms = sdma_preemption_time * 2 + cp workload 119 * The format here makes CP workload 10% of total timeout 120 */ 121 #define KFD_UNMAP_LATENCY_MS \ 122 ((queue_preemption_timeout_ms - queue_preemption_timeout_ms / 10) >> 1) 123 124 #define KFD_MAX_SDMA_QUEUES 128 125 126 /* 127 * 512 = 0x200 128 * The doorbell index distance between SDMA RLC (2*i) and (2*i+1) in the 129 * same SDMA engine on SOC15, which has 8-byte doorbells for SDMA. 130 * 512 8-byte doorbell distance (i.e. one page away) ensures that SDMA RLC 131 * (2*i+1) doorbells (in terms of the lower 12 bit address) lie exactly in 132 * the OFFSET and SIZE set in registers like BIF_SDMA0_DOORBELL_RANGE. 133 */ 134 #define KFD_QUEUE_DOORBELL_MIRROR_OFFSET 512 135 136 /* 137 * On GC 9.4.3, 9.4.4 and 9.5.0, doorbell writes issued from shaders are 138 * routed to an SDMA engine at 32-byte granularity, i.e. 4 indices with the 139 * 8-byte SDMA doorbells used on SOC15. 140 */ 141 #define KFD_SDMA_SHADER_DOORBELL_GRANULARITY 4 142 143 /** 144 * enum kfd_ioctl_flags - KFD ioctl flags 145 * Various flags that can be set in &amdkfd_ioctl_desc.flags to control how 146 * userspace can use a given ioctl. 147 */ 148 enum kfd_ioctl_flags { 149 /* 150 * @KFD_IOC_FLAG_CHECKPOINT_RESTORE: 151 * Certain KFD ioctls such as AMDKFD_IOC_CRIU_OP can potentially 152 * perform privileged operations and load arbitrary data into MQDs and 153 * eventually HQD registers when the queue is mapped by HWS. In order to 154 * prevent this we should perform additional security checks. 155 * 156 * This is equivalent to callers with the CHECKPOINT_RESTORE capability. 157 * 158 * Note: Since earlier versions of docker do not support CHECKPOINT_RESTORE, 159 * we also allow ioctls with SYS_ADMIN capability. 160 */ 161 KFD_IOC_FLAG_CHECKPOINT_RESTORE = BIT(0), 162 }; 163 /* 164 * Kernel module parameter to specify maximum number of supported queues per 165 * device 166 */ 167 extern int max_num_of_queues_per_device; 168 169 170 /* Kernel module parameter to specify the scheduling policy */ 171 extern int sched_policy; 172 173 /* 174 * Kernel module parameter to specify the maximum process 175 * number per HW scheduler 176 */ 177 extern int hws_max_conc_proc; 178 179 extern int cwsr_enable; 180 181 /* 182 * Kernel module parameter to specify whether to send sigterm to HSA process on 183 * unhandled exception 184 */ 185 extern int send_sigterm; 186 187 /* 188 * This kernel module is used to simulate large bar machine on non-large bar 189 * enabled machines. 190 */ 191 extern int debug_largebar; 192 193 /* Set sh_mem_config.retry_disable on GFX v9 */ 194 extern int amdgpu_noretry; 195 196 /* Halt if HWS hang is detected */ 197 extern int halt_if_hws_hang; 198 199 /* Whether MEC FW support GWS barriers */ 200 extern bool hws_gws_support; 201 202 /* Queue preemption timeout in ms */ 203 extern int queue_preemption_timeout_ms; 204 205 /* 206 * Don't evict process queues on vm fault 207 */ 208 extern int amdgpu_no_queue_eviction_on_vm_fault; 209 210 /* Enable eviction debug messages */ 211 extern bool debug_evictions; 212 213 extern struct mutex kfd_processes_mutex; 214 215 enum cache_policy { 216 cache_policy_coherent, 217 cache_policy_noncoherent 218 }; 219 220 #define KFD_GC_VERSION(dev) (amdgpu_ip_version((dev)->adev, GC_HWIP, 0)) 221 #define KFD_IS_SOC15(dev) ((KFD_GC_VERSION(dev)) >= (IP_VERSION(9, 0, 1))) 222 #define KFD_SUPPORT_XNACK_PER_PROCESS(dev)\ 223 ((KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 2)) || \ 224 (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3)) || \ 225 (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4)) || \ 226 (KFD_GC_VERSION(dev) == IP_VERSION(9, 5, 0))) 227 228 struct kfd_node; 229 230 struct kfd_event_interrupt_class { 231 bool (*interrupt_isr)(struct kfd_node *dev, 232 const uint32_t *ih_ring_entry, uint32_t *patched_ihre, 233 bool *patched_flag); 234 void (*interrupt_wq)(struct kfd_node *dev, 235 const uint32_t *ih_ring_entry); 236 }; 237 238 struct kfd_device_info { 239 uint32_t gfx_target_version; 240 const struct kfd_event_interrupt_class *event_interrupt_class; 241 unsigned int max_pasid_bits; 242 unsigned int max_no_of_hqd; 243 unsigned int doorbell_size; 244 size_t ih_ring_entry_size; 245 uint8_t num_of_watch_points; 246 uint16_t mqd_size_aligned; 247 bool supports_cwsr; 248 bool needs_pci_atomics; 249 uint32_t no_atomic_fw_version; 250 unsigned int num_sdma_queues_per_engine; 251 unsigned int num_reserved_sdma_queues_per_engine; 252 }; 253 254 unsigned int kfd_get_num_sdma_engines(struct kfd_node *kdev); 255 unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_node *kdev); 256 257 struct kfd_mem_obj { 258 uint32_t range_start; 259 uint32_t range_end; 260 uint64_t gpu_addr; 261 uint32_t *cpu_ptr; 262 void *mem; 263 }; 264 265 struct kfd_vmid_info { 266 uint32_t first_vmid_kfd; 267 uint32_t last_vmid_kfd; 268 uint32_t vmid_num_kfd; 269 }; 270 271 #define MAX_KFD_NODES 8 272 273 struct kfd_dev; 274 275 struct kfd_node { 276 unsigned int node_id; 277 struct amdgpu_device *adev; /* Duplicated here along with keeping 278 * a copy in kfd_dev to save a hop 279 */ 280 const struct kfd2kgd_calls *kfd2kgd; /* Duplicated here along with 281 * keeping a copy in kfd_dev to 282 * save a hop 283 */ 284 struct kfd_vmid_info vm_info; 285 unsigned int id; /* topology stub index */ 286 uint32_t xcc_mask; /* Instance mask of XCCs present */ 287 struct amdgpu_xcp *xcp; 288 289 /* Interrupts */ 290 struct kfifo ih_fifo; 291 struct work_struct interrupt_work; 292 spinlock_t interrupt_lock; 293 294 /* 295 * Interrupts of interest to KFD are copied 296 * from the HW ring into a SW ring. 297 */ 298 bool interrupts_active; 299 uint32_t interrupt_bitmap; /* Only used for GFX 9.4.3 */ 300 301 /* QCM Device instance */ 302 struct device_queue_manager *dqm; 303 304 /* Global GWS resource shared between processes */ 305 void *gws; 306 307 /* Clients watching SMI events */ 308 struct list_head smi_clients; 309 spinlock_t smi_lock; 310 uint32_t reset_seq_num; 311 312 /* SRAM ECC flag */ 313 atomic_t sram_ecc_flag; 314 315 /*spm process id */ 316 unsigned int spm_pasid; 317 318 /* Maximum process number mapped to HW scheduler */ 319 unsigned int max_proc_per_quantum; 320 321 unsigned int compute_vmid_bitmap; 322 323 struct kfd_local_mem_info local_mem_info; 324 325 struct kfd_dev *kfd; 326 327 /* Track per device allocated watch points */ 328 uint32_t alloc_watch_ids; 329 spinlock_t watch_points_lock; 330 }; 331 332 struct kfd_dev { 333 struct amdgpu_device *adev; 334 335 struct kfd_device_info device_info; 336 337 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells 338 * page used by kernel queue 339 */ 340 341 struct kgd2kfd_shared_resources shared_resources; 342 343 const struct kfd2kgd_calls *kfd2kgd; 344 struct mutex doorbell_mutex; 345 346 void *gtt_mem; 347 uint64_t gtt_start_gpu_addr; 348 void *gtt_start_cpu_ptr; 349 void *gtt_sa_bitmap; 350 struct mutex gtt_sa_lock; 351 unsigned int gtt_sa_chunk_size; 352 unsigned int gtt_sa_num_of_chunks; 353 354 bool init_complete; 355 356 /* Firmware versions */ 357 uint16_t mec_fw_version; 358 uint16_t mec2_fw_version; 359 uint16_t sdma_fw_version; 360 361 /* CWSR */ 362 bool cwsr_enabled; 363 const void *cwsr_isa; 364 unsigned int cwsr_isa_size; 365 366 /* xGMI */ 367 uint64_t hive_id; 368 369 bool pci_atomic_requested; 370 371 /* Compute Profile ref. count */ 372 atomic_t compute_profile; 373 374 struct ida doorbell_ida; 375 unsigned int max_doorbell_slices; 376 377 int noretry; 378 379 struct kfd_node *nodes[MAX_KFD_NODES]; 380 unsigned int num_nodes; 381 382 struct workqueue_struct *ih_wq; 383 384 /* Kernel doorbells for KFD device */ 385 struct amdgpu_bo *doorbells; 386 387 /* bitmap for dynamic doorbell allocation from doorbell object */ 388 unsigned long *doorbell_bitmap; 389 390 /* for dynamic partitioning */ 391 int kfd_dev_lock; 392 393 atomic_t kfd_processes_count; 394 395 /* Lock for profiler process */ 396 struct mutex profiler_lock; 397 /* Process currently holding the lock */ 398 struct kfd_process *profiler_process; 399 }; 400 401 enum kfd_mempool { 402 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1, 403 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2, 404 KFD_MEMPOOL_FRAMEBUFFER = 3, 405 }; 406 407 /* Character device interface */ 408 int kfd_chardev_init(void); 409 void kfd_chardev_exit(void); 410 void kfd_dev_unmap_mapping_range(loff_t const holebegin, loff_t const holelen); 411 412 /** 413 * enum kfd_unmap_queues_filter - Enum for queue filters. 414 * 415 * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the 416 * running queues list. 417 * 418 * @KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES: Preempts all non-static queues 419 * in the run list. 420 * 421 * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to 422 * specific process. 423 * 424 */ 425 enum kfd_unmap_queues_filter { 426 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES = 1, 427 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES = 2, 428 KFD_UNMAP_QUEUES_FILTER_BY_PASID = 3 429 }; 430 431 /** 432 * enum kfd_queue_type - Enum for various queue types. 433 * 434 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type. 435 * 436 * @KFD_QUEUE_TYPE_SDMA: SDMA user mode queue type. 437 * 438 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type. 439 * 440 * @KFD_QUEUE_TYPE_SDMA_XGMI: Special SDMA queue for XGMI interface. 441 * 442 * @KFD_QUEUE_TYPE_SDMA_BY_ENG_ID: SDMA user mode queue with target SDMA engine ID. 443 */ 444 enum kfd_queue_type { 445 KFD_QUEUE_TYPE_COMPUTE, 446 KFD_QUEUE_TYPE_SDMA, 447 KFD_QUEUE_TYPE_HIQ, 448 KFD_QUEUE_TYPE_SDMA_XGMI, 449 KFD_QUEUE_TYPE_SDMA_BY_ENG_ID, 450 KFD_QUEUE_TYPE_MAX, 451 }; 452 453 enum kfd_queue_format { 454 KFD_QUEUE_FORMAT_PM4, 455 KFD_QUEUE_FORMAT_AQL 456 }; 457 458 enum KFD_QUEUE_PRIORITY { 459 KFD_QUEUE_PRIORITY_MINIMUM = 0, 460 KFD_QUEUE_PRIORITY_MAXIMUM = 15 461 }; 462 463 /** 464 * struct queue_properties 465 * 466 * @type: The queue type. 467 * 468 * @queue_id: Queue identifier. 469 * 470 * @queue_address: Queue ring buffer address. 471 * 472 * @queue_size: Queue ring buffer size. 473 * 474 * @priority: Defines the queue priority relative to other queues in the 475 * process. 476 * This is just an indication and HW scheduling may override the priority as 477 * necessary while keeping the relative prioritization. 478 * the priority granularity is from 0 to f which f is the highest priority. 479 * currently all queues are initialized with the highest priority. 480 * 481 * @queue_percent: This field is partially implemented and currently a zero in 482 * this field defines that the queue is non active. 483 * 484 * @read_ptr: User space address which points to the number of dwords the 485 * cp read from the ring buffer. This field updates automatically by the H/W. 486 * 487 * @write_ptr: Defines the number of dwords written to the ring buffer. 488 * 489 * @doorbell_ptr: Notifies the H/W of new packet written to the queue ring 490 * buffer. This field should be similar to write_ptr and the user should 491 * update this field after updating the write_ptr. 492 * 493 * @doorbell_off: The doorbell offset in the doorbell pci-bar. 494 * 495 * @is_interop: Defines if this is a interop queue. Interop queue means that 496 * the queue can access both graphics and compute resources. 497 * 498 * @is_evicted: Defines if the queue is evicted. Only active queues 499 * are evicted, rendering them inactive. 500 * 501 * @is_active: Defines if the queue is active or not. @is_active and 502 * @is_evicted are protected by the DQM lock. 503 * 504 * @is_gws: Defines if the queue has been updated to be GWS-capable or not. 505 * @is_gws should be protected by the DQM lock, since changing it can yield the 506 * possibility of updating DQM state on number of GWS queues. 507 * 508 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid 509 * of the queue. 510 * 511 * This structure represents the queue properties for each queue no matter if 512 * it's user mode or kernel mode queue. 513 * 514 */ 515 516 struct queue_properties { 517 enum kfd_queue_type type; 518 enum kfd_queue_format format; 519 unsigned int queue_id; 520 uint64_t queue_address; 521 uint64_t queue_size; 522 uint64_t metadata_queue_size; 523 uint32_t priority; 524 uint32_t queue_percent; 525 void __user *read_ptr; 526 void __user *write_ptr; 527 void __iomem *doorbell_ptr; 528 uint32_t doorbell_off; 529 bool is_interop; 530 bool is_evicted; 531 bool is_suspended; 532 bool is_being_destroyed; 533 bool is_active; 534 bool is_gws; 535 uint32_t pm4_target_xcc; 536 bool is_dbg_wa; 537 bool is_user_cu_masked; 538 bool is_reset; 539 /* Not relevant for user mode queues in cp scheduling */ 540 unsigned int vmid; 541 /* Relevant only for sdma queues*/ 542 uint32_t sdma_engine_id; 543 uint32_t sdma_queue_id; 544 uint32_t sdma_vm_addr; 545 /* Relevant only for VI */ 546 uint64_t eop_ring_buffer_address; 547 uint32_t eop_ring_buffer_size; 548 uint64_t ctx_save_restore_area_address; 549 uint32_t ctx_save_restore_area_size; 550 uint32_t ctl_stack_size; 551 uint64_t tba_addr; 552 uint64_t tma_addr; 553 uint64_t exception_status; 554 555 struct amdgpu_bo *wptr_bo; 556 struct amdgpu_bo *rptr_bo; 557 struct amdgpu_bo *ring_bo; 558 struct amdgpu_bo *eop_buf_bo; 559 struct amdgpu_bo *cwsr_bo; 560 }; 561 562 #define QUEUE_IS_ACTIVE(q) ((q).queue_size > 0 && \ 563 (q).queue_address != 0 && \ 564 (q).queue_percent > 0 && \ 565 !(q).is_evicted && \ 566 !(q).is_suspended) 567 568 enum mqd_update_flag { 569 UPDATE_FLAG_DBG_WA_ENABLE = 1, 570 UPDATE_FLAG_DBG_WA_DISABLE = 2, 571 UPDATE_FLAG_IS_GWS = 4, /* quirk for gfx9 IP */ 572 UPDATE_FLAG_PERFCOUNT_ENABLE = 5, 573 UPDATE_FLAG_PERFCOUNT_DISABLE = 6, 574 }; 575 576 struct mqd_update_info { 577 union { 578 struct { 579 uint32_t count; /* Must be a multiple of 32 */ 580 uint32_t *ptr; 581 } cu_mask; 582 }; 583 enum mqd_update_flag update_flag; 584 }; 585 586 /** 587 * struct queue 588 * 589 * @list: Queue linked list. 590 * 591 * @mqd: The queue MQD (memory queue descriptor). 592 * 593 * @mqd_mem_obj: The MQD local gpu memory object. 594 * 595 * @gart_mqd_addr: The MQD gart mc address. 596 * 597 * @properties: The queue properties. 598 * 599 * @mec: Used only in no cp scheduling mode and identifies to micro engine id 600 * that the queue should be executed on. 601 * 602 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe 603 * id. 604 * 605 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot. 606 * 607 * @process: The kfd process that created this queue. 608 * 609 * @device: The kfd device that created this queue. 610 * 611 * @gws: Pointing to gws kgd_mem if this is a gws control queue; NULL 612 * otherwise. 613 * 614 * This structure represents user mode compute queues. 615 * It contains all the necessary data to handle such queues. 616 * 617 */ 618 619 struct queue { 620 struct list_head list; 621 void *mqd; 622 struct kfd_mem_obj *mqd_mem_obj; 623 uint64_t gart_mqd_addr; 624 struct queue_properties properties; 625 626 uint32_t mec; 627 uint32_t pipe; 628 uint32_t queue; 629 630 unsigned int sdma_id; 631 unsigned int doorbell_id; 632 633 struct kfd_process *process; 634 struct kfd_node *device; 635 void *gws; 636 637 /* procfs */ 638 struct kobject kobj; 639 640 void *gang_ctx_bo; 641 uint64_t gang_ctx_gpu_addr; 642 void *gang_ctx_cpu_ptr; 643 uint32_t gang_ctx_array_index; 644 645 struct amdgpu_bo *wptr_bo_gart; 646 647 /* The VRAM-resident MQD BO (mqd_on_vram()) is unpinned at S4 suspend so 648 * TTM evicts it into the hibernation image, and repinned on resume. Set 649 * while the BO is unpinned so the resume path knows to repin it. 650 */ 651 bool needs_mqd_repin; 652 }; 653 654 enum KFD_MQD_TYPE { 655 KFD_MQD_TYPE_HIQ = 0, /* for hiq */ 656 KFD_MQD_TYPE_CP, /* for cp queues and diq */ 657 KFD_MQD_TYPE_SDMA, /* for sdma queues */ 658 KFD_MQD_TYPE_DIQ, /* for diq */ 659 KFD_MQD_TYPE_MAX 660 }; 661 662 enum KFD_PIPE_PRIORITY { 663 KFD_PIPE_PRIORITY_CS_LOW = 0, 664 KFD_PIPE_PRIORITY_CS_MEDIUM, 665 KFD_PIPE_PRIORITY_CS_HIGH 666 }; 667 668 struct scheduling_resources { 669 unsigned int vmid_mask; 670 enum kfd_queue_type type; 671 uint64_t queue_mask; 672 uint64_t gws_mask; 673 uint32_t oac_mask; 674 uint32_t gds_heap_base; 675 uint32_t gds_heap_size; 676 }; 677 678 struct process_queue_manager { 679 /* data */ 680 struct kfd_process *process; 681 struct list_head queues; 682 unsigned long *queue_slot_bitmap; 683 }; 684 685 struct qcm_process_device { 686 /* The Device Queue Manager that owns this data */ 687 struct device_queue_manager *dqm; 688 struct process_queue_manager *pqm; 689 /* Queues list */ 690 struct list_head queues_list; 691 struct list_head priv_queue_list; 692 693 unsigned int queue_count; 694 unsigned int vmid; 695 bool is_debug; 696 unsigned int evicted; /* eviction counter, 0=active */ 697 698 /* Set after fault clean runs; never cleared. 699 * Prevents redundant cleaning from pipelined interrupt workers. 700 */ 701 bool fault_cleaned; 702 703 /* This flag tells if we should reset all wavefronts on 704 * process termination 705 */ 706 bool reset_wavefronts; 707 708 /* This flag tells us if this process has a GWS-capable 709 * queue that will be mapped into the runlist. It's 710 * possible to request a GWS BO, but not have the queue 711 * currently mapped, and this changes how the MAP_PROCESS 712 * PM4 packet is configured. 713 */ 714 bool mapped_gws_queue; 715 716 /* All the memory management data should be here too */ 717 uint64_t gds_context_area; 718 /* Contains page table flags such as AMDGPU_PTE_VALID since gfx9 */ 719 uint64_t page_table_base; 720 uint32_t sh_mem_config; 721 uint32_t sh_mem_bases; 722 uint32_t sh_mem_ape1_base; 723 uint32_t sh_mem_ape1_limit; 724 uint32_t gds_size; 725 uint32_t num_gws; 726 uint32_t num_oac; 727 uint32_t sh_hidden_private_base; 728 uint32_t vm_cntx_cntl; 729 730 /* CWSR memory */ 731 struct kgd_mem *cwsr_mem; 732 struct iosys_map cwsr_map; 733 uint64_t cwsr_base; 734 uint64_t tba_addr; 735 uint64_t tma_addr; 736 737 /* IB memory */ 738 struct kgd_mem *ib_mem; 739 uint64_t ib_base; 740 void *ib_kaddr; 741 742 /* doorbells for kfd process */ 743 struct amdgpu_bo *proc_doorbells; 744 745 /* bitmap for dynamic doorbell allocation from the bo */ 746 unsigned long *doorbell_bitmap; 747 }; 748 749 /* KFD Memory Eviction */ 750 751 /* Approx. wait time before attempting to restore evicted BOs */ 752 #define PROCESS_RESTORE_TIME_MS 100 753 /* Approx. back off time if restore fails due to lack of memory */ 754 #define PROCESS_BACK_OFF_TIME_MS 100 755 /* Approx. time before evicting the process again */ 756 #define PROCESS_ACTIVE_TIME_MS 10 757 758 /* 8 byte handle containing GPU ID in the most significant 4 bytes and 759 * idr_handle in the least significant 4 bytes 760 */ 761 #define MAKE_HANDLE(gpu_id, idr_handle) \ 762 (((uint64_t)(gpu_id) << 32) + idr_handle) 763 #define GET_GPU_ID(handle) (handle >> 32) 764 #define GET_IDR_HANDLE(handle) (handle & 0xFFFFFFFF) 765 766 enum kfd_pdd_bound { 767 PDD_UNBOUND = 0, 768 PDD_BOUND, 769 PDD_BOUND_SUSPENDED, 770 }; 771 772 #define MAX_SYSFS_FILENAME_LEN 15 773 774 /* 775 * SDMA counter runs at 100MHz frequency. 776 * We display SDMA activity in microsecond granularity in sysfs. 777 * As a result, the divisor is 100. 778 */ 779 #define SDMA_ACTIVITY_DIVISOR 100 780 781 /* Data that is per-process-per device. */ 782 struct kfd_process_device { 783 /* The device that owns this data. */ 784 struct kfd_node *dev; 785 786 /* The process that owns this kfd_process_device. */ 787 struct kfd_process *process; 788 789 /* per-process-per device QCM data structure */ 790 struct qcm_process_device qpd; 791 792 /* Serializes process doorbell allocation for this device */ 793 struct mutex doorbell_mutex; 794 795 /*Apertures*/ 796 uint64_t lds_base; 797 uint64_t lds_limit; 798 uint64_t gpuvm_base; 799 uint64_t gpuvm_limit; 800 uint64_t scratch_base; 801 uint64_t scratch_limit; 802 803 /* VM context for GPUVM allocations */ 804 struct file *drm_file; 805 void *drm_priv; 806 807 /* GPUVM allocations storage */ 808 struct idr alloc_idr; 809 810 /* Flag used to tell the pdd has dequeued from the dqm. 811 * This is used to prevent dev->dqm->ops.process_termination() from 812 * being called twice when it is already called in IOMMU callback 813 * function. 814 */ 815 bool already_dequeued; 816 bool runtime_inuse; 817 818 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */ 819 enum kfd_pdd_bound bound; 820 821 /* VRAM usage */ 822 atomic64_t vram_usage; 823 struct attribute attr_vram; 824 char vram_filename[MAX_SYSFS_FILENAME_LEN]; 825 826 /* SDMA activity tracking */ 827 uint64_t sdma_past_activity_counter; 828 struct attribute attr_sdma; 829 char sdma_filename[MAX_SYSFS_FILENAME_LEN]; 830 831 /* Eviction activity tracking */ 832 uint64_t last_evict_timestamp; 833 atomic64_t evict_duration_counter; 834 struct attribute attr_evict; 835 836 struct kobject *kobj_stats; 837 838 /* 839 * @cu_occupancy: Reports occupancy of Compute Units (CU) of a process 840 * that is associated with device encoded by "this" struct instance. The 841 * value reflects CU usage by all of the waves launched by this process 842 * on this device. A very important property of occupancy parameter is 843 * that its value is a snapshot of current use. 844 * 845 * Following is to be noted regarding how this parameter is reported: 846 * 847 * The number of waves that a CU can launch is limited by couple of 848 * parameters. These are encoded by struct amdgpu_cu_info instance 849 * that is part of every device definition. For GFX9 devices this 850 * translates to 40 waves (simd_per_cu * max_waves_per_simd) when waves 851 * do not use scratch memory and 32 waves (max_scratch_slots_per_cu) 852 * when they do use scratch memory. This could change for future 853 * devices and therefore this example should be considered as a guide. 854 * 855 * All CU's of a device are available for the process. This may not be true 856 * under certain conditions - e.g. CU masking. 857 * 858 * Finally number of CU's that are occupied by a process is affected by both 859 * number of CU's a device has along with number of other competing processes 860 */ 861 struct attribute attr_cu_occupancy; 862 863 /* sysfs counters for GPU retry fault and page migration tracking */ 864 struct kobject *kobj_counters; 865 struct attribute attr_faults; 866 struct attribute attr_page_in; 867 struct attribute attr_page_out; 868 uint64_t faults; 869 uint64_t page_in; 870 uint64_t page_out; 871 872 /* Exception code status*/ 873 uint64_t exception_status; 874 void *vm_fault_exc_data; 875 size_t vm_fault_exc_data_size; 876 877 /* Tracks debug per-vmid request settings */ 878 uint32_t spi_dbg_override; 879 uint32_t spi_dbg_launch_mode; 880 uint32_t watch_points[4]; 881 uint32_t alloc_watch_ids; 882 883 /* 884 * If this process has been checkpointed before, then the user 885 * application will use the original gpu_id on the 886 * checkpointed node to refer to this device. 887 */ 888 uint32_t user_gpu_id; 889 890 void *proc_ctx_bo; 891 uint64_t proc_ctx_gpu_addr; 892 void *proc_ctx_cpu_ptr; 893 894 uint32_t proc_ctx_array_index; 895 896 /* Tracks queue reset status */ 897 bool has_reset_queue; 898 899 u32 pasid; 900 /* Indicates this process has requested PTL stay disabled */ 901 bool ptl_disable_req; 902 }; 903 904 #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd) 905 906 struct svm_range_list { 907 struct mutex lock; 908 struct rb_root_cached objects; 909 struct list_head list; 910 struct work_struct deferred_list_work; 911 struct list_head deferred_range_list; 912 struct list_head criu_svm_metadata_list; 913 spinlock_t deferred_list_lock; 914 /* serializes XNACK-off evictions with the queue quiesce */ 915 struct mutex evict_lock; 916 atomic_t evicted_ranges; 917 atomic_t drain_pagefaults; 918 struct delayed_work restore_work; 919 DECLARE_BITMAP(bitmap_supported, MAX_GPU_INSTANCE); 920 struct task_struct *faulting_task; 921 /* check point ts decides if page fault recovery need be dropped */ 922 atomic64_t checkpoint_ts[MAX_GPU_INSTANCE]; 923 924 /* Default granularity to use in buffer migration 925 * and restoration of backing memory while handling 926 * recoverable page faults 927 */ 928 uint8_t default_granularity; 929 }; 930 931 /* Process data */ 932 struct kfd_process { 933 /* 934 * kfd_process are stored in an mm_struct*->kfd_process* 935 * hash table (kfd_processes in kfd_process.c) 936 */ 937 struct hlist_node kfd_processes; 938 939 /* 940 * Opaque pointer to mm_struct. We don't hold a reference to 941 * it so it should never be dereferenced from here. This is 942 * only used for looking up processes by their mm. 943 */ 944 void *mm; 945 946 struct kref ref; 947 struct work_struct release_work; 948 949 struct mutex mutex; 950 951 /* 952 * In any process, the thread that started main() is the lead 953 * thread and outlives the rest. 954 * It is here because amd_iommu_bind_pasid wants a task_struct. 955 * It can also be used for safely getting a reference to the 956 * mm_struct of the process. 957 */ 958 struct task_struct *lead_thread; 959 960 /* We want to receive a notification when the mm_struct is destroyed */ 961 struct mmu_notifier mmu_notifier; 962 963 /* 964 * Array of kfd_process_device pointers, 965 * one for each device the process is using. 966 */ 967 struct kfd_process_device *pdds[MAX_GPU_INSTANCE]; 968 uint32_t n_pdds; 969 970 struct process_queue_manager pqm; 971 972 /*Is the user space process 32 bit?*/ 973 bool is_32bit_user_mode; 974 975 /* Event-related data */ 976 struct mutex event_mutex; 977 /* Event ID allocator and lookup */ 978 struct idr event_idr; 979 /* Event page */ 980 u64 signal_handle; 981 /* 982 * Each signal event needs a 64-bit signal slot where the signaler will 983 * write a 1 before sending an interrupt. (This is needed because some 984 * interrupts do not contain enough spare data bits to identify an 985 * event.) The signal page is allocated in user mode and mapped to the 986 * kernel; individual signal events use their event_id as slot index. 987 */ 988 uint64_t *signal_page; 989 size_t signal_mapped_size; 990 size_t signal_event_count; 991 bool signal_event_limit_reached; 992 993 /** 994 * @kfd_sigbus_delay_ms: Per-process KFD SIGBUS delivery option for 995 * poison/RAS events (set via DRM_IOCTL_AMDGPU_PROC_OPTIONS / 996 * AMDGPU_PROC_OPTIONS_OP_KFD_SIGBUS_DELAY). 997 * 998 * 0 - send SIGBUS immediately (default) 999 * 0xFFFFFFFF - suppress SIGBUS delivery 1000 * other - delay SIGBUS delivery by this many milliseconds 1001 */ 1002 atomic_t kfd_sigbus_delay_ms; 1003 1004 /* Delayed signal delivery to user */ 1005 struct delayed_work signal_work; 1006 1007 /* Information used for memory eviction */ 1008 void *kgd_process_info; 1009 /* Eviction fence that is attached to all the BOs of this process. The 1010 * fence will be triggered during eviction and new one will be created 1011 * during restore 1012 */ 1013 struct dma_fence __rcu *ef; 1014 1015 /* Work items for evicting and restoring BOs */ 1016 struct delayed_work eviction_work; 1017 struct delayed_work restore_work; 1018 /* seqno of the last scheduled eviction */ 1019 unsigned int last_eviction_seqno; 1020 /* Approx. the last timestamp (in jiffies) when the process was 1021 * restored after an eviction 1022 */ 1023 unsigned long last_restore_timestamp; 1024 1025 /* Indicates device process is debug attached with reserved vmid. */ 1026 bool debug_trap_enabled; 1027 1028 /* per-process-per device debug event fd file */ 1029 struct file *dbg_ev_file; 1030 1031 /* If the process is a kfd debugger, we need to know so we can clean 1032 * up at exit time. If a process enables debugging on itself, it does 1033 * its own clean-up, so we don't set the flag here. We track this by 1034 * counting the number of processes this process is debugging. 1035 */ 1036 atomic_t debugged_process_count; 1037 1038 /* If the process is a debugged, this is the debugger process */ 1039 struct kfd_process *debugger_process; 1040 1041 /* Kobj for our procfs */ 1042 struct kobject *kobj; 1043 struct kobject *kobj_queues; 1044 struct attribute attr_pasid; 1045 1046 /* Exception code enable mask and status */ 1047 uint64_t exception_enable_mask; 1048 uint64_t exception_status; 1049 1050 /* Used to drain stale interrupts */ 1051 wait_queue_head_t wait_irq_drain; 1052 bool irq_drain_is_open; 1053 1054 /* shared virtual memory registered by this process */ 1055 struct svm_range_list svms; 1056 1057 bool xnack_enabled; 1058 1059 /* Work area for debugger event writer worker. */ 1060 struct work_struct debug_event_workarea; 1061 1062 /* Tracks debug per-vmid request for debug flags */ 1063 u32 dbg_flags; 1064 1065 atomic_t poison; 1066 /* Queues are in paused stated because we are in the process of doing a CRIU checkpoint */ 1067 bool queues_paused; 1068 1069 /* Tracks runtime enable status */ 1070 struct semaphore runtime_enable_sema; 1071 bool is_runtime_retry; 1072 struct kfd_runtime_info runtime_info; 1073 1074 /* if gpu page fault sent to KFD */ 1075 bool gpu_page_fault; 1076 1077 /*kfd context id */ 1078 u16 context_id; 1079 1080 /* The primary kfd_process allocating IDs for its secondary kfd_process, 0 for primary kfd_process */ 1081 struct ida id_table; 1082 1083 }; 1084 1085 #define KFD_PROCESS_TABLE_SIZE 8 /* bits: 256 entries */ 1086 #define KFD_CONTEXT_ID_PRIMARY 0xFFFF 1087 #define KFD_CONTEXT_ID_MIN 0 1088 1089 extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); 1090 extern struct srcu_struct kfd_processes_srcu; 1091 1092 /** 1093 * typedef amdkfd_ioctl_t - typedef for ioctl function pointer. 1094 * 1095 * @filep: pointer to file structure. 1096 * @p: amdkfd process pointer. 1097 * @data: pointer to arg that was copied from user. 1098 * 1099 * Return: returns ioctl completion code. 1100 */ 1101 typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p, 1102 void *data); 1103 1104 typedef int amdkfd_ioctl_validate_t(void *kdata, unsigned int usize); 1105 1106 struct amdkfd_ioctl_desc { 1107 unsigned int cmd; 1108 int flags; 1109 amdkfd_ioctl_t *func; 1110 amdkfd_ioctl_validate_t *validate; 1111 unsigned int cmd_drv; 1112 const char *name; 1113 }; 1114 bool kfd_dev_is_large_bar(struct kfd_node *dev); 1115 1116 struct kfd_process *create_process(const struct task_struct *thread, bool primary); 1117 int kfd_process_create_wq(void); 1118 void kfd_process_destroy_wq(void); 1119 void kfd_cleanup_processes(void); 1120 struct kfd_process *kfd_create_process(struct task_struct *thread); 1121 int kfd_create_process_sysfs(struct kfd_process *process); 1122 struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid, 1123 struct kfd_process_device **pdd); 1124 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm); 1125 struct kfd_process *kfd_lookup_process_by_id(const struct mm_struct *mm, u16 id); 1126 1127 int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id); 1128 int kfd_process_gpuid_from_node(struct kfd_process *p, struct kfd_node *node, 1129 uint32_t *gpuid, uint32_t *gpuidx); 1130 static inline int kfd_process_gpuid_from_gpuidx(struct kfd_process *p, 1131 uint32_t gpuidx, uint32_t *gpuid) { 1132 return gpuidx < p->n_pdds ? p->pdds[gpuidx]->dev->id : -EINVAL; 1133 } 1134 static inline struct kfd_process_device *kfd_process_device_from_gpuidx( 1135 struct kfd_process *p, uint32_t gpuidx) { 1136 return gpuidx < p->n_pdds ? p->pdds[gpuidx] : NULL; 1137 } 1138 1139 void kfd_unref_process(struct kfd_process *p); 1140 int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger); 1141 int kfd_process_restore_queues(struct kfd_process *p); 1142 void kfd_suspend_all_processes(void); 1143 void kfd_process_wait_gpu_reset_complete(struct kfd_process *p); 1144 int kfd_resume_all_processes(void); 1145 1146 struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *process, 1147 uint32_t gpu_id); 1148 1149 int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id); 1150 1151 int kfd_process_device_init_vm(struct kfd_process_device *pdd, 1152 struct file *drm_file); 1153 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev, 1154 struct kfd_process *p); 1155 struct kfd_process_device *kfd_get_process_device_data(struct kfd_node *dev, 1156 struct kfd_process *p); 1157 struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev, 1158 struct kfd_process *p); 1159 1160 bool kfd_process_xnack_mode(struct kfd_process *p, bool supported); 1161 1162 void kfd_process_notifier_release_internal(struct kfd_process *p); 1163 1164 /* KFD process API for creating and translating handles */ 1165 int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd, 1166 void *mem); 1167 void *kfd_process_device_translate_handle(struct kfd_process_device *p, 1168 int handle); 1169 void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd, 1170 int handle); 1171 struct kfd_process *kfd_lookup_process_by_pid(struct pid *pid); 1172 1173 /* PASIDs */ 1174 int kfd_pasid_init(void); 1175 void kfd_pasid_exit(void); 1176 u32 kfd_pasid_alloc(void); 1177 void kfd_pasid_free(u32 pasid); 1178 1179 /* Doorbells */ 1180 size_t kfd_doorbell_process_slice(struct kfd_dev *kfd); 1181 int kfd_doorbell_init(struct kfd_dev *kfd); 1182 void kfd_doorbell_fini(struct kfd_dev *kfd); 1183 int kfd_doorbell_mmap(struct kfd_node *dev, struct kfd_process *process, 1184 struct vm_area_struct *vma); 1185 void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd, 1186 unsigned int *doorbell_off); 1187 void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr); 1188 u32 read_kernel_doorbell(u32 __iomem *db); 1189 void write_kernel_doorbell(void __iomem *db, u32 value); 1190 void write_kernel_doorbell64(void __iomem *db, u64 value); 1191 unsigned int kfd_get_doorbell_dw_offset_in_bar(struct kfd_dev *kfd, 1192 struct kfd_process_device *pdd, 1193 unsigned int doorbell_id); 1194 phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd); 1195 int kfd_alloc_process_doorbells(struct kfd_dev *kfd, 1196 struct kfd_process_device *pdd); 1197 void kfd_free_process_doorbells(struct kfd_dev *kfd, 1198 struct kfd_process_device *pdd); 1199 /* GTT Sub-Allocator */ 1200 1201 int kfd_gtt_sa_allocate(struct kfd_node *node, unsigned int size, 1202 struct kfd_mem_obj **mem_obj); 1203 1204 int kfd_gtt_sa_free(struct kfd_node *node, struct kfd_mem_obj *mem_obj); 1205 1206 extern struct device *kfd_device; 1207 1208 /* KFD's procfs */ 1209 void kfd_procfs_init(void); 1210 void kfd_procfs_shutdown(void); 1211 int kfd_procfs_add_queue(struct queue *q); 1212 void kfd_procfs_del_queue(struct queue *q); 1213 1214 /* Topology */ 1215 int kfd_topology_init(void); 1216 void kfd_topology_shutdown(void); 1217 int kfd_topology_add_device(struct kfd_node *gpu); 1218 int kfd_topology_remove_device(struct kfd_node *gpu); 1219 struct kfd_topology_device *kfd_topology_device_by_proximity_domain( 1220 uint32_t proximity_domain); 1221 struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock( 1222 uint32_t proximity_domain); 1223 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id); 1224 struct kfd_node *kfd_device_by_id(uint32_t gpu_id); 1225 static inline bool kfd_irq_is_from_node(struct kfd_node *node, uint32_t node_id, 1226 uint32_t vmid) 1227 { 1228 return (node->interrupt_bitmap & (1 << node_id)) != 0 && 1229 (node->compute_vmid_bitmap & (1 << vmid)) != 0; 1230 } 1231 static inline struct kfd_node *kfd_node_by_irq_ids(struct amdgpu_device *adev, 1232 uint32_t node_id, uint32_t vmid) { 1233 struct kfd_dev *dev = adev->kfd.dev; 1234 uint32_t i; 1235 1236 /* 1237 * On multi-aid system, attempt per-node matching. Otherwise, 1238 * fall back to the first node. 1239 */ 1240 if (!amdgpu_is_multi_aid(adev)) 1241 return dev->nodes[0]; 1242 1243 for (i = 0; i < dev->num_nodes; i++) 1244 if (kfd_irq_is_from_node(dev->nodes[i], node_id, vmid)) 1245 return dev->nodes[i]; 1246 1247 return NULL; 1248 } 1249 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev); 1250 uint32_t kfd_topology_get_num_devices(void); 1251 int kfd_numa_node_to_apic_id(int numa_node_id); 1252 uint32_t kfd_gpu_node_num(void); 1253 1254 /* Interrupts */ 1255 #define KFD_IRQ_FENCE_CLIENTID 0xff 1256 #define KFD_IRQ_FENCE_SOURCEID 0xff 1257 #define KFD_IRQ_IS_FENCE(client, source) \ 1258 ((client) == KFD_IRQ_FENCE_CLIENTID && \ 1259 (source) == KFD_IRQ_FENCE_SOURCEID) 1260 int kfd_interrupt_init(struct kfd_node *dev); 1261 void kfd_interrupt_exit(struct kfd_node *dev); 1262 bool enqueue_ih_ring_entry(struct kfd_node *kfd, const void *ih_ring_entry); 1263 bool interrupt_is_wanted(struct kfd_node *dev, 1264 const uint32_t *ih_ring_entry, 1265 uint32_t *patched_ihre, bool *flag); 1266 int kfd_process_drain_interrupts(struct kfd_process_device *pdd); 1267 void kfd_process_close_interrupt_drain(unsigned int pasid); 1268 1269 /* amdkfd Apertures */ 1270 int kfd_init_apertures(struct kfd_process *process); 1271 1272 void kfd_process_set_trap_handler(struct qcm_process_device *qpd, 1273 uint64_t tba_addr, 1274 uint64_t tma_addr); 1275 void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd, 1276 bool enabled); 1277 1278 /* CRIU */ 1279 /* 1280 * Need to increment KFD_CRIU_PRIV_VERSION each time a change is made to any of the CRIU private 1281 * structures: 1282 * kfd_criu_process_priv_data 1283 * kfd_criu_device_priv_data 1284 * kfd_criu_bo_priv_data 1285 * kfd_criu_queue_priv_data 1286 * kfd_criu_event_priv_data 1287 * kfd_criu_svm_range_priv_data 1288 */ 1289 1290 #define KFD_CRIU_PRIV_VERSION 1 1291 1292 struct kfd_criu_process_priv_data { 1293 uint32_t version; 1294 uint32_t xnack_mode; 1295 }; 1296 1297 struct kfd_criu_device_priv_data { 1298 /* For future use */ 1299 uint64_t reserved; 1300 }; 1301 1302 struct kfd_criu_bo_priv_data { 1303 uint64_t user_addr; 1304 uint32_t idr_handle; 1305 uint32_t mapped_gpuids[MAX_GPU_INSTANCE]; 1306 }; 1307 1308 /* 1309 * The first 4 bytes of kfd_criu_queue_priv_data, kfd_criu_event_priv_data, 1310 * kfd_criu_svm_range_priv_data is the object type 1311 */ 1312 enum kfd_criu_object_type { 1313 KFD_CRIU_OBJECT_TYPE_QUEUE, 1314 KFD_CRIU_OBJECT_TYPE_EVENT, 1315 KFD_CRIU_OBJECT_TYPE_SVM_RANGE, 1316 }; 1317 1318 struct kfd_criu_svm_range_priv_data { 1319 uint32_t object_type; 1320 uint64_t start_addr; 1321 uint64_t size; 1322 /* Variable length array of attributes */ 1323 struct kfd_ioctl_svm_attribute attrs[]; 1324 }; 1325 1326 struct kfd_criu_queue_priv_data { 1327 uint32_t object_type; 1328 uint64_t q_address; 1329 uint64_t q_size; 1330 uint64_t read_ptr_addr; 1331 uint64_t write_ptr_addr; 1332 uint64_t doorbell_off; 1333 uint64_t eop_ring_buffer_address; 1334 uint64_t ctx_save_restore_area_address; 1335 uint32_t gpu_id; 1336 uint32_t type; 1337 uint32_t format; 1338 uint32_t q_id; 1339 uint32_t priority; 1340 uint32_t q_percent; 1341 uint32_t doorbell_id; 1342 uint32_t gws; 1343 uint32_t sdma_id; 1344 uint32_t eop_ring_buffer_size; 1345 uint32_t ctx_save_restore_area_size; 1346 uint32_t ctl_stack_size; 1347 uint32_t mqd_size; 1348 }; 1349 1350 struct kfd_criu_event_priv_data { 1351 uint32_t object_type; 1352 uint64_t user_handle; 1353 uint32_t event_id; 1354 uint32_t auto_reset; 1355 uint32_t type; 1356 uint32_t signaled; 1357 1358 union { 1359 struct kfd_hsa_memory_exception_data memory_exception_data; 1360 struct kfd_hsa_hw_exception_data hw_exception_data; 1361 }; 1362 }; 1363 1364 int kfd_process_get_queue_info(struct kfd_process *p, 1365 uint32_t *num_queues, 1366 uint64_t *priv_data_sizes); 1367 1368 int kfd_criu_checkpoint_queues(struct kfd_process *p, 1369 uint8_t __user *user_priv_data, 1370 uint64_t *priv_data_offset); 1371 1372 int kfd_criu_restore_queue(struct kfd_process *p, 1373 uint8_t __user *user_priv_data, 1374 uint64_t *priv_data_offset, 1375 uint64_t max_priv_data_size); 1376 1377 int kfd_criu_checkpoint_events(struct kfd_process *p, 1378 uint8_t __user *user_priv_data, 1379 uint64_t *priv_data_offset); 1380 1381 int kfd_criu_restore_event(struct file *devkfd, 1382 struct kfd_process *p, 1383 uint8_t __user *user_priv_data, 1384 uint64_t *priv_data_offset, 1385 uint64_t max_priv_data_size); 1386 1387 int kfd_criu_resume_queues(struct kfd_process *p); 1388 /* CRIU - End */ 1389 1390 /* Queue Context Management */ 1391 int init_queue(struct queue **q, const struct queue_properties *properties); 1392 void uninit_queue(struct queue *q); 1393 void print_queue_properties(struct queue_properties *q); 1394 void print_queue(struct queue *q); 1395 int kfd_queue_buffer_get(struct amdgpu_vm *vm, void __user *addr, struct amdgpu_bo **pbo, 1396 u64 expected_size); 1397 void kfd_queue_buffer_put(struct amdgpu_bo **bo); 1398 int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_properties *properties, bool is_criu); 1399 int kfd_queue_release_buffers(struct kfd_process_device *pdd, struct queue_properties *properties, bool is_criu); 1400 void kfd_queue_unref_bo_va(struct amdgpu_vm *vm, struct amdgpu_bo **bo); 1401 int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd, 1402 struct queue_properties *properties); 1403 void kfd_queue_ctx_save_restore_size(struct kfd_topology_device *dev); 1404 int kfd_queue_buffer_svm_get(struct kfd_process_device *pdd, u64 addr, u64 size); 1405 1406 struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type, 1407 struct kfd_node *dev); 1408 struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type, 1409 struct kfd_node *dev); 1410 struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type, 1411 struct kfd_node *dev); 1412 struct mqd_manager *mqd_manager_init_v10(enum KFD_MQD_TYPE type, 1413 struct kfd_node *dev); 1414 struct mqd_manager *mqd_manager_init_v11(enum KFD_MQD_TYPE type, 1415 struct kfd_node *dev); 1416 struct mqd_manager *mqd_manager_init_v12(enum KFD_MQD_TYPE type, 1417 struct kfd_node *dev); 1418 struct mqd_manager *mqd_manager_init_v12_1(enum KFD_MQD_TYPE type, 1419 struct kfd_node *dev); 1420 struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev); 1421 void device_queue_manager_uninit(struct device_queue_manager *dqm); 1422 struct kernel_queue *kernel_queue_init(struct kfd_node *dev, 1423 enum kfd_queue_type type); 1424 void kernel_queue_uninit(struct kernel_queue *kq); 1425 int kfd_evict_process_device(struct kfd_process_device *pdd); 1426 int kfd_dqm_suspend_bad_queue_mes(struct kfd_node *knode, u32 pasid, u32 doorbell_id); 1427 1428 /* Process Queue Manager */ 1429 struct process_queue_node { 1430 struct queue *q; 1431 struct kernel_queue *kq; 1432 struct list_head process_queue_list; 1433 }; 1434 1435 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd); 1436 void kfd_process_dequeue_from_all_devices(struct kfd_process *p); 1437 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p); 1438 void pqm_uninit(struct process_queue_manager *pqm); 1439 int pqm_create_queue(struct process_queue_manager *pqm, 1440 struct kfd_node *dev, 1441 struct queue_properties *properties, 1442 unsigned int *qid, 1443 const struct kfd_criu_queue_priv_data *q_data, 1444 const void *restore_mqd, 1445 const void *restore_ctl_stack, 1446 uint32_t *p_doorbell_offset_in_process); 1447 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid); 1448 int pqm_update_queue_properties(struct process_queue_manager *pqm, unsigned int qid, 1449 struct queue_properties *p); 1450 int pqm_update_mqd(struct process_queue_manager *pqm, unsigned int qid, 1451 struct mqd_update_info *minfo); 1452 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid, 1453 void *gws); 1454 struct queue *pqm_get_user_queue(struct process_queue_manager *pqm, 1455 unsigned int qid); 1456 int pqm_get_wave_state(struct process_queue_manager *pqm, 1457 unsigned int qid, 1458 void __user *ctl_stack, 1459 u32 *ctl_stack_used_size, 1460 u32 *save_area_used_size); 1461 int pqm_get_queue_snapshot(struct process_queue_manager *pqm, 1462 uint64_t exception_clear_mask, 1463 void __user *buf, 1464 int *num_qss_entries, 1465 uint32_t *entry_size); 1466 1467 int amdkfd_fence_wait_timeout(struct device_queue_manager *dqm, 1468 uint64_t fence_value, 1469 unsigned int timeout_ms); 1470 1471 int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm, 1472 unsigned int qid, 1473 u32 *mqd_size, 1474 u32 *ctl_stack_size); 1475 /* Packet Manager */ 1476 1477 #define KFD_FENCE_COMPLETED (100) 1478 #define KFD_FENCE_INIT (10) 1479 1480 /** 1481 * enum kfd_config_dequeue_wait_counts_cmd - Command for configuring 1482 * dequeue wait counts. 1483 * 1484 * @KFD_DEQUEUE_WAIT_INIT: Set optimized dequeue wait counts for a 1485 * certain ASICs. For these ASICs, this is default value used by RESET 1486 * @KFD_DEQUEUE_WAIT_RESET: Reset dequeue wait counts to the optimized value 1487 * for certain ASICs. For others set it to default hardware reset value 1488 * @KFD_DEQUEUE_WAIT_SET_SCH_WAVE: Set context switch latency wait 1489 * 1490 */ 1491 enum kfd_config_dequeue_wait_counts_cmd { 1492 KFD_DEQUEUE_WAIT_INIT = 1, 1493 KFD_DEQUEUE_WAIT_RESET = 2, 1494 KFD_DEQUEUE_WAIT_SET_SCH_WAVE = 3 1495 }; 1496 1497 1498 struct packet_manager { 1499 struct device_queue_manager *dqm; 1500 struct kernel_queue *priv_queue; 1501 struct mutex lock; 1502 bool allocated; 1503 struct kfd_mem_obj *ib_buffer_obj; 1504 unsigned int ib_size_bytes; 1505 bool is_over_subscription; 1506 1507 const struct packet_manager_funcs *pmf; 1508 }; 1509 1510 struct packet_manager_funcs { 1511 /* Support ASIC-specific packet formats for PM4 packets */ 1512 int (*map_process)(struct packet_manager *pm, uint32_t *buffer, 1513 struct qcm_process_device *qpd); 1514 int (*runlist)(struct packet_manager *pm, uint32_t *buffer, 1515 uint64_t ib, size_t ib_size_in_dwords, bool chain); 1516 int (*set_resources)(struct packet_manager *pm, uint32_t *buffer, 1517 struct scheduling_resources *res); 1518 int (*map_queues)(struct packet_manager *pm, uint32_t *buffer, 1519 struct queue *q, bool is_static); 1520 int (*unmap_queues)(struct packet_manager *pm, uint32_t *buffer, 1521 enum kfd_unmap_queues_filter mode, 1522 uint32_t filter_param, bool reset); 1523 int (*config_dequeue_wait_counts)(struct packet_manager *pm, uint32_t *buffer, 1524 enum kfd_config_dequeue_wait_counts_cmd cmd, uint32_t value); 1525 int (*query_status)(struct packet_manager *pm, uint32_t *buffer, 1526 uint64_t fence_address, uint64_t fence_value); 1527 int (*release_mem)(uint64_t gpu_addr, uint32_t *buffer); 1528 1529 /* Packet sizes */ 1530 int map_process_size; 1531 int runlist_size; 1532 int set_resources_size; 1533 int map_queues_size; 1534 int unmap_queues_size; 1535 int config_dequeue_wait_counts_size; 1536 int query_status_size; 1537 int release_mem_size; 1538 }; 1539 1540 extern const struct packet_manager_funcs kfd_vi_pm_funcs; 1541 extern const struct packet_manager_funcs kfd_v9_pm_funcs; 1542 extern const struct packet_manager_funcs kfd_aldebaran_pm_funcs; 1543 1544 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm); 1545 void pm_uninit(struct packet_manager *pm); 1546 int pm_send_set_resources(struct packet_manager *pm, 1547 struct scheduling_resources *res); 1548 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues); 1549 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address, 1550 uint64_t fence_value); 1551 1552 int pm_send_unmap_queue(struct packet_manager *pm, 1553 enum kfd_unmap_queues_filter mode, 1554 uint32_t filter_param, bool reset); 1555 1556 void pm_release_ib(struct packet_manager *pm); 1557 1558 int pm_config_dequeue_wait_counts(struct packet_manager *pm, 1559 enum kfd_config_dequeue_wait_counts_cmd cmd, 1560 uint32_t wait_counts_config); 1561 1562 /* Following PM funcs can be shared among VI and AI */ 1563 unsigned int pm_build_pm4_header(unsigned int opcode, size_t packet_size); 1564 1565 uint64_t kfd_get_number_elems(struct kfd_dev *kfd); 1566 1567 /* Events */ 1568 extern const struct kfd_event_interrupt_class event_interrupt_class_cik; 1569 extern const struct kfd_event_interrupt_class event_interrupt_class_v9; 1570 extern const struct kfd_event_interrupt_class event_interrupt_class_v9_4_3; 1571 extern const struct kfd_event_interrupt_class event_interrupt_class_v10; 1572 extern const struct kfd_event_interrupt_class event_interrupt_class_v11; 1573 extern const struct kfd_event_interrupt_class event_interrupt_class_v12_1; 1574 1575 extern const struct kfd_device_global_init_class device_global_init_class_cik; 1576 1577 int kfd_event_init_process(struct kfd_process *p); 1578 void kfd_event_free_process(struct kfd_process *p); 1579 int kfd_wait_on_events(struct kfd_process *p, 1580 uint32_t num_events, void __user *data, 1581 bool all, uint32_t *user_timeout_ms, 1582 uint32_t *wait_result); 1583 void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id, 1584 uint32_t valid_id_bits, bool signal_mailbox_updated); 1585 void kfd_signal_hw_exception_event(u32 pasid); 1586 int kfd_set_event(struct kfd_process *p, uint32_t event_id); 1587 int kfd_reset_event(struct kfd_process *p, uint32_t event_id); 1588 int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset); 1589 1590 int kfd_event_create(struct file *devkfd, struct kfd_process *p, 1591 uint32_t event_type, bool auto_reset, uint32_t node_id, 1592 uint32_t *event_id, uint32_t *event_trigger_data, 1593 uint64_t *event_page_offset, uint32_t *event_slot_index); 1594 1595 int kfd_get_num_events(struct kfd_process *p); 1596 int kfd_event_destroy(struct kfd_process *p, uint32_t event_id); 1597 1598 void kfd_signal_vm_fault_event_with_userptr(struct kfd_process *p, uint64_t gpu_va); 1599 1600 void kfd_signal_vm_fault_event(struct kfd_process_device *pdd, 1601 struct kfd_vm_fault_info *info, 1602 struct kfd_hsa_memory_exception_data *data); 1603 1604 void kfd_signal_reset_event(struct kfd_node *dev); 1605 1606 void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid); 1607 void kfd_signal_sigbus_delayed_fn(struct work_struct *work); 1608 void kfd_signal_process_terminate_event(struct kfd_process *p); 1609 1610 static inline void kfd_flush_tlb(struct kfd_process_device *pdd) 1611 { 1612 struct amdgpu_device *adev = pdd->dev->adev; 1613 struct amdgpu_vm *vm = drm_priv_to_vm(pdd->drm_priv); 1614 1615 amdgpu_vm_flush_compute_tlb(adev, vm, TLB_FLUSH_HEAVYWEIGHT, 1616 pdd->dev->xcc_mask); 1617 } 1618 1619 static inline bool kfd_flush_tlb_after_unmap(struct kfd_dev *dev) 1620 { 1621 return KFD_GC_VERSION(dev) >= IP_VERSION(9, 4, 2) || 1622 (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 1) && dev->sdma_fw_version >= 18) || 1623 KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 0); 1624 } 1625 1626 int kfd_send_exception_to_runtime(struct kfd_process *p, 1627 unsigned int queue_id, 1628 uint64_t error_reason); 1629 bool kfd_is_locked(struct kfd_dev *kfd); 1630 1631 /* Compute profile */ 1632 void kfd_inc_compute_active(struct kfd_node *dev); 1633 void kfd_dec_compute_active(struct kfd_node *dev); 1634 1635 /* Cgroup Support */ 1636 /* Check with device cgroup if @kfd device is accessible */ 1637 static inline int kfd_devcgroup_check_permission(struct kfd_node *node) 1638 { 1639 #if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF) 1640 struct drm_device *ddev; 1641 1642 if (node->xcp) 1643 ddev = node->xcp->ddev; 1644 else 1645 ddev = adev_to_drm(node->adev); 1646 1647 return devcgroup_check_permission(DEVCG_DEV_CHAR, DRM_MAJOR, 1648 ddev->render->index, 1649 DEVCG_ACC_WRITE | DEVCG_ACC_READ); 1650 #else 1651 return 0; 1652 #endif 1653 } 1654 1655 static inline bool kfd_is_first_node(struct kfd_node *node) 1656 { 1657 return (node == node->kfd->nodes[0]); 1658 } 1659 1660 /* PTL support */ 1661 int kfd_ptl_disable_request(struct kfd_process_device *pdd, 1662 struct kfd_process *p); 1663 int kfd_ptl_disable_release(struct kfd_process_device *pdd, 1664 struct kfd_process *p); 1665 1666 /* Debugfs */ 1667 #if defined(CONFIG_DEBUG_FS) 1668 1669 void kfd_debugfs_init(void); 1670 void kfd_debugfs_fini(void); 1671 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data); 1672 int pqm_debugfs_mqds(struct seq_file *m, void *data); 1673 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data); 1674 int dqm_debugfs_hqds(struct seq_file *m, void *data); 1675 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data); 1676 int pm_debugfs_runlist(struct seq_file *m, void *data); 1677 1678 int kfd_debugfs_hang_hws(struct kfd_node *dev); 1679 int pm_debugfs_hang_hws(struct packet_manager *pm); 1680 int dqm_debugfs_hang_hws(struct device_queue_manager *dqm); 1681 1682 int kfd_debugfs_add_process(struct kfd_process *p); 1683 void kfd_debugfs_remove_process(struct kfd_process *p); 1684 1685 #else 1686 1687 static inline void kfd_debugfs_init(void) {} 1688 static inline void kfd_debugfs_fini(void) {} 1689 static inline int kfd_debugfs_add_process(struct kfd_process *p) { return 0; } 1690 static inline void kfd_debugfs_remove_process(struct kfd_process *p) {} 1691 1692 #endif 1693 1694 #endif 1695