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