amdkfd: Add basic modules to amdkfd
[firefly-linux-kernel-4.4.55.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 <kgd_kfd_interface.h>
35
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
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
61 struct kfd_device_info {
62         unsigned int max_pasid_bits;
63         size_t ih_ring_entry_size;
64         uint16_t mqd_size_aligned;
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
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
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
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
117 /* Character device interface */
118 int kfd_chardev_init(void);
119 void kfd_chardev_exit(void);
120 struct device *kfd_chardev(void);
121
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
147 /* Process data */
148 struct kfd_process {
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;
190 };
191
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
197 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
198                                                         struct kfd_process *p,
199                                                         int create_pdd);
200
201 /* PASIDs */
202 int kfd_pasid_init(void);
203 void kfd_pasid_exit(void);
204 bool kfd_set_pasid_limit(unsigned int new_limit);
205 unsigned int kfd_get_pasid_limit(void);
206 unsigned int kfd_pasid_alloc(void);
207 void kfd_pasid_free(unsigned int pasid);
208
209 /* Doorbells */
210 void kfd_doorbell_init(struct kfd_dev *kfd);
211 int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
212 u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
213                                         unsigned int *doorbell_off);
214 void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
215 u32 read_kernel_doorbell(u32 __iomem *db);
216 void write_kernel_doorbell(u32 __iomem *db, u32 value);
217 unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
218                                         struct kfd_process *process,
219                                         unsigned int queue_id);
220
221 extern struct device *kfd_device;
222
223 /* Topology */
224 int kfd_topology_init(void);
225 void kfd_topology_shutdown(void);
226 int kfd_topology_add_device(struct kfd_dev *gpu);
227 int kfd_topology_remove_device(struct kfd_dev *gpu);
228 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
229 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
230 struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx);
231
232 /* Interrupts */
233 void kgd2kfd_interrupt(struct kfd_dev *dev, const void *ih_ring_entry);
234
235 /* Power Management */
236 void kgd2kfd_suspend(struct kfd_dev *dev);
237 int kgd2kfd_resume(struct kfd_dev *dev);
238
239 /* amdkfd Apertures */
240 int kfd_init_apertures(struct kfd_process *process);
241
242 uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
243 phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
244                                         struct kfd_process *process);
245
246 #endif