blob: 78b5d61780cbde3cc3d4b4c80501e848bae0c95a [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>
Felix Kuehling482f0772017-10-27 19:35:27 -040034#include <linux/idr.h>
Andres Rodriguez04ad47b2017-10-27 19:35:31 -040035#include <linux/kfifo.h>
Oded Gabbay4a488a72014-07-16 21:08:55 +030036#include <kgd_kfd_interface.h>
37
Yong Zhaoe596b902017-09-20 18:10:19 -040038#include "amd_shared.h"
39
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +030040#define KFD_SYSFS_FILE_MODE 0444
41
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030042#define KFD_MMAP_DOORBELL_MASK 0x8000000000000
43#define KFD_MMAP_EVENTS_MASK 0x4000000000000
44
Ben Gozed6e6a32014-07-17 00:45:35 +030045/*
46 * When working with cp scheduler we should assign the HIQ manually or via
47 * the radeon driver to a fixed hqd slot, here are the fixed HIQ hqd slot
48 * definitions for Kaveri. In Kaveri only the first ME queues participates
49 * in the cp scheduling taking that in mind we set the HIQ slot in the
50 * second ME.
51 */
52#define KFD_CIK_HIQ_PIPE 4
53#define KFD_CIK_HIQ_QUEUE 0
54
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +030055/* GPU ID hash width in bits */
56#define KFD_GPU_ID_HASH_WIDTH 16
57
58/* Macro for allocating structures */
59#define kfd_alloc_struct(ptr_to_struct) \
60 ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
61
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030062#define KFD_MAX_NUM_OF_PROCESSES 512
Oded Gabbayb8cbab02015-01-18 13:18:01 +020063#define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030064
65/*
Oded Gabbayb8cbab02015-01-18 13:18:01 +020066 * Kernel module parameter to specify maximum number of supported queues per
67 * device
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030068 */
Oded Gabbayb8cbab02015-01-18 13:18:01 +020069extern int max_num_of_queues_per_device;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030070
Oded Gabbayb8cbab02015-01-18 13:18:01 +020071#define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT 4096
72#define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \
73 (KFD_MAX_NUM_OF_PROCESSES * \
74 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030075
Ben Gozed6e6a32014-07-17 00:45:35 +030076#define KFD_KERNEL_QUEUE_SIZE 2048
77
Ben Goz31c21fe2014-07-17 00:48:28 +030078/* Kernel module parameter to specify the scheduling policy */
79extern int sched_policy;
80
Oded Gabbay81663012014-12-24 13:30:52 +020081/*
82 * Kernel module parameter to specify whether to send sigterm to HSA process on
83 * unhandled exception
84 */
85extern int send_sigterm;
86
Ben Goz31c21fe2014-07-17 00:48:28 +030087/**
88 * enum kfd_sched_policy
89 *
90 * @KFD_SCHED_POLICY_HWS: H/W scheduling policy known as command processor (cp)
91 * scheduling. In this scheduling mode we're using the firmware code to
92 * schedule the user mode queues and kernel queues such as HIQ and DIQ.
93 * the HIQ queue is used as a special queue that dispatches the configuration
94 * to the cp and the user mode queues list that are currently running.
95 * the DIQ queue is a debugging queue that dispatches debugging commands to the
96 * firmware.
97 * in this scheduling mode user mode queues over subscription feature is
98 * enabled.
99 *
100 * @KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: The same as above but the over
101 * subscription feature disabled.
102 *
103 * @KFD_SCHED_POLICY_NO_HWS: no H/W scheduling policy is a mode which directly
104 * set the command processor registers and sets the queues "manually". This
105 * mode is used *ONLY* for debugging proposes.
106 *
107 */
108enum kfd_sched_policy {
109 KFD_SCHED_POLICY_HWS = 0,
110 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION,
111 KFD_SCHED_POLICY_NO_HWS
112};
113
Ben Gozed6e6a32014-07-17 00:45:35 +0300114enum cache_policy {
115 cache_policy_coherent,
116 cache_policy_noncoherent
117};
118
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300119struct kfd_event_interrupt_class {
120 bool (*interrupt_isr)(struct kfd_dev *dev,
121 const uint32_t *ih_ring_entry);
122 void (*interrupt_wq)(struct kfd_dev *dev,
123 const uint32_t *ih_ring_entry);
124};
125
Oded Gabbay4a488a72014-07-16 21:08:55 +0300126struct kfd_device_info {
Yong Zhaoe596b902017-09-20 18:10:19 -0400127 enum amd_asic_type asic_family;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300128 const struct kfd_event_interrupt_class *event_interrupt_class;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300129 unsigned int max_pasid_bits;
Yair Shachar992839a2015-05-20 13:43:04 +0300130 unsigned int max_no_of_hqd;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300131 size_t ih_ring_entry_size;
Alexey Skidanovf7c826a2014-10-13 16:35:12 +0300132 uint8_t num_of_watch_points;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300133 uint16_t mqd_size_aligned;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300134};
135
Oded Gabbay36b5c082014-10-26 09:53:10 +0200136struct kfd_mem_obj {
137 uint32_t range_start;
138 uint32_t range_end;
139 uint64_t gpu_addr;
140 uint32_t *cpu_ptr;
141};
142
Yong Zhao44008d72017-09-20 18:10:18 -0400143struct kfd_vmid_info {
144 uint32_t first_vmid_kfd;
145 uint32_t last_vmid_kfd;
146 uint32_t vmid_num_kfd;
147};
148
Oded Gabbay4a488a72014-07-16 21:08:55 +0300149struct kfd_dev {
150 struct kgd_dev *kgd;
151
152 const struct kfd_device_info *device_info;
153 struct pci_dev *pdev;
154
155 unsigned int id; /* topology stub index */
156
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300157 phys_addr_t doorbell_base; /* Start of actual doorbells used by
158 * KFD. It is aligned for mapping
159 * into user mode
160 */
161 size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell
162 * to HW doorbell, GFX reserved some
163 * at the start)
164 */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300165 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
166 * page used by kernel queue
167 */
168
Oded Gabbay4a488a72014-07-16 21:08:55 +0300169 struct kgd2kfd_shared_resources shared_resources;
Yong Zhao44008d72017-09-20 18:10:18 -0400170 struct kfd_vmid_info vm_info;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300171
Xihan Zhangcea405b2015-03-17 19:32:53 +0800172 const struct kfd2kgd_calls *kfd2kgd;
173 struct mutex doorbell_mutex;
Joe Perchesf761d8b2015-05-19 18:37:51 -0700174 DECLARE_BITMAP(doorbell_available_index,
175 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800176
Oded Gabbay36b5c082014-10-26 09:53:10 +0200177 void *gtt_mem;
178 uint64_t gtt_start_gpu_addr;
179 void *gtt_start_cpu_ptr;
180 void *gtt_sa_bitmap;
181 struct mutex gtt_sa_lock;
182 unsigned int gtt_sa_chunk_size;
183 unsigned int gtt_sa_num_of_chunks;
184
Andrew Lewycky2249d552014-07-17 01:37:30 +0300185 /* Interrupts */
Andres Rodriguez04ad47b2017-10-27 19:35:31 -0400186 struct kfifo ih_fifo;
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400187 struct workqueue_struct *ih_wq;
Andrew Lewycky2249d552014-07-17 01:37:30 +0300188 struct work_struct interrupt_work;
189 spinlock_t interrupt_lock;
190
Ben Gozed6e6a32014-07-17 00:45:35 +0300191 /* QCM Device instance */
192 struct device_queue_manager *dqm;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300193
Ben Gozed6e6a32014-07-17 00:45:35 +0300194 bool init_complete;
Andrew Lewycky2249d552014-07-17 01:37:30 +0300195 /*
196 * Interrupts of interest to KFD are copied
197 * from the HW ring into a SW ring.
198 */
199 bool interrupts_active;
Yair Shacharfbeb6612015-05-20 13:48:26 +0300200
201 /* Debug manager */
202 struct kfd_dbgmgr *dbgmgr;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300203};
204
205/* KGD2KFD callbacks */
206void kgd2kfd_exit(void);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800207struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
208 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300209bool kgd2kfd_device_init(struct kfd_dev *kfd,
Xihan Zhangcea405b2015-03-17 19:32:53 +0800210 const struct kgd2kfd_shared_resources *gpu_resources);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300211void kgd2kfd_device_exit(struct kfd_dev *kfd);
212
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300213enum kfd_mempool {
214 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
215 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
216 KFD_MEMPOOL_FRAMEBUFFER = 3,
217};
218
Oded Gabbay4a488a72014-07-16 21:08:55 +0300219/* Character device interface */
220int kfd_chardev_init(void);
221void kfd_chardev_exit(void);
222struct device *kfd_chardev(void);
223
Ben Goz241f24f2014-07-17 00:55:28 +0300224/**
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400225 * enum kfd_unmap_queues_filter
Ben Goz241f24f2014-07-17 00:55:28 +0300226 *
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400227 * @KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE: Preempts single queue.
Ben Goz241f24f2014-07-17 00:55:28 +0300228 *
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400229 * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
Ben Goz241f24f2014-07-17 00:55:28 +0300230 * running queues list.
231 *
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400232 * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
Ben Goz241f24f2014-07-17 00:55:28 +0300233 * specific process.
234 *
235 */
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400236enum kfd_unmap_queues_filter {
237 KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE,
238 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES,
239 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
240 KFD_UNMAP_QUEUES_FILTER_BY_PASID
Ben Goz241f24f2014-07-17 00:55:28 +0300241};
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300242
Ben Gozed8aab42014-07-17 00:18:51 +0300243/**
244 * enum kfd_queue_type
245 *
246 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
247 *
248 * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type.
249 *
250 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
251 *
252 * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
253 */
254enum kfd_queue_type {
255 KFD_QUEUE_TYPE_COMPUTE,
256 KFD_QUEUE_TYPE_SDMA,
257 KFD_QUEUE_TYPE_HIQ,
258 KFD_QUEUE_TYPE_DIQ
259};
260
Ben Goz6e99df52014-07-17 00:36:17 +0300261enum kfd_queue_format {
262 KFD_QUEUE_FORMAT_PM4,
263 KFD_QUEUE_FORMAT_AQL
264};
265
Ben Gozed8aab42014-07-17 00:18:51 +0300266/**
267 * struct queue_properties
268 *
269 * @type: The queue type.
270 *
271 * @queue_id: Queue identifier.
272 *
273 * @queue_address: Queue ring buffer address.
274 *
275 * @queue_size: Queue ring buffer size.
276 *
277 * @priority: Defines the queue priority relative to other queues in the
278 * process.
279 * This is just an indication and HW scheduling may override the priority as
280 * necessary while keeping the relative prioritization.
281 * the priority granularity is from 0 to f which f is the highest priority.
282 * currently all queues are initialized with the highest priority.
283 *
284 * @queue_percent: This field is partially implemented and currently a zero in
285 * this field defines that the queue is non active.
286 *
287 * @read_ptr: User space address which points to the number of dwords the
288 * cp read from the ring buffer. This field updates automatically by the H/W.
289 *
290 * @write_ptr: Defines the number of dwords written to the ring buffer.
291 *
292 * @doorbell_ptr: This field aim is to notify the H/W of new packet written to
Kent Russell8eabaf52017-08-15 23:00:04 -0400293 * the queue ring buffer. This field should be similar to write_ptr and the
294 * user should update this field after he updated the write_ptr.
Ben Gozed8aab42014-07-17 00:18:51 +0300295 *
296 * @doorbell_off: The doorbell offset in the doorbell pci-bar.
297 *
Kent Russell8eabaf52017-08-15 23:00:04 -0400298 * @is_interop: Defines if this is a interop queue. Interop queue means that
299 * the queue can access both graphics and compute resources.
Ben Gozed8aab42014-07-17 00:18:51 +0300300 *
301 * @is_active: Defines if the queue is active or not.
302 *
303 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
304 * of the queue.
305 *
306 * This structure represents the queue properties for each queue no matter if
307 * it's user mode or kernel mode queue.
308 *
309 */
310struct queue_properties {
311 enum kfd_queue_type type;
Ben Goz6e99df52014-07-17 00:36:17 +0300312 enum kfd_queue_format format;
Ben Gozed8aab42014-07-17 00:18:51 +0300313 unsigned int queue_id;
314 uint64_t queue_address;
315 uint64_t queue_size;
316 uint32_t priority;
317 uint32_t queue_percent;
318 uint32_t *read_ptr;
319 uint32_t *write_ptr;
Oded Gabbay5cd78de2014-11-20 16:14:56 +0200320 uint32_t __iomem *doorbell_ptr;
Ben Gozed8aab42014-07-17 00:18:51 +0300321 uint32_t doorbell_off;
322 bool is_interop;
323 bool is_active;
324 /* Not relevant for user mode queues in cp scheduling */
325 unsigned int vmid;
Ben Goz77669eb2015-01-03 22:12:31 +0200326 /* Relevant only for sdma queues*/
327 uint32_t sdma_engine_id;
328 uint32_t sdma_queue_id;
329 uint32_t sdma_vm_addr;
Ben Gozff3d04a2015-01-04 10:37:18 +0200330 /* Relevant only for VI */
331 uint64_t eop_ring_buffer_address;
332 uint32_t eop_ring_buffer_size;
333 uint64_t ctx_save_restore_area_address;
334 uint32_t ctx_save_restore_area_size;
Ben Gozed8aab42014-07-17 00:18:51 +0300335};
336
337/**
338 * struct queue
339 *
340 * @list: Queue linked list.
341 *
342 * @mqd: The queue MQD.
343 *
344 * @mqd_mem_obj: The MQD local gpu memory object.
345 *
346 * @gart_mqd_addr: The MQD gart mc address.
347 *
348 * @properties: The queue properties.
349 *
350 * @mec: Used only in no cp scheduling mode and identifies to micro engine id
Kent Russell8eabaf52017-08-15 23:00:04 -0400351 * that the queue should be execute on.
Ben Gozed8aab42014-07-17 00:18:51 +0300352 *
Kent Russell8eabaf52017-08-15 23:00:04 -0400353 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
354 * id.
Ben Gozed8aab42014-07-17 00:18:51 +0300355 *
356 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
357 *
358 * @process: The kfd process that created this queue.
359 *
360 * @device: The kfd device that created this queue.
361 *
362 * This structure represents user mode compute queues.
363 * It contains all the necessary data to handle such queues.
364 *
365 */
366
367struct queue {
368 struct list_head list;
369 void *mqd;
370 struct kfd_mem_obj *mqd_mem_obj;
371 uint64_t gart_mqd_addr;
372 struct queue_properties properties;
373
374 uint32_t mec;
375 uint32_t pipe;
376 uint32_t queue;
377
Ben Goz77669eb2015-01-03 22:12:31 +0200378 unsigned int sdma_id;
379
Ben Gozed8aab42014-07-17 00:18:51 +0300380 struct kfd_process *process;
381 struct kfd_dev *device;
382};
383
Ben Goz6e99df52014-07-17 00:36:17 +0300384/*
385 * Please read the kfd_mqd_manager.h description.
386 */
387enum KFD_MQD_TYPE {
Ben Goz85d258f2015-01-04 10:36:30 +0200388 KFD_MQD_TYPE_COMPUTE = 0, /* for no cp scheduling */
389 KFD_MQD_TYPE_HIQ, /* for hiq */
390 KFD_MQD_TYPE_CP, /* for cp queues and diq */
391 KFD_MQD_TYPE_SDMA, /* for sdma queues */
Ben Goz6e99df52014-07-17 00:36:17 +0300392 KFD_MQD_TYPE_MAX
393};
394
Ben Goz241f24f2014-07-17 00:55:28 +0300395struct scheduling_resources {
396 unsigned int vmid_mask;
397 enum kfd_queue_type type;
398 uint64_t queue_mask;
399 uint64_t gws_mask;
400 uint32_t oac_mask;
401 uint32_t gds_heap_base;
402 uint32_t gds_heap_size;
403};
404
405struct process_queue_manager {
406 /* data */
407 struct kfd_process *process;
Ben Goz241f24f2014-07-17 00:55:28 +0300408 struct list_head queues;
409 unsigned long *queue_slot_bitmap;
410};
411
412struct qcm_process_device {
413 /* The Device Queue Manager that owns this data */
414 struct device_queue_manager *dqm;
415 struct process_queue_manager *pqm;
Ben Goz241f24f2014-07-17 00:55:28 +0300416 /* Queues list */
417 struct list_head queues_list;
418 struct list_head priv_queue_list;
419
420 unsigned int queue_count;
421 unsigned int vmid;
422 bool is_debug;
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400423
424 /* This flag tells if we should reset all wavefronts on
425 * process termination
426 */
427 bool reset_wavefronts;
428
Ben Goz241f24f2014-07-17 00:55:28 +0300429 /*
430 * All the memory management data should be here too
431 */
432 uint64_t gds_context_area;
433 uint32_t sh_mem_config;
434 uint32_t sh_mem_bases;
435 uint32_t sh_mem_ape1_base;
436 uint32_t sh_mem_ape1_limit;
437 uint32_t page_table_base;
438 uint32_t gds_size;
439 uint32_t num_gws;
440 uint32_t num_oac;
Moses Reuben6a1c9512017-08-15 23:00:20 -0400441 uint32_t sh_hidden_private_base;
Ben Goz241f24f2014-07-17 00:55:28 +0300442};
443
Yong Zhao733fa1f2017-09-20 18:10:14 -0400444
445enum kfd_pdd_bound {
446 PDD_UNBOUND = 0,
447 PDD_BOUND,
448 PDD_BOUND_SUSPENDED,
449};
450
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300451/* Data that is per-process-per device. */
452struct kfd_process_device {
453 /*
454 * List of all per-device data for a process.
455 * Starts from kfd_process.per_device_data.
456 */
457 struct list_head per_device_list;
458
459 /* The device that owns this data. */
460 struct kfd_dev *dev;
461
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400462 /* The process that owns this kfd_process_device. */
463 struct kfd_process *process;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300464
Ben Goz45102042014-07-17 01:04:10 +0300465 /* per-process-per device QCM data structure */
466 struct qcm_process_device qpd;
467
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300468 /*Apertures*/
469 uint64_t lds_base;
470 uint64_t lds_limit;
471 uint64_t gpuvm_base;
472 uint64_t gpuvm_limit;
473 uint64_t scratch_base;
474 uint64_t scratch_limit;
475
476 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
Yong Zhao733fa1f2017-09-20 18:10:14 -0400477 enum kfd_pdd_bound bound;
Ben Goza82918f2015-03-25 13:12:20 +0200478
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400479 /* Flag used to tell the pdd has dequeued from the dqm.
480 * This is used to prevent dev->dqm->ops.process_termination() from
481 * being called twice when it is already called in IOMMU callback
482 * function.
Ben Goza82918f2015-03-25 13:12:20 +0200483 */
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400484 bool already_dequeued;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300485};
486
Alexey Skidanov52a5fdc2014-11-19 17:07:00 +0200487#define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
488
Oded Gabbay4a488a72014-07-16 21:08:55 +0300489/* Process data */
490struct kfd_process {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300491 /*
492 * kfd_process are stored in an mm_struct*->kfd_process*
493 * hash table (kfd_processes in kfd_process.c)
494 */
495 struct hlist_node kfd_processes;
496
Felix Kuehling9b56bb12017-10-27 19:35:19 -0400497 /*
498 * Opaque pointer to mm_struct. We don't hold a reference to
499 * it so it should never be dereferenced from here. This is
500 * only used for looking up processes by their mm.
501 */
502 void *mm;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300503
504 struct mutex mutex;
505
506 /*
507 * In any process, the thread that started main() is the lead
508 * thread and outlives the rest.
509 * It is here because amd_iommu_bind_pasid wants a task_struct.
510 */
511 struct task_struct *lead_thread;
512
513 /* We want to receive a notification when the mm_struct is destroyed */
514 struct mmu_notifier mmu_notifier;
515
516 /* Use for delayed freeing of kfd_process structure */
517 struct rcu_head rcu;
518
519 unsigned int pasid;
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400520 unsigned int doorbell_index;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300521
522 /*
523 * List of kfd_process_device structures,
524 * one for each device the process is using.
525 */
526 struct list_head per_device_data;
527
Ben Goz45102042014-07-17 01:04:10 +0300528 struct process_queue_manager pqm;
529
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300530 /*Is the user space process 32 bit?*/
531 bool is_32bit_user_mode;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300532
533 /* Event-related data */
534 struct mutex event_mutex;
Felix Kuehling482f0772017-10-27 19:35:27 -0400535 /* Event ID allocator and lookup */
536 struct idr event_idr;
Felix Kuehling50cb7dd2017-10-27 19:35:26 -0400537 /* Event page */
538 struct kfd_signal_page *signal_page;
Felix Kuehlingb9a5d0a2017-10-27 19:35:29 -0400539 size_t signal_mapped_size;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300540 size_t signal_event_count;
Felix Kuehlingc9861692017-09-20 18:10:22 -0400541 bool signal_event_limit_reached;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300542};
543
Oded Gabbay76baee62014-12-29 14:20:05 +0200544/**
545 * Ioctl function type.
546 *
547 * \param filep pointer to file structure.
548 * \param p amdkfd process pointer.
549 * \param data pointer to arg that was copied from user.
550 */
551typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
552 void *data);
553
554struct amdkfd_ioctl_desc {
555 unsigned int cmd;
556 int flags;
557 amdkfd_ioctl_t *func;
558 unsigned int cmd_drv;
559 const char *name;
560};
561
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300562void kfd_process_create_wq(void);
563void kfd_process_destroy_wq(void);
564struct kfd_process *kfd_create_process(const struct task_struct *);
565struct kfd_process *kfd_get_process(const struct task_struct *);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300566struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300567
Ben Goz64c7f8c2014-07-17 01:27:00 +0300568struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
Yong Zhao733fa1f2017-09-20 18:10:14 -0400569 struct kfd_process *p);
570int kfd_bind_processes_to_device(struct kfd_dev *dev);
571void kfd_unbind_processes_from_device(struct kfd_dev *dev);
572void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300573struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200574 struct kfd_process *p);
575struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
576 struct kfd_process *p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300577
Alexey Skidanov775921e2014-07-17 01:49:36 +0300578/* Process device data iterator */
Kent Russell8eabaf52017-08-15 23:00:04 -0400579struct kfd_process_device *kfd_get_first_process_device_data(
580 struct kfd_process *p);
581struct kfd_process_device *kfd_get_next_process_device_data(
582 struct kfd_process *p,
Alexey Skidanov775921e2014-07-17 01:49:36 +0300583 struct kfd_process_device *pdd);
584bool kfd_has_process_device_data(struct kfd_process *p);
585
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300586/* PASIDs */
587int kfd_pasid_init(void);
588void kfd_pasid_exit(void);
589bool kfd_set_pasid_limit(unsigned int new_limit);
590unsigned int kfd_get_pasid_limit(void);
591unsigned int kfd_pasid_alloc(void);
592void kfd_pasid_free(unsigned int pasid);
593
594/* Doorbells */
Felix Kuehling735df2b2017-08-15 23:00:10 -0400595int kfd_doorbell_init(struct kfd_dev *kfd);
596void kfd_doorbell_fini(struct kfd_dev *kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300597int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
598u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
599 unsigned int *doorbell_off);
600void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
601u32 read_kernel_doorbell(u32 __iomem *db);
602void write_kernel_doorbell(u32 __iomem *db, u32 value);
603unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
604 struct kfd_process *process,
605 unsigned int queue_id);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400606phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
607 struct kfd_process *process);
608int kfd_alloc_process_doorbells(struct kfd_process *process);
609void kfd_free_process_doorbells(struct kfd_process *process);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300610
Oded Gabbay6e810902014-10-27 14:36:07 +0200611/* GTT Sub-Allocator */
612
613int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
614 struct kfd_mem_obj **mem_obj);
615
616int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);
617
Oded Gabbay4a488a72014-07-16 21:08:55 +0300618extern struct device *kfd_device;
619
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300620/* Topology */
621int kfd_topology_init(void);
622void kfd_topology_shutdown(void);
623int kfd_topology_add_device(struct kfd_dev *gpu);
624int kfd_topology_remove_device(struct kfd_dev *gpu);
625struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
626struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
627struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx);
628
Oded Gabbay4a488a72014-07-16 21:08:55 +0300629/* Interrupts */
Andrew Lewycky2249d552014-07-17 01:37:30 +0300630int kfd_interrupt_init(struct kfd_dev *dev);
631void kfd_interrupt_exit(struct kfd_dev *dev);
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300632void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300633bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry);
634bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300635
636/* Power Management */
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300637void kgd2kfd_suspend(struct kfd_dev *kfd);
638int kgd2kfd_resume(struct kfd_dev *kfd);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300639
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300640/* amdkfd Apertures */
641int kfd_init_apertures(struct kfd_process *process);
642
Ben Gozed6e6a32014-07-17 00:45:35 +0300643/* Queue Context Management */
Ben Goz77669eb2015-01-03 22:12:31 +0200644struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd);
Ben Goz241f24f2014-07-17 00:55:28 +0300645
Edward O'Callaghane88a6142016-09-17 15:01:45 +1000646int init_queue(struct queue **q, const struct queue_properties *properties);
Ben Gozed6e6a32014-07-17 00:45:35 +0300647void uninit_queue(struct queue *q);
Ben Goz45102042014-07-17 01:04:10 +0300648void print_queue_properties(struct queue_properties *q);
Ben Gozed6e6a32014-07-17 00:45:35 +0300649void print_queue(struct queue *q);
650
Ben Goz64c7f8c2014-07-17 01:27:00 +0300651struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
652 struct kfd_dev *dev);
Ben Goz4b8f5892015-01-04 11:24:25 +0200653struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
654 struct kfd_dev *dev);
655struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
656 struct kfd_dev *dev);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300657struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
658void device_queue_manager_uninit(struct device_queue_manager *dqm);
Ben Goz241f24f2014-07-17 00:55:28 +0300659struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
660 enum kfd_queue_type type);
661void kernel_queue_uninit(struct kernel_queue *kq);
662
Ben Goz45102042014-07-17 01:04:10 +0300663/* Process Queue Manager */
664struct process_queue_node {
665 struct queue *q;
666 struct kernel_queue *kq;
667 struct list_head process_queue_list;
668};
669
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400670void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
671void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
Ben Goz45102042014-07-17 01:04:10 +0300672int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
673void pqm_uninit(struct process_queue_manager *pqm);
674int pqm_create_queue(struct process_queue_manager *pqm,
675 struct kfd_dev *dev,
676 struct file *f,
677 struct queue_properties *properties,
Ben Goz45102042014-07-17 01:04:10 +0300678 unsigned int *qid);
679int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
680int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
681 struct queue_properties *p);
Yair Shacharfbeb6612015-05-20 13:48:26 +0300682struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
683 unsigned int qid);
Ben Goz45102042014-07-17 01:04:10 +0300684
Yair Shachar788bf832015-05-20 13:58:12 +0300685int amdkfd_fence_wait_timeout(unsigned int *fence_addr,
686 unsigned int fence_value,
Yong Zhao8c72c3d2017-09-20 18:10:15 -0400687 unsigned int timeout_ms);
Yair Shachar788bf832015-05-20 13:58:12 +0300688
Ben Gozed6e6a32014-07-17 00:45:35 +0300689/* Packet Manager */
690
Ben Goz64c7f8c2014-07-17 01:27:00 +0300691#define KFD_FENCE_COMPLETED (100)
692#define KFD_FENCE_INIT (10)
Ben Goz241f24f2014-07-17 00:55:28 +0300693
Ben Gozed6e6a32014-07-17 00:45:35 +0300694struct packet_manager {
695 struct device_queue_manager *dqm;
696 struct kernel_queue *priv_queue;
697 struct mutex lock;
698 bool allocated;
699 struct kfd_mem_obj *ib_buffer_obj;
700};
701
Ben Goz64c7f8c2014-07-17 01:27:00 +0300702int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
703void pm_uninit(struct packet_manager *pm);
704int pm_send_set_resources(struct packet_manager *pm,
705 struct scheduling_resources *res);
706int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
707int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
708 uint32_t fence_value);
709
710int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400711 enum kfd_unmap_queues_filter mode,
Ben Goz64c7f8c2014-07-17 01:27:00 +0300712 uint32_t filter_param, bool reset,
713 unsigned int sdma_engine);
714
Ben Goz241f24f2014-07-17 00:55:28 +0300715void pm_release_ib(struct packet_manager *pm);
716
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300717uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300718
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300719/* Events */
720extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
Alexey Skidanov930c5ff2014-11-25 10:34:31 +0200721extern const struct kfd_device_global_init_class device_global_init_class_cik;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300722
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300723void kfd_event_init_process(struct kfd_process *p);
724void kfd_event_free_process(struct kfd_process *p);
725int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
726int kfd_wait_on_events(struct kfd_process *p,
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300727 uint32_t num_events, void __user *data,
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300728 bool all, uint32_t user_timeout_ms,
Felix Kuehlingfdf0c832017-10-27 19:35:22 -0400729 uint32_t *wait_result);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300730void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id,
731 uint32_t valid_id_bits);
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300732void kfd_signal_iommu_event(struct kfd_dev *dev,
733 unsigned int pasid, unsigned long address,
734 bool is_write_requested, bool is_execute_requested);
Alexey Skidanov930c5ff2014-11-25 10:34:31 +0200735void kfd_signal_hw_exception_event(unsigned int pasid);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300736int kfd_set_event(struct kfd_process *p, uint32_t event_id);
737int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
738int kfd_event_create(struct file *devkfd, struct kfd_process *p,
739 uint32_t event_type, bool auto_reset, uint32_t node_id,
740 uint32_t *event_id, uint32_t *event_trigger_data,
741 uint64_t *event_page_offset, uint32_t *event_slot_index);
742int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
743
Ben Gozc3447e82015-05-20 18:05:44 +0300744int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p);
745
Oded Gabbay4a488a72014-07-16 21:08:55 +0300746#endif