xref: /freebsd/sys/kern/subr_autoconf.c (revision 1843dfb05ed80149f5a412180af882e3cb8f451b)
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 #include "opt_ddb.h"
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/linker.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/systm.h>
49 
50 /*
51  * Autoconfiguration subroutines.
52  */
53 
54 /*
55  * "Interrupt driven config" functions.
56  */
57 static STAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
58 	STAILQ_HEAD_INITIALIZER(intr_config_hook_list);
59 static struct intr_config_hook *next_to_notify;
60 static struct mtx intr_config_hook_lock;
61 MTX_SYSINIT(intr_config_hook, &intr_config_hook_lock, "intr config", MTX_DEF);
62 
63 /* ARGSUSED */
64 static void run_interrupt_driven_config_hooks(void);
65 
66 /*
67  * Private data and a shim function for implementing config_interhook_oneshot().
68  */
69 struct oneshot_config_hook {
70 	struct intr_config_hook
71 			och_hook;		/* Must be first */
72 	ich_func_t	och_func;
73 	void		*och_arg;
74 };
75 
76 static void
77 config_intrhook_oneshot_func(void *arg)
78 {
79 	struct oneshot_config_hook *ohook;
80 
81 	ohook = arg;
82 	ohook->och_func(ohook->och_arg);
83 	config_intrhook_disestablish(&ohook->och_hook);
84 	free(ohook, M_DEVBUF);
85 }
86 
87 /*
88  * If we wait too long for an interrupt-driven config hook to return, print
89  * a diagnostic.
90  */
91 #define	WARNING_INTERVAL_SECS	60
92 static void
93 run_interrupt_driven_config_hooks_warning(int warned)
94 {
95 	struct intr_config_hook *hook_entry;
96 	char namebuf[64];
97 	long offset;
98 
99 	if (warned < 6) {
100 		printf("run_interrupt_driven_hooks: still waiting after %d "
101 		    "seconds for", warned * WARNING_INTERVAL_SECS);
102 		STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
103 			if (linker_search_symbol_name(
104 			    (caddr_t)hook_entry->ich_func, namebuf,
105 			    sizeof(namebuf), &offset) == 0)
106 				printf(" %s", namebuf);
107 			else
108 				printf(" %p", hook_entry->ich_func);
109 		}
110 		printf("\n");
111 	}
112 	KASSERT(warned < 6,
113 	    ("run_interrupt_driven_config_hooks: waited too long"));
114 }
115 
116 static void
117 run_interrupt_driven_config_hooks(void)
118 {
119 	static int running;
120 	struct intr_config_hook *hook_entry;
121 
122 	TSENTER();
123 	mtx_lock(&intr_config_hook_lock);
124 
125 	/*
126 	 * If hook processing is already active, any newly
127 	 * registered hooks will eventually be notified.
128 	 * Let the currently running session issue these
129 	 * notifications.
130 	 */
131 	if (running != 0) {
132 		mtx_unlock(&intr_config_hook_lock);
133 		return;
134 	}
135 	running = 1;
136 
137 	while (next_to_notify != NULL) {
138 		hook_entry = next_to_notify;
139 		next_to_notify = STAILQ_NEXT(hook_entry, ich_links);
140 		hook_entry->ich_state = ICHS_RUNNING;
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 	TSEXIT();
149 }
150 
151 static void
152 boot_run_interrupt_driven_config_hooks(void *dummy)
153 {
154 	int warned;
155 
156 	run_interrupt_driven_config_hooks();
157 
158 	/* Block boot processing until all hooks are disestablished. */
159 	TSWAIT("config hooks");
160 	mtx_lock(&intr_config_hook_lock);
161 	warned = 0;
162 	while (!STAILQ_EMPTY(&intr_config_hook_list)) {
163 		if (msleep(&intr_config_hook_list, &intr_config_hook_lock,
164 		    0, "conifhk", WARNING_INTERVAL_SECS * hz) ==
165 		    EWOULDBLOCK) {
166 			mtx_unlock(&intr_config_hook_lock);
167 			warned++;
168 			run_interrupt_driven_config_hooks_warning(warned);
169 			mtx_lock(&intr_config_hook_lock);
170 		}
171 	}
172 	mtx_unlock(&intr_config_hook_lock);
173 	TSUNWAIT("config hooks");
174 }
175 
176 SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
177 	boot_run_interrupt_driven_config_hooks, NULL);
178 
179 /*
180  * Register a hook that will be called after "cold"
181  * autoconfiguration is complete and interrupts can
182  * be used to complete initialization.
183  */
184 int
185 config_intrhook_establish(struct intr_config_hook *hook)
186 {
187 	struct intr_config_hook *hook_entry;
188 
189 	TSHOLD("config hooks");
190 	mtx_lock(&intr_config_hook_lock);
191 	STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
192 		if (hook_entry == hook)
193 			break;
194 	if (hook_entry != NULL) {
195 		mtx_unlock(&intr_config_hook_lock);
196 		printf("config_intrhook_establish: establishing an "
197 		       "already established hook.\n");
198 		return (1);
199 	}
200 	STAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
201 	if (next_to_notify == NULL)
202 		next_to_notify = hook;
203 	hook->ich_state = ICHS_QUEUED;
204 	mtx_unlock(&intr_config_hook_lock);
205 	if (cold == 0)
206 		/*
207 		 * XXX Call from a task since not all drivers expect
208 		 *     to be re-entered at the time a hook is established.
209 		 */
210 		/* XXX Sufficient for modules loaded after initial config??? */
211 		run_interrupt_driven_config_hooks();
212 	return (0);
213 }
214 
215 /*
216  * Register a hook function that is automatically unregistered after it runs.
217  */
218 void
219 config_intrhook_oneshot(ich_func_t func, void *arg)
220 {
221 	struct oneshot_config_hook *ohook;
222 
223 	ohook = malloc(sizeof(*ohook), M_DEVBUF, M_WAITOK);
224 	ohook->och_func = func;
225 	ohook->och_arg  = arg;
226 	ohook->och_hook.ich_func = config_intrhook_oneshot_func;
227 	ohook->och_hook.ich_arg  = ohook;
228 	config_intrhook_establish(&ohook->och_hook);
229 }
230 
231 static void
232 config_intrhook_disestablish_locked(struct intr_config_hook *hook)
233 {
234 	struct intr_config_hook *hook_entry;
235 
236 	STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
237 		if (hook_entry == hook)
238 			break;
239 	if (hook_entry == NULL)
240 		panic("config_intrhook_disestablish: disestablishing an "
241 		      "unestablished hook");
242 
243 	if (next_to_notify == hook)
244 		next_to_notify = STAILQ_NEXT(hook, ich_links);
245 	STAILQ_REMOVE(&intr_config_hook_list, hook, intr_config_hook, ich_links);
246 	TSRELEASE("config hooks");
247 
248 	/* Wakeup anyone watching the list */
249 	hook->ich_state = ICHS_DONE;
250 	wakeup(&intr_config_hook_list);
251 }
252 
253 void
254 config_intrhook_disestablish(struct intr_config_hook *hook)
255 {
256 	mtx_lock(&intr_config_hook_lock);
257 	config_intrhook_disestablish_locked(hook);
258 	mtx_unlock(&intr_config_hook_lock);
259 }
260 
261 int
262 config_intrhook_drain(struct intr_config_hook *hook)
263 {
264 	mtx_lock(&intr_config_hook_lock);
265 
266 	/*
267 	 * The config hook has completed, so just return.
268 	 */
269 	if (hook->ich_state == ICHS_DONE) {
270 		mtx_unlock(&intr_config_hook_lock);
271 		return (ICHS_DONE);
272 	}
273 
274 	/*
275 	 * The config hook hasn't started running, just call disestablish.
276 	 */
277 	if (hook->ich_state == ICHS_QUEUED) {
278 		config_intrhook_disestablish_locked(hook);
279 		mtx_unlock(&intr_config_hook_lock);
280 		return (ICHS_QUEUED);
281 	}
282 
283 	/*
284 	 * The config hook is running, so wait for it to complete and return.
285 	 */
286 	while (hook->ich_state != ICHS_DONE) {
287 		if (msleep(&intr_config_hook_list, &intr_config_hook_lock,
288 		    0, "confhd", hz) == EWOULDBLOCK) {
289 			// XXX do I whine?
290 		}
291 	}
292 	mtx_unlock(&intr_config_hook_lock);
293 	return (ICHS_RUNNING);
294 }
295 
296 #ifdef DDB
297 #include <ddb/ddb.h>
298 
299 DB_SHOW_COMMAND_FLAGS(conifhk, db_show_conifhk, DB_CMD_MEMSAFE)
300 {
301 	struct intr_config_hook *hook_entry;
302 	char namebuf[64];
303 	long offset;
304 
305 	STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
306 		if (linker_ddb_search_symbol_name(
307 		    (caddr_t)hook_entry->ich_func, namebuf, sizeof(namebuf),
308 		    &offset) == 0) {
309 			db_printf("hook: %p at %s+%#lx arg: %p\n",
310 			    hook_entry->ich_func, namebuf, offset,
311 			    hook_entry->ich_arg);
312 		} else {
313 			db_printf("hook: %p at ??+?? arg %p\n",
314 			    hook_entry->ich_func, hook_entry->ich_arg);
315 		}
316 	}
317 }
318 #endif /* DDB */
319