1 /* $NetBSD: vmwgfx_module.c,v 1.2 2022/07/17 15:37:10 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2022 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_module.c,v 1.2 2022/07/17 15:37:10 riastradh Exp $");
31
32 #include <sys/types.h>
33 #include <sys/module.h>
34 #ifndef _MODULE
35 #include <sys/once.h>
36 #endif
37 #include <sys/systm.h>
38
39 #include <drm/drm_device.h>
40 #include <drm/drm_drv.h>
41 #include <drm/drm_sysctl.h>
42
43 #include "vmwgfx_drv.h"
44
45 MODULE(MODULE_CLASS_DRIVER, vmwgfx, "drmkms,drmkms_pci,drmkms_ttm"); /* XXX drmkms_i2c */
46
47 #ifdef _MODULE
48 #include "ioconf.c"
49 #endif
50
51 struct drm_sysctl_def vmwgfx_def = DRM_SYSCTL_INIT();
52
53 static int
vmwgfx_init(void)54 vmwgfx_init(void)
55 {
56 int error;
57
58 error = drm_guarantee_initialized();
59 if (error)
60 return error;
61
62 drm_sysctl_init(&vmwgfx_def);
63
64 return 0;
65 }
66
67 int vmwgfx_guarantee_initialized(void); /* XXX */
68 int
vmwgfx_guarantee_initialized(void)69 vmwgfx_guarantee_initialized(void)
70 {
71 #ifdef _MODULE
72 return 0;
73 #else
74 static ONCE_DECL(vmwgfx_init_once);
75
76 return RUN_ONCE(&vmwgfx_init_once, &vmwgfx_init);
77 #endif
78 }
79
80 static void
vmwgfx_fini(void)81 vmwgfx_fini(void)
82 {
83
84 drm_sysctl_fini(&vmwgfx_def);
85 }
86
87 static int
vmwgfx_modcmd(modcmd_t cmd,void * arg __unused)88 vmwgfx_modcmd(modcmd_t cmd, void *arg __unused)
89 {
90 int error;
91
92 switch (cmd) {
93 case MODULE_CMD_INIT:
94 /* XXX Kludge it up... Must happen before attachment. */
95 #ifdef _MODULE
96 error = vmwgfx_init();
97 #else
98 error = vmwgfx_guarantee_initialized();
99 #endif
100 if (error) {
101 aprint_error("vmwgfx: failed to initialize: %d\n",
102 error);
103 return error;
104 }
105 #ifdef _MODULE
106 error = config_init_component(cfdriver_ioconf_vmwgfx,
107 cfattach_ioconf_vmwgfx, cfdata_ioconf_vmwgfx);
108 if (error) {
109 aprint_error("vmwgfx: failed to init component"
110 ": %d\n", error);
111 vmwgfx_fini();
112 return error;
113 }
114 #endif
115 return 0;
116
117 case MODULE_CMD_FINI:
118 #ifdef _MODULE
119 error = config_fini_component(cfdriver_ioconf_vmwgfx,
120 cfattach_ioconf_vmwgfx, cfdata_ioconf_vmwgfx);
121 if (error) {
122 aprint_error("vmwgfx: failed to fini component"
123 ": %d\n", error);
124 return error;
125 }
126 #endif
127 vmwgfx_fini();
128 return 0;
129
130 default:
131 return ENOTTY;
132 }
133 }
134