xref: /linux/drivers/accel/qaic/qaic_drv.c (revision ab779466166348eecf17d20f620aa9a47965c934)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved. */
4 /* Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved. */
5 
6 #include <linux/delay.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/idr.h>
9 #include <linux/interrupt.h>
10 #include <linux/list.h>
11 #include <linux/kref.h>
12 #include <linux/mhi.h>
13 #include <linux/module.h>
14 #include <linux/msi.h>
15 #include <linux/mutex.h>
16 #include <linux/pci.h>
17 #include <linux/spinlock.h>
18 #include <linux/workqueue.h>
19 #include <linux/wait.h>
20 #include <drm/drm_accel.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_file.h>
23 #include <drm/drm_gem.h>
24 #include <drm/drm_ioctl.h>
25 #include <drm/drm_managed.h>
26 #include <uapi/drm/qaic_accel.h>
27 
28 #include "mhi_controller.h"
29 #include "qaic.h"
30 #include "qaic_timesync.h"
31 
32 MODULE_IMPORT_NS(DMA_BUF);
33 
34 #define PCI_DEV_AIC100			0xa100
35 #define QAIC_NAME			"qaic"
36 #define QAIC_DESC			"Qualcomm Cloud AI Accelerators"
37 #define CNTL_MAJOR			5
38 #define CNTL_MINOR			0
39 
40 bool datapath_polling;
41 module_param(datapath_polling, bool, 0400);
42 MODULE_PARM_DESC(datapath_polling, "Operate the datapath in polling mode");
43 static bool link_up;
44 static DEFINE_IDA(qaic_usrs);
45 
46 static int qaic_create_drm_device(struct qaic_device *qdev, s32 partition_id);
47 static void qaic_destroy_drm_device(struct qaic_device *qdev, s32 partition_id);
48 
49 static void free_usr(struct kref *kref)
50 {
51 	struct qaic_user *usr = container_of(kref, struct qaic_user, ref_count);
52 
53 	cleanup_srcu_struct(&usr->qddev_lock);
54 	ida_free(&qaic_usrs, usr->handle);
55 	kfree(usr);
56 }
57 
58 static int qaic_open(struct drm_device *dev, struct drm_file *file)
59 {
60 	struct qaic_drm_device *qddev = to_qaic_drm_device(dev);
61 	struct qaic_device *qdev = qddev->qdev;
62 	struct qaic_user *usr;
63 	int rcu_id;
64 	int ret;
65 
66 	rcu_id = srcu_read_lock(&qdev->dev_lock);
67 	if (qdev->in_reset) {
68 		ret = -ENODEV;
69 		goto dev_unlock;
70 	}
71 
72 	usr = kmalloc(sizeof(*usr), GFP_KERNEL);
73 	if (!usr) {
74 		ret = -ENOMEM;
75 		goto dev_unlock;
76 	}
77 
78 	usr->handle = ida_alloc(&qaic_usrs, GFP_KERNEL);
79 	if (usr->handle < 0) {
80 		ret = usr->handle;
81 		goto free_usr;
82 	}
83 	usr->qddev = qddev;
84 	atomic_set(&usr->chunk_id, 0);
85 	init_srcu_struct(&usr->qddev_lock);
86 	kref_init(&usr->ref_count);
87 
88 	ret = mutex_lock_interruptible(&qddev->users_mutex);
89 	if (ret)
90 		goto cleanup_usr;
91 
92 	list_add(&usr->node, &qddev->users);
93 	mutex_unlock(&qddev->users_mutex);
94 
95 	file->driver_priv = usr;
96 
97 	srcu_read_unlock(&qdev->dev_lock, rcu_id);
98 	return 0;
99 
100 cleanup_usr:
101 	cleanup_srcu_struct(&usr->qddev_lock);
102 	ida_free(&qaic_usrs, usr->handle);
103 free_usr:
104 	kfree(usr);
105 dev_unlock:
106 	srcu_read_unlock(&qdev->dev_lock, rcu_id);
107 	return ret;
108 }
109 
110 static void qaic_postclose(struct drm_device *dev, struct drm_file *file)
111 {
112 	struct qaic_user *usr = file->driver_priv;
113 	struct qaic_drm_device *qddev;
114 	struct qaic_device *qdev;
115 	int qdev_rcu_id;
116 	int usr_rcu_id;
117 	int i;
118 
119 	qddev = usr->qddev;
120 	usr_rcu_id = srcu_read_lock(&usr->qddev_lock);
121 	if (qddev) {
122 		qdev = qddev->qdev;
123 		qdev_rcu_id = srcu_read_lock(&qdev->dev_lock);
124 		if (!qdev->in_reset) {
125 			qaic_release_usr(qdev, usr);
126 			for (i = 0; i < qdev->num_dbc; ++i)
127 				if (qdev->dbc[i].usr && qdev->dbc[i].usr->handle == usr->handle)
128 					release_dbc(qdev, i);
129 		}
130 		srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id);
131 
132 		mutex_lock(&qddev->users_mutex);
133 		if (!list_empty(&usr->node))
134 			list_del_init(&usr->node);
135 		mutex_unlock(&qddev->users_mutex);
136 	}
137 
138 	srcu_read_unlock(&usr->qddev_lock, usr_rcu_id);
139 	kref_put(&usr->ref_count, free_usr);
140 
141 	file->driver_priv = NULL;
142 }
143 
144 DEFINE_DRM_ACCEL_FOPS(qaic_accel_fops);
145 
146 static const struct drm_ioctl_desc qaic_drm_ioctls[] = {
147 	DRM_IOCTL_DEF_DRV(QAIC_MANAGE, qaic_manage_ioctl, 0),
148 	DRM_IOCTL_DEF_DRV(QAIC_CREATE_BO, qaic_create_bo_ioctl, 0),
149 	DRM_IOCTL_DEF_DRV(QAIC_MMAP_BO, qaic_mmap_bo_ioctl, 0),
150 	DRM_IOCTL_DEF_DRV(QAIC_ATTACH_SLICE_BO, qaic_attach_slice_bo_ioctl, 0),
151 	DRM_IOCTL_DEF_DRV(QAIC_EXECUTE_BO, qaic_execute_bo_ioctl, 0),
152 	DRM_IOCTL_DEF_DRV(QAIC_PARTIAL_EXECUTE_BO, qaic_partial_execute_bo_ioctl, 0),
153 	DRM_IOCTL_DEF_DRV(QAIC_WAIT_BO, qaic_wait_bo_ioctl, 0),
154 	DRM_IOCTL_DEF_DRV(QAIC_PERF_STATS_BO, qaic_perf_stats_bo_ioctl, 0),
155 	DRM_IOCTL_DEF_DRV(QAIC_DETACH_SLICE_BO, qaic_detach_slice_bo_ioctl, 0),
156 };
157 
158 static const struct drm_driver qaic_accel_driver = {
159 	.driver_features	= DRIVER_GEM | DRIVER_COMPUTE_ACCEL,
160 
161 	.name			= QAIC_NAME,
162 	.desc			= QAIC_DESC,
163 	.date			= "20190618",
164 
165 	.fops			= &qaic_accel_fops,
166 	.open			= qaic_open,
167 	.postclose		= qaic_postclose,
168 
169 	.ioctls			= qaic_drm_ioctls,
170 	.num_ioctls		= ARRAY_SIZE(qaic_drm_ioctls),
171 	.gem_prime_import	= qaic_gem_prime_import,
172 };
173 
174 static int qaic_create_drm_device(struct qaic_device *qdev, s32 partition_id)
175 {
176 	struct qaic_drm_device *qddev = qdev->qddev;
177 	struct drm_device *drm = to_drm(qddev);
178 	int ret;
179 
180 	/* Hold off implementing partitions until the uapi is determined */
181 	if (partition_id != QAIC_NO_PARTITION)
182 		return -EINVAL;
183 
184 	qddev->partition_id = partition_id;
185 
186 	/*
187 	 * drm_dev_unregister() sets the driver data to NULL and
188 	 * drm_dev_register() does not update the driver data. During a SOC
189 	 * reset drm dev is unregistered and registered again leaving the
190 	 * driver data to NULL.
191 	 */
192 	dev_set_drvdata(to_accel_kdev(qddev), drm->accel);
193 	ret = drm_dev_register(drm, 0);
194 	if (ret)
195 		pci_dbg(qdev->pdev, "drm_dev_register failed %d\n", ret);
196 
197 	return ret;
198 }
199 
200 static void qaic_destroy_drm_device(struct qaic_device *qdev, s32 partition_id)
201 {
202 	struct qaic_drm_device *qddev = qdev->qddev;
203 	struct drm_device *drm = to_drm(qddev);
204 	struct qaic_user *usr;
205 
206 	drm_dev_get(drm);
207 	drm_dev_unregister(drm);
208 	qddev->partition_id = 0;
209 	/*
210 	 * Existing users get unresolvable errors till they close FDs.
211 	 * Need to sync carefully with users calling close(). The
212 	 * list of users can be modified elsewhere when the lock isn't
213 	 * held here, but the sync'ing the srcu with the mutex held
214 	 * could deadlock. Grab the mutex so that the list will be
215 	 * unmodified. The user we get will exist as long as the
216 	 * lock is held. Signal that the qcdev is going away, and
217 	 * grab a reference to the user so they don't go away for
218 	 * synchronize_srcu(). Then release the mutex to avoid
219 	 * deadlock and make sure the user has observed the signal.
220 	 * With the lock released, we cannot maintain any state of the
221 	 * user list.
222 	 */
223 	mutex_lock(&qddev->users_mutex);
224 	while (!list_empty(&qddev->users)) {
225 		usr = list_first_entry(&qddev->users, struct qaic_user, node);
226 		list_del_init(&usr->node);
227 		kref_get(&usr->ref_count);
228 		usr->qddev = NULL;
229 		mutex_unlock(&qddev->users_mutex);
230 		synchronize_srcu(&usr->qddev_lock);
231 		kref_put(&usr->ref_count, free_usr);
232 		mutex_lock(&qddev->users_mutex);
233 	}
234 	mutex_unlock(&qddev->users_mutex);
235 	drm_dev_put(drm);
236 }
237 
238 static int qaic_mhi_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id)
239 {
240 	u16 major = -1, minor = -1;
241 	struct qaic_device *qdev;
242 	int ret;
243 
244 	/*
245 	 * Invoking this function indicates that the control channel to the
246 	 * device is available. We use that as a signal to indicate that
247 	 * the device side firmware has booted. The device side firmware
248 	 * manages the device resources, so we need to communicate with it
249 	 * via the control channel in order to utilize the device. Therefore
250 	 * we wait until this signal to create the drm dev that userspace will
251 	 * use to control the device, because without the device side firmware,
252 	 * userspace can't do anything useful.
253 	 */
254 
255 	qdev = pci_get_drvdata(to_pci_dev(mhi_dev->mhi_cntrl->cntrl_dev));
256 
257 	qdev->in_reset = false;
258 
259 	dev_set_drvdata(&mhi_dev->dev, qdev);
260 	qdev->cntl_ch = mhi_dev;
261 
262 	ret = qaic_control_open(qdev);
263 	if (ret) {
264 		pci_dbg(qdev->pdev, "%s: control_open failed %d\n", __func__, ret);
265 		return ret;
266 	}
267 
268 	ret = get_cntl_version(qdev, NULL, &major, &minor);
269 	if (ret || major != CNTL_MAJOR || minor > CNTL_MINOR) {
270 		pci_err(qdev->pdev, "%s: Control protocol version (%d.%d) not supported. Supported version is (%d.%d). Ret: %d\n",
271 			__func__, major, minor, CNTL_MAJOR, CNTL_MINOR, ret);
272 		ret = -EINVAL;
273 		goto close_control;
274 	}
275 
276 	ret = qaic_create_drm_device(qdev, QAIC_NO_PARTITION);
277 
278 	return ret;
279 
280 close_control:
281 	qaic_control_close(qdev);
282 	return ret;
283 }
284 
285 static void qaic_mhi_remove(struct mhi_device *mhi_dev)
286 {
287 /* This is redundant since we have already observed the device crash */
288 }
289 
290 static void qaic_notify_reset(struct qaic_device *qdev)
291 {
292 	int i;
293 
294 	qdev->in_reset = true;
295 	/* wake up any waiters to avoid waiting for timeouts at sync */
296 	wake_all_cntl(qdev);
297 	for (i = 0; i < qdev->num_dbc; ++i)
298 		wakeup_dbc(qdev, i);
299 	synchronize_srcu(&qdev->dev_lock);
300 }
301 
302 void qaic_dev_reset_clean_local_state(struct qaic_device *qdev, bool exit_reset)
303 {
304 	int i;
305 
306 	qaic_notify_reset(qdev);
307 
308 	/* remove drmdevs to prevent new users from coming in */
309 	qaic_destroy_drm_device(qdev, QAIC_NO_PARTITION);
310 
311 	/* start tearing things down */
312 	for (i = 0; i < qdev->num_dbc; ++i)
313 		release_dbc(qdev, i);
314 
315 	if (exit_reset)
316 		qdev->in_reset = false;
317 }
318 
319 static void cleanup_qdev(struct qaic_device *qdev)
320 {
321 	int i;
322 
323 	for (i = 0; i < qdev->num_dbc; ++i)
324 		cleanup_srcu_struct(&qdev->dbc[i].ch_lock);
325 	cleanup_srcu_struct(&qdev->dev_lock);
326 	pci_set_drvdata(qdev->pdev, NULL);
327 	destroy_workqueue(qdev->cntl_wq);
328 	destroy_workqueue(qdev->qts_wq);
329 }
330 
331 static struct qaic_device *create_qdev(struct pci_dev *pdev, const struct pci_device_id *id)
332 {
333 	struct qaic_drm_device *qddev;
334 	struct qaic_device *qdev;
335 	int i;
336 
337 	qdev = devm_kzalloc(&pdev->dev, sizeof(*qdev), GFP_KERNEL);
338 	if (!qdev)
339 		return NULL;
340 
341 	if (id->device == PCI_DEV_AIC100) {
342 		qdev->num_dbc = 16;
343 		qdev->dbc = devm_kcalloc(&pdev->dev, qdev->num_dbc, sizeof(*qdev->dbc), GFP_KERNEL);
344 		if (!qdev->dbc)
345 			return NULL;
346 	}
347 
348 	qdev->cntl_wq = alloc_workqueue("qaic_cntl", WQ_UNBOUND, 0);
349 	if (!qdev->cntl_wq)
350 		return NULL;
351 
352 	qdev->qts_wq = alloc_workqueue("qaic_ts", WQ_UNBOUND, 0);
353 	if (!qdev->qts_wq) {
354 		destroy_workqueue(qdev->cntl_wq);
355 		return NULL;
356 	}
357 
358 	pci_set_drvdata(pdev, qdev);
359 	qdev->pdev = pdev;
360 
361 	mutex_init(&qdev->cntl_mutex);
362 	INIT_LIST_HEAD(&qdev->cntl_xfer_list);
363 	init_srcu_struct(&qdev->dev_lock);
364 
365 	for (i = 0; i < qdev->num_dbc; ++i) {
366 		spin_lock_init(&qdev->dbc[i].xfer_lock);
367 		qdev->dbc[i].qdev = qdev;
368 		qdev->dbc[i].id = i;
369 		INIT_LIST_HEAD(&qdev->dbc[i].xfer_list);
370 		init_srcu_struct(&qdev->dbc[i].ch_lock);
371 		init_waitqueue_head(&qdev->dbc[i].dbc_release);
372 		INIT_LIST_HEAD(&qdev->dbc[i].bo_lists);
373 	}
374 
375 	qddev = devm_drm_dev_alloc(&pdev->dev, &qaic_accel_driver, struct qaic_drm_device, drm);
376 	if (IS_ERR(qddev)) {
377 		cleanup_qdev(qdev);
378 		return NULL;
379 	}
380 
381 	drmm_mutex_init(to_drm(qddev), &qddev->users_mutex);
382 	INIT_LIST_HEAD(&qddev->users);
383 	qddev->qdev = qdev;
384 	qdev->qddev = qddev;
385 
386 	return qdev;
387 }
388 
389 static int init_pci(struct qaic_device *qdev, struct pci_dev *pdev)
390 {
391 	int bars;
392 	int ret;
393 
394 	bars = pci_select_bars(pdev, IORESOURCE_MEM);
395 
396 	/* make sure the device has the expected BARs */
397 	if (bars != (BIT(0) | BIT(2) | BIT(4))) {
398 		pci_dbg(pdev, "%s: expected BARs 0, 2, and 4 not found in device. Found 0x%x\n",
399 			__func__, bars);
400 		return -EINVAL;
401 	}
402 
403 	ret = pcim_enable_device(pdev);
404 	if (ret)
405 		return ret;
406 
407 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
408 	if (ret)
409 		return ret;
410 	ret = dma_set_max_seg_size(&pdev->dev, UINT_MAX);
411 	if (ret)
412 		return ret;
413 
414 	qdev->bar_0 = devm_ioremap_resource(&pdev->dev, &pdev->resource[0]);
415 	if (IS_ERR(qdev->bar_0))
416 		return PTR_ERR(qdev->bar_0);
417 
418 	qdev->bar_2 = devm_ioremap_resource(&pdev->dev, &pdev->resource[2]);
419 	if (IS_ERR(qdev->bar_2))
420 		return PTR_ERR(qdev->bar_2);
421 
422 	/* Managed release since we use pcim_enable_device above */
423 	pci_set_master(pdev);
424 
425 	return 0;
426 }
427 
428 static int init_msi(struct qaic_device *qdev, struct pci_dev *pdev)
429 {
430 	int mhi_irq;
431 	int ret;
432 	int i;
433 
434 	/* Managed release since we use pcim_enable_device */
435 	ret = pci_alloc_irq_vectors(pdev, 32, 32, PCI_IRQ_MSI);
436 	if (ret == -ENOSPC) {
437 		ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
438 		if (ret < 0)
439 			return ret;
440 
441 		/*
442 		 * Operate in one MSI mode. All interrupts will be directed to
443 		 * MSI0; every interrupt will wake up all the interrupt handlers
444 		 * (MHI and DBC[0-15]). Since the interrupt is now shared, it is
445 		 * not disabled during DBC threaded handler, but only one thread
446 		 * will be allowed to run per DBC, so while it can be
447 		 * interrupted, it shouldn't race with itself.
448 		 */
449 		qdev->single_msi = true;
450 		pci_info(pdev, "Allocating 32 MSIs failed, operating in 1 MSI mode. Performance may be impacted.\n");
451 	} else if (ret < 0) {
452 		return ret;
453 	}
454 
455 	mhi_irq = pci_irq_vector(pdev, 0);
456 	if (mhi_irq < 0)
457 		return mhi_irq;
458 
459 	for (i = 0; i < qdev->num_dbc; ++i) {
460 		ret = devm_request_threaded_irq(&pdev->dev,
461 						pci_irq_vector(pdev, qdev->single_msi ? 0 : i + 1),
462 						dbc_irq_handler, dbc_irq_threaded_fn, IRQF_SHARED,
463 						"qaic_dbc", &qdev->dbc[i]);
464 		if (ret)
465 			return ret;
466 
467 		if (datapath_polling) {
468 			qdev->dbc[i].irq = pci_irq_vector(pdev, qdev->single_msi ? 0 : i + 1);
469 			if (!qdev->single_msi)
470 				disable_irq_nosync(qdev->dbc[i].irq);
471 			INIT_WORK(&qdev->dbc[i].poll_work, irq_polling_work);
472 		}
473 	}
474 
475 	return mhi_irq;
476 }
477 
478 static int qaic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
479 {
480 	struct qaic_device *qdev;
481 	int mhi_irq;
482 	int ret;
483 	int i;
484 
485 	qdev = create_qdev(pdev, id);
486 	if (!qdev)
487 		return -ENOMEM;
488 
489 	ret = init_pci(qdev, pdev);
490 	if (ret)
491 		goto cleanup_qdev;
492 
493 	for (i = 0; i < qdev->num_dbc; ++i)
494 		qdev->dbc[i].dbc_base = qdev->bar_2 + QAIC_DBC_OFF(i);
495 
496 	mhi_irq = init_msi(qdev, pdev);
497 	if (mhi_irq < 0) {
498 		ret = mhi_irq;
499 		goto cleanup_qdev;
500 	}
501 
502 	qdev->mhi_cntrl = qaic_mhi_register_controller(pdev, qdev->bar_0, mhi_irq,
503 						       qdev->single_msi);
504 	if (IS_ERR(qdev->mhi_cntrl)) {
505 		ret = PTR_ERR(qdev->mhi_cntrl);
506 		goto cleanup_qdev;
507 	}
508 
509 	return 0;
510 
511 cleanup_qdev:
512 	cleanup_qdev(qdev);
513 	return ret;
514 }
515 
516 static void qaic_pci_remove(struct pci_dev *pdev)
517 {
518 	struct qaic_device *qdev = pci_get_drvdata(pdev);
519 
520 	if (!qdev)
521 		return;
522 
523 	qaic_dev_reset_clean_local_state(qdev, false);
524 	qaic_mhi_free_controller(qdev->mhi_cntrl, link_up);
525 	cleanup_qdev(qdev);
526 }
527 
528 static void qaic_pci_shutdown(struct pci_dev *pdev)
529 {
530 	/* see qaic_exit for what link_up is doing */
531 	link_up = true;
532 	qaic_pci_remove(pdev);
533 }
534 
535 static pci_ers_result_t qaic_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t error)
536 {
537 	return PCI_ERS_RESULT_NEED_RESET;
538 }
539 
540 static void qaic_pci_reset_prepare(struct pci_dev *pdev)
541 {
542 	struct qaic_device *qdev = pci_get_drvdata(pdev);
543 
544 	qaic_notify_reset(qdev);
545 	qaic_mhi_start_reset(qdev->mhi_cntrl);
546 	qaic_dev_reset_clean_local_state(qdev, false);
547 }
548 
549 static void qaic_pci_reset_done(struct pci_dev *pdev)
550 {
551 	struct qaic_device *qdev = pci_get_drvdata(pdev);
552 
553 	qdev->in_reset = false;
554 	qaic_mhi_reset_done(qdev->mhi_cntrl);
555 }
556 
557 static const struct mhi_device_id qaic_mhi_match_table[] = {
558 	{ .chan = "QAIC_CONTROL", },
559 	{},
560 };
561 
562 static struct mhi_driver qaic_mhi_driver = {
563 	.id_table = qaic_mhi_match_table,
564 	.remove = qaic_mhi_remove,
565 	.probe = qaic_mhi_probe,
566 	.ul_xfer_cb = qaic_mhi_ul_xfer_cb,
567 	.dl_xfer_cb = qaic_mhi_dl_xfer_cb,
568 	.driver = {
569 		.name = "qaic_mhi",
570 	},
571 };
572 
573 static const struct pci_device_id qaic_ids[] = {
574 	{ PCI_DEVICE(PCI_VENDOR_ID_QCOM, PCI_DEV_AIC100), },
575 	{ }
576 };
577 MODULE_DEVICE_TABLE(pci, qaic_ids);
578 
579 static const struct pci_error_handlers qaic_pci_err_handler = {
580 	.error_detected = qaic_pci_error_detected,
581 	.reset_prepare = qaic_pci_reset_prepare,
582 	.reset_done = qaic_pci_reset_done,
583 };
584 
585 static struct pci_driver qaic_pci_driver = {
586 	.name = QAIC_NAME,
587 	.id_table = qaic_ids,
588 	.probe = qaic_pci_probe,
589 	.remove = qaic_pci_remove,
590 	.shutdown = qaic_pci_shutdown,
591 	.err_handler = &qaic_pci_err_handler,
592 };
593 
594 static int __init qaic_init(void)
595 {
596 	int ret;
597 
598 	ret = pci_register_driver(&qaic_pci_driver);
599 	if (ret) {
600 		pr_debug("qaic: pci_register_driver failed %d\n", ret);
601 		return ret;
602 	}
603 
604 	ret = mhi_driver_register(&qaic_mhi_driver);
605 	if (ret) {
606 		pr_debug("qaic: mhi_driver_register failed %d\n", ret);
607 		goto free_pci;
608 	}
609 
610 	ret = qaic_timesync_init();
611 	if (ret)
612 		pr_debug("qaic: qaic_timesync_init failed %d\n", ret);
613 
614 	return 0;
615 
616 free_pci:
617 	pci_unregister_driver(&qaic_pci_driver);
618 	return ret;
619 }
620 
621 static void __exit qaic_exit(void)
622 {
623 	/*
624 	 * We assume that qaic_pci_remove() is called due to a hotplug event
625 	 * which would mean that the link is down, and thus
626 	 * qaic_mhi_free_controller() should not try to access the device during
627 	 * cleanup.
628 	 * We call pci_unregister_driver() below, which also triggers
629 	 * qaic_pci_remove(), but since this is module exit, we expect the link
630 	 * to the device to be up, in which case qaic_mhi_free_controller()
631 	 * should try to access the device during cleanup to put the device in
632 	 * a sane state.
633 	 * For that reason, we set link_up here to let qaic_mhi_free_controller
634 	 * know the expected link state. Since the module is going to be
635 	 * removed at the end of this, we don't need to worry about
636 	 * reinitializing the link_up state after the cleanup is done.
637 	 */
638 	link_up = true;
639 	qaic_timesync_deinit();
640 	mhi_driver_unregister(&qaic_mhi_driver);
641 	pci_unregister_driver(&qaic_pci_driver);
642 }
643 
644 module_init(qaic_init);
645 module_exit(qaic_exit);
646 
647 MODULE_AUTHOR(QAIC_DESC " Kernel Driver Team");
648 MODULE_DESCRIPTION(QAIC_DESC " Accel Driver");
649 MODULE_LICENSE("GPL");
650