Home | History | Annotate | Line # | Download | only in drm
drm_module.c revision 1.16
      1 /*	$NetBSD: drm_module.c,v 1.16 2019/09/23 05:54:31 mrg 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: drm_module.c,v 1.16 2019/09/23 05:54:31 mrg Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/condvar.h>
     37 #include <sys/conf.h>
     38 #include <sys/device.h>
     39 #include <sys/module.h>
     40 #include <sys/mutex.h>
     41 #include <sys/once.h>
     42 #include <sys/reboot.h>
     43 #include <sys/systm.h>
     44 
     45 #include <linux/mutex.h>
     46 
     47 #include <drm/drmP.h>
     48 #include <drm/drm_encoder_slave.h>
     49 #include <drm/drm_internal.h>
     50 #include <drm/drm_sysctl.h>
     51 
     52 /*
     53  * XXX This is stupid.
     54  *
     55  * 1. Builtin modules are broken: they don't get initialized before
     56  *    autoconf matches devices, but we need the initialization to be
     57  *    run in order to match and attach drmkms drivers.
     58  *
     59  * 2. The following dependencies are _not_ correct:
     60  *    - drmkms can't depend on agp because not all drmkms drivers run
     61  *      on platforms guaranteed to have pci, let alone agp
     62  *    - drmkms_pci can't depend on agp because not all _pci_ has agp
     63  *      (e.g., tegra)
     64  *    - radeon (e.g.) can't depend on agp because not all radeon
     65  *      devices are on platforms guaranteed to have agp
     66  *
     67  * 3. We need to register the agp hooks before we try to attach a
     68  *    device.
     69  *
     70  * 4. The only mechanism we have to force this is the
     71  *    mumblefrotz_guarantee_initialized kludge.
     72  *
     73  * 5. We don't know if we even _can_ call
     74  *    drmkms_agp_guarantee_initialized unless we know NAGP.
     75  *
     76  * 6. We don't know NAGP unless we include "agp.h".
     77  *
     78  * 7. We can't include "agp.h" if the platform has agp.
     79  *
     80  * 8. The way we determine whether we have agp is NAGP.
     81  *
     82  * 9. @!*#&^@&*@!&^#@
     83  */
     84 #if defined(__powerpc__) || defined(__i386__) || defined(__x86_64__)
     85 #include "agp.h"
     86 #endif
     87 
     88 /*
     89  * XXX I2C stuff should be moved to a separate drmkms_i2c module.
     90  */
     91 MODULE(MODULE_CLASS_DRIVER, drmkms, "drmkms_linux");
     92 
     93 struct mutex	drm_global_mutex;
     94 
     95 struct drm_sysctl_def drm_def = DRM_SYSCTL_INIT();
     96 
     97 static int
     98 drm_init(void)
     99 {
    100 	extern int linux_guarantee_initialized(void);
    101 	int error;
    102 
    103 	error = linux_guarantee_initialized();
    104 	if (error)
    105 		return error;
    106 
    107 	drm_agp_hooks_init();
    108 #if NAGP > 0
    109 	extern int drmkms_agp_guarantee_initialized(void);
    110 	error = drmkms_agp_guarantee_initialized();
    111 	if (error) {
    112 		drm_agp_hooks_fini();
    113 		return error;
    114 	}
    115 #endif
    116 
    117 	if (ISSET(boothowto, AB_DEBUG))
    118 		drm_debug = DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS;
    119 
    120 	spin_lock_init(&drm_minor_lock);
    121 	idr_init(&drm_minors_idr);
    122 	linux_mutex_init(&drm_global_mutex);
    123 	drm_connector_ida_init();
    124 	drm_global_init();
    125 	drm_sysctl_init(&drm_def);
    126 	drm_i2c_encoders_init();
    127 
    128 	return 0;
    129 }
    130 
    131 int
    132 drm_guarantee_initialized(void)
    133 {
    134 #ifdef _MODULE
    135 	return 0;
    136 #else
    137 	static ONCE_DECL(drm_init_once);
    138 
    139 	return RUN_ONCE(&drm_init_once, &drm_init);
    140 #endif
    141 }
    142 
    143 static void
    144 drm_fini(void)
    145 {
    146 
    147 	drm_i2c_encoders_fini();
    148 	drm_sysctl_fini(&drm_def);
    149 	drm_global_release();
    150 	drm_connector_ida_destroy();
    151 	linux_mutex_destroy(&drm_global_mutex);
    152 	idr_destroy(&drm_minors_idr);
    153 	spin_lock_destroy(&drm_minor_lock);
    154 	drm_agp_hooks_fini();
    155 }
    156 
    157 int
    158 drm_irq_by_busid(struct drm_device *dev, void *data, struct drm_file *file)
    159 {
    160 
    161 	return -ENODEV;
    162 }
    163 
    164 static int
    165 drmkms_modcmd(modcmd_t cmd, void *arg __unused)
    166 {
    167 #ifdef _MODULE
    168 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
    169 #endif
    170 	int error;
    171 
    172 	switch (cmd) {
    173 	case MODULE_CMD_INIT:
    174 #ifdef _MODULE
    175 		error = drm_init();
    176 #else
    177 		error = drm_guarantee_initialized();
    178 #endif
    179 		if (error)
    180 			return error;
    181 #ifdef _MODULE
    182 		error = devsw_attach("drm", NULL, &bmajor,
    183 		    &drm_cdevsw, &cmajor);
    184 		if (error) {
    185 			aprint_error("drmkms: unable to attach devsw: %d\n",
    186 			    error);
    187 			return error;
    188 		}
    189 #endif
    190 		return 0;
    191 
    192 	case MODULE_CMD_FINI:
    193 #ifdef _MODULE
    194 		error = devsw_detach(NULL, &drm_cdevsw);
    195 		if (error)
    196 			return error;
    197 #endif
    198 		drm_fini();
    199 		return 0;
    200 
    201 	default:
    202 		return ENOTTY;
    203 	}
    204 }
    205