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 | |
Tomer Tayar | e00dac3 | 2019-04-10 15:18:46 +0300 | [diff] [blame] | 9 | #define pr_fmt(fmt) "habanalabs: " fmt |
| 10 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 11 | #include "habanalabs.h" |
| 12 | |
| 13 | #include <linux/pci.h> |
| 14 | #include <linux/module.h> |
| 15 | |
| 16 | #define HL_DRIVER_AUTHOR "HabanaLabs Kernel Driver Team" |
| 17 | |
| 18 | #define HL_DRIVER_DESC "Driver for HabanaLabs's AI Accelerators" |
| 19 | |
| 20 | MODULE_AUTHOR(HL_DRIVER_AUTHOR); |
| 21 | MODULE_DESCRIPTION(HL_DRIVER_DESC); |
| 22 | MODULE_LICENSE("GPL v2"); |
| 23 | |
| 24 | static int hl_major; |
| 25 | static struct class *hl_class; |
Oded Gabbay | 5e6e023 | 2019-02-27 12:15:16 +0200 | [diff] [blame] | 26 | static DEFINE_IDR(hl_devs_idr); |
| 27 | static DEFINE_MUTEX(hl_devs_idr_lock); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 28 | |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 29 | static int timeout_locked = 5; |
| 30 | static int reset_on_lockup = 1; |
| 31 | |
| 32 | module_param(timeout_locked, int, 0444); |
| 33 | MODULE_PARM_DESC(timeout_locked, |
| 34 | "Device lockup timeout in seconds (0 = disabled, default 5s)"); |
| 35 | |
| 36 | module_param(reset_on_lockup, int, 0444); |
| 37 | MODULE_PARM_DESC(reset_on_lockup, |
| 38 | "Do device reset on lockup (0 = no, 1 = yes, default yes)"); |
| 39 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 40 | #define PCI_VENDOR_ID_HABANALABS 0x1da3 |
| 41 | |
| 42 | #define PCI_IDS_GOYA 0x0001 |
Oded Gabbay | 6966d9e | 2020-03-21 10:58:32 +0200 | [diff] [blame] | 43 | #define PCI_IDS_GAUDI 0x1000 |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 44 | |
| 45 | static const struct pci_device_id ids[] = { |
| 46 | { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), }, |
Oded Gabbay | 6966d9e | 2020-03-21 10:58:32 +0200 | [diff] [blame] | 47 | { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI), }, |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 48 | { 0, } |
| 49 | }; |
Oded Gabbay | 824b457 | 2020-05-03 15:30:55 +0300 | [diff] [blame] | 50 | MODULE_DEVICE_TABLE(pci, ids); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * get_asic_type - translate device id to asic type |
| 54 | * |
| 55 | * @device: id of the PCI device |
| 56 | * |
| 57 | * Translate device id to asic type. |
| 58 | * In case of unidentified device, return -1 |
| 59 | */ |
| 60 | static enum hl_asic_type get_asic_type(u16 device) |
| 61 | { |
| 62 | enum hl_asic_type asic_type; |
| 63 | |
| 64 | switch (device) { |
| 65 | case PCI_IDS_GOYA: |
| 66 | asic_type = ASIC_GOYA; |
| 67 | break; |
Oded Gabbay | 6966d9e | 2020-03-21 10:58:32 +0200 | [diff] [blame] | 68 | case PCI_IDS_GAUDI: |
| 69 | asic_type = ASIC_GAUDI; |
| 70 | break; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 71 | default: |
| 72 | asic_type = ASIC_INVALID; |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | return asic_type; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * hl_device_open - open function for habanalabs device |
| 81 | * |
| 82 | * @inode: pointer to inode structure |
| 83 | * @filp: pointer to file structure |
| 84 | * |
| 85 | * Called when process opens an habanalabs device. |
| 86 | */ |
| 87 | int hl_device_open(struct inode *inode, struct file *filp) |
| 88 | { |
| 89 | struct hl_device *hdev; |
| 90 | struct hl_fpriv *hpriv; |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 91 | int rc; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 92 | |
| 93 | mutex_lock(&hl_devs_idr_lock); |
| 94 | hdev = idr_find(&hl_devs_idr, iminor(inode)); |
| 95 | mutex_unlock(&hl_devs_idr_lock); |
| 96 | |
| 97 | if (!hdev) { |
| 98 | pr_err("Couldn't find device %d:%d\n", |
| 99 | imajor(inode), iminor(inode)); |
| 100 | return -ENXIO; |
| 101 | } |
| 102 | |
| 103 | hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL); |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 104 | if (!hpriv) |
| 105 | return -ENOMEM; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 106 | |
| 107 | hpriv->hdev = hdev; |
| 108 | filp->private_data = hpriv; |
| 109 | hpriv->filp = filp; |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 110 | mutex_init(&hpriv->restore_phase_mutex); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 111 | kref_init(&hpriv->refcount); |
| 112 | nonseekable_open(inode, filp); |
| 113 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 114 | hl_cb_mgr_init(&hpriv->cb_mgr); |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 115 | hl_ctx_mgr_init(&hpriv->ctx_mgr); |
| 116 | |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 117 | hpriv->taskpid = find_get_pid(current->pid); |
| 118 | |
| 119 | mutex_lock(&hdev->fpriv_list_lock); |
| 120 | |
| 121 | if (hl_device_disabled_or_in_reset(hdev)) { |
| 122 | dev_err_ratelimited(hdev->dev, |
| 123 | "Can't open %s because it is disabled or in reset\n", |
| 124 | dev_name(hdev->dev)); |
| 125 | rc = -EPERM; |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 126 | goto out_err; |
| 127 | } |
| 128 | |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 129 | if (hdev->in_debug) { |
| 130 | dev_err_ratelimited(hdev->dev, |
| 131 | "Can't open %s because it is being debugged by another user\n", |
| 132 | dev_name(hdev->dev)); |
| 133 | rc = -EPERM; |
| 134 | goto out_err; |
| 135 | } |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 136 | |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 137 | if (hdev->compute_ctx) { |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 138 | dev_dbg_ratelimited(hdev->dev, |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 139 | "Can't open %s because another user is working on it\n", |
| 140 | dev_name(hdev->dev)); |
| 141 | rc = -EBUSY; |
| 142 | goto out_err; |
| 143 | } |
| 144 | |
| 145 | rc = hl_ctx_create(hdev, hpriv); |
| 146 | if (rc) { |
| 147 | dev_err(hdev->dev, "Failed to create context %d\n", rc); |
| 148 | goto out_err; |
| 149 | } |
| 150 | |
| 151 | /* Device is IDLE at this point so it is legal to change PLLs. |
| 152 | * There is no need to check anything because if the PLL is |
| 153 | * already HIGH, the set function will return without doing |
| 154 | * anything |
Oded Gabbay | d91389b | 2019-02-16 00:39:19 +0200 | [diff] [blame] | 155 | */ |
| 156 | hl_device_set_frequency(hdev, PLL_HIGH); |
| 157 | |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 158 | list_add(&hpriv->dev_node, &hdev->fpriv_list); |
| 159 | mutex_unlock(&hdev->fpriv_list_lock); |
| 160 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 161 | hl_debugfs_add_file(hpriv); |
| 162 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 163 | return 0; |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 164 | |
| 165 | out_err: |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 166 | mutex_unlock(&hdev->fpriv_list_lock); |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 167 | |
Oded Gabbay | eb7caf8 | 2019-07-30 11:56:09 +0300 | [diff] [blame] | 168 | hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr); |
| 169 | hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr); |
| 170 | filp->private_data = NULL; |
| 171 | mutex_destroy(&hpriv->restore_phase_mutex); |
| 172 | put_pid(hpriv->taskpid); |
| 173 | |
| 174 | kfree(hpriv); |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 175 | return rc; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 178 | int hl_device_open_ctrl(struct inode *inode, struct file *filp) |
| 179 | { |
| 180 | struct hl_device *hdev; |
| 181 | struct hl_fpriv *hpriv; |
| 182 | int rc; |
| 183 | |
| 184 | mutex_lock(&hl_devs_idr_lock); |
| 185 | hdev = idr_find(&hl_devs_idr, iminor(inode)); |
| 186 | mutex_unlock(&hl_devs_idr_lock); |
| 187 | |
| 188 | if (!hdev) { |
| 189 | pr_err("Couldn't find device %d:%d\n", |
| 190 | imajor(inode), iminor(inode)); |
| 191 | return -ENXIO; |
| 192 | } |
| 193 | |
| 194 | hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL); |
| 195 | if (!hpriv) |
| 196 | return -ENOMEM; |
| 197 | |
| 198 | mutex_lock(&hdev->fpriv_list_lock); |
| 199 | |
| 200 | if (hl_device_disabled_or_in_reset(hdev)) { |
| 201 | dev_err_ratelimited(hdev->dev_ctrl, |
| 202 | "Can't open %s because it is disabled or in reset\n", |
| 203 | dev_name(hdev->dev_ctrl)); |
| 204 | rc = -EPERM; |
| 205 | goto out_err; |
| 206 | } |
| 207 | |
| 208 | list_add(&hpriv->dev_node, &hdev->fpriv_list); |
| 209 | mutex_unlock(&hdev->fpriv_list_lock); |
| 210 | |
| 211 | hpriv->hdev = hdev; |
| 212 | filp->private_data = hpriv; |
| 213 | hpriv->filp = filp; |
| 214 | hpriv->is_control = true; |
| 215 | nonseekable_open(inode, filp); |
| 216 | |
| 217 | hpriv->taskpid = find_get_pid(current->pid); |
| 218 | |
| 219 | return 0; |
| 220 | |
| 221 | out_err: |
| 222 | mutex_unlock(&hdev->fpriv_list_lock); |
| 223 | kfree(hpriv); |
| 224 | return rc; |
| 225 | } |
| 226 | |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 227 | static void set_driver_behavior_per_device(struct hl_device *hdev) |
| 228 | { |
| 229 | hdev->mmu_enable = 1; |
| 230 | hdev->cpu_enable = 1; |
| 231 | hdev->fw_loading = 1; |
| 232 | hdev->cpu_queues_enable = 1; |
| 233 | hdev->heartbeat = 1; |
Oded Gabbay | ca62433 | 2020-05-09 12:17:21 +0300 | [diff] [blame] | 234 | hdev->clock_gating = 1; |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 235 | |
| 236 | hdev->reset_pcilink = 0; |
| 237 | } |
| 238 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 239 | /* |
| 240 | * create_hdev - create habanalabs device instance |
| 241 | * |
| 242 | * @dev: will hold the pointer to the new habanalabs device structure |
| 243 | * @pdev: pointer to the pci device |
| 244 | * @asic_type: in case of simulator device, which device is it |
| 245 | * @minor: in case of simulator device, the minor of the device |
| 246 | * |
| 247 | * Allocate memory for habanalabs device and initialize basic fields |
| 248 | * Identify the ASIC type |
| 249 | * Allocate ID (minor) for the device (only for real devices) |
| 250 | */ |
| 251 | int create_hdev(struct hl_device **dev, struct pci_dev *pdev, |
| 252 | enum hl_asic_type asic_type, int minor) |
| 253 | { |
| 254 | struct hl_device *hdev; |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 255 | int rc, main_id, ctrl_id = 0; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 256 | |
| 257 | *dev = NULL; |
| 258 | |
| 259 | hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); |
| 260 | if (!hdev) |
| 261 | return -ENOMEM; |
| 262 | |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 263 | /* First, we must find out which ASIC are we handling. This is needed |
| 264 | * to configure the behavior of the driver (kernel parameters) |
| 265 | */ |
Oded Gabbay | 2959384 | 2019-04-04 14:33:34 +0300 | [diff] [blame] | 266 | if (pdev) { |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 267 | hdev->asic_type = get_asic_type(pdev->device); |
| 268 | if (hdev->asic_type == ASIC_INVALID) { |
| 269 | dev_err(&pdev->dev, "Unsupported ASIC\n"); |
| 270 | rc = -ENODEV; |
| 271 | goto free_hdev; |
Oded Gabbay | 6966d9e | 2020-03-21 10:58:32 +0200 | [diff] [blame] | 272 | } else if (hdev->asic_type == ASIC_GAUDI) { |
| 273 | dev_err(&pdev->dev, |
| 274 | "GAUDI is not supported by the current kernel\n"); |
| 275 | rc = -ENODEV; |
| 276 | goto free_hdev; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 277 | } |
| 278 | } else { |
| 279 | hdev->asic_type = asic_type; |
| 280 | } |
| 281 | |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 282 | hdev->major = hl_major; |
| 283 | hdev->reset_on_lockup = reset_on_lockup; |
| 284 | hdev->pldm = 0; |
| 285 | |
| 286 | set_driver_behavior_per_device(hdev); |
| 287 | |
| 288 | if (timeout_locked) |
| 289 | hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000); |
| 290 | else |
| 291 | hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT; |
| 292 | |
| 293 | hdev->disabled = true; |
| 294 | hdev->pdev = pdev; /* can be NULL in case of simulator device */ |
| 295 | |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 296 | /* Set default DMA mask to 32 bits */ |
| 297 | hdev->dma_mask = 32; |
| 298 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 299 | mutex_lock(&hl_devs_idr_lock); |
| 300 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 301 | /* Always save 2 numbers, 1 for main device and 1 for control. |
| 302 | * They must be consecutive |
| 303 | */ |
| 304 | main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS, |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 305 | GFP_KERNEL); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 306 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 307 | if (main_id >= 0) |
| 308 | ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1, |
| 309 | main_id + 2, GFP_KERNEL); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 310 | |
| 311 | mutex_unlock(&hl_devs_idr_lock); |
| 312 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 313 | if ((main_id < 0) || (ctrl_id < 0)) { |
| 314 | if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC)) |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 315 | pr_err("too many devices in the system\n"); |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 316 | |
| 317 | if (main_id >= 0) { |
| 318 | mutex_lock(&hl_devs_idr_lock); |
| 319 | idr_remove(&hl_devs_idr, main_id); |
| 320 | mutex_unlock(&hl_devs_idr_lock); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 321 | } |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 322 | |
| 323 | rc = -EBUSY; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 324 | goto free_hdev; |
| 325 | } |
| 326 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 327 | hdev->id = main_id; |
| 328 | hdev->id_control = ctrl_id; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 329 | |
| 330 | *dev = hdev; |
| 331 | |
| 332 | return 0; |
| 333 | |
| 334 | free_hdev: |
| 335 | kfree(hdev); |
| 336 | return rc; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * destroy_hdev - destroy habanalabs device instance |
| 341 | * |
| 342 | * @dev: pointer to the habanalabs device structure |
| 343 | * |
| 344 | */ |
| 345 | void destroy_hdev(struct hl_device *hdev) |
| 346 | { |
| 347 | /* Remove device from the device list */ |
| 348 | mutex_lock(&hl_devs_idr_lock); |
| 349 | idr_remove(&hl_devs_idr, hdev->id); |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 350 | idr_remove(&hl_devs_idr, hdev->id_control); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 351 | mutex_unlock(&hl_devs_idr_lock); |
| 352 | |
| 353 | kfree(hdev); |
| 354 | } |
| 355 | |
| 356 | static int hl_pmops_suspend(struct device *dev) |
| 357 | { |
Chuhong Yuan | 30f2732 | 2019-07-23 20:46:08 +0800 | [diff] [blame] | 358 | struct hl_device *hdev = dev_get_drvdata(dev); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 359 | |
| 360 | pr_debug("Going to suspend PCI device\n"); |
| 361 | |
| 362 | if (!hdev) { |
| 363 | pr_err("device pointer is NULL in suspend\n"); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | return hl_device_suspend(hdev); |
| 368 | } |
| 369 | |
| 370 | static int hl_pmops_resume(struct device *dev) |
| 371 | { |
Chuhong Yuan | 30f2732 | 2019-07-23 20:46:08 +0800 | [diff] [blame] | 372 | struct hl_device *hdev = dev_get_drvdata(dev); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 373 | |
| 374 | pr_debug("Going to resume PCI device\n"); |
| 375 | |
| 376 | if (!hdev) { |
| 377 | pr_err("device pointer is NULL in resume\n"); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | return hl_device_resume(hdev); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * hl_pci_probe - probe PCI habanalabs devices |
| 386 | * |
| 387 | * @pdev: pointer to pci device |
| 388 | * @id: pointer to pci device id structure |
| 389 | * |
| 390 | * Standard PCI probe function for habanalabs device. |
| 391 | * Create a new habanalabs device and initialize it according to the |
| 392 | * device's type |
| 393 | */ |
| 394 | static int hl_pci_probe(struct pci_dev *pdev, |
| 395 | const struct pci_device_id *id) |
| 396 | { |
| 397 | struct hl_device *hdev; |
| 398 | int rc; |
| 399 | |
| 400 | dev_info(&pdev->dev, HL_NAME |
| 401 | " device found [%04x:%04x] (rev %x)\n", |
| 402 | (int)pdev->vendor, (int)pdev->device, (int)pdev->revision); |
| 403 | |
Oded Gabbay | 2959384 | 2019-04-04 14:33:34 +0300 | [diff] [blame] | 404 | rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 405 | if (rc) |
| 406 | return rc; |
| 407 | |
| 408 | pci_set_drvdata(pdev, hdev); |
| 409 | |
| 410 | rc = hl_device_init(hdev, hl_class); |
| 411 | if (rc) { |
| 412 | dev_err(&pdev->dev, "Fatal error during habanalabs device init\n"); |
| 413 | rc = -ENODEV; |
| 414 | goto disable_device; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | |
| 419 | disable_device: |
| 420 | pci_set_drvdata(pdev, NULL); |
| 421 | destroy_hdev(hdev); |
| 422 | |
| 423 | return rc; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * hl_pci_remove - remove PCI habanalabs devices |
| 428 | * |
| 429 | * @pdev: pointer to pci device |
| 430 | * |
| 431 | * Standard PCI remove function for habanalabs device |
| 432 | */ |
| 433 | static void hl_pci_remove(struct pci_dev *pdev) |
| 434 | { |
| 435 | struct hl_device *hdev; |
| 436 | |
| 437 | hdev = pci_get_drvdata(pdev); |
| 438 | if (!hdev) |
| 439 | return; |
| 440 | |
| 441 | hl_device_fini(hdev); |
| 442 | pci_set_drvdata(pdev, NULL); |
| 443 | |
| 444 | destroy_hdev(hdev); |
| 445 | } |
| 446 | |
| 447 | static const struct dev_pm_ops hl_pm_ops = { |
| 448 | .suspend = hl_pmops_suspend, |
| 449 | .resume = hl_pmops_resume, |
| 450 | }; |
| 451 | |
| 452 | static struct pci_driver hl_pci_driver = { |
| 453 | .name = HL_NAME, |
| 454 | .id_table = ids, |
| 455 | .probe = hl_pci_probe, |
| 456 | .remove = hl_pci_remove, |
| 457 | .driver.pm = &hl_pm_ops, |
| 458 | }; |
| 459 | |
| 460 | /* |
| 461 | * hl_init - Initialize the habanalabs kernel driver |
| 462 | */ |
| 463 | static int __init hl_init(void) |
| 464 | { |
| 465 | int rc; |
| 466 | dev_t dev; |
| 467 | |
| 468 | pr_info("loading driver\n"); |
| 469 | |
| 470 | rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME); |
| 471 | if (rc < 0) { |
| 472 | pr_err("unable to get major\n"); |
| 473 | return rc; |
| 474 | } |
| 475 | |
| 476 | hl_major = MAJOR(dev); |
| 477 | |
| 478 | hl_class = class_create(THIS_MODULE, HL_NAME); |
| 479 | if (IS_ERR(hl_class)) { |
| 480 | pr_err("failed to allocate class\n"); |
| 481 | rc = PTR_ERR(hl_class); |
| 482 | goto remove_major; |
| 483 | } |
| 484 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 485 | hl_debugfs_init(); |
| 486 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 487 | rc = pci_register_driver(&hl_pci_driver); |
| 488 | if (rc) { |
| 489 | pr_err("failed to register pci device\n"); |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 490 | goto remove_debugfs; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | pr_debug("driver loaded\n"); |
| 494 | |
| 495 | return 0; |
| 496 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 497 | remove_debugfs: |
| 498 | hl_debugfs_fini(); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 499 | class_destroy(hl_class); |
| 500 | remove_major: |
| 501 | unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS); |
| 502 | return rc; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * hl_exit - Release all resources of the habanalabs kernel driver |
| 507 | */ |
| 508 | static void __exit hl_exit(void) |
| 509 | { |
| 510 | pci_unregister_driver(&hl_pci_driver); |
| 511 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 512 | /* |
| 513 | * Removing debugfs must be after all devices or simulator devices |
| 514 | * have been removed because otherwise we get a bug in the |
| 515 | * debugfs module for referencing NULL objects |
| 516 | */ |
| 517 | hl_debugfs_fini(); |
| 518 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 519 | class_destroy(hl_class); |
| 520 | unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS); |
| 521 | |
| 522 | idr_destroy(&hl_devs_idr); |
| 523 | |
| 524 | pr_debug("driver removed\n"); |
| 525 | } |
| 526 | |
| 527 | module_init(hl_init); |
| 528 | module_exit(hl_exit); |