1 /*
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 
24 #ifndef KFD_DEVICE_QUEUE_MANAGER_H_
25 #define KFD_DEVICE_QUEUE_MANAGER_H_
26 
27 #include <linux/rwsem.h>
28 #include <linux/list.h>
29 #include "kfd_priv.h"
30 #include "kfd_mqd_manager.h"
31 
32 #define QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS	(500)
33 #define QUEUES_PER_PIPE				(8)
34 #define PIPE_PER_ME_CP_SCHEDULING		(3)
35 #define CIK_VMID_NUM				(8)
36 #define KFD_VMID_START_OFFSET			(8)
37 #define VMID_PER_DEVICE				CIK_VMID_NUM
38 #define KFD_DQM_FIRST_PIPE			(0)
39 
40 struct device_process_node {
41 	struct qcm_process_device *qpd;
42 	struct list_head list;
43 };
44 
45 /**
46  * struct device_queue_manager
47  *
48  * @create_queue: Queue creation routine.
49  *
50  * @destroy_queue: Queue destruction routine.
51  *
52  * @update_queue: Queue update routine.
53  *
54  * @get_mqd_manager: Returns the mqd manager according to the mqd type.
55  *
56  * @exeute_queues: Dispatches the queues list to the H/W.
57  *
58  * @register_process: This routine associates a specific process with device.
59  *
60  * @unregister_process: destroys the associations between process to device.
61  *
62  * @initialize: Initializes the pipelines and memory module for that device.
63  *
64  * @start: Initializes the resources/modules the the device needs for queues
65  * execution. This function is called on device initialization and after the
66  * system woke up after suspension.
67  *
68  * @stop: This routine stops execution of all the active queue running on the
69  * H/W and basically this function called on system suspend.
70  *
71  * @uninitialize: Destroys all the device queue manager resources allocated in
72  * initialize routine.
73  *
74  * @create_kernel_queue: Creates kernel queue. Used for debug queue.
75  *
76  * @destroy_kernel_queue: Destroys kernel queue. Used for debug queue.
77  *
78  * @set_cache_memory_policy: Sets memory policy (cached/ non cached) for the
79  * memory apertures.
80  *
81  * This struct is a base class for the kfd queues scheduler in the
82  * device level. The device base class should expose the basic operations
83  * for queue creation and queue destruction. This base class hides the
84  * scheduling mode of the driver and the specific implementation of the
85  * concrete device. This class is the only class in the queues scheduler
86  * that configures the H/W.
87  */
88 
89 struct device_queue_manager {
90 	int	(*create_queue)(struct device_queue_manager *dqm,
91 				struct queue *q,
92 				struct qcm_process_device *qpd,
93 				int *allocate_vmid);
94 	int	(*destroy_queue)(struct device_queue_manager *dqm,
95 				struct qcm_process_device *qpd,
96 				struct queue *q);
97 	int	(*update_queue)(struct device_queue_manager *dqm,
98 				struct queue *q);
99 
100 	struct mqd_manager * (*get_mqd_manager)
101 					(struct device_queue_manager *dqm,
102 					enum KFD_MQD_TYPE type);
103 
104 	int	(*register_process)(struct device_queue_manager *dqm,
105 					struct qcm_process_device *qpd);
106 	int	(*unregister_process)(struct device_queue_manager *dqm,
107 					struct qcm_process_device *qpd);
108 	int	(*initialize)(struct device_queue_manager *dqm);
109 	int	(*start)(struct device_queue_manager *dqm);
110 	int	(*stop)(struct device_queue_manager *dqm);
111 	void	(*uninitialize)(struct device_queue_manager *dqm);
112 	int	(*create_kernel_queue)(struct device_queue_manager *dqm,
113 					struct kernel_queue *kq,
114 					struct qcm_process_device *qpd);
115 	void	(*destroy_kernel_queue)(struct device_queue_manager *dqm,
116 					struct kernel_queue *kq,
117 					struct qcm_process_device *qpd);
118 	bool	(*set_cache_memory_policy)(struct device_queue_manager *dqm,
119 					   struct qcm_process_device *qpd,
120 					   enum cache_policy default_policy,
121 					   enum cache_policy alternate_policy,
122 					   void __user *alternate_aperture_base,
123 					   uint64_t alternate_aperture_size);
124 
125 
126 	struct mqd_manager	*mqds[KFD_MQD_TYPE_MAX];
127 	struct packet_manager	packets;
128 	struct kfd_dev		*dev;
129 	struct mutex		lock;
130 	struct list_head	queues;
131 	unsigned int		processes_count;
132 	unsigned int		queue_count;
133 	unsigned int		total_queue_count;
134 	unsigned int		next_pipe_to_allocate;
135 	unsigned int		*allocated_queues;
136 	unsigned int		vmid_bitmap;
137 	uint64_t		pipelines_addr;
138 	struct kfd_mem_obj	*pipeline_mem;
139 	uint64_t		fence_gpu_addr;
140 	unsigned int		*fence_addr;
141 	struct kfd_mem_obj	*fence_mem;
142 	bool			active_runlist;
143 };
144 
145 
146 
147 #endif /* KFD_DEVICE_QUEUE_MANAGER_H_ */
148