Home | History | Annotate | Line # | Download | only in i915drm
i915_module.c revision 1.12
      1 /*	$NetBSD: i915_module.c,v 1.12 2021/12/19 11:26:35 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.12 2021/12/19 11:26:35 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 
     47 MODULE(MODULE_CLASS_DRIVER, i915drmkms, "drmkms,drmkms_pci"); /* XXX drmkms_i2c */
     48 
     49 #ifdef _MODULE
     50 #include "ioconf.c"
     51 #endif
     52 
     53 struct drm_sysctl_def i915_def = DRM_SYSCTL_INIT();
     54 
     55 int i915_global_buddy_init(void); /* XXX */
     56 void i915_gem_client_blt_init(void); /* XXX */
     57 void i915_gem_client_blt_fini(void); /* XXX */
     58 
     59 static int
     60 i915drmkms_init(void)
     61 {
     62 	int error;
     63 
     64 	error = drm_guarantee_initialized();
     65 	if (error)
     66 		return error;
     67 
     68 	error = -i915_global_buddy_init();
     69 	if (error)
     70 		return error;
     71 
     72 	drm_sysctl_init(&i915_def);
     73 	spin_lock_init(&mchdev_lock);
     74 	i915_gem_client_blt_init();
     75 
     76 	return 0;
     77 }
     78 
     79 int	i915drmkms_guarantee_initialized(void); /* XXX */
     80 int
     81 i915drmkms_guarantee_initialized(void)
     82 {
     83 #ifdef _MODULE
     84 	return 0;
     85 #else
     86 	static ONCE_DECL(i915drmkms_init_once);
     87 
     88 	return RUN_ONCE(&i915drmkms_init_once, &i915drmkms_init);
     89 #endif
     90 }
     91 
     92 static void
     93 i915drmkms_fini(void)
     94 {
     95 
     96 	i915_gem_client_blt_fini();
     97 	spin_lock_destroy(&mchdev_lock);
     98 	drm_sysctl_fini(&i915_def);
     99 }
    100 
    101 static int
    102 i915drmkms_modcmd(modcmd_t cmd, void *arg __unused)
    103 {
    104 	int error;
    105 
    106 	switch (cmd) {
    107 	case MODULE_CMD_INIT:
    108 		/* XXX Kludge it up...  Must happen before attachment.  */
    109 #ifdef _MODULE
    110 		error = i915drmkms_init();
    111 #else
    112 		error = i915drmkms_guarantee_initialized();
    113 #endif
    114 		if (error) {
    115 			aprint_error("i915drmkms: failed to initialize: %d\n",
    116 			    error);
    117 			return error;
    118 		}
    119 #ifdef _MODULE
    120 		error = config_init_component(cfdriver_ioconf_i915drmkms,
    121 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
    122 		if (error) {
    123 			aprint_error("i915drmkms: failed to init component"
    124 			    ": %d\n", error);
    125 			i915drmkms_fini();
    126 			return error;
    127 		}
    128 #endif
    129 		return 0;
    130 
    131 	case MODULE_CMD_FINI:
    132 #ifdef _MODULE
    133 		error = config_fini_component(cfdriver_ioconf_i915drmkms,
    134 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
    135 		if (error) {
    136 			aprint_error("i915drmkms: failed to fini component"
    137 			    ": %d\n", error);
    138 			return error;
    139 		}
    140 #endif
    141 		i915drmkms_fini();
    142 		return 0;
    143 
    144 	default:
    145 		return ENOTTY;
    146 	}
    147 }
    148