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
)
254 switch (hdev
->asic_type
) {
256 goya_set_asic_funcs(hdev
);
257 strlcpy(hdev
->asic_name
, "GOYA", sizeof(hdev
->asic_name
));
260 dev_err(hdev
->dev
, "Unrecognized ASIC type %d\n",
265 rc
= hdev
->asic_funcs
->early_init(hdev
);
269 rc
= hl_asid_init(hdev
);
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");
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");
287 hdev
->hl_chip_info
= kzalloc(sizeof(struct hwmon_chip_info
),
289 if (!hdev
->hl_chip_info
) {
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
) {
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);
316 kfree(hdev
->hl_chip_info
);
318 destroy_workqueue(hdev
->eq_wq
);
320 destroy_workqueue(hdev
->cq_wq
);
324 if (hdev
->asic_funcs
->early_fini
)
325 hdev
->asic_funcs
->early_fini(hdev
);
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
);
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
,
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
))
382 if (!hdev
->asic_funcs
->send_heartbeat(hdev
))
385 dev_err(hdev
->dev
, "Device heartbeat failed!\n");
386 hl_device_reset(hdev
, true, false);
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
)
407 if (hdev
->asic_funcs
->late_init
) {
408 rc
= hdev
->asic_funcs
->late_init(hdev
);
411 "failed late initialization for the H/W\n");
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
);
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;
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
)
452 cancel_delayed_work_sync(&hdev
->work_freq
);
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
)) {
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
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
,
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
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
)
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
);
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
));
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
;
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
];
552 total_busy_time_ms
= DIV_ROUND_UP_ULL(total_busy_time_us
,
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
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
))
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
;
587 int hl_device_set_debug_mode(struct hl_device
*hdev
, bool enable
)
591 mutex_lock(&hdev
->debug_lock
);
594 if (!hdev
->in_debug
) {
596 "Failed to disable debug mode because device was not in debug mode\n");
601 hdev
->asic_funcs
->halt_coresight(hdev
);
607 if (hdev
->in_debug
) {
609 "Failed to enable debug mode because device is already in debug mode\n");
617 mutex_unlock(&hdev
->debug_lock
);
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
)
635 pci_save_state(hdev
->pdev
);
637 /* Block future CS/VM/JOB completion operations */
638 rc
= atomic_cmpxchg(&hdev
->in_reset
, 0, 1);
640 dev_err(hdev
->dev
, "Can't suspend while in reset\n");
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
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
);
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
);
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
)
683 pci_set_power_state(hdev
->pdev
, PCI_D0
);
684 pci_restore_state(hdev
->pdev
);
685 rc
= pci_enable_device_mem(hdev
->pdev
);
688 "Failed to enable PCI device in resume\n");
692 pci_set_master(hdev
->pdev
);
694 rc
= hdev
->asic_funcs
->resume(hdev
);
696 dev_err(hdev
->dev
, "Failed to resume device after suspend\n");
701 hdev
->disabled
= false;
702 atomic_set(&hdev
->in_reset
, 0);
704 rc
= hl_device_reset(hdev
, true, false);
706 dev_err(hdev
->dev
, "Failed to reset device during resume\n");
713 pci_clear_master(hdev
->pdev
);
714 pci_disable_device(hdev
->pdev
);
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
;
726 pending_total
= HL_PLDM_PENDING_RESET_PER_SEC
;
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
))
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
);
744 dev_info(hdev
->dev
, "Killing user process pid=%d\n",
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
761 pending_cnt
= pending_total
;
763 while ((!list_empty(&hdev
->fpriv_list
)) && (pending_cnt
)) {
765 "Waiting for all unmap operations to finish before hard reset\n");
772 if (!list_empty(&hdev
->fpriv_list
))
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
797 * Flush all completions
798 * Re-initialize all internal data structures
799 * Call ASIC H/W init, late_init
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
)
810 if (!hdev
->init_done
) {
812 "Can't reset before initialization is done\n");
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);
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
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");
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
),
851 if (!device_reset_work
) {
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
);
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
);
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
);
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 */
942 hdev
->device_cpu_disabled
= false;
943 hdev
->hard_reset_pending
= false;
945 if (hdev
->kernel_ctx
) {
947 "kernel ctx was alive during hard reset, something is terribly wrong\n");
952 rc
= hl_mmu_init(hdev
);
955 "Failed to initialize MMU S/W after hard reset\n");
959 /* Allocate the kernel context */
960 hdev
->kernel_ctx
= kzalloc(sizeof(*hdev
->kernel_ctx
),
962 if (!hdev
->kernel_ctx
) {
967 hdev
->compute_ctx
= NULL
;
969 rc
= hl_ctx_init(hdev
, hdev
->kernel_ctx
, true);
972 "failed to init kernel ctx in hard reset\n");
973 kfree(hdev
->kernel_ctx
);
974 hdev
->kernel_ctx
= NULL
;
979 rc
= hdev
->asic_funcs
->hw_init(hdev
);
982 "failed to initialize the H/W after reset\n");
986 hdev
->disabled
= false;
988 /* Check that the communication with the device is working */
989 rc
= hdev
->asic_funcs
->test_queues(hdev
);
992 "Failed to detect if device is alive after reset\n");
997 rc
= device_late_init(hdev
);
1000 "Failed late init after hard reset\n");
1004 rc
= hl_vm_init(hdev
);
1007 "Failed to init memory module after hard reset\n");
1011 hl_set_max_power(hdev
, hdev
->max_power
);
1013 rc
= hdev
->asic_funcs
->soft_reset_late_init(hdev
);
1016 "Failed late init after soft reset\n");
1021 atomic_set(&hdev
->in_reset
, 0);
1024 hdev
->hard_reset_cnt
++;
1026 hdev
->soft_reset_cnt
++;
1028 dev_warn(hdev
->dev
, "Successfully finished resetting the device\n");
1033 hdev
->disabled
= true;
1037 "Failed to reset! Device is NOT usable\n");
1038 hdev
->hard_reset_cnt
++;
1041 "Failed to do soft-reset, trying hard reset\n");
1042 hdev
->soft_reset_cnt
++;
1047 atomic_set(&hdev
->in_reset
, 0);
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
;
1065 bool add_cdev_sysfs_on_err
= false;
1067 name
= kasprintf(GFP_KERNEL
, "hl%d", hdev
->id
/ 2);
1073 /* Initialize cdev and device structures */
1074 rc
= device_init_cdev(hdev
, hclass
, hdev
->id
, &hl_ops
, name
,
1075 &hdev
->cdev
, &hdev
->dev
);
1082 name
= kasprintf(GFP_KERNEL
, "hl_controlD%d", hdev
->id
/ 2);
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
);
1097 /* Initialize ASIC function pointers and perform early init */
1098 rc
= device_early_init(hdev
);
1103 * Start calling ASIC initialization. First S/W then H/W and finally
1106 rc
= hdev
->asic_funcs
->sw_init(hdev
);
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
);
1117 dev_err(hdev
->dev
, "failed to initialize kernel queues\n");
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");
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
);
1142 "failed to initialize completion queue\n");
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
);
1154 dev_err(hdev
->dev
, "failed to initialize event queue\n");
1158 /* MMU S/W must be initialized before kernel context is created */
1159 rc
= hl_mmu_init(hdev
);
1161 dev_err(hdev
->dev
, "Failed to initialize MMU S/W structures\n");
1165 /* Allocate the kernel context */
1166 hdev
->kernel_ctx
= kzalloc(sizeof(*hdev
->kernel_ctx
), GFP_KERNEL
);
1167 if (!hdev
->kernel_ctx
) {
1172 hdev
->compute_ctx
= NULL
;
1174 rc
= hl_ctx_init(hdev
, hdev
->kernel_ctx
, true);
1176 dev_err(hdev
->dev
, "failed to initialize kernel context\n");
1177 kfree(hdev
->kernel_ctx
);
1181 rc
= hl_cb_pool_init(hdev
);
1183 dev_err(hdev
->dev
, "failed to initialize CB pool\n");
1187 hl_debugfs_add_device(hdev
);
1189 if (hdev
->asic_funcs
->get_hw_state(hdev
) == HL_DEVICE_HW_STATE_DIRTY
) {
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
);
1203 dev_err(hdev
->dev
, "failed to initialize the H/W\n");
1208 hdev
->disabled
= false;
1210 /* Check that the communication with the device is working */
1211 rc
= hdev
->asic_funcs
->test_queues(hdev
);
1213 dev_err(hdev
->dev
, "Failed to detect if device is alive\n");
1218 rc
= device_late_init(hdev
);
1220 dev_err(hdev
->dev
, "Failed late initialization\n");
1225 dev_info(hdev
->dev
, "Found %s device with %lluGB DRAM\n",
1227 hdev
->asic_prop
.dram_size
/ 1024 / 1024 / 1024);
1229 rc
= hl_vm_init(hdev
);
1231 dev_err(hdev
->dev
, "Failed to initialize memory module\n");
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
);
1245 "Failed to add char devices and sysfs nodes\n");
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
);
1258 dev_err(hdev
->dev
, "Failed to initialize hwmon\n");
1263 dev_notice(hdev
->dev
,
1264 "Successfully added device to habanalabs driver\n");
1266 hdev
->init_done
= true;
1271 if (hl_ctx_put(hdev
->kernel_ctx
) != 1)
1273 "kernel ctx is still alive on initialization failure\n");
1277 hl_eq_fini(hdev
, &hdev
->event_queue
);
1279 for (i
= 0 ; i
< cq_ready_cnt
; i
++)
1280 hl_cq_fini(hdev
, &hdev
->completion_queue
[i
]);
1281 kfree(hdev
->completion_queue
);
1283 hl_hw_queues_destroy(hdev
);
1285 hdev
->asic_funcs
->sw_fini(hdev
);
1287 device_early_fini(hdev
);
1289 kfree(hdev
->dev_ctrl
);
1293 hdev
->disabled
= true;
1294 if (add_cdev_sysfs_on_err
)
1295 device_cdev_sysfs_add(hdev
);
1297 dev_err(&hdev
->pdev
->dev
,
1298 "Failed to initialize hl%d. Device is NOT usable !\n",
1301 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
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
)
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);
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");
1340 /* Mark device as disabled */
1341 hdev
->disabled
= true;
1343 /* Flush anyone that is inside the critical section of enqueue
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
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);
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
);