blob: 32bca8a674235bc472de47ab34760039253acc37 [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
Ben Gozed6e6a32014-07-17 00:45:35 +030038/*
39 * When working with cp scheduler we should assign the HIQ manually or via
40 * the radeon driver to a fixed hqd slot, here are the fixed HIQ hqd slot
41 * definitions for Kaveri. In Kaveri only the first ME queues participates
42 * in the cp scheduling taking that in mind we set the HIQ slot in the
43 * second ME.
44 */
45#define KFD_CIK_HIQ_PIPE 4
46#define KFD_CIK_HIQ_QUEUE 0
47
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +030048/* GPU ID hash width in bits */
49#define KFD_GPU_ID_HASH_WIDTH 16
50
51/* Macro for allocating structures */
52#define kfd_alloc_struct(ptr_to_struct) \
53 ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
54
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030055/* Kernel module parameter to specify maximum number of supported processes */
56extern int max_num_of_processes;
57
58#define KFD_MAX_NUM_OF_PROCESSES_DEFAULT 32
59#define KFD_MAX_NUM_OF_PROCESSES 512
60
61/*
62 * Kernel module parameter to specify maximum number of supported queues
63 * per process
64 */
65extern int max_num_of_queues_per_process;
66
67#define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS_DEFAULT 128
68#define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
69
Ben Gozed6e6a32014-07-17 00:45:35 +030070#define KFD_KERNEL_QUEUE_SIZE 2048
71
Ben Goz31c21fe2014-07-17 00:48:28 +030072/* Kernel module parameter to specify the scheduling policy */
73extern int sched_policy;
74
75/**
76 * enum kfd_sched_policy
77 *
78 * @KFD_SCHED_POLICY_HWS: H/W scheduling policy known as command processor (cp)
79 * scheduling. In this scheduling mode we're using the firmware code to
80 * schedule the user mode queues and kernel queues such as HIQ and DIQ.
81 * the HIQ queue is used as a special queue that dispatches the configuration
82 * to the cp and the user mode queues list that are currently running.
83 * the DIQ queue is a debugging queue that dispatches debugging commands to the
84 * firmware.
85 * in this scheduling mode user mode queues over subscription feature is
86 * enabled.
87 *
88 * @KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: The same as above but the over
89 * subscription feature disabled.
90 *
91 * @KFD_SCHED_POLICY_NO_HWS: no H/W scheduling policy is a mode which directly
92 * set the command processor registers and sets the queues "manually". This
93 * mode is used *ONLY* for debugging proposes.
94 *
95 */
96enum kfd_sched_policy {
97 KFD_SCHED_POLICY_HWS = 0,
98 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION,
99 KFD_SCHED_POLICY_NO_HWS
100};
101
Ben Gozed6e6a32014-07-17 00:45:35 +0300102enum cache_policy {
103 cache_policy_coherent,
104 cache_policy_noncoherent
105};
106
Oded Gabbay4a488a72014-07-16 21:08:55 +0300107struct kfd_device_info {
108 unsigned int max_pasid_bits;
109 size_t ih_ring_entry_size;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300110 uint16_t mqd_size_aligned;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300111};
112
113struct kfd_dev {
114 struct kgd_dev *kgd;
115
116 const struct kfd_device_info *device_info;
117 struct pci_dev *pdev;
118
119 unsigned int id; /* topology stub index */
120
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300121 phys_addr_t doorbell_base; /* Start of actual doorbells used by
122 * KFD. It is aligned for mapping
123 * into user mode
124 */
125 size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell
126 * to HW doorbell, GFX reserved some
127 * at the start)
128 */
129 size_t doorbell_process_limit; /* Number of processes we have doorbell
130 * space for.
131 */
132 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
133 * page used by kernel queue
134 */
135
Oded Gabbay4a488a72014-07-16 21:08:55 +0300136 struct kgd2kfd_shared_resources shared_resources;
137
Ben Gozed6e6a32014-07-17 00:45:35 +0300138 /* QCM Device instance */
139 struct device_queue_manager *dqm;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300140
Ben Gozed6e6a32014-07-17 00:45:35 +0300141 bool init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300142};
143
144/* KGD2KFD callbacks */
145void kgd2kfd_exit(void);
146struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev);
147bool kgd2kfd_device_init(struct kfd_dev *kfd,
148 const struct kgd2kfd_shared_resources *gpu_resources);
149void kgd2kfd_device_exit(struct kfd_dev *kfd);
150
151extern const struct kfd2kgd_calls *kfd2kgd;
152
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300153struct kfd_mem_obj {
154 void *bo;
155 uint64_t gpu_addr;
156 uint32_t *cpu_ptr;
157};
158
159enum kfd_mempool {
160 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
161 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
162 KFD_MEMPOOL_FRAMEBUFFER = 3,
163};
164
Oded Gabbay4a488a72014-07-16 21:08:55 +0300165/* Character device interface */
166int kfd_chardev_init(void);
167void kfd_chardev_exit(void);
168struct device *kfd_chardev(void);
169
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300170
Ben Goz6e99df52014-07-17 00:36:17 +0300171enum kfd_preempt_type {
172 KFD_PREEMPT_TYPE_WAVEFRONT,
173 KFD_PREEMPT_TYPE_WAVEFRONT_RESET
174};
175
Ben Gozed8aab42014-07-17 00:18:51 +0300176/**
177 * enum kfd_queue_type
178 *
179 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
180 *
181 * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type.
182 *
183 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
184 *
185 * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
186 */
187enum kfd_queue_type {
188 KFD_QUEUE_TYPE_COMPUTE,
189 KFD_QUEUE_TYPE_SDMA,
190 KFD_QUEUE_TYPE_HIQ,
191 KFD_QUEUE_TYPE_DIQ
192};
193
Ben Goz6e99df52014-07-17 00:36:17 +0300194enum kfd_queue_format {
195 KFD_QUEUE_FORMAT_PM4,
196 KFD_QUEUE_FORMAT_AQL
197};
198
Ben Gozed8aab42014-07-17 00:18:51 +0300199/**
200 * struct queue_properties
201 *
202 * @type: The queue type.
203 *
204 * @queue_id: Queue identifier.
205 *
206 * @queue_address: Queue ring buffer address.
207 *
208 * @queue_size: Queue ring buffer size.
209 *
210 * @priority: Defines the queue priority relative to other queues in the
211 * process.
212 * This is just an indication and HW scheduling may override the priority as
213 * necessary while keeping the relative prioritization.
214 * the priority granularity is from 0 to f which f is the highest priority.
215 * currently all queues are initialized with the highest priority.
216 *
217 * @queue_percent: This field is partially implemented and currently a zero in
218 * this field defines that the queue is non active.
219 *
220 * @read_ptr: User space address which points to the number of dwords the
221 * cp read from the ring buffer. This field updates automatically by the H/W.
222 *
223 * @write_ptr: Defines the number of dwords written to the ring buffer.
224 *
225 * @doorbell_ptr: This field aim is to notify the H/W of new packet written to
226 * the queue ring buffer. This field should be similar to write_ptr and the user
227 * should update this field after he updated the write_ptr.
228 *
229 * @doorbell_off: The doorbell offset in the doorbell pci-bar.
230 *
231 * @is_interop: Defines if this is a interop queue. Interop queue means that the
232 * queue can access both graphics and compute resources.
233 *
234 * @is_active: Defines if the queue is active or not.
235 *
236 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
237 * of the queue.
238 *
239 * This structure represents the queue properties for each queue no matter if
240 * it's user mode or kernel mode queue.
241 *
242 */
243struct queue_properties {
244 enum kfd_queue_type type;
Ben Goz6e99df52014-07-17 00:36:17 +0300245 enum kfd_queue_format format;
Ben Gozed8aab42014-07-17 00:18:51 +0300246 unsigned int queue_id;
247 uint64_t queue_address;
248 uint64_t queue_size;
249 uint32_t priority;
250 uint32_t queue_percent;
251 uint32_t *read_ptr;
252 uint32_t *write_ptr;
253 uint32_t *doorbell_ptr;
254 uint32_t doorbell_off;
255 bool is_interop;
256 bool is_active;
257 /* Not relevant for user mode queues in cp scheduling */
258 unsigned int vmid;
259};
260
261/**
262 * struct queue
263 *
264 * @list: Queue linked list.
265 *
266 * @mqd: The queue MQD.
267 *
268 * @mqd_mem_obj: The MQD local gpu memory object.
269 *
270 * @gart_mqd_addr: The MQD gart mc address.
271 *
272 * @properties: The queue properties.
273 *
274 * @mec: Used only in no cp scheduling mode and identifies to micro engine id
275 * that the queue should be execute on.
276 *
277 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe id.
278 *
279 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
280 *
281 * @process: The kfd process that created this queue.
282 *
283 * @device: The kfd device that created this queue.
284 *
285 * This structure represents user mode compute queues.
286 * It contains all the necessary data to handle such queues.
287 *
288 */
289
290struct queue {
291 struct list_head list;
292 void *mqd;
293 struct kfd_mem_obj *mqd_mem_obj;
294 uint64_t gart_mqd_addr;
295 struct queue_properties properties;
296
297 uint32_t mec;
298 uint32_t pipe;
299 uint32_t queue;
300
301 struct kfd_process *process;
302 struct kfd_dev *device;
303};
304
Ben Goz6e99df52014-07-17 00:36:17 +0300305/*
306 * Please read the kfd_mqd_manager.h description.
307 */
308enum KFD_MQD_TYPE {
309 KFD_MQD_TYPE_CIK_COMPUTE = 0, /* for no cp scheduling */
310 KFD_MQD_TYPE_CIK_HIQ, /* for hiq */
311 KFD_MQD_TYPE_CIK_CP, /* for cp queues and diq */
312 KFD_MQD_TYPE_CIK_SDMA, /* for sdma queues */
313 KFD_MQD_TYPE_MAX
314};
315
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300316/* Data that is per-process-per device. */
317struct kfd_process_device {
318 /*
319 * List of all per-device data for a process.
320 * Starts from kfd_process.per_device_data.
321 */
322 struct list_head per_device_list;
323
324 /* The device that owns this data. */
325 struct kfd_dev *dev;
326
327
328 /*Apertures*/
329 uint64_t lds_base;
330 uint64_t lds_limit;
331 uint64_t gpuvm_base;
332 uint64_t gpuvm_limit;
333 uint64_t scratch_base;
334 uint64_t scratch_limit;
335
336 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
337 bool bound;
338};
339
Oded Gabbay4a488a72014-07-16 21:08:55 +0300340/* Process data */
341struct kfd_process {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300342 /*
343 * kfd_process are stored in an mm_struct*->kfd_process*
344 * hash table (kfd_processes in kfd_process.c)
345 */
346 struct hlist_node kfd_processes;
347
348 struct mm_struct *mm;
349
350 struct mutex mutex;
351
352 /*
353 * In any process, the thread that started main() is the lead
354 * thread and outlives the rest.
355 * It is here because amd_iommu_bind_pasid wants a task_struct.
356 */
357 struct task_struct *lead_thread;
358
359 /* We want to receive a notification when the mm_struct is destroyed */
360 struct mmu_notifier mmu_notifier;
361
362 /* Use for delayed freeing of kfd_process structure */
363 struct rcu_head rcu;
364
365 unsigned int pasid;
366
367 /*
368 * List of kfd_process_device structures,
369 * one for each device the process is using.
370 */
371 struct list_head per_device_data;
372
373 /* The process's queues. */
374 size_t queue_array_size;
375
376 /* Size is queue_array_size, up to MAX_PROCESS_QUEUES. */
377 struct kfd_queue **queues;
378
379 unsigned long allocated_queue_bitmap[DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, BITS_PER_LONG)];
380
381 /*Is the user space process 32 bit?*/
382 bool is_32bit_user_mode;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300383};
384
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300385void kfd_process_create_wq(void);
386void kfd_process_destroy_wq(void);
387struct kfd_process *kfd_create_process(const struct task_struct *);
388struct kfd_process *kfd_get_process(const struct task_struct *);
389
Oded Gabbayb17f0682014-07-17 00:06:27 +0300390void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300391struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
392 struct kfd_process *p,
393 int create_pdd);
394
395/* PASIDs */
396int kfd_pasid_init(void);
397void kfd_pasid_exit(void);
398bool kfd_set_pasid_limit(unsigned int new_limit);
399unsigned int kfd_get_pasid_limit(void);
400unsigned int kfd_pasid_alloc(void);
401void kfd_pasid_free(unsigned int pasid);
402
403/* Doorbells */
404void kfd_doorbell_init(struct kfd_dev *kfd);
405int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
406u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
407 unsigned int *doorbell_off);
408void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
409u32 read_kernel_doorbell(u32 __iomem *db);
410void write_kernel_doorbell(u32 __iomem *db, u32 value);
411unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
412 struct kfd_process *process,
413 unsigned int queue_id);
414
Oded Gabbay4a488a72014-07-16 21:08:55 +0300415extern struct device *kfd_device;
416
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300417/* Topology */
418int kfd_topology_init(void);
419void kfd_topology_shutdown(void);
420int kfd_topology_add_device(struct kfd_dev *gpu);
421int kfd_topology_remove_device(struct kfd_dev *gpu);
422struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
423struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
424struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx);
425
Oded Gabbay4a488a72014-07-16 21:08:55 +0300426/* Interrupts */
427void kgd2kfd_interrupt(struct kfd_dev *dev, const void *ih_ring_entry);
428
429/* Power Management */
430void kgd2kfd_suspend(struct kfd_dev *dev);
431int kgd2kfd_resume(struct kfd_dev *dev);
432
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300433/* amdkfd Apertures */
434int kfd_init_apertures(struct kfd_process *process);
435
Ben Gozed6e6a32014-07-17 00:45:35 +0300436/* Queue Context Management */
437int init_queue(struct queue **q, struct queue_properties properties);
438void uninit_queue(struct queue *q);
439void print_queue(struct queue *q);
440
441/* Packet Manager */
442
443struct packet_manager {
444 struct device_queue_manager *dqm;
445 struct kernel_queue *priv_queue;
446 struct mutex lock;
447 bool allocated;
448 struct kfd_mem_obj *ib_buffer_obj;
449};
450
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300451uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
452phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
453 struct kfd_process *process);
454
Oded Gabbay4a488a72014-07-16 21:08:55 +0300455#endif