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