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