1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
9 #include "habanalabs.h"
11 #include <linux/pci.h>
12 #include <linux/module.h>
14 #define HL_DRIVER_AUTHOR "HabanaLabs Kernel Driver Team"
16 #define HL_DRIVER_DESC "Driver for HabanaLabs's AI Accelerators"
18 MODULE_AUTHOR(HL_DRIVER_AUTHOR);
19 MODULE_DESCRIPTION(HL_DRIVER_DESC);
20 MODULE_LICENSE("GPL v2");
23 static struct class *hl_class;
24 static DEFINE_IDR(hl_devs_idr);
25 static DEFINE_MUTEX(hl_devs_idr_lock);
27 static int timeout_locked = 5;
28 static int reset_on_lockup = 1;
30 module_param(timeout_locked, int, 0444);
31 MODULE_PARM_DESC(timeout_locked,
32 "Device lockup timeout in seconds (0 = disabled, default 5s)");
34 module_param(reset_on_lockup, int, 0444);
35 MODULE_PARM_DESC(reset_on_lockup,
36 "Do device reset on lockup (0 = no, 1 = yes, default yes)");
38 #define PCI_VENDOR_ID_HABANALABS 0x1da3
40 #define PCI_IDS_GOYA 0x0001
42 static const struct pci_device_id ids[] = {
43 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
46 MODULE_DEVICE_TABLE(pci, ids);
49 * get_asic_type - translate device id to asic type
51 * @device: id of the PCI device
53 * Translate device id to asic type.
54 * In case of unidentified device, return -1
56 static enum hl_asic_type get_asic_type(u16 device)
58 enum hl_asic_type asic_type;
62 asic_type = ASIC_GOYA;
65 asic_type = ASIC_INVALID;
73 * hl_device_open - open function for habanalabs device
75 * @inode: pointer to inode structure
76 * @filp: pointer to file structure
78 * Called when process opens an habanalabs device.
80 int hl_device_open(struct inode *inode, struct file *filp)
82 struct hl_device *hdev;
83 struct hl_fpriv *hpriv;
86 mutex_lock(&hl_devs_idr_lock);
87 hdev = idr_find(&hl_devs_idr, iminor(inode));
88 mutex_unlock(&hl_devs_idr_lock);
91 pr_err("Couldn't find device %d:%d\n",
92 imajor(inode), iminor(inode));
96 mutex_lock(&hdev->fd_open_cnt_lock);
98 if (hl_device_disabled_or_in_reset(hdev)) {
99 dev_err_ratelimited(hdev->dev,
100 "Can't open %s because it is disabled or in reset\n",
101 dev_name(hdev->dev));
102 mutex_unlock(&hdev->fd_open_cnt_lock);
106 if (atomic_read(&hdev->fd_open_cnt)) {
107 dev_info_ratelimited(hdev->dev,
108 "Device %s is already attached to application\n",
109 dev_name(hdev->dev));
110 mutex_unlock(&hdev->fd_open_cnt_lock);
114 atomic_inc(&hdev->fd_open_cnt);
116 mutex_unlock(&hdev->fd_open_cnt_lock);
118 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
125 filp->private_data = hpriv;
127 mutex_init(&hpriv->restore_phase_mutex);
128 kref_init(&hpriv->refcount);
129 nonseekable_open(inode, filp);
131 hl_cb_mgr_init(&hpriv->cb_mgr);
132 hl_ctx_mgr_init(&hpriv->ctx_mgr);
134 rc = hl_ctx_create(hdev, hpriv);
136 dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
140 hpriv->taskpid = find_get_pid(current->pid);
143 * Device is IDLE at this point so it is legal to change PLLs. There
144 * is no need to check anything because if the PLL is already HIGH, the
145 * set function will return without doing anything
147 hl_device_set_frequency(hdev, PLL_HIGH);
149 hl_debugfs_add_file(hpriv);
154 filp->private_data = NULL;
155 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
156 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
157 mutex_destroy(&hpriv->restore_phase_mutex);
161 atomic_dec(&hdev->fd_open_cnt);
166 * create_hdev - create habanalabs device instance
168 * @dev: will hold the pointer to the new habanalabs device structure
169 * @pdev: pointer to the pci device
170 * @asic_type: in case of simulator device, which device is it
171 * @minor: in case of simulator device, the minor of the device
173 * Allocate memory for habanalabs device and initialize basic fields
174 * Identify the ASIC type
175 * Allocate ID (minor) for the device (only for real devices)
177 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
178 enum hl_asic_type asic_type, int minor)
180 struct hl_device *hdev;
185 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
189 hdev->major = hl_major;
190 hdev->reset_on_lockup = reset_on_lockup;
192 /* Parameters for bring-up - set them to defaults */
193 hdev->mmu_enable = 1;
194 hdev->cpu_enable = 1;
195 hdev->reset_pcilink = 0;
196 hdev->cpu_queues_enable = 1;
197 hdev->fw_loading = 1;
201 /* If CPU is disabled, no point in loading FW */
202 if (!hdev->cpu_enable)
203 hdev->fw_loading = 0;
205 /* If we don't load FW, no need to initialize CPU queues */
206 if (!hdev->fw_loading)
207 hdev->cpu_queues_enable = 0;
209 /* If CPU queues not enabled, no way to do heartbeat */
210 if (!hdev->cpu_queues_enable)
214 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
216 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
218 hdev->disabled = true;
219 hdev->pdev = pdev; /* can be NULL in case of simulator device */
221 if (asic_type == ASIC_AUTO_DETECT) {
222 hdev->asic_type = get_asic_type(pdev->device);
223 if (hdev->asic_type == ASIC_INVALID) {
224 dev_err(&pdev->dev, "Unsupported ASIC\n");
229 hdev->asic_type = asic_type;
232 /* Set default DMA mask to 32 bits */
235 mutex_lock(&hl_devs_idr_lock);
238 rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
241 void *old_idr = idr_replace(&hl_devs_idr, hdev, minor);
243 if (IS_ERR_VALUE(old_idr)) {
244 rc = PTR_ERR(old_idr);
245 pr_err("Error %d when trying to replace minor %d\n",
247 mutex_unlock(&hl_devs_idr_lock);
253 mutex_unlock(&hl_devs_idr_lock);
257 pr_err("too many devices in the system\n");
275 * destroy_hdev - destroy habanalabs device instance
277 * @dev: pointer to the habanalabs device structure
280 void destroy_hdev(struct hl_device *hdev)
282 /* Remove device from the device list */
283 mutex_lock(&hl_devs_idr_lock);
284 idr_remove(&hl_devs_idr, hdev->id);
285 mutex_unlock(&hl_devs_idr_lock);
290 static int hl_pmops_suspend(struct device *dev)
292 struct pci_dev *pdev = to_pci_dev(dev);
293 struct hl_device *hdev = pci_get_drvdata(pdev);
295 pr_debug("Going to suspend PCI device\n");
298 pr_err("device pointer is NULL in suspend\n");
302 return hl_device_suspend(hdev);
305 static int hl_pmops_resume(struct device *dev)
307 struct pci_dev *pdev = to_pci_dev(dev);
308 struct hl_device *hdev = pci_get_drvdata(pdev);
310 pr_debug("Going to resume PCI device\n");
313 pr_err("device pointer is NULL in resume\n");
317 return hl_device_resume(hdev);
321 * hl_pci_probe - probe PCI habanalabs devices
323 * @pdev: pointer to pci device
324 * @id: pointer to pci device id structure
326 * Standard PCI probe function for habanalabs device.
327 * Create a new habanalabs device and initialize it according to the
330 static int hl_pci_probe(struct pci_dev *pdev,
331 const struct pci_device_id *id)
333 struct hl_device *hdev;
336 dev_info(&pdev->dev, HL_NAME
337 " device found [%04x:%04x] (rev %x)\n",
338 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
340 rc = create_hdev(&hdev, pdev, ASIC_AUTO_DETECT, -1);
344 pci_set_drvdata(pdev, hdev);
346 rc = hl_device_init(hdev, hl_class);
348 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
356 pci_set_drvdata(pdev, NULL);
363 * hl_pci_remove - remove PCI habanalabs devices
365 * @pdev: pointer to pci device
367 * Standard PCI remove function for habanalabs device
369 static void hl_pci_remove(struct pci_dev *pdev)
371 struct hl_device *hdev;
373 hdev = pci_get_drvdata(pdev);
377 hl_device_fini(hdev);
378 pci_set_drvdata(pdev, NULL);
383 static const struct dev_pm_ops hl_pm_ops = {
384 .suspend = hl_pmops_suspend,
385 .resume = hl_pmops_resume,
388 static struct pci_driver hl_pci_driver = {
391 .probe = hl_pci_probe,
392 .remove = hl_pci_remove,
393 .driver.pm = &hl_pm_ops,
397 * hl_init - Initialize the habanalabs kernel driver
399 static int __init hl_init(void)
404 pr_info("loading driver\n");
406 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
408 pr_err("unable to get major\n");
412 hl_major = MAJOR(dev);
414 hl_class = class_create(THIS_MODULE, HL_NAME);
415 if (IS_ERR(hl_class)) {
416 pr_err("failed to allocate class\n");
417 rc = PTR_ERR(hl_class);
423 rc = pci_register_driver(&hl_pci_driver);
425 pr_err("failed to register pci device\n");
429 pr_debug("driver loaded\n");
435 class_destroy(hl_class);
437 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
442 * hl_exit - Release all resources of the habanalabs kernel driver
444 static void __exit hl_exit(void)
446 pci_unregister_driver(&hl_pci_driver);
449 * Removing debugfs must be after all devices or simulator devices
450 * have been removed because otherwise we get a bug in the
451 * debugfs module for referencing NULL objects
455 class_destroy(hl_class);
456 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
458 idr_destroy(&hl_devs_idr);
460 pr_debug("driver removed\n");
463 module_init(hl_init);
464 module_exit(hl_exit);