1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
11 #include <linux/sched/signal.h>
12 #include <linux/hwmon.h>
14 bool hl_device_disabled_or_in_reset(struct hl_device *hdev)
16 if ((hdev->disabled) || (atomic_read(&hdev->in_reset)))
22 static void hpriv_release(struct kref *ref)
24 struct hl_fpriv *hpriv;
25 struct hl_device *hdev;
27 hpriv = container_of(ref, struct hl_fpriv, refcount);
31 put_pid(hpriv->taskpid);
33 hl_debugfs_remove_file(hpriv);
35 mutex_destroy(&hpriv->restore_phase_mutex);
39 /* Now the FD is really closed */
40 atomic_dec(&hdev->fd_open_cnt);
42 /* This allows a new user context to open the device */
43 hdev->user_ctx = NULL;
46 void hl_hpriv_get(struct hl_fpriv *hpriv)
48 kref_get(&hpriv->refcount);
51 void hl_hpriv_put(struct hl_fpriv *hpriv)
53 kref_put(&hpriv->refcount, hpriv_release);
57 * hl_device_release - release function for habanalabs device
59 * @inode: pointer to inode structure
60 * @filp: pointer to file structure
62 * Called when process closes an habanalabs device
64 static int hl_device_release(struct inode *inode, struct file *filp)
66 struct hl_fpriv *hpriv = filp->private_data;
68 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
69 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
71 filp->private_data = NULL;
79 * hl_mmap - mmap function for habanalabs device
81 * @*filp: pointer to file structure
82 * @*vma: pointer to vm_area_struct of the process
84 * Called when process does an mmap on habanalabs device. Call the device's mmap
85 * function at the end of the common code.
87 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
89 struct hl_fpriv *hpriv = filp->private_data;
91 if ((vma->vm_pgoff & HL_MMAP_CB_MASK) == HL_MMAP_CB_MASK) {
92 vma->vm_pgoff ^= HL_MMAP_CB_MASK;
93 return hl_cb_mmap(hpriv, vma);
99 static const struct file_operations hl_ops = {
100 .owner = THIS_MODULE,
101 .open = hl_device_open,
102 .release = hl_device_release,
104 .unlocked_ioctl = hl_ioctl,
105 .compat_ioctl = hl_ioctl
109 * device_setup_cdev - setup cdev and device for habanalabs device
111 * @hdev: pointer to habanalabs device structure
112 * @hclass: pointer to the class object of the device
113 * @minor: minor number of the specific device
114 * @fpos : file operations to install for this device
116 * Create a cdev and a Linux device for habanalabs's device. Need to be
117 * called at the end of the habanalabs device initialization process,
118 * because this function exposes the device to the user
120 static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
121 int minor, const struct file_operations *fops)
123 int err, devno = MKDEV(hdev->major, minor);
124 struct cdev *hdev_cdev = &hdev->cdev;
127 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id);
131 cdev_init(hdev_cdev, fops);
132 hdev_cdev->owner = THIS_MODULE;
133 err = cdev_add(hdev_cdev, devno, 1);
135 pr_err("Failed to add char device %s\n", name);
139 hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
140 if (IS_ERR(hdev->dev)) {
141 pr_err("Failed to create device %s\n", name);
142 err = PTR_ERR(hdev->dev);
143 goto err_device_create;
146 dev_set_drvdata(hdev->dev, hdev);
160 * device_early_init - do some early initialization for the habanalabs device
162 * @hdev: pointer to habanalabs device structure
164 * Install the relevant function pointers and call the early_init function,
165 * if such a function exists
167 static int device_early_init(struct hl_device *hdev)
171 switch (hdev->asic_type) {
173 goya_set_asic_funcs(hdev);
174 strlcpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
177 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
182 rc = hdev->asic_funcs->early_init(hdev);
186 rc = hl_asid_init(hdev);
190 hdev->cq_wq = alloc_workqueue("hl-free-jobs", WQ_UNBOUND, 0);
191 if (hdev->cq_wq == NULL) {
192 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
197 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
198 if (hdev->eq_wq == NULL) {
199 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
204 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
206 if (!hdev->hl_chip_info) {
211 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
213 mutex_init(&hdev->fd_open_cnt_lock);
214 mutex_init(&hdev->send_cpu_message_lock);
215 INIT_LIST_HEAD(&hdev->hw_queues_mirror_list);
216 spin_lock_init(&hdev->hw_queues_mirror_lock);
217 atomic_set(&hdev->in_reset, 0);
218 atomic_set(&hdev->fd_open_cnt, 0);
223 destroy_workqueue(hdev->eq_wq);
225 destroy_workqueue(hdev->cq_wq);
229 if (hdev->asic_funcs->early_fini)
230 hdev->asic_funcs->early_fini(hdev);
236 * device_early_fini - finalize all that was done in device_early_init
238 * @hdev: pointer to habanalabs device structure
241 static void device_early_fini(struct hl_device *hdev)
243 mutex_destroy(&hdev->send_cpu_message_lock);
245 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
247 kfree(hdev->hl_chip_info);
249 destroy_workqueue(hdev->eq_wq);
250 destroy_workqueue(hdev->cq_wq);
254 if (hdev->asic_funcs->early_fini)
255 hdev->asic_funcs->early_fini(hdev);
257 mutex_destroy(&hdev->fd_open_cnt_lock);
260 static void set_freq_to_low_job(struct work_struct *work)
262 struct hl_device *hdev = container_of(work, struct hl_device,
265 if (atomic_read(&hdev->fd_open_cnt) == 0)
266 hl_device_set_frequency(hdev, PLL_LOW);
268 schedule_delayed_work(&hdev->work_freq,
269 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
272 static void hl_device_heartbeat(struct work_struct *work)
274 struct hl_device *hdev = container_of(work, struct hl_device,
275 work_heartbeat.work);
277 if (hl_device_disabled_or_in_reset(hdev))
280 if (!hdev->asic_funcs->send_heartbeat(hdev))
283 dev_err(hdev->dev, "Device heartbeat failed!\n");
284 hl_device_reset(hdev, true, false);
289 schedule_delayed_work(&hdev->work_heartbeat,
290 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
294 * device_late_init - do late stuff initialization for the habanalabs device
296 * @hdev: pointer to habanalabs device structure
298 * Do stuff that either needs the device H/W queues to be active or needs
299 * to happen after all the rest of the initialization is finished
301 static int device_late_init(struct hl_device *hdev)
305 INIT_DELAYED_WORK(&hdev->work_freq, set_freq_to_low_job);
306 hdev->high_pll = hdev->asic_prop.high_pll;
308 /* force setting to low frequency */
309 atomic_set(&hdev->curr_pll_profile, PLL_LOW);
311 if (hdev->pm_mng_profile == PM_AUTO)
312 hdev->asic_funcs->set_pll_profile(hdev, PLL_LOW);
314 hdev->asic_funcs->set_pll_profile(hdev, PLL_LAST);
316 if (hdev->asic_funcs->late_init) {
317 rc = hdev->asic_funcs->late_init(hdev);
320 "failed late initialization for the H/W\n");
325 schedule_delayed_work(&hdev->work_freq,
326 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
328 if (hdev->heartbeat) {
329 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
330 schedule_delayed_work(&hdev->work_heartbeat,
331 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
334 hdev->late_init_done = true;
340 * device_late_fini - finalize all that was done in device_late_init
342 * @hdev: pointer to habanalabs device structure
345 static void device_late_fini(struct hl_device *hdev)
347 if (!hdev->late_init_done)
350 cancel_delayed_work_sync(&hdev->work_freq);
352 cancel_delayed_work_sync(&hdev->work_heartbeat);
354 if (hdev->asic_funcs->late_fini)
355 hdev->asic_funcs->late_fini(hdev);
357 hdev->late_init_done = false;
361 * hl_device_set_frequency - set the frequency of the device
363 * @hdev: pointer to habanalabs device structure
364 * @freq: the new frequency value
366 * Change the frequency if needed.
367 * We allose to set PLL to low only if there is no user process
368 * Returns 0 if no change was done, otherwise returns 1;
370 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq)
372 enum hl_pll_frequency old_freq =
373 (freq == PLL_HIGH) ? PLL_LOW : PLL_HIGH;
376 if (hdev->pm_mng_profile == PM_MANUAL)
379 ret = atomic_cmpxchg(&hdev->curr_pll_profile, old_freq, freq);
384 * in case we want to lower frequency, check if device is not
385 * opened. We must have a check here to workaround race condition with
388 if ((freq == PLL_LOW) && (atomic_read(&hdev->fd_open_cnt) > 0)) {
389 atomic_set(&hdev->curr_pll_profile, PLL_HIGH);
393 dev_dbg(hdev->dev, "Changing device frequency to %s\n",
394 freq == PLL_HIGH ? "high" : "low");
396 hdev->asic_funcs->set_pll_profile(hdev, freq);
402 * hl_device_suspend - initiate device suspend
404 * @hdev: pointer to habanalabs device structure
406 * Puts the hw in the suspend state (all asics).
407 * Returns 0 for success or an error on failure.
408 * Called at driver suspend.
410 int hl_device_suspend(struct hl_device *hdev)
414 pci_save_state(hdev->pdev);
416 rc = hdev->asic_funcs->suspend(hdev);
419 "Failed to disable PCI access of device CPU\n");
421 /* Shut down the device */
422 pci_disable_device(hdev->pdev);
423 pci_set_power_state(hdev->pdev, PCI_D3hot);
429 * hl_device_resume - initiate device resume
431 * @hdev: pointer to habanalabs device structure
433 * Bring the hw back to operating state (all asics).
434 * Returns 0 for success or an error on failure.
435 * Called at driver resume.
437 int hl_device_resume(struct hl_device *hdev)
441 pci_set_power_state(hdev->pdev, PCI_D0);
442 pci_restore_state(hdev->pdev);
443 rc = pci_enable_device(hdev->pdev);
446 "Failed to enable PCI device in resume\n");
450 rc = hdev->asic_funcs->resume(hdev);
453 "Failed to enable PCI access from device CPU\n");
460 static void hl_device_hard_reset_pending(struct work_struct *work)
462 struct hl_device_reset_work *device_reset_work =
463 container_of(work, struct hl_device_reset_work, reset_work);
464 struct hl_device *hdev = device_reset_work->hdev;
465 u16 pending_cnt = HL_PENDING_RESET_PER_SEC;
466 struct task_struct *task = NULL;
468 /* Flush all processes that are inside hl_open */
469 mutex_lock(&hdev->fd_open_cnt_lock);
471 while ((atomic_read(&hdev->fd_open_cnt)) && (pending_cnt)) {
476 "Can't HARD reset, waiting for user to close FD\n");
480 if (atomic_read(&hdev->fd_open_cnt)) {
481 task = get_pid_task(hdev->user_ctx->hpriv->taskpid,
484 dev_info(hdev->dev, "Killing user processes\n");
485 send_sig(SIGKILL, task, 1);
488 put_task_struct(task);
492 mutex_unlock(&hdev->fd_open_cnt_lock);
494 hl_device_reset(hdev, true, true);
496 kfree(device_reset_work);
500 * hl_device_reset - reset the device
502 * @hdev: pointer to habanalabs device structure
503 * @hard_reset: should we do hard reset to all engines or just reset the
504 * compute/dma engines
506 * Block future CS and wait for pending CS to be enqueued
508 * Flush all completions
509 * Re-initialize all internal data structures
510 * Call ASIC H/W init, late_init
514 * Returns 0 for success or an error on failure.
516 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
517 bool from_hard_reset_thread)
521 if (!hdev->init_done) {
523 "Can't reset before initialization is done\n");
528 * Prevent concurrency in this function - only one reset should be
529 * done at any given time. Only need to perform this if we didn't
530 * get from the dedicated hard reset thread
532 if (!from_hard_reset_thread) {
533 /* Block future CS/VM/JOB completion operations */
534 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
538 /* This also blocks future CS/VM/JOB completion operations */
539 hdev->disabled = true;
542 * Flush anyone that is inside the critical section of enqueue
545 hdev->asic_funcs->hw_queues_lock(hdev);
546 hdev->asic_funcs->hw_queues_unlock(hdev);
548 dev_err(hdev->dev, "Going to RESET device!\n");
552 if ((hard_reset) && (!from_hard_reset_thread)) {
553 struct hl_device_reset_work *device_reset_work;
557 "Reset action is NOT supported in simulator\n");
562 hdev->hard_reset_pending = true;
564 device_reset_work = kzalloc(sizeof(*device_reset_work),
566 if (!device_reset_work) {
572 * Because the reset function can't run from interrupt or
573 * from heartbeat work, we need to call the reset function
574 * from a dedicated work
576 INIT_WORK(&device_reset_work->reset_work,
577 hl_device_hard_reset_pending);
578 device_reset_work->hdev = hdev;
579 schedule_work(&device_reset_work->reset_work);
585 device_late_fini(hdev);
588 * Now that the heartbeat thread is closed, flush processes
589 * which are sending messages to CPU
591 mutex_lock(&hdev->send_cpu_message_lock);
592 mutex_unlock(&hdev->send_cpu_message_lock);
596 * Halt the engines and disable interrupts so we won't get any more
597 * completions from H/W and we won't have any accesses from the
598 * H/W to the host machine
600 hdev->asic_funcs->halt_engines(hdev, hard_reset);
602 /* Go over all the queues, release all CS and their jobs */
603 hl_cs_rollback_all(hdev);
606 /* Release kernel context */
607 if (hl_ctx_put(hdev->kernel_ctx) != 1) {
609 "kernel ctx is alive during hard reset\n");
614 hdev->kernel_ctx = NULL;
617 /* Reset the H/W. It will be in idle state after this returns */
618 hdev->asic_funcs->hw_fini(hdev, hard_reset);
622 hl_eq_reset(hdev, &hdev->event_queue);
625 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
626 hl_hw_queue_reset(hdev, hard_reset);
627 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
628 hl_cq_reset(hdev, &hdev->completion_queue[i]);
630 /* Make sure the setup phase for the user context will run again */
631 if (hdev->user_ctx) {
632 atomic_set(&hdev->user_ctx->thread_restore_token, 1);
633 hdev->user_ctx->thread_restore_wait_token = 0;
636 /* Finished tear-down, starting to re-initialize */
639 hdev->device_cpu_disabled = false;
641 /* Allocate the kernel context */
642 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
644 if (!hdev->kernel_ctx) {
649 hdev->user_ctx = NULL;
651 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
654 "failed to init kernel ctx in hard reset\n");
655 kfree(hdev->kernel_ctx);
656 hdev->kernel_ctx = NULL;
661 rc = hdev->asic_funcs->hw_init(hdev);
664 "failed to initialize the H/W after reset\n");
668 hdev->disabled = false;
670 /* Check that the communication with the device is working */
671 rc = hdev->asic_funcs->test_queues(hdev);
674 "Failed to detect if device is alive after reset\n");
679 rc = device_late_init(hdev);
682 "Failed late init after hard reset\n");
686 rc = hl_vm_init(hdev);
689 "Failed to init memory module after hard reset\n");
693 hl_set_max_power(hdev, hdev->max_power);
695 hdev->hard_reset_pending = false;
697 rc = hdev->asic_funcs->soft_reset_late_init(hdev);
700 "Failed late init after soft reset\n");
705 atomic_set(&hdev->in_reset, 0);
708 hdev->hard_reset_cnt++;
710 hdev->soft_reset_cnt++;
715 hdev->disabled = true;
719 "Failed to reset! Device is NOT usable\n");
720 hdev->hard_reset_cnt++;
723 "Failed to do soft-reset, trying hard reset\n");
724 hdev->soft_reset_cnt++;
729 atomic_set(&hdev->in_reset, 0);
735 * hl_device_init - main initialization function for habanalabs device
737 * @hdev: pointer to habanalabs device structure
739 * Allocate an id for the device, do early initialization and then call the
740 * ASIC specific initialization functions. Finally, create the cdev and the
741 * Linux device to expose it to the user
743 int hl_device_init(struct hl_device *hdev, struct class *hclass)
745 int i, rc, cq_ready_cnt;
748 rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops);
753 /* Initialize ASIC function pointers and perform early init */
754 rc = device_early_init(hdev);
759 * Start calling ASIC initialization. First S/W then H/W and finally
762 rc = hdev->asic_funcs->sw_init(hdev);
767 * Initialize the H/W queues. Must be done before hw_init, because
768 * there the addresses of the kernel queue are being written to the
769 * registers of the device
771 rc = hl_hw_queues_create(hdev);
773 dev_err(hdev->dev, "failed to initialize kernel queues\n");
778 * Initialize the completion queues. Must be done before hw_init,
779 * because there the addresses of the completion queues are being
780 * passed as arguments to request_irq
782 hdev->completion_queue =
783 kcalloc(hdev->asic_prop.completion_queues_count,
784 sizeof(*hdev->completion_queue), GFP_KERNEL);
786 if (!hdev->completion_queue) {
787 dev_err(hdev->dev, "failed to allocate completion queues\n");
789 goto hw_queues_destroy;
792 for (i = 0, cq_ready_cnt = 0;
793 i < hdev->asic_prop.completion_queues_count;
794 i++, cq_ready_cnt++) {
795 rc = hl_cq_init(hdev, &hdev->completion_queue[i], i);
798 "failed to initialize completion queue\n");
804 * Initialize the event queue. Must be done before hw_init,
805 * because there the address of the event queue is being
806 * passed as argument to request_irq
808 rc = hl_eq_init(hdev, &hdev->event_queue);
810 dev_err(hdev->dev, "failed to initialize event queue\n");
814 /* Allocate the kernel context */
815 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
816 if (!hdev->kernel_ctx) {
821 hdev->user_ctx = NULL;
823 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
825 dev_err(hdev->dev, "failed to initialize kernel context\n");
829 rc = hl_cb_pool_init(hdev);
831 dev_err(hdev->dev, "failed to initialize CB pool\n");
835 rc = hl_sysfs_init(hdev);
837 dev_err(hdev->dev, "failed to initialize sysfs\n");
841 hl_debugfs_add_device(hdev);
843 if (hdev->asic_funcs->get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
845 "H/W state is dirty, must reset before initializing\n");
846 hdev->asic_funcs->hw_fini(hdev, true);
849 rc = hdev->asic_funcs->hw_init(hdev);
851 dev_err(hdev->dev, "failed to initialize the H/W\n");
856 hdev->disabled = false;
858 /* Check that the communication with the device is working */
859 rc = hdev->asic_funcs->test_queues(hdev);
861 dev_err(hdev->dev, "Failed to detect if device is alive\n");
866 /* After test_queues, KMD can start sending messages to device CPU */
868 rc = device_late_init(hdev);
870 dev_err(hdev->dev, "Failed late initialization\n");
875 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
877 hdev->asic_prop.dram_size / 1024 / 1024 / 1024);
879 rc = hl_vm_init(hdev);
881 dev_err(hdev->dev, "Failed to initialize memory module\n");
887 * hl_hwmon_init must be called after device_late_init, because only
888 * there we get the information from the device about which
889 * hwmon-related sensors the device supports
891 rc = hl_hwmon_init(hdev);
893 dev_err(hdev->dev, "Failed to initialize hwmon\n");
898 dev_notice(hdev->dev,
899 "Successfully added device to habanalabs driver\n");
901 hdev->init_done = true;
906 hl_cb_pool_fini(hdev);
908 if (hl_ctx_put(hdev->kernel_ctx) != 1)
910 "kernel ctx is still alive on initialization failure\n");
912 kfree(hdev->kernel_ctx);
914 hl_eq_fini(hdev, &hdev->event_queue);
916 for (i = 0 ; i < cq_ready_cnt ; i++)
917 hl_cq_fini(hdev, &hdev->completion_queue[i]);
918 kfree(hdev->completion_queue);
920 hl_hw_queues_destroy(hdev);
922 hdev->asic_funcs->sw_fini(hdev);
924 device_early_fini(hdev);
926 device_destroy(hclass, hdev->dev->devt);
927 cdev_del(&hdev->cdev);
929 hdev->disabled = true;
931 dev_err(&hdev->pdev->dev,
932 "Failed to initialize hl%d. Device is NOT usable !\n",
935 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
942 * hl_device_fini - main tear-down function for habanalabs device
944 * @hdev: pointer to habanalabs device structure
946 * Destroy the device, call ASIC fini functions and release the id
948 void hl_device_fini(struct hl_device *hdev)
953 dev_info(hdev->dev, "Removing device\n");
956 * This function is competing with the reset function, so try to
957 * take the reset atomic and if we are already in middle of reset,
958 * wait until reset function is finished. Reset function is designed
959 * to always finish (could take up to a few seconds in worst case).
962 timeout = ktime_add_us(ktime_get(),
963 HL_PENDING_RESET_PER_SEC * 1000 * 1000 * 4);
964 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
966 usleep_range(50, 200);
967 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
968 if (ktime_compare(ktime_get(), timeout) > 0) {
969 WARN(1, "Failed to remove device because reset function did not finish\n");
974 /* Mark device as disabled */
975 hdev->disabled = true;
979 device_late_fini(hdev);
981 hl_debugfs_remove_device(hdev);
986 * Halt the engines and disable interrupts so we won't get any more
987 * completions from H/W and we won't have any accesses from the
988 * H/W to the host machine
990 hdev->asic_funcs->halt_engines(hdev, true);
992 /* Go over all the queues, release all CS and their jobs */
993 hl_cs_rollback_all(hdev);
995 hl_cb_pool_fini(hdev);
997 /* Release kernel context */
998 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
999 dev_err(hdev->dev, "kernel ctx is still alive\n");
1001 /* Reset the H/W. It will be in idle state after this returns */
1002 hdev->asic_funcs->hw_fini(hdev, true);
1006 hl_eq_fini(hdev, &hdev->event_queue);
1008 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1009 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1010 kfree(hdev->completion_queue);
1012 hl_hw_queues_destroy(hdev);
1014 /* Call ASIC S/W finalize function */
1015 hdev->asic_funcs->sw_fini(hdev);
1017 device_early_fini(hdev);
1019 /* Hide device from user */
1020 device_destroy(hdev->dev->class, hdev->dev->devt);
1021 cdev_del(&hdev->cdev);
1023 pr_info("removed device successfully\n");
1027 * hl_poll_timeout_memory - Periodically poll a host memory address
1028 * until it is not zero or a timeout occurs
1029 * @hdev: pointer to habanalabs device structure
1030 * @addr: Address to poll
1031 * @timeout_us: timeout in us
1032 * @val: Variable to read the value into
1034 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
1035 * case, the last read value at @addr is stored in @val. Must not
1036 * be called from atomic context if sleep_us or timeout_us are used.
1038 * The function sleeps for 100us with timeout value of
1041 int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr,
1042 u32 timeout_us, u32 *val)
1045 * address in this function points always to a memory location in the
1046 * host's (server's) memory. That location is updated asynchronously
1047 * either by the direct access of the device or by another core
1049 u32 *paddr = (u32 *) (uintptr_t) addr;
1050 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
1056 * Flush CPU read/write buffers to make sure we read updates
1057 * done by other cores or by the device
1063 if (ktime_compare(ktime_get(), timeout) > 0) {
1067 usleep_range((100 >> 2) + 1, 100);
1070 return *val ? 0 : -ETIMEDOUT;
1074 * hl_poll_timeout_devicememory - Periodically poll a device memory address
1075 * until it is not zero or a timeout occurs
1076 * @hdev: pointer to habanalabs device structure
1077 * @addr: Device address to poll
1078 * @timeout_us: timeout in us
1079 * @val: Variable to read the value into
1081 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
1082 * case, the last read value at @addr is stored in @val. Must not
1083 * be called from atomic context if sleep_us or timeout_us are used.
1085 * The function sleeps for 100us with timeout value of
1088 int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
1089 u32 timeout_us, u32 *val)
1091 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
1099 if (ktime_compare(ktime_get(), timeout) > 0) {
1103 usleep_range((100 >> 2) + 1, 100);
1106 return *val ? 0 : -ETIMEDOUT;
1110 * MMIO register access helper functions.
1114 * hl_rreg - Read an MMIO register
1116 * @hdev: pointer to habanalabs device structure
1117 * @reg: MMIO register offset (in bytes)
1119 * Returns the value of the MMIO register we are asked to read
1122 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1124 return readl(hdev->rmmio + reg);
1128 * hl_wreg - Write to an MMIO register
1130 * @hdev: pointer to habanalabs device structure
1131 * @reg: MMIO register offset (in bytes)
1132 * @val: 32-bit value
1134 * Writes the 32-bit value into the MMIO register
1137 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1139 writel(val, hdev->rmmio + reg);