xref: /netbsd-src/sys/external/bsd/drm2/amdgpu/amdgpu_module.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: amdgpu_module.c,v 1.4 2018/08/28 03:35:08 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.4 2018/08/28 03:35:08 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, "drmkms,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 	int error;
64 
65 	error = drm_guarantee_initialized();
66 	if (error)
67 		return error;
68 
69 	amdgpu_drm_driver->num_ioctls = amdgpu_max_kms_ioctl;
70 	amdgpu_drm_driver->driver_features |= DRIVER_MODESET;
71 
72 #if notyet			/* XXX amdgpu acpi */
73 	amdgpu_register_atpx_handler();
74 #endif
75 
76 	amdgpu_amdkfd_init();
77 	drm_sysctl_init(&amdgpu_def);
78 
79 	return 0;
80 }
81 
82 int	amdgpu_guarantee_initialized(void); /* XXX */
83 int
84 amdgpu_guarantee_initialized(void)
85 {
86 #ifdef _MODULE
87 	return 0;
88 #else
89 	static ONCE_DECL(amdgpu_init_once);
90 
91 	return RUN_ONCE(&amdgpu_init_once, &amdgpu_init);
92 #endif
93 }
94 
95 static void
96 amdgpu_fini(void)
97 {
98 
99 	drm_sysctl_fini(&amdgpu_def);
100 	amdgpu_amdkfd_fini();
101 #if notyet			/* XXX amdgpu acpi */
102 	amdgpu_unregister_atpx_handler();
103 #endif
104 }
105 
106 static int
107 amdgpu_modcmd(modcmd_t cmd, void *arg __unused)
108 {
109 	int error;
110 
111 	switch (cmd) {
112 	case MODULE_CMD_INIT:
113 		/* XXX Kludge it up...  Must happen before attachment.  */
114 #ifdef _MODULE
115 		error = amdgpu_init();
116 #else
117 		error = amdgpu_guarantee_initialized();
118 #endif
119 		if (error) {
120 			aprint_error("amdgpu: failed to initialize: %d\n",
121 			    error);
122 			return error;
123 		}
124 #ifdef _MODULE
125 		error = config_init_component(cfdriver_ioconf_amdgpu,
126 		    cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
127 		if (error) {
128 			aprint_error("amdgpu: failed to init component"
129 			    ": %d\n", error);
130 			amdgpu_fini();
131 			return error;
132 		}
133 #endif
134 		return 0;
135 
136 	case MODULE_CMD_FINI:
137 #ifdef _MODULE
138 		error = config_fini_component(cfdriver_ioconf_amdgpu,
139 		    cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
140 		if (error) {
141 			aprint_error("amdgpu: failed to fini component"
142 			    ": %d\n", error);
143 			return error;
144 		}
145 #endif
146 		amdgpu_fini();
147 		return 0;
148 
149 	default:
150 		return ENOTTY;
151 	}
152 }
153