blob: f9067d3ef43765b661ed0b31c0a044c958fa0148 [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>
Ofir Bitton2e5eda42020-05-20 16:35:08 +030014#include <linux/aer.h>
Oded Gabbayc4d66342019-02-16 00:39:11 +020015#include <linux/module.h>
16
17#define HL_DRIVER_AUTHOR "HabanaLabs Kernel Driver Team"
18
19#define HL_DRIVER_DESC "Driver for HabanaLabs's AI Accelerators"
20
21MODULE_AUTHOR(HL_DRIVER_AUTHOR);
22MODULE_DESCRIPTION(HL_DRIVER_DESC);
23MODULE_LICENSE("GPL v2");
24
25static int hl_major;
26static struct class *hl_class;
Oded Gabbay5e6e0232019-02-27 12:15:16 +020027static DEFINE_IDR(hl_devs_idr);
28static DEFINE_MUTEX(hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +020029
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020030static int timeout_locked = 5;
31static int reset_on_lockup = 1;
32
33module_param(timeout_locked, int, 0444);
34MODULE_PARM_DESC(timeout_locked,
35 "Device lockup timeout in seconds (0 = disabled, default 5s)");
36
37module_param(reset_on_lockup, int, 0444);
38MODULE_PARM_DESC(reset_on_lockup,
39 "Do device reset on lockup (0 = no, 1 = yes, default yes)");
40
Oded Gabbayc4d66342019-02-16 00:39:11 +020041#define PCI_VENDOR_ID_HABANALABS 0x1da3
42
43#define PCI_IDS_GOYA 0x0001
Oded Gabbay6966d9e2020-03-21 10:58:32 +020044#define PCI_IDS_GAUDI 0x1000
Oded Gabbayc4d66342019-02-16 00:39:11 +020045
46static const struct pci_device_id ids[] = {
47 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
Oded Gabbay6966d9e2020-03-21 10:58:32 +020048 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI), },
Oded Gabbayc4d66342019-02-16 00:39:11 +020049 { 0, }
50};
Oded Gabbay824b4572020-05-03 15:30:55 +030051MODULE_DEVICE_TABLE(pci, ids);
Oded Gabbayc4d66342019-02-16 00:39:11 +020052
53/*
54 * get_asic_type - translate device id to asic type
55 *
56 * @device: id of the PCI device
57 *
58 * Translate device id to asic type.
59 * In case of unidentified device, return -1
60 */
61static enum hl_asic_type get_asic_type(u16 device)
62{
63 enum hl_asic_type asic_type;
64
65 switch (device) {
66 case PCI_IDS_GOYA:
67 asic_type = ASIC_GOYA;
68 break;
Oded Gabbay6966d9e2020-03-21 10:58:32 +020069 case PCI_IDS_GAUDI:
70 asic_type = ASIC_GAUDI;
71 break;
Oded Gabbayc4d66342019-02-16 00:39:11 +020072 default:
73 asic_type = ASIC_INVALID;
74 break;
75 }
76
77 return asic_type;
78}
79
80/*
81 * hl_device_open - open function for habanalabs device
82 *
83 * @inode: pointer to inode structure
84 * @filp: pointer to file structure
85 *
86 * Called when process opens an habanalabs device.
87 */
88int hl_device_open(struct inode *inode, struct file *filp)
89{
90 struct hl_device *hdev;
91 struct hl_fpriv *hpriv;
Oded Gabbay0861e412019-02-16 00:39:14 +020092 int rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +020093
94 mutex_lock(&hl_devs_idr_lock);
95 hdev = idr_find(&hl_devs_idr, iminor(inode));
96 mutex_unlock(&hl_devs_idr_lock);
97
98 if (!hdev) {
99 pr_err("Couldn't find device %d:%d\n",
100 imajor(inode), iminor(inode));
101 return -ENXIO;
102 }
103
104 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300105 if (!hpriv)
106 return -ENOMEM;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200107
108 hpriv->hdev = hdev;
109 filp->private_data = hpriv;
110 hpriv->filp = filp;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200111 mutex_init(&hpriv->restore_phase_mutex);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200112 kref_init(&hpriv->refcount);
113 nonseekable_open(inode, filp);
114
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200115 hl_cb_mgr_init(&hpriv->cb_mgr);
Oded Gabbay0861e412019-02-16 00:39:14 +0200116 hl_ctx_mgr_init(&hpriv->ctx_mgr);
117
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300118 hpriv->taskpid = find_get_pid(current->pid);
119
120 mutex_lock(&hdev->fpriv_list_lock);
121
122 if (hl_device_disabled_or_in_reset(hdev)) {
123 dev_err_ratelimited(hdev->dev,
124 "Can't open %s because it is disabled or in reset\n",
125 dev_name(hdev->dev));
126 rc = -EPERM;
Oded Gabbay0861e412019-02-16 00:39:14 +0200127 goto out_err;
128 }
129
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300130 if (hdev->in_debug) {
131 dev_err_ratelimited(hdev->dev,
132 "Can't open %s because it is being debugged by another user\n",
133 dev_name(hdev->dev));
134 rc = -EPERM;
135 goto out_err;
136 }
Oded Gabbayc4d66342019-02-16 00:39:11 +0200137
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300138 if (hdev->compute_ctx) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300139 dev_dbg_ratelimited(hdev->dev,
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300140 "Can't open %s because another user is working on it\n",
141 dev_name(hdev->dev));
142 rc = -EBUSY;
143 goto out_err;
144 }
145
146 rc = hl_ctx_create(hdev, hpriv);
147 if (rc) {
148 dev_err(hdev->dev, "Failed to create context %d\n", rc);
149 goto out_err;
150 }
151
152 /* Device is IDLE at this point so it is legal to change PLLs.
153 * There is no need to check anything because if the PLL is
154 * already HIGH, the set function will return without doing
155 * anything
Oded Gabbayd91389b2019-02-16 00:39:19 +0200156 */
157 hl_device_set_frequency(hdev, PLL_HIGH);
158
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300159 list_add(&hpriv->dev_node, &hdev->fpriv_list);
160 mutex_unlock(&hdev->fpriv_list_lock);
161
Oded Gabbayc2164772019-02-16 00:39:24 +0200162 hl_debugfs_add_file(hpriv);
163
Oded Gabbayc4d66342019-02-16 00:39:11 +0200164 return 0;
Oded Gabbay0861e412019-02-16 00:39:14 +0200165
166out_err:
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300167 mutex_unlock(&hdev->fpriv_list_lock);
Oded Gabbay0861e412019-02-16 00:39:14 +0200168
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300169 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
170 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
171 filp->private_data = NULL;
172 mutex_destroy(&hpriv->restore_phase_mutex);
173 put_pid(hpriv->taskpid);
174
175 kfree(hpriv);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300176
Oded Gabbay0861e412019-02-16 00:39:14 +0200177 return rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200178}
179
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300180int hl_device_open_ctrl(struct inode *inode, struct file *filp)
181{
182 struct hl_device *hdev;
183 struct hl_fpriv *hpriv;
184 int rc;
185
186 mutex_lock(&hl_devs_idr_lock);
187 hdev = idr_find(&hl_devs_idr, iminor(inode));
188 mutex_unlock(&hl_devs_idr_lock);
189
190 if (!hdev) {
191 pr_err("Couldn't find device %d:%d\n",
192 imajor(inode), iminor(inode));
193 return -ENXIO;
194 }
195
196 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
197 if (!hpriv)
198 return -ENOMEM;
199
200 mutex_lock(&hdev->fpriv_list_lock);
201
202 if (hl_device_disabled_or_in_reset(hdev)) {
203 dev_err_ratelimited(hdev->dev_ctrl,
204 "Can't open %s because it is disabled or in reset\n",
205 dev_name(hdev->dev_ctrl));
206 rc = -EPERM;
207 goto out_err;
208 }
209
210 list_add(&hpriv->dev_node, &hdev->fpriv_list);
211 mutex_unlock(&hdev->fpriv_list_lock);
212
213 hpriv->hdev = hdev;
214 filp->private_data = hpriv;
215 hpriv->filp = filp;
216 hpriv->is_control = true;
217 nonseekable_open(inode, filp);
218
219 hpriv->taskpid = find_get_pid(current->pid);
220
221 return 0;
222
223out_err:
224 mutex_unlock(&hdev->fpriv_list_lock);
225 kfree(hpriv);
226 return rc;
227}
228
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300229static void set_driver_behavior_per_device(struct hl_device *hdev)
230{
231 hdev->mmu_enable = 1;
232 hdev->cpu_enable = 1;
233 hdev->fw_loading = 1;
234 hdev->cpu_queues_enable = 1;
235 hdev->heartbeat = 1;
Oded Gabbaye38bfd32020-07-03 20:46:12 +0300236 hdev->clock_gating_mask = ULONG_MAX;
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300237
238 hdev->reset_pcilink = 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300239 hdev->axi_drain = 0;
240 hdev->sram_scrambler_enable = 1;
241 hdev->dram_scrambler_enable = 1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300242 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;
279 }
280 } else {
281 hdev->asic_type = asic_type;
282 }
283
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300284 hdev->major = hl_major;
285 hdev->reset_on_lockup = reset_on_lockup;
286 hdev->pldm = 0;
287
288 set_driver_behavior_per_device(hdev);
289
290 if (timeout_locked)
291 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
292 else
293 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
294
295 hdev->disabled = true;
296 hdev->pdev = pdev; /* can be NULL in case of simulator device */
297
Oded Gabbayd9973872019-03-07 18:03:23 +0200298 /* Set default DMA mask to 32 bits */
299 hdev->dma_mask = 32;
300
Oded Gabbayc4d66342019-02-16 00:39:11 +0200301 mutex_lock(&hl_devs_idr_lock);
302
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300303 /* Always save 2 numbers, 1 for main device and 1 for control.
304 * They must be consecutive
305 */
306 main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
Oded Gabbayc4d66342019-02-16 00:39:11 +0200307 GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200308
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300309 if (main_id >= 0)
310 ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1,
311 main_id + 2, GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200312
313 mutex_unlock(&hl_devs_idr_lock);
314
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300315 if ((main_id < 0) || (ctrl_id < 0)) {
316 if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC))
Oded Gabbayc4d66342019-02-16 00:39:11 +0200317 pr_err("too many devices in the system\n");
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300318
319 if (main_id >= 0) {
320 mutex_lock(&hl_devs_idr_lock);
321 idr_remove(&hl_devs_idr, main_id);
322 mutex_unlock(&hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200323 }
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300324
325 rc = -EBUSY;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200326 goto free_hdev;
327 }
328
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300329 hdev->id = main_id;
330 hdev->id_control = ctrl_id;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200331
332 *dev = hdev;
333
334 return 0;
335
336free_hdev:
337 kfree(hdev);
338 return rc;
339}
340
341/*
342 * destroy_hdev - destroy habanalabs device instance
343 *
344 * @dev: pointer to the habanalabs device structure
345 *
346 */
347void destroy_hdev(struct hl_device *hdev)
348{
349 /* Remove device from the device list */
350 mutex_lock(&hl_devs_idr_lock);
351 idr_remove(&hl_devs_idr, hdev->id);
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300352 idr_remove(&hl_devs_idr, hdev->id_control);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200353 mutex_unlock(&hl_devs_idr_lock);
354
355 kfree(hdev);
356}
357
358static int hl_pmops_suspend(struct device *dev)
359{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800360 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200361
362 pr_debug("Going to suspend PCI device\n");
363
364 if (!hdev) {
365 pr_err("device pointer is NULL in suspend\n");
366 return 0;
367 }
368
369 return hl_device_suspend(hdev);
370}
371
372static int hl_pmops_resume(struct device *dev)
373{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800374 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200375
376 pr_debug("Going to resume PCI device\n");
377
378 if (!hdev) {
379 pr_err("device pointer is NULL in resume\n");
380 return 0;
381 }
382
383 return hl_device_resume(hdev);
384}
385
386/*
387 * hl_pci_probe - probe PCI habanalabs devices
388 *
389 * @pdev: pointer to pci device
390 * @id: pointer to pci device id structure
391 *
392 * Standard PCI probe function for habanalabs device.
393 * Create a new habanalabs device and initialize it according to the
394 * device's type
395 */
396static int hl_pci_probe(struct pci_dev *pdev,
397 const struct pci_device_id *id)
398{
399 struct hl_device *hdev;
400 int rc;
401
402 dev_info(&pdev->dev, HL_NAME
403 " device found [%04x:%04x] (rev %x)\n",
404 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
405
Oded Gabbay29593842019-04-04 14:33:34 +0300406 rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200407 if (rc)
408 return rc;
409
410 pci_set_drvdata(pdev, hdev);
411
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300412 pci_enable_pcie_error_reporting(pdev);
413
Oded Gabbayc4d66342019-02-16 00:39:11 +0200414 rc = hl_device_init(hdev, hl_class);
415 if (rc) {
416 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
417 rc = -ENODEV;
418 goto disable_device;
419 }
420
421 return 0;
422
423disable_device:
424 pci_set_drvdata(pdev, NULL);
425 destroy_hdev(hdev);
426
427 return rc;
428}
429
430/*
431 * hl_pci_remove - remove PCI habanalabs devices
432 *
433 * @pdev: pointer to pci device
434 *
435 * Standard PCI remove function for habanalabs device
436 */
437static void hl_pci_remove(struct pci_dev *pdev)
438{
439 struct hl_device *hdev;
440
441 hdev = pci_get_drvdata(pdev);
442 if (!hdev)
443 return;
444
445 hl_device_fini(hdev);
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300446 pci_disable_pcie_error_reporting(pdev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200447 pci_set_drvdata(pdev, NULL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200448 destroy_hdev(hdev);
449}
450
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300451/**
452 * hl_pci_err_detected - a PCI bus error detected on this device
453 *
454 * @pdev: pointer to pci device
455 * @state: PCI error type
456 *
457 * Called by the PCI subsystem whenever a non-correctable
458 * PCI bus error is detected
459 */
460static pci_ers_result_t
461hl_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t state)
462{
463 struct hl_device *hdev = pci_get_drvdata(pdev);
464 enum pci_ers_result result;
465
466 switch (state) {
467 case pci_channel_io_normal:
468 return PCI_ERS_RESULT_CAN_RECOVER;
469
470 case pci_channel_io_frozen:
471 dev_warn(hdev->dev, "frozen state error detected\n");
472 result = PCI_ERS_RESULT_NEED_RESET;
473 break;
474
475 case pci_channel_io_perm_failure:
476 dev_warn(hdev->dev, "failure state error detected\n");
477 result = PCI_ERS_RESULT_DISCONNECT;
478 break;
479
480 default:
481 result = PCI_ERS_RESULT_NONE;
482 }
483
484 hdev->asic_funcs->halt_engines(hdev, true);
485
486 return result;
487}
488
489/**
490 * hl_pci_err_resume - resume after a PCI slot reset
491 *
492 * @pdev: pointer to pci device
493 *
494 */
495static void hl_pci_err_resume(struct pci_dev *pdev)
496{
497 struct hl_device *hdev = pci_get_drvdata(pdev);
498
499 dev_warn(hdev->dev, "Resuming device after PCI slot reset\n");
500 hl_device_resume(hdev);
501}
502
503/**
504 * hl_pci_err_slot_reset - a PCI slot reset has just happened
505 *
506 * @pdev: pointer to pci device
507 *
508 * Determine if the driver can recover from the PCI slot reset
509 */
510static pci_ers_result_t hl_pci_err_slot_reset(struct pci_dev *pdev)
511{
512 return PCI_ERS_RESULT_RECOVERED;
513}
514
Oded Gabbayc4d66342019-02-16 00:39:11 +0200515static const struct dev_pm_ops hl_pm_ops = {
516 .suspend = hl_pmops_suspend,
517 .resume = hl_pmops_resume,
518};
519
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300520static const struct pci_error_handlers hl_pci_err_handler = {
521 .error_detected = hl_pci_err_detected,
522 .slot_reset = hl_pci_err_slot_reset,
523 .resume = hl_pci_err_resume,
524};
525
Oded Gabbayc4d66342019-02-16 00:39:11 +0200526static struct pci_driver hl_pci_driver = {
527 .name = HL_NAME,
528 .id_table = ids,
529 .probe = hl_pci_probe,
530 .remove = hl_pci_remove,
531 .driver.pm = &hl_pm_ops,
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300532 .err_handler = &hl_pci_err_handler,
Oded Gabbayc4d66342019-02-16 00:39:11 +0200533};
534
535/*
536 * hl_init - Initialize the habanalabs kernel driver
537 */
538static int __init hl_init(void)
539{
540 int rc;
541 dev_t dev;
542
543 pr_info("loading driver\n");
544
545 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
546 if (rc < 0) {
547 pr_err("unable to get major\n");
548 return rc;
549 }
550
551 hl_major = MAJOR(dev);
552
553 hl_class = class_create(THIS_MODULE, HL_NAME);
554 if (IS_ERR(hl_class)) {
555 pr_err("failed to allocate class\n");
556 rc = PTR_ERR(hl_class);
557 goto remove_major;
558 }
559
Oded Gabbayc2164772019-02-16 00:39:24 +0200560 hl_debugfs_init();
561
Oded Gabbayc4d66342019-02-16 00:39:11 +0200562 rc = pci_register_driver(&hl_pci_driver);
563 if (rc) {
564 pr_err("failed to register pci device\n");
Oded Gabbayc2164772019-02-16 00:39:24 +0200565 goto remove_debugfs;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200566 }
567
568 pr_debug("driver loaded\n");
569
570 return 0;
571
Oded Gabbayc2164772019-02-16 00:39:24 +0200572remove_debugfs:
573 hl_debugfs_fini();
Oded Gabbayc4d66342019-02-16 00:39:11 +0200574 class_destroy(hl_class);
575remove_major:
576 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
577 return rc;
578}
579
580/*
581 * hl_exit - Release all resources of the habanalabs kernel driver
582 */
583static void __exit hl_exit(void)
584{
585 pci_unregister_driver(&hl_pci_driver);
586
Oded Gabbayc2164772019-02-16 00:39:24 +0200587 /*
588 * Removing debugfs must be after all devices or simulator devices
589 * have been removed because otherwise we get a bug in the
590 * debugfs module for referencing NULL objects
591 */
592 hl_debugfs_fini();
593
Oded Gabbayc4d66342019-02-16 00:39:11 +0200594 class_destroy(hl_class);
595 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
596
597 idr_destroy(&hl_devs_idr);
598
599 pr_debug("driver removed\n");
600}
601
602module_init(hl_init);
603module_exit(hl_exit);