blob: 9d009b50aa0ddc66507667c57977273056af6aac [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 Gabbayac0ae6a2020-05-11 10:29:27 +0300175
Oded Gabbay0861e412019-02-16 00:39:14 +0200176 return rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200177}
178
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300179int hl_device_open_ctrl(struct inode *inode, struct file *filp)
180{
181 struct hl_device *hdev;
182 struct hl_fpriv *hpriv;
183 int rc;
184
185 mutex_lock(&hl_devs_idr_lock);
186 hdev = idr_find(&hl_devs_idr, iminor(inode));
187 mutex_unlock(&hl_devs_idr_lock);
188
189 if (!hdev) {
190 pr_err("Couldn't find device %d:%d\n",
191 imajor(inode), iminor(inode));
192 return -ENXIO;
193 }
194
195 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
196 if (!hpriv)
197 return -ENOMEM;
198
199 mutex_lock(&hdev->fpriv_list_lock);
200
201 if (hl_device_disabled_or_in_reset(hdev)) {
202 dev_err_ratelimited(hdev->dev_ctrl,
203 "Can't open %s because it is disabled or in reset\n",
204 dev_name(hdev->dev_ctrl));
205 rc = -EPERM;
206 goto out_err;
207 }
208
209 list_add(&hpriv->dev_node, &hdev->fpriv_list);
210 mutex_unlock(&hdev->fpriv_list_lock);
211
212 hpriv->hdev = hdev;
213 filp->private_data = hpriv;
214 hpriv->filp = filp;
215 hpriv->is_control = true;
216 nonseekable_open(inode, filp);
217
218 hpriv->taskpid = find_get_pid(current->pid);
219
220 return 0;
221
222out_err:
223 mutex_unlock(&hdev->fpriv_list_lock);
224 kfree(hpriv);
225 return rc;
226}
227
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300228static void set_driver_behavior_per_device(struct hl_device *hdev)
229{
230 hdev->mmu_enable = 1;
231 hdev->cpu_enable = 1;
232 hdev->fw_loading = 1;
233 hdev->cpu_queues_enable = 1;
234 hdev->heartbeat = 1;
Oded Gabbayca624332020-05-09 12:17:21 +0300235 hdev->clock_gating = 1;
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300236
237 hdev->reset_pcilink = 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300238 hdev->axi_drain = 0;
239 hdev->sram_scrambler_enable = 1;
240 hdev->dram_scrambler_enable = 1;
241 hdev->rl_enable = 1;
242 hdev->bmc_enable = 1;
243 hdev->hard_reset_on_fw_events = 1;
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300244}
245
Oded Gabbayc4d66342019-02-16 00:39:11 +0200246/*
247 * create_hdev - create habanalabs device instance
248 *
249 * @dev: will hold the pointer to the new habanalabs device structure
250 * @pdev: pointer to the pci device
251 * @asic_type: in case of simulator device, which device is it
252 * @minor: in case of simulator device, the minor of the device
253 *
254 * Allocate memory for habanalabs device and initialize basic fields
255 * Identify the ASIC type
256 * Allocate ID (minor) for the device (only for real devices)
257 */
258int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
259 enum hl_asic_type asic_type, int minor)
260{
261 struct hl_device *hdev;
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300262 int rc, main_id, ctrl_id = 0;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200263
264 *dev = NULL;
265
266 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
267 if (!hdev)
268 return -ENOMEM;
269
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300270 /* First, we must find out which ASIC are we handling. This is needed
271 * to configure the behavior of the driver (kernel parameters)
272 */
Oded Gabbay29593842019-04-04 14:33:34 +0300273 if (pdev) {
Oded Gabbayc4d66342019-02-16 00:39:11 +0200274 hdev->asic_type = get_asic_type(pdev->device);
275 if (hdev->asic_type == ASIC_INVALID) {
276 dev_err(&pdev->dev, "Unsupported ASIC\n");
277 rc = -ENODEV;
278 goto free_hdev;
Oded Gabbay6966d9e2020-03-21 10:58:32 +0200279 } else if (hdev->asic_type == ASIC_GAUDI) {
280 dev_err(&pdev->dev,
281 "GAUDI is not supported by the current kernel\n");
282 rc = -ENODEV;
283 goto free_hdev;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200284 }
285 } else {
286 hdev->asic_type = asic_type;
287 }
288
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300289 hdev->major = hl_major;
290 hdev->reset_on_lockup = reset_on_lockup;
291 hdev->pldm = 0;
292
293 set_driver_behavior_per_device(hdev);
294
295 if (timeout_locked)
296 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
297 else
298 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
299
300 hdev->disabled = true;
301 hdev->pdev = pdev; /* can be NULL in case of simulator device */
302
Oded Gabbayd9973872019-03-07 18:03:23 +0200303 /* Set default DMA mask to 32 bits */
304 hdev->dma_mask = 32;
305
Oded Gabbayc4d66342019-02-16 00:39:11 +0200306 mutex_lock(&hl_devs_idr_lock);
307
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300308 /* Always save 2 numbers, 1 for main device and 1 for control.
309 * They must be consecutive
310 */
311 main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
Oded Gabbayc4d66342019-02-16 00:39:11 +0200312 GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200313
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300314 if (main_id >= 0)
315 ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1,
316 main_id + 2, GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200317
318 mutex_unlock(&hl_devs_idr_lock);
319
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300320 if ((main_id < 0) || (ctrl_id < 0)) {
321 if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC))
Oded Gabbayc4d66342019-02-16 00:39:11 +0200322 pr_err("too many devices in the system\n");
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300323
324 if (main_id >= 0) {
325 mutex_lock(&hl_devs_idr_lock);
326 idr_remove(&hl_devs_idr, main_id);
327 mutex_unlock(&hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200328 }
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300329
330 rc = -EBUSY;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200331 goto free_hdev;
332 }
333
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300334 hdev->id = main_id;
335 hdev->id_control = ctrl_id;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200336
337 *dev = hdev;
338
339 return 0;
340
341free_hdev:
342 kfree(hdev);
343 return rc;
344}
345
346/*
347 * destroy_hdev - destroy habanalabs device instance
348 *
349 * @dev: pointer to the habanalabs device structure
350 *
351 */
352void destroy_hdev(struct hl_device *hdev)
353{
354 /* Remove device from the device list */
355 mutex_lock(&hl_devs_idr_lock);
356 idr_remove(&hl_devs_idr, hdev->id);
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300357 idr_remove(&hl_devs_idr, hdev->id_control);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200358 mutex_unlock(&hl_devs_idr_lock);
359
360 kfree(hdev);
361}
362
363static int hl_pmops_suspend(struct device *dev)
364{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800365 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200366
367 pr_debug("Going to suspend PCI device\n");
368
369 if (!hdev) {
370 pr_err("device pointer is NULL in suspend\n");
371 return 0;
372 }
373
374 return hl_device_suspend(hdev);
375}
376
377static int hl_pmops_resume(struct device *dev)
378{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800379 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200380
381 pr_debug("Going to resume PCI device\n");
382
383 if (!hdev) {
384 pr_err("device pointer is NULL in resume\n");
385 return 0;
386 }
387
388 return hl_device_resume(hdev);
389}
390
391/*
392 * hl_pci_probe - probe PCI habanalabs devices
393 *
394 * @pdev: pointer to pci device
395 * @id: pointer to pci device id structure
396 *
397 * Standard PCI probe function for habanalabs device.
398 * Create a new habanalabs device and initialize it according to the
399 * device's type
400 */
401static int hl_pci_probe(struct pci_dev *pdev,
402 const struct pci_device_id *id)
403{
404 struct hl_device *hdev;
405 int rc;
406
407 dev_info(&pdev->dev, HL_NAME
408 " device found [%04x:%04x] (rev %x)\n",
409 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
410
Oded Gabbay29593842019-04-04 14:33:34 +0300411 rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200412 if (rc)
413 return rc;
414
415 pci_set_drvdata(pdev, hdev);
416
417 rc = hl_device_init(hdev, hl_class);
418 if (rc) {
419 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
420 rc = -ENODEV;
421 goto disable_device;
422 }
423
424 return 0;
425
426disable_device:
427 pci_set_drvdata(pdev, NULL);
428 destroy_hdev(hdev);
429
430 return rc;
431}
432
433/*
434 * hl_pci_remove - remove PCI habanalabs devices
435 *
436 * @pdev: pointer to pci device
437 *
438 * Standard PCI remove function for habanalabs device
439 */
440static void hl_pci_remove(struct pci_dev *pdev)
441{
442 struct hl_device *hdev;
443
444 hdev = pci_get_drvdata(pdev);
445 if (!hdev)
446 return;
447
448 hl_device_fini(hdev);
449 pci_set_drvdata(pdev, NULL);
450
451 destroy_hdev(hdev);
452}
453
454static const struct dev_pm_ops hl_pm_ops = {
455 .suspend = hl_pmops_suspend,
456 .resume = hl_pmops_resume,
457};
458
459static struct pci_driver hl_pci_driver = {
460 .name = HL_NAME,
461 .id_table = ids,
462 .probe = hl_pci_probe,
463 .remove = hl_pci_remove,
464 .driver.pm = &hl_pm_ops,
465};
466
467/*
468 * hl_init - Initialize the habanalabs kernel driver
469 */
470static int __init hl_init(void)
471{
472 int rc;
473 dev_t dev;
474
475 pr_info("loading driver\n");
476
477 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
478 if (rc < 0) {
479 pr_err("unable to get major\n");
480 return rc;
481 }
482
483 hl_major = MAJOR(dev);
484
485 hl_class = class_create(THIS_MODULE, HL_NAME);
486 if (IS_ERR(hl_class)) {
487 pr_err("failed to allocate class\n");
488 rc = PTR_ERR(hl_class);
489 goto remove_major;
490 }
491
Oded Gabbayc2164772019-02-16 00:39:24 +0200492 hl_debugfs_init();
493
Oded Gabbayc4d66342019-02-16 00:39:11 +0200494 rc = pci_register_driver(&hl_pci_driver);
495 if (rc) {
496 pr_err("failed to register pci device\n");
Oded Gabbayc2164772019-02-16 00:39:24 +0200497 goto remove_debugfs;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200498 }
499
500 pr_debug("driver loaded\n");
501
502 return 0;
503
Oded Gabbayc2164772019-02-16 00:39:24 +0200504remove_debugfs:
505 hl_debugfs_fini();
Oded Gabbayc4d66342019-02-16 00:39:11 +0200506 class_destroy(hl_class);
507remove_major:
508 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
509 return rc;
510}
511
512/*
513 * hl_exit - Release all resources of the habanalabs kernel driver
514 */
515static void __exit hl_exit(void)
516{
517 pci_unregister_driver(&hl_pci_driver);
518
Oded Gabbayc2164772019-02-16 00:39:24 +0200519 /*
520 * Removing debugfs must be after all devices or simulator devices
521 * have been removed because otherwise we get a bug in the
522 * debugfs module for referencing NULL objects
523 */
524 hl_debugfs_fini();
525
Oded Gabbayc4d66342019-02-16 00:39:11 +0200526 class_destroy(hl_class);
527 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
528
529 idr_destroy(&hl_devs_idr);
530
531 pr_debug("driver removed\n");
532}
533
534module_init(hl_init);
535module_exit(hl_exit);