subr_autoconf.c revision 180673
178344Sobrien/*-
278344Sobrien * Copyright (c) 1992, 1993
398184Sgordon *	The Regents of the University of California.  All rights reserved.
498184Sgordon *
578344Sobrien * This software was developed by the Computer Systems Engineering group
678344Sobrien * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
778344Sobrien * contributed to Berkeley.
8128470Sdarrenr *
9113959Smtm * Redistribution and use in source and binary forms, with or without
10126744Spjd * modification, are permitted provided that the following conditions
1178344Sobrien * are met:
1278344Sobrien * 1. Redistributions of source code must retain the above copyright
1378344Sobrien *    notice, this list of conditions and the following disclaimer.
1478344Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1598184Sgordon *    notice, this list of conditions and the following disclaimer in the
1698184Sgordon *    documentation and/or other materials provided with the distribution.
17124618Smtm * 4. Neither the name of the University nor the names of its contributors
1898184Sgordon *    may be used to endorse or promote products derived from this software
1978344Sobrien *    without specific prior written permission.
2078344Sobrien *
2178344Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2278344Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2378344Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2498184Sgordon * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2598184Sgordon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2678344Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2778344Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2898184Sgordon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2978344Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30124928Smux * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31124928Smux * SUCH DAMAGE.
32124928Smux *
33124928Smux *	@(#)subr_autoconf.c	8.1 (Berkeley) 6/10/93
34124928Smux *
35124928Smux */
36124928Smux
37124928Smux#include <sys/cdefs.h>
38124928Smux__FBSDID("$FreeBSD: head/sys/kern/subr_autoconf.c 180673 2008-07-21 20:50:49Z rwatson $");
3978344Sobrien
4078344Sobrien#include "opt_ddb.h"
4198184Sgordon
42124928Smux#include <sys/param.h>
43104980Sschweikh#include <sys/kernel.h>
44114271Smtm#include <sys/linker.h>
4598184Sgordon#include <sys/lock.h>
46113959Smtm#include <sys/mutex.h>
4798184Sgordon#include <sys/systm.h>
4898184Sgordon
4998184Sgordon/*
5098184Sgordon * Autoconfiguration subroutines.
51106333Sume */
52106333Sume
5398184Sgordon/*
5498184Sgordon * "Interrupt driven config" functions.
5598184Sgordon */
5678344Sobrienstatic TAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
5778344Sobrien	TAILQ_HEAD_INITIALIZER(intr_config_hook_list);
5878344Sobrienstatic struct mtx intr_config_hook_lock;
5978344SobrienMTX_SYSINIT(intr_config_hook, &intr_config_hook_lock, "intr config", MTX_DEF);
6078344Sobrien
6178344Sobrien/* ARGSUSED */
62124618Smtmstatic void run_interrupt_driven_config_hooks(void *dummy);
63124618Smtm
64124618Smtm/*
65124618Smtm * If we wait too long for an interrupt-driven config hook to return, print
66124618Smtm * a diagnostic.
67124618Smtm */
68124618Smtm#define	WARNING_INTERVAL_SECS	60
69124618Smtmstatic void
70124618Smtmrun_interrupt_driven_config_hooks_warning(int warned)
71124618Smtm{
72124618Smtm	struct intr_config_hook *hook_entry;
73124618Smtm	char namebuf[64];
74124618Smtm	long offset;
7578344Sobrien
7678344Sobrien	if (warned < 6) {
7778344Sobrien		printf("run_interrupt_driven_hooks: still waiting after %d "
7878344Sobrien		    "seconds for", warned * WARNING_INTERVAL_SECS);
79120515Smux		TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
80120515Smux			if (linker_search_symbol_name(
81124618Smtm			    (caddr_t)hook_entry->ich_func, namebuf,
82124618Smtm			    sizeof(namebuf), &offset) == 0)
83124618Smtm				printf(" %s", namebuf);
84124618Smtm			else
85120515Smux				printf(" %p", hook_entry->ich_func);
8678344Sobrien		}
8778344Sobrien		printf("\n");
8878344Sobrien	}
8978344Sobrien	KASSERT(warned < 6,
9078344Sobrien	    ("run_interrupt_driven_config_hooks: waited too long"));
9178344Sobrien}
92124618Smtm
93124618Smtmstatic void
94124618Smtmrun_interrupt_driven_config_hooks(dummy)
95124618Smtm	void *dummy;
96124618Smtm{
97124618Smtm	struct intr_config_hook *hook_entry, *next_entry;
98124618Smtm	int warned;
99124618Smtm
100124618Smtm	mtx_lock(&intr_config_hook_lock);
101124618Smtm	TAILQ_FOREACH_SAFE(hook_entry, &intr_config_hook_list, ich_links,
102124618Smtm	    next_entry) {
10398184Sgordon		mtx_unlock(&intr_config_hook_lock);
10478344Sobrien		(*hook_entry->ich_func)(hook_entry->ich_arg);
10578344Sobrien		mtx_lock(&intr_config_hook_lock);
10698184Sgordon	}
10798184Sgordon
108124618Smtm	warned = 0;
109124928Smux	while (!TAILQ_EMPTY(&intr_config_hook_list)) {
110124618Smtm		if (msleep(&intr_config_hook_list, &intr_config_hook_lock,
111124618Smtm		    PCONFIG, "conifhk", WARNING_INTERVAL_SECS * hz) ==
11298184Sgordon		    EWOULDBLOCK) {
11398184Sgordon			mtx_unlock(&intr_config_hook_lock);
11498184Sgordon			warned++;
11578344Sobrien			run_interrupt_driven_config_hooks_warning(warned);
11678344Sobrien			mtx_lock(&intr_config_hook_lock);
11798184Sgordon		}
11878344Sobrien	}
11978344Sobrien	mtx_unlock(&intr_config_hook_lock);
12078344Sobrien}
121SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
122	run_interrupt_driven_config_hooks, NULL);
123
124/*
125 * Register a hook that will be called after "cold"
126 * autoconfiguration is complete and interrupts can
127 * be used to complete initialization.
128 */
129int
130config_intrhook_establish(hook)
131	struct intr_config_hook *hook;
132{
133	struct intr_config_hook *hook_entry;
134
135	mtx_lock(&intr_config_hook_lock);
136	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
137		if (hook_entry == hook)
138			break;
139	if (hook_entry != NULL) {
140		mtx_unlock(&intr_config_hook_lock);
141		printf("config_intrhook_establish: establishing an "
142		       "already established hook.\n");
143		return (1);
144	}
145	TAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
146	mtx_unlock(&intr_config_hook_lock);
147	if (cold == 0)
148		/* XXX Sufficient for modules loaded after initial config??? */
149		run_interrupt_driven_config_hooks(NULL);
150	return (0);
151}
152
153void
154config_intrhook_disestablish(hook)
155	struct intr_config_hook *hook;
156{
157	struct intr_config_hook *hook_entry;
158
159	mtx_lock(&intr_config_hook_lock);
160	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
161		if (hook_entry == hook)
162			break;
163	if (hook_entry == NULL)
164		panic("config_intrhook_disestablish: disestablishing an "
165		      "unestablished hook");
166
167	TAILQ_REMOVE(&intr_config_hook_list, hook, ich_links);
168
169	/* Wakeup anyone watching the list */
170	wakeup(&intr_config_hook_list);
171	mtx_unlock(&intr_config_hook_lock);
172}
173
174#ifdef DDB
175#include <ddb/ddb.h>
176
177DB_SHOW_COMMAND(conifhk, db_show_conifhk)
178{
179	struct intr_config_hook *hook_entry;
180	char namebuf[64];
181	long offset;
182
183	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
184		if (linker_ddb_search_symbol_name(
185		    (caddr_t)hook_entry->ich_func, namebuf, sizeof(namebuf),
186		    &offset) == 0) {
187			db_printf("hook: %p at %s+%#lx arg: %p\n",
188			    hook_entry->ich_func, namebuf, offset,
189			    hook_entry->ich_arg);
190		} else {
191			db_printf("hook: %p at ??+?? arg %p\n",
192			    hook_entry->ich_func, hook_entry->ich_arg);
193		}
194	}
195}
196#endif /* DDB */
197