1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2022 HabanaLabs, Ltd.
8 #define pr_fmt(fmt) "habanalabs: " fmt
10 #include <uapi/misc/habanalabs.h>
11 #include "habanalabs.h"
13 #include <linux/pci.h>
14 #include <linux/hwmon.h>
16 #define HL_RESET_DELAY_USEC 10000 /* 10ms */
18 enum hl_device_status hl_device_status(struct hl_device *hdev)
20 enum hl_device_status status;
22 if (hdev->reset_info.in_reset)
23 status = HL_DEVICE_STATUS_IN_RESET;
24 else if (hdev->reset_info.needs_reset)
25 status = HL_DEVICE_STATUS_NEEDS_RESET;
26 else if (hdev->disabled)
27 status = HL_DEVICE_STATUS_MALFUNCTION;
28 else if (!hdev->init_done)
29 status = HL_DEVICE_STATUS_IN_DEVICE_CREATION;
31 status = HL_DEVICE_STATUS_OPERATIONAL;
36 bool hl_device_operational(struct hl_device *hdev,
37 enum hl_device_status *status)
39 enum hl_device_status current_status;
41 current_status = hl_device_status(hdev);
43 *status = current_status;
45 switch (current_status) {
46 case HL_DEVICE_STATUS_IN_RESET:
47 case HL_DEVICE_STATUS_MALFUNCTION:
48 case HL_DEVICE_STATUS_NEEDS_RESET:
50 case HL_DEVICE_STATUS_OPERATIONAL:
51 case HL_DEVICE_STATUS_IN_DEVICE_CREATION:
57 static void hpriv_release(struct kref *ref)
59 u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0};
60 bool device_is_idle = true;
61 struct hl_fpriv *hpriv;
62 struct hl_device *hdev;
64 hpriv = container_of(ref, struct hl_fpriv, refcount);
68 put_pid(hpriv->taskpid);
70 hl_debugfs_remove_file(hpriv);
72 mutex_destroy(&hpriv->restore_phase_mutex);
74 if ((!hdev->pldm) && (hdev->pdev) &&
75 (!hdev->asic_funcs->is_device_idle(hdev,
77 HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL))) {
79 "device not idle after user context is closed (0x%llx_%llx)\n",
80 idle_mask[1], idle_mask[0]);
82 device_is_idle = false;
85 /* We need to remove the user from the list to make sure the reset process won't
86 * try to kill the user process. Because, if we got here, it means there are no
87 * more driver/device resources that the user process is occupying so there is
90 * However, we can't set the compute_ctx to NULL at this stage. This is to prevent
91 * a race between the release and opening the device again. We don't want to let
92 * a user open the device while there a reset is about to happen.
94 mutex_lock(&hdev->fpriv_list_lock);
95 list_del(&hpriv->dev_node);
96 mutex_unlock(&hdev->fpriv_list_lock);
98 if ((hdev->reset_if_device_not_idle && !device_is_idle)
99 || hdev->reset_upon_device_release)
100 hl_device_reset(hdev, HL_DRV_RESET_DEV_RELEASE);
102 /* Now we can mark the compute_ctx as not active. Even if a reset is running in a different
103 * thread, we don't care because the in_reset is marked so if a user will try to open
104 * the device it will fail on that, even if compute_ctx is false.
106 mutex_lock(&hdev->fpriv_list_lock);
107 hdev->is_compute_ctx_active = false;
108 mutex_unlock(&hdev->fpriv_list_lock);
113 void hl_hpriv_get(struct hl_fpriv *hpriv)
115 kref_get(&hpriv->refcount);
118 int hl_hpriv_put(struct hl_fpriv *hpriv)
120 return kref_put(&hpriv->refcount, hpriv_release);
124 * hl_device_release - release function for habanalabs device
126 * @inode: pointer to inode structure
127 * @filp: pointer to file structure
129 * Called when process closes an habanalabs device
131 static int hl_device_release(struct inode *inode, struct file *filp)
133 struct hl_fpriv *hpriv = filp->private_data;
134 struct hl_device *hdev = hpriv->hdev;
136 filp->private_data = NULL;
139 pr_crit("Closing FD after device was removed. Memory leak will occur and it is advised to reboot.\n");
140 put_pid(hpriv->taskpid);
144 /* Each pending user interrupt holds the user's context, hence we
145 * must release them all before calling hl_ctx_mgr_fini().
147 hl_release_pending_user_interrupts(hpriv->hdev);
149 hl_cb_mgr_fini(hdev, &hpriv->cb_mgr);
150 hl_ts_mgr_fini(hpriv->hdev, &hpriv->ts_mem_mgr);
151 hl_ctx_mgr_fini(hdev, &hpriv->ctx_mgr);
153 if (!hl_hpriv_put(hpriv))
154 dev_notice(hdev->dev,
155 "User process closed FD but device still in use\n");
157 hdev->last_open_session_duration_jif =
158 jiffies - hdev->last_successful_open_jif;
163 static int hl_device_release_ctrl(struct inode *inode, struct file *filp)
165 struct hl_fpriv *hpriv = filp->private_data;
166 struct hl_device *hdev = hpriv->hdev;
168 filp->private_data = NULL;
171 pr_err("Closing FD after device was removed\n");
175 mutex_lock(&hdev->fpriv_ctrl_list_lock);
176 list_del(&hpriv->dev_node);
177 mutex_unlock(&hdev->fpriv_ctrl_list_lock);
179 put_pid(hpriv->taskpid);
187 * hl_mmap - mmap function for habanalabs device
189 * @*filp: pointer to file structure
190 * @*vma: pointer to vm_area_struct of the process
192 * Called when process does an mmap on habanalabs device. Call the device's mmap
193 * function at the end of the common code.
195 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
197 struct hl_fpriv *hpriv = filp->private_data;
198 struct hl_device *hdev = hpriv->hdev;
199 unsigned long vm_pgoff;
202 pr_err_ratelimited("Trying to mmap after device was removed! Please close FD\n");
206 vm_pgoff = vma->vm_pgoff;
207 vma->vm_pgoff = HL_MMAP_OFFSET_VALUE_GET(vm_pgoff);
209 switch (vm_pgoff & HL_MMAP_TYPE_MASK) {
210 case HL_MMAP_TYPE_CB:
211 return hl_cb_mmap(hpriv, vma);
213 case HL_MMAP_TYPE_BLOCK:
214 return hl_hw_block_mmap(hpriv, vma);
216 case HL_MMAP_TYPE_TS_BUFF:
217 return hl_ts_mmap(hpriv, vma);
223 static const struct file_operations hl_ops = {
224 .owner = THIS_MODULE,
225 .open = hl_device_open,
226 .release = hl_device_release,
228 .unlocked_ioctl = hl_ioctl,
229 .compat_ioctl = hl_ioctl
232 static const struct file_operations hl_ctrl_ops = {
233 .owner = THIS_MODULE,
234 .open = hl_device_open_ctrl,
235 .release = hl_device_release_ctrl,
236 .unlocked_ioctl = hl_ioctl_control,
237 .compat_ioctl = hl_ioctl_control
240 static void device_release_func(struct device *dev)
246 * device_init_cdev - Initialize cdev and device for habanalabs device
248 * @hdev: pointer to habanalabs device structure
249 * @hclass: pointer to the class object of the device
250 * @minor: minor number of the specific device
251 * @fpos: file operations to install for this device
252 * @name: name of the device as it will appear in the filesystem
253 * @cdev: pointer to the char device object that will be initialized
254 * @dev: pointer to the device object that will be initialized
256 * Initialize a cdev and a Linux device for habanalabs's device.
258 static int device_init_cdev(struct hl_device *hdev, struct class *hclass,
259 int minor, const struct file_operations *fops,
260 char *name, struct cdev *cdev,
263 cdev_init(cdev, fops);
264 cdev->owner = THIS_MODULE;
266 *dev = kzalloc(sizeof(**dev), GFP_KERNEL);
270 device_initialize(*dev);
271 (*dev)->devt = MKDEV(hdev->major, minor);
272 (*dev)->class = hclass;
273 (*dev)->release = device_release_func;
274 dev_set_drvdata(*dev, hdev);
275 dev_set_name(*dev, "%s", name);
280 static int device_cdev_sysfs_add(struct hl_device *hdev)
284 rc = cdev_device_add(&hdev->cdev, hdev->dev);
287 "failed to add a char device to the system\n");
291 rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl);
294 "failed to add a control char device to the system\n");
295 goto delete_cdev_device;
298 /* hl_sysfs_init() must be done after adding the device to the system */
299 rc = hl_sysfs_init(hdev);
301 dev_err(hdev->dev, "failed to initialize sysfs\n");
302 goto delete_ctrl_cdev_device;
305 hdev->cdev_sysfs_created = true;
309 delete_ctrl_cdev_device:
310 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
312 cdev_device_del(&hdev->cdev, hdev->dev);
316 static void device_cdev_sysfs_del(struct hl_device *hdev)
318 if (!hdev->cdev_sysfs_created)
322 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
323 cdev_device_del(&hdev->cdev, hdev->dev);
326 put_device(hdev->dev);
327 put_device(hdev->dev_ctrl);
330 static void device_hard_reset_pending(struct work_struct *work)
332 struct hl_device_reset_work *device_reset_work =
333 container_of(work, struct hl_device_reset_work, reset_work.work);
334 struct hl_device *hdev = device_reset_work->hdev;
338 flags = device_reset_work->flags | HL_DRV_RESET_FROM_RESET_THR;
340 rc = hl_device_reset(hdev, flags);
341 if ((rc == -EBUSY) && !hdev->device_fini_pending) {
343 "Could not reset device. will try again in %u seconds",
344 HL_PENDING_RESET_PER_SEC);
346 queue_delayed_work(device_reset_work->wq,
347 &device_reset_work->reset_work,
348 msecs_to_jiffies(HL_PENDING_RESET_PER_SEC * 1000));
353 * device_early_init - do some early initialization for the habanalabs device
355 * @hdev: pointer to habanalabs device structure
357 * Install the relevant function pointers and call the early_init function,
358 * if such a function exists
360 static int device_early_init(struct hl_device *hdev)
365 switch (hdev->asic_type) {
367 goya_set_asic_funcs(hdev);
368 strscpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
371 gaudi_set_asic_funcs(hdev);
372 strscpy(hdev->asic_name, "GAUDI", sizeof(hdev->asic_name));
375 gaudi_set_asic_funcs(hdev);
376 strscpy(hdev->asic_name, "GAUDI SEC", sizeof(hdev->asic_name));
379 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
384 rc = hdev->asic_funcs->early_init(hdev);
388 rc = hl_asid_init(hdev);
392 if (hdev->asic_prop.completion_queues_count) {
393 hdev->cq_wq = kcalloc(hdev->asic_prop.completion_queues_count,
394 sizeof(*hdev->cq_wq),
402 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) {
403 snprintf(workq_name, 32, "hl-free-jobs-%u", (u32) i);
404 hdev->cq_wq[i] = create_singlethread_workqueue(workq_name);
405 if (hdev->cq_wq[i] == NULL) {
406 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
412 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
413 if (hdev->eq_wq == NULL) {
414 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
419 hdev->ts_free_obj_wq = alloc_workqueue("hl-ts-free-obj", WQ_UNBOUND, 0);
420 if (!hdev->ts_free_obj_wq) {
422 "Failed to allocate Timestamp registration free workqueue\n");
427 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
429 if (!hdev->hl_chip_info) {
431 goto free_ts_free_wq;
434 rc = hl_mmu_if_set_funcs(hdev);
438 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
440 hdev->device_reset_work.wq =
441 create_singlethread_workqueue("hl_device_reset");
442 if (!hdev->device_reset_work.wq) {
444 dev_err(hdev->dev, "Failed to create device reset WQ\n");
448 INIT_DELAYED_WORK(&hdev->device_reset_work.reset_work,
449 device_hard_reset_pending);
450 hdev->device_reset_work.hdev = hdev;
451 hdev->device_fini_pending = 0;
453 mutex_init(&hdev->send_cpu_message_lock);
454 mutex_init(&hdev->debug_lock);
455 INIT_LIST_HEAD(&hdev->cs_mirror_list);
456 spin_lock_init(&hdev->cs_mirror_lock);
457 spin_lock_init(&hdev->reset_info.lock);
458 INIT_LIST_HEAD(&hdev->fpriv_list);
459 INIT_LIST_HEAD(&hdev->fpriv_ctrl_list);
460 mutex_init(&hdev->fpriv_list_lock);
461 mutex_init(&hdev->fpriv_ctrl_list_lock);
462 mutex_init(&hdev->clk_throttling.lock);
467 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
469 kfree(hdev->hl_chip_info);
471 destroy_workqueue(hdev->ts_free_obj_wq);
473 destroy_workqueue(hdev->eq_wq);
475 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
477 destroy_workqueue(hdev->cq_wq[i]);
482 if (hdev->asic_funcs->early_fini)
483 hdev->asic_funcs->early_fini(hdev);
489 * device_early_fini - finalize all that was done in device_early_init
491 * @hdev: pointer to habanalabs device structure
494 static void device_early_fini(struct hl_device *hdev)
498 mutex_destroy(&hdev->debug_lock);
499 mutex_destroy(&hdev->send_cpu_message_lock);
501 mutex_destroy(&hdev->fpriv_list_lock);
502 mutex_destroy(&hdev->fpriv_ctrl_list_lock);
504 mutex_destroy(&hdev->clk_throttling.lock);
506 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
508 kfree(hdev->hl_chip_info);
510 destroy_workqueue(hdev->ts_free_obj_wq);
511 destroy_workqueue(hdev->eq_wq);
512 destroy_workqueue(hdev->device_reset_work.wq);
514 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
515 destroy_workqueue(hdev->cq_wq[i]);
520 if (hdev->asic_funcs->early_fini)
521 hdev->asic_funcs->early_fini(hdev);
524 static void hl_device_heartbeat(struct work_struct *work)
526 struct hl_device *hdev = container_of(work, struct hl_device,
527 work_heartbeat.work);
529 if (!hl_device_operational(hdev, NULL))
532 if (!hdev->asic_funcs->send_heartbeat(hdev))
535 if (hl_device_operational(hdev, NULL))
536 dev_err(hdev->dev, "Device heartbeat failed!\n");
538 hl_device_reset(hdev, HL_DRV_RESET_HARD | HL_DRV_RESET_HEARTBEAT);
544 * prev_reset_trigger tracks consecutive fatal h/w errors until first
545 * heartbeat immediately post reset.
546 * If control reached here, then at least one heartbeat work has been
547 * scheduled since last reset/init cycle.
548 * So if the device is not already in reset cycle, reset the flag
549 * prev_reset_trigger as no reset occurred with HL_DRV_RESET_FW_FATAL_ERR
550 * status for at least one heartbeat. From this point driver restarts
551 * tracking future consecutive fatal errors.
553 if (!hdev->reset_info.in_reset)
554 hdev->reset_info.prev_reset_trigger = HL_RESET_TRIGGER_DEFAULT;
556 schedule_delayed_work(&hdev->work_heartbeat,
557 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
561 * device_late_init - do late stuff initialization for the habanalabs device
563 * @hdev: pointer to habanalabs device structure
565 * Do stuff that either needs the device H/W queues to be active or needs
566 * to happen after all the rest of the initialization is finished
568 static int device_late_init(struct hl_device *hdev)
572 if (hdev->asic_funcs->late_init) {
573 rc = hdev->asic_funcs->late_init(hdev);
576 "failed late initialization for the H/W\n");
581 hdev->high_pll = hdev->asic_prop.high_pll;
583 if (hdev->heartbeat) {
584 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
585 schedule_delayed_work(&hdev->work_heartbeat,
586 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
589 hdev->late_init_done = true;
595 * device_late_fini - finalize all that was done in device_late_init
597 * @hdev: pointer to habanalabs device structure
600 static void device_late_fini(struct hl_device *hdev)
602 if (!hdev->late_init_done)
606 cancel_delayed_work_sync(&hdev->work_heartbeat);
608 if (hdev->asic_funcs->late_fini)
609 hdev->asic_funcs->late_fini(hdev);
611 hdev->late_init_done = false;
614 int hl_device_utilization(struct hl_device *hdev, u32 *utilization)
616 u64 max_power, curr_power, dc_power, dividend;
619 max_power = hdev->max_power;
620 dc_power = hdev->asic_prop.dc_power_default;
621 rc = hl_fw_cpucp_power_get(hdev, &curr_power);
626 curr_power = clamp(curr_power, dc_power, max_power);
628 dividend = (curr_power - dc_power) * 100;
629 *utilization = (u32) div_u64(dividend, (max_power - dc_power));
634 int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable)
638 mutex_lock(&hdev->debug_lock);
641 if (!hdev->in_debug) {
643 "Failed to disable debug mode because device was not in debug mode\n");
648 if (!hdev->reset_info.hard_reset_pending)
649 hdev->asic_funcs->halt_coresight(hdev, ctx);
656 if (hdev->in_debug) {
658 "Failed to enable debug mode because device is already in debug mode\n");
666 mutex_unlock(&hdev->debug_lock);
671 static void take_release_locks(struct hl_device *hdev)
673 /* Flush anyone that is inside the critical section of enqueue
676 hdev->asic_funcs->hw_queues_lock(hdev);
677 hdev->asic_funcs->hw_queues_unlock(hdev);
679 /* Flush processes that are sending message to CPU */
680 mutex_lock(&hdev->send_cpu_message_lock);
681 mutex_unlock(&hdev->send_cpu_message_lock);
683 /* Flush anyone that is inside device open */
684 mutex_lock(&hdev->fpriv_list_lock);
685 mutex_unlock(&hdev->fpriv_list_lock);
686 mutex_lock(&hdev->fpriv_ctrl_list_lock);
687 mutex_unlock(&hdev->fpriv_ctrl_list_lock);
690 static void cleanup_resources(struct hl_device *hdev, bool hard_reset, bool fw_reset,
694 device_late_fini(hdev);
697 * Halt the engines and disable interrupts so we won't get any more
698 * completions from H/W and we won't have any accesses from the
699 * H/W to the host machine
701 hdev->asic_funcs->halt_engines(hdev, hard_reset, fw_reset);
703 /* Go over all the queues, release all CS and their jobs */
704 hl_cs_rollback_all(hdev, skip_wq_flush);
706 /* Release all pending user interrupts, each pending user interrupt
707 * holds a reference to user context
709 hl_release_pending_user_interrupts(hdev);
713 * hl_device_suspend - initiate device suspend
715 * @hdev: pointer to habanalabs device structure
717 * Puts the hw in the suspend state (all asics).
718 * Returns 0 for success or an error on failure.
719 * Called at driver suspend.
721 int hl_device_suspend(struct hl_device *hdev)
725 pci_save_state(hdev->pdev);
727 /* Block future CS/VM/JOB completion operations */
728 spin_lock(&hdev->reset_info.lock);
729 if (hdev->reset_info.in_reset) {
730 spin_unlock(&hdev->reset_info.lock);
731 dev_err(hdev->dev, "Can't suspend while in reset\n");
734 hdev->reset_info.in_reset = 1;
735 spin_unlock(&hdev->reset_info.lock);
737 /* This blocks all other stuff that is not blocked by in_reset */
738 hdev->disabled = true;
740 take_release_locks(hdev);
742 rc = hdev->asic_funcs->suspend(hdev);
745 "Failed to disable PCI access of device CPU\n");
747 /* Shut down the device */
748 pci_disable_device(hdev->pdev);
749 pci_set_power_state(hdev->pdev, PCI_D3hot);
755 * hl_device_resume - initiate device resume
757 * @hdev: pointer to habanalabs device structure
759 * Bring the hw back to operating state (all asics).
760 * Returns 0 for success or an error on failure.
761 * Called at driver resume.
763 int hl_device_resume(struct hl_device *hdev)
767 pci_set_power_state(hdev->pdev, PCI_D0);
768 pci_restore_state(hdev->pdev);
769 rc = pci_enable_device_mem(hdev->pdev);
772 "Failed to enable PCI device in resume\n");
776 pci_set_master(hdev->pdev);
778 rc = hdev->asic_funcs->resume(hdev);
780 dev_err(hdev->dev, "Failed to resume device after suspend\n");
785 /* 'in_reset' was set to true during suspend, now we must clear it in order
786 * for hard reset to be performed
788 hdev->reset_info.in_reset = 0;
790 rc = hl_device_reset(hdev, HL_DRV_RESET_HARD);
792 dev_err(hdev->dev, "Failed to reset device during resume\n");
799 pci_clear_master(hdev->pdev);
800 pci_disable_device(hdev->pdev);
805 static int device_kill_open_processes(struct hl_device *hdev, u32 timeout, bool control_dev)
807 struct task_struct *task = NULL;
808 struct list_head *fd_list;
809 struct hl_fpriv *hpriv;
810 struct mutex *fd_lock;
813 fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock;
814 fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list;
816 /* Giving time for user to close FD, and for processes that are inside
817 * hl_device_open to finish
819 if (!list_empty(fd_list))
823 pending_cnt = timeout;
825 if (hdev->process_kill_trial_cnt) {
826 /* Processes have been already killed */
828 goto wait_for_processes;
830 /* Wait a small period after process kill */
831 pending_cnt = HL_PENDING_RESET_PER_SEC;
837 /* This section must be protected because we are dereferencing
838 * pointers that are freed if the process exits
840 list_for_each_entry(hpriv, fd_list, dev_node) {
841 task = get_pid_task(hpriv->taskpid, PIDTYPE_PID);
843 dev_info(hdev->dev, "Killing user process pid=%d\n",
845 send_sig(SIGKILL, task, 1);
846 usleep_range(1000, 10000);
848 put_task_struct(task);
851 "Can't get task struct for PID so giving up on killing process\n");
852 mutex_unlock(fd_lock);
857 mutex_unlock(fd_lock);
860 * We killed the open users, but that doesn't mean they are closed.
861 * It could be that they are running a long cleanup phase in the driver
862 * e.g. MMU unmappings, or running other long teardown flow even before
864 * Therefore we need to wait again to make sure they are closed before
865 * continuing with the reset.
869 while ((!list_empty(fd_list)) && (pending_cnt)) {
871 "Waiting for all unmap operations to finish before hard reset\n");
878 /* All processes exited successfully */
879 if (list_empty(fd_list))
882 /* Give up waiting for processes to exit */
883 if (hdev->process_kill_trial_cnt == HL_PENDING_RESET_MAX_TRIALS)
886 hdev->process_kill_trial_cnt++;
891 static void device_disable_open_processes(struct hl_device *hdev, bool control_dev)
893 struct list_head *fd_list;
894 struct hl_fpriv *hpriv;
895 struct mutex *fd_lock;
897 fd_lock = control_dev ? &hdev->fpriv_ctrl_list_lock : &hdev->fpriv_list_lock;
898 fd_list = control_dev ? &hdev->fpriv_ctrl_list : &hdev->fpriv_list;
901 list_for_each_entry(hpriv, fd_list, dev_node)
903 mutex_unlock(fd_lock);
906 static void handle_reset_trigger(struct hl_device *hdev, u32 flags)
908 u32 cur_reset_trigger = HL_RESET_TRIGGER_DEFAULT;
911 * 'reset cause' is being updated here, because getting here
912 * means that it's the 1st time and the last time we're here
913 * ('in_reset' makes sure of it). This makes sure that
914 * 'reset_cause' will continue holding its 1st recorded reason!
916 if (flags & HL_DRV_RESET_HEARTBEAT) {
917 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_HEARTBEAT;
918 cur_reset_trigger = HL_DRV_RESET_HEARTBEAT;
919 } else if (flags & HL_DRV_RESET_TDR) {
920 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_TDR;
921 cur_reset_trigger = HL_DRV_RESET_TDR;
922 } else if (flags & HL_DRV_RESET_FW_FATAL_ERR) {
923 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
924 cur_reset_trigger = HL_DRV_RESET_FW_FATAL_ERR;
926 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
930 * If reset cause is same twice, then reset_trigger_repeated
931 * is set and if this reset is due to a fatal FW error
932 * device is set to an unstable state.
934 if (hdev->reset_info.prev_reset_trigger != cur_reset_trigger) {
935 hdev->reset_info.prev_reset_trigger = cur_reset_trigger;
936 hdev->reset_info.reset_trigger_repeated = 0;
938 hdev->reset_info.reset_trigger_repeated = 1;
941 /* If reset is due to heartbeat, device CPU is no responsive in
942 * which case no point sending PCI disable message to it.
944 * If F/W is performing the reset, no need to send it a message to disable
947 if ((flags & HL_DRV_RESET_HARD) &&
948 !(flags & (HL_DRV_RESET_HEARTBEAT | HL_DRV_RESET_BYPASS_REQ_TO_FW))) {
949 /* Disable PCI access from device F/W so he won't send
950 * us additional interrupts. We disable MSI/MSI-X at
951 * the halt_engines function and we can't have the F/W
952 * sending us interrupts after that. We need to disable
953 * the access here because if the device is marked
954 * disable, the message won't be send. Also, in case
955 * of heartbeat, the device CPU is marked as disable
956 * so this message won't be sent
958 if (hl_fw_send_pci_access_msg(hdev,
959 CPUCP_PACKET_DISABLE_PCI_ACCESS))
961 "Failed to disable PCI access by F/W\n");
966 * hl_device_reset - reset the device
968 * @hdev: pointer to habanalabs device structure
969 * @flags: reset flags.
971 * Block future CS and wait for pending CS to be enqueued
973 * Flush all completions
974 * Re-initialize all internal data structures
975 * Call ASIC H/W init, late_init
979 * Returns 0 for success or an error on failure.
981 int hl_device_reset(struct hl_device *hdev, u32 flags)
983 bool hard_reset, from_hard_reset_thread, fw_reset, hard_instead_soft = false,
984 reset_upon_device_release = false, schedule_hard_reset = false,
985 skip_wq_flush, delay_reset;
986 u64 idle_mask[HL_BUSY_ENGINES_MASK_EXT_SIZE] = {0};
990 if (!hdev->init_done) {
991 dev_err(hdev->dev, "Can't reset before initialization is done\n");
995 hard_reset = !!(flags & HL_DRV_RESET_HARD);
996 from_hard_reset_thread = !!(flags & HL_DRV_RESET_FROM_RESET_THR);
997 fw_reset = !!(flags & HL_DRV_RESET_BYPASS_REQ_TO_FW);
998 skip_wq_flush = !!(flags & HL_DRV_RESET_DEV_RELEASE);
999 delay_reset = !!(flags & HL_DRV_RESET_DELAY);
1001 if (!hard_reset && !hdev->asic_prop.supports_soft_reset) {
1002 hard_instead_soft = true;
1006 if (hdev->reset_upon_device_release && (flags & HL_DRV_RESET_DEV_RELEASE)) {
1009 "Aborting reset because hard-reset is mutually exclusive with reset-on-device-release\n");
1013 reset_upon_device_release = true;
1018 if (!hard_reset && !hdev->asic_prop.allow_inference_soft_reset) {
1019 hard_instead_soft = true;
1023 if (hard_instead_soft)
1024 dev_dbg(hdev->dev, "Doing hard-reset instead of soft-reset\n");
1027 /* Re-entry of reset thread */
1028 if (from_hard_reset_thread && hdev->process_kill_trial_cnt)
1029 goto kill_processes;
1032 * Prevent concurrency in this function - only one reset should be
1033 * done at any given time. Only need to perform this if we didn't
1034 * get from the dedicated hard reset thread
1036 if (!from_hard_reset_thread) {
1037 /* Block future CS/VM/JOB completion operations */
1038 spin_lock(&hdev->reset_info.lock);
1039 if (hdev->reset_info.in_reset) {
1040 /* We only allow scheduling of a hard reset during soft reset */
1041 if (hard_reset && hdev->reset_info.is_in_soft_reset)
1042 hdev->reset_info.hard_reset_schedule_flags = flags;
1043 spin_unlock(&hdev->reset_info.lock);
1046 hdev->reset_info.in_reset = 1;
1047 spin_unlock(&hdev->reset_info.lock);
1050 usleep_range(HL_RESET_DELAY_USEC, HL_RESET_DELAY_USEC << 1);
1052 handle_reset_trigger(hdev, flags);
1054 /* This still allows the completion of some KDMA ops */
1055 hdev->reset_info.is_in_soft_reset = !hard_reset;
1057 /* This also blocks future CS/VM/JOB completion operations */
1058 hdev->disabled = true;
1060 take_release_locks(hdev);
1063 dev_info(hdev->dev, "Going to reset device\n");
1064 else if (reset_upon_device_release)
1065 dev_info(hdev->dev, "Going to reset device after release by user\n");
1067 dev_info(hdev->dev, "Going to reset engines of inference device\n");
1071 if ((hard_reset) && (!from_hard_reset_thread)) {
1072 hdev->reset_info.hard_reset_pending = true;
1074 hdev->process_kill_trial_cnt = 0;
1076 hdev->device_reset_work.flags = flags;
1079 * Because the reset function can't run from heartbeat work,
1080 * we need to call the reset function from a dedicated work.
1082 queue_delayed_work(hdev->device_reset_work.wq,
1083 &hdev->device_reset_work.reset_work, 0);
1088 cleanup_resources(hdev, hard_reset, fw_reset, skip_wq_flush);
1092 /* Kill processes here after CS rollback. This is because the
1093 * process can't really exit until all its CSs are done, which
1094 * is what we do in cs rollback
1096 rc = device_kill_open_processes(hdev, 0, false);
1099 if (hdev->device_fini_pending) {
1101 "Failed to kill all open processes, stopping hard reset\n");
1105 /* signal reset thread to reschedule */
1111 "Failed to kill all open processes, stopping hard reset\n");
1115 /* Flush the Event queue workers to make sure no other thread is
1116 * reading or writing to registers during the reset
1118 flush_workqueue(hdev->eq_wq);
1121 /* Reset the H/W. It will be in idle state after this returns */
1122 hdev->asic_funcs->hw_fini(hdev, hard_reset, fw_reset);
1125 hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE;
1127 /* Release kernel context */
1128 if (hdev->kernel_ctx && hl_ctx_put(hdev->kernel_ctx) == 1)
1129 hdev->kernel_ctx = NULL;
1133 hl_eq_reset(hdev, &hdev->event_queue);
1136 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
1137 hl_hw_queue_reset(hdev, hard_reset);
1138 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1139 hl_cq_reset(hdev, &hdev->completion_queue[i]);
1141 /* Make sure the context switch phase will run again */
1142 ctx = hl_get_compute_ctx(hdev);
1144 atomic_set(&ctx->thread_ctx_switch_token, 1);
1145 ctx->thread_ctx_switch_wait_token = 0;
1149 /* Finished tear-down, starting to re-initialize */
1152 hdev->device_cpu_disabled = false;
1153 hdev->reset_info.hard_reset_pending = false;
1155 if (hdev->reset_info.reset_trigger_repeated &&
1156 (hdev->reset_info.prev_reset_trigger ==
1157 HL_DRV_RESET_FW_FATAL_ERR)) {
1158 /* if there 2 back to back resets from FW,
1159 * ensure driver puts the driver in a unusable state
1162 "Consecutive FW fatal errors received, stopping hard reset\n");
1167 if (hdev->kernel_ctx) {
1169 "kernel ctx was alive during hard reset, something is terribly wrong\n");
1174 rc = hl_mmu_init(hdev);
1177 "Failed to initialize MMU S/W after hard reset\n");
1181 /* Allocate the kernel context */
1182 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
1184 if (!hdev->kernel_ctx) {
1190 hdev->is_compute_ctx_active = false;
1192 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1195 "failed to init kernel ctx in hard reset\n");
1196 kfree(hdev->kernel_ctx);
1197 hdev->kernel_ctx = NULL;
1203 /* Device is now enabled as part of the initialization requires
1204 * communication with the device firmware to get information that
1205 * is required for the initialization itself
1207 hdev->disabled = false;
1209 rc = hdev->asic_funcs->hw_init(hdev);
1211 dev_err(hdev->dev, "failed to initialize the H/W after reset\n");
1215 /* If device is not idle fail the reset process */
1216 if (!hdev->asic_funcs->is_device_idle(hdev, idle_mask,
1217 HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL)) {
1218 dev_err(hdev->dev, "device is not idle (mask 0x%llx_%llx) after reset\n",
1219 idle_mask[1], idle_mask[0]);
1224 /* Check that the communication with the device is working */
1225 rc = hdev->asic_funcs->test_queues(hdev);
1227 dev_err(hdev->dev, "Failed to detect if device is alive after reset\n");
1232 rc = device_late_init(hdev);
1234 dev_err(hdev->dev, "Failed late init after hard reset\n");
1238 rc = hl_vm_init(hdev);
1240 dev_err(hdev->dev, "Failed to init memory module after hard reset\n");
1244 hl_fw_set_max_power(hdev);
1246 rc = hdev->asic_funcs->non_hard_reset_late_init(hdev);
1248 if (reset_upon_device_release)
1250 "Failed late init in reset after device release\n");
1252 dev_err(hdev->dev, "Failed late init after soft reset\n");
1257 spin_lock(&hdev->reset_info.lock);
1258 hdev->reset_info.is_in_soft_reset = false;
1260 /* Schedule hard reset only if requested and if not already in hard reset.
1261 * We keep 'in_reset' enabled, so no other reset can go in during the hard
1264 if (!hard_reset && hdev->reset_info.hard_reset_schedule_flags)
1265 schedule_hard_reset = true;
1267 hdev->reset_info.in_reset = 0;
1269 spin_unlock(&hdev->reset_info.lock);
1271 hdev->reset_info.needs_reset = false;
1273 dev_notice(hdev->dev, "Successfully finished resetting the device\n");
1276 hdev->reset_info.hard_reset_cnt++;
1278 /* After reset is done, we are ready to receive events from
1279 * the F/W. We can't do it before because we will ignore events
1280 * and if those events are fatal, we won't know about it and
1281 * the device will be operational although it shouldn't be
1283 hdev->asic_funcs->enable_events_from_fw(hdev);
1284 } else if (!reset_upon_device_release) {
1285 hdev->reset_info.soft_reset_cnt++;
1288 if (schedule_hard_reset) {
1289 dev_info(hdev->dev, "Performing hard reset scheduled during soft reset\n");
1290 flags = hdev->reset_info.hard_reset_schedule_flags;
1291 hdev->reset_info.hard_reset_schedule_flags = 0;
1292 hdev->disabled = true;
1294 handle_reset_trigger(hdev, flags);
1301 hdev->disabled = true;
1302 hdev->reset_info.is_in_soft_reset = false;
1305 dev_err(hdev->dev, "Failed to reset! Device is NOT usable\n");
1306 hdev->reset_info.hard_reset_cnt++;
1307 } else if (reset_upon_device_release) {
1308 dev_err(hdev->dev, "Failed to reset device after user release\n");
1309 flags |= HL_DRV_RESET_HARD;
1310 flags &= ~HL_DRV_RESET_DEV_RELEASE;
1314 dev_err(hdev->dev, "Failed to do soft-reset\n");
1315 hdev->reset_info.soft_reset_cnt++;
1316 flags |= HL_DRV_RESET_HARD;
1321 hdev->reset_info.in_reset = 0;
1327 * hl_device_init - main initialization function for habanalabs device
1329 * @hdev: pointer to habanalabs device structure
1331 * Allocate an id for the device, do early initialization and then call the
1332 * ASIC specific initialization functions. Finally, create the cdev and the
1333 * Linux device to expose it to the user
1335 int hl_device_init(struct hl_device *hdev, struct class *hclass)
1337 int i, rc, cq_cnt, user_interrupt_cnt, cq_ready_cnt;
1339 bool add_cdev_sysfs_on_err = false;
1341 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id / 2);
1347 /* Initialize cdev and device structures */
1348 rc = device_init_cdev(hdev, hclass, hdev->id, &hl_ops, name,
1349 &hdev->cdev, &hdev->dev);
1356 name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->id / 2);
1362 /* Initialize cdev and device structures for control device */
1363 rc = device_init_cdev(hdev, hclass, hdev->id_control, &hl_ctrl_ops,
1364 name, &hdev->cdev_ctrl, &hdev->dev_ctrl);
1371 /* Initialize ASIC function pointers and perform early init */
1372 rc = device_early_init(hdev);
1376 user_interrupt_cnt = hdev->asic_prop.user_interrupt_count;
1378 if (user_interrupt_cnt) {
1379 hdev->user_interrupt = kcalloc(user_interrupt_cnt,
1380 sizeof(*hdev->user_interrupt),
1383 if (!hdev->user_interrupt) {
1390 * Start calling ASIC initialization. First S/W then H/W and finally
1393 rc = hdev->asic_funcs->sw_init(hdev);
1395 goto user_interrupts_fini;
1398 /* initialize completion structure for multi CS wait */
1399 hl_multi_cs_completion_init(hdev);
1402 * Initialize the H/W queues. Must be done before hw_init, because
1403 * there the addresses of the kernel queue are being written to the
1404 * registers of the device
1406 rc = hl_hw_queues_create(hdev);
1408 dev_err(hdev->dev, "failed to initialize kernel queues\n");
1412 cq_cnt = hdev->asic_prop.completion_queues_count;
1415 * Initialize the completion queues. Must be done before hw_init,
1416 * because there the addresses of the completion queues are being
1417 * passed as arguments to request_irq
1420 hdev->completion_queue = kcalloc(cq_cnt,
1421 sizeof(*hdev->completion_queue),
1424 if (!hdev->completion_queue) {
1426 "failed to allocate completion queues\n");
1428 goto hw_queues_destroy;
1432 for (i = 0, cq_ready_cnt = 0 ; i < cq_cnt ; i++, cq_ready_cnt++) {
1433 rc = hl_cq_init(hdev, &hdev->completion_queue[i],
1434 hdev->asic_funcs->get_queue_id_for_cq(hdev, i));
1437 "failed to initialize completion queue\n");
1440 hdev->completion_queue[i].cq_idx = i;
1444 * Initialize the event queue. Must be done before hw_init,
1445 * because there the address of the event queue is being
1446 * passed as argument to request_irq
1448 rc = hl_eq_init(hdev, &hdev->event_queue);
1450 dev_err(hdev->dev, "failed to initialize event queue\n");
1454 /* MMU S/W must be initialized before kernel context is created */
1455 rc = hl_mmu_init(hdev);
1457 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n");
1461 /* Allocate the kernel context */
1462 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
1463 if (!hdev->kernel_ctx) {
1468 hdev->is_compute_ctx_active = false;
1470 hdev->asic_funcs->state_dump_init(hdev);
1472 hl_debugfs_add_device(hdev);
1474 /* debugfs nodes are created in hl_ctx_init so it must be called after
1475 * hl_debugfs_add_device.
1477 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1479 dev_err(hdev->dev, "failed to initialize kernel context\n");
1480 kfree(hdev->kernel_ctx);
1481 goto remove_device_from_debugfs;
1484 rc = hl_cb_pool_init(hdev);
1486 dev_err(hdev->dev, "failed to initialize CB pool\n");
1491 * From this point, override rc (=0) in case of an error to allow
1492 * debugging (by adding char devices and create sysfs nodes as part of
1495 add_cdev_sysfs_on_err = true;
1497 /* Device is now enabled as part of the initialization requires
1498 * communication with the device firmware to get information that
1499 * is required for the initialization itself
1501 hdev->disabled = false;
1503 rc = hdev->asic_funcs->hw_init(hdev);
1505 dev_err(hdev->dev, "failed to initialize the H/W\n");
1510 /* Check that the communication with the device is working */
1511 rc = hdev->asic_funcs->test_queues(hdev);
1513 dev_err(hdev->dev, "Failed to detect if device is alive\n");
1518 rc = device_late_init(hdev);
1520 dev_err(hdev->dev, "Failed late initialization\n");
1525 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
1527 hdev->asic_prop.dram_size / SZ_1G);
1529 rc = hl_vm_init(hdev);
1531 dev_err(hdev->dev, "Failed to initialize memory module\n");
1537 * Expose devices and sysfs nodes to user.
1538 * From here there is no need to add char devices and create sysfs nodes
1539 * in case of an error.
1541 add_cdev_sysfs_on_err = false;
1542 rc = device_cdev_sysfs_add(hdev);
1545 "Failed to add char devices and sysfs nodes\n");
1550 /* Need to call this again because the max power might change,
1551 * depending on card type for certain ASICs
1553 if (hdev->asic_prop.set_max_power_on_device_init)
1554 hl_fw_set_max_power(hdev);
1557 * hl_hwmon_init() must be called after device_late_init(), because only
1558 * there we get the information from the device about which
1559 * hwmon-related sensors the device supports.
1560 * Furthermore, it must be done after adding the device to the system.
1562 rc = hl_hwmon_init(hdev);
1564 dev_err(hdev->dev, "Failed to initialize hwmon\n");
1569 dev_notice(hdev->dev,
1570 "Successfully added device to habanalabs driver\n");
1572 hdev->init_done = true;
1574 /* After initialization is done, we are ready to receive events from
1575 * the F/W. We can't do it before because we will ignore events and if
1576 * those events are fatal, we won't know about it and the device will
1577 * be operational although it shouldn't be
1579 hdev->asic_funcs->enable_events_from_fw(hdev);
1584 if (hl_ctx_put(hdev->kernel_ctx) != 1)
1586 "kernel ctx is still alive on initialization failure\n");
1587 remove_device_from_debugfs:
1588 hl_debugfs_remove_device(hdev);
1592 hl_eq_fini(hdev, &hdev->event_queue);
1594 for (i = 0 ; i < cq_ready_cnt ; i++)
1595 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1596 kfree(hdev->completion_queue);
1598 hl_hw_queues_destroy(hdev);
1600 hdev->asic_funcs->sw_fini(hdev);
1601 user_interrupts_fini:
1602 kfree(hdev->user_interrupt);
1604 device_early_fini(hdev);
1606 put_device(hdev->dev_ctrl);
1608 put_device(hdev->dev);
1610 hdev->disabled = true;
1611 if (add_cdev_sysfs_on_err)
1612 device_cdev_sysfs_add(hdev);
1614 dev_err(&hdev->pdev->dev,
1615 "Failed to initialize hl%d. Device is NOT usable !\n",
1618 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
1625 * hl_device_fini - main tear-down function for habanalabs device
1627 * @hdev: pointer to habanalabs device structure
1629 * Destroy the device, call ASIC fini functions and release the id
1631 void hl_device_fini(struct hl_device *hdev)
1633 bool device_in_reset;
1638 dev_info(hdev->dev, "Removing device\n");
1640 hdev->device_fini_pending = 1;
1641 flush_delayed_work(&hdev->device_reset_work.reset_work);
1644 reset_sec = HL_PLDM_HARD_RESET_MAX_TIMEOUT;
1646 reset_sec = HL_HARD_RESET_MAX_TIMEOUT;
1649 * This function is competing with the reset function, so try to
1650 * take the reset atomic and if we are already in middle of reset,
1651 * wait until reset function is finished. Reset function is designed
1652 * to always finish. However, in Gaudi, because of all the network
1653 * ports, the hard reset could take between 10-30 seconds
1656 timeout = ktime_add_us(ktime_get(), reset_sec * 1000 * 1000);
1658 spin_lock(&hdev->reset_info.lock);
1659 device_in_reset = !!hdev->reset_info.in_reset;
1660 if (!device_in_reset)
1661 hdev->reset_info.in_reset = 1;
1662 spin_unlock(&hdev->reset_info.lock);
1664 while (device_in_reset) {
1665 usleep_range(50, 200);
1667 spin_lock(&hdev->reset_info.lock);
1668 device_in_reset = !!hdev->reset_info.in_reset;
1669 if (!device_in_reset)
1670 hdev->reset_info.in_reset = 1;
1671 spin_unlock(&hdev->reset_info.lock);
1673 if (ktime_compare(ktime_get(), timeout) > 0) {
1675 "Failed to remove device because reset function did not finish\n");
1680 /* Disable PCI access from device F/W so it won't send us additional
1681 * interrupts. We disable MSI/MSI-X at the halt_engines function and we
1682 * can't have the F/W sending us interrupts after that. We need to
1683 * disable the access here because if the device is marked disable, the
1684 * message won't be send. Also, in case of heartbeat, the device CPU is
1685 * marked as disable so this message won't be sent
1687 hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS);
1689 /* Mark device as disabled */
1690 hdev->disabled = true;
1692 take_release_locks(hdev);
1694 hdev->reset_info.hard_reset_pending = true;
1696 hl_hwmon_fini(hdev);
1698 cleanup_resources(hdev, true, false, false);
1700 /* Kill processes here after CS rollback. This is because the process
1701 * can't really exit until all its CSs are done, which is what we
1705 "Waiting for all processes to exit (timeout of %u seconds)",
1706 HL_PENDING_RESET_LONG_SEC);
1708 rc = device_kill_open_processes(hdev, HL_PENDING_RESET_LONG_SEC, false);
1710 dev_crit(hdev->dev, "Failed to kill all open processes\n");
1711 device_disable_open_processes(hdev, false);
1714 rc = device_kill_open_processes(hdev, 0, true);
1716 dev_crit(hdev->dev, "Failed to kill all control device open processes\n");
1717 device_disable_open_processes(hdev, true);
1720 hl_cb_pool_fini(hdev);
1722 /* Reset the H/W. It will be in idle state after this returns */
1723 hdev->asic_funcs->hw_fini(hdev, true, false);
1725 hdev->fw_loader.fw_comp_loaded = FW_TYPE_NONE;
1727 /* Release kernel context */
1728 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
1729 dev_err(hdev->dev, "kernel ctx is still alive\n");
1731 hl_debugfs_remove_device(hdev);
1737 hl_eq_fini(hdev, &hdev->event_queue);
1739 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1740 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1741 kfree(hdev->completion_queue);
1742 kfree(hdev->user_interrupt);
1744 hl_hw_queues_destroy(hdev);
1746 /* Call ASIC S/W finalize function */
1747 hdev->asic_funcs->sw_fini(hdev);
1749 device_early_fini(hdev);
1751 /* Hide devices and sysfs nodes from user */
1752 device_cdev_sysfs_del(hdev);
1754 pr_info("removed device successfully\n");
1758 * MMIO register access helper functions.
1762 * hl_rreg - Read an MMIO register
1764 * @hdev: pointer to habanalabs device structure
1765 * @reg: MMIO register offset (in bytes)
1767 * Returns the value of the MMIO register we are asked to read
1770 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1772 return readl(hdev->rmmio + reg);
1776 * hl_wreg - Write to an MMIO register
1778 * @hdev: pointer to habanalabs device structure
1779 * @reg: MMIO register offset (in bytes)
1780 * @val: 32-bit value
1782 * Writes the 32-bit value into the MMIO register
1785 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1787 writel(val, hdev->rmmio + reg);