blob: 1667df7ca64c53515e448ab0d7e1d1acbe3ec8f6 [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
9#include "habanalabs.h"
10
11#include <linux/pci.h>
12#include <linux/module.h>
13
14#define HL_DRIVER_AUTHOR "HabanaLabs Kernel Driver Team"
15
16#define HL_DRIVER_DESC "Driver for HabanaLabs's AI Accelerators"
17
18MODULE_AUTHOR(HL_DRIVER_AUTHOR);
19MODULE_DESCRIPTION(HL_DRIVER_DESC);
20MODULE_LICENSE("GPL v2");
21
22static int hl_major;
23static struct class *hl_class;
Oded Gabbay5e6e0232019-02-27 12:15:16 +020024static DEFINE_IDR(hl_devs_idr);
25static DEFINE_MUTEX(hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +020026
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020027static int timeout_locked = 5;
28static int reset_on_lockup = 1;
29
30module_param(timeout_locked, int, 0444);
31MODULE_PARM_DESC(timeout_locked,
32 "Device lockup timeout in seconds (0 = disabled, default 5s)");
33
34module_param(reset_on_lockup, int, 0444);
35MODULE_PARM_DESC(reset_on_lockup,
36 "Do device reset on lockup (0 = no, 1 = yes, default yes)");
37
Oded Gabbayc4d66342019-02-16 00:39:11 +020038#define PCI_VENDOR_ID_HABANALABS 0x1da3
39
40#define PCI_IDS_GOYA 0x0001
41
42static const struct pci_device_id ids[] = {
43 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
44 { 0, }
45};
46MODULE_DEVICE_TABLE(pci, ids);
47
48/*
49 * get_asic_type - translate device id to asic type
50 *
51 * @device: id of the PCI device
52 *
53 * Translate device id to asic type.
54 * In case of unidentified device, return -1
55 */
56static enum hl_asic_type get_asic_type(u16 device)
57{
58 enum hl_asic_type asic_type;
59
60 switch (device) {
61 case PCI_IDS_GOYA:
62 asic_type = ASIC_GOYA;
63 break;
64 default:
65 asic_type = ASIC_INVALID;
66 break;
67 }
68
69 return asic_type;
70}
71
72/*
73 * hl_device_open - open function for habanalabs device
74 *
75 * @inode: pointer to inode structure
76 * @filp: pointer to file structure
77 *
78 * Called when process opens an habanalabs device.
79 */
80int hl_device_open(struct inode *inode, struct file *filp)
81{
82 struct hl_device *hdev;
83 struct hl_fpriv *hpriv;
Oded Gabbay0861e412019-02-16 00:39:14 +020084 int rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +020085
86 mutex_lock(&hl_devs_idr_lock);
87 hdev = idr_find(&hl_devs_idr, iminor(inode));
88 mutex_unlock(&hl_devs_idr_lock);
89
90 if (!hdev) {
91 pr_err("Couldn't find device %d:%d\n",
92 imajor(inode), iminor(inode));
93 return -ENXIO;
94 }
95
Oded Gabbay0861e412019-02-16 00:39:14 +020096 mutex_lock(&hdev->fd_open_cnt_lock);
97
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +020098 if (hl_device_disabled_or_in_reset(hdev)) {
Oded Gabbay0861e412019-02-16 00:39:14 +020099 dev_err_ratelimited(hdev->dev,
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200100 "Can't open %s because it is disabled or in reset\n",
Oded Gabbay0861e412019-02-16 00:39:14 +0200101 dev_name(hdev->dev));
102 mutex_unlock(&hdev->fd_open_cnt_lock);
103 return -EPERM;
104 }
105
106 if (atomic_read(&hdev->fd_open_cnt)) {
107 dev_info_ratelimited(hdev->dev,
108 "Device %s is already attached to application\n",
109 dev_name(hdev->dev));
110 mutex_unlock(&hdev->fd_open_cnt_lock);
111 return -EBUSY;
112 }
113
114 atomic_inc(&hdev->fd_open_cnt);
115
116 mutex_unlock(&hdev->fd_open_cnt_lock);
117
Oded Gabbayc4d66342019-02-16 00:39:11 +0200118 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
Oded Gabbay0861e412019-02-16 00:39:14 +0200119 if (!hpriv) {
120 rc = -ENOMEM;
121 goto close_device;
122 }
Oded Gabbayc4d66342019-02-16 00:39:11 +0200123
124 hpriv->hdev = hdev;
125 filp->private_data = hpriv;
126 hpriv->filp = filp;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200127 mutex_init(&hpriv->restore_phase_mutex);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200128 kref_init(&hpriv->refcount);
129 nonseekable_open(inode, filp);
130
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200131 hl_cb_mgr_init(&hpriv->cb_mgr);
Oded Gabbay0861e412019-02-16 00:39:14 +0200132 hl_ctx_mgr_init(&hpriv->ctx_mgr);
133
134 rc = hl_ctx_create(hdev, hpriv);
135 if (rc) {
136 dev_err(hdev->dev, "Failed to open FD (CTX fail)\n");
137 goto out_err;
138 }
139
Oded Gabbayc4d66342019-02-16 00:39:11 +0200140 hpriv->taskpid = find_get_pid(current->pid);
141
Oded Gabbayd91389b2019-02-16 00:39:19 +0200142 /*
143 * Device is IDLE at this point so it is legal to change PLLs. There
144 * is no need to check anything because if the PLL is already HIGH, the
145 * set function will return without doing anything
146 */
147 hl_device_set_frequency(hdev, PLL_HIGH);
148
Oded Gabbayc2164772019-02-16 00:39:24 +0200149 hl_debugfs_add_file(hpriv);
150
Oded Gabbayc4d66342019-02-16 00:39:11 +0200151 return 0;
Oded Gabbay0861e412019-02-16 00:39:14 +0200152
153out_err:
154 filp->private_data = NULL;
155 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200156 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200157 mutex_destroy(&hpriv->restore_phase_mutex);
Oded Gabbay0861e412019-02-16 00:39:14 +0200158 kfree(hpriv);
159
160close_device:
161 atomic_dec(&hdev->fd_open_cnt);
162 return rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200163}
164
165/*
166 * create_hdev - create habanalabs device instance
167 *
168 * @dev: will hold the pointer to the new habanalabs device structure
169 * @pdev: pointer to the pci device
170 * @asic_type: in case of simulator device, which device is it
171 * @minor: in case of simulator device, the minor of the device
172 *
173 * Allocate memory for habanalabs device and initialize basic fields
174 * Identify the ASIC type
175 * Allocate ID (minor) for the device (only for real devices)
176 */
177int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
178 enum hl_asic_type asic_type, int minor)
179{
180 struct hl_device *hdev;
181 int rc;
182
183 *dev = NULL;
184
185 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
186 if (!hdev)
187 return -ENOMEM;
188
189 hdev->major = hl_major;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200190 hdev->reset_on_lockup = reset_on_lockup;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200191
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200192 /* Parameters for bring-up - set them to defaults */
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200193 hdev->mmu_enable = 1;
Oded Gabbay839c4802019-02-16 00:39:16 +0200194 hdev->cpu_enable = 1;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200195 hdev->reset_pcilink = 0;
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200196 hdev->cpu_queues_enable = 1;
Oded Gabbay839c4802019-02-16 00:39:16 +0200197 hdev->fw_loading = 1;
198 hdev->pldm = 0;
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200199 hdev->heartbeat = 1;
Oded Gabbay839c4802019-02-16 00:39:16 +0200200
201 /* If CPU is disabled, no point in loading FW */
202 if (!hdev->cpu_enable)
203 hdev->fw_loading = 0;
Oded Gabbay99b9d7b2019-02-16 00:39:13 +0200204
Oded Gabbay9494a8d2019-02-16 00:39:17 +0200205 /* If we don't load FW, no need to initialize CPU queues */
206 if (!hdev->fw_loading)
207 hdev->cpu_queues_enable = 0;
208
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200209 /* If CPU queues not enabled, no way to do heartbeat */
210 if (!hdev->cpu_queues_enable)
211 hdev->heartbeat = 0;
212
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200213 if (timeout_locked)
214 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
215 else
216 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
217
Oded Gabbayc4d66342019-02-16 00:39:11 +0200218 hdev->disabled = true;
219 hdev->pdev = pdev; /* can be NULL in case of simulator device */
220
Oded Gabbay29593842019-04-04 14:33:34 +0300221 if (pdev) {
Oded Gabbayc4d66342019-02-16 00:39:11 +0200222 hdev->asic_type = get_asic_type(pdev->device);
223 if (hdev->asic_type == ASIC_INVALID) {
224 dev_err(&pdev->dev, "Unsupported ASIC\n");
225 rc = -ENODEV;
226 goto free_hdev;
227 }
228 } else {
229 hdev->asic_type = asic_type;
230 }
231
Oded Gabbayd9973872019-03-07 18:03:23 +0200232 /* Set default DMA mask to 32 bits */
233 hdev->dma_mask = 32;
234
Oded Gabbayc4d66342019-02-16 00:39:11 +0200235 mutex_lock(&hl_devs_idr_lock);
236
237 if (minor == -1) {
238 rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
239 GFP_KERNEL);
240 } else {
241 void *old_idr = idr_replace(&hl_devs_idr, hdev, minor);
242
243 if (IS_ERR_VALUE(old_idr)) {
244 rc = PTR_ERR(old_idr);
245 pr_err("Error %d when trying to replace minor %d\n",
246 rc, minor);
247 mutex_unlock(&hl_devs_idr_lock);
248 goto free_hdev;
249 }
250 rc = minor;
251 }
252
253 mutex_unlock(&hl_devs_idr_lock);
254
255 if (rc < 0) {
256 if (rc == -ENOSPC) {
257 pr_err("too many devices in the system\n");
258 rc = -EBUSY;
259 }
260 goto free_hdev;
261 }
262
263 hdev->id = rc;
264
265 *dev = hdev;
266
267 return 0;
268
269free_hdev:
270 kfree(hdev);
271 return rc;
272}
273
274/*
275 * destroy_hdev - destroy habanalabs device instance
276 *
277 * @dev: pointer to the habanalabs device structure
278 *
279 */
280void destroy_hdev(struct hl_device *hdev)
281{
282 /* Remove device from the device list */
283 mutex_lock(&hl_devs_idr_lock);
284 idr_remove(&hl_devs_idr, hdev->id);
285 mutex_unlock(&hl_devs_idr_lock);
286
287 kfree(hdev);
288}
289
290static int hl_pmops_suspend(struct device *dev)
291{
292 struct pci_dev *pdev = to_pci_dev(dev);
293 struct hl_device *hdev = pci_get_drvdata(pdev);
294
295 pr_debug("Going to suspend PCI device\n");
296
297 if (!hdev) {
298 pr_err("device pointer is NULL in suspend\n");
299 return 0;
300 }
301
302 return hl_device_suspend(hdev);
303}
304
305static int hl_pmops_resume(struct device *dev)
306{
307 struct pci_dev *pdev = to_pci_dev(dev);
308 struct hl_device *hdev = pci_get_drvdata(pdev);
309
310 pr_debug("Going to resume PCI device\n");
311
312 if (!hdev) {
313 pr_err("device pointer is NULL in resume\n");
314 return 0;
315 }
316
317 return hl_device_resume(hdev);
318}
319
320/*
321 * hl_pci_probe - probe PCI habanalabs devices
322 *
323 * @pdev: pointer to pci device
324 * @id: pointer to pci device id structure
325 *
326 * Standard PCI probe function for habanalabs device.
327 * Create a new habanalabs device and initialize it according to the
328 * device's type
329 */
330static int hl_pci_probe(struct pci_dev *pdev,
331 const struct pci_device_id *id)
332{
333 struct hl_device *hdev;
334 int rc;
335
336 dev_info(&pdev->dev, HL_NAME
337 " device found [%04x:%04x] (rev %x)\n",
338 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
339
Oded Gabbay29593842019-04-04 14:33:34 +0300340 rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200341 if (rc)
342 return rc;
343
344 pci_set_drvdata(pdev, hdev);
345
346 rc = hl_device_init(hdev, hl_class);
347 if (rc) {
348 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
349 rc = -ENODEV;
350 goto disable_device;
351 }
352
353 return 0;
354
355disable_device:
356 pci_set_drvdata(pdev, NULL);
357 destroy_hdev(hdev);
358
359 return rc;
360}
361
362/*
363 * hl_pci_remove - remove PCI habanalabs devices
364 *
365 * @pdev: pointer to pci device
366 *
367 * Standard PCI remove function for habanalabs device
368 */
369static void hl_pci_remove(struct pci_dev *pdev)
370{
371 struct hl_device *hdev;
372
373 hdev = pci_get_drvdata(pdev);
374 if (!hdev)
375 return;
376
377 hl_device_fini(hdev);
378 pci_set_drvdata(pdev, NULL);
379
380 destroy_hdev(hdev);
381}
382
383static const struct dev_pm_ops hl_pm_ops = {
384 .suspend = hl_pmops_suspend,
385 .resume = hl_pmops_resume,
386};
387
388static struct pci_driver hl_pci_driver = {
389 .name = HL_NAME,
390 .id_table = ids,
391 .probe = hl_pci_probe,
392 .remove = hl_pci_remove,
393 .driver.pm = &hl_pm_ops,
394};
395
396/*
397 * hl_init - Initialize the habanalabs kernel driver
398 */
399static int __init hl_init(void)
400{
401 int rc;
402 dev_t dev;
403
404 pr_info("loading driver\n");
405
406 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
407 if (rc < 0) {
408 pr_err("unable to get major\n");
409 return rc;
410 }
411
412 hl_major = MAJOR(dev);
413
414 hl_class = class_create(THIS_MODULE, HL_NAME);
415 if (IS_ERR(hl_class)) {
416 pr_err("failed to allocate class\n");
417 rc = PTR_ERR(hl_class);
418 goto remove_major;
419 }
420
Oded Gabbayc2164772019-02-16 00:39:24 +0200421 hl_debugfs_init();
422
Oded Gabbayc4d66342019-02-16 00:39:11 +0200423 rc = pci_register_driver(&hl_pci_driver);
424 if (rc) {
425 pr_err("failed to register pci device\n");
Oded Gabbayc2164772019-02-16 00:39:24 +0200426 goto remove_debugfs;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200427 }
428
429 pr_debug("driver loaded\n");
430
431 return 0;
432
Oded Gabbayc2164772019-02-16 00:39:24 +0200433remove_debugfs:
434 hl_debugfs_fini();
Oded Gabbayc4d66342019-02-16 00:39:11 +0200435 class_destroy(hl_class);
436remove_major:
437 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
438 return rc;
439}
440
441/*
442 * hl_exit - Release all resources of the habanalabs kernel driver
443 */
444static void __exit hl_exit(void)
445{
446 pci_unregister_driver(&hl_pci_driver);
447
Oded Gabbayc2164772019-02-16 00:39:24 +0200448 /*
449 * Removing debugfs must be after all devices or simulator devices
450 * have been removed because otherwise we get a bug in the
451 * debugfs module for referencing NULL objects
452 */
453 hl_debugfs_fini();
454
Oded Gabbayc4d66342019-02-16 00:39:11 +0200455 class_destroy(hl_class);
456 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
457
458 idr_destroy(&hl_devs_idr);
459
460 pr_debug("driver removed\n");
461}
462
463module_init(hl_init);
464module_exit(hl_exit);