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