Home | History | Annotate | Line # | Download | only in i915drm
i915_module.c revision 1.17
      1 /*	$NetBSD: i915_module.c,v 1.17 2021/12/19 11:49:12 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: i915_module.c,v 1.17 2021/12/19 11:49:12 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/module.h>
     37 #ifndef _MODULE
     38 #include <sys/once.h>
     39 #endif
     40 #include <sys/systm.h>
     41 
     42 #include <drm/drm_device.h>
     43 #include <drm/drm_sysctl.h>
     44 
     45 #include "i915_drv.h"
     46 #include "i915_globals.h"
     47 #include "gt/intel_rps.h"
     48 
     49 MODULE(MODULE_CLASS_DRIVER, i915drmkms, "drmkms,drmkms_pci"); /* XXX drmkms_i2c */
     50 
     51 #ifdef _MODULE
     52 #include "ioconf.c"
     53 #endif
     54 
     55 struct drm_sysctl_def i915_def = DRM_SYSCTL_INIT();
     56 
     57 /* XXX use link sets for DEFINE_SPINLOCK */
     58 extern spinlock_t i915_sw_fence_lock;
     59 extern spinlock_t *const i915_schedule_lock;
     60 
     61 int i915_global_buddy_init(void); /* XXX */
     62 
     63 static int
     64 i915drmkms_init(void)
     65 {
     66 	int error;
     67 
     68 	error = drm_guarantee_initialized();
     69 	if (error)
     70 		return error;
     71 
     72 	/* XXX errno Linux->NetBSD */
     73 	error = -i915_globals_init();
     74 	if (error)
     75 		return error;
     76 
     77 	drm_sysctl_init(&i915_def);
     78 	spin_lock_init(&mchdev_lock);
     79 	spin_lock_init(&i915_sw_fence_lock);
     80 	spin_lock_init(i915_schedule_lock);
     81 
     82 	return 0;
     83 }
     84 
     85 int	i915drmkms_guarantee_initialized(void); /* XXX */
     86 int
     87 i915drmkms_guarantee_initialized(void)
     88 {
     89 #ifdef _MODULE
     90 	return 0;
     91 #else
     92 	static ONCE_DECL(i915drmkms_init_once);
     93 
     94 	return RUN_ONCE(&i915drmkms_init_once, &i915drmkms_init);
     95 #endif
     96 }
     97 
     98 static void
     99 i915drmkms_fini(void)
    100 {
    101 
    102 	spin_lock_destroy(i915_schedule_lock);
    103 	spin_lock_destroy(&i915_sw_fence_lock);
    104 	spin_lock_destroy(&mchdev_lock);
    105 	drm_sysctl_fini(&i915_def);
    106 
    107 	i915_globals_exit();
    108 }
    109 
    110 static int
    111 i915drmkms_modcmd(modcmd_t cmd, void *arg __unused)
    112 {
    113 	int error;
    114 
    115 	switch (cmd) {
    116 	case MODULE_CMD_INIT:
    117 		/* XXX Kludge it up...  Must happen before attachment.  */
    118 #ifdef _MODULE
    119 		error = i915drmkms_init();
    120 #else
    121 		error = i915drmkms_guarantee_initialized();
    122 #endif
    123 		if (error) {
    124 			aprint_error("i915drmkms: failed to initialize: %d\n",
    125 			    error);
    126 			return error;
    127 		}
    128 #ifdef _MODULE
    129 		error = config_init_component(cfdriver_ioconf_i915drmkms,
    130 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
    131 		if (error) {
    132 			aprint_error("i915drmkms: failed to init component"
    133 			    ": %d\n", error);
    134 			i915drmkms_fini();
    135 			return error;
    136 		}
    137 #endif
    138 		return 0;
    139 
    140 	case MODULE_CMD_FINI:
    141 #ifdef _MODULE
    142 		error = config_fini_component(cfdriver_ioconf_i915drmkms,
    143 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
    144 		if (error) {
    145 			aprint_error("i915drmkms: failed to fini component"
    146 			    ": %d\n", error);
    147 			return error;
    148 		}
    149 #endif
    150 		i915drmkms_fini();
    151 		return 0;
    152 
    153 	default:
    154 		return ENOTTY;
    155 	}
    156 }
    157