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/sched/signal.h>
14 #include <linux/hwmon.h>
15 #include <uapi/misc/habanalabs.h>
17 #define HL_PLDM_PENDING_RESET_PER_SEC (HL_PENDING_RESET_PER_SEC * 10)
19 bool hl_device_disabled_or_in_reset(struct hl_device *hdev)
21 if ((hdev->disabled) || (atomic_read(&hdev->in_reset)))
27 enum hl_device_status hl_device_status(struct hl_device *hdev)
29 enum hl_device_status status;
32 status = HL_DEVICE_STATUS_MALFUNCTION;
33 else if (atomic_read(&hdev->in_reset))
34 status = HL_DEVICE_STATUS_IN_RESET;
36 status = HL_DEVICE_STATUS_OPERATIONAL;
41 static void hpriv_release(struct kref *ref)
43 struct hl_fpriv *hpriv;
44 struct hl_device *hdev;
47 hpriv = container_of(ref, struct hl_fpriv, refcount);
52 put_pid(hpriv->taskpid);
54 hl_debugfs_remove_file(hpriv);
56 mutex_destroy(&hpriv->restore_phase_mutex);
58 mutex_lock(&hdev->fpriv_list_lock);
59 list_del(&hpriv->dev_node);
60 hdev->compute_ctx = NULL;
61 mutex_unlock(&hdev->fpriv_list_lock);
66 void hl_hpriv_get(struct hl_fpriv *hpriv)
68 kref_get(&hpriv->refcount);
71 void hl_hpriv_put(struct hl_fpriv *hpriv)
73 kref_put(&hpriv->refcount, hpriv_release);
77 * hl_device_release - release function for habanalabs device
79 * @inode: pointer to inode structure
80 * @filp: pointer to file structure
82 * Called when process closes an habanalabs device
84 static int hl_device_release(struct inode *inode, struct file *filp)
86 struct hl_fpriv *hpriv = filp->private_data;
88 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
89 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
91 filp->private_data = NULL;
98 static int hl_device_release_ctrl(struct inode *inode, struct file *filp)
100 struct hl_fpriv *hpriv = filp->private_data;
101 struct hl_device *hdev;
103 filp->private_data = NULL;
107 mutex_lock(&hdev->fpriv_list_lock);
108 list_del(&hpriv->dev_node);
109 mutex_unlock(&hdev->fpriv_list_lock);
117 * hl_mmap - mmap function for habanalabs device
119 * @*filp: pointer to file structure
120 * @*vma: pointer to vm_area_struct of the process
122 * Called when process does an mmap on habanalabs device. Call the device's mmap
123 * function at the end of the common code.
125 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
127 struct hl_fpriv *hpriv = filp->private_data;
129 if ((vma->vm_pgoff & HL_MMAP_CB_MASK) == HL_MMAP_CB_MASK) {
130 vma->vm_pgoff ^= HL_MMAP_CB_MASK;
131 return hl_cb_mmap(hpriv, vma);
137 static const struct file_operations hl_ops = {
138 .owner = THIS_MODULE,
139 .open = hl_device_open,
140 .release = hl_device_release,
142 .unlocked_ioctl = hl_ioctl,
143 .compat_ioctl = hl_ioctl
146 static const struct file_operations hl_ctrl_ops = {
147 .owner = THIS_MODULE,
148 .open = hl_device_open_ctrl,
149 .release = hl_device_release_ctrl,
150 .unlocked_ioctl = hl_ioctl_control,
151 .compat_ioctl = hl_ioctl_control
155 * device_setup_cdev - setup cdev and device for habanalabs device
157 * @hdev: pointer to habanalabs device structure
158 * @hclass: pointer to the class object of the device
159 * @minor: minor number of the specific device
160 * @fpos: file operations to install for this device
161 * @name: name of the device as it will appear in the filesystem
162 * @cdev: pointer to the char device object that will be created
163 * @dev: pointer to the device object that will be created
165 * Create a cdev and a Linux device for habanalabs's device. Need to be
166 * called at the end of the habanalabs device initialization process,
167 * because this function exposes the device to the user
169 static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
170 int minor, const struct file_operations *fops,
171 char *name, struct cdev *cdev,
174 int err, devno = MKDEV(hdev->major, minor);
176 cdev_init(cdev, fops);
177 cdev->owner = THIS_MODULE;
178 err = cdev_add(cdev, devno, 1);
180 pr_err("Failed to add char device %s\n", name);
184 *dev = device_create(hclass, NULL, devno, NULL, "%s", name);
186 pr_err("Failed to create device %s\n", name);
188 goto err_device_create;
191 dev_set_drvdata(*dev, hdev);
201 * device_early_init - do some early initialization for the habanalabs device
203 * @hdev: pointer to habanalabs device structure
205 * Install the relevant function pointers and call the early_init function,
206 * if such a function exists
208 static int device_early_init(struct hl_device *hdev)
212 switch (hdev->asic_type) {
214 goya_set_asic_funcs(hdev);
215 strlcpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
218 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
223 rc = hdev->asic_funcs->early_init(hdev);
227 rc = hl_asid_init(hdev);
231 hdev->cq_wq = alloc_workqueue("hl-free-jobs", WQ_UNBOUND, 0);
232 if (hdev->cq_wq == NULL) {
233 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
238 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
239 if (hdev->eq_wq == NULL) {
240 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
245 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
247 if (!hdev->hl_chip_info) {
252 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
254 mutex_init(&hdev->send_cpu_message_lock);
255 mutex_init(&hdev->debug_lock);
256 mutex_init(&hdev->mmu_cache_lock);
257 INIT_LIST_HEAD(&hdev->hw_queues_mirror_list);
258 spin_lock_init(&hdev->hw_queues_mirror_lock);
259 INIT_LIST_HEAD(&hdev->fpriv_list);
260 mutex_init(&hdev->fpriv_list_lock);
261 atomic_set(&hdev->in_reset, 0);
262 atomic_set(&hdev->cs_active_cnt, 0);
267 destroy_workqueue(hdev->eq_wq);
269 destroy_workqueue(hdev->cq_wq);
273 if (hdev->asic_funcs->early_fini)
274 hdev->asic_funcs->early_fini(hdev);
280 * device_early_fini - finalize all that was done in device_early_init
282 * @hdev: pointer to habanalabs device structure
285 static void device_early_fini(struct hl_device *hdev)
287 mutex_destroy(&hdev->mmu_cache_lock);
288 mutex_destroy(&hdev->debug_lock);
289 mutex_destroy(&hdev->send_cpu_message_lock);
291 mutex_destroy(&hdev->fpriv_list_lock);
293 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
295 kfree(hdev->hl_chip_info);
297 destroy_workqueue(hdev->eq_wq);
298 destroy_workqueue(hdev->cq_wq);
302 if (hdev->asic_funcs->early_fini)
303 hdev->asic_funcs->early_fini(hdev);
306 static void set_freq_to_low_job(struct work_struct *work)
308 struct hl_device *hdev = container_of(work, struct hl_device,
311 mutex_lock(&hdev->fpriv_list_lock);
313 if (!hdev->compute_ctx)
314 hl_device_set_frequency(hdev, PLL_LOW);
316 mutex_unlock(&hdev->fpriv_list_lock);
318 schedule_delayed_work(&hdev->work_freq,
319 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
322 static void hl_device_heartbeat(struct work_struct *work)
324 struct hl_device *hdev = container_of(work, struct hl_device,
325 work_heartbeat.work);
327 if (hl_device_disabled_or_in_reset(hdev))
330 if (!hdev->asic_funcs->send_heartbeat(hdev))
333 dev_err(hdev->dev, "Device heartbeat failed!\n");
334 hl_device_reset(hdev, true, false);
339 schedule_delayed_work(&hdev->work_heartbeat,
340 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
344 * device_late_init - do late stuff initialization for the habanalabs device
346 * @hdev: pointer to habanalabs device structure
348 * Do stuff that either needs the device H/W queues to be active or needs
349 * to happen after all the rest of the initialization is finished
351 static int device_late_init(struct hl_device *hdev)
355 if (hdev->asic_funcs->late_init) {
356 rc = hdev->asic_funcs->late_init(hdev);
359 "failed late initialization for the H/W\n");
364 hdev->high_pll = hdev->asic_prop.high_pll;
366 /* force setting to low frequency */
367 hdev->curr_pll_profile = PLL_LOW;
369 if (hdev->pm_mng_profile == PM_AUTO)
370 hdev->asic_funcs->set_pll_profile(hdev, PLL_LOW);
372 hdev->asic_funcs->set_pll_profile(hdev, PLL_LAST);
374 INIT_DELAYED_WORK(&hdev->work_freq, set_freq_to_low_job);
375 schedule_delayed_work(&hdev->work_freq,
376 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
378 if (hdev->heartbeat) {
379 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
380 schedule_delayed_work(&hdev->work_heartbeat,
381 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
384 hdev->late_init_done = true;
390 * device_late_fini - finalize all that was done in device_late_init
392 * @hdev: pointer to habanalabs device structure
395 static void device_late_fini(struct hl_device *hdev)
397 if (!hdev->late_init_done)
400 cancel_delayed_work_sync(&hdev->work_freq);
402 cancel_delayed_work_sync(&hdev->work_heartbeat);
404 if (hdev->asic_funcs->late_fini)
405 hdev->asic_funcs->late_fini(hdev);
407 hdev->late_init_done = false;
411 * hl_device_set_frequency - set the frequency of the device
413 * @hdev: pointer to habanalabs device structure
414 * @freq: the new frequency value
416 * Change the frequency if needed. This function has no protection against
417 * concurrency, therefore it is assumed that the calling function has protected
418 * itself against the case of calling this function from multiple threads with
421 * Returns 0 if no change was done, otherwise returns 1
423 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq)
425 if ((hdev->pm_mng_profile == PM_MANUAL) ||
426 (hdev->curr_pll_profile == freq))
429 dev_dbg(hdev->dev, "Changing device frequency to %s\n",
430 freq == PLL_HIGH ? "high" : "low");
432 hdev->asic_funcs->set_pll_profile(hdev, freq);
434 hdev->curr_pll_profile = freq;
439 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable)
443 mutex_lock(&hdev->debug_lock);
446 if (!hdev->in_debug) {
448 "Failed to disable debug mode because device was not in debug mode\n");
453 hdev->asic_funcs->halt_coresight(hdev);
459 if (hdev->in_debug) {
461 "Failed to enable debug mode because device is already in debug mode\n");
469 mutex_unlock(&hdev->debug_lock);
475 * hl_device_suspend - initiate device suspend
477 * @hdev: pointer to habanalabs device structure
479 * Puts the hw in the suspend state (all asics).
480 * Returns 0 for success or an error on failure.
481 * Called at driver suspend.
483 int hl_device_suspend(struct hl_device *hdev)
487 pci_save_state(hdev->pdev);
489 /* Block future CS/VM/JOB completion operations */
490 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
492 dev_err(hdev->dev, "Can't suspend while in reset\n");
496 /* This blocks all other stuff that is not blocked by in_reset */
497 hdev->disabled = true;
500 * Flush anyone that is inside the critical section of enqueue
503 hdev->asic_funcs->hw_queues_lock(hdev);
504 hdev->asic_funcs->hw_queues_unlock(hdev);
506 /* Flush processes that are sending message to CPU */
507 mutex_lock(&hdev->send_cpu_message_lock);
508 mutex_unlock(&hdev->send_cpu_message_lock);
510 rc = hdev->asic_funcs->suspend(hdev);
513 "Failed to disable PCI access of device CPU\n");
515 /* Shut down the device */
516 pci_disable_device(hdev->pdev);
517 pci_set_power_state(hdev->pdev, PCI_D3hot);
523 * hl_device_resume - initiate device resume
525 * @hdev: pointer to habanalabs device structure
527 * Bring the hw back to operating state (all asics).
528 * Returns 0 for success or an error on failure.
529 * Called at driver resume.
531 int hl_device_resume(struct hl_device *hdev)
535 pci_set_power_state(hdev->pdev, PCI_D0);
536 pci_restore_state(hdev->pdev);
537 rc = pci_enable_device_mem(hdev->pdev);
540 "Failed to enable PCI device in resume\n");
544 pci_set_master(hdev->pdev);
546 rc = hdev->asic_funcs->resume(hdev);
548 dev_err(hdev->dev, "Failed to resume device after suspend\n");
553 hdev->disabled = false;
554 atomic_set(&hdev->in_reset, 0);
556 rc = hl_device_reset(hdev, true, false);
558 dev_err(hdev->dev, "Failed to reset device during resume\n");
565 pci_clear_master(hdev->pdev);
566 pci_disable_device(hdev->pdev);
571 static void device_kill_open_processes(struct hl_device *hdev)
573 u16 pending_total, pending_cnt;
574 struct hl_fpriv *hpriv;
575 struct task_struct *task = NULL;
578 pending_total = HL_PLDM_PENDING_RESET_PER_SEC;
580 pending_total = HL_PENDING_RESET_PER_SEC;
582 /* Giving time for user to close FD, and for processes that are inside
583 * hl_device_open to finish
585 if (!list_empty(&hdev->fpriv_list))
588 mutex_lock(&hdev->fpriv_list_lock);
590 /* This section must be protected because we are dereferencing
591 * pointers that are freed if the process exits
593 list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) {
594 task = get_pid_task(hpriv->taskpid, PIDTYPE_PID);
596 dev_info(hdev->dev, "Killing user process pid=%d\n",
598 send_sig(SIGKILL, task, 1);
599 usleep_range(1000, 10000);
601 put_task_struct(task);
605 mutex_unlock(&hdev->fpriv_list_lock);
607 /* We killed the open users, but because the driver cleans up after the
608 * user contexts are closed (e.g. mmu mappings), we need to wait again
609 * to make sure the cleaning phase is finished before continuing with
613 pending_cnt = pending_total;
615 while ((!list_empty(&hdev->fpriv_list)) && (pending_cnt)) {
617 "Waiting for all unmap operations to finish before hard reset\n");
624 if (!list_empty(&hdev->fpriv_list))
626 "Going to hard reset with open user contexts\n");
629 static void device_hard_reset_pending(struct work_struct *work)
631 struct hl_device_reset_work *device_reset_work =
632 container_of(work, struct hl_device_reset_work, reset_work);
633 struct hl_device *hdev = device_reset_work->hdev;
635 hl_device_reset(hdev, true, true);
637 kfree(device_reset_work);
641 * hl_device_reset - reset the device
643 * @hdev: pointer to habanalabs device structure
644 * @hard_reset: should we do hard reset to all engines or just reset the
645 * compute/dma engines
647 * Block future CS and wait for pending CS to be enqueued
649 * Flush all completions
650 * Re-initialize all internal data structures
651 * Call ASIC H/W init, late_init
655 * Returns 0 for success or an error on failure.
657 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
658 bool from_hard_reset_thread)
662 if (!hdev->init_done) {
664 "Can't reset before initialization is done\n");
669 * Prevent concurrency in this function - only one reset should be
670 * done at any given time. Only need to perform this if we didn't
671 * get from the dedicated hard reset thread
673 if (!from_hard_reset_thread) {
674 /* Block future CS/VM/JOB completion operations */
675 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
679 /* This also blocks future CS/VM/JOB completion operations */
680 hdev->disabled = true;
682 /* Flush anyone that is inside the critical section of enqueue
685 hdev->asic_funcs->hw_queues_lock(hdev);
686 hdev->asic_funcs->hw_queues_unlock(hdev);
688 /* Flush anyone that is inside device open */
689 mutex_lock(&hdev->fpriv_list_lock);
690 mutex_unlock(&hdev->fpriv_list_lock);
692 dev_err(hdev->dev, "Going to RESET device!\n");
696 if ((hard_reset) && (!from_hard_reset_thread)) {
697 struct hl_device_reset_work *device_reset_work;
699 hdev->hard_reset_pending = true;
701 device_reset_work = kzalloc(sizeof(*device_reset_work),
703 if (!device_reset_work) {
709 * Because the reset function can't run from interrupt or
710 * from heartbeat work, we need to call the reset function
711 * from a dedicated work
713 INIT_WORK(&device_reset_work->reset_work,
714 device_hard_reset_pending);
715 device_reset_work->hdev = hdev;
716 schedule_work(&device_reset_work->reset_work);
722 device_late_fini(hdev);
725 * Now that the heartbeat thread is closed, flush processes
726 * which are sending messages to CPU
728 mutex_lock(&hdev->send_cpu_message_lock);
729 mutex_unlock(&hdev->send_cpu_message_lock);
733 * Halt the engines and disable interrupts so we won't get any more
734 * completions from H/W and we won't have any accesses from the
735 * H/W to the host machine
737 hdev->asic_funcs->halt_engines(hdev, hard_reset);
739 /* Go over all the queues, release all CS and their jobs */
740 hl_cs_rollback_all(hdev);
742 /* Kill processes here after CS rollback. This is because the process
743 * can't really exit until all its CSs are done, which is what we
746 if (from_hard_reset_thread)
747 device_kill_open_processes(hdev);
749 /* Release kernel context */
750 if ((hard_reset) && (hl_ctx_put(hdev->kernel_ctx) == 1))
751 hdev->kernel_ctx = NULL;
753 /* Reset the H/W. It will be in idle state after this returns */
754 hdev->asic_funcs->hw_fini(hdev, hard_reset);
759 hl_eq_reset(hdev, &hdev->event_queue);
762 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
763 hl_hw_queue_reset(hdev, hard_reset);
764 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
765 hl_cq_reset(hdev, &hdev->completion_queue[i]);
767 mutex_lock(&hdev->fpriv_list_lock);
769 /* Make sure the context switch phase will run again */
770 if (hdev->compute_ctx) {
771 atomic_set(&hdev->compute_ctx->thread_ctx_switch_token, 1);
772 hdev->compute_ctx->thread_ctx_switch_wait_token = 0;
775 mutex_unlock(&hdev->fpriv_list_lock);
777 /* Finished tear-down, starting to re-initialize */
780 hdev->device_cpu_disabled = false;
781 hdev->hard_reset_pending = false;
783 if (hdev->kernel_ctx) {
785 "kernel ctx was alive during hard reset, something is terribly wrong\n");
790 rc = hl_mmu_init(hdev);
793 "Failed to initialize MMU S/W after hard reset\n");
797 /* Allocate the kernel context */
798 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
800 if (!hdev->kernel_ctx) {
805 hdev->compute_ctx = NULL;
807 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
810 "failed to init kernel ctx in hard reset\n");
811 kfree(hdev->kernel_ctx);
812 hdev->kernel_ctx = NULL;
817 rc = hdev->asic_funcs->hw_init(hdev);
820 "failed to initialize the H/W after reset\n");
824 hdev->disabled = false;
826 /* Check that the communication with the device is working */
827 rc = hdev->asic_funcs->test_queues(hdev);
830 "Failed to detect if device is alive after reset\n");
835 rc = device_late_init(hdev);
838 "Failed late init after hard reset\n");
842 rc = hl_vm_init(hdev);
845 "Failed to init memory module after hard reset\n");
849 hl_set_max_power(hdev, hdev->max_power);
851 rc = hdev->asic_funcs->soft_reset_late_init(hdev);
854 "Failed late init after soft reset\n");
859 atomic_set(&hdev->in_reset, 0);
862 hdev->hard_reset_cnt++;
864 hdev->soft_reset_cnt++;
869 hdev->disabled = true;
873 "Failed to reset! Device is NOT usable\n");
874 hdev->hard_reset_cnt++;
877 "Failed to do soft-reset, trying hard reset\n");
878 hdev->soft_reset_cnt++;
883 atomic_set(&hdev->in_reset, 0);
889 * hl_device_init - main initialization function for habanalabs device
891 * @hdev: pointer to habanalabs device structure
893 * Allocate an id for the device, do early initialization and then call the
894 * ASIC specific initialization functions. Finally, create the cdev and the
895 * Linux device to expose it to the user
897 int hl_device_init(struct hl_device *hdev, struct class *hclass)
899 int i, rc, cq_ready_cnt;
902 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id / 2);
907 rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops, name,
908 &hdev->cdev, &hdev->dev);
915 name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->id / 2);
921 /* Create control device */
922 rc = device_setup_cdev(hdev, hclass, hdev->id_control, &hl_ctrl_ops,
923 name, &hdev->cdev_ctrl, &hdev->dev_ctrl);
930 /* Initialize ASIC function pointers and perform early init */
931 rc = device_early_init(hdev);
933 goto release_control_device;
936 * Start calling ASIC initialization. First S/W then H/W and finally
939 rc = hdev->asic_funcs->sw_init(hdev);
944 * Initialize the H/W queues. Must be done before hw_init, because
945 * there the addresses of the kernel queue are being written to the
946 * registers of the device
948 rc = hl_hw_queues_create(hdev);
950 dev_err(hdev->dev, "failed to initialize kernel queues\n");
955 * Initialize the completion queues. Must be done before hw_init,
956 * because there the addresses of the completion queues are being
957 * passed as arguments to request_irq
959 hdev->completion_queue =
960 kcalloc(hdev->asic_prop.completion_queues_count,
961 sizeof(*hdev->completion_queue), GFP_KERNEL);
963 if (!hdev->completion_queue) {
964 dev_err(hdev->dev, "failed to allocate completion queues\n");
966 goto hw_queues_destroy;
969 for (i = 0, cq_ready_cnt = 0;
970 i < hdev->asic_prop.completion_queues_count;
971 i++, cq_ready_cnt++) {
972 rc = hl_cq_init(hdev, &hdev->completion_queue[i], i);
975 "failed to initialize completion queue\n");
981 * Initialize the event queue. Must be done before hw_init,
982 * because there the address of the event queue is being
983 * passed as argument to request_irq
985 rc = hl_eq_init(hdev, &hdev->event_queue);
987 dev_err(hdev->dev, "failed to initialize event queue\n");
991 /* MMU S/W must be initialized before kernel context is created */
992 rc = hl_mmu_init(hdev);
994 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n");
998 /* Allocate the kernel context */
999 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
1000 if (!hdev->kernel_ctx) {
1005 hdev->compute_ctx = NULL;
1007 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1009 dev_err(hdev->dev, "failed to initialize kernel context\n");
1010 kfree(hdev->kernel_ctx);
1014 rc = hl_cb_pool_init(hdev);
1016 dev_err(hdev->dev, "failed to initialize CB pool\n");
1020 rc = hl_sysfs_init(hdev);
1022 dev_err(hdev->dev, "failed to initialize sysfs\n");
1026 hl_debugfs_add_device(hdev);
1028 if (hdev->asic_funcs->get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
1030 "H/W state is dirty, must reset before initializing\n");
1031 hdev->asic_funcs->hw_fini(hdev, true);
1034 rc = hdev->asic_funcs->hw_init(hdev);
1036 dev_err(hdev->dev, "failed to initialize the H/W\n");
1041 hdev->disabled = false;
1043 /* Check that the communication with the device is working */
1044 rc = hdev->asic_funcs->test_queues(hdev);
1046 dev_err(hdev->dev, "Failed to detect if device is alive\n");
1051 rc = device_late_init(hdev);
1053 dev_err(hdev->dev, "Failed late initialization\n");
1058 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
1060 hdev->asic_prop.dram_size / 1024 / 1024 / 1024);
1062 rc = hl_vm_init(hdev);
1064 dev_err(hdev->dev, "Failed to initialize memory module\n");
1070 * hl_hwmon_init must be called after device_late_init, because only
1071 * there we get the information from the device about which
1072 * hwmon-related sensors the device supports
1074 rc = hl_hwmon_init(hdev);
1076 dev_err(hdev->dev, "Failed to initialize hwmon\n");
1081 dev_notice(hdev->dev,
1082 "Successfully added device to habanalabs driver\n");
1084 hdev->init_done = true;
1089 hl_cb_pool_fini(hdev);
1091 if (hl_ctx_put(hdev->kernel_ctx) != 1)
1093 "kernel ctx is still alive on initialization failure\n");
1097 hl_eq_fini(hdev, &hdev->event_queue);
1099 for (i = 0 ; i < cq_ready_cnt ; i++)
1100 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1101 kfree(hdev->completion_queue);
1103 hl_hw_queues_destroy(hdev);
1105 hdev->asic_funcs->sw_fini(hdev);
1107 device_early_fini(hdev);
1108 release_control_device:
1109 device_destroy(hclass, hdev->dev_ctrl->devt);
1110 cdev_del(&hdev->cdev_ctrl);
1112 device_destroy(hclass, hdev->dev->devt);
1113 cdev_del(&hdev->cdev);
1115 hdev->disabled = true;
1117 dev_err(&hdev->pdev->dev,
1118 "Failed to initialize hl%d. Device is NOT usable !\n",
1121 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
1128 * hl_device_fini - main tear-down function for habanalabs device
1130 * @hdev: pointer to habanalabs device structure
1132 * Destroy the device, call ASIC fini functions and release the id
1134 void hl_device_fini(struct hl_device *hdev)
1139 dev_info(hdev->dev, "Removing device\n");
1142 * This function is competing with the reset function, so try to
1143 * take the reset atomic and if we are already in middle of reset,
1144 * wait until reset function is finished. Reset function is designed
1145 * to always finish (could take up to a few seconds in worst case).
1148 timeout = ktime_add_us(ktime_get(),
1149 HL_PENDING_RESET_PER_SEC * 1000 * 1000 * 4);
1150 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1152 usleep_range(50, 200);
1153 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1154 if (ktime_compare(ktime_get(), timeout) > 0) {
1155 WARN(1, "Failed to remove device because reset function did not finish\n");
1160 /* Mark device as disabled */
1161 hdev->disabled = true;
1163 /* Flush anyone that is inside the critical section of enqueue
1166 hdev->asic_funcs->hw_queues_lock(hdev);
1167 hdev->asic_funcs->hw_queues_unlock(hdev);
1169 /* Flush anyone that is inside device open */
1170 mutex_lock(&hdev->fpriv_list_lock);
1171 mutex_unlock(&hdev->fpriv_list_lock);
1173 hdev->hard_reset_pending = true;
1175 hl_hwmon_fini(hdev);
1177 device_late_fini(hdev);
1179 hl_debugfs_remove_device(hdev);
1181 hl_sysfs_fini(hdev);
1184 * Halt the engines and disable interrupts so we won't get any more
1185 * completions from H/W and we won't have any accesses from the
1186 * H/W to the host machine
1188 hdev->asic_funcs->halt_engines(hdev, true);
1190 /* Go over all the queues, release all CS and their jobs */
1191 hl_cs_rollback_all(hdev);
1193 /* Kill processes here after CS rollback. This is because the process
1194 * can't really exit until all its CSs are done, which is what we
1197 device_kill_open_processes(hdev);
1199 hl_cb_pool_fini(hdev);
1201 /* Release kernel context */
1202 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
1203 dev_err(hdev->dev, "kernel ctx is still alive\n");
1205 /* Reset the H/W. It will be in idle state after this returns */
1206 hdev->asic_funcs->hw_fini(hdev, true);
1212 hl_eq_fini(hdev, &hdev->event_queue);
1214 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1215 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1216 kfree(hdev->completion_queue);
1218 hl_hw_queues_destroy(hdev);
1220 /* Call ASIC S/W finalize function */
1221 hdev->asic_funcs->sw_fini(hdev);
1223 device_early_fini(hdev);
1225 /* Hide device from user */
1226 device_destroy(hdev->dev_ctrl->class, hdev->dev_ctrl->devt);
1227 cdev_del(&hdev->cdev_ctrl);
1228 device_destroy(hdev->dev->class, hdev->dev->devt);
1229 cdev_del(&hdev->cdev);
1231 pr_info("removed device successfully\n");
1235 * MMIO register access helper functions.
1239 * hl_rreg - Read an MMIO register
1241 * @hdev: pointer to habanalabs device structure
1242 * @reg: MMIO register offset (in bytes)
1244 * Returns the value of the MMIO register we are asked to read
1247 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1249 return readl(hdev->rmmio + reg);
1253 * hl_wreg - Write to an MMIO register
1255 * @hdev: pointer to habanalabs device structure
1256 * @reg: MMIO register offset (in bytes)
1257 * @val: 32-bit value
1259 * Writes the 32-bit value into the MMIO register
1262 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1264 writel(val, hdev->rmmio + reg);