1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
9 #define pr_fmt(fmt) "habanalabs: " fmt
11 #include "habanalabs.h"
13 #include <linux/pci.h>
14 #include <linux/aer.h>
15 #include <linux/module.h>
17 #define HL_DRIVER_AUTHOR "HabanaLabs Kernel Driver Team"
19 #define HL_DRIVER_DESC "Driver for HabanaLabs's AI Accelerators"
21 MODULE_AUTHOR(HL_DRIVER_AUTHOR);
22 MODULE_DESCRIPTION(HL_DRIVER_DESC);
23 MODULE_LICENSE("GPL v2");
26 static struct class *hl_class;
27 static DEFINE_IDR(hl_devs_idr);
28 static DEFINE_MUTEX(hl_devs_idr_lock);
30 static int timeout_locked = 5;
31 static int reset_on_lockup = 1;
32 static int memory_scrub = 1;
34 module_param(timeout_locked, int, 0444);
35 MODULE_PARM_DESC(timeout_locked,
36 "Device lockup timeout in seconds (0 = disabled, default 5s)");
38 module_param(reset_on_lockup, int, 0444);
39 MODULE_PARM_DESC(reset_on_lockup,
40 "Do device reset on lockup (0 = no, 1 = yes, default yes)");
42 module_param(memory_scrub, int, 0444);
43 MODULE_PARM_DESC(memory_scrub,
44 "Scrub device memory in various states (0 = no, 1 = yes, default yes)");
46 #define PCI_VENDOR_ID_HABANALABS 0x1da3
48 #define PCI_IDS_GOYA 0x0001
49 #define PCI_IDS_GAUDI 0x1000
51 static const struct pci_device_id ids[] = {
52 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
53 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI), },
56 MODULE_DEVICE_TABLE(pci, ids);
59 * get_asic_type - translate device id to asic type
61 * @device: id of the PCI device
63 * Translate device id to asic type.
64 * In case of unidentified device, return -1
66 static enum hl_asic_type get_asic_type(u16 device)
68 enum hl_asic_type asic_type;
72 asic_type = ASIC_GOYA;
75 asic_type = ASIC_GAUDI;
78 asic_type = ASIC_INVALID;
86 * hl_device_open - open function for habanalabs device
88 * @inode: pointer to inode structure
89 * @filp: pointer to file structure
91 * Called when process opens an habanalabs device.
93 int hl_device_open(struct inode *inode, struct file *filp)
95 enum hl_device_status status;
96 struct hl_device *hdev;
97 struct hl_fpriv *hpriv;
100 mutex_lock(&hl_devs_idr_lock);
101 hdev = idr_find(&hl_devs_idr, iminor(inode));
102 mutex_unlock(&hl_devs_idr_lock);
105 pr_err("Couldn't find device %d:%d\n",
106 imajor(inode), iminor(inode));
110 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
115 filp->private_data = hpriv;
117 mutex_init(&hpriv->restore_phase_mutex);
118 kref_init(&hpriv->refcount);
119 nonseekable_open(inode, filp);
121 hl_cb_mgr_init(&hpriv->cb_mgr);
122 hl_ctx_mgr_init(&hpriv->ctx_mgr);
124 hpriv->taskpid = find_get_pid(current->pid);
126 mutex_lock(&hdev->fpriv_list_lock);
128 if (!hl_device_operational(hdev, &status)) {
129 dev_err_ratelimited(hdev->dev,
130 "Can't open %s because it is %s\n",
131 dev_name(hdev->dev), hdev->status[status]);
136 if (hdev->in_debug) {
137 dev_err_ratelimited(hdev->dev,
138 "Can't open %s because it is being debugged by another user\n",
139 dev_name(hdev->dev));
144 if (hdev->compute_ctx) {
145 dev_dbg_ratelimited(hdev->dev,
146 "Can't open %s because another user is working on it\n",
147 dev_name(hdev->dev));
152 rc = hl_ctx_create(hdev, hpriv);
154 dev_err(hdev->dev, "Failed to create context %d\n", rc);
158 /* Device is IDLE at this point so it is legal to change PLLs.
159 * There is no need to check anything because if the PLL is
160 * already HIGH, the set function will return without doing
163 hl_device_set_frequency(hdev, PLL_HIGH);
165 list_add(&hpriv->dev_node, &hdev->fpriv_list);
166 mutex_unlock(&hdev->fpriv_list_lock);
168 hl_debugfs_add_file(hpriv);
173 mutex_unlock(&hdev->fpriv_list_lock);
175 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
176 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
177 filp->private_data = NULL;
178 mutex_destroy(&hpriv->restore_phase_mutex);
179 put_pid(hpriv->taskpid);
186 int hl_device_open_ctrl(struct inode *inode, struct file *filp)
188 struct hl_device *hdev;
189 struct hl_fpriv *hpriv;
192 mutex_lock(&hl_devs_idr_lock);
193 hdev = idr_find(&hl_devs_idr, iminor(inode));
194 mutex_unlock(&hl_devs_idr_lock);
197 pr_err("Couldn't find device %d:%d\n",
198 imajor(inode), iminor(inode));
202 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
206 mutex_lock(&hdev->fpriv_list_lock);
208 if (!hl_device_operational(hdev, NULL)) {
209 dev_err_ratelimited(hdev->dev_ctrl,
210 "Can't open %s because it is disabled or in reset\n",
211 dev_name(hdev->dev_ctrl));
216 list_add(&hpriv->dev_node, &hdev->fpriv_list);
217 mutex_unlock(&hdev->fpriv_list_lock);
220 filp->private_data = hpriv;
222 hpriv->is_control = true;
223 nonseekable_open(inode, filp);
225 hpriv->taskpid = find_get_pid(current->pid);
230 mutex_unlock(&hdev->fpriv_list_lock);
235 static void set_driver_behavior_per_device(struct hl_device *hdev)
237 hdev->cpu_enable = 1;
238 hdev->fw_loading = FW_TYPE_ALL_TYPES;
239 hdev->cpu_queues_enable = 1;
241 hdev->mmu_enable = 1;
242 hdev->clock_gating_mask = ULONG_MAX;
243 hdev->sram_scrambler_enable = 1;
244 hdev->dram_scrambler_enable = 1;
245 hdev->bmc_enable = 1;
246 hdev->hard_reset_on_fw_events = 1;
247 hdev->reset_on_preboot_fail = 1;
249 hdev->reset_pcilink = 0;
254 * create_hdev - create habanalabs device instance
256 * @dev: will hold the pointer to the new habanalabs device structure
257 * @pdev: pointer to the pci device
258 * @asic_type: in case of simulator device, which device is it
259 * @minor: in case of simulator device, the minor of the device
261 * Allocate memory for habanalabs device and initialize basic fields
262 * Identify the ASIC type
263 * Allocate ID (minor) for the device (only for real devices)
265 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
266 enum hl_asic_type asic_type, int minor)
268 struct hl_device *hdev;
269 int rc, main_id, ctrl_id = 0;
273 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
277 /* First, we must find out which ASIC are we handling. This is needed
278 * to configure the behavior of the driver (kernel parameters)
281 hdev->asic_type = get_asic_type(pdev->device);
282 if (hdev->asic_type == ASIC_INVALID) {
283 dev_err(&pdev->dev, "Unsupported ASIC\n");
288 hdev->asic_type = asic_type;
291 /* Assign status description string */
292 strncpy(hdev->status[HL_DEVICE_STATUS_MALFUNCTION],
293 "disabled", HL_STR_MAX);
294 strncpy(hdev->status[HL_DEVICE_STATUS_IN_RESET],
295 "in reset", HL_STR_MAX);
296 strncpy(hdev->status[HL_DEVICE_STATUS_NEEDS_RESET],
297 "needs reset", HL_STR_MAX);
299 hdev->major = hl_major;
300 hdev->reset_on_lockup = reset_on_lockup;
301 hdev->memory_scrub = memory_scrub;
304 set_driver_behavior_per_device(hdev);
307 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
309 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
311 hdev->disabled = true;
312 hdev->pdev = pdev; /* can be NULL in case of simulator device */
314 /* Set default DMA mask to 32 bits */
317 mutex_lock(&hl_devs_idr_lock);
319 /* Always save 2 numbers, 1 for main device and 1 for control.
320 * They must be consecutive
322 main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
326 ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1,
327 main_id + 2, GFP_KERNEL);
329 mutex_unlock(&hl_devs_idr_lock);
331 if ((main_id < 0) || (ctrl_id < 0)) {
332 if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC))
333 pr_err("too many devices in the system\n");
336 mutex_lock(&hl_devs_idr_lock);
337 idr_remove(&hl_devs_idr, main_id);
338 mutex_unlock(&hl_devs_idr_lock);
346 hdev->id_control = ctrl_id;
358 * destroy_hdev - destroy habanalabs device instance
360 * @dev: pointer to the habanalabs device structure
363 void destroy_hdev(struct hl_device *hdev)
365 /* Remove device from the device list */
366 mutex_lock(&hl_devs_idr_lock);
367 idr_remove(&hl_devs_idr, hdev->id);
368 idr_remove(&hl_devs_idr, hdev->id_control);
369 mutex_unlock(&hl_devs_idr_lock);
374 static int hl_pmops_suspend(struct device *dev)
376 struct hl_device *hdev = dev_get_drvdata(dev);
378 pr_debug("Going to suspend PCI device\n");
381 pr_err("device pointer is NULL in suspend\n");
385 return hl_device_suspend(hdev);
388 static int hl_pmops_resume(struct device *dev)
390 struct hl_device *hdev = dev_get_drvdata(dev);
392 pr_debug("Going to resume PCI device\n");
395 pr_err("device pointer is NULL in resume\n");
399 return hl_device_resume(hdev);
403 * hl_pci_probe - probe PCI habanalabs devices
405 * @pdev: pointer to pci device
406 * @id: pointer to pci device id structure
408 * Standard PCI probe function for habanalabs device.
409 * Create a new habanalabs device and initialize it according to the
412 static int hl_pci_probe(struct pci_dev *pdev,
413 const struct pci_device_id *id)
415 struct hl_device *hdev;
418 dev_info(&pdev->dev, HL_NAME
419 " device found [%04x:%04x] (rev %x)\n",
420 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
422 rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1);
426 pci_set_drvdata(pdev, hdev);
428 pci_enable_pcie_error_reporting(pdev);
430 rc = hl_device_init(hdev, hl_class);
432 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
440 pci_set_drvdata(pdev, NULL);
447 * hl_pci_remove - remove PCI habanalabs devices
449 * @pdev: pointer to pci device
451 * Standard PCI remove function for habanalabs device
453 static void hl_pci_remove(struct pci_dev *pdev)
455 struct hl_device *hdev;
457 hdev = pci_get_drvdata(pdev);
461 hl_device_fini(hdev);
462 pci_disable_pcie_error_reporting(pdev);
463 pci_set_drvdata(pdev, NULL);
468 * hl_pci_err_detected - a PCI bus error detected on this device
470 * @pdev: pointer to pci device
471 * @state: PCI error type
473 * Called by the PCI subsystem whenever a non-correctable
474 * PCI bus error is detected
476 static pci_ers_result_t
477 hl_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t state)
479 struct hl_device *hdev = pci_get_drvdata(pdev);
480 enum pci_ers_result result;
483 case pci_channel_io_normal:
484 return PCI_ERS_RESULT_CAN_RECOVER;
486 case pci_channel_io_frozen:
487 dev_warn(hdev->dev, "frozen state error detected\n");
488 result = PCI_ERS_RESULT_NEED_RESET;
491 case pci_channel_io_perm_failure:
492 dev_warn(hdev->dev, "failure state error detected\n");
493 result = PCI_ERS_RESULT_DISCONNECT;
497 result = PCI_ERS_RESULT_NONE;
500 hdev->asic_funcs->halt_engines(hdev, true);
506 * hl_pci_err_resume - resume after a PCI slot reset
508 * @pdev: pointer to pci device
511 static void hl_pci_err_resume(struct pci_dev *pdev)
513 struct hl_device *hdev = pci_get_drvdata(pdev);
515 dev_warn(hdev->dev, "Resuming device after PCI slot reset\n");
516 hl_device_resume(hdev);
520 * hl_pci_err_slot_reset - a PCI slot reset has just happened
522 * @pdev: pointer to pci device
524 * Determine if the driver can recover from the PCI slot reset
526 static pci_ers_result_t hl_pci_err_slot_reset(struct pci_dev *pdev)
528 return PCI_ERS_RESULT_RECOVERED;
531 static const struct dev_pm_ops hl_pm_ops = {
532 .suspend = hl_pmops_suspend,
533 .resume = hl_pmops_resume,
536 static const struct pci_error_handlers hl_pci_err_handler = {
537 .error_detected = hl_pci_err_detected,
538 .slot_reset = hl_pci_err_slot_reset,
539 .resume = hl_pci_err_resume,
542 static struct pci_driver hl_pci_driver = {
545 .probe = hl_pci_probe,
546 .remove = hl_pci_remove,
547 .shutdown = hl_pci_remove,
548 .driver.pm = &hl_pm_ops,
549 .err_handler = &hl_pci_err_handler,
553 * hl_init - Initialize the habanalabs kernel driver
555 static int __init hl_init(void)
560 pr_info("loading driver\n");
562 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
564 pr_err("unable to get major\n");
568 hl_major = MAJOR(dev);
570 hl_class = class_create(THIS_MODULE, HL_NAME);
571 if (IS_ERR(hl_class)) {
572 pr_err("failed to allocate class\n");
573 rc = PTR_ERR(hl_class);
579 rc = pci_register_driver(&hl_pci_driver);
581 pr_err("failed to register pci device\n");
585 pr_debug("driver loaded\n");
591 class_destroy(hl_class);
593 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
598 * hl_exit - Release all resources of the habanalabs kernel driver
600 static void __exit hl_exit(void)
602 pci_unregister_driver(&hl_pci_driver);
605 * Removing debugfs must be after all devices or simulator devices
606 * have been removed because otherwise we get a bug in the
607 * debugfs module for referencing NULL objects
611 class_destroy(hl_class);
612 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
614 idr_destroy(&hl_devs_idr);
616 pr_debug("driver removed\n");
619 module_init(hl_init);
620 module_exit(hl_exit);