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 #include <linux/slab.h>
25 #include <linux/list.h>
26 #include "kfd_device_queue_manager.h"
27 #include "kfd_priv.h"
28 #include "kfd_kernel_queue.h"
29 
30 static inline struct process_queue_node *get_queue_by_qid(
31 			struct process_queue_manager *pqm, unsigned int qid)
32 {
33 	struct process_queue_node *pqn;
34 
35 	BUG_ON(!pqm);
36 
37 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
38 		if (pqn->q && pqn->q->properties.queue_id == qid)
39 			return pqn;
40 		if (pqn->kq && pqn->kq->queue->properties.queue_id == qid)
41 			return pqn;
42 	}
43 
44 	return NULL;
45 }
46 
47 static int find_available_queue_slot(struct process_queue_manager *pqm,
48 					unsigned int *qid)
49 {
50 	unsigned long found;
51 
52 	BUG_ON(!pqm || !qid);
53 
54 	pr_debug("kfd: in %s\n", __func__);
55 
56 	found = find_first_zero_bit(pqm->queue_slot_bitmap,
57 			max_num_of_queues_per_process);
58 
59 	pr_debug("kfd: the new slot id %lu\n", found);
60 
61 	if (found >= max_num_of_queues_per_process) {
62 		pr_info("amdkfd: Can not open more queues for process with pasid %d\n",
63 				pqm->process->pasid);
64 		return -ENOMEM;
65 	}
66 
67 	set_bit(found, pqm->queue_slot_bitmap);
68 	*qid = found;
69 
70 	return 0;
71 }
72 
73 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
74 {
75 	BUG_ON(!pqm);
76 
77 	INIT_LIST_HEAD(&pqm->queues);
78 	pqm->queue_slot_bitmap =
79 			kzalloc(DIV_ROUND_UP(max_num_of_queues_per_process,
80 					BITS_PER_BYTE), GFP_KERNEL);
81 	if (pqm->queue_slot_bitmap == NULL)
82 		return -ENOMEM;
83 	pqm->process = p;
84 
85 	return 0;
86 }
87 
88 void pqm_uninit(struct process_queue_manager *pqm)
89 {
90 	int retval;
91 	struct process_queue_node *pqn, *next;
92 
93 	BUG_ON(!pqm);
94 
95 	pr_debug("In func %s\n", __func__);
96 
97 	list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
98 		retval = pqm_destroy_queue(
99 				pqm,
100 				(pqn->q != NULL) ?
101 					pqn->q->properties.queue_id :
102 					pqn->kq->queue->properties.queue_id);
103 
104 		if (retval != 0) {
105 			pr_err("kfd: failed to destroy queue\n");
106 			return;
107 		}
108 	}
109 	kfree(pqm->queue_slot_bitmap);
110 	pqm->queue_slot_bitmap = NULL;
111 }
112 
113 static int create_cp_queue(struct process_queue_manager *pqm,
114 				struct kfd_dev *dev, struct queue **q,
115 				struct queue_properties *q_properties,
116 				struct file *f, unsigned int qid)
117 {
118 	int retval;
119 
120 	retval = 0;
121 
122 	/* Doorbell initialized in user space*/
123 	q_properties->doorbell_ptr = NULL;
124 
125 	q_properties->doorbell_off =
126 			kfd_queue_id_to_doorbell(dev, pqm->process, qid);
127 
128 	/* let DQM handle it*/
129 	q_properties->vmid = 0;
130 	q_properties->queue_id = qid;
131 
132 	retval = init_queue(q, *q_properties);
133 	if (retval != 0)
134 		goto err_init_queue;
135 
136 	(*q)->device = dev;
137 	(*q)->process = pqm->process;
138 
139 	pr_debug("kfd: PQM After init queue");
140 
141 	return retval;
142 
143 err_init_queue:
144 	return retval;
145 }
146 
147 int pqm_create_queue(struct process_queue_manager *pqm,
148 			    struct kfd_dev *dev,
149 			    struct file *f,
150 			    struct queue_properties *properties,
151 			    unsigned int flags,
152 			    enum kfd_queue_type type,
153 			    unsigned int *qid)
154 {
155 	int retval;
156 	struct kfd_process_device *pdd;
157 	struct queue_properties q_properties;
158 	struct queue *q;
159 	struct process_queue_node *pqn;
160 	struct kernel_queue *kq;
161 
162 	BUG_ON(!pqm || !dev || !properties || !qid);
163 
164 	memset(&q_properties, 0, sizeof(struct queue_properties));
165 	memcpy(&q_properties, properties, sizeof(struct queue_properties));
166 	q = NULL;
167 	kq = NULL;
168 
169 	pdd = kfd_get_process_device_data(dev, pqm->process);
170 	if (!pdd) {
171 		pr_err("Process device data doesn't exist\n");
172 		return -1;
173 	}
174 
175 	retval = find_available_queue_slot(pqm, qid);
176 	if (retval != 0)
177 		return retval;
178 
179 	if (list_empty(&pqm->queues)) {
180 		pdd->qpd.pqm = pqm;
181 		dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
182 	}
183 
184 	pqn = kzalloc(sizeof(struct process_queue_node), GFP_KERNEL);
185 	if (!pqn) {
186 		retval = -ENOMEM;
187 		goto err_allocate_pqn;
188 	}
189 
190 	switch (type) {
191 	case KFD_QUEUE_TYPE_SDMA:
192 	case KFD_QUEUE_TYPE_COMPUTE:
193 		/* check if there is over subscription */
194 		if ((sched_policy == KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
195 		((dev->dqm->processes_count >= VMID_PER_DEVICE) ||
196 		(dev->dqm->queue_count >= PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE))) {
197 			pr_err("kfd: over-subscription is not allowed in radeon_kfd.sched_policy == 1\n");
198 			retval = -EPERM;
199 			goto err_create_queue;
200 		}
201 
202 		retval = create_cp_queue(pqm, dev, &q, &q_properties, f, *qid);
203 		if (retval != 0)
204 			goto err_create_queue;
205 		pqn->q = q;
206 		pqn->kq = NULL;
207 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd,
208 						&q->properties.vmid);
209 		print_queue(q);
210 		break;
211 	case KFD_QUEUE_TYPE_DIQ:
212 		kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
213 		if (kq == NULL) {
214 			retval = -ENOMEM;
215 			goto err_create_queue;
216 		}
217 		kq->queue->properties.queue_id = *qid;
218 		pqn->kq = kq;
219 		pqn->q = NULL;
220 		retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
221 							kq, &pdd->qpd);
222 		break;
223 	default:
224 		BUG();
225 		break;
226 	}
227 
228 	if (retval != 0) {
229 		pr_err("kfd: error dqm create queue\n");
230 		goto err_create_queue;
231 	}
232 
233 	pr_debug("kfd: PQM After DQM create queue\n");
234 
235 	list_add(&pqn->process_queue_list, &pqm->queues);
236 
237 	if (q) {
238 		*properties = q->properties;
239 		pr_debug("kfd: PQM done creating queue\n");
240 		print_queue_properties(properties);
241 	}
242 
243 	return retval;
244 
245 err_create_queue:
246 	kfree(pqn);
247 err_allocate_pqn:
248 	clear_bit(*qid, pqm->queue_slot_bitmap);
249 	return retval;
250 }
251 
252 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
253 {
254 	struct process_queue_node *pqn;
255 	struct kfd_process_device *pdd;
256 	struct device_queue_manager *dqm;
257 	struct kfd_dev *dev;
258 	int retval;
259 
260 	dqm = NULL;
261 
262 	BUG_ON(!pqm);
263 	retval = 0;
264 
265 	pr_debug("kfd: In Func %s\n", __func__);
266 
267 	pqn = get_queue_by_qid(pqm, qid);
268 	if (pqn == NULL) {
269 		pr_err("kfd: queue id does not match any known queue\n");
270 		return -EINVAL;
271 	}
272 
273 	dev = NULL;
274 	if (pqn->kq)
275 		dev = pqn->kq->dev;
276 	if (pqn->q)
277 		dev = pqn->q->device;
278 	BUG_ON(!dev);
279 
280 	pdd = kfd_get_process_device_data(dev, pqm->process);
281 	if (!pdd) {
282 		pr_err("Process device data doesn't exist\n");
283 		return -1;
284 	}
285 
286 	if (pqn->kq) {
287 		/* destroy kernel queue (DIQ) */
288 		dqm = pqn->kq->dev->dqm;
289 		dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
290 		kernel_queue_uninit(pqn->kq);
291 	}
292 
293 	if (pqn->q) {
294 		dqm = pqn->q->device->dqm;
295 		retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
296 		if (retval != 0)
297 			return retval;
298 
299 		uninit_queue(pqn->q);
300 	}
301 
302 	list_del(&pqn->process_queue_list);
303 	kfree(pqn);
304 	clear_bit(qid, pqm->queue_slot_bitmap);
305 
306 	if (list_empty(&pqm->queues))
307 		dqm->ops.unregister_process(dqm, &pdd->qpd);
308 
309 	return retval;
310 }
311 
312 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
313 			struct queue_properties *p)
314 {
315 	int retval;
316 	struct process_queue_node *pqn;
317 
318 	BUG_ON(!pqm);
319 
320 	pqn = get_queue_by_qid(pqm, qid);
321 	BUG_ON(!pqn);
322 
323 	pqn->q->properties.queue_address = p->queue_address;
324 	pqn->q->properties.queue_size = p->queue_size;
325 	pqn->q->properties.queue_percent = p->queue_percent;
326 	pqn->q->properties.priority = p->priority;
327 
328 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
329 							pqn->q);
330 	if (retval != 0)
331 		return retval;
332 
333 	return 0;
334 }
335 
336 static __attribute__((unused)) struct kernel_queue *pqm_get_kernel_queue(
337 					struct process_queue_manager *pqm,
338 					unsigned int qid)
339 {
340 	struct process_queue_node *pqn;
341 
342 	BUG_ON(!pqm);
343 
344 	pqn = get_queue_by_qid(pqm, qid);
345 	if (pqn && pqn->kq)
346 		return pqn->kq;
347 
348 	return NULL;
349 }
350 
351 
352