1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Copyright 2016-2022 HabanaLabs, Ltd. 5 * All Rights Reserved. 6 */ 7 8 #define pr_fmt(fmt) "habanalabs: " fmt 9 10 #include <uapi/drm/habanalabs_accel.h> 11 #include "habanalabs.h" 12 13 #include <linux/pci.h> 14 #include <linux/hwmon.h> 15 #include <linux/vmalloc.h> 16 17 #include <trace/events/habanalabs.h> 18 19 #define HL_RESET_DELAY_USEC 10000 /* 10ms */ 20 21 #define HL_DEVICE_RELEASE_WATCHDOG_TIMEOUT_SEC 5 22 23 enum dma_alloc_type { 24 DMA_ALLOC_COHERENT, 25 DMA_ALLOC_POOL, 26 }; 27 28 #define MEM_SCRUB_DEFAULT_VAL 0x1122334455667788 29 30 /* 31 * hl_set_dram_bar- sets the bar to allow later access to address 32 * 33 * @hdev: pointer to habanalabs device structure. 34 * @addr: the address the caller wants to access. 35 * @region: the PCI region. 36 * @new_bar_region_base: the new BAR region base address. 37 * 38 * @return: the old BAR base address on success, U64_MAX for failure. 39 * The caller should set it back to the old address after use. 40 * 41 * In case the bar space does not cover the whole address space, 42 * the bar base address should be set to allow access to a given address. 43 * This function can be called also if the bar doesn't need to be set, 44 * in that case it just won't change the base. 45 */ 46 static u64 hl_set_dram_bar(struct hl_device *hdev, u64 addr, struct pci_mem_region *region, 47 u64 *new_bar_region_base) 48 { 49 struct asic_fixed_properties *prop = &hdev->asic_prop; 50 u64 bar_base_addr, old_base; 51 52 if (is_power_of_2(prop->dram_pci_bar_size)) 53 bar_base_addr = addr & ~(prop->dram_pci_bar_size - 0x1ull); 54 else 55 bar_base_addr = DIV_ROUND_DOWN_ULL(addr, prop->dram_pci_bar_size) * 56 prop->dram_pci_bar_size; 57 58 old_base = hdev->asic_funcs->set_dram_bar_base(hdev, bar_base_addr); 59 60 /* in case of success we need to update the new BAR base */ 61 if ((old_base != U64_MAX) && new_bar_region_base) 62 *new_bar_region_base = bar_base_addr; 63 64 return old_base; 65 } 66 67 int hl_access_sram_dram_region(struct hl_device *hdev, u64 addr, u64 *val, 68 enum debugfs_access_type acc_type, enum pci_region region_type, bool set_dram_bar) 69 { 70 struct pci_mem_region *region = &hdev->pci_mem_region[region_type]; 71 u64 old_base = 0, rc, bar_region_base = region->region_base; 72 void __iomem *acc_addr; 73 74 if (set_dram_bar) { 75 old_base = hl_set_dram_bar(hdev, addr, region, &bar_region_base); 76 if (old_base == U64_MAX) 77 return -EIO; 78 } 79 80 acc_addr = hdev->pcie_bar[region->bar_id] + region->offset_in_bar + 81 (addr - bar_region_base); 82 83 switch (acc_type) { 84 case DEBUGFS_READ8: 85 *val = readb(acc_addr); 86 break; 87 case DEBUGFS_WRITE8: 88 writeb(*val, acc_addr); 89 break; 90 case DEBUGFS_READ32: 91 *val = readl(acc_addr); 92 break; 93 case DEBUGFS_WRITE32: 94 writel(*val, acc_addr); 95 break; 96 case DEBUGFS_READ64: 97 *val = readq(acc_addr); 98 break; 99 case DEBUGFS_WRITE64: 100 writeq(*val, acc_addr); 101 break; 102 } 103 104 if (set_dram_bar) { 105 rc = hl_set_dram_bar(hdev, old_base, region, NULL); 106 if (rc == U64_MAX) 107 return -EIO; 108 } 109 110 return 0; 111 } 112 113 static void *hl_dma_alloc_common(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle, 114 gfp_t flag, enum dma_alloc_type alloc_type, 115 const char *caller) 116 { 117 void *ptr = NULL; 118 119 switch (alloc_type) { 120 case DMA_ALLOC_COHERENT: 121 ptr = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, size, dma_handle, flag); 122 break; 123 case DMA_ALLOC_POOL: 124 ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, size, flag, dma_handle); 125 break; 126 } 127 128 if (trace_habanalabs_dma_alloc_enabled() && !ZERO_OR_NULL_PTR(ptr)) 129 trace_habanalabs_dma_alloc(hdev->dev, (u64) (uintptr_t) ptr, *dma_handle, size, 130 caller); 131 132 return ptr; 133 } 134 135 static void hl_asic_dma_free_common(struct hl_device *hdev, size_t size, void *cpu_addr, 136 dma_addr_t dma_handle, enum dma_alloc_type alloc_type, 137 const char *caller) 138 { 139 /* this is needed to avoid warning on using freed pointer */ 140 u64 store_cpu_addr = (u64) (uintptr_t) cpu_addr; 141 142 switch (alloc_type) { 143 case DMA_ALLOC_COHERENT: 144 hdev->asic_funcs->asic_dma_free_coherent(hdev, size, cpu_addr, dma_handle); 145 break; 146 case DMA_ALLOC_POOL: 147 hdev->asic_funcs->asic_dma_pool_free(hdev, cpu_addr, dma_handle); 148 break; 149 } 150 151 trace_habanalabs_dma_free(hdev->dev, store_cpu_addr, dma_handle, size, caller); 152 } 153 154 void *hl_asic_dma_alloc_coherent_caller(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle, 155 gfp_t flag, const char *caller) 156 { 157 return hl_dma_alloc_common(hdev, size, dma_handle, flag, DMA_ALLOC_COHERENT, caller); 158 } 159 160 void hl_asic_dma_free_coherent_caller(struct hl_device *hdev, size_t size, void *cpu_addr, 161 dma_addr_t dma_handle, const char *caller) 162 { 163 hl_asic_dma_free_common(hdev, size, cpu_addr, dma_handle, DMA_ALLOC_COHERENT, caller); 164 } 165 166 void *hl_asic_dma_pool_zalloc_caller(struct hl_device *hdev, size_t size, gfp_t mem_flags, 167 dma_addr_t *dma_handle, const char *caller) 168 { 169 return hl_dma_alloc_common(hdev, size, dma_handle, mem_flags, DMA_ALLOC_POOL, caller); 170 } 171 172 void hl_asic_dma_pool_free_caller(struct hl_device *hdev, void *vaddr, dma_addr_t dma_addr, 173 const char *caller) 174 { 175 hl_asic_dma_free_common(hdev, 0, vaddr, dma_addr, DMA_ALLOC_POOL, caller); 176 } 177 178 void *hl_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle) 179 { 180 return hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, size, dma_handle); 181 } 182 183 void hl_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size, void *vaddr) 184 { 185 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, size, vaddr); 186 } 187 188 int hl_dma_map_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir) 189 { 190 struct asic_fixed_properties *prop = &hdev->asic_prop; 191 struct scatterlist *sg; 192 int rc, i; 193 194 rc = dma_map_sgtable(&hdev->pdev->dev, sgt, dir, 0); 195 if (rc) 196 return rc; 197 198 /* Shift to the device's base physical address of host memory if necessary */ 199 if (prop->device_dma_offset_for_host_access) 200 for_each_sgtable_dma_sg(sgt, sg, i) 201 sg->dma_address += prop->device_dma_offset_for_host_access; 202 203 return 0; 204 } 205 206 void hl_dma_unmap_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir) 207 { 208 struct asic_fixed_properties *prop = &hdev->asic_prop; 209 struct scatterlist *sg; 210 int i; 211 212 /* Cancel the device's base physical address of host memory if necessary */ 213 if (prop->device_dma_offset_for_host_access) 214 for_each_sgtable_dma_sg(sgt, sg, i) 215 sg->dma_address -= prop->device_dma_offset_for_host_access; 216 217 dma_unmap_sgtable(&hdev->pdev->dev, sgt, dir, 0); 218 } 219 220 /* 221 * hl_access_cfg_region - access the config region 222 * 223 * @hdev: pointer to habanalabs device structure 224 * @addr: the address to access 225 * @val: the value to write from or read to 226 * @acc_type: the type of access (read/write 64/32) 227 */ 228 int hl_access_cfg_region(struct hl_device *hdev, u64 addr, u64 *val, 229 enum debugfs_access_type acc_type) 230 { 231 struct pci_mem_region *cfg_region = &hdev->pci_mem_region[PCI_REGION_CFG]; 232 u32 val_h, val_l; 233 234 if (!IS_ALIGNED(addr, sizeof(u32))) { 235 dev_err(hdev->dev, "address %#llx not a multiple of %zu\n", addr, sizeof(u32)); 236 return -EINVAL; 237 } 238 239 switch (acc_type) { 240 case DEBUGFS_READ32: 241 *val = RREG32(addr - cfg_region->region_base); 242 break; 243 case DEBUGFS_WRITE32: 244 WREG32(addr - cfg_region->region_base, *val); 245 break; 246 case DEBUGFS_READ64: 247 val_l = RREG32(addr - cfg_region->region_base); 248 val_h = RREG32(addr + sizeof(u32) - cfg_region->region_base); 249 250 *val = (((u64) val_h) << 32) | val_l; 251 break; 252 case DEBUGFS_WRITE64: 253 WREG32(addr - cfg_region->region_base, lower_32_bits(*val)); 254 WREG32(addr + sizeof(u32) - cfg_region->region_base, upper_32_bits(*val)); 255 break; 256 default: 257 dev_err(hdev->dev, "access type %d is not supported\n", acc_type); 258 return -EOPNOTSUPP; 259 } 260 261 return 0; 262 } 263 264 /* 265 * hl_access_dev_mem - access device memory 266 * 267 * @hdev: pointer to habanalabs device structure 268 * @region_type: the type of the region the address belongs to 269 * @addr: the address to access 270 * @val: the value to write from or read to 271 * @acc_type: the type of access (r/w, 32/64) 272 */ 273 int hl_access_dev_mem(struct hl_device *hdev, enum pci_region region_type, 274 u64 addr, u64 *val, enum debugfs_access_type acc_type) 275 { 276 switch (region_type) { 277 case PCI_REGION_CFG: 278 return hl_access_cfg_region(hdev, addr, val, acc_type); 279 case PCI_REGION_SRAM: 280 case PCI_REGION_DRAM: 281 return hl_access_sram_dram_region(hdev, addr, val, acc_type, 282 region_type, (region_type == PCI_REGION_DRAM)); 283 default: 284 return -EFAULT; 285 } 286 287 return 0; 288 } 289 290 void hl_engine_data_sprintf(struct engines_data *e, const char *fmt, ...) 291 { 292 va_list args; 293 int str_size; 294 295 va_start(args, fmt); 296 /* Calculate formatted string length. Assuming each string is null terminated, hence 297 * increment result by 1 298 */ 299 str_size = vsnprintf(NULL, 0, fmt, args) + 1; 300 va_end(args); 301 302 if ((e->actual_size + str_size) < e->allocated_buf_size) { 303 va_start(args, fmt); 304 vsnprintf(e->buf + e->actual_size, str_size, fmt, args); 305 va_end(args); 306 } 307 308 /* Need to update the size even when not updating destination buffer to get the exact size 309 * of all input strings 310 */ 311 e->actual_size += str_size; 312 } 313 314 enum hl_device_status hl_device_status(struct hl_device *hdev) 315 { 316 enum hl_device_status status; 317 318 if (hdev->reset_info.in_reset) { 319 if (hdev->reset_info.in_compute_reset) 320 status = HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE; 321 else 322 status = HL_DEVICE_STATUS_IN_RESET; 323 } else if (hdev->reset_info.needs_reset) { 324 status = HL_DEVICE_STATUS_NEEDS_RESET; 325 } else if (hdev->disabled) { 326 status = HL_DEVICE_STATUS_MALFUNCTION; 327 } else if (!hdev->init_done) { 328 status = HL_DEVICE_STATUS_IN_DEVICE_CREATION; 329 } else { 330 status = HL_DEVICE_STATUS_OPERATIONAL; 331 } 332 333 return status; 334 } 335 336 bool hl_device_operational(struct hl_device *hdev, 337 enum hl_device_status *status) 338 { 339 enum hl_device_status current_status; 340 341 current_status = hl_device_status(hdev); 342 if (status) 343 *status = current_status; 344 345 switch (current_status) { 346 case HL_DEVICE_STATUS_IN_RESET: 347 case HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE: 348 case HL_DEVICE_STATUS_MALFUNCTION: 349 case HL_DEVICE_STATUS_NEEDS_RESET: 350 return false; 351 case HL_DEVICE_STATUS_OPERATIONAL: 352 case HL_DEVICE_STATUS_IN_DEVICE_CREATION: 353 default: 354 return true; 355 } 356 } 357 358 bool hl_ctrl_device_operational(struct hl_device *hdev, 359 enum hl_device_status *status) 360 { 361 enum hl_device_status current_status; 362 363 current_status = hl_device_status(hdev); 364 if (status) 365 *status = current_status; 366 367 switch (current_status) { 368 case HL_DEVICE_STATUS_MALFUNCTION: 369 return false; 370 case HL_DEVICE_STATUS_IN_RESET: 371 case HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE: 372 case HL_DEVICE_STATUS_NEEDS_RESET: 373 case HL_DEVICE_STATUS_OPERATIONAL: 374 case HL_DEVICE_STATUS_IN_DEVICE_CREATION: 375 default: 376 return true; 377 } 378 } 379 380 static void print_idle_status_mask(struct hl_device *hdev, const char *message, 381 u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE]) 382 { 383 if (idle_mask[3]) 384 dev_err(hdev->dev, "%s (mask %#llx_%016llx_%016llx_%016llx)\n", 385 message, idle_mask[3], idle_mask[2], idle_mask[1], idle_mask[0]); 386 else if (idle_mask[2]) 387 dev_err(hdev->dev, "%s (mask %#llx_%016llx_%016llx)\n", 388 message, idle_mask[2], idle_mask[1], idle_mask[0]); 389 else if (idle_mask[1]) 390 dev_err(hdev->dev, "%s (mask %#llx_%016llx)\n", 391 message, idle_mask[1], idle_mask[0]); 392 else 393 dev_err(hdev->dev, "%s (mask %#llx)\n", message, idle_mask[0]); 394 } 395 396 static void hpriv_release(struct kref *ref) 397 { 398 u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0}; 399 bool reset_device, device_is_idle = true; 400 struct hl_fpriv *hpriv; 401 struct hl_device *hdev; 402 403 hpriv = container_of(ref, struct hl_fpriv, refcount); 404 405 hdev = hpriv->hdev; 406 407 hdev->asic_funcs->send_device_activity(hdev, false); 408 409 put_pid(hpriv->taskpid); 410 411 hl_debugfs_remove_file(hpriv); 412 413 mutex_destroy(&hpriv->ctx_lock); 414 mutex_destroy(&hpriv->restore_phase_mutex); 415 416 /* There should be no memory buffers at this point and handles IDR can be destroyed */ 417 hl_mem_mgr_idr_destroy(&hpriv->mem_mgr); 418 419 /* Device should be reset if reset-upon-device-release is enabled, or if there is a pending 420 * reset that waits for device release. 421 */ 422 reset_device = hdev->reset_upon_device_release || hdev->reset_info.watchdog_active; 423 424 /* Check the device idle status and reset if not idle. 425 * Skip it if already in reset, or if device is going to be reset in any case. 426 */ 427 if (!hdev->reset_info.in_reset && !reset_device && hdev->pdev && !hdev->pldm) 428 device_is_idle = hdev->asic_funcs->is_device_idle(hdev, idle_mask, 429 HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL); 430 if (!device_is_idle) { 431 print_idle_status_mask(hdev, "device is not idle after user context is closed", 432 idle_mask); 433 reset_device = true; 434 } 435 436 /* We need to remove the user from the list to make sure the reset process won't 437 * try to kill the user process. Because, if we got here, it means there are no 438 * more driver/device resources that the user process is occupying so there is 439 * no need to kill it 440 * 441 * However, we can't set the compute_ctx to NULL at this stage. This is to prevent 442 * a race between the release and opening the device again. We don't want to let 443 * a user open the device while there a reset is about to happen. 444 */ 445 mutex_lock(&hdev->fpriv_list_lock); 446 list_del(&hpriv->dev_node); 447 mutex_unlock(&hdev->fpriv_list_lock); 448 449 if (reset_device) { 450 hl_device_reset(hdev, HL_DRV_RESET_DEV_RELEASE); 451 } else { 452 /* Scrubbing is handled within hl_device_reset(), so here need to do it directly */ 453 int rc = hdev->asic_funcs->scrub_device_mem(hdev); 454 455 if (rc) 456 dev_err(hdev->dev, "failed to scrub memory from hpriv release (%d)\n", rc); 457 } 458 459 /* Now we can mark the compute_ctx as not active. Even if a reset is running in a different 460 * thread, we don't care because the in_reset is marked so if a user will try to open 461 * the device it will fail on that, even if compute_ctx is false. 462 */ 463 mutex_lock(&hdev->fpriv_list_lock); 464 hdev->is_compute_ctx_active = false; 465 mutex_unlock(&hdev->fpriv_list_lock); 466 467 hdev->compute_ctx_in_release = 0; 468 469 /* release the eventfd */ 470 if (hpriv->notifier_event.eventfd) 471 eventfd_ctx_put(hpriv->notifier_event.eventfd); 472 473 mutex_destroy(&hpriv->notifier_event.lock); 474 475 kfree(hpriv); 476 } 477 478 void hl_hpriv_get(struct hl_fpriv *hpriv) 479 { 480 kref_get(&hpriv->refcount); 481 } 482 483 int hl_hpriv_put(struct hl_fpriv *hpriv) 484 { 485 return kref_put(&hpriv->refcount, hpriv_release); 486 } 487 488 static void print_device_in_use_info(struct hl_device *hdev, const char *message) 489 { 490 u32 active_cs_num, dmabuf_export_cnt; 491 bool unknown_reason = true; 492 char buf[128]; 493 size_t size; 494 int offset; 495 496 size = sizeof(buf); 497 offset = 0; 498 499 active_cs_num = hl_get_active_cs_num(hdev); 500 if (active_cs_num) { 501 unknown_reason = false; 502 offset += scnprintf(buf + offset, size - offset, " [%u active CS]", active_cs_num); 503 } 504 505 dmabuf_export_cnt = atomic_read(&hdev->dmabuf_export_cnt); 506 if (dmabuf_export_cnt) { 507 unknown_reason = false; 508 offset += scnprintf(buf + offset, size - offset, " [%u exported dma-buf]", 509 dmabuf_export_cnt); 510 } 511 512 if (unknown_reason) 513 scnprintf(buf + offset, size - offset, " [unknown reason]"); 514 515 dev_notice(hdev->dev, "%s%s\n", message, buf); 516 } 517 518 /* 519 * hl_device_release - release function for habanalabs device 520 * 521 * @inode: pointer to inode structure 522 * @filp: pointer to file structure 523 * 524 * Called when process closes an habanalabs device 525 */ 526 static int hl_device_release(struct inode *inode, struct file *filp) 527 { 528 struct hl_fpriv *hpriv = filp->private_data; 529 struct hl_device *hdev = hpriv->hdev; 530 531 filp->private_data = NULL; 532 533 if (!hdev) { 534 pr_crit("Closing FD after device was removed. Memory leak will occur and it is advised to reboot.\n"); 535 put_pid(hpriv->taskpid); 536 return 0; 537 } 538 539 hl_ctx_mgr_fini(hdev, &hpriv->ctx_mgr); 540 541 /* Memory buffers might be still in use at this point and thus the handles IDR destruction 542 * is postponed to hpriv_release(). 543 */ 544 hl_mem_mgr_fini(&hpriv->mem_mgr); 545 546 hdev->compute_ctx_in_release = 1; 547 548 if (!hl_hpriv_put(hpriv)) { 549 print_device_in_use_info(hdev, "User process closed FD but device still in use"); 550 hl_device_reset(hdev, HL_DRV_RESET_HARD); 551 } 552 553 hdev->last_open_session_duration_jif = jiffies - hdev->last_successful_open_jif; 554 555 return 0; 556 } 557 558 static int hl_device_release_ctrl(struct inode *inode, struct file *filp) 559 { 560 struct hl_fpriv *hpriv = filp->private_data; 561 struct hl_device *hdev = hpriv->hdev; 562 563 filp->private_data = NULL; 564 565 if (!hdev) { 566 pr_err("Closing FD after device was removed\n"); 567 goto out; 568 } 569 570 mutex_lock(&hdev->fpriv_ctrl_list_lock); 571 list_del(&hpriv->dev_node); 572 mutex_unlock(&hdev->fpriv_ctrl_list_lock); 573 out: 574 /* release the eventfd */ 575 if (hpriv->notifier_event.eventfd) 576 eventfd_ctx_put(hpriv->notifier_event.eventfd); 577 578 mutex_destroy(&hpriv->notifier_event.lock); 579 put_pid(hpriv->taskpid); 580 581 kfree(hpriv); 582 583 return 0; 584 } 585 586 /* 587 * hl_mmap - mmap function for habanalabs device 588 * 589 * @*filp: pointer to file structure 590 * @*vma: pointer to vm_area_struct of the process 591 * 592 * Called when process does an mmap on habanalabs device. Call the relevant mmap 593 * function at the end of the common code. 594 */ 595 static int hl_mmap(struct file *filp, struct vm_area_struct *vma) 596 { 597 struct hl_fpriv *hpriv = filp->private_data; 598 struct hl_device *hdev = hpriv->hdev; 599 unsigned long vm_pgoff; 600 601 if (!hdev) { 602 pr_err_ratelimited("Trying to mmap after device was removed! Please close FD\n"); 603 return -ENODEV; 604 } 605 606 vm_pgoff = vma->vm_pgoff; 607 608 switch (vm_pgoff & HL_MMAP_TYPE_MASK) { 609 case HL_MMAP_TYPE_BLOCK: 610 vma->vm_pgoff = HL_MMAP_OFFSET_VALUE_GET(vm_pgoff); 611 return hl_hw_block_mmap(hpriv, vma); 612 613 case HL_MMAP_TYPE_CB: 614 case HL_MMAP_TYPE_TS_BUFF: 615 return hl_mem_mgr_mmap(&hpriv->mem_mgr, vma, NULL); 616 } 617 return -EINVAL; 618 } 619 620 static const struct file_operations hl_ops = { 621 .owner = THIS_MODULE, 622 .open = hl_device_open, 623 .release = hl_device_release, 624 .mmap = hl_mmap, 625 .unlocked_ioctl = hl_ioctl, 626 .compat_ioctl = hl_ioctl 627 }; 628 629 static const struct file_operations hl_ctrl_ops = { 630 .owner = THIS_MODULE, 631 .open = hl_device_open_ctrl, 632 .release = hl_device_release_ctrl, 633 .unlocked_ioctl = hl_ioctl_control, 634 .compat_ioctl = hl_ioctl_control 635 }; 636 637 static void device_release_func(struct device *dev) 638 { 639 kfree(dev); 640 } 641 642 /* 643 * device_init_cdev - Initialize cdev and device for habanalabs device 644 * 645 * @hdev: pointer to habanalabs device structure 646 * @class: pointer to the class object of the device 647 * @minor: minor number of the specific device 648 * @fpos: file operations to install for this device 649 * @name: name of the device as it will appear in the filesystem 650 * @cdev: pointer to the char device object that will be initialized 651 * @dev: pointer to the device object that will be initialized 652 * 653 * Initialize a cdev and a Linux device for habanalabs's device. 654 */ 655 static int device_init_cdev(struct hl_device *hdev, struct class *class, 656 int minor, const struct file_operations *fops, 657 char *name, struct cdev *cdev, 658 struct device **dev) 659 { 660 cdev_init(cdev, fops); 661 cdev->owner = THIS_MODULE; 662 663 *dev = kzalloc(sizeof(**dev), GFP_KERNEL); 664 if (!*dev) 665 return -ENOMEM; 666 667 device_initialize(*dev); 668 (*dev)->devt = MKDEV(hdev->major, minor); 669 (*dev)->class = class; 670 (*dev)->release = device_release_func; 671 dev_set_drvdata(*dev, hdev); 672 dev_set_name(*dev, "%s", name); 673 674 return 0; 675 } 676 677 static int cdev_sysfs_debugfs_add(struct hl_device *hdev) 678 { 679 int rc; 680 681 rc = cdev_device_add(&hdev->cdev, hdev->dev); 682 if (rc) { 683 dev_err(hdev->dev, 684 "failed to add a char device to the system\n"); 685 return rc; 686 } 687 688 rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl); 689 if (rc) { 690 dev_err(hdev->dev, 691 "failed to add a control char device to the system\n"); 692 goto delete_cdev_device; 693 } 694 695 /* hl_sysfs_init() must be done after adding the device to the system */ 696 rc = hl_sysfs_init(hdev); 697 if (rc) { 698 dev_err(hdev->dev, "failed to initialize sysfs\n"); 699 goto delete_ctrl_cdev_device; 700 } 701 702 hl_debugfs_add_device(hdev); 703 704 hdev->cdev_sysfs_debugfs_created = true; 705 706 return 0; 707 708 delete_ctrl_cdev_device: 709 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl); 710 delete_cdev_device: 711 cdev_device_del(&hdev->cdev, hdev->dev); 712 return rc; 713 } 714 715 static void cdev_sysfs_debugfs_remove(struct hl_device *hdev) 716 { 717 if (!hdev->cdev_sysfs_debugfs_created) 718 goto put_devices; 719 720 hl_debugfs_remove_device(hdev); 721 hl_sysfs_fini(hdev); 722 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl); 723 cdev_device_del(&hdev->cdev, hdev->dev); 724 725 put_devices: 726 put_device(hdev->dev); 727 put_device(hdev->dev_ctrl); 728 } 729 730 static void device_hard_reset_pending(struct work_struct *work) 731 { 732 struct hl_device_reset_work *device_reset_work = 733 container_of(work, struct hl_device_reset_work, reset_work.work); 734 struct hl_device *hdev = device_reset_work->hdev; 735 u32 flags; 736 int rc; 737 738 flags = device_reset_work->flags | HL_DRV_RESET_FROM_RESET_THR; 739 740 rc = hl_device_reset(hdev, flags); 741 742 if ((rc == -EBUSY) && !hdev->device_fini_pending) { 743 struct hl_ctx *ctx = hl_get_compute_ctx(hdev); 744 745 if (ctx) { 746 /* The read refcount value should subtracted by one, because the read is 747 * protected with hl_get_compute_ctx(). 748 */ 749 dev_info(hdev->dev, 750 "Could not reset device (compute_ctx refcount %u). will try again in %u seconds", 751 kref_read(&ctx->refcount) - 1, HL_PENDING_RESET_PER_SEC); 752 hl_ctx_put(ctx); 753 } else { 754 dev_info(hdev->dev, "Could not reset device. will try again in %u seconds", 755 HL_PENDING_RESET_PER_SEC); 756 } 757 758 queue_delayed_work(hdev->reset_wq, &device_reset_work->reset_work, 759 msecs_to_jiffies(HL_PENDING_RESET_PER_SEC * 1000)); 760 } 761 } 762 763 static void device_release_watchdog_func(struct work_struct *work) 764 { 765 struct hl_device_reset_work *watchdog_work = 766 container_of(work, struct hl_device_reset_work, reset_work.work); 767 struct hl_device *hdev = watchdog_work->hdev; 768 u32 flags; 769 770 dev_dbg(hdev->dev, "Device wasn't released in time. Initiate hard-reset.\n"); 771 772 flags = watchdog_work->flags | HL_DRV_RESET_HARD | HL_DRV_RESET_FROM_WD_THR; 773 774 hl_device_reset(hdev, flags); 775 } 776 777 /* 778 * device_early_init - do some early initialization for the habanalabs device 779 * 780 * @hdev: pointer to habanalabs device structure 781 * 782 * Install the relevant function pointers and call the early_init function, 783 * if such a function exists 784 */ 785 static int device_early_init(struct hl_device *hdev) 786 { 787 int i, rc; 788 char workq_name[32]; 789 790 switch (hdev->asic_type) { 791 case ASIC_GOYA: 792 goya_set_asic_funcs(hdev); 793 strscpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name)); 794 break; 795 case ASIC_GAUDI: 796 gaudi_set_asic_funcs(hdev); 797 strscpy(hdev->asic_name, "GAUDI", sizeof(hdev->asic_name)); 798 break; 799 case ASIC_GAUDI_SEC: 800 gaudi_set_asic_funcs(hdev); 801 strscpy(hdev->asic_name, "GAUDI SEC", sizeof(hdev->asic_name)); 802 break; 803 case ASIC_GAUDI2: 804 gaudi2_set_asic_funcs(hdev); 805 strscpy(hdev->asic_name, "GAUDI2", sizeof(hdev->asic_name)); 806 break; 807 case ASIC_GAUDI2B: 808 gaudi2_set_asic_funcs(hdev); 809 strscpy(hdev->asic_name, "GAUDI2B", sizeof(hdev->asic_name)); 810 break; 811 break; 812 default: 813 dev_err(hdev->dev, "Unrecognized ASIC type %d\n", 814 hdev->asic_type); 815 return -EINVAL; 816 } 817 818 rc = hdev->asic_funcs->early_init(hdev); 819 if (rc) 820 return rc; 821 822 rc = hl_asid_init(hdev); 823 if (rc) 824 goto early_fini; 825 826 if (hdev->asic_prop.completion_queues_count) { 827 hdev->cq_wq = kcalloc(hdev->asic_prop.completion_queues_count, 828 sizeof(struct workqueue_struct *), 829 GFP_KERNEL); 830 if (!hdev->cq_wq) { 831 rc = -ENOMEM; 832 goto asid_fini; 833 } 834 } 835 836 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) { 837 snprintf(workq_name, 32, "hl%u-free-jobs-%u", hdev->cdev_idx, (u32) i); 838 hdev->cq_wq[i] = create_singlethread_workqueue(workq_name); 839 if (hdev->cq_wq[i] == NULL) { 840 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n"); 841 rc = -ENOMEM; 842 goto free_cq_wq; 843 } 844 } 845 846 snprintf(workq_name, 32, "hl%u-events", hdev->cdev_idx); 847 hdev->eq_wq = create_singlethread_workqueue(workq_name); 848 if (hdev->eq_wq == NULL) { 849 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n"); 850 rc = -ENOMEM; 851 goto free_cq_wq; 852 } 853 854 snprintf(workq_name, 32, "hl%u-cs-completions", hdev->cdev_idx); 855 hdev->cs_cmplt_wq = alloc_workqueue(workq_name, WQ_UNBOUND, 0); 856 if (!hdev->cs_cmplt_wq) { 857 dev_err(hdev->dev, 858 "Failed to allocate CS completions workqueue\n"); 859 rc = -ENOMEM; 860 goto free_eq_wq; 861 } 862 863 snprintf(workq_name, 32, "hl%u-ts-free-obj", hdev->cdev_idx); 864 hdev->ts_free_obj_wq = alloc_workqueue(workq_name, WQ_UNBOUND, 0); 865 if (!hdev->ts_free_obj_wq) { 866 dev_err(hdev->dev, 867 "Failed to allocate Timestamp registration free workqueue\n"); 868 rc = -ENOMEM; 869 goto free_cs_cmplt_wq; 870 } 871 872 snprintf(workq_name, 32, "hl%u-prefetch", hdev->cdev_idx); 873 hdev->prefetch_wq = alloc_workqueue(workq_name, WQ_UNBOUND, 0); 874 if (!hdev->prefetch_wq) { 875 dev_err(hdev->dev, "Failed to allocate MMU prefetch workqueue\n"); 876 rc = -ENOMEM; 877 goto free_ts_free_wq; 878 } 879 880 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info), GFP_KERNEL); 881 if (!hdev->hl_chip_info) { 882 rc = -ENOMEM; 883 goto free_prefetch_wq; 884 } 885 886 rc = hl_mmu_if_set_funcs(hdev); 887 if (rc) 888 goto free_chip_info; 889 890 hl_mem_mgr_init(hdev->dev, &hdev->kernel_mem_mgr); 891 892 snprintf(workq_name, 32, "hl%u_device_reset", hdev->cdev_idx); 893 hdev->reset_wq = create_singlethread_workqueue(workq_name); 894 if (!hdev->reset_wq) { 895 rc = -ENOMEM; 896 dev_err(hdev->dev, "Failed to create device reset WQ\n"); 897 goto free_cb_mgr; 898 } 899 900 INIT_DELAYED_WORK(&hdev->device_reset_work.reset_work, device_hard_reset_pending); 901 hdev->device_reset_work.hdev = hdev; 902 hdev->device_fini_pending = 0; 903 904 INIT_DELAYED_WORK(&hdev->device_release_watchdog_work.reset_work, 905 device_release_watchdog_func); 906 hdev->device_release_watchdog_work.hdev = hdev; 907 908 mutex_init(&hdev->send_cpu_message_lock); 909 mutex_init(&hdev->debug_lock); 910 INIT_LIST_HEAD(&hdev->cs_mirror_list); 911 spin_lock_init(&hdev->cs_mirror_lock); 912 spin_lock_init(&hdev->reset_info.lock); 913 INIT_LIST_HEAD(&hdev->fpriv_list); 914 INIT_LIST_HEAD(&hdev->fpriv_ctrl_list); 915 mutex_init(&hdev->fpriv_list_lock); 916 mutex_init(&hdev->fpriv_ctrl_list_lock); 917 mutex_init(&hdev->clk_throttling.lock); 918 919 return 0; 920 921 free_cb_mgr: 922 hl_mem_mgr_fini(&hdev->kernel_mem_mgr); 923 hl_mem_mgr_idr_destroy(&hdev->kernel_mem_mgr); 924 free_chip_info: 925 kfree(hdev->hl_chip_info); 926 free_prefetch_wq: 927 destroy_workqueue(hdev->prefetch_wq); 928 free_ts_free_wq: 929 destroy_workqueue(hdev->ts_free_obj_wq); 930 free_cs_cmplt_wq: 931 destroy_workqueue(hdev->cs_cmplt_wq); 932 free_eq_wq: 933 destroy_workqueue(hdev->eq_wq); 934 free_cq_wq: 935 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) 936 if (hdev->cq_wq[i]) 937 destroy_workqueue(hdev->cq_wq[i]); 938 kfree(hdev->cq_wq); 939 asid_fini: 940 hl_asid_fini(hdev); 941 early_fini: 942 if (hdev->asic_funcs->early_fini) 943 hdev->asic_funcs->early_fini(hdev); 944 945 return rc; 946 } 947 948 /* 949 * device_early_fini - finalize all that was done in device_early_init 950 * 951 * @hdev: pointer to habanalabs device structure 952 * 953 */ 954 static void device_early_fini(struct hl_device *hdev) 955 { 956 int i; 957 958 mutex_destroy(&hdev->debug_lock); 959 mutex_destroy(&hdev->send_cpu_message_lock); 960 961 mutex_destroy(&hdev->fpriv_list_lock); 962 mutex_destroy(&hdev->fpriv_ctrl_list_lock); 963 964 mutex_destroy(&hdev->clk_throttling.lock); 965 966 hl_mem_mgr_fini(&hdev->kernel_mem_mgr); 967 hl_mem_mgr_idr_destroy(&hdev->kernel_mem_mgr); 968 969 kfree(hdev->hl_chip_info); 970 971 destroy_workqueue(hdev->prefetch_wq); 972 destroy_workqueue(hdev->ts_free_obj_wq); 973 destroy_workqueue(hdev->cs_cmplt_wq); 974 destroy_workqueue(hdev->eq_wq); 975 destroy_workqueue(hdev->reset_wq); 976 977 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) 978 destroy_workqueue(hdev->cq_wq[i]); 979 kfree(hdev->cq_wq); 980 981 hl_asid_fini(hdev); 982 983 if (hdev->asic_funcs->early_fini) 984 hdev->asic_funcs->early_fini(hdev); 985 } 986 987 static bool is_pci_link_healthy(struct hl_device *hdev) 988 { 989 u16 vendor_id; 990 991 if (!hdev->pdev) 992 return false; 993 994 pci_read_config_word(hdev->pdev, PCI_VENDOR_ID, &vendor_id); 995 996 return (vendor_id == PCI_VENDOR_ID_HABANALABS); 997 } 998 999 static void hl_device_heartbeat(struct work_struct *work) 1000 { 1001 struct hl_device *hdev = container_of(work, struct hl_device, 1002 work_heartbeat.work); 1003 struct hl_info_fw_err_info info = {0}; 1004 u64 event_mask = HL_NOTIFIER_EVENT_DEVICE_RESET | HL_NOTIFIER_EVENT_DEVICE_UNAVAILABLE; 1005 1006 if (!hl_device_operational(hdev, NULL)) 1007 goto reschedule; 1008 1009 if (!hdev->asic_funcs->send_heartbeat(hdev)) 1010 goto reschedule; 1011 1012 if (hl_device_operational(hdev, NULL)) 1013 dev_err(hdev->dev, "Device heartbeat failed! PCI link is %s\n", 1014 is_pci_link_healthy(hdev) ? "healthy" : "broken"); 1015 1016 info.err_type = HL_INFO_FW_HEARTBEAT_ERR; 1017 info.event_mask = &event_mask; 1018 hl_handle_fw_err(hdev, &info); 1019 hl_device_cond_reset(hdev, HL_DRV_RESET_HARD | HL_DRV_RESET_HEARTBEAT, event_mask); 1020 1021 return; 1022 1023 reschedule: 1024 /* 1025 * prev_reset_trigger tracks consecutive fatal h/w errors until first 1026 * heartbeat immediately post reset. 1027 * If control reached here, then at least one heartbeat work has been 1028 * scheduled since last reset/init cycle. 1029 * So if the device is not already in reset cycle, reset the flag 1030 * prev_reset_trigger as no reset occurred with HL_DRV_RESET_FW_FATAL_ERR 1031 * status for at least one heartbeat. From this point driver restarts 1032 * tracking future consecutive fatal errors. 1033 */ 1034 if (!hdev->reset_info.in_reset) 1035 hdev->reset_info.prev_reset_trigger = HL_RESET_TRIGGER_DEFAULT; 1036 1037 schedule_delayed_work(&hdev->work_heartbeat, 1038 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC)); 1039 } 1040 1041 /* 1042 * device_late_init - do late stuff initialization for the habanalabs device 1043 * 1044 * @hdev: pointer to habanalabs device structure 1045 * 1046 * Do stuff that either needs the device H/W queues to be active or needs 1047 * to happen after all the rest of the initialization is finished 1048 */ 1049 static int device_late_init(struct hl_device *hdev) 1050 { 1051 int rc; 1052 1053 if (hdev->asic_funcs->late_init) { 1054 rc = hdev->asic_funcs->late_init(hdev); 1055 if (rc) { 1056 dev_err(hdev->dev, 1057 "failed late initialization for the H/W\n"); 1058 return rc; 1059 } 1060 } 1061 1062 hdev->high_pll = hdev->asic_prop.high_pll; 1063 1064 if (hdev->heartbeat) { 1065 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat); 1066 schedule_delayed_work(&hdev->work_heartbeat, 1067 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC)); 1068 } 1069 1070 hdev->late_init_done = true; 1071 1072 return 0; 1073 } 1074 1075 /* 1076 * device_late_fini - finalize all that was done in device_late_init 1077 * 1078 * @hdev: pointer to habanalabs device structure 1079 * 1080 */ 1081 static void device_late_fini(struct hl_device *hdev) 1082 { 1083 if (!hdev->late_init_done) 1084 return; 1085 1086 if (hdev->heartbeat) 1087 cancel_delayed_work_sync(&hdev->work_heartbeat); 1088 1089 if (hdev->asic_funcs->late_fini) 1090 hdev->asic_funcs->late_fini(hdev); 1091 1092 hdev->late_init_done = false; 1093 } 1094 1095 int hl_device_utilization(struct hl_device *hdev, u32 *utilization) 1096 { 1097 u64 max_power, curr_power, dc_power, dividend, divisor; 1098 int rc; 1099 1100 max_power = hdev->max_power; 1101 dc_power = hdev->asic_prop.dc_power_default; 1102 divisor = max_power - dc_power; 1103 if (!divisor) { 1104 dev_warn(hdev->dev, "device utilization is not supported\n"); 1105 return -EOPNOTSUPP; 1106 } 1107 rc = hl_fw_cpucp_power_get(hdev, &curr_power); 1108 1109 if (rc) 1110 return rc; 1111 1112 curr_power = clamp(curr_power, dc_power, max_power); 1113 1114 dividend = (curr_power - dc_power) * 100; 1115 *utilization = (u32) div_u64(dividend, divisor); 1116 1117 return 0; 1118 } 1119 1120 int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable) 1121 { 1122 int rc = 0; 1123 1124 mutex_lock(&hdev->debug_lock); 1125 1126 if (!enable) { 1127 if (!hdev->in_debug) { 1128 dev_err(hdev->dev, 1129 "Failed to disable debug mode because device was not in debug mode\n"); 1130 rc = -EFAULT; 1131 goto out; 1132 } 1133 1134 if (!hdev->reset_info.hard_reset_pending) 1135 hdev->asic_funcs->halt_coresight(hdev, ctx); 1136 1137 hdev->in_debug = 0; 1138 1139 goto out; 1140 } 1141 1142 if (hdev->in_debug) { 1143 dev_err(hdev->dev, 1144 "Failed to enable debug mode because device is already in debug mode\n"); 1145 rc = -EFAULT; 1146 goto out; 1147 } 1148 1149 hdev->in_debug = 1; 1150 1151 out: 1152 mutex_unlock(&hdev->debug_lock); 1153 1154 return rc; 1155 } 1156 1157 static void take_release_locks(struct hl_device *hdev) 1158 { 1159 /* Flush anyone that is inside the critical section of enqueue 1160 * jobs to the H/W 1161 */ 1162 hdev->asic_funcs->hw_queues_lock(hdev); 1163 hdev->asic_funcs->hw_queues_unlock(hdev); 1164 1165 /* Flush processes that are sending message to CPU */ 1166 mutex_lock(&hdev->send_cpu_message_lock); 1167 mutex_unlock(&hdev->send_cpu_message_lock); 1168 1169 /* Flush anyone that is inside device open */ 1170 mutex_lock(&hdev->fpriv_list_lock); 1171 mutex_unlock(&hdev->fpriv_list_lock); 1172 mutex_lock(&hdev->fpriv_ctrl_list_lock); 1173 mutex_unlock(&hdev->fpriv_ctrl_list_lock); 1174 } 1175 1176 static void hl_abort_waiting_for_completions(struct hl_device *hdev) 1177 { 1178 hl_abort_waiting_for_cs_completions(hdev); 1179 1180 /* Release all pending user interrupts, each pending user interrupt 1181 * holds a reference to a user context. 1182 */ 1183 hl_release_pending_user_interrupts(hdev); 1184 } 1185 1186 static void cleanup_resources(struct hl_device *hdev, bool hard_reset, bool fw_reset, 1187 bool skip_wq_flush) 1188 { 1189 if (hard_reset) 1190 device_late_fini(hdev); 1191 1192 /* 1193 * Halt the engines and disable interrupts so we won't get any more 1194 * completions from H/W and we won't have any accesses from the 1195 * H/W to the host machine 1196 */ 1197 hdev->asic_funcs->halt_engines(hdev, hard_reset, fw_reset); 1198 1199 /* Go over all the queues, release all CS and their jobs */ 1200 hl_cs_rollback_all(hdev, skip_wq_flush); 1201 1202 /* flush the MMU prefetch workqueue */ 1203 flush_workqueue(hdev->prefetch_wq); 1204 1205 hl_abort_waiting_for_completions(hdev); 1206 } 1207 1208 /* 1209 * hl_device_suspend - initiate device suspend 1210 * 1211 * @hdev: pointer to habanalabs device structure 1212 * 1213 * Puts the hw in the suspend state (all asics). 1214 * Returns 0 for success or an error on failure. 1215 * Called at driver suspend. 1216 */ 1217 int hl_device_suspend(struct hl_device *hdev) 1218 { 1219 int rc; 1220 1221 pci_save_state(hdev->pdev); 1222 1223 /* Block future CS/VM/JOB completion operations */ 1224 spin_lock(&hdev->reset_info.lock); 1225 if (hdev->reset_info.in_reset) { 1226 spin_unlock(&hdev->reset_info.lock); 1227 dev_err(hdev->dev, "Can't suspend while in reset\n"); 1228 return -EIO; 1229 } 1230 hdev->reset_info.in_reset = 1; 1231 spin_unlock(&hdev->reset_info.lock); 1232 1233 /* This blocks all other stuff that is not blocked by in_reset */ 1234 hdev->disabled = true; 1235 1236 take_release_locks(hdev); 1237 1238 rc = hdev->asic_funcs->suspend(hdev); 1239 if (rc) 1240 dev_err(hdev->dev, 1241 "Failed to disable PCI access of device CPU\n"); 1242 1243 /* Shut down the device */ 1244 pci_disable_device(hdev->pdev); 1245 pci_set_power_state(hdev->pdev, PCI_D3hot); 1246 1247 return 0; 1248 } 1249 1250 /* 1251 * hl_device_resume - initiate device resume 1252 * 1253 * @hdev: pointer to habanalabs device structure 1254 * 1255 * Bring the hw back to operating state (all asics). 1256 * Returns 0 for success or an error on failure. 1257 * Called at driver resume. 1258 */ 1259 int hl_device_resume(struct hl_device *hdev) 1260 { 1261 int rc; 1262 1263 pci_set_power_state(hdev->pdev, PCI_D0); 1264 pci_restore_state(hdev->pdev); 1265 rc = pci_enable_device_mem(hdev->pdev); 1266 if (rc) { 1267 dev_err(hdev->dev, 1268 "Failed to enable PCI device in resume\n"); 1269 return rc; 1270 } 1271 1272 pci_set_master(hdev->pdev); 1273 1274 rc = hdev->asic_funcs->resume(hdev); 1275 if (rc) { 1276 dev_err(hdev->dev, "Failed to resume device after suspend\n"); 1277 goto disable_device; 1278 } 1279 1280 1281 /* 'in_reset' was set to true during suspend, now we must clear it in order 1282 * for hard reset to be performed 1283 */ 1284 spin_lock(&hdev->reset_info.lock); 1285 hdev->reset_info.in_reset = 0; 1286 spin_unlock(&hdev->reset_info.lock); 1287 1288 rc = hl_device_reset(hdev, HL_DRV_RESET_HARD); 1289 if (rc) { 1290 dev_err(hdev->dev, "Failed to reset device during resume\n"); 1291 goto disable_device; 1292 } 1293 1294 return 0; 1295 1296 disable_device: 1297 pci_disable_device(hdev->pdev); 1298 1299 return rc; 1300 } 1301 1302 static int device_kill_open_processes(struct hl_device *hdev, u32 timeout, bool control_dev) 1303 { 1304 struct task_struct *task = NULL; 1305 struct list_head *fd_list; 1306 struct hl_fpriv *hpriv; 1307 struct mutex *fd_lock; 1308 u32 pending_cnt; 1309 1310 fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock; 1311 fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list; 1312 1313 /* Giving time for user to close FD, and for processes that are inside 1314 * hl_device_open to finish 1315 */ 1316 if (!list_empty(fd_list)) 1317 ssleep(1); 1318 1319 if (timeout) { 1320 pending_cnt = timeout; 1321 } else { 1322 if (hdev->process_kill_trial_cnt) { 1323 /* Processes have been already killed */ 1324 pending_cnt = 1; 1325 goto wait_for_processes; 1326 } else { 1327 /* Wait a small period after process kill */ 1328 pending_cnt = HL_PENDING_RESET_PER_SEC; 1329 } 1330 } 1331 1332 mutex_lock(fd_lock); 1333 1334 /* This section must be protected because we are dereferencing 1335 * pointers that are freed if the process exits 1336 */ 1337 list_for_each_entry(hpriv, fd_list, dev_node) { 1338 task = get_pid_task(hpriv->taskpid, PIDTYPE_PID); 1339 if (task) { 1340 dev_info(hdev->dev, "Killing user process pid=%d\n", 1341 task_pid_nr(task)); 1342 send_sig(SIGKILL, task, 1); 1343 usleep_range(1000, 10000); 1344 1345 put_task_struct(task); 1346 } else { 1347 /* 1348 * If we got here, it means that process was killed from outside the driver 1349 * right after it started looping on fd_list and before get_pid_task, thus 1350 * we don't need to kill it. 1351 */ 1352 dev_dbg(hdev->dev, 1353 "Can't get task struct for user process, assuming process was killed from outside the driver\n"); 1354 } 1355 } 1356 1357 mutex_unlock(fd_lock); 1358 1359 /* 1360 * We killed the open users, but that doesn't mean they are closed. 1361 * It could be that they are running a long cleanup phase in the driver 1362 * e.g. MMU unmappings, or running other long teardown flow even before 1363 * our cleanup. 1364 * Therefore we need to wait again to make sure they are closed before 1365 * continuing with the reset. 1366 */ 1367 1368 wait_for_processes: 1369 while ((!list_empty(fd_list)) && (pending_cnt)) { 1370 dev_dbg(hdev->dev, 1371 "Waiting for all unmap operations to finish before hard reset\n"); 1372 1373 pending_cnt--; 1374 1375 ssleep(1); 1376 } 1377 1378 /* All processes exited successfully */ 1379 if (list_empty(fd_list)) 1380 return 0; 1381 1382 /* Give up waiting for processes to exit */ 1383 if (hdev->process_kill_trial_cnt == HL_PENDING_RESET_MAX_TRIALS) 1384 return -ETIME; 1385 1386 hdev->process_kill_trial_cnt++; 1387 1388 return -EBUSY; 1389 } 1390 1391 static void device_disable_open_processes(struct hl_device *hdev, bool control_dev) 1392 { 1393 struct list_head *fd_list; 1394 struct hl_fpriv *hpriv; 1395 struct mutex *fd_lock; 1396 1397 fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock; 1398 fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list; 1399 1400 mutex_lock(fd_lock); 1401 list_for_each_entry(hpriv, fd_list, dev_node) 1402 hpriv->hdev = NULL; 1403 mutex_unlock(fd_lock); 1404 } 1405 1406 static void send_disable_pci_access(struct hl_device *hdev, u32 flags) 1407 { 1408 /* If reset is due to heartbeat, device CPU is no responsive in 1409 * which case no point sending PCI disable message to it. 1410 */ 1411 if ((flags & HL_DRV_RESET_HARD) && 1412 !(flags & (HL_DRV_RESET_HEARTBEAT | HL_DRV_RESET_BYPASS_REQ_TO_FW))) { 1413 /* Disable PCI access from device F/W so he won't send 1414 * us additional interrupts. We disable MSI/MSI-X at 1415 * the halt_engines function and we can't have the F/W 1416 * sending us interrupts after that. We need to disable 1417 * the access here because if the device is marked 1418 * disable, the message won't be send. Also, in case 1419 * of heartbeat, the device CPU is marked as disable 1420 * so this message won't be sent 1421 */ 1422 if (hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0)) { 1423 dev_warn(hdev->dev, "Failed to disable FW's PCI access\n"); 1424 return; 1425 } 1426 1427 /* verify that last EQs are handled before disabled is set */ 1428 if (hdev->cpu_queues_enable) 1429 synchronize_irq(pci_irq_vector(hdev->pdev, 1430 hdev->asic_prop.eq_interrupt_id)); 1431 } 1432 } 1433 1434 static void handle_reset_trigger(struct hl_device *hdev, u32 flags) 1435 { 1436 u32 cur_reset_trigger = HL_RESET_TRIGGER_DEFAULT; 1437 1438 /* No consecutive mechanism when user context exists */ 1439 if (hdev->is_compute_ctx_active) 1440 return; 1441 1442 /* 1443 * 'reset cause' is being updated here, because getting here 1444 * means that it's the 1st time and the last time we're here 1445 * ('in_reset' makes sure of it). This makes sure that 1446 * 'reset_cause' will continue holding its 1st recorded reason! 1447 */ 1448 if (flags & HL_DRV_RESET_HEARTBEAT) { 1449 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_HEARTBEAT; 1450 cur_reset_trigger = HL_DRV_RESET_HEARTBEAT; 1451 } else if (flags & HL_DRV_RESET_TDR) { 1452 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_TDR; 1453 cur_reset_trigger = HL_DRV_RESET_TDR; 1454 } else if (flags & HL_DRV_RESET_FW_FATAL_ERR) { 1455 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN; 1456 cur_reset_trigger = HL_DRV_RESET_FW_FATAL_ERR; 1457 } else { 1458 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN; 1459 } 1460 1461 /* 1462 * If reset cause is same twice, then reset_trigger_repeated 1463 * is set and if this reset is due to a fatal FW error 1464 * device is set to an unstable state. 1465 */ 1466 if (hdev->reset_info.prev_reset_trigger != cur_reset_trigger) { 1467 hdev->reset_info.prev_reset_trigger = cur_reset_trigger; 1468 hdev->reset_info.reset_trigger_repeated = 0; 1469 } else { 1470 hdev->reset_info.reset_trigger_repeated = 1; 1471 } 1472 } 1473 1474 /* 1475 * hl_device_reset - reset the device 1476 * 1477 * @hdev: pointer to habanalabs device structure 1478 * @flags: reset flags. 1479 * 1480 * Block future CS and wait for pending CS to be enqueued 1481 * Call ASIC H/W fini 1482 * Flush all completions 1483 * Re-initialize all internal data structures 1484 * Call ASIC H/W init, late_init 1485 * Test queues 1486 * Enable device 1487 * 1488 * Returns 0 for success or an error on failure. 1489 */ 1490 int hl_device_reset(struct hl_device *hdev, u32 flags) 1491 { 1492 bool hard_reset, from_hard_reset_thread, fw_reset, reset_upon_device_release, 1493 schedule_hard_reset = false, delay_reset, from_dev_release, from_watchdog_thread; 1494 u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0}; 1495 struct hl_ctx *ctx; 1496 int i, rc, hw_fini_rc; 1497 1498 if (!hdev->init_done) { 1499 dev_err(hdev->dev, "Can't reset before initialization is done\n"); 1500 return 0; 1501 } 1502 1503 hard_reset = !!(flags & HL_DRV_RESET_HARD); 1504 from_hard_reset_thread = !!(flags & HL_DRV_RESET_FROM_RESET_THR); 1505 fw_reset = !!(flags & HL_DRV_RESET_BYPASS_REQ_TO_FW); 1506 from_dev_release = !!(flags & HL_DRV_RESET_DEV_RELEASE); 1507 delay_reset = !!(flags & HL_DRV_RESET_DELAY); 1508 from_watchdog_thread = !!(flags & HL_DRV_RESET_FROM_WD_THR); 1509 reset_upon_device_release = hdev->reset_upon_device_release && from_dev_release; 1510 1511 if (!hard_reset && (hl_device_status(hdev) == HL_DEVICE_STATUS_MALFUNCTION)) { 1512 dev_dbg(hdev->dev, "soft-reset isn't supported on a malfunctioning device\n"); 1513 return 0; 1514 } 1515 1516 if (!hard_reset && !hdev->asic_prop.supports_compute_reset) { 1517 dev_dbg(hdev->dev, "asic doesn't support compute reset - do hard-reset instead\n"); 1518 hard_reset = true; 1519 } 1520 1521 if (reset_upon_device_release) { 1522 if (hard_reset) { 1523 dev_crit(hdev->dev, 1524 "Aborting reset because hard-reset is mutually exclusive with reset-on-device-release\n"); 1525 return -EINVAL; 1526 } 1527 1528 goto do_reset; 1529 } 1530 1531 if (!hard_reset && !hdev->asic_prop.allow_inference_soft_reset) { 1532 dev_dbg(hdev->dev, 1533 "asic doesn't allow inference soft reset - do hard-reset instead\n"); 1534 hard_reset = true; 1535 } 1536 1537 do_reset: 1538 /* Re-entry of reset thread */ 1539 if (from_hard_reset_thread && hdev->process_kill_trial_cnt) 1540 goto kill_processes; 1541 1542 /* 1543 * Prevent concurrency in this function - only one reset should be 1544 * done at any given time. We need to perform this only if we didn't 1545 * get here from a dedicated hard reset thread. 1546 */ 1547 if (!from_hard_reset_thread) { 1548 /* Block future CS/VM/JOB completion operations */ 1549 spin_lock(&hdev->reset_info.lock); 1550 if (hdev->reset_info.in_reset) { 1551 /* We allow scheduling of a hard reset only during a compute reset */ 1552 if (hard_reset && hdev->reset_info.in_compute_reset) 1553 hdev->reset_info.hard_reset_schedule_flags = flags; 1554 spin_unlock(&hdev->reset_info.lock); 1555 return 0; 1556 } 1557 1558 /* This still allows the completion of some KDMA ops 1559 * Update this before in_reset because in_compute_reset implies we are in reset 1560 */ 1561 hdev->reset_info.in_compute_reset = !hard_reset; 1562 1563 hdev->reset_info.in_reset = 1; 1564 1565 spin_unlock(&hdev->reset_info.lock); 1566 1567 /* Cancel the device release watchdog work if required. 1568 * In case of reset-upon-device-release while the release watchdog work is 1569 * scheduled due to a hard-reset, do hard-reset instead of compute-reset. 1570 */ 1571 if ((hard_reset || from_dev_release) && hdev->reset_info.watchdog_active) { 1572 struct hl_device_reset_work *watchdog_work = 1573 &hdev->device_release_watchdog_work; 1574 1575 hdev->reset_info.watchdog_active = 0; 1576 if (!from_watchdog_thread) 1577 cancel_delayed_work_sync(&watchdog_work->reset_work); 1578 1579 if (from_dev_release && (watchdog_work->flags & HL_DRV_RESET_HARD)) { 1580 hdev->reset_info.in_compute_reset = 0; 1581 flags |= HL_DRV_RESET_HARD; 1582 flags &= ~HL_DRV_RESET_DEV_RELEASE; 1583 hard_reset = true; 1584 } 1585 } 1586 1587 if (delay_reset) 1588 usleep_range(HL_RESET_DELAY_USEC, HL_RESET_DELAY_USEC << 1); 1589 1590 escalate_reset_flow: 1591 handle_reset_trigger(hdev, flags); 1592 send_disable_pci_access(hdev, flags); 1593 1594 /* This also blocks future CS/VM/JOB completion operations */ 1595 hdev->disabled = true; 1596 1597 take_release_locks(hdev); 1598 1599 if (hard_reset) 1600 dev_info(hdev->dev, "Going to reset device\n"); 1601 else if (reset_upon_device_release) 1602 dev_dbg(hdev->dev, "Going to reset device after release by user\n"); 1603 else 1604 dev_dbg(hdev->dev, "Going to reset engines of inference device\n"); 1605 } 1606 1607 if ((hard_reset) && (!from_hard_reset_thread)) { 1608 hdev->reset_info.hard_reset_pending = true; 1609 1610 hdev->process_kill_trial_cnt = 0; 1611 1612 hdev->device_reset_work.flags = flags; 1613 1614 /* 1615 * Because the reset function can't run from heartbeat work, 1616 * we need to call the reset function from a dedicated work. 1617 */ 1618 queue_delayed_work(hdev->reset_wq, &hdev->device_reset_work.reset_work, 0); 1619 1620 return 0; 1621 } 1622 1623 cleanup_resources(hdev, hard_reset, fw_reset, from_dev_release); 1624 1625 kill_processes: 1626 if (hard_reset) { 1627 /* Kill processes here after CS rollback. This is because the 1628 * process can't really exit until all its CSs are done, which 1629 * is what we do in cs rollback 1630 */ 1631 rc = device_kill_open_processes(hdev, 0, false); 1632 1633 if (rc == -EBUSY) { 1634 if (hdev->device_fini_pending) { 1635 dev_crit(hdev->dev, 1636 "%s Failed to kill all open processes, stopping hard reset\n", 1637 dev_name(&(hdev)->pdev->dev)); 1638 goto out_err; 1639 } 1640 1641 /* signal reset thread to reschedule */ 1642 return rc; 1643 } 1644 1645 if (rc) { 1646 dev_crit(hdev->dev, 1647 "%s Failed to kill all open processes, stopping hard reset\n", 1648 dev_name(&(hdev)->pdev->dev)); 1649 goto out_err; 1650 } 1651 1652 /* Flush the Event queue workers to make sure no other thread is 1653 * reading or writing to registers during the reset 1654 */ 1655 flush_workqueue(hdev->eq_wq); 1656 } 1657 1658 /* Reset the H/W. It will be in idle state after this returns */ 1659 hw_fini_rc = hdev->asic_funcs->hw_fini(hdev, hard_reset, fw_reset); 1660 1661 if (hard_reset) { 1662 hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE; 1663 1664 /* Release kernel context */ 1665 if (hdev->kernel_ctx && hl_ctx_put(hdev->kernel_ctx) == 1) 1666 hdev->kernel_ctx = NULL; 1667 1668 hl_vm_fini(hdev); 1669 hl_mmu_fini(hdev); 1670 hl_eq_reset(hdev, &hdev->event_queue); 1671 } 1672 1673 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */ 1674 hl_hw_queue_reset(hdev, hard_reset); 1675 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) 1676 hl_cq_reset(hdev, &hdev->completion_queue[i]); 1677 1678 /* Make sure the context switch phase will run again */ 1679 ctx = hl_get_compute_ctx(hdev); 1680 if (ctx) { 1681 atomic_set(&ctx->thread_ctx_switch_token, 1); 1682 ctx->thread_ctx_switch_wait_token = 0; 1683 hl_ctx_put(ctx); 1684 } 1685 1686 if (hw_fini_rc) { 1687 rc = hw_fini_rc; 1688 goto out_err; 1689 } 1690 /* Finished tear-down, starting to re-initialize */ 1691 1692 if (hard_reset) { 1693 hdev->device_cpu_disabled = false; 1694 hdev->reset_info.hard_reset_pending = false; 1695 1696 if (hdev->reset_info.reset_trigger_repeated && 1697 (hdev->reset_info.prev_reset_trigger == 1698 HL_DRV_RESET_FW_FATAL_ERR)) { 1699 /* if there 2 back to back resets from FW, 1700 * ensure driver puts the driver in a unusable state 1701 */ 1702 dev_crit(hdev->dev, 1703 "%s Consecutive FW fatal errors received, stopping hard reset\n", 1704 dev_name(&(hdev)->pdev->dev)); 1705 rc = -EIO; 1706 goto out_err; 1707 } 1708 1709 if (hdev->kernel_ctx) { 1710 dev_crit(hdev->dev, 1711 "%s kernel ctx was alive during hard reset, something is terribly wrong\n", 1712 dev_name(&(hdev)->pdev->dev)); 1713 rc = -EBUSY; 1714 goto out_err; 1715 } 1716 1717 rc = hl_mmu_init(hdev); 1718 if (rc) { 1719 dev_err(hdev->dev, 1720 "Failed to initialize MMU S/W after hard reset\n"); 1721 goto out_err; 1722 } 1723 1724 /* Allocate the kernel context */ 1725 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), 1726 GFP_KERNEL); 1727 if (!hdev->kernel_ctx) { 1728 rc = -ENOMEM; 1729 hl_mmu_fini(hdev); 1730 goto out_err; 1731 } 1732 1733 hdev->is_compute_ctx_active = false; 1734 1735 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true); 1736 if (rc) { 1737 dev_err(hdev->dev, 1738 "failed to init kernel ctx in hard reset\n"); 1739 kfree(hdev->kernel_ctx); 1740 hdev->kernel_ctx = NULL; 1741 hl_mmu_fini(hdev); 1742 goto out_err; 1743 } 1744 } 1745 1746 /* Device is now enabled as part of the initialization requires 1747 * communication with the device firmware to get information that 1748 * is required for the initialization itself 1749 */ 1750 hdev->disabled = false; 1751 1752 /* F/W security enabled indication might be updated after hard-reset */ 1753 if (hard_reset) { 1754 rc = hl_fw_read_preboot_status(hdev); 1755 if (rc) 1756 goto out_err; 1757 } 1758 1759 rc = hdev->asic_funcs->hw_init(hdev); 1760 if (rc) { 1761 dev_err(hdev->dev, "failed to initialize the H/W after reset\n"); 1762 goto out_err; 1763 } 1764 1765 /* If device is not idle fail the reset process */ 1766 if (!hdev->asic_funcs->is_device_idle(hdev, idle_mask, 1767 HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL)) { 1768 print_idle_status_mask(hdev, "device is not idle after reset", idle_mask); 1769 rc = -EIO; 1770 goto out_err; 1771 } 1772 1773 /* Check that the communication with the device is working */ 1774 rc = hdev->asic_funcs->test_queues(hdev); 1775 if (rc) { 1776 dev_err(hdev->dev, "Failed to detect if device is alive after reset\n"); 1777 goto out_err; 1778 } 1779 1780 if (hard_reset) { 1781 rc = device_late_init(hdev); 1782 if (rc) { 1783 dev_err(hdev->dev, "Failed late init after hard reset\n"); 1784 goto out_err; 1785 } 1786 1787 rc = hl_vm_init(hdev); 1788 if (rc) { 1789 dev_err(hdev->dev, "Failed to init memory module after hard reset\n"); 1790 goto out_err; 1791 } 1792 1793 if (!hdev->asic_prop.fw_security_enabled) 1794 hl_fw_set_max_power(hdev); 1795 } else { 1796 rc = hdev->asic_funcs->compute_reset_late_init(hdev); 1797 if (rc) { 1798 if (reset_upon_device_release) 1799 dev_err(hdev->dev, 1800 "Failed late init in reset after device release\n"); 1801 else 1802 dev_err(hdev->dev, "Failed late init after compute reset\n"); 1803 goto out_err; 1804 } 1805 } 1806 1807 rc = hdev->asic_funcs->scrub_device_mem(hdev); 1808 if (rc) { 1809 dev_err(hdev->dev, "scrub mem failed from device reset (%d)\n", rc); 1810 goto out_err; 1811 } 1812 1813 spin_lock(&hdev->reset_info.lock); 1814 hdev->reset_info.in_compute_reset = 0; 1815 1816 /* Schedule hard reset only if requested and if not already in hard reset. 1817 * We keep 'in_reset' enabled, so no other reset can go in during the hard 1818 * reset schedule 1819 */ 1820 if (!hard_reset && hdev->reset_info.hard_reset_schedule_flags) 1821 schedule_hard_reset = true; 1822 else 1823 hdev->reset_info.in_reset = 0; 1824 1825 spin_unlock(&hdev->reset_info.lock); 1826 1827 hdev->reset_info.needs_reset = false; 1828 1829 if (hard_reset) 1830 dev_info(hdev->dev, 1831 "Successfully finished resetting the %s device\n", 1832 dev_name(&(hdev)->pdev->dev)); 1833 else 1834 dev_dbg(hdev->dev, 1835 "Successfully finished resetting the %s device\n", 1836 dev_name(&(hdev)->pdev->dev)); 1837 1838 if (hard_reset) { 1839 hdev->reset_info.hard_reset_cnt++; 1840 1841 /* After reset is done, we are ready to receive events from 1842 * the F/W. We can't do it before because we will ignore events 1843 * and if those events are fatal, we won't know about it and 1844 * the device will be operational although it shouldn't be 1845 */ 1846 hdev->asic_funcs->enable_events_from_fw(hdev); 1847 } else { 1848 if (!reset_upon_device_release) 1849 hdev->reset_info.compute_reset_cnt++; 1850 1851 if (schedule_hard_reset) { 1852 dev_info(hdev->dev, "Performing hard reset scheduled during compute reset\n"); 1853 flags = hdev->reset_info.hard_reset_schedule_flags; 1854 hdev->reset_info.hard_reset_schedule_flags = 0; 1855 hard_reset = true; 1856 goto escalate_reset_flow; 1857 } 1858 } 1859 1860 return 0; 1861 1862 out_err: 1863 hdev->disabled = true; 1864 1865 spin_lock(&hdev->reset_info.lock); 1866 hdev->reset_info.in_compute_reset = 0; 1867 1868 if (hard_reset) { 1869 dev_err(hdev->dev, 1870 "%s Failed to reset! Device is NOT usable\n", 1871 dev_name(&(hdev)->pdev->dev)); 1872 hdev->reset_info.hard_reset_cnt++; 1873 } else { 1874 if (reset_upon_device_release) { 1875 dev_err(hdev->dev, "Failed to reset device after user release\n"); 1876 flags &= ~HL_DRV_RESET_DEV_RELEASE; 1877 } else { 1878 dev_err(hdev->dev, "Failed to do compute reset\n"); 1879 hdev->reset_info.compute_reset_cnt++; 1880 } 1881 1882 spin_unlock(&hdev->reset_info.lock); 1883 flags |= HL_DRV_RESET_HARD; 1884 hard_reset = true; 1885 goto escalate_reset_flow; 1886 } 1887 1888 hdev->reset_info.in_reset = 0; 1889 1890 spin_unlock(&hdev->reset_info.lock); 1891 1892 return rc; 1893 } 1894 1895 /* 1896 * hl_device_cond_reset() - conditionally reset the device. 1897 * @hdev: pointer to habanalabs device structure. 1898 * @reset_flags: reset flags. 1899 * @event_mask: events to notify user about. 1900 * 1901 * Conditionally reset the device, or alternatively schedule a watchdog work to reset the device 1902 * unless another reset precedes it. 1903 */ 1904 int hl_device_cond_reset(struct hl_device *hdev, u32 flags, u64 event_mask) 1905 { 1906 struct hl_ctx *ctx = NULL; 1907 1908 /* F/W reset cannot be postponed */ 1909 if (flags & HL_DRV_RESET_BYPASS_REQ_TO_FW) 1910 goto device_reset; 1911 1912 /* Device release watchdog is relevant only if user exists and gets a reset notification */ 1913 if (!(event_mask & HL_NOTIFIER_EVENT_DEVICE_RESET)) { 1914 dev_err(hdev->dev, "Resetting device without a reset indication to user\n"); 1915 goto device_reset; 1916 } 1917 1918 ctx = hl_get_compute_ctx(hdev); 1919 if (!ctx || !ctx->hpriv->notifier_event.eventfd) 1920 goto device_reset; 1921 1922 /* Schedule the device release watchdog work unless reset is already in progress or if the 1923 * work is already scheduled. 1924 */ 1925 spin_lock(&hdev->reset_info.lock); 1926 if (hdev->reset_info.in_reset) { 1927 spin_unlock(&hdev->reset_info.lock); 1928 goto device_reset; 1929 } 1930 1931 if (hdev->reset_info.watchdog_active) 1932 goto out; 1933 1934 hdev->device_release_watchdog_work.flags = flags; 1935 dev_dbg(hdev->dev, "Device is going to be hard-reset in %u sec unless being released\n", 1936 hdev->device_release_watchdog_timeout_sec); 1937 schedule_delayed_work(&hdev->device_release_watchdog_work.reset_work, 1938 msecs_to_jiffies(hdev->device_release_watchdog_timeout_sec * 1000)); 1939 hdev->reset_info.watchdog_active = 1; 1940 out: 1941 spin_unlock(&hdev->reset_info.lock); 1942 1943 hl_notifier_event_send_all(hdev, event_mask); 1944 1945 hl_ctx_put(ctx); 1946 1947 hl_abort_waiting_for_completions(hdev); 1948 1949 return 0; 1950 1951 device_reset: 1952 if (event_mask) 1953 hl_notifier_event_send_all(hdev, event_mask); 1954 if (ctx) 1955 hl_ctx_put(ctx); 1956 1957 return hl_device_reset(hdev, flags); 1958 } 1959 1960 static void hl_notifier_event_send(struct hl_notifier_event *notifier_event, u64 event_mask) 1961 { 1962 mutex_lock(¬ifier_event->lock); 1963 notifier_event->events_mask |= event_mask; 1964 1965 if (notifier_event->eventfd) 1966 eventfd_signal(notifier_event->eventfd, 1); 1967 1968 mutex_unlock(¬ifier_event->lock); 1969 } 1970 1971 /* 1972 * hl_notifier_event_send_all - notify all user processes via eventfd 1973 * 1974 * @hdev: pointer to habanalabs device structure 1975 * @event_mask: the occurred event/s 1976 * Returns 0 for success or an error on failure. 1977 */ 1978 void hl_notifier_event_send_all(struct hl_device *hdev, u64 event_mask) 1979 { 1980 struct hl_fpriv *hpriv; 1981 1982 if (!event_mask) { 1983 dev_warn(hdev->dev, "Skip sending zero event"); 1984 return; 1985 } 1986 1987 mutex_lock(&hdev->fpriv_list_lock); 1988 1989 list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) 1990 hl_notifier_event_send(&hpriv->notifier_event, event_mask); 1991 1992 mutex_unlock(&hdev->fpriv_list_lock); 1993 1994 /* control device */ 1995 mutex_lock(&hdev->fpriv_ctrl_list_lock); 1996 1997 list_for_each_entry(hpriv, &hdev->fpriv_ctrl_list, dev_node) 1998 hl_notifier_event_send(&hpriv->notifier_event, event_mask); 1999 2000 mutex_unlock(&hdev->fpriv_ctrl_list_lock); 2001 } 2002 2003 static int create_cdev(struct hl_device *hdev) 2004 { 2005 char *name; 2006 int rc; 2007 2008 hdev->cdev_idx = hdev->id / 2; 2009 2010 name = kasprintf(GFP_KERNEL, "hl%d", hdev->cdev_idx); 2011 if (!name) { 2012 rc = -ENOMEM; 2013 goto out_err; 2014 } 2015 2016 /* Initialize cdev and device structures */ 2017 rc = device_init_cdev(hdev, hdev->hclass, hdev->id, &hl_ops, name, 2018 &hdev->cdev, &hdev->dev); 2019 2020 kfree(name); 2021 2022 if (rc) 2023 goto out_err; 2024 2025 name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->cdev_idx); 2026 if (!name) { 2027 rc = -ENOMEM; 2028 goto free_dev; 2029 } 2030 2031 /* Initialize cdev and device structures for control device */ 2032 rc = device_init_cdev(hdev, hdev->hclass, hdev->id_control, &hl_ctrl_ops, 2033 name, &hdev->cdev_ctrl, &hdev->dev_ctrl); 2034 2035 kfree(name); 2036 2037 if (rc) 2038 goto free_dev; 2039 2040 return 0; 2041 2042 free_dev: 2043 put_device(hdev->dev); 2044 out_err: 2045 return rc; 2046 } 2047 2048 /* 2049 * hl_device_init - main initialization function for habanalabs device 2050 * 2051 * @hdev: pointer to habanalabs device structure 2052 * 2053 * Allocate an id for the device, do early initialization and then call the 2054 * ASIC specific initialization functions. Finally, create the cdev and the 2055 * Linux device to expose it to the user 2056 */ 2057 int hl_device_init(struct hl_device *hdev) 2058 { 2059 int i, rc, cq_cnt, user_interrupt_cnt, cq_ready_cnt; 2060 bool expose_interfaces_on_err = false; 2061 2062 rc = create_cdev(hdev); 2063 if (rc) 2064 goto out_disabled; 2065 2066 /* Initialize ASIC function pointers and perform early init */ 2067 rc = device_early_init(hdev); 2068 if (rc) 2069 goto free_dev; 2070 2071 user_interrupt_cnt = hdev->asic_prop.user_dec_intr_count + 2072 hdev->asic_prop.user_interrupt_count; 2073 2074 if (user_interrupt_cnt) { 2075 hdev->user_interrupt = kcalloc(user_interrupt_cnt, sizeof(*hdev->user_interrupt), 2076 GFP_KERNEL); 2077 if (!hdev->user_interrupt) { 2078 rc = -ENOMEM; 2079 goto early_fini; 2080 } 2081 } 2082 2083 /* 2084 * Start calling ASIC initialization. First S/W then H/W and finally 2085 * late init 2086 */ 2087 rc = hdev->asic_funcs->sw_init(hdev); 2088 if (rc) 2089 goto free_usr_intr_mem; 2090 2091 2092 /* initialize completion structure for multi CS wait */ 2093 hl_multi_cs_completion_init(hdev); 2094 2095 /* 2096 * Initialize the H/W queues. Must be done before hw_init, because 2097 * there the addresses of the kernel queue are being written to the 2098 * registers of the device 2099 */ 2100 rc = hl_hw_queues_create(hdev); 2101 if (rc) { 2102 dev_err(hdev->dev, "failed to initialize kernel queues\n"); 2103 goto sw_fini; 2104 } 2105 2106 cq_cnt = hdev->asic_prop.completion_queues_count; 2107 2108 /* 2109 * Initialize the completion queues. Must be done before hw_init, 2110 * because there the addresses of the completion queues are being 2111 * passed as arguments to request_irq 2112 */ 2113 if (cq_cnt) { 2114 hdev->completion_queue = kcalloc(cq_cnt, 2115 sizeof(*hdev->completion_queue), 2116 GFP_KERNEL); 2117 2118 if (!hdev->completion_queue) { 2119 dev_err(hdev->dev, 2120 "failed to allocate completion queues\n"); 2121 rc = -ENOMEM; 2122 goto hw_queues_destroy; 2123 } 2124 } 2125 2126 for (i = 0, cq_ready_cnt = 0 ; i < cq_cnt ; i++, cq_ready_cnt++) { 2127 rc = hl_cq_init(hdev, &hdev->completion_queue[i], 2128 hdev->asic_funcs->get_queue_id_for_cq(hdev, i)); 2129 if (rc) { 2130 dev_err(hdev->dev, 2131 "failed to initialize completion queue\n"); 2132 goto cq_fini; 2133 } 2134 hdev->completion_queue[i].cq_idx = i; 2135 } 2136 2137 hdev->shadow_cs_queue = kcalloc(hdev->asic_prop.max_pending_cs, 2138 sizeof(struct hl_cs *), GFP_KERNEL); 2139 if (!hdev->shadow_cs_queue) { 2140 rc = -ENOMEM; 2141 goto cq_fini; 2142 } 2143 2144 /* 2145 * Initialize the event queue. Must be done before hw_init, 2146 * because there the address of the event queue is being 2147 * passed as argument to request_irq 2148 */ 2149 rc = hl_eq_init(hdev, &hdev->event_queue); 2150 if (rc) { 2151 dev_err(hdev->dev, "failed to initialize event queue\n"); 2152 goto free_shadow_cs_queue; 2153 } 2154 2155 /* MMU S/W must be initialized before kernel context is created */ 2156 rc = hl_mmu_init(hdev); 2157 if (rc) { 2158 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n"); 2159 goto eq_fini; 2160 } 2161 2162 /* Allocate the kernel context */ 2163 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL); 2164 if (!hdev->kernel_ctx) { 2165 rc = -ENOMEM; 2166 goto mmu_fini; 2167 } 2168 2169 hdev->is_compute_ctx_active = false; 2170 2171 hdev->asic_funcs->state_dump_init(hdev); 2172 2173 hdev->device_release_watchdog_timeout_sec = HL_DEVICE_RELEASE_WATCHDOG_TIMEOUT_SEC; 2174 2175 hdev->memory_scrub_val = MEM_SCRUB_DEFAULT_VAL; 2176 2177 rc = hl_debugfs_device_init(hdev); 2178 if (rc) { 2179 dev_err(hdev->dev, "failed to initialize debugfs entry structure\n"); 2180 kfree(hdev->kernel_ctx); 2181 goto mmu_fini; 2182 } 2183 2184 /* The debugfs entry structure is accessed in hl_ctx_init(), so it must be called after 2185 * hl_debugfs_device_init(). 2186 */ 2187 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true); 2188 if (rc) { 2189 dev_err(hdev->dev, "failed to initialize kernel context\n"); 2190 kfree(hdev->kernel_ctx); 2191 goto debugfs_device_fini; 2192 } 2193 2194 rc = hl_cb_pool_init(hdev); 2195 if (rc) { 2196 dev_err(hdev->dev, "failed to initialize CB pool\n"); 2197 goto release_ctx; 2198 } 2199 2200 rc = hl_dec_init(hdev); 2201 if (rc) { 2202 dev_err(hdev->dev, "Failed to initialize the decoder module\n"); 2203 goto cb_pool_fini; 2204 } 2205 2206 /* 2207 * From this point, override rc (=0) in case of an error to allow debugging 2208 * (by adding char devices and creating sysfs/debugfs files as part of the error flow). 2209 */ 2210 expose_interfaces_on_err = true; 2211 2212 /* Device is now enabled as part of the initialization requires 2213 * communication with the device firmware to get information that 2214 * is required for the initialization itself 2215 */ 2216 hdev->disabled = false; 2217 2218 rc = hdev->asic_funcs->hw_init(hdev); 2219 if (rc) { 2220 dev_err(hdev->dev, "failed to initialize the H/W\n"); 2221 rc = 0; 2222 goto out_disabled; 2223 } 2224 2225 /* Check that the communication with the device is working */ 2226 rc = hdev->asic_funcs->test_queues(hdev); 2227 if (rc) { 2228 dev_err(hdev->dev, "Failed to detect if device is alive\n"); 2229 rc = 0; 2230 goto out_disabled; 2231 } 2232 2233 rc = device_late_init(hdev); 2234 if (rc) { 2235 dev_err(hdev->dev, "Failed late initialization\n"); 2236 rc = 0; 2237 goto out_disabled; 2238 } 2239 2240 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n", 2241 hdev->asic_name, 2242 hdev->asic_prop.dram_size / SZ_1G); 2243 2244 rc = hl_vm_init(hdev); 2245 if (rc) { 2246 dev_err(hdev->dev, "Failed to initialize memory module\n"); 2247 rc = 0; 2248 goto out_disabled; 2249 } 2250 2251 /* 2252 * Expose devices and sysfs/debugfs files to user. 2253 * From here there is no need to expose them in case of an error. 2254 */ 2255 expose_interfaces_on_err = false; 2256 rc = cdev_sysfs_debugfs_add(hdev); 2257 if (rc) { 2258 dev_err(hdev->dev, "Failed to add char devices and sysfs/debugfs files\n"); 2259 rc = 0; 2260 goto out_disabled; 2261 } 2262 2263 /* Need to call this again because the max power might change, 2264 * depending on card type for certain ASICs 2265 */ 2266 if (hdev->asic_prop.set_max_power_on_device_init && 2267 !hdev->asic_prop.fw_security_enabled) 2268 hl_fw_set_max_power(hdev); 2269 2270 /* 2271 * hl_hwmon_init() must be called after device_late_init(), because only 2272 * there we get the information from the device about which 2273 * hwmon-related sensors the device supports. 2274 * Furthermore, it must be done after adding the device to the system. 2275 */ 2276 rc = hl_hwmon_init(hdev); 2277 if (rc) { 2278 dev_err(hdev->dev, "Failed to initialize hwmon\n"); 2279 rc = 0; 2280 goto out_disabled; 2281 } 2282 2283 dev_notice(hdev->dev, 2284 "Successfully added device %s to habanalabs driver\n", 2285 dev_name(&(hdev)->pdev->dev)); 2286 2287 hdev->init_done = true; 2288 2289 /* After initialization is done, we are ready to receive events from 2290 * the F/W. We can't do it before because we will ignore events and if 2291 * those events are fatal, we won't know about it and the device will 2292 * be operational although it shouldn't be 2293 */ 2294 hdev->asic_funcs->enable_events_from_fw(hdev); 2295 2296 return 0; 2297 2298 cb_pool_fini: 2299 hl_cb_pool_fini(hdev); 2300 release_ctx: 2301 if (hl_ctx_put(hdev->kernel_ctx) != 1) 2302 dev_err(hdev->dev, 2303 "kernel ctx is still alive on initialization failure\n"); 2304 debugfs_device_fini: 2305 hl_debugfs_device_fini(hdev); 2306 mmu_fini: 2307 hl_mmu_fini(hdev); 2308 eq_fini: 2309 hl_eq_fini(hdev, &hdev->event_queue); 2310 free_shadow_cs_queue: 2311 kfree(hdev->shadow_cs_queue); 2312 cq_fini: 2313 for (i = 0 ; i < cq_ready_cnt ; i++) 2314 hl_cq_fini(hdev, &hdev->completion_queue[i]); 2315 kfree(hdev->completion_queue); 2316 hw_queues_destroy: 2317 hl_hw_queues_destroy(hdev); 2318 sw_fini: 2319 hdev->asic_funcs->sw_fini(hdev); 2320 free_usr_intr_mem: 2321 kfree(hdev->user_interrupt); 2322 early_fini: 2323 device_early_fini(hdev); 2324 free_dev: 2325 put_device(hdev->dev_ctrl); 2326 put_device(hdev->dev); 2327 out_disabled: 2328 hdev->disabled = true; 2329 if (expose_interfaces_on_err) 2330 cdev_sysfs_debugfs_add(hdev); 2331 dev_err(&hdev->pdev->dev, 2332 "Failed to initialize hl%d. Device %s is NOT usable !\n", 2333 hdev->cdev_idx, dev_name(&hdev->pdev->dev)); 2334 2335 return rc; 2336 } 2337 2338 /* 2339 * hl_device_fini - main tear-down function for habanalabs device 2340 * 2341 * @hdev: pointer to habanalabs device structure 2342 * 2343 * Destroy the device, call ASIC fini functions and release the id 2344 */ 2345 void hl_device_fini(struct hl_device *hdev) 2346 { 2347 bool device_in_reset; 2348 ktime_t timeout; 2349 u64 reset_sec; 2350 int i, rc; 2351 2352 dev_info(hdev->dev, "Removing device\n"); 2353 2354 hdev->device_fini_pending = 1; 2355 flush_delayed_work(&hdev->device_reset_work.reset_work); 2356 2357 if (hdev->pldm) 2358 reset_sec = HL_PLDM_HARD_RESET_MAX_TIMEOUT; 2359 else 2360 reset_sec = HL_HARD_RESET_MAX_TIMEOUT; 2361 2362 /* 2363 * This function is competing with the reset function, so try to 2364 * take the reset atomic and if we are already in middle of reset, 2365 * wait until reset function is finished. Reset function is designed 2366 * to always finish. However, in Gaudi, because of all the network 2367 * ports, the hard reset could take between 10-30 seconds 2368 */ 2369 2370 timeout = ktime_add_us(ktime_get(), reset_sec * 1000 * 1000); 2371 2372 spin_lock(&hdev->reset_info.lock); 2373 device_in_reset = !!hdev->reset_info.in_reset; 2374 if (!device_in_reset) 2375 hdev->reset_info.in_reset = 1; 2376 spin_unlock(&hdev->reset_info.lock); 2377 2378 while (device_in_reset) { 2379 usleep_range(50, 200); 2380 2381 spin_lock(&hdev->reset_info.lock); 2382 device_in_reset = !!hdev->reset_info.in_reset; 2383 if (!device_in_reset) 2384 hdev->reset_info.in_reset = 1; 2385 spin_unlock(&hdev->reset_info.lock); 2386 2387 if (ktime_compare(ktime_get(), timeout) > 0) { 2388 dev_crit(hdev->dev, 2389 "%s Failed to remove device because reset function did not finish\n", 2390 dev_name(&(hdev)->pdev->dev)); 2391 return; 2392 } 2393 } 2394 2395 cancel_delayed_work_sync(&hdev->device_release_watchdog_work.reset_work); 2396 2397 /* Disable PCI access from device F/W so it won't send us additional 2398 * interrupts. We disable MSI/MSI-X at the halt_engines function and we 2399 * can't have the F/W sending us interrupts after that. We need to 2400 * disable the access here because if the device is marked disable, the 2401 * message won't be send. Also, in case of heartbeat, the device CPU is 2402 * marked as disable so this message won't be sent 2403 */ 2404 hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0); 2405 2406 /* Mark device as disabled */ 2407 hdev->disabled = true; 2408 2409 take_release_locks(hdev); 2410 2411 hdev->reset_info.hard_reset_pending = true; 2412 2413 hl_hwmon_fini(hdev); 2414 2415 cleanup_resources(hdev, true, false, false); 2416 2417 /* Kill processes here after CS rollback. This is because the process 2418 * can't really exit until all its CSs are done, which is what we 2419 * do in cs rollback 2420 */ 2421 dev_info(hdev->dev, 2422 "Waiting for all processes to exit (timeout of %u seconds)", 2423 HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI); 2424 2425 hdev->process_kill_trial_cnt = 0; 2426 rc = device_kill_open_processes(hdev, HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI, false); 2427 if (rc) { 2428 dev_crit(hdev->dev, "Failed to kill all open processes\n"); 2429 device_disable_open_processes(hdev, false); 2430 } 2431 2432 hdev->process_kill_trial_cnt = 0; 2433 rc = device_kill_open_processes(hdev, 0, true); 2434 if (rc) { 2435 dev_crit(hdev->dev, "Failed to kill all control device open processes\n"); 2436 device_disable_open_processes(hdev, true); 2437 } 2438 2439 hl_cb_pool_fini(hdev); 2440 2441 /* Reset the H/W. It will be in idle state after this returns */ 2442 rc = hdev->asic_funcs->hw_fini(hdev, true, false); 2443 if (rc) 2444 dev_err(hdev->dev, "hw_fini failed in device fini while removing device %d\n", rc); 2445 2446 hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE; 2447 2448 /* Release kernel context */ 2449 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1)) 2450 dev_err(hdev->dev, "kernel ctx is still alive\n"); 2451 2452 hl_dec_fini(hdev); 2453 2454 hl_vm_fini(hdev); 2455 2456 hl_mmu_fini(hdev); 2457 2458 vfree(hdev->captured_err_info.page_fault_info.user_mappings); 2459 2460 hl_eq_fini(hdev, &hdev->event_queue); 2461 2462 kfree(hdev->shadow_cs_queue); 2463 2464 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) 2465 hl_cq_fini(hdev, &hdev->completion_queue[i]); 2466 kfree(hdev->completion_queue); 2467 kfree(hdev->user_interrupt); 2468 2469 hl_hw_queues_destroy(hdev); 2470 2471 /* Call ASIC S/W finalize function */ 2472 hdev->asic_funcs->sw_fini(hdev); 2473 2474 device_early_fini(hdev); 2475 2476 /* Hide devices and sysfs/debugfs files from user */ 2477 cdev_sysfs_debugfs_remove(hdev); 2478 2479 hl_debugfs_device_fini(hdev); 2480 2481 pr_info("removed device successfully\n"); 2482 } 2483 2484 /* 2485 * MMIO register access helper functions. 2486 */ 2487 2488 /* 2489 * hl_rreg - Read an MMIO register 2490 * 2491 * @hdev: pointer to habanalabs device structure 2492 * @reg: MMIO register offset (in bytes) 2493 * 2494 * Returns the value of the MMIO register we are asked to read 2495 * 2496 */ 2497 inline u32 hl_rreg(struct hl_device *hdev, u32 reg) 2498 { 2499 u32 val = readl(hdev->rmmio + reg); 2500 2501 if (unlikely(trace_habanalabs_rreg32_enabled())) 2502 trace_habanalabs_rreg32(hdev->dev, reg, val); 2503 2504 return val; 2505 } 2506 2507 /* 2508 * hl_wreg - Write to an MMIO register 2509 * 2510 * @hdev: pointer to habanalabs device structure 2511 * @reg: MMIO register offset (in bytes) 2512 * @val: 32-bit value 2513 * 2514 * Writes the 32-bit value into the MMIO register 2515 * 2516 */ 2517 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val) 2518 { 2519 if (unlikely(trace_habanalabs_wreg32_enabled())) 2520 trace_habanalabs_wreg32(hdev->dev, reg, val); 2521 2522 writel(val, hdev->rmmio + reg); 2523 } 2524 2525 void hl_capture_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines, 2526 u8 flags) 2527 { 2528 struct razwi_info *razwi_info = &hdev->captured_err_info.razwi_info; 2529 2530 if (num_of_engines > HL_RAZWI_MAX_NUM_OF_ENGINES_PER_RTR) { 2531 dev_err(hdev->dev, 2532 "Number of possible razwi initiators (%u) exceeded limit (%u)\n", 2533 num_of_engines, HL_RAZWI_MAX_NUM_OF_ENGINES_PER_RTR); 2534 return; 2535 } 2536 2537 /* In case it's the first razwi since the device was opened, capture its parameters */ 2538 if (atomic_cmpxchg(&hdev->captured_err_info.razwi_info.razwi_detected, 0, 1)) 2539 return; 2540 2541 razwi_info->razwi.timestamp = ktime_to_ns(ktime_get()); 2542 razwi_info->razwi.addr = addr; 2543 razwi_info->razwi.num_of_possible_engines = num_of_engines; 2544 memcpy(&razwi_info->razwi.engine_id[0], &engine_id[0], 2545 num_of_engines * sizeof(u16)); 2546 razwi_info->razwi.flags = flags; 2547 2548 razwi_info->razwi_info_available = true; 2549 } 2550 2551 void hl_handle_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines, 2552 u8 flags, u64 *event_mask) 2553 { 2554 hl_capture_razwi(hdev, addr, engine_id, num_of_engines, flags); 2555 2556 if (event_mask) 2557 *event_mask |= HL_NOTIFIER_EVENT_RAZWI; 2558 } 2559 2560 static void hl_capture_user_mappings(struct hl_device *hdev, bool is_pmmu) 2561 { 2562 struct page_fault_info *pgf_info = &hdev->captured_err_info.page_fault_info; 2563 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL; 2564 struct hl_vm_hash_node *hnode; 2565 struct hl_userptr *userptr; 2566 enum vm_type *vm_type; 2567 struct hl_ctx *ctx; 2568 u32 map_idx = 0; 2569 int i; 2570 2571 /* Reset previous session count*/ 2572 pgf_info->num_of_user_mappings = 0; 2573 2574 ctx = hl_get_compute_ctx(hdev); 2575 if (!ctx) { 2576 dev_err(hdev->dev, "Can't get user context for user mappings\n"); 2577 return; 2578 } 2579 2580 mutex_lock(&ctx->mem_hash_lock); 2581 hash_for_each(ctx->mem_hash, i, hnode, node) { 2582 vm_type = hnode->ptr; 2583 if (((*vm_type == VM_TYPE_USERPTR) && is_pmmu) || 2584 ((*vm_type == VM_TYPE_PHYS_PACK) && !is_pmmu)) 2585 pgf_info->num_of_user_mappings++; 2586 2587 } 2588 2589 if (!pgf_info->num_of_user_mappings) 2590 goto finish; 2591 2592 /* In case we already allocated in previous session, need to release it before 2593 * allocating new buffer. 2594 */ 2595 vfree(pgf_info->user_mappings); 2596 pgf_info->user_mappings = 2597 vzalloc(pgf_info->num_of_user_mappings * sizeof(struct hl_user_mapping)); 2598 if (!pgf_info->user_mappings) { 2599 pgf_info->num_of_user_mappings = 0; 2600 goto finish; 2601 } 2602 2603 hash_for_each(ctx->mem_hash, i, hnode, node) { 2604 vm_type = hnode->ptr; 2605 if ((*vm_type == VM_TYPE_USERPTR) && (is_pmmu)) { 2606 userptr = hnode->ptr; 2607 pgf_info->user_mappings[map_idx].dev_va = hnode->vaddr; 2608 pgf_info->user_mappings[map_idx].size = userptr->size; 2609 map_idx++; 2610 } else if ((*vm_type == VM_TYPE_PHYS_PACK) && (!is_pmmu)) { 2611 phys_pg_pack = hnode->ptr; 2612 pgf_info->user_mappings[map_idx].dev_va = hnode->vaddr; 2613 pgf_info->user_mappings[map_idx].size = phys_pg_pack->total_size; 2614 map_idx++; 2615 } 2616 } 2617 finish: 2618 mutex_unlock(&ctx->mem_hash_lock); 2619 hl_ctx_put(ctx); 2620 } 2621 2622 void hl_capture_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu) 2623 { 2624 struct page_fault_info *pgf_info = &hdev->captured_err_info.page_fault_info; 2625 2626 /* Capture only the first page fault */ 2627 if (atomic_cmpxchg(&pgf_info->page_fault_detected, 0, 1)) 2628 return; 2629 2630 pgf_info->page_fault.timestamp = ktime_to_ns(ktime_get()); 2631 pgf_info->page_fault.addr = addr; 2632 pgf_info->page_fault.engine_id = eng_id; 2633 hl_capture_user_mappings(hdev, is_pmmu); 2634 2635 pgf_info->page_fault_info_available = true; 2636 } 2637 2638 void hl_handle_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu, 2639 u64 *event_mask) 2640 { 2641 hl_capture_page_fault(hdev, addr, eng_id, is_pmmu); 2642 2643 if (event_mask) 2644 *event_mask |= HL_NOTIFIER_EVENT_PAGE_FAULT; 2645 } 2646 2647 static void hl_capture_hw_err(struct hl_device *hdev, u16 event_id) 2648 { 2649 struct hw_err_info *info = &hdev->captured_err_info.hw_err; 2650 2651 /* Capture only the first HW err */ 2652 if (atomic_cmpxchg(&info->event_detected, 0, 1)) 2653 return; 2654 2655 info->event.timestamp = ktime_to_ns(ktime_get()); 2656 info->event.event_id = event_id; 2657 2658 info->event_info_available = true; 2659 } 2660 2661 void hl_handle_critical_hw_err(struct hl_device *hdev, u16 event_id, u64 *event_mask) 2662 { 2663 hl_capture_hw_err(hdev, event_id); 2664 2665 if (event_mask) 2666 *event_mask |= HL_NOTIFIER_EVENT_CRITICL_HW_ERR; 2667 } 2668 2669 static void hl_capture_fw_err(struct hl_device *hdev, struct hl_info_fw_err_info *fw_info) 2670 { 2671 struct fw_err_info *info = &hdev->captured_err_info.fw_err; 2672 2673 /* Capture only the first FW error */ 2674 if (atomic_cmpxchg(&info->event_detected, 0, 1)) 2675 return; 2676 2677 info->event.timestamp = ktime_to_ns(ktime_get()); 2678 info->event.err_type = fw_info->err_type; 2679 if (fw_info->err_type == HL_INFO_FW_REPORTED_ERR) 2680 info->event.event_id = fw_info->event_id; 2681 2682 info->event_info_available = true; 2683 } 2684 2685 void hl_handle_fw_err(struct hl_device *hdev, struct hl_info_fw_err_info *info) 2686 { 2687 hl_capture_fw_err(hdev, info); 2688 2689 if (info->event_mask) 2690 *info->event_mask |= HL_NOTIFIER_EVENT_CRITICL_FW_ERR; 2691 } 2692 2693 void hl_enable_err_info_capture(struct hl_error_info *captured_err_info) 2694 { 2695 vfree(captured_err_info->page_fault_info.user_mappings); 2696 memset(captured_err_info, 0, sizeof(struct hl_error_info)); 2697 atomic_set(&captured_err_info->cs_timeout.write_enable, 1); 2698 captured_err_info->undef_opcode.write_enable = true; 2699 } 2700