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;
46 hpriv = container_of(ref, struct hl_fpriv, refcount);
50 put_pid(hpriv->taskpid);
52 hl_debugfs_remove_file(hpriv);
54 mutex_destroy(&hpriv->restore_phase_mutex);
58 /* Now the FD is really closed */
59 atomic_dec(&hdev->fd_open_cnt);
61 /* This allows a new user context to open the device */
62 hdev->user_ctx = NULL;
65 void hl_hpriv_get(struct hl_fpriv *hpriv)
67 kref_get(&hpriv->refcount);
70 void hl_hpriv_put(struct hl_fpriv *hpriv)
72 kref_put(&hpriv->refcount, hpriv_release);
76 * hl_device_release - release function for habanalabs device
78 * @inode: pointer to inode structure
79 * @filp: pointer to file structure
81 * Called when process closes an habanalabs device
83 static int hl_device_release(struct inode *inode, struct file *filp)
85 struct hl_fpriv *hpriv = filp->private_data;
87 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
88 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
90 filp->private_data = NULL;
98 * hl_mmap - mmap function for habanalabs device
100 * @*filp: pointer to file structure
101 * @*vma: pointer to vm_area_struct of the process
103 * Called when process does an mmap on habanalabs device. Call the device's mmap
104 * function at the end of the common code.
106 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
108 struct hl_fpriv *hpriv = filp->private_data;
110 if ((vma->vm_pgoff & HL_MMAP_CB_MASK) == HL_MMAP_CB_MASK) {
111 vma->vm_pgoff ^= HL_MMAP_CB_MASK;
112 return hl_cb_mmap(hpriv, vma);
118 static const struct file_operations hl_ops = {
119 .owner = THIS_MODULE,
120 .open = hl_device_open,
121 .release = hl_device_release,
123 .unlocked_ioctl = hl_ioctl,
124 .compat_ioctl = hl_ioctl
128 * device_setup_cdev - setup cdev and device for habanalabs device
130 * @hdev: pointer to habanalabs device structure
131 * @hclass: pointer to the class object of the device
132 * @minor: minor number of the specific device
133 * @fpos : file operations to install for this device
135 * Create a cdev and a Linux device for habanalabs's device. Need to be
136 * called at the end of the habanalabs device initialization process,
137 * because this function exposes the device to the user
139 static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
140 int minor, const struct file_operations *fops)
142 int err, devno = MKDEV(hdev->major, minor);
143 struct cdev *hdev_cdev = &hdev->cdev;
146 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id);
150 cdev_init(hdev_cdev, fops);
151 hdev_cdev->owner = THIS_MODULE;
152 err = cdev_add(hdev_cdev, devno, 1);
154 pr_err("Failed to add char device %s\n", name);
158 hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
159 if (IS_ERR(hdev->dev)) {
160 pr_err("Failed to create device %s\n", name);
161 err = PTR_ERR(hdev->dev);
162 goto err_device_create;
165 dev_set_drvdata(hdev->dev, hdev);
179 * device_early_init - do some early initialization for the habanalabs device
181 * @hdev: pointer to habanalabs device structure
183 * Install the relevant function pointers and call the early_init function,
184 * if such a function exists
186 static int device_early_init(struct hl_device *hdev)
190 switch (hdev->asic_type) {
192 goya_set_asic_funcs(hdev);
193 strlcpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
196 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
201 rc = hdev->asic_funcs->early_init(hdev);
205 rc = hl_asid_init(hdev);
209 hdev->cq_wq = alloc_workqueue("hl-free-jobs", WQ_UNBOUND, 0);
210 if (hdev->cq_wq == NULL) {
211 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
216 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
217 if (hdev->eq_wq == NULL) {
218 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
223 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
225 if (!hdev->hl_chip_info) {
230 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
232 mutex_init(&hdev->fd_open_cnt_lock);
233 mutex_init(&hdev->send_cpu_message_lock);
234 mutex_init(&hdev->debug_lock);
235 mutex_init(&hdev->mmu_cache_lock);
236 INIT_LIST_HEAD(&hdev->hw_queues_mirror_list);
237 spin_lock_init(&hdev->hw_queues_mirror_lock);
238 atomic_set(&hdev->in_reset, 0);
239 atomic_set(&hdev->fd_open_cnt, 0);
240 atomic_set(&hdev->cs_active_cnt, 0);
245 destroy_workqueue(hdev->eq_wq);
247 destroy_workqueue(hdev->cq_wq);
251 if (hdev->asic_funcs->early_fini)
252 hdev->asic_funcs->early_fini(hdev);
258 * device_early_fini - finalize all that was done in device_early_init
260 * @hdev: pointer to habanalabs device structure
263 static void device_early_fini(struct hl_device *hdev)
265 mutex_destroy(&hdev->mmu_cache_lock);
266 mutex_destroy(&hdev->debug_lock);
267 mutex_destroy(&hdev->send_cpu_message_lock);
269 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
271 kfree(hdev->hl_chip_info);
273 destroy_workqueue(hdev->eq_wq);
274 destroy_workqueue(hdev->cq_wq);
278 if (hdev->asic_funcs->early_fini)
279 hdev->asic_funcs->early_fini(hdev);
281 mutex_destroy(&hdev->fd_open_cnt_lock);
284 static void set_freq_to_low_job(struct work_struct *work)
286 struct hl_device *hdev = container_of(work, struct hl_device,
289 if (atomic_read(&hdev->fd_open_cnt) == 0)
290 hl_device_set_frequency(hdev, PLL_LOW);
292 schedule_delayed_work(&hdev->work_freq,
293 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
296 static void hl_device_heartbeat(struct work_struct *work)
298 struct hl_device *hdev = container_of(work, struct hl_device,
299 work_heartbeat.work);
301 if (hl_device_disabled_or_in_reset(hdev))
304 if (!hdev->asic_funcs->send_heartbeat(hdev))
307 dev_err(hdev->dev, "Device heartbeat failed!\n");
308 hl_device_reset(hdev, true, false);
313 schedule_delayed_work(&hdev->work_heartbeat,
314 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
318 * device_late_init - do late stuff initialization for the habanalabs device
320 * @hdev: pointer to habanalabs device structure
322 * Do stuff that either needs the device H/W queues to be active or needs
323 * to happen after all the rest of the initialization is finished
325 static int device_late_init(struct hl_device *hdev)
329 if (hdev->asic_funcs->late_init) {
330 rc = hdev->asic_funcs->late_init(hdev);
333 "failed late initialization for the H/W\n");
338 hdev->high_pll = hdev->asic_prop.high_pll;
340 /* force setting to low frequency */
341 atomic_set(&hdev->curr_pll_profile, PLL_LOW);
343 if (hdev->pm_mng_profile == PM_AUTO)
344 hdev->asic_funcs->set_pll_profile(hdev, PLL_LOW);
346 hdev->asic_funcs->set_pll_profile(hdev, PLL_LAST);
348 INIT_DELAYED_WORK(&hdev->work_freq, set_freq_to_low_job);
349 schedule_delayed_work(&hdev->work_freq,
350 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
352 if (hdev->heartbeat) {
353 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
354 schedule_delayed_work(&hdev->work_heartbeat,
355 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
358 hdev->late_init_done = true;
364 * device_late_fini - finalize all that was done in device_late_init
366 * @hdev: pointer to habanalabs device structure
369 static void device_late_fini(struct hl_device *hdev)
371 if (!hdev->late_init_done)
374 cancel_delayed_work_sync(&hdev->work_freq);
376 cancel_delayed_work_sync(&hdev->work_heartbeat);
378 if (hdev->asic_funcs->late_fini)
379 hdev->asic_funcs->late_fini(hdev);
381 hdev->late_init_done = false;
385 * hl_device_set_frequency - set the frequency of the device
387 * @hdev: pointer to habanalabs device structure
388 * @freq: the new frequency value
390 * Change the frequency if needed.
391 * We allose to set PLL to low only if there is no user process
392 * Returns 0 if no change was done, otherwise returns 1;
394 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq)
396 enum hl_pll_frequency old_freq =
397 (freq == PLL_HIGH) ? PLL_LOW : PLL_HIGH;
400 if (hdev->pm_mng_profile == PM_MANUAL)
403 ret = atomic_cmpxchg(&hdev->curr_pll_profile, old_freq, freq);
408 * in case we want to lower frequency, check if device is not
409 * opened. We must have a check here to workaround race condition with
412 if ((freq == PLL_LOW) && (atomic_read(&hdev->fd_open_cnt) > 0)) {
413 atomic_set(&hdev->curr_pll_profile, PLL_HIGH);
417 dev_dbg(hdev->dev, "Changing device frequency to %s\n",
418 freq == PLL_HIGH ? "high" : "low");
420 hdev->asic_funcs->set_pll_profile(hdev, freq);
425 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable)
429 mutex_lock(&hdev->debug_lock);
432 if (!hdev->in_debug) {
434 "Failed to disable debug mode because device was not in debug mode\n");
439 hdev->asic_funcs->halt_coresight(hdev);
445 if (hdev->in_debug) {
447 "Failed to enable debug mode because device is already in debug mode\n");
452 mutex_lock(&hdev->fd_open_cnt_lock);
454 if (atomic_read(&hdev->fd_open_cnt) > 1) {
456 "Failed to enable debug mode. More then a single user is using the device\n");
458 goto unlock_fd_open_lock;
464 mutex_unlock(&hdev->fd_open_cnt_lock);
466 mutex_unlock(&hdev->debug_lock);
472 * hl_device_suspend - initiate device suspend
474 * @hdev: pointer to habanalabs device structure
476 * Puts the hw in the suspend state (all asics).
477 * Returns 0 for success or an error on failure.
478 * Called at driver suspend.
480 int hl_device_suspend(struct hl_device *hdev)
484 pci_save_state(hdev->pdev);
486 /* Block future CS/VM/JOB completion operations */
487 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
489 dev_err(hdev->dev, "Can't suspend while in reset\n");
493 /* This blocks all other stuff that is not blocked by in_reset */
494 hdev->disabled = true;
497 * Flush anyone that is inside the critical section of enqueue
500 hdev->asic_funcs->hw_queues_lock(hdev);
501 hdev->asic_funcs->hw_queues_unlock(hdev);
503 /* Flush processes that are sending message to CPU */
504 mutex_lock(&hdev->send_cpu_message_lock);
505 mutex_unlock(&hdev->send_cpu_message_lock);
507 rc = hdev->asic_funcs->suspend(hdev);
510 "Failed to disable PCI access of device CPU\n");
512 /* Shut down the device */
513 pci_disable_device(hdev->pdev);
514 pci_set_power_state(hdev->pdev, PCI_D3hot);
520 * hl_device_resume - initiate device resume
522 * @hdev: pointer to habanalabs device structure
524 * Bring the hw back to operating state (all asics).
525 * Returns 0 for success or an error on failure.
526 * Called at driver resume.
528 int hl_device_resume(struct hl_device *hdev)
532 pci_set_power_state(hdev->pdev, PCI_D0);
533 pci_restore_state(hdev->pdev);
534 rc = pci_enable_device_mem(hdev->pdev);
537 "Failed to enable PCI device in resume\n");
541 pci_set_master(hdev->pdev);
543 rc = hdev->asic_funcs->resume(hdev);
545 dev_err(hdev->dev, "Failed to resume device after suspend\n");
550 hdev->disabled = false;
551 atomic_set(&hdev->in_reset, 0);
553 rc = hl_device_reset(hdev, true, false);
555 dev_err(hdev->dev, "Failed to reset device during resume\n");
562 pci_clear_master(hdev->pdev);
563 pci_disable_device(hdev->pdev);
568 static void device_kill_open_processes(struct hl_device *hdev)
570 u16 pending_total, pending_cnt;
571 struct task_struct *task = NULL;
574 pending_total = HL_PLDM_PENDING_RESET_PER_SEC;
576 pending_total = HL_PENDING_RESET_PER_SEC;
578 pending_cnt = pending_total;
580 /* Flush all processes that are inside hl_open */
581 mutex_lock(&hdev->fd_open_cnt_lock);
583 while ((atomic_read(&hdev->fd_open_cnt)) && (pending_cnt)) {
588 "Can't HARD reset, waiting for user to close FD\n");
592 if (atomic_read(&hdev->fd_open_cnt)) {
593 task = get_pid_task(hdev->user_ctx->hpriv->taskpid,
596 dev_info(hdev->dev, "Killing user processes\n");
597 send_sig(SIGKILL, task, 1);
600 put_task_struct(task);
604 /* We killed the open users, but because the driver cleans up after the
605 * user contexts are closed (e.g. mmu mappings), we need to wait again
606 * to make sure the cleaning phase is finished before continuing with
610 pending_cnt = pending_total;
612 while ((atomic_read(&hdev->fd_open_cnt)) && (pending_cnt)) {
619 if (atomic_read(&hdev->fd_open_cnt))
621 "Going to hard reset with open user contexts\n");
623 mutex_unlock(&hdev->fd_open_cnt_lock);
627 static void device_hard_reset_pending(struct work_struct *work)
629 struct hl_device_reset_work *device_reset_work =
630 container_of(work, struct hl_device_reset_work, reset_work);
631 struct hl_device *hdev = device_reset_work->hdev;
633 device_kill_open_processes(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;
683 * Flush anyone that is inside the critical section of enqueue
686 hdev->asic_funcs->hw_queues_lock(hdev);
687 hdev->asic_funcs->hw_queues_unlock(hdev);
689 dev_err(hdev->dev, "Going to RESET device!\n");
693 if ((hard_reset) && (!from_hard_reset_thread)) {
694 struct hl_device_reset_work *device_reset_work;
696 hdev->hard_reset_pending = true;
698 device_reset_work = kzalloc(sizeof(*device_reset_work),
700 if (!device_reset_work) {
706 * Because the reset function can't run from interrupt or
707 * from heartbeat work, we need to call the reset function
708 * from a dedicated work
710 INIT_WORK(&device_reset_work->reset_work,
711 device_hard_reset_pending);
712 device_reset_work->hdev = hdev;
713 schedule_work(&device_reset_work->reset_work);
719 device_late_fini(hdev);
722 * Now that the heartbeat thread is closed, flush processes
723 * which are sending messages to CPU
725 mutex_lock(&hdev->send_cpu_message_lock);
726 mutex_unlock(&hdev->send_cpu_message_lock);
730 * Halt the engines and disable interrupts so we won't get any more
731 * completions from H/W and we won't have any accesses from the
732 * H/W to the host machine
734 hdev->asic_funcs->halt_engines(hdev, hard_reset);
736 /* Go over all the queues, release all CS and their jobs */
737 hl_cs_rollback_all(hdev);
739 /* Release kernel context */
740 if ((hard_reset) && (hl_ctx_put(hdev->kernel_ctx) == 1))
741 hdev->kernel_ctx = NULL;
743 /* Reset the H/W. It will be in idle state after this returns */
744 hdev->asic_funcs->hw_fini(hdev, hard_reset);
749 hl_eq_reset(hdev, &hdev->event_queue);
752 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
753 hl_hw_queue_reset(hdev, hard_reset);
754 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
755 hl_cq_reset(hdev, &hdev->completion_queue[i]);
757 /* Make sure the context switch phase will run again */
758 if (hdev->user_ctx) {
759 atomic_set(&hdev->user_ctx->thread_ctx_switch_token, 1);
760 hdev->user_ctx->thread_ctx_switch_wait_token = 0;
763 /* Finished tear-down, starting to re-initialize */
766 hdev->device_cpu_disabled = false;
767 hdev->hard_reset_pending = false;
769 if (hdev->kernel_ctx) {
771 "kernel ctx was alive during hard reset, something is terribly wrong\n");
776 rc = hl_mmu_init(hdev);
779 "Failed to initialize MMU S/W after hard reset\n");
783 /* Allocate the kernel context */
784 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
786 if (!hdev->kernel_ctx) {
791 hdev->user_ctx = NULL;
793 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
796 "failed to init kernel ctx in hard reset\n");
797 kfree(hdev->kernel_ctx);
798 hdev->kernel_ctx = NULL;
803 rc = hdev->asic_funcs->hw_init(hdev);
806 "failed to initialize the H/W after reset\n");
810 hdev->disabled = false;
812 /* Check that the communication with the device is working */
813 rc = hdev->asic_funcs->test_queues(hdev);
816 "Failed to detect if device is alive after reset\n");
821 rc = device_late_init(hdev);
824 "Failed late init after hard reset\n");
828 rc = hl_vm_init(hdev);
831 "Failed to init memory module after hard reset\n");
835 hl_set_max_power(hdev, hdev->max_power);
837 rc = hdev->asic_funcs->soft_reset_late_init(hdev);
840 "Failed late init after soft reset\n");
845 atomic_set(&hdev->in_reset, 0);
848 hdev->hard_reset_cnt++;
850 hdev->soft_reset_cnt++;
855 hdev->disabled = true;
859 "Failed to reset! Device is NOT usable\n");
860 hdev->hard_reset_cnt++;
863 "Failed to do soft-reset, trying hard reset\n");
864 hdev->soft_reset_cnt++;
869 atomic_set(&hdev->in_reset, 0);
875 * hl_device_init - main initialization function for habanalabs device
877 * @hdev: pointer to habanalabs device structure
879 * Allocate an id for the device, do early initialization and then call the
880 * ASIC specific initialization functions. Finally, create the cdev and the
881 * Linux device to expose it to the user
883 int hl_device_init(struct hl_device *hdev, struct class *hclass)
885 int i, rc, cq_ready_cnt;
888 rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops);
893 /* Initialize ASIC function pointers and perform early init */
894 rc = device_early_init(hdev);
899 * Start calling ASIC initialization. First S/W then H/W and finally
902 rc = hdev->asic_funcs->sw_init(hdev);
907 * Initialize the H/W queues. Must be done before hw_init, because
908 * there the addresses of the kernel queue are being written to the
909 * registers of the device
911 rc = hl_hw_queues_create(hdev);
913 dev_err(hdev->dev, "failed to initialize kernel queues\n");
918 * Initialize the completion queues. Must be done before hw_init,
919 * because there the addresses of the completion queues are being
920 * passed as arguments to request_irq
922 hdev->completion_queue =
923 kcalloc(hdev->asic_prop.completion_queues_count,
924 sizeof(*hdev->completion_queue), GFP_KERNEL);
926 if (!hdev->completion_queue) {
927 dev_err(hdev->dev, "failed to allocate completion queues\n");
929 goto hw_queues_destroy;
932 for (i = 0, cq_ready_cnt = 0;
933 i < hdev->asic_prop.completion_queues_count;
934 i++, cq_ready_cnt++) {
935 rc = hl_cq_init(hdev, &hdev->completion_queue[i], i);
938 "failed to initialize completion queue\n");
944 * Initialize the event queue. Must be done before hw_init,
945 * because there the address of the event queue is being
946 * passed as argument to request_irq
948 rc = hl_eq_init(hdev, &hdev->event_queue);
950 dev_err(hdev->dev, "failed to initialize event queue\n");
954 /* MMU S/W must be initialized before kernel context is created */
955 rc = hl_mmu_init(hdev);
957 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n");
961 /* Allocate the kernel context */
962 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
963 if (!hdev->kernel_ctx) {
968 hdev->user_ctx = NULL;
970 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
972 dev_err(hdev->dev, "failed to initialize kernel context\n");
973 kfree(hdev->kernel_ctx);
977 rc = hl_cb_pool_init(hdev);
979 dev_err(hdev->dev, "failed to initialize CB pool\n");
983 rc = hl_sysfs_init(hdev);
985 dev_err(hdev->dev, "failed to initialize sysfs\n");
989 hl_debugfs_add_device(hdev);
991 if (hdev->asic_funcs->get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
993 "H/W state is dirty, must reset before initializing\n");
994 hdev->asic_funcs->hw_fini(hdev, true);
997 rc = hdev->asic_funcs->hw_init(hdev);
999 dev_err(hdev->dev, "failed to initialize the H/W\n");
1004 hdev->disabled = false;
1006 /* Check that the communication with the device is working */
1007 rc = hdev->asic_funcs->test_queues(hdev);
1009 dev_err(hdev->dev, "Failed to detect if device is alive\n");
1014 rc = device_late_init(hdev);
1016 dev_err(hdev->dev, "Failed late initialization\n");
1021 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
1023 hdev->asic_prop.dram_size / 1024 / 1024 / 1024);
1025 rc = hl_vm_init(hdev);
1027 dev_err(hdev->dev, "Failed to initialize memory module\n");
1033 * hl_hwmon_init must be called after device_late_init, because only
1034 * there we get the information from the device about which
1035 * hwmon-related sensors the device supports
1037 rc = hl_hwmon_init(hdev);
1039 dev_err(hdev->dev, "Failed to initialize hwmon\n");
1044 dev_notice(hdev->dev,
1045 "Successfully added device to habanalabs driver\n");
1047 hdev->init_done = true;
1052 hl_cb_pool_fini(hdev);
1054 if (hl_ctx_put(hdev->kernel_ctx) != 1)
1056 "kernel ctx is still alive on initialization failure\n");
1060 hl_eq_fini(hdev, &hdev->event_queue);
1062 for (i = 0 ; i < cq_ready_cnt ; i++)
1063 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1064 kfree(hdev->completion_queue);
1066 hl_hw_queues_destroy(hdev);
1068 hdev->asic_funcs->sw_fini(hdev);
1070 device_early_fini(hdev);
1072 device_destroy(hclass, hdev->dev->devt);
1073 cdev_del(&hdev->cdev);
1075 hdev->disabled = true;
1077 dev_err(&hdev->pdev->dev,
1078 "Failed to initialize hl%d. Device is NOT usable !\n",
1081 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
1088 * hl_device_fini - main tear-down function for habanalabs device
1090 * @hdev: pointer to habanalabs device structure
1092 * Destroy the device, call ASIC fini functions and release the id
1094 void hl_device_fini(struct hl_device *hdev)
1099 dev_info(hdev->dev, "Removing device\n");
1102 * This function is competing with the reset function, so try to
1103 * take the reset atomic and if we are already in middle of reset,
1104 * wait until reset function is finished. Reset function is designed
1105 * to always finish (could take up to a few seconds in worst case).
1108 timeout = ktime_add_us(ktime_get(),
1109 HL_PENDING_RESET_PER_SEC * 1000 * 1000 * 4);
1110 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1112 usleep_range(50, 200);
1113 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1114 if (ktime_compare(ktime_get(), timeout) > 0) {
1115 WARN(1, "Failed to remove device because reset function did not finish\n");
1120 /* Mark device as disabled */
1121 hdev->disabled = true;
1124 * Flush anyone that is inside the critical section of enqueue
1127 hdev->asic_funcs->hw_queues_lock(hdev);
1128 hdev->asic_funcs->hw_queues_unlock(hdev);
1130 hdev->hard_reset_pending = true;
1132 device_kill_open_processes(hdev);
1134 hl_hwmon_fini(hdev);
1136 device_late_fini(hdev);
1138 hl_debugfs_remove_device(hdev);
1140 hl_sysfs_fini(hdev);
1143 * Halt the engines and disable interrupts so we won't get any more
1144 * completions from H/W and we won't have any accesses from the
1145 * H/W to the host machine
1147 hdev->asic_funcs->halt_engines(hdev, true);
1149 /* Go over all the queues, release all CS and their jobs */
1150 hl_cs_rollback_all(hdev);
1152 hl_cb_pool_fini(hdev);
1154 /* Release kernel context */
1155 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
1156 dev_err(hdev->dev, "kernel ctx is still alive\n");
1158 /* Reset the H/W. It will be in idle state after this returns */
1159 hdev->asic_funcs->hw_fini(hdev, true);
1165 hl_eq_fini(hdev, &hdev->event_queue);
1167 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1168 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1169 kfree(hdev->completion_queue);
1171 hl_hw_queues_destroy(hdev);
1173 /* Call ASIC S/W finalize function */
1174 hdev->asic_funcs->sw_fini(hdev);
1176 device_early_fini(hdev);
1178 /* Hide device from user */
1179 device_destroy(hdev->dev->class, hdev->dev->devt);
1180 cdev_del(&hdev->cdev);
1182 pr_info("removed device successfully\n");
1186 * MMIO register access helper functions.
1190 * hl_rreg - Read an MMIO register
1192 * @hdev: pointer to habanalabs device structure
1193 * @reg: MMIO register offset (in bytes)
1195 * Returns the value of the MMIO register we are asked to read
1198 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1200 return readl(hdev->rmmio + reg);
1204 * hl_wreg - Write to an MMIO register
1206 * @hdev: pointer to habanalabs device structure
1207 * @reg: MMIO register offset (in bytes)
1208 * @val: 32-bit value
1210 * Writes the 32-bit value into the MMIO register
1213 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1215 writel(val, hdev->rmmio + reg);