]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/habanalabs_drv.c
b697339d39049466162e7f79be7de3002639610b
[linux.git] / drivers / misc / habanalabs / habanalabs_drv.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright 2016-2019 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  *
7  */
8
9 #include "habanalabs.h"
10
11 #include <linux/pci.h>
12 #include <linux/module.h>
13
14 #define HL_DRIVER_AUTHOR        "HabanaLabs Kernel Driver Team"
15
16 #define HL_DRIVER_DESC          "Driver for HabanaLabs's AI Accelerators"
17
18 MODULE_AUTHOR(HL_DRIVER_AUTHOR);
19 MODULE_DESCRIPTION(HL_DRIVER_DESC);
20 MODULE_LICENSE("GPL v2");
21
22 static int hl_major;
23 static struct class *hl_class;
24 static DEFINE_IDR(hl_devs_idr);
25 static DEFINE_MUTEX(hl_devs_idr_lock);
26
27 static int timeout_locked = 5;
28 static int reset_on_lockup = 1;
29
30 module_param(timeout_locked, int, 0444);
31 MODULE_PARM_DESC(timeout_locked,
32         "Device lockup timeout in seconds (0 = disabled, default 5s)");
33
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)");
37
38 #define PCI_VENDOR_ID_HABANALABS        0x1da3
39
40 #define PCI_IDS_GOYA                    0x0001
41
42 static const struct pci_device_id ids[] = {
43         { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
44         { 0, }
45 };
46 MODULE_DEVICE_TABLE(pci, ids);
47
48 /*
49  * get_asic_type - translate device id to asic type
50  *
51  * @device: id of the PCI device
52  *
53  * Translate device id to asic type.
54  * In case of unidentified device, return -1
55  */
56 static enum hl_asic_type get_asic_type(u16 device)
57 {
58         enum hl_asic_type asic_type;
59
60         switch (device) {
61         case PCI_IDS_GOYA:
62                 asic_type = ASIC_GOYA;
63                 break;
64         default:
65                 asic_type = ASIC_INVALID;
66                 break;
67         }
68
69         return asic_type;
70 }
71
72 /*
73  * hl_device_open - open function for habanalabs device
74  *
75  * @inode: pointer to inode structure
76  * @filp: pointer to file structure
77  *
78  * Called when process opens an habanalabs device.
79  */
80 int hl_device_open(struct inode *inode, struct file *filp)
81 {
82         struct hl_device *hdev;
83         struct hl_fpriv *hpriv;
84         int rc;
85
86         mutex_lock(&hl_devs_idr_lock);
87         hdev = idr_find(&hl_devs_idr, iminor(inode));
88         mutex_unlock(&hl_devs_idr_lock);
89
90         if (!hdev) {
91                 pr_err("Couldn't find device %d:%d\n",
92                         imajor(inode), iminor(inode));
93                 return -ENXIO;
94         }
95
96         mutex_lock(&hdev->fd_open_cnt_lock);
97
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);
103                 return -EPERM;
104         }
105
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);
111                 return -EBUSY;
112         }
113
114         atomic_inc(&hdev->fd_open_cnt);
115
116         mutex_unlock(&hdev->fd_open_cnt_lock);
117
118         hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
119         if (!hpriv) {
120                 rc = -ENOMEM;
121                 goto close_device;
122         }
123
124         hpriv->hdev = hdev;
125         filp->private_data = hpriv;
126         hpriv->filp = filp;
127         mutex_init(&hpriv->restore_phase_mutex);
128         kref_init(&hpriv->refcount);
129         nonseekable_open(inode, filp);
130
131         hl_cb_mgr_init(&hpriv->cb_mgr);
132         hl_ctx_mgr_init(&hpriv->ctx_mgr);
133
134         rc = hl_ctx_create(hdev, hpriv);
135         if (rc) {
136                 dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
137                 goto out_err;
138         }
139
140         hpriv->taskpid = find_get_pid(current->pid);
141
142         /*
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
146          */
147         hl_device_set_frequency(hdev, PLL_HIGH);
148
149         hl_debugfs_add_file(hpriv);
150
151         return 0;
152
153 out_err:
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);
158         kfree(hpriv);
159
160 close_device:
161         atomic_dec(&hdev->fd_open_cnt);
162         return rc;
163 }
164
165 /*
166  * create_hdev - create habanalabs device instance
167  *
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
172  *
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)
176  */
177 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
178                 enum hl_asic_type asic_type, int minor)
179 {
180         struct hl_device *hdev;
181         int rc;
182
183         *dev = NULL;
184
185         hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
186         if (!hdev)
187                 return -ENOMEM;
188
189         hdev->major = hl_major;
190         hdev->reset_on_lockup = reset_on_lockup;
191
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;
198         hdev->pldm = 0;
199         hdev->heartbeat = 1;
200
201         /* If CPU is disabled, no point in loading FW */
202         if (!hdev->cpu_enable)
203                 hdev->fw_loading = 0;
204
205         /* If we don't load FW, no need to initialize CPU queues */
206         if (!hdev->fw_loading)
207                 hdev->cpu_queues_enable = 0;
208
209         /* If CPU queues not enabled, no way to do heartbeat */
210         if (!hdev->cpu_queues_enable)
211                 hdev->heartbeat = 0;
212
213         if (timeout_locked)
214                 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
215         else
216                 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
217
218         hdev->disabled = true;
219         hdev->pdev = pdev; /* can be NULL in case of simulator device */
220
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");
225                         rc = -ENODEV;
226                         goto free_hdev;
227                 }
228         } else {
229                 hdev->asic_type = asic_type;
230         }
231
232         /* Set default DMA mask to 32 bits */
233         hdev->dma_mask = 32;
234
235         mutex_lock(&hl_devs_idr_lock);
236
237         if (minor == -1) {
238                 rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
239                                 GFP_KERNEL);
240         } else {
241                 void *old_idr = idr_replace(&hl_devs_idr, hdev, minor);
242
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",
246                                 rc, minor);
247                         mutex_unlock(&hl_devs_idr_lock);
248                         goto free_hdev;
249                 }
250                 rc = minor;
251         }
252
253         mutex_unlock(&hl_devs_idr_lock);
254
255         if (rc < 0) {
256                 if (rc == -ENOSPC) {
257                         pr_err("too many devices in the system\n");
258                         rc = -EBUSY;
259                 }
260                 goto free_hdev;
261         }
262
263         hdev->id = rc;
264
265         *dev = hdev;
266
267         return 0;
268
269 free_hdev:
270         kfree(hdev);
271         return rc;
272 }
273
274 /*
275  * destroy_hdev - destroy habanalabs device instance
276  *
277  * @dev: pointer to the habanalabs device structure
278  *
279  */
280 void destroy_hdev(struct hl_device *hdev)
281 {
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);
286
287         kfree(hdev);
288 }
289
290 static int hl_pmops_suspend(struct device *dev)
291 {
292         struct pci_dev *pdev = to_pci_dev(dev);
293         struct hl_device *hdev = pci_get_drvdata(pdev);
294
295         pr_debug("Going to suspend PCI device\n");
296
297         if (!hdev) {
298                 pr_err("device pointer is NULL in suspend\n");
299                 return 0;
300         }
301
302         return hl_device_suspend(hdev);
303 }
304
305 static int hl_pmops_resume(struct device *dev)
306 {
307         struct pci_dev *pdev = to_pci_dev(dev);
308         struct hl_device *hdev = pci_get_drvdata(pdev);
309
310         pr_debug("Going to resume PCI device\n");
311
312         if (!hdev) {
313                 pr_err("device pointer is NULL in resume\n");
314                 return 0;
315         }
316
317         return hl_device_resume(hdev);
318 }
319
320 /*
321  * hl_pci_probe - probe PCI habanalabs devices
322  *
323  * @pdev: pointer to pci device
324  * @id: pointer to pci device id structure
325  *
326  * Standard PCI probe function for habanalabs device.
327  * Create a new habanalabs device and initialize it according to the
328  * device's type
329  */
330 static int hl_pci_probe(struct pci_dev *pdev,
331                                 const struct pci_device_id *id)
332 {
333         struct hl_device *hdev;
334         int rc;
335
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);
339
340         rc = create_hdev(&hdev, pdev, ASIC_AUTO_DETECT, -1);
341         if (rc)
342                 return rc;
343
344         pci_set_drvdata(pdev, hdev);
345
346         rc = hl_device_init(hdev, hl_class);
347         if (rc) {
348                 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
349                 rc = -ENODEV;
350                 goto disable_device;
351         }
352
353         return 0;
354
355 disable_device:
356         pci_set_drvdata(pdev, NULL);
357         destroy_hdev(hdev);
358
359         return rc;
360 }
361
362 /*
363  * hl_pci_remove - remove PCI habanalabs devices
364  *
365  * @pdev: pointer to pci device
366  *
367  * Standard PCI remove function for habanalabs device
368  */
369 static void hl_pci_remove(struct pci_dev *pdev)
370 {
371         struct hl_device *hdev;
372
373         hdev = pci_get_drvdata(pdev);
374         if (!hdev)
375                 return;
376
377         hl_device_fini(hdev);
378         pci_set_drvdata(pdev, NULL);
379
380         destroy_hdev(hdev);
381 }
382
383 static const struct dev_pm_ops hl_pm_ops = {
384         .suspend = hl_pmops_suspend,
385         .resume = hl_pmops_resume,
386 };
387
388 static struct pci_driver hl_pci_driver = {
389         .name = HL_NAME,
390         .id_table = ids,
391         .probe = hl_pci_probe,
392         .remove = hl_pci_remove,
393         .driver.pm = &hl_pm_ops,
394 };
395
396 /*
397  * hl_init - Initialize the habanalabs kernel driver
398  */
399 static int __init hl_init(void)
400 {
401         int rc;
402         dev_t dev;
403
404         pr_info("loading driver\n");
405
406         rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
407         if (rc < 0) {
408                 pr_err("unable to get major\n");
409                 return rc;
410         }
411
412         hl_major = MAJOR(dev);
413
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);
418                 goto remove_major;
419         }
420
421         hl_debugfs_init();
422
423         rc = pci_register_driver(&hl_pci_driver);
424         if (rc) {
425                 pr_err("failed to register pci device\n");
426                 goto remove_debugfs;
427         }
428
429         pr_debug("driver loaded\n");
430
431         return 0;
432
433 remove_debugfs:
434         hl_debugfs_fini();
435         class_destroy(hl_class);
436 remove_major:
437         unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
438         return rc;
439 }
440
441 /*
442  * hl_exit - Release all resources of the habanalabs kernel driver
443  */
444 static void __exit hl_exit(void)
445 {
446         pci_unregister_driver(&hl_pci_driver);
447
448         /*
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
452          */
453         hl_debugfs_fini();
454
455         class_destroy(hl_class);
456         unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
457
458         idr_destroy(&hl_devs_idr);
459
460         pr_debug("driver removed\n");
461 }
462
463 module_init(hl_init);
464 module_exit(hl_exit);