xref: /linux/drivers/gpu/drm/amd/amdkfd/kfd_packet_manager.c (revision 0a8d25285feb68608acdf778983ee5f4d72707e8)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include "kfd_device_queue_manager.h"
28 #include "kfd_kernel_queue.h"
29 #include "kfd_priv.h"
30 
31 static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
32 				unsigned int buffer_size_bytes)
33 {
34 	unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
35 
36 	WARN((temp * sizeof(uint32_t)) > buffer_size_bytes,
37 	     "Runlist IB overflow");
38 	*wptr = temp;
39 }
40 
41 static void pm_calc_rlib_size(struct packet_manager *pm,
42 				unsigned int *rlib_size,
43 				bool *over_subscription)
44 {
45 	unsigned int process_count, queue_count, compute_queue_count, gws_queue_count;
46 	unsigned int map_queue_size;
47 	unsigned int max_proc_per_quantum = 1;
48 	struct kfd_node *dev = pm->dqm->dev;
49 
50 	process_count = pm->dqm->processes_count;
51 	queue_count = pm->dqm->active_queue_count;
52 	compute_queue_count = pm->dqm->active_cp_queue_count;
53 	gws_queue_count = pm->dqm->gws_queue_count;
54 
55 	/* check if there is over subscription
56 	 * Note: the arbitration between the number of VMIDs and
57 	 * hws_max_conc_proc has been done in
58 	 * kgd2kfd_device_init().
59 	 */
60 	*over_subscription = false;
61 
62 	if (dev->max_proc_per_quantum > 1)
63 		max_proc_per_quantum = dev->max_proc_per_quantum;
64 
65 	if ((process_count > max_proc_per_quantum) ||
66 	    compute_queue_count > get_cp_queues_num(pm->dqm) ||
67 	    gws_queue_count > 1) {
68 		*over_subscription = true;
69 		pr_debug("Over subscribed runlist\n");
70 	}
71 
72 	map_queue_size = pm->pmf->map_queues_size;
73 	/* calculate run list ib allocation size */
74 	*rlib_size = process_count * pm->pmf->map_process_size +
75 		     queue_count * map_queue_size;
76 
77 	/*
78 	 * Increase the allocation size in case we need a chained run list
79 	 * when over subscription
80 	 */
81 	if (*over_subscription)
82 		*rlib_size += pm->pmf->runlist_size;
83 
84 	pr_debug("runlist ib size %d\n", *rlib_size);
85 }
86 
87 static int pm_allocate_runlist_ib(struct packet_manager *pm,
88 				unsigned int **rl_buffer,
89 				uint64_t *rl_gpu_buffer,
90 				unsigned int *rl_buffer_size,
91 				bool *is_over_subscription)
92 {
93 	int retval;
94 
95 	if (WARN_ON(pm->allocated))
96 		return -EINVAL;
97 
98 	pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
99 
100 	mutex_lock(&pm->lock);
101 
102 	retval = kfd_gtt_sa_allocate(pm->dqm->dev, *rl_buffer_size,
103 					&pm->ib_buffer_obj);
104 
105 	if (retval) {
106 		pr_err("Failed to allocate runlist IB\n");
107 		goto out;
108 	}
109 
110 	*(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
111 	*rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
112 
113 	memset(*rl_buffer, 0, *rl_buffer_size);
114 	pm->allocated = true;
115 
116 out:
117 	mutex_unlock(&pm->lock);
118 	return retval;
119 }
120 
121 static int pm_create_runlist_ib(struct packet_manager *pm,
122 				struct list_head *queues,
123 				uint64_t *rl_gpu_addr,
124 				size_t *rl_size_bytes)
125 {
126 	unsigned int alloc_size_bytes;
127 	unsigned int *rl_buffer, rl_wptr, i;
128 	int retval, processes_mapped;
129 	struct device_process_node *cur;
130 	struct qcm_process_device *qpd;
131 	struct queue *q;
132 	struct kernel_queue *kq;
133 	bool is_over_subscription;
134 
135 	rl_wptr = retval = processes_mapped = 0;
136 
137 	retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
138 				&alloc_size_bytes, &is_over_subscription);
139 	if (retval)
140 		return retval;
141 
142 	*rl_size_bytes = alloc_size_bytes;
143 	pm->ib_size_bytes = alloc_size_bytes;
144 
145 	pr_debug("Building runlist ib process count: %d queues count %d\n",
146 		pm->dqm->processes_count, pm->dqm->active_queue_count);
147 
148 	/* build the run list ib packet */
149 	list_for_each_entry(cur, queues, list) {
150 		qpd = cur->qpd;
151 		/* build map process packet */
152 		if (processes_mapped >= pm->dqm->processes_count) {
153 			pr_debug("Not enough space left in runlist IB\n");
154 			pm_release_ib(pm);
155 			return -ENOMEM;
156 		}
157 
158 		retval = pm->pmf->map_process(pm, &rl_buffer[rl_wptr], qpd);
159 		if (retval)
160 			return retval;
161 
162 		processes_mapped++;
163 		inc_wptr(&rl_wptr, pm->pmf->map_process_size,
164 				alloc_size_bytes);
165 
166 		list_for_each_entry(kq, &qpd->priv_queue_list, list) {
167 			if (!kq->queue->properties.is_active)
168 				continue;
169 
170 			pr_debug("static_queue, mapping kernel q %d, is debug status %d\n",
171 				kq->queue->queue, qpd->is_debug);
172 
173 			retval = pm->pmf->map_queues(pm,
174 						&rl_buffer[rl_wptr],
175 						kq->queue,
176 						qpd->is_debug);
177 			if (retval)
178 				return retval;
179 
180 			inc_wptr(&rl_wptr,
181 				pm->pmf->map_queues_size,
182 				alloc_size_bytes);
183 		}
184 
185 		list_for_each_entry(q, &qpd->queues_list, list) {
186 			if (!q->properties.is_active)
187 				continue;
188 
189 			pr_debug("static_queue, mapping user queue %d, is debug status %d\n",
190 				q->queue, qpd->is_debug);
191 
192 			retval = pm->pmf->map_queues(pm,
193 						&rl_buffer[rl_wptr],
194 						q,
195 						qpd->is_debug);
196 
197 			if (retval)
198 				return retval;
199 
200 			inc_wptr(&rl_wptr,
201 				pm->pmf->map_queues_size,
202 				alloc_size_bytes);
203 		}
204 	}
205 
206 	pr_debug("Finished map process and queues to runlist\n");
207 
208 	if (is_over_subscription) {
209 		if (!pm->is_over_subscription)
210 			pr_warn("Runlist is getting oversubscribed. Expect reduced ROCm performance.\n");
211 		retval = pm->pmf->runlist(pm, &rl_buffer[rl_wptr],
212 					*rl_gpu_addr,
213 					alloc_size_bytes / sizeof(uint32_t),
214 					true);
215 	}
216 	pm->is_over_subscription = is_over_subscription;
217 
218 	for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
219 		pr_debug("0x%2X ", rl_buffer[i]);
220 	pr_debug("\n");
221 
222 	return retval;
223 }
224 
225 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
226 {
227 	switch (dqm->dev->adev->asic_type) {
228 	case CHIP_KAVERI:
229 	case CHIP_HAWAII:
230 		/* PM4 packet structures on CIK are the same as on VI */
231 	case CHIP_CARRIZO:
232 	case CHIP_TONGA:
233 	case CHIP_FIJI:
234 	case CHIP_POLARIS10:
235 	case CHIP_POLARIS11:
236 	case CHIP_POLARIS12:
237 	case CHIP_VEGAM:
238 		pm->pmf = &kfd_vi_pm_funcs;
239 		break;
240 	default:
241 		if (KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 2) ||
242 		    KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 3) ||
243 		    KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 4))
244 			pm->pmf = &kfd_aldebaran_pm_funcs;
245 		else if (KFD_GC_VERSION(dqm->dev) >= IP_VERSION(9, 0, 1))
246 			pm->pmf = &kfd_v9_pm_funcs;
247 		else {
248 			WARN(1, "Unexpected ASIC family %u",
249 			     dqm->dev->adev->asic_type);
250 			return -EINVAL;
251 		}
252 	}
253 
254 	pm->dqm = dqm;
255 	mutex_init(&pm->lock);
256 	pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
257 	if (!pm->priv_queue) {
258 		mutex_destroy(&pm->lock);
259 		return -ENOMEM;
260 	}
261 	pm->allocated = false;
262 
263 	return 0;
264 }
265 
266 void pm_uninit(struct packet_manager *pm, bool hanging)
267 {
268 	mutex_destroy(&pm->lock);
269 	kernel_queue_uninit(pm->priv_queue, hanging);
270 	pm->priv_queue = NULL;
271 }
272 
273 int pm_send_set_resources(struct packet_manager *pm,
274 				struct scheduling_resources *res)
275 {
276 	uint32_t *buffer, size;
277 	int retval = 0;
278 
279 	size = pm->pmf->set_resources_size;
280 	mutex_lock(&pm->lock);
281 	kq_acquire_packet_buffer(pm->priv_queue,
282 					size / sizeof(uint32_t),
283 					(unsigned int **)&buffer);
284 	if (!buffer) {
285 		pr_err("Failed to allocate buffer on kernel queue\n");
286 		retval = -ENOMEM;
287 		goto out;
288 	}
289 
290 	retval = pm->pmf->set_resources(pm, buffer, res);
291 	if (!retval)
292 		retval = kq_submit_packet(pm->priv_queue);
293 	else
294 		kq_rollback_packet(pm->priv_queue);
295 
296 out:
297 	mutex_unlock(&pm->lock);
298 
299 	return retval;
300 }
301 
302 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
303 {
304 	uint64_t rl_gpu_ib_addr;
305 	uint32_t *rl_buffer;
306 	size_t rl_ib_size, packet_size_dwords;
307 	int retval;
308 
309 	retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
310 					&rl_ib_size);
311 	if (retval)
312 		goto fail_create_runlist_ib;
313 
314 	pr_debug("runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
315 
316 	packet_size_dwords = pm->pmf->runlist_size / sizeof(uint32_t);
317 	mutex_lock(&pm->lock);
318 
319 	retval = kq_acquire_packet_buffer(pm->priv_queue,
320 					packet_size_dwords, &rl_buffer);
321 	if (retval)
322 		goto fail_acquire_packet_buffer;
323 
324 	retval = pm->pmf->runlist(pm, rl_buffer, rl_gpu_ib_addr,
325 					rl_ib_size / sizeof(uint32_t), false);
326 	if (retval)
327 		goto fail_create_runlist;
328 
329 	retval = kq_submit_packet(pm->priv_queue);
330 
331 	mutex_unlock(&pm->lock);
332 
333 	return retval;
334 
335 fail_create_runlist:
336 	kq_rollback_packet(pm->priv_queue);
337 fail_acquire_packet_buffer:
338 	mutex_unlock(&pm->lock);
339 fail_create_runlist_ib:
340 	pm_release_ib(pm);
341 	return retval;
342 }
343 
344 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
345 			uint64_t fence_value)
346 {
347 	uint32_t *buffer, size;
348 	int retval = 0;
349 
350 	if (WARN_ON(!fence_address))
351 		return -EFAULT;
352 
353 	size = pm->pmf->query_status_size;
354 	mutex_lock(&pm->lock);
355 	kq_acquire_packet_buffer(pm->priv_queue,
356 			size / sizeof(uint32_t), (unsigned int **)&buffer);
357 	if (!buffer) {
358 		pr_err("Failed to allocate buffer on kernel queue\n");
359 		retval = -ENOMEM;
360 		goto out;
361 	}
362 
363 	retval = pm->pmf->query_status(pm, buffer, fence_address, fence_value);
364 	if (!retval)
365 		retval = kq_submit_packet(pm->priv_queue);
366 	else
367 		kq_rollback_packet(pm->priv_queue);
368 
369 out:
370 	mutex_unlock(&pm->lock);
371 	return retval;
372 }
373 
374 int pm_update_grace_period(struct packet_manager *pm, uint32_t grace_period)
375 {
376 	int retval = 0;
377 	uint32_t *buffer, size;
378 
379 	size = pm->pmf->set_grace_period_size;
380 
381 	mutex_lock(&pm->lock);
382 
383 	if (size) {
384 		kq_acquire_packet_buffer(pm->priv_queue,
385 			size / sizeof(uint32_t),
386 			(unsigned int **)&buffer);
387 
388 		if (!buffer) {
389 			pr_err("Failed to allocate buffer on kernel queue\n");
390 			retval = -ENOMEM;
391 			goto out;
392 		}
393 
394 		retval = pm->pmf->set_grace_period(pm, buffer, grace_period);
395 		if (!retval)
396 			retval = kq_submit_packet(pm->priv_queue);
397 		else
398 			kq_rollback_packet(pm->priv_queue);
399 	}
400 
401 out:
402 	mutex_unlock(&pm->lock);
403 	return retval;
404 }
405 
406 int pm_send_unmap_queue(struct packet_manager *pm,
407 			enum kfd_unmap_queues_filter filter,
408 			uint32_t filter_param, bool reset)
409 {
410 	uint32_t *buffer, size;
411 	int retval = 0;
412 
413 	size = pm->pmf->unmap_queues_size;
414 	mutex_lock(&pm->lock);
415 	kq_acquire_packet_buffer(pm->priv_queue,
416 			size / sizeof(uint32_t), (unsigned int **)&buffer);
417 	if (!buffer) {
418 		pr_err("Failed to allocate buffer on kernel queue\n");
419 		retval = -ENOMEM;
420 		goto out;
421 	}
422 
423 	retval = pm->pmf->unmap_queues(pm, buffer, filter, filter_param, reset);
424 	if (!retval)
425 		retval = kq_submit_packet(pm->priv_queue);
426 	else
427 		kq_rollback_packet(pm->priv_queue);
428 
429 out:
430 	mutex_unlock(&pm->lock);
431 	return retval;
432 }
433 
434 void pm_release_ib(struct packet_manager *pm)
435 {
436 	mutex_lock(&pm->lock);
437 	if (pm->allocated) {
438 		kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj);
439 		pm->allocated = false;
440 	}
441 	mutex_unlock(&pm->lock);
442 }
443 
444 #if defined(CONFIG_DEBUG_FS)
445 
446 int pm_debugfs_runlist(struct seq_file *m, void *data)
447 {
448 	struct packet_manager *pm = data;
449 
450 	mutex_lock(&pm->lock);
451 
452 	if (!pm->allocated) {
453 		seq_puts(m, "  No active runlist\n");
454 		goto out;
455 	}
456 
457 	seq_hex_dump(m, "  ", DUMP_PREFIX_OFFSET, 32, 4,
458 		     pm->ib_buffer_obj->cpu_ptr, pm->ib_size_bytes, false);
459 
460 out:
461 	mutex_unlock(&pm->lock);
462 	return 0;
463 }
464 
465 int pm_debugfs_hang_hws(struct packet_manager *pm)
466 {
467 	uint32_t *buffer, size;
468 	int r = 0;
469 
470 	if (!pm->priv_queue)
471 		return -EAGAIN;
472 
473 	size = pm->pmf->query_status_size;
474 	mutex_lock(&pm->lock);
475 	kq_acquire_packet_buffer(pm->priv_queue,
476 			size / sizeof(uint32_t), (unsigned int **)&buffer);
477 	if (!buffer) {
478 		pr_err("Failed to allocate buffer on kernel queue\n");
479 		r = -ENOMEM;
480 		goto out;
481 	}
482 	memset(buffer, 0x55, size);
483 	kq_submit_packet(pm->priv_queue);
484 
485 	pr_info("Submitting %x %x %x %x %x %x %x to HIQ to hang the HWS.",
486 		buffer[0], buffer[1], buffer[2], buffer[3],
487 		buffer[4], buffer[5], buffer[6]);
488 out:
489 	mutex_unlock(&pm->lock);
490 	return r;
491 }
492 
493 
494 #endif
495