blob: 594f8535539711d831f5949e99a4e650f283375b [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>
Felix Kuehling851a6452017-11-27 18:29:49 -050036#include <linux/seq_file.h>
Felix Kuehling5ce10682017-11-27 18:29:51 -050037#include <linux/kref.h>
Oded Gabbay4a488a72014-07-16 21:08:55 +030038#include <kgd_kfd_interface.h>
39
Yong Zhaoe596b902017-09-20 18:10:19 -040040#include "amd_shared.h"
41
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +030042#define KFD_SYSFS_FILE_MODE 0444
43
Oded Gabbaya1235e12018-01-10 12:55:17 +020044#define KFD_MMAP_DOORBELL_MASK 0x8000000000000ull
45#define KFD_MMAP_EVENTS_MASK 0x4000000000000ull
46#define KFD_MMAP_RESERVED_MEM_MASK 0x2000000000000ull
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030047
Ben Gozed6e6a32014-07-17 00:45:35 +030048/*
49 * When working with cp scheduler we should assign the HIQ manually or via
50 * the radeon driver to a fixed hqd slot, here are the fixed HIQ hqd slot
51 * definitions for Kaveri. In Kaveri only the first ME queues participates
52 * in the cp scheduling taking that in mind we set the HIQ slot in the
53 * second ME.
54 */
55#define KFD_CIK_HIQ_PIPE 4
56#define KFD_CIK_HIQ_QUEUE 0
57
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +030058/* GPU ID hash width in bits */
59#define KFD_GPU_ID_HASH_WIDTH 16
60
61/* Macro for allocating structures */
62#define kfd_alloc_struct(ptr_to_struct) \
63 ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
64
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030065#define KFD_MAX_NUM_OF_PROCESSES 512
Oded Gabbayb8cbab02015-01-18 13:18:01 +020066#define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030067
68/*
Felix Kuehling373d7082017-11-14 16:41:19 -050069 * Size of the per-process TBA+TMA buffer: 2 pages
70 *
71 * The first page is the TBA used for the CWSR ISA code. The second
72 * page is used as TMA for daisy changing a user-mode trap handler.
73 */
74#define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2)
75#define KFD_CWSR_TMA_OFFSET PAGE_SIZE
76
77/*
Oded Gabbayb8cbab02015-01-18 13:18:01 +020078 * Kernel module parameter to specify maximum number of supported queues per
79 * device
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030080 */
Oded Gabbayb8cbab02015-01-18 13:18:01 +020081extern int max_num_of_queues_per_device;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030082
Oded Gabbayb8cbab02015-01-18 13:18:01 +020083#define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT 4096
84#define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \
85 (KFD_MAX_NUM_OF_PROCESSES * \
86 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030087
Ben Gozed6e6a32014-07-17 00:45:35 +030088#define KFD_KERNEL_QUEUE_SIZE 2048
89
Ben Goz31c21fe2014-07-17 00:48:28 +030090/* Kernel module parameter to specify the scheduling policy */
91extern int sched_policy;
92
Felix Kuehlinga99c6d42017-11-27 18:29:45 -050093/*
94 * Kernel module parameter to specify the maximum process
95 * number per HW scheduler
96 */
97extern int hws_max_conc_proc;
98
Felix Kuehling373d7082017-11-14 16:41:19 -050099extern int cwsr_enable;
100
Oded Gabbay81663012014-12-24 13:30:52 +0200101/*
102 * Kernel module parameter to specify whether to send sigterm to HSA process on
103 * unhandled exception
104 */
105extern int send_sigterm;
106
Felix Kuehlingebcfd1e2017-12-08 23:09:03 -0500107/*
108 * Ignore CRAT table during KFD initialization, can be used to work around
109 * broken CRAT tables on some AMD systems
110 */
111extern int ignore_crat;
112
Ben Goz31c21fe2014-07-17 00:48:28 +0300113/**
114 * enum kfd_sched_policy
115 *
116 * @KFD_SCHED_POLICY_HWS: H/W scheduling policy known as command processor (cp)
117 * scheduling. In this scheduling mode we're using the firmware code to
118 * schedule the user mode queues and kernel queues such as HIQ and DIQ.
119 * the HIQ queue is used as a special queue that dispatches the configuration
120 * to the cp and the user mode queues list that are currently running.
121 * the DIQ queue is a debugging queue that dispatches debugging commands to the
122 * firmware.
123 * in this scheduling mode user mode queues over subscription feature is
124 * enabled.
125 *
126 * @KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: The same as above but the over
127 * subscription feature disabled.
128 *
129 * @KFD_SCHED_POLICY_NO_HWS: no H/W scheduling policy is a mode which directly
130 * set the command processor registers and sets the queues "manually". This
131 * mode is used *ONLY* for debugging proposes.
132 *
133 */
134enum kfd_sched_policy {
135 KFD_SCHED_POLICY_HWS = 0,
136 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION,
137 KFD_SCHED_POLICY_NO_HWS
138};
139
Ben Gozed6e6a32014-07-17 00:45:35 +0300140enum cache_policy {
141 cache_policy_coherent,
142 cache_policy_noncoherent
143};
144
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300145struct kfd_event_interrupt_class {
146 bool (*interrupt_isr)(struct kfd_dev *dev,
147 const uint32_t *ih_ring_entry);
148 void (*interrupt_wq)(struct kfd_dev *dev,
149 const uint32_t *ih_ring_entry);
150};
151
Oded Gabbay4a488a72014-07-16 21:08:55 +0300152struct kfd_device_info {
Yong Zhaoe596b902017-09-20 18:10:19 -0400153 enum amd_asic_type asic_family;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300154 const struct kfd_event_interrupt_class *event_interrupt_class;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300155 unsigned int max_pasid_bits;
Yair Shachar992839a2015-05-20 13:43:04 +0300156 unsigned int max_no_of_hqd;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300157 size_t ih_ring_entry_size;
Alexey Skidanovf7c826a2014-10-13 16:35:12 +0300158 uint8_t num_of_watch_points;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300159 uint16_t mqd_size_aligned;
Felix Kuehling373d7082017-11-14 16:41:19 -0500160 bool supports_cwsr;
Felix Kuehling3ee2d002018-01-04 17:17:41 -0500161 bool needs_pci_atomics;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300162};
163
Oded Gabbay36b5c082014-10-26 09:53:10 +0200164struct kfd_mem_obj {
165 uint32_t range_start;
166 uint32_t range_end;
167 uint64_t gpu_addr;
168 uint32_t *cpu_ptr;
169};
170
Yong Zhao44008d72017-09-20 18:10:18 -0400171struct kfd_vmid_info {
172 uint32_t first_vmid_kfd;
173 uint32_t last_vmid_kfd;
174 uint32_t vmid_num_kfd;
175};
176
Oded Gabbay4a488a72014-07-16 21:08:55 +0300177struct kfd_dev {
178 struct kgd_dev *kgd;
179
180 const struct kfd_device_info *device_info;
181 struct pci_dev *pdev;
182
183 unsigned int id; /* topology stub index */
184
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300185 phys_addr_t doorbell_base; /* Start of actual doorbells used by
186 * KFD. It is aligned for mapping
187 * into user mode
188 */
189 size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell
190 * to HW doorbell, GFX reserved some
191 * at the start)
192 */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300193 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
194 * page used by kernel queue
195 */
196
Oded Gabbay4a488a72014-07-16 21:08:55 +0300197 struct kgd2kfd_shared_resources shared_resources;
Yong Zhao44008d72017-09-20 18:10:18 -0400198 struct kfd_vmid_info vm_info;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300199
Xihan Zhangcea405b2015-03-17 19:32:53 +0800200 const struct kfd2kgd_calls *kfd2kgd;
201 struct mutex doorbell_mutex;
Joe Perchesf761d8b2015-05-19 18:37:51 -0700202 DECLARE_BITMAP(doorbell_available_index,
203 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800204
Oded Gabbay36b5c082014-10-26 09:53:10 +0200205 void *gtt_mem;
206 uint64_t gtt_start_gpu_addr;
207 void *gtt_start_cpu_ptr;
208 void *gtt_sa_bitmap;
209 struct mutex gtt_sa_lock;
210 unsigned int gtt_sa_chunk_size;
211 unsigned int gtt_sa_num_of_chunks;
212
Andrew Lewycky2249d552014-07-17 01:37:30 +0300213 /* Interrupts */
Andres Rodriguez04ad47b2017-10-27 19:35:31 -0400214 struct kfifo ih_fifo;
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400215 struct workqueue_struct *ih_wq;
Andrew Lewycky2249d552014-07-17 01:37:30 +0300216 struct work_struct interrupt_work;
217 spinlock_t interrupt_lock;
218
Ben Gozed6e6a32014-07-17 00:45:35 +0300219 /* QCM Device instance */
220 struct device_queue_manager *dqm;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300221
Ben Gozed6e6a32014-07-17 00:45:35 +0300222 bool init_complete;
Andrew Lewycky2249d552014-07-17 01:37:30 +0300223 /*
224 * Interrupts of interest to KFD are copied
225 * from the HW ring into a SW ring.
226 */
227 bool interrupts_active;
Yair Shacharfbeb6612015-05-20 13:48:26 +0300228
229 /* Debug manager */
230 struct kfd_dbgmgr *dbgmgr;
Felix Kuehling373d7082017-11-14 16:41:19 -0500231
Felix Kuehlinga99c6d42017-11-27 18:29:45 -0500232 /* Maximum process number mapped to HW scheduler */
233 unsigned int max_proc_per_quantum;
234
Felix Kuehling373d7082017-11-14 16:41:19 -0500235 /* CWSR */
236 bool cwsr_enabled;
237 const void *cwsr_isa;
238 unsigned int cwsr_isa_size;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300239};
240
241/* KGD2KFD callbacks */
242void kgd2kfd_exit(void);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800243struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
244 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300245bool kgd2kfd_device_init(struct kfd_dev *kfd,
Xihan Zhangcea405b2015-03-17 19:32:53 +0800246 const struct kgd2kfd_shared_resources *gpu_resources);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300247void kgd2kfd_device_exit(struct kfd_dev *kfd);
248
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300249enum kfd_mempool {
250 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
251 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
252 KFD_MEMPOOL_FRAMEBUFFER = 3,
253};
254
Oded Gabbay4a488a72014-07-16 21:08:55 +0300255/* Character device interface */
256int kfd_chardev_init(void);
257void kfd_chardev_exit(void);
258struct device *kfd_chardev(void);
259
Ben Goz241f24f2014-07-17 00:55:28 +0300260/**
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400261 * enum kfd_unmap_queues_filter
Ben Goz241f24f2014-07-17 00:55:28 +0300262 *
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400263 * @KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE: Preempts single queue.
Ben Goz241f24f2014-07-17 00:55:28 +0300264 *
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400265 * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
Ben Goz241f24f2014-07-17 00:55:28 +0300266 * running queues list.
267 *
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400268 * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
Ben Goz241f24f2014-07-17 00:55:28 +0300269 * specific process.
270 *
271 */
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400272enum kfd_unmap_queues_filter {
273 KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE,
274 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES,
275 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
276 KFD_UNMAP_QUEUES_FILTER_BY_PASID
Ben Goz241f24f2014-07-17 00:55:28 +0300277};
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300278
Ben Gozed8aab42014-07-17 00:18:51 +0300279/**
280 * enum kfd_queue_type
281 *
282 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
283 *
284 * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type.
285 *
286 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
287 *
288 * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
289 */
290enum kfd_queue_type {
291 KFD_QUEUE_TYPE_COMPUTE,
292 KFD_QUEUE_TYPE_SDMA,
293 KFD_QUEUE_TYPE_HIQ,
294 KFD_QUEUE_TYPE_DIQ
295};
296
Ben Goz6e99df52014-07-17 00:36:17 +0300297enum kfd_queue_format {
298 KFD_QUEUE_FORMAT_PM4,
299 KFD_QUEUE_FORMAT_AQL
300};
301
Ben Gozed8aab42014-07-17 00:18:51 +0300302/**
303 * struct queue_properties
304 *
305 * @type: The queue type.
306 *
307 * @queue_id: Queue identifier.
308 *
309 * @queue_address: Queue ring buffer address.
310 *
311 * @queue_size: Queue ring buffer size.
312 *
313 * @priority: Defines the queue priority relative to other queues in the
314 * process.
315 * This is just an indication and HW scheduling may override the priority as
316 * necessary while keeping the relative prioritization.
317 * the priority granularity is from 0 to f which f is the highest priority.
318 * currently all queues are initialized with the highest priority.
319 *
320 * @queue_percent: This field is partially implemented and currently a zero in
321 * this field defines that the queue is non active.
322 *
323 * @read_ptr: User space address which points to the number of dwords the
324 * cp read from the ring buffer. This field updates automatically by the H/W.
325 *
326 * @write_ptr: Defines the number of dwords written to the ring buffer.
327 *
328 * @doorbell_ptr: This field aim is to notify the H/W of new packet written to
Kent Russell8eabaf52017-08-15 23:00:04 -0400329 * the queue ring buffer. This field should be similar to write_ptr and the
330 * user should update this field after he updated the write_ptr.
Ben Gozed8aab42014-07-17 00:18:51 +0300331 *
332 * @doorbell_off: The doorbell offset in the doorbell pci-bar.
333 *
Kent Russell8eabaf52017-08-15 23:00:04 -0400334 * @is_interop: Defines if this is a interop queue. Interop queue means that
335 * the queue can access both graphics and compute resources.
Ben Gozed8aab42014-07-17 00:18:51 +0300336 *
337 * @is_active: Defines if the queue is active or not.
338 *
339 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
340 * of the queue.
341 *
342 * This structure represents the queue properties for each queue no matter if
343 * it's user mode or kernel mode queue.
344 *
345 */
346struct queue_properties {
347 enum kfd_queue_type type;
Ben Goz6e99df52014-07-17 00:36:17 +0300348 enum kfd_queue_format format;
Ben Gozed8aab42014-07-17 00:18:51 +0300349 unsigned int queue_id;
350 uint64_t queue_address;
351 uint64_t queue_size;
352 uint32_t priority;
353 uint32_t queue_percent;
354 uint32_t *read_ptr;
355 uint32_t *write_ptr;
Oded Gabbay5cd78de2014-11-20 16:14:56 +0200356 uint32_t __iomem *doorbell_ptr;
Ben Gozed8aab42014-07-17 00:18:51 +0300357 uint32_t doorbell_off;
358 bool is_interop;
359 bool is_active;
360 /* Not relevant for user mode queues in cp scheduling */
361 unsigned int vmid;
Ben Goz77669eb2015-01-03 22:12:31 +0200362 /* Relevant only for sdma queues*/
363 uint32_t sdma_engine_id;
364 uint32_t sdma_queue_id;
365 uint32_t sdma_vm_addr;
Ben Gozff3d04a2015-01-04 10:37:18 +0200366 /* Relevant only for VI */
367 uint64_t eop_ring_buffer_address;
368 uint32_t eop_ring_buffer_size;
369 uint64_t ctx_save_restore_area_address;
370 uint32_t ctx_save_restore_area_size;
Felix Kuehling373d7082017-11-14 16:41:19 -0500371 uint32_t ctl_stack_size;
372 uint64_t tba_addr;
373 uint64_t tma_addr;
Ben Gozed8aab42014-07-17 00:18:51 +0300374};
375
376/**
377 * struct queue
378 *
379 * @list: Queue linked list.
380 *
381 * @mqd: The queue MQD.
382 *
383 * @mqd_mem_obj: The MQD local gpu memory object.
384 *
385 * @gart_mqd_addr: The MQD gart mc address.
386 *
387 * @properties: The queue properties.
388 *
389 * @mec: Used only in no cp scheduling mode and identifies to micro engine id
Kent Russell8eabaf52017-08-15 23:00:04 -0400390 * that the queue should be execute on.
Ben Gozed8aab42014-07-17 00:18:51 +0300391 *
Kent Russell8eabaf52017-08-15 23:00:04 -0400392 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
393 * id.
Ben Gozed8aab42014-07-17 00:18:51 +0300394 *
395 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
396 *
397 * @process: The kfd process that created this queue.
398 *
399 * @device: The kfd device that created this queue.
400 *
401 * This structure represents user mode compute queues.
402 * It contains all the necessary data to handle such queues.
403 *
404 */
405
406struct queue {
407 struct list_head list;
408 void *mqd;
409 struct kfd_mem_obj *mqd_mem_obj;
410 uint64_t gart_mqd_addr;
411 struct queue_properties properties;
412
413 uint32_t mec;
414 uint32_t pipe;
415 uint32_t queue;
416
Ben Goz77669eb2015-01-03 22:12:31 +0200417 unsigned int sdma_id;
418
Ben Gozed8aab42014-07-17 00:18:51 +0300419 struct kfd_process *process;
420 struct kfd_dev *device;
421};
422
Ben Goz6e99df52014-07-17 00:36:17 +0300423/*
424 * Please read the kfd_mqd_manager.h description.
425 */
426enum KFD_MQD_TYPE {
Ben Goz85d258f2015-01-04 10:36:30 +0200427 KFD_MQD_TYPE_COMPUTE = 0, /* for no cp scheduling */
428 KFD_MQD_TYPE_HIQ, /* for hiq */
429 KFD_MQD_TYPE_CP, /* for cp queues and diq */
430 KFD_MQD_TYPE_SDMA, /* for sdma queues */
Ben Goz6e99df52014-07-17 00:36:17 +0300431 KFD_MQD_TYPE_MAX
432};
433
Ben Goz241f24f2014-07-17 00:55:28 +0300434struct scheduling_resources {
435 unsigned int vmid_mask;
436 enum kfd_queue_type type;
437 uint64_t queue_mask;
438 uint64_t gws_mask;
439 uint32_t oac_mask;
440 uint32_t gds_heap_base;
441 uint32_t gds_heap_size;
442};
443
444struct process_queue_manager {
445 /* data */
446 struct kfd_process *process;
Ben Goz241f24f2014-07-17 00:55:28 +0300447 struct list_head queues;
448 unsigned long *queue_slot_bitmap;
449};
450
451struct qcm_process_device {
452 /* The Device Queue Manager that owns this data */
453 struct device_queue_manager *dqm;
454 struct process_queue_manager *pqm;
Ben Goz241f24f2014-07-17 00:55:28 +0300455 /* Queues list */
456 struct list_head queues_list;
457 struct list_head priv_queue_list;
458
459 unsigned int queue_count;
460 unsigned int vmid;
461 bool is_debug;
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400462
463 /* This flag tells if we should reset all wavefronts on
464 * process termination
465 */
466 bool reset_wavefronts;
467
Ben Goz241f24f2014-07-17 00:55:28 +0300468 /*
469 * All the memory management data should be here too
470 */
471 uint64_t gds_context_area;
472 uint32_t sh_mem_config;
473 uint32_t sh_mem_bases;
474 uint32_t sh_mem_ape1_base;
475 uint32_t sh_mem_ape1_limit;
476 uint32_t page_table_base;
477 uint32_t gds_size;
478 uint32_t num_gws;
479 uint32_t num_oac;
Moses Reuben6a1c9512017-08-15 23:00:20 -0400480 uint32_t sh_hidden_private_base;
Felix Kuehling373d7082017-11-14 16:41:19 -0500481
482 /* CWSR memory */
483 void *cwsr_kaddr;
484 uint64_t tba_addr;
485 uint64_t tma_addr;
Ben Goz241f24f2014-07-17 00:55:28 +0300486};
487
Yong Zhao733fa1f2017-09-20 18:10:14 -0400488
489enum kfd_pdd_bound {
490 PDD_UNBOUND = 0,
491 PDD_BOUND,
492 PDD_BOUND_SUSPENDED,
493};
494
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300495/* Data that is per-process-per device. */
496struct kfd_process_device {
497 /*
498 * List of all per-device data for a process.
499 * Starts from kfd_process.per_device_data.
500 */
501 struct list_head per_device_list;
502
503 /* The device that owns this data. */
504 struct kfd_dev *dev;
505
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400506 /* The process that owns this kfd_process_device. */
507 struct kfd_process *process;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300508
Ben Goz45102042014-07-17 01:04:10 +0300509 /* per-process-per device QCM data structure */
510 struct qcm_process_device qpd;
511
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300512 /*Apertures*/
513 uint64_t lds_base;
514 uint64_t lds_limit;
515 uint64_t gpuvm_base;
516 uint64_t gpuvm_limit;
517 uint64_t scratch_base;
518 uint64_t scratch_limit;
519
520 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
Yong Zhao733fa1f2017-09-20 18:10:14 -0400521 enum kfd_pdd_bound bound;
Ben Goza82918f2015-03-25 13:12:20 +0200522
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400523 /* Flag used to tell the pdd has dequeued from the dqm.
524 * This is used to prevent dev->dqm->ops.process_termination() from
525 * being called twice when it is already called in IOMMU callback
526 * function.
Ben Goza82918f2015-03-25 13:12:20 +0200527 */
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400528 bool already_dequeued;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300529};
530
Alexey Skidanov52a5fdc2014-11-19 17:07:00 +0200531#define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
532
Oded Gabbay4a488a72014-07-16 21:08:55 +0300533/* Process data */
534struct kfd_process {
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300535 /*
536 * kfd_process are stored in an mm_struct*->kfd_process*
537 * hash table (kfd_processes in kfd_process.c)
538 */
539 struct hlist_node kfd_processes;
540
Felix Kuehling9b56bb12017-10-27 19:35:19 -0400541 /*
542 * Opaque pointer to mm_struct. We don't hold a reference to
543 * it so it should never be dereferenced from here. This is
544 * only used for looking up processes by their mm.
545 */
546 void *mm;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300547
Felix Kuehling5ce10682017-11-27 18:29:51 -0500548 struct kref ref;
549 struct work_struct release_work;
550
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300551 struct mutex mutex;
552
553 /*
554 * In any process, the thread that started main() is the lead
555 * thread and outlives the rest.
556 * It is here because amd_iommu_bind_pasid wants a task_struct.
Felix Kuehling894a8292017-11-01 19:21:33 -0400557 * It can also be used for safely getting a reference to the
558 * mm_struct of the process.
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300559 */
560 struct task_struct *lead_thread;
561
562 /* We want to receive a notification when the mm_struct is destroyed */
563 struct mmu_notifier mmu_notifier;
564
565 /* Use for delayed freeing of kfd_process structure */
566 struct rcu_head rcu;
567
568 unsigned int pasid;
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400569 unsigned int doorbell_index;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300570
571 /*
572 * List of kfd_process_device structures,
573 * one for each device the process is using.
574 */
575 struct list_head per_device_data;
576
Ben Goz45102042014-07-17 01:04:10 +0300577 struct process_queue_manager pqm;
578
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300579 /*Is the user space process 32 bit?*/
580 bool is_32bit_user_mode;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300581
582 /* Event-related data */
583 struct mutex event_mutex;
Felix Kuehling482f0772017-10-27 19:35:27 -0400584 /* Event ID allocator and lookup */
585 struct idr event_idr;
Felix Kuehling50cb7dd2017-10-27 19:35:26 -0400586 /* Event page */
587 struct kfd_signal_page *signal_page;
Felix Kuehlingb9a5d0a2017-10-27 19:35:29 -0400588 size_t signal_mapped_size;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300589 size_t signal_event_count;
Felix Kuehlingc9861692017-09-20 18:10:22 -0400590 bool signal_event_limit_reached;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300591};
592
Oded Gabbay76baee62014-12-29 14:20:05 +0200593/**
594 * Ioctl function type.
595 *
596 * \param filep pointer to file structure.
597 * \param p amdkfd process pointer.
598 * \param data pointer to arg that was copied from user.
599 */
600typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
601 void *data);
602
603struct amdkfd_ioctl_desc {
604 unsigned int cmd;
605 int flags;
606 amdkfd_ioctl_t *func;
607 unsigned int cmd_drv;
608 const char *name;
609};
610
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300611void kfd_process_create_wq(void);
612void kfd_process_destroy_wq(void);
Felix Kuehling373d7082017-11-14 16:41:19 -0500613struct kfd_process *kfd_create_process(struct file *filep);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300614struct kfd_process *kfd_get_process(const struct task_struct *);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300615struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid);
Felix Kuehlingabb208a2017-11-27 18:29:52 -0500616void kfd_unref_process(struct kfd_process *p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300617
Ben Goz64c7f8c2014-07-17 01:27:00 +0300618struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
Yong Zhao733fa1f2017-09-20 18:10:14 -0400619 struct kfd_process *p);
620int kfd_bind_processes_to_device(struct kfd_dev *dev);
621void kfd_unbind_processes_from_device(struct kfd_dev *dev);
622void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300623struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
Alexey Skidanov093c7d82014-11-18 14:00:04 +0200624 struct kfd_process *p);
625struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
626 struct kfd_process *p);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300627
Felix Kuehling373d7082017-11-14 16:41:19 -0500628int kfd_reserved_mem_mmap(struct kfd_process *process,
629 struct vm_area_struct *vma);
630
Alexey Skidanov775921e2014-07-17 01:49:36 +0300631/* Process device data iterator */
Kent Russell8eabaf52017-08-15 23:00:04 -0400632struct kfd_process_device *kfd_get_first_process_device_data(
633 struct kfd_process *p);
634struct kfd_process_device *kfd_get_next_process_device_data(
635 struct kfd_process *p,
Alexey Skidanov775921e2014-07-17 01:49:36 +0300636 struct kfd_process_device *pdd);
637bool kfd_has_process_device_data(struct kfd_process *p);
638
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300639/* PASIDs */
640int kfd_pasid_init(void);
641void kfd_pasid_exit(void);
642bool kfd_set_pasid_limit(unsigned int new_limit);
643unsigned int kfd_get_pasid_limit(void);
644unsigned int kfd_pasid_alloc(void);
645void kfd_pasid_free(unsigned int pasid);
646
647/* Doorbells */
Felix Kuehling735df2b2017-08-15 23:00:10 -0400648int kfd_doorbell_init(struct kfd_dev *kfd);
649void kfd_doorbell_fini(struct kfd_dev *kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300650int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
651u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
652 unsigned int *doorbell_off);
653void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
654u32 read_kernel_doorbell(u32 __iomem *db);
655void write_kernel_doorbell(u32 __iomem *db, u32 value);
656unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
657 struct kfd_process *process,
658 unsigned int queue_id);
Felix Kuehlinga91e70e2017-08-26 02:00:57 -0400659phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
660 struct kfd_process *process);
661int kfd_alloc_process_doorbells(struct kfd_process *process);
662void kfd_free_process_doorbells(struct kfd_process *process);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300663
Oded Gabbay6e810902014-10-27 14:36:07 +0200664/* GTT Sub-Allocator */
665
666int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
667 struct kfd_mem_obj **mem_obj);
668
669int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);
670
Oded Gabbay4a488a72014-07-16 21:08:55 +0300671extern struct device *kfd_device;
672
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300673/* Topology */
674int kfd_topology_init(void);
675void kfd_topology_shutdown(void);
676int kfd_topology_add_device(struct kfd_dev *gpu);
677int kfd_topology_remove_device(struct kfd_dev *gpu);
Harish Kasiviswanathan3a871772017-12-08 23:08:59 -0500678struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
679 uint32_t proximity_domain);
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300680struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
681struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
Harish Kasiviswanathan6d82eb02017-12-08 23:08:53 -0500682int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev);
Felix Kuehling520b8fb2017-12-08 23:08:58 -0500683int kfd_numa_node_to_apic_id(int numa_node_id);
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300684
Oded Gabbay4a488a72014-07-16 21:08:55 +0300685/* Interrupts */
Andrew Lewycky2249d552014-07-17 01:37:30 +0300686int kfd_interrupt_init(struct kfd_dev *dev);
687void kfd_interrupt_exit(struct kfd_dev *dev);
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300688void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300689bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry);
690bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300691
692/* Power Management */
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300693void kgd2kfd_suspend(struct kfd_dev *kfd);
694int kgd2kfd_resume(struct kfd_dev *kfd);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300695
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300696/* amdkfd Apertures */
697int kfd_init_apertures(struct kfd_process *process);
698
Ben Gozed6e6a32014-07-17 00:45:35 +0300699/* Queue Context Management */
Edward O'Callaghane88a6142016-09-17 15:01:45 +1000700int init_queue(struct queue **q, const struct queue_properties *properties);
Ben Gozed6e6a32014-07-17 00:45:35 +0300701void uninit_queue(struct queue *q);
Ben Goz45102042014-07-17 01:04:10 +0300702void print_queue_properties(struct queue_properties *q);
Ben Gozed6e6a32014-07-17 00:45:35 +0300703void print_queue(struct queue *q);
704
Ben Goz64c7f8c2014-07-17 01:27:00 +0300705struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
706 struct kfd_dev *dev);
Ben Goz4b8f5892015-01-04 11:24:25 +0200707struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
708 struct kfd_dev *dev);
Felix Kuehlingee04955a2018-01-04 17:17:45 -0500709struct mqd_manager *mqd_manager_init_cik_hawaii(enum KFD_MQD_TYPE type,
710 struct kfd_dev *dev);
Ben Goz4b8f5892015-01-04 11:24:25 +0200711struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
712 struct kfd_dev *dev);
Felix Kuehlingee04955a2018-01-04 17:17:45 -0500713struct mqd_manager *mqd_manager_init_vi_tonga(enum KFD_MQD_TYPE type,
714 struct kfd_dev *dev);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300715struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
716void device_queue_manager_uninit(struct device_queue_manager *dqm);
Ben Goz241f24f2014-07-17 00:55:28 +0300717struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
718 enum kfd_queue_type type);
719void kernel_queue_uninit(struct kernel_queue *kq);
720
Ben Goz45102042014-07-17 01:04:10 +0300721/* Process Queue Manager */
722struct process_queue_node {
723 struct queue *q;
724 struct kernel_queue *kq;
725 struct list_head process_queue_list;
726};
727
Felix Kuehling9fd3f1bf2017-09-27 00:09:52 -0400728void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
729void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
Ben Goz45102042014-07-17 01:04:10 +0300730int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
731void pqm_uninit(struct process_queue_manager *pqm);
732int pqm_create_queue(struct process_queue_manager *pqm,
733 struct kfd_dev *dev,
734 struct file *f,
735 struct queue_properties *properties,
Ben Goz45102042014-07-17 01:04:10 +0300736 unsigned int *qid);
737int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
738int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
739 struct queue_properties *p);
Yair Shacharfbeb6612015-05-20 13:48:26 +0300740struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
741 unsigned int qid);
Ben Goz45102042014-07-17 01:04:10 +0300742
Yair Shachar788bf832015-05-20 13:58:12 +0300743int amdkfd_fence_wait_timeout(unsigned int *fence_addr,
744 unsigned int fence_value,
Yong Zhao8c72c3d2017-09-20 18:10:15 -0400745 unsigned int timeout_ms);
Yair Shachar788bf832015-05-20 13:58:12 +0300746
Ben Gozed6e6a32014-07-17 00:45:35 +0300747/* Packet Manager */
748
Ben Goz64c7f8c2014-07-17 01:27:00 +0300749#define KFD_FENCE_COMPLETED (100)
750#define KFD_FENCE_INIT (10)
Ben Goz241f24f2014-07-17 00:55:28 +0300751
Ben Gozed6e6a32014-07-17 00:45:35 +0300752struct packet_manager {
753 struct device_queue_manager *dqm;
754 struct kernel_queue *priv_queue;
755 struct mutex lock;
756 bool allocated;
757 struct kfd_mem_obj *ib_buffer_obj;
Felix Kuehling851a6452017-11-27 18:29:49 -0500758 unsigned int ib_size_bytes;
Ben Gozed6e6a32014-07-17 00:45:35 +0300759};
760
Ben Goz64c7f8c2014-07-17 01:27:00 +0300761int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
762void pm_uninit(struct packet_manager *pm);
763int pm_send_set_resources(struct packet_manager *pm,
764 struct scheduling_resources *res);
765int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
766int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
767 uint32_t fence_value);
768
769int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
Yong Zhao7da2bcf2017-09-27 00:09:48 -0400770 enum kfd_unmap_queues_filter mode,
Ben Goz64c7f8c2014-07-17 01:27:00 +0300771 uint32_t filter_param, bool reset,
772 unsigned int sdma_engine);
773
Ben Goz241f24f2014-07-17 00:55:28 +0300774void pm_release_ib(struct packet_manager *pm);
775
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300776uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300777
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300778/* Events */
779extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
Alexey Skidanov930c5ff2014-11-25 10:34:31 +0200780extern const struct kfd_device_global_init_class device_global_init_class_cik;
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300781
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300782void kfd_event_init_process(struct kfd_process *p);
783void kfd_event_free_process(struct kfd_process *p);
784int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
785int kfd_wait_on_events(struct kfd_process *p,
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300786 uint32_t num_events, void __user *data,
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300787 bool all, uint32_t user_timeout_ms,
Felix Kuehlingfdf0c832017-10-27 19:35:22 -0400788 uint32_t *wait_result);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300789void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id,
790 uint32_t valid_id_bits);
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300791void kfd_signal_iommu_event(struct kfd_dev *dev,
792 unsigned int pasid, unsigned long address,
793 bool is_write_requested, bool is_execute_requested);
Alexey Skidanov930c5ff2014-11-25 10:34:31 +0200794void kfd_signal_hw_exception_event(unsigned int pasid);
Andrew Lewyckyf3a39812015-05-10 12:15:46 +0300795int kfd_set_event(struct kfd_process *p, uint32_t event_id);
796int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
797int kfd_event_create(struct file *devkfd, struct kfd_process *p,
798 uint32_t event_type, bool auto_reset, uint32_t node_id,
799 uint32_t *event_id, uint32_t *event_trigger_data,
800 uint64_t *event_page_offset, uint32_t *event_slot_index);
801int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
802
Ben Gozc3447e82015-05-20 18:05:44 +0300803int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p);
804
Felix Kuehling851a6452017-11-27 18:29:49 -0500805/* Debugfs */
806#if defined(CONFIG_DEBUG_FS)
807
808void kfd_debugfs_init(void);
809void kfd_debugfs_fini(void);
810int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data);
811int pqm_debugfs_mqds(struct seq_file *m, void *data);
812int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data);
813int dqm_debugfs_hqds(struct seq_file *m, void *data);
814int kfd_debugfs_rls_by_device(struct seq_file *m, void *data);
815int pm_debugfs_runlist(struct seq_file *m, void *data);
816
817#else
818
819static inline void kfd_debugfs_init(void) {}
820static inline void kfd_debugfs_fini(void) {}
821
822#endif
823
Oded Gabbay4a488a72014-07-16 21:08:55 +0300824#endif