include: gcc 7's cpp has problems with the line continuations in .x files
[unleashed.git] / kernel / os / softint.c
blobecdb038c79aa571d02e211e6827f251afa9b4987
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/t_lock.h>
29 #include <sys/systm.h>
30 #include <sys/spl.h>
31 #include <sys/cmn_err.h>
32 #include <sys/debug.h>
33 #include <sys/kdi_impl.h>
34 #include <sys/cpuvar.h>
35 #include <sys/cpuvar.h>
36 #include <sys/archsystm.h>
39 * Handle software interrupts through 'softcall' mechanism
41 * At present softcall mechanism uses a global list headed by softhead.
42 * Entries are added to tail and removed from head so as to preserve FIFO
43 * nature of entries in the softcall list. softcall() takes care of adding
44 * entries to the softtail.
46 * softint must take care of executing the entries in the FIFO
47 * order. It could be called simultaneously from multiple cpus, however only
48 * one instance of softint should process the softcall list with the exception
49 * when CPU is stuck due to high interrupt load and can't execute callbacks.
50 * State diagram is as follows :-
52 * - Upper half which is same as old state machine
53 * (IDLE->PEND->DRAIN->IDLE)
55 * - Lower half which steals the entries from softcall queue and execute
56 * in the context of softint interrupt handler. The interrupt handler
57 * is fired on a different CPU by sending a cross-call.
59 * Starting state is IDLE.
61 * softint()
64 * (c)
65 * ____________________________________________________
66 * | ^ ^
67 * v (a) | (b) |
68 * IDLE--------------------->PEND--------------------->DRAIN
69 * ^ | |
70 * | | |
71 * | | |
72 * | | |
73 * | | |
74 * | d d
75 * | | |
76 * | v v
77 * | PEND DRAIN
78 * | (e) & &
79 * |<-----------------------STEAL STEAL
80 * ^ |
81 * | |
82 * | (e) v
83 * |_________________________<__________________________|
87 * Edge (a)->(b)->(c) are same as old state machine and these
88 * are mutually exclusive state.
90 * a - When an entry is being enqueued to softcall queue then the state
91 * moves from IDLE to PEND.
93 * b - When interrupt handler has started processing softcall queue.
95 * c - When interrupt handler finished processing softcall queue, the
96 * state of machines goes back to IDLE.
98 * d - softcall() generates another softlevel1 iff interrupt handler
99 * hasn't run recently.
101 * e - Either PEND|STEAL or DRAIN|STEAL is set. We let softlevel1
102 * handler exit because we have processed all the entries.
104 * When CPU is being pinned by higher level interrupts for more than
105 * softcall_delay clock ticks, SOFT_STEAL is OR'ed so that softlevel1
106 * handler on the other CPU can drain the queue.
108 * These states are needed for softcall mechanism since Solaris has only
109 * one interface (ie. siron ) as of now for :
111 * - raising a soft interrupt architecture independently (ie not through
112 * setsoftint(..) )
113 * - to process the softcall queue.
116 #define NSOFTCALLS 200
119 * Defined states for softcall processing.
121 #define SOFT_IDLE 0x01 /* no processing is needed */
122 #define SOFT_PEND 0x02 /* softcall list needs processing */
123 #define SOFT_DRAIN 0x04 /* list is being processed */
124 #define SOFT_STEAL 0x08 /* list is being stolen for draining */
126 typedef struct softcall {
127 void (*sc_func)(void *); /* function to call */
128 void *sc_arg; /* arg to pass to func */
129 struct softcall *sc_next; /* next in list */
130 } softcall_t;
133 * softcall list and state variables.
135 static softcall_t *softcalls;
136 static softcall_t *softhead, *softtail, *softfree;
137 static uint_t softcall_state;
138 static clock_t softcall_tick;
139 static clock_t softcall_countstart, softcall_lastpoke;
140 static uint_t softcall_pokecount;
143 * Max number of pokes per second before increasing softcall_delay
145 uint_t softcall_pokemax = 10;
148 * This ensures that softcall entries don't get stuck for long. It's expressed
149 * in 10 milliseconds as 1 unit. When hires_tick is set or other clock frequency
150 * is used, softcall_init() ensures that it's still expressed as 1 = 10 milli
151 * seconds.
153 unsigned int softcall_delay = 1;
156 * The last CPU which will drain softcall queue.
158 static int softcall_latest_cpuid = -1;
161 * CPUSET to hold the CPU which is processing softcall queue
162 * currently. There can be more than one CPU having bit set
163 * but it will happen only when they are stuck.
165 static cpuset_t *softcall_cpuset = NULL;
168 * protects softcall lists and control variable softcall_state.
170 static kmutex_t softcall_lock;
172 static void (*kdi_softcall_func)(void);
173 extern void siron_poke_cpu(cpuset_t);
175 extern void siron(void);
176 extern void kdi_siron(void);
179 void
180 softcall_init(void)
182 softcall_t *sc;
184 softcalls = kmem_zalloc(sizeof (softcall_t) * NSOFTCALLS, KM_SLEEP);
185 softcall_cpuset = kmem_zalloc(sizeof (cpuset_t), KM_SLEEP);
186 for (sc = softcalls; sc < &softcalls[NSOFTCALLS]; sc++) {
187 sc->sc_next = softfree;
188 softfree = sc;
190 mutex_init(&softcall_lock, NULL, MUTEX_SPIN,
191 (void *)ipltospl(SPL8));
192 softcall_state = SOFT_IDLE;
193 softcall_tick = ddi_get_lbolt();
196 * Since softcall_delay is expressed as 1 = 10 milliseconds.
198 softcall_delay = softcall_delay * (hz/100);
199 CPUSET_ZERO(*softcall_cpuset);
203 * Gets called when softcall queue is not moving forward. We choose
204 * a CPU and poke except the ones which are already poked.
206 static int
207 softcall_choose_cpu()
209 cpu_t *cplist = CPU;
210 cpu_t *cp;
211 int intr_load = INT_MAX;
212 int cpuid = -1;
213 cpuset_t poke;
214 int s;
216 ASSERT(getpil() >= DISP_LEVEL);
217 ASSERT(ncpus > 1);
218 ASSERT(MUTEX_HELD(&softcall_lock));
220 CPUSET_ZERO(poke);
223 * The hint is to start from current CPU.
225 cp = cplist;
226 do {
228 * Don't select this CPU if :
229 * - in cpuset already
230 * - CPU is not accepting interrupts
231 * - CPU is being offlined
233 if (CPU_IN_SET(*softcall_cpuset, cp->cpu_id) ||
234 (cp->cpu_flags & CPU_ENABLE) == 0 ||
235 (cp == cpu_inmotion))
236 continue;
237 #if defined(__x86)
239 * Don't select this CPU if a hypervisor indicates it
240 * isn't currently scheduled onto a physical cpu. We are
241 * looking for a cpu that can respond quickly and the time
242 * to get the virtual cpu scheduled and switched to running
243 * state is likely to be relatively lengthy.
245 if (vcpu_on_pcpu(cp->cpu_id) == VCPU_NOT_ON_PCPU)
246 continue;
247 #endif /* __x86 */
249 /* if CPU is not busy */
250 if (cp->cpu_intrload == 0) {
251 cpuid = cp->cpu_id;
252 break;
255 if (cp->cpu_intrload < intr_load) {
256 cpuid = cp->cpu_id;
257 intr_load = cp->cpu_intrload;
258 } else if (cp->cpu_intrload == intr_load) {
260 * We want to poke CPUs having similar
261 * load because we don't know which CPU is
262 * can acknowledge level1 interrupt. The
263 * list of such CPUs should not be large.
265 if (cpuid != -1) {
267 * Put the last CPU chosen because
268 * it also has same interrupt load.
270 CPUSET_ADD(poke, cpuid);
271 cpuid = -1;
274 CPUSET_ADD(poke, cp->cpu_id);
276 } while ((cp = cp->cpu_next_onln) != cplist);
278 /* if we found a CPU which suits best to poke */
279 if (cpuid != -1) {
280 CPUSET_ZERO(poke);
281 CPUSET_ADD(poke, cpuid);
284 if (CPUSET_ISNULL(poke)) {
285 mutex_exit(&softcall_lock);
286 return (0);
290 * We first set the bit in cpuset and then poke.
292 CPUSET_XOR(*softcall_cpuset, poke);
293 mutex_exit(&softcall_lock);
296 * If softcall() was called at low pil then we may
297 * get preempted before we raise PIL. It should be okay
298 * because we are just going to poke CPUs now or at most
299 * another thread may start choosing CPUs in this routine.
301 s = splhigh();
302 siron_poke_cpu(poke);
303 splx(s);
304 return (1);
309 * Call function func with argument arg
310 * at some later time at software interrupt priority
312 void
313 softcall(void (*func)(void *), void *arg)
315 softcall_t *sc;
316 clock_t w, now;
319 * protect against cross-calls
321 mutex_enter(&softcall_lock);
322 /* coalesce identical softcalls */
323 for (sc = softhead; sc != 0; sc = sc->sc_next) {
324 if (sc->sc_func == func && sc->sc_arg == arg) {
325 goto intr;
329 if ((sc = softfree) == 0)
330 panic("too many softcalls");
332 softfree = sc->sc_next;
333 sc->sc_func = func;
334 sc->sc_arg = arg;
335 sc->sc_next = 0;
337 if (softhead) {
338 softtail->sc_next = sc;
339 softtail = sc;
340 } else
341 softhead = softtail = sc;
343 intr:
344 if (softcall_state & SOFT_IDLE) {
345 softcall_state = SOFT_PEND;
346 softcall_tick = ddi_get_lbolt();
347 mutex_exit(&softcall_lock);
348 siron();
349 } else if (softcall_state & (SOFT_DRAIN|SOFT_PEND)) {
350 now = ddi_get_lbolt();
351 w = now - softcall_tick;
352 if (w <= softcall_delay || ncpus == 1) {
353 mutex_exit(&softcall_lock);
354 return;
357 * Did we poke less than a second ago?
359 if (now - softcall_lastpoke < hz) {
361 * We did, increment the poke count and
362 * see if we are poking too often
364 if (softcall_pokecount++ == 0)
365 softcall_countstart = now;
366 if (softcall_pokecount > softcall_pokemax) {
368 * If poking too much increase the delay
370 if (now - softcall_countstart <= hz)
371 softcall_delay++;
372 softcall_pokecount = 0;
374 } else {
376 * poke rate has dropped off, reset the poke monitor
378 softcall_pokecount = 0;
380 softcall_lastpoke = now;
381 if (!(softcall_state & SOFT_STEAL)) {
382 softcall_state |= SOFT_STEAL;
385 * We want to give some more chance before
386 * fishing around again.
388 softcall_tick = now;
391 /* softcall_lock will be released by this routine */
392 (void) softcall_choose_cpu();
396 void
397 kdi_softcall(void (*func)(void))
399 kdi_softcall_func = func;
401 if (softhead == NULL)
402 kdi_siron();
406 * Called to process software interrupts take one off queue, call it,
407 * repeat.
409 * Note queue may change during call; softcall_lock, state variables
410 * softcall_state and softcall_latest_cpuid ensures that -
411 * - we don't have multiple cpus pulling from the list (thus causing
412 * a violation of FIFO order with an exception when we are stuck).
413 * - we don't miss a new entry having been added to the head.
414 * - we don't miss a wakeup.
417 void
418 softint(void)
420 softcall_t *sc = NULL;
421 void (*func)();
422 caddr_t arg;
423 int cpu_id = CPU->cpu_id;
426 * Don't process softcall queue if current CPU is quiesced or
427 * offlined. This can happen when a CPU is running pause
428 * thread but softcall already sent a xcall.
430 if (CPU->cpu_flags & (CPU_QUIESCED|CPU_OFFLINE)) {
431 if (softcall_cpuset != NULL &&
432 CPU_IN_SET(*softcall_cpuset, cpu_id)) {
433 CPUSET_DEL(*softcall_cpuset, cpu_id);
434 goto out;
438 mutex_enter(&softcall_lock);
440 if (softcall_state & (SOFT_STEAL|SOFT_PEND)) {
441 softcall_state = SOFT_DRAIN;
442 } else {
444 * The check for softcall_cpuset being
445 * NULL is required because it may get
446 * called very early during boot.
448 if (softcall_cpuset != NULL &&
449 CPU_IN_SET(*softcall_cpuset, cpu_id))
450 CPUSET_DEL(*softcall_cpuset, cpu_id);
451 mutex_exit(&softcall_lock);
452 goto out;
456 * Setting softcall_latest_cpuid to current CPU ensures
457 * that there is only one active softlevel1 handler to
458 * process softcall queues.
460 * Since softcall_lock lock is dropped before calling
461 * func (callback), we need softcall_latest_cpuid
462 * to prevent two softlevel1 hanlders working on the
463 * queue when the first softlevel1 handler gets
464 * stuck due to high interrupt load.
466 softcall_latest_cpuid = cpu_id;
468 /* add ourself to the cpuset */
469 if (!CPU_IN_SET(*softcall_cpuset, cpu_id))
470 CPUSET_ADD(*softcall_cpuset, cpu_id);
472 for (;;) {
473 softcall_tick = ddi_get_lbolt();
474 if ((sc = softhead) != NULL) {
475 func = sc->sc_func;
476 arg = sc->sc_arg;
477 softhead = sc->sc_next;
478 sc->sc_next = softfree;
479 softfree = sc;
482 if (sc == NULL) {
483 if (CPU_IN_SET(*softcall_cpuset, cpu_id))
484 CPUSET_DEL(*softcall_cpuset, cpu_id);
486 softcall_state = SOFT_IDLE;
487 ASSERT(softcall_latest_cpuid == cpu_id);
488 softcall_latest_cpuid = -1;
490 mutex_exit(&softcall_lock);
491 break;
494 mutex_exit(&softcall_lock);
495 func(arg);
496 mutex_enter(&softcall_lock);
499 * No longer need softcall processing from current
500 * interrupt handler because either
501 * (a) softcall is in SOFT_IDLE state or
502 * (b) There is a CPU already draining softcall
503 * queue and the current softlevel1 is no
504 * longer required.
506 if (softcall_latest_cpuid != cpu_id) {
507 if (CPU_IN_SET(*softcall_cpuset, cpu_id))
508 CPUSET_DEL(*softcall_cpuset, cpu_id);
510 mutex_exit(&softcall_lock);
511 break;
515 out:
516 if ((func = kdi_softcall_func) != NULL) {
517 kdi_softcall_func = NULL;
518 func();