Merge tag 'v5.13' into next
[linux-2.6-microblaze.git] / drivers / misc / habanalabs / common / 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 #define pr_fmt(fmt)             "habanalabs: " fmt
10
11 #include "habanalabs.h"
12
13 #include <linux/pci.h>
14 #include <linux/aer.h>
15 #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
21 MODULE_AUTHOR(HL_DRIVER_AUTHOR);
22 MODULE_DESCRIPTION(HL_DRIVER_DESC);
23 MODULE_LICENSE("GPL v2");
24
25 static int hl_major;
26 static struct class *hl_class;
27 static DEFINE_IDR(hl_devs_idr);
28 static DEFINE_MUTEX(hl_devs_idr_lock);
29
30 static int timeout_locked = 30;
31 static int reset_on_lockup = 1;
32 static int memory_scrub = 1;
33 static ulong boot_error_status_mask = ULONG_MAX;
34
35 module_param(timeout_locked, int, 0444);
36 MODULE_PARM_DESC(timeout_locked,
37         "Device lockup timeout in seconds (0 = disabled, default 30s)");
38
39 module_param(reset_on_lockup, int, 0444);
40 MODULE_PARM_DESC(reset_on_lockup,
41         "Do device reset on lockup (0 = no, 1 = yes, default yes)");
42
43 module_param(memory_scrub, int, 0444);
44 MODULE_PARM_DESC(memory_scrub,
45         "Scrub device memory in various states (0 = no, 1 = yes, default yes)");
46
47 module_param(boot_error_status_mask, ulong, 0444);
48 MODULE_PARM_DESC(boot_error_status_mask,
49         "Mask of the error status during device CPU boot (If bitX is cleared then error X is masked. Default all 1's)");
50
51 #define PCI_VENDOR_ID_HABANALABS        0x1da3
52
53 #define PCI_IDS_GOYA                    0x0001
54 #define PCI_IDS_GAUDI                   0x1000
55 #define PCI_IDS_GAUDI_SEC               0x1010
56
57 static const struct pci_device_id ids[] = {
58         { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
59         { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI), },
60         { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI_SEC), },
61         { 0, }
62 };
63 MODULE_DEVICE_TABLE(pci, ids);
64
65 /*
66  * get_asic_type - translate device id to asic type
67  *
68  * @device: id of the PCI device
69  *
70  * Translate device id to asic type.
71  * In case of unidentified device, return -1
72  */
73 static enum hl_asic_type get_asic_type(u16 device)
74 {
75         enum hl_asic_type asic_type;
76
77         switch (device) {
78         case PCI_IDS_GOYA:
79                 asic_type = ASIC_GOYA;
80                 break;
81         case PCI_IDS_GAUDI:
82                 asic_type = ASIC_GAUDI;
83                 break;
84         case PCI_IDS_GAUDI_SEC:
85                 asic_type = ASIC_GAUDI_SEC;
86                 break;
87         default:
88                 asic_type = ASIC_INVALID;
89                 break;
90         }
91
92         return asic_type;
93 }
94
95 static bool is_asic_secured(enum hl_asic_type asic_type)
96 {
97         switch (asic_type) {
98         case ASIC_GAUDI_SEC:
99                 return true;
100         default:
101                 return false;
102         }
103 }
104
105 /*
106  * hl_device_open - open function for habanalabs device
107  *
108  * @inode: pointer to inode structure
109  * @filp: pointer to file structure
110  *
111  * Called when process opens an habanalabs device.
112  */
113 int hl_device_open(struct inode *inode, struct file *filp)
114 {
115         enum hl_device_status status;
116         struct hl_device *hdev;
117         struct hl_fpriv *hpriv;
118         int rc;
119
120         mutex_lock(&hl_devs_idr_lock);
121         hdev = idr_find(&hl_devs_idr, iminor(inode));
122         mutex_unlock(&hl_devs_idr_lock);
123
124         if (!hdev) {
125                 pr_err("Couldn't find device %d:%d\n",
126                         imajor(inode), iminor(inode));
127                 return -ENXIO;
128         }
129
130         hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
131         if (!hpriv)
132                 return -ENOMEM;
133
134         hpriv->hdev = hdev;
135         filp->private_data = hpriv;
136         hpriv->filp = filp;
137         mutex_init(&hpriv->restore_phase_mutex);
138         kref_init(&hpriv->refcount);
139         nonseekable_open(inode, filp);
140
141         hl_cb_mgr_init(&hpriv->cb_mgr);
142         hl_ctx_mgr_init(&hpriv->ctx_mgr);
143
144         hpriv->taskpid = find_get_pid(current->pid);
145
146         mutex_lock(&hdev->fpriv_list_lock);
147
148         if (!hl_device_operational(hdev, &status)) {
149                 dev_err_ratelimited(hdev->dev,
150                         "Can't open %s because it is %s\n",
151                         dev_name(hdev->dev), hdev->status[status]);
152                 rc = -EPERM;
153                 goto out_err;
154         }
155
156         if (hdev->in_debug) {
157                 dev_err_ratelimited(hdev->dev,
158                         "Can't open %s because it is being debugged by another user\n",
159                         dev_name(hdev->dev));
160                 rc = -EPERM;
161                 goto out_err;
162         }
163
164         if (hdev->compute_ctx) {
165                 dev_dbg_ratelimited(hdev->dev,
166                         "Can't open %s because another user is working on it\n",
167                         dev_name(hdev->dev));
168                 rc = -EBUSY;
169                 goto out_err;
170         }
171
172         rc = hl_ctx_create(hdev, hpriv);
173         if (rc) {
174                 dev_err(hdev->dev, "Failed to create context %d\n", rc);
175                 goto out_err;
176         }
177
178         /* Device is IDLE at this point so it is legal to change PLLs.
179          * There is no need to check anything because if the PLL is
180          * already HIGH, the set function will return without doing
181          * anything
182          */
183         hl_device_set_frequency(hdev, PLL_HIGH);
184
185         list_add(&hpriv->dev_node, &hdev->fpriv_list);
186         mutex_unlock(&hdev->fpriv_list_lock);
187
188         hl_debugfs_add_file(hpriv);
189
190         return 0;
191
192 out_err:
193         mutex_unlock(&hdev->fpriv_list_lock);
194
195         hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
196         hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
197         filp->private_data = NULL;
198         mutex_destroy(&hpriv->restore_phase_mutex);
199         put_pid(hpriv->taskpid);
200
201         kfree(hpriv);
202
203         return rc;
204 }
205
206 int hl_device_open_ctrl(struct inode *inode, struct file *filp)
207 {
208         struct hl_device *hdev;
209         struct hl_fpriv *hpriv;
210         int rc;
211
212         mutex_lock(&hl_devs_idr_lock);
213         hdev = idr_find(&hl_devs_idr, iminor(inode));
214         mutex_unlock(&hl_devs_idr_lock);
215
216         if (!hdev) {
217                 pr_err("Couldn't find device %d:%d\n",
218                         imajor(inode), iminor(inode));
219                 return -ENXIO;
220         }
221
222         hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
223         if (!hpriv)
224                 return -ENOMEM;
225
226         mutex_lock(&hdev->fpriv_list_lock);
227
228         if (!hl_device_operational(hdev, NULL)) {
229                 dev_err_ratelimited(hdev->dev_ctrl,
230                         "Can't open %s because it is disabled or in reset\n",
231                         dev_name(hdev->dev_ctrl));
232                 rc = -EPERM;
233                 goto out_err;
234         }
235
236         list_add(&hpriv->dev_node, &hdev->fpriv_list);
237         mutex_unlock(&hdev->fpriv_list_lock);
238
239         hpriv->hdev = hdev;
240         filp->private_data = hpriv;
241         hpriv->filp = filp;
242         hpriv->is_control = true;
243         nonseekable_open(inode, filp);
244
245         hpriv->taskpid = find_get_pid(current->pid);
246
247         return 0;
248
249 out_err:
250         mutex_unlock(&hdev->fpriv_list_lock);
251         kfree(hpriv);
252         return rc;
253 }
254
255 static void set_driver_behavior_per_device(struct hl_device *hdev)
256 {
257         hdev->fw_components = FW_TYPE_ALL_TYPES;
258         hdev->cpu_queues_enable = 1;
259         hdev->heartbeat = 1;
260         hdev->mmu_enable = 1;
261         hdev->clock_gating_mask = ULONG_MAX;
262         hdev->sram_scrambler_enable = 1;
263         hdev->dram_scrambler_enable = 1;
264         hdev->bmc_enable = 1;
265         hdev->hard_reset_on_fw_events = 1;
266         hdev->reset_on_preboot_fail = 1;
267
268         hdev->reset_pcilink = 0;
269         hdev->axi_drain = 0;
270 }
271
272 /*
273  * create_hdev - create habanalabs device instance
274  *
275  * @dev: will hold the pointer to the new habanalabs device structure
276  * @pdev: pointer to the pci device
277  * @asic_type: in case of simulator device, which device is it
278  * @minor: in case of simulator device, the minor of the device
279  *
280  * Allocate memory for habanalabs device and initialize basic fields
281  * Identify the ASIC type
282  * Allocate ID (minor) for the device (only for real devices)
283  */
284 int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
285                 enum hl_asic_type asic_type, int minor)
286 {
287         struct hl_device *hdev;
288         int rc, main_id, ctrl_id = 0;
289
290         *dev = NULL;
291
292         hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
293         if (!hdev)
294                 return -ENOMEM;
295
296         /* First, we must find out which ASIC are we handling. This is needed
297          * to configure the behavior of the driver (kernel parameters)
298          */
299         if (pdev) {
300                 hdev->asic_type = get_asic_type(pdev->device);
301                 if (hdev->asic_type == ASIC_INVALID) {
302                         dev_err(&pdev->dev, "Unsupported ASIC\n");
303                         rc = -ENODEV;
304                         goto free_hdev;
305                 }
306         } else {
307                 hdev->asic_type = asic_type;
308         }
309
310         if (pdev)
311                 hdev->asic_prop.fw_security_disabled =
312                                 !is_asic_secured(pdev->device);
313         else
314                 hdev->asic_prop.fw_security_disabled = true;
315
316         /* Assign status description string */
317         strncpy(hdev->status[HL_DEVICE_STATUS_MALFUNCTION],
318                                         "disabled", HL_STR_MAX);
319         strncpy(hdev->status[HL_DEVICE_STATUS_IN_RESET],
320                                         "in reset", HL_STR_MAX);
321         strncpy(hdev->status[HL_DEVICE_STATUS_NEEDS_RESET],
322                                         "needs reset", HL_STR_MAX);
323
324         hdev->major = hl_major;
325         hdev->reset_on_lockup = reset_on_lockup;
326         hdev->memory_scrub = memory_scrub;
327         hdev->boot_error_status_mask = boot_error_status_mask;
328
329         hdev->pldm = 0;
330
331         set_driver_behavior_per_device(hdev);
332
333         if (timeout_locked)
334                 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
335         else
336                 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
337
338         hdev->disabled = true;
339         hdev->pdev = pdev; /* can be NULL in case of simulator device */
340
341         /* Set default DMA mask to 32 bits */
342         hdev->dma_mask = 32;
343
344         mutex_lock(&hl_devs_idr_lock);
345
346         /* Always save 2 numbers, 1 for main device and 1 for control.
347          * They must be consecutive
348          */
349         main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
350                                 GFP_KERNEL);
351
352         if (main_id >= 0)
353                 ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1,
354                                         main_id + 2, GFP_KERNEL);
355
356         mutex_unlock(&hl_devs_idr_lock);
357
358         if ((main_id < 0) || (ctrl_id < 0)) {
359                 if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC))
360                         pr_err("too many devices in the system\n");
361
362                 if (main_id >= 0) {
363                         mutex_lock(&hl_devs_idr_lock);
364                         idr_remove(&hl_devs_idr, main_id);
365                         mutex_unlock(&hl_devs_idr_lock);
366                 }
367
368                 rc = -EBUSY;
369                 goto free_hdev;
370         }
371
372         hdev->id = main_id;
373         hdev->id_control = ctrl_id;
374
375         *dev = hdev;
376
377         return 0;
378
379 free_hdev:
380         kfree(hdev);
381         return rc;
382 }
383
384 /*
385  * destroy_hdev - destroy habanalabs device instance
386  *
387  * @dev: pointer to the habanalabs device structure
388  *
389  */
390 void destroy_hdev(struct hl_device *hdev)
391 {
392         /* Remove device from the device list */
393         mutex_lock(&hl_devs_idr_lock);
394         idr_remove(&hl_devs_idr, hdev->id);
395         idr_remove(&hl_devs_idr, hdev->id_control);
396         mutex_unlock(&hl_devs_idr_lock);
397
398         kfree(hdev);
399 }
400
401 static int hl_pmops_suspend(struct device *dev)
402 {
403         struct hl_device *hdev = dev_get_drvdata(dev);
404
405         pr_debug("Going to suspend PCI device\n");
406
407         if (!hdev) {
408                 pr_err("device pointer is NULL in suspend\n");
409                 return 0;
410         }
411
412         return hl_device_suspend(hdev);
413 }
414
415 static int hl_pmops_resume(struct device *dev)
416 {
417         struct hl_device *hdev = dev_get_drvdata(dev);
418
419         pr_debug("Going to resume PCI device\n");
420
421         if (!hdev) {
422                 pr_err("device pointer is NULL in resume\n");
423                 return 0;
424         }
425
426         return hl_device_resume(hdev);
427 }
428
429 /*
430  * hl_pci_probe - probe PCI habanalabs devices
431  *
432  * @pdev: pointer to pci device
433  * @id: pointer to pci device id structure
434  *
435  * Standard PCI probe function for habanalabs device.
436  * Create a new habanalabs device and initialize it according to the
437  * device's type
438  */
439 static int hl_pci_probe(struct pci_dev *pdev,
440                                 const struct pci_device_id *id)
441 {
442         struct hl_device *hdev;
443         int rc;
444
445         dev_info(&pdev->dev, HL_NAME
446                  " device found [%04x:%04x] (rev %x)\n",
447                  (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
448
449         rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1);
450         if (rc)
451                 return rc;
452
453         pci_set_drvdata(pdev, hdev);
454
455         pci_enable_pcie_error_reporting(pdev);
456
457         rc = hl_device_init(hdev, hl_class);
458         if (rc) {
459                 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
460                 rc = -ENODEV;
461                 goto disable_device;
462         }
463
464         return 0;
465
466 disable_device:
467         pci_set_drvdata(pdev, NULL);
468         destroy_hdev(hdev);
469
470         return rc;
471 }
472
473 /*
474  * hl_pci_remove - remove PCI habanalabs devices
475  *
476  * @pdev: pointer to pci device
477  *
478  * Standard PCI remove function for habanalabs device
479  */
480 static void hl_pci_remove(struct pci_dev *pdev)
481 {
482         struct hl_device *hdev;
483
484         hdev = pci_get_drvdata(pdev);
485         if (!hdev)
486                 return;
487
488         hl_device_fini(hdev);
489         pci_disable_pcie_error_reporting(pdev);
490         pci_set_drvdata(pdev, NULL);
491         destroy_hdev(hdev);
492 }
493
494 /**
495  * hl_pci_err_detected - a PCI bus error detected on this device
496  *
497  * @pdev: pointer to pci device
498  * @state: PCI error type
499  *
500  * Called by the PCI subsystem whenever a non-correctable
501  * PCI bus error is detected
502  */
503 static pci_ers_result_t
504 hl_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t state)
505 {
506         struct hl_device *hdev = pci_get_drvdata(pdev);
507         enum pci_ers_result result;
508
509         switch (state) {
510         case pci_channel_io_normal:
511                 return PCI_ERS_RESULT_CAN_RECOVER;
512
513         case pci_channel_io_frozen:
514                 dev_warn(hdev->dev, "frozen state error detected\n");
515                 result = PCI_ERS_RESULT_NEED_RESET;
516                 break;
517
518         case pci_channel_io_perm_failure:
519                 dev_warn(hdev->dev, "failure state error detected\n");
520                 result = PCI_ERS_RESULT_DISCONNECT;
521                 break;
522
523         default:
524                 result = PCI_ERS_RESULT_NONE;
525         }
526
527         hdev->asic_funcs->halt_engines(hdev, true);
528
529         return result;
530 }
531
532 /**
533  * hl_pci_err_resume - resume after a PCI slot reset
534  *
535  * @pdev: pointer to pci device
536  *
537  */
538 static void hl_pci_err_resume(struct pci_dev *pdev)
539 {
540         struct hl_device *hdev = pci_get_drvdata(pdev);
541
542         dev_warn(hdev->dev, "Resuming device after PCI slot reset\n");
543         hl_device_resume(hdev);
544 }
545
546 /**
547  * hl_pci_err_slot_reset - a PCI slot reset has just happened
548  *
549  * @pdev: pointer to pci device
550  *
551  * Determine if the driver can recover from the PCI slot reset
552  */
553 static pci_ers_result_t hl_pci_err_slot_reset(struct pci_dev *pdev)
554 {
555         return PCI_ERS_RESULT_RECOVERED;
556 }
557
558 static const struct dev_pm_ops hl_pm_ops = {
559         .suspend = hl_pmops_suspend,
560         .resume = hl_pmops_resume,
561 };
562
563 static const struct pci_error_handlers hl_pci_err_handler = {
564         .error_detected = hl_pci_err_detected,
565         .slot_reset = hl_pci_err_slot_reset,
566         .resume = hl_pci_err_resume,
567 };
568
569 static struct pci_driver hl_pci_driver = {
570         .name = HL_NAME,
571         .id_table = ids,
572         .probe = hl_pci_probe,
573         .remove = hl_pci_remove,
574         .shutdown = hl_pci_remove,
575         .driver.pm = &hl_pm_ops,
576         .err_handler = &hl_pci_err_handler,
577 };
578
579 /*
580  * hl_init - Initialize the habanalabs kernel driver
581  */
582 static int __init hl_init(void)
583 {
584         int rc;
585         dev_t dev;
586
587         pr_info("loading driver\n");
588
589         rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
590         if (rc < 0) {
591                 pr_err("unable to get major\n");
592                 return rc;
593         }
594
595         hl_major = MAJOR(dev);
596
597         hl_class = class_create(THIS_MODULE, HL_NAME);
598         if (IS_ERR(hl_class)) {
599                 pr_err("failed to allocate class\n");
600                 rc = PTR_ERR(hl_class);
601                 goto remove_major;
602         }
603
604         hl_debugfs_init();
605
606         rc = pci_register_driver(&hl_pci_driver);
607         if (rc) {
608                 pr_err("failed to register pci device\n");
609                 goto remove_debugfs;
610         }
611
612         pr_debug("driver loaded\n");
613
614         return 0;
615
616 remove_debugfs:
617         hl_debugfs_fini();
618         class_destroy(hl_class);
619 remove_major:
620         unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
621         return rc;
622 }
623
624 /*
625  * hl_exit - Release all resources of the habanalabs kernel driver
626  */
627 static void __exit hl_exit(void)
628 {
629         pci_unregister_driver(&hl_pci_driver);
630
631         /*
632          * Removing debugfs must be after all devices or simulator devices
633          * have been removed because otherwise we get a bug in the
634          * debugfs module for referencing NULL objects
635          */
636         hl_debugfs_fini();
637
638         class_destroy(hl_class);
639         unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
640
641         idr_destroy(&hl_devs_idr);
642
643         pr_debug("driver removed\n");
644 }
645
646 module_init(hl_init);
647 module_exit(hl_exit);