subr_autoconf.c revision 180616
1/*-
2 * Copyright (c) 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)subr_autoconf.c	8.1 (Berkeley) 6/10/93
34 *
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/subr_autoconf.c 180616 2008-07-19 19:08:35Z rwatson $");
39
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/mutex.h>
47#include <sys/systm.h>
48
49/*
50 * Autoconfiguration subroutines.
51 */
52
53/*
54 * "Interrupt driven config" functions.
55 */
56static TAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
57	TAILQ_HEAD_INITIALIZER(intr_config_hook_list);
58static struct mtx intr_config_hook_lock;
59MTX_SYSINIT(intr_config_hook, &intr_config_hook_lock, "intr config", MTX_DEF);
60
61/* ARGSUSED */
62static void run_interrupt_driven_config_hooks(void *dummy);
63
64/*
65 * If we wait too long for an interrupt-driven config hook to return, print
66 * a diagnostic.
67 */
68#define	WARNING_INTERVAL_SECS	60
69static void
70run_interrupt_driven_config_hooks_warning(int warned)
71{
72	struct intr_config_hook *hook_entry;
73	char namebuf[64];
74	long offset;
75
76	printf("run_interrupt_driven_hooks: still waiting after %d seconds "
77	    "for", warned * WARNING_INTERVAL_SECS);
78	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
79		if (linker_search_symbol_name((caddr_t)hook_entry->ich_func,
80		    namebuf, sizeof(namebuf), &offset) == 0)
81			printf(" %s", namebuf);
82		else
83			printf(" %p", hook_entry->ich_func);
84	}
85	printf("\n");
86}
87
88static void
89run_interrupt_driven_config_hooks(dummy)
90	void *dummy;
91{
92	struct intr_config_hook *hook_entry, *next_entry;
93	int warned;
94
95	mtx_lock(&intr_config_hook_lock);
96	TAILQ_FOREACH_SAFE(hook_entry, &intr_config_hook_list, ich_links,
97	    next_entry) {
98		mtx_unlock(&intr_config_hook_lock);
99		(*hook_entry->ich_func)(hook_entry->ich_arg);
100		mtx_lock(&intr_config_hook_lock);
101	}
102
103	warned = 0;
104	while (!TAILQ_EMPTY(&intr_config_hook_list)) {
105		if (msleep(&intr_config_hook_list, &intr_config_hook_lock,
106		    PCONFIG, "conifhk", WARNING_INTERVAL_SECS * hz) ==
107		    EWOULDBLOCK && warned < 5) {
108			mtx_unlock(&intr_config_hook_lock);
109			warned++;
110			run_interrupt_driven_config_hooks_warning(warned);
111			mtx_lock(&intr_config_hook_lock);
112		}
113	}
114	mtx_unlock(&intr_config_hook_lock);
115}
116SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
117	run_interrupt_driven_config_hooks, NULL);
118
119/*
120 * Register a hook that will be called after "cold"
121 * autoconfiguration is complete and interrupts can
122 * be used to complete initialization.
123 */
124int
125config_intrhook_establish(hook)
126	struct intr_config_hook *hook;
127{
128	struct intr_config_hook *hook_entry;
129
130	mtx_lock(&intr_config_hook_lock);
131	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
132		if (hook_entry == hook)
133			break;
134	if (hook_entry != NULL) {
135		mtx_unlock(&intr_config_hook_lock);
136		printf("config_intrhook_establish: establishing an "
137		       "already established hook.\n");
138		return (1);
139	}
140	TAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
141	mtx_unlock(&intr_config_hook_lock);
142	if (cold == 0)
143		/* XXX Sufficient for modules loaded after initial config??? */
144		run_interrupt_driven_config_hooks(NULL);
145	return (0);
146}
147
148void
149config_intrhook_disestablish(hook)
150	struct intr_config_hook *hook;
151{
152	struct intr_config_hook *hook_entry;
153
154	mtx_lock(&intr_config_hook_lock);
155	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
156		if (hook_entry == hook)
157			break;
158	if (hook_entry == NULL)
159		panic("config_intrhook_disestablish: disestablishing an "
160		      "unestablished hook");
161
162	TAILQ_REMOVE(&intr_config_hook_list, hook, ich_links);
163
164	/* Wakeup anyone watching the list */
165	wakeup(&intr_config_hook_list);
166	mtx_unlock(&intr_config_hook_lock);
167}
168
169#ifdef DDB
170#include <ddb/ddb.h>
171
172DB_SHOW_COMMAND(conifhk, db_show_conifhk)
173{
174	struct intr_config_hook *hook_entry;
175	char namebuf[64];
176	long offset;
177
178	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
179		if (linker_ddb_search_symbol_name(
180		    (caddr_t)hook_entry->ich_func, namebuf, sizeof(namebuf),
181		    &offset) == 0) {
182			db_printf("hook: %p at %s+%#lx arg: %p\n",
183			    hook_entry->ich_func, namebuf, offset,
184			    hook_entry->ich_arg);
185		} else {
186			db_printf("hook: %p at ??+?? arg %p\n",
187			    hook_entry->ich_func, hook_entry->ich_arg);
188		}
189	}
190}
191#endif /* DDB */
192