2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #ifndef KFD_PRIV_H_INCLUDED
24 #define KFD_PRIV_H_INCLUDED
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 <kgd_kfd_interface.h>
36 #define KFD_SYSFS_FILE_MODE 0444
38 /* GPU ID hash width in bits */
39 #define KFD_GPU_ID_HASH_WIDTH 16
41 /* Macro for allocating structures */
42 #define kfd_alloc_struct(ptr_to_struct) \
43 ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
45 /* Kernel module parameter to specify maximum number of supported processes */
46 extern int max_num_of_processes;
48 #define KFD_MAX_NUM_OF_PROCESSES_DEFAULT 32
49 #define KFD_MAX_NUM_OF_PROCESSES 512
52 * Kernel module parameter to specify maximum number of supported queues
55 extern int max_num_of_queues_per_process;
57 #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS_DEFAULT 128
58 #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
61 struct kfd_device_info {
62 unsigned int max_pasid_bits;
63 size_t ih_ring_entry_size;
64 uint16_t mqd_size_aligned;
70 const struct kfd_device_info *device_info;
73 unsigned int id; /* topology stub index */
75 phys_addr_t doorbell_base; /* Start of actual doorbells used by
76 * KFD. It is aligned for mapping
79 size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell
80 * to HW doorbell, GFX reserved some
83 size_t doorbell_process_limit; /* Number of processes we have doorbell
86 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
87 * page used by kernel queue
90 struct kgd2kfd_shared_resources shared_resources;
96 /* KGD2KFD callbacks */
97 void kgd2kfd_exit(void);
98 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev);
99 bool kgd2kfd_device_init(struct kfd_dev *kfd,
100 const struct kgd2kfd_shared_resources *gpu_resources);
101 void kgd2kfd_device_exit(struct kfd_dev *kfd);
103 extern const struct kfd2kgd_calls *kfd2kgd;
112 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
113 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
114 KFD_MEMPOOL_FRAMEBUFFER = 3,
117 /* Character device interface */
118 int kfd_chardev_init(void);
119 void kfd_chardev_exit(void);
120 struct device *kfd_chardev(void);
123 /* Data that is per-process-per device. */
124 struct kfd_process_device {
126 * List of all per-device data for a process.
127 * Starts from kfd_process.per_device_data.
129 struct list_head per_device_list;
131 /* The device that owns this data. */
139 uint64_t gpuvm_limit;
140 uint64_t scratch_base;
141 uint64_t scratch_limit;
143 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
150 * kfd_process are stored in an mm_struct*->kfd_process*
151 * hash table (kfd_processes in kfd_process.c)
153 struct hlist_node kfd_processes;
155 struct mm_struct *mm;
160 * In any process, the thread that started main() is the lead
161 * thread and outlives the rest.
162 * It is here because amd_iommu_bind_pasid wants a task_struct.
164 struct task_struct *lead_thread;
166 /* We want to receive a notification when the mm_struct is destroyed */
167 struct mmu_notifier mmu_notifier;
169 /* Use for delayed freeing of kfd_process structure */
175 * List of kfd_process_device structures,
176 * one for each device the process is using.
178 struct list_head per_device_data;
180 /* The process's queues. */
181 size_t queue_array_size;
183 /* Size is queue_array_size, up to MAX_PROCESS_QUEUES. */
184 struct kfd_queue **queues;
186 unsigned long allocated_queue_bitmap[DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, BITS_PER_LONG)];
188 /*Is the user space process 32 bit?*/
189 bool is_32bit_user_mode;
192 void kfd_process_create_wq(void);
193 void kfd_process_destroy_wq(void);
194 struct kfd_process *kfd_create_process(const struct task_struct *);
195 struct kfd_process *kfd_get_process(const struct task_struct *);
197 void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid);
198 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
199 struct kfd_process *p,
203 int kfd_pasid_init(void);
204 void kfd_pasid_exit(void);
205 bool kfd_set_pasid_limit(unsigned int new_limit);
206 unsigned int kfd_get_pasid_limit(void);
207 unsigned int kfd_pasid_alloc(void);
208 void kfd_pasid_free(unsigned int pasid);
211 void kfd_doorbell_init(struct kfd_dev *kfd);
212 int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
213 u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
214 unsigned int *doorbell_off);
215 void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
216 u32 read_kernel_doorbell(u32 __iomem *db);
217 void write_kernel_doorbell(u32 __iomem *db, u32 value);
218 unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
219 struct kfd_process *process,
220 unsigned int queue_id);
222 extern struct device *kfd_device;
225 int kfd_topology_init(void);
226 void kfd_topology_shutdown(void);
227 int kfd_topology_add_device(struct kfd_dev *gpu);
228 int kfd_topology_remove_device(struct kfd_dev *gpu);
229 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
230 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
231 struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx);
234 void kgd2kfd_interrupt(struct kfd_dev *dev, const void *ih_ring_entry);
236 /* Power Management */
237 void kgd2kfd_suspend(struct kfd_dev *dev);
238 int kgd2kfd_resume(struct kfd_dev *dev);
240 /* amdkfd Apertures */
241 int kfd_init_apertures(struct kfd_process *process);
243 uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
244 phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
245 struct kfd_process *process);