xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_priv.h (revision 133f9794)
1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef KFD_PRIV_H_INCLUDED
24 #define KFD_PRIV_H_INCLUDED
25 
26 #include <linux/hashtable.h>
27 #include <linux/mmu_notifier.h>
28 #include <linux/mutex.h>
29 #include <linux/types.h>
30 #include <linux/atomic.h>
31 #include <linux/workqueue.h>
32 #include <linux/spinlock.h>
33 #include <linux/kfd_ioctl.h>
34 #include <linux/idr.h>
35 #include <linux/kfifo.h>
36 #include <linux/seq_file.h>
37 #include <linux/kref.h>
38 #include <kgd_kfd_interface.h>
39 
40 #include "amd_shared.h"
41 
42 #define KFD_SYSFS_FILE_MODE 0444
43 
44 #define KFD_MMAP_DOORBELL_MASK 0x8000000000000ull
45 #define KFD_MMAP_EVENTS_MASK 0x4000000000000ull
46 #define KFD_MMAP_RESERVED_MEM_MASK 0x2000000000000ull
47 
48 /*
49  * When working with cp scheduler we should assign the HIQ manually or via
50  * the radeon driver to a fixed hqd slot, here are the fixed HIQ hqd slot
51  * definitions for Kaveri. In Kaveri only the first ME queues participates
52  * in the cp scheduling taking that in mind we set the HIQ slot in the
53  * second ME.
54  */
55 #define KFD_CIK_HIQ_PIPE 4
56 #define KFD_CIK_HIQ_QUEUE 0
57 
58 /* GPU ID hash width in bits */
59 #define KFD_GPU_ID_HASH_WIDTH 16
60 
61 /* Macro for allocating structures */
62 #define kfd_alloc_struct(ptr_to_struct)	\
63 	((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
64 
65 #define KFD_MAX_NUM_OF_PROCESSES 512
66 #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
67 
68 /*
69  * Size of the per-process TBA+TMA buffer: 2 pages
70  *
71  * The first page is the TBA used for the CWSR ISA code. The second
72  * page is used as TMA for daisy changing a user-mode trap handler.
73  */
74 #define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2)
75 #define KFD_CWSR_TMA_OFFSET PAGE_SIZE
76 
77 /*
78  * Kernel module parameter to specify maximum number of supported queues per
79  * device
80  */
81 extern int max_num_of_queues_per_device;
82 
83 #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT 4096
84 #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE		\
85 	(KFD_MAX_NUM_OF_PROCESSES *			\
86 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
87 
88 #define KFD_KERNEL_QUEUE_SIZE 2048
89 
90 /* Kernel module parameter to specify the scheduling policy */
91 extern int sched_policy;
92 
93 /*
94  * Kernel module parameter to specify the maximum process
95  * number per HW scheduler
96  */
97 extern int hws_max_conc_proc;
98 
99 extern int cwsr_enable;
100 
101 /*
102  * Kernel module parameter to specify whether to send sigterm to HSA process on
103  * unhandled exception
104  */
105 extern int send_sigterm;
106 
107 /*
108  * Ignore CRAT table during KFD initialization, can be used to work around
109  * broken CRAT tables on some AMD systems
110  */
111 extern int ignore_crat;
112 
113 /**
114  * enum kfd_sched_policy
115  *
116  * @KFD_SCHED_POLICY_HWS: H/W scheduling policy known as command processor (cp)
117  * scheduling. In this scheduling mode we're using the firmware code to
118  * schedule the user mode queues and kernel queues such as HIQ and DIQ.
119  * the HIQ queue is used as a special queue that dispatches the configuration
120  * to the cp and the user mode queues list that are currently running.
121  * the DIQ queue is a debugging queue that dispatches debugging commands to the
122  * firmware.
123  * in this scheduling mode user mode queues over subscription feature is
124  * enabled.
125  *
126  * @KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: The same as above but the over
127  * subscription feature disabled.
128  *
129  * @KFD_SCHED_POLICY_NO_HWS: no H/W scheduling policy is a mode which directly
130  * set the command processor registers and sets the queues "manually". This
131  * mode is used *ONLY* for debugging proposes.
132  *
133  */
134 enum kfd_sched_policy {
135 	KFD_SCHED_POLICY_HWS = 0,
136 	KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION,
137 	KFD_SCHED_POLICY_NO_HWS
138 };
139 
140 enum cache_policy {
141 	cache_policy_coherent,
142 	cache_policy_noncoherent
143 };
144 
145 struct kfd_event_interrupt_class {
146 	bool (*interrupt_isr)(struct kfd_dev *dev,
147 				const uint32_t *ih_ring_entry);
148 	void (*interrupt_wq)(struct kfd_dev *dev,
149 				const uint32_t *ih_ring_entry);
150 };
151 
152 struct kfd_device_info {
153 	enum amd_asic_type asic_family;
154 	const struct kfd_event_interrupt_class *event_interrupt_class;
155 	unsigned int max_pasid_bits;
156 	unsigned int max_no_of_hqd;
157 	size_t ih_ring_entry_size;
158 	uint8_t num_of_watch_points;
159 	uint16_t mqd_size_aligned;
160 	bool supports_cwsr;
161 	bool needs_iommu_device;
162 	bool needs_pci_atomics;
163 };
164 
165 struct kfd_mem_obj {
166 	uint32_t range_start;
167 	uint32_t range_end;
168 	uint64_t gpu_addr;
169 	uint32_t *cpu_ptr;
170 };
171 
172 struct kfd_vmid_info {
173 	uint32_t first_vmid_kfd;
174 	uint32_t last_vmid_kfd;
175 	uint32_t vmid_num_kfd;
176 };
177 
178 struct kfd_dev {
179 	struct kgd_dev *kgd;
180 
181 	const struct kfd_device_info *device_info;
182 	struct pci_dev *pdev;
183 
184 	unsigned int id;		/* topology stub index */
185 
186 	phys_addr_t doorbell_base;	/* Start of actual doorbells used by
187 					 * KFD. It is aligned for mapping
188 					 * into user mode
189 					 */
190 	size_t doorbell_id_offset;	/* Doorbell offset (from KFD doorbell
191 					 * to HW doorbell, GFX reserved some
192 					 * at the start)
193 					 */
194 	u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
195 					   * page used by kernel queue
196 					   */
197 
198 	struct kgd2kfd_shared_resources shared_resources;
199 	struct kfd_vmid_info vm_info;
200 
201 	const struct kfd2kgd_calls *kfd2kgd;
202 	struct mutex doorbell_mutex;
203 	DECLARE_BITMAP(doorbell_available_index,
204 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
205 
206 	void *gtt_mem;
207 	uint64_t gtt_start_gpu_addr;
208 	void *gtt_start_cpu_ptr;
209 	void *gtt_sa_bitmap;
210 	struct mutex gtt_sa_lock;
211 	unsigned int gtt_sa_chunk_size;
212 	unsigned int gtt_sa_num_of_chunks;
213 
214 	/* Interrupts */
215 	struct kfifo ih_fifo;
216 	struct workqueue_struct *ih_wq;
217 	struct work_struct interrupt_work;
218 	spinlock_t interrupt_lock;
219 
220 	/* QCM Device instance */
221 	struct device_queue_manager *dqm;
222 
223 	bool init_complete;
224 	/*
225 	 * Interrupts of interest to KFD are copied
226 	 * from the HW ring into a SW ring.
227 	 */
228 	bool interrupts_active;
229 
230 	/* Debug manager */
231 	struct kfd_dbgmgr           *dbgmgr;
232 
233 	/* Maximum process number mapped to HW scheduler */
234 	unsigned int max_proc_per_quantum;
235 
236 	/* CWSR */
237 	bool cwsr_enabled;
238 	const void *cwsr_isa;
239 	unsigned int cwsr_isa_size;
240 };
241 
242 /* KGD2KFD callbacks */
243 void kgd2kfd_exit(void);
244 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
245 			struct pci_dev *pdev, const struct kfd2kgd_calls *f2g);
246 bool kgd2kfd_device_init(struct kfd_dev *kfd,
247 			const struct kgd2kfd_shared_resources *gpu_resources);
248 void kgd2kfd_device_exit(struct kfd_dev *kfd);
249 
250 enum kfd_mempool {
251 	KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
252 	KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
253 	KFD_MEMPOOL_FRAMEBUFFER = 3,
254 };
255 
256 /* Character device interface */
257 int kfd_chardev_init(void);
258 void kfd_chardev_exit(void);
259 struct device *kfd_chardev(void);
260 
261 /**
262  * enum kfd_unmap_queues_filter
263  *
264  * @KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE: Preempts single queue.
265  *
266  * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
267  *						running queues list.
268  *
269  * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
270  *						specific process.
271  *
272  */
273 enum kfd_unmap_queues_filter {
274 	KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE,
275 	KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES,
276 	KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
277 	KFD_UNMAP_QUEUES_FILTER_BY_PASID
278 };
279 
280 /**
281  * enum kfd_queue_type
282  *
283  * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
284  *
285  * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type.
286  *
287  * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
288  *
289  * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
290  */
291 enum kfd_queue_type  {
292 	KFD_QUEUE_TYPE_COMPUTE,
293 	KFD_QUEUE_TYPE_SDMA,
294 	KFD_QUEUE_TYPE_HIQ,
295 	KFD_QUEUE_TYPE_DIQ
296 };
297 
298 enum kfd_queue_format {
299 	KFD_QUEUE_FORMAT_PM4,
300 	KFD_QUEUE_FORMAT_AQL
301 };
302 
303 /**
304  * struct queue_properties
305  *
306  * @type: The queue type.
307  *
308  * @queue_id: Queue identifier.
309  *
310  * @queue_address: Queue ring buffer address.
311  *
312  * @queue_size: Queue ring buffer size.
313  *
314  * @priority: Defines the queue priority relative to other queues in the
315  * process.
316  * This is just an indication and HW scheduling may override the priority as
317  * necessary while keeping the relative prioritization.
318  * the priority granularity is from 0 to f which f is the highest priority.
319  * currently all queues are initialized with the highest priority.
320  *
321  * @queue_percent: This field is partially implemented and currently a zero in
322  * this field defines that the queue is non active.
323  *
324  * @read_ptr: User space address which points to the number of dwords the
325  * cp read from the ring buffer. This field updates automatically by the H/W.
326  *
327  * @write_ptr: Defines the number of dwords written to the ring buffer.
328  *
329  * @doorbell_ptr: This field aim is to notify the H/W of new packet written to
330  * the queue ring buffer. This field should be similar to write_ptr and the
331  * user should update this field after he updated the write_ptr.
332  *
333  * @doorbell_off: The doorbell offset in the doorbell pci-bar.
334  *
335  * @is_interop: Defines if this is a interop queue. Interop queue means that
336  * the queue can access both graphics and compute resources.
337  *
338  * @is_evicted: Defines if the queue is evicted. Only active queues
339  * are evicted, rendering them inactive.
340  *
341  * @is_active: Defines if the queue is active or not. @is_active and
342  * @is_evicted are protected by the DQM lock.
343  *
344  * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
345  * of the queue.
346  *
347  * This structure represents the queue properties for each queue no matter if
348  * it's user mode or kernel mode queue.
349  *
350  */
351 struct queue_properties {
352 	enum kfd_queue_type type;
353 	enum kfd_queue_format format;
354 	unsigned int queue_id;
355 	uint64_t queue_address;
356 	uint64_t  queue_size;
357 	uint32_t priority;
358 	uint32_t queue_percent;
359 	uint32_t *read_ptr;
360 	uint32_t *write_ptr;
361 	uint32_t __iomem *doorbell_ptr;
362 	uint32_t doorbell_off;
363 	bool is_interop;
364 	bool is_evicted;
365 	bool is_active;
366 	/* Not relevant for user mode queues in cp scheduling */
367 	unsigned int vmid;
368 	/* Relevant only for sdma queues*/
369 	uint32_t sdma_engine_id;
370 	uint32_t sdma_queue_id;
371 	uint32_t sdma_vm_addr;
372 	/* Relevant only for VI */
373 	uint64_t eop_ring_buffer_address;
374 	uint32_t eop_ring_buffer_size;
375 	uint64_t ctx_save_restore_area_address;
376 	uint32_t ctx_save_restore_area_size;
377 	uint32_t ctl_stack_size;
378 	uint64_t tba_addr;
379 	uint64_t tma_addr;
380 };
381 
382 /**
383  * struct queue
384  *
385  * @list: Queue linked list.
386  *
387  * @mqd: The queue MQD.
388  *
389  * @mqd_mem_obj: The MQD local gpu memory object.
390  *
391  * @gart_mqd_addr: The MQD gart mc address.
392  *
393  * @properties: The queue properties.
394  *
395  * @mec: Used only in no cp scheduling mode and identifies to micro engine id
396  *	 that the queue should be execute on.
397  *
398  * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
399  *	  id.
400  *
401  * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
402  *
403  * @process: The kfd process that created this queue.
404  *
405  * @device: The kfd device that created this queue.
406  *
407  * This structure represents user mode compute queues.
408  * It contains all the necessary data to handle such queues.
409  *
410  */
411 
412 struct queue {
413 	struct list_head list;
414 	void *mqd;
415 	struct kfd_mem_obj *mqd_mem_obj;
416 	uint64_t gart_mqd_addr;
417 	struct queue_properties properties;
418 
419 	uint32_t mec;
420 	uint32_t pipe;
421 	uint32_t queue;
422 
423 	unsigned int sdma_id;
424 
425 	struct kfd_process	*process;
426 	struct kfd_dev		*device;
427 };
428 
429 /*
430  * Please read the kfd_mqd_manager.h description.
431  */
432 enum KFD_MQD_TYPE {
433 	KFD_MQD_TYPE_COMPUTE = 0,	/* for no cp scheduling */
434 	KFD_MQD_TYPE_HIQ,		/* for hiq */
435 	KFD_MQD_TYPE_CP,		/* for cp queues and diq */
436 	KFD_MQD_TYPE_SDMA,		/* for sdma queues */
437 	KFD_MQD_TYPE_MAX
438 };
439 
440 struct scheduling_resources {
441 	unsigned int vmid_mask;
442 	enum kfd_queue_type type;
443 	uint64_t queue_mask;
444 	uint64_t gws_mask;
445 	uint32_t oac_mask;
446 	uint32_t gds_heap_base;
447 	uint32_t gds_heap_size;
448 };
449 
450 struct process_queue_manager {
451 	/* data */
452 	struct kfd_process	*process;
453 	struct list_head	queues;
454 	unsigned long		*queue_slot_bitmap;
455 };
456 
457 struct qcm_process_device {
458 	/* The Device Queue Manager that owns this data */
459 	struct device_queue_manager *dqm;
460 	struct process_queue_manager *pqm;
461 	/* Queues list */
462 	struct list_head queues_list;
463 	struct list_head priv_queue_list;
464 
465 	unsigned int queue_count;
466 	unsigned int vmid;
467 	bool is_debug;
468 	unsigned int evicted; /* eviction counter, 0=active */
469 
470 	/* This flag tells if we should reset all wavefronts on
471 	 * process termination
472 	 */
473 	bool reset_wavefronts;
474 
475 	/*
476 	 * All the memory management data should be here too
477 	 */
478 	uint64_t gds_context_area;
479 	uint32_t sh_mem_config;
480 	uint32_t sh_mem_bases;
481 	uint32_t sh_mem_ape1_base;
482 	uint32_t sh_mem_ape1_limit;
483 	uint32_t page_table_base;
484 	uint32_t gds_size;
485 	uint32_t num_gws;
486 	uint32_t num_oac;
487 	uint32_t sh_hidden_private_base;
488 
489 	/* CWSR memory */
490 	void *cwsr_kaddr;
491 	uint64_t tba_addr;
492 	uint64_t tma_addr;
493 };
494 
495 /* KFD Memory Eviction */
496 
497 /* Approx. wait time before attempting to restore evicted BOs */
498 #define PROCESS_RESTORE_TIME_MS 100
499 /* Approx. back off time if restore fails due to lack of memory */
500 #define PROCESS_BACK_OFF_TIME_MS 100
501 /* Approx. time before evicting the process again */
502 #define PROCESS_ACTIVE_TIME_MS 10
503 
504 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
505 					       struct dma_fence *fence);
506 
507 enum kfd_pdd_bound {
508 	PDD_UNBOUND = 0,
509 	PDD_BOUND,
510 	PDD_BOUND_SUSPENDED,
511 };
512 
513 /* Data that is per-process-per device. */
514 struct kfd_process_device {
515 	/*
516 	 * List of all per-device data for a process.
517 	 * Starts from kfd_process.per_device_data.
518 	 */
519 	struct list_head per_device_list;
520 
521 	/* The device that owns this data. */
522 	struct kfd_dev *dev;
523 
524 	/* The process that owns this kfd_process_device. */
525 	struct kfd_process *process;
526 
527 	/* per-process-per device QCM data structure */
528 	struct qcm_process_device qpd;
529 
530 	/*Apertures*/
531 	uint64_t lds_base;
532 	uint64_t lds_limit;
533 	uint64_t gpuvm_base;
534 	uint64_t gpuvm_limit;
535 	uint64_t scratch_base;
536 	uint64_t scratch_limit;
537 
538 	/* VM context for GPUVM allocations */
539 	void *vm;
540 
541 	/* Flag used to tell the pdd has dequeued from the dqm.
542 	 * This is used to prevent dev->dqm->ops.process_termination() from
543 	 * being called twice when it is already called in IOMMU callback
544 	 * function.
545 	 */
546 	bool already_dequeued;
547 
548 	/* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
549 	enum kfd_pdd_bound bound;
550 };
551 
552 #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
553 
554 /* Process data */
555 struct kfd_process {
556 	/*
557 	 * kfd_process are stored in an mm_struct*->kfd_process*
558 	 * hash table (kfd_processes in kfd_process.c)
559 	 */
560 	struct hlist_node kfd_processes;
561 
562 	/*
563 	 * Opaque pointer to mm_struct. We don't hold a reference to
564 	 * it so it should never be dereferenced from here. This is
565 	 * only used for looking up processes by their mm.
566 	 */
567 	void *mm;
568 
569 	struct kref ref;
570 	struct work_struct release_work;
571 
572 	struct mutex mutex;
573 
574 	/*
575 	 * In any process, the thread that started main() is the lead
576 	 * thread and outlives the rest.
577 	 * It is here because amd_iommu_bind_pasid wants a task_struct.
578 	 * It can also be used for safely getting a reference to the
579 	 * mm_struct of the process.
580 	 */
581 	struct task_struct *lead_thread;
582 
583 	/* We want to receive a notification when the mm_struct is destroyed */
584 	struct mmu_notifier mmu_notifier;
585 
586 	/* Use for delayed freeing of kfd_process structure */
587 	struct rcu_head	rcu;
588 
589 	unsigned int pasid;
590 	unsigned int doorbell_index;
591 
592 	/*
593 	 * List of kfd_process_device structures,
594 	 * one for each device the process is using.
595 	 */
596 	struct list_head per_device_data;
597 
598 	struct process_queue_manager pqm;
599 
600 	/*Is the user space process 32 bit?*/
601 	bool is_32bit_user_mode;
602 
603 	/* Event-related data */
604 	struct mutex event_mutex;
605 	/* Event ID allocator and lookup */
606 	struct idr event_idr;
607 	/* Event page */
608 	struct kfd_signal_page *signal_page;
609 	size_t signal_mapped_size;
610 	size_t signal_event_count;
611 	bool signal_event_limit_reached;
612 
613 	/* Information used for memory eviction */
614 	void *kgd_process_info;
615 	/* Eviction fence that is attached to all the BOs of this process. The
616 	 * fence will be triggered during eviction and new one will be created
617 	 * during restore
618 	 */
619 	struct dma_fence *ef;
620 
621 	/* Work items for evicting and restoring BOs */
622 	struct delayed_work eviction_work;
623 	struct delayed_work restore_work;
624 	/* seqno of the last scheduled eviction */
625 	unsigned int last_eviction_seqno;
626 	/* Approx. the last timestamp (in jiffies) when the process was
627 	 * restored after an eviction
628 	 */
629 	unsigned long last_restore_timestamp;
630 };
631 
632 #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
633 extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
634 extern struct srcu_struct kfd_processes_srcu;
635 
636 /**
637  * Ioctl function type.
638  *
639  * \param filep pointer to file structure.
640  * \param p amdkfd process pointer.
641  * \param data pointer to arg that was copied from user.
642  */
643 typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
644 				void *data);
645 
646 struct amdkfd_ioctl_desc {
647 	unsigned int cmd;
648 	int flags;
649 	amdkfd_ioctl_t *func;
650 	unsigned int cmd_drv;
651 	const char *name;
652 };
653 
654 void kfd_process_create_wq(void);
655 void kfd_process_destroy_wq(void);
656 struct kfd_process *kfd_create_process(struct file *filep);
657 struct kfd_process *kfd_get_process(const struct task_struct *);
658 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid);
659 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
660 void kfd_unref_process(struct kfd_process *p);
661 void kfd_suspend_all_processes(void);
662 int kfd_resume_all_processes(void);
663 
664 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
665 						struct kfd_process *p);
666 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
667 							struct kfd_process *p);
668 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
669 							struct kfd_process *p);
670 
671 int kfd_reserved_mem_mmap(struct kfd_process *process,
672 			  struct vm_area_struct *vma);
673 
674 /* Process device data iterator */
675 struct kfd_process_device *kfd_get_first_process_device_data(
676 							struct kfd_process *p);
677 struct kfd_process_device *kfd_get_next_process_device_data(
678 						struct kfd_process *p,
679 						struct kfd_process_device *pdd);
680 bool kfd_has_process_device_data(struct kfd_process *p);
681 
682 /* PASIDs */
683 int kfd_pasid_init(void);
684 void kfd_pasid_exit(void);
685 bool kfd_set_pasid_limit(unsigned int new_limit);
686 unsigned int kfd_get_pasid_limit(void);
687 unsigned int kfd_pasid_alloc(void);
688 void kfd_pasid_free(unsigned int pasid);
689 
690 /* Doorbells */
691 int kfd_doorbell_init(struct kfd_dev *kfd);
692 void kfd_doorbell_fini(struct kfd_dev *kfd);
693 int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
694 u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
695 					unsigned int *doorbell_off);
696 void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
697 u32 read_kernel_doorbell(u32 __iomem *db);
698 void write_kernel_doorbell(u32 __iomem *db, u32 value);
699 unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
700 					struct kfd_process *process,
701 					unsigned int queue_id);
702 phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
703 					struct kfd_process *process);
704 int kfd_alloc_process_doorbells(struct kfd_process *process);
705 void kfd_free_process_doorbells(struct kfd_process *process);
706 
707 /* GTT Sub-Allocator */
708 
709 int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
710 			struct kfd_mem_obj **mem_obj);
711 
712 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);
713 
714 extern struct device *kfd_device;
715 
716 /* Topology */
717 int kfd_topology_init(void);
718 void kfd_topology_shutdown(void);
719 int kfd_topology_add_device(struct kfd_dev *gpu);
720 int kfd_topology_remove_device(struct kfd_dev *gpu);
721 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
722 						uint32_t proximity_domain);
723 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
724 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
725 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev);
726 int kfd_numa_node_to_apic_id(int numa_node_id);
727 
728 /* Interrupts */
729 int kfd_interrupt_init(struct kfd_dev *dev);
730 void kfd_interrupt_exit(struct kfd_dev *dev);
731 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
732 bool enqueue_ih_ring_entry(struct kfd_dev *kfd,	const void *ih_ring_entry);
733 bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry);
734 
735 /* Power Management */
736 void kgd2kfd_suspend(struct kfd_dev *kfd);
737 int kgd2kfd_resume(struct kfd_dev *kfd);
738 
739 /* amdkfd Apertures */
740 int kfd_init_apertures(struct kfd_process *process);
741 
742 /* Queue Context Management */
743 int init_queue(struct queue **q, const struct queue_properties *properties);
744 void uninit_queue(struct queue *q);
745 void print_queue_properties(struct queue_properties *q);
746 void print_queue(struct queue *q);
747 
748 struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
749 					struct kfd_dev *dev);
750 struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
751 		struct kfd_dev *dev);
752 struct mqd_manager *mqd_manager_init_cik_hawaii(enum KFD_MQD_TYPE type,
753 		struct kfd_dev *dev);
754 struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
755 		struct kfd_dev *dev);
756 struct mqd_manager *mqd_manager_init_vi_tonga(enum KFD_MQD_TYPE type,
757 		struct kfd_dev *dev);
758 struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
759 void device_queue_manager_uninit(struct device_queue_manager *dqm);
760 struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
761 					enum kfd_queue_type type);
762 void kernel_queue_uninit(struct kernel_queue *kq);
763 
764 /* Process Queue Manager */
765 struct process_queue_node {
766 	struct queue *q;
767 	struct kernel_queue *kq;
768 	struct list_head process_queue_list;
769 };
770 
771 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
772 void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
773 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
774 void pqm_uninit(struct process_queue_manager *pqm);
775 int pqm_create_queue(struct process_queue_manager *pqm,
776 			    struct kfd_dev *dev,
777 			    struct file *f,
778 			    struct queue_properties *properties,
779 			    unsigned int *qid);
780 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
781 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
782 			struct queue_properties *p);
783 struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
784 						unsigned int qid);
785 
786 int amdkfd_fence_wait_timeout(unsigned int *fence_addr,
787 				unsigned int fence_value,
788 				unsigned int timeout_ms);
789 
790 /* Packet Manager */
791 
792 #define KFD_FENCE_COMPLETED (100)
793 #define KFD_FENCE_INIT   (10)
794 
795 struct packet_manager {
796 	struct device_queue_manager *dqm;
797 	struct kernel_queue *priv_queue;
798 	struct mutex lock;
799 	bool allocated;
800 	struct kfd_mem_obj *ib_buffer_obj;
801 	unsigned int ib_size_bytes;
802 };
803 
804 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
805 void pm_uninit(struct packet_manager *pm);
806 int pm_send_set_resources(struct packet_manager *pm,
807 				struct scheduling_resources *res);
808 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
809 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
810 				uint32_t fence_value);
811 
812 int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
813 			enum kfd_unmap_queues_filter mode,
814 			uint32_t filter_param, bool reset,
815 			unsigned int sdma_engine);
816 
817 void pm_release_ib(struct packet_manager *pm);
818 
819 uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
820 
821 /* Events */
822 extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
823 extern const struct kfd_device_global_init_class device_global_init_class_cik;
824 
825 void kfd_event_init_process(struct kfd_process *p);
826 void kfd_event_free_process(struct kfd_process *p);
827 int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
828 int kfd_wait_on_events(struct kfd_process *p,
829 		       uint32_t num_events, void __user *data,
830 		       bool all, uint32_t user_timeout_ms,
831 		       uint32_t *wait_result);
832 void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id,
833 				uint32_t valid_id_bits);
834 void kfd_signal_iommu_event(struct kfd_dev *dev,
835 		unsigned int pasid, unsigned long address,
836 		bool is_write_requested, bool is_execute_requested);
837 void kfd_signal_hw_exception_event(unsigned int pasid);
838 int kfd_set_event(struct kfd_process *p, uint32_t event_id);
839 int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
840 int kfd_event_create(struct file *devkfd, struct kfd_process *p,
841 		     uint32_t event_type, bool auto_reset, uint32_t node_id,
842 		     uint32_t *event_id, uint32_t *event_trigger_data,
843 		     uint64_t *event_page_offset, uint32_t *event_slot_index);
844 int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
845 
846 void kfd_flush_tlb(struct kfd_process_device *pdd);
847 
848 int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p);
849 
850 /* Debugfs */
851 #if defined(CONFIG_DEBUG_FS)
852 
853 void kfd_debugfs_init(void);
854 void kfd_debugfs_fini(void);
855 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data);
856 int pqm_debugfs_mqds(struct seq_file *m, void *data);
857 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data);
858 int dqm_debugfs_hqds(struct seq_file *m, void *data);
859 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data);
860 int pm_debugfs_runlist(struct seq_file *m, void *data);
861 
862 #else
863 
864 static inline void kfd_debugfs_init(void) {}
865 static inline void kfd_debugfs_fini(void) {}
866 
867 #endif
868 
869 #endif
870