Lines Matching refs:hdev

46 static u64 hl_set_dram_bar(struct hl_device *hdev, u64 addr, struct pci_mem_region *region,  in hl_set_dram_bar()  argument
49 struct asic_fixed_properties *prop = &hdev->asic_prop; in hl_set_dram_bar()
58 old_base = hdev->asic_funcs->set_dram_bar_base(hdev, bar_base_addr); in hl_set_dram_bar()
67 int hl_access_sram_dram_region(struct hl_device *hdev, u64 addr, u64 *val, in hl_access_sram_dram_region() argument
70 struct pci_mem_region *region = &hdev->pci_mem_region[region_type]; in hl_access_sram_dram_region()
75 old_base = hl_set_dram_bar(hdev, addr, region, &bar_region_base); in hl_access_sram_dram_region()
80 acc_addr = hdev->pcie_bar[region->bar_id] + region->offset_in_bar + in hl_access_sram_dram_region()
105 rc = hl_set_dram_bar(hdev, old_base, region, NULL); in hl_access_sram_dram_region()
113 static void *hl_dma_alloc_common(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle, in hl_dma_alloc_common() argument
121 ptr = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, size, dma_handle, flag); in hl_dma_alloc_common()
124 ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, size, flag, dma_handle); in hl_dma_alloc_common()
129 trace_habanalabs_dma_alloc(hdev->dev, (u64) (uintptr_t) ptr, *dma_handle, size, in hl_dma_alloc_common()
135 static void hl_asic_dma_free_common(struct hl_device *hdev, size_t size, void *cpu_addr, in hl_asic_dma_free_common() argument
144 hdev->asic_funcs->asic_dma_free_coherent(hdev, size, cpu_addr, dma_handle); in hl_asic_dma_free_common()
147 hdev->asic_funcs->asic_dma_pool_free(hdev, cpu_addr, dma_handle); in hl_asic_dma_free_common()
151 trace_habanalabs_dma_free(hdev->dev, store_cpu_addr, dma_handle, size, caller); in hl_asic_dma_free_common()
154 void *hl_asic_dma_alloc_coherent_caller(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle, in hl_asic_dma_alloc_coherent_caller() argument
157 return hl_dma_alloc_common(hdev, size, dma_handle, flag, DMA_ALLOC_COHERENT, caller); in hl_asic_dma_alloc_coherent_caller()
160 void hl_asic_dma_free_coherent_caller(struct hl_device *hdev, size_t size, void *cpu_addr, in hl_asic_dma_free_coherent_caller() argument
163 hl_asic_dma_free_common(hdev, size, cpu_addr, dma_handle, DMA_ALLOC_COHERENT, caller); in hl_asic_dma_free_coherent_caller()
166 void *hl_asic_dma_pool_zalloc_caller(struct hl_device *hdev, size_t size, gfp_t mem_flags, in hl_asic_dma_pool_zalloc_caller() argument
169 return hl_dma_alloc_common(hdev, size, dma_handle, mem_flags, DMA_ALLOC_POOL, caller); in hl_asic_dma_pool_zalloc_caller()
172 void hl_asic_dma_pool_free_caller(struct hl_device *hdev, void *vaddr, dma_addr_t dma_addr, in hl_asic_dma_pool_free_caller() argument
175 hl_asic_dma_free_common(hdev, 0, vaddr, dma_addr, DMA_ALLOC_POOL, caller); in hl_asic_dma_pool_free_caller()
178 void *hl_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle) in hl_cpu_accessible_dma_pool_alloc() argument
180 return hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, size, dma_handle); in hl_cpu_accessible_dma_pool_alloc()
183 void hl_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size, void *vaddr) in hl_cpu_accessible_dma_pool_free() argument
185 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, size, vaddr); in hl_cpu_accessible_dma_pool_free()
188 int hl_dma_map_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir) in hl_dma_map_sgtable() argument
190 struct asic_fixed_properties *prop = &hdev->asic_prop; in hl_dma_map_sgtable()
194 rc = dma_map_sgtable(&hdev->pdev->dev, sgt, dir, 0); in hl_dma_map_sgtable()
206 void hl_dma_unmap_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir) in hl_dma_unmap_sgtable() argument
208 struct asic_fixed_properties *prop = &hdev->asic_prop; in hl_dma_unmap_sgtable()
217 dma_unmap_sgtable(&hdev->pdev->dev, sgt, dir, 0); in hl_dma_unmap_sgtable()
228 int hl_access_cfg_region(struct hl_device *hdev, u64 addr, u64 *val, in hl_access_cfg_region() argument
231 struct pci_mem_region *cfg_region = &hdev->pci_mem_region[PCI_REGION_CFG]; in hl_access_cfg_region()
235 dev_err(hdev->dev, "address %#llx not a multiple of %zu\n", addr, sizeof(u32)); in hl_access_cfg_region()
257 dev_err(hdev->dev, "access type %d is not supported\n", acc_type); in hl_access_cfg_region()
273 int hl_access_dev_mem(struct hl_device *hdev, enum pci_region region_type, in hl_access_dev_mem() argument
278 return hl_access_cfg_region(hdev, addr, val, acc_type); in hl_access_dev_mem()
281 return hl_access_sram_dram_region(hdev, addr, val, acc_type, in hl_access_dev_mem()
314 enum hl_device_status hl_device_status(struct hl_device *hdev) in hl_device_status() argument
318 if (hdev->reset_info.in_reset) { in hl_device_status()
319 if (hdev->reset_info.in_compute_reset) in hl_device_status()
323 } else if (hdev->reset_info.needs_reset) { in hl_device_status()
325 } else if (hdev->disabled) { in hl_device_status()
327 } else if (!hdev->init_done) { in hl_device_status()
336 bool hl_device_operational(struct hl_device *hdev, in hl_device_operational() argument
341 current_status = hl_device_status(hdev); in hl_device_operational()
358 bool hl_ctrl_device_operational(struct hl_device *hdev, in hl_ctrl_device_operational() argument
363 current_status = hl_device_status(hdev); in hl_ctrl_device_operational()
380 static void print_idle_status_mask(struct hl_device *hdev, const char *message, in print_idle_status_mask() argument
384 dev_err(hdev->dev, "%s (mask %#llx_%016llx_%016llx_%016llx)\n", in print_idle_status_mask()
387 dev_err(hdev->dev, "%s (mask %#llx_%016llx_%016llx)\n", in print_idle_status_mask()
390 dev_err(hdev->dev, "%s (mask %#llx_%016llx)\n", in print_idle_status_mask()
393 dev_err(hdev->dev, "%s (mask %#llx)\n", message, idle_mask[0]); in print_idle_status_mask()
401 struct hl_device *hdev; in hpriv_release() local
405 hdev = hpriv->hdev; in hpriv_release()
407 hdev->asic_funcs->send_device_activity(hdev, false); in hpriv_release()
422 reset_device = hdev->reset_upon_device_release || hdev->reset_info.watchdog_active; in hpriv_release()
427 if (!hdev->reset_info.in_reset && !reset_device && hdev->pdev && !hdev->pldm) in hpriv_release()
428 device_is_idle = hdev->asic_funcs->is_device_idle(hdev, idle_mask, in hpriv_release()
431 print_idle_status_mask(hdev, "device is not idle after user context is closed", in hpriv_release()
445 mutex_lock(&hdev->fpriv_list_lock); in hpriv_release()
447 mutex_unlock(&hdev->fpriv_list_lock); in hpriv_release()
450 hl_device_reset(hdev, HL_DRV_RESET_DEV_RELEASE); in hpriv_release()
453 int rc = hdev->asic_funcs->scrub_device_mem(hdev); in hpriv_release()
456 dev_err(hdev->dev, "failed to scrub memory from hpriv release (%d)\n", rc); in hpriv_release()
463 mutex_lock(&hdev->fpriv_list_lock); in hpriv_release()
464 hdev->is_compute_ctx_active = false; in hpriv_release()
465 mutex_unlock(&hdev->fpriv_list_lock); in hpriv_release()
467 hdev->compute_ctx_in_release = 0; in hpriv_release()
488 static void print_device_in_use_info(struct hl_device *hdev, const char *message) in print_device_in_use_info() argument
499 active_cs_num = hl_get_active_cs_num(hdev); in print_device_in_use_info()
505 dmabuf_export_cnt = atomic_read(&hdev->dmabuf_export_cnt); in print_device_in_use_info()
515 dev_notice(hdev->dev, "%s%s\n", message, buf); in print_device_in_use_info()
529 struct hl_device *hdev = hpriv->hdev; in hl_device_release() local
533 if (!hdev) { in hl_device_release()
539 hl_ctx_mgr_fini(hdev, &hpriv->ctx_mgr); in hl_device_release()
546 hdev->compute_ctx_in_release = 1; in hl_device_release()
549 print_device_in_use_info(hdev, "User process closed FD but device still in use"); in hl_device_release()
550 hl_device_reset(hdev, HL_DRV_RESET_HARD); in hl_device_release()
553 hdev->last_open_session_duration_jif = jiffies - hdev->last_successful_open_jif; in hl_device_release()
561 struct hl_device *hdev = hpriv->hdev; in hl_device_release_ctrl() local
565 if (!hdev) { in hl_device_release_ctrl()
570 mutex_lock(&hdev->fpriv_ctrl_list_lock); in hl_device_release_ctrl()
572 mutex_unlock(&hdev->fpriv_ctrl_list_lock); in hl_device_release_ctrl()
598 struct hl_device *hdev = hpriv->hdev; in hl_mmap() local
601 if (!hdev) { in hl_mmap()
655 static int device_init_cdev(struct hl_device *hdev, struct class *class, in device_init_cdev() argument
668 (*dev)->devt = MKDEV(hdev->major, minor); in device_init_cdev()
671 dev_set_drvdata(*dev, hdev); in device_init_cdev()
677 static int cdev_sysfs_debugfs_add(struct hl_device *hdev) in cdev_sysfs_debugfs_add() argument
681 rc = cdev_device_add(&hdev->cdev, hdev->dev); in cdev_sysfs_debugfs_add()
683 dev_err(hdev->dev, in cdev_sysfs_debugfs_add()
688 rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl); in cdev_sysfs_debugfs_add()
690 dev_err(hdev->dev, in cdev_sysfs_debugfs_add()
696 rc = hl_sysfs_init(hdev); in cdev_sysfs_debugfs_add()
698 dev_err(hdev->dev, "failed to initialize sysfs\n"); in cdev_sysfs_debugfs_add()
702 hl_debugfs_add_device(hdev); in cdev_sysfs_debugfs_add()
704 hdev->cdev_sysfs_debugfs_created = true; in cdev_sysfs_debugfs_add()
709 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl); in cdev_sysfs_debugfs_add()
711 cdev_device_del(&hdev->cdev, hdev->dev); in cdev_sysfs_debugfs_add()
715 static void cdev_sysfs_debugfs_remove(struct hl_device *hdev) in cdev_sysfs_debugfs_remove() argument
717 if (!hdev->cdev_sysfs_debugfs_created) in cdev_sysfs_debugfs_remove()
720 hl_debugfs_remove_device(hdev); in cdev_sysfs_debugfs_remove()
721 hl_sysfs_fini(hdev); in cdev_sysfs_debugfs_remove()
722 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl); in cdev_sysfs_debugfs_remove()
723 cdev_device_del(&hdev->cdev, hdev->dev); in cdev_sysfs_debugfs_remove()
726 put_device(hdev->dev); in cdev_sysfs_debugfs_remove()
727 put_device(hdev->dev_ctrl); in cdev_sysfs_debugfs_remove()
734 struct hl_device *hdev = device_reset_work->hdev; in device_hard_reset_pending() local
740 rc = hl_device_reset(hdev, flags); in device_hard_reset_pending()
742 if ((rc == -EBUSY) && !hdev->device_fini_pending) { in device_hard_reset_pending()
743 struct hl_ctx *ctx = hl_get_compute_ctx(hdev); in device_hard_reset_pending()
749 dev_info(hdev->dev, in device_hard_reset_pending()
754 dev_info(hdev->dev, "Could not reset device. will try again in %u seconds", in device_hard_reset_pending()
758 queue_delayed_work(hdev->reset_wq, &device_reset_work->reset_work, in device_hard_reset_pending()
767 struct hl_device *hdev = watchdog_work->hdev; in device_release_watchdog_func() local
770 dev_dbg(hdev->dev, "Device wasn't released in time. Initiate hard-reset.\n"); in device_release_watchdog_func()
774 hl_device_reset(hdev, flags); in device_release_watchdog_func()
785 static int device_early_init(struct hl_device *hdev) in device_early_init() argument
790 switch (hdev->asic_type) { in device_early_init()
792 goya_set_asic_funcs(hdev); in device_early_init()
793 strscpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name)); in device_early_init()
796 gaudi_set_asic_funcs(hdev); in device_early_init()
797 strscpy(hdev->asic_name, "GAUDI", sizeof(hdev->asic_name)); in device_early_init()
800 gaudi_set_asic_funcs(hdev); in device_early_init()
801 strscpy(hdev->asic_name, "GAUDI SEC", sizeof(hdev->asic_name)); in device_early_init()
804 gaudi2_set_asic_funcs(hdev); in device_early_init()
805 strscpy(hdev->asic_name, "GAUDI2", sizeof(hdev->asic_name)); in device_early_init()
808 gaudi2_set_asic_funcs(hdev); in device_early_init()
809 strscpy(hdev->asic_name, "GAUDI2B", sizeof(hdev->asic_name)); in device_early_init()
812 gaudi2_set_asic_funcs(hdev); in device_early_init()
813 strscpy(hdev->asic_name, "GAUDI2C", sizeof(hdev->asic_name)); in device_early_init()
816 dev_err(hdev->dev, "Unrecognized ASIC type %d\n", in device_early_init()
817 hdev->asic_type); in device_early_init()
821 rc = hdev->asic_funcs->early_init(hdev); in device_early_init()
825 rc = hl_asid_init(hdev); in device_early_init()
829 if (hdev->asic_prop.completion_queues_count) { in device_early_init()
830 hdev->cq_wq = kcalloc(hdev->asic_prop.completion_queues_count, in device_early_init()
833 if (!hdev->cq_wq) { in device_early_init()
839 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) { in device_early_init()
840 snprintf(workq_name, 32, "hl%u-free-jobs-%u", hdev->cdev_idx, (u32) i); in device_early_init()
841 hdev->cq_wq[i] = create_singlethread_workqueue(workq_name); in device_early_init()
842 if (hdev->cq_wq[i] == NULL) { in device_early_init()
843 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n"); in device_early_init()
849 snprintf(workq_name, 32, "hl%u-events", hdev->cdev_idx); in device_early_init()
850 hdev->eq_wq = create_singlethread_workqueue(workq_name); in device_early_init()
851 if (hdev->eq_wq == NULL) { in device_early_init()
852 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n"); in device_early_init()
857 snprintf(workq_name, 32, "hl%u-cs-completions", hdev->cdev_idx); in device_early_init()
858 hdev->cs_cmplt_wq = alloc_workqueue(workq_name, WQ_UNBOUND, 0); in device_early_init()
859 if (!hdev->cs_cmplt_wq) { in device_early_init()
860 dev_err(hdev->dev, in device_early_init()
866 snprintf(workq_name, 32, "hl%u-ts-free-obj", hdev->cdev_idx); in device_early_init()
867 hdev->ts_free_obj_wq = alloc_workqueue(workq_name, WQ_UNBOUND, 0); in device_early_init()
868 if (!hdev->ts_free_obj_wq) { in device_early_init()
869 dev_err(hdev->dev, in device_early_init()
875 snprintf(workq_name, 32, "hl%u-prefetch", hdev->cdev_idx); in device_early_init()
876 hdev->prefetch_wq = alloc_workqueue(workq_name, WQ_UNBOUND, 0); in device_early_init()
877 if (!hdev->prefetch_wq) { in device_early_init()
878 dev_err(hdev->dev, "Failed to allocate MMU prefetch workqueue\n"); in device_early_init()
883 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info), GFP_KERNEL); in device_early_init()
884 if (!hdev->hl_chip_info) { in device_early_init()
889 rc = hl_mmu_if_set_funcs(hdev); in device_early_init()
893 hl_mem_mgr_init(hdev->dev, &hdev->kernel_mem_mgr); in device_early_init()
895 snprintf(workq_name, 32, "hl%u_device_reset", hdev->cdev_idx); in device_early_init()
896 hdev->reset_wq = create_singlethread_workqueue(workq_name); in device_early_init()
897 if (!hdev->reset_wq) { in device_early_init()
899 dev_err(hdev->dev, "Failed to create device reset WQ\n"); in device_early_init()
903 INIT_DELAYED_WORK(&hdev->device_reset_work.reset_work, device_hard_reset_pending); in device_early_init()
904 hdev->device_reset_work.hdev = hdev; in device_early_init()
905 hdev->device_fini_pending = 0; in device_early_init()
907 INIT_DELAYED_WORK(&hdev->device_release_watchdog_work.reset_work, in device_early_init()
909 hdev->device_release_watchdog_work.hdev = hdev; in device_early_init()
911 mutex_init(&hdev->send_cpu_message_lock); in device_early_init()
912 mutex_init(&hdev->debug_lock); in device_early_init()
913 INIT_LIST_HEAD(&hdev->cs_mirror_list); in device_early_init()
914 spin_lock_init(&hdev->cs_mirror_lock); in device_early_init()
915 spin_lock_init(&hdev->reset_info.lock); in device_early_init()
916 INIT_LIST_HEAD(&hdev->fpriv_list); in device_early_init()
917 INIT_LIST_HEAD(&hdev->fpriv_ctrl_list); in device_early_init()
918 mutex_init(&hdev->fpriv_list_lock); in device_early_init()
919 mutex_init(&hdev->fpriv_ctrl_list_lock); in device_early_init()
920 mutex_init(&hdev->clk_throttling.lock); in device_early_init()
925 hl_mem_mgr_fini(&hdev->kernel_mem_mgr); in device_early_init()
926 hl_mem_mgr_idr_destroy(&hdev->kernel_mem_mgr); in device_early_init()
928 kfree(hdev->hl_chip_info); in device_early_init()
930 destroy_workqueue(hdev->prefetch_wq); in device_early_init()
932 destroy_workqueue(hdev->ts_free_obj_wq); in device_early_init()
934 destroy_workqueue(hdev->cs_cmplt_wq); in device_early_init()
936 destroy_workqueue(hdev->eq_wq); in device_early_init()
938 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) in device_early_init()
939 if (hdev->cq_wq[i]) in device_early_init()
940 destroy_workqueue(hdev->cq_wq[i]); in device_early_init()
941 kfree(hdev->cq_wq); in device_early_init()
943 hl_asid_fini(hdev); in device_early_init()
945 if (hdev->asic_funcs->early_fini) in device_early_init()
946 hdev->asic_funcs->early_fini(hdev); in device_early_init()
957 static void device_early_fini(struct hl_device *hdev) in device_early_fini() argument
961 mutex_destroy(&hdev->debug_lock); in device_early_fini()
962 mutex_destroy(&hdev->send_cpu_message_lock); in device_early_fini()
964 mutex_destroy(&hdev->fpriv_list_lock); in device_early_fini()
965 mutex_destroy(&hdev->fpriv_ctrl_list_lock); in device_early_fini()
967 mutex_destroy(&hdev->clk_throttling.lock); in device_early_fini()
969 hl_mem_mgr_fini(&hdev->kernel_mem_mgr); in device_early_fini()
970 hl_mem_mgr_idr_destroy(&hdev->kernel_mem_mgr); in device_early_fini()
972 kfree(hdev->hl_chip_info); in device_early_fini()
974 destroy_workqueue(hdev->prefetch_wq); in device_early_fini()
975 destroy_workqueue(hdev->ts_free_obj_wq); in device_early_fini()
976 destroy_workqueue(hdev->cs_cmplt_wq); in device_early_fini()
977 destroy_workqueue(hdev->eq_wq); in device_early_fini()
978 destroy_workqueue(hdev->reset_wq); in device_early_fini()
980 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) in device_early_fini()
981 destroy_workqueue(hdev->cq_wq[i]); in device_early_fini()
982 kfree(hdev->cq_wq); in device_early_fini()
984 hl_asid_fini(hdev); in device_early_fini()
986 if (hdev->asic_funcs->early_fini) in device_early_fini()
987 hdev->asic_funcs->early_fini(hdev); in device_early_fini()
990 static bool is_pci_link_healthy(struct hl_device *hdev) in is_pci_link_healthy() argument
994 if (!hdev->pdev) in is_pci_link_healthy()
997 pci_read_config_word(hdev->pdev, PCI_VENDOR_ID, &vendor_id); in is_pci_link_healthy()
1004 struct hl_device *hdev = container_of(work, struct hl_device, in hl_device_heartbeat() local
1009 if (!hl_device_operational(hdev, NULL)) in hl_device_heartbeat()
1012 if (!hdev->asic_funcs->send_heartbeat(hdev)) in hl_device_heartbeat()
1015 if (hl_device_operational(hdev, NULL)) in hl_device_heartbeat()
1016 dev_err(hdev->dev, "Device heartbeat failed! PCI link is %s\n", in hl_device_heartbeat()
1017 is_pci_link_healthy(hdev) ? "healthy" : "broken"); in hl_device_heartbeat()
1021 hl_handle_fw_err(hdev, &info); in hl_device_heartbeat()
1022 hl_device_cond_reset(hdev, HL_DRV_RESET_HARD | HL_DRV_RESET_HEARTBEAT, event_mask); in hl_device_heartbeat()
1037 if (!hdev->reset_info.in_reset) in hl_device_heartbeat()
1038 hdev->reset_info.prev_reset_trigger = HL_RESET_TRIGGER_DEFAULT; in hl_device_heartbeat()
1040 schedule_delayed_work(&hdev->work_heartbeat, in hl_device_heartbeat()
1052 static int device_late_init(struct hl_device *hdev) in device_late_init() argument
1056 if (hdev->asic_funcs->late_init) { in device_late_init()
1057 rc = hdev->asic_funcs->late_init(hdev); in device_late_init()
1059 dev_err(hdev->dev, in device_late_init()
1065 hdev->high_pll = hdev->asic_prop.high_pll; in device_late_init()
1067 if (hdev->heartbeat) { in device_late_init()
1068 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat); in device_late_init()
1069 schedule_delayed_work(&hdev->work_heartbeat, in device_late_init()
1073 hdev->late_init_done = true; in device_late_init()
1084 static void device_late_fini(struct hl_device *hdev) in device_late_fini() argument
1086 if (!hdev->late_init_done) in device_late_fini()
1089 if (hdev->heartbeat) in device_late_fini()
1090 cancel_delayed_work_sync(&hdev->work_heartbeat); in device_late_fini()
1092 if (hdev->asic_funcs->late_fini) in device_late_fini()
1093 hdev->asic_funcs->late_fini(hdev); in device_late_fini()
1095 hdev->late_init_done = false; in device_late_fini()
1098 int hl_device_utilization(struct hl_device *hdev, u32 *utilization) in hl_device_utilization() argument
1103 max_power = hdev->max_power; in hl_device_utilization()
1104 dc_power = hdev->asic_prop.dc_power_default; in hl_device_utilization()
1107 dev_warn(hdev->dev, "device utilization is not supported\n"); in hl_device_utilization()
1110 rc = hl_fw_cpucp_power_get(hdev, &curr_power); in hl_device_utilization()
1123 int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable) in hl_device_set_debug_mode() argument
1127 mutex_lock(&hdev->debug_lock); in hl_device_set_debug_mode()
1130 if (!hdev->in_debug) { in hl_device_set_debug_mode()
1131 dev_err(hdev->dev, in hl_device_set_debug_mode()
1137 if (!hdev->reset_info.hard_reset_pending) in hl_device_set_debug_mode()
1138 hdev->asic_funcs->halt_coresight(hdev, ctx); in hl_device_set_debug_mode()
1140 hdev->in_debug = 0; in hl_device_set_debug_mode()
1145 if (hdev->in_debug) { in hl_device_set_debug_mode()
1146 dev_err(hdev->dev, in hl_device_set_debug_mode()
1152 hdev->in_debug = 1; in hl_device_set_debug_mode()
1155 mutex_unlock(&hdev->debug_lock); in hl_device_set_debug_mode()
1160 static void take_release_locks(struct hl_device *hdev) in take_release_locks() argument
1165 hdev->asic_funcs->hw_queues_lock(hdev); in take_release_locks()
1166 hdev->asic_funcs->hw_queues_unlock(hdev); in take_release_locks()
1169 mutex_lock(&hdev->send_cpu_message_lock); in take_release_locks()
1170 mutex_unlock(&hdev->send_cpu_message_lock); in take_release_locks()
1173 mutex_lock(&hdev->fpriv_list_lock); in take_release_locks()
1174 mutex_unlock(&hdev->fpriv_list_lock); in take_release_locks()
1175 mutex_lock(&hdev->fpriv_ctrl_list_lock); in take_release_locks()
1176 mutex_unlock(&hdev->fpriv_ctrl_list_lock); in take_release_locks()
1179 static void hl_abort_waiting_for_completions(struct hl_device *hdev) in hl_abort_waiting_for_completions() argument
1181 hl_abort_waiting_for_cs_completions(hdev); in hl_abort_waiting_for_completions()
1186 hl_release_pending_user_interrupts(hdev); in hl_abort_waiting_for_completions()
1189 static void cleanup_resources(struct hl_device *hdev, bool hard_reset, bool fw_reset, in cleanup_resources() argument
1193 device_late_fini(hdev); in cleanup_resources()
1200 hdev->asic_funcs->halt_engines(hdev, hard_reset, fw_reset); in cleanup_resources()
1203 hl_cs_rollback_all(hdev, skip_wq_flush); in cleanup_resources()
1206 flush_workqueue(hdev->prefetch_wq); in cleanup_resources()
1208 hl_abort_waiting_for_completions(hdev); in cleanup_resources()
1220 int hl_device_suspend(struct hl_device *hdev) in hl_device_suspend() argument
1224 pci_save_state(hdev->pdev); in hl_device_suspend()
1227 spin_lock(&hdev->reset_info.lock); in hl_device_suspend()
1228 if (hdev->reset_info.in_reset) { in hl_device_suspend()
1229 spin_unlock(&hdev->reset_info.lock); in hl_device_suspend()
1230 dev_err(hdev->dev, "Can't suspend while in reset\n"); in hl_device_suspend()
1233 hdev->reset_info.in_reset = 1; in hl_device_suspend()
1234 spin_unlock(&hdev->reset_info.lock); in hl_device_suspend()
1237 hdev->disabled = true; in hl_device_suspend()
1239 take_release_locks(hdev); in hl_device_suspend()
1241 rc = hdev->asic_funcs->suspend(hdev); in hl_device_suspend()
1243 dev_err(hdev->dev, in hl_device_suspend()
1247 pci_disable_device(hdev->pdev); in hl_device_suspend()
1248 pci_set_power_state(hdev->pdev, PCI_D3hot); in hl_device_suspend()
1262 int hl_device_resume(struct hl_device *hdev) in hl_device_resume() argument
1266 pci_set_power_state(hdev->pdev, PCI_D0); in hl_device_resume()
1267 pci_restore_state(hdev->pdev); in hl_device_resume()
1268 rc = pci_enable_device_mem(hdev->pdev); in hl_device_resume()
1270 dev_err(hdev->dev, in hl_device_resume()
1275 pci_set_master(hdev->pdev); in hl_device_resume()
1277 rc = hdev->asic_funcs->resume(hdev); in hl_device_resume()
1279 dev_err(hdev->dev, "Failed to resume device after suspend\n"); in hl_device_resume()
1287 spin_lock(&hdev->reset_info.lock); in hl_device_resume()
1288 hdev->reset_info.in_reset = 0; in hl_device_resume()
1289 spin_unlock(&hdev->reset_info.lock); in hl_device_resume()
1291 rc = hl_device_reset(hdev, HL_DRV_RESET_HARD); in hl_device_resume()
1293 dev_err(hdev->dev, "Failed to reset device during resume\n"); in hl_device_resume()
1300 pci_disable_device(hdev->pdev); in hl_device_resume()
1305 static int device_kill_open_processes(struct hl_device *hdev, u32 timeout, bool control_dev) in device_kill_open_processes() argument
1313 fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock; in device_kill_open_processes()
1314 fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list; in device_kill_open_processes()
1325 if (hdev->process_kill_trial_cnt) { in device_kill_open_processes()
1343 dev_info(hdev->dev, "Killing user process pid=%d\n", in device_kill_open_processes()
1355 dev_dbg(hdev->dev, in device_kill_open_processes()
1373 dev_dbg(hdev->dev, in device_kill_open_processes()
1386 if (hdev->process_kill_trial_cnt == HL_PENDING_RESET_MAX_TRIALS) in device_kill_open_processes()
1389 hdev->process_kill_trial_cnt++; in device_kill_open_processes()
1394 static void device_disable_open_processes(struct hl_device *hdev, bool control_dev) in device_disable_open_processes() argument
1400 fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock; in device_disable_open_processes()
1401 fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list; in device_disable_open_processes()
1405 hpriv->hdev = NULL; in device_disable_open_processes()
1409 static void send_disable_pci_access(struct hl_device *hdev, u32 flags) in send_disable_pci_access() argument
1425 if (hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0)) { in send_disable_pci_access()
1426 dev_warn(hdev->dev, "Failed to disable FW's PCI access\n"); in send_disable_pci_access()
1431 if (hdev->cpu_queues_enable) in send_disable_pci_access()
1432 synchronize_irq(pci_irq_vector(hdev->pdev, in send_disable_pci_access()
1433 hdev->asic_prop.eq_interrupt_id)); in send_disable_pci_access()
1437 static void handle_reset_trigger(struct hl_device *hdev, u32 flags) in handle_reset_trigger() argument
1442 if (hdev->is_compute_ctx_active) in handle_reset_trigger()
1452 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_HEARTBEAT; in handle_reset_trigger()
1455 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_TDR; in handle_reset_trigger()
1458 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN; in handle_reset_trigger()
1461 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN; in handle_reset_trigger()
1469 if (hdev->reset_info.prev_reset_trigger != cur_reset_trigger) { in handle_reset_trigger()
1470 hdev->reset_info.prev_reset_trigger = cur_reset_trigger; in handle_reset_trigger()
1471 hdev->reset_info.reset_trigger_repeated = 0; in handle_reset_trigger()
1473 hdev->reset_info.reset_trigger_repeated = 1; in handle_reset_trigger()
1493 int hl_device_reset(struct hl_device *hdev, u32 flags) in hl_device_reset() argument
1501 if (!hdev->init_done) { in hl_device_reset()
1502 dev_err(hdev->dev, "Can't reset before initialization is done\n"); in hl_device_reset()
1512 reset_upon_device_release = hdev->reset_upon_device_release && from_dev_release; in hl_device_reset()
1514 if (!hard_reset && (hl_device_status(hdev) == HL_DEVICE_STATUS_MALFUNCTION)) { in hl_device_reset()
1515 dev_dbg(hdev->dev, "soft-reset isn't supported on a malfunctioning device\n"); in hl_device_reset()
1519 if (!hard_reset && !hdev->asic_prop.supports_compute_reset) { in hl_device_reset()
1520 dev_dbg(hdev->dev, "asic doesn't support compute reset - do hard-reset instead\n"); in hl_device_reset()
1526 dev_crit(hdev->dev, in hl_device_reset()
1534 if (!hard_reset && !hdev->asic_prop.allow_inference_soft_reset) { in hl_device_reset()
1535 dev_dbg(hdev->dev, in hl_device_reset()
1542 if (from_hard_reset_thread && hdev->process_kill_trial_cnt) in hl_device_reset()
1552 spin_lock(&hdev->reset_info.lock); in hl_device_reset()
1553 if (hdev->reset_info.in_reset) { in hl_device_reset()
1555 if (hard_reset && hdev->reset_info.in_compute_reset) in hl_device_reset()
1556 hdev->reset_info.hard_reset_schedule_flags = flags; in hl_device_reset()
1557 spin_unlock(&hdev->reset_info.lock); in hl_device_reset()
1564 hdev->reset_info.in_compute_reset = !hard_reset; in hl_device_reset()
1566 hdev->reset_info.in_reset = 1; in hl_device_reset()
1568 spin_unlock(&hdev->reset_info.lock); in hl_device_reset()
1574 if ((hard_reset || from_dev_release) && hdev->reset_info.watchdog_active) { in hl_device_reset()
1576 &hdev->device_release_watchdog_work; in hl_device_reset()
1578 hdev->reset_info.watchdog_active = 0; in hl_device_reset()
1583 hdev->reset_info.in_compute_reset = 0; in hl_device_reset()
1594 handle_reset_trigger(hdev, flags); in hl_device_reset()
1595 send_disable_pci_access(hdev, flags); in hl_device_reset()
1598 hdev->disabled = true; in hl_device_reset()
1600 take_release_locks(hdev); in hl_device_reset()
1603 dev_info(hdev->dev, "Going to reset device\n"); in hl_device_reset()
1605 dev_dbg(hdev->dev, "Going to reset device after release by user\n"); in hl_device_reset()
1607 dev_dbg(hdev->dev, "Going to reset engines of inference device\n"); in hl_device_reset()
1611 hdev->reset_info.hard_reset_pending = true; in hl_device_reset()
1613 hdev->process_kill_trial_cnt = 0; in hl_device_reset()
1615 hdev->device_reset_work.flags = flags; in hl_device_reset()
1621 queue_delayed_work(hdev->reset_wq, &hdev->device_reset_work.reset_work, 0); in hl_device_reset()
1626 cleanup_resources(hdev, hard_reset, fw_reset, from_dev_release); in hl_device_reset()
1634 rc = device_kill_open_processes(hdev, 0, false); in hl_device_reset()
1637 if (hdev->device_fini_pending) { in hl_device_reset()
1638 dev_crit(hdev->dev, in hl_device_reset()
1640 dev_name(&(hdev)->pdev->dev)); in hl_device_reset()
1649 dev_crit(hdev->dev, in hl_device_reset()
1651 dev_name(&(hdev)->pdev->dev)); in hl_device_reset()
1658 flush_workqueue(hdev->eq_wq); in hl_device_reset()
1662 hw_fini_rc = hdev->asic_funcs->hw_fini(hdev, hard_reset, fw_reset); in hl_device_reset()
1665 hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE; in hl_device_reset()
1668 if (hdev->kernel_ctx && hl_ctx_put(hdev->kernel_ctx) == 1) in hl_device_reset()
1669 hdev->kernel_ctx = NULL; in hl_device_reset()
1671 hl_vm_fini(hdev); in hl_device_reset()
1672 hl_mmu_fini(hdev); in hl_device_reset()
1673 hl_eq_reset(hdev, &hdev->event_queue); in hl_device_reset()
1677 hl_hw_queue_reset(hdev, hard_reset); in hl_device_reset()
1678 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) in hl_device_reset()
1679 hl_cq_reset(hdev, &hdev->completion_queue[i]); in hl_device_reset()
1682 ctx = hl_get_compute_ctx(hdev); in hl_device_reset()
1696 hdev->device_cpu_disabled = false; in hl_device_reset()
1697 hdev->reset_info.hard_reset_pending = false; in hl_device_reset()
1699 if (hdev->reset_info.reset_trigger_repeated && in hl_device_reset()
1700 (hdev->reset_info.prev_reset_trigger == in hl_device_reset()
1705 dev_crit(hdev->dev, in hl_device_reset()
1707 dev_name(&(hdev)->pdev->dev)); in hl_device_reset()
1712 if (hdev->kernel_ctx) { in hl_device_reset()
1713 dev_crit(hdev->dev, in hl_device_reset()
1715 dev_name(&(hdev)->pdev->dev)); in hl_device_reset()
1720 rc = hl_mmu_init(hdev); in hl_device_reset()
1722 dev_err(hdev->dev, in hl_device_reset()
1728 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), in hl_device_reset()
1730 if (!hdev->kernel_ctx) { in hl_device_reset()
1732 hl_mmu_fini(hdev); in hl_device_reset()
1736 hdev->is_compute_ctx_active = false; in hl_device_reset()
1738 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true); in hl_device_reset()
1740 dev_err(hdev->dev, in hl_device_reset()
1742 kfree(hdev->kernel_ctx); in hl_device_reset()
1743 hdev->kernel_ctx = NULL; in hl_device_reset()
1744 hl_mmu_fini(hdev); in hl_device_reset()
1753 hdev->disabled = false; in hl_device_reset()
1757 rc = hl_fw_read_preboot_status(hdev); in hl_device_reset()
1762 rc = hdev->asic_funcs->hw_init(hdev); in hl_device_reset()
1764 dev_err(hdev->dev, "failed to initialize the H/W after reset\n"); in hl_device_reset()
1769 if (!hdev->asic_funcs->is_device_idle(hdev, idle_mask, in hl_device_reset()
1771 print_idle_status_mask(hdev, "device is not idle after reset", idle_mask); in hl_device_reset()
1777 rc = hdev->asic_funcs->test_queues(hdev); in hl_device_reset()
1779 dev_err(hdev->dev, "Failed to detect if device is alive after reset\n"); in hl_device_reset()
1784 rc = device_late_init(hdev); in hl_device_reset()
1786 dev_err(hdev->dev, "Failed late init after hard reset\n"); in hl_device_reset()
1790 rc = hl_vm_init(hdev); in hl_device_reset()
1792 dev_err(hdev->dev, "Failed to init memory module after hard reset\n"); in hl_device_reset()
1796 if (!hdev->asic_prop.fw_security_enabled) in hl_device_reset()
1797 hl_fw_set_max_power(hdev); in hl_device_reset()
1799 rc = hdev->asic_funcs->compute_reset_late_init(hdev); in hl_device_reset()
1802 dev_err(hdev->dev, in hl_device_reset()
1805 dev_err(hdev->dev, "Failed late init after compute reset\n"); in hl_device_reset()
1810 rc = hdev->asic_funcs->scrub_device_mem(hdev); in hl_device_reset()
1812 dev_err(hdev->dev, "scrub mem failed from device reset (%d)\n", rc); in hl_device_reset()
1816 spin_lock(&hdev->reset_info.lock); in hl_device_reset()
1817 hdev->reset_info.in_compute_reset = 0; in hl_device_reset()
1823 if (!hard_reset && hdev->reset_info.hard_reset_schedule_flags) in hl_device_reset()
1826 hdev->reset_info.in_reset = 0; in hl_device_reset()
1828 spin_unlock(&hdev->reset_info.lock); in hl_device_reset()
1830 hdev->reset_info.needs_reset = false; in hl_device_reset()
1833 dev_info(hdev->dev, in hl_device_reset()
1835 dev_name(&(hdev)->pdev->dev)); in hl_device_reset()
1837 dev_dbg(hdev->dev, in hl_device_reset()
1839 dev_name(&(hdev)->pdev->dev)); in hl_device_reset()
1842 hdev->reset_info.hard_reset_cnt++; in hl_device_reset()
1849 hdev->asic_funcs->enable_events_from_fw(hdev); in hl_device_reset()
1852 hdev->reset_info.compute_reset_cnt++; in hl_device_reset()
1855 dev_info(hdev->dev, "Performing hard reset scheduled during compute reset\n"); in hl_device_reset()
1856 flags = hdev->reset_info.hard_reset_schedule_flags; in hl_device_reset()
1857 hdev->reset_info.hard_reset_schedule_flags = 0; in hl_device_reset()
1866 hdev->disabled = true; in hl_device_reset()
1868 spin_lock(&hdev->reset_info.lock); in hl_device_reset()
1869 hdev->reset_info.in_compute_reset = 0; in hl_device_reset()
1872 dev_err(hdev->dev, in hl_device_reset()
1874 dev_name(&(hdev)->pdev->dev)); in hl_device_reset()
1875 hdev->reset_info.hard_reset_cnt++; in hl_device_reset()
1878 dev_err(hdev->dev, "Failed to reset device after user release\n"); in hl_device_reset()
1881 dev_err(hdev->dev, "Failed to do compute reset\n"); in hl_device_reset()
1882 hdev->reset_info.compute_reset_cnt++; in hl_device_reset()
1885 spin_unlock(&hdev->reset_info.lock); in hl_device_reset()
1891 hdev->reset_info.in_reset = 0; in hl_device_reset()
1893 spin_unlock(&hdev->reset_info.lock); in hl_device_reset()
1907 int hl_device_cond_reset(struct hl_device *hdev, u32 flags, u64 event_mask) in hl_device_cond_reset() argument
1917 dev_err(hdev->dev, "Resetting device without a reset indication to user\n"); in hl_device_cond_reset()
1921 ctx = hl_get_compute_ctx(hdev); in hl_device_cond_reset()
1928 spin_lock(&hdev->reset_info.lock); in hl_device_cond_reset()
1929 if (hdev->reset_info.in_reset) { in hl_device_cond_reset()
1930 spin_unlock(&hdev->reset_info.lock); in hl_device_cond_reset()
1934 if (hdev->reset_info.watchdog_active) in hl_device_cond_reset()
1937 hdev->device_release_watchdog_work.flags = flags; in hl_device_cond_reset()
1938 dev_dbg(hdev->dev, "Device is going to be hard-reset in %u sec unless being released\n", in hl_device_cond_reset()
1939 hdev->device_release_watchdog_timeout_sec); in hl_device_cond_reset()
1940 schedule_delayed_work(&hdev->device_release_watchdog_work.reset_work, in hl_device_cond_reset()
1941 msecs_to_jiffies(hdev->device_release_watchdog_timeout_sec * 1000)); in hl_device_cond_reset()
1942 hdev->reset_info.watchdog_active = 1; in hl_device_cond_reset()
1944 spin_unlock(&hdev->reset_info.lock); in hl_device_cond_reset()
1946 hl_notifier_event_send_all(hdev, event_mask); in hl_device_cond_reset()
1950 hl_abort_waiting_for_completions(hdev); in hl_device_cond_reset()
1956 hl_notifier_event_send_all(hdev, event_mask); in hl_device_cond_reset()
1960 return hl_device_reset(hdev, flags); in hl_device_cond_reset()
1981 void hl_notifier_event_send_all(struct hl_device *hdev, u64 event_mask) in hl_notifier_event_send_all() argument
1986 dev_warn(hdev->dev, "Skip sending zero event"); in hl_notifier_event_send_all()
1990 mutex_lock(&hdev->fpriv_list_lock); in hl_notifier_event_send_all()
1992 list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) in hl_notifier_event_send_all()
1995 mutex_unlock(&hdev->fpriv_list_lock); in hl_notifier_event_send_all()
1998 mutex_lock(&hdev->fpriv_ctrl_list_lock); in hl_notifier_event_send_all()
2000 list_for_each_entry(hpriv, &hdev->fpriv_ctrl_list, dev_node) in hl_notifier_event_send_all()
2003 mutex_unlock(&hdev->fpriv_ctrl_list_lock); in hl_notifier_event_send_all()
2006 static int create_cdev(struct hl_device *hdev) in create_cdev() argument
2011 hdev->cdev_idx = hdev->id / 2; in create_cdev()
2013 name = kasprintf(GFP_KERNEL, "hl%d", hdev->cdev_idx); in create_cdev()
2020 rc = device_init_cdev(hdev, hdev->hclass, hdev->id, &hl_ops, name, in create_cdev()
2021 &hdev->cdev, &hdev->dev); in create_cdev()
2028 name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->cdev_idx); in create_cdev()
2035 rc = device_init_cdev(hdev, hdev->hclass, hdev->id_control, &hl_ctrl_ops, in create_cdev()
2036 name, &hdev->cdev_ctrl, &hdev->dev_ctrl); in create_cdev()
2046 put_device(hdev->dev); in create_cdev()
2060 int hl_device_init(struct hl_device *hdev) in hl_device_init() argument
2065 rc = create_cdev(hdev); in hl_device_init()
2070 rc = device_early_init(hdev); in hl_device_init()
2074 user_interrupt_cnt = hdev->asic_prop.user_dec_intr_count + in hl_device_init()
2075 hdev->asic_prop.user_interrupt_count; in hl_device_init()
2078 hdev->user_interrupt = kcalloc(user_interrupt_cnt, sizeof(*hdev->user_interrupt), in hl_device_init()
2080 if (!hdev->user_interrupt) { in hl_device_init()
2090 rc = hdev->asic_funcs->sw_init(hdev); in hl_device_init()
2096 hl_multi_cs_completion_init(hdev); in hl_device_init()
2103 rc = hl_hw_queues_create(hdev); in hl_device_init()
2105 dev_err(hdev->dev, "failed to initialize kernel queues\n"); in hl_device_init()
2109 cq_cnt = hdev->asic_prop.completion_queues_count; in hl_device_init()
2117 hdev->completion_queue = kcalloc(cq_cnt, in hl_device_init()
2118 sizeof(*hdev->completion_queue), in hl_device_init()
2121 if (!hdev->completion_queue) { in hl_device_init()
2122 dev_err(hdev->dev, in hl_device_init()
2130 rc = hl_cq_init(hdev, &hdev->completion_queue[i], in hl_device_init()
2131 hdev->asic_funcs->get_queue_id_for_cq(hdev, i)); in hl_device_init()
2133 dev_err(hdev->dev, in hl_device_init()
2137 hdev->completion_queue[i].cq_idx = i; in hl_device_init()
2140 hdev->shadow_cs_queue = kcalloc(hdev->asic_prop.max_pending_cs, in hl_device_init()
2142 if (!hdev->shadow_cs_queue) { in hl_device_init()
2152 rc = hl_eq_init(hdev, &hdev->event_queue); in hl_device_init()
2154 dev_err(hdev->dev, "failed to initialize event queue\n"); in hl_device_init()
2159 rc = hl_mmu_init(hdev); in hl_device_init()
2161 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n"); in hl_device_init()
2166 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL); in hl_device_init()
2167 if (!hdev->kernel_ctx) { in hl_device_init()
2172 hdev->is_compute_ctx_active = false; in hl_device_init()
2174 hdev->asic_funcs->state_dump_init(hdev); in hl_device_init()
2176 hdev->device_release_watchdog_timeout_sec = HL_DEVICE_RELEASE_WATCHDOG_TIMEOUT_SEC; in hl_device_init()
2178 hdev->memory_scrub_val = MEM_SCRUB_DEFAULT_VAL; in hl_device_init()
2180 rc = hl_debugfs_device_init(hdev); in hl_device_init()
2182 dev_err(hdev->dev, "failed to initialize debugfs entry structure\n"); in hl_device_init()
2183 kfree(hdev->kernel_ctx); in hl_device_init()
2190 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true); in hl_device_init()
2192 dev_err(hdev->dev, "failed to initialize kernel context\n"); in hl_device_init()
2193 kfree(hdev->kernel_ctx); in hl_device_init()
2197 rc = hl_cb_pool_init(hdev); in hl_device_init()
2199 dev_err(hdev->dev, "failed to initialize CB pool\n"); in hl_device_init()
2203 rc = hl_dec_init(hdev); in hl_device_init()
2205 dev_err(hdev->dev, "Failed to initialize the decoder module\n"); in hl_device_init()
2219 hdev->disabled = false; in hl_device_init()
2221 rc = hdev->asic_funcs->hw_init(hdev); in hl_device_init()
2223 dev_err(hdev->dev, "failed to initialize the H/W\n"); in hl_device_init()
2229 rc = hdev->asic_funcs->test_queues(hdev); in hl_device_init()
2231 dev_err(hdev->dev, "Failed to detect if device is alive\n"); in hl_device_init()
2236 rc = device_late_init(hdev); in hl_device_init()
2238 dev_err(hdev->dev, "Failed late initialization\n"); in hl_device_init()
2243 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n", in hl_device_init()
2244 hdev->asic_name, in hl_device_init()
2245 hdev->asic_prop.dram_size / SZ_1G); in hl_device_init()
2247 rc = hl_vm_init(hdev); in hl_device_init()
2249 dev_err(hdev->dev, "Failed to initialize memory module\n"); in hl_device_init()
2259 rc = cdev_sysfs_debugfs_add(hdev); in hl_device_init()
2261 dev_err(hdev->dev, "Failed to add char devices and sysfs/debugfs files\n"); in hl_device_init()
2269 if (hdev->asic_prop.set_max_power_on_device_init && in hl_device_init()
2270 !hdev->asic_prop.fw_security_enabled) in hl_device_init()
2271 hl_fw_set_max_power(hdev); in hl_device_init()
2279 rc = hl_hwmon_init(hdev); in hl_device_init()
2281 dev_err(hdev->dev, "Failed to initialize hwmon\n"); in hl_device_init()
2286 dev_notice(hdev->dev, in hl_device_init()
2288 dev_name(&(hdev)->pdev->dev)); in hl_device_init()
2290 hdev->init_done = true; in hl_device_init()
2297 hdev->asic_funcs->enable_events_from_fw(hdev); in hl_device_init()
2302 hl_cb_pool_fini(hdev); in hl_device_init()
2304 if (hl_ctx_put(hdev->kernel_ctx) != 1) in hl_device_init()
2305 dev_err(hdev->dev, in hl_device_init()
2308 hl_debugfs_device_fini(hdev); in hl_device_init()
2310 hl_mmu_fini(hdev); in hl_device_init()
2312 hl_eq_fini(hdev, &hdev->event_queue); in hl_device_init()
2314 kfree(hdev->shadow_cs_queue); in hl_device_init()
2317 hl_cq_fini(hdev, &hdev->completion_queue[i]); in hl_device_init()
2318 kfree(hdev->completion_queue); in hl_device_init()
2320 hl_hw_queues_destroy(hdev); in hl_device_init()
2322 hdev->asic_funcs->sw_fini(hdev); in hl_device_init()
2324 kfree(hdev->user_interrupt); in hl_device_init()
2326 device_early_fini(hdev); in hl_device_init()
2328 put_device(hdev->dev_ctrl); in hl_device_init()
2329 put_device(hdev->dev); in hl_device_init()
2331 hdev->disabled = true; in hl_device_init()
2333 cdev_sysfs_debugfs_add(hdev); in hl_device_init()
2334 dev_err(&hdev->pdev->dev, in hl_device_init()
2336 hdev->cdev_idx, dev_name(&hdev->pdev->dev)); in hl_device_init()
2348 void hl_device_fini(struct hl_device *hdev) in hl_device_fini() argument
2355 dev_info(hdev->dev, "Removing device\n"); in hl_device_fini()
2357 hdev->device_fini_pending = 1; in hl_device_fini()
2358 flush_delayed_work(&hdev->device_reset_work.reset_work); in hl_device_fini()
2360 if (hdev->pldm) in hl_device_fini()
2375 spin_lock(&hdev->reset_info.lock); in hl_device_fini()
2376 device_in_reset = !!hdev->reset_info.in_reset; in hl_device_fini()
2378 hdev->reset_info.in_reset = 1; in hl_device_fini()
2379 spin_unlock(&hdev->reset_info.lock); in hl_device_fini()
2384 spin_lock(&hdev->reset_info.lock); in hl_device_fini()
2385 device_in_reset = !!hdev->reset_info.in_reset; in hl_device_fini()
2387 hdev->reset_info.in_reset = 1; in hl_device_fini()
2388 spin_unlock(&hdev->reset_info.lock); in hl_device_fini()
2391 dev_crit(hdev->dev, in hl_device_fini()
2393 dev_name(&(hdev)->pdev->dev)); in hl_device_fini()
2398 cancel_delayed_work_sync(&hdev->device_release_watchdog_work.reset_work); in hl_device_fini()
2407 hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0); in hl_device_fini()
2410 hdev->disabled = true; in hl_device_fini()
2412 take_release_locks(hdev); in hl_device_fini()
2414 hdev->reset_info.hard_reset_pending = true; in hl_device_fini()
2416 hl_hwmon_fini(hdev); in hl_device_fini()
2418 cleanup_resources(hdev, true, false, false); in hl_device_fini()
2424 dev_info(hdev->dev, in hl_device_fini()
2428 hdev->process_kill_trial_cnt = 0; in hl_device_fini()
2429 rc = device_kill_open_processes(hdev, HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI, false); in hl_device_fini()
2431 dev_crit(hdev->dev, "Failed to kill all open processes\n"); in hl_device_fini()
2432 device_disable_open_processes(hdev, false); in hl_device_fini()
2435 hdev->process_kill_trial_cnt = 0; in hl_device_fini()
2436 rc = device_kill_open_processes(hdev, 0, true); in hl_device_fini()
2438 dev_crit(hdev->dev, "Failed to kill all control device open processes\n"); in hl_device_fini()
2439 device_disable_open_processes(hdev, true); in hl_device_fini()
2442 hl_cb_pool_fini(hdev); in hl_device_fini()
2445 rc = hdev->asic_funcs->hw_fini(hdev, true, false); in hl_device_fini()
2447 dev_err(hdev->dev, "hw_fini failed in device fini while removing device %d\n", rc); in hl_device_fini()
2449 hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE; in hl_device_fini()
2452 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1)) in hl_device_fini()
2453 dev_err(hdev->dev, "kernel ctx is still alive\n"); in hl_device_fini()
2455 hl_dec_fini(hdev); in hl_device_fini()
2457 hl_vm_fini(hdev); in hl_device_fini()
2459 hl_mmu_fini(hdev); in hl_device_fini()
2461 vfree(hdev->captured_err_info.page_fault_info.user_mappings); in hl_device_fini()
2463 hl_eq_fini(hdev, &hdev->event_queue); in hl_device_fini()
2465 kfree(hdev->shadow_cs_queue); in hl_device_fini()
2467 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) in hl_device_fini()
2468 hl_cq_fini(hdev, &hdev->completion_queue[i]); in hl_device_fini()
2469 kfree(hdev->completion_queue); in hl_device_fini()
2470 kfree(hdev->user_interrupt); in hl_device_fini()
2472 hl_hw_queues_destroy(hdev); in hl_device_fini()
2475 hdev->asic_funcs->sw_fini(hdev); in hl_device_fini()
2477 device_early_fini(hdev); in hl_device_fini()
2480 cdev_sysfs_debugfs_remove(hdev); in hl_device_fini()
2482 hl_debugfs_device_fini(hdev); in hl_device_fini()
2500 inline u32 hl_rreg(struct hl_device *hdev, u32 reg) in hl_rreg() argument
2502 u32 val = readl(hdev->rmmio + reg); in hl_rreg()
2505 trace_habanalabs_rreg32(hdev->dev, reg, val); in hl_rreg()
2520 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val) in hl_wreg() argument
2523 trace_habanalabs_wreg32(hdev->dev, reg, val); in hl_wreg()
2525 writel(val, hdev->rmmio + reg); in hl_wreg()
2528 void hl_capture_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines, in hl_capture_razwi() argument
2531 struct razwi_info *razwi_info = &hdev->captured_err_info.razwi_info; in hl_capture_razwi()
2534 dev_err(hdev->dev, in hl_capture_razwi()
2541 if (atomic_cmpxchg(&hdev->captured_err_info.razwi_info.razwi_detected, 0, 1)) in hl_capture_razwi()
2554 void hl_handle_razwi(struct hl_device *hdev, u64 addr, u16 *engine_id, u16 num_of_engines, in hl_handle_razwi() argument
2557 hl_capture_razwi(hdev, addr, engine_id, num_of_engines, flags); in hl_handle_razwi()
2563 static void hl_capture_user_mappings(struct hl_device *hdev, bool is_pmmu) in hl_capture_user_mappings() argument
2565 struct page_fault_info *pgf_info = &hdev->captured_err_info.page_fault_info; in hl_capture_user_mappings()
2577 ctx = hl_get_compute_ctx(hdev); in hl_capture_user_mappings()
2579 dev_err(hdev->dev, "Can't get user context for user mappings\n"); in hl_capture_user_mappings()
2625 void hl_capture_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu) in hl_capture_page_fault() argument
2627 struct page_fault_info *pgf_info = &hdev->captured_err_info.page_fault_info; in hl_capture_page_fault()
2636 hl_capture_user_mappings(hdev, is_pmmu); in hl_capture_page_fault()
2641 void hl_handle_page_fault(struct hl_device *hdev, u64 addr, u16 eng_id, bool is_pmmu, in hl_handle_page_fault() argument
2644 hl_capture_page_fault(hdev, addr, eng_id, is_pmmu); in hl_handle_page_fault()
2650 static void hl_capture_hw_err(struct hl_device *hdev, u16 event_id) in hl_capture_hw_err() argument
2652 struct hw_err_info *info = &hdev->captured_err_info.hw_err; in hl_capture_hw_err()
2664 void hl_handle_critical_hw_err(struct hl_device *hdev, u16 event_id, u64 *event_mask) in hl_handle_critical_hw_err() argument
2666 hl_capture_hw_err(hdev, event_id); in hl_handle_critical_hw_err()
2672 static void hl_capture_fw_err(struct hl_device *hdev, struct hl_info_fw_err_info *fw_info) in hl_capture_fw_err() argument
2674 struct fw_err_info *info = &hdev->captured_err_info.fw_err; in hl_capture_fw_err()
2688 void hl_handle_fw_err(struct hl_device *hdev, struct hl_info_fw_err_info *info) in hl_handle_fw_err() argument
2690 hl_capture_fw_err(hdev, info); in hl_handle_fw_err()