1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
11 #include <linux/sched/signal.h>
12 #include <linux/hwmon.h>
14 bool hl_device_disabled_or_in_reset(struct hl_device *hdev)
16 if ((hdev->disabled) || (atomic_read(&hdev->in_reset)))
22 static void hpriv_release(struct kref *ref)
24 struct hl_fpriv *hpriv;
25 struct hl_device *hdev;
27 hpriv = container_of(ref, struct hl_fpriv, refcount);
31 put_pid(hpriv->taskpid);
33 mutex_destroy(&hpriv->restore_phase_mutex);
37 /* Now the FD is really closed */
38 atomic_dec(&hdev->fd_open_cnt);
40 /* This allows a new user context to open the device */
41 hdev->user_ctx = NULL;
44 void hl_hpriv_get(struct hl_fpriv *hpriv)
46 kref_get(&hpriv->refcount);
49 void hl_hpriv_put(struct hl_fpriv *hpriv)
51 kref_put(&hpriv->refcount, hpriv_release);
55 * hl_device_release - release function for habanalabs device
57 * @inode: pointer to inode structure
58 * @filp: pointer to file structure
60 * Called when process closes an habanalabs device
62 static int hl_device_release(struct inode *inode, struct file *filp)
64 struct hl_fpriv *hpriv = filp->private_data;
66 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
67 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
69 filp->private_data = NULL;
77 * hl_mmap - mmap function for habanalabs device
79 * @*filp: pointer to file structure
80 * @*vma: pointer to vm_area_struct of the process
82 * Called when process does an mmap on habanalabs device. Call the device's mmap
83 * function at the end of the common code.
85 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
87 struct hl_fpriv *hpriv = filp->private_data;
89 if ((vma->vm_pgoff & HL_MMAP_CB_MASK) == HL_MMAP_CB_MASK) {
90 vma->vm_pgoff ^= HL_MMAP_CB_MASK;
91 return hl_cb_mmap(hpriv, vma);
94 return hpriv->hdev->asic_funcs->mmap(hpriv, vma);
97 static const struct file_operations hl_ops = {
99 .open = hl_device_open,
100 .release = hl_device_release,
102 .unlocked_ioctl = hl_ioctl,
103 .compat_ioctl = hl_ioctl
107 * device_setup_cdev - setup cdev and device for habanalabs device
109 * @hdev: pointer to habanalabs device structure
110 * @hclass: pointer to the class object of the device
111 * @minor: minor number of the specific device
112 * @fpos : file operations to install for this device
114 * Create a cdev and a Linux device for habanalabs's device. Need to be
115 * called at the end of the habanalabs device initialization process,
116 * because this function exposes the device to the user
118 static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
119 int minor, const struct file_operations *fops)
121 int err, devno = MKDEV(hdev->major, minor);
122 struct cdev *hdev_cdev = &hdev->cdev;
125 name = kasprintf(GFP_KERNEL, "hl%d", hdev->id);
129 cdev_init(hdev_cdev, fops);
130 hdev_cdev->owner = THIS_MODULE;
131 err = cdev_add(hdev_cdev, devno, 1);
133 pr_err("Failed to add char device %s\n", name);
137 hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
138 if (IS_ERR(hdev->dev)) {
139 pr_err("Failed to create device %s\n", name);
140 err = PTR_ERR(hdev->dev);
141 goto err_device_create;
144 dev_set_drvdata(hdev->dev, hdev);
158 * device_early_init - do some early initialization for the habanalabs device
160 * @hdev: pointer to habanalabs device structure
162 * Install the relevant function pointers and call the early_init function,
163 * if such a function exists
165 static int device_early_init(struct hl_device *hdev)
169 switch (hdev->asic_type) {
171 goya_set_asic_funcs(hdev);
172 strlcpy(hdev->asic_name, "GOYA", sizeof(hdev->asic_name));
175 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
180 rc = hdev->asic_funcs->early_init(hdev);
184 rc = hl_asid_init(hdev);
188 hdev->cq_wq = alloc_workqueue("hl-free-jobs", WQ_UNBOUND, 0);
189 if (hdev->cq_wq == NULL) {
190 dev_err(hdev->dev, "Failed to allocate CQ workqueue\n");
195 hdev->eq_wq = alloc_workqueue("hl-events", WQ_UNBOUND, 0);
196 if (hdev->eq_wq == NULL) {
197 dev_err(hdev->dev, "Failed to allocate EQ workqueue\n");
202 hdev->hl_chip_info = kzalloc(sizeof(struct hwmon_chip_info),
204 if (!hdev->hl_chip_info) {
209 hl_cb_mgr_init(&hdev->kernel_cb_mgr);
211 mutex_init(&hdev->fd_open_cnt_lock);
212 mutex_init(&hdev->send_cpu_message_lock);
213 INIT_LIST_HEAD(&hdev->hw_queues_mirror_list);
214 spin_lock_init(&hdev->hw_queues_mirror_lock);
215 atomic_set(&hdev->in_reset, 0);
216 atomic_set(&hdev->fd_open_cnt, 0);
221 destroy_workqueue(hdev->eq_wq);
223 destroy_workqueue(hdev->cq_wq);
227 if (hdev->asic_funcs->early_fini)
228 hdev->asic_funcs->early_fini(hdev);
234 * device_early_fini - finalize all that was done in device_early_init
236 * @hdev: pointer to habanalabs device structure
239 static void device_early_fini(struct hl_device *hdev)
241 mutex_destroy(&hdev->send_cpu_message_lock);
243 hl_cb_mgr_fini(hdev, &hdev->kernel_cb_mgr);
245 kfree(hdev->hl_chip_info);
247 destroy_workqueue(hdev->eq_wq);
248 destroy_workqueue(hdev->cq_wq);
252 if (hdev->asic_funcs->early_fini)
253 hdev->asic_funcs->early_fini(hdev);
255 mutex_destroy(&hdev->fd_open_cnt_lock);
258 static void set_freq_to_low_job(struct work_struct *work)
260 struct hl_device *hdev = container_of(work, struct hl_device,
263 if (atomic_read(&hdev->fd_open_cnt) == 0)
264 hl_device_set_frequency(hdev, PLL_LOW);
266 schedule_delayed_work(&hdev->work_freq,
267 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
270 static void hl_device_heartbeat(struct work_struct *work)
272 struct hl_device *hdev = container_of(work, struct hl_device,
273 work_heartbeat.work);
275 if (hl_device_disabled_or_in_reset(hdev))
278 if (!hdev->asic_funcs->send_heartbeat(hdev))
281 dev_err(hdev->dev, "Device heartbeat failed!\n");
282 hl_device_reset(hdev, true, false);
287 schedule_delayed_work(&hdev->work_heartbeat,
288 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
292 * device_late_init - do late stuff initialization for the habanalabs device
294 * @hdev: pointer to habanalabs device structure
296 * Do stuff that either needs the device H/W queues to be active or needs
297 * to happen after all the rest of the initialization is finished
299 static int device_late_init(struct hl_device *hdev)
303 INIT_DELAYED_WORK(&hdev->work_freq, set_freq_to_low_job);
304 hdev->high_pll = hdev->asic_prop.high_pll;
306 /* force setting to low frequency */
307 atomic_set(&hdev->curr_pll_profile, PLL_LOW);
309 if (hdev->pm_mng_profile == PM_AUTO)
310 hdev->asic_funcs->set_pll_profile(hdev, PLL_LOW);
312 hdev->asic_funcs->set_pll_profile(hdev, PLL_LAST);
314 if (hdev->asic_funcs->late_init) {
315 rc = hdev->asic_funcs->late_init(hdev);
318 "failed late initialization for the H/W\n");
323 schedule_delayed_work(&hdev->work_freq,
324 usecs_to_jiffies(HL_PLL_LOW_JOB_FREQ_USEC));
326 if (hdev->heartbeat) {
327 INIT_DELAYED_WORK(&hdev->work_heartbeat, hl_device_heartbeat);
328 schedule_delayed_work(&hdev->work_heartbeat,
329 usecs_to_jiffies(HL_HEARTBEAT_PER_USEC));
332 hdev->late_init_done = true;
338 * device_late_fini - finalize all that was done in device_late_init
340 * @hdev: pointer to habanalabs device structure
343 static void device_late_fini(struct hl_device *hdev)
345 if (!hdev->late_init_done)
348 cancel_delayed_work_sync(&hdev->work_freq);
350 cancel_delayed_work_sync(&hdev->work_heartbeat);
352 if (hdev->asic_funcs->late_fini)
353 hdev->asic_funcs->late_fini(hdev);
355 hdev->late_init_done = false;
359 * hl_device_set_frequency - set the frequency of the device
361 * @hdev: pointer to habanalabs device structure
362 * @freq: the new frequency value
364 * Change the frequency if needed.
365 * We allose to set PLL to low only if there is no user process
366 * Returns 0 if no change was done, otherwise returns 1;
368 int hl_device_set_frequency(struct hl_device *hdev, enum hl_pll_frequency freq)
370 enum hl_pll_frequency old_freq =
371 (freq == PLL_HIGH) ? PLL_LOW : PLL_HIGH;
374 if (hdev->pm_mng_profile == PM_MANUAL)
377 ret = atomic_cmpxchg(&hdev->curr_pll_profile, old_freq, freq);
382 * in case we want to lower frequency, check if device is not
383 * opened. We must have a check here to workaround race condition with
386 if ((freq == PLL_LOW) && (atomic_read(&hdev->fd_open_cnt) > 0)) {
387 atomic_set(&hdev->curr_pll_profile, PLL_HIGH);
391 dev_dbg(hdev->dev, "Changing device frequency to %s\n",
392 freq == PLL_HIGH ? "high" : "low");
394 hdev->asic_funcs->set_pll_profile(hdev, freq);
400 * hl_device_suspend - initiate device suspend
402 * @hdev: pointer to habanalabs device structure
404 * Puts the hw in the suspend state (all asics).
405 * Returns 0 for success or an error on failure.
406 * Called at driver suspend.
408 int hl_device_suspend(struct hl_device *hdev)
412 pci_save_state(hdev->pdev);
414 rc = hdev->asic_funcs->suspend(hdev);
417 "Failed to disable PCI access of device CPU\n");
419 /* Shut down the device */
420 pci_disable_device(hdev->pdev);
421 pci_set_power_state(hdev->pdev, PCI_D3hot);
427 * hl_device_resume - initiate device resume
429 * @hdev: pointer to habanalabs device structure
431 * Bring the hw back to operating state (all asics).
432 * Returns 0 for success or an error on failure.
433 * Called at driver resume.
435 int hl_device_resume(struct hl_device *hdev)
439 pci_set_power_state(hdev->pdev, PCI_D0);
440 pci_restore_state(hdev->pdev);
441 rc = pci_enable_device(hdev->pdev);
444 "Failed to enable PCI device in resume\n");
448 rc = hdev->asic_funcs->resume(hdev);
451 "Failed to enable PCI access from device CPU\n");
458 static void hl_device_hard_reset_pending(struct work_struct *work)
460 struct hl_device_reset_work *device_reset_work =
461 container_of(work, struct hl_device_reset_work, reset_work);
462 struct hl_device *hdev = device_reset_work->hdev;
463 u16 pending_cnt = HL_PENDING_RESET_PER_SEC;
464 struct task_struct *task = NULL;
466 /* Flush all processes that are inside hl_open */
467 mutex_lock(&hdev->fd_open_cnt_lock);
469 while ((atomic_read(&hdev->fd_open_cnt)) && (pending_cnt)) {
474 "Can't HARD reset, waiting for user to close FD\n");
478 if (atomic_read(&hdev->fd_open_cnt)) {
479 task = get_pid_task(hdev->user_ctx->hpriv->taskpid,
482 dev_info(hdev->dev, "Killing user processes\n");
483 send_sig(SIGKILL, task, 1);
486 put_task_struct(task);
490 mutex_unlock(&hdev->fd_open_cnt_lock);
492 hl_device_reset(hdev, true, true);
494 kfree(device_reset_work);
498 * hl_device_reset - reset the device
500 * @hdev: pointer to habanalabs device structure
501 * @hard_reset: should we do hard reset to all engines or just reset the
502 * compute/dma engines
504 * Block future CS and wait for pending CS to be enqueued
506 * Flush all completions
507 * Re-initialize all internal data structures
508 * Call ASIC H/W init, late_init
512 * Returns 0 for success or an error on failure.
514 int hl_device_reset(struct hl_device *hdev, bool hard_reset,
515 bool from_hard_reset_thread)
519 if (!hdev->init_done) {
521 "Can't reset before initialization is done\n");
526 * Prevent concurrency in this function - only one reset should be
527 * done at any given time. Only need to perform this if we didn't
528 * get from the dedicated hard reset thread
530 if (!from_hard_reset_thread) {
531 /* Block future CS/VM/JOB completion operations */
532 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
536 /* This also blocks future CS/VM/JOB completion operations */
537 hdev->disabled = true;
540 * Flush anyone that is inside the critical section of enqueue
543 hdev->asic_funcs->hw_queues_lock(hdev);
544 hdev->asic_funcs->hw_queues_unlock(hdev);
546 dev_err(hdev->dev, "Going to RESET device!\n");
550 if ((hard_reset) && (!from_hard_reset_thread)) {
551 struct hl_device_reset_work *device_reset_work;
555 "Reset action is NOT supported in simulator\n");
560 hdev->hard_reset_pending = true;
562 device_reset_work = kzalloc(sizeof(*device_reset_work),
564 if (!device_reset_work) {
570 * Because the reset function can't run from interrupt or
571 * from heartbeat work, we need to call the reset function
572 * from a dedicated work
574 INIT_WORK(&device_reset_work->reset_work,
575 hl_device_hard_reset_pending);
576 device_reset_work->hdev = hdev;
577 schedule_work(&device_reset_work->reset_work);
583 device_late_fini(hdev);
586 * Now that the heartbeat thread is closed, flush processes
587 * which are sending messages to CPU
589 mutex_lock(&hdev->send_cpu_message_lock);
590 mutex_unlock(&hdev->send_cpu_message_lock);
594 * Halt the engines and disable interrupts so we won't get any more
595 * completions from H/W and we won't have any accesses from the
596 * H/W to the host machine
598 hdev->asic_funcs->halt_engines(hdev, hard_reset);
600 /* Go over all the queues, release all CS and their jobs */
601 hl_cs_rollback_all(hdev);
604 /* Release kernel context */
605 if (hl_ctx_put(hdev->kernel_ctx) != 1) {
607 "kernel ctx is alive during hard reset\n");
612 hdev->kernel_ctx = NULL;
615 /* Reset the H/W. It will be in idle state after this returns */
616 hdev->asic_funcs->hw_fini(hdev, hard_reset);
620 hl_eq_reset(hdev, &hdev->event_queue);
623 /* Re-initialize PI,CI to 0 in all queues (hw queue, cq) */
624 hl_hw_queue_reset(hdev, hard_reset);
625 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
626 hl_cq_reset(hdev, &hdev->completion_queue[i]);
628 /* Make sure the setup phase for the user context will run again */
629 if (hdev->user_ctx) {
630 atomic_set(&hdev->user_ctx->thread_restore_token, 1);
631 hdev->user_ctx->thread_restore_wait_token = 0;
634 /* Finished tear-down, starting to re-initialize */
637 /* Allocate the kernel context */
638 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx),
640 if (!hdev->kernel_ctx) {
645 hdev->user_ctx = NULL;
647 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
650 "failed to init kernel ctx in hard reset\n");
651 kfree(hdev->kernel_ctx);
652 hdev->kernel_ctx = NULL;
657 rc = hdev->asic_funcs->hw_init(hdev);
660 "failed to initialize the H/W after reset\n");
664 hdev->disabled = false;
666 /* Check that the communication with the device is working */
667 rc = hdev->asic_funcs->test_queues(hdev);
670 "Failed to detect if device is alive after reset\n");
675 rc = device_late_init(hdev);
678 "Failed late init after hard reset\n");
682 rc = hl_vm_init(hdev);
685 "Failed to init memory module after hard reset\n");
689 hl_set_max_power(hdev, hdev->max_power);
691 hdev->hard_reset_pending = false;
693 rc = hdev->asic_funcs->soft_reset_late_init(hdev);
696 "Failed late init after soft reset\n");
701 atomic_set(&hdev->in_reset, 0);
704 hdev->hard_reset_cnt++;
706 hdev->soft_reset_cnt++;
711 hdev->disabled = true;
715 "Failed to reset! Device is NOT usable\n");
716 hdev->hard_reset_cnt++;
719 "Failed to do soft-reset, trying hard reset\n");
720 hdev->soft_reset_cnt++;
725 atomic_set(&hdev->in_reset, 0);
731 * hl_device_init - main initialization function for habanalabs device
733 * @hdev: pointer to habanalabs device structure
735 * Allocate an id for the device, do early initialization and then call the
736 * ASIC specific initialization functions. Finally, create the cdev and the
737 * Linux device to expose it to the user
739 int hl_device_init(struct hl_device *hdev, struct class *hclass)
741 int i, rc, cq_ready_cnt;
744 rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops);
749 /* Initialize ASIC function pointers and perform early init */
750 rc = device_early_init(hdev);
755 * Start calling ASIC initialization. First S/W then H/W and finally
758 rc = hdev->asic_funcs->sw_init(hdev);
763 * Initialize the H/W queues. Must be done before hw_init, because
764 * there the addresses of the kernel queue are being written to the
765 * registers of the device
767 rc = hl_hw_queues_create(hdev);
769 dev_err(hdev->dev, "failed to initialize kernel queues\n");
774 * Initialize the completion queues. Must be done before hw_init,
775 * because there the addresses of the completion queues are being
776 * passed as arguments to request_irq
778 hdev->completion_queue =
779 kcalloc(hdev->asic_prop.completion_queues_count,
780 sizeof(*hdev->completion_queue), GFP_KERNEL);
782 if (!hdev->completion_queue) {
783 dev_err(hdev->dev, "failed to allocate completion queues\n");
785 goto hw_queues_destroy;
788 for (i = 0, cq_ready_cnt = 0;
789 i < hdev->asic_prop.completion_queues_count;
790 i++, cq_ready_cnt++) {
791 rc = hl_cq_init(hdev, &hdev->completion_queue[i], i);
794 "failed to initialize completion queue\n");
800 * Initialize the event queue. Must be done before hw_init,
801 * because there the address of the event queue is being
802 * passed as argument to request_irq
804 rc = hl_eq_init(hdev, &hdev->event_queue);
806 dev_err(hdev->dev, "failed to initialize event queue\n");
810 /* Allocate the kernel context */
811 hdev->kernel_ctx = kzalloc(sizeof(*hdev->kernel_ctx), GFP_KERNEL);
812 if (!hdev->kernel_ctx) {
817 hdev->user_ctx = NULL;
819 rc = hl_ctx_init(hdev, hdev->kernel_ctx, true);
821 dev_err(hdev->dev, "failed to initialize kernel context\n");
825 rc = hl_cb_pool_init(hdev);
827 dev_err(hdev->dev, "failed to initialize CB pool\n");
831 rc = hl_sysfs_init(hdev);
833 dev_err(hdev->dev, "failed to initialize sysfs\n");
837 if (hdev->asic_funcs->get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
839 "H/W state is dirty, must reset before initializing\n");
840 hdev->asic_funcs->hw_fini(hdev, true);
843 rc = hdev->asic_funcs->hw_init(hdev);
845 dev_err(hdev->dev, "failed to initialize the H/W\n");
850 hdev->disabled = false;
852 /* Check that the communication with the device is working */
853 rc = hdev->asic_funcs->test_queues(hdev);
855 dev_err(hdev->dev, "Failed to detect if device is alive\n");
860 /* After test_queues, KMD can start sending messages to device CPU */
862 rc = device_late_init(hdev);
864 dev_err(hdev->dev, "Failed late initialization\n");
869 dev_info(hdev->dev, "Found %s device with %lluGB DRAM\n",
871 hdev->asic_prop.dram_size / 1024 / 1024 / 1024);
873 rc = hl_vm_init(hdev);
875 dev_err(hdev->dev, "Failed to initialize memory module\n");
881 * hl_hwmon_init must be called after device_late_init, because only
882 * there we get the information from the device about which
883 * hwmon-related sensors the device supports
885 rc = hl_hwmon_init(hdev);
887 dev_err(hdev->dev, "Failed to initialize hwmon\n");
892 dev_notice(hdev->dev,
893 "Successfully added device to habanalabs driver\n");
895 hdev->init_done = true;
900 hl_cb_pool_fini(hdev);
902 if (hl_ctx_put(hdev->kernel_ctx) != 1)
904 "kernel ctx is still alive on initialization failure\n");
906 kfree(hdev->kernel_ctx);
908 hl_eq_fini(hdev, &hdev->event_queue);
910 for (i = 0 ; i < cq_ready_cnt ; i++)
911 hl_cq_fini(hdev, &hdev->completion_queue[i]);
912 kfree(hdev->completion_queue);
914 hl_hw_queues_destroy(hdev);
916 hdev->asic_funcs->sw_fini(hdev);
918 device_early_fini(hdev);
920 device_destroy(hclass, hdev->dev->devt);
921 cdev_del(&hdev->cdev);
923 hdev->disabled = true;
925 dev_err(&hdev->pdev->dev,
926 "Failed to initialize hl%d. Device is NOT usable !\n",
929 pr_err("Failed to initialize hl%d. Device is NOT usable !\n",
936 * hl_device_fini - main tear-down function for habanalabs device
938 * @hdev: pointer to habanalabs device structure
940 * Destroy the device, call ASIC fini functions and release the id
942 void hl_device_fini(struct hl_device *hdev)
947 dev_info(hdev->dev, "Removing device\n");
950 * This function is competing with the reset function, so try to
951 * take the reset atomic and if we are already in middle of reset,
952 * wait until reset function is finished. Reset function is designed
953 * to always finish (could take up to a few seconds in worst case).
956 timeout = ktime_add_us(ktime_get(),
957 HL_PENDING_RESET_PER_SEC * 1000 * 1000 * 4);
958 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
960 usleep_range(50, 200);
961 rc = atomic_cmpxchg(&hdev->in_reset, 0, 1);
962 if (ktime_compare(ktime_get(), timeout) > 0) {
963 WARN(1, "Failed to remove device because reset function did not finish\n");
968 /* Mark device as disabled */
969 hdev->disabled = true;
973 device_late_fini(hdev);
978 * Halt the engines and disable interrupts so we won't get any more
979 * completions from H/W and we won't have any accesses from the
980 * H/W to the host machine
982 hdev->asic_funcs->halt_engines(hdev, true);
984 /* Go over all the queues, release all CS and their jobs */
985 hl_cs_rollback_all(hdev);
987 hl_cb_pool_fini(hdev);
989 /* Release kernel context */
990 if ((hdev->kernel_ctx) && (hl_ctx_put(hdev->kernel_ctx) != 1))
991 dev_err(hdev->dev, "kernel ctx is still alive\n");
993 /* Reset the H/W. It will be in idle state after this returns */
994 hdev->asic_funcs->hw_fini(hdev, true);
998 hl_eq_fini(hdev, &hdev->event_queue);
1000 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1001 hl_cq_fini(hdev, &hdev->completion_queue[i]);
1002 kfree(hdev->completion_queue);
1004 hl_hw_queues_destroy(hdev);
1006 /* Call ASIC S/W finalize function */
1007 hdev->asic_funcs->sw_fini(hdev);
1009 device_early_fini(hdev);
1011 /* Hide device from user */
1012 device_destroy(hdev->dev->class, hdev->dev->devt);
1013 cdev_del(&hdev->cdev);
1015 pr_info("removed device successfully\n");
1019 * hl_poll_timeout_memory - Periodically poll a host memory address
1020 * until it is not zero or a timeout occurs
1021 * @hdev: pointer to habanalabs device structure
1022 * @addr: Address to poll
1023 * @timeout_us: timeout in us
1024 * @val: Variable to read the value into
1026 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
1027 * case, the last read value at @addr is stored in @val. Must not
1028 * be called from atomic context if sleep_us or timeout_us are used.
1030 * The function sleeps for 100us with timeout value of
1033 int hl_poll_timeout_memory(struct hl_device *hdev, u64 addr,
1034 u32 timeout_us, u32 *val)
1037 * address in this function points always to a memory location in the
1038 * host's (server's) memory. That location is updated asynchronously
1039 * either by the direct access of the device or by another core
1041 u32 *paddr = (u32 *) (uintptr_t) addr;
1042 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
1048 * Flush CPU read/write buffers to make sure we read updates
1049 * done by other cores or by the device
1055 if (ktime_compare(ktime_get(), timeout) > 0) {
1059 usleep_range((100 >> 2) + 1, 100);
1062 return *val ? 0 : -ETIMEDOUT;
1066 * hl_poll_timeout_devicememory - Periodically poll a device memory address
1067 * until it is not zero or a timeout occurs
1068 * @hdev: pointer to habanalabs device structure
1069 * @addr: Device address to poll
1070 * @timeout_us: timeout in us
1071 * @val: Variable to read the value into
1073 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
1074 * case, the last read value at @addr is stored in @val. Must not
1075 * be called from atomic context if sleep_us or timeout_us are used.
1077 * The function sleeps for 100us with timeout value of
1080 int hl_poll_timeout_device_memory(struct hl_device *hdev, void __iomem *addr,
1081 u32 timeout_us, u32 *val)
1083 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
1091 if (ktime_compare(ktime_get(), timeout) > 0) {
1095 usleep_range((100 >> 2) + 1, 100);
1098 return *val ? 0 : -ETIMEDOUT;
1102 * MMIO register access helper functions.
1106 * hl_rreg - Read an MMIO register
1108 * @hdev: pointer to habanalabs device structure
1109 * @reg: MMIO register offset (in bytes)
1111 * Returns the value of the MMIO register we are asked to read
1114 inline u32 hl_rreg(struct hl_device *hdev, u32 reg)
1116 return readl(hdev->rmmio + reg);
1120 * hl_wreg - Write to an MMIO register
1122 * @hdev: pointer to habanalabs device structure
1123 * @reg: MMIO register offset (in bytes)
1124 * @val: 32-bit value
1126 * Writes the 32-bit value into the MMIO register
1129 inline void hl_wreg(struct hl_device *hdev, u32 reg, u32 val)
1131 writel(val, hdev->rmmio + reg);