amdgpu_module.c revision 1.1 1 /* $NetBSD: amdgpu_module.c,v 1.1 2018/08/27 14:02:32 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2018 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: amdgpu_module.c,v 1.1 2018/08/27 14:02:32 riastradh 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 "amdgpu_amdkfd.h"
46 #include "amdgpu_drv.h"
47
48 MODULE(MODULE_CLASS_DRIVER, amdgpu, "amdgpu,drmkms_pci"); /* XXX drmkms_i2c, drmkms_ttm */
49
50 #ifdef _MODULE
51 #include "ioconf.c"
52 #endif
53
54 /* XXX Kludge to get these from amdgpu_drv.c. */
55 extern struct drm_driver *const amdgpu_drm_driver;
56 extern int amdgpu_max_kms_ioctl;
57
58 struct drm_sysctl_def amdgpu_def = DRM_SYSCTL_INIT();
59
60 static int
61 amdgpu_init(void)
62 {
63 extern int drm_guarantee_initialized(void);
64 int error;
65
66 error = drm_guarantee_initialized();
67 if (error)
68 return error;
69
70 amdgpu_drm_driver->num_ioctls = amdgpu_max_kms_ioctl;
71 amdgpu_drm_driver->driver_features |= DRIVER_MODESET;
72
73 #if notyet /* XXX amdgpu acpi */
74 amdgpu_register_atpx_handler();
75 #endif
76
77 amdgpu_amdkfd_init();
78
79 error = drm_pci_init(amdgpu_drm_driver, NULL);
80 if (error) {
81 aprint_error("amdgpu: failed to init pci: %d\n",
82 error);
83 return error;
84 }
85 drm_sysctl_init(&amdgpu_def);
86
87 return 0;
88 }
89
90 int amdgpu_guarantee_initialized(void); /* XXX */
91 int
92 amdgpu_guarantee_initialized(void)
93 {
94 #ifdef _MODULE
95 return 0;
96 #else
97 static ONCE_DECL(amdgpu_init_once);
98
99 return RUN_ONCE(&amdgpu_init_once, &amdgpu_init);
100 #endif
101 }
102
103 static void
104 amdgpu_fini(void)
105 {
106
107 drm_sysctl_fini(&amdgpu_def);
108 drm_pci_exit(amdgpu_drm_driver, NULL);
109 amdgpu_amdkfd_fini();
110 #if notyet /* XXX amdgpu acpi */
111 amdgpu_unregister_atpx_handler();
112 #endif
113 }
114
115 static int
116 amdgpu_modcmd(modcmd_t cmd, void *arg __unused)
117 {
118 int error;
119
120 switch (cmd) {
121 case MODULE_CMD_INIT:
122 /* XXX Kludge it up... Must happen before attachment. */
123 #ifdef _MODULE
124 error = amdgpu_init();
125 #else
126 error = amdgpu_guarantee_initialized();
127 #endif
128 if (error) {
129 aprint_error("amdgpu: failed to initialize: %d\n",
130 error);
131 return error;
132 }
133 #ifdef _MODULE
134 error = config_init_component(cfdriver_ioconf_amdgpu,
135 cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
136 if (error) {
137 aprint_error("amdgpu: failed to init component"
138 ": %d\n", error);
139 amdgpu_fini();
140 return error;
141 }
142 #endif
143 return 0;
144
145 case MODULE_CMD_FINI:
146 #ifdef _MODULE
147 error = config_fini_component(cfdriver_ioconf_amdgpu,
148 cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
149 if (error) {
150 aprint_error("amdgpu: failed to fini component"
151 ": %d\n", error);
152 return error;
153 }
154 #endif
155 amdgpu_fini();
156 return 0;
157
158 default:
159 return ENOTTY;
160 }
161 }
162