blob: f0ca60f27d9a75af7d4c0f754ef5c2fa27c65dd8 [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
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030038#define KFD_MMAP_DOORBELL_MASK 0x8000000000000
39#define KFD_MMAP_EVENTS_MASK 0x4000000000000
40
Ben Gozed6e6a32014-07-17 00:45:35 +030041/*
42 * When working with cp scheduler we should assign the HIQ manually or via
43 * the radeon driver to a fixed hqd slot, here are the fixed HIQ hqd slot
44 * definitions for Kaveri. In Kaveri only the first ME queues participates
45 * in the cp scheduling taking that in mind we set the HIQ slot in the
46 * second ME.
47 */
48#define KFD_CIK_HIQ_PIPE 4
49#define KFD_CIK_HIQ_QUEUE 0
50
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +030051/* GPU ID hash width in bits */
52#define KFD_GPU_ID_HASH_WIDTH 16
53
54/* Macro for allocating structures */
55#define kfd_alloc_struct(ptr_to_struct) \
56 ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
57
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030058#define KFD_MAX_NUM_OF_PROCESSES 512
Oded Gabbayb8cbab02015-01-18 13:18:01 +020059#define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030060
61/*
Oded Gabbayb8cbab02015-01-18 13:18:01 +020062 * Kernel module parameter to specify maximum number of supported queues per
63 * device
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030064 */
Oded Gabbayb8cbab02015-01-18 13:18:01 +020065extern int max_num_of_queues_per_device;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030066
Oded Gabbayb8cbab02015-01-18 13:18:01 +020067#define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT 4096
68#define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \
69 (KFD_MAX_NUM_OF_PROCESSES * \
70 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030071
Ben Gozed6e6a32014-07-17 00:45:35 +030072#define KFD_KERNEL_QUEUE_SIZE 2048
73
Ben Goz31c21fe2014-07-17 00:48:28 +030074/* Kernel module parameter to specify the scheduling policy */
75extern int sched_policy;
76
Oded Gabbay81663012014-12-24 13:30:52 +020077/*
78 * Kernel module parameter to specify whether to send sigterm to HSA process on
79 * unhandled exception
80 */
81extern int send_sigterm;
82
Ben Goz31c21fe2014-07-17 00:48:28 +030083/**
84 * enum kfd_sched_policy
85 *
86 * @KFD_SCHED_POLICY_HWS: H/W scheduling policy known as command processor (cp)
87 * scheduling. In this scheduling mode we're using the firmware code to
88 * schedule the user mode queues and kernel queues such as HIQ and DIQ.
89 * the HIQ queue is used as a special queue that dispatches the configuration
90 * to the cp and the user mode queues list that are currently running.
91 * the DIQ queue is a debugging queue that dispatches debugging commands to the
92 * firmware.
93 * in this scheduling mode user mode queues over subscription feature is
94 * enabled.
95 *
96 * @KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: The same as above but the over
97 * subscription feature disabled.
98 *
99 * @KFD_SCHED_POLICY_NO_HWS: no H/W scheduling policy is a mode which directly
100 * set the command processor registers and sets the queues "manually". This
101 * mode is used *ONLY* for debugging proposes.
102 *
103 */
104enum kfd_sched_policy {
105 KFD_SCHED_POLICY_HWS = 0,
106 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION,
107 KFD_SCHED_POLICY_NO_HWS
108};
109
Ben Gozed6e6a32014-07-17 00:45:35 +0300110enum cache_policy {
111 cache_policy_coherent,
112 cache_policy_noncoherent
113};
114
Ben Goz0da75582015-01-01 17:10:01 +0200115enum asic_family_type {
116 CHIP_KAVERI = 0,
117 CHIP_CARRIZO
118};
119
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300120struct kfd_event_interrupt_class {
121 bool (*interrupt_isr)(struct kfd_dev *dev,
122 const uint32_t *ih_ring_entry);
123 void (*interrupt_wq)(struct kfd_dev *dev,
124 const uint32_t *ih_ring_entry);
125};
126
Oded Gabbay4a488a72014-07-16 21:08:55 +0300127struct kfd_device_info {
Ben Goz0da75582015-01-01 17:10:01 +0200128 unsigned int asic_family;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300129 const struct kfd_event_interrupt_class *event_interrupt_class;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300130 unsigned int max_pasid_bits;
Yair Shachar992839a2015-05-20 13:43:04 +0300131 unsigned int max_no_of_hqd;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300132 size_t ih_ring_entry_size;
Alexey Skidanovf7c826a2014-10-13 16:35:12 +0300133 uint8_t num_of_watch_points;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300134 uint16_t mqd_size_aligned;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300135};
136
Oded Gabbay36b5c082014-10-26 09:53:10 +0200137struct kfd_mem_obj {
138 uint32_t range_start;
139 uint32_t range_end;
140 uint64_t gpu_addr;
141 uint32_t *cpu_ptr;
142};
143
Yong Zhao44008d72017-09-20 18:10:18 -0400144struct kfd_vmid_info {
145 uint32_t first_vmid_kfd;
146 uint32_t last_vmid_kfd;
147 uint32_t vmid_num_kfd;
148};
149
Oded Gabbay4a488a72014-07-16 21:08:55 +0300150struct kfd_dev {
151 struct kgd_dev *kgd;
152
153 const struct kfd_device_info *device_info;
154 struct pci_dev *pdev;
155
156 unsigned int id; /* topology stub index */
157
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300158 phys_addr_t doorbell_base; /* Start of actual doorbells used by
159 * KFD. It is aligned for mapping
160 * into user mode
161 */
162 size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell
163 * to HW doorbell, GFX reserved some
164 * at the start)
165 */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300166 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
167 * page used by kernel queue
168 */
169
Oded Gabbay4a488a72014-07-16 21:08:55 +0300170 struct kgd2kfd_shared_resources shared_resources;
Yong Zhao44008d72017-09-20 18:10:18 -0400171 struct kfd_vmid_info vm_info;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300172
Xihan Zhangcea405b2015-03-17 19:32:53 +0800173 const struct kfd2kgd_calls *kfd2kgd;
174 struct mutex doorbell_mutex;
Joe Perchesf761d8b2015-05-19 18:37:51 -0700175 DECLARE_BITMAP(doorbell_available_index,
176 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800177
Oded Gabbay36b5c082014-10-26 09:53:10 +0200178 void *gtt_mem;
179 uint64_t gtt_start_gpu_addr;
180 void *gtt_start_cpu_ptr;
181 void *gtt_sa_bitmap;
182 struct mutex gtt_sa_lock;
183 unsigned int gtt_sa_chunk_size;
184 unsigned int gtt_sa_num_of_chunks;
185
Andrew Lewycky2249d552014-07-17 01:37:30 +0300186 /* Interrupts */
187 void *interrupt_ring;
188 size_t interrupt_ring_size;
189 atomic_t interrupt_ring_rptr;
190 atomic_t interrupt_ring_wptr;
191 struct work_struct interrupt_work;
192 spinlock_t interrupt_lock;
193
Ben Gozed6e6a32014-07-17 00:45:35 +0300194 /* QCM Device instance */
195 struct device_queue_manager *dqm;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300196
Ben Gozed6e6a32014-07-17 00:45:35 +0300197 bool init_complete;
Andrew Lewycky2249d552014-07-17 01:37:30 +0300198 /*
199 * Interrupts of interest to KFD are copied
200 * from the HW ring into a SW ring.
201 */
202 bool interrupts_active;
Yair Shacharfbeb6612015-05-20 13:48:26 +0300203
204 /* Debug manager */
205 struct kfd_dbgmgr *dbgmgr;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300206};
207
208/* KGD2KFD callbacks */
209void kgd2kfd_exit(void);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800210struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
211 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300212bool kgd2kfd_device_init(struct kfd_dev *kfd,
Xihan Zhangcea405b2015-03-17 19:32:53 +0800213 const struct kgd2kfd_shared_resources *gpu_resources);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300214void kgd2kfd_device_exit(struct kfd_dev *kfd);
215
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300216enum kfd_mempool {
217 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
218 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
219 KFD_MEMPOOL_FRAMEBUFFER = 3,
220};
221
Oded Gabbay4a488a72014-07-16 21:08:55 +0300222/* Character device interface */
223int kfd_chardev_init(void);
224void kfd_chardev_exit(void);
225struct device *kfd_chardev(void);
226
Ben Goz241f24f2014-07-17 00:55:28 +0300227/**
228 * enum kfd_preempt_type_filter
229 *
230 * @KFD_PREEMPT_TYPE_FILTER_SINGLE_QUEUE: Preempts single queue.
231 *
232 * @KFD_PRERMPT_TYPE_FILTER_ALL_QUEUES: Preempts all queues in the
233 * running queues list.
234 *
235 * @KFD_PRERMPT_TYPE_FILTER_BY_PASID: Preempts queues that belongs to
236 * specific process.
237 *
238 */
239enum kfd_preempt_type_filter {
240 KFD_PREEMPT_TYPE_FILTER_SINGLE_QUEUE,
241 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES,
Yair Shachar992839a2015-05-20 13:43:04 +0300242 KFD_PREEMPT_TYPE_FILTER_DYNAMIC_QUEUES,
Ben Goz241f24f2014-07-17 00:55:28 +0300243 KFD_PREEMPT_TYPE_FILTER_BY_PASID
244};
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300245
Ben Gozed8aab42014-07-17 00:18:51 +0300246/**
247 * enum kfd_queue_type
248 *
249 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
250 *
251 * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type.
252 *
253 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
254 *
255 * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
256 */
257enum kfd_queue_type {
258 KFD_QUEUE_TYPE_COMPUTE,
259 KFD_QUEUE_TYPE_SDMA,
260 KFD_QUEUE_TYPE_HIQ,
261 KFD_QUEUE_TYPE_DIQ
262};
263
Ben Goz6e99df52014-07-17 00:36:17 +0300264enum kfd_queue_format {
265 KFD_QUEUE_FORMAT_PM4,
266 KFD_QUEUE_FORMAT_AQL
267};
268
Ben Gozed8aab42014-07-17 00:18:51 +0300269/**
270 * struct queue_properties
271 *
272 * @type: The queue type.
273 *
274 * @queue_id: Queue identifier.
275 *
276 * @queue_address: Queue ring buffer address.
277 *
278 * @queue_size: Queue ring buffer size.
279 *
280 * @priority: Defines the queue priority relative to other queues in the
281 * process.
282 * This is just an indication and HW scheduling may override the priority as
283 * necessary while keeping the relative prioritization.
284 * the priority granularity is from 0 to f which f is the highest priority.
285 * currently all queues are initialized with the highest priority.
286 *
287 * @queue_percent: This field is partially implemented and currently a zero in
288 * this field defines that the queue is non active.
289 *
290 * @read_ptr: User space address which points to the number of dwords the
291 * cp read from the ring buffer. This field updates automatically by the H/W.
292 *
293 * @write_ptr: Defines the number of dwords written to the ring buffer.
294 *
295 * @doorbell_ptr: This field aim is to notify the H/W of new packet written to
Kent Russell8eabaf52017-08-15 23:00:04 -0400296 * the queue ring buffer. This field should be similar to write_ptr and the
297 * user should update this field after he updated the write_ptr.
Ben Gozed8aab42014-07-17 00:18:51 +0300298 *
299 * @doorbell_off: The doorbell offset in the doorbell pci-bar.
300 *
Kent Russell8eabaf52017-08-15 23:00:04 -0400301 * @is_interop: Defines if this is a interop queue. Interop queue means that
302 * the queue can access both graphics and compute resources.
Ben Gozed8aab42014-07-17 00:18:51 +0300303 *
304 * @is_active: Defines if the queue is active or not.
305 *
306 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
307 * of the queue.
308 *
309 * This structure represents the queue properties for each queue no matter if
310 * it's user mode or kernel mode queue.
311 *
312 */
313struct queue_properties {
314 enum kfd_queue_type type;
Ben Goz6e99df52014-07-17 00:36:17 +0300315 enum kfd_queue_format format;
Ben Gozed8aab42014-07-17 00:18:51 +0300316 unsigned int queue_id;
317 uint64_t queue_address;
318 uint64_t queue_size;
319 uint32_t priority;
320 uint32_t queue_percent;
321 uint32_t *read_ptr;
322 uint32_t *write_ptr;
Oded Gabbay5cd78de2014-11-20 16:14:56 +0200323 uint32_t __iomem *doorbell_ptr;
Ben Gozed8aab42014-07-17 00:18:51 +0300324 uint32_t doorbell_off;
325 bool is_interop;
326 bool is_active;
327 /* Not relevant for user mode queues in cp scheduling */
328 unsigned int vmid;
Ben Goz77669eb2015-01-03 22:12:31 +0200329 /* Relevant only for sdma queues*/
330 uint32_t sdma_engine_id;
331 uint32_t sdma_queue_id;
332 uint32_t sdma_vm_addr;
Ben Gozff3d04a2015-01-04 10:37:18 +0200333 /* Relevant only for VI */
334 uint64_t eop_ring_buffer_address;
335 uint32_t eop_ring_buffer_size;
336 uint64_t ctx_save_restore_area_address;
337 uint32_t ctx_save_restore_area_size;
Ben Gozed8aab42014-07-17 00:18:51 +0300338};
339
340/**
341 * struct queue
342 *
343 * @list: Queue linked list.
344 *
345 * @mqd: The queue MQD.
346 *
347 * @mqd_mem_obj: The MQD local gpu memory object.
348 *
349 * @gart_mqd_addr: The MQD gart mc address.
350 *
351 * @properties: The queue properties.
352 *
353 * @mec: Used only in no cp scheduling mode and identifies to micro engine id
Kent Russell8eabaf52017-08-15 23:00:04 -0400354 * that the queue should be execute on.
Ben Gozed8aab42014-07-17 00:18:51 +0300355 *
Kent Russell8eabaf52017-08-15 23:00:04 -0400356 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
357 * id.
Ben Gozed8aab42014-07-17 00:18:51 +0300358 *
359 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
360 *
361 * @process: The kfd process that created this queue.
362 *
363 * @device: The kfd device that created this queue.
364 *
365 * This structure represents user mode compute queues.
366 * It contains all the necessary data to handle such queues.
367 *
368 */
369
370struct queue {
371 struct list_head list;
372 void *mqd;
373 struct kfd_mem_obj *mqd_mem_obj;
374 uint64_t gart_mqd_addr;
375 struct queue_properties properties;
376
377 uint32_t mec;
378 uint32_t pipe;
379 uint32_t queue;
380
Ben Goz77669eb2015-01-03 22:12:31 +0200381 unsigned int sdma_id;
382
Ben Gozed8aab42014-07-17 00:18:51 +0300383 struct kfd_process *process;
384 struct kfd_dev *device;
385};
386
Ben Goz6e99df52014-07-17 00:36:17 +0300387/*
388 * Please read the kfd_mqd_manager.h description.
389 */
390enum KFD_MQD_TYPE {
Ben Goz85d258f2015-01-04 10:36:30 +0200391 KFD_MQD_TYPE_COMPUTE = 0, /* for no cp scheduling */
392 KFD_MQD_TYPE_HIQ, /* for hiq */
393 KFD_MQD_TYPE_CP, /* for cp queues and diq */
394 KFD_MQD_TYPE_SDMA, /* for sdma queues */
Ben Goz6e99df52014-07-17 00:36:17 +0300395 KFD_MQD_TYPE_MAX
396};
397
Ben Goz241f24f2014-07-17 00:55:28 +0300398struct scheduling_resources {
399 unsigned int vmid_mask;
400 enum kfd_queue_type type;
401 uint64_t queue_mask;
402 uint64_t gws_mask;
403 uint32_t oac_mask;
404 uint32_t gds_heap_base;
405 uint32_t gds_heap_size;
406};
407
408struct process_queue_manager {
409 /* data */
410 struct kfd_process *process;
411 unsigned int num_concurrent_processes;
412 struct list_head queues;
413 unsigned long *queue_slot_bitmap;
414};
415
416struct qcm_process_device {
417 /* The Device Queue Manager that owns this data */
418 struct device_queue_manager *dqm;
419 struct process_queue_manager *pqm;
Ben Goz241f24f2014-07-17 00:55:28 +0300420 /* Queues list */
421 struct list_head queues_list;
422 struct list_head priv_queue_list;
423
424 unsigned int queue_count;
425 unsigned int vmid;
426 bool is_debug;
427 /*
428 * All the memory management data should be here too
429 */
430 uint64_t gds_context_area;
431 uint32_t sh_mem_config;
432 uint32_t sh_mem_bases;
433 uint32_t sh_mem_ape1_base;
434 uint32_t sh_mem_ape1_limit;
435 uint32_t page_table_base;
436 uint32_t gds_size;
437 uint32_t num_gws;
438 uint32_t num_oac;
Moses Reuben6a1c9512017-08-15 23:00:20 -0400439 uint32_t sh_hidden_private_base;
Ben Goz241f24f2014-07-17 00:55:28 +0300440};
441
Yong Zhao733fa1f2017-09-20 18:10:14 -0400442
443enum kfd_pdd_bound {
444 PDD_UNBOUND = 0,
445 PDD_BOUND,
446 PDD_BOUND_SUSPENDED,
447};
448
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300449/* Data that is per-process-per device. */
450struct kfd_process_device {
451 /*
452 * List of all per-device data for a process.
453 * Starts from kfd_process.per_device_data.
454 */
455 struct list_head per_device_list;
456
457 /* The device that owns this data. */
458 struct kfd_dev *dev;
459
460
Ben Goz45102042014-07-17 01:04:10 +0300461 /* per-process-per device QCM data structure */
462 struct qcm_process_device qpd;
463
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300464 /*Apertures*/
465 uint64_t lds_base;
466 uint64_t lds_limit;
467 uint64_t gpuvm_base;
468 uint64_t gpuvm_limit;
469 uint64_t scratch_base;
470 uint64_t scratch_limit;
471
472 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
Yong Zhao733fa1f2017-09-20 18:10:14 -0400473 enum kfd_pdd_bound bound;
Ben Goza82918f2015-03-25 13:12:20 +0200474
475 /* This flag tells if we should reset all
476 * wavefronts on process termination
477 */
478 bool reset_wavefronts;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300479};
480
Alexey Skidanov52a5fdc2014-11-19 17:07:00 +0200481#define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
482
Oded Gabbay4a488a72014-07-16 21:08:55 +0300483/* Process data */
484struct kfd_process {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300485 /*
486 * kfd_process are stored in an mm_struct*->kfd_process*
487 * hash table (kfd_processes in kfd_process.c)
488 */
489 struct hlist_node kfd_processes;
490
491 struct mm_struct *mm;
492
493 struct mutex mutex;
494
495 /*
496 * In any process, the thread that started main() is the lead
497 * thread and outlives the rest.
498 * It is here because amd_iommu_bind_pasid wants a task_struct.
499 */
500 struct task_struct *lead_thread;
501
502 /* We want to receive a notification when the mm_struct is destroyed */
503 struct mmu_notifier mmu_notifier;
504
505 /* Use for delayed freeing of kfd_process structure */
506 struct rcu_head rcu;
507
508 unsigned int pasid;
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400509 unsigned int doorbell_index;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300510
511 /*
512 * List of kfd_process_device structures,
513 * one for each device the process is using.
514 */
515 struct list_head per_device_data;
516
Ben Goz45102042014-07-17 01:04:10 +0300517 struct process_queue_manager pqm;
518
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300519 /* The process's queues. */
520 size_t queue_array_size;
521
522 /* Size is queue_array_size, up to MAX_PROCESS_QUEUES. */
523 struct kfd_queue **queues;
524
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300525 /*Is the user space process 32 bit?*/
526 bool is_32bit_user_mode;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300527
528 /* Event-related data */
529 struct mutex event_mutex;
530 /* All events in process hashed by ID, linked on kfd_event.events. */
531 DECLARE_HASHTABLE(events, 4);
Kent Russell8eabaf52017-08-15 23:00:04 -0400532 /* struct slot_page_header.event_pages */
533 struct list_head signal_event_pages;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300534 u32 next_nonsignal_event_id;
535 size_t signal_event_count;
Felix Kuehlingc9861692017-09-20 18:10:22 -0400536 bool signal_event_limit_reached;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300537};
538
Oded Gabbay76baee62014-12-29 14:20:05 +0200539/**
540 * Ioctl function type.
541 *
542 * \param filep pointer to file structure.
543 * \param p amdkfd process pointer.
544 * \param data pointer to arg that was copied from user.
545 */
546typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
547 void *data);
548
549struct amdkfd_ioctl_desc {
550 unsigned int cmd;
551 int flags;
552 amdkfd_ioctl_t *func;
553 unsigned int cmd_drv;
554 const char *name;
555};
556
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300557void kfd_process_create_wq(void);
558void kfd_process_destroy_wq(void);
559struct kfd_process *kfd_create_process(const struct task_struct *);
560struct kfd_process *kfd_get_process(const struct task_struct *);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300561struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300562
Ben Goz64c7f8c2014-07-17 01:27:00 +0300563struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
Yong Zhao733fa1f2017-09-20 18:10:14 -0400564 struct kfd_process *p);
565int kfd_bind_processes_to_device(struct kfd_dev *dev);
566void kfd_unbind_processes_from_device(struct kfd_dev *dev);
567void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300568struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200569 struct kfd_process *p);
570struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
571 struct kfd_process *p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300572
Alexey Skidanov775921e2014-07-17 01:49:36 +0300573/* Process device data iterator */
Kent Russell8eabaf52017-08-15 23:00:04 -0400574struct kfd_process_device *kfd_get_first_process_device_data(
575 struct kfd_process *p);
576struct kfd_process_device *kfd_get_next_process_device_data(
577 struct kfd_process *p,
Alexey Skidanov775921e2014-07-17 01:49:36 +0300578 struct kfd_process_device *pdd);
579bool kfd_has_process_device_data(struct kfd_process *p);
580
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300581/* PASIDs */
582int kfd_pasid_init(void);
583void kfd_pasid_exit(void);
584bool kfd_set_pasid_limit(unsigned int new_limit);
585unsigned int kfd_get_pasid_limit(void);
586unsigned int kfd_pasid_alloc(void);
587void kfd_pasid_free(unsigned int pasid);
588
589/* Doorbells */
Felix Kuehling735df2b2017-08-15 23:00:10 -0400590int kfd_doorbell_init(struct kfd_dev *kfd);
591void kfd_doorbell_fini(struct kfd_dev *kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300592int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
593u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
594 unsigned int *doorbell_off);
595void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
596u32 read_kernel_doorbell(u32 __iomem *db);
597void write_kernel_doorbell(u32 __iomem *db, u32 value);
598unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
599 struct kfd_process *process,
600 unsigned int queue_id);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400601phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
602 struct kfd_process *process);
603int kfd_alloc_process_doorbells(struct kfd_process *process);
604void kfd_free_process_doorbells(struct kfd_process *process);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300605
Oded Gabbay6e810902014-10-27 14:36:07 +0200606/* GTT Sub-Allocator */
607
608int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
609 struct kfd_mem_obj **mem_obj);
610
611int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);
612
Oded Gabbay4a488a72014-07-16 21:08:55 +0300613extern struct device *kfd_device;
614
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300615/* Topology */
616int kfd_topology_init(void);
617void kfd_topology_shutdown(void);
618int kfd_topology_add_device(struct kfd_dev *gpu);
619int kfd_topology_remove_device(struct kfd_dev *gpu);
620struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
621struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
622struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx);
623
Oded Gabbay4a488a72014-07-16 21:08:55 +0300624/* Interrupts */
Andrew Lewycky2249d552014-07-17 01:37:30 +0300625int kfd_interrupt_init(struct kfd_dev *dev);
626void kfd_interrupt_exit(struct kfd_dev *dev);
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300627void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300628bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry);
629bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300630
631/* Power Management */
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300632void kgd2kfd_suspend(struct kfd_dev *kfd);
633int kgd2kfd_resume(struct kfd_dev *kfd);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300634
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300635/* amdkfd Apertures */
636int kfd_init_apertures(struct kfd_process *process);
637
Ben Gozed6e6a32014-07-17 00:45:35 +0300638/* Queue Context Management */
Ben Goz77669eb2015-01-03 22:12:31 +0200639struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd);
Ben Goz241f24f2014-07-17 00:55:28 +0300640
Edward O'Callaghane88a6142016-09-17 15:01:45 +1000641int init_queue(struct queue **q, const struct queue_properties *properties);
Ben Gozed6e6a32014-07-17 00:45:35 +0300642void uninit_queue(struct queue *q);
Ben Goz45102042014-07-17 01:04:10 +0300643void print_queue_properties(struct queue_properties *q);
Ben Gozed6e6a32014-07-17 00:45:35 +0300644void print_queue(struct queue *q);
645
Ben Goz64c7f8c2014-07-17 01:27:00 +0300646struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
647 struct kfd_dev *dev);
Ben Goz4b8f5892015-01-04 11:24:25 +0200648struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
649 struct kfd_dev *dev);
650struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
651 struct kfd_dev *dev);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300652struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
653void device_queue_manager_uninit(struct device_queue_manager *dqm);
Ben Goz241f24f2014-07-17 00:55:28 +0300654struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
655 enum kfd_queue_type type);
656void kernel_queue_uninit(struct kernel_queue *kq);
657
Ben Goz45102042014-07-17 01:04:10 +0300658/* Process Queue Manager */
659struct process_queue_node {
660 struct queue *q;
661 struct kernel_queue *kq;
662 struct list_head process_queue_list;
663};
664
665int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
666void pqm_uninit(struct process_queue_manager *pqm);
667int pqm_create_queue(struct process_queue_manager *pqm,
668 struct kfd_dev *dev,
669 struct file *f,
670 struct queue_properties *properties,
671 unsigned int flags,
672 enum kfd_queue_type type,
673 unsigned int *qid);
674int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
675int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
676 struct queue_properties *p);
Yair Shacharfbeb6612015-05-20 13:48:26 +0300677struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
678 unsigned int qid);
Ben Goz45102042014-07-17 01:04:10 +0300679
Yair Shachar788bf832015-05-20 13:58:12 +0300680int amdkfd_fence_wait_timeout(unsigned int *fence_addr,
681 unsigned int fence_value,
Yong Zhao8c72c3d2017-09-20 18:10:15 -0400682 unsigned int timeout_ms);
Yair Shachar788bf832015-05-20 13:58:12 +0300683
Ben Gozed6e6a32014-07-17 00:45:35 +0300684/* Packet Manager */
685
Ben Goz64c7f8c2014-07-17 01:27:00 +0300686#define KFD_FENCE_COMPLETED (100)
687#define KFD_FENCE_INIT (10)
Ben Goz241f24f2014-07-17 00:55:28 +0300688
Ben Gozed6e6a32014-07-17 00:45:35 +0300689struct packet_manager {
690 struct device_queue_manager *dqm;
691 struct kernel_queue *priv_queue;
692 struct mutex lock;
693 bool allocated;
694 struct kfd_mem_obj *ib_buffer_obj;
695};
696
Ben Goz64c7f8c2014-07-17 01:27:00 +0300697int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
698void pm_uninit(struct packet_manager *pm);
699int pm_send_set_resources(struct packet_manager *pm,
700 struct scheduling_resources *res);
701int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
702int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
703 uint32_t fence_value);
704
705int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
706 enum kfd_preempt_type_filter mode,
707 uint32_t filter_param, bool reset,
708 unsigned int sdma_engine);
709
Ben Goz241f24f2014-07-17 00:55:28 +0300710void pm_release_ib(struct packet_manager *pm);
711
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300712uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300713
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300714/* Events */
715extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
Alexey Skidanov930c5ff2014-11-25 10:34:31 +0200716extern const struct kfd_device_global_init_class device_global_init_class_cik;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300717
718enum kfd_event_wait_result {
719 KFD_WAIT_COMPLETE,
720 KFD_WAIT_TIMEOUT,
721 KFD_WAIT_ERROR
722};
723
724void kfd_event_init_process(struct kfd_process *p);
725void kfd_event_free_process(struct kfd_process *p);
726int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
727int kfd_wait_on_events(struct kfd_process *p,
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300728 uint32_t num_events, void __user *data,
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300729 bool all, uint32_t user_timeout_ms,
730 enum kfd_event_wait_result *wait_result);
731void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id,
732 uint32_t valid_id_bits);
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300733void kfd_signal_iommu_event(struct kfd_dev *dev,
734 unsigned int pasid, unsigned long address,
735 bool is_write_requested, bool is_execute_requested);
Alexey Skidanov930c5ff2014-11-25 10:34:31 +0200736void kfd_signal_hw_exception_event(unsigned int pasid);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300737int kfd_set_event(struct kfd_process *p, uint32_t event_id);
738int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
739int kfd_event_create(struct file *devkfd, struct kfd_process *p,
740 uint32_t event_type, bool auto_reset, uint32_t node_id,
741 uint32_t *event_id, uint32_t *event_trigger_data,
742 uint64_t *event_page_offset, uint32_t *event_slot_index);
743int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
744
Ben Gozc3447e82015-05-20 18:05:44 +0300745int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p);
746
Oded Gabbay4a488a72014-07-16 21:08:55 +0300747#endif