1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #define pr_fmt(fmt) "habanalabs: " fmt
10 #include "habanalabs.h"
12 #include <linux/pci.h>
13 #include <linux/hwmon.h>
14 #include <uapi/misc/habanalabs.h>
16 enum hl_device_status hl_device_status(struct hl_device *hdev)
18 enum hl_device_status status;
20 if (atomic_read(&hdev->in_reset))
21 status = HL_DEVICE_STATUS_IN_RESET;
22 else if (hdev->needs_reset)
23 status = HL_DEVICE_STATUS_NEEDS_RESET;
24 else if (hdev->disabled)
25 status = HL_DEVICE_STATUS_MALFUNCTION;
27 status = HL_DEVICE_STATUS_OPERATIONAL;
32 bool hl_device_operational(struct hl_device *hdev,
33 enum hl_device_status *status)
35 enum hl_device_status current_status;
37 current_status = hl_device_status(hdev);
39 *status = current_status;
41 switch (current_status) {
42 case HL_DEVICE_STATUS_IN_RESET:
43 case HL_DEVICE_STATUS_MALFUNCTION:
44 case HL_DEVICE_STATUS_NEEDS_RESET:
46 case HL_DEVICE_STATUS_OPERATIONAL:
52 static void hpriv_release(struct kref *ref)
54 u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0};
55 bool device_is_idle = true;
56 struct hl_fpriv *hpriv;
57 struct hl_device *hdev;
59 hpriv = container_of(ref, struct hl_fpriv, refcount);
63 put_pid(hpriv->taskpid);
65 hl_debugfs_remove_file(hpriv);
67 mutex_destroy(&hpriv->restore_phase_mutex);
69 mutex_lock(&hdev->fpriv_list_lock);
70 list_del(&hpriv->dev_node);
71 hdev->compute_ctx = NULL;
72 mutex_unlock(&hdev->fpriv_list_lock);
76 if ((!hdev->pldm) && (hdev->pdev) &&
77 (!hdev->asic_funcs->is_device_idle(hdev,
79 HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL))) {
81 "device not idle after user context is closed (0x%llx_%llx)\n",
82 idle_mask[1], idle_mask[0]);
84 device_is_idle = false;
87 if ((hdev->reset_if_device_not_idle && !device_is_idle)
88 || hdev->reset_upon_device_release)
89 hl_device_reset(hdev, HL_RESET_DEVICE_RELEASE);
92 void hl_hpriv_get(struct hl_fpriv *hpriv)
94 kref_get(&hpriv->refcount);
97 int hl_hpriv_put(struct hl_fpriv *hpriv)
99 return kref_put(&hpriv->refcount, hpriv_release);
103 * hl_device_release - release function for habanalabs device
105 * @inode: pointer to inode structure
106 * @filp: pointer to file structure
108 * Called when process closes an habanalabs device
110 static int hl_device_release(struct inode *inode, struct file *filp)
112 struct hl_fpriv *hpriv = filp->private_data;
113 struct hl_device *hdev = hpriv->hdev;
115 filp->private_data = NULL;
118 pr_crit("Closing FD after device was removed. Memory leak will occur and it is advised to reboot.\n");
119 put_pid(hpriv->taskpid);
123 /* Each pending user interrupt holds the user's context, hence we
124 * must release them all before calling hl_ctx_mgr_fini().
126 hl_release_pending_user_interrupts(hpriv->hdev);
128 hl_cb_mgr_fini(hdev, &hpriv->cb_mgr);
129 hl_ctx_mgr_fini(hdev, &hpriv->ctx_mgr);
131 if (!hl_hpriv_put(hpriv))
133 "Device is still in use because there are live CS and/or memory mappings\n");
135 hdev->last_open_session_duration_jif =
136 jiffies - hdev->last_successful_open_jif;
141 static int hl_device_release_ctrl(struct inode *inode, struct file *filp)
143 struct hl_fpriv *hpriv = filp->private_data;
144 struct hl_device *hdev = hpriv->hdev;
146 filp->private_data = NULL;
149 pr_err("Closing FD after device was removed\n");
153 mutex_lock(&hdev->fpriv_list_lock);
154 list_del(&hpriv->dev_node);
155 mutex_unlock(&hdev->fpriv_list_lock);
157 put_pid(hpriv->taskpid);
165 * hl_mmap - mmap function for habanalabs device
167 * @*filp: pointer to file structure
168 * @*vma: pointer to vm_area_struct of the process
170 * Called when process does an mmap on habanalabs device. Call the device's mmap
171 * function at the end of the common code.
173 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
175 struct hl_fpriv *hpriv = filp->private_data;
176 struct hl_device *hdev = hpriv->hdev;
177 unsigned long vm_pgoff;
180 pr_err_ratelimited("Trying to mmap after device was removed! Please close FD\n");
184 vm_pgoff = vma->vm_pgoff;
185 vma->vm_pgoff = HL_MMAP_OFFSET_VALUE_GET(vm_pgoff);
187 switch (vm_pgoff & HL_MMAP_TYPE_MASK) {
188 case HL_MMAP_TYPE_CB:
189 return hl_cb_mmap(hpriv, vma);
191 case HL_MMAP_TYPE_BLOCK:
192 return hl_hw_block_mmap(hpriv, vma);
198 static const struct file_operations hl_ops = {
199 .owner = THIS_MODULE,
200 .open = hl_device_open,
201 .release = hl_device_release,
203 .unlocked_ioctl = hl_ioctl,
204 .compat_ioctl = hl_ioctl
207 static const struct file_operations hl_ctrl_ops = {
208 .owner = THIS_MODULE,
209 .open = hl_device_open_ctrl,
210 .release = hl_device_release_ctrl,
211 .unlocked_ioctl = hl_ioctl_control,
212 .compat_ioctl = hl_ioctl_control
215 static void device_release_func(struct device *dev)
221 * device_init_cdev - Initialize cdev and device for habanalabs device
223 * @hdev: pointer to habanalabs device structure
224 * @hclass: pointer to the class object of the device
225 * @minor: minor number of the specific device
226 * @fpos: file operations to install for this device
227 * @name: name of the device as it will appear in the filesystem
228 * @cdev: pointer to the char device object that will be initialized
229 * @dev: pointer to the device object that will be initialized
231 * Initialize a cdev and a Linux device for habanalabs's device.
233 static int device_init_cdev(struct hl_device *hdev, struct class *hclass,
234 int minor, const struct file_operations *fops,
235 char *name, struct cdev *cdev,
238 cdev_init(cdev, fops);
239 cdev->owner = THIS_MODULE;
241 *dev = kzalloc(sizeof(**dev), GFP_KERNEL);
245 device_initialize(*dev);
246 (*dev)->devt = MKDEV(hdev->major, minor);
247 (*dev)->class = hclass;
248 (*dev)->release = device_release_func;
249 dev_set_drvdata(*dev, hdev);
250 dev_set_name(*dev, "%s", name);
255 static int device_cdev_sysfs_add(struct hl_device *hdev)
259 rc = cdev_device_add(&hdev->cdev, hdev->dev);
262 "failed to add a char device to the system\n");
266 rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl);
269 "failed to add a control char device to the system\n");
270 goto delete_cdev_device;
273 /* hl_sysfs_init() must be done after adding the device to the system */
274 rc = hl_sysfs_init(hdev);
276 dev_err(hdev->dev, "failed to initialize sysfs\n");
277 goto delete_ctrl_cdev_device;
280 hdev->cdev_sysfs_created = true;
284 delete_ctrl_cdev_device:
285 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
287 cdev_device_del(&hdev->cdev, hdev->dev);
291 static void device_cdev_sysfs_del(struct hl_device *hdev)
293 if (!hdev->cdev_sysfs_created)
297 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
298 cdev_device_del(&hdev->cdev, hdev->dev);
301 put_device(hdev->dev);
302 put_device(hdev->dev_ctrl);
305 static void device_hard_reset_pending(struct work_struct *work)
307 struct hl_device_reset_work *device_reset_work =
308 container_of(work, struct hl_device_reset_work,
310 struct hl_device *hdev = device_reset_work->hdev;
313 rc = hl_device_reset(hdev, HL_RESET_HARD | HL_RESET_FROM_RESET_THREAD);
314 if ((rc == -EBUSY) && !hdev->device_fini_pending) {
316 "Could not reset device. will try again in %u seconds",
317 HL_PENDING_RESET_PER_SEC);
319 queue_delayed_work(device_reset_work->wq,
320 &device_reset_work->reset_work,
321 msecs_to_jiffies(HL_PENDING_RESET_PER_SEC * 1000));
326 * device_early_init - do some early initialization for the habanalabs device
328 * @hdev: pointer to habanalabs device structure
330 * Install the relevant function pointers and call the early_init function,
331 * if such a function exists
333 static int device_early_init(struct hl_device *hdev)
338 switch (hdev->asic_type) {
340 goya_set_asic_funcs(hdev);
341 strscpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
344 gaudi_set_asic_funcs(hdev);
345 strscpy(hdev->asic_name, "GAUDI", sizeof(hdev->asic_name));
348 gaudi_set_asic_funcs(hdev);
349 strscpy(hdev->asic_name, "GAUDI SEC", sizeof(hdev->asic_name));
352 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
357 rc = hdev->asic_funcs->early_init(hdev);
361 rc = hl_asid_init(hdev);
365 if (hdev->asic_prop.completion_queues_count) {
366 hdev->cq_wq = kcalloc(hdev->asic_prop.completion_queues_count,
367 sizeof(*hdev->cq_wq),
375 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) {
376 snprintf(workq_name, 32, "hl-free-jobs-%u", (u32) i);
377 hdev->cq_wq[i] = create_singlethread_workqueue(workq_name);
378 if (hdev->cq_wq[i] == NULL) {
379 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
385 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
386 if (hdev->eq_wq == NULL) {
387 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
392 hdev->sob_reset_wq = alloc_workqueue("hl-sob-reset", WQ_UNBOUND, 0);
393 if (!hdev->sob_reset_wq) {
395 "Failed to allocate SOB reset workqueue\n");
400 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
402 if (!hdev->hl_chip_info) {
404 goto free_sob_reset_wq;
407 rc = hl_mmu_if_set_funcs(hdev);
411 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
413 hdev->device_reset_work.wq =
414 create_singlethread_workqueue("hl_device_reset");
415 if (!hdev->device_reset_work.wq) {
417 dev_err(hdev->dev, "Failed to create device reset WQ\n");
421 INIT_DELAYED_WORK(&hdev->device_reset_work.reset_work,
422 device_hard_reset_pending);
423 hdev->device_reset_work.hdev = hdev;
424 hdev->device_fini_pending = 0;
426 mutex_init(&hdev->send_cpu_message_lock);
427 mutex_init(&hdev->debug_lock);
428 INIT_LIST_HEAD(&hdev->cs_mirror_list);
429 spin_lock_init(&hdev->cs_mirror_lock);
430 INIT_LIST_HEAD(&hdev->fpriv_list);
431 mutex_init(&hdev->fpriv_list_lock);
432 atomic_set(&hdev->in_reset, 0);
437 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
439 kfree(hdev->hl_chip_info);
441 destroy_workqueue(hdev->sob_reset_wq);
443 destroy_workqueue(hdev->eq_wq);
445 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
447 destroy_workqueue(hdev->cq_wq[i]);
452 if (hdev->asic_funcs->early_fini)
453 hdev->asic_funcs->early_fini(hdev);
459 * device_early_fini - finalize all that was done in device_early_init
461 * @hdev: pointer to habanalabs device structure
464 static void device_early_fini(struct hl_device *hdev)
468 mutex_destroy(&hdev->debug_lock);
469 mutex_destroy(&hdev->send_cpu_message_lock);
471 mutex_destroy(&hdev->fpriv_list_lock);
473 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
475 kfree(hdev->hl_chip_info);
477 destroy_workqueue(hdev->sob_reset_wq);
478 destroy_workqueue(hdev->eq_wq);
479 destroy_workqueue(hdev->device_reset_work.wq);
481 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
482 destroy_workqueue(hdev->cq_wq[i]);
487 if (hdev->asic_funcs->early_fini)
488 hdev->asic_funcs->early_fini(hdev);
491 static void set_freq_to_low_job(struct work_struct *work)
493 struct hl_device *hdev = container_of(work, struct hl_device,
496 mutex_lock(&hdev->fpriv_list_lock);
498 if (!hdev->compute_ctx)
499 hl_device_set_frequency(hdev, PLL_LOW);
501 mutex_unlock(&hdev->fpriv_list_lock);
503 schedule_delayed_work(&hdev->work_freq,
504 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
507 static void hl_device_heartbeat(struct work_struct *work)
509 struct hl_device *hdev = container_of(work, struct hl_device,
510 work_heartbeat.work);
512 if (!hl_device_operational(hdev, NULL))
515 if (!hdev->asic_funcs->send_heartbeat(hdev))
518 dev_err(hdev->dev, "Device heartbeat failed!\n");
519 hl_device_reset(hdev, HL_RESET_HARD | HL_RESET_HEARTBEAT);
524 schedule_delayed_work(&hdev->work_heartbeat,
525 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
529 * device_late_init - do late stuff initialization for the habanalabs device
531 * @hdev: pointer to habanalabs device structure
533 * Do stuff that either needs the device H/W queues to be active or needs
534 * to happen after all the rest of the initialization is finished
536 static int device_late_init(struct hl_device *hdev)
540 if (hdev->asic_funcs->late_init) {
541 rc = hdev->asic_funcs->late_init(hdev);
544 "failed late initialization for the H/W\n");
549 hdev->high_pll = hdev->asic_prop.high_pll;
551 /* force setting to low frequency */
552 hdev->curr_pll_profile = PLL_LOW;
554 if (hdev->pm_mng_profile == PM_AUTO)
555 hdev->asic_funcs->set_pll_profile(hdev, PLL_LOW);
557 hdev->asic_funcs->set_pll_profile(hdev, PLL_LAST);
559 INIT_DELAYED_WORK(&hdev->work_freq, set_freq_to_low_job);
560 schedule_delayed_work(&hdev->work_freq,
561 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
563 if (hdev->heartbeat) {
564 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
565 schedule_delayed_work(&hdev->work_heartbeat,
566 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
569 hdev->late_init_done = true;
575 * device_late_fini - finalize all that was done in device_late_init
577 * @hdev: pointer to habanalabs device structure
580 static void device_late_fini(struct hl_device *hdev)
582 if (!hdev->late_init_done)
585 cancel_delayed_work_sync(&hdev->work_freq);
587 cancel_delayed_work_sync(&hdev->work_heartbeat);
589 if (hdev->asic_funcs->late_fini)
590 hdev->asic_funcs->late_fini(hdev);
592 hdev->late_init_done = false;
595 int hl_device_utilization(struct hl_device *hdev, u32 *utilization)
597 u64 max_power, curr_power, dc_power, dividend;
600 max_power = hdev->asic_prop.max_power_default;
601 dc_power = hdev->asic_prop.dc_power_default;
602 rc = hl_fw_cpucp_power_get(hdev, &curr_power);
607 curr_power = clamp(curr_power, dc_power, max_power);
609 dividend = (curr_power - dc_power) * 100;
610 *utilization = (u32) div_u64(dividend, (max_power - dc_power));
616 * hl_device_set_frequency - set the frequency of the device
618 * @hdev: pointer to habanalabs device structure
619 * @freq: the new frequency value
621 * Change the frequency if needed. This function has no protection against
622 * concurrency, therefore it is assumed that the calling function has protected
623 * itself against the case of calling this function from multiple threads with
626 * Returns 0 if no change was done, otherwise returns 1
628 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq)
630 if ((hdev->pm_mng_profile == PM_MANUAL) ||
631 (hdev->curr_pll_profile == freq))
634 dev_dbg(hdev->dev, "Changing device frequency to %s\n",
635 freq == PLL_HIGH ? "high" : "low");
637 hdev->asic_funcs->set_pll_profile(hdev, freq);
639 hdev->curr_pll_profile = freq;
644 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable)
648 mutex_lock(&hdev->debug_lock);
651 if (!hdev->in_debug) {
653 "Failed to disable debug mode because device was not in debug mode\n");
658 if (!hdev->hard_reset_pending)
659 hdev->asic_funcs->halt_coresight(hdev);
663 if (!hdev->hard_reset_pending)
664 hdev->asic_funcs->set_clock_gating(hdev);
669 if (hdev->in_debug) {
671 "Failed to enable debug mode because device is already in debug mode\n");
676 hdev->asic_funcs->disable_clock_gating(hdev);
680 mutex_unlock(&hdev->debug_lock);
686 * hl_device_suspend - initiate device suspend
688 * @hdev: pointer to habanalabs device structure
690 * Puts the hw in the suspend state (all asics).
691 * Returns 0 for success or an error on failure.
692 * Called at driver suspend.
694 int hl_device_suspend(struct hl_device *hdev)
698 pci_save_state(hdev->pdev);
700 /* Block future CS/VM/JOB completion operations */
701 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
703 dev_err(hdev->dev, "Can't suspend while in reset\n");
707 /* This blocks all other stuff that is not blocked by in_reset */
708 hdev->disabled = true;
711 * Flush anyone that is inside the critical section of enqueue
714 hdev->asic_funcs->hw_queues_lock(hdev);
715 hdev->asic_funcs->hw_queues_unlock(hdev);
717 /* Flush processes that are sending message to CPU */
718 mutex_lock(&hdev->send_cpu_message_lock);
719 mutex_unlock(&hdev->send_cpu_message_lock);
721 rc = hdev->asic_funcs->suspend(hdev);
724 "Failed to disable PCI access of device CPU\n");
726 /* Shut down the device */
727 pci_disable_device(hdev->pdev);
728 pci_set_power_state(hdev->pdev, PCI_D3hot);
734 * hl_device_resume - initiate device resume
736 * @hdev: pointer to habanalabs device structure
738 * Bring the hw back to operating state (all asics).
739 * Returns 0 for success or an error on failure.
740 * Called at driver resume.
742 int hl_device_resume(struct hl_device *hdev)
746 pci_set_power_state(hdev->pdev, PCI_D0);
747 pci_restore_state(hdev->pdev);
748 rc = pci_enable_device_mem(hdev->pdev);
751 "Failed to enable PCI device in resume\n");
755 pci_set_master(hdev->pdev);
757 rc = hdev->asic_funcs->resume(hdev);
759 dev_err(hdev->dev, "Failed to resume device after suspend\n");
764 hdev->disabled = false;
765 atomic_set(&hdev->in_reset, 0);
767 rc = hl_device_reset(hdev, HL_RESET_HARD);
769 dev_err(hdev->dev, "Failed to reset device during resume\n");
776 pci_clear_master(hdev->pdev);
777 pci_disable_device(hdev->pdev);
782 static int device_kill_open_processes(struct hl_device *hdev, u32 timeout)
784 struct hl_fpriv *hpriv;
785 struct task_struct *task = NULL;
789 /* Giving time for user to close FD, and for processes that are inside
790 * hl_device_open to finish
792 if (!list_empty(&hdev->fpriv_list))
796 pending_cnt = timeout;
798 if (hdev->process_kill_trial_cnt) {
799 /* Processes have been already killed */
801 goto wait_for_processes;
803 /* Wait a small period after process kill */
804 pending_cnt = HL_PENDING_RESET_PER_SEC;
808 mutex_lock(&hdev->fpriv_list_lock);
810 /* This section must be protected because we are dereferencing
811 * pointers that are freed if the process exits
813 list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) {
814 task = get_pid_task(hpriv->taskpid, PIDTYPE_PID);
816 dev_info(hdev->dev, "Killing user process pid=%d\n",
818 send_sig(SIGKILL, task, 1);
819 usleep_range(1000, 10000);
821 put_task_struct(task);
825 mutex_unlock(&hdev->fpriv_list_lock);
828 * We killed the open users, but that doesn't mean they are closed.
829 * It could be that they are running a long cleanup phase in the driver
830 * e.g. MMU unmappings, or running other long teardown flow even before
832 * Therefore we need to wait again to make sure they are closed before
833 * continuing with the reset.
837 while ((!list_empty(&hdev->fpriv_list)) && (pending_cnt)) {
839 "Waiting for all unmap operations to finish before hard reset\n");
846 /* All processes exited successfully */
847 if (list_empty(&hdev->fpriv_list))
850 /* Give up waiting for processes to exit */
851 if (hdev->process_kill_trial_cnt == HL_PENDING_RESET_MAX_TRIALS)
854 hdev->process_kill_trial_cnt++;
859 static void device_disable_open_processes(struct hl_device *hdev)
861 struct hl_fpriv *hpriv;
863 mutex_lock(&hdev->fpriv_list_lock);
864 list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node)
866 mutex_unlock(&hdev->fpriv_list_lock);
870 * hl_device_reset - reset the device
872 * @hdev: pointer to habanalabs device structure
873 * @flags: reset flags.
875 * Block future CS and wait for pending CS to be enqueued
877 * Flush all completions
878 * Re-initialize all internal data structures
879 * Call ASIC H/W init, late_init
883 * Returns 0 for success or an error on failure.
885 int hl_device_reset(struct hl_device *hdev, u32 flags)
887 u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0};
888 bool hard_reset, from_hard_reset_thread, hard_instead_soft = false;
891 if (!hdev->init_done) {
893 "Can't reset before initialization is done\n");
897 hard_reset = (flags & HL_RESET_HARD) != 0;
898 from_hard_reset_thread = (flags & HL_RESET_FROM_RESET_THREAD) != 0;
900 if (!hard_reset && !hdev->supports_soft_reset) {
901 hard_instead_soft = true;
905 if (hdev->reset_upon_device_release &&
906 (flags & HL_RESET_DEVICE_RELEASE)) {
908 "Perform %s-reset upon device release\n",
909 hard_reset ? "hard" : "soft");
913 if (!hard_reset && !hdev->allow_external_soft_reset) {
914 hard_instead_soft = true;
918 if (hard_instead_soft)
919 dev_dbg(hdev->dev, "Doing hard-reset instead of soft-reset\n");
922 /* Re-entry of reset thread */
923 if (from_hard_reset_thread && hdev->process_kill_trial_cnt)
927 * Prevent concurrency in this function - only one reset should be
928 * done at any given time. Only need to perform this if we didn't
929 * get from the dedicated hard reset thread
931 if (!from_hard_reset_thread) {
932 /* Block future CS/VM/JOB completion operations */
933 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
938 * 'reset cause' is being updated here, because getting here
939 * means that it's the 1st time and the last time we're here
940 * ('in_reset' makes sure of it). This makes sure that
941 * 'reset_cause' will continue holding its 1st recorded reason!
943 if (flags & HL_RESET_HEARTBEAT)
944 hdev->curr_reset_cause = HL_RESET_CAUSE_HEARTBEAT;
945 else if (flags & HL_RESET_TDR)
946 hdev->curr_reset_cause = HL_RESET_CAUSE_TDR;
948 hdev->curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
951 * if reset is due to heartbeat, device CPU is no responsive in
952 * which case no point sending PCI disable message to it
954 if (hard_reset && !(flags & HL_RESET_HEARTBEAT)) {
955 /* Disable PCI access from device F/W so he won't send
956 * us additional interrupts. We disable MSI/MSI-X at
957 * the halt_engines function and we can't have the F/W
958 * sending us interrupts after that. We need to disable
959 * the access here because if the device is marked
960 * disable, the message won't be send. Also, in case
961 * of heartbeat, the device CPU is marked as disable
962 * so this message won't be sent
964 if (hl_fw_send_pci_access_msg(hdev,
965 CPUCP_PACKET_DISABLE_PCI_ACCESS))
967 "Failed to disable PCI access by F/W\n");
970 /* This also blocks future CS/VM/JOB completion operations */
971 hdev->disabled = true;
973 /* Flush anyone that is inside the critical section of enqueue
976 hdev->asic_funcs->hw_queues_lock(hdev);
977 hdev->asic_funcs->hw_queues_unlock(hdev);
979 /* Flush anyone that is inside device open */
980 mutex_lock(&hdev->fpriv_list_lock);
981 mutex_unlock(&hdev->fpriv_list_lock);
983 dev_err(hdev->dev, "Going to RESET device!\n");
987 if ((hard_reset) && (!from_hard_reset_thread)) {
988 hdev->hard_reset_pending = true;
990 hdev->process_kill_trial_cnt = 0;
993 * Because the reset function can't run from heartbeat work,
994 * we need to call the reset function from a dedicated work.
996 queue_delayed_work(hdev->device_reset_work.wq,
997 &hdev->device_reset_work.reset_work, 0);
1003 device_late_fini(hdev);
1006 * Now that the heartbeat thread is closed, flush processes
1007 * which are sending messages to CPU
1009 mutex_lock(&hdev->send_cpu_message_lock);
1010 mutex_unlock(&hdev->send_cpu_message_lock);
1014 * Halt the engines and disable interrupts so we won't get any more
1015 * completions from H/W and we won't have any accesses from the
1016 * H/W to the host machine
1018 hdev->asic_funcs->halt_engines(hdev, hard_reset);
1020 /* Go over all the queues, release all CS and their jobs */
1021 hl_cs_rollback_all(hdev);
1023 /* Release all pending user interrupts, each pending user interrupt
1024 * holds a reference to user context
1026 hl_release_pending_user_interrupts(hdev);
1030 /* Kill processes here after CS rollback. This is because the
1031 * process can't really exit until all its CSs are done, which
1032 * is what we do in cs rollback
1034 rc = device_kill_open_processes(hdev, 0);
1037 if (hdev->device_fini_pending) {
1039 "Failed to kill all open processes, stopping hard reset\n");
1043 /* signal reset thread to reschedule */
1049 "Failed to kill all open processes, stopping hard reset\n");
1053 /* Flush the Event queue workers to make sure no other thread is
1054 * reading or writing to registers during the reset
1056 flush_workqueue(hdev->eq_wq);
1059 /* Reset the H/W. It will be in idle state after this returns */
1060 hdev->asic_funcs->hw_fini(hdev, hard_reset);
1063 /* Release kernel context */
1064 if (hdev->kernel_ctx && hl_ctx_put(hdev->kernel_ctx) == 1)
1065 hdev->kernel_ctx = NULL;
1068 hl_eq_reset(hdev, &hdev->event_queue);
1071 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
1072 hl_hw_queue_reset(hdev, hard_reset);
1073 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1074 hl_cq_reset(hdev, &hdev->completion_queue[i]);
1076 mutex_lock(&hdev->fpriv_list_lock);
1078 /* Make sure the context switch phase will run again */
1079 if (hdev->compute_ctx) {
1080 atomic_set(&hdev->compute_ctx->thread_ctx_switch_token, 1);
1081 hdev->compute_ctx->thread_ctx_switch_wait_token = 0;
1084 mutex_unlock(&hdev->fpriv_list_lock);
1086 /* Finished tear-down, starting to re-initialize */
1089 hdev->device_cpu_disabled = false;
1090 hdev->hard_reset_pending = false;
1092 if (hdev->kernel_ctx) {
1094 "kernel ctx was alive during hard reset, something is terribly wrong\n");
1099 rc = hl_mmu_init(hdev);
1102 "Failed to initialize MMU S/W after hard reset\n");
1106 /* Allocate the kernel context */
1107 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
1109 if (!hdev->kernel_ctx) {
1115 hdev->compute_ctx = NULL;
1117 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1120 "failed to init kernel ctx in hard reset\n");
1121 kfree(hdev->kernel_ctx);
1122 hdev->kernel_ctx = NULL;
1128 /* Device is now enabled as part of the initialization requires
1129 * communication with the device firmware to get information that
1130 * is required for the initialization itself
1132 hdev->disabled = false;
1134 rc = hdev->asic_funcs->hw_init(hdev);
1137 "failed to initialize the H/W after reset\n");
1141 /* If device is not idle fail the reset process */
1142 if (!hdev->asic_funcs->is_device_idle(hdev, idle_mask,
1143 HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL)) {
1145 "device is not idle (mask 0x%llx_%llx) after reset\n",
1146 idle_mask[1], idle_mask[0]);
1151 /* Check that the communication with the device is working */
1152 rc = hdev->asic_funcs->test_queues(hdev);
1155 "Failed to detect if device is alive after reset\n");
1160 rc = device_late_init(hdev);
1163 "Failed late init after hard reset\n");
1167 rc = hl_vm_init(hdev);
1170 "Failed to init memory module after hard reset\n");
1174 hl_set_max_power(hdev);
1176 rc = hdev->asic_funcs->soft_reset_late_init(hdev);
1179 "Failed late init after soft reset\n");
1184 atomic_set(&hdev->in_reset, 0);
1185 hdev->needs_reset = false;
1187 dev_notice(hdev->dev, "Successfully finished resetting the device\n");
1190 hdev->hard_reset_cnt++;
1192 /* After reset is done, we are ready to receive events from
1193 * the F/W. We can't do it before because we will ignore events
1194 * and if those events are fatal, we won't know about it and
1195 * the device will be operational although it shouldn't be
1197 hdev->asic_funcs->enable_events_from_fw(hdev);
1199 hdev->soft_reset_cnt++;
1205 hdev->disabled = true;
1209 "Failed to reset! Device is NOT usable\n");
1210 hdev->hard_reset_cnt++;
1213 "Failed to do soft-reset, trying hard reset\n");
1214 hdev->soft_reset_cnt++;
1219 atomic_set(&hdev->in_reset, 0);
1225 * hl_device_init - main initialization function for habanalabs device
1227 * @hdev: pointer to habanalabs device structure
1229 * Allocate an id for the device, do early initialization and then call the
1230 * ASIC specific initialization functions. Finally, create the cdev and the
1231 * Linux device to expose it to the user
1233 int hl_device_init(struct hl_device *hdev, struct class *hclass)
1235 int i, rc, cq_cnt, user_interrupt_cnt, cq_ready_cnt;
1237 bool add_cdev_sysfs_on_err = false;
1239 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id / 2);
1245 /* Initialize cdev and device structures */
1246 rc = device_init_cdev(hdev, hclass, hdev->id, &hl_ops, name,
1247 &hdev->cdev, &hdev->dev);
1254 name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->id / 2);
1260 /* Initialize cdev and device structures for control device */
1261 rc = device_init_cdev(hdev, hclass, hdev->id_control, &hl_ctrl_ops,
1262 name, &hdev->cdev_ctrl, &hdev->dev_ctrl);
1269 /* Initialize ASIC function pointers and perform early init */
1270 rc = device_early_init(hdev);
1274 user_interrupt_cnt = hdev->asic_prop.user_interrupt_count;
1276 if (user_interrupt_cnt) {
1277 hdev->user_interrupt = kcalloc(user_interrupt_cnt,
1278 sizeof(*hdev->user_interrupt),
1281 if (!hdev->user_interrupt) {
1288 * Start calling ASIC initialization. First S/W then H/W and finally
1291 rc = hdev->asic_funcs->sw_init(hdev);
1293 goto user_interrupts_fini;
1296 * Initialize the H/W queues. Must be done before hw_init, because
1297 * there the addresses of the kernel queue are being written to the
1298 * registers of the device
1300 rc = hl_hw_queues_create(hdev);
1302 dev_err(hdev->dev, "failed to initialize kernel queues\n");
1306 cq_cnt = hdev->asic_prop.completion_queues_count;
1309 * Initialize the completion queues. Must be done before hw_init,
1310 * because there the addresses of the completion queues are being
1311 * passed as arguments to request_irq
1314 hdev->completion_queue = kcalloc(cq_cnt,
1315 sizeof(*hdev->completion_queue),
1318 if (!hdev->completion_queue) {
1320 "failed to allocate completion queues\n");
1322 goto hw_queues_destroy;
1326 for (i = 0, cq_ready_cnt = 0 ; i < cq_cnt ; i++, cq_ready_cnt++) {
1327 rc = hl_cq_init(hdev, &hdev->completion_queue[i],
1328 hdev->asic_funcs->get_queue_id_for_cq(hdev, i));
1331 "failed to initialize completion queue\n");
1334 hdev->completion_queue[i].cq_idx = i;
1338 * Initialize the event queue. Must be done before hw_init,
1339 * because there the address of the event queue is being
1340 * passed as argument to request_irq
1342 rc = hl_eq_init(hdev, &hdev->event_queue);
1344 dev_err(hdev->dev, "failed to initialize event queue\n");
1348 /* MMU S/W must be initialized before kernel context is created */
1349 rc = hl_mmu_init(hdev);
1351 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n");
1355 /* Allocate the kernel context */
1356 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
1357 if (!hdev->kernel_ctx) {
1362 hdev->compute_ctx = NULL;
1364 hl_debugfs_add_device(hdev);
1366 /* debugfs nodes are created in hl_ctx_init so it must be called after
1367 * hl_debugfs_add_device.
1369 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1371 dev_err(hdev->dev, "failed to initialize kernel context\n");
1372 kfree(hdev->kernel_ctx);
1373 goto remove_device_from_debugfs;
1376 rc = hl_cb_pool_init(hdev);
1378 dev_err(hdev->dev, "failed to initialize CB pool\n");
1383 * From this point, override rc (=0) in case of an error to allow
1384 * debugging (by adding char devices and create sysfs nodes as part of
1387 add_cdev_sysfs_on_err = true;
1389 /* Device is now enabled as part of the initialization requires
1390 * communication with the device firmware to get information that
1391 * is required for the initialization itself
1393 hdev->disabled = false;
1395 rc = hdev->asic_funcs->hw_init(hdev);
1397 dev_err(hdev->dev, "failed to initialize the H/W\n");
1402 /* Check that the communication with the device is working */
1403 rc = hdev->asic_funcs->test_queues(hdev);
1405 dev_err(hdev->dev, "Failed to detect if device is alive\n");
1410 rc = device_late_init(hdev);
1412 dev_err(hdev->dev, "Failed late initialization\n");
1417 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
1419 hdev->asic_prop.dram_size / SZ_1G);
1421 rc = hl_vm_init(hdev);
1423 dev_err(hdev->dev, "Failed to initialize memory module\n");
1429 * Expose devices and sysfs nodes to user.
1430 * From here there is no need to add char devices and create sysfs nodes
1431 * in case of an error.
1433 add_cdev_sysfs_on_err = false;
1434 rc = device_cdev_sysfs_add(hdev);
1437 "Failed to add char devices and sysfs nodes\n");
1442 /* Need to call this again because the max power might change,
1443 * depending on card type for certain ASICs
1445 hl_set_max_power(hdev);
1448 * hl_hwmon_init() must be called after device_late_init(), because only
1449 * there we get the information from the device about which
1450 * hwmon-related sensors the device supports.
1451 * Furthermore, it must be done after adding the device to the system.
1453 rc = hl_hwmon_init(hdev);
1455 dev_err(hdev->dev, "Failed to initialize hwmon\n");
1460 dev_notice(hdev->dev,
1461 "Successfully added device to habanalabs driver\n");
1463 hdev->init_done = true;
1465 /* After initialization is done, we are ready to receive events from
1466 * the F/W. We can't do it before because we will ignore events and if
1467 * those events are fatal, we won't know about it and the device will
1468 * be operational although it shouldn't be
1470 hdev->asic_funcs->enable_events_from_fw(hdev);
1475 if (hl_ctx_put(hdev->kernel_ctx) != 1)
1477 "kernel ctx is still alive on initialization failure\n");
1478 remove_device_from_debugfs:
1479 hl_debugfs_remove_device(hdev);
1483 hl_eq_fini(hdev, &hdev->event_queue);
1485 for (i = 0 ; i < cq_ready_cnt ; i++)
1486 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1487 kfree(hdev->completion_queue);
1489 hl_hw_queues_destroy(hdev);
1491 hdev->asic_funcs->sw_fini(hdev);
1492 user_interrupts_fini:
1493 kfree(hdev->user_interrupt);
1495 device_early_fini(hdev);
1497 put_device(hdev->dev_ctrl);
1499 put_device(hdev->dev);
1501 hdev->disabled = true;
1502 if (add_cdev_sysfs_on_err)
1503 device_cdev_sysfs_add(hdev);
1505 dev_err(&hdev->pdev->dev,
1506 "Failed to initialize hl%d. Device is NOT usable !\n",
1509 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
1516 * hl_device_fini - main tear-down function for habanalabs device
1518 * @hdev: pointer to habanalabs device structure
1520 * Destroy the device, call ASIC fini functions and release the id
1522 void hl_device_fini(struct hl_device *hdev)
1528 dev_info(hdev->dev, "Removing device\n");
1530 hdev->device_fini_pending = 1;
1531 flush_delayed_work(&hdev->device_reset_work.reset_work);
1534 reset_sec = HL_PLDM_HARD_RESET_MAX_TIMEOUT;
1536 reset_sec = HL_HARD_RESET_MAX_TIMEOUT;
1539 * This function is competing with the reset function, so try to
1540 * take the reset atomic and if we are already in middle of reset,
1541 * wait until reset function is finished. Reset function is designed
1542 * to always finish. However, in Gaudi, because of all the network
1543 * ports, the hard reset could take between 10-30 seconds
1546 timeout = ktime_add_us(ktime_get(), reset_sec * 1000 * 1000);
1547 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1549 usleep_range(50, 200);
1550 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1551 if (ktime_compare(ktime_get(), timeout) > 0) {
1553 "Failed to remove device because reset function did not finish\n");
1558 /* Disable PCI access from device F/W so it won't send us additional
1559 * interrupts. We disable MSI/MSI-X at the halt_engines function and we
1560 * can't have the F/W sending us interrupts after that. We need to
1561 * disable the access here because if the device is marked disable, the
1562 * message won't be send. Also, in case of heartbeat, the device CPU is
1563 * marked as disable so this message won't be sent
1565 hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS);
1567 /* Mark device as disabled */
1568 hdev->disabled = true;
1570 /* Flush anyone that is inside the critical section of enqueue
1573 hdev->asic_funcs->hw_queues_lock(hdev);
1574 hdev->asic_funcs->hw_queues_unlock(hdev);
1576 /* Flush anyone that is inside device open */
1577 mutex_lock(&hdev->fpriv_list_lock);
1578 mutex_unlock(&hdev->fpriv_list_lock);
1580 hdev->hard_reset_pending = true;
1582 hl_hwmon_fini(hdev);
1584 device_late_fini(hdev);
1587 * Halt the engines and disable interrupts so we won't get any more
1588 * completions from H/W and we won't have any accesses from the
1589 * H/W to the host machine
1591 hdev->asic_funcs->halt_engines(hdev, true);
1593 /* Go over all the queues, release all CS and their jobs */
1594 hl_cs_rollback_all(hdev);
1596 /* Kill processes here after CS rollback. This is because the process
1597 * can't really exit until all its CSs are done, which is what we
1601 "Waiting for all processes to exit (timeout of %u seconds)",
1602 HL_PENDING_RESET_LONG_SEC);
1604 rc = device_kill_open_processes(hdev, HL_PENDING_RESET_LONG_SEC);
1606 dev_crit(hdev->dev, "Failed to kill all open processes\n");
1607 device_disable_open_processes(hdev);
1610 hl_cb_pool_fini(hdev);
1612 /* Reset the H/W. It will be in idle state after this returns */
1613 hdev->asic_funcs->hw_fini(hdev, true);
1615 /* Release kernel context */
1616 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
1617 dev_err(hdev->dev, "kernel ctx is still alive\n");
1619 hl_debugfs_remove_device(hdev);
1625 hl_eq_fini(hdev, &hdev->event_queue);
1627 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1628 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1629 kfree(hdev->completion_queue);
1630 kfree(hdev->user_interrupt);
1632 hl_hw_queues_destroy(hdev);
1634 /* Call ASIC S/W finalize function */
1635 hdev->asic_funcs->sw_fini(hdev);
1637 device_early_fini(hdev);
1639 /* Hide devices and sysfs nodes from user */
1640 device_cdev_sysfs_del(hdev);
1642 pr_info("removed device successfully\n");
1646 * MMIO register access helper functions.
1650 * hl_rreg - Read an MMIO register
1652 * @hdev: pointer to habanalabs device structure
1653 * @reg: MMIO register offset (in bytes)
1655 * Returns the value of the MMIO register we are asked to read
1658 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1660 return readl(hdev->rmmio + reg);
1664 * hl_wreg - Write to an MMIO register
1666 * @hdev: pointer to habanalabs device structure
1667 * @reg: MMIO register offset (in bytes)
1668 * @val: 32-bit value
1670 * Writes the 32-bit value into the MMIO register
1673 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1675 writel(val, hdev->rmmio + reg);