kern_cpu.c revision 1.2.10.2 1 1.2.10.2 jmcneill /* $NetBSD: kern_cpu.c,v 1.2.10.2 2007/08/09 02:37:18 jmcneill Exp $ */
2 1.2.10.1 jmcneill
3 1.2.10.1 jmcneill /*-
4 1.2.10.1 jmcneill * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.2.10.1 jmcneill * All rights reserved.
6 1.2.10.1 jmcneill *
7 1.2.10.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation
8 1.2.10.1 jmcneill * by Andrew Doran.
9 1.2.10.1 jmcneill *
10 1.2.10.1 jmcneill * Redistribution and use in source and binary forms, with or without
11 1.2.10.1 jmcneill * modification, are permitted provided that the following conditions
12 1.2.10.1 jmcneill * are met:
13 1.2.10.1 jmcneill * 1. Redistributions of source code must retain the above copyright
14 1.2.10.1 jmcneill * notice, this list of conditions and the following disclaimer.
15 1.2.10.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.10.1 jmcneill * notice, this list of conditions and the following disclaimer in the
17 1.2.10.1 jmcneill * documentation and/or other materials provided with the distribution.
18 1.2.10.1 jmcneill * 3. All advertising materials mentioning features or use of this software
19 1.2.10.1 jmcneill * must display the following acknowledgement:
20 1.2.10.1 jmcneill * This product includes software developed by the NetBSD
21 1.2.10.1 jmcneill * Foundation, Inc. and its contributors.
22 1.2.10.1 jmcneill * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.10.1 jmcneill * contributors may be used to endorse or promote products derived
24 1.2.10.1 jmcneill * from this software without specific prior written permission.
25 1.2.10.1 jmcneill *
26 1.2.10.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.10.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.10.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.10.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.10.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.10.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.10.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.10.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.10.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.10.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.10.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
37 1.2.10.1 jmcneill */
38 1.2 yamt
39 1.2 yamt /*-
40 1.2 yamt * Copyright (c)2007 YAMAMOTO Takashi,
41 1.2 yamt * All rights reserved.
42 1.2 yamt *
43 1.2 yamt * Redistribution and use in source and binary forms, with or without
44 1.2 yamt * modification, are permitted provided that the following conditions
45 1.2 yamt * are met:
46 1.2 yamt * 1. Redistributions of source code must retain the above copyright
47 1.2 yamt * notice, this list of conditions and the following disclaimer.
48 1.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
49 1.2 yamt * notice, this list of conditions and the following disclaimer in the
50 1.2 yamt * documentation and/or other materials provided with the distribution.
51 1.2 yamt *
52 1.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 1.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 1.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.2 yamt * SUCH DAMAGE.
63 1.2 yamt */
64 1.2 yamt
65 1.2 yamt #include <sys/cdefs.h>
66 1.2 yamt
67 1.2.10.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.2.10.2 2007/08/09 02:37:18 jmcneill Exp $");
68 1.2 yamt
69 1.2 yamt #include <sys/param.h>
70 1.2 yamt #include <sys/systm.h>
71 1.2 yamt #include <sys/idle.h>
72 1.2 yamt #include <sys/sched.h>
73 1.2.10.1 jmcneill #include <sys/conf.h>
74 1.2.10.1 jmcneill #include <sys/cpu.h>
75 1.2.10.1 jmcneill #include <sys/cpuio.h>
76 1.2.10.1 jmcneill #include <sys/proc.h>
77 1.2.10.1 jmcneill #include <sys/kernel.h>
78 1.2.10.1 jmcneill #include <sys/kauth.h>
79 1.2.10.1 jmcneill
80 1.2.10.1 jmcneill void cpuctlattach(int);
81 1.2.10.1 jmcneill
82 1.2.10.1 jmcneill dev_type_ioctl(cpuctl_ioctl);
83 1.2.10.1 jmcneill
84 1.2.10.1 jmcneill const struct cdevsw cpuctl_cdevsw = {
85 1.2.10.1 jmcneill nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
86 1.2.10.1 jmcneill nullstop, notty, nopoll, nommap, nokqfilter,
87 1.2.10.1 jmcneill D_OTHER | D_MPSAFE
88 1.2.10.1 jmcneill };
89 1.2 yamt
90 1.2.10.1 jmcneill kmutex_t cpu_lock;
91 1.2 yamt
92 1.2 yamt int
93 1.2 yamt mi_cpu_attach(struct cpu_info *ci)
94 1.2 yamt {
95 1.2 yamt struct schedstate_percpu *spc = &ci->ci_schedstate;
96 1.2 yamt int error;
97 1.2 yamt
98 1.2.10.2 jmcneill ci->ci_index = ncpu;
99 1.2.10.2 jmcneill
100 1.2 yamt mutex_init(&spc->spc_lwplock, MUTEX_SPIN, IPL_SCHED);
101 1.2 yamt sched_cpuattach(ci);
102 1.2 yamt
103 1.2 yamt error = create_idle_lwp(ci);
104 1.2 yamt if (error != 0) {
105 1.2 yamt /* XXX revert sched_cpuattach */
106 1.2 yamt return error;
107 1.2 yamt }
108 1.2 yamt
109 1.2 yamt ncpu++;
110 1.2 yamt
111 1.2 yamt return 0;
112 1.2 yamt }
113 1.2.10.1 jmcneill
114 1.2.10.1 jmcneill void
115 1.2.10.1 jmcneill cpuctlattach(int dummy)
116 1.2.10.1 jmcneill {
117 1.2.10.1 jmcneill
118 1.2.10.1 jmcneill }
119 1.2.10.1 jmcneill
120 1.2.10.1 jmcneill int
121 1.2.10.1 jmcneill cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
122 1.2.10.1 jmcneill {
123 1.2.10.1 jmcneill CPU_INFO_ITERATOR cii;
124 1.2.10.1 jmcneill cpustate_t *cs;
125 1.2.10.1 jmcneill struct cpu_info *ci;
126 1.2.10.1 jmcneill int error, i;
127 1.2.10.1 jmcneill u_int id;
128 1.2.10.1 jmcneill
129 1.2.10.1 jmcneill error = 0;
130 1.2.10.1 jmcneill
131 1.2.10.1 jmcneill mutex_enter(&cpu_lock);
132 1.2.10.1 jmcneill switch (cmd) {
133 1.2.10.1 jmcneill case IOC_CPU_SETSTATE:
134 1.2.10.1 jmcneill error = kauth_authorize_generic(l->l_cred,
135 1.2.10.1 jmcneill KAUTH_GENERIC_ISSUSER, NULL);
136 1.2.10.1 jmcneill if (error != 0)
137 1.2.10.1 jmcneill break;
138 1.2.10.1 jmcneill cs = data;
139 1.2.10.1 jmcneill if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
140 1.2.10.1 jmcneill error = ESRCH;
141 1.2.10.1 jmcneill break;
142 1.2.10.1 jmcneill }
143 1.2.10.1 jmcneill if (!cs->cs_intr) {
144 1.2.10.1 jmcneill error = EOPNOTSUPP;
145 1.2.10.1 jmcneill break;
146 1.2.10.1 jmcneill }
147 1.2.10.1 jmcneill error = cpu_setonline(ci, cs->cs_online);
148 1.2.10.1 jmcneill break;
149 1.2.10.1 jmcneill
150 1.2.10.1 jmcneill case IOC_CPU_GETSTATE:
151 1.2.10.1 jmcneill cs = data;
152 1.2.10.1 jmcneill id = cs->cs_id;
153 1.2.10.1 jmcneill memset(cs, sizeof(*cs), 0);
154 1.2.10.1 jmcneill cs->cs_id = id;
155 1.2.10.1 jmcneill if ((ci = cpu_lookup(id)) == NULL) {
156 1.2.10.1 jmcneill error = ESRCH;
157 1.2.10.1 jmcneill break;
158 1.2.10.1 jmcneill }
159 1.2.10.1 jmcneill if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
160 1.2.10.1 jmcneill cs->cs_online = false;
161 1.2.10.1 jmcneill else
162 1.2.10.1 jmcneill cs->cs_online = true;
163 1.2.10.1 jmcneill cs->cs_intr = true;
164 1.2.10.1 jmcneill cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
165 1.2.10.1 jmcneill break;
166 1.2.10.1 jmcneill
167 1.2.10.1 jmcneill case IOC_CPU_MAPID:
168 1.2.10.1 jmcneill i = 0;
169 1.2.10.1 jmcneill for (CPU_INFO_FOREACH(cii, ci)) {
170 1.2.10.1 jmcneill if (i++ == *(int *)data)
171 1.2.10.1 jmcneill break;
172 1.2.10.1 jmcneill }
173 1.2.10.1 jmcneill if (ci == NULL)
174 1.2.10.1 jmcneill error = ESRCH;
175 1.2.10.1 jmcneill else
176 1.2.10.1 jmcneill *(int *)data = ci->ci_cpuid;
177 1.2.10.1 jmcneill break;
178 1.2.10.1 jmcneill
179 1.2.10.1 jmcneill case IOC_CPU_GETCOUNT:
180 1.2.10.1 jmcneill *(int *)data = ncpu;
181 1.2.10.1 jmcneill break;
182 1.2.10.1 jmcneill
183 1.2.10.1 jmcneill default:
184 1.2.10.1 jmcneill error = ENOTTY;
185 1.2.10.1 jmcneill break;
186 1.2.10.1 jmcneill }
187 1.2.10.1 jmcneill mutex_exit(&cpu_lock);
188 1.2.10.1 jmcneill
189 1.2.10.1 jmcneill return error;
190 1.2.10.1 jmcneill }
191 1.2.10.1 jmcneill
192 1.2.10.1 jmcneill struct cpu_info *
193 1.2.10.1 jmcneill cpu_lookup(cpuid_t id)
194 1.2.10.1 jmcneill {
195 1.2.10.1 jmcneill CPU_INFO_ITERATOR cii;
196 1.2.10.1 jmcneill struct cpu_info *ci;
197 1.2.10.1 jmcneill
198 1.2.10.1 jmcneill for (CPU_INFO_FOREACH(cii, ci)) {
199 1.2.10.1 jmcneill if (ci->ci_cpuid == id)
200 1.2.10.1 jmcneill return ci;
201 1.2.10.1 jmcneill }
202 1.2.10.1 jmcneill
203 1.2.10.1 jmcneill return NULL;
204 1.2.10.1 jmcneill }
205 1.2.10.1 jmcneill
206 1.2.10.1 jmcneill int
207 1.2.10.1 jmcneill cpu_setonline(struct cpu_info *ci, bool online)
208 1.2.10.1 jmcneill {
209 1.2.10.1 jmcneill struct schedstate_percpu *spc;
210 1.2.10.1 jmcneill CPU_INFO_ITERATOR cii;
211 1.2.10.1 jmcneill struct cpu_info *ci2;
212 1.2.10.1 jmcneill int nonline;
213 1.2.10.1 jmcneill
214 1.2.10.1 jmcneill spc = &ci->ci_schedstate;
215 1.2.10.1 jmcneill
216 1.2.10.1 jmcneill KASSERT(mutex_owned(&cpu_lock));
217 1.2.10.1 jmcneill
218 1.2.10.1 jmcneill if (online) {
219 1.2.10.1 jmcneill if ((spc->spc_flags & SPCF_OFFLINE) == 0)
220 1.2.10.1 jmcneill return 0;
221 1.2.10.1 jmcneill spc_lock(ci);
222 1.2.10.1 jmcneill spc->spc_flags &= ~SPCF_OFFLINE;
223 1.2.10.1 jmcneill cpu_need_resched(ci, true);
224 1.2.10.1 jmcneill spc_unlock(ci);
225 1.2.10.1 jmcneill spc->spc_lastmod = time_second;
226 1.2.10.1 jmcneill } else {
227 1.2.10.1 jmcneill if ((spc->spc_flags & SPCF_OFFLINE) != 0)
228 1.2.10.1 jmcneill return 0;
229 1.2.10.1 jmcneill nonline = 0;
230 1.2.10.1 jmcneill for (CPU_INFO_FOREACH(cii, ci2)) {
231 1.2.10.1 jmcneill nonline += ((ci2->ci_schedstate.spc_flags &
232 1.2.10.1 jmcneill SPCF_OFFLINE) == 0);
233 1.2.10.1 jmcneill }
234 1.2.10.1 jmcneill if (nonline == 1)
235 1.2.10.1 jmcneill return EBUSY;
236 1.2.10.1 jmcneill spc_lock(ci);
237 1.2.10.1 jmcneill spc->spc_flags |= SPCF_OFFLINE;
238 1.2.10.1 jmcneill cpu_need_resched(ci, true);
239 1.2.10.1 jmcneill spc_unlock(ci);
240 1.2.10.1 jmcneill do {
241 1.2.10.1 jmcneill kpause("cpu", false, 1, NULL);
242 1.2.10.1 jmcneill #ifdef MULTIPROCESOR
243 1.2.10.1 jmcneill } while (ci->ci_curlwp != ci->ci_data.cpu_idlelwp);
244 1.2.10.1 jmcneill #else
245 1.2.10.1 jmcneill } while (0);
246 1.2.10.1 jmcneill #endif
247 1.2.10.1 jmcneill spc->spc_lastmod = time_second;
248 1.2.10.1 jmcneill }
249 1.2.10.1 jmcneill
250 1.2.10.1 jmcneill return 0;
251 1.2.10.1 jmcneill }
252