xref: /dragonfly/sys/kern/subr_autoconf.c (revision c89a6c1b)
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  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, Lawrence Berkeley Laboratories.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)subr_autoconf.c	8.1 (Berkeley) 6/10/93
43  *
44  * $FreeBSD: src/sys/kern/subr_autoconf.c,v 1.14 1999/10/05 21:19:41 n_hibma Exp $
45  * $DragonFly: src/sys/kern/subr_autoconf.c,v 1.9 2007/07/28 23:24:33 dillon Exp $
46  */
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/thread.h>
52 #include <sys/thread2.h>
53 
54 /*
55  * Autoconfiguration subroutines.
56  */
57 
58 /*
59  * "Interrupt driven config" functions.
60  */
61 static TAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
62 	TAILQ_HEAD_INITIALIZER(intr_config_hook_list);
63 
64 
65 /* ARGSUSED */
66 static void run_interrupt_driven_config_hooks (void *dummy);
67 static int ran_config_hooks;
68 
69 static void
70 run_interrupt_driven_config_hooks(void *dummy)
71 {
72 	struct intr_config_hook *hook_entry;
73 	int waiting;
74 
75 	waiting = 0;
76 
77 	ran_config_hooks = 1;
78 	while (!TAILQ_EMPTY(&intr_config_hook_list)) {
79 		TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
80 			if (hook_entry->ich_ran == 0) {
81 				hook_entry->ich_ran = 1;
82 				(*hook_entry->ich_func)(hook_entry->ich_arg);
83 				break;
84 			}
85 		}
86 		if (hook_entry)
87 			continue;
88 		if (TAILQ_EMPTY(&intr_config_hook_list))
89 			break;
90 
91 		if (waiting >= 20 && (waiting % 10) == 0) {
92 			crit_enter();
93 			kprintf("**WARNING** waiting for the following device "
94 				"to finish configuring:\n");
95 			TAILQ_FOREACH(hook_entry, &intr_config_hook_list,
96 				      ich_links) {
97 			    kprintf("  %s:\tfunc=%p arg=%p\n",
98 				(hook_entry->ich_desc ?
99 				    hook_entry->ich_desc : "?"),
100 				hook_entry->ich_func,
101 				hook_entry->ich_arg);
102 			}
103 			crit_exit();
104 			if (waiting >= 60) {
105 				kprintf("Giving up, interrupt routing is "
106 					"probably hosed\n");
107 				break;
108 			}
109 		}
110 		tsleep(&intr_config_hook_list, 0, "conifhk", hz);
111 		++waiting;
112 	}
113 }
114 SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
115 	run_interrupt_driven_config_hooks, NULL)
116 
117 /*
118  * Register a hook that will be called after "cold"
119  * autoconfiguration is complete and interrupts can
120  * be used to complete initialization.
121  */
122 int
123 config_intrhook_establish(struct intr_config_hook *hook)
124 {
125 	struct intr_config_hook *hook_entry;
126 
127 	for (hook_entry = TAILQ_FIRST(&intr_config_hook_list);
128 	     hook_entry != NULL;
129 	     hook_entry = TAILQ_NEXT(hook_entry, ich_links)) {
130 		if (hook_entry == hook)
131 			break;
132 		if (hook_entry->ich_order > hook->ich_order)
133 			break;
134 	}
135 	if (hook_entry == hook) {
136 		kprintf("config_intrhook_establish: establishing an "
137 		       "already established hook.\n");
138 		return (1);
139 	}
140 	hook->ich_ran = 0;
141 	if (hook_entry)
142 		TAILQ_INSERT_BEFORE(hook_entry, hook, ich_links);
143 	else
144 		TAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
145 
146 	/*
147 	 * Late hook, run immediately.
148 	 */
149 	if (ran_config_hooks)
150 		run_interrupt_driven_config_hooks(NULL);
151 	return (0);
152 }
153 
154 void
155 config_intrhook_disestablish(struct intr_config_hook *hook)
156 {
157 	struct intr_config_hook *hook_entry;
158 
159 	for (hook_entry = TAILQ_FIRST(&intr_config_hook_list);
160 	     hook_entry != NULL;
161 	     hook_entry = TAILQ_NEXT(hook_entry, ich_links))
162 		if (hook_entry == hook)
163 			break;
164 	if (hook_entry == NULL)
165 		panic("config_intrhook_disestablish: disestablishing an "
166 		    "unestablished hook");
167 
168 	TAILQ_REMOVE(&intr_config_hook_list, hook, ich_links);
169 	/* Wakeup anyone watching the list */
170 	wakeup(&intr_config_hook_list);
171 }
172