2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #ifndef KFD_PRIV_H_INCLUDED
24 #define KFD_PRIV_H_INCLUDED
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>
33 #include <linux/kfd_ioctl.h>
34 #include <kgd_kfd_interface.h>
36 #define KFD_SYSFS_FILE_MODE 0444
39 * When working with cp scheduler we should assign the HIQ manually or via
40 * the radeon driver to a fixed hqd slot, here are the fixed HIQ hqd slot
41 * definitions for Kaveri. In Kaveri only the first ME queues participates
42 * in the cp scheduling taking that in mind we set the HIQ slot in the
45 #define KFD_CIK_HIQ_PIPE 4
46 #define KFD_CIK_HIQ_QUEUE 0
48 /* GPU ID hash width in bits */
49 #define KFD_GPU_ID_HASH_WIDTH 16
51 /* Macro for allocating structures */
52 #define kfd_alloc_struct(ptr_to_struct) \
53 ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
55 #define KFD_MAX_NUM_OF_PROCESSES 512
56 #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
59 * Kernel module parameter to specify maximum number of supported queues per
62 extern int max_num_of_queues_per_device;
64 #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT 4096
65 #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \
66 (KFD_MAX_NUM_OF_PROCESSES * \
67 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
69 #define KFD_KERNEL_QUEUE_SIZE 2048
71 /* Kernel module parameter to specify the scheduling policy */
72 extern int sched_policy;
75 * enum kfd_sched_policy
77 * @KFD_SCHED_POLICY_HWS: H/W scheduling policy known as command processor (cp)
78 * scheduling. In this scheduling mode we're using the firmware code to
79 * schedule the user mode queues and kernel queues such as HIQ and DIQ.
80 * the HIQ queue is used as a special queue that dispatches the configuration
81 * to the cp and the user mode queues list that are currently running.
82 * the DIQ queue is a debugging queue that dispatches debugging commands to the
84 * in this scheduling mode user mode queues over subscription feature is
87 * @KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: The same as above but the over
88 * subscription feature disabled.
90 * @KFD_SCHED_POLICY_NO_HWS: no H/W scheduling policy is a mode which directly
91 * set the command processor registers and sets the queues "manually". This
92 * mode is used *ONLY* for debugging proposes.
95 enum kfd_sched_policy {
96 KFD_SCHED_POLICY_HWS = 0,
97 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION,
98 KFD_SCHED_POLICY_NO_HWS
102 cache_policy_coherent,
103 cache_policy_noncoherent
106 enum asic_family_type {
111 struct kfd_device_info {
112 unsigned int asic_family;
113 unsigned int max_pasid_bits;
114 size_t ih_ring_entry_size;
115 uint8_t num_of_watch_points;
116 uint16_t mqd_size_aligned;
120 uint32_t range_start;
129 const struct kfd_device_info *device_info;
130 struct pci_dev *pdev;
132 unsigned int id; /* topology stub index */
134 phys_addr_t doorbell_base; /* Start of actual doorbells used by
135 * KFD. It is aligned for mapping
138 size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell
139 * to HW doorbell, GFX reserved some
142 size_t doorbell_process_limit; /* Number of processes we have doorbell
145 u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
146 * page used by kernel queue
149 struct kgd2kfd_shared_resources shared_resources;
151 const struct kfd2kgd_calls *kfd2kgd;
152 struct mutex doorbell_mutex;
153 unsigned long doorbell_available_index[DIV_ROUND_UP(
154 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, BITS_PER_LONG)];
157 uint64_t gtt_start_gpu_addr;
158 void *gtt_start_cpu_ptr;
160 struct mutex gtt_sa_lock;
161 unsigned int gtt_sa_chunk_size;
162 unsigned int gtt_sa_num_of_chunks;
164 /* QCM Device instance */
165 struct device_queue_manager *dqm;
170 /* KGD2KFD callbacks */
171 void kgd2kfd_exit(void);
172 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
173 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g);
174 bool kgd2kfd_device_init(struct kfd_dev *kfd,
175 const struct kgd2kfd_shared_resources *gpu_resources);
176 void kgd2kfd_device_exit(struct kfd_dev *kfd);
179 KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
180 KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
181 KFD_MEMPOOL_FRAMEBUFFER = 3,
184 /* Character device interface */
185 int kfd_chardev_init(void);
186 void kfd_chardev_exit(void);
187 struct device *kfd_chardev(void);
190 * enum kfd_preempt_type_filter
192 * @KFD_PREEMPT_TYPE_FILTER_SINGLE_QUEUE: Preempts single queue.
194 * @KFD_PRERMPT_TYPE_FILTER_ALL_QUEUES: Preempts all queues in the
195 * running queues list.
197 * @KFD_PRERMPT_TYPE_FILTER_BY_PASID: Preempts queues that belongs to
201 enum kfd_preempt_type_filter {
202 KFD_PREEMPT_TYPE_FILTER_SINGLE_QUEUE,
203 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES,
204 KFD_PREEMPT_TYPE_FILTER_BY_PASID
207 enum kfd_preempt_type {
208 KFD_PREEMPT_TYPE_WAVEFRONT,
209 KFD_PREEMPT_TYPE_WAVEFRONT_RESET
213 * enum kfd_queue_type
215 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
217 * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type.
219 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
221 * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
223 enum kfd_queue_type {
224 KFD_QUEUE_TYPE_COMPUTE,
230 enum kfd_queue_format {
231 KFD_QUEUE_FORMAT_PM4,
236 * struct queue_properties
238 * @type: The queue type.
240 * @queue_id: Queue identifier.
242 * @queue_address: Queue ring buffer address.
244 * @queue_size: Queue ring buffer size.
246 * @priority: Defines the queue priority relative to other queues in the
248 * This is just an indication and HW scheduling may override the priority as
249 * necessary while keeping the relative prioritization.
250 * the priority granularity is from 0 to f which f is the highest priority.
251 * currently all queues are initialized with the highest priority.
253 * @queue_percent: This field is partially implemented and currently a zero in
254 * this field defines that the queue is non active.
256 * @read_ptr: User space address which points to the number of dwords the
257 * cp read from the ring buffer. This field updates automatically by the H/W.
259 * @write_ptr: Defines the number of dwords written to the ring buffer.
261 * @doorbell_ptr: This field aim is to notify the H/W of new packet written to
262 * the queue ring buffer. This field should be similar to write_ptr and the user
263 * should update this field after he updated the write_ptr.
265 * @doorbell_off: The doorbell offset in the doorbell pci-bar.
267 * @is_interop: Defines if this is a interop queue. Interop queue means that the
268 * queue can access both graphics and compute resources.
270 * @is_active: Defines if the queue is active or not.
272 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
275 * This structure represents the queue properties for each queue no matter if
276 * it's user mode or kernel mode queue.
279 struct queue_properties {
280 enum kfd_queue_type type;
281 enum kfd_queue_format format;
282 unsigned int queue_id;
283 uint64_t queue_address;
286 uint32_t queue_percent;
289 uint32_t __iomem *doorbell_ptr;
290 uint32_t doorbell_off;
293 /* Not relevant for user mode queues in cp scheduling */
295 /* Relevant only for sdma queues*/
296 uint32_t sdma_engine_id;
297 uint32_t sdma_queue_id;
298 uint32_t sdma_vm_addr;
299 /* Relevant only for VI */
300 uint64_t eop_ring_buffer_address;
301 uint32_t eop_ring_buffer_size;
302 uint64_t ctx_save_restore_area_address;
303 uint32_t ctx_save_restore_area_size;
309 * @list: Queue linked list.
311 * @mqd: The queue MQD.
313 * @mqd_mem_obj: The MQD local gpu memory object.
315 * @gart_mqd_addr: The MQD gart mc address.
317 * @properties: The queue properties.
319 * @mec: Used only in no cp scheduling mode and identifies to micro engine id
320 * that the queue should be execute on.
322 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe id.
324 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
326 * @process: The kfd process that created this queue.
328 * @device: The kfd device that created this queue.
330 * This structure represents user mode compute queues.
331 * It contains all the necessary data to handle such queues.
336 struct list_head list;
338 struct kfd_mem_obj *mqd_mem_obj;
339 uint64_t gart_mqd_addr;
340 struct queue_properties properties;
346 unsigned int sdma_id;
348 struct kfd_process *process;
349 struct kfd_dev *device;
353 * Please read the kfd_mqd_manager.h description.
356 KFD_MQD_TYPE_COMPUTE = 0, /* for no cp scheduling */
357 KFD_MQD_TYPE_HIQ, /* for hiq */
358 KFD_MQD_TYPE_CP, /* for cp queues and diq */
359 KFD_MQD_TYPE_SDMA, /* for sdma queues */
363 struct scheduling_resources {
364 unsigned int vmid_mask;
365 enum kfd_queue_type type;
369 uint32_t gds_heap_base;
370 uint32_t gds_heap_size;
373 struct process_queue_manager {
375 struct kfd_process *process;
376 unsigned int num_concurrent_processes;
377 struct list_head queues;
378 unsigned long *queue_slot_bitmap;
381 struct qcm_process_device {
382 /* The Device Queue Manager that owns this data */
383 struct device_queue_manager *dqm;
384 struct process_queue_manager *pqm;
386 struct list_head queues_list;
387 struct list_head priv_queue_list;
389 unsigned int queue_count;
393 * All the memory management data should be here too
395 uint64_t gds_context_area;
396 uint32_t sh_mem_config;
397 uint32_t sh_mem_bases;
398 uint32_t sh_mem_ape1_base;
399 uint32_t sh_mem_ape1_limit;
400 uint32_t page_table_base;
406 /* Data that is per-process-per device. */
407 struct kfd_process_device {
409 * List of all per-device data for a process.
410 * Starts from kfd_process.per_device_data.
412 struct list_head per_device_list;
414 /* The device that owns this data. */
418 /* per-process-per device QCM data structure */
419 struct qcm_process_device qpd;
425 uint64_t gpuvm_limit;
426 uint64_t scratch_base;
427 uint64_t scratch_limit;
429 /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
433 #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
438 * kfd_process are stored in an mm_struct*->kfd_process*
439 * hash table (kfd_processes in kfd_process.c)
441 struct hlist_node kfd_processes;
443 struct mm_struct *mm;
448 * In any process, the thread that started main() is the lead
449 * thread and outlives the rest.
450 * It is here because amd_iommu_bind_pasid wants a task_struct.
452 struct task_struct *lead_thread;
454 /* We want to receive a notification when the mm_struct is destroyed */
455 struct mmu_notifier mmu_notifier;
457 /* Use for delayed freeing of kfd_process structure */
463 * List of kfd_process_device structures,
464 * one for each device the process is using.
466 struct list_head per_device_data;
468 struct process_queue_manager pqm;
470 /* The process's queues. */
471 size_t queue_array_size;
473 /* Size is queue_array_size, up to MAX_PROCESS_QUEUES. */
474 struct kfd_queue **queues;
476 unsigned long allocated_queue_bitmap[DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, BITS_PER_LONG)];
478 /*Is the user space process 32 bit?*/
479 bool is_32bit_user_mode;
483 * Ioctl function type.
485 * \param filep pointer to file structure.
486 * \param p amdkfd process pointer.
487 * \param data pointer to arg that was copied from user.
489 typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
492 struct amdkfd_ioctl_desc {
495 amdkfd_ioctl_t *func;
496 unsigned int cmd_drv;
500 void kfd_process_create_wq(void);
501 void kfd_process_destroy_wq(void);
502 struct kfd_process *kfd_create_process(const struct task_struct *);
503 struct kfd_process *kfd_get_process(const struct task_struct *);
505 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
506 struct kfd_process *p);
507 void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid);
508 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
509 struct kfd_process *p);
510 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
511 struct kfd_process *p);
513 /* Process device data iterator */
514 struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p);
515 struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p,
516 struct kfd_process_device *pdd);
517 bool kfd_has_process_device_data(struct kfd_process *p);
520 int kfd_pasid_init(void);
521 void kfd_pasid_exit(void);
522 bool kfd_set_pasid_limit(unsigned int new_limit);
523 unsigned int kfd_get_pasid_limit(void);
524 unsigned int kfd_pasid_alloc(void);
525 void kfd_pasid_free(unsigned int pasid);
528 void kfd_doorbell_init(struct kfd_dev *kfd);
529 int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma);
530 u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
531 unsigned int *doorbell_off);
532 void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
533 u32 read_kernel_doorbell(u32 __iomem *db);
534 void write_kernel_doorbell(u32 __iomem *db, u32 value);
535 unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
536 struct kfd_process *process,
537 unsigned int queue_id);
539 /* GTT Sub-Allocator */
541 int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
542 struct kfd_mem_obj **mem_obj);
544 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);
546 extern struct device *kfd_device;
549 int kfd_topology_init(void);
550 void kfd_topology_shutdown(void);
551 int kfd_topology_add_device(struct kfd_dev *gpu);
552 int kfd_topology_remove_device(struct kfd_dev *gpu);
553 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
554 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
555 struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx);
558 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
560 /* Power Management */
561 void kgd2kfd_suspend(struct kfd_dev *kfd);
562 int kgd2kfd_resume(struct kfd_dev *kfd);
564 /* amdkfd Apertures */
565 int kfd_init_apertures(struct kfd_process *process);
567 /* Queue Context Management */
568 inline uint32_t lower_32(uint64_t x);
569 inline uint32_t upper_32(uint64_t x);
570 struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd);
571 inline uint32_t get_sdma_base_addr(struct cik_sdma_rlc_registers *m);
573 int init_queue(struct queue **q, struct queue_properties properties);
574 void uninit_queue(struct queue *q);
575 void print_queue_properties(struct queue_properties *q);
576 void print_queue(struct queue *q);
578 struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
579 struct kfd_dev *dev);
580 struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
581 struct kfd_dev *dev);
582 struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
583 struct kfd_dev *dev);
584 struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
585 void device_queue_manager_uninit(struct device_queue_manager *dqm);
586 struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
587 enum kfd_queue_type type);
588 void kernel_queue_uninit(struct kernel_queue *kq);
590 /* Process Queue Manager */
591 struct process_queue_node {
593 struct kernel_queue *kq;
594 struct list_head process_queue_list;
597 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
598 void pqm_uninit(struct process_queue_manager *pqm);
599 int pqm_create_queue(struct process_queue_manager *pqm,
602 struct queue_properties *properties,
604 enum kfd_queue_type type,
606 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
607 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
608 struct queue_properties *p);
612 #define KFD_HIQ_TIMEOUT (500)
614 #define KFD_FENCE_COMPLETED (100)
615 #define KFD_FENCE_INIT (10)
616 #define KFD_UNMAP_LATENCY (150)
618 struct packet_manager {
619 struct device_queue_manager *dqm;
620 struct kernel_queue *priv_queue;
623 struct kfd_mem_obj *ib_buffer_obj;
626 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
627 void pm_uninit(struct packet_manager *pm);
628 int pm_send_set_resources(struct packet_manager *pm,
629 struct scheduling_resources *res);
630 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
631 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
632 uint32_t fence_value);
634 int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
635 enum kfd_preempt_type_filter mode,
636 uint32_t filter_param, bool reset,
637 unsigned int sdma_engine);
639 void pm_release_ib(struct packet_manager *pm);
641 uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
642 phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
643 struct kfd_process *process);