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 #define OVER_SUBSCRIPTION_PROCESS_COUNT (1 << 0) 32 #define OVER_SUBSCRIPTION_COMPUTE_QUEUE_COUNT (1 << 1) 33 #define OVER_SUBSCRIPTION_GWS_QUEUE_COUNT (1 << 2) 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 int *over_subscription) 48 { 49 unsigned int process_count, queue_count, compute_queue_count, gws_queue_count; 50 unsigned int map_queue_size; 51 unsigned int max_proc_per_quantum = 1; 52 struct kfd_node *node = pm->dqm->dev; 53 struct device *dev = node->adev->dev; 54 55 process_count = pm->dqm->processes_count; 56 queue_count = pm->dqm->active_queue_count; 57 compute_queue_count = pm->dqm->active_cp_queue_count; 58 gws_queue_count = pm->dqm->gws_queue_count; 59 60 /* check if there is over subscription 61 * Note: the arbitration between the number of VMIDs and 62 * hws_max_conc_proc has been done in 63 * kgd2kfd_device_init(). 64 */ 65 *over_subscription = 0; 66 67 if (node->max_proc_per_quantum > 1) 68 max_proc_per_quantum = node->max_proc_per_quantum; 69 70 if (process_count > max_proc_per_quantum) 71 *over_subscription |= OVER_SUBSCRIPTION_PROCESS_COUNT; 72 if (compute_queue_count > get_cp_queues_num(pm->dqm)) 73 *over_subscription |= OVER_SUBSCRIPTION_COMPUTE_QUEUE_COUNT; 74 if (gws_queue_count > 1) 75 *over_subscription |= OVER_SUBSCRIPTION_GWS_QUEUE_COUNT; 76 77 if (*over_subscription) 78 dev_dbg(dev, "Over subscribed runlist\n"); 79 80 map_queue_size = pm->pmf->map_queues_size; 81 /* calculate run list ib allocation size */ 82 *rlib_size = process_count * pm->pmf->map_process_size + 83 queue_count * map_queue_size; 84 85 /* 86 * Increase the allocation size in case we need a chained run list 87 * when over subscription 88 */ 89 if (*over_subscription) 90 *rlib_size += pm->pmf->runlist_size; 91 92 dev_dbg(dev, "runlist ib size %d\n", *rlib_size); 93 } 94 95 static int pm_allocate_runlist_ib(struct packet_manager *pm, 96 unsigned int **rl_buffer, 97 uint64_t *rl_gpu_buffer, 98 unsigned int *rl_buffer_size, 99 int *is_over_subscription) 100 { 101 struct kfd_node *node = pm->dqm->dev; 102 struct device *dev = node->adev->dev; 103 int retval; 104 105 if (WARN_ON(pm->allocated)) 106 return -EINVAL; 107 108 pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription); 109 110 mutex_lock(&pm->lock); 111 112 retval = kfd_gtt_sa_allocate(node, *rl_buffer_size, &pm->ib_buffer_obj); 113 114 if (retval) { 115 dev_err(dev, "Failed to allocate runlist IB\n"); 116 goto out; 117 } 118 119 *(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr; 120 *rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr; 121 122 memset(*rl_buffer, 0, *rl_buffer_size); 123 pm->allocated = true; 124 125 out: 126 mutex_unlock(&pm->lock); 127 return retval; 128 } 129 130 static int pm_create_runlist_ib(struct packet_manager *pm, 131 struct list_head *queues, 132 uint64_t *rl_gpu_addr, 133 size_t *rl_size_bytes) 134 { 135 unsigned int alloc_size_bytes; 136 unsigned int *rl_buffer, rl_wptr, i; 137 struct kfd_node *node = pm->dqm->dev; 138 struct device *dev = node->adev->dev; 139 int retval, processes_mapped; 140 struct device_process_node *cur; 141 struct qcm_process_device *qpd; 142 struct queue *q; 143 struct kernel_queue *kq; 144 int is_over_subscription; 145 146 rl_wptr = retval = processes_mapped = 0; 147 148 retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr, 149 &alloc_size_bytes, &is_over_subscription); 150 if (retval) 151 return retval; 152 153 *rl_size_bytes = alloc_size_bytes; 154 pm->ib_size_bytes = alloc_size_bytes; 155 156 dev_dbg(dev, "Building runlist ib process count: %d queues count %d\n", 157 pm->dqm->processes_count, pm->dqm->active_queue_count); 158 159 /* build the run list ib packet */ 160 list_for_each_entry(cur, queues, list) { 161 qpd = cur->qpd; 162 /* build map process packet */ 163 if (processes_mapped >= pm->dqm->processes_count) { 164 dev_dbg(dev, "Not enough space left in runlist IB\n"); 165 pm_release_ib(pm); 166 return -ENOMEM; 167 } 168 169 retval = pm->pmf->map_process(pm, &rl_buffer[rl_wptr], qpd); 170 if (retval) 171 return retval; 172 173 processes_mapped++; 174 inc_wptr(&rl_wptr, pm->pmf->map_process_size, 175 alloc_size_bytes); 176 177 list_for_each_entry(kq, &qpd->priv_queue_list, list) { 178 if (!kq->queue->properties.is_active) 179 continue; 180 181 dev_dbg(dev, 182 "static_queue, mapping kernel q %d, is debug status %d\n", 183 kq->queue->queue, qpd->is_debug); 184 185 retval = pm->pmf->map_queues(pm, 186 &rl_buffer[rl_wptr], 187 kq->queue, 188 qpd->is_debug); 189 if (retval) 190 return retval; 191 192 inc_wptr(&rl_wptr, 193 pm->pmf->map_queues_size, 194 alloc_size_bytes); 195 } 196 197 list_for_each_entry(q, &qpd->queues_list, list) { 198 if (!q->properties.is_active) 199 continue; 200 201 dev_dbg(dev, 202 "static_queue, mapping user queue %d, is debug status %d\n", 203 q->queue, qpd->is_debug); 204 205 retval = pm->pmf->map_queues(pm, 206 &rl_buffer[rl_wptr], 207 q, 208 qpd->is_debug); 209 210 if (retval) 211 return retval; 212 213 inc_wptr(&rl_wptr, 214 pm->pmf->map_queues_size, 215 alloc_size_bytes); 216 } 217 } 218 219 dev_dbg(dev, "Finished map process and queues to runlist\n"); 220 221 if (is_over_subscription) { 222 if (!pm->is_over_subscription) 223 dev_warn(dev, "Runlist is getting oversubscribed due to%s%s%s. Expect reduced ROCm performance.\n", 224 is_over_subscription & OVER_SUBSCRIPTION_PROCESS_COUNT ? 225 " too many processes." : "", 226 is_over_subscription & OVER_SUBSCRIPTION_COMPUTE_QUEUE_COUNT ? 227 " too many queues." : "", 228 is_over_subscription & OVER_SUBSCRIPTION_GWS_QUEUE_COUNT ? 229 " multiple processes using cooperative launch." : ""); 230 231 retval = pm->pmf->runlist(pm, &rl_buffer[rl_wptr], 232 *rl_gpu_addr, 233 alloc_size_bytes / sizeof(uint32_t), 234 true); 235 } 236 pm->is_over_subscription = !!is_over_subscription; 237 238 for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++) 239 pr_debug("0x%2X ", rl_buffer[i]); 240 pr_debug("\n"); 241 242 return retval; 243 } 244 245 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm) 246 { 247 switch (dqm->dev->adev->asic_type) { 248 case CHIP_KAVERI: 249 case CHIP_HAWAII: 250 /* PM4 packet structures on CIK are the same as on VI */ 251 case CHIP_CARRIZO: 252 case CHIP_TONGA: 253 case CHIP_FIJI: 254 case CHIP_POLARIS10: 255 case CHIP_POLARIS11: 256 case CHIP_POLARIS12: 257 case CHIP_VEGAM: 258 pm->pmf = &kfd_vi_pm_funcs; 259 break; 260 default: 261 if (KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 2) || 262 KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 3) || 263 KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 4) || 264 KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 5, 0)) 265 pm->pmf = &kfd_aldebaran_pm_funcs; 266 else if (KFD_GC_VERSION(dqm->dev) >= IP_VERSION(9, 0, 1)) 267 pm->pmf = &kfd_v9_pm_funcs; 268 else { 269 WARN(1, "Unexpected ASIC family %u", 270 dqm->dev->adev->asic_type); 271 return -EINVAL; 272 } 273 } 274 275 pm->dqm = dqm; 276 mutex_init(&pm->lock); 277 pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ); 278 if (!pm->priv_queue) { 279 mutex_destroy(&pm->lock); 280 return -ENOMEM; 281 } 282 pm->allocated = false; 283 284 return 0; 285 } 286 287 void pm_uninit(struct packet_manager *pm) 288 { 289 mutex_destroy(&pm->lock); 290 kernel_queue_uninit(pm->priv_queue); 291 pm->priv_queue = NULL; 292 } 293 294 int pm_send_set_resources(struct packet_manager *pm, 295 struct scheduling_resources *res) 296 { 297 struct kfd_node *node = pm->dqm->dev; 298 struct device *dev = node->adev->dev; 299 uint32_t *buffer, size; 300 int retval = 0; 301 302 size = pm->pmf->set_resources_size; 303 mutex_lock(&pm->lock); 304 kq_acquire_packet_buffer(pm->priv_queue, 305 size / sizeof(uint32_t), 306 (unsigned int **)&buffer); 307 if (!buffer) { 308 dev_err(dev, "Failed to allocate buffer on kernel queue\n"); 309 retval = -ENOMEM; 310 goto out; 311 } 312 313 retval = pm->pmf->set_resources(pm, buffer, res); 314 if (!retval) 315 retval = kq_submit_packet(pm->priv_queue); 316 else 317 kq_rollback_packet(pm->priv_queue); 318 319 out: 320 mutex_unlock(&pm->lock); 321 322 return retval; 323 } 324 325 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues) 326 { 327 uint64_t rl_gpu_ib_addr; 328 uint32_t *rl_buffer; 329 size_t rl_ib_size, packet_size_dwords; 330 int retval; 331 332 retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr, 333 &rl_ib_size); 334 if (retval) 335 goto fail_create_runlist_ib; 336 337 pr_debug("runlist IB address: 0x%llX\n", rl_gpu_ib_addr); 338 339 packet_size_dwords = pm->pmf->runlist_size / sizeof(uint32_t); 340 mutex_lock(&pm->lock); 341 342 retval = kq_acquire_packet_buffer(pm->priv_queue, 343 packet_size_dwords, &rl_buffer); 344 if (retval) 345 goto fail_acquire_packet_buffer; 346 347 retval = pm->pmf->runlist(pm, rl_buffer, rl_gpu_ib_addr, 348 rl_ib_size / sizeof(uint32_t), false); 349 if (retval) 350 goto fail_create_runlist; 351 352 retval = kq_submit_packet(pm->priv_queue); 353 354 mutex_unlock(&pm->lock); 355 356 return retval; 357 358 fail_create_runlist: 359 kq_rollback_packet(pm->priv_queue); 360 fail_acquire_packet_buffer: 361 mutex_unlock(&pm->lock); 362 fail_create_runlist_ib: 363 pm_release_ib(pm); 364 return retval; 365 } 366 367 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address, 368 uint64_t fence_value) 369 { 370 struct kfd_node *node = pm->dqm->dev; 371 struct device *dev = node->adev->dev; 372 uint32_t *buffer, size; 373 int retval = 0; 374 375 if (WARN_ON(!fence_address)) 376 return -EFAULT; 377 378 size = pm->pmf->query_status_size; 379 mutex_lock(&pm->lock); 380 kq_acquire_packet_buffer(pm->priv_queue, 381 size / sizeof(uint32_t), (unsigned int **)&buffer); 382 if (!buffer) { 383 dev_err(dev, "Failed to allocate buffer on kernel queue\n"); 384 retval = -ENOMEM; 385 goto out; 386 } 387 388 retval = pm->pmf->query_status(pm, buffer, fence_address, fence_value); 389 if (!retval) 390 retval = kq_submit_packet(pm->priv_queue); 391 else 392 kq_rollback_packet(pm->priv_queue); 393 394 out: 395 mutex_unlock(&pm->lock); 396 return retval; 397 } 398 399 /* pm_config_dequeue_wait_counts: Configure dequeue timer Wait Counts 400 * by writing to CP_IQ_WAIT_TIME2 registers. 401 * 402 * @cmd: See emum kfd_config_dequeue_wait_counts_cmd definition 403 * @value: Depends on the cmd. This parameter is unused for 404 * KFD_DEQUEUE_WAIT_INIT and KFD_DEQUEUE_WAIT_RESET. For 405 * KFD_DEQUEUE_WAIT_SET_SCH_WAVE it holds value to be set 406 * 407 */ 408 int pm_config_dequeue_wait_counts(struct packet_manager *pm, 409 enum kfd_config_dequeue_wait_counts_cmd cmd, 410 uint32_t value) 411 { 412 struct kfd_node *node = pm->dqm->dev; 413 struct device *dev = node->adev->dev; 414 int retval = 0; 415 uint32_t *buffer, size; 416 417 if (!pm->pmf->config_dequeue_wait_counts || 418 !pm->pmf->config_dequeue_wait_counts_size) 419 return 0; 420 421 if (cmd == KFD_DEQUEUE_WAIT_INIT && (KFD_GC_VERSION(pm->dqm->dev) < IP_VERSION(9, 4, 1) || 422 KFD_GC_VERSION(pm->dqm->dev) >= IP_VERSION(10, 0, 0))) 423 return 0; 424 425 size = pm->pmf->config_dequeue_wait_counts_size; 426 427 mutex_lock(&pm->lock); 428 429 if (size) { 430 kq_acquire_packet_buffer(pm->priv_queue, 431 size / sizeof(uint32_t), 432 (unsigned int **)&buffer); 433 434 if (!buffer) { 435 dev_err(dev, 436 "Failed to allocate buffer on kernel queue\n"); 437 retval = -ENOMEM; 438 goto out; 439 } 440 441 retval = pm->pmf->config_dequeue_wait_counts(pm, buffer, 442 cmd, value); 443 if (!retval) { 444 retval = kq_submit_packet(pm->priv_queue); 445 446 /* If default value is modified, cache that in dqm->wait_times */ 447 if (!retval && cmd == KFD_DEQUEUE_WAIT_INIT) 448 update_dqm_wait_times(pm->dqm); 449 } else { 450 kq_rollback_packet(pm->priv_queue); 451 } 452 } 453 out: 454 mutex_unlock(&pm->lock); 455 return retval; 456 } 457 458 int pm_send_unmap_queue(struct packet_manager *pm, 459 enum kfd_unmap_queues_filter filter, 460 uint32_t filter_param, bool reset) 461 { 462 struct kfd_node *node = pm->dqm->dev; 463 struct device *dev = node->adev->dev; 464 uint32_t *buffer, size; 465 int retval = 0; 466 467 size = pm->pmf->unmap_queues_size; 468 mutex_lock(&pm->lock); 469 kq_acquire_packet_buffer(pm->priv_queue, 470 size / sizeof(uint32_t), (unsigned int **)&buffer); 471 if (!buffer) { 472 dev_err(dev, "Failed to allocate buffer on kernel queue\n"); 473 retval = -ENOMEM; 474 goto out; 475 } 476 477 retval = pm->pmf->unmap_queues(pm, buffer, filter, filter_param, reset); 478 if (!retval) 479 retval = kq_submit_packet(pm->priv_queue); 480 else 481 kq_rollback_packet(pm->priv_queue); 482 483 out: 484 mutex_unlock(&pm->lock); 485 return retval; 486 } 487 488 void pm_release_ib(struct packet_manager *pm) 489 { 490 mutex_lock(&pm->lock); 491 if (pm->allocated) { 492 kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj); 493 pm->allocated = false; 494 } 495 mutex_unlock(&pm->lock); 496 } 497 498 #if defined(CONFIG_DEBUG_FS) 499 500 int pm_debugfs_runlist(struct seq_file *m, void *data) 501 { 502 struct packet_manager *pm = data; 503 504 mutex_lock(&pm->lock); 505 506 if (!pm->allocated) { 507 seq_puts(m, " No active runlist\n"); 508 goto out; 509 } 510 511 seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 32, 4, 512 pm->ib_buffer_obj->cpu_ptr, pm->ib_size_bytes, false); 513 514 out: 515 mutex_unlock(&pm->lock); 516 return 0; 517 } 518 519 int pm_debugfs_hang_hws(struct packet_manager *pm) 520 { 521 struct kfd_node *node = pm->dqm->dev; 522 struct device *dev = node->adev->dev; 523 uint32_t *buffer, size; 524 int r = 0; 525 526 if (!pm->priv_queue) 527 return -EAGAIN; 528 529 size = pm->pmf->query_status_size; 530 mutex_lock(&pm->lock); 531 kq_acquire_packet_buffer(pm->priv_queue, 532 size / sizeof(uint32_t), (unsigned int **)&buffer); 533 if (!buffer) { 534 dev_err(dev, "Failed to allocate buffer on kernel queue\n"); 535 r = -ENOMEM; 536 goto out; 537 } 538 memset(buffer, 0x55, size); 539 kq_submit_packet(pm->priv_queue); 540 541 dev_info(dev, "Submitting %x %x %x %x %x %x %x to HIQ to hang the HWS.", 542 buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], 543 buffer[5], buffer[6]); 544 out: 545 mutex_unlock(&pm->lock); 546 return r; 547 } 548 549 550 #endif 551