1 1.32 riastrad /* $NetBSD: drm_module.c,v 1.32 2023/09/05 20:15:10 riastradh Exp $ */ 2 1.2 riastrad 3 1.2 riastrad /*- 4 1.2 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 1.2 riastrad * All rights reserved. 6 1.2 riastrad * 7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.2 riastrad * by Taylor R. Campbell. 9 1.2 riastrad * 10 1.2 riastrad * Redistribution and use in source and binary forms, with or without 11 1.2 riastrad * modification, are permitted provided that the following conditions 12 1.2 riastrad * are met: 13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.2 riastrad * notice, this list of conditions and the following disclaimer. 15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.2 riastrad * documentation and/or other materials provided with the distribution. 18 1.2 riastrad * 19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.2 riastrad */ 31 1.2 riastrad 32 1.2 riastrad #include <sys/cdefs.h> 33 1.32 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_module.c,v 1.32 2023/09/05 20:15:10 riastradh Exp $"); 34 1.2 riastrad 35 1.2 riastrad #include <sys/types.h> 36 1.12 riastrad #include <sys/condvar.h> 37 1.8 riastrad #include <sys/conf.h> 38 1.2 riastrad #include <sys/device.h> 39 1.2 riastrad #include <sys/module.h> 40 1.12 riastrad #include <sys/mutex.h> 41 1.6 riastrad #include <sys/once.h> 42 1.4 riastrad #include <sys/reboot.h> 43 1.2 riastrad #include <sys/systm.h> 44 1.2 riastrad 45 1.6 riastrad #include <linux/mutex.h> 46 1.6 riastrad 47 1.23 riastrad #include <drm/drm_agpsupport.h> 48 1.22 riastrad #include <drm/drm_bridge.h> 49 1.24 riastrad #include <drm/drm_device.h> 50 1.10 riastrad #include <drm/drm_encoder_slave.h> 51 1.17 jmcneill #include <drm/drm_panel.h> 52 1.23 riastrad #include <drm/drm_print.h> 53 1.24 riastrad #include <drm/drm_sysctl.h> 54 1.2 riastrad 55 1.19 riastrad #include "../dist/drm/drm_crtc_internal.h" 56 1.18 riastrad #include "../dist/drm/drm_internal.h" 57 1.18 riastrad 58 1.2 riastrad /* 59 1.15 riastrad * XXX This is stupid. 60 1.15 riastrad * 61 1.15 riastrad * 1. Builtin modules are broken: they don't get initialized before 62 1.15 riastrad * autoconf matches devices, but we need the initialization to be 63 1.15 riastrad * run in order to match and attach drmkms drivers. 64 1.15 riastrad * 65 1.15 riastrad * 2. The following dependencies are _not_ correct: 66 1.15 riastrad * - drmkms can't depend on agp because not all drmkms drivers run 67 1.15 riastrad * on platforms guaranteed to have pci, let alone agp 68 1.15 riastrad * - drmkms_pci can't depend on agp because not all _pci_ has agp 69 1.15 riastrad * (e.g., tegra) 70 1.15 riastrad * - radeon (e.g.) can't depend on agp because not all radeon 71 1.15 riastrad * devices are on platforms guaranteed to have agp 72 1.15 riastrad * 73 1.15 riastrad * 3. We need to register the agp hooks before we try to attach a 74 1.15 riastrad * device. 75 1.15 riastrad * 76 1.15 riastrad * 4. The only mechanism we have to force this is the 77 1.15 riastrad * mumblefrotz_guarantee_initialized kludge. 78 1.15 riastrad * 79 1.15 riastrad * 5. We don't know if we even _can_ call 80 1.15 riastrad * drmkms_agp_guarantee_initialized unless we know NAGP. 81 1.15 riastrad * 82 1.15 riastrad * 6. We don't know NAGP unless we include "agp.h". 83 1.15 riastrad * 84 1.15 riastrad * 7. We can't include "agp.h" if the platform has agp. 85 1.15 riastrad * 86 1.15 riastrad * 8. The way we determine whether we have agp is NAGP. 87 1.15 riastrad * 88 1.15 riastrad * 9. @!*#&^@&*@!&^#@ 89 1.15 riastrad */ 90 1.15 riastrad #if defined(__powerpc__) || defined(__i386__) || defined(__x86_64__) 91 1.15 riastrad #include "agp.h" 92 1.32 riastrad #include "drmkms_pci.h" 93 1.15 riastrad #endif 94 1.15 riastrad 95 1.15 riastrad /* 96 1.2 riastrad * XXX I2C stuff should be moved to a separate drmkms_i2c module. 97 1.2 riastrad */ 98 1.31 riastrad MODULE(MODULE_CLASS_DRIVER, drmkms, "drmkms_linux,sysmon_power"); 99 1.2 riastrad 100 1.6 riastrad struct mutex drm_global_mutex; 101 1.6 riastrad 102 1.9 christos struct drm_sysctl_def drm_def = DRM_SYSCTL_INIT(); 103 1.9 christos 104 1.6 riastrad static int 105 1.6 riastrad drm_init(void) 106 1.6 riastrad { 107 1.6 riastrad extern int linux_guarantee_initialized(void); 108 1.6 riastrad int error; 109 1.6 riastrad 110 1.6 riastrad error = linux_guarantee_initialized(); 111 1.6 riastrad if (error) 112 1.6 riastrad return error; 113 1.6 riastrad 114 1.26 riastrad extern bool drm_core_init_complete; 115 1.26 riastrad drm_core_init_complete = true; 116 1.26 riastrad 117 1.15 riastrad drm_agp_hooks_init(); 118 1.32 riastrad #if NDRMKMS_PCI > 0 && NAGP > 0 119 1.15 riastrad extern int drmkms_agp_guarantee_initialized(void); 120 1.15 riastrad error = drmkms_agp_guarantee_initialized(); 121 1.15 riastrad if (error) { 122 1.15 riastrad drm_agp_hooks_fini(); 123 1.15 riastrad return error; 124 1.15 riastrad } 125 1.15 riastrad #endif 126 1.15 riastrad 127 1.6 riastrad if (ISSET(boothowto, AB_DEBUG)) 128 1.29 riastrad __drm_debug = DRM_UT_DRIVER; 129 1.6 riastrad 130 1.6 riastrad spin_lock_init(&drm_minor_lock); 131 1.6 riastrad idr_init(&drm_minors_idr); 132 1.27 riastrad _init_srcu_struct(&drm_unplug_srcu, "drmunplg"); 133 1.6 riastrad linux_mutex_init(&drm_global_mutex); 134 1.21 riastrad linux_mutex_init(&drm_kernel_fb_helper_lock); 135 1.6 riastrad drm_connector_ida_init(); 136 1.17 jmcneill drm_panel_init_lock(); 137 1.17 jmcneill drm_bridge_init_lock(); 138 1.9 christos drm_sysctl_init(&drm_def); 139 1.10 riastrad drm_i2c_encoders_init(); 140 1.6 riastrad 141 1.6 riastrad return 0; 142 1.6 riastrad } 143 1.6 riastrad 144 1.6 riastrad int 145 1.6 riastrad drm_guarantee_initialized(void) 146 1.6 riastrad { 147 1.5 riastrad #ifdef _MODULE 148 1.6 riastrad return 0; 149 1.5 riastrad #else 150 1.6 riastrad static ONCE_DECL(drm_init_once); 151 1.6 riastrad 152 1.6 riastrad return RUN_ONCE(&drm_init_once, &drm_init); 153 1.2 riastrad #endif 154 1.6 riastrad } 155 1.6 riastrad 156 1.6 riastrad static void 157 1.6 riastrad drm_fini(void) 158 1.6 riastrad { 159 1.10 riastrad 160 1.10 riastrad drm_i2c_encoders_fini(); 161 1.9 christos drm_sysctl_fini(&drm_def); 162 1.17 jmcneill drm_bridge_fini_lock(); 163 1.17 jmcneill drm_panel_fini_lock(); 164 1.6 riastrad drm_connector_ida_destroy(); 165 1.21 riastrad linux_mutex_destroy(&drm_kernel_fb_helper_lock); 166 1.6 riastrad linux_mutex_destroy(&drm_global_mutex); 167 1.27 riastrad cleanup_srcu_struct(&drm_unplug_srcu); 168 1.6 riastrad idr_destroy(&drm_minors_idr); 169 1.6 riastrad spin_lock_destroy(&drm_minor_lock); 170 1.15 riastrad drm_agp_hooks_fini(); 171 1.6 riastrad } 172 1.2 riastrad 173 1.12 riastrad int 174 1.12 riastrad drm_irq_by_busid(struct drm_device *dev, void *data, struct drm_file *file) 175 1.12 riastrad { 176 1.12 riastrad 177 1.12 riastrad return -ENODEV; 178 1.12 riastrad } 179 1.12 riastrad 180 1.2 riastrad static int 181 1.2 riastrad drmkms_modcmd(modcmd_t cmd, void *arg __unused) 182 1.2 riastrad { 183 1.2 riastrad #ifdef _MODULE 184 1.2 riastrad devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR; 185 1.6 riastrad #endif 186 1.2 riastrad int error; 187 1.2 riastrad 188 1.2 riastrad switch (cmd) { 189 1.2 riastrad case MODULE_CMD_INIT: 190 1.2 riastrad #ifdef _MODULE 191 1.6 riastrad error = drm_init(); 192 1.6 riastrad #else 193 1.6 riastrad error = drm_guarantee_initialized(); 194 1.6 riastrad #endif 195 1.6 riastrad if (error) 196 1.6 riastrad return error; 197 1.6 riastrad #ifdef _MODULE 198 1.2 riastrad error = devsw_attach("drm", NULL, &bmajor, 199 1.2 riastrad &drm_cdevsw, &cmajor); 200 1.2 riastrad if (error) { 201 1.2 riastrad aprint_error("drmkms: unable to attach devsw: %d\n", 202 1.2 riastrad error); 203 1.6 riastrad return error; 204 1.2 riastrad } 205 1.2 riastrad #endif 206 1.2 riastrad return 0; 207 1.2 riastrad 208 1.2 riastrad case MODULE_CMD_FINI: 209 1.2 riastrad #ifdef _MODULE 210 1.30 riastrad devsw_detach(NULL, &drm_cdevsw); 211 1.2 riastrad #endif 212 1.6 riastrad drm_fini(); 213 1.2 riastrad return 0; 214 1.2 riastrad 215 1.2 riastrad default: 216 1.2 riastrad return ENOTTY; 217 1.2 riastrad } 218 1.2 riastrad } 219