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