1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2023-2024, Advanced Micro Devices, Inc. 4 */ 5 6 #include <drm/amdxdna_accel.h> 7 #include <drm/drm_cache.h> 8 #include <drm/drm_device.h> 9 #include <drm/drm_gem.h> 10 #include <drm/drm_gem_shmem_helper.h> 11 #include <drm/drm_print.h> 12 #include <drm/gpu_scheduler.h> 13 #include <linux/bitfield.h> 14 #include <linux/errno.h> 15 #include <linux/pci.h> 16 #include <linux/types.h> 17 #include <linux/xarray.h> 18 19 #include "aie2_msg_priv.h" 20 #include "aie2_pci.h" 21 #include "amdxdna_ctx.h" 22 #include "amdxdna_gem.h" 23 #include "amdxdna_mailbox.h" 24 #include "amdxdna_mailbox_helper.h" 25 #include "amdxdna_pci_drv.h" 26 27 #define DECLARE_AIE2_MSG(name, op) \ 28 DECLARE_XDNA_MSG_COMMON(name, op, MAX_AIE2_STATUS_CODE) 29 30 #define EXEC_MSG_OPS(xdna) ((xdna)->dev_handle->exec_msg_ops) 31 32 static int aie2_send_mgmt_msg_wait(struct amdxdna_dev_hdl *ndev, 33 struct xdna_mailbox_msg *msg) 34 { 35 struct amdxdna_dev *xdna = ndev->xdna; 36 struct xdna_notify *hdl = msg->handle; 37 int ret; 38 39 if (!ndev->mgmt_chann) 40 return -ENODEV; 41 42 ret = xdna_send_msg_wait(xdna, ndev->mgmt_chann, msg); 43 if (ret == -ETIME) { 44 xdna_mailbox_stop_channel(ndev->mgmt_chann); 45 xdna_mailbox_destroy_channel(ndev->mgmt_chann); 46 ndev->mgmt_chann = NULL; 47 } 48 49 if (!ret && *hdl->status != AIE2_STATUS_SUCCESS) { 50 XDNA_ERR(xdna, "command opcode 0x%x failed, status 0x%x", 51 msg->opcode, *hdl->data); 52 ret = -EINVAL; 53 } 54 55 return ret; 56 } 57 58 void *aie2_alloc_msg_buffer(struct amdxdna_dev_hdl *ndev, u32 *size, 59 dma_addr_t *dma_addr) 60 { 61 struct amdxdna_dev *xdna = ndev->xdna; 62 int order; 63 64 *size = max(*size, SZ_8K); 65 order = get_order(*size); 66 if (order > MAX_PAGE_ORDER) 67 return NULL; 68 *size = PAGE_SIZE << order; 69 70 return dma_alloc_noncoherent(xdna->ddev.dev, *size, dma_addr, 71 DMA_FROM_DEVICE, GFP_KERNEL); 72 } 73 74 int aie2_suspend_fw(struct amdxdna_dev_hdl *ndev) 75 { 76 DECLARE_AIE2_MSG(suspend, MSG_OP_SUSPEND); 77 int ret; 78 79 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 80 if (ret) { 81 XDNA_ERR(ndev->xdna, "Failed to suspend fw, ret %d", ret); 82 return ret; 83 } 84 85 return aie2_psp_waitmode_poll(ndev->psp_hdl); 86 } 87 88 int aie2_resume_fw(struct amdxdna_dev_hdl *ndev) 89 { 90 DECLARE_AIE2_MSG(suspend, MSG_OP_RESUME); 91 92 return aie2_send_mgmt_msg_wait(ndev, &msg); 93 } 94 95 int aie2_set_runtime_cfg(struct amdxdna_dev_hdl *ndev, u32 type, u64 value) 96 { 97 DECLARE_AIE2_MSG(set_runtime_cfg, MSG_OP_SET_RUNTIME_CONFIG); 98 int ret; 99 100 req.type = type; 101 req.value = value; 102 103 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 104 if (ret) { 105 XDNA_ERR(ndev->xdna, "Failed to set runtime config, ret %d", ret); 106 return ret; 107 } 108 109 return 0; 110 } 111 112 int aie2_get_runtime_cfg(struct amdxdna_dev_hdl *ndev, u32 type, u64 *value) 113 { 114 DECLARE_AIE2_MSG(get_runtime_cfg, MSG_OP_GET_RUNTIME_CONFIG); 115 int ret; 116 117 req.type = type; 118 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 119 if (ret) { 120 XDNA_ERR(ndev->xdna, "Failed to get runtime config, ret %d", ret); 121 return ret; 122 } 123 124 *value = resp.value; 125 return 0; 126 } 127 128 int aie2_assign_mgmt_pasid(struct amdxdna_dev_hdl *ndev, u16 pasid) 129 { 130 DECLARE_AIE2_MSG(assign_mgmt_pasid, MSG_OP_ASSIGN_MGMT_PASID); 131 132 req.pasid = pasid; 133 134 return aie2_send_mgmt_msg_wait(ndev, &msg); 135 } 136 137 int aie2_query_aie_version(struct amdxdna_dev_hdl *ndev, struct aie_version *version) 138 { 139 DECLARE_AIE2_MSG(aie_version_info, MSG_OP_QUERY_AIE_VERSION); 140 struct amdxdna_dev *xdna = ndev->xdna; 141 int ret; 142 143 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 144 if (ret) 145 return ret; 146 147 XDNA_DBG(xdna, "Query AIE version - major: %u minor: %u completed", 148 resp.major, resp.minor); 149 150 version->major = resp.major; 151 version->minor = resp.minor; 152 153 return 0; 154 } 155 156 int aie2_query_aie_metadata(struct amdxdna_dev_hdl *ndev, struct aie_metadata *metadata) 157 { 158 DECLARE_AIE2_MSG(aie_tile_info, MSG_OP_QUERY_AIE_TILE_INFO); 159 int ret; 160 161 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 162 if (ret) 163 return ret; 164 165 metadata->size = resp.info.size; 166 metadata->cols = resp.info.cols; 167 metadata->rows = resp.info.rows; 168 169 metadata->version.major = resp.info.major; 170 metadata->version.minor = resp.info.minor; 171 172 metadata->core.row_count = resp.info.core_rows; 173 metadata->core.row_start = resp.info.core_row_start; 174 metadata->core.dma_channel_count = resp.info.core_dma_channels; 175 metadata->core.lock_count = resp.info.core_locks; 176 metadata->core.event_reg_count = resp.info.core_events; 177 178 metadata->mem.row_count = resp.info.mem_rows; 179 metadata->mem.row_start = resp.info.mem_row_start; 180 metadata->mem.dma_channel_count = resp.info.mem_dma_channels; 181 metadata->mem.lock_count = resp.info.mem_locks; 182 metadata->mem.event_reg_count = resp.info.mem_events; 183 184 metadata->shim.row_count = resp.info.shim_rows; 185 metadata->shim.row_start = resp.info.shim_row_start; 186 metadata->shim.dma_channel_count = resp.info.shim_dma_channels; 187 metadata->shim.lock_count = resp.info.shim_locks; 188 metadata->shim.event_reg_count = resp.info.shim_events; 189 190 return 0; 191 } 192 193 int aie2_query_firmware_version(struct amdxdna_dev_hdl *ndev, 194 struct amdxdna_fw_ver *fw_ver) 195 { 196 DECLARE_AIE2_MSG(firmware_version, MSG_OP_GET_FIRMWARE_VERSION); 197 int ret; 198 199 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 200 if (ret) 201 return ret; 202 203 fw_ver->major = resp.major; 204 fw_ver->minor = resp.minor; 205 fw_ver->sub = resp.sub; 206 fw_ver->build = resp.build; 207 208 return 0; 209 } 210 211 static int aie2_destroy_context_req(struct amdxdna_dev_hdl *ndev, u32 id) 212 { 213 DECLARE_AIE2_MSG(destroy_ctx, MSG_OP_DESTROY_CONTEXT); 214 struct amdxdna_dev *xdna = ndev->xdna; 215 int ret; 216 217 req.context_id = id; 218 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 219 if (ret) 220 XDNA_WARN(xdna, "Destroy context failed, ret %d", ret); 221 222 return ret; 223 } 224 225 static u32 aie2_get_context_priority(struct amdxdna_dev_hdl *ndev, 226 struct amdxdna_hwctx *hwctx) 227 { 228 if (!AIE2_FEATURE_ON(ndev, AIE2_PREEMPT)) 229 return PRIORITY_HIGH; 230 231 switch (hwctx->qos.priority) { 232 case AMDXDNA_QOS_REALTIME_PRIORITY: 233 return PRIORITY_REALTIME; 234 case AMDXDNA_QOS_HIGH_PRIORITY: 235 return PRIORITY_HIGH; 236 case AMDXDNA_QOS_NORMAL_PRIORITY: 237 return PRIORITY_NORMAL; 238 case AMDXDNA_QOS_LOW_PRIORITY: 239 return PRIORITY_LOW; 240 default: 241 return PRIORITY_HIGH; 242 } 243 } 244 245 int aie2_create_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwctx) 246 { 247 DECLARE_AIE2_MSG(create_ctx, MSG_OP_CREATE_CONTEXT); 248 struct amdxdna_dev *xdna = ndev->xdna; 249 struct xdna_mailbox_chann_res x2i; 250 struct xdna_mailbox_chann_res i2x; 251 struct cq_pair *cq_pair; 252 u32 intr_reg; 253 int ret; 254 255 req.aie_type = 1; 256 req.start_col = hwctx->start_col; 257 req.num_col = hwctx->num_col; 258 req.num_unused_col = hwctx->num_unused_col; 259 req.num_cq_pairs_requested = 1; 260 req.pasid = hwctx->client->pasid; 261 req.context_priority = aie2_get_context_priority(ndev, hwctx); 262 263 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 264 if (ret) 265 return ret; 266 267 hwctx->fw_ctx_id = resp.context_id; 268 if (WARN_ON_ONCE(hwctx->fw_ctx_id == -1)) 269 return -EINVAL; 270 271 if (ndev->force_preempt_enabled) { 272 ret = aie2_runtime_cfg(ndev, AIE2_RT_CFG_FORCE_PREEMPT, &hwctx->fw_ctx_id); 273 if (ret) { 274 XDNA_ERR(xdna, "failed to enable force preempt %d", ret); 275 goto del_ctx_req; 276 } 277 } 278 279 cq_pair = &resp.cq_pair[0]; 280 x2i.mb_head_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->x2i_q.head_addr); 281 x2i.mb_tail_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->x2i_q.tail_addr); 282 x2i.rb_start_addr = AIE2_SRAM_OFF(ndev, cq_pair->x2i_q.buf_addr); 283 x2i.rb_size = cq_pair->x2i_q.buf_size; 284 285 i2x.mb_head_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->i2x_q.head_addr); 286 i2x.mb_tail_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->i2x_q.tail_addr); 287 i2x.rb_start_addr = AIE2_SRAM_OFF(ndev, cq_pair->i2x_q.buf_addr); 288 i2x.rb_size = cq_pair->i2x_q.buf_size; 289 290 ret = pci_irq_vector(to_pci_dev(xdna->ddev.dev), resp.msix_id); 291 if (ret == -EINVAL) { 292 XDNA_ERR(xdna, "Alloc IRQ failed %d", ret); 293 goto del_ctx_req; 294 } 295 296 intr_reg = i2x.mb_head_ptr_reg + 4; 297 hwctx->priv->mbox_chann = xdna_mailbox_create_channel(ndev->mbox, &x2i, &i2x, 298 intr_reg, ret); 299 if (!hwctx->priv->mbox_chann) { 300 XDNA_ERR(xdna, "Not able to create channel"); 301 ret = -EINVAL; 302 goto del_ctx_req; 303 } 304 ndev->hwctx_num++; 305 306 XDNA_DBG(xdna, "Mailbox channel irq: %d, msix_id: %d", ret, resp.msix_id); 307 XDNA_DBG(xdna, "Created fw ctx %d pasid %d", hwctx->fw_ctx_id, hwctx->client->pasid); 308 309 return 0; 310 311 del_ctx_req: 312 aie2_destroy_context_req(ndev, hwctx->fw_ctx_id); 313 return ret; 314 } 315 316 int aie2_destroy_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwctx) 317 { 318 struct amdxdna_dev *xdna = ndev->xdna; 319 int ret; 320 321 xdna_mailbox_stop_channel(hwctx->priv->mbox_chann); 322 ret = aie2_destroy_context_req(ndev, hwctx->fw_ctx_id); 323 xdna_mailbox_destroy_channel(hwctx->priv->mbox_chann); 324 XDNA_DBG(xdna, "Destroyed fw ctx %d", hwctx->fw_ctx_id); 325 hwctx->priv->mbox_chann = NULL; 326 hwctx->fw_ctx_id = -1; 327 ndev->hwctx_num--; 328 329 return ret; 330 } 331 332 int aie2_map_host_buf(struct amdxdna_dev_hdl *ndev, u32 context_id, u64 addr, u64 size) 333 { 334 DECLARE_AIE2_MSG(map_host_buffer, MSG_OP_MAP_HOST_BUFFER); 335 struct amdxdna_dev *xdna = ndev->xdna; 336 int ret; 337 338 req.context_id = context_id; 339 req.buf_addr = addr; 340 req.buf_size = size; 341 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 342 if (ret) 343 return ret; 344 345 XDNA_DBG(xdna, "fw ctx %d map host buf addr 0x%llx size 0x%llx", 346 context_id, addr, size); 347 348 return 0; 349 } 350 351 static int amdxdna_hwctx_col_map(struct amdxdna_hwctx *hwctx, void *arg) 352 { 353 u32 *bitmap = arg; 354 355 *bitmap |= GENMASK(hwctx->start_col + hwctx->num_col - 1, hwctx->start_col); 356 357 return 0; 358 } 359 360 int aie2_query_status(struct amdxdna_dev_hdl *ndev, char __user *buf, 361 u32 size, u32 *cols_filled) 362 { 363 DECLARE_AIE2_MSG(aie_column_info, MSG_OP_QUERY_COL_STATUS); 364 struct amdxdna_dev *xdna = ndev->xdna; 365 u32 buf_sz = size, aie_bitmap = 0; 366 struct amdxdna_client *client; 367 dma_addr_t dma_addr; 368 u8 *buff_addr; 369 int ret; 370 371 buff_addr = aie2_alloc_msg_buffer(ndev, &buf_sz, &dma_addr); 372 if (!buff_addr) 373 return -ENOMEM; 374 375 /* Go through each hardware context and mark the AIE columns that are active */ 376 list_for_each_entry(client, &xdna->client_list, node) 377 amdxdna_hwctx_walk(client, &aie_bitmap, amdxdna_hwctx_col_map); 378 379 *cols_filled = 0; 380 req.dump_buff_addr = dma_addr; 381 req.dump_buff_size = buf_sz; 382 req.num_cols = hweight32(aie_bitmap); 383 req.aie_bitmap = aie_bitmap; 384 385 drm_clflush_virt_range(buff_addr, size); /* device can access */ 386 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 387 if (ret) { 388 XDNA_ERR(xdna, "Error during NPU query, status %d", ret); 389 goto fail; 390 } 391 392 XDNA_DBG(xdna, "Query NPU status completed"); 393 394 if (size < resp.size) { 395 ret = -EINVAL; 396 XDNA_ERR(xdna, "Bad buffer size. Available: %u. Needs: %u", size, resp.size); 397 goto fail; 398 } 399 400 if (copy_to_user(buf, buff_addr, resp.size)) { 401 ret = -EFAULT; 402 XDNA_ERR(xdna, "Failed to copy NPU status to user space"); 403 goto fail; 404 } 405 406 *cols_filled = aie_bitmap; 407 408 fail: 409 aie2_free_msg_buffer(ndev, buf_sz, buff_addr, dma_addr); 410 return ret; 411 } 412 413 int aie2_query_telemetry(struct amdxdna_dev_hdl *ndev, 414 char __user *buf, u32 size, 415 struct amdxdna_drm_query_telemetry_header *header) 416 { 417 DECLARE_AIE2_MSG(get_telemetry, MSG_OP_GET_TELEMETRY); 418 struct amdxdna_dev *xdna = ndev->xdna; 419 dma_addr_t dma_addr; 420 u32 buf_sz = size; 421 u8 *addr; 422 int ret; 423 424 if (header->type >= MAX_TELEMETRY_TYPE) 425 return -EINVAL; 426 427 addr = aie2_alloc_msg_buffer(ndev, &buf_sz, &dma_addr); 428 if (!addr) 429 return -ENOMEM; 430 431 req.buf_addr = dma_addr; 432 req.buf_size = buf_sz; 433 req.type = header->type; 434 435 drm_clflush_virt_range(addr, size); /* device can access */ 436 ret = aie2_send_mgmt_msg_wait(ndev, &msg); 437 if (ret) { 438 XDNA_ERR(xdna, "Query telemetry failed, status %d", ret); 439 goto free_buf; 440 } 441 442 if (size < resp.size) { 443 ret = -EINVAL; 444 XDNA_ERR(xdna, "Bad buffer size. Available: %u. Needs: %u", size, resp.size); 445 goto free_buf; 446 } 447 448 if (copy_to_user(buf, addr, resp.size)) { 449 ret = -EFAULT; 450 XDNA_ERR(xdna, "Failed to copy telemetry to user space"); 451 goto free_buf; 452 } 453 454 header->major = resp.major; 455 header->minor = resp.minor; 456 457 free_buf: 458 aie2_free_msg_buffer(ndev, buf_sz, addr, dma_addr); 459 return ret; 460 } 461 462 int aie2_register_asyn_event_msg(struct amdxdna_dev_hdl *ndev, dma_addr_t addr, u32 size, 463 void *handle, int (*cb)(void*, void __iomem *, size_t)) 464 { 465 struct async_event_msg_req req = { 0 }; 466 struct xdna_mailbox_msg msg = { 467 .send_data = (u8 *)&req, 468 .send_size = sizeof(req), 469 .handle = handle, 470 .opcode = MSG_OP_REGISTER_ASYNC_EVENT_MSG, 471 .notify_cb = cb, 472 }; 473 474 req.buf_addr = addr; 475 req.buf_size = size; 476 477 XDNA_DBG(ndev->xdna, "Register addr 0x%llx size 0x%x", addr, size); 478 return xdna_mailbox_send_msg(ndev->mgmt_chann, &msg, TX_TIMEOUT); 479 } 480 481 int aie2_config_cu(struct amdxdna_hwctx *hwctx, 482 int (*notify_cb)(void *, void __iomem *, size_t)) 483 { 484 struct mailbox_channel *chann = hwctx->priv->mbox_chann; 485 struct amdxdna_dev *xdna = hwctx->client->xdna; 486 u32 shift = xdna->dev_info->dev_mem_buf_shift; 487 struct config_cu_req req = { 0 }; 488 struct xdna_mailbox_msg msg; 489 struct drm_gem_object *gobj; 490 struct amdxdna_gem_obj *abo; 491 int i; 492 493 if (!chann) 494 return -ENODEV; 495 496 if (hwctx->cus->num_cus > MAX_NUM_CUS) { 497 XDNA_DBG(xdna, "Exceed maximum CU %d", MAX_NUM_CUS); 498 return -EINVAL; 499 } 500 501 for (i = 0; i < hwctx->cus->num_cus; i++) { 502 struct amdxdna_cu_config *cu = &hwctx->cus->cu_configs[i]; 503 504 if (XDNA_MBZ_DBG(xdna, cu->pad, sizeof(cu->pad))) 505 return -EINVAL; 506 507 gobj = drm_gem_object_lookup(hwctx->client->filp, cu->cu_bo); 508 if (!gobj) { 509 XDNA_ERR(xdna, "Lookup GEM object failed"); 510 return -EINVAL; 511 } 512 abo = to_xdna_obj(gobj); 513 514 if (abo->type != AMDXDNA_BO_DEV) { 515 drm_gem_object_put(gobj); 516 XDNA_ERR(xdna, "Invalid BO type"); 517 return -EINVAL; 518 } 519 520 req.cfgs[i] = FIELD_PREP(AIE2_MSG_CFG_CU_PDI_ADDR, 521 abo->mem.dev_addr >> shift); 522 req.cfgs[i] |= FIELD_PREP(AIE2_MSG_CFG_CU_FUNC, cu->cu_func); 523 XDNA_DBG(xdna, "CU %d full addr 0x%llx, cfg 0x%x", i, 524 abo->mem.dev_addr, req.cfgs[i]); 525 drm_gem_object_put(gobj); 526 } 527 req.num_cus = hwctx->cus->num_cus; 528 529 msg.send_data = (u8 *)&req; 530 msg.send_size = sizeof(req); 531 msg.handle = hwctx; 532 msg.opcode = MSG_OP_CONFIG_CU; 533 msg.notify_cb = notify_cb; 534 return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT); 535 } 536 537 static int aie2_init_exec_cu_req(struct amdxdna_gem_obj *cmd_bo, void *req, 538 size_t *size, u32 *msg_op) 539 { 540 struct execute_buffer_req *cu_req = req; 541 u32 cmd_len; 542 void *cmd; 543 544 cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 545 if (cmd_len > sizeof(cu_req->payload)) 546 return -EINVAL; 547 548 cu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo); 549 if (cu_req->cu_idx == INVALID_CU_IDX) 550 return -EINVAL; 551 552 memcpy(cu_req->payload, cmd, cmd_len); 553 554 *size = sizeof(*cu_req); 555 *msg_op = MSG_OP_EXECUTE_BUFFER_CF; 556 return 0; 557 } 558 559 static int aie2_init_exec_dpu_req(struct amdxdna_gem_obj *cmd_bo, void *req, 560 size_t *size, u32 *msg_op) 561 { 562 struct exec_dpu_req *dpu_req = req; 563 struct amdxdna_cmd_start_npu *sn; 564 u32 cmd_len; 565 566 sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 567 if (cmd_len - sizeof(*sn) > sizeof(dpu_req->payload)) 568 return -EINVAL; 569 570 dpu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo); 571 if (dpu_req->cu_idx == INVALID_CU_IDX) 572 return -EINVAL; 573 574 dpu_req->inst_buf_addr = sn->buffer; 575 dpu_req->inst_size = sn->buffer_size; 576 dpu_req->inst_prop_cnt = sn->prop_count; 577 memcpy(dpu_req->payload, sn->prop_args, cmd_len - sizeof(*sn)); 578 579 *size = sizeof(*dpu_req); 580 *msg_op = MSG_OP_EXEC_DPU; 581 return 0; 582 } 583 584 static void aie2_init_exec_chain_req(void *req, u64 slot_addr, size_t size, u32 cmd_cnt) 585 { 586 struct cmd_chain_req *chain_req = req; 587 588 chain_req->buf_addr = slot_addr; 589 chain_req->buf_size = size; 590 chain_req->count = cmd_cnt; 591 } 592 593 static void aie2_init_npu_chain_req(void *req, u64 slot_addr, size_t size, u32 cmd_cnt) 594 { 595 struct cmd_chain_npu_req *npu_chain_req = req; 596 597 npu_chain_req->flags = 0; 598 npu_chain_req->reserved = 0; 599 npu_chain_req->buf_addr = slot_addr; 600 npu_chain_req->buf_size = size; 601 npu_chain_req->count = cmd_cnt; 602 } 603 604 static int 605 aie2_cmdlist_fill_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size) 606 { 607 struct cmd_chain_slot_execbuf_cf *cf_slot = slot; 608 u32 cmd_len; 609 void *cmd; 610 611 cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 612 if (*size < sizeof(*cf_slot) + cmd_len) 613 return -EINVAL; 614 615 cf_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo); 616 if (cf_slot->cu_idx == INVALID_CU_IDX) 617 return -EINVAL; 618 619 cf_slot->arg_cnt = cmd_len / sizeof(u32); 620 memcpy(cf_slot->args, cmd, cmd_len); 621 /* Accurate slot size to hint firmware to do necessary copy */ 622 *size = sizeof(*cf_slot) + cmd_len; 623 return 0; 624 } 625 626 static int 627 aie2_cmdlist_fill_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size) 628 { 629 struct cmd_chain_slot_dpu *dpu_slot = slot; 630 struct amdxdna_cmd_start_npu *sn; 631 u32 cmd_len; 632 u32 arg_sz; 633 634 sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 635 arg_sz = cmd_len - sizeof(*sn); 636 if (cmd_len < sizeof(*sn) || arg_sz > MAX_DPU_ARGS_SIZE) 637 return -EINVAL; 638 639 if (*size < sizeof(*dpu_slot) + arg_sz) 640 return -EINVAL; 641 642 dpu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo); 643 if (dpu_slot->cu_idx == INVALID_CU_IDX) 644 return -EINVAL; 645 646 dpu_slot->inst_buf_addr = sn->buffer; 647 dpu_slot->inst_size = sn->buffer_size; 648 dpu_slot->inst_prop_cnt = sn->prop_count; 649 dpu_slot->arg_cnt = arg_sz / sizeof(u32); 650 memcpy(dpu_slot->args, sn->prop_args, arg_sz); 651 652 /* Accurate slot size to hint firmware to do necessary copy */ 653 *size = sizeof(*dpu_slot) + arg_sz; 654 return 0; 655 } 656 657 static int aie2_cmdlist_unsupp(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size) 658 { 659 return -EOPNOTSUPP; 660 } 661 662 static u32 aie2_get_chain_msg_op(u32 cmd_op) 663 { 664 switch (cmd_op) { 665 case ERT_START_CU: 666 return MSG_OP_CHAIN_EXEC_BUFFER_CF; 667 case ERT_START_NPU: 668 return MSG_OP_CHAIN_EXEC_DPU; 669 default: 670 break; 671 } 672 673 return MSG_OP_MAX_OPCODE; 674 } 675 676 static struct aie2_exec_msg_ops legacy_exec_message_ops = { 677 .init_cu_req = aie2_init_exec_cu_req, 678 .init_dpu_req = aie2_init_exec_dpu_req, 679 .init_chain_req = aie2_init_exec_chain_req, 680 .fill_cf_slot = aie2_cmdlist_fill_cf, 681 .fill_dpu_slot = aie2_cmdlist_fill_dpu, 682 .fill_preempt_slot = aie2_cmdlist_unsupp, 683 .fill_elf_slot = aie2_cmdlist_unsupp, 684 .get_chain_msg_op = aie2_get_chain_msg_op, 685 }; 686 687 static int 688 aie2_cmdlist_fill_npu_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size) 689 { 690 struct cmd_chain_slot_npu *npu_slot = slot; 691 u32 cmd_len; 692 void *cmd; 693 694 memset(npu_slot, 0, sizeof(*npu_slot)); 695 cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 696 if (*size < sizeof(*npu_slot) + cmd_len) 697 return -EINVAL; 698 699 npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo); 700 if (npu_slot->cu_idx == INVALID_CU_IDX) 701 return -EINVAL; 702 703 npu_slot->type = EXEC_NPU_TYPE_NON_ELF; 704 npu_slot->arg_cnt = cmd_len / sizeof(u32); 705 memcpy(npu_slot->args, cmd, cmd_len); 706 707 *size = sizeof(*npu_slot) + cmd_len; 708 return 0; 709 } 710 711 static int 712 aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size) 713 { 714 struct cmd_chain_slot_npu *npu_slot = slot; 715 struct amdxdna_cmd_start_npu *sn; 716 u32 cmd_len; 717 u32 arg_sz; 718 719 memset(npu_slot, 0, sizeof(*npu_slot)); 720 sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 721 arg_sz = cmd_len - sizeof(*sn); 722 if (cmd_len < sizeof(*sn) || arg_sz > MAX_NPU_ARGS_SIZE) 723 return -EINVAL; 724 725 if (*size < sizeof(*npu_slot) + arg_sz) 726 return -EINVAL; 727 728 npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo); 729 if (npu_slot->cu_idx == INVALID_CU_IDX) 730 return -EINVAL; 731 732 npu_slot->type = EXEC_NPU_TYPE_PARTIAL_ELF; 733 npu_slot->inst_buf_addr = sn->buffer; 734 npu_slot->inst_size = sn->buffer_size; 735 npu_slot->inst_prop_cnt = sn->prop_count; 736 npu_slot->arg_cnt = arg_sz / sizeof(u32); 737 memcpy(npu_slot->args, sn->prop_args, arg_sz); 738 739 *size = sizeof(*npu_slot) + arg_sz; 740 return 0; 741 } 742 743 static int 744 aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size) 745 { 746 struct cmd_chain_slot_npu *npu_slot = slot; 747 struct amdxdna_cmd_preempt_data *pd; 748 u32 cmd_len; 749 u32 arg_sz; 750 751 memset(npu_slot, 0, sizeof(*npu_slot)); 752 pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 753 arg_sz = cmd_len - sizeof(*pd); 754 if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE) 755 return -EINVAL; 756 757 if (*size < sizeof(*npu_slot) + arg_sz) 758 return -EINVAL; 759 760 npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo); 761 if (npu_slot->cu_idx == INVALID_CU_IDX) 762 return -EINVAL; 763 764 npu_slot->type = EXEC_NPU_TYPE_PREEMPT; 765 npu_slot->inst_buf_addr = pd->inst_buf; 766 npu_slot->save_buf_addr = pd->save_buf; 767 npu_slot->restore_buf_addr = pd->restore_buf; 768 npu_slot->inst_size = pd->inst_size; 769 npu_slot->save_size = pd->save_size; 770 npu_slot->restore_size = pd->restore_size; 771 npu_slot->inst_prop_cnt = pd->inst_prop_cnt; 772 npu_slot->arg_cnt = arg_sz / sizeof(u32); 773 memcpy(npu_slot->args, pd->prop_args, arg_sz); 774 775 *size = sizeof(*npu_slot) + arg_sz; 776 return 0; 777 } 778 779 static int 780 aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size) 781 { 782 struct cmd_chain_slot_npu *npu_slot = slot; 783 struct amdxdna_cmd_preempt_data *pd; 784 u32 cmd_len; 785 u32 arg_sz; 786 787 memset(npu_slot, 0, sizeof(*npu_slot)); 788 pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len); 789 arg_sz = cmd_len - sizeof(*pd); 790 if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE) 791 return -EINVAL; 792 793 if (*size < sizeof(*npu_slot) + arg_sz) 794 return -EINVAL; 795 796 npu_slot->type = EXEC_NPU_TYPE_ELF; 797 npu_slot->inst_buf_addr = pd->inst_buf; 798 npu_slot->save_buf_addr = pd->save_buf; 799 npu_slot->restore_buf_addr = pd->restore_buf; 800 npu_slot->inst_size = pd->inst_size; 801 npu_slot->save_size = pd->save_size; 802 npu_slot->restore_size = pd->restore_size; 803 npu_slot->inst_prop_cnt = pd->inst_prop_cnt; 804 npu_slot->arg_cnt = 1; 805 npu_slot->args[0] = AIE2_EXEC_BUFFER_KERNEL_OP_TXN; 806 807 *size = struct_size(npu_slot, args, npu_slot->arg_cnt); 808 return 0; 809 } 810 811 static u32 aie2_get_npu_chain_msg_op(u32 cmd_op) 812 { 813 return MSG_OP_CHAIN_EXEC_NPU; 814 } 815 816 static struct aie2_exec_msg_ops npu_exec_message_ops = { 817 .init_cu_req = aie2_init_exec_cu_req, 818 .init_dpu_req = aie2_init_exec_dpu_req, 819 .init_chain_req = aie2_init_npu_chain_req, 820 .fill_cf_slot = aie2_cmdlist_fill_npu_cf, 821 .fill_dpu_slot = aie2_cmdlist_fill_npu_dpu, 822 .fill_preempt_slot = aie2_cmdlist_fill_npu_preempt, 823 .fill_elf_slot = aie2_cmdlist_fill_npu_elf, 824 .get_chain_msg_op = aie2_get_npu_chain_msg_op, 825 }; 826 827 static int aie2_init_exec_req(void *req, struct amdxdna_gem_obj *cmd_abo, 828 size_t *size, u32 *msg_op) 829 { 830 struct amdxdna_dev *xdna = cmd_abo->client->xdna; 831 int ret; 832 u32 op; 833 834 835 op = amdxdna_cmd_get_op(cmd_abo); 836 switch (op) { 837 case ERT_START_CU: 838 ret = EXEC_MSG_OPS(xdna)->init_cu_req(cmd_abo, req, size, msg_op); 839 if (ret) { 840 XDNA_DBG(xdna, "Init CU req failed ret %d", ret); 841 return ret; 842 } 843 break; 844 case ERT_START_NPU: 845 ret = EXEC_MSG_OPS(xdna)->init_dpu_req(cmd_abo, req, size, msg_op); 846 if (ret) { 847 XDNA_DBG(xdna, "Init DPU req failed ret %d", ret); 848 return ret; 849 } 850 851 break; 852 default: 853 XDNA_ERR(xdna, "Unsupported op %d", op); 854 ret = -EOPNOTSUPP; 855 break; 856 } 857 858 return ret; 859 } 860 861 static int 862 aie2_cmdlist_fill_slot(void *slot, struct amdxdna_gem_obj *cmd_abo, 863 size_t *size, u32 *cmd_op) 864 { 865 struct amdxdna_dev *xdna = cmd_abo->client->xdna; 866 int ret; 867 u32 op; 868 869 op = amdxdna_cmd_get_op(cmd_abo); 870 if (*cmd_op == ERT_INVALID_CMD) 871 *cmd_op = op; 872 else if (op != *cmd_op) 873 return -EINVAL; 874 875 switch (op) { 876 case ERT_START_CU: 877 ret = EXEC_MSG_OPS(xdna)->fill_cf_slot(cmd_abo, slot, size); 878 break; 879 case ERT_START_NPU: 880 ret = EXEC_MSG_OPS(xdna)->fill_dpu_slot(cmd_abo, slot, size); 881 break; 882 case ERT_START_NPU_PREEMPT: 883 if (!AIE2_FEATURE_ON(xdna->dev_handle, AIE2_PREEMPT)) 884 return -EOPNOTSUPP; 885 ret = EXEC_MSG_OPS(xdna)->fill_preempt_slot(cmd_abo, slot, size); 886 break; 887 case ERT_START_NPU_PREEMPT_ELF: 888 if (!AIE2_FEATURE_ON(xdna->dev_handle, AIE2_PREEMPT)) 889 return -EOPNOTSUPP; 890 ret = EXEC_MSG_OPS(xdna)->fill_elf_slot(cmd_abo, slot, size); 891 break; 892 default: 893 XDNA_INFO(xdna, "Unsupported op %d", op); 894 ret = -EOPNOTSUPP; 895 break; 896 } 897 898 return ret; 899 } 900 901 void aie2_msg_init(struct amdxdna_dev_hdl *ndev) 902 { 903 if (AIE2_FEATURE_ON(ndev, AIE2_NPU_COMMAND)) 904 ndev->exec_msg_ops = &npu_exec_message_ops; 905 else 906 ndev->exec_msg_ops = &legacy_exec_message_ops; 907 } 908 909 static inline struct amdxdna_gem_obj * 910 aie2_cmdlist_get_cmd_buf(struct amdxdna_sched_job *job) 911 { 912 int idx = get_job_idx(job->seq); 913 914 return job->hwctx->priv->cmd_buf[idx]; 915 } 916 917 int aie2_execbuf(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, 918 int (*notify_cb)(void *, void __iomem *, size_t)) 919 { 920 struct mailbox_channel *chann = hwctx->priv->mbox_chann; 921 struct amdxdna_dev *xdna = hwctx->client->xdna; 922 struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; 923 struct xdna_mailbox_msg msg; 924 union exec_req req; 925 int ret; 926 927 if (!chann) 928 return -ENODEV; 929 930 ret = aie2_init_exec_req(&req, cmd_abo, &msg.send_size, &msg.opcode); 931 if (ret) 932 return ret; 933 934 msg.handle = job; 935 msg.notify_cb = notify_cb; 936 msg.send_data = (u8 *)&req; 937 print_hex_dump_debug("cmd: ", DUMP_PREFIX_OFFSET, 16, 4, &req, 938 0x40, false); 939 940 ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT); 941 if (ret) { 942 XDNA_ERR(xdna, "Send message failed"); 943 return ret; 944 } 945 946 return 0; 947 } 948 949 int aie2_cmdlist_multi_execbuf(struct amdxdna_hwctx *hwctx, 950 struct amdxdna_sched_job *job, 951 int (*notify_cb)(void *, void __iomem *, size_t)) 952 { 953 struct amdxdna_gem_obj *cmdbuf_abo = aie2_cmdlist_get_cmd_buf(job); 954 struct mailbox_channel *chann = hwctx->priv->mbox_chann; 955 struct amdxdna_client *client = hwctx->client; 956 struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; 957 struct amdxdna_dev *xdna = client->xdna; 958 struct amdxdna_cmd_chain *payload; 959 struct xdna_mailbox_msg msg; 960 union exec_chain_req req; 961 u32 payload_len; 962 u32 offset = 0; 963 size_t size; 964 int ret; 965 u32 op; 966 u32 i; 967 968 op = amdxdna_cmd_get_op(cmd_abo); 969 payload = amdxdna_cmd_get_payload(cmd_abo, &payload_len); 970 if (op != ERT_CMD_CHAIN || !payload || 971 payload_len < struct_size(payload, data, payload->command_count)) 972 return -EINVAL; 973 974 op = ERT_INVALID_CMD; 975 for (i = 0; i < payload->command_count; i++) { 976 u32 boh = (u32)(payload->data[i]); 977 struct amdxdna_gem_obj *abo; 978 979 abo = amdxdna_gem_get_obj(client, boh, AMDXDNA_BO_CMD); 980 if (!abo) { 981 XDNA_ERR(xdna, "Failed to find cmd BO %d", boh); 982 return -ENOENT; 983 } 984 985 size = cmdbuf_abo->mem.size - offset; 986 ret = aie2_cmdlist_fill_slot(cmdbuf_abo->mem.kva + offset, 987 abo, &size, &op); 988 amdxdna_gem_put_obj(abo); 989 if (ret) 990 return ret; 991 992 offset += size; 993 } 994 msg.opcode = EXEC_MSG_OPS(xdna)->get_chain_msg_op(op); 995 if (msg.opcode == MSG_OP_MAX_OPCODE) 996 return -EOPNOTSUPP; 997 998 /* The offset is the accumulated total size of the cmd buffer */ 999 EXEC_MSG_OPS(xdna)->init_chain_req(&req, cmdbuf_abo->mem.dev_addr, 1000 offset, payload->command_count); 1001 drm_clflush_virt_range(cmdbuf_abo->mem.kva, offset); 1002 1003 msg.handle = job; 1004 msg.notify_cb = notify_cb; 1005 msg.send_data = (u8 *)&req; 1006 msg.send_size = sizeof(req); 1007 ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT); 1008 if (ret) { 1009 XDNA_ERR(xdna, "Send message failed"); 1010 return ret; 1011 } 1012 1013 return 0; 1014 } 1015 1016 int aie2_cmdlist_single_execbuf(struct amdxdna_hwctx *hwctx, 1017 struct amdxdna_sched_job *job, 1018 int (*notify_cb)(void *, void __iomem *, size_t)) 1019 { 1020 struct amdxdna_gem_obj *cmdbuf_abo = aie2_cmdlist_get_cmd_buf(job); 1021 struct mailbox_channel *chann = hwctx->priv->mbox_chann; 1022 struct amdxdna_dev *xdna = hwctx->client->xdna; 1023 struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; 1024 struct xdna_mailbox_msg msg; 1025 union exec_chain_req req; 1026 u32 op = ERT_INVALID_CMD; 1027 size_t size; 1028 int ret; 1029 1030 size = cmdbuf_abo->mem.size; 1031 ret = aie2_cmdlist_fill_slot(cmdbuf_abo->mem.kva, cmd_abo, &size, &op); 1032 if (ret) 1033 return ret; 1034 1035 msg.opcode = EXEC_MSG_OPS(xdna)->get_chain_msg_op(op); 1036 if (msg.opcode == MSG_OP_MAX_OPCODE) 1037 return -EOPNOTSUPP; 1038 1039 EXEC_MSG_OPS(xdna)->init_chain_req(&req, cmdbuf_abo->mem.dev_addr, 1040 size, 1); 1041 drm_clflush_virt_range(cmdbuf_abo->mem.kva, size); 1042 1043 msg.handle = job; 1044 msg.notify_cb = notify_cb; 1045 msg.send_data = (u8 *)&req; 1046 msg.send_size = sizeof(req); 1047 ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT); 1048 if (ret) { 1049 XDNA_ERR(hwctx->client->xdna, "Send message failed"); 1050 return ret; 1051 } 1052 1053 return 0; 1054 } 1055 1056 int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, 1057 int (*notify_cb)(void *, void __iomem *, size_t)) 1058 { 1059 struct mailbox_channel *chann = hwctx->priv->mbox_chann; 1060 struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]); 1061 struct amdxdna_dev *xdna = hwctx->client->xdna; 1062 struct xdna_mailbox_msg msg; 1063 struct sync_bo_req req; 1064 int ret = 0; 1065 1066 req.src_addr = 0; 1067 req.dst_addr = amdxdna_dev_bo_offset(abo); 1068 req.size = abo->mem.size; 1069 1070 /* Device to Host */ 1071 req.type = FIELD_PREP(AIE2_MSG_SYNC_BO_SRC_TYPE, SYNC_BO_DEV_MEM) | 1072 FIELD_PREP(AIE2_MSG_SYNC_BO_DST_TYPE, SYNC_BO_HOST_MEM); 1073 1074 XDNA_DBG(xdna, "sync %d bytes src(0x%llx) to dst(0x%llx) completed", 1075 req.size, req.src_addr, req.dst_addr); 1076 1077 msg.handle = job; 1078 msg.notify_cb = notify_cb; 1079 msg.send_data = (u8 *)&req; 1080 msg.send_size = sizeof(req); 1081 msg.opcode = MSG_OP_SYNC_BO; 1082 1083 ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT); 1084 if (ret) { 1085 XDNA_ERR(xdna, "Send message failed"); 1086 return ret; 1087 } 1088 1089 return 0; 1090 } 1091 1092 int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, 1093 int (*notify_cb)(void *, void __iomem *, size_t)) 1094 { 1095 struct mailbox_channel *chann = hwctx->priv->mbox_chann; 1096 struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]); 1097 struct amdxdna_dev *xdna = hwctx->client->xdna; 1098 struct config_debug_bo_req req; 1099 struct xdna_mailbox_msg msg; 1100 1101 if (job->drv_cmd->opcode == ATTACH_DEBUG_BO) 1102 req.config = DEBUG_BO_REGISTER; 1103 else 1104 req.config = DEBUG_BO_UNREGISTER; 1105 1106 req.offset = amdxdna_dev_bo_offset(abo); 1107 req.size = abo->mem.size; 1108 1109 XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d", 1110 req.offset, req.size, req.config); 1111 1112 msg.handle = job; 1113 msg.notify_cb = notify_cb; 1114 msg.send_data = (u8 *)&req; 1115 msg.send_size = sizeof(req); 1116 msg.opcode = MSG_OP_CONFIG_DEBUG_BO; 1117 1118 return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT); 1119 } 1120