Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 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> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 33 | #include <linux/kfd_ioctl.h> |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 34 | #include <kgd_kfd_interface.h> |
| 35 | |
Evgeny Pinchuk | 5b5c4e4 | 2014-07-16 21:22:32 +0300 | [diff] [blame] | 36 | #define KFD_SYSFS_FILE_MODE 0444 |
| 37 | |
| 38 | /* GPU ID hash width in bits */ |
| 39 | #define KFD_GPU_ID_HASH_WIDTH 16 |
| 40 | |
| 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)) |
| 44 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 45 | /* Kernel module parameter to specify maximum number of supported processes */ |
| 46 | extern int max_num_of_processes; |
| 47 | |
| 48 | #define KFD_MAX_NUM_OF_PROCESSES_DEFAULT 32 |
| 49 | #define KFD_MAX_NUM_OF_PROCESSES 512 |
| 50 | |
| 51 | /* |
| 52 | * Kernel module parameter to specify maximum number of supported queues |
| 53 | * per process |
| 54 | */ |
| 55 | extern int max_num_of_queues_per_process; |
| 56 | |
| 57 | #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS_DEFAULT 128 |
| 58 | #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024 |
| 59 | |
| 60 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 61 | struct kfd_device_info { |
| 62 | unsigned int max_pasid_bits; |
| 63 | size_t ih_ring_entry_size; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 64 | uint16_t mqd_size_aligned; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct kfd_dev { |
| 68 | struct kgd_dev *kgd; |
| 69 | |
| 70 | const struct kfd_device_info *device_info; |
| 71 | struct pci_dev *pdev; |
| 72 | |
| 73 | unsigned int id; /* topology stub index */ |
| 74 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 75 | phys_addr_t doorbell_base; /* Start of actual doorbells used by |
| 76 | * KFD. It is aligned for mapping |
| 77 | * into user mode |
| 78 | */ |
| 79 | size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell |
| 80 | * to HW doorbell, GFX reserved some |
| 81 | * at the start) |
| 82 | */ |
| 83 | size_t doorbell_process_limit; /* Number of processes we have doorbell |
| 84 | * space for. |
| 85 | */ |
| 86 | u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells |
| 87 | * page used by kernel queue |
| 88 | */ |
| 89 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 90 | struct kgd2kfd_shared_resources shared_resources; |
| 91 | |
| 92 | bool init_complete; |
| 93 | |
| 94 | }; |
| 95 | |
| 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); |
| 102 | |
| 103 | extern const struct kfd2kgd_calls *kfd2kgd; |
| 104 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 105 | struct kfd_mem_obj { |
| 106 | void *bo; |
| 107 | uint64_t gpu_addr; |
| 108 | uint32_t *cpu_ptr; |
| 109 | }; |
| 110 | |
| 111 | enum kfd_mempool { |
| 112 | KFD_MEMPOOL_SYSTEM_CACHEABLE = 1, |
| 113 | KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2, |
| 114 | KFD_MEMPOOL_FRAMEBUFFER = 3, |
| 115 | }; |
| 116 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 117 | /* Character device interface */ |
| 118 | int kfd_chardev_init(void); |
| 119 | void kfd_chardev_exit(void); |
| 120 | struct device *kfd_chardev(void); |
| 121 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 122 | |
| 123 | /* Data that is per-process-per device. */ |
| 124 | struct kfd_process_device { |
| 125 | /* |
| 126 | * List of all per-device data for a process. |
| 127 | * Starts from kfd_process.per_device_data. |
| 128 | */ |
| 129 | struct list_head per_device_list; |
| 130 | |
| 131 | /* The device that owns this data. */ |
| 132 | struct kfd_dev *dev; |
| 133 | |
| 134 | |
| 135 | /*Apertures*/ |
| 136 | uint64_t lds_base; |
| 137 | uint64_t lds_limit; |
| 138 | uint64_t gpuvm_base; |
| 139 | uint64_t gpuvm_limit; |
| 140 | uint64_t scratch_base; |
| 141 | uint64_t scratch_limit; |
| 142 | |
| 143 | /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */ |
| 144 | bool bound; |
| 145 | }; |
| 146 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 147 | /* Process data */ |
| 148 | struct kfd_process { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 149 | /* |
| 150 | * kfd_process are stored in an mm_struct*->kfd_process* |
| 151 | * hash table (kfd_processes in kfd_process.c) |
| 152 | */ |
| 153 | struct hlist_node kfd_processes; |
| 154 | |
| 155 | struct mm_struct *mm; |
| 156 | |
| 157 | struct mutex mutex; |
| 158 | |
| 159 | /* |
| 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. |
| 163 | */ |
| 164 | struct task_struct *lead_thread; |
| 165 | |
| 166 | /* We want to receive a notification when the mm_struct is destroyed */ |
| 167 | struct mmu_notifier mmu_notifier; |
| 168 | |
| 169 | /* Use for delayed freeing of kfd_process structure */ |
| 170 | struct rcu_head rcu; |
| 171 | |
| 172 | unsigned int pasid; |
| 173 | |
| 174 | /* |
| 175 | * List of kfd_process_device structures, |
| 176 | * one for each device the process is using. |
| 177 | */ |
| 178 | struct list_head per_device_data; |
| 179 | |
| 180 | /* The process's queues. */ |
| 181 | size_t queue_array_size; |
| 182 | |
| 183 | /* Size is queue_array_size, up to MAX_PROCESS_QUEUES. */ |
| 184 | struct kfd_queue **queues; |
| 185 | |
| 186 | unsigned long allocated_queue_bitmap[DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, BITS_PER_LONG)]; |
| 187 | |
| 188 | /*Is the user space process 32 bit?*/ |
| 189 | bool is_32bit_user_mode; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 190 | }; |
| 191 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 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 *); |
| 196 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame^] | 197 | void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 198 | struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, |
| 199 | struct kfd_process *p, |
| 200 | int create_pdd); |
| 201 | |
| 202 | /* PASIDs */ |
| 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); |
| 209 | |
| 210 | /* Doorbells */ |
| 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); |
| 221 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 222 | extern struct device *kfd_device; |
| 223 | |
Evgeny Pinchuk | 5b5c4e4 | 2014-07-16 21:22:32 +0300 | [diff] [blame] | 224 | /* Topology */ |
| 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); |
| 232 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 233 | /* Interrupts */ |
| 234 | void kgd2kfd_interrupt(struct kfd_dev *dev, const void *ih_ring_entry); |
| 235 | |
| 236 | /* Power Management */ |
| 237 | void kgd2kfd_suspend(struct kfd_dev *dev); |
| 238 | int kgd2kfd_resume(struct kfd_dev *dev); |
| 239 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 240 | /* amdkfd Apertures */ |
| 241 | int kfd_init_apertures(struct kfd_process *process); |
| 242 | |
| 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); |
| 246 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 247 | #endif |