blob: 822acb5a00bb8ca16e26003b83e56c9c29a42e2c [file] [log] [blame]
Oded Gabbayc4d66342019-02-16 00:39:11 +02001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 *
7 */
8
Tomer Tayare00dac32019-04-10 15:18:46 +03009#define pr_fmt(fmt) "habanalabs: " fmt
10
Oded Gabbayc4d66342019-02-16 00:39:11 +020011#include "habanalabs.h"
12
13#include <linux/pci.h>
14#include <linux/module.h>
15
16#define HL_DRIVER_AUTHOR "HabanaLabs Kernel Driver Team"
17
18#define HL_DRIVER_DESC "Driver for HabanaLabs's AI Accelerators"
19
20MODULE_AUTHOR(HL_DRIVER_AUTHOR);
21MODULE_DESCRIPTION(HL_DRIVER_DESC);
22MODULE_LICENSE("GPL v2");
23
24static int hl_major;
25static struct class *hl_class;
Oded Gabbay5e6e0232019-02-27 12:15:16 +020026static DEFINE_IDR(hl_devs_idr);
27static DEFINE_MUTEX(hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +020028
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020029static int timeout_locked = 5;
30static int reset_on_lockup = 1;
31
32module_param(timeout_locked, int, 0444);
33MODULE_PARM_DESC(timeout_locked,
34 "Device lockup timeout in seconds (0 = disabled, default 5s)");
35
36module_param(reset_on_lockup, int, 0444);
37MODULE_PARM_DESC(reset_on_lockup,
38 "Do device reset on lockup (0 = no, 1 = yes, default yes)");
39
Oded Gabbayc4d66342019-02-16 00:39:11 +020040#define PCI_VENDOR_ID_HABANALABS 0x1da3
41
42#define PCI_IDS_GOYA 0x0001
Oded Gabbay6966d9e2020-03-21 10:58:32 +020043#define PCI_IDS_GAUDI 0x1000
Oded Gabbayc4d66342019-02-16 00:39:11 +020044
45static const struct pci_device_id ids[] = {
46 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
Oded Gabbay6966d9e2020-03-21 10:58:32 +020047 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI), },
Oded Gabbayc4d66342019-02-16 00:39:11 +020048 { 0, }
49};
Oded Gabbay824b4572020-05-03 15:30:55 +030050MODULE_DEVICE_TABLE(pci, ids);
Oded Gabbayc4d66342019-02-16 00:39:11 +020051
52/*
53 * get_asic_type - translate device id to asic type
54 *
55 * @device: id of the PCI device
56 *
57 * Translate device id to asic type.
58 * In case of unidentified device, return -1
59 */
60static enum hl_asic_type get_asic_type(u16 device)
61{
62 enum hl_asic_type asic_type;
63
64 switch (device) {
65 case PCI_IDS_GOYA:
66 asic_type = ASIC_GOYA;
67 break;
Oded Gabbay6966d9e2020-03-21 10:58:32 +020068 case PCI_IDS_GAUDI:
69 asic_type = ASIC_GAUDI;
70 break;
Oded Gabbayc4d66342019-02-16 00:39:11 +020071 default:
72 asic_type = ASIC_INVALID;
73 break;
74 }
75
76 return asic_type;
77}
78
79/*
80 * hl_device_open - open function for habanalabs device
81 *
82 * @inode: pointer to inode structure
83 * @filp: pointer to file structure
84 *
85 * Called when process opens an habanalabs device.
86 */
87int hl_device_open(struct inode *inode, struct file *filp)
88{
89 struct hl_device *hdev;
90 struct hl_fpriv *hpriv;
Oded Gabbay0861e412019-02-16 00:39:14 +020091 int rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +020092
93 mutex_lock(&hl_devs_idr_lock);
94 hdev = idr_find(&hl_devs_idr, iminor(inode));
95 mutex_unlock(&hl_devs_idr_lock);
96
97 if (!hdev) {
98 pr_err("Couldn't find device %d:%d\n",
99 imajor(inode), iminor(inode));
100 return -ENXIO;
101 }
102
103 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300104 if (!hpriv)
105 return -ENOMEM;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200106
107 hpriv->hdev = hdev;
108 filp->private_data = hpriv;
109 hpriv->filp = filp;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200110 mutex_init(&hpriv->restore_phase_mutex);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200111 kref_init(&hpriv->refcount);
112 nonseekable_open(inode, filp);
113
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200114 hl_cb_mgr_init(&hpriv->cb_mgr);
Oded Gabbay0861e412019-02-16 00:39:14 +0200115 hl_ctx_mgr_init(&hpriv->ctx_mgr);
116
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300117 hpriv->taskpid = find_get_pid(current->pid);
118
119 mutex_lock(&hdev->fpriv_list_lock);
120
121 if (hl_device_disabled_or_in_reset(hdev)) {
122 dev_err_ratelimited(hdev->dev,
123 "Can't open %s because it is disabled or in reset\n",
124 dev_name(hdev->dev));
125 rc = -EPERM;
Oded Gabbay0861e412019-02-16 00:39:14 +0200126 goto out_err;
127 }
128
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300129 if (hdev->in_debug) {
130 dev_err_ratelimited(hdev->dev,
131 "Can't open %s because it is being debugged by another user\n",
132 dev_name(hdev->dev));
133 rc = -EPERM;
134 goto out_err;
135 }
Oded Gabbayc4d66342019-02-16 00:39:11 +0200136
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300137 if (hdev->compute_ctx) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300138 dev_dbg_ratelimited(hdev->dev,
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300139 "Can't open %s because another user is working on it\n",
140 dev_name(hdev->dev));
141 rc = -EBUSY;
142 goto out_err;
143 }
144
145 rc = hl_ctx_create(hdev, hpriv);
146 if (rc) {
147 dev_err(hdev->dev, "Failed to create context %d\n", rc);
148 goto out_err;
149 }
150
151 /* Device is IDLE at this point so it is legal to change PLLs.
152 * There is no need to check anything because if the PLL is
153 * already HIGH, the set function will return without doing
154 * anything
Oded Gabbayd91389b2019-02-16 00:39:19 +0200155 */
156 hl_device_set_frequency(hdev, PLL_HIGH);
157
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300158 list_add(&hpriv->dev_node, &hdev->fpriv_list);
159 mutex_unlock(&hdev->fpriv_list_lock);
160
Oded Gabbayc2164772019-02-16 00:39:24 +0200161 hl_debugfs_add_file(hpriv);
162
Oded Gabbayc4d66342019-02-16 00:39:11 +0200163 return 0;
Oded Gabbay0861e412019-02-16 00:39:14 +0200164
165out_err:
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300166 mutex_unlock(&hdev->fpriv_list_lock);
Oded Gabbay0861e412019-02-16 00:39:14 +0200167
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300168 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
169 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
170 filp->private_data = NULL;
171 mutex_destroy(&hpriv->restore_phase_mutex);
172 put_pid(hpriv->taskpid);
173
174 kfree(hpriv);
Oded Gabbay0861e412019-02-16 00:39:14 +0200175 return rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200176}
177
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300178int hl_device_open_ctrl(struct inode *inode, struct file *filp)
179{
180 struct hl_device *hdev;
181 struct hl_fpriv *hpriv;
182 int rc;
183
184 mutex_lock(&hl_devs_idr_lock);
185 hdev = idr_find(&hl_devs_idr, iminor(inode));
186 mutex_unlock(&hl_devs_idr_lock);
187
188 if (!hdev) {
189 pr_err("Couldn't find device %d:%d\n",
190 imajor(inode), iminor(inode));
191 return -ENXIO;
192 }
193
194 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
195 if (!hpriv)
196 return -ENOMEM;
197
198 mutex_lock(&hdev->fpriv_list_lock);
199
200 if (hl_device_disabled_or_in_reset(hdev)) {
201 dev_err_ratelimited(hdev->dev_ctrl,
202 "Can't open %s because it is disabled or in reset\n",
203 dev_name(hdev->dev_ctrl));
204 rc = -EPERM;
205 goto out_err;
206 }
207
208 list_add(&hpriv->dev_node, &hdev->fpriv_list);
209 mutex_unlock(&hdev->fpriv_list_lock);
210
211 hpriv->hdev = hdev;
212 filp->private_data = hpriv;
213 hpriv->filp = filp;
214 hpriv->is_control = true;
215 nonseekable_open(inode, filp);
216
217 hpriv->taskpid = find_get_pid(current->pid);
218
219 return 0;
220
221out_err:
222 mutex_unlock(&hdev->fpriv_list_lock);
223 kfree(hpriv);
224 return rc;
225}
226
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300227static void set_driver_behavior_per_device(struct hl_device *hdev)
228{
229 hdev->mmu_enable = 1;
230 hdev->cpu_enable = 1;
231 hdev->fw_loading = 1;
232 hdev->cpu_queues_enable = 1;
233 hdev->heartbeat = 1;
Oded Gabbayca624332020-05-09 12:17:21 +0300234 hdev->clock_gating = 1;
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300235
236 hdev->reset_pcilink = 0;
237}
238
Oded Gabbayc4d66342019-02-16 00:39:11 +0200239/*
240 * create_hdev - create habanalabs device instance
241 *
242 * @dev: will hold the pointer to the new habanalabs device structure
243 * @pdev: pointer to the pci device
244 * @asic_type: in case of simulator device, which device is it
245 * @minor: in case of simulator device, the minor of the device
246 *
247 * Allocate memory for habanalabs device and initialize basic fields
248 * Identify the ASIC type
249 * Allocate ID (minor) for the device (only for real devices)
250 */
251int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
252 enum hl_asic_type asic_type, int minor)
253{
254 struct hl_device *hdev;
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300255 int rc, main_id, ctrl_id = 0;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200256
257 *dev = NULL;
258
259 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
260 if (!hdev)
261 return -ENOMEM;
262
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300263 /* First, we must find out which ASIC are we handling. This is needed
264 * to configure the behavior of the driver (kernel parameters)
265 */
Oded Gabbay29593842019-04-04 14:33:34 +0300266 if (pdev) {
Oded Gabbayc4d66342019-02-16 00:39:11 +0200267 hdev->asic_type = get_asic_type(pdev->device);
268 if (hdev->asic_type == ASIC_INVALID) {
269 dev_err(&pdev->dev, "Unsupported ASIC\n");
270 rc = -ENODEV;
271 goto free_hdev;
Oded Gabbay6966d9e2020-03-21 10:58:32 +0200272 } else if (hdev->asic_type == ASIC_GAUDI) {
273 dev_err(&pdev->dev,
274 "GAUDI is not supported by the current kernel\n");
275 rc = -ENODEV;
276 goto free_hdev;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200277 }
278 } else {
279 hdev->asic_type = asic_type;
280 }
281
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300282 hdev->major = hl_major;
283 hdev->reset_on_lockup = reset_on_lockup;
284 hdev->pldm = 0;
285
286 set_driver_behavior_per_device(hdev);
287
288 if (timeout_locked)
289 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
290 else
291 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
292
293 hdev->disabled = true;
294 hdev->pdev = pdev; /* can be NULL in case of simulator device */
295
Oded Gabbayd9973872019-03-07 18:03:23 +0200296 /* Set default DMA mask to 32 bits */
297 hdev->dma_mask = 32;
298
Oded Gabbayc4d66342019-02-16 00:39:11 +0200299 mutex_lock(&hl_devs_idr_lock);
300
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300301 /* Always save 2 numbers, 1 for main device and 1 for control.
302 * They must be consecutive
303 */
304 main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
Oded Gabbayc4d66342019-02-16 00:39:11 +0200305 GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200306
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300307 if (main_id >= 0)
308 ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1,
309 main_id + 2, GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200310
311 mutex_unlock(&hl_devs_idr_lock);
312
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300313 if ((main_id < 0) || (ctrl_id < 0)) {
314 if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC))
Oded Gabbayc4d66342019-02-16 00:39:11 +0200315 pr_err("too many devices in the system\n");
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300316
317 if (main_id >= 0) {
318 mutex_lock(&hl_devs_idr_lock);
319 idr_remove(&hl_devs_idr, main_id);
320 mutex_unlock(&hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200321 }
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300322
323 rc = -EBUSY;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200324 goto free_hdev;
325 }
326
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300327 hdev->id = main_id;
328 hdev->id_control = ctrl_id;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200329
330 *dev = hdev;
331
332 return 0;
333
334free_hdev:
335 kfree(hdev);
336 return rc;
337}
338
339/*
340 * destroy_hdev - destroy habanalabs device instance
341 *
342 * @dev: pointer to the habanalabs device structure
343 *
344 */
345void destroy_hdev(struct hl_device *hdev)
346{
347 /* Remove device from the device list */
348 mutex_lock(&hl_devs_idr_lock);
349 idr_remove(&hl_devs_idr, hdev->id);
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300350 idr_remove(&hl_devs_idr, hdev->id_control);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200351 mutex_unlock(&hl_devs_idr_lock);
352
353 kfree(hdev);
354}
355
356static int hl_pmops_suspend(struct device *dev)
357{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800358 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200359
360 pr_debug("Going to suspend PCI device\n");
361
362 if (!hdev) {
363 pr_err("device pointer is NULL in suspend\n");
364 return 0;
365 }
366
367 return hl_device_suspend(hdev);
368}
369
370static int hl_pmops_resume(struct device *dev)
371{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800372 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200373
374 pr_debug("Going to resume PCI device\n");
375
376 if (!hdev) {
377 pr_err("device pointer is NULL in resume\n");
378 return 0;
379 }
380
381 return hl_device_resume(hdev);
382}
383
384/*
385 * hl_pci_probe - probe PCI habanalabs devices
386 *
387 * @pdev: pointer to pci device
388 * @id: pointer to pci device id structure
389 *
390 * Standard PCI probe function for habanalabs device.
391 * Create a new habanalabs device and initialize it according to the
392 * device's type
393 */
394static int hl_pci_probe(struct pci_dev *pdev,
395 const struct pci_device_id *id)
396{
397 struct hl_device *hdev;
398 int rc;
399
400 dev_info(&pdev->dev, HL_NAME
401 " device found [%04x:%04x] (rev %x)\n",
402 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
403
Oded Gabbay29593842019-04-04 14:33:34 +0300404 rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200405 if (rc)
406 return rc;
407
408 pci_set_drvdata(pdev, hdev);
409
410 rc = hl_device_init(hdev, hl_class);
411 if (rc) {
412 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
413 rc = -ENODEV;
414 goto disable_device;
415 }
416
417 return 0;
418
419disable_device:
420 pci_set_drvdata(pdev, NULL);
421 destroy_hdev(hdev);
422
423 return rc;
424}
425
426/*
427 * hl_pci_remove - remove PCI habanalabs devices
428 *
429 * @pdev: pointer to pci device
430 *
431 * Standard PCI remove function for habanalabs device
432 */
433static void hl_pci_remove(struct pci_dev *pdev)
434{
435 struct hl_device *hdev;
436
437 hdev = pci_get_drvdata(pdev);
438 if (!hdev)
439 return;
440
441 hl_device_fini(hdev);
442 pci_set_drvdata(pdev, NULL);
443
444 destroy_hdev(hdev);
445}
446
447static const struct dev_pm_ops hl_pm_ops = {
448 .suspend = hl_pmops_suspend,
449 .resume = hl_pmops_resume,
450};
451
452static struct pci_driver hl_pci_driver = {
453 .name = HL_NAME,
454 .id_table = ids,
455 .probe = hl_pci_probe,
456 .remove = hl_pci_remove,
457 .driver.pm = &hl_pm_ops,
458};
459
460/*
461 * hl_init - Initialize the habanalabs kernel driver
462 */
463static int __init hl_init(void)
464{
465 int rc;
466 dev_t dev;
467
468 pr_info("loading driver\n");
469
470 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
471 if (rc < 0) {
472 pr_err("unable to get major\n");
473 return rc;
474 }
475
476 hl_major = MAJOR(dev);
477
478 hl_class = class_create(THIS_MODULE, HL_NAME);
479 if (IS_ERR(hl_class)) {
480 pr_err("failed to allocate class\n");
481 rc = PTR_ERR(hl_class);
482 goto remove_major;
483 }
484
Oded Gabbayc2164772019-02-16 00:39:24 +0200485 hl_debugfs_init();
486
Oded Gabbayc4d66342019-02-16 00:39:11 +0200487 rc = pci_register_driver(&hl_pci_driver);
488 if (rc) {
489 pr_err("failed to register pci device\n");
Oded Gabbayc2164772019-02-16 00:39:24 +0200490 goto remove_debugfs;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200491 }
492
493 pr_debug("driver loaded\n");
494
495 return 0;
496
Oded Gabbayc2164772019-02-16 00:39:24 +0200497remove_debugfs:
498 hl_debugfs_fini();
Oded Gabbayc4d66342019-02-16 00:39:11 +0200499 class_destroy(hl_class);
500remove_major:
501 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
502 return rc;
503}
504
505/*
506 * hl_exit - Release all resources of the habanalabs kernel driver
507 */
508static void __exit hl_exit(void)
509{
510 pci_unregister_driver(&hl_pci_driver);
511
Oded Gabbayc2164772019-02-16 00:39:24 +0200512 /*
513 * Removing debugfs must be after all devices or simulator devices
514 * have been removed because otherwise we get a bug in the
515 * debugfs module for referencing NULL objects
516 */
517 hl_debugfs_fini();
518
Oded Gabbayc4d66342019-02-16 00:39:11 +0200519 class_destroy(hl_class);
520 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
521
522 idr_destroy(&hl_devs_idr);
523
524 pr_debug("driver removed\n");
525}
526
527module_init(hl_init);
528module_exit(hl_exit);