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