]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/habanalabs_drv.c
habanalabs: add sysfs and hwmon support
[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 DEFINE_IDR(hl_devs_idr);
25 DEFINE_MUTEX(hl_devs_idr_lock);
26
27 #define PCI_VENDOR_ID_HABANALABS        0x1da3
28
29 #define PCI_IDS_GOYA                    0x0001
30
31 static const struct pci_device_id ids[] = {
32         { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
33         { 0, }
34 };
35 MODULE_DEVICE_TABLE(pci, ids);
36
37 /*
38  * get_asic_type - translate device id to asic type
39  *
40  * @device: id of the PCI device
41  *
42  * Translate device id to asic type.
43  * In case of unidentified device, return -1
44  */
45 static enum hl_asic_type get_asic_type(u16 device)
46 {
47         enum hl_asic_type asic_type;
48
49         switch (device) {
50         case PCI_IDS_GOYA:
51                 asic_type = ASIC_GOYA;
52                 break;
53         default:
54                 asic_type = ASIC_INVALID;
55                 break;
56         }
57
58         return asic_type;
59 }
60
61 /*
62  * hl_device_open - open function for habanalabs device
63  *
64  * @inode: pointer to inode structure
65  * @filp: pointer to file structure
66  *
67  * Called when process opens an habanalabs device.
68  */
69 int hl_device_open(struct inode *inode, struct file *filp)
70 {
71         struct hl_device *hdev;
72         struct hl_fpriv *hpriv;
73         int rc;
74
75         mutex_lock(&hl_devs_idr_lock);
76         hdev = idr_find(&hl_devs_idr, iminor(inode));
77         mutex_unlock(&hl_devs_idr_lock);
78
79         if (!hdev) {
80                 pr_err("Couldn't find device %d:%d\n",
81                         imajor(inode), iminor(inode));
82                 return -ENXIO;
83         }
84
85         mutex_lock(&hdev->fd_open_cnt_lock);
86
87         if (hdev->disabled) {
88                 dev_err_ratelimited(hdev->dev,
89                         "Can't open %s because it is disabled\n",
90                         dev_name(hdev->dev));
91                 mutex_unlock(&hdev->fd_open_cnt_lock);
92                 return -EPERM;
93         }
94
95         if (atomic_read(&hdev->fd_open_cnt)) {
96                 dev_info_ratelimited(hdev->dev,
97                         "Device %s is already attached to application\n",
98                         dev_name(hdev->dev));
99                 mutex_unlock(&hdev->fd_open_cnt_lock);
100                 return -EBUSY;
101         }
102
103         atomic_inc(&hdev->fd_open_cnt);
104
105         mutex_unlock(&hdev->fd_open_cnt_lock);
106
107         hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
108         if (!hpriv) {
109                 rc = -ENOMEM;
110                 goto close_device;
111         }
112
113         hpriv->hdev = hdev;
114         filp->private_data = hpriv;
115         hpriv->filp = filp;
116         kref_init(&hpriv->refcount);
117         nonseekable_open(inode, filp);
118
119         hl_cb_mgr_init(&hpriv->cb_mgr);
120         hl_ctx_mgr_init(&hpriv->ctx_mgr);
121
122         rc = hl_ctx_create(hdev, hpriv);
123         if (rc) {
124                 dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
125                 goto out_err;
126         }
127
128         hpriv->taskpid = find_get_pid(current->pid);
129
130         /*
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
134          */
135         hl_device_set_frequency(hdev, PLL_HIGH);
136
137         return 0;
138
139 out_err:
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);
143         kfree(hpriv);
144
145 close_device:
146         atomic_dec(&hdev->fd_open_cnt);
147         return rc;
148 }
149
150 /*
151  * create_hdev - create habanalabs device instance
152  *
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
157  *
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)
161  */
162 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
163                 enum hl_asic_type asic_type, int minor)
164 {
165         struct hl_device *hdev;
166         int rc;
167
168         *dev = NULL;
169
170         hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
171         if (!hdev)
172                 return -ENOMEM;
173
174         hdev->major = hl_major;
175
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;
181         hdev->pldm = 0;
182
183         /* If CPU is disabled, no point in loading FW */
184         if (!hdev->cpu_enable)
185                 hdev->fw_loading = 0;
186
187         /* If we don't load FW, no need to initialize CPU queues */
188         if (!hdev->fw_loading)
189                 hdev->cpu_queues_enable = 0;
190
191         hdev->disabled = true;
192         hdev->pdev = pdev; /* can be NULL in case of simulator device */
193
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");
198                         rc = -ENODEV;
199                         goto free_hdev;
200                 }
201         } else {
202                 hdev->asic_type = asic_type;
203         }
204
205         mutex_lock(&hl_devs_idr_lock);
206
207         if (minor == -1) {
208                 rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
209                                 GFP_KERNEL);
210         } else {
211                 void *old_idr = idr_replace(&hl_devs_idr, hdev, minor);
212
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",
216                                 rc, minor);
217                         mutex_unlock(&hl_devs_idr_lock);
218                         goto free_hdev;
219                 }
220                 rc = minor;
221         }
222
223         mutex_unlock(&hl_devs_idr_lock);
224
225         if (rc < 0) {
226                 if (rc == -ENOSPC) {
227                         pr_err("too many devices in the system\n");
228                         rc = -EBUSY;
229                 }
230                 goto free_hdev;
231         }
232
233         hdev->id = rc;
234
235         *dev = hdev;
236
237         return 0;
238
239 free_hdev:
240         kfree(hdev);
241         return rc;
242 }
243
244 /*
245  * destroy_hdev - destroy habanalabs device instance
246  *
247  * @dev: pointer to the habanalabs device structure
248  *
249  */
250 void destroy_hdev(struct hl_device *hdev)
251 {
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);
256
257         kfree(hdev);
258 }
259
260 static int hl_pmops_suspend(struct device *dev)
261 {
262         struct pci_dev *pdev = to_pci_dev(dev);
263         struct hl_device *hdev = pci_get_drvdata(pdev);
264
265         pr_debug("Going to suspend PCI device\n");
266
267         if (!hdev) {
268                 pr_err("device pointer is NULL in suspend\n");
269                 return 0;
270         }
271
272         return hl_device_suspend(hdev);
273 }
274
275 static int hl_pmops_resume(struct device *dev)
276 {
277         struct pci_dev *pdev = to_pci_dev(dev);
278         struct hl_device *hdev = pci_get_drvdata(pdev);
279
280         pr_debug("Going to resume PCI device\n");
281
282         if (!hdev) {
283                 pr_err("device pointer is NULL in resume\n");
284                 return 0;
285         }
286
287         return hl_device_resume(hdev);
288 }
289
290 /*
291  * hl_pci_probe - probe PCI habanalabs devices
292  *
293  * @pdev: pointer to pci device
294  * @id: pointer to pci device id structure
295  *
296  * Standard PCI probe function for habanalabs device.
297  * Create a new habanalabs device and initialize it according to the
298  * device's type
299  */
300 static int hl_pci_probe(struct pci_dev *pdev,
301                                 const struct pci_device_id *id)
302 {
303         struct hl_device *hdev;
304         int rc;
305
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);
309
310         rc = create_hdev(&hdev, pdev, ASIC_AUTO_DETECT, -1);
311         if (rc)
312                 return rc;
313
314         pci_set_drvdata(pdev, hdev);
315
316         rc = hl_device_init(hdev, hl_class);
317         if (rc) {
318                 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
319                 rc = -ENODEV;
320                 goto disable_device;
321         }
322
323         return 0;
324
325 disable_device:
326         pci_set_drvdata(pdev, NULL);
327         destroy_hdev(hdev);
328
329         return rc;
330 }
331
332 /*
333  * hl_pci_remove - remove PCI habanalabs devices
334  *
335  * @pdev: pointer to pci device
336  *
337  * Standard PCI remove function for habanalabs device
338  */
339 static void hl_pci_remove(struct pci_dev *pdev)
340 {
341         struct hl_device *hdev;
342
343         hdev = pci_get_drvdata(pdev);
344         if (!hdev)
345                 return;
346
347         hl_device_fini(hdev);
348         pci_set_drvdata(pdev, NULL);
349
350         destroy_hdev(hdev);
351 }
352
353 static const struct dev_pm_ops hl_pm_ops = {
354         .suspend = hl_pmops_suspend,
355         .resume = hl_pmops_resume,
356 };
357
358 static struct pci_driver hl_pci_driver = {
359         .name = HL_NAME,
360         .id_table = ids,
361         .probe = hl_pci_probe,
362         .remove = hl_pci_remove,
363         .driver.pm = &hl_pm_ops,
364 };
365
366 /*
367  * hl_init - Initialize the habanalabs kernel driver
368  */
369 static int __init hl_init(void)
370 {
371         int rc;
372         dev_t dev;
373
374         pr_info("loading driver\n");
375
376         rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
377         if (rc < 0) {
378                 pr_err("unable to get major\n");
379                 return rc;
380         }
381
382         hl_major = MAJOR(dev);
383
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);
388                 goto remove_major;
389         }
390
391         rc = pci_register_driver(&hl_pci_driver);
392         if (rc) {
393                 pr_err("failed to register pci device\n");
394                 goto remove_class;
395         }
396
397         pr_debug("driver loaded\n");
398
399         return 0;
400
401 remove_class:
402         class_destroy(hl_class);
403 remove_major:
404         unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
405         return rc;
406 }
407
408 /*
409  * hl_exit - Release all resources of the habanalabs kernel driver
410  */
411 static void __exit hl_exit(void)
412 {
413         pci_unregister_driver(&hl_pci_driver);
414
415         class_destroy(hl_class);
416         unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
417
418         idr_destroy(&hl_devs_idr);
419
420         pr_debug("driver removed\n");
421 }
422
423 module_init(hl_init);
424 module_exit(hl_exit);