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 DEFINE_IDR(hl_devs_idr);
25 DEFINE_MUTEX(hl_devs_idr_lock);
27 #define PCI_VENDOR_ID_HABANALABS 0x1da3
29 #define PCI_IDS_GOYA 0x0001
31 static const struct pci_device_id ids[] = {
32 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
35 MODULE_DEVICE_TABLE(pci, ids);
38 * get_asic_type - translate device id to asic type
40 * @device: id of the PCI device
42 * Translate device id to asic type.
43 * In case of unidentified device, return -1
45 static enum hl_asic_type get_asic_type(u16 device)
47 enum hl_asic_type asic_type;
51 asic_type = ASIC_GOYA;
54 asic_type = ASIC_INVALID;
62 * hl_device_open - open function for habanalabs device
64 * @inode: pointer to inode structure
65 * @filp: pointer to file structure
67 * Called when process opens an habanalabs device.
69 int hl_device_open(struct inode *inode, struct file *filp)
71 struct hl_device *hdev;
72 struct hl_fpriv *hpriv;
75 mutex_lock(&hl_devs_idr_lock);
76 hdev = idr_find(&hl_devs_idr, iminor(inode));
77 mutex_unlock(&hl_devs_idr_lock);
80 pr_err("Couldn't find device %d:%d\n",
81 imajor(inode), iminor(inode));
85 mutex_lock(&hdev->fd_open_cnt_lock);
88 dev_err_ratelimited(hdev->dev,
89 "Can't open %s because it is disabled\n",
91 mutex_unlock(&hdev->fd_open_cnt_lock);
95 if (atomic_read(&hdev->fd_open_cnt)) {
96 dev_info_ratelimited(hdev->dev,
97 "Device %s is already attached to application\n",
99 mutex_unlock(&hdev->fd_open_cnt_lock);
103 atomic_inc(&hdev->fd_open_cnt);
105 mutex_unlock(&hdev->fd_open_cnt_lock);
107 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
114 filp->private_data = hpriv;
116 kref_init(&hpriv->refcount);
117 nonseekable_open(inode, filp);
119 hl_cb_mgr_init(&hpriv->cb_mgr);
120 hl_ctx_mgr_init(&hpriv->ctx_mgr);
122 rc = hl_ctx_create(hdev, hpriv);
124 dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
128 hpriv->taskpid = find_get_pid(current->pid);
131 * Device is IDLE at this point so it is legal to change PLLs. There
132 * is no need to check anything because if the PLL is already HIGH, the
133 * set function will return without doing anything
135 hl_device_set_frequency(hdev, PLL_HIGH);
140 filp->private_data = NULL;
141 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
142 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
146 atomic_dec(&hdev->fd_open_cnt);
151 * create_hdev - create habanalabs device instance
153 * @dev: will hold the pointer to the new habanalabs device structure
154 * @pdev: pointer to the pci device
155 * @asic_type: in case of simulator device, which device is it
156 * @minor: in case of simulator device, the minor of the device
158 * Allocate memory for habanalabs device and initialize basic fields
159 * Identify the ASIC type
160 * Allocate ID (minor) for the device (only for real devices)
162 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
163 enum hl_asic_type asic_type, int minor)
165 struct hl_device *hdev;
170 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
174 hdev->major = hl_major;
176 /* Parameters for bring-up - set them to defaults */
177 hdev->cpu_enable = 1;
178 hdev->reset_pcilink = 0;
179 hdev->cpu_queues_enable = 1;
180 hdev->fw_loading = 1;
183 /* If CPU is disabled, no point in loading FW */
184 if (!hdev->cpu_enable)
185 hdev->fw_loading = 0;
187 /* If we don't load FW, no need to initialize CPU queues */
188 if (!hdev->fw_loading)
189 hdev->cpu_queues_enable = 0;
191 hdev->disabled = true;
192 hdev->pdev = pdev; /* can be NULL in case of simulator device */
194 if (asic_type == ASIC_AUTO_DETECT) {
195 hdev->asic_type = get_asic_type(pdev->device);
196 if (hdev->asic_type == ASIC_INVALID) {
197 dev_err(&pdev->dev, "Unsupported ASIC\n");
202 hdev->asic_type = asic_type;
205 mutex_lock(&hl_devs_idr_lock);
208 rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
211 void *old_idr = idr_replace(&hl_devs_idr, hdev, minor);
213 if (IS_ERR_VALUE(old_idr)) {
214 rc = PTR_ERR(old_idr);
215 pr_err("Error %d when trying to replace minor %d\n",
217 mutex_unlock(&hl_devs_idr_lock);
223 mutex_unlock(&hl_devs_idr_lock);
227 pr_err("too many devices in the system\n");
245 * destroy_hdev - destroy habanalabs device instance
247 * @dev: pointer to the habanalabs device structure
250 void destroy_hdev(struct hl_device *hdev)
252 /* Remove device from the device list */
253 mutex_lock(&hl_devs_idr_lock);
254 idr_remove(&hl_devs_idr, hdev->id);
255 mutex_unlock(&hl_devs_idr_lock);
260 static int hl_pmops_suspend(struct device *dev)
262 struct pci_dev *pdev = to_pci_dev(dev);
263 struct hl_device *hdev = pci_get_drvdata(pdev);
265 pr_debug("Going to suspend PCI device\n");
268 pr_err("device pointer is NULL in suspend\n");
272 return hl_device_suspend(hdev);
275 static int hl_pmops_resume(struct device *dev)
277 struct pci_dev *pdev = to_pci_dev(dev);
278 struct hl_device *hdev = pci_get_drvdata(pdev);
280 pr_debug("Going to resume PCI device\n");
283 pr_err("device pointer is NULL in resume\n");
287 return hl_device_resume(hdev);
291 * hl_pci_probe - probe PCI habanalabs devices
293 * @pdev: pointer to pci device
294 * @id: pointer to pci device id structure
296 * Standard PCI probe function for habanalabs device.
297 * Create a new habanalabs device and initialize it according to the
300 static int hl_pci_probe(struct pci_dev *pdev,
301 const struct pci_device_id *id)
303 struct hl_device *hdev;
306 dev_info(&pdev->dev, HL_NAME
307 " device found [%04x:%04x] (rev %x)\n",
308 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
310 rc = create_hdev(&hdev, pdev, ASIC_AUTO_DETECT, -1);
314 pci_set_drvdata(pdev, hdev);
316 rc = hl_device_init(hdev, hl_class);
318 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
326 pci_set_drvdata(pdev, NULL);
333 * hl_pci_remove - remove PCI habanalabs devices
335 * @pdev: pointer to pci device
337 * Standard PCI remove function for habanalabs device
339 static void hl_pci_remove(struct pci_dev *pdev)
341 struct hl_device *hdev;
343 hdev = pci_get_drvdata(pdev);
347 hl_device_fini(hdev);
348 pci_set_drvdata(pdev, NULL);
353 static const struct dev_pm_ops hl_pm_ops = {
354 .suspend = hl_pmops_suspend,
355 .resume = hl_pmops_resume,
358 static struct pci_driver hl_pci_driver = {
361 .probe = hl_pci_probe,
362 .remove = hl_pci_remove,
363 .driver.pm = &hl_pm_ops,
367 * hl_init - Initialize the habanalabs kernel driver
369 static int __init hl_init(void)
374 pr_info("loading driver\n");
376 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
378 pr_err("unable to get major\n");
382 hl_major = MAJOR(dev);
384 hl_class = class_create(THIS_MODULE, HL_NAME);
385 if (IS_ERR(hl_class)) {
386 pr_err("failed to allocate class\n");
387 rc = PTR_ERR(hl_class);
391 rc = pci_register_driver(&hl_pci_driver);
393 pr_err("failed to register pci device\n");
397 pr_debug("driver loaded\n");
402 class_destroy(hl_class);
404 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
409 * hl_exit - Release all resources of the habanalabs kernel driver
411 static void __exit hl_exit(void)
413 pci_unregister_driver(&hl_pci_driver);
415 class_destroy(hl_class);
416 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
418 idr_destroy(&hl_devs_idr);
420 pr_debug("driver removed\n");
423 module_init(hl_init);
424 module_exit(hl_exit);