xref: /netbsd-src/sys/dev/acpi/acpi_debug.c (revision 5c46dd73a9bcb28b2994504ea090f64066b17a77)
1 /* $NetBSD: acpi_debug.c,v 1.2 2010/04/12 12:14:26 jruoho 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.2 2010/04/12 12:14:26 jruoho 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, "hw",
82 	    NULL, NULL, 0, NULL, 0,
83 	    CTL_HW, CTL_EOL);
84 
85 	if (rv != 0)
86 		goto fail;
87 
88 	rv = sysctl_createv(NULL, 0, &rnode, &rnode,
89 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
90 	    NULL, NULL, 0, NULL, 0,
91 	    CTL_CREATE, CTL_EOL);
92 
93 	if (rv != 0)
94 		goto fail;
95 
96 	rv = sysctl_createv(NULL, 0, &rnode, &rnode,
97 	    0, CTLTYPE_NODE, "debug",
98 	    SYSCTL_DESCR("ACPI debug subtree"),
99 	    NULL, 0, NULL, 0,
100 	    CTL_CREATE, CTL_EOL);
101 
102 	if (rv != 0)
103 		goto fail;
104 
105 	rv = sysctl_createv(NULL, 0, &rnode, NULL,
106 	    CTLFLAG_READWRITE, CTLTYPE_STRING, "layer",
107 	    SYSCTL_DESCR("ACPI debug layer"),
108 	    acpi_debug_sysctl_layer, 0, acpi_debug_layer_s, ACPI_DEBUG_MAX,
109 	    CTL_CREATE, CTL_EOL);
110 
111 	if (rv != 0)
112 		goto fail;
113 
114 	rv = sysctl_createv(NULL, 0, &rnode, NULL,
115 	    CTLFLAG_READWRITE, CTLTYPE_STRING, "level",
116 	    SYSCTL_DESCR("ACPI debug level"),
117 	    acpi_debug_sysctl_level, 0, acpi_debug_level_s, ACPI_DEBUG_MAX,
118 	    CTL_CREATE, CTL_EOL);
119 
120 	if (rv != 0)
121 		goto fail;
122 
123 	layer = acpi_debug_getkey(acpi_debug_layer_d, AcpiDbgLayer);
124 	level = acpi_debug_getkey(acpi_debug_level_d, AcpiDbgLevel);
125 
126 	(void)memcpy(acpi_debug_layer_s, layer, ACPI_DEBUG_MAX);
127 	(void)memcpy(acpi_debug_level_s, level, ACPI_DEBUG_MAX);
128 
129 	return;
130 
131 fail:
132 	aprint_error("acpi0: failed to initialize ACPI debug\n");
133 }
134 
135 static int
136 acpi_debug_create(void)
137 {
138 
139 	acpi_debug_layer_d = prop_dictionary_create();
140 	acpi_debug_level_d = prop_dictionary_create();
141 
142 	KASSERT(acpi_debug_layer_d != NULL);
143 	KASSERT(acpi_debug_level_d != NULL);
144 
145 	/*
146 	 * General components.
147 	 */
148 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_UTILITIES);
149 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_HARDWARE);
150 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EVENTS);
151 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_TABLES);
152 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_NAMESPACE);
153 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_PARSER);
154 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DISPATCHER);
155 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EXECUTER);
156 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_RESOURCES);
157 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_CA_DEBUGGER);
158 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_OS_SERVICES);
159 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_CA_DISASSEMBLER);
160 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_COMPILER);
161 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_TOOLS);
162 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EXAMPLE);
163 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DRIVER);
164 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_ALL_COMPONENTS);
165 
166 	/*
167 	 * NetBSD specific components.
168 	 */
169 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_BUS_COMPONENT);
170 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_ACAD_COMPONENT);
171 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_BAT_COMPONENT);
172 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_BUTTON_COMPONENT);
173 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_EC_COMPONENT);
174 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_LID_COMPONENT);
175 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_RESOURCE_COMPONENT);
176 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_TZ_COMPONENT);
177 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_ALL_DRIVERS);
178 
179 	/*
180 	 * Debug levels.
181 	 */
182 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INIT);
183 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_DEBUG_OBJECT);
184 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INFO);
185 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_ALL_EXCEPTIONS);
186 
187 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INIT_NAMES);
188 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_PARSE);
189 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_LOAD);
190 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_DISPATCH);
191 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_EXEC);
192 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_NAMES);
193 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_OPREGION);
194 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_BFIELD);
195 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_TABLES);
196 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VALUES);
197 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_OBJECTS);
198 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_RESOURCES);
199 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_USER_REQUESTS);
200 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_PACKAGE);
201 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSITY1);
202 
203 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_ALLOCATIONS);
204 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_FUNCTIONS);
205 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_OPTIMIZATIONS);
206 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSITY2);
207 
208 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_MUTEX);
209 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_THREADS);
210 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_IO);
211 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_INTERRUPTS);
212 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSITY3);
213 
214 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_AML_DISASSEMBLE);
215 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSE_INFO);
216 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_FULL_TABLES);
217 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_EVENTS);
218 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_LV_VERBOSE);
219 
220 	/*
221 	 * The default debug level.
222 	 */
223 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_DEBUG_DEFAULT);
224 
225 	/*
226 	 * A custom ACPI_DEBUG_NONE disables debugging.
227 	 */
228 	ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DEBUG_NONE);
229 	ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_DEBUG_NONE);
230 
231 	prop_dictionary_make_immutable(acpi_debug_layer_d);
232 	prop_dictionary_make_immutable(acpi_debug_level_d);
233 
234 	return 0;
235 }
236 
237 static const char *
238 acpi_debug_getkey(prop_dictionary_t dict, uint32_t arg)
239 {
240 	prop_object_iterator_t i;
241 	prop_object_t obj, val;
242 	const char *key;
243 	uint32_t num;
244 
245 	i = prop_dictionary_iterator(dict);
246 
247 	while ((obj = prop_object_iterator_next(i)) != NULL) {
248 
249 		key = prop_dictionary_keysym_cstring_nocopy(obj);
250 		val = prop_dictionary_get(dict, key);
251 		num = prop_number_unsigned_integer_value(val);
252 
253 		if (arg == num)
254 			return key;
255 	}
256 
257 	return "UNKNOWN";
258 }
259 
260 static int
261 acpi_debug_sysctl_layer(SYSCTLFN_ARGS)
262 {
263 	char buf[ACPI_DEBUG_MAX];
264 	struct sysctlnode node;
265 	prop_object_t obj;
266 	int error;
267 
268 	node = *rnode;
269 	node.sysctl_data = buf;
270 
271 	(void)memcpy(node.sysctl_data, rnode->sysctl_data, ACPI_DEBUG_MAX);
272 
273 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
274 
275 	if (error || newp == NULL)
276 		return error;
277 
278 	obj = prop_dictionary_get(acpi_debug_layer_d, node.sysctl_data);
279 
280 	if (obj == NULL)
281 		return EINVAL;
282 
283 	AcpiDbgLayer = prop_number_unsigned_integer_value(obj);
284 
285 	(void)memcpy(rnode->sysctl_data, node.sysctl_data, ACPI_DEBUG_MAX);
286 
287 	return 0;
288 }
289 
290 static int
291 acpi_debug_sysctl_level(SYSCTLFN_ARGS)
292 {
293 	char buf[ACPI_DEBUG_MAX];
294 	struct sysctlnode node;
295 	prop_object_t obj;
296 	int error;
297 
298 	node = *rnode;
299 	node.sysctl_data = buf;
300 
301 	(void)memcpy(node.sysctl_data, rnode->sysctl_data, ACPI_DEBUG_MAX);
302 
303 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
304 
305 	if (error || newp == NULL)
306 		return error;
307 
308 	obj = prop_dictionary_get(acpi_debug_level_d, node.sysctl_data);
309 
310 	if (obj == NULL)
311 		return EINVAL;
312 
313 	AcpiDbgLevel = prop_number_unsigned_integer_value(obj);
314 
315 	(void)memcpy(rnode->sysctl_data, node.sysctl_data, ACPI_DEBUG_MAX);
316 
317 	return 0;
318 }
319 
320 #endif	/* ACPI_DEBUG */
321