blob: a75e4fceb9d85156fbf42ac04e97c5f2e550f775 [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
Tomer Tayare00dac32019-04-10 15:18:46 +03009#define pr_fmt(fmt) "habanalabs: " fmt
10
Oded Gabbayc4d66342019-02-16 00:39:11 +020011#include "habanalabs.h"
12
13#include <linux/pci.h>
Ofir Bitton2e5eda42020-05-20 16:35:08 +030014#include <linux/aer.h>
Oded Gabbayc4d66342019-02-16 00:39:11 +020015#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
21MODULE_AUTHOR(HL_DRIVER_AUTHOR);
22MODULE_DESCRIPTION(HL_DRIVER_DESC);
23MODULE_LICENSE("GPL v2");
24
25static int hl_major;
26static struct class *hl_class;
Oded Gabbay5e6e0232019-02-27 12:15:16 +020027static DEFINE_IDR(hl_devs_idr);
28static DEFINE_MUTEX(hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +020029
Oded Gabbay17b59dd2021-02-17 13:34:44 +020030static int timeout_locked = 30;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020031static int reset_on_lockup = 1;
Oded Gabbay7fb2a1f2021-05-12 20:46:12 +030032static int memory_scrub;
Oded Gabbay27a9e352021-04-12 09:52:05 +030033static ulong boot_error_status_mask = ULONG_MAX;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020034
35module_param(timeout_locked, int, 0444);
36MODULE_PARM_DESC(timeout_locked,
Oded Gabbay17b59dd2021-02-17 13:34:44 +020037 "Device lockup timeout in seconds (0 = disabled, default 30s)");
Oded Gabbayeff6f4a2019-02-16 00:39:21 +020038
39module_param(reset_on_lockup, int, 0444);
40MODULE_PARM_DESC(reset_on_lockup,
41 "Do device reset on lockup (0 = no, 1 = yes, default yes)");
42
farah kassabri03df1362020-05-06 11:17:38 +030043module_param(memory_scrub, int, 0444);
44MODULE_PARM_DESC(memory_scrub,
Oded Gabbay7fb2a1f2021-05-12 20:46:12 +030045 "Scrub device memory in various states (0 = no, 1 = yes, default no)");
farah kassabri03df1362020-05-06 11:17:38 +030046
Oded Gabbay27a9e352021-04-12 09:52:05 +030047module_param(boot_error_status_mask, ulong, 0444);
48MODULE_PARM_DESC(boot_error_status_mask,
49 "Mask of the error status during device CPU boot (If bitX is cleared then error X is masked. Default all 1's)");
50
Oded Gabbayc4d66342019-02-16 00:39:11 +020051#define PCI_VENDOR_ID_HABANALABS 0x1da3
52
53#define PCI_IDS_GOYA 0x0001
Oded Gabbay6966d9e2020-03-21 10:58:32 +020054#define PCI_IDS_GAUDI 0x1000
Ofir Bittone5042a62021-04-01 13:43:40 +030055#define PCI_IDS_GAUDI_SEC 0x1010
Oded Gabbayc4d66342019-02-16 00:39:11 +020056
57static const struct pci_device_id ids[] = {
58 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },
Oded Gabbay6966d9e2020-03-21 10:58:32 +020059 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI), },
Ofir Bittone5042a62021-04-01 13:43:40 +030060 { PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI_SEC), },
Oded Gabbayc4d66342019-02-16 00:39:11 +020061 { 0, }
62};
Oded Gabbay824b4572020-05-03 15:30:55 +030063MODULE_DEVICE_TABLE(pci, ids);
Oded Gabbayc4d66342019-02-16 00:39:11 +020064
65/*
66 * get_asic_type - translate device id to asic type
67 *
68 * @device: id of the PCI device
69 *
70 * Translate device id to asic type.
71 * In case of unidentified device, return -1
72 */
73static enum hl_asic_type get_asic_type(u16 device)
74{
75 enum hl_asic_type asic_type;
76
77 switch (device) {
78 case PCI_IDS_GOYA:
79 asic_type = ASIC_GOYA;
80 break;
Oded Gabbay6966d9e2020-03-21 10:58:32 +020081 case PCI_IDS_GAUDI:
82 asic_type = ASIC_GAUDI;
83 break;
Ofir Bittone5042a62021-04-01 13:43:40 +030084 case PCI_IDS_GAUDI_SEC:
85 asic_type = ASIC_GAUDI_SEC;
86 break;
Oded Gabbayc4d66342019-02-16 00:39:11 +020087 default:
88 asic_type = ASIC_INVALID;
89 break;
90 }
91
92 return asic_type;
93}
94
Ofir Bittone5042a62021-04-01 13:43:40 +030095static bool is_asic_secured(enum hl_asic_type asic_type)
96{
97 switch (asic_type) {
98 case ASIC_GAUDI_SEC:
99 return true;
100 default:
101 return false;
102 }
103}
104
Oded Gabbayc4d66342019-02-16 00:39:11 +0200105/*
106 * hl_device_open - open function for habanalabs device
107 *
108 * @inode: pointer to inode structure
109 * @filp: pointer to file structure
110 *
111 * Called when process opens an habanalabs device.
112 */
113int hl_device_open(struct inode *inode, struct file *filp)
114{
Ofir Bitton66a76402020-10-05 14:40:10 +0300115 enum hl_device_status status;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200116 struct hl_device *hdev;
117 struct hl_fpriv *hpriv;
Oded Gabbay0861e412019-02-16 00:39:14 +0200118 int rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200119
120 mutex_lock(&hl_devs_idr_lock);
121 hdev = idr_find(&hl_devs_idr, iminor(inode));
122 mutex_unlock(&hl_devs_idr_lock);
123
124 if (!hdev) {
125 pr_err("Couldn't find device %d:%d\n",
126 imajor(inode), iminor(inode));
127 return -ENXIO;
128 }
129
130 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300131 if (!hpriv)
132 return -ENOMEM;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200133
134 hpriv->hdev = hdev;
135 filp->private_data = hpriv;
136 hpriv->filp = filp;
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200137 mutex_init(&hpriv->restore_phase_mutex);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200138 kref_init(&hpriv->refcount);
139 nonseekable_open(inode, filp);
140
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200141 hl_cb_mgr_init(&hpriv->cb_mgr);
Oded Gabbay0861e412019-02-16 00:39:14 +0200142 hl_ctx_mgr_init(&hpriv->ctx_mgr);
143
Oded Gabbaye79e7452021-07-03 11:50:32 +0300144 hpriv->taskpid = get_task_pid(current, PIDTYPE_PID);
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300145
146 mutex_lock(&hdev->fpriv_list_lock);
147
Ofir Bitton66a76402020-10-05 14:40:10 +0300148 if (!hl_device_operational(hdev, &status)) {
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300149 dev_err_ratelimited(hdev->dev,
Ofir Bitton66a76402020-10-05 14:40:10 +0300150 "Can't open %s because it is %s\n",
151 dev_name(hdev->dev), hdev->status[status]);
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300152 rc = -EPERM;
Oded Gabbay0861e412019-02-16 00:39:14 +0200153 goto out_err;
154 }
155
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300156 if (hdev->in_debug) {
157 dev_err_ratelimited(hdev->dev,
158 "Can't open %s because it is being debugged by another user\n",
159 dev_name(hdev->dev));
160 rc = -EPERM;
161 goto out_err;
162 }
Oded Gabbayc4d66342019-02-16 00:39:11 +0200163
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300164 if (hdev->compute_ctx) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300165 dev_dbg_ratelimited(hdev->dev,
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300166 "Can't open %s because another user is working on it\n",
167 dev_name(hdev->dev));
168 rc = -EBUSY;
169 goto out_err;
170 }
171
172 rc = hl_ctx_create(hdev, hpriv);
173 if (rc) {
174 dev_err(hdev->dev, "Failed to create context %d\n", rc);
175 goto out_err;
176 }
177
178 /* Device is IDLE at this point so it is legal to change PLLs.
179 * There is no need to check anything because if the PLL is
180 * already HIGH, the set function will return without doing
181 * anything
Oded Gabbayd91389b2019-02-16 00:39:19 +0200182 */
183 hl_device_set_frequency(hdev, PLL_HIGH);
184
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300185 list_add(&hpriv->dev_node, &hdev->fpriv_list);
186 mutex_unlock(&hdev->fpriv_list_lock);
187
Oded Gabbayc2164772019-02-16 00:39:24 +0200188 hl_debugfs_add_file(hpriv);
189
Yuri Nudelmane307b302021-05-24 11:25:21 +0300190 hdev->open_counter++;
191 hdev->last_successful_open_jif = jiffies;
192
Oded Gabbayc4d66342019-02-16 00:39:11 +0200193 return 0;
Oded Gabbay0861e412019-02-16 00:39:14 +0200194
195out_err:
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300196 mutex_unlock(&hdev->fpriv_list_lock);
Oded Gabbayeb7caf82019-07-30 11:56:09 +0300197 hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
198 hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
199 filp->private_data = NULL;
200 mutex_destroy(&hpriv->restore_phase_mutex);
201 put_pid(hpriv->taskpid);
202
203 kfree(hpriv);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300204
Oded Gabbay0861e412019-02-16 00:39:14 +0200205 return rc;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200206}
207
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300208int hl_device_open_ctrl(struct inode *inode, struct file *filp)
209{
210 struct hl_device *hdev;
211 struct hl_fpriv *hpriv;
212 int rc;
213
214 mutex_lock(&hl_devs_idr_lock);
215 hdev = idr_find(&hl_devs_idr, iminor(inode));
216 mutex_unlock(&hl_devs_idr_lock);
217
218 if (!hdev) {
219 pr_err("Couldn't find device %d:%d\n",
220 imajor(inode), iminor(inode));
221 return -ENXIO;
222 }
223
224 hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);
225 if (!hpriv)
226 return -ENOMEM;
227
228 mutex_lock(&hdev->fpriv_list_lock);
229
Ofir Bitton66a76402020-10-05 14:40:10 +0300230 if (!hl_device_operational(hdev, NULL)) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300231 dev_err_ratelimited(hdev->dev_ctrl,
232 "Can't open %s because it is disabled or in reset\n",
233 dev_name(hdev->dev_ctrl));
234 rc = -EPERM;
235 goto out_err;
236 }
237
238 list_add(&hpriv->dev_node, &hdev->fpriv_list);
239 mutex_unlock(&hdev->fpriv_list_lock);
240
241 hpriv->hdev = hdev;
242 filp->private_data = hpriv;
243 hpriv->filp = filp;
244 hpriv->is_control = true;
245 nonseekable_open(inode, filp);
246
247 hpriv->taskpid = find_get_pid(current->pid);
248
249 return 0;
250
251out_err:
252 mutex_unlock(&hdev->fpriv_list_lock);
253 kfree(hpriv);
254 return rc;
255}
256
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300257static void set_driver_behavior_per_device(struct hl_device *hdev)
258{
Ofir Bitton6a2f5d72021-02-15 13:23:04 +0200259 hdev->fw_components = FW_TYPE_ALL_TYPES;
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300260 hdev->cpu_queues_enable = 1;
261 hdev->heartbeat = 1;
Ofir Bittond1ddd902020-10-19 17:04:20 +0300262 hdev->mmu_enable = 1;
Oded Gabbaye38bfd32020-07-03 20:46:12 +0300263 hdev->clock_gating_mask = ULONG_MAX;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300264 hdev->sram_scrambler_enable = 1;
265 hdev->dram_scrambler_enable = 1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300266 hdev->bmc_enable = 1;
267 hdev->hard_reset_on_fw_events = 1;
Ofir Bittond1ddd902020-10-19 17:04:20 +0300268 hdev->reset_on_preboot_fail = 1;
Ofir Bitton84586de2021-05-20 13:30:31 +0300269 hdev->reset_if_device_not_idle = 1;
Ofir Bittond1ddd902020-10-19 17:04:20 +0300270
271 hdev->reset_pcilink = 0;
272 hdev->axi_drain = 0;
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300273}
274
Oded Gabbayc4d66342019-02-16 00:39:11 +0200275/*
276 * create_hdev - create habanalabs device instance
277 *
278 * @dev: will hold the pointer to the new habanalabs device structure
279 * @pdev: pointer to the pci device
280 * @asic_type: in case of simulator device, which device is it
281 * @minor: in case of simulator device, the minor of the device
282 *
283 * Allocate memory for habanalabs device and initialize basic fields
284 * Identify the ASIC type
285 * Allocate ID (minor) for the device (only for real devices)
286 */
287int create_hdev(struct hl_device **dev, struct pci_dev *pdev,
288 enum hl_asic_type asic_type, int minor)
289{
290 struct hl_device *hdev;
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300291 int rc, main_id, ctrl_id = 0;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200292
293 *dev = NULL;
294
295 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
296 if (!hdev)
297 return -ENOMEM;
298
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300299 /* First, we must find out which ASIC are we handling. This is needed
300 * to configure the behavior of the driver (kernel parameters)
301 */
Oded Gabbay29593842019-04-04 14:33:34 +0300302 if (pdev) {
Oded Gabbayc4d66342019-02-16 00:39:11 +0200303 hdev->asic_type = get_asic_type(pdev->device);
304 if (hdev->asic_type == ASIC_INVALID) {
305 dev_err(&pdev->dev, "Unsupported ASIC\n");
306 rc = -ENODEV;
307 goto free_hdev;
308 }
309 } else {
310 hdev->asic_type = asic_type;
311 }
312
Ofir Bittone5042a62021-04-01 13:43:40 +0300313 if (pdev)
Ohad Sharabi4cb45082021-05-20 09:09:03 +0300314 hdev->asic_prop.fw_security_enabled =
315 is_asic_secured(hdev->asic_type);
Ofir Bittone5042a62021-04-01 13:43:40 +0300316 else
Ohad Sharabi4cb45082021-05-20 09:09:03 +0300317 hdev->asic_prop.fw_security_enabled = false;
Ofir Bittone5042a62021-04-01 13:43:40 +0300318
Ofir Bitton66a76402020-10-05 14:40:10 +0300319 /* Assign status description string */
Omer Shpigelman71731092021-08-16 13:27:12 +0300320 strncpy(hdev->status[HL_DEVICE_STATUS_OPERATIONAL],
321 "operational", HL_STR_MAX);
Ofir Bitton66a76402020-10-05 14:40:10 +0300322 strncpy(hdev->status[HL_DEVICE_STATUS_IN_RESET],
323 "in reset", HL_STR_MAX);
Omer Shpigelman71731092021-08-16 13:27:12 +0300324 strncpy(hdev->status[HL_DEVICE_STATUS_MALFUNCTION],
325 "disabled", HL_STR_MAX);
Ofir Bitton66a76402020-10-05 14:40:10 +0300326 strncpy(hdev->status[HL_DEVICE_STATUS_NEEDS_RESET],
327 "needs reset", HL_STR_MAX);
Omer Shpigelman71731092021-08-16 13:27:12 +0300328 strncpy(hdev->status[HL_DEVICE_STATUS_IN_DEVICE_CREATION],
329 "in device creation", HL_STR_MAX);
Ofir Bitton66a76402020-10-05 14:40:10 +0300330
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300331 hdev->major = hl_major;
332 hdev->reset_on_lockup = reset_on_lockup;
farah kassabri03df1362020-05-06 11:17:38 +0300333 hdev->memory_scrub = memory_scrub;
Oded Gabbay27a9e352021-04-12 09:52:05 +0300334 hdev->boot_error_status_mask = boot_error_status_mask;
Ofir Bitton358526be2021-02-11 11:09:12 +0200335 hdev->stop_on_err = true;
Oded Gabbay27a9e352021-04-12 09:52:05 +0300336
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300337 hdev->pldm = 0;
338
339 set_driver_behavior_per_device(hdev);
340
Koby Elbaz3e0ca9f2021-05-04 20:10:47 +0300341 hdev->curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
342
Oded Gabbay8c173dc2019-05-08 09:55:23 +0300343 if (timeout_locked)
344 hdev->timeout_jiffies = msecs_to_jiffies(timeout_locked * 1000);
345 else
346 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
347
348 hdev->disabled = true;
349 hdev->pdev = pdev; /* can be NULL in case of simulator device */
350
Oded Gabbayd9973872019-03-07 18:03:23 +0200351 /* Set default DMA mask to 32 bits */
352 hdev->dma_mask = 32;
353
Oded Gabbayc4d66342019-02-16 00:39:11 +0200354 mutex_lock(&hl_devs_idr_lock);
355
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300356 /* Always save 2 numbers, 1 for main device and 1 for control.
357 * They must be consecutive
358 */
359 main_id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS,
Oded Gabbayc4d66342019-02-16 00:39:11 +0200360 GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200361
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300362 if (main_id >= 0)
363 ctrl_id = idr_alloc(&hl_devs_idr, hdev, main_id + 1,
364 main_id + 2, GFP_KERNEL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200365
366 mutex_unlock(&hl_devs_idr_lock);
367
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300368 if ((main_id < 0) || (ctrl_id < 0)) {
369 if ((main_id == -ENOSPC) || (ctrl_id == -ENOSPC))
Oded Gabbayc4d66342019-02-16 00:39:11 +0200370 pr_err("too many devices in the system\n");
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300371
372 if (main_id >= 0) {
373 mutex_lock(&hl_devs_idr_lock);
374 idr_remove(&hl_devs_idr, main_id);
375 mutex_unlock(&hl_devs_idr_lock);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200376 }
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300377
378 rc = -EBUSY;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200379 goto free_hdev;
380 }
381
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300382 hdev->id = main_id;
383 hdev->id_control = ctrl_id;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200384
385 *dev = hdev;
386
387 return 0;
388
389free_hdev:
390 kfree(hdev);
391 return rc;
392}
393
394/*
395 * destroy_hdev - destroy habanalabs device instance
396 *
397 * @dev: pointer to the habanalabs device structure
398 *
399 */
400void destroy_hdev(struct hl_device *hdev)
401{
402 /* Remove device from the device list */
403 mutex_lock(&hl_devs_idr_lock);
404 idr_remove(&hl_devs_idr, hdev->id);
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300405 idr_remove(&hl_devs_idr, hdev->id_control);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200406 mutex_unlock(&hl_devs_idr_lock);
407
408 kfree(hdev);
409}
410
411static int hl_pmops_suspend(struct device *dev)
412{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800413 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200414
415 pr_debug("Going to suspend PCI device\n");
416
417 if (!hdev) {
418 pr_err("device pointer is NULL in suspend\n");
419 return 0;
420 }
421
422 return hl_device_suspend(hdev);
423}
424
425static int hl_pmops_resume(struct device *dev)
426{
Chuhong Yuan30f27322019-07-23 20:46:08 +0800427 struct hl_device *hdev = dev_get_drvdata(dev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200428
429 pr_debug("Going to resume PCI device\n");
430
431 if (!hdev) {
432 pr_err("device pointer is NULL in resume\n");
433 return 0;
434 }
435
436 return hl_device_resume(hdev);
437}
438
439/*
440 * hl_pci_probe - probe PCI habanalabs devices
441 *
442 * @pdev: pointer to pci device
443 * @id: pointer to pci device id structure
444 *
445 * Standard PCI probe function for habanalabs device.
446 * Create a new habanalabs device and initialize it according to the
447 * device's type
448 */
449static int hl_pci_probe(struct pci_dev *pdev,
450 const struct pci_device_id *id)
451{
452 struct hl_device *hdev;
453 int rc;
454
455 dev_info(&pdev->dev, HL_NAME
456 " device found [%04x:%04x] (rev %x)\n",
457 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
458
Oded Gabbay29593842019-04-04 14:33:34 +0300459 rc = create_hdev(&hdev, pdev, ASIC_INVALID, -1);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200460 if (rc)
461 return rc;
462
463 pci_set_drvdata(pdev, hdev);
464
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300465 pci_enable_pcie_error_reporting(pdev);
466
Oded Gabbayc4d66342019-02-16 00:39:11 +0200467 rc = hl_device_init(hdev, hl_class);
468 if (rc) {
469 dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");
470 rc = -ENODEV;
471 goto disable_device;
472 }
473
474 return 0;
475
476disable_device:
Christophe JAILLET3002f462021-06-12 07:39:51 +0200477 pci_disable_pcie_error_reporting(pdev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200478 pci_set_drvdata(pdev, NULL);
479 destroy_hdev(hdev);
480
481 return rc;
482}
483
484/*
485 * hl_pci_remove - remove PCI habanalabs devices
486 *
487 * @pdev: pointer to pci device
488 *
489 * Standard PCI remove function for habanalabs device
490 */
491static void hl_pci_remove(struct pci_dev *pdev)
492{
493 struct hl_device *hdev;
494
495 hdev = pci_get_drvdata(pdev);
496 if (!hdev)
497 return;
498
499 hl_device_fini(hdev);
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300500 pci_disable_pcie_error_reporting(pdev);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200501 pci_set_drvdata(pdev, NULL);
Oded Gabbayc4d66342019-02-16 00:39:11 +0200502 destroy_hdev(hdev);
503}
504
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300505/**
506 * hl_pci_err_detected - a PCI bus error detected on this device
507 *
508 * @pdev: pointer to pci device
509 * @state: PCI error type
510 *
511 * Called by the PCI subsystem whenever a non-correctable
512 * PCI bus error is detected
513 */
514static pci_ers_result_t
515hl_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t state)
516{
517 struct hl_device *hdev = pci_get_drvdata(pdev);
518 enum pci_ers_result result;
519
520 switch (state) {
521 case pci_channel_io_normal:
522 return PCI_ERS_RESULT_CAN_RECOVER;
523
524 case pci_channel_io_frozen:
525 dev_warn(hdev->dev, "frozen state error detected\n");
526 result = PCI_ERS_RESULT_NEED_RESET;
527 break;
528
529 case pci_channel_io_perm_failure:
530 dev_warn(hdev->dev, "failure state error detected\n");
531 result = PCI_ERS_RESULT_DISCONNECT;
532 break;
533
534 default:
535 result = PCI_ERS_RESULT_NONE;
536 }
537
Oded Gabbay8d9aa982021-08-09 22:43:37 +0300538 hdev->asic_funcs->halt_engines(hdev, true, false);
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300539
540 return result;
541}
542
543/**
544 * hl_pci_err_resume - resume after a PCI slot reset
545 *
546 * @pdev: pointer to pci device
547 *
548 */
549static void hl_pci_err_resume(struct pci_dev *pdev)
550{
551 struct hl_device *hdev = pci_get_drvdata(pdev);
552
553 dev_warn(hdev->dev, "Resuming device after PCI slot reset\n");
554 hl_device_resume(hdev);
555}
556
557/**
558 * hl_pci_err_slot_reset - a PCI slot reset has just happened
559 *
560 * @pdev: pointer to pci device
561 *
562 * Determine if the driver can recover from the PCI slot reset
563 */
564static pci_ers_result_t hl_pci_err_slot_reset(struct pci_dev *pdev)
565{
566 return PCI_ERS_RESULT_RECOVERED;
567}
568
Oded Gabbayc4d66342019-02-16 00:39:11 +0200569static const struct dev_pm_ops hl_pm_ops = {
570 .suspend = hl_pmops_suspend,
571 .resume = hl_pmops_resume,
572};
573
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300574static const struct pci_error_handlers hl_pci_err_handler = {
575 .error_detected = hl_pci_err_detected,
576 .slot_reset = hl_pci_err_slot_reset,
577 .resume = hl_pci_err_resume,
578};
579
Oded Gabbayc4d66342019-02-16 00:39:11 +0200580static struct pci_driver hl_pci_driver = {
581 .name = HL_NAME,
582 .id_table = ids,
583 .probe = hl_pci_probe,
584 .remove = hl_pci_remove,
Oded Gabbayfcaebc72020-12-14 12:52:06 +0200585 .shutdown = hl_pci_remove,
Oded Gabbay135ade02021-05-26 11:14:21 +0300586 .driver = {
587 .name = HL_NAME,
588 .pm = &hl_pm_ops,
589 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
590 },
Ofir Bitton2e5eda42020-05-20 16:35:08 +0300591 .err_handler = &hl_pci_err_handler,
Oded Gabbayc4d66342019-02-16 00:39:11 +0200592};
593
594/*
595 * hl_init - Initialize the habanalabs kernel driver
596 */
597static int __init hl_init(void)
598{
599 int rc;
600 dev_t dev;
601
602 pr_info("loading driver\n");
603
604 rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);
605 if (rc < 0) {
606 pr_err("unable to get major\n");
607 return rc;
608 }
609
610 hl_major = MAJOR(dev);
611
612 hl_class = class_create(THIS_MODULE, HL_NAME);
613 if (IS_ERR(hl_class)) {
614 pr_err("failed to allocate class\n");
615 rc = PTR_ERR(hl_class);
616 goto remove_major;
617 }
618
Oded Gabbayc2164772019-02-16 00:39:24 +0200619 hl_debugfs_init();
620
Oded Gabbayc4d66342019-02-16 00:39:11 +0200621 rc = pci_register_driver(&hl_pci_driver);
622 if (rc) {
623 pr_err("failed to register pci device\n");
Oded Gabbayc2164772019-02-16 00:39:24 +0200624 goto remove_debugfs;
Oded Gabbayc4d66342019-02-16 00:39:11 +0200625 }
626
627 pr_debug("driver loaded\n");
628
629 return 0;
630
Oded Gabbayc2164772019-02-16 00:39:24 +0200631remove_debugfs:
632 hl_debugfs_fini();
Oded Gabbayc4d66342019-02-16 00:39:11 +0200633 class_destroy(hl_class);
634remove_major:
635 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
636 return rc;
637}
638
639/*
640 * hl_exit - Release all resources of the habanalabs kernel driver
641 */
642static void __exit hl_exit(void)
643{
644 pci_unregister_driver(&hl_pci_driver);
645
Oded Gabbayc2164772019-02-16 00:39:24 +0200646 /*
647 * Removing debugfs must be after all devices or simulator devices
648 * have been removed because otherwise we get a bug in the
649 * debugfs module for referencing NULL objects
650 */
651 hl_debugfs_fini();
652
Oded Gabbayc4d66342019-02-16 00:39:11 +0200653 class_destroy(hl_class);
654 unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
655
656 idr_destroy(&hl_devs_idr);
657
658 pr_debug("driver removed\n");
659}
660
661module_init(hl_init);
662module_exit(hl_exit);