treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / habanalabs / device.c
blobb155e95490761271262d46c4c46adface12c561a
1 // SPDX-License-Identifier: GPL-2.0
3 /*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
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)))
22 return true;
23 else
24 return false;
27 enum hl_device_status hl_device_status(struct hl_device *hdev)
29 enum hl_device_status status;
31 if (hdev->disabled)
32 status = HL_DEVICE_STATUS_MALFUNCTION;
33 else if (atomic_read(&hdev->in_reset))
34 status = HL_DEVICE_STATUS_IN_RESET;
35 else
36 status = HL_DEVICE_STATUS_OPERATIONAL;
38 return status;
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);
48 hdev = hpriv->hdev;
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);
61 kfree(hpriv);
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;
91 hl_hpriv_put(hpriv);
93 return 0;
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;
103 hdev = hpriv->hdev;
105 mutex_lock(&hdev->fpriv_list_lock);
106 list_del(&hpriv->dev_node);
107 mutex_unlock(&hdev->fpriv_list_lock);
109 kfree(hpriv);
111 return 0;
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);
132 return -EINVAL;
135 static const struct file_operations hl_ops = {
136 .owner = THIS_MODULE,
137 .open = hl_device_open,
138 .release = hl_device_release,
139 .mmap = hl_mmap,
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)
154 kfree(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,
173 struct device **dev)
175 cdev_init(cdev, fops);
176 cdev->owner = THIS_MODULE;
178 *dev = kzalloc(sizeof(**dev), GFP_KERNEL);
179 if (!*dev)
180 return -ENOMEM;
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);
189 return 0;
192 static int device_cdev_sysfs_add(struct hl_device *hdev)
194 int rc;
196 rc = cdev_device_add(&hdev->cdev, hdev->dev);
197 if (rc) {
198 dev_err(hdev->dev,
199 "failed to add a char device to the system\n");
200 return rc;
203 rc = cdev_device_add(&hdev->cdev_ctrl, hdev->dev_ctrl);
204 if (rc) {
205 dev_err(hdev->dev,
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);
212 if (rc) {
213 dev_err(hdev->dev, "failed to initialize sysfs\n");
214 goto delete_ctrl_cdev_device;
217 hdev->cdev_sysfs_created = true;
219 return 0;
221 delete_ctrl_cdev_device:
222 cdev_device_del(&hdev->cdev_ctrl, hdev->dev_ctrl);
223 delete_cdev_device:
224 cdev_device_del(&hdev->cdev, hdev->dev);
225 return rc;
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);
233 kfree(hdev->dev);
234 return;
237 hl_sysfs_fini(hdev);
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)
252 int rc;
254 switch (hdev->asic_type) {
255 case ASIC_GOYA:
256 goya_set_asic_funcs(hdev);
257 strlcpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
258 break;
259 default:
260 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
261 hdev->asic_type);
262 return -EINVAL;
265 rc = hdev->asic_funcs->early_init(hdev);
266 if (rc)
267 return rc;
269 rc = hl_asid_init(hdev);
270 if (rc)
271 goto early_fini;
273 hdev->cq_wq = alloc_workqueue("hl-free-jobs", WQ_UNBOUND, 0);
274 if (hdev->cq_wq == NULL) {
275 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
276 rc = -ENOMEM;
277 goto asid_fini;
280 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
281 if (hdev->eq_wq == NULL) {
282 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
283 rc = -ENOMEM;
284 goto free_cq_wq;
287 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
288 GFP_KERNEL);
289 if (!hdev->hl_chip_info) {
290 rc = -ENOMEM;
291 goto free_eq_wq;
294 hdev->idle_busy_ts_arr = kmalloc_array(HL_IDLE_BUSY_TS_ARR_SIZE,
295 sizeof(struct hl_device_idle_busy_ts),
296 (GFP_KERNEL | __GFP_ZERO));
297 if (!hdev->idle_busy_ts_arr) {
298 rc = -ENOMEM;
299 goto free_chip_info;
302 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
304 mutex_init(&hdev->send_cpu_message_lock);
305 mutex_init(&hdev->debug_lock);
306 mutex_init(&hdev->mmu_cache_lock);
307 INIT_LIST_HEAD(&hdev->hw_queues_mirror_list);
308 spin_lock_init(&hdev->hw_queues_mirror_lock);
309 INIT_LIST_HEAD(&hdev->fpriv_list);
310 mutex_init(&hdev->fpriv_list_lock);
311 atomic_set(&hdev->in_reset, 0);
313 return 0;
315 free_chip_info:
316 kfree(hdev->hl_chip_info);
317 free_eq_wq:
318 destroy_workqueue(hdev->eq_wq);
319 free_cq_wq:
320 destroy_workqueue(hdev->cq_wq);
321 asid_fini:
322 hl_asid_fini(hdev);
323 early_fini:
324 if (hdev->asic_funcs->early_fini)
325 hdev->asic_funcs->early_fini(hdev);
327 return rc;
331 * device_early_fini - finalize all that was done in device_early_init
333 * @hdev: pointer to habanalabs device structure
336 static void device_early_fini(struct hl_device *hdev)
338 mutex_destroy(&hdev->mmu_cache_lock);
339 mutex_destroy(&hdev->debug_lock);
340 mutex_destroy(&hdev->send_cpu_message_lock);
342 mutex_destroy(&hdev->fpriv_list_lock);
344 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
346 kfree(hdev->idle_busy_ts_arr);
347 kfree(hdev->hl_chip_info);
349 destroy_workqueue(hdev->eq_wq);
350 destroy_workqueue(hdev->cq_wq);
352 hl_asid_fini(hdev);
354 if (hdev->asic_funcs->early_fini)
355 hdev->asic_funcs->early_fini(hdev);
358 static void set_freq_to_low_job(struct work_struct *work)
360 struct hl_device *hdev = container_of(work, struct hl_device,
361 work_freq.work);
363 mutex_lock(&hdev->fpriv_list_lock);
365 if (!hdev->compute_ctx)
366 hl_device_set_frequency(hdev, PLL_LOW);
368 mutex_unlock(&hdev->fpriv_list_lock);
370 schedule_delayed_work(&hdev->work_freq,
371 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
374 static void hl_device_heartbeat(struct work_struct *work)
376 struct hl_device *hdev = container_of(work, struct hl_device,
377 work_heartbeat.work);
379 if (hl_device_disabled_or_in_reset(hdev))
380 goto reschedule;
382 if (!hdev->asic_funcs->send_heartbeat(hdev))
383 goto reschedule;
385 dev_err(hdev->dev, "Device heartbeat failed!\n");
386 hl_device_reset(hdev, true, false);
388 return;
390 reschedule:
391 schedule_delayed_work(&hdev->work_heartbeat,
392 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
396 * device_late_init - do late stuff initialization for the habanalabs device
398 * @hdev: pointer to habanalabs device structure
400 * Do stuff that either needs the device H/W queues to be active or needs
401 * to happen after all the rest of the initialization is finished
403 static int device_late_init(struct hl_device *hdev)
405 int rc;
407 if (hdev->asic_funcs->late_init) {
408 rc = hdev->asic_funcs->late_init(hdev);
409 if (rc) {
410 dev_err(hdev->dev,
411 "failed late initialization for the H/W\n");
412 return rc;
416 hdev->high_pll = hdev->asic_prop.high_pll;
418 /* force setting to low frequency */
419 hdev->curr_pll_profile = PLL_LOW;
421 if (hdev->pm_mng_profile == PM_AUTO)
422 hdev->asic_funcs->set_pll_profile(hdev, PLL_LOW);
423 else
424 hdev->asic_funcs->set_pll_profile(hdev, PLL_LAST);
426 INIT_DELAYED_WORK(&hdev->work_freq, set_freq_to_low_job);
427 schedule_delayed_work(&hdev->work_freq,
428 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
430 if (hdev->heartbeat) {
431 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
432 schedule_delayed_work(&hdev->work_heartbeat,
433 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
436 hdev->late_init_done = true;
438 return 0;
442 * device_late_fini - finalize all that was done in device_late_init
444 * @hdev: pointer to habanalabs device structure
447 static void device_late_fini(struct hl_device *hdev)
449 if (!hdev->late_init_done)
450 return;
452 cancel_delayed_work_sync(&hdev->work_freq);
453 if (hdev->heartbeat)
454 cancel_delayed_work_sync(&hdev->work_heartbeat);
456 if (hdev->asic_funcs->late_fini)
457 hdev->asic_funcs->late_fini(hdev);
459 hdev->late_init_done = false;
462 uint32_t hl_device_utilization(struct hl_device *hdev, uint32_t period_ms)
464 struct hl_device_idle_busy_ts *ts;
465 ktime_t zero_ktime, curr = ktime_get();
466 u32 overlap_cnt = 0, last_index = hdev->idle_busy_ts_idx;
467 s64 period_us, last_start_us, last_end_us, last_busy_time_us,
468 total_busy_time_us = 0, total_busy_time_ms;
470 zero_ktime = ktime_set(0, 0);
471 period_us = period_ms * USEC_PER_MSEC;
472 ts = &hdev->idle_busy_ts_arr[last_index];
474 /* check case that device is currently in idle */
475 if (!ktime_compare(ts->busy_to_idle_ts, zero_ktime) &&
476 !ktime_compare(ts->idle_to_busy_ts, zero_ktime)) {
478 last_index--;
479 /* Handle case idle_busy_ts_idx was 0 */
480 if (last_index > HL_IDLE_BUSY_TS_ARR_SIZE)
481 last_index = HL_IDLE_BUSY_TS_ARR_SIZE - 1;
483 ts = &hdev->idle_busy_ts_arr[last_index];
486 while (overlap_cnt < HL_IDLE_BUSY_TS_ARR_SIZE) {
487 /* Check if we are in last sample case. i.e. if the sample
488 * begun before the sampling period. This could be a real
489 * sample or 0 so need to handle both cases
491 last_start_us = ktime_to_us(
492 ktime_sub(curr, ts->idle_to_busy_ts));
494 if (last_start_us > period_us) {
496 /* First check two cases:
497 * 1. If the device is currently busy
498 * 2. If the device was idle during the whole sampling
499 * period
502 if (!ktime_compare(ts->busy_to_idle_ts, zero_ktime)) {
503 /* Check if the device is currently busy */
504 if (ktime_compare(ts->idle_to_busy_ts,
505 zero_ktime))
506 return 100;
508 /* We either didn't have any activity or we
509 * reached an entry which is 0. Either way,
510 * exit and return what was accumulated so far
512 break;
515 /* If sample has finished, check it is relevant */
516 last_end_us = ktime_to_us(
517 ktime_sub(curr, ts->busy_to_idle_ts));
519 if (last_end_us > period_us)
520 break;
522 /* It is relevant so add it but with adjustment */
523 last_busy_time_us = ktime_to_us(
524 ktime_sub(ts->busy_to_idle_ts,
525 ts->idle_to_busy_ts));
526 total_busy_time_us += last_busy_time_us -
527 (last_start_us - period_us);
528 break;
531 /* Check if the sample is finished or still open */
532 if (ktime_compare(ts->busy_to_idle_ts, zero_ktime))
533 last_busy_time_us = ktime_to_us(
534 ktime_sub(ts->busy_to_idle_ts,
535 ts->idle_to_busy_ts));
536 else
537 last_busy_time_us = ktime_to_us(
538 ktime_sub(curr, ts->idle_to_busy_ts));
540 total_busy_time_us += last_busy_time_us;
542 last_index--;
543 /* Handle case idle_busy_ts_idx was 0 */
544 if (last_index > HL_IDLE_BUSY_TS_ARR_SIZE)
545 last_index = HL_IDLE_BUSY_TS_ARR_SIZE - 1;
547 ts = &hdev->idle_busy_ts_arr[last_index];
549 overlap_cnt++;
552 total_busy_time_ms = DIV_ROUND_UP_ULL(total_busy_time_us,
553 USEC_PER_MSEC);
555 return DIV_ROUND_UP_ULL(total_busy_time_ms * 100, period_ms);
559 * hl_device_set_frequency - set the frequency of the device
561 * @hdev: pointer to habanalabs device structure
562 * @freq: the new frequency value
564 * Change the frequency if needed. This function has no protection against
565 * concurrency, therefore it is assumed that the calling function has protected
566 * itself against the case of calling this function from multiple threads with
567 * different values
569 * Returns 0 if no change was done, otherwise returns 1
571 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq)
573 if ((hdev->pm_mng_profile == PM_MANUAL) ||
574 (hdev->curr_pll_profile == freq))
575 return 0;
577 dev_dbg(hdev->dev, "Changing device frequency to %s\n",
578 freq == PLL_HIGH ? "high" : "low");
580 hdev->asic_funcs->set_pll_profile(hdev, freq);
582 hdev->curr_pll_profile = freq;
584 return 1;
587 int hl_device_set_debug_mode(struct hl_device *hdev, bool enable)
589 int rc = 0;
591 mutex_lock(&hdev->debug_lock);
593 if (!enable) {
594 if (!hdev->in_debug) {
595 dev_err(hdev->dev,
596 "Failed to disable debug mode because device was not in debug mode\n");
597 rc = -EFAULT;
598 goto out;
601 hdev->asic_funcs->halt_coresight(hdev);
602 hdev->in_debug = 0;
604 goto out;
607 if (hdev->in_debug) {
608 dev_err(hdev->dev,
609 "Failed to enable debug mode because device is already in debug mode\n");
610 rc = -EFAULT;
611 goto out;
614 hdev->in_debug = 1;
616 out:
617 mutex_unlock(&hdev->debug_lock);
619 return rc;
623 * hl_device_suspend - initiate device suspend
625 * @hdev: pointer to habanalabs device structure
627 * Puts the hw in the suspend state (all asics).
628 * Returns 0 for success or an error on failure.
629 * Called at driver suspend.
631 int hl_device_suspend(struct hl_device *hdev)
633 int rc;
635 pci_save_state(hdev->pdev);
637 /* Block future CS/VM/JOB completion operations */
638 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
639 if (rc) {
640 dev_err(hdev->dev, "Can't suspend while in reset\n");
641 return -EIO;
644 /* This blocks all other stuff that is not blocked by in_reset */
645 hdev->disabled = true;
648 * Flush anyone that is inside the critical section of enqueue
649 * jobs to the H/W
651 hdev->asic_funcs->hw_queues_lock(hdev);
652 hdev->asic_funcs->hw_queues_unlock(hdev);
654 /* Flush processes that are sending message to CPU */
655 mutex_lock(&hdev->send_cpu_message_lock);
656 mutex_unlock(&hdev->send_cpu_message_lock);
658 rc = hdev->asic_funcs->suspend(hdev);
659 if (rc)
660 dev_err(hdev->dev,
661 "Failed to disable PCI access of device CPU\n");
663 /* Shut down the device */
664 pci_disable_device(hdev->pdev);
665 pci_set_power_state(hdev->pdev, PCI_D3hot);
667 return 0;
671 * hl_device_resume - initiate device resume
673 * @hdev: pointer to habanalabs device structure
675 * Bring the hw back to operating state (all asics).
676 * Returns 0 for success or an error on failure.
677 * Called at driver resume.
679 int hl_device_resume(struct hl_device *hdev)
681 int rc;
683 pci_set_power_state(hdev->pdev, PCI_D0);
684 pci_restore_state(hdev->pdev);
685 rc = pci_enable_device_mem(hdev->pdev);
686 if (rc) {
687 dev_err(hdev->dev,
688 "Failed to enable PCI device in resume\n");
689 return rc;
692 pci_set_master(hdev->pdev);
694 rc = hdev->asic_funcs->resume(hdev);
695 if (rc) {
696 dev_err(hdev->dev, "Failed to resume device after suspend\n");
697 goto disable_device;
701 hdev->disabled = false;
702 atomic_set(&hdev->in_reset, 0);
704 rc = hl_device_reset(hdev, true, false);
705 if (rc) {
706 dev_err(hdev->dev, "Failed to reset device during resume\n");
707 goto disable_device;
710 return 0;
712 disable_device:
713 pci_clear_master(hdev->pdev);
714 pci_disable_device(hdev->pdev);
716 return rc;
719 static void device_kill_open_processes(struct hl_device *hdev)
721 u16 pending_total, pending_cnt;
722 struct hl_fpriv *hpriv;
723 struct task_struct *task = NULL;
725 if (hdev->pldm)
726 pending_total = HL_PLDM_PENDING_RESET_PER_SEC;
727 else
728 pending_total = HL_PENDING_RESET_PER_SEC;
730 /* Giving time for user to close FD, and for processes that are inside
731 * hl_device_open to finish
733 if (!list_empty(&hdev->fpriv_list))
734 ssleep(1);
736 mutex_lock(&hdev->fpriv_list_lock);
738 /* This section must be protected because we are dereferencing
739 * pointers that are freed if the process exits
741 list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) {
742 task = get_pid_task(hpriv->taskpid, PIDTYPE_PID);
743 if (task) {
744 dev_info(hdev->dev, "Killing user process pid=%d\n",
745 task_pid_nr(task));
746 send_sig(SIGKILL, task, 1);
747 usleep_range(1000, 10000);
749 put_task_struct(task);
753 mutex_unlock(&hdev->fpriv_list_lock);
755 /* We killed the open users, but because the driver cleans up after the
756 * user contexts are closed (e.g. mmu mappings), we need to wait again
757 * to make sure the cleaning phase is finished before continuing with
758 * the reset
761 pending_cnt = pending_total;
763 while ((!list_empty(&hdev->fpriv_list)) && (pending_cnt)) {
764 dev_info(hdev->dev,
765 "Waiting for all unmap operations to finish before hard reset\n");
767 pending_cnt--;
769 ssleep(1);
772 if (!list_empty(&hdev->fpriv_list))
773 dev_crit(hdev->dev,
774 "Going to hard reset with open user contexts\n");
777 static void device_hard_reset_pending(struct work_struct *work)
779 struct hl_device_reset_work *device_reset_work =
780 container_of(work, struct hl_device_reset_work, reset_work);
781 struct hl_device *hdev = device_reset_work->hdev;
783 hl_device_reset(hdev, true, true);
785 kfree(device_reset_work);
789 * hl_device_reset - reset the device
791 * @hdev: pointer to habanalabs device structure
792 * @hard_reset: should we do hard reset to all engines or just reset the
793 * compute/dma engines
795 * Block future CS and wait for pending CS to be enqueued
796 * Call ASIC H/W fini
797 * Flush all completions
798 * Re-initialize all internal data structures
799 * Call ASIC H/W init, late_init
800 * Test queues
801 * Enable device
803 * Returns 0 for success or an error on failure.
805 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
806 bool from_hard_reset_thread)
808 int i, rc;
810 if (!hdev->init_done) {
811 dev_err(hdev->dev,
812 "Can't reset before initialization is done\n");
813 return 0;
817 * Prevent concurrency in this function - only one reset should be
818 * done at any given time. Only need to perform this if we didn't
819 * get from the dedicated hard reset thread
821 if (!from_hard_reset_thread) {
822 /* Block future CS/VM/JOB completion operations */
823 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
824 if (rc)
825 return 0;
827 /* This also blocks future CS/VM/JOB completion operations */
828 hdev->disabled = true;
830 /* Flush anyone that is inside the critical section of enqueue
831 * jobs to the H/W
833 hdev->asic_funcs->hw_queues_lock(hdev);
834 hdev->asic_funcs->hw_queues_unlock(hdev);
836 /* Flush anyone that is inside device open */
837 mutex_lock(&hdev->fpriv_list_lock);
838 mutex_unlock(&hdev->fpriv_list_lock);
840 dev_err(hdev->dev, "Going to RESET device!\n");
843 again:
844 if ((hard_reset) && (!from_hard_reset_thread)) {
845 struct hl_device_reset_work *device_reset_work;
847 hdev->hard_reset_pending = true;
849 device_reset_work = kzalloc(sizeof(*device_reset_work),
850 GFP_ATOMIC);
851 if (!device_reset_work) {
852 rc = -ENOMEM;
853 goto out_err;
857 * Because the reset function can't run from interrupt or
858 * from heartbeat work, we need to call the reset function
859 * from a dedicated work
861 INIT_WORK(&device_reset_work->reset_work,
862 device_hard_reset_pending);
863 device_reset_work->hdev = hdev;
864 schedule_work(&device_reset_work->reset_work);
866 return 0;
869 if (hard_reset) {
870 device_late_fini(hdev);
873 * Now that the heartbeat thread is closed, flush processes
874 * which are sending messages to CPU
876 mutex_lock(&hdev->send_cpu_message_lock);
877 mutex_unlock(&hdev->send_cpu_message_lock);
881 * Halt the engines and disable interrupts so we won't get any more
882 * completions from H/W and we won't have any accesses from the
883 * H/W to the host machine
885 hdev->asic_funcs->halt_engines(hdev, hard_reset);
887 /* Go over all the queues, release all CS and their jobs */
888 hl_cs_rollback_all(hdev);
890 if (hard_reset) {
891 /* Kill processes here after CS rollback. This is because the
892 * process can't really exit until all its CSs are done, which
893 * is what we do in cs rollback
895 device_kill_open_processes(hdev);
897 /* Flush the Event queue workers to make sure no other thread is
898 * reading or writing to registers during the reset
900 flush_workqueue(hdev->eq_wq);
903 /* Release kernel context */
904 if ((hard_reset) && (hl_ctx_put(hdev->kernel_ctx) == 1))
905 hdev->kernel_ctx = NULL;
907 /* Reset the H/W. It will be in idle state after this returns */
908 hdev->asic_funcs->hw_fini(hdev, hard_reset);
910 if (hard_reset) {
911 hl_vm_fini(hdev);
912 hl_mmu_fini(hdev);
913 hl_eq_reset(hdev, &hdev->event_queue);
916 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
917 hl_hw_queue_reset(hdev, hard_reset);
918 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
919 hl_cq_reset(hdev, &hdev->completion_queue[i]);
921 hdev->idle_busy_ts_idx = 0;
922 hdev->idle_busy_ts_arr[0].busy_to_idle_ts = ktime_set(0, 0);
923 hdev->idle_busy_ts_arr[0].idle_to_busy_ts = ktime_set(0, 0);
925 if (hdev->cs_active_cnt)
926 dev_crit(hdev->dev, "CS active cnt %d is not 0 during reset\n",
927 hdev->cs_active_cnt);
929 mutex_lock(&hdev->fpriv_list_lock);
931 /* Make sure the context switch phase will run again */
932 if (hdev->compute_ctx) {
933 atomic_set(&hdev->compute_ctx->thread_ctx_switch_token, 1);
934 hdev->compute_ctx->thread_ctx_switch_wait_token = 0;
937 mutex_unlock(&hdev->fpriv_list_lock);
939 /* Finished tear-down, starting to re-initialize */
941 if (hard_reset) {
942 hdev->device_cpu_disabled = false;
943 hdev->hard_reset_pending = false;
945 if (hdev->kernel_ctx) {
946 dev_crit(hdev->dev,
947 "kernel ctx was alive during hard reset, something is terribly wrong\n");
948 rc = -EBUSY;
949 goto out_err;
952 rc = hl_mmu_init(hdev);
953 if (rc) {
954 dev_err(hdev->dev,
955 "Failed to initialize MMU S/W after hard reset\n");
956 goto out_err;
959 /* Allocate the kernel context */
960 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
961 GFP_KERNEL);
962 if (!hdev->kernel_ctx) {
963 rc = -ENOMEM;
964 goto out_err;
967 hdev->compute_ctx = NULL;
969 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
970 if (rc) {
971 dev_err(hdev->dev,
972 "failed to init kernel ctx in hard reset\n");
973 kfree(hdev->kernel_ctx);
974 hdev->kernel_ctx = NULL;
975 goto out_err;
979 rc = hdev->asic_funcs->hw_init(hdev);
980 if (rc) {
981 dev_err(hdev->dev,
982 "failed to initialize the H/W after reset\n");
983 goto out_err;
986 hdev->disabled = false;
988 /* Check that the communication with the device is working */
989 rc = hdev->asic_funcs->test_queues(hdev);
990 if (rc) {
991 dev_err(hdev->dev,
992 "Failed to detect if device is alive after reset\n");
993 goto out_err;
996 if (hard_reset) {
997 rc = device_late_init(hdev);
998 if (rc) {
999 dev_err(hdev->dev,
1000 "Failed late init after hard reset\n");
1001 goto out_err;
1004 rc = hl_vm_init(hdev);
1005 if (rc) {
1006 dev_err(hdev->dev,
1007 "Failed to init memory module after hard reset\n");
1008 goto out_err;
1011 hl_set_max_power(hdev, hdev->max_power);
1012 } else {
1013 rc = hdev->asic_funcs->soft_reset_late_init(hdev);
1014 if (rc) {
1015 dev_err(hdev->dev,
1016 "Failed late init after soft reset\n");
1017 goto out_err;
1021 atomic_set(&hdev->in_reset, 0);
1023 if (hard_reset)
1024 hdev->hard_reset_cnt++;
1025 else
1026 hdev->soft_reset_cnt++;
1028 dev_warn(hdev->dev, "Successfully finished resetting the device\n");
1030 return 0;
1032 out_err:
1033 hdev->disabled = true;
1035 if (hard_reset) {
1036 dev_err(hdev->dev,
1037 "Failed to reset! Device is NOT usable\n");
1038 hdev->hard_reset_cnt++;
1039 } else {
1040 dev_err(hdev->dev,
1041 "Failed to do soft-reset, trying hard reset\n");
1042 hdev->soft_reset_cnt++;
1043 hard_reset = true;
1044 goto again;
1047 atomic_set(&hdev->in_reset, 0);
1049 return rc;
1053 * hl_device_init - main initialization function for habanalabs device
1055 * @hdev: pointer to habanalabs device structure
1057 * Allocate an id for the device, do early initialization and then call the
1058 * ASIC specific initialization functions. Finally, create the cdev and the
1059 * Linux device to expose it to the user
1061 int hl_device_init(struct hl_device *hdev, struct class *hclass)
1063 int i, rc, cq_ready_cnt;
1064 char *name;
1065 bool add_cdev_sysfs_on_err = false;
1067 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id / 2);
1068 if (!name) {
1069 rc = -ENOMEM;
1070 goto out_disabled;
1073 /* Initialize cdev and device structures */
1074 rc = device_init_cdev(hdev, hclass, hdev->id, &hl_ops, name,
1075 &hdev->cdev, &hdev->dev);
1077 kfree(name);
1079 if (rc)
1080 goto out_disabled;
1082 name = kasprintf(GFP_KERNEL, "hl_controlD%d", hdev->id / 2);
1083 if (!name) {
1084 rc = -ENOMEM;
1085 goto free_dev;
1088 /* Initialize cdev and device structures for control device */
1089 rc = device_init_cdev(hdev, hclass, hdev->id_control, &hl_ctrl_ops,
1090 name, &hdev->cdev_ctrl, &hdev->dev_ctrl);
1092 kfree(name);
1094 if (rc)
1095 goto free_dev;
1097 /* Initialize ASIC function pointers and perform early init */
1098 rc = device_early_init(hdev);
1099 if (rc)
1100 goto free_dev_ctrl;
1103 * Start calling ASIC initialization. First S/W then H/W and finally
1104 * late init
1106 rc = hdev->asic_funcs->sw_init(hdev);
1107 if (rc)
1108 goto early_fini;
1111 * Initialize the H/W queues. Must be done before hw_init, because
1112 * there the addresses of the kernel queue are being written to the
1113 * registers of the device
1115 rc = hl_hw_queues_create(hdev);
1116 if (rc) {
1117 dev_err(hdev->dev, "failed to initialize kernel queues\n");
1118 goto sw_fini;
1122 * Initialize the completion queues. Must be done before hw_init,
1123 * because there the addresses of the completion queues are being
1124 * passed as arguments to request_irq
1126 hdev->completion_queue =
1127 kcalloc(hdev->asic_prop.completion_queues_count,
1128 sizeof(*hdev->completion_queue), GFP_KERNEL);
1130 if (!hdev->completion_queue) {
1131 dev_err(hdev->dev, "failed to allocate completion queues\n");
1132 rc = -ENOMEM;
1133 goto hw_queues_destroy;
1136 for (i = 0, cq_ready_cnt = 0;
1137 i < hdev->asic_prop.completion_queues_count;
1138 i++, cq_ready_cnt++) {
1139 rc = hl_cq_init(hdev, &hdev->completion_queue[i], i);
1140 if (rc) {
1141 dev_err(hdev->dev,
1142 "failed to initialize completion queue\n");
1143 goto cq_fini;
1148 * Initialize the event queue. Must be done before hw_init,
1149 * because there the address of the event queue is being
1150 * passed as argument to request_irq
1152 rc = hl_eq_init(hdev, &hdev->event_queue);
1153 if (rc) {
1154 dev_err(hdev->dev, "failed to initialize event queue\n");
1155 goto cq_fini;
1158 /* MMU S/W must be initialized before kernel context is created */
1159 rc = hl_mmu_init(hdev);
1160 if (rc) {
1161 dev_err(hdev->dev, "Failed to initialize MMU S/W structures\n");
1162 goto eq_fini;
1165 /* Allocate the kernel context */
1166 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
1167 if (!hdev->kernel_ctx) {
1168 rc = -ENOMEM;
1169 goto mmu_fini;
1172 hdev->compute_ctx = NULL;
1174 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
1175 if (rc) {
1176 dev_err(hdev->dev, "failed to initialize kernel context\n");
1177 kfree(hdev->kernel_ctx);
1178 goto mmu_fini;
1181 rc = hl_cb_pool_init(hdev);
1182 if (rc) {
1183 dev_err(hdev->dev, "failed to initialize CB pool\n");
1184 goto release_ctx;
1187 hl_debugfs_add_device(hdev);
1189 if (hdev->asic_funcs->get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
1190 dev_info(hdev->dev,
1191 "H/W state is dirty, must reset before initializing\n");
1192 hdev->asic_funcs->hw_fini(hdev, true);
1196 * From this point, in case of an error, add char devices and create
1197 * sysfs nodes as part of the error flow, to allow debugging.
1199 add_cdev_sysfs_on_err = true;
1201 rc = hdev->asic_funcs->hw_init(hdev);
1202 if (rc) {
1203 dev_err(hdev->dev, "failed to initialize the H/W\n");
1204 rc = 0;
1205 goto out_disabled;
1208 hdev->disabled = false;
1210 /* Check that the communication with the device is working */
1211 rc = hdev->asic_funcs->test_queues(hdev);
1212 if (rc) {
1213 dev_err(hdev->dev, "Failed to detect if device is alive\n");
1214 rc = 0;
1215 goto out_disabled;
1218 rc = device_late_init(hdev);
1219 if (rc) {
1220 dev_err(hdev->dev, "Failed late initialization\n");
1221 rc = 0;
1222 goto out_disabled;
1225 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
1226 hdev->asic_name,
1227 hdev->asic_prop.dram_size / 1024 / 1024 / 1024);
1229 rc = hl_vm_init(hdev);
1230 if (rc) {
1231 dev_err(hdev->dev, "Failed to initialize memory module\n");
1232 rc = 0;
1233 goto out_disabled;
1237 * Expose devices and sysfs nodes to user.
1238 * From here there is no need to add char devices and create sysfs nodes
1239 * in case of an error.
1241 add_cdev_sysfs_on_err = false;
1242 rc = device_cdev_sysfs_add(hdev);
1243 if (rc) {
1244 dev_err(hdev->dev,
1245 "Failed to add char devices and sysfs nodes\n");
1246 rc = 0;
1247 goto out_disabled;
1251 * hl_hwmon_init() must be called after device_late_init(), because only
1252 * there we get the information from the device about which
1253 * hwmon-related sensors the device supports.
1254 * Furthermore, it must be done after adding the device to the system.
1256 rc = hl_hwmon_init(hdev);
1257 if (rc) {
1258 dev_err(hdev->dev, "Failed to initialize hwmon\n");
1259 rc = 0;
1260 goto out_disabled;
1263 dev_notice(hdev->dev,
1264 "Successfully added device to habanalabs driver\n");
1266 hdev->init_done = true;
1268 return 0;
1270 release_ctx:
1271 if (hl_ctx_put(hdev->kernel_ctx) != 1)
1272 dev_err(hdev->dev,
1273 "kernel ctx is still alive on initialization failure\n");
1274 mmu_fini:
1275 hl_mmu_fini(hdev);
1276 eq_fini:
1277 hl_eq_fini(hdev, &hdev->event_queue);
1278 cq_fini:
1279 for (i = 0 ; i < cq_ready_cnt ; i++)
1280 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1281 kfree(hdev->completion_queue);
1282 hw_queues_destroy:
1283 hl_hw_queues_destroy(hdev);
1284 sw_fini:
1285 hdev->asic_funcs->sw_fini(hdev);
1286 early_fini:
1287 device_early_fini(hdev);
1288 free_dev_ctrl:
1289 kfree(hdev->dev_ctrl);
1290 free_dev:
1291 kfree(hdev->dev);
1292 out_disabled:
1293 hdev->disabled = true;
1294 if (add_cdev_sysfs_on_err)
1295 device_cdev_sysfs_add(hdev);
1296 if (hdev->pdev)
1297 dev_err(&hdev->pdev->dev,
1298 "Failed to initialize hl%d. Device is NOT usable !\n",
1299 hdev->id / 2);
1300 else
1301 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
1302 hdev->id / 2);
1304 return rc;
1308 * hl_device_fini - main tear-down function for habanalabs device
1310 * @hdev: pointer to habanalabs device structure
1312 * Destroy the device, call ASIC fini functions and release the id
1314 void hl_device_fini(struct hl_device *hdev)
1316 int i, rc;
1317 ktime_t timeout;
1319 dev_info(hdev->dev, "Removing device\n");
1322 * This function is competing with the reset function, so try to
1323 * take the reset atomic and if we are already in middle of reset,
1324 * wait until reset function is finished. Reset function is designed
1325 * to always finish (could take up to a few seconds in worst case).
1328 timeout = ktime_add_us(ktime_get(),
1329 HL_PENDING_RESET_PER_SEC * 1000 * 1000 * 4);
1330 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1331 while (rc) {
1332 usleep_range(50, 200);
1333 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
1334 if (ktime_compare(ktime_get(), timeout) > 0) {
1335 WARN(1, "Failed to remove device because reset function did not finish\n");
1336 return;
1340 /* Mark device as disabled */
1341 hdev->disabled = true;
1343 /* Flush anyone that is inside the critical section of enqueue
1344 * jobs to the H/W
1346 hdev->asic_funcs->hw_queues_lock(hdev);
1347 hdev->asic_funcs->hw_queues_unlock(hdev);
1349 /* Flush anyone that is inside device open */
1350 mutex_lock(&hdev->fpriv_list_lock);
1351 mutex_unlock(&hdev->fpriv_list_lock);
1353 hdev->hard_reset_pending = true;
1355 hl_hwmon_fini(hdev);
1357 device_late_fini(hdev);
1359 hl_debugfs_remove_device(hdev);
1362 * Halt the engines and disable interrupts so we won't get any more
1363 * completions from H/W and we won't have any accesses from the
1364 * H/W to the host machine
1366 hdev->asic_funcs->halt_engines(hdev, true);
1368 /* Go over all the queues, release all CS and their jobs */
1369 hl_cs_rollback_all(hdev);
1371 /* Kill processes here after CS rollback. This is because the process
1372 * can't really exit until all its CSs are done, which is what we
1373 * do in cs rollback
1375 device_kill_open_processes(hdev);
1377 hl_cb_pool_fini(hdev);
1379 /* Release kernel context */
1380 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
1381 dev_err(hdev->dev, "kernel ctx is still alive\n");
1383 /* Reset the H/W. It will be in idle state after this returns */
1384 hdev->asic_funcs->hw_fini(hdev, true);
1386 hl_vm_fini(hdev);
1388 hl_mmu_fini(hdev);
1390 hl_eq_fini(hdev, &hdev->event_queue);
1392 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1393 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1394 kfree(hdev->completion_queue);
1396 hl_hw_queues_destroy(hdev);
1398 /* Call ASIC S/W finalize function */
1399 hdev->asic_funcs->sw_fini(hdev);
1401 device_early_fini(hdev);
1403 /* Hide devices and sysfs nodes from user */
1404 device_cdev_sysfs_del(hdev);
1406 pr_info("removed device successfully\n");
1410 * MMIO register access helper functions.
1414 * hl_rreg - Read an MMIO register
1416 * @hdev: pointer to habanalabs device structure
1417 * @reg: MMIO register offset (in bytes)
1419 * Returns the value of the MMIO register we are asked to read
1422 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1424 return readl(hdev->rmmio + reg);
1428 * hl_wreg - Write to an MMIO register
1430 * @hdev: pointer to habanalabs device structure
1431 * @reg: MMIO register offset (in bytes)
1432 * @val: 32-bit value
1434 * Writes the 32-bit value into the MMIO register
1437 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1439 writel(val, hdev->rmmio + reg);