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 | ac0ae6a | 2020-05-11 10:29:27 +0300 | [diff] [blame^] | 175 | |
Oded Gabbay | 0861e41 | 2019-02-16 00:39:14 +0200 | [diff] [blame] | 176 | return rc; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 179 | int hl_device_open_ctrl(struct inode *inode, struct file *filp) |
| 180 | { |
| 181 | struct hl_device *hdev; |
| 182 | struct hl_fpriv *hpriv; |
| 183 | int rc; |
| 184 | |
| 185 | mutex_lock(&hl_devs_idr_lock); |
| 186 | hdev = idr_find(&hl_devs_idr, iminor(inode)); |
| 187 | mutex_unlock(&hl_devs_idr_lock); |
| 188 | |
| 189 | if (!hdev) { |
| 190 | pr_err("Couldn't find device %d:%d\n", |
| 191 | imajor(inode), iminor(inode)); |
| 192 | return -ENXIO; |
| 193 | } |
| 194 | |
| 195 | hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL); |
| 196 | if (!hpriv) |
| 197 | return -ENOMEM; |
| 198 | |
| 199 | mutex_lock(&hdev->fpriv_list_lock); |
| 200 | |
| 201 | if (hl_device_disabled_or_in_reset(hdev)) { |
| 202 | dev_err_ratelimited(hdev->dev_ctrl, |
| 203 | "Can't open %s because it is disabled or in reset\n", |
| 204 | dev_name(hdev->dev_ctrl)); |
| 205 | rc = -EPERM; |
| 206 | goto out_err; |
| 207 | } |
| 208 | |
| 209 | list_add(&hpriv->dev_node, &hdev->fpriv_list); |
| 210 | mutex_unlock(&hdev->fpriv_list_lock); |
| 211 | |
| 212 | hpriv->hdev = hdev; |
| 213 | filp->private_data = hpriv; |
| 214 | hpriv->filp = filp; |
| 215 | hpriv->is_control = true; |
| 216 | nonseekable_open(inode, filp); |
| 217 | |
| 218 | hpriv->taskpid = find_get_pid(current->pid); |
| 219 | |
| 220 | return 0; |
| 221 | |
| 222 | out_err: |
| 223 | mutex_unlock(&hdev->fpriv_list_lock); |
| 224 | kfree(hpriv); |
| 225 | return rc; |
| 226 | } |
| 227 | |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 228 | static void set_driver_behavior_per_device(struct hl_device *hdev) |
| 229 | { |
| 230 | hdev->mmu_enable = 1; |
| 231 | hdev->cpu_enable = 1; |
| 232 | hdev->fw_loading = 1; |
| 233 | hdev->cpu_queues_enable = 1; |
| 234 | hdev->heartbeat = 1; |
Oded Gabbay | ca62433 | 2020-05-09 12:17:21 +0300 | [diff] [blame] | 235 | hdev->clock_gating = 1; |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 236 | |
| 237 | hdev->reset_pcilink = 0; |
Oded Gabbay | ac0ae6a | 2020-05-11 10:29:27 +0300 | [diff] [blame^] | 238 | hdev->axi_drain = 0; |
| 239 | hdev->sram_scrambler_enable = 1; |
| 240 | hdev->dram_scrambler_enable = 1; |
| 241 | hdev->rl_enable = 1; |
| 242 | hdev->bmc_enable = 1; |
| 243 | hdev->hard_reset_on_fw_events = 1; |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 244 | } |
| 245 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 246 | /* |
| 247 | * create_hdev - create habanalabs device instance |
| 248 | * |
| 249 | * @dev: will hold the pointer to the new habanalabs device structure |
| 250 | * @pdev: pointer to the pci device |
| 251 | * @asic_type: in case of simulator device, which device is it |
| 252 | * @minor: in case of simulator device, the minor of the device |
| 253 | * |
| 254 | * Allocate memory for habanalabs device and initialize basic fields |
| 255 | * Identify the ASIC type |
| 256 | * Allocate ID (minor) for the device (only for real devices) |
| 257 | */ |
| 258 | int create_hdev(struct hl_device **dev, struct pci_dev *pdev, |
| 259 | enum hl_asic_type asic_type, int minor) |
| 260 | { |
| 261 | struct hl_device *hdev; |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 262 | int rc, main_id, ctrl_id = 0; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 263 | |
| 264 | *dev = NULL; |
| 265 | |
| 266 | hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); |
| 267 | if (!hdev) |
| 268 | return -ENOMEM; |
| 269 | |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 270 | /* First, we must find out which ASIC are we handling. This is needed |
| 271 | * to configure the behavior of the driver (kernel parameters) |
| 272 | */ |
Oded Gabbay | 2959384 | 2019-04-04 14:33:34 +0300 | [diff] [blame] | 273 | if (pdev) { |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 274 | hdev->asic_type = get_asic_type(pdev->device); |
| 275 | if (hdev->asic_type == ASIC_INVALID) { |
| 276 | dev_err(&pdev->dev, "Unsupported ASIC\n"); |
| 277 | rc = -ENODEV; |
| 278 | goto free_hdev; |
Oded Gabbay | 6966d9e | 2020-03-21 10:58:32 +0200 | [diff] [blame] | 279 | } else if (hdev->asic_type == ASIC_GAUDI) { |
| 280 | dev_err(&pdev->dev, |
| 281 | "GAUDI is not supported by the current kernel\n"); |
| 282 | rc = -ENODEV; |
| 283 | goto free_hdev; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 284 | } |
| 285 | } else { |
| 286 | hdev->asic_type = asic_type; |
| 287 | } |
| 288 | |
Oded Gabbay | 8c173dc | 2019-05-08 09:55:23 +0300 | [diff] [blame] | 289 | hdev->major = hl_major; |
| 290 | hdev->reset_on_lockup = reset_on_lockup; |
| 291 | hdev->pldm = 0; |
| 292 | |
| 293 | set_driver_behavior_per_device(hdev); |
| 294 | |
| 295 | if (timeout_locked) |
| 296 | hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000); |
| 297 | else |
| 298 | hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT; |
| 299 | |
| 300 | hdev->disabled = true; |
| 301 | hdev->pdev = pdev; /* can be NULL in case of simulator device */ |
| 302 | |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 303 | /* Set default DMA mask to 32 bits */ |
| 304 | hdev->dma_mask = 32; |
| 305 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 306 | mutex_lock(&hl_devs_idr_lock); |
| 307 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 308 | /* Always save 2 numbers, 1 for main device and 1 for control. |
| 309 | * They must be consecutive |
| 310 | */ |
| 311 | main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS, |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 312 | GFP_KERNEL); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 313 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 314 | if (main_id >= 0) |
| 315 | ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1, |
| 316 | main_id + 2, GFP_KERNEL); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 317 | |
| 318 | mutex_unlock(&hl_devs_idr_lock); |
| 319 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 320 | if ((main_id < 0) || (ctrl_id < 0)) { |
| 321 | if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC)) |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 322 | pr_err("too many devices in the system\n"); |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 323 | |
| 324 | if (main_id >= 0) { |
| 325 | mutex_lock(&hl_devs_idr_lock); |
| 326 | idr_remove(&hl_devs_idr, main_id); |
| 327 | mutex_unlock(&hl_devs_idr_lock); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 328 | } |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 329 | |
| 330 | rc = -EBUSY; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 331 | goto free_hdev; |
| 332 | } |
| 333 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 334 | hdev->id = main_id; |
| 335 | hdev->id_control = ctrl_id; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 336 | |
| 337 | *dev = hdev; |
| 338 | |
| 339 | return 0; |
| 340 | |
| 341 | free_hdev: |
| 342 | kfree(hdev); |
| 343 | return rc; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * destroy_hdev - destroy habanalabs device instance |
| 348 | * |
| 349 | * @dev: pointer to the habanalabs device structure |
| 350 | * |
| 351 | */ |
| 352 | void destroy_hdev(struct hl_device *hdev) |
| 353 | { |
| 354 | /* Remove device from the device list */ |
| 355 | mutex_lock(&hl_devs_idr_lock); |
| 356 | idr_remove(&hl_devs_idr, hdev->id); |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 357 | idr_remove(&hl_devs_idr, hdev->id_control); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 358 | mutex_unlock(&hl_devs_idr_lock); |
| 359 | |
| 360 | kfree(hdev); |
| 361 | } |
| 362 | |
| 363 | static int hl_pmops_suspend(struct device *dev) |
| 364 | { |
Chuhong Yuan | 30f2732 | 2019-07-23 20:46:08 +0800 | [diff] [blame] | 365 | struct hl_device *hdev = dev_get_drvdata(dev); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 366 | |
| 367 | pr_debug("Going to suspend PCI device\n"); |
| 368 | |
| 369 | if (!hdev) { |
| 370 | pr_err("device pointer is NULL in suspend\n"); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | return hl_device_suspend(hdev); |
| 375 | } |
| 376 | |
| 377 | static int hl_pmops_resume(struct device *dev) |
| 378 | { |
Chuhong Yuan | 30f2732 | 2019-07-23 20:46:08 +0800 | [diff] [blame] | 379 | struct hl_device *hdev = dev_get_drvdata(dev); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 380 | |
| 381 | pr_debug("Going to resume PCI device\n"); |
| 382 | |
| 383 | if (!hdev) { |
| 384 | pr_err("device pointer is NULL in resume\n"); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | return hl_device_resume(hdev); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * hl_pci_probe - probe PCI habanalabs devices |
| 393 | * |
| 394 | * @pdev: pointer to pci device |
| 395 | * @id: pointer to pci device id structure |
| 396 | * |
| 397 | * Standard PCI probe function for habanalabs device. |
| 398 | * Create a new habanalabs device and initialize it according to the |
| 399 | * device's type |
| 400 | */ |
| 401 | static int hl_pci_probe(struct pci_dev *pdev, |
| 402 | const struct pci_device_id *id) |
| 403 | { |
| 404 | struct hl_device *hdev; |
| 405 | int rc; |
| 406 | |
| 407 | dev_info(&pdev->dev, HL_NAME |
| 408 | " device found [%04x:%04x] (rev %x)\n", |
| 409 | (int)pdev->vendor, (int)pdev->device, (int)pdev->revision); |
| 410 | |
Oded Gabbay | 2959384 | 2019-04-04 14:33:34 +0300 | [diff] [blame] | 411 | rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 412 | if (rc) |
| 413 | return rc; |
| 414 | |
| 415 | pci_set_drvdata(pdev, hdev); |
| 416 | |
| 417 | rc = hl_device_init(hdev, hl_class); |
| 418 | if (rc) { |
| 419 | dev_err(&pdev->dev, "Fatal error during habanalabs device init\n"); |
| 420 | rc = -ENODEV; |
| 421 | goto disable_device; |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | |
| 426 | disable_device: |
| 427 | pci_set_drvdata(pdev, NULL); |
| 428 | destroy_hdev(hdev); |
| 429 | |
| 430 | return rc; |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * hl_pci_remove - remove PCI habanalabs devices |
| 435 | * |
| 436 | * @pdev: pointer to pci device |
| 437 | * |
| 438 | * Standard PCI remove function for habanalabs device |
| 439 | */ |
| 440 | static void hl_pci_remove(struct pci_dev *pdev) |
| 441 | { |
| 442 | struct hl_device *hdev; |
| 443 | |
| 444 | hdev = pci_get_drvdata(pdev); |
| 445 | if (!hdev) |
| 446 | return; |
| 447 | |
| 448 | hl_device_fini(hdev); |
| 449 | pci_set_drvdata(pdev, NULL); |
| 450 | |
| 451 | destroy_hdev(hdev); |
| 452 | } |
| 453 | |
| 454 | static const struct dev_pm_ops hl_pm_ops = { |
| 455 | .suspend = hl_pmops_suspend, |
| 456 | .resume = hl_pmops_resume, |
| 457 | }; |
| 458 | |
| 459 | static struct pci_driver hl_pci_driver = { |
| 460 | .name = HL_NAME, |
| 461 | .id_table = ids, |
| 462 | .probe = hl_pci_probe, |
| 463 | .remove = hl_pci_remove, |
| 464 | .driver.pm = &hl_pm_ops, |
| 465 | }; |
| 466 | |
| 467 | /* |
| 468 | * hl_init - Initialize the habanalabs kernel driver |
| 469 | */ |
| 470 | static int __init hl_init(void) |
| 471 | { |
| 472 | int rc; |
| 473 | dev_t dev; |
| 474 | |
| 475 | pr_info("loading driver\n"); |
| 476 | |
| 477 | rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME); |
| 478 | if (rc < 0) { |
| 479 | pr_err("unable to get major\n"); |
| 480 | return rc; |
| 481 | } |
| 482 | |
| 483 | hl_major = MAJOR(dev); |
| 484 | |
| 485 | hl_class = class_create(THIS_MODULE, HL_NAME); |
| 486 | if (IS_ERR(hl_class)) { |
| 487 | pr_err("failed to allocate class\n"); |
| 488 | rc = PTR_ERR(hl_class); |
| 489 | goto remove_major; |
| 490 | } |
| 491 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 492 | hl_debugfs_init(); |
| 493 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 494 | rc = pci_register_driver(&hl_pci_driver); |
| 495 | if (rc) { |
| 496 | pr_err("failed to register pci device\n"); |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 497 | goto remove_debugfs; |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | pr_debug("driver loaded\n"); |
| 501 | |
| 502 | return 0; |
| 503 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 504 | remove_debugfs: |
| 505 | hl_debugfs_fini(); |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 506 | class_destroy(hl_class); |
| 507 | remove_major: |
| 508 | unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS); |
| 509 | return rc; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * hl_exit - Release all resources of the habanalabs kernel driver |
| 514 | */ |
| 515 | static void __exit hl_exit(void) |
| 516 | { |
| 517 | pci_unregister_driver(&hl_pci_driver); |
| 518 | |
Oded Gabbay | c216477 | 2019-02-16 00:39:24 +0200 | [diff] [blame] | 519 | /* |
| 520 | * Removing debugfs must be after all devices or simulator devices |
| 521 | * have been removed because otherwise we get a bug in the |
| 522 | * debugfs module for referencing NULL objects |
| 523 | */ |
| 524 | hl_debugfs_fini(); |
| 525 | |
Oded Gabbay | c4d6634 | 2019-02-16 00:39:11 +0200 | [diff] [blame] | 526 | class_destroy(hl_class); |
| 527 | unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS); |
| 528 | |
| 529 | idr_destroy(&hl_devs_idr); |
| 530 | |
| 531 | pr_debug("driver removed\n"); |
| 532 | } |
| 533 | |
| 534 | module_init(hl_init); |
| 535 | module_exit(hl_exit); |