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