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 #include "amdgpu_amdkfd.h"
30 
31 static inline struct process_queue_node *get_queue_by_qid(
32 			struct process_queue_manager *pqm, unsigned int qid)
33 {
34 	struct process_queue_node *pqn;
35 
36 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
37 		if ((pqn->q && pqn->q->properties.queue_id == qid) ||
38 		    (pqn->kq && pqn->kq->queue->properties.queue_id == qid))
39 			return pqn;
40 	}
41 
42 	return NULL;
43 }
44 
45 static int find_available_queue_slot(struct process_queue_manager *pqm,
46 					unsigned int *qid)
47 {
48 	unsigned long found;
49 
50 	found = find_first_zero_bit(pqm->queue_slot_bitmap,
51 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
52 
53 	pr_debug("The new slot id %lu\n", found);
54 
55 	if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
56 		pr_info("Cannot open more queues for process with pasid 0x%x\n",
57 				pqm->process->pasid);
58 		return -ENOMEM;
59 	}
60 
61 	set_bit(found, pqm->queue_slot_bitmap);
62 	*qid = found;
63 
64 	return 0;
65 }
66 
67 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
68 {
69 	struct kfd_dev *dev = pdd->dev;
70 
71 	if (pdd->already_dequeued)
72 		return;
73 
74 	dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
75 	pdd->already_dequeued = true;
76 }
77 
78 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
79 			void *gws)
80 {
81 	struct kfd_dev *dev = NULL;
82 	struct process_queue_node *pqn;
83 	struct kfd_process_device *pdd;
84 	struct kgd_mem *mem = NULL;
85 	int ret;
86 
87 	pqn = get_queue_by_qid(pqm, qid);
88 	if (!pqn) {
89 		pr_err("Queue id does not match any known queue\n");
90 		return -EINVAL;
91 	}
92 
93 	if (pqn->q)
94 		dev = pqn->q->device;
95 	if (WARN_ON(!dev))
96 		return -ENODEV;
97 
98 	pdd = kfd_get_process_device_data(dev, pqm->process);
99 	if (!pdd) {
100 		pr_err("Process device data doesn't exist\n");
101 		return -EINVAL;
102 	}
103 
104 	/* Only allow one queue per process can have GWS assigned */
105 	if (gws && pdd->qpd.num_gws)
106 		return -EBUSY;
107 
108 	if (!gws && pdd->qpd.num_gws == 0)
109 		return -EINVAL;
110 
111 	if (gws)
112 		ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info,
113 			gws, &mem);
114 	else
115 		ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info,
116 			pqn->q->gws);
117 	if (unlikely(ret))
118 		return ret;
119 
120 	pqn->q->gws = mem;
121 	pdd->qpd.num_gws = gws ? amdgpu_amdkfd_get_num_gws(dev->kgd) : 0;
122 
123 	return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
124 							pqn->q);
125 }
126 
127 void kfd_process_dequeue_from_all_devices(struct kfd_process *p)
128 {
129 	struct kfd_process_device *pdd;
130 
131 	list_for_each_entry(pdd, &p->per_device_data, per_device_list)
132 		kfd_process_dequeue_from_device(pdd);
133 }
134 
135 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
136 {
137 	INIT_LIST_HEAD(&pqm->queues);
138 	pqm->queue_slot_bitmap =
139 			kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
140 					BITS_PER_BYTE), GFP_KERNEL);
141 	if (!pqm->queue_slot_bitmap)
142 		return -ENOMEM;
143 	pqm->process = p;
144 
145 	return 0;
146 }
147 
148 void pqm_uninit(struct process_queue_manager *pqm)
149 {
150 	struct process_queue_node *pqn, *next;
151 
152 	list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) {
153 		if (pqn->q && pqn->q->gws)
154 			amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info,
155 				pqn->q->gws);
156 		uninit_queue(pqn->q);
157 		list_del(&pqn->process_queue_list);
158 		kfree(pqn);
159 	}
160 
161 	kfree(pqm->queue_slot_bitmap);
162 	pqm->queue_slot_bitmap = NULL;
163 }
164 
165 static int init_user_queue(struct process_queue_manager *pqm,
166 				struct kfd_dev *dev, struct queue **q,
167 				struct queue_properties *q_properties,
168 				struct file *f, unsigned int qid)
169 {
170 	int retval;
171 
172 	/* Doorbell initialized in user space*/
173 	q_properties->doorbell_ptr = NULL;
174 
175 	/* let DQM handle it*/
176 	q_properties->vmid = 0;
177 	q_properties->queue_id = qid;
178 
179 	retval = init_queue(q, q_properties);
180 	if (retval != 0)
181 		return retval;
182 
183 	(*q)->device = dev;
184 	(*q)->process = pqm->process;
185 
186 	pr_debug("PQM After init queue");
187 
188 	return retval;
189 }
190 
191 int pqm_create_queue(struct process_queue_manager *pqm,
192 			    struct kfd_dev *dev,
193 			    struct file *f,
194 			    struct queue_properties *properties,
195 			    unsigned int *qid,
196 			    uint32_t *p_doorbell_offset_in_process)
197 {
198 	int retval;
199 	struct kfd_process_device *pdd;
200 	struct queue *q;
201 	struct process_queue_node *pqn;
202 	struct kernel_queue *kq;
203 	enum kfd_queue_type type = properties->type;
204 	unsigned int max_queues = 127; /* HWS limit */
205 
206 	q = NULL;
207 	kq = NULL;
208 
209 	pdd = kfd_get_process_device_data(dev, pqm->process);
210 	if (!pdd) {
211 		pr_err("Process device data doesn't exist\n");
212 		return -1;
213 	}
214 
215 	/*
216 	 * for debug process, verify that it is within the static queues limit
217 	 * currently limit is set to half of the total avail HQD slots
218 	 * If we are just about to create DIQ, the is_debug flag is not set yet
219 	 * Hence we also check the type as well
220 	 */
221 	if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ))
222 		max_queues = dev->device_info->max_no_of_hqd/2;
223 
224 	if (pdd->qpd.queue_count >= max_queues)
225 		return -ENOSPC;
226 
227 	retval = find_available_queue_slot(pqm, qid);
228 	if (retval != 0)
229 		return retval;
230 
231 	if (list_empty(&pdd->qpd.queues_list) &&
232 	    list_empty(&pdd->qpd.priv_queue_list))
233 		dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
234 
235 	pqn = kzalloc(sizeof(*pqn), GFP_KERNEL);
236 	if (!pqn) {
237 		retval = -ENOMEM;
238 		goto err_allocate_pqn;
239 	}
240 
241 	switch (type) {
242 	case KFD_QUEUE_TYPE_SDMA:
243 	case KFD_QUEUE_TYPE_SDMA_XGMI:
244 		if ((type == KFD_QUEUE_TYPE_SDMA && dev->dqm->sdma_queue_count
245 			>= get_num_sdma_queues(dev->dqm)) ||
246 			(type == KFD_QUEUE_TYPE_SDMA_XGMI &&
247 			dev->dqm->xgmi_sdma_queue_count
248 			>= get_num_xgmi_sdma_queues(dev->dqm))) {
249 			pr_debug("Over-subscription is not allowed for SDMA.\n");
250 			retval = -EPERM;
251 			goto err_create_queue;
252 		}
253 
254 		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
255 		if (retval != 0)
256 			goto err_create_queue;
257 		pqn->q = q;
258 		pqn->kq = NULL;
259 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
260 		pr_debug("DQM returned %d for create_queue\n", retval);
261 		print_queue(q);
262 		break;
263 
264 	case KFD_QUEUE_TYPE_COMPUTE:
265 		/* check if there is over subscription */
266 		if ((dev->dqm->sched_policy ==
267 		     KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) &&
268 		((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) ||
269 		(dev->dqm->queue_count >= get_queues_num(dev->dqm)))) {
270 			pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n");
271 			retval = -EPERM;
272 			goto err_create_queue;
273 		}
274 
275 		retval = init_user_queue(pqm, dev, &q, properties, f, *qid);
276 		if (retval != 0)
277 			goto err_create_queue;
278 		pqn->q = q;
279 		pqn->kq = NULL;
280 		retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd);
281 		pr_debug("DQM returned %d for create_queue\n", retval);
282 		print_queue(q);
283 		break;
284 	case KFD_QUEUE_TYPE_DIQ:
285 		kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ);
286 		if (!kq) {
287 			retval = -ENOMEM;
288 			goto err_create_queue;
289 		}
290 		kq->queue->properties.queue_id = *qid;
291 		pqn->kq = kq;
292 		pqn->q = NULL;
293 		retval = dev->dqm->ops.create_kernel_queue(dev->dqm,
294 							kq, &pdd->qpd);
295 		break;
296 	default:
297 		WARN(1, "Invalid queue type %d", type);
298 		retval = -EINVAL;
299 	}
300 
301 	if (retval != 0) {
302 		pr_err("Pasid 0x%x DQM create queue %d failed. ret %d\n",
303 			pqm->process->pasid, type, retval);
304 		goto err_create_queue;
305 	}
306 
307 	if (q && p_doorbell_offset_in_process)
308 		/* Return the doorbell offset within the doorbell page
309 		 * to the caller so it can be passed up to user mode
310 		 * (in bytes).
311 		 * There are always 1024 doorbells per process, so in case
312 		 * of 8-byte doorbells, there are two doorbell pages per
313 		 * process.
314 		 */
315 		*p_doorbell_offset_in_process =
316 			(q->properties.doorbell_off * sizeof(uint32_t)) &
317 			(kfd_doorbell_process_slice(dev) - 1);
318 
319 	pr_debug("PQM After DQM create queue\n");
320 
321 	list_add(&pqn->process_queue_list, &pqm->queues);
322 
323 	if (q) {
324 		pr_debug("PQM done creating queue\n");
325 		kfd_procfs_add_queue(q);
326 		print_queue_properties(&q->properties);
327 	}
328 
329 	return retval;
330 
331 err_create_queue:
332 	kfree(pqn);
333 err_allocate_pqn:
334 	/* check if queues list is empty unregister process from device */
335 	clear_bit(*qid, pqm->queue_slot_bitmap);
336 	if (list_empty(&pdd->qpd.queues_list) &&
337 	    list_empty(&pdd->qpd.priv_queue_list))
338 		dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd);
339 	return retval;
340 }
341 
342 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
343 {
344 	struct process_queue_node *pqn;
345 	struct kfd_process_device *pdd;
346 	struct device_queue_manager *dqm;
347 	struct kfd_dev *dev;
348 	int retval;
349 
350 	dqm = NULL;
351 
352 	retval = 0;
353 
354 	pqn = get_queue_by_qid(pqm, qid);
355 	if (!pqn) {
356 		pr_err("Queue id does not match any known queue\n");
357 		return -EINVAL;
358 	}
359 
360 	dev = NULL;
361 	if (pqn->kq)
362 		dev = pqn->kq->dev;
363 	if (pqn->q)
364 		dev = pqn->q->device;
365 	if (WARN_ON(!dev))
366 		return -ENODEV;
367 
368 	pdd = kfd_get_process_device_data(dev, pqm->process);
369 	if (!pdd) {
370 		pr_err("Process device data doesn't exist\n");
371 		return -1;
372 	}
373 
374 	if (pqn->kq) {
375 		/* destroy kernel queue (DIQ) */
376 		dqm = pqn->kq->dev->dqm;
377 		dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd);
378 		kernel_queue_uninit(pqn->kq, false);
379 	}
380 
381 	if (pqn->q) {
382 		kfd_procfs_del_queue(pqn->q);
383 		dqm = pqn->q->device->dqm;
384 		retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
385 		if (retval) {
386 			pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
387 				pqm->process->pasid,
388 				pqn->q->properties.queue_id, retval);
389 			if (retval != -ETIME)
390 				goto err_destroy_queue;
391 		}
392 
393 		if (pqn->q->gws) {
394 			amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info,
395 				pqn->q->gws);
396 			pdd->qpd.num_gws = 0;
397 		}
398 
399 		kfree(pqn->q->properties.cu_mask);
400 		pqn->q->properties.cu_mask = NULL;
401 		uninit_queue(pqn->q);
402 	}
403 
404 	list_del(&pqn->process_queue_list);
405 	kfree(pqn);
406 	clear_bit(qid, pqm->queue_slot_bitmap);
407 
408 	if (list_empty(&pdd->qpd.queues_list) &&
409 	    list_empty(&pdd->qpd.priv_queue_list))
410 		dqm->ops.unregister_process(dqm, &pdd->qpd);
411 
412 err_destroy_queue:
413 	return retval;
414 }
415 
416 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
417 			struct queue_properties *p)
418 {
419 	int retval;
420 	struct process_queue_node *pqn;
421 
422 	pqn = get_queue_by_qid(pqm, qid);
423 	if (!pqn) {
424 		pr_debug("No queue %d exists for update operation\n", qid);
425 		return -EFAULT;
426 	}
427 
428 	pqn->q->properties.queue_address = p->queue_address;
429 	pqn->q->properties.queue_size = p->queue_size;
430 	pqn->q->properties.queue_percent = p->queue_percent;
431 	pqn->q->properties.priority = p->priority;
432 
433 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
434 							pqn->q);
435 	if (retval != 0)
436 		return retval;
437 
438 	return 0;
439 }
440 
441 int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid,
442 			struct queue_properties *p)
443 {
444 	int retval;
445 	struct process_queue_node *pqn;
446 
447 	pqn = get_queue_by_qid(pqm, qid);
448 	if (!pqn) {
449 		pr_debug("No queue %d exists for update operation\n", qid);
450 		return -EFAULT;
451 	}
452 
453 	/* Free the old CU mask memory if it is already allocated, then
454 	 * allocate memory for the new CU mask.
455 	 */
456 	kfree(pqn->q->properties.cu_mask);
457 
458 	pqn->q->properties.cu_mask_count = p->cu_mask_count;
459 	pqn->q->properties.cu_mask = p->cu_mask;
460 
461 	retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
462 							pqn->q);
463 	if (retval != 0)
464 		return retval;
465 
466 	return 0;
467 }
468 
469 struct kernel_queue *pqm_get_kernel_queue(
470 					struct process_queue_manager *pqm,
471 					unsigned int qid)
472 {
473 	struct process_queue_node *pqn;
474 
475 	pqn = get_queue_by_qid(pqm, qid);
476 	if (pqn && pqn->kq)
477 		return pqn->kq;
478 
479 	return NULL;
480 }
481 
482 int pqm_get_wave_state(struct process_queue_manager *pqm,
483 		       unsigned int qid,
484 		       void __user *ctl_stack,
485 		       u32 *ctl_stack_used_size,
486 		       u32 *save_area_used_size)
487 {
488 	struct process_queue_node *pqn;
489 
490 	pqn = get_queue_by_qid(pqm, qid);
491 	if (!pqn) {
492 		pr_debug("amdkfd: No queue %d exists for operation\n",
493 			 qid);
494 		return -EFAULT;
495 	}
496 
497 	return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm,
498 						       pqn->q,
499 						       ctl_stack,
500 						       ctl_stack_used_size,
501 						       save_area_used_size);
502 }
503 
504 #if defined(CONFIG_DEBUG_FS)
505 
506 int pqm_debugfs_mqds(struct seq_file *m, void *data)
507 {
508 	struct process_queue_manager *pqm = data;
509 	struct process_queue_node *pqn;
510 	struct queue *q;
511 	enum KFD_MQD_TYPE mqd_type;
512 	struct mqd_manager *mqd_mgr;
513 	int r = 0;
514 
515 	list_for_each_entry(pqn, &pqm->queues, process_queue_list) {
516 		if (pqn->q) {
517 			q = pqn->q;
518 			switch (q->properties.type) {
519 			case KFD_QUEUE_TYPE_SDMA:
520 			case KFD_QUEUE_TYPE_SDMA_XGMI:
521 				seq_printf(m, "  SDMA queue on device %x\n",
522 					   q->device->id);
523 				mqd_type = KFD_MQD_TYPE_SDMA;
524 				break;
525 			case KFD_QUEUE_TYPE_COMPUTE:
526 				seq_printf(m, "  Compute queue on device %x\n",
527 					   q->device->id);
528 				mqd_type = KFD_MQD_TYPE_CP;
529 				break;
530 			default:
531 				seq_printf(m,
532 				"  Bad user queue type %d on device %x\n",
533 					   q->properties.type, q->device->id);
534 				continue;
535 			}
536 			mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type];
537 		} else if (pqn->kq) {
538 			q = pqn->kq->queue;
539 			mqd_mgr = pqn->kq->mqd_mgr;
540 			switch (q->properties.type) {
541 			case KFD_QUEUE_TYPE_DIQ:
542 				seq_printf(m, "  DIQ on device %x\n",
543 					   pqn->kq->dev->id);
544 				break;
545 			default:
546 				seq_printf(m,
547 				"  Bad kernel queue type %d on device %x\n",
548 					   q->properties.type,
549 					   pqn->kq->dev->id);
550 				continue;
551 			}
552 		} else {
553 			seq_printf(m,
554 		"  Weird: Queue node with neither kernel nor user queue\n");
555 			continue;
556 		}
557 
558 		r = mqd_mgr->debugfs_show_mqd(m, q->mqd);
559 		if (r != 0)
560 			break;
561 	}
562 
563 	return r;
564 }
565 
566 #endif
567