xref: /netbsd-src/sys/dev/acpi/acpi_debug.c (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /* $NetBSD: acpi_debug.c,v 1.6 2019/01/05 20:40:26 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: acpi_debug.c,v 1.6 2019/01/05 20:40:26 christos Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 
35 #include <dev/acpi/acpireg.h>
36 #include <dev/acpi/acpivar.h>
37 
38 #include <prop/proplib.h>
39 
40 #ifdef ACPI_DEBUG
41 
42 #define _COMPONENT          ACPI_UTILITIES
43 ACPI_MODULE_NAME            ("acpi_debug")
44 
45 #define ACPI_DEBUG_MAX  64
46 #define ACPI_DEBUG_NONE  0
47 
48 #define ACPI_DEBUG_ADD(d, x)						      \
49 	do {								      \
50 		(void)prop_dictionary_set_uint32(d, #x, x);		      \
51 									      \
52 	} while (/* CONSTCOND */ 0)
53 
54 
55 static prop_dictionary_t acpi_debug_layer_d;
56 static prop_dictionary_t acpi_debug_level_d;
57 static char              acpi_debug_layer_s[ACPI_DEBUG_MAX];
58 static char              acpi_debug_level_s[ACPI_DEBUG_MAX];
59 
60 static int               acpi_debug_create(void);
61 static const char       *acpi_debug_getkey(prop_dictionary_t, uint32_t);
62 static int               acpi_debug_sysctl_layer(SYSCTLFN_PROTO);
63 static int               acpi_debug_sysctl_level(SYSCTLFN_PROTO);
64 
65 void
66 acpi_debug_init(void)
67 {
68 	const struct sysctlnode *rnode;
69 	const char *layer, *level;
70 	int rv;
71 
72 	KASSERT(acpi_debug_layer_d == NULL);
73 	KASSERT(acpi_debug_level_d == NULL);
74 
75 	rv = acpi_debug_create();
76 
77 	if (rv != 0)
78 		goto fail;
79 
80 	rv = sysctl_createv(NULL, 0, NULL, &rnode,
81 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
82 	    NULL, NULL, 0, NULL, 0,
83 	    CTL_HW, CTL_CREATE, CTL_EOL);
84 
85 	if (rv != 0)
86 		goto fail;
87 
88 	rv = sysctl_createv(NULL, 0, &rnode, &rnode,
89 	    0, CTLTYPE_NODE, "debug",
90 	    SYSCTL_DESCR("ACPI debug subtree"),
91 	    NULL, 0, NULL, 0,
92 	    CTL_CREATE, CTL_EOL);
93 
94 	if (rv != 0)
95 		goto fail;
96 
97 	rv = sysctl_createv(NULL, 0, &rnode, NULL,
98 	    CTLFLAG_READWRITE, CTLTYPE_STRING, "layer",
99 	    SYSCTL_DESCR("ACPI debug layer"),
100 	    acpi_debug_sysctl_layer, 0, acpi_debug_layer_s, ACPI_DEBUG_MAX,
101 	    CTL_CREATE, CTL_EOL);
102 
103 	if (rv != 0)
104 		goto fail;
105 
106 	rv = sysctl_createv(NULL, 0, &rnode, NULL,
107 	    CTLFLAG_READWRITE, CTLTYPE_STRING, "level",
108 	    SYSCTL_DESCR("ACPI debug level"),
109 	    acpi_debug_sysctl_level, 0, acpi_debug_level_s, ACPI_DEBUG_MAX,
110 	    CTL_CREATE, CTL_EOL);
111 
112 	if (rv != 0)
113 		goto fail;
114 
115 	rv = sysctl_createv(NULL, 0, &rnode, NULL,
116 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "object",
117 	    SYSCTL_DESCR("ACPI debug object"),
118 	    NULL, 0, &AcpiGbl_EnableAmlDebugObject, 0,
119 	    CTL_CREATE, CTL_EOL);
120 
121 	if (rv != 0)
122 		goto fail;
123 
124 	layer = acpi_debug_getkey(acpi_debug_layer_d, AcpiDbgLayer);
125 	level = acpi_debug_getkey(acpi_debug_level_d, AcpiDbgLevel);
126 
127 	(void)memcpy(acpi_debug_layer_s, layer, ACPI_DEBUG_MAX);
128 	(void)memcpy(acpi_debug_level_s, level, ACPI_DEBUG_MAX);
129 
130 	return;
131 
132 fail:
133 	aprint_error("acpi0: failed to initialize ACPI debug\n");
134 }
135 
136 static int
137 acpi_debug_create(void)
138 {
139 
140 	acpi_debug_layer_d = prop_dictionary_create();
141 	acpi_debug_level_d = prop_dictionary_create();
142 
143 	KASSERT(acpi_debug_layer_d != NULL);
144 	KASSERT(acpi_debug_level_d != NULL);
145 
146 	/*
147 	 * General components.
148 	 */
149 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_UTILITIES);
150 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_HARDWARE);
151 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EVENTS);
152 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_TABLES);
153 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_NAMESPACE);
154 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_PARSER);
155 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DISPATCHER);
156 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EXECUTER);
157 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_RESOURCES);
158 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_CA_DEBUGGER);
159 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_OS_SERVICES);
160 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_CA_DISASSEMBLER);
161 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_COMPILER);
162 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_TOOLS);
163 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EXAMPLE);
164 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DRIVER);
165 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_ALL_COMPONENTS);
166 
167 	/*
168 	 * NetBSD specific components.
169 	 */
170 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_BUS_COMPONENT);
171 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_ACAD_COMPONENT);
172 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_BAT_COMPONENT);
173 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_BUTTON_COMPONENT);
174 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EC_COMPONENT);
175 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_LID_COMPONENT);
176 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_RESOURCE_COMPONENT);
177 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_TZ_COMPONENT);
178 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DISPLAY_COMPONENT);
179 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_ALL_DRIVERS);
180 
181 	/*
182 	 * Debug levels.
183 	 */
184 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INIT);
185 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_DEBUG_OBJECT);
186 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INFO);
187 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_ALL_EXCEPTIONS);
188 
189 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INIT_NAMES);
190 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_PARSE);
191 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_LOAD);
192 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_DISPATCH);
193 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_EXEC);
194 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_NAMES);
195 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_OPREGION);
196 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_BFIELD);
197 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_TABLES);
198 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VALUES);
199 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_OBJECTS);
200 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_RESOURCES);
201 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_USER_REQUESTS);
202 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_PACKAGE);
203 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSITY1);
204 
205 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_ALLOCATIONS);
206 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_FUNCTIONS);
207 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_OPTIMIZATIONS);
208 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSITY2);
209 
210 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_MUTEX);
211 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_THREADS);
212 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_IO);
213 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INTERRUPTS);
214 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSITY3);
215 
216 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_AML_DISASSEMBLE);
217 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSE_INFO);
218 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_FULL_TABLES);
219 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_EVENTS);
220 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSE);
221 
222 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_NORMAL_DEFAULT);
223 
224 	/*
225 	 * The default debug level.
226 	 */
227 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_DEBUG_DEFAULT);
228 
229 	/*
230 	 * A custom ACPI_DEBUG_NONE disables debugging.
231 	 */
232 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DEBUG_NONE);
233 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_DEBUG_NONE);
234 
235 	prop_dictionary_make_immutable(acpi_debug_layer_d);
236 	prop_dictionary_make_immutable(acpi_debug_level_d);
237 
238 	return 0;
239 }
240 
241 static const char *
242 acpi_debug_getkey(prop_dictionary_t dict, uint32_t arg)
243 {
244 	prop_object_iterator_t i;
245 	prop_object_t obj, val;
246 	const char *key;
247 	uint32_t num;
248 
249 	i = prop_dictionary_iterator(dict);
250 
251 	while ((obj = prop_object_iterator_next(i)) != NULL) {
252 
253 		key = prop_dictionary_keysym_cstring_nocopy(obj);
254 		val = prop_dictionary_get(dict, key);
255 		num = prop_number_unsigned_integer_value(val);
256 
257 		if (arg == num)
258 			return key;
259 	}
260 
261 	return "UNKNOWN";
262 }
263 
264 static int
265 acpi_debug_sysctl_layer(SYSCTLFN_ARGS)
266 {
267 	char buf[ACPI_DEBUG_MAX];
268 	struct sysctlnode node;
269 	prop_object_t obj;
270 	int error;
271 
272 	node = *rnode;
273 	node.sysctl_data = buf;
274 
275 	(void)memcpy(node.sysctl_data, rnode->sysctl_data, ACPI_DEBUG_MAX);
276 
277 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
278 
279 	if (error || newp == NULL)
280 		return error;
281 
282 	obj = prop_dictionary_get(acpi_debug_layer_d, node.sysctl_data);
283 
284 	if (obj == NULL)
285 		return EINVAL;
286 
287 	AcpiDbgLayer = prop_number_unsigned_integer_value(obj);
288 
289 	(void)memcpy(rnode->sysctl_data, node.sysctl_data, ACPI_DEBUG_MAX);
290 
291 	return 0;
292 }
293 
294 static int
295 acpi_debug_sysctl_level(SYSCTLFN_ARGS)
296 {
297 	char buf[ACPI_DEBUG_MAX];
298 	struct sysctlnode node;
299 	prop_object_t obj;
300 	int error;
301 
302 	node = *rnode;
303 	node.sysctl_data = buf;
304 
305 	(void)memcpy(node.sysctl_data, rnode->sysctl_data, ACPI_DEBUG_MAX);
306 
307 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
308 
309 	if (error || newp == NULL)
310 		return error;
311 
312 	obj = prop_dictionary_get(acpi_debug_level_d, node.sysctl_data);
313 
314 	if (obj == NULL)
315 		return EINVAL;
316 
317 	AcpiDbgLevel = prop_number_unsigned_integer_value(obj);
318 
319 	(void)memcpy(rnode->sysctl_data, node.sysctl_data, ACPI_DEBUG_MAX);
320 
321 	return 0;
322 }
323 
324 #endif	/* ACPI_DEBUG */
325