rc = dma_map_sgtable(&hdev->pdev->dev, sgt, dir, 0); if (rc) return rc;
/* Shift to the device's base physical address of host memory if necessary */ if (prop->device_dma_offset_for_host_access)
for_each_sgtable_dma_sg(sgt, sg, i)
sg->dma_address += prop->device_dma_offset_for_host_access;
/* Cancel the device's base physical address of host memory if necessary */ if (prop->device_dma_offset_for_host_access)
for_each_sgtable_dma_sg(sgt, sg, i)
sg->dma_address -= prop->device_dma_offset_for_host_access;
current_status = hl_device_status(hdev); if (status)
*status = current_status;
switch (current_status) { case HL_DEVICE_STATUS_MALFUNCTION: case HL_DEVICE_STATUS_IN_RESET: case HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE: case HL_DEVICE_STATUS_NEEDS_RESET: returnfalse; case HL_DEVICE_STATUS_OPERATIONAL: case HL_DEVICE_STATUS_IN_DEVICE_CREATION: default: returntrue;
}
}
current_status = hl_device_status(hdev); if (status)
*status = current_status;
switch (current_status) { case HL_DEVICE_STATUS_MALFUNCTION: returnfalse; case HL_DEVICE_STATUS_IN_RESET: case HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE: case HL_DEVICE_STATUS_NEEDS_RESET: case HL_DEVICE_STATUS_OPERATIONAL: case HL_DEVICE_STATUS_IN_DEVICE_CREATION: default: returntrue;
}
}
/* There should be no memory buffers at this point and handles IDR can be destroyed */
hl_mem_mgr_idr_destroy(&hpriv->mem_mgr);
/* Device should be reset if reset-upon-device-release is enabled, or if there is a pending *resetthatwaitsfordevicerelease.
*/
reset_device = hdev->reset_upon_device_release || hdev->reset_info.watchdog_active;
/* Check the device idle status and reset if not idle. *Skipitifalreadyinreset,orifdeviceisgoingtoberesetinanycase.
*/ if (!hdev->reset_info.in_reset && !reset_device && !hdev->pldm)
device_is_idle = hdev->asic_funcs->is_device_idle(hdev, idle_mask,
HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL); if (!device_is_idle) {
print_idle_status_mask(hdev, "device is not idle after user context is closed",
idle_mask);
reset_device = true;
}
/* We need to remove the user from the list to make sure the reset process won't *trytokilltheuserprocess.Because,ifwegothere,itmeansthereareno *moredriver/deviceresourcesthattheuserprocessisoccupyingsothereis *noneedtokillit * *However,wecan'tsetthecompute_ctxtoNULLatthisstage.Thisistoprevent *aracebetweenthereleaseandopeningthedeviceagain.Wedon'twanttolet *auseropenthedevicewhiletherearesetisabouttohappen.
*/
mutex_lock(&hdev->fpriv_list_lock);
list_del(&hpriv->dev_node);
mutex_unlock(&hdev->fpriv_list_lock);
put_pid(hpriv->taskpid);
if (reset_device) {
hl_device_reset(hdev, HL_DRV_RESET_DEV_RELEASE);
} else { /* Scrubbing is handled within hl_device_reset(), so here need to do it directly */ int rc = hdev->asic_funcs->scrub_device_mem(hdev);
if (rc) {
dev_err(hdev->dev, "failed to scrub memory from hpriv release (%d)\n", rc);
hl_device_reset(hdev, HL_DRV_RESET_HARD);
}
}
/* Now we can mark the compute_ctx as not active. Even if a reset is running in a different *thread,wedon'tcarebecausethein_resetismarkedsoifauserwilltrytoopen *thedeviceitwillfailonthat,evenifcompute_ctxisfalse.
*/
mutex_lock(&hdev->fpriv_list_lock);
hdev->is_compute_ctx_active = false;
mutex_unlock(&hdev->fpriv_list_lock);
hdev->compute_ctx_in_release = 0;
/* release the eventfd */ if (hpriv->notifier_event.eventfd)
eventfd_ctx_put(hpriv->notifier_event.eventfd);
if (!hdev) {
pr_crit("Closing FD after device was removed. Memory leak will occur and it is advised to reboot.\n");
put_pid(hpriv->taskpid);
}
hl_ctx_mgr_fini(hdev, &hpriv->ctx_mgr);
/* Memory buffers might be still in use at this point and thus the handles IDR destruction *ispostponedtohpriv_release().
*/
hl_mem_mgr_fini(&hpriv->mem_mgr, &mm_fini_stats);
hdev->compute_ctx_in_release = 1;
if (!hl_hpriv_put(hpriv)) {
print_device_in_use_info(hdev, &mm_fini_stats, "User process closed FD but device still in use");
hl_device_reset(hdev, HL_DRV_RESET_HARD);
}
/* Initialize cdev and device structures for the control device */
snprintf(name, sizeof(name), "accel_controlD%d", hdev->cdev_idx);
rc = device_init_cdev(hdev, accel_class, hdev->cdev_idx, &hl_ctrl_ops, name,
&hdev->cdev_ctrl, &hdev->dev_ctrl); if (rc) return rc;
rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl); if (rc) {
dev_err(hdev->dev_ctrl, "failed to add an accel control char device to the system\n"); goto free_ctrl_device;
}
rc = hl_sysfs_init(hdev); if (rc) {
dev_err(hdev->dev, "failed to initialize sysfs\n"); goto delete_ctrl_cdev_device;
}
if (ctx) { /* The read refcount value should subtracted by one, because the read is *protectedwithhl_get_compute_ctx().
*/
dev_info(hdev->dev, "Could not reset device (compute_ctx refcount %u). will try again in %u seconds",
kref_read(&ctx->refcount) - 1, HL_PENDING_RESET_PER_SEC);
hl_ctx_put(ctx);
} else {
dev_info(hdev->dev, "Could not reset device. will try again in %u seconds",
HL_PENDING_RESET_PER_SEC);
}
if (hdev->asic_funcs->late_init) {
rc = hdev->asic_funcs->late_init(hdev); if (rc) {
dev_err(hdev->dev, "failed late initialization for the H/W\n"); return rc;
}
}
int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable)
{ int rc = 0;
mutex_lock(&hdev->debug_lock);
if (!enable) { if (!hdev->in_debug) {
dev_err(hdev->dev, "Failed to disable debug mode because device was not in debug mode\n");
rc = -EFAULT; goto out;
}
if (!hdev->reset_info.hard_reset_pending)
hdev->asic_funcs->halt_coresight(hdev, ctx);
hdev->in_debug = 0;
goto out;
}
if (hdev->in_debug) {
dev_err(hdev->dev, "Failed to enable debug mode because device is already in debug mode\n");
rc = -EFAULT; goto out;
}
hdev->in_debug = 1;
out:
mutex_unlock(&hdev->debug_lock);
return rc;
}
staticvoid take_release_locks(struct hl_device *hdev)
{ /* Flush anyone that is inside the critical section of enqueue *jobstotheH/W
*/
hdev->asic_funcs->hw_queues_lock(hdev);
hdev->asic_funcs->hw_queues_unlock(hdev);
/* Flush processes that are sending message to CPU */
mutex_lock(&hdev->send_cpu_message_lock);
mutex_unlock(&hdev->send_cpu_message_lock);
/* Flush anyone that is inside device open */
mutex_lock(&hdev->fpriv_list_lock);
mutex_unlock(&hdev->fpriv_list_lock);
mutex_lock(&hdev->fpriv_ctrl_list_lock);
mutex_unlock(&hdev->fpriv_ctrl_list_lock);
}
/* Go over all the queues, release all CS and their jobs */
hl_cs_rollback_all(hdev, skip_wq_flush);
/* flush the MMU prefetch workqueue */
flush_workqueue(hdev->prefetch_wq);
hl_abort_waiting_for_completions(hdev);
}
/* *hl_device_suspend-initiatedevicesuspend * *@hdev:pointertohabanalabsdevicestructure * *Putsthehwinthesuspendstate(allasics). *Returns0forsuccessoranerroronfailure. *Calledatdriversuspend.
*/ int hl_device_suspend(struct hl_device *hdev)
{ int rc;
pci_save_state(hdev->pdev);
/* Block future CS/VM/JOB completion operations */
spin_lock(&hdev->reset_info.lock); if (hdev->reset_info.in_reset) {
spin_unlock(&hdev->reset_info.lock);
dev_err(hdev->dev, "Can't suspend while in reset\n"); return -EIO;
}
hdev->reset_info.in_reset = 1;
spin_unlock(&hdev->reset_info.lock);
/* This blocks all other stuff that is not blocked by in_reset */
hdev->disabled = true;
take_release_locks(hdev);
rc = hdev->asic_funcs->suspend(hdev); if (rc)
dev_err(hdev->dev, "Failed to disable PCI access of device CPU\n");
/* Shut down the device */
pci_disable_device(hdev->pdev);
pci_set_power_state(hdev->pdev, PCI_D3hot);
return0;
}
/* *hl_device_resume-initiatedeviceresume * *@hdev:pointertohabanalabsdevicestructure * *Bringthehwbacktooperatingstate(allasics). *Returns0forsuccessoranerroronfailure. *Calledatdriverresume.
*/ int hl_device_resume(struct hl_device *hdev)
{ int rc;
pci_set_power_state(hdev->pdev, PCI_D0);
pci_restore_state(hdev->pdev);
rc = pci_enable_device_mem(hdev->pdev); if (rc) {
dev_err(hdev->dev, "Failed to enable PCI device in resume\n"); return rc;
}
pci_set_master(hdev->pdev);
rc = hdev->asic_funcs->resume(hdev); if (rc) {
dev_err(hdev->dev, "Failed to resume device after suspend\n"); goto disable_device;
}
/* 'in_reset' was set to true during suspend, now we must clear it in order *forhardresettobeperformed
*/
spin_lock(&hdev->reset_info.lock);
hdev->reset_info.in_reset = 0;
spin_unlock(&hdev->reset_info.lock);
rc = hl_device_reset(hdev, HL_DRV_RESET_HARD); if (rc) {
dev_err(hdev->dev, "Failed to reset device during resume\n"); goto disable_device;
}
/* Giving time for user to close FD, and for processes that are inside *hl_device_opentofinish
*/ if (!list_empty(hpriv_list))
ssleep(1);
if (timeout) {
pending_cnt = timeout;
} else { if (hdev->process_kill_trial_cnt) { /* Processes have been already killed */
pending_cnt = 1; goto wait_for_processes;
} else { /* Wait a small period after process kill */
pending_cnt = HL_PENDING_RESET_PER_SEC;
}
}
mutex_lock(hpriv_lock);
/* This section must be protected because we are dereferencing *pointersthatarefreediftheprocessexits
*/
list_for_each_entry(hpriv, hpriv_list, dev_node) {
task = get_pid_task(hpriv->taskpid, PIDTYPE_PID); if (task) {
dev_info(hdev->dev, "Killing user process pid=%d\n",
task_pid_nr(task));
send_sig(SIGKILL, task, 1);
usleep_range(1000, 10000);
put_task_struct(task);
} else {
dev_dbg(hdev->dev, "Can't get task struct for user process %d, process was killed from outside the driver\n",
pid_nr(hpriv->taskpid));
}
}
wait_for_processes: while ((!list_empty(hpriv_list)) && (pending_cnt)) {
dev_dbg(hdev->dev, "Waiting for all unmap operations to finish before hard reset\n");
pending_cnt--;
ssleep(1);
}
/* All processes exited successfully */ if (list_empty(hpriv_list)) return0;
/* Give up waiting for processes to exit */ if (hdev->process_kill_trial_cnt == HL_PENDING_RESET_MAX_TRIALS) return -ETIME;
staticvoid send_disable_pci_access(struct hl_device *hdev, u32 flags)
{ /* If reset is due to heartbeat, device CPU is no responsive in *whichcasenopointsendingPCIdisablemessagetoit.
*/ if ((flags & HL_DRV_RESET_HARD) &&
!(flags & (HL_DRV_RESET_HEARTBEAT | HL_DRV_RESET_BYPASS_REQ_TO_FW))) { /* Disable PCI access from device F/W so he won't send *usadditionalinterrupts.WedisableMSI/MSI-Xat *thehalt_enginesfunctionandwecan'thavetheF/W *sendingusinterruptsafterthat.Weneedtodisable *theaccessherebecauseifthedeviceismarked *disable,themessagewon'tbesend.Also,incase *ofheartbeat,thedeviceCPUismarkedasdisable *sothismessagewon'tbesent
*/ if (hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0)) return;
/* disable_irq also generates sync irq, this verifies that last EQs are handled *beforedisabledisset.TheIRQwillbeenabledagaininrequest_irqcall.
*/ if (hdev->cpu_queues_enable)
disable_irq(pci_irq_vector(hdev->pdev, hdev->asic_prop.eq_interrupt_id));
}
}
if (!hard_reset && (hl_device_status(hdev) == HL_DEVICE_STATUS_MALFUNCTION)) {
dev_dbg(hdev->dev, "soft-reset isn't supported on a malfunctioning device\n"); return0;
}
if (!hard_reset && !hdev->asic_prop.supports_compute_reset) {
dev_dbg(hdev->dev, "asic doesn't support compute reset - do hard-reset instead\n");
hard_reset = true;
}
if (reset_upon_device_release) { if (hard_reset) {
dev_crit(hdev->dev, "Aborting reset because hard-reset is mutually exclusive with reset-on-device-release\n"); return -EINVAL;
}
goto do_reset;
}
if (!hard_reset && !hdev->asic_prop.allow_inference_soft_reset) {
dev_dbg(hdev->dev, "asic doesn't allow inference soft reset - do hard-reset instead\n");
hard_reset = true;
}
do_reset: /* Re-entry of reset thread */ if (from_hard_reset_thread && hdev->process_kill_trial_cnt) goto kill_processes;
/* *Preventconcurrencyinthisfunction-onlyoneresetshouldbe *doneatanygiventime.Weneedtoperformthisonlyifwedidn't *getherefromadedicatedhardresetthread.
*/ if (!from_hard_reset_thread) { /* Block future CS/VM/JOB completion operations */
spin_lock(&hdev->reset_info.lock); if (hdev->reset_info.in_reset) { /* We allow scheduling of a hard reset only during a compute reset */ if (hard_reset && hdev->reset_info.in_compute_reset)
hdev->reset_info.hard_reset_schedule_flags = flags;
spin_unlock(&hdev->reset_info.lock); return0;
}
/* This still allows the completion of some KDMA ops *Updatethisbeforein_resetbecausein_compute_resetimpliesweareinreset
*/
hdev->reset_info.in_compute_reset = !hard_reset;
hdev->reset_info.in_reset = 1;
spin_unlock(&hdev->reset_info.lock);
/* Cancel the device release watchdog work if required. *Incaseofreset-upon-device-releasewhilethereleasewatchdogworkis *scheduledduetoahard-reset,dohard-resetinsteadofcompute-reset.
*/ if ((hard_reset || from_dev_release) && hdev->reset_info.watchdog_active) { struct hl_device_reset_work *watchdog_work =
&hdev->device_release_watchdog_work;
hdev->reset_info.watchdog_active = 0; if (!from_watchdog_thread)
cancel_delayed_work_sync(&watchdog_work->reset_work);
/* This also blocks future CS/VM/JOB completion operations */
hdev->disabled = true;
take_release_locks(hdev);
if (hard_reset)
dev_info(hdev->dev, "Going to reset device\n"); elseif (reset_upon_device_release)
dev_dbg(hdev->dev, "Going to reset device after release by user\n"); else
dev_dbg(hdev->dev, "Going to reset engines of inference device\n");
}
if ((hard_reset) && (!from_hard_reset_thread)) {
hdev->reset_info.hard_reset_pending = true;
kill_processes: if (hard_reset) { /* Kill processes here after CS rollback. This is because the *processcan'treallyexituntilallitsCSsaredone,which *iswhatwedoincsrollback
*/
rc = device_kill_open_processes(hdev, 0, false);
if (rc == -EBUSY) { if (hdev->device_fini_pending) {
dev_crit(hdev->dev, "%s Failed to kill all open processes, stopping hard reset\n",
dev_name(&(hdev)->pdev->dev)); goto out_err;
}
/* signal reset thread to reschedule */ return rc;
}
if (rc) {
dev_crit(hdev->dev, "%s Failed to kill all open processes, stopping hard reset\n",
dev_name(&(hdev)->pdev->dev)); goto out_err;
}
/* Flush the Event queue workers to make sure no other thread is *readingorwritingtoregistersduringthereset
*/
flush_workqueue(hdev->eq_wq);
}
/* Reset the H/W. It will be in idle state after this returns */
hw_fini_rc = hdev->asic_funcs->hw_fini(hdev, hard_reset, fw_reset);
if (hard_reset) {
hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE;
/* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
hl_hw_queue_reset(hdev, hard_reset); for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
hl_cq_reset(hdev, &hdev->completion_queue[i]);
/* Make sure the context switch phase will run again */
ctx = hl_get_compute_ctx(hdev); if (ctx) {
atomic_set(&ctx->thread_ctx_switch_token, 1);
ctx->thread_ctx_switch_wait_token = 0;
hl_ctx_put(ctx);
}
if (hw_fini_rc) {
rc = hw_fini_rc; goto out_err;
} /* Finished tear-down, starting to re-initialize */
if (hard_reset) {
hdev->device_cpu_disabled = false;
hdev->reset_info.hard_reset_pending = false;
if (hdev->kernel_ctx) {
dev_crit(hdev->dev, "%s kernel ctx was alive during hard reset, something is terribly wrong\n",
dev_name(&(hdev)->pdev->dev));
rc = -EBUSY; goto out_err;
}
rc = hl_mmu_init(hdev); if (rc) {
dev_err(hdev->dev, "Failed to initialize MMU S/W after hard reset\n"); goto out_err;
}
/* Allocate the kernel context */
hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
GFP_KERNEL); if (!hdev->kernel_ctx) {
rc = -ENOMEM;
hl_mmu_fini(hdev); goto out_err;
}
hdev->is_compute_ctx_active = false;
rc = hl_ctx_init(hdev, hdev->kernel_ctx, true); if (rc) {
dev_err(hdev->dev, "failed to init kernel ctx in hard reset\n");
kfree(hdev->kernel_ctx);
hdev->kernel_ctx = NULL;
hl_mmu_fini(hdev); goto out_err;
}
}
/* Device is now enabled as part of the initialization requires *communicationwiththedevicefirmwaretogetinformationthat *isrequiredfortheinitializationitself
*/
hdev->disabled = false;
/* F/W security enabled indication might be updated after hard-reset */ if (hard_reset) {
rc = hl_fw_read_preboot_status(hdev); if (rc) goto out_err;
}
rc = hdev->asic_funcs->hw_init(hdev); if (rc) {
dev_err(hdev->dev, "failed to initialize the H/W after reset\n"); goto out_err;
}
/* If device is not idle fail the reset process */ if (!hdev->asic_funcs->is_device_idle(hdev, idle_mask,
HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL)) {
print_idle_status_mask(hdev, "device is not idle after reset", idle_mask);
rc = -EIO; goto out_err;
}
/* Check that the communication with the device is working */
rc = hdev->asic_funcs->test_queues(hdev); if (rc) {
dev_err(hdev->dev, "Failed to detect if device is alive after reset\n"); goto out_err;
}
if (hard_reset) {
rc = device_late_init(hdev); if (rc) {
dev_err(hdev->dev, "Failed late init after hard reset\n"); goto out_err;
}
rc = hl_vm_init(hdev); if (rc) {
dev_err(hdev->dev, "Failed to init memory module after hard reset\n"); goto out_err;
}
if (!hdev->asic_prop.fw_security_enabled)
hl_fw_set_max_power(hdev);
} else {
rc = hdev->asic_funcs->compute_reset_late_init(hdev); if (rc) { if (reset_upon_device_release)
dev_err(hdev->dev, "Failed late init in reset after device release\n"); else
dev_err(hdev->dev, "Failed late init after compute reset\n"); goto out_err;
}
}
rc = hdev->asic_funcs->scrub_device_mem(hdev); if (rc) {
dev_err(hdev->dev, "scrub mem failed from device reset (%d)\n", rc); goto out_err;
}
/* Schedule hard reset only if requested and if not already in hard reset. *Wekeep'in_reset'enabled,sonootherresetcangoinduringthehard *resetschedule
*/ if (!hard_reset && hdev->reset_info.hard_reset_schedule_flags)
schedule_hard_reset = true; else
hdev->reset_info.in_reset = 0;
spin_unlock(&hdev->reset_info.lock);
hdev->reset_info.needs_reset = false;
if (hard_reset)
dev_info(hdev->dev, "Successfully finished resetting the %s device\n",
dev_name(&(hdev)->pdev->dev)); else
dev_dbg(hdev->dev, "Successfully finished resetting the %s device\n",
dev_name(&(hdev)->pdev->dev));
if (hard_reset) {
hdev->reset_info.hard_reset_cnt++;
device_heartbeat_schedule(hdev);
/* After reset is done, we are ready to receive events from *theF/W.Wecan'tdoitbeforebecausewewillignoreevents *andifthoseeventsarefatal,wewon'tknowaboutitand *thedevicewillbeoperationalalthoughitshouldn'tbe
*/
hdev->asic_funcs->enable_events_from_fw(hdev);
} else { if (!reset_upon_device_release)
hdev->reset_info.compute_reset_cnt++;
if (schedule_hard_reset) {
dev_info(hdev->dev, "Performing hard reset scheduled during compute reset\n");
flags = hdev->reset_info.hard_reset_schedule_flags;
hdev->reset_info.hard_reset_schedule_flags = 0;
hard_reset = true; goto escalate_reset_flow;
}
}
if (hard_reset) {
dev_err(hdev->dev, "%s Failed to reset! Device is NOT usable\n",
dev_name(&(hdev)->pdev->dev));
hdev->reset_info.hard_reset_cnt++;
} else { if (reset_upon_device_release) {
dev_err(hdev->dev, "Failed to reset device after user release\n");
flags &= ~HL_DRV_RESET_DEV_RELEASE;
} else {
dev_err(hdev->dev, "Failed to do compute reset\n");
hdev->reset_info.compute_reset_cnt++;
}
/* F/W reset cannot be postponed */ if (flags & HL_DRV_RESET_BYPASS_REQ_TO_FW) goto device_reset;
/* Device release watchdog is relevant only if user exists and gets a reset notification */ if (!(event_mask & HL_NOTIFIER_EVENT_DEVICE_RESET)) {
dev_err(hdev->dev, "Resetting device without a reset indication to user\n"); goto device_reset;
}
ctx = hl_get_compute_ctx(hdev); if (!ctx) goto device_reset;
/* Schedule the device release watchdog work unless reset is already in progress or if the *workisalreadyscheduled.
*/
spin_lock(&hdev->reset_info.lock); if (hdev->reset_info.in_reset) {
spin_unlock(&hdev->reset_info.lock); goto device_reset;
}
if (hdev->reset_info.watchdog_active) {
hdev->device_release_watchdog_work.flags |= flags; goto out;
}
hdev->device_release_watchdog_work.flags = flags;
dev_dbg(hdev->dev, "Device is going to be hard-reset in %u sec unless being released\n",
hdev->device_release_watchdog_timeout_sec);
schedule_delayed_work(&hdev->device_release_watchdog_work.reset_work,
secs_to_jiffies(hdev->device_release_watchdog_timeout_sec));
hdev->reset_info.watchdog_active = 1;
out:
spin_unlock(&hdev->reset_info.lock);
hl_notifier_event_send_all(hdev, event_mask);
hl_ctx_put(ctx);
hl_abort_waiting_for_completions(hdev);
return0;
device_reset: if (event_mask)
hl_notifier_event_send_all(hdev, event_mask); if (ctx)
hl_ctx_put(ctx);
/* MMU S/W must be initialized before kernel context is created */
rc = hl_mmu_init(hdev); if (rc) {
dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n"); goto eq_fini;
}
/* Allocate the kernel context */
hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL); if (!hdev->kernel_ctx) {
rc = -ENOMEM; goto mmu_fini;
}
rc = hl_debugfs_device_init(hdev); if (rc) {
dev_err(hdev->dev, "failed to initialize debugfs entry structure\n");
kfree(hdev->kernel_ctx); goto mmu_fini;
}
/* The debugfs entry structure is accessed in hl_ctx_init(), so it must be called after *hl_debugfs_device_init().
*/
rc = hl_ctx_init(hdev, hdev->kernel_ctx, true); if (rc) {
dev_err(hdev->dev, "failed to initialize kernel context\n");
kfree(hdev->kernel_ctx); goto debugfs_device_fini;
}
rc = hl_cb_pool_init(hdev); if (rc) {
dev_err(hdev->dev, "failed to initialize CB pool\n"); goto release_ctx;
}
rc = hl_dec_init(hdev); if (rc) {
dev_err(hdev->dev, "Failed to initialize the decoder module\n"); goto cb_pool_fini;
}
/* Device is now enabled as part of the initialization requires *communicationwiththedevicefirmwaretogetinformationthat *isrequiredfortheinitializationitself
*/
hdev->disabled = false;
rc = hdev->asic_funcs->hw_init(hdev); if (rc) {
dev_err(hdev->dev, "failed to initialize the H/W\n");
rc = 0; goto out_disabled;
}
/* Check that the communication with the device is working */
rc = hdev->asic_funcs->test_queues(hdev); if (rc) {
dev_err(hdev->dev, "Failed to detect if device is alive\n");
rc = 0; goto out_disabled;
}
rc = device_late_init(hdev); if (rc) {
dev_err(hdev->dev, "Failed late initialization\n");
rc = 0; goto out_disabled;
}
rc = cdev_sysfs_debugfs_add(hdev); if (rc) {
dev_err(hdev->dev, "Failed to add char devices and sysfs/debugfs files\n");
rc = 0; goto out_disabled;
}
/* Need to call this again because the max power might change, *dependingoncardtypeforcertainASICs
*/ if (hdev->asic_prop.set_max_power_on_device_init &&
!hdev->asic_prop.fw_security_enabled)
hl_fw_set_max_power(hdev);
/* Scheduling the EQ heartbeat thread must come after driver is done with all *initializations,aswewanttomakesuretheFWgetsenoughtimetobeprepared *torespondtoheartbeatpackets.
*/
device_heartbeat_schedule(hdev);
dev_notice(hdev->dev, "Successfully added device %s to habanalabs driver\n",
dev_name(&(hdev)->pdev->dev));
/* After initialization is done, we are ready to receive events from *theF/W.Wecan'tdoitbeforebecausewewillignoreeventsandif *thoseeventsarefatal,wewon'tknowaboutitandthedevicewill *beoperationalalthoughitshouldn'tbe
*/
hdev->asic_funcs->enable_events_from_fw(hdev);
hdev->init_done = true;
return0;
cb_pool_fini:
hl_cb_pool_fini(hdev);
release_ctx: if (hl_ctx_put(hdev->kernel_ctx) != 1)
dev_err(hdev->dev, "kernel ctx is still alive on initialization failure\n");
debugfs_device_fini:
hl_debugfs_device_fini(hdev);
mmu_fini:
hl_mmu_fini(hdev);
eq_fini:
hl_eq_fini(hdev, &hdev->event_queue);
free_shadow_cs_queue:
kfree(hdev->shadow_cs_queue);
cq_fini: for (i = 0 ; i < cq_ready_cnt ; i++)
hl_cq_fini(hdev, &hdev->completion_queue[i]);
kfree(hdev->completion_queue);
hw_queues_destroy:
hl_hw_queues_destroy(hdev);
sw_fini:
hdev->asic_funcs->sw_fini(hdev);
free_common_usr_intr_mem:
vfree(hdev->common_user_cq_interrupt.ts_free_jobs_data.free_nodes_pool);
free_usr_intr_mem: if (user_interrupt_cnt) { for (i = 0 ; i < user_interrupt_cnt ; i++) { if (!hdev->user_interrupt[i].ts_free_jobs_data.free_nodes_pool) break;
vfree(hdev->user_interrupt[i].ts_free_jobs_data.free_nodes_pool);
}
kfree(hdev->user_interrupt);
}
early_fini:
device_early_fini(hdev);
out_disabled:
hdev->disabled = true; if (expose_interfaces_on_err) {
drm_dev_register(&hdev->drm, 0);
cdev_sysfs_debugfs_add(hdev);
}
pr_err("Failed to initialize accel%d. Device %s is NOT usable!\n",
hdev->cdev_idx, dev_name(&hdev->pdev->dev));
spin_lock(&hdev->reset_info.lock);
device_in_reset = !!hdev->reset_info.in_reset; if (!device_in_reset)
hdev->reset_info.in_reset = 1;
spin_unlock(&hdev->reset_info.lock);
while (device_in_reset) {
usleep_range(50, 200);
spin_lock(&hdev->reset_info.lock);
device_in_reset = !!hdev->reset_info.in_reset; if (!device_in_reset)
hdev->reset_info.in_reset = 1;
spin_unlock(&hdev->reset_info.lock);
if (ktime_compare(ktime_get(), timeout) > 0) {
dev_crit(hdev->dev, "%s Failed to remove device because reset function did not finish\n",
dev_name(&(hdev)->pdev->dev)); return;
}
}
/* Disable PCI access from device F/W so it won't send us additional *interrupts.WedisableMSI/MSI-Xatthehalt_enginesfunctionandwe *can'thavetheF/Wsendingusinterruptsafterthat.Weneedto *disabletheaccessherebecauseifthedeviceismarkeddisable,the *messagewon'tbesend.Also,incaseofheartbeat,thedeviceCPUis *markedasdisablesothismessagewon'tbesent
*/
hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0);
/* Mark device as disabled */
hdev->disabled = true;
take_release_locks(hdev);
hdev->reset_info.hard_reset_pending = true;
hl_hwmon_fini(hdev);
cleanup_resources(hdev, true, false, false);
/* Kill processes here after CS rollback. This is because the process *can'treallyexituntilallitsCSsaredone,whichiswhatwe *doincsrollback
*/
dev_info(hdev->dev, "Waiting for all processes to exit (timeout of %u seconds)",
HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI);
hdev->process_kill_trial_cnt = 0;
rc = device_kill_open_processes(hdev, HL_WAIT_PROCESS_KILL_ON_DEVICE_FINI, false); if (rc) {
dev_crit(hdev->dev, "Failed to kill all open processes (%d)\n", rc);
device_disable_open_processes(hdev, false);
}
hdev->process_kill_trial_cnt = 0;
rc = device_kill_open_processes(hdev, 0, true); if (rc) {
dev_crit(hdev->dev, "Failed to kill all control device open processes (%d)\n", rc);
device_disable_open_processes(hdev, true);
}
hl_cb_pool_fini(hdev);
/* Reset the H/W. It will be in idle state after this returns */
rc = hdev->asic_funcs->hw_fini(hdev, true, false); if (rc)
dev_err(hdev->dev, "hw_fini failed in device fini while removing device %d\n", rc);
hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE;
/* Release kernel context */ if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
dev_err(hdev->dev, "kernel ctx is still alive\n");
if (user_interrupt_cnt) { if (hdev->asic_prop.first_available_cq[0] != USHRT_MAX) { for (i = 0 ; i < user_interrupt_cnt ; i++)
vfree(hdev->user_interrupt[i].ts_free_jobs_data.free_nodes_pool);
}
if (num_of_engines > HL_RAZWI_MAX_NUM_OF_ENGINES_PER_RTR) {
dev_err(hdev->dev, "Number of possible razwi initiators (%u) exceeded limit (%u)\n",
num_of_engines, HL_RAZWI_MAX_NUM_OF_ENGINES_PER_RTR); return;
}
/* In case it's the first razwi since the device was opened, capture its parameters */ if (atomic_cmpxchg(&hdev->captured_err_info.razwi_info.razwi_detected, 0, 1)) return;
/* In case we already allocated in previous session, need to release it before *allocatingnewbuffer.
*/
vfree(pgf_info->user_mappings);
pgf_info->user_mappings =
vzalloc(pgf_info->num_of_user_mappings * sizeof(struct hl_user_mapping)); if (!pgf_info->user_mappings) {
pgf_info->num_of_user_mappings = 0; goto finish;
}
if (!cpumask_and(&cpu_mask, cpumask_of_node(numa_node), cpu_online_mask)) {
dev_err(hdev->dev, "No available affinities in current numa node\n"); return;
}
switch (event_type) { case EQ_EVENT_POWER_EVT_START:
clk_throttle->current_reason |= HL_CLK_THROTTLE_POWER;
clk_throttle->aggregated_reason |= HL_CLK_THROTTLE_POWER;
clk_throttle->timestamp[HL_CLK_THROTTLE_TYPE_POWER].start = ktime_get();
clk_throttle->timestamp[HL_CLK_THROTTLE_TYPE_POWER].end = zero_time;
dev_dbg_ratelimited(hdev->dev, "Clock throttling due to power consumption\n"); break;
case EQ_EVENT_POWER_EVT_END:
clk_throttle->current_reason &= ~HL_CLK_THROTTLE_POWER;
clk_throttle->timestamp[HL_CLK_THROTTLE_TYPE_POWER].end = ktime_get();
dev_dbg_ratelimited(hdev->dev, "Power envelop is safe, back to optimal clock\n"); break;
case EQ_EVENT_THERMAL_EVT_START:
clk_throttle->current_reason |= HL_CLK_THROTTLE_THERMAL;
clk_throttle->aggregated_reason |= HL_CLK_THROTTLE_THERMAL;
clk_throttle->timestamp[HL_CLK_THROTTLE_TYPE_THERMAL].start = ktime_get();
clk_throttle->timestamp[HL_CLK_THROTTLE_TYPE_THERMAL].end = zero_time;
*event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
dev_info_ratelimited(hdev->dev, "Clock throttling due to overheating\n"); break;
case EQ_EVENT_THERMAL_EVT_END:
clk_throttle->current_reason &= ~HL_CLK_THROTTLE_THERMAL;
clk_throttle->timestamp[HL_CLK_THROTTLE_TYPE_THERMAL].end = ktime_get();
*event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
dev_info_ratelimited(hdev->dev, "Thermal envelop is safe, back to optimal clock\n"); break;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.