i915_module.c revision 1.5.18.1 1 /* $NetBSD: i915_module.c,v 1.5.18.1 2018/09/06 06:56:36 pgoyette 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.5.18.1 2018/09/06 06:56:36 pgoyette 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/drmP.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 /* XXX Kludge to get these from i915_drv.c. */
54 extern struct drm_driver *const i915_drm_driver;
55 extern const struct pci_device_id *const i915_device_ids;
56 extern const size_t i915_n_device_ids;
57
58 struct drm_sysctl_def i915_def = DRM_SYSCTL_INIT();
59
60 static int
61 i915drmkms_init(void)
62 {
63 int error;
64
65 error = drm_guarantee_initialized();
66 if (error)
67 return error;
68
69 i915_drm_driver->num_ioctls = i915_max_ioctl;
70 i915_drm_driver->driver_features |= DRIVER_MODESET;
71 i915_drm_driver->driver_features &= ~DRIVER_USE_AGP;
72
73 drm_sysctl_init(&i915_def);
74 spin_lock_init(&mchdev_lock);
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 spin_lock_destroy(&mchdev_lock);
97 drm_sysctl_fini(&i915_def);
98 }
99
100 static int
101 i915drmkms_modcmd(modcmd_t cmd, void *arg __unused)
102 {
103 int error;
104
105 switch (cmd) {
106 case MODULE_CMD_INIT:
107 /* XXX Kludge it up... Must happen before attachment. */
108 #ifdef _MODULE
109 error = i915drmkms_init();
110 #else
111 error = i915drmkms_guarantee_initialized();
112 #endif
113 if (error) {
114 aprint_error("i915drmkms: failed to initialize: %d\n",
115 error);
116 return error;
117 }
118 #ifdef _MODULE
119 error = config_init_component(cfdriver_ioconf_i915drmkms,
120 cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
121 if (error) {
122 aprint_error("i915drmkms: failed to init component"
123 ": %d\n", error);
124 i915drmkms_fini();
125 return error;
126 }
127 #endif
128 return 0;
129
130 case MODULE_CMD_FINI:
131 #ifdef _MODULE
132 error = config_fini_component(cfdriver_ioconf_i915drmkms,
133 cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
134 if (error) {
135 aprint_error("i915drmkms: failed to fini component"
136 ": %d\n", error);
137 return error;
138 }
139 #endif
140 i915drmkms_fini();
141 return 0;
142
143 default:
144 return ENOTTY;
145 }
146 }
147