blob: 431a09bbab6240847eb32fa3cd5729c52a9f09d4 [file] [log] [blame]
Oded Gabbay4a488a72014-07-16 21:08:55 +03001/*
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 Gabbay19f6d2a2014-07-16 23:25:31 +030033#include <linux/kfd_ioctl.h>
Oded Gabbay4a488a72014-07-16 21:08:55 +030034#include <kgd_kfd_interface.h>
35
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +030036#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 Gabbay19f6d2a2014-07-16 23:25:31 +030045/* Kernel module parameter to specify maximum number of supported processes */
46extern 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 */
55extern 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 Gabbay4a488a72014-07-16 21:08:55 +030061struct kfd_device_info {
62 unsigned int max_pasid_bits;
63 size_t ih_ring_entry_size;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030064 uint16_t mqd_size_aligned;
Oded Gabbay4a488a72014-07-16 21:08:55 +030065};
66
67struct 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 Gabbay19f6d2a2014-07-16 23:25:31 +030075 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 Gabbay4a488a72014-07-16 21:08:55 +030090 struct kgd2kfd_shared_resources shared_resources;
91
92 bool init_complete;
93
94};
95
96/* KGD2KFD callbacks */
97void kgd2kfd_exit(void);
98struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev);
99bool kgd2kfd_device_init(struct kfd_dev *kfd,
100 const struct kgd2kfd_shared_resources *gpu_resources);
101void kgd2kfd_device_exit(struct kfd_dev *kfd);
102
103extern const struct kfd2kgd_calls *kfd2kgd;
104
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300105struct kfd_mem_obj {
106 void *bo;
107 uint64_t gpu_addr;
108 uint32_t *cpu_ptr;
109};
110
111enum kfd_mempool {
112 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
113 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
114 KFD_MEMPOOL_FRAMEBUFFER = 3,
115};
116
Oded Gabbay4a488a72014-07-16 21:08:55 +0300117/* Character device interface */
118int kfd_chardev_init(void);
119void kfd_chardev_exit(void);
120struct device *kfd_chardev(void);
121
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300122
123/* Data that is per-process-per device. */
124struct 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 Gabbay4a488a72014-07-16 21:08:55 +0300147/* Process data */
148struct kfd_process {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300149 /*
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 Gabbay4a488a72014-07-16 21:08:55 +0300190};
191
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300192void kfd_process_create_wq(void);
193void kfd_process_destroy_wq(void);
194struct kfd_process *kfd_create_process(const struct task_struct *);
195struct kfd_process *kfd_get_process(const struct task_struct *);
196
Oded Gabbayb17f0682014-07-17 00:06:27 +0300197void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300198struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
199 struct kfd_process *p,
200 int create_pdd);
201
202/* PASIDs */
203int kfd_pasid_init(void);
204void kfd_pasid_exit(void);
205bool kfd_set_pasid_limit(unsigned int new_limit);
206unsigned int kfd_get_pasid_limit(void);
207unsigned int kfd_pasid_alloc(void);
208void kfd_pasid_free(unsigned int pasid);
209
210/* Doorbells */
211void kfd_doorbell_init(struct kfd_dev *kfd);
212int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
213u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
214 unsigned int *doorbell_off);
215void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
216u32 read_kernel_doorbell(u32 __iomem *db);
217void write_kernel_doorbell(u32 __iomem *db, u32 value);
218unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
219 struct kfd_process *process,
220 unsigned int queue_id);
221
Oded Gabbay4a488a72014-07-16 21:08:55 +0300222extern struct device *kfd_device;
223
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300224/* Topology */
225int kfd_topology_init(void);
226void kfd_topology_shutdown(void);
227int kfd_topology_add_device(struct kfd_dev *gpu);
228int kfd_topology_remove_device(struct kfd_dev *gpu);
229struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
230struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
231struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx);
232
Oded Gabbay4a488a72014-07-16 21:08:55 +0300233/* Interrupts */
234void kgd2kfd_interrupt(struct kfd_dev *dev, const void *ih_ring_entry);
235
236/* Power Management */
237void kgd2kfd_suspend(struct kfd_dev *dev);
238int kgd2kfd_resume(struct kfd_dev *dev);
239
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300240/* amdkfd Apertures */
241int kfd_init_apertures(struct kfd_process *process);
242
243uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
244phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
245 struct kfd_process *process);
246
Oded Gabbay4a488a72014-07-16 21:08:55 +0300247#endif