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