xref: /freebsd/sys/kern/subr_autoconf.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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$");
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/systm.h>
45 
46 /*
47  * Autoconfiguration subroutines.
48  */
49 
50 /*
51  * "Interrupt driven config" functions.
52  */
53 static TAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
54 	TAILQ_HEAD_INITIALIZER(intr_config_hook_list);
55 static struct mtx intr_config_hook_lock;
56 MTX_SYSINIT(intr_config_hook, &intr_config_hook_lock, "intr config", MTX_DEF);
57 
58 /* ARGSUSED */
59 static void run_interrupt_driven_config_hooks(void *dummy);
60 
61 static void
62 run_interrupt_driven_config_hooks(dummy)
63 	void *dummy;
64 {
65 	struct intr_config_hook *hook_entry, *next_entry;
66 
67 	mtx_lock(&intr_config_hook_lock);
68 	TAILQ_FOREACH_SAFE(hook_entry, &intr_config_hook_list, ich_links,
69 	    next_entry) {
70 		next_entry = TAILQ_NEXT(hook_entry, ich_links);
71 		mtx_unlock(&intr_config_hook_lock);
72 		(*hook_entry->ich_func)(hook_entry->ich_arg);
73 		mtx_lock(&intr_config_hook_lock);
74 	}
75 
76 	while (!TAILQ_EMPTY(&intr_config_hook_list)) {
77 		msleep(&intr_config_hook_list, &intr_config_hook_lock, PCONFIG,
78 		    "conifhk", 0);
79 	}
80 	mtx_unlock(&intr_config_hook_lock);
81 }
82 SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
83 	run_interrupt_driven_config_hooks, NULL)
84 
85 /*
86  * Register a hook that will be called after "cold"
87  * autoconfiguration is complete and interrupts can
88  * be used to complete initialization.
89  */
90 int
91 config_intrhook_establish(hook)
92 	struct intr_config_hook *hook;
93 {
94 	struct intr_config_hook *hook_entry;
95 
96 	mtx_lock(&intr_config_hook_lock);
97 	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
98 		if (hook_entry == hook)
99 			break;
100 	if (hook_entry != NULL) {
101 		mtx_unlock(&intr_config_hook_lock);
102 		printf("config_intrhook_establish: establishing an "
103 		       "already established hook.\n");
104 		return (1);
105 	}
106 	TAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
107 	mtx_unlock(&intr_config_hook_lock);
108 	if (cold == 0)
109 		/* XXX Sufficient for modules loaded after initial config??? */
110 		run_interrupt_driven_config_hooks(NULL);
111 	return (0);
112 }
113 
114 void
115 config_intrhook_disestablish(hook)
116 	struct intr_config_hook *hook;
117 {
118 	struct intr_config_hook *hook_entry;
119 
120 	mtx_lock(&intr_config_hook_lock);
121 	TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
122 		if (hook_entry == hook)
123 			break;
124 	if (hook_entry == NULL)
125 		panic("config_intrhook_disestablish: disestablishing an "
126 		      "unestablished hook");
127 
128 	TAILQ_REMOVE(&intr_config_hook_list, hook, ich_links);
129 
130 	/* Wakeup anyone watching the list */
131 	wakeup(&intr_config_hook_list);
132 	mtx_unlock(&intr_config_hook_lock);
133 }
134