xref: /freebsd/sys/kern/subr_autoconf.c (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)subr_autoconf.c	8.1 (Berkeley) 6/10/93
36  *
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_ddb.h"
43 
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/linker.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/systm.h>
51 
52 /*
53  * Autoconfiguration subroutines.
54  */
55 
56 /*
57  * "Interrupt driven config" functions.
58  */
59 static TAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
60 	TAILQ_HEAD_INITIALIZER(intr_config_hook_list);
61 static struct intr_config_hook *next_to_notify;
62 static struct mtx intr_config_hook_lock;
63 MTX_SYSINIT(intr_config_hook, &intr_config_hook_lock, "intr config", MTX_DEF);
64 
65 /* ARGSUSED */
66 static void run_interrupt_driven_config_hooks(void);
67 
68 /*
69  * Private data and a shim function for implementing config_interhook_oneshot().
70  */
71 struct oneshot_config_hook {
72 	struct intr_config_hook
73 			och_hook;		/* Must be first */
74 	ich_func_t	och_func;
75 	void		*och_arg;
76 };
77 
78 static void
79 config_intrhook_oneshot_func(void *arg)
80 {
81 	struct oneshot_config_hook *ohook;
82 
83 	ohook = arg;
84 	ohook->och_func(ohook->och_arg);
85 	config_intrhook_disestablish(&ohook->och_hook);
86 	free(ohook, M_DEVBUF);
87 }
88 
89 /*
90  * If we wait too long for an interrupt-driven config hook to return, print
91  * a diagnostic.
92  */
93 #define	WARNING_INTERVAL_SECS	60
94 static void
95 run_interrupt_driven_config_hooks_warning(int warned)
96 {
97 	struct intr_config_hook *hook_entry;
98 	char namebuf[64];
99 	long offset;
100 
101 	if (warned < 6) {
102 		printf("run_interrupt_driven_hooks: still waiting after %d "
103 		    "seconds for", warned * WARNING_INTERVAL_SECS);
104 		TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
105 			if (linker_search_symbol_name(
106 			    (caddr_t)hook_entry->ich_func, namebuf,
107 			    sizeof(namebuf), &offset) == 0)
108 				printf(" %s", namebuf);
109 			else
110 				printf(" %p", hook_entry->ich_func);
111 		}
112 		printf("\n");
113 	}
114 	KASSERT(warned < 6,
115 	    ("run_interrupt_driven_config_hooks: waited too long"));
116 }
117 
118 static void
119 run_interrupt_driven_config_hooks()
120 {
121 	static int running;
122 	struct intr_config_hook *hook_entry;
123 
124 	mtx_lock(&intr_config_hook_lock);
125 
126 	/*
127 	 * If hook processing is already active, any newly
128 	 * registered hooks will eventually be notified.
129 	 * Let the currently running session issue these
130 	 * notifications.
131 	 */
132 	if (running != 0) {
133 		mtx_unlock(&intr_config_hook_lock);
134 		return;
135 	}
136 	running = 1;
137 
138 	while (next_to_notify != NULL) {
139 		hook_entry = next_to_notify;
140 		next_to_notify = TAILQ_NEXT(hook_entry, ich_links);
141 		mtx_unlock(&intr_config_hook_lock);
142 		(*hook_entry->ich_func)(hook_entry->ich_arg);
143 		mtx_lock(&intr_config_hook_lock);
144 	}
145 
146 	running = 0;
147 	mtx_unlock(&intr_config_hook_lock);
148 }
149 
150 static void
151 boot_run_interrupt_driven_config_hooks(void *dummy)
152 {
153 	int warned;
154 
155 	run_interrupt_driven_config_hooks();
156 
157 	/* Block boot processing until all hooks are disestablished. */
158 	mtx_lock(&intr_config_hook_lock);
159 	warned = 0;
160 	while (!TAILQ_EMPTY(&intr_config_hook_list)) {
161 		if (msleep(&intr_config_hook_list, &intr_config_hook_lock,
162 		    0, "conifhk", WARNING_INTERVAL_SECS * hz) ==
163 		    EWOULDBLOCK) {
164 			mtx_unlock(&intr_config_hook_lock);
165 			warned++;
166 			run_interrupt_driven_config_hooks_warning(warned);
167 			mtx_lock(&intr_config_hook_lock);
168 		}
169 	}
170 	mtx_unlock(&intr_config_hook_lock);
171 }
172 
173 SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
174 	boot_run_interrupt_driven_config_hooks, NULL);
175 
176 /*
177  * Register a hook that will be called after "cold"
178  * autoconfiguration is complete and interrupts can
179  * be used to complete initialization.
180  */
181 int
182 config_intrhook_establish(struct intr_config_hook *hook)
183 {
184 	struct intr_config_hook *hook_entry;
185 
186 	mtx_lock(&intr_config_hook_lock);
187 	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
188 		if (hook_entry == hook)
189 			break;
190 	if (hook_entry != NULL) {
191 		mtx_unlock(&intr_config_hook_lock);
192 		printf("config_intrhook_establish: establishing an "
193 		       "already established hook.\n");
194 		return (1);
195 	}
196 	TAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
197 	if (next_to_notify == NULL)
198 		next_to_notify = hook;
199 	mtx_unlock(&intr_config_hook_lock);
200 	if (cold == 0)
201 		/*
202 		 * XXX Call from a task since not all drivers expect
203 		 *     to be re-entered at the time a hook is established.
204 		 */
205 		/* XXX Sufficient for modules loaded after initial config??? */
206 		run_interrupt_driven_config_hooks();
207 	return (0);
208 }
209 
210 /*
211  * Register a hook function that is automatically unregistered after it runs.
212  */
213 void
214 config_intrhook_oneshot(ich_func_t func, void *arg)
215 {
216 	struct oneshot_config_hook *ohook;
217 
218 	ohook = malloc(sizeof(*ohook), M_DEVBUF, M_WAITOK);
219 	ohook->och_func = func;
220 	ohook->och_arg  = arg;
221 	ohook->och_hook.ich_func = config_intrhook_oneshot_func;
222 	ohook->och_hook.ich_arg  = ohook;
223 	config_intrhook_establish(&ohook->och_hook);
224 }
225 
226 void
227 config_intrhook_disestablish(struct intr_config_hook *hook)
228 {
229 	struct intr_config_hook *hook_entry;
230 
231 	mtx_lock(&intr_config_hook_lock);
232 	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
233 		if (hook_entry == hook)
234 			break;
235 	if (hook_entry == NULL)
236 		panic("config_intrhook_disestablish: disestablishing an "
237 		      "unestablished hook");
238 
239 	if (next_to_notify == hook)
240 		next_to_notify = TAILQ_NEXT(hook, ich_links);
241 	TAILQ_REMOVE(&intr_config_hook_list, hook, ich_links);
242 
243 	/* Wakeup anyone watching the list */
244 	wakeup(&intr_config_hook_list);
245 	mtx_unlock(&intr_config_hook_lock);
246 }
247 
248 #ifdef DDB
249 #include <ddb/ddb.h>
250 
251 DB_SHOW_COMMAND(conifhk, db_show_conifhk)
252 {
253 	struct intr_config_hook *hook_entry;
254 	char namebuf[64];
255 	long offset;
256 
257 	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
258 		if (linker_ddb_search_symbol_name(
259 		    (caddr_t)hook_entry->ich_func, namebuf, sizeof(namebuf),
260 		    &offset) == 0) {
261 			db_printf("hook: %p at %s+%#lx arg: %p\n",
262 			    hook_entry->ich_func, namebuf, offset,
263 			    hook_entry->ich_arg);
264 		} else {
265 			db_printf("hook: %p at ??+?? arg %p\n",
266 			    hook_entry->ich_func, hook_entry->ich_arg);
267 		}
268 	}
269 }
270 #endif /* DDB */
271