1100b72f4Sandrei /*
2100b72f4Sandrei  * CDDL HEADER START
3100b72f4Sandrei  *
4100b72f4Sandrei  * The contents of this file are subject to the terms of the
5100b72f4Sandrei  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
7100b72f4Sandrei  *
8100b72f4Sandrei  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9100b72f4Sandrei  * or http://www.opensolaris.org/os/licensing.
10100b72f4Sandrei  * See the License for the specific language governing permissions
11100b72f4Sandrei  * and limitations under the License.
12100b72f4Sandrei  *
13100b72f4Sandrei  * When distributing Covered Code, include this CDDL HEADER in each
14100b72f4Sandrei  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15100b72f4Sandrei  * If applicable, add the following below this CDDL HEADER, with the
16100b72f4Sandrei  * fields enclosed by brackets "[]" replaced with your own identifying
17100b72f4Sandrei  * information: Portions Copyright [yyyy] [name of copyright owner]
18100b72f4Sandrei  *
19100b72f4Sandrei  * CDDL HEADER END
20100b72f4Sandrei  */
21100b72f4Sandrei 
22100b72f4Sandrei /*
23100b72f4Sandrei  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24100b72f4Sandrei  * Use is subject to license terms.
25100b72f4Sandrei  */
26*f1b92f9dSRobert Mustacchi /*
27*f1b92f9dSRobert Mustacchi  * Copyright 2015, Joyent, Inc.
28*f1b92f9dSRobert Mustacchi  */
29100b72f4Sandrei 
30100b72f4Sandrei #include <sys/cpuvar.h>
31100b72f4Sandrei #include <sys/stack.h>
32100b72f4Sandrei #include <vm/seg_kp.h>
33*f1b92f9dSRobert Mustacchi #include <sys/machparam.h>
34100b72f4Sandrei #include <sys/proc.h>
35100b72f4Sandrei #include <sys/pset.h>
36100b72f4Sandrei #include <sys/sysmacros.h>
37100b72f4Sandrei 
38100b72f4Sandrei /*
39100b72f4Sandrei  * Create and initialize an interrupt thread.
40100b72f4Sandrei  */
41100b72f4Sandrei static void
thread_create_intr(cpu_t * cp)42100b72f4Sandrei thread_create_intr(cpu_t *cp)
43100b72f4Sandrei {
44100b72f4Sandrei 	kthread_t *tp;
45100b72f4Sandrei 
46*f1b92f9dSRobert Mustacchi 	tp = thread_create(NULL, LL_INTR_STKSZ,
47100b72f4Sandrei 	    (void (*)())thread_create_intr, NULL, 0, &p0, TS_ONPROC, 0);
48100b72f4Sandrei 
49100b72f4Sandrei 	/*
50100b72f4Sandrei 	 * Set the thread in the TS_FREE state.  The state will change
51100b72f4Sandrei 	 * to TS_ONPROC only while the interrupt is active.  Think of these
52100b72f4Sandrei 	 * as being on a private free list for the CPU.  Being TS_FREE keeps
53100b72f4Sandrei 	 * inactive interrupt threads out of debugger thread lists.
54100b72f4Sandrei 	 *
55100b72f4Sandrei 	 * We cannot call thread_create with TS_FREE because of the current
56100b72f4Sandrei 	 * checks there for ONPROC.  Fix this when thread_create takes flags.
57100b72f4Sandrei 	 */
58100b72f4Sandrei 	THREAD_FREEINTR(tp, cp);
59100b72f4Sandrei 
60100b72f4Sandrei 	/*
61100b72f4Sandrei 	 * Nobody should ever reference the credentials of an interrupt
62100b72f4Sandrei 	 * thread so make it NULL to catch any such references.
63100b72f4Sandrei 	 */
64100b72f4Sandrei 	tp->t_cred = NULL;
65100b72f4Sandrei 	tp->t_flag |= T_INTR_THREAD;
66100b72f4Sandrei 	tp->t_cpu = cp;
67100b72f4Sandrei 	tp->t_bound_cpu = cp;
68100b72f4Sandrei 	tp->t_disp_queue = cp->cpu_disp;
69100b72f4Sandrei 	tp->t_affinitycnt = 1;
70100b72f4Sandrei 	tp->t_preempt = 1;
71100b72f4Sandrei 
72100b72f4Sandrei 	/*
73100b72f4Sandrei 	 * Don't make a user-requested binding on this thread so that
74100b72f4Sandrei 	 * the processor can be offlined.
75100b72f4Sandrei 	 */
76100b72f4Sandrei 	tp->t_bind_cpu = PBIND_NONE;	/* no USER-requested binding */
77100b72f4Sandrei 	tp->t_bind_pset = PS_NONE;
78100b72f4Sandrei 
7986ef0a63SRichard Lowe #if defined(__x86)
80100b72f4Sandrei 	tp->t_stk -= STACK_ALIGN;
81100b72f4Sandrei 	*(tp->t_stk) = 0;		/* terminate intr thread stack */
82100b72f4Sandrei #endif
83100b72f4Sandrei 
84100b72f4Sandrei 	/*
85100b72f4Sandrei 	 * Link onto CPU's interrupt pool.
86100b72f4Sandrei 	 */
87100b72f4Sandrei 	tp->t_link = cp->cpu_intr_thread;
88100b72f4Sandrei 	cp->cpu_intr_thread = tp;
89100b72f4Sandrei }
90100b72f4Sandrei 
91100b72f4Sandrei /*
92*f1b92f9dSRobert Mustacchi  * Allocate a given number of interrupt threads for a given CPU.  These threads
93*f1b92f9dSRobert Mustacchi  * will get freed by cpu_destroy_bound_threads() when the CPU gets unconfigured.
94*f1b92f9dSRobert Mustacchi  *
95*f1b92f9dSRobert Mustacchi  * Note, high level interrupts are always serviced using cpu_intr_stack and are
96*f1b92f9dSRobert Mustacchi  * not allowed to block. Low level interrupts or soft-interrupts use the
97*f1b92f9dSRobert Mustacchi  * kthread_t's that we create through the calls to thread_create_intr().
98100b72f4Sandrei  */
99100b72f4Sandrei void
cpu_intr_alloc(cpu_t * cp,int n)100100b72f4Sandrei cpu_intr_alloc(cpu_t *cp, int n)
101100b72f4Sandrei {
102100b72f4Sandrei 	int i;
103100b72f4Sandrei 
104100b72f4Sandrei 	for (i = 0; i < n; i++)
105100b72f4Sandrei 		thread_create_intr(cp);
106100b72f4Sandrei 
107100b72f4Sandrei 	cp->cpu_intr_stack = (caddr_t)segkp_get(segkp, INTR_STACK_SIZE,
10886ef0a63SRichard Lowe 	    KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED) +
10986ef0a63SRichard Lowe 	    INTR_STACK_SIZE - SA(MINFRAME);
110100b72f4Sandrei }
111