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