xref: /netbsd-src/sys/external/bsd/drm2/i915drm/i915_module.c (revision d90df327193d174d9a1e65991de539e06a199d6a)
1*d90df327Sriastradh /*	$NetBSD: i915_module.c,v 1.19 2022/07/17 14:10:43 riastradh Exp $	*/
26cb10275Sriastradh 
36cb10275Sriastradh /*-
46cb10275Sriastradh  * Copyright (c) 2013 The NetBSD Foundation, Inc.
56cb10275Sriastradh  * All rights reserved.
66cb10275Sriastradh  *
76cb10275Sriastradh  * This code is derived from software contributed to The NetBSD Foundation
86cb10275Sriastradh  * by Taylor R. Campbell.
96cb10275Sriastradh  *
106cb10275Sriastradh  * Redistribution and use in source and binary forms, with or without
116cb10275Sriastradh  * modification, are permitted provided that the following conditions
126cb10275Sriastradh  * are met:
136cb10275Sriastradh  * 1. Redistributions of source code must retain the above copyright
146cb10275Sriastradh  *    notice, this list of conditions and the following disclaimer.
156cb10275Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
166cb10275Sriastradh  *    notice, this list of conditions and the following disclaimer in the
176cb10275Sriastradh  *    documentation and/or other materials provided with the distribution.
186cb10275Sriastradh  *
196cb10275Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206cb10275Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216cb10275Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226cb10275Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236cb10275Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246cb10275Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256cb10275Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266cb10275Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276cb10275Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286cb10275Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296cb10275Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
306cb10275Sriastradh  */
316cb10275Sriastradh 
326cb10275Sriastradh #include <sys/cdefs.h>
33*d90df327Sriastradh __KERNEL_RCSID(0, "$NetBSD: i915_module.c,v 1.19 2022/07/17 14:10:43 riastradh Exp $");
346cb10275Sriastradh 
356cb10275Sriastradh #include <sys/types.h>
366cb10275Sriastradh #include <sys/module.h>
3777b5597aSriastradh #ifndef _MODULE
3877b5597aSriastradh #include <sys/once.h>
3977b5597aSriastradh #endif
406cb10275Sriastradh #include <sys/systm.h>
416cb10275Sriastradh 
42f21b21b0Sriastradh #include <drm/drm_device.h>
43bf1c1789Schristos #include <drm/drm_sysctl.h>
446cb10275Sriastradh 
456cb10275Sriastradh #include "i915_drv.h"
469e5fbd4fSriastradh #include "i915_globals.h"
479c351cd5Sriastradh #include "gt/intel_rps.h"
486cb10275Sriastradh 
492071d9e9Sriastradh MODULE(MODULE_CLASS_DRIVER, i915drmkms, "acpivga,drmkms,drmkms_pci"); /* XXX drmkms_i2c */
506cb10275Sriastradh 
516cb10275Sriastradh #ifdef _MODULE
526cb10275Sriastradh #include "ioconf.c"
536cb10275Sriastradh #endif
546cb10275Sriastradh 
558ba1623fSchristos struct drm_sysctl_def i915_def = DRM_SYSCTL_INIT();
56bf1c1789Schristos 
5739bff1e0Sriastradh /* XXX use link sets for DEFINE_SPINLOCK */
58099ee55fSriastradh extern spinlock_t i915_sw_fence_lock;
5939bff1e0Sriastradh extern spinlock_t *const i915_schedule_lock;
60099ee55fSriastradh 
6177b5597aSriastradh static int
i915drmkms_init(void)6277b5597aSriastradh i915drmkms_init(void)
6377b5597aSriastradh {
6477b5597aSriastradh 	int error;
6577b5597aSriastradh 
6677b5597aSriastradh 	error = drm_guarantee_initialized();
6777b5597aSriastradh 	if (error)
6877b5597aSriastradh 		return error;
6977b5597aSriastradh 
709e5fbd4fSriastradh 	/* XXX errno Linux->NetBSD */
719e5fbd4fSriastradh 	error = -i915_globals_init();
72ecb6e743Sriastradh 	if (error)
73ecb6e743Sriastradh 		return error;
74ecb6e743Sriastradh 
758ba1623fSchristos 	drm_sysctl_init(&i915_def);
76199f703dSriastradh 	spin_lock_init(&mchdev_lock);
77099ee55fSriastradh 	spin_lock_init(&i915_sw_fence_lock);
7839bff1e0Sriastradh 	spin_lock_init(i915_schedule_lock);
7977b5597aSriastradh 
8077b5597aSriastradh 	return 0;
8177b5597aSriastradh }
8277b5597aSriastradh 
8377b5597aSriastradh int	i915drmkms_guarantee_initialized(void); /* XXX */
8477b5597aSriastradh int
i915drmkms_guarantee_initialized(void)8577b5597aSriastradh i915drmkms_guarantee_initialized(void)
8677b5597aSriastradh {
8777b5597aSriastradh #ifdef _MODULE
8877b5597aSriastradh 	return 0;
8977b5597aSriastradh #else
9077b5597aSriastradh 	static ONCE_DECL(i915drmkms_init_once);
9177b5597aSriastradh 
9277b5597aSriastradh 	return RUN_ONCE(&i915drmkms_init_once, &i915drmkms_init);
9377b5597aSriastradh #endif
9477b5597aSriastradh }
9577b5597aSriastradh 
9677b5597aSriastradh static void
i915drmkms_fini(void)9777b5597aSriastradh i915drmkms_fini(void)
9877b5597aSriastradh {
9977b5597aSriastradh 
10039bff1e0Sriastradh 	spin_lock_destroy(i915_schedule_lock);
101099ee55fSriastradh 	spin_lock_destroy(&i915_sw_fence_lock);
102199f703dSriastradh 	spin_lock_destroy(&mchdev_lock);
1038ba1623fSchristos 	drm_sysctl_fini(&i915_def);
1049e5fbd4fSriastradh 
1059e5fbd4fSriastradh 	i915_globals_exit();
10677b5597aSriastradh }
1076cb10275Sriastradh 
1086cb10275Sriastradh static int
i915drmkms_modcmd(modcmd_t cmd,void * arg __unused)1096cb10275Sriastradh i915drmkms_modcmd(modcmd_t cmd, void *arg __unused)
1106cb10275Sriastradh {
1116cb10275Sriastradh 	int error;
1126cb10275Sriastradh 
1136cb10275Sriastradh 	switch (cmd) {
1146cb10275Sriastradh 	case MODULE_CMD_INIT:
1156cb10275Sriastradh 		/* XXX Kludge it up...  Must happen before attachment.  */
11677b5597aSriastradh #ifdef _MODULE
11777b5597aSriastradh 		error = i915drmkms_init();
11877b5597aSriastradh #else
11977b5597aSriastradh 		error = i915drmkms_guarantee_initialized();
12077b5597aSriastradh #endif
1216cb10275Sriastradh 		if (error) {
12277b5597aSriastradh 			aprint_error("i915drmkms: failed to initialize: %d\n",
1236cb10275Sriastradh 			    error);
1246cb10275Sriastradh 			return error;
1256cb10275Sriastradh 		}
1266cb10275Sriastradh #ifdef _MODULE
1276cb10275Sriastradh 		error = config_init_component(cfdriver_ioconf_i915drmkms,
1286cb10275Sriastradh 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
1296cb10275Sriastradh 		if (error) {
1306cb10275Sriastradh 			aprint_error("i915drmkms: failed to init component"
1316cb10275Sriastradh 			    ": %d\n", error);
13277b5597aSriastradh 			i915drmkms_fini();
1336cb10275Sriastradh 			return error;
1346cb10275Sriastradh 		}
1356cb10275Sriastradh #endif
1366cb10275Sriastradh 		return 0;
1376cb10275Sriastradh 
1386cb10275Sriastradh 	case MODULE_CMD_FINI:
1396cb10275Sriastradh #ifdef _MODULE
1406cb10275Sriastradh 		error = config_fini_component(cfdriver_ioconf_i915drmkms,
1416cb10275Sriastradh 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
1426cb10275Sriastradh 		if (error) {
1436cb10275Sriastradh 			aprint_error("i915drmkms: failed to fini component"
1446cb10275Sriastradh 			    ": %d\n", error);
1456cb10275Sriastradh 			return error;
1466cb10275Sriastradh 		}
1476cb10275Sriastradh #endif
14877b5597aSriastradh 		i915drmkms_fini();
1496cb10275Sriastradh 		return 0;
1506cb10275Sriastradh 
1516cb10275Sriastradh 	default:
1526cb10275Sriastradh 		return ENOTTY;
1536cb10275Sriastradh 	}
1546cb10275Sriastradh }
155