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);
56 mutex_lock(&hdev->fpriv_list_lock);
57 list_del(&hpriv->dev_node);
58 hdev->compute_ctx = NULL;
59 mutex_unlock(&hdev->fpriv_list_lock);
64 void hl_hpriv_get(struct hl_fpriv *hpriv)
66 kref_get(&hpriv->refcount);
69 void hl_hpriv_put(struct hl_fpriv *hpriv)
71 kref_put(&hpriv->refcount, hpriv_release);
75 * hl_device_release - release function for habanalabs device
77 * @inode: pointer to inode structure
78 * @filp: pointer to file structure
80 * Called when process closes an habanalabs device
82 static int hl_device_release(struct inode *inode, struct file *filp)
84 struct hl_fpriv *hpriv = filp->private_data;
86 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
87 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
89 filp->private_data = NULL;
96 static int hl_device_release_ctrl(struct inode *inode, struct file *filp)
98 struct hl_fpriv *hpriv = filp->private_data;
99 struct hl_device *hdev;
101 filp->private_data = NULL;
105 mutex_lock(&hdev->fpriv_list_lock);
106 list_del(&hpriv->dev_node);
107 mutex_unlock(&hdev->fpriv_list_lock);
115 * hl_mmap - mmap function for habanalabs device
117 * @*filp: pointer to file structure
118 * @*vma: pointer to vm_area_struct of the process
120 * Called when process does an mmap on habanalabs device. Call the device's mmap
121 * function at the end of the common code.
123 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
125 struct hl_fpriv *hpriv = filp->private_data;
127 if ((vma->vm_pgoff & HL_MMAP_CB_MASK) == HL_MMAP_CB_MASK) {
128 vma->vm_pgoff ^= HL_MMAP_CB_MASK;
129 return hl_cb_mmap(hpriv, vma);
135 static const struct file_operations hl_ops = {
136 .owner = THIS_MODULE,
137 .open = hl_device_open,
138 .release = hl_device_release,
140 .unlocked_ioctl = hl_ioctl,
141 .compat_ioctl = hl_ioctl
144 static const struct file_operations hl_ctrl_ops = {
145 .owner = THIS_MODULE,
146 .open = hl_device_open_ctrl,
147 .release = hl_device_release_ctrl,
148 .unlocked_ioctl = hl_ioctl_control,
149 .compat_ioctl = hl_ioctl_control
152 static void device_release_func(struct device *dev)
158 * device_init_cdev - Initialize cdev and device for habanalabs device
160 * @hdev: pointer to habanalabs device structure
161 * @hclass: pointer to the class object of the device
162 * @minor: minor number of the specific device
163 * @fpos: file operations to install for this device
164 * @name: name of the device as it will appear in the filesystem
165 * @cdev: pointer to the char device object that will be initialized
166 * @dev: pointer to the device object that will be initialized
168 * Initialize a cdev and a Linux device for habanalabs's device.
170 static int device_init_cdev(struct hl_device *hdev, struct class *hclass,
171 int minor, const struct file_operations *fops,
172 char *name, struct cdev *cdev,
175 cdev_init(cdev, fops);
176 cdev->owner = THIS_MODULE;
178 *dev = kzalloc(sizeof(**dev), GFP_KERNEL);
182 device_initialize(*dev);
183 (*dev)->devt = MKDEV(hdev->major, minor);
184 (*dev)->class = hclass;
185 (*dev)->release = device_release_func;
186 dev_set_drvdata(*dev, hdev);
187 dev_set_name(*dev, "%s", name);
192 static int device_cdev_sysfs_add(struct hl_device *hdev)
196 rc = cdev_device_add(&hdev->cdev, hdev->dev);
199 "failed to add a char device to the system\n");
203 rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl);
206 "failed to add a control char device to the system\n");
207 goto delete_cdev_device;
210 /* hl_sysfs_init() must be done after adding the device to the system */
211 rc = hl_sysfs_init(hdev);
213 dev_err(hdev->dev, "failed to initialize sysfs\n");
214 goto delete_ctrl_cdev_device;
217 hdev->cdev_sysfs_created = true;
221 delete_ctrl_cdev_device:
222 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
224 cdev_device_del(&hdev->cdev, hdev->dev);
228 static void device_cdev_sysfs_del(struct hl_device *hdev)
230 /* device_release() won't be called so must free devices explicitly */
231 if (!hdev->cdev_sysfs_created) {
232 kfree(hdev->dev_ctrl);
238 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
239 cdev_device_del(&hdev->cdev, hdev->dev);
243 * device_early_init - do some early initialization for the habanalabs device
245 * @hdev: pointer to habanalabs device structure
247 * Install the relevant function pointers and call the early_init function,
248 * if such a function exists
250 static int device_early_init(struct hl_device *hdev)
255 switch (hdev->asic_type) {
257 goya_set_asic_funcs(hdev);
258 strlcpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
261 gaudi_set_asic_funcs(hdev);
262 sprintf(hdev->asic_name, "GAUDI");
265 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
270 rc = hdev->asic_funcs->early_init(hdev);
274 rc = hl_asid_init(hdev);
278 if (hdev->asic_prop.completion_queues_count) {
279 hdev->cq_wq = kcalloc(hdev->asic_prop.completion_queues_count,
280 sizeof(*hdev->cq_wq),
288 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) {
289 snprintf(workq_name, 32, "hl-free-jobs-%u", i);
290 hdev->cq_wq[i] = create_singlethread_workqueue(workq_name);
291 if (hdev->cq_wq[i] == NULL) {
292 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
298 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
299 if (hdev->eq_wq == NULL) {
300 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
305 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
307 if (!hdev->hl_chip_info) {
312 hdev->idle_busy_ts_arr = kmalloc_array(HL_IDLE_BUSY_TS_ARR_SIZE,
313 sizeof(struct hl_device_idle_busy_ts),
314 (GFP_KERNEL | __GFP_ZERO));
315 if (!hdev->idle_busy_ts_arr) {
320 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
322 mutex_init(&hdev->send_cpu_message_lock);
323 mutex_init(&hdev->debug_lock);
324 mutex_init(&hdev->mmu_cache_lock);
325 INIT_LIST_HEAD(&hdev->hw_queues_mirror_list);
326 spin_lock_init(&hdev->hw_queues_mirror_lock);
327 INIT_LIST_HEAD(&hdev->fpriv_list);
328 mutex_init(&hdev->fpriv_list_lock);
329 atomic_set(&hdev->in_reset, 0);
334 kfree(hdev->hl_chip_info);
336 destroy_workqueue(hdev->eq_wq);
338 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
340 destroy_workqueue(hdev->cq_wq[i]);
345 if (hdev->asic_funcs->early_fini)
346 hdev->asic_funcs->early_fini(hdev);
352 * device_early_fini - finalize all that was done in device_early_init
354 * @hdev: pointer to habanalabs device structure
357 static void device_early_fini(struct hl_device *hdev)
361 mutex_destroy(&hdev->mmu_cache_lock);
362 mutex_destroy(&hdev->debug_lock);
363 mutex_destroy(&hdev->send_cpu_message_lock);
365 mutex_destroy(&hdev->fpriv_list_lock);
367 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
369 kfree(hdev->idle_busy_ts_arr);
370 kfree(hdev->hl_chip_info);
372 destroy_workqueue(hdev->eq_wq);
374 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
375 destroy_workqueue(hdev->cq_wq[i]);
380 if (hdev->asic_funcs->early_fini)
381 hdev->asic_funcs->early_fini(hdev);
384 static void set_freq_to_low_job(struct work_struct *work)
386 struct hl_device *hdev = container_of(work, struct hl_device,
389 mutex_lock(&hdev->fpriv_list_lock);
391 if (!hdev->compute_ctx)
392 hl_device_set_frequency(hdev, PLL_LOW);
394 mutex_unlock(&hdev->fpriv_list_lock);
396 schedule_delayed_work(&hdev->work_freq,
397 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
400 static void hl_device_heartbeat(struct work_struct *work)
402 struct hl_device *hdev = container_of(work, struct hl_device,
403 work_heartbeat.work);
405 if (hl_device_disabled_or_in_reset(hdev))
408 if (!hdev->asic_funcs->send_heartbeat(hdev))
411 dev_err(hdev->dev, "Device heartbeat failed!\n");
412 hl_device_reset(hdev, true, false);
417 schedule_delayed_work(&hdev->work_heartbeat,
418 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
422 * device_late_init - do late stuff initialization for the habanalabs device
424 * @hdev: pointer to habanalabs device structure
426 * Do stuff that either needs the device H/W queues to be active or needs
427 * to happen after all the rest of the initialization is finished
429 static int device_late_init(struct hl_device *hdev)
433 if (hdev->asic_funcs->late_init) {
434 rc = hdev->asic_funcs->late_init(hdev);
437 "failed late initialization for the H/W\n");
442 hdev->high_pll = hdev->asic_prop.high_pll;
444 /* force setting to low frequency */
445 hdev->curr_pll_profile = PLL_LOW;
447 if (hdev->pm_mng_profile == PM_AUTO)
448 hdev->asic_funcs->set_pll_profile(hdev, PLL_LOW);
450 hdev->asic_funcs->set_pll_profile(hdev, PLL_LAST);
452 INIT_DELAYED_WORK(&hdev->work_freq, set_freq_to_low_job);
453 schedule_delayed_work(&hdev->work_freq,
454 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
456 if (hdev->heartbeat) {
457 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
458 schedule_delayed_work(&hdev->work_heartbeat,
459 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
462 hdev->late_init_done = true;
468 * device_late_fini - finalize all that was done in device_late_init
470 * @hdev: pointer to habanalabs device structure
473 static void device_late_fini(struct hl_device *hdev)
475 if (!hdev->late_init_done)
478 cancel_delayed_work_sync(&hdev->work_freq);
480 cancel_delayed_work_sync(&hdev->work_heartbeat);
482 if (hdev->asic_funcs->late_fini)
483 hdev->asic_funcs->late_fini(hdev);
485 hdev->late_init_done = false;
488 uint32_t hl_device_utilization(struct hl_device *hdev, uint32_t period_ms)
490 struct hl_device_idle_busy_ts *ts;
491 ktime_t zero_ktime, curr = ktime_get();
492 u32 overlap_cnt = 0, last_index = hdev->idle_busy_ts_idx;
493 s64 period_us, last_start_us, last_end_us, last_busy_time_us,
494 total_busy_time_us = 0, total_busy_time_ms;
496 zero_ktime = ktime_set(0, 0);
497 period_us = period_ms * USEC_PER_MSEC;
498 ts = &hdev->idle_busy_ts_arr[last_index];
500 /* check case that device is currently in idle */
501 if (!ktime_compare(ts->busy_to_idle_ts, zero_ktime) &&
502 !ktime_compare(ts->idle_to_busy_ts, zero_ktime)) {
505 /* Handle case idle_busy_ts_idx was 0 */
506 if (last_index > HL_IDLE_BUSY_TS_ARR_SIZE)
507 last_index = HL_IDLE_BUSY_TS_ARR_SIZE - 1;
509 ts = &hdev->idle_busy_ts_arr[last_index];
512 while (overlap_cnt < HL_IDLE_BUSY_TS_ARR_SIZE) {
513 /* Check if we are in last sample case. i.e. if the sample
514 * begun before the sampling period. This could be a real
515 * sample or 0 so need to handle both cases
517 last_start_us = ktime_to_us(
518 ktime_sub(curr, ts->idle_to_busy_ts));
520 if (last_start_us > period_us) {
522 /* First check two cases:
523 * 1. If the device is currently busy
524 * 2. If the device was idle during the whole sampling
528 if (!ktime_compare(ts->busy_to_idle_ts, zero_ktime)) {
529 /* Check if the device is currently busy */
530 if (ktime_compare(ts->idle_to_busy_ts,
534 /* We either didn't have any activity or we
535 * reached an entry which is 0. Either way,
536 * exit and return what was accumulated so far
541 /* If sample has finished, check it is relevant */
542 last_end_us = ktime_to_us(
543 ktime_sub(curr, ts->busy_to_idle_ts));
545 if (last_end_us > period_us)
548 /* It is relevant so add it but with adjustment */
549 last_busy_time_us = ktime_to_us(
550 ktime_sub(ts->busy_to_idle_ts,
551 ts->idle_to_busy_ts));
552 total_busy_time_us += last_busy_time_us -
553 (last_start_us - period_us);
557 /* Check if the sample is finished or still open */
558 if (ktime_compare(ts->busy_to_idle_ts, zero_ktime))
559 last_busy_time_us = ktime_to_us(
560 ktime_sub(ts->busy_to_idle_ts,
561 ts->idle_to_busy_ts));
563 last_busy_time_us = ktime_to_us(
564 ktime_sub(curr, ts->idle_to_busy_ts));
566 total_busy_time_us += last_busy_time_us;
569 /* Handle case idle_busy_ts_idx was 0 */
570 if (last_index > HL_IDLE_BUSY_TS_ARR_SIZE)
571 last_index = HL_IDLE_BUSY_TS_ARR_SIZE - 1;
573 ts = &hdev->idle_busy_ts_arr[last_index];
578 total_busy_time_ms = DIV_ROUND_UP_ULL(total_busy_time_us,
581 return DIV_ROUND_UP_ULL(total_busy_time_ms * 100, period_ms);
585 * hl_device_set_frequency - set the frequency of the device
587 * @hdev: pointer to habanalabs device structure
588 * @freq: the new frequency value
590 * Change the frequency if needed. This function has no protection against
591 * concurrency, therefore it is assumed that the calling function has protected
592 * itself against the case of calling this function from multiple threads with
595 * Returns 0 if no change was done, otherwise returns 1
597 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq)
599 if ((hdev->pm_mng_profile == PM_MANUAL) ||
600 (hdev->curr_pll_profile == freq))
603 dev_dbg(hdev->dev, "Changing device frequency to %s\n",
604 freq == PLL_HIGH ? "high" : "low");
606 hdev->asic_funcs->set_pll_profile(hdev, freq);
608 hdev->curr_pll_profile = freq;
613 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable)
617 mutex_lock(&hdev->debug_lock);
620 if (!hdev->in_debug) {
622 "Failed to disable debug mode because device was not in debug mode\n");
627 if (!hdev->hard_reset_pending)
628 hdev->asic_funcs->halt_coresight(hdev);
632 if (!hdev->hard_reset_pending)
633 hdev->asic_funcs->set_clock_gating(hdev);
638 if (hdev->in_debug) {
640 "Failed to enable debug mode because device is already in debug mode\n");
645 hdev->asic_funcs->disable_clock_gating(hdev);
649 mutex_unlock(&hdev->debug_lock);
655 * hl_device_suspend - initiate device suspend
657 * @hdev: pointer to habanalabs device structure
659 * Puts the hw in the suspend state (all asics).
660 * Returns 0 for success or an error on failure.
661 * Called at driver suspend.
663 int hl_device_suspend(struct hl_device *hdev)
667 pci_save_state(hdev->pdev);
669 /* Block future CS/VM/JOB completion operations */
670 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
672 dev_err(hdev->dev, "Can't suspend while in reset\n");
676 /* This blocks all other stuff that is not blocked by in_reset */
677 hdev->disabled = true;
680 * Flush anyone that is inside the critical section of enqueue
683 hdev->asic_funcs->hw_queues_lock(hdev);
684 hdev->asic_funcs->hw_queues_unlock(hdev);
686 /* Flush processes that are sending message to CPU */
687 mutex_lock(&hdev->send_cpu_message_lock);
688 mutex_unlock(&hdev->send_cpu_message_lock);
690 rc = hdev->asic_funcs->suspend(hdev);
693 "Failed to disable PCI access of device CPU\n");
695 /* Shut down the device */
696 pci_disable_device(hdev->pdev);
697 pci_set_power_state(hdev->pdev, PCI_D3hot);
703 * hl_device_resume - initiate device resume
705 * @hdev: pointer to habanalabs device structure
707 * Bring the hw back to operating state (all asics).
708 * Returns 0 for success or an error on failure.
709 * Called at driver resume.
711 int hl_device_resume(struct hl_device *hdev)
715 pci_set_power_state(hdev->pdev, PCI_D0);
716 pci_restore_state(hdev->pdev);
717 rc = pci_enable_device_mem(hdev->pdev);
720 "Failed to enable PCI device in resume\n");
724 pci_set_master(hdev->pdev);
726 rc = hdev->asic_funcs->resume(hdev);
728 dev_err(hdev->dev, "Failed to resume device after suspend\n");
733 hdev->disabled = false;
734 atomic_set(&hdev->in_reset, 0);
736 rc = hl_device_reset(hdev, true, false);
738 dev_err(hdev->dev, "Failed to reset device during resume\n");
745 pci_clear_master(hdev->pdev);
746 pci_disable_device(hdev->pdev);
751 static int device_kill_open_processes(struct hl_device *hdev)
753 u16 pending_total, pending_cnt;
754 struct hl_fpriv *hpriv;
755 struct task_struct *task = NULL;
758 pending_total = HL_PLDM_PENDING_RESET_PER_SEC;
760 pending_total = HL_PENDING_RESET_PER_SEC;
762 /* Giving time for user to close FD, and for processes that are inside
763 * hl_device_open to finish
765 if (!list_empty(&hdev->fpriv_list))
768 mutex_lock(&hdev->fpriv_list_lock);
770 /* This section must be protected because we are dereferencing
771 * pointers that are freed if the process exits
773 list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) {
774 task = get_pid_task(hpriv->taskpid, PIDTYPE_PID);
776 dev_info(hdev->dev, "Killing user process pid=%d\n",
778 send_sig(SIGKILL, task, 1);
779 usleep_range(1000, 10000);
781 put_task_struct(task);
785 mutex_unlock(&hdev->fpriv_list_lock);
787 /* We killed the open users, but because the driver cleans up after the
788 * user contexts are closed (e.g. mmu mappings), we need to wait again
789 * to make sure the cleaning phase is finished before continuing with
793 pending_cnt = pending_total;
795 while ((!list_empty(&hdev->fpriv_list)) && (pending_cnt)) {
797 "Waiting for all unmap operations to finish before hard reset\n");
804 return list_empty(&hdev->fpriv_list) ? 0 : -EBUSY;
807 static void device_hard_reset_pending(struct work_struct *work)
809 struct hl_device_reset_work *device_reset_work =
810 container_of(work, struct hl_device_reset_work, reset_work);
811 struct hl_device *hdev = device_reset_work->hdev;
813 hl_device_reset(hdev, true, true);
815 kfree(device_reset_work);
819 * hl_device_reset - reset the device
821 * @hdev: pointer to habanalabs device structure
822 * @hard_reset: should we do hard reset to all engines or just reset the
823 * compute/dma engines
824 * @from_hard_reset_thread: is the caller the hard-reset thread
826 * Block future CS and wait for pending CS to be enqueued
828 * Flush all completions
829 * Re-initialize all internal data structures
830 * Call ASIC H/W init, late_init
834 * Returns 0 for success or an error on failure.
836 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
837 bool from_hard_reset_thread)
841 if (!hdev->init_done) {
843 "Can't reset before initialization is done\n");
847 if ((!hard_reset) && (!hdev->supports_soft_reset)) {
848 dev_dbg(hdev->dev, "Doing hard-reset instead of soft-reset\n");
853 * Prevent concurrency in this function - only one reset should be
854 * done at any given time. Only need to perform this if we didn't
855 * get from the dedicated hard reset thread
857 if (!from_hard_reset_thread) {
858 /* Block future CS/VM/JOB completion operations */
859 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
864 /* Disable PCI access from device F/W so he won't send
865 * us additional interrupts. We disable MSI/MSI-X at
866 * the halt_engines function and we can't have the F/W
867 * sending us interrupts after that. We need to disable
868 * the access here because if the device is marked
869 * disable, the message won't be send. Also, in case
870 * of heartbeat, the device CPU is marked as disable
871 * so this message won't be sent
873 if (hl_fw_send_pci_access_msg(hdev,
874 ARMCP_PACKET_DISABLE_PCI_ACCESS))
876 "Failed to disable PCI access by F/W\n");
879 /* This also blocks future CS/VM/JOB completion operations */
880 hdev->disabled = true;
882 /* Flush anyone that is inside the critical section of enqueue
885 hdev->asic_funcs->hw_queues_lock(hdev);
886 hdev->asic_funcs->hw_queues_unlock(hdev);
888 /* Flush anyone that is inside device open */
889 mutex_lock(&hdev->fpriv_list_lock);
890 mutex_unlock(&hdev->fpriv_list_lock);
892 dev_err(hdev->dev, "Going to RESET device!\n");
896 if ((hard_reset) && (!from_hard_reset_thread)) {
897 struct hl_device_reset_work *device_reset_work;
899 hdev->hard_reset_pending = true;
901 device_reset_work = kzalloc(sizeof(*device_reset_work),
903 if (!device_reset_work) {
909 * Because the reset function can't run from interrupt or
910 * from heartbeat work, we need to call the reset function
911 * from a dedicated work
913 INIT_WORK(&device_reset_work->reset_work,
914 device_hard_reset_pending);
915 device_reset_work->hdev = hdev;
916 schedule_work(&device_reset_work->reset_work);
922 device_late_fini(hdev);
925 * Now that the heartbeat thread is closed, flush processes
926 * which are sending messages to CPU
928 mutex_lock(&hdev->send_cpu_message_lock);
929 mutex_unlock(&hdev->send_cpu_message_lock);
933 * Halt the engines and disable interrupts so we won't get any more
934 * completions from H/W and we won't have any accesses from the
935 * H/W to the host machine
937 hdev->asic_funcs->halt_engines(hdev, hard_reset);
939 /* Go over all the queues, release all CS and their jobs */
940 hl_cs_rollback_all(hdev);
943 /* Kill processes here after CS rollback. This is because the
944 * process can't really exit until all its CSs are done, which
945 * is what we do in cs rollback
947 rc = device_kill_open_processes(hdev);
950 "Failed to kill all open processes, stopping hard reset\n");
954 /* Flush the Event queue workers to make sure no other thread is
955 * reading or writing to registers during the reset
957 flush_workqueue(hdev->eq_wq);
960 /* Release kernel context */
961 if ((hard_reset) && (hl_ctx_put(hdev->kernel_ctx) == 1))
962 hdev->kernel_ctx = NULL;
964 /* Reset the H/W. It will be in idle state after this returns */
965 hdev->asic_funcs->hw_fini(hdev, hard_reset);
970 hl_eq_reset(hdev, &hdev->event_queue);
973 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
974 hl_hw_queue_reset(hdev, hard_reset);
975 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
976 hl_cq_reset(hdev, &hdev->completion_queue[i]);
978 hdev->idle_busy_ts_idx = 0;
979 hdev->idle_busy_ts_arr[0].busy_to_idle_ts = ktime_set(0, 0);
980 hdev->idle_busy_ts_arr[0].idle_to_busy_ts = ktime_set(0, 0);
982 if (hdev->cs_active_cnt)
983 dev_crit(hdev->dev, "CS active cnt %d is not 0 during reset\n",
984 hdev->cs_active_cnt);
986 mutex_lock(&hdev->fpriv_list_lock);
988 /* Make sure the context switch phase will run again */
989 if (hdev->compute_ctx) {
990 atomic_set(&hdev->compute_ctx->thread_ctx_switch_token, 1);
991 hdev->compute_ctx->thread_ctx_switch_wait_token = 0;
994 mutex_unlock(&hdev->fpriv_list_lock);
996 /* Finished tear-down, starting to re-initialize */
999 hdev->device_cpu_disabled = false;
1000 hdev->hard_reset_pending = false;
1002 if (hdev->kernel_ctx) {
1004 "kernel ctx was alive during hard reset, something is terribly wrong\n");
1009 rc = hl_mmu_init(hdev);
1012 "Failed to initialize MMU S/W after hard reset\n");
1016 /* Allocate the kernel context */
1017 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
1019 if (!hdev->kernel_ctx) {
1024 hdev->compute_ctx = NULL;
1026 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1029 "failed to init kernel ctx in hard reset\n");
1030 kfree(hdev->kernel_ctx);
1031 hdev->kernel_ctx = NULL;
1036 /* Device is now enabled as part of the initialization requires
1037 * communication with the device firmware to get information that
1038 * is required for the initialization itself
1040 hdev->disabled = false;
1042 rc = hdev->asic_funcs->hw_init(hdev);
1045 "failed to initialize the H/W after reset\n");
1049 /* Check that the communication with the device is working */
1050 rc = hdev->asic_funcs->test_queues(hdev);
1053 "Failed to detect if device is alive after reset\n");
1058 rc = device_late_init(hdev);
1061 "Failed late init after hard reset\n");
1065 rc = hl_vm_init(hdev);
1068 "Failed to init memory module after hard reset\n");
1072 hl_set_max_power(hdev);
1074 rc = hdev->asic_funcs->soft_reset_late_init(hdev);
1077 "Failed late init after soft reset\n");
1082 atomic_set(&hdev->in_reset, 0);
1085 hdev->hard_reset_cnt++;
1087 hdev->soft_reset_cnt++;
1089 dev_warn(hdev->dev, "Successfully finished resetting the device\n");
1094 hdev->disabled = true;
1098 "Failed to reset! Device is NOT usable\n");
1099 hdev->hard_reset_cnt++;
1102 "Failed to do soft-reset, trying hard reset\n");
1103 hdev->soft_reset_cnt++;
1108 atomic_set(&hdev->in_reset, 0);
1114 * hl_device_init - main initialization function for habanalabs device
1116 * @hdev: pointer to habanalabs device structure
1118 * Allocate an id for the device, do early initialization and then call the
1119 * ASIC specific initialization functions. Finally, create the cdev and the
1120 * Linux device to expose it to the user
1122 int hl_device_init(struct hl_device *hdev, struct class *hclass)
1124 int i, rc, cq_cnt, cq_ready_cnt;
1126 bool add_cdev_sysfs_on_err = false;
1128 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id / 2);
1134 /* Initialize cdev and device structures */
1135 rc = device_init_cdev(hdev, hclass, hdev->id, &hl_ops, name,
1136 &hdev->cdev, &hdev->dev);
1143 name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->id / 2);
1149 /* Initialize cdev and device structures for control device */
1150 rc = device_init_cdev(hdev, hclass, hdev->id_control, &hl_ctrl_ops,
1151 name, &hdev->cdev_ctrl, &hdev->dev_ctrl);
1158 /* Initialize ASIC function pointers and perform early init */
1159 rc = device_early_init(hdev);
1164 * Start calling ASIC initialization. First S/W then H/W and finally
1167 rc = hdev->asic_funcs->sw_init(hdev);
1172 * Initialize the H/W queues. Must be done before hw_init, because
1173 * there the addresses of the kernel queue are being written to the
1174 * registers of the device
1176 rc = hl_hw_queues_create(hdev);
1178 dev_err(hdev->dev, "failed to initialize kernel queues\n");
1182 cq_cnt = hdev->asic_prop.completion_queues_count;
1185 * Initialize the completion queues. Must be done before hw_init,
1186 * because there the addresses of the completion queues are being
1187 * passed as arguments to request_irq
1190 hdev->completion_queue = kcalloc(cq_cnt,
1191 sizeof(*hdev->completion_queue),
1194 if (!hdev->completion_queue) {
1196 "failed to allocate completion queues\n");
1198 goto hw_queues_destroy;
1202 for (i = 0, cq_ready_cnt = 0 ; i < cq_cnt ; i++, cq_ready_cnt++) {
1203 rc = hl_cq_init(hdev, &hdev->completion_queue[i],
1204 hdev->asic_funcs->get_queue_id_for_cq(hdev, i));
1207 "failed to initialize completion queue\n");
1210 hdev->completion_queue[i].cq_idx = i;
1214 * Initialize the event queue. Must be done before hw_init,
1215 * because there the address of the event queue is being
1216 * passed as argument to request_irq
1218 rc = hl_eq_init(hdev, &hdev->event_queue);
1220 dev_err(hdev->dev, "failed to initialize event queue\n");
1224 /* MMU S/W must be initialized before kernel context is created */
1225 rc = hl_mmu_init(hdev);
1227 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n");
1231 /* Allocate the kernel context */
1232 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
1233 if (!hdev->kernel_ctx) {
1238 hdev->compute_ctx = NULL;
1240 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1242 dev_err(hdev->dev, "failed to initialize kernel context\n");
1243 kfree(hdev->kernel_ctx);
1247 rc = hl_cb_pool_init(hdev);
1249 dev_err(hdev->dev, "failed to initialize CB pool\n");
1253 hl_debugfs_add_device(hdev);
1255 if (hdev->asic_funcs->get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
1257 "H/W state is dirty, must reset before initializing\n");
1258 hdev->asic_funcs->halt_engines(hdev, true);
1259 hdev->asic_funcs->hw_fini(hdev, true);
1263 * From this point, in case of an error, add char devices and create
1264 * sysfs nodes as part of the error flow, to allow debugging.
1266 add_cdev_sysfs_on_err = true;
1268 /* Device is now enabled as part of the initialization requires
1269 * communication with the device firmware to get information that
1270 * is required for the initialization itself
1272 hdev->disabled = false;
1274 rc = hdev->asic_funcs->hw_init(hdev);
1276 dev_err(hdev->dev, "failed to initialize the H/W\n");
1281 /* Check that the communication with the device is working */
1282 rc = hdev->asic_funcs->test_queues(hdev);
1284 dev_err(hdev->dev, "Failed to detect if device is alive\n");
1289 rc = device_late_init(hdev);
1291 dev_err(hdev->dev, "Failed late initialization\n");
1296 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
1298 hdev->asic_prop.dram_size / 1024 / 1024 / 1024);
1300 rc = hl_vm_init(hdev);
1302 dev_err(hdev->dev, "Failed to initialize memory module\n");
1308 * Expose devices and sysfs nodes to user.
1309 * From here there is no need to add char devices and create sysfs nodes
1310 * in case of an error.
1312 add_cdev_sysfs_on_err = false;
1313 rc = device_cdev_sysfs_add(hdev);
1316 "Failed to add char devices and sysfs nodes\n");
1321 /* Need to call this again because the max power might change,
1322 * depending on card type for certain ASICs
1324 hl_set_max_power(hdev);
1327 * hl_hwmon_init() must be called after device_late_init(), because only
1328 * there we get the information from the device about which
1329 * hwmon-related sensors the device supports.
1330 * Furthermore, it must be done after adding the device to the system.
1332 rc = hl_hwmon_init(hdev);
1334 dev_err(hdev->dev, "Failed to initialize hwmon\n");
1339 dev_notice(hdev->dev,
1340 "Successfully added device to habanalabs driver\n");
1342 hdev->init_done = true;
1347 if (hl_ctx_put(hdev->kernel_ctx) != 1)
1349 "kernel ctx is still alive on initialization failure\n");
1353 hl_eq_fini(hdev, &hdev->event_queue);
1355 for (i = 0 ; i < cq_ready_cnt ; i++)
1356 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1357 kfree(hdev->completion_queue);
1359 hl_hw_queues_destroy(hdev);
1361 hdev->asic_funcs->sw_fini(hdev);
1363 device_early_fini(hdev);
1365 kfree(hdev->dev_ctrl);
1369 hdev->disabled = true;
1370 if (add_cdev_sysfs_on_err)
1371 device_cdev_sysfs_add(hdev);
1373 dev_err(&hdev->pdev->dev,
1374 "Failed to initialize hl%d. Device is NOT usable !\n",
1377 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
1384 * hl_device_fini - main tear-down function for habanalabs device
1386 * @hdev: pointer to habanalabs device structure
1388 * Destroy the device, call ASIC fini functions and release the id
1390 void hl_device_fini(struct hl_device *hdev)
1395 dev_info(hdev->dev, "Removing device\n");
1398 * This function is competing with the reset function, so try to
1399 * take the reset atomic and if we are already in middle of reset,
1400 * wait until reset function is finished. Reset function is designed
1401 * to always finish. However, in Gaudi, because of all the network
1402 * ports, the hard reset could take between 10-30 seconds
1405 timeout = ktime_add_us(ktime_get(),
1406 HL_HARD_RESET_MAX_TIMEOUT * 1000 * 1000);
1407 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1409 usleep_range(50, 200);
1410 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1411 if (ktime_compare(ktime_get(), timeout) > 0) {
1412 WARN(1, "Failed to remove device because reset function did not finish\n");
1417 /* Mark device as disabled */
1418 hdev->disabled = true;
1420 /* Flush anyone that is inside the critical section of enqueue
1423 hdev->asic_funcs->hw_queues_lock(hdev);
1424 hdev->asic_funcs->hw_queues_unlock(hdev);
1426 /* Flush anyone that is inside device open */
1427 mutex_lock(&hdev->fpriv_list_lock);
1428 mutex_unlock(&hdev->fpriv_list_lock);
1430 hdev->hard_reset_pending = true;
1432 hl_hwmon_fini(hdev);
1434 device_late_fini(hdev);
1436 hl_debugfs_remove_device(hdev);
1439 * Halt the engines and disable interrupts so we won't get any more
1440 * completions from H/W and we won't have any accesses from the
1441 * H/W to the host machine
1443 hdev->asic_funcs->halt_engines(hdev, true);
1445 /* Go over all the queues, release all CS and their jobs */
1446 hl_cs_rollback_all(hdev);
1448 /* Kill processes here after CS rollback. This is because the process
1449 * can't really exit until all its CSs are done, which is what we
1452 rc = device_kill_open_processes(hdev);
1454 dev_crit(hdev->dev, "Failed to kill all open processes\n");
1456 hl_cb_pool_fini(hdev);
1458 /* Release kernel context */
1459 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
1460 dev_err(hdev->dev, "kernel ctx is still alive\n");
1462 /* Reset the H/W. It will be in idle state after this returns */
1463 hdev->asic_funcs->hw_fini(hdev, true);
1469 hl_eq_fini(hdev, &hdev->event_queue);
1471 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1472 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1473 kfree(hdev->completion_queue);
1475 hl_hw_queues_destroy(hdev);
1477 /* Call ASIC S/W finalize function */
1478 hdev->asic_funcs->sw_fini(hdev);
1480 device_early_fini(hdev);
1482 /* Hide devices and sysfs nodes from user */
1483 device_cdev_sysfs_del(hdev);
1485 pr_info("removed device successfully\n");
1489 * MMIO register access helper functions.
1493 * hl_rreg - Read an MMIO register
1495 * @hdev: pointer to habanalabs device structure
1496 * @reg: MMIO register offset (in bytes)
1498 * Returns the value of the MMIO register we are asked to read
1501 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1503 return readl(hdev->rmmio + reg);
1507 * hl_wreg - Write to an MMIO register
1509 * @hdev: pointer to habanalabs device structure
1510 * @reg: MMIO register offset (in bytes)
1511 * @val: 32-bit value
1513 * Writes the 32-bit value into the MMIO register
1516 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1518 writel(val, hdev->rmmio + reg);