xref: /illumos-gate/usr/src/uts/sun4/os/mp_startup.c (revision 70f54ead78cbbb40e97bd3ff9f175c5c71783c3c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/sysmacros.h>
30 #include <sys/prom_plat.h>
31 #include <sys/prom_debug.h>
32 #include <vm/hat_sfmmu.h>
33 #include <vm/seg_kp.h>
34 #include <vm/seg_kmem.h>
35 #include <sys/machsystm.h>
36 #include <sys/callb.h>
37 #include <sys/cpu_module.h>
38 #include <sys/chip.h>
39 #include <sys/dtrace.h>
40 #include <sys/reboot.h>
41 #include <sys/kdi.h>
42 
43 #ifdef TRAPTRACE
44 #include <sys/traptrace.h>
45 #include <sys/bootconf.h>
46 #endif /* TRAPTRACE */
47 
48 #include <sys/cpu_sgnblk_defs.h>
49 
50 extern void cpu_intrq_setup(struct cpu *);
51 extern void cpu_intrq_register(struct cpu *);
52 
53 struct cpu	*cpus;	/* pointer to other cpus; dynamically allocate */
54 struct cpu	*cpu[NCPU];	/* pointers to all CPUs */
55 uint64_t	cpu_pa[NCPU];	/* pointers to all CPUs in PA */
56 cpu_core_t	cpu_core[NCPU];	/* cpu_core structures */
57 
58 #ifdef TRAPTRACE
59 caddr_t	ttrace_buf;	/* bop alloced traptrace for all cpus except 0 */
60 #endif /* TRAPTRACE */
61 
62 /* bit mask of cpus ready for x-calls, protected by cpu_lock */
63 cpuset_t cpu_ready_set;
64 
65 /* bit mask used to communicate with cpus during bringup */
66 static cpuset_t proxy_ready_set;
67 
68 static void	slave_startup(void);
69 
70 /*
71  * Defined in $KARCH/os/mach_mp_startup.c
72  */
73 #pragma weak init_cpu_info
74 
75 /*
76  * Amount of time (in milliseconds) we should wait before giving up on CPU
77  * initialization and assuming that the CPU we're trying to wake up is dead
78  * or out of control.
79  */
80 #define	CPU_WAKEUP_GRACE_MSEC 1000
81 
82 /*
83  * MP configurations may reserve additional interrupt request entries.
84  * intr_add_{div,max} can be modified to tune memory usage.
85  */
86 
87 uint_t	intr_add_div = 1;			/* 1=worst case memory usage */
88 size_t	intr_add_max = 0;
89 
90 /* intr_add_{pools,head,tail} calculated based on intr_add_{div,max} */
91 
92 size_t	intr_add_pools = 0;			/* additional pools per cpu */
93 struct intr_req	*intr_add_head = (struct intr_req *)NULL;
94 #ifdef	DEBUG
95 struct intr_req	*intr_add_tail = (struct intr_req *)NULL;
96 #endif	/* DEBUG */
97 
98 
99 #ifdef	TRAPTRACE
100 /*
101  * This function bop allocs traptrace buffers for all cpus
102  * other than boot cpu.
103  */
104 caddr_t
105 trap_trace_alloc(caddr_t base)
106 {
107 	caddr_t	vaddr;
108 	extern int max_ncpus;
109 
110 	if (max_ncpus == 1) {
111 		return (base);
112 	}
113 
114 	if ((vaddr = (caddr_t)BOP_ALLOC(bootops, base, (TRAP_TBUF_SIZE *
115 		(max_ncpus - 1)), TRAP_TBUF_SIZE)) == NULL) {
116 		panic("traptrace_alloc: can't bop alloc");
117 	}
118 	ttrace_buf = vaddr;
119 	PRM_DEBUG(ttrace_buf);
120 	return (vaddr + (TRAP_TBUF_SIZE * (max_ncpus - 1)));
121 }
122 #endif	/* TRAPTRACE */
123 
124 /*
125  * common slave cpu initialization code
126  */
127 void
128 common_startup_init(cpu_t *cp, int cpuid)
129 {
130 	kthread_id_t tp;
131 	sfmmu_t *sfmmup;
132 	caddr_t	sp;
133 
134 	/*
135 	 * Allocate and initialize the startup thread for this CPU.
136 	 */
137 	tp = thread_create(NULL, 0, slave_startup, NULL, 0, &p0,
138 	    TS_STOPPED, maxclsyspri);
139 
140 	/*
141 	 * Set state to TS_ONPROC since this thread will start running
142 	 * as soon as the CPU comes online.
143 	 *
144 	 * All the other fields of the thread structure are setup by
145 	 * thread_create().
146 	 */
147 	THREAD_ONPROC(tp, cp);
148 	tp->t_preempt = 1;
149 	tp->t_bound_cpu = cp;
150 	tp->t_affinitycnt = 1;
151 	tp->t_cpu = cp;
152 	tp->t_disp_queue = cp->cpu_disp;
153 
154 	sfmmup = astosfmmu(&kas);
155 	CPUSET_ADD(sfmmup->sfmmu_cpusran, cpuid);
156 
157 	/*
158 	 * Setup thread to start in slave_startup.
159 	 */
160 	sp = tp->t_stk;
161 	tp->t_pc = (uintptr_t)slave_startup - 8;
162 	tp->t_sp = (uintptr_t)((struct rwindow *)sp - 1) - STACK_BIAS;
163 
164 	cp->cpu_id = cpuid;
165 	cp->cpu_self = cp;
166 	cp->cpu_thread = tp;
167 	cp->cpu_lwp = NULL;
168 	cp->cpu_dispthread = tp;
169 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
170 }
171 
172 /*
173  * parametric flag setting functions.  these routines set the cpu
174  * state just prior to releasing the slave cpu.
175  */
176 void
177 cold_flag_set(int cpuid)
178 {
179 	cpu_t *cp;
180 
181 	ASSERT(MUTEX_HELD(&cpu_lock));
182 
183 	cp = cpu[cpuid];
184 	cp->cpu_flags |= CPU_RUNNING | CPU_ENABLE | CPU_EXISTS;
185 	cpu_add_active(cp);
186 	/*
187 	 * Add CPU_READY after the cpu_add_active() call
188 	 * to avoid pausing cp.
189 	 */
190 	cp->cpu_flags |= CPU_READY;		/* ready */
191 	cpu_set_state(cp);
192 }
193 
194 static void
195 warm_flag_set(int cpuid)
196 {
197 	cpu_t *cp;
198 
199 	ASSERT(MUTEX_HELD(&cpu_lock));
200 
201 	/*
202 	 * warm start activates cpus into the OFFLINE state
203 	 */
204 	cp = cpu[cpuid];
205 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS
206 		| CPU_OFFLINE | CPU_QUIESCED;
207 	cpu_set_state(cp);
208 }
209 
210 /*
211  * Internal cpu startup sequencer
212  * The sequence is as follows:
213  *
214  * MASTER	SLAVE
215  * -------	----------
216  * assume the kernel data is initialized
217  * clear the proxy bit
218  * start the slave cpu
219  * wait for the slave cpu to set the proxy
220  *
221  *		the slave runs slave_startup and then sets the proxy
222  *		the slave waits for the master to add slave to the ready set
223  *
224  * the master finishes the initialization and
225  * adds the slave to the ready set
226  *
227  *		the slave exits the startup thread and is running
228  */
229 void
230 start_cpu(int cpuid, void(*flag_func)(int))
231 {
232 	extern caddr_t cpu_startup;
233 	int timout;
234 
235 	ASSERT(MUTEX_HELD(&cpu_lock));
236 
237 	/*
238 	 * Before we begin the dance, tell DTrace that we're about to start
239 	 * a CPU.
240 	 */
241 	if (dtrace_cpustart_init != NULL)
242 		(*dtrace_cpustart_init)();
243 
244 	/* start the slave cpu */
245 	CPUSET_DEL(proxy_ready_set, cpuid);
246 	if (prom_test("SUNW,start-cpu-by-cpuid") == 0) {
247 		(void) prom_startcpu_bycpuid(cpuid, (caddr_t)&cpu_startup,
248 		    cpuid);
249 	} else {
250 		/* "by-cpuid" interface didn't exist.  Do it the old way */
251 		dnode_t nodeid = cpunodes[cpuid].nodeid;
252 
253 		ASSERT(nodeid != (dnode_t)0);
254 		(void) prom_startcpu(nodeid, (caddr_t)&cpu_startup, cpuid);
255 	}
256 
257 	/* wait for the slave cpu to check in. */
258 	for (timout = CPU_WAKEUP_GRACE_MSEC; timout; timout--) {
259 		if (CPU_IN_SET(proxy_ready_set, cpuid))
260 			break;
261 		DELAY(1000);
262 	}
263 	if (timout == 0) {
264 		panic("cpu%d failed to start (2)", cpuid);
265 	}
266 
267 	/*
268 	 * The slave has started; we can tell DTrace that it's safe again.
269 	 */
270 	if (dtrace_cpustart_fini != NULL)
271 		(*dtrace_cpustart_fini)();
272 
273 	/* run the master side of stick synchronization for the slave cpu */
274 	sticksync_master();
275 
276 	/*
277 	 * deal with the cpu flags in a phase-specific manner
278 	 * for various reasons, this needs to run after the slave
279 	 * is checked in but before the slave is released.
280 	 */
281 	(*flag_func)(cpuid);
282 
283 	/* release the slave */
284 	CPUSET_ADD(cpu_ready_set, cpuid);
285 }
286 
287 #ifdef TRAPTRACE
288 int trap_tr0_inuse = 1;	/* it is always used on the boot cpu */
289 int trap_trace_inuse[NCPU];
290 #endif /* TRAPTRACE */
291 
292 #define	cpu_next_free	cpu_prev
293 
294 /*
295  * Routine to set up a CPU to prepare for starting it up.
296  */
297 void
298 setup_cpu_common(int cpuid)
299 {
300 	struct cpu *cp = NULL;
301 	kthread_id_t tp;
302 #ifdef TRAPTRACE
303 	int tt_index;
304 	TRAP_TRACE_CTL	*ctlp;
305 	caddr_t	newbuf;
306 #endif /* TRAPTRACE */
307 
308 	extern void idle();
309 	extern void init_intr_threads(struct cpu *);
310 
311 	ASSERT(MUTEX_HELD(&cpu_lock));
312 	ASSERT(cpu[cpuid] == NULL);
313 
314 	ASSERT(ncpus <= max_ncpus);
315 
316 #ifdef TRAPTRACE
317 	/*
318 	 * allocate a traptrace buffer for this CPU.
319 	 */
320 	ctlp = &trap_trace_ctl[cpuid];
321 	if (!trap_tr0_inuse) {
322 		trap_tr0_inuse = 1;
323 		newbuf = trap_tr0;
324 		tt_index = -1;
325 	} else {
326 		for (tt_index = 0; tt_index < (max_ncpus-1); tt_index++)
327 			if (!trap_trace_inuse[tt_index])
328 			    break;
329 		ASSERT(tt_index < max_ncpus - 1);
330 		trap_trace_inuse[tt_index] = 1;
331 		newbuf = (caddr_t)(ttrace_buf + (tt_index * TRAP_TBUF_SIZE));
332 	}
333 	ctlp->d.vaddr_base = newbuf;
334 	ctlp->d.offset = ctlp->d.last_offset = 0;
335 	ctlp->d.limit = trap_trace_bufsize;
336 	ctlp->d.paddr_base = va_to_pa(newbuf);
337 	ASSERT(ctlp->d.paddr_base != (uint64_t)-1);
338 	/*
339 	 * initialize HV trap trace buffer for other cpus
340 	 */
341 	htrap_trace_setup((newbuf + TRAP_TSIZE), cpuid);
342 #endif /* TRAPTRACE */
343 
344 	/*
345 	 * Obtain pointer to the appropriate cpu structure.
346 	 */
347 	if (cpu0.cpu_flags == 0) {
348 		cp = &cpu0;
349 	} else {
350 		/*
351 		 *  When dynamically allocating cpu structs,
352 		 *  cpus is used as a pointer to a list of freed
353 		 *  cpu structs.
354 		 */
355 		if (cpus) {
356 			/* grab the first cpu struct on the free list */
357 			cp = cpus;
358 			if (cp->cpu_next_free)
359 				cpus = cp->cpu_next_free;
360 			else
361 				cpus = NULL;
362 		}
363 	}
364 
365 	if (cp == NULL)
366 		cp = vmem_xalloc(static_alloc_arena, CPU_ALLOC_SIZE,
367 		    CPU_ALLOC_SIZE, 0, 0, NULL, NULL, VM_SLEEP);
368 
369 	bzero(cp, sizeof (*cp));
370 
371 	cp->cpu_id = cpuid;
372 	cp->cpu_self = cp;
373 
374 	/*
375 	 * Initialize ptl1_panic stack
376 	 */
377 	ptl1_init_cpu(cp);
378 
379 	/*
380 	 * Initialize the dispatcher for this CPU.
381 	 */
382 	disp_cpu_init(cp);
383 
384 	/*
385 	 * Now, initialize per-CPU idle thread for this CPU.
386 	 */
387 	tp = thread_create(NULL, 0, idle, NULL, 0, &p0, TS_ONPROC, -1);
388 
389 	cp->cpu_idle_thread = tp;
390 
391 	tp->t_preempt = 1;
392 	tp->t_bound_cpu = cp;
393 	tp->t_affinitycnt = 1;
394 	tp->t_cpu = cp;
395 	tp->t_disp_queue = cp->cpu_disp;
396 
397 	/*
398 	 * Registering a thread in the callback table is usually
399 	 * done in the initialization code of the thread. In this
400 	 * case, we do it right after thread creation to avoid
401 	 * blocking idle thread while registering itself. It also
402 	 * avoids the possibility of reregistration in case a CPU
403 	 * restarts its idle thread.
404 	 */
405 	CALLB_CPR_INIT_SAFE(tp, "idle");
406 
407 	init_cpu_info(cp);
408 
409 	/*
410 	 * Initialize the interrupt threads for this CPU
411 	 */
412 	init_intr_pool(cp);
413 	init_intr_threads(cp);
414 
415 	/*
416 	 * Add CPU to list of available CPUs.
417 	 * It'll be on the active list after it is started.
418 	 */
419 	cpu_add_unit(cp);
420 
421 	/*
422 	 * Allocate and init cpu module private data structures,
423 	 * including scrubber.
424 	 */
425 	cpu_init_private(cp);
426 
427 	/*
428 	 * Associate this CPU with a physical processor
429 	 */
430 	chip_cpu_init(cp);
431 
432 	cpu_intrq_setup(cp);
433 }
434 
435 /*
436  * Routine to clean up a CPU after shutting it down.
437  */
438 int
439 cleanup_cpu_common(int cpuid)
440 {
441 	struct cpu *cp;
442 #ifdef TRAPTRACE
443 	int i;
444 	TRAP_TRACE_CTL	*ctlp;
445 	caddr_t	newbuf;
446 #endif /* TRAPTRACE */
447 
448 	ASSERT(MUTEX_HELD(&cpu_lock));
449 	ASSERT(cpu[cpuid] != NULL);
450 
451 	cp = cpu[cpuid];
452 
453 	/* Free cpu module private data structures, including scrubber. */
454 	cpu_uninit_private(cp);
455 
456 	/*
457 	 * Remove CPU from list of available CPUs.
458 	 */
459 	cpu_del_unit(cpuid);
460 
461 	/*
462 	 * Clean up the interrupt pool.
463 	 */
464 	cleanup_intr_pool(cp);
465 
466 	/*
467 	 * At this point, the only threads bound to this CPU should be
468 	 * special per-cpu threads: it's idle thread, it's pause thread,
469 	 * and it's interrupt threads.  Clean these up.
470 	 */
471 	cpu_destroy_bound_threads(cp);
472 
473 	/*
474 	 * Free the interrupt stack.
475 	 */
476 	segkp_release(segkp, cp->cpu_intr_stack);
477 
478 #ifdef TRAPTRACE
479 	/*
480 	 * Free the traptrace buffer for this CPU.
481 	 */
482 	ctlp = &trap_trace_ctl[cpuid];
483 	newbuf = ctlp->d.vaddr_base;
484 	i = (newbuf - ttrace_buf) / (TRAP_TBUF_SIZE);
485 	if (((newbuf - ttrace_buf) % (TRAP_TBUF_SIZE) == 0) &&
486 	    ((i >= 0) && (i < (max_ncpus-1)))) {
487 		/*
488 		 * This CPU got it's trap trace buffer from the
489 		 * boot-alloc'd bunch of them.
490 		 */
491 		trap_trace_inuse[i] = 0;
492 		bzero(newbuf, (TRAP_TBUF_SIZE));
493 	} else if (newbuf == trap_tr0) {
494 		trap_tr0_inuse = 0;
495 		bzero(trap_tr0, (TRAP_TBUF_SIZE));
496 	} else {
497 		cmn_err(CE_WARN, "failed to free trap trace buffer from cpu%d",
498 		    cpuid);
499 	}
500 	bzero(ctlp, sizeof (*ctlp));
501 #endif /* TRAPTRACE */
502 
503 	/*
504 	 * There is a race condition with mutex_vector_enter() which
505 	 * caches a cpu pointer. The race is detected by checking cpu_next.
506 	 */
507 	disp_cpu_fini(cp);
508 	cpu_pa[cpuid] = 0;
509 	bzero(cp, sizeof (*cp));
510 
511 	/*
512 	 * Place the freed cpu structure on the list of freed cpus.
513 	 */
514 	if (cp != &cpu0) {
515 		if (cpus) {
516 			cp->cpu_next_free = cpus;
517 			cpus = cp;
518 		}
519 		else
520 			cpus = cp;
521 	}
522 
523 	return (0);
524 }
525 
526 /*
527  * This routine is used to start a previously powered off processor.
528  * Note that restarted cpus are initialized into the offline state.
529  */
530 void
531 restart_other_cpu(int cpuid)
532 {
533 	struct cpu *cp;
534 	kthread_id_t tp;
535 	caddr_t	sp;
536 	extern void idle();
537 
538 	ASSERT(MUTEX_HELD(&cpu_lock));
539 	ASSERT(cpuid < NCPU && cpu[cpuid] != NULL);
540 
541 	/*
542 	 * Obtain pointer to the appropriate cpu structure.
543 	 */
544 	cp = cpu[cpuid];
545 
546 	common_startup_init(cp, cpuid);
547 
548 	/*
549 	 * idle thread t_lock is held when the idle thread is suspended.
550 	 * Manually unlock the t_lock of idle loop so that we can resume
551 	 * the suspended idle thread.
552 	 * Also adjust the PC of idle thread for re-retry.
553 	 */
554 	cp->cpu_intr_actv = 0;	/* clear the value from previous life */
555 	cp->cpu_m.mutex_ready = 0; /* we are not ready yet */
556 	lock_clear(&cp->cpu_idle_thread->t_lock);
557 	tp = cp->cpu_idle_thread;
558 
559 	sp = tp->t_stk;
560 	tp->t_sp = (uintptr_t)((struct rwindow *)sp - 1) - STACK_BIAS;
561 	tp->t_pc = (uintptr_t)idle - 8;
562 
563 	/*
564 	 * restart the cpu now
565 	 */
566 	promsafe_pause_cpus();
567 	start_cpu(cpuid, warm_flag_set);
568 	start_cpus();
569 
570 	/* call cmn_err outside pause_cpus/start_cpus to avoid deadlock */
571 	cmn_err(CE_CONT, "!cpu%d initialization complete - restarted\n",
572 	    cpuid);
573 }
574 
575 /*
576  * Startup function executed on 'other' CPUs.  This is the first
577  * C function after cpu_start sets up the cpu registers.
578  */
579 static void
580 slave_startup(void)
581 {
582 	struct cpu	*cp = CPU;
583 	ushort_t	original_flags = cp->cpu_flags;
584 
585 #ifdef TRAPTRACE
586 	htrap_trace_register(cp->cpu_id);
587 #endif
588 	cpu_intrq_register(CPU);
589 	cp->cpu_m.mutex_ready = 1;
590 	cp->cpu_m.poke_cpu_outstanding = B_FALSE;
591 
592 	/* acknowledge that we are done with initialization */
593 	CPUSET_ADD(proxy_ready_set, cp->cpu_id);
594 
595 	/* synchronize STICK */
596 	sticksync_slave();
597 
598 	if (boothowto & RB_DEBUG)
599 		kdi_dvec_cpu_init(cp);
600 
601 	/*
602 	 * the slave will wait here forever -- assuming that the master
603 	 * will get back to us.  if it doesn't we've got bigger problems
604 	 * than a master not replying to this slave.
605 	 * the small delay improves the slave's responsiveness to the
606 	 * master's ack and decreases the time window between master and
607 	 * slave operations.
608 	 */
609 	while (!CPU_IN_SET(cpu_ready_set, cp->cpu_id))
610 		DELAY(1);
611 
612 	/* enable interrupts */
613 	(void) spl0();
614 
615 	/*
616 	 * Signature block update to indicate that this CPU is in OS now.
617 	 * This needs to be done after the PIL is lowered since on
618 	 * some platforms the update code may block.
619 	 */
620 	CPU_SIGNATURE(OS_SIG, SIGST_RUN, SIGSUBST_NULL, cp->cpu_id);
621 
622 	/*
623 	 * park the slave thread in a safe/quiet state and wait for the master
624 	 * to finish configuring this CPU before proceeding to thread_exit().
625 	 */
626 	while (((volatile ushort_t)cp->cpu_flags) & CPU_QUIESCED)
627 		DELAY(1);
628 
629 	/*
630 	 * Initialize CPC CPU state.
631 	 */
632 	kcpc_hw_startup_cpu(original_flags);
633 
634 	/*
635 	 * Notify the CMT subsystem that the slave has started
636 	 */
637 	chip_cpu_startup(CPU);
638 
639 	/*
640 	 * Now we are done with the startup thread, so free it up.
641 	 */
642 	thread_exit();
643 	cmn_err(CE_PANIC, "slave_startup: cannot return");
644 	/*NOTREACHED*/
645 }
646 
647 /*
648  * 4163850 changes the allocation method for cpu structs. cpu structs
649  * are dynamically allocated. This routine now determines if additional
650  * per-cpu intr_req entries need to be allocated.
651  */
652 int
653 ndata_alloc_cpus(struct memlist *ndata)
654 {
655 	size_t real_sz;
656 	extern int niobus;
657 
658 	if (niobus > 1) {
659 
660 		/*
661 		 * Allocate additional intr_req entries if we have more than
662 		 * one io bus.  The memory to allocate is calculated from four
663 		 * variables: niobus, max_ncpus, intr_add_div, and intr_add_max.
664 		 * Allocate multiple of INTR_POOL_SIZE bytes (512).  Each cpu
665 		 * already reserves 512 bytes in its machcpu structure, so the
666 		 * worst case is (512 * (niobus - 1) * max_ncpus) add'l bytes.
667 		 *
668 		 * While niobus and max_ncpus reflect the h/w, the following
669 		 * may be tuned (before boot):
670 		 *
671 		 *	intr_add_div -	divisor for scaling the number of
672 		 *			additional intr_req entries. use '1'
673 		 *			for worst case memory, '2' for half,
674 		 *			etc.
675 		 *
676 		 *   intr_add_max - upper limit on bytes of memory to reserve
677 		 */
678 
679 		real_sz = INTR_POOL_SIZE * (niobus - 1) * max_ncpus;
680 
681 		/* tune memory usage by applying divisor and maximum */
682 
683 		if (intr_add_max == 0)
684 			intr_add_max = max_ncpus * INTR_POOL_SIZE;
685 		real_sz = MIN(intr_add_max, real_sz / MAX(intr_add_div, 1));
686 
687 		/* round down to multiple of (max_ncpus * INTR_POOL_SIZE) */
688 
689 		intr_add_pools = real_sz / (max_ncpus * INTR_POOL_SIZE);
690 		real_sz = intr_add_pools * (max_ncpus * INTR_POOL_SIZE);
691 
692 		/* actually reserve the space */
693 
694 		intr_add_head = ndata_alloc(ndata, real_sz, ecache_alignsize);
695 		if (intr_add_head == NULL)
696 			return (-1);
697 
698 		PRM_DEBUG(intr_add_head);
699 #ifdef	DEBUG
700 		intr_add_tail = (struct intr_req *)
701 		    ((uintptr_t)intr_add_head + real_sz);
702 #endif	/* DEBUG */
703 	}
704 
705 	return (0);
706 }
707 
708 
709 extern struct cpu	*cpu[NCPU];	/* pointers to all CPUs */
710 
711 extern void setup_cpu_common(int);
712 extern void common_startup_init(cpu_t *, int);
713 extern void start_cpu(int, void(*func)(int));
714 extern void cold_flag_set(int cpuid);
715 
716 /*
717  * cpu_bringup_set is a tunable (via /etc/system, debugger, etc.) that
718  * can be used during debugging to control which processors are brought
719  * online at boot time.  The variable represents a bitmap of the id's
720  * of the processors that will be brought online.  The initialization
721  * of this variable depends on the type of cpuset_t, which varies
722  * depending on the number of processors supported (see cpuvar.h).
723  */
724 cpuset_t cpu_bringup_set;
725 
726 
727 /*
728  * Generic start-all cpus entry.  Typically used during cold initialization.
729  * Note that cold start cpus are initialized into the online state.
730  */
731 /*ARGSUSED*/
732 void
733 start_other_cpus(int flag)
734 {
735 	int cpuid;
736 	extern void idlestop_init(void);
737 	int bootcpu;
738 
739 	/*
740 	 * Check if cpu_bringup_set has been explicitly set before
741 	 * initializing it.
742 	 */
743 	if (CPUSET_ISNULL(cpu_bringup_set)) {
744 #ifdef MPSAS
745 		/* just CPU 0 */
746 		CPUSET_ADD(cpu_bringup_set, 0);
747 #else
748 		CPUSET_ALL(cpu_bringup_set);
749 #endif
750 	}
751 
752 	if (&cpu_feature_init)
753 		cpu_feature_init();
754 
755 	/*
756 	 * Initialize CPC.
757 	 */
758 	kcpc_hw_init();
759 
760 	mutex_enter(&cpu_lock);
761 
762 	/*
763 	 * Initialize our own cpu_info.
764 	 */
765 	init_cpu_info(CPU);
766 
767 	/*
768 	 * Initialize CPU 0 cpu module private data area, including scrubber.
769 	 */
770 	cpu_init_private(CPU);
771 
772 	/*
773 	 * perform such initialization as is needed
774 	 * to be able to take CPUs on- and off-line.
775 	 */
776 	cpu_pause_init();
777 	xc_init();		/* initialize processor crosscalls */
778 	idlestop_init();
779 
780 	if (!use_mp) {
781 		mutex_exit(&cpu_lock);
782 		cmn_err(CE_CONT, "?***** Not in MP mode\n");
783 		return;
784 	}
785 	/*
786 	 * should we be initializing this cpu?
787 	 */
788 	bootcpu = getprocessorid();
789 
790 	/*
791 	 * launch all the slave cpus now
792 	 */
793 	for (cpuid = 0; cpuid < NCPU; cpuid++) {
794 		dnode_t nodeid = cpunodes[cpuid].nodeid;
795 
796 		if (nodeid == (dnode_t)0)
797 			continue;
798 
799 		if (cpuid == bootcpu) {
800 			if (!CPU_IN_SET(cpu_bringup_set, cpuid)) {
801 				cmn_err(CE_WARN, "boot cpu not a member "
802 				    "of cpu_bringup_set, adding it");
803 				CPUSET_ADD(cpu_bringup_set, cpuid);
804 			}
805 			continue;
806 		}
807 		if (!CPU_IN_SET(cpu_bringup_set, cpuid))
808 			continue;
809 
810 		ASSERT(cpu[cpuid] == NULL);
811 
812 		setup_cpu_common(cpuid);
813 
814 		common_startup_init(cpu[cpuid], cpuid);
815 
816 		start_cpu(cpuid, cold_flag_set);
817 		/*
818 		 * Because slave_startup() gets fired off after init()
819 		 * starts, we can't use the '?' trick to do 'boot -v'
820 		 * printing - so we always direct the 'cpu .. online'
821 		 * messages to the log.
822 		 */
823 		cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
824 		    cpuid);
825 
826 		/*
827 		 * XXX: register_cpu_setup() callbacks should be called here
828 		 * with a new setup code, CPU_BOOT (or something).
829 		 */
830 		if (dtrace_cpu_init != NULL)
831 			(*dtrace_cpu_init)(cpuid);
832 	}
833 
834 	/*
835 	 * since all the cpus are online now, redistribute interrupts to them.
836 	 */
837 	intr_redist_all_cpus();
838 
839 	mutex_exit(&cpu_lock);
840 
841 	/*
842 	 * Start the Ecache scrubber.  Must be done after all calls to
843 	 * cpu_init_private for every cpu (including CPU 0).
844 	 */
845 	cpu_init_cache_scrub();
846 
847 	if (&cpu_mp_init)
848 		cpu_mp_init();
849 }
850