Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 1 | // 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 | |
| 18 | MODULE_AUTHOR(HL_DRIVER_AUTHOR); |
| 19 | MODULE_DESCRIPTION(HL_DRIVER_DESC); |
| 20 | MODULE_LICENSE("GPL v2"); |
| 21 | |
| 22 | static int hl_major; |
| 23 | static struct class *hl_class; |
| 24 | DEFINE_IDR(hl_devs_idr); |
| 25 | DEFINE_MUTEX(hl_devs_idr_lock); |
| 26 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame^] | 27 | static int timeout_locked = 5; |
| 28 | static int reset_on_lockup = 1; |
| 29 | |
| 30 | module_param(timeout_locked, int, 0444); |
| 31 | MODULE_PARM_DESC(timeout_locked, |
| 32 | "Device lockup timeout in seconds (0 = disabled, default 5s)"); |
| 33 | |
| 34 | module_param(reset_on_lockup, int, 0444); |
| 35 | MODULE_PARM_DESC(reset_on_lockup, |
| 36 | "Do device reset on lockup (0 = no, 1 = yes, default yes)"); |
| 37 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 38 | #define PCI_VENDOR_ID_HABANALABS 0x1da3 |
| 39 | |
| 40 | #define PCI_IDS_GOYA 0x0001 |
| 41 | |
| 42 | static const struct pci_device_id ids[] = { |
| 43 | { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), }, |
| 44 | { 0, } |
| 45 | }; |
| 46 | MODULE_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 | */ |
| 56 | static 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 | */ |
| 80 | int hl_device_open(struct inode *inode, struct file *filp) |
| 81 | { |
| 82 | struct hl_device *hdev; |
| 83 | struct hl_fpriv *hpriv; |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 84 | int rc; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 85 | |
| 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 Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 96 | mutex_lock(&hdev->fd_open_cnt_lock); |
| 97 | |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 98 | if (hl_device_disabled_or_in_reset(hdev)) { |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 99 | dev_err_ratelimited(hdev->dev, |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 100 | "Can't open %s because it is disabled or in reset\n", |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 101 | 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 Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 118 | hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL); |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 119 | if (!hpriv) { |
| 120 | rc = -ENOMEM; |
| 121 | goto close_device; |
| 122 | } |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 123 | |
| 124 | hpriv->hdev = hdev; |
| 125 | filp->private_data = hpriv; |
| 126 | hpriv->filp = filp; |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame^] | 127 | mutex_init(&hpriv->restore_phase_mutex); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 128 | kref_init(&hpriv->refcount); |
| 129 | nonseekable_open(inode, filp); |
| 130 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 131 | hl_cb_mgr_init(&hpriv->cb_mgr); |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 132 | 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 Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 140 | hpriv->taskpid = find_get_pid(current->pid); |
| 141 | |
Oded Gabbay | d91389b | 2019-02-16 00:39:19 +0200 | [diff] [blame] | 142 | /* |
| 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 Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 149 | return 0; |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 150 | |
| 151 | out_err: |
| 152 | filp->private_data = NULL; |
| 153 | hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 154 | hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr); |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame^] | 155 | mutex_destroy(&hpriv->restore_phase_mutex); |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 156 | kfree(hpriv); |
| 157 | |
| 158 | close_device: |
| 159 | atomic_dec(&hdev->fd_open_cnt); |
| 160 | return rc; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* |
| 164 | * create_hdev - create habanalabs device instance |
| 165 | * |
| 166 | * @dev: will hold the pointer to the new habanalabs device structure |
| 167 | * @pdev: pointer to the pci device |
| 168 | * @asic_type: in case of simulator device, which device is it |
| 169 | * @minor: in case of simulator device, the minor of the device |
| 170 | * |
| 171 | * Allocate memory for habanalabs device and initialize basic fields |
| 172 | * Identify the ASIC type |
| 173 | * Allocate ID (minor) for the device (only for real devices) |
| 174 | */ |
| 175 | int create_hdev(struct hl_device **dev, struct pci_dev *pdev, |
| 176 | enum hl_asic_type asic_type, int minor) |
| 177 | { |
| 178 | struct hl_device *hdev; |
| 179 | int rc; |
| 180 | |
| 181 | *dev = NULL; |
| 182 | |
| 183 | hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); |
| 184 | if (!hdev) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | hdev->major = hl_major; |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame^] | 188 | hdev->reset_on_lockup = reset_on_lockup; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 189 | |
Oded Gabbay | 99b9d7b | 2019-02-16 00:39:13 +0200 | [diff] [blame] | 190 | /* Parameters for bring-up - set them to defaults */ |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame^] | 191 | hdev->mmu_enable = 0; |
Oded Gabbay | 839c480 | 2019-02-16 00:39:16 +0200 | [diff] [blame] | 192 | hdev->cpu_enable = 1; |
Oded Gabbay | 99b9d7b | 2019-02-16 00:39:13 +0200 | [diff] [blame] | 193 | hdev->reset_pcilink = 0; |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 194 | hdev->cpu_queues_enable = 1; |
Oded Gabbay | 839c480 | 2019-02-16 00:39:16 +0200 | [diff] [blame] | 195 | hdev->fw_loading = 1; |
| 196 | hdev->pldm = 0; |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 197 | hdev->heartbeat = 1; |
Oded Gabbay | 839c480 | 2019-02-16 00:39:16 +0200 | [diff] [blame] | 198 | |
| 199 | /* If CPU is disabled, no point in loading FW */ |
| 200 | if (!hdev->cpu_enable) |
| 201 | hdev->fw_loading = 0; |
Oded Gabbay | 99b9d7b | 2019-02-16 00:39:13 +0200 | [diff] [blame] | 202 | |
Oded Gabbay | 9494a8d | 2019-02-16 00:39:17 +0200 | [diff] [blame] | 203 | /* If we don't load FW, no need to initialize CPU queues */ |
| 204 | if (!hdev->fw_loading) |
| 205 | hdev->cpu_queues_enable = 0; |
| 206 | |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 207 | /* If CPU queues not enabled, no way to do heartbeat */ |
| 208 | if (!hdev->cpu_queues_enable) |
| 209 | hdev->heartbeat = 0; |
| 210 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame^] | 211 | if (timeout_locked) |
| 212 | hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000); |
| 213 | else |
| 214 | hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT; |
| 215 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 216 | hdev->disabled = true; |
| 217 | hdev->pdev = pdev; /* can be NULL in case of simulator device */ |
| 218 | |
| 219 | if (asic_type == ASIC_AUTO_DETECT) { |
| 220 | hdev->asic_type = get_asic_type(pdev->device); |
| 221 | if (hdev->asic_type == ASIC_INVALID) { |
| 222 | dev_err(&pdev->dev, "Unsupported ASIC\n"); |
| 223 | rc = -ENODEV; |
| 224 | goto free_hdev; |
| 225 | } |
| 226 | } else { |
| 227 | hdev->asic_type = asic_type; |
| 228 | } |
| 229 | |
| 230 | mutex_lock(&hl_devs_idr_lock); |
| 231 | |
| 232 | if (minor == -1) { |
| 233 | rc = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS, |
| 234 | GFP_KERNEL); |
| 235 | } else { |
| 236 | void *old_idr = idr_replace(&hl_devs_idr, hdev, minor); |
| 237 | |
| 238 | if (IS_ERR_VALUE(old_idr)) { |
| 239 | rc = PTR_ERR(old_idr); |
| 240 | pr_err("Error %d when trying to replace minor %d\n", |
| 241 | rc, minor); |
| 242 | mutex_unlock(&hl_devs_idr_lock); |
| 243 | goto free_hdev; |
| 244 | } |
| 245 | rc = minor; |
| 246 | } |
| 247 | |
| 248 | mutex_unlock(&hl_devs_idr_lock); |
| 249 | |
| 250 | if (rc < 0) { |
| 251 | if (rc == -ENOSPC) { |
| 252 | pr_err("too many devices in the system\n"); |
| 253 | rc = -EBUSY; |
| 254 | } |
| 255 | goto free_hdev; |
| 256 | } |
| 257 | |
| 258 | hdev->id = rc; |
| 259 | |
| 260 | *dev = hdev; |
| 261 | |
| 262 | return 0; |
| 263 | |
| 264 | free_hdev: |
| 265 | kfree(hdev); |
| 266 | return rc; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * destroy_hdev - destroy habanalabs device instance |
| 271 | * |
| 272 | * @dev: pointer to the habanalabs device structure |
| 273 | * |
| 274 | */ |
| 275 | void destroy_hdev(struct hl_device *hdev) |
| 276 | { |
| 277 | /* Remove device from the device list */ |
| 278 | mutex_lock(&hl_devs_idr_lock); |
| 279 | idr_remove(&hl_devs_idr, hdev->id); |
| 280 | mutex_unlock(&hl_devs_idr_lock); |
| 281 | |
| 282 | kfree(hdev); |
| 283 | } |
| 284 | |
| 285 | static int hl_pmops_suspend(struct device *dev) |
| 286 | { |
| 287 | struct pci_dev *pdev = to_pci_dev(dev); |
| 288 | struct hl_device *hdev = pci_get_drvdata(pdev); |
| 289 | |
| 290 | pr_debug("Going to suspend PCI device\n"); |
| 291 | |
| 292 | if (!hdev) { |
| 293 | pr_err("device pointer is NULL in suspend\n"); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | return hl_device_suspend(hdev); |
| 298 | } |
| 299 | |
| 300 | static int hl_pmops_resume(struct device *dev) |
| 301 | { |
| 302 | struct pci_dev *pdev = to_pci_dev(dev); |
| 303 | struct hl_device *hdev = pci_get_drvdata(pdev); |
| 304 | |
| 305 | pr_debug("Going to resume PCI device\n"); |
| 306 | |
| 307 | if (!hdev) { |
| 308 | pr_err("device pointer is NULL in resume\n"); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | return hl_device_resume(hdev); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * hl_pci_probe - probe PCI habanalabs devices |
| 317 | * |
| 318 | * @pdev: pointer to pci device |
| 319 | * @id: pointer to pci device id structure |
| 320 | * |
| 321 | * Standard PCI probe function for habanalabs device. |
| 322 | * Create a new habanalabs device and initialize it according to the |
| 323 | * device's type |
| 324 | */ |
| 325 | static int hl_pci_probe(struct pci_dev *pdev, |
| 326 | const struct pci_device_id *id) |
| 327 | { |
| 328 | struct hl_device *hdev; |
| 329 | int rc; |
| 330 | |
| 331 | dev_info(&pdev->dev, HL_NAME |
| 332 | " device found [%04x:%04x] (rev %x)\n", |
| 333 | (int)pdev->vendor, (int)pdev->device, (int)pdev->revision); |
| 334 | |
| 335 | rc = create_hdev(&hdev, pdev, ASIC_AUTO_DETECT, -1); |
| 336 | if (rc) |
| 337 | return rc; |
| 338 | |
| 339 | pci_set_drvdata(pdev, hdev); |
| 340 | |
| 341 | rc = hl_device_init(hdev, hl_class); |
| 342 | if (rc) { |
| 343 | dev_err(&pdev->dev, "Fatal error during habanalabs device init\n"); |
| 344 | rc = -ENODEV; |
| 345 | goto disable_device; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | |
| 350 | disable_device: |
| 351 | pci_set_drvdata(pdev, NULL); |
| 352 | destroy_hdev(hdev); |
| 353 | |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * hl_pci_remove - remove PCI habanalabs devices |
| 359 | * |
| 360 | * @pdev: pointer to pci device |
| 361 | * |
| 362 | * Standard PCI remove function for habanalabs device |
| 363 | */ |
| 364 | static void hl_pci_remove(struct pci_dev *pdev) |
| 365 | { |
| 366 | struct hl_device *hdev; |
| 367 | |
| 368 | hdev = pci_get_drvdata(pdev); |
| 369 | if (!hdev) |
| 370 | return; |
| 371 | |
| 372 | hl_device_fini(hdev); |
| 373 | pci_set_drvdata(pdev, NULL); |
| 374 | |
| 375 | destroy_hdev(hdev); |
| 376 | } |
| 377 | |
| 378 | static const struct dev_pm_ops hl_pm_ops = { |
| 379 | .suspend = hl_pmops_suspend, |
| 380 | .resume = hl_pmops_resume, |
| 381 | }; |
| 382 | |
| 383 | static struct pci_driver hl_pci_driver = { |
| 384 | .name = HL_NAME, |
| 385 | .id_table = ids, |
| 386 | .probe = hl_pci_probe, |
| 387 | .remove = hl_pci_remove, |
| 388 | .driver.pm = &hl_pm_ops, |
| 389 | }; |
| 390 | |
| 391 | /* |
| 392 | * hl_init - Initialize the habanalabs kernel driver |
| 393 | */ |
| 394 | static int __init hl_init(void) |
| 395 | { |
| 396 | int rc; |
| 397 | dev_t dev; |
| 398 | |
| 399 | pr_info("loading driver\n"); |
| 400 | |
| 401 | rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME); |
| 402 | if (rc < 0) { |
| 403 | pr_err("unable to get major\n"); |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | hl_major = MAJOR(dev); |
| 408 | |
| 409 | hl_class = class_create(THIS_MODULE, HL_NAME); |
| 410 | if (IS_ERR(hl_class)) { |
| 411 | pr_err("failed to allocate class\n"); |
| 412 | rc = PTR_ERR(hl_class); |
| 413 | goto remove_major; |
| 414 | } |
| 415 | |
| 416 | rc = pci_register_driver(&hl_pci_driver); |
| 417 | if (rc) { |
| 418 | pr_err("failed to register pci device\n"); |
| 419 | goto remove_class; |
| 420 | } |
| 421 | |
| 422 | pr_debug("driver loaded\n"); |
| 423 | |
| 424 | return 0; |
| 425 | |
| 426 | remove_class: |
| 427 | class_destroy(hl_class); |
| 428 | remove_major: |
| 429 | unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS); |
| 430 | return rc; |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * hl_exit - Release all resources of the habanalabs kernel driver |
| 435 | */ |
| 436 | static void __exit hl_exit(void) |
| 437 | { |
| 438 | pci_unregister_driver(&hl_pci_driver); |
| 439 | |
| 440 | class_destroy(hl_class); |
| 441 | unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS); |
| 442 | |
| 443 | idr_destroy(&hl_devs_idr); |
| 444 | |
| 445 | pr_debug("driver removed\n"); |
| 446 | } |
| 447 | |
| 448 | module_init(hl_init); |
| 449 | module_exit(hl_exit); |