1/* $NetBSD: acpi_debug.c,v 1.5 2014/02/25 18:30:09 pooka 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.5 2014/02/25 18:30:09 pooka 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
43ACPI_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
55static prop_dictionary_t acpi_debug_layer_d;
56static prop_dictionary_t acpi_debug_level_d;
57static char acpi_debug_layer_s[ACPI_DEBUG_MAX];
58static char acpi_debug_level_s[ACPI_DEBUG_MAX];
59
60static int acpi_debug_create(void);
61static const char *acpi_debug_getkey(prop_dictionary_t, uint32_t);
62static int acpi_debug_sysctl_layer(SYSCTLFN_PROTO);
63static int acpi_debug_sysctl_level(SYSCTLFN_PROTO);
64
65void
66acpi_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
132fail:
133 aprint_error("acpi0: failed to initialize ACPI debug\n");
134}
135
136static int
137acpi_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 /*
223 * The default debug level.
224 */
225 ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_DEBUG_DEFAULT);
226
227 /*
228 * A custom ACPI_DEBUG_NONE disables debugging.
229 */
230 ACPI_DEBUG_ADD(acpi_debug_layer_d, ACPI_DEBUG_NONE);
231 ACPI_DEBUG_ADD(acpi_debug_level_d, ACPI_DEBUG_NONE);
232
233 prop_dictionary_make_immutable(acpi_debug_layer_d);
234 prop_dictionary_make_immutable(acpi_debug_level_d);
235
236 return 0;
237}
238
239static const char *
240acpi_debug_getkey(prop_dictionary_t dict, uint32_t arg)
241{
242 prop_object_iterator_t i;
243 prop_object_t obj, val;
244 const char *key;
245 uint32_t num;
246
247 i = prop_dictionary_iterator(dict);
248
249 while ((obj = prop_object_iterator_next(i)) != NULL) {
250
251 key = prop_dictionary_keysym_cstring_nocopy(obj);
252 val = prop_dictionary_get(dict, key);
253 num = prop_number_unsigned_integer_value(val);
254
255 if (arg == num)
256 return key;
257 }
258
259 return "UNKNOWN";
260}
261
262static int
263acpi_debug_sysctl_layer(SYSCTLFN_ARGS)
264{
265 char buf[ACPI_DEBUG_MAX];
266 struct sysctlnode node;
267 prop_object_t obj;
268 int error;
269
270 node = *rnode;
271 node.sysctl_data = buf;
272
273 (void)memcpy(node.sysctl_data, rnode->sysctl_data, ACPI_DEBUG_MAX);
274
275 error = sysctl_lookup(SYSCTLFN_CALL(&node));
276
277 if (error || newp == NULL)
278 return error;
279
280 obj = prop_dictionary_get(acpi_debug_layer_d, node.sysctl_data);
281
282 if (obj == NULL)
283 return EINVAL;
284
285 AcpiDbgLayer = prop_number_unsigned_integer_value(obj);
286
287 (void)memcpy(rnode->sysctl_data, node.sysctl_data, ACPI_DEBUG_MAX);
288
289 return 0;
290}
291
292static int
293acpi_debug_sysctl_level(SYSCTLFN_ARGS)
294{
295 char buf[ACPI_DEBUG_MAX];
296 struct sysctlnode node;
297 prop_object_t obj;
298 int error;
299
300 node = *rnode;
301 node.sysctl_data = buf;
302
303 (void)memcpy(node.sysctl_data, rnode->sysctl_data, ACPI_DEBUG_MAX);
304
305 error = sysctl_lookup(SYSCTLFN_CALL(&node));
306
307 if (error || newp == NULL)
308 return error;
309
310 obj = prop_dictionary_get(acpi_debug_level_d, node.sysctl_data);
311
312 if (obj == NULL)
313 return EINVAL;
314
315 AcpiDbgLevel = prop_number_unsigned_integer_value(obj);
316
317 (void)memcpy(rnode->sysctl_data, node.sysctl_data, ACPI_DEBUG_MAX);
318
319 return 0;
320}
321
322#endif /* ACPI_DEBUG */
323