1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2024, Advanced Micro Devices, Inc. 4 */ 5 6 #include <drm/amdxdna_accel.h> 7 #include <drm/drm_device.h> 8 #include <drm/drm_gem.h> 9 #include <drm/drm_gem_shmem_helper.h> 10 #include <drm/drm_print.h> 11 #include <drm/drm_syncobj.h> 12 #include <linux/hmm.h> 13 #include <linux/types.h> 14 #include <linux/xarray.h> 15 #include <trace/events/amdxdna.h> 16 17 #include "aie2_msg_priv.h" 18 #include "aie2_pci.h" 19 #include "aie2_solver.h" 20 #include "amdxdna_ctx.h" 21 #include "amdxdna_gem.h" 22 #include "amdxdna_mailbox.h" 23 #include "amdxdna_pci_drv.h" 24 #include "amdxdna_pm.h" 25 26 static bool force_cmdlist = true; 27 module_param(force_cmdlist, bool, 0600); 28 MODULE_PARM_DESC(force_cmdlist, "Force use command list (Default true)"); 29 30 #define HWCTX_MAX_TIMEOUT 60000 /* milliseconds */ 31 32 static void aie2_job_release(struct kref *ref) 33 { 34 struct amdxdna_sched_job *job; 35 36 job = container_of(ref, struct amdxdna_sched_job, refcnt); 37 amdxdna_sched_job_cleanup(job); 38 atomic64_inc(&job->hwctx->job_free_cnt); 39 wake_up(&job->hwctx->priv->job_free_wq); 40 if (job->out_fence) 41 dma_fence_put(job->out_fence); 42 kfree(job); 43 } 44 45 static void aie2_job_put(struct amdxdna_sched_job *job) 46 { 47 kref_put(&job->refcnt, aie2_job_release); 48 } 49 50 /* The bad_job is used in aie2_sched_job_timedout, otherwise, set it to NULL */ 51 static void aie2_hwctx_stop(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hwctx, 52 struct drm_sched_job *bad_job) 53 { 54 drm_sched_stop(&hwctx->priv->sched, bad_job); 55 aie2_destroy_context(xdna->dev_handle, hwctx); 56 drm_sched_start(&hwctx->priv->sched, 0); 57 } 58 59 static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hwctx) 60 { 61 struct amdxdna_gem_obj *heap = hwctx->priv->heap; 62 int ret; 63 64 ret = aie2_create_context(xdna->dev_handle, hwctx); 65 if (ret) { 66 XDNA_ERR(xdna, "Create hwctx failed, ret %d", ret); 67 goto out; 68 } 69 70 ret = aie2_map_host_buf(xdna->dev_handle, hwctx->fw_ctx_id, 71 heap->mem.userptr, heap->mem.size); 72 if (ret) { 73 XDNA_ERR(xdna, "Map host buf failed, ret %d", ret); 74 goto out; 75 } 76 77 ret = aie2_config_cu(hwctx, NULL); 78 if (ret) { 79 XDNA_ERR(xdna, "Config cu failed, ret %d", ret); 80 goto out; 81 } 82 83 out: 84 XDNA_DBG(xdna, "%s restarted, ret %d", hwctx->name, ret); 85 return ret; 86 } 87 88 static struct dma_fence *aie2_cmd_get_out_fence(struct amdxdna_hwctx *hwctx, u64 seq) 89 { 90 struct dma_fence *fence, *out_fence = NULL; 91 int ret; 92 93 fence = drm_syncobj_fence_get(hwctx->priv->syncobj); 94 if (!fence) 95 return NULL; 96 97 ret = dma_fence_chain_find_seqno(&fence, seq); 98 if (ret) 99 goto out; 100 101 out_fence = dma_fence_get(dma_fence_chain_contained(fence)); 102 103 out: 104 dma_fence_put(fence); 105 return out_fence; 106 } 107 108 static void aie2_hwctx_wait_for_idle(struct amdxdna_hwctx *hwctx) 109 { 110 struct dma_fence *fence; 111 112 fence = aie2_cmd_get_out_fence(hwctx, hwctx->priv->seq - 1); 113 if (!fence) 114 return; 115 116 /* Wait up to 2 seconds for fw to finish all pending requests */ 117 dma_fence_wait_timeout(fence, false, msecs_to_jiffies(2000)); 118 dma_fence_put(fence); 119 } 120 121 static int aie2_hwctx_suspend_cb(struct amdxdna_hwctx *hwctx, void *arg) 122 { 123 struct amdxdna_dev *xdna = hwctx->client->xdna; 124 125 aie2_hwctx_wait_for_idle(hwctx); 126 aie2_hwctx_stop(xdna, hwctx, NULL); 127 128 return 0; 129 } 130 131 void aie2_hwctx_suspend(struct amdxdna_client *client) 132 { 133 struct amdxdna_dev *xdna = client->xdna; 134 135 /* 136 * Command timeout is unlikely. But if it happens, it doesn't 137 * break the system. aie2_hwctx_stop() will destroy mailbox 138 * and abort all commands. 139 */ 140 drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock)); 141 amdxdna_hwctx_walk(client, NULL, aie2_hwctx_suspend_cb); 142 } 143 144 static int aie2_hwctx_resume_cb(struct amdxdna_hwctx *hwctx, void *arg) 145 { 146 struct amdxdna_dev *xdna = hwctx->client->xdna; 147 148 return aie2_hwctx_restart(xdna, hwctx); 149 } 150 151 int aie2_hwctx_resume(struct amdxdna_client *client) 152 { 153 /* 154 * The resume path cannot guarantee that mailbox channel can be 155 * regenerated. If this happen, when submit message to this 156 * mailbox channel, error will return. 157 */ 158 return amdxdna_hwctx_walk(client, NULL, aie2_hwctx_resume_cb); 159 } 160 161 static void 162 aie2_sched_notify(struct amdxdna_sched_job *job) 163 { 164 struct dma_fence *fence = job->fence; 165 166 trace_xdna_job(&job->base, job->hwctx->name, "signaled fence", job->seq); 167 168 amdxdna_pm_suspend_put(job->hwctx->client->xdna); 169 job->hwctx->priv->completed++; 170 dma_fence_signal(fence); 171 172 up(&job->hwctx->priv->job_sem); 173 job->job_done = true; 174 mmput_async(job->mm); 175 aie2_job_put(job); 176 } 177 178 static int 179 aie2_sched_resp_handler(void *handle, void __iomem *data, size_t size) 180 { 181 struct amdxdna_sched_job *job = handle; 182 struct amdxdna_gem_obj *cmd_abo; 183 int ret = 0; 184 u32 status; 185 186 cmd_abo = job->cmd_bo; 187 188 if (unlikely(job->job_timeout)) { 189 amdxdna_cmd_set_error(cmd_abo, job, 0, ERT_CMD_STATE_TIMEOUT); 190 ret = -EINVAL; 191 goto out; 192 } 193 194 if (unlikely(!data) || unlikely(size != sizeof(u32))) { 195 amdxdna_cmd_set_error(cmd_abo, job, 0, ERT_CMD_STATE_ABORT); 196 ret = -EINVAL; 197 goto out; 198 } 199 200 status = readl(data); 201 XDNA_DBG(job->hwctx->client->xdna, "Resp status 0x%x", status); 202 if (status == AIE2_STATUS_SUCCESS) 203 amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_COMPLETED); 204 else 205 amdxdna_cmd_set_error(cmd_abo, job, 0, ERT_CMD_STATE_ERROR); 206 207 out: 208 aie2_sched_notify(job); 209 return ret; 210 } 211 212 static int 213 aie2_sched_drvcmd_resp_handler(void *handle, void __iomem *data, size_t size) 214 { 215 struct amdxdna_sched_job *job = handle; 216 int ret = 0; 217 218 if (unlikely(!data)) 219 goto out; 220 221 if (unlikely(size != sizeof(u32))) { 222 ret = -EINVAL; 223 goto out; 224 } 225 226 job->drv_cmd->result = readl(data); 227 228 out: 229 aie2_sched_notify(job); 230 return ret; 231 } 232 233 static int 234 aie2_sched_cmdlist_resp_handler(void *handle, void __iomem *data, size_t size) 235 { 236 struct amdxdna_sched_job *job = handle; 237 struct amdxdna_gem_obj *cmd_abo; 238 struct amdxdna_dev *xdna; 239 u32 fail_cmd_status; 240 u32 fail_cmd_idx; 241 u32 cmd_status; 242 int ret = 0; 243 244 cmd_abo = job->cmd_bo; 245 246 if (unlikely(job->job_timeout)) { 247 amdxdna_cmd_set_error(cmd_abo, job, 0, ERT_CMD_STATE_TIMEOUT); 248 ret = -EINVAL; 249 goto out; 250 } 251 252 if (unlikely(!data) || unlikely(size != sizeof(u32) * 3)) { 253 amdxdna_cmd_set_error(cmd_abo, job, 0, ERT_CMD_STATE_ABORT); 254 ret = -EINVAL; 255 goto out; 256 } 257 258 cmd_status = readl(data + offsetof(struct cmd_chain_resp, status)); 259 xdna = job->hwctx->client->xdna; 260 XDNA_DBG(xdna, "Status 0x%x", cmd_status); 261 if (cmd_status == AIE2_STATUS_SUCCESS) { 262 amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_COMPLETED); 263 goto out; 264 } 265 266 /* Slow path to handle error, read from ringbuf on BAR */ 267 fail_cmd_idx = readl(data + offsetof(struct cmd_chain_resp, fail_cmd_idx)); 268 fail_cmd_status = readl(data + offsetof(struct cmd_chain_resp, fail_cmd_status)); 269 XDNA_DBG(xdna, "Failed cmd idx %d, status 0x%x", 270 fail_cmd_idx, fail_cmd_status); 271 272 if (fail_cmd_status == AIE2_STATUS_SUCCESS) { 273 amdxdna_cmd_set_error(cmd_abo, job, fail_cmd_idx, ERT_CMD_STATE_ABORT); 274 ret = -EINVAL; 275 } else { 276 amdxdna_cmd_set_error(cmd_abo, job, fail_cmd_idx, ERT_CMD_STATE_ERROR); 277 } 278 279 out: 280 aie2_sched_notify(job); 281 return ret; 282 } 283 284 static struct dma_fence * 285 aie2_sched_job_run(struct drm_sched_job *sched_job) 286 { 287 struct amdxdna_sched_job *job = drm_job_to_xdna_job(sched_job); 288 struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; 289 struct amdxdna_hwctx *hwctx = job->hwctx; 290 struct dma_fence *fence; 291 int ret; 292 293 ret = amdxdna_pm_resume_get(hwctx->client->xdna); 294 if (ret) 295 return NULL; 296 297 if (!hwctx->priv->mbox_chann) { 298 amdxdna_pm_suspend_put(hwctx->client->xdna); 299 return NULL; 300 } 301 302 if (!mmget_not_zero(job->mm)) { 303 amdxdna_pm_suspend_put(hwctx->client->xdna); 304 return ERR_PTR(-ESRCH); 305 } 306 307 kref_get(&job->refcnt); 308 fence = dma_fence_get(job->fence); 309 310 if (job->drv_cmd) { 311 switch (job->drv_cmd->opcode) { 312 case SYNC_DEBUG_BO: 313 ret = aie2_sync_bo(hwctx, job, aie2_sched_drvcmd_resp_handler); 314 break; 315 case ATTACH_DEBUG_BO: 316 ret = aie2_config_debug_bo(hwctx, job, aie2_sched_drvcmd_resp_handler); 317 break; 318 default: 319 ret = -EINVAL; 320 break; 321 } 322 goto out; 323 } 324 325 amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_NEW); 326 327 if (amdxdna_cmd_get_op(cmd_abo) == ERT_CMD_CHAIN) 328 ret = aie2_cmdlist_multi_execbuf(hwctx, job, aie2_sched_cmdlist_resp_handler); 329 else if (force_cmdlist) 330 ret = aie2_cmdlist_single_execbuf(hwctx, job, aie2_sched_cmdlist_resp_handler); 331 else 332 ret = aie2_execbuf(hwctx, job, aie2_sched_resp_handler); 333 334 out: 335 if (ret) { 336 amdxdna_pm_suspend_put(hwctx->client->xdna); 337 dma_fence_put(job->fence); 338 aie2_job_put(job); 339 mmput(job->mm); 340 fence = ERR_PTR(ret); 341 } 342 trace_xdna_job(sched_job, hwctx->name, "sent to device", job->seq); 343 344 return fence; 345 } 346 347 static void aie2_sched_job_free(struct drm_sched_job *sched_job) 348 { 349 struct amdxdna_sched_job *job = drm_job_to_xdna_job(sched_job); 350 struct amdxdna_hwctx *hwctx = job->hwctx; 351 352 trace_xdna_job(sched_job, hwctx->name, "job free", job->seq); 353 if (!job->job_done) 354 up(&hwctx->priv->job_sem); 355 356 drm_sched_job_cleanup(sched_job); 357 aie2_job_put(job); 358 } 359 360 static enum drm_gpu_sched_stat 361 aie2_sched_job_timedout(struct drm_sched_job *sched_job) 362 { 363 struct amdxdna_sched_job *job = drm_job_to_xdna_job(sched_job); 364 struct amdxdna_hwctx *hwctx = job->hwctx; 365 struct amdxdna_dev *xdna; 366 367 xdna = hwctx->client->xdna; 368 trace_xdna_job(sched_job, hwctx->name, "job timedout", job->seq); 369 job->job_timeout = true; 370 mutex_lock(&xdna->dev_lock); 371 aie2_hwctx_stop(xdna, hwctx, sched_job); 372 373 aie2_hwctx_restart(xdna, hwctx); 374 mutex_unlock(&xdna->dev_lock); 375 376 return DRM_GPU_SCHED_STAT_RESET; 377 } 378 379 static const struct drm_sched_backend_ops sched_ops = { 380 .run_job = aie2_sched_job_run, 381 .free_job = aie2_sched_job_free, 382 .timedout_job = aie2_sched_job_timedout, 383 }; 384 385 static int aie2_hwctx_col_list(struct amdxdna_hwctx *hwctx) 386 { 387 struct amdxdna_dev *xdna = hwctx->client->xdna; 388 struct amdxdna_dev_hdl *ndev; 389 int start, end, first, last; 390 u32 width = 1, entries = 0; 391 int i; 392 393 if (!hwctx->num_tiles) { 394 XDNA_ERR(xdna, "Number of tiles is zero"); 395 return -EINVAL; 396 } 397 398 ndev = xdna->dev_handle; 399 if (unlikely(!ndev->metadata.core.row_count)) { 400 XDNA_WARN(xdna, "Core tile row count is zero"); 401 return -EINVAL; 402 } 403 404 hwctx->num_col = hwctx->num_tiles / ndev->metadata.core.row_count; 405 if (!hwctx->num_col || hwctx->num_col > ndev->total_col) { 406 XDNA_ERR(xdna, "Invalid num_col %d", hwctx->num_col); 407 return -EINVAL; 408 } 409 410 if (ndev->priv->col_align == COL_ALIGN_NATURE) 411 width = hwctx->num_col; 412 413 /* 414 * In range [start, end], find out columns that is multiple of width. 415 * 'first' is the first column, 416 * 'last' is the last column, 417 * 'entries' is the total number of columns. 418 */ 419 start = xdna->dev_info->first_col; 420 end = ndev->total_col - hwctx->num_col; 421 if (start > 0 && end == 0) { 422 XDNA_DBG(xdna, "Force start from col 0"); 423 start = 0; 424 } 425 first = start + (width - start % width) % width; 426 last = end - end % width; 427 if (last >= first) 428 entries = (last - first) / width + 1; 429 XDNA_DBG(xdna, "start %d end %d first %d last %d", 430 start, end, first, last); 431 432 if (unlikely(!entries)) { 433 XDNA_ERR(xdna, "Start %d end %d width %d", 434 start, end, width); 435 return -EINVAL; 436 } 437 438 hwctx->col_list = kmalloc_array(entries, sizeof(*hwctx->col_list), GFP_KERNEL); 439 if (!hwctx->col_list) 440 return -ENOMEM; 441 442 hwctx->col_list_len = entries; 443 hwctx->col_list[0] = first; 444 for (i = 1; i < entries; i++) 445 hwctx->col_list[i] = hwctx->col_list[i - 1] + width; 446 447 print_hex_dump_debug("col_list: ", DUMP_PREFIX_OFFSET, 16, 4, hwctx->col_list, 448 entries * sizeof(*hwctx->col_list), false); 449 return 0; 450 } 451 452 static int aie2_alloc_resource(struct amdxdna_hwctx *hwctx) 453 { 454 struct amdxdna_dev *xdna = hwctx->client->xdna; 455 struct alloc_requests *xrs_req; 456 int ret; 457 458 if (AIE2_FEATURE_ON(xdna->dev_handle, AIE2_TEMPORAL_ONLY)) { 459 hwctx->num_unused_col = xdna->dev_handle->total_col - hwctx->num_col; 460 hwctx->num_col = xdna->dev_handle->total_col; 461 return aie2_create_context(xdna->dev_handle, hwctx); 462 } 463 464 xrs_req = kzalloc_obj(*xrs_req); 465 if (!xrs_req) 466 return -ENOMEM; 467 468 xrs_req->cdo.start_cols = hwctx->col_list; 469 xrs_req->cdo.cols_len = hwctx->col_list_len; 470 xrs_req->cdo.ncols = hwctx->num_col; 471 xrs_req->cdo.qos_cap.opc = hwctx->max_opc; 472 473 xrs_req->rqos.gops = hwctx->qos.gops; 474 xrs_req->rqos.fps = hwctx->qos.fps; 475 xrs_req->rqos.dma_bw = hwctx->qos.dma_bandwidth; 476 xrs_req->rqos.latency = hwctx->qos.latency; 477 xrs_req->rqos.exec_time = hwctx->qos.frame_exec_time; 478 xrs_req->rqos.priority = hwctx->qos.priority; 479 480 xrs_req->rid = (uintptr_t)hwctx; 481 482 ret = xrs_allocate_resource(xdna->xrs_hdl, xrs_req, hwctx); 483 if (ret) 484 XDNA_ERR(xdna, "Allocate AIE resource failed, ret %d", ret); 485 486 kfree(xrs_req); 487 return ret; 488 } 489 490 static void aie2_release_resource(struct amdxdna_hwctx *hwctx) 491 { 492 struct amdxdna_dev *xdna = hwctx->client->xdna; 493 int ret; 494 495 if (AIE2_FEATURE_ON(xdna->dev_handle, AIE2_TEMPORAL_ONLY)) { 496 ret = aie2_destroy_context(xdna->dev_handle, hwctx); 497 if (ret && ret != -ENODEV) 498 XDNA_ERR(xdna, "Destroy temporal only context failed, ret %d", ret); 499 } else { 500 ret = xrs_release_resource(xdna->xrs_hdl, (uintptr_t)hwctx); 501 if (ret) 502 XDNA_ERR(xdna, "Release AIE resource failed, ret %d", ret); 503 } 504 } 505 506 static int aie2_ctx_syncobj_create(struct amdxdna_hwctx *hwctx) 507 { 508 struct amdxdna_dev *xdna = hwctx->client->xdna; 509 struct drm_file *filp = hwctx->client->filp; 510 struct drm_syncobj *syncobj; 511 u32 hdl; 512 int ret; 513 514 hwctx->syncobj_hdl = AMDXDNA_INVALID_FENCE_HANDLE; 515 516 ret = drm_syncobj_create(&syncobj, 0, NULL); 517 if (ret) { 518 XDNA_ERR(xdna, "Create ctx syncobj failed, ret %d", ret); 519 return ret; 520 } 521 ret = drm_syncobj_get_handle(filp, syncobj, &hdl); 522 if (ret) { 523 drm_syncobj_put(syncobj); 524 XDNA_ERR(xdna, "Create ctx syncobj handle failed, ret %d", ret); 525 return ret; 526 } 527 hwctx->priv->syncobj = syncobj; 528 hwctx->syncobj_hdl = hdl; 529 530 return 0; 531 } 532 533 static void aie2_ctx_syncobj_destroy(struct amdxdna_hwctx *hwctx) 534 { 535 /* 536 * The syncobj_hdl is owned by user space and will be cleaned up 537 * separately. 538 */ 539 drm_syncobj_put(hwctx->priv->syncobj); 540 } 541 542 int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) 543 { 544 struct amdxdna_client *client = hwctx->client; 545 struct amdxdna_dev *xdna = client->xdna; 546 const struct drm_sched_init_args args = { 547 .ops = &sched_ops, 548 .num_rqs = DRM_SCHED_PRIORITY_COUNT, 549 .credit_limit = HWCTX_MAX_CMDS, 550 .timeout = msecs_to_jiffies(HWCTX_MAX_TIMEOUT), 551 .name = "amdxdna_js", 552 .dev = xdna->ddev.dev, 553 }; 554 struct drm_gpu_scheduler *sched; 555 struct amdxdna_hwctx_priv *priv; 556 struct amdxdna_gem_obj *heap; 557 int i, ret; 558 559 priv = kzalloc_obj(*hwctx->priv); 560 if (!priv) 561 return -ENOMEM; 562 hwctx->priv = priv; 563 564 mutex_lock(&client->mm_lock); 565 heap = client->dev_heap; 566 if (!heap) { 567 XDNA_ERR(xdna, "The client dev heap object not exist"); 568 mutex_unlock(&client->mm_lock); 569 ret = -ENOENT; 570 goto free_priv; 571 } 572 drm_gem_object_get(to_gobj(heap)); 573 mutex_unlock(&client->mm_lock); 574 priv->heap = heap; 575 sema_init(&priv->job_sem, HWCTX_MAX_CMDS); 576 577 ret = amdxdna_gem_pin(heap); 578 if (ret) { 579 XDNA_ERR(xdna, "Dev heap pin failed, ret %d", ret); 580 goto put_heap; 581 } 582 583 for (i = 0; i < ARRAY_SIZE(priv->cmd_buf); i++) { 584 struct amdxdna_gem_obj *abo; 585 struct amdxdna_drm_create_bo args = { 586 .flags = 0, 587 .type = AMDXDNA_BO_DEV, 588 .vaddr = 0, 589 .size = MAX_CHAIN_CMDBUF_SIZE, 590 }; 591 592 abo = amdxdna_drm_alloc_dev_bo(&xdna->ddev, &args, client->filp); 593 if (IS_ERR(abo)) { 594 ret = PTR_ERR(abo); 595 goto free_cmd_bufs; 596 } 597 598 XDNA_DBG(xdna, "Command buf %d addr 0x%llx size 0x%lx", 599 i, abo->mem.dev_addr, abo->mem.size); 600 priv->cmd_buf[i] = abo; 601 } 602 603 sched = &priv->sched; 604 mutex_init(&priv->io_lock); 605 606 fs_reclaim_acquire(GFP_KERNEL); 607 might_lock(&priv->io_lock); 608 fs_reclaim_release(GFP_KERNEL); 609 610 ret = drm_sched_init(sched, &args); 611 if (ret) { 612 XDNA_ERR(xdna, "Failed to init DRM scheduler. ret %d", ret); 613 goto free_cmd_bufs; 614 } 615 616 ret = drm_sched_entity_init(&priv->entity, DRM_SCHED_PRIORITY_NORMAL, 617 &sched, 1, NULL); 618 if (ret) { 619 XDNA_ERR(xdna, "Failed to initial sched entiry. ret %d", ret); 620 goto free_sched; 621 } 622 623 ret = aie2_hwctx_col_list(hwctx); 624 if (ret) { 625 XDNA_ERR(xdna, "Create col list failed, ret %d", ret); 626 goto free_entity; 627 } 628 629 ret = amdxdna_pm_resume_get_locked(xdna); 630 if (ret) 631 goto free_col_list; 632 633 ret = aie2_alloc_resource(hwctx); 634 if (ret) { 635 XDNA_ERR(xdna, "Alloc hw resource failed, ret %d", ret); 636 goto suspend_put; 637 } 638 639 ret = aie2_map_host_buf(xdna->dev_handle, hwctx->fw_ctx_id, 640 heap->mem.userptr, heap->mem.size); 641 if (ret) { 642 XDNA_ERR(xdna, "Map host buffer failed, ret %d", ret); 643 goto release_resource; 644 } 645 646 ret = aie2_ctx_syncobj_create(hwctx); 647 if (ret) { 648 XDNA_ERR(xdna, "Create syncobj failed, ret %d", ret); 649 goto release_resource; 650 } 651 amdxdna_pm_suspend_put(xdna); 652 653 init_waitqueue_head(&priv->job_free_wq); 654 655 XDNA_DBG(xdna, "hwctx %s init completed", hwctx->name); 656 657 return 0; 658 659 release_resource: 660 aie2_release_resource(hwctx); 661 suspend_put: 662 amdxdna_pm_suspend_put(xdna); 663 free_col_list: 664 kfree(hwctx->col_list); 665 free_entity: 666 drm_sched_entity_destroy(&priv->entity); 667 free_sched: 668 drm_sched_fini(&priv->sched); 669 free_cmd_bufs: 670 for (i = 0; i < ARRAY_SIZE(priv->cmd_buf); i++) { 671 if (!priv->cmd_buf[i]) 672 continue; 673 drm_gem_object_put(to_gobj(priv->cmd_buf[i])); 674 } 675 amdxdna_gem_unpin(heap); 676 put_heap: 677 drm_gem_object_put(to_gobj(heap)); 678 free_priv: 679 kfree(priv); 680 return ret; 681 } 682 683 void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx) 684 { 685 struct amdxdna_dev *xdna; 686 int idx; 687 688 xdna = hwctx->client->xdna; 689 690 XDNA_DBG(xdna, "%s sequence number %lld", hwctx->name, hwctx->priv->seq); 691 aie2_hwctx_wait_for_idle(hwctx); 692 693 /* Request fw to destroy hwctx and cancel the rest pending requests */ 694 drm_sched_stop(&hwctx->priv->sched, NULL); 695 aie2_release_resource(hwctx); 696 drm_sched_start(&hwctx->priv->sched, 0); 697 698 mutex_unlock(&xdna->dev_lock); 699 drm_sched_entity_destroy(&hwctx->priv->entity); 700 701 /* Wait for all submitted jobs to be completed or canceled */ 702 wait_event(hwctx->priv->job_free_wq, 703 atomic64_read(&hwctx->job_submit_cnt) == 704 atomic64_read(&hwctx->job_free_cnt)); 705 mutex_lock(&xdna->dev_lock); 706 707 drm_sched_fini(&hwctx->priv->sched); 708 aie2_ctx_syncobj_destroy(hwctx); 709 710 for (idx = 0; idx < ARRAY_SIZE(hwctx->priv->cmd_buf); idx++) 711 drm_gem_object_put(to_gobj(hwctx->priv->cmd_buf[idx])); 712 amdxdna_gem_unpin(hwctx->priv->heap); 713 drm_gem_object_put(to_gobj(hwctx->priv->heap)); 714 715 mutex_destroy(&hwctx->priv->io_lock); 716 kfree(hwctx->col_list); 717 kfree(hwctx->priv); 718 kfree(hwctx->cus); 719 } 720 721 static int aie2_config_cu_resp_handler(void *handle, void __iomem *data, size_t size) 722 { 723 struct amdxdna_hwctx *hwctx = handle; 724 725 amdxdna_pm_suspend_put(hwctx->client->xdna); 726 return 0; 727 } 728 729 static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size) 730 { 731 struct amdxdna_hwctx_param_config_cu *config = buf; 732 struct amdxdna_dev *xdna = hwctx->client->xdna; 733 u32 total_size; 734 int ret; 735 736 XDNA_DBG(xdna, "Config %d CU to %s", config->num_cus, hwctx->name); 737 if (XDNA_MBZ_DBG(xdna, config->pad, sizeof(config->pad))) 738 return -EINVAL; 739 740 if (hwctx->cus) { 741 XDNA_ERR(xdna, "Not support re-config CU"); 742 return -EINVAL; 743 } 744 745 if (!config->num_cus) { 746 XDNA_ERR(xdna, "Number of CU is zero"); 747 return -EINVAL; 748 } 749 750 total_size = struct_size(config, cu_configs, config->num_cus); 751 if (total_size > size) { 752 XDNA_ERR(xdna, "CU config larger than size"); 753 return -EINVAL; 754 } 755 756 hwctx->cus = kmemdup(config, total_size, GFP_KERNEL); 757 if (!hwctx->cus) 758 return -ENOMEM; 759 760 ret = amdxdna_pm_resume_get_locked(xdna); 761 if (ret) 762 goto free_cus; 763 764 ret = aie2_config_cu(hwctx, aie2_config_cu_resp_handler); 765 if (ret) { 766 XDNA_ERR(xdna, "Config CU to firmware failed, ret %d", ret); 767 goto pm_suspend_put; 768 } 769 770 wmb(); /* To avoid locking in command submit when check status */ 771 772 return 0; 773 774 pm_suspend_put: 775 amdxdna_pm_suspend_put(xdna); 776 free_cus: 777 kfree(hwctx->cus); 778 hwctx->cus = NULL; 779 return ret; 780 } 781 782 static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq) 783 { 784 struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq); 785 786 if (!out_fence) { 787 XDNA_ERR(hwctx->client->xdna, "Failed to get fence"); 788 return; 789 } 790 791 dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT); 792 dma_fence_put(out_fence); 793 } 794 795 static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, u32 bo_hdl, 796 bool attach) 797 { 798 struct amdxdna_client *client = hwctx->client; 799 struct amdxdna_dev *xdna = client->xdna; 800 struct amdxdna_drv_cmd cmd = { 0 }; 801 struct amdxdna_gem_obj *abo; 802 u64 seq; 803 int ret; 804 805 abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV); 806 if (!abo) { 807 XDNA_ERR(xdna, "Get bo %d failed", bo_hdl); 808 return -EINVAL; 809 } 810 811 if (attach) { 812 if (abo->assigned_hwctx != AMDXDNA_INVALID_CTX_HANDLE) { 813 ret = -EBUSY; 814 goto put_obj; 815 } 816 cmd.opcode = ATTACH_DEBUG_BO; 817 } else { 818 if (abo->assigned_hwctx != hwctx->id) { 819 ret = -EINVAL; 820 goto put_obj; 821 } 822 cmd.opcode = DETACH_DEBUG_BO; 823 } 824 825 ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE, 826 &bo_hdl, 1, hwctx->id, &seq); 827 if (ret) { 828 XDNA_ERR(xdna, "Submit command failed"); 829 goto put_obj; 830 } 831 832 aie2_cmd_wait(hwctx, seq); 833 if (cmd.result) { 834 XDNA_ERR(xdna, "Response failure 0x%x", cmd.result); 835 goto put_obj; 836 } 837 838 if (attach) 839 abo->assigned_hwctx = hwctx->id; 840 else 841 abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE; 842 843 XDNA_DBG(xdna, "Config debug BO %d to %s", bo_hdl, hwctx->name); 844 845 put_obj: 846 amdxdna_gem_put_obj(abo); 847 return ret; 848 } 849 850 int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size) 851 { 852 struct amdxdna_dev *xdna = hwctx->client->xdna; 853 854 drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock)); 855 switch (type) { 856 case DRM_AMDXDNA_HWCTX_CONFIG_CU: 857 return aie2_hwctx_cu_config(hwctx, buf, size); 858 case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF: 859 return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, true); 860 case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF: 861 return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, false); 862 default: 863 XDNA_DBG(xdna, "Not supported type %d", type); 864 return -EOPNOTSUPP; 865 } 866 } 867 868 int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl) 869 { 870 struct amdxdna_client *client = hwctx->client; 871 struct amdxdna_dev *xdna = client->xdna; 872 struct amdxdna_drv_cmd cmd = { 0 }; 873 u64 seq; 874 int ret; 875 876 cmd.opcode = SYNC_DEBUG_BO; 877 ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE, 878 &debug_bo_hdl, 1, hwctx->id, &seq); 879 if (ret) { 880 XDNA_ERR(xdna, "Submit command failed"); 881 return ret; 882 } 883 884 aie2_cmd_wait(hwctx, seq); 885 if (cmd.result) { 886 XDNA_ERR(xdna, "Response failure 0x%x", cmd.result); 887 return -EINVAL; 888 } 889 890 return 0; 891 } 892 893 static int aie2_populate_range(struct amdxdna_gem_obj *abo) 894 { 895 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 896 struct amdxdna_umap *mapp; 897 unsigned long timeout; 898 struct mm_struct *mm; 899 bool found; 900 int ret; 901 902 timeout = jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); 903 again: 904 found = false; 905 down_write(&xdna->notifier_lock); 906 list_for_each_entry(mapp, &abo->mem.umap_list, node) { 907 if (mapp->invalid) { 908 found = true; 909 break; 910 } 911 } 912 913 if (!found) { 914 abo->mem.map_invalid = false; 915 up_write(&xdna->notifier_lock); 916 return 0; 917 } 918 kref_get(&mapp->refcnt); 919 up_write(&xdna->notifier_lock); 920 921 XDNA_DBG(xdna, "populate memory range %lx %lx", 922 mapp->vma->vm_start, mapp->vma->vm_end); 923 mm = mapp->notifier.mm; 924 if (!mmget_not_zero(mm)) { 925 amdxdna_umap_put(mapp); 926 return -EFAULT; 927 } 928 929 mapp->range.notifier_seq = mmu_interval_read_begin(&mapp->notifier); 930 mmap_read_lock(mm); 931 ret = hmm_range_fault(&mapp->range); 932 mmap_read_unlock(mm); 933 if (ret) { 934 if (time_after(jiffies, timeout)) { 935 ret = -ETIME; 936 goto put_mm; 937 } 938 939 if (ret == -EBUSY) { 940 amdxdna_umap_put(mapp); 941 goto again; 942 } 943 944 goto put_mm; 945 } 946 947 down_write(&xdna->notifier_lock); 948 if (mmu_interval_read_retry(&mapp->notifier, mapp->range.notifier_seq)) { 949 up_write(&xdna->notifier_lock); 950 amdxdna_umap_put(mapp); 951 goto again; 952 } 953 mapp->invalid = false; 954 up_write(&xdna->notifier_lock); 955 amdxdna_umap_put(mapp); 956 goto again; 957 958 put_mm: 959 amdxdna_umap_put(mapp); 960 mmput(mm); 961 return ret; 962 } 963 964 int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq) 965 { 966 struct amdxdna_dev *xdna = hwctx->client->xdna; 967 struct ww_acquire_ctx acquire_ctx; 968 struct dma_fence_chain *chain; 969 struct amdxdna_gem_obj *abo; 970 unsigned long timeout = 0; 971 int ret, i; 972 973 ret = down_interruptible(&hwctx->priv->job_sem); 974 if (ret) { 975 XDNA_ERR(xdna, "Grab job sem failed, ret %d", ret); 976 return ret; 977 } 978 979 chain = dma_fence_chain_alloc(); 980 if (!chain) { 981 XDNA_ERR(xdna, "Alloc fence chain failed"); 982 ret = -ENOMEM; 983 goto up_sem; 984 } 985 986 ret = drm_sched_job_init(&job->base, &hwctx->priv->entity, 1, hwctx, 987 hwctx->client->filp->client_id); 988 if (ret) { 989 XDNA_ERR(xdna, "DRM job init failed, ret %d", ret); 990 goto free_chain; 991 } 992 993 retry: 994 ret = drm_gem_lock_reservations(job->bos, job->bo_cnt, &acquire_ctx); 995 if (ret) { 996 XDNA_WARN(xdna, "Failed to lock BOs, ret %d", ret); 997 goto cleanup_job; 998 } 999 1000 for (i = 0; i < job->bo_cnt; i++) { 1001 ret = dma_resv_reserve_fences(job->bos[i]->resv, 1); 1002 if (ret) { 1003 XDNA_WARN(xdna, "Failed to reserve fences %d", ret); 1004 drm_gem_unlock_reservations(job->bos, job->bo_cnt, &acquire_ctx); 1005 goto cleanup_job; 1006 } 1007 } 1008 1009 down_read(&xdna->notifier_lock); 1010 for (i = 0; i < job->bo_cnt; i++) { 1011 abo = to_xdna_obj(job->bos[i]); 1012 if (abo->mem.map_invalid) { 1013 up_read(&xdna->notifier_lock); 1014 drm_gem_unlock_reservations(job->bos, job->bo_cnt, &acquire_ctx); 1015 if (!timeout) { 1016 timeout = jiffies + 1017 msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); 1018 } else if (time_after(jiffies, timeout)) { 1019 ret = -ETIME; 1020 goto cleanup_job; 1021 } 1022 1023 ret = aie2_populate_range(abo); 1024 if (ret) 1025 goto cleanup_job; 1026 goto retry; 1027 } 1028 } 1029 1030 mutex_lock(&hwctx->priv->io_lock); 1031 drm_sched_job_arm(&job->base); 1032 job->out_fence = dma_fence_get(&job->base.s_fence->finished); 1033 for (i = 0; i < job->bo_cnt; i++) 1034 dma_resv_add_fence(job->bos[i]->resv, job->out_fence, DMA_RESV_USAGE_WRITE); 1035 job->seq = hwctx->priv->seq++; 1036 kref_get(&job->refcnt); 1037 drm_sched_entity_push_job(&job->base); 1038 1039 *seq = job->seq; 1040 drm_syncobj_add_point(hwctx->priv->syncobj, chain, job->out_fence, *seq); 1041 mutex_unlock(&hwctx->priv->io_lock); 1042 1043 up_read(&xdna->notifier_lock); 1044 drm_gem_unlock_reservations(job->bos, job->bo_cnt, &acquire_ctx); 1045 1046 aie2_job_put(job); 1047 atomic64_inc(&hwctx->job_submit_cnt); 1048 1049 return 0; 1050 1051 cleanup_job: 1052 drm_sched_job_cleanup(&job->base); 1053 free_chain: 1054 dma_fence_chain_free(chain); 1055 up_sem: 1056 up(&hwctx->priv->job_sem); 1057 job->job_done = true; 1058 return ret; 1059 } 1060 1061 void aie2_hmm_invalidate(struct amdxdna_gem_obj *abo, 1062 unsigned long cur_seq) 1063 { 1064 struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); 1065 struct drm_gem_object *gobj = to_gobj(abo); 1066 long ret; 1067 1068 ret = dma_resv_wait_timeout(gobj->resv, DMA_RESV_USAGE_BOOKKEEP, 1069 true, MAX_SCHEDULE_TIMEOUT); 1070 if (!ret) 1071 XDNA_ERR(xdna, "Failed to wait for bo, ret %ld", ret); 1072 else if (ret == -ERESTARTSYS) 1073 XDNA_DBG(xdna, "Wait for bo interrupted by signal"); 1074 } 1075