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