xref: /titanic_44/usr/src/uts/sun4u/ngdr/io/dr_cpu.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
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 /*
29  * CPU support routines for DR
30  */
31 
32 #include <sys/note.h>
33 #include <sys/debug.h>
34 #include <sys/types.h>
35 #include <sys/errno.h>
36 #include <sys/cred.h>
37 #include <sys/dditypes.h>
38 #include <sys/devops.h>
39 #include <sys/modctl.h>
40 #include <sys/poll.h>
41 #include <sys/conf.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 #include <sys/sunndi.h>
45 #include <sys/ndi_impldefs.h>
46 #include <sys/stat.h>
47 #include <sys/kmem.h>
48 #include <sys/processor.h>
49 #include <sys/cpuvar.h>
50 #include <sys/mem_config.h>
51 #include <sys/promif.h>
52 #include <sys/x_call.h>
53 #include <sys/cpu_sgnblk_defs.h>
54 #include <sys/membar.h>
55 #include <sys/stack.h>
56 #include <sys/sysmacros.h>
57 #include <sys/machsystm.h>
58 #include <sys/spitregs.h>
59 
60 #include <sys/archsystm.h>
61 #include <vm/hat_sfmmu.h>
62 #include <sys/pte.h>
63 #include <sys/mmu.h>
64 #include <sys/x_call.h>
65 #include <sys/cpu_module.h>
66 #include <sys/cpu_impl.h>
67 
68 #include <sys/autoconf.h>
69 #include <sys/cmn_err.h>
70 
71 #include <sys/dr.h>
72 #include <sys/dr_util.h>
73 
74 #ifdef _STARFIRE
75 #include <sys/starfire.h>
76 extern struct cpu	*SIGBCPU;
77 #else
78 /* for the DR*INTERNAL_ERROR macros.  see sys/dr.h. */
79 static char *dr_ie_fmt = "dr_cpu.c %d";
80 #endif /* _STARFIRE */
81 
82 int
83 dr_cpu_unit_is_sane(dr_board_t *bp, dr_cpu_unit_t *cp)
84 {
85 #ifdef DEBUG
86 	processorid_t	cpuid;
87 
88 	/*
89 	 * cpuid and unit number should never be different
90 	 * than they were at discovery/connect time
91 	 */
92 	ASSERT(drmach_cpu_get_id(cp->sbc_cm.sbdev_id, &cpuid) == 0);
93 
94 	ASSERT(cp->sbc_cm.sbdev_bp == bp);
95 	ASSERT(cp->sbc_cm.sbdev_type == SBD_COMP_CPU);
96 	ASSERT(cp->sbc_cpu_id == cpuid);
97 #else
98 	_NOTE(ARGUNUSED(bp))
99 	_NOTE(ARGUNUSED(cp))
100 #endif
101 
102 	return (1);
103 }
104 
105 static int
106 dr_errno2ecode(int error)
107 {
108 	int	rv;
109 
110 	switch (error) {
111 	case EBUSY:
112 		rv = ESBD_BUSY;
113 		break;
114 	case EINVAL:
115 		rv = ESBD_INVAL;
116 		break;
117 	case EALREADY:
118 		rv = ESBD_ALREADY;
119 		break;
120 	case ENODEV:
121 		rv = ESBD_NODEV;
122 		break;
123 	case ENOMEM:
124 		rv = ESBD_NOMEM;
125 		break;
126 	default:
127 		rv = ESBD_INVAL;
128 	}
129 
130 	return (rv);
131 }
132 
133 static void
134 dr_cpu_set_prop(dr_cpu_unit_t *cp)
135 {
136 	sbd_error_t	*err;
137 	dev_info_t	*dip;
138 	uint64_t	clock_freq;
139 	int		ecache_size = 0;
140 	char		*cache_str = NULL;
141 
142 	err = drmach_get_dip(cp->sbc_cm.sbdev_id, &dip);
143 	if (err) {
144 		DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
145 		return;
146 	}
147 
148 	if (dip == NULL) {
149 #ifndef _STARFIRE
150 		/*
151 		 * Do not report an error on Starfire since
152 		 * the dip will not be created until after
153 		 * the CPU has been configured.
154 		 */
155 		DR_DEV_INTERNAL_ERROR(&cp->sbc_cm);
156 #endif /* !_STARFIRE */
157 		return;
158 	}
159 
160 	/* read in the CPU speed */
161 
162 	/*
163 	 * If the property is not found in the CPU node, it has to be
164 	 * kept in the core or cmp node so we just keep looking.
165 	 */
166 	clock_freq = (unsigned int)ddi_prop_get_int(DDI_DEV_T_ANY,
167 		dip, 0, "clock-frequency", 0);
168 
169 	ASSERT(clock_freq != 0);
170 
171 	/*
172 	 * The ecache property string is not the same
173 	 * for all CPU implementations.
174 	 */
175 
176 	switch (cp->sbc_cpu_impl) {
177 	case BLACKBIRD_IMPL:
178 	case CHEETAH_IMPL:
179 	case CHEETAH_PLUS_IMPL:
180 		cache_str = "ecache-size";
181 		break;
182 	case JAGUAR_IMPL:
183 	case OLYMPUS_C_IMPL:
184 		cache_str = "l2-cache-size";
185 		break;
186 	case PANTHER_IMPL:
187 		cache_str = "l3-cache-size";
188 		break;
189 	default:
190 		cmn_err(CE_WARN, "Unknown cpu implementation=0x%x",
191 		    cp->sbc_cpu_impl);
192 		ASSERT(0);
193 		break;
194 	}
195 
196 	if (cache_str != NULL) {
197 		/* read in the ecache size */
198 		/*
199 		 * If the property is not found in the CPU node,
200 		 * it has to be kept in the core or cmp node so
201 		 * we just keep looking.
202 		 */
203 
204 		ecache_size = ddi_prop_get_int(DDI_DEV_T_ANY,
205 			dip, 0, cache_str, 0);
206 	}
207 
208 	ASSERT(ecache_size != 0);
209 
210 	/* convert to the proper units */
211 	cp->sbc_speed = (clock_freq + 500000) / 1000000;
212 	cp->sbc_ecache = ecache_size / (1024 * 1024);
213 }
214 
215 void
216 dr_init_cpu_unit(dr_cpu_unit_t *cp)
217 {
218 	sbd_error_t	*err;
219 	dr_state_t	new_state;
220 	int		cpuid;
221 	int		impl;
222 
223 	if (DR_DEV_IS_ATTACHED(&cp->sbc_cm)) {
224 		new_state = DR_STATE_CONFIGURED;
225 		cp->sbc_cm.sbdev_cond = SBD_COND_OK;
226 	} else if (DR_DEV_IS_PRESENT(&cp->sbc_cm)) {
227 		new_state = DR_STATE_CONNECTED;
228 		cp->sbc_cm.sbdev_cond = SBD_COND_OK;
229 	} else {
230 		new_state = DR_STATE_EMPTY;
231 		cp->sbc_cm.sbdev_cond = SBD_COND_UNKNOWN;
232 	}
233 
234 	if (DR_DEV_IS_PRESENT(&cp->sbc_cm)) {
235 		err = drmach_cpu_get_id(cp->sbc_cm.sbdev_id, &cpuid);
236 		if (err) {
237 			DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
238 			new_state = DR_STATE_FATAL;
239 			goto done;
240 		}
241 
242 		err = drmach_cpu_get_impl(cp->sbc_cm.sbdev_id, &impl);
243 		if (err) {
244 			DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
245 			new_state = DR_STATE_FATAL;
246 			goto done;
247 		}
248 	} else {
249 		cp->sbc_cpu_id = -1;
250 		cp->sbc_cpu_impl = -1;
251 		goto done;
252 	}
253 
254 	cp->sbc_cpu_id = cpuid;
255 	cp->sbc_cpu_impl = impl;
256 
257 	/* if true at init time, it must always be true */
258 	ASSERT(dr_cpu_unit_is_sane(cp->sbc_cm.sbdev_bp, cp));
259 
260 	mutex_enter(&cpu_lock);
261 	if ((cpuid >= 0) && cpu[cpuid])
262 		cp->sbc_cpu_flags = cpu[cpuid]->cpu_flags;
263 	else
264 		cp->sbc_cpu_flags = P_OFFLINE | P_POWEROFF;
265 	mutex_exit(&cpu_lock);
266 
267 	dr_cpu_set_prop(cp);
268 
269 done:
270 	/* delay transition until fully initialized */
271 	dr_device_transition(&cp->sbc_cm, new_state);
272 }
273 
274 int
275 dr_pre_attach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
276 {
277 	int		i;
278 	int		curr_cpu;
279 	int		next_cpu;
280 	static fn_t	f = "dr_pre_attach_cpu";
281 
282 	PR_CPU("%s...\n", f);
283 
284 	for (next_cpu = 0, i = 0; i < devnum; i++) {
285 		dr_cpu_unit_t *up = (dr_cpu_unit_t *)devlist[i];
286 
287 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
288 
289 		/*
290 		 * Print a console message for each attachment
291 		 * point. For CMP devices, this means that only
292 		 * one message should be printed, no matter how
293 		 * many cores are actually present.
294 		 */
295 		curr_cpu = DR_UNUM2SBD_UNUM(up->sbc_cm.sbdev_unum,
296 			SBD_COMP_CPU);
297 		if (curr_cpu >= next_cpu) {
298 			cmn_err(CE_CONT, "OS configure %s",
299 			    up->sbc_cm.sbdev_path);
300 			next_cpu = curr_cpu + 1;
301 		}
302 
303 		if (up->sbc_cm.sbdev_state == DR_STATE_UNCONFIGURED) {
304 			/*
305 			 * If we're coming from the UNCONFIGURED
306 			 * state then the cpu's sigblock will
307 			 * still be mapped in.  Need to unmap it
308 			 * before continuing with attachment.
309 			 */
310 			PR_CPU("%s: unmapping sigblk for cpu %d\n",
311 				f, up->sbc_cpu_id);
312 
313 			CPU_SGN_MAPOUT(up->sbc_cpu_id);
314 		}
315 	}
316 
317 	/*
318 	 * Block out status threads while creating
319 	 * devinfo tree branches
320 	 */
321 	dr_lock_status(hp->h_bd);
322 	mutex_enter(&cpu_lock);
323 
324 	return (0);
325 }
326 
327 /*ARGSUSED*/
328 void
329 dr_attach_cpu(dr_handle_t *hp, dr_common_unit_t *cp)
330 {
331 	sbd_error_t	*err;
332 	processorid_t	 cpuid;
333 	int		 rv;
334 
335 	ASSERT(MUTEX_HELD(&cpu_lock));
336 
337 	err = drmach_configure(cp->sbdev_id, 0);
338 	if (err) {
339 		DRERR_SET_C(&cp->sbdev_error, &err);
340 		return;
341 	}
342 
343 	err = drmach_cpu_get_id(cp->sbdev_id, &cpuid);
344 	if (err) {
345 		DRERR_SET_C(&cp->sbdev_error, &err);
346 
347 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
348 		if (err)
349 			sbd_err_clear(&err);
350 	} else if ((rv = cpu_configure(cpuid)) != 0) {
351 		dr_dev_err(CE_WARN, cp, dr_errno2ecode(rv));
352 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
353 		if (err)
354 			sbd_err_clear(&err);
355 	}
356 }
357 
358 /*
359  * dr_post_attach_cpu
360  *
361  * sbd error policy: Does not stop on error.  Processes all units in list.
362  */
363 int
364 dr_post_attach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
365 {
366 	int		i;
367 	int		errflag = 0;
368 	static fn_t	f = "dr_post_attach_cpu";
369 
370 	PR_CPU("%s...\n", f);
371 	hp->h_ndi = 0;
372 
373 	/* Startup and online newly-attached CPUs */
374 	for (i = 0; i < devnum; i++) {
375 		dr_cpu_unit_t *up = (dr_cpu_unit_t *)devlist[i];
376 		struct cpu	*cp;
377 
378 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
379 
380 		cp = cpu_get(up->sbc_cpu_id);
381 		if (cp == NULL) {
382 			cmn_err(CE_WARN, "%s: cpu_get failed for cpu %d",
383 			    f, up->sbc_cpu_id);
384 			continue;
385 		}
386 
387 		if (cpu_is_poweredoff(cp)) {
388 			if (cpu_poweron(cp) != 0) {
389 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_CPUSTART);
390 				errflag = 1;
391 			}
392 			PR_CPU("%s: cpu %d powered ON\n", f, up->sbc_cpu_id);
393 		}
394 
395 		if (cpu_is_offline(cp)) {
396 			PR_CPU("%s: onlining cpu %d...\n", f, up->sbc_cpu_id);
397 
398 			if (cpu_online(cp) != 0) {
399 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_ONLINE);
400 				errflag = 1;
401 			}
402 		}
403 
404 	}
405 
406 	mutex_exit(&cpu_lock);
407 	dr_unlock_status(hp->h_bd);
408 
409 	if (errflag)
410 		return (-1);
411 	else
412 		return (0);
413 }
414 
415 /*
416  * dr_pre_release_cpu
417  *
418  * sbd error policy: Stops on first error.
419  */
420 int
421 dr_pre_release_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
422 {
423 	int		c, cix, i, lastoffline = -1, rv = 0;
424 	processorid_t	cpuid;
425 	struct cpu	*cp;
426 	dr_cpu_unit_t	*up;
427 	dr_devset_t	devset;
428 	sbd_dev_stat_t	*ds;
429 	static fn_t	f = "dr_pre_release_cpu";
430 	int		cpu_flags = 0;
431 
432 	devset = DR_DEVS_PRESENT(hp->h_bd);
433 
434 	/* allocate status struct storage. */
435 	ds = (sbd_dev_stat_t *) kmem_zalloc(sizeof (sbd_dev_stat_t) *
436 		MAX_CPU_UNITS_PER_BOARD, KM_SLEEP);
437 
438 	cix = dr_cpu_status(hp, devset, ds);
439 
440 	mutex_enter(&cpu_lock);
441 
442 	for (i = 0; i < devnum; i++) {
443 		up = (dr_cpu_unit_t *)devlist[i];
444 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
445 
446 		/*
447 		 * The STARCAT platform borrows cpus for use by POST in
448 		 * iocage testing.  These cpus cannot be unconfigured
449 		 * while they are in use for the iocage.
450 		 * This check determines if a CPU is currently in use
451 		 * for iocage testing, and if so, returns a "Device busy"
452 		 * error.
453 		 */
454 		for (c = 0; c < cix; c++) {
455 			if (ds[c].d_cpu.cs_unit == up->sbc_cm.sbdev_unum) {
456 				if (ds[c].d_cpu.cs_busy) {
457 					dr_dev_err(CE_WARN,
458 						&up->sbc_cm, ESBD_BUSY);
459 					rv = -1;
460 					break;
461 				}
462 			}
463 		}
464 		if (c < cix)
465 			break;
466 		cpuid = up->sbc_cpu_id;
467 		if ((cp = cpu_get(cpuid)) == NULL) {
468 			dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_OFFLINE);
469 			rv = -1;
470 			break;
471 		}
472 
473 		/* used by dr_cancel_cpu during error flow */
474 		up->sbc_cpu_flags = cp->cpu_flags;
475 
476 		if (CPU_ACTIVE(cp)) {
477 			if (dr_cmd_flags(hp) & SBD_FLAG_FORCE)
478 				cpu_flags = CPU_FORCED;
479 
480 			PR_CPU("%s: offlining cpu %d\n", f, cpuid);
481 			if (cpu_offline(cp, cpu_flags)) {
482 				PR_CPU("%s: failed to offline cpu %d\n",
483 					f, cpuid);
484 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_OFFLINE);
485 				if (disp_bound_threads(cp, 0)) {
486 					cmn_err(CE_WARN, "%s: thread(s) "
487 						"bound to cpu %d",
488 							f, cp->cpu_id);
489 				}
490 				rv = -1;
491 				break;
492 			} else
493 				lastoffline = i;
494 		}
495 
496 		if (!rv) {
497 			sbd_error_t *err;
498 
499 			err = drmach_release(up->sbc_cm.sbdev_id);
500 			if (err) {
501 				DRERR_SET_C(&up->sbc_cm.sbdev_error, &err);
502 				rv = -1;
503 				break;
504 			}
505 		}
506 	}
507 
508 	mutex_exit(&cpu_lock);
509 
510 	if (rv) {
511 		/*
512 		 * Need to unwind others since at this level (pre-release)
513 		 * the device state has not yet transitioned and failures
514 		 * will prevent us from reaching the "post" release
515 		 * function where states are normally transitioned.
516 		 */
517 		for (i = lastoffline; i >= 0; i--) {
518 			up = (dr_cpu_unit_t *)devlist[i];
519 			(void) dr_cancel_cpu(up);
520 		}
521 	}
522 
523 	kmem_free(ds, sizeof (sbd_dev_stat_t) * MAX_CPU_UNITS_PER_BOARD);
524 	return (rv);
525 }
526 
527 /*
528  * dr_pre_detach_cpu
529  *
530  * sbd error policy: Stops on first error.
531  */
532 int
533 dr_pre_detach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
534 {
535 	_NOTE(ARGUNUSED(hp))
536 
537 	int		i;
538 	int		curr_cpu;
539 	int		next_cpu;
540 	int		cpu_flags = 0;
541 	static fn_t	f = "dr_pre_detach_cpu";
542 
543 	PR_CPU("%s...\n", f);
544 
545 	/*
546 	 * Block out status threads while destroying devinfo tree
547 	 * branches
548 	 */
549 	dr_lock_status(hp->h_bd);
550 	mutex_enter(&cpu_lock);
551 
552 	for (next_cpu = 0, i = 0; i < devnum; i++) {
553 		dr_cpu_unit_t *up = (dr_cpu_unit_t *)devlist[i];
554 		struct cpu	*cp;
555 
556 		ASSERT(dr_cpu_unit_is_sane(hp->h_bd, up));
557 
558 		cp = cpu_get(up->sbc_cpu_id);
559 		if (cp == NULL)
560 			continue;
561 
562 		/*
563 		 * Print a console message for each attachment
564 		 * point. For CMP devices, this means that only
565 		 * one message should be printed, no matter how
566 		 * many cores are actually present.
567 		 */
568 		curr_cpu = DR_UNUM2SBD_UNUM(up->sbc_cm.sbdev_unum,
569 			SBD_COMP_CPU);
570 		if (curr_cpu >= next_cpu) {
571 			cmn_err(CE_CONT, "OS unconfigure %s\n",
572 			    up->sbc_cm.sbdev_path);
573 			next_cpu = curr_cpu + 1;
574 		}
575 
576 		/*
577 		 * CPUs were offlined during Release.
578 		 */
579 		if (cpu_is_poweredoff(cp)) {
580 			PR_CPU("%s: cpu %d already powered OFF\n",
581 			    f, up->sbc_cpu_id);
582 			continue;
583 		}
584 
585 		if (!cpu_is_offline(cp)) {
586 			if (dr_cmd_flags(hp) & SBD_FLAG_FORCE)
587 				cpu_flags = CPU_FORCED;
588 			/* cpu was onlined after release.  Offline it again */
589 			PR_CPU("%s: offlining cpu %d\n", f, up->sbc_cpu_id);
590 			if (cpu_offline(cp, cpu_flags)) {
591 				PR_CPU("%s: failed to offline cpu %d\n",
592 				    f, up->sbc_cpu_id);
593 				dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_OFFLINE);
594 				if (disp_bound_threads(cp, 0)) {
595 					cmn_err(CE_WARN, "%s: thread(s) "
596 						"bound to cpu %d",
597 							f, cp->cpu_id);
598 				}
599 				goto err;
600 			}
601 		}
602 		if (cpu_poweroff(cp) != 0) {
603 			dr_dev_err(CE_WARN, &up->sbc_cm, ESBD_CPUSTOP);
604 			goto err;
605 		} else {
606 			PR_CPU("%s: cpu %d powered OFF\n", f, up->sbc_cpu_id);
607 		}
608 	}
609 
610 	return (0);
611 
612 err:
613 	mutex_exit(&cpu_lock);
614 	dr_unlock_status(hp->h_bd);
615 	return (-1);
616 }
617 
618 /*ARGSUSED*/
619 void
620 dr_detach_cpu(dr_handle_t *hp, dr_common_unit_t *cp)
621 {
622 	sbd_error_t	*err;
623 	processorid_t	 cpuid;
624 	int		 rv;
625 
626 	ASSERT(MUTEX_HELD(&cpu_lock));
627 
628 	err = drmach_cpu_get_id(cp->sbdev_id, &cpuid);
629 	if (err) {
630 		DRERR_SET_C(&cp->sbdev_error, &err);
631 	} else if ((rv = cpu_unconfigure(cpuid)) != 0) {
632 		dr_dev_err(CE_IGNORE, cp, dr_errno2ecode(rv));
633 	} else {
634 		err = drmach_unconfigure(cp->sbdev_id, DEVI_BRANCH_DESTROY);
635 		if (err) {
636 			DRERR_SET_C(&cp->sbdev_error, &err);
637 		}
638 	}
639 }
640 
641 /*ARGSUSED1*/
642 int
643 dr_post_detach_cpu(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
644 {
645 	static fn_t	f = "dr_post_detach_cpu";
646 
647 	PR_CPU("%s...\n", f);
648 	hp->h_ndi = 0;
649 
650 	mutex_exit(&cpu_lock);
651 	dr_unlock_status(hp->h_bd);
652 
653 	return (0);
654 }
655 
656 static void
657 dr_fill_cpu_stat(dr_cpu_unit_t *cp, drmach_status_t *pstat, sbd_cpu_stat_t *csp)
658 {
659 	ASSERT(cp && pstat && csp);
660 
661 	/* Fill in the common status information */
662 	bzero((caddr_t)csp, sizeof (*csp));
663 	csp->cs_type = cp->sbc_cm.sbdev_type;
664 	csp->cs_unit = cp->sbc_cm.sbdev_unum;
665 	strncpy(csp->cs_name, pstat->type, sizeof (csp->cs_name));
666 	csp->cs_cond = cp->sbc_cm.sbdev_cond;
667 	csp->cs_busy = cp->sbc_cm.sbdev_busy | pstat->busy;
668 	csp->cs_time = cp->sbc_cm.sbdev_time;
669 	csp->cs_ostate = cp->sbc_cm.sbdev_ostate;
670 	csp->cs_suspend = 0;
671 
672 	/* CPU specific status data */
673 	csp->cs_cpuid = cp->sbc_cpu_id;
674 
675 #ifdef _STARFIRE
676 	csp->cs_isbootproc = (SIGBCPU->cpu_id == cp->sbc_cpu_id) ? 1 : 0;
677 #endif /* _STARFIRE */
678 
679 	/*
680 	 * If the speed and ecache properties have not been
681 	 * cached yet, read them in from the device tree.
682 	 */
683 	if ((cp->sbc_speed == 0) || (cp->sbc_ecache == 0))
684 		dr_cpu_set_prop(cp);
685 
686 	/* use the cached speed and ecache values */
687 	csp->cs_speed = cp->sbc_speed;
688 	csp->cs_ecache = cp->sbc_ecache;
689 
690 	mutex_enter(&cpu_lock);
691 	if (!cpu_get(csp->cs_cpuid)) {
692 		/* ostate must be UNCONFIGURED */
693 		csp->cs_cm.c_ostate = SBD_STAT_UNCONFIGURED;
694 	}
695 	mutex_exit(&cpu_lock);
696 }
697 
698 static void
699 dr_fill_cmp_stat(sbd_cpu_stat_t *csp, int ncores, int impl, sbd_cmp_stat_t *psp)
700 {
701 	int	core;
702 
703 	ASSERT(csp && psp && (ncores >= 1));
704 
705 	bzero((caddr_t)psp, sizeof (*psp));
706 
707 	/*
708 	 * Fill in the common status information based
709 	 * on the data for the first core.
710 	 */
711 	psp->ps_type = SBD_COMP_CMP;
712 	psp->ps_unit = DR_UNUM2SBD_UNUM(csp->cs_unit, SBD_COMP_CMP);
713 	strncpy(psp->ps_name, csp->cs_name, sizeof (psp->ps_name));
714 	psp->ps_cond = csp->cs_cond;
715 	psp->ps_busy = csp->cs_busy;
716 	psp->ps_time = csp->cs_time;
717 	psp->ps_ostate = csp->cs_ostate;
718 	psp->ps_suspend = csp->cs_suspend;
719 
720 	/* CMP specific status data */
721 	*psp->ps_cpuid = csp->cs_cpuid;
722 	psp->ps_ncores = 1;
723 	psp->ps_speed = csp->cs_speed;
724 	psp->ps_ecache = csp->cs_ecache;
725 
726 	/*
727 	 * Walk through the data for the remaining cores.
728 	 * Make any adjustments to the common status data,
729 	 * or the shared CMP specific data if necessary.
730 	 */
731 	for (core = 1; core < ncores; core++) {
732 
733 		/*
734 		 * The following properties should be the same
735 		 * for all the cores of the CMP.
736 		 */
737 		ASSERT(psp->ps_unit == DR_UNUM2SBD_UNUM(
738 			csp[core].cs_unit, SBD_COMP_CMP));
739 		ASSERT(psp->ps_speed == csp[core].cs_speed);
740 
741 		psp->ps_cpuid[core] = csp[core].cs_cpuid;
742 		psp->ps_ncores++;
743 
744 		/*
745 		 * Jaguar has a split ecache, so the ecache
746 		 * for each core must be added together to
747 		 * get the total ecache for the whole chip.
748 		 */
749 		if (IS_JAGUAR(impl)) {
750 			psp->ps_ecache += csp[core].cs_ecache;
751 		}
752 
753 		/* adjust time if necessary */
754 		if (csp[core].cs_time > psp->ps_time) {
755 			psp->ps_time = csp[core].cs_time;
756 		}
757 
758 		psp->ps_busy |= csp[core].cs_busy;
759 
760 		/*
761 		 * If any of the cores are configured, the
762 		 * entire CMP is marked as configured.
763 		 */
764 		if (csp[core].cs_ostate == SBD_STAT_CONFIGURED) {
765 			psp->ps_ostate = csp[core].cs_ostate;
766 		}
767 	}
768 }
769 
770 int
771 dr_cpu_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp)
772 {
773 	int		cmp;
774 	int		core;
775 	int		ncpu;
776 	dr_board_t	*bp;
777 	sbd_cpu_stat_t	cstat[MAX_CORES_PER_CMP];
778 	int		impl;
779 
780 	bp = hp->h_bd;
781 	ncpu = 0;
782 
783 	devset &= DR_DEVS_PRESENT(bp);
784 
785 	/*
786 	 * Treat every CPU as a CMP. In the case where the
787 	 * device is not a CMP, treat it as a CMP with only
788 	 * one core.
789 	 */
790 	for (cmp = 0; cmp < MAX_CMP_UNITS_PER_BOARD; cmp++) {
791 
792 		int		ncores;
793 		dr_cpu_unit_t	*cp;
794 		drmach_status_t	pstat;
795 		sbd_error_t	*err;
796 		sbd_cmp_stat_t	*psp;
797 
798 		if ((devset & DEVSET(SBD_COMP_CMP, cmp)) == 0) {
799 			continue;
800 		}
801 
802 		ncores = 0;
803 
804 		for (core = 0; core < MAX_CORES_PER_CMP; core++) {
805 
806 			cp = dr_get_cpu_unit(bp, DR_CMP_CORE_UNUM(cmp, core));
807 
808 			if (cp->sbc_cm.sbdev_state == DR_STATE_EMPTY) {
809 				/* present, but not fully initialized */
810 				continue;
811 			}
812 
813 			ASSERT(dr_cpu_unit_is_sane(hp->h_bd, cp));
814 
815 			/* skip if not present */
816 			if (cp->sbc_cm.sbdev_id == (drmachid_t)0) {
817 				continue;
818 			}
819 
820 			/* fetch platform status */
821 			err = drmach_status(cp->sbc_cm.sbdev_id, &pstat);
822 			if (err) {
823 				DRERR_SET_C(&cp->sbc_cm.sbdev_error, &err);
824 				continue;
825 			}
826 
827 			dr_fill_cpu_stat(cp, &pstat, &cstat[ncores++]);
828 			/*
829 			 * We should set impl here because the last core
830 			 * found might be EMPTY or not present.
831 			 */
832 			impl = cp->sbc_cpu_impl;
833 		}
834 
835 		if (ncores == 0) {
836 			continue;
837 		}
838 
839 		/*
840 		 * Store the data to the outgoing array. If the
841 		 * device is a CMP, combine all the data for the
842 		 * cores into a single stat structure.
843 		 *
844 		 * The check for a CMP device uses the last core
845 		 * found, assuming that all cores will have the
846 		 * same implementation.
847 		 */
848 
849 		if (CPU_IMPL_IS_CMP(impl)) {
850 			psp = (sbd_cmp_stat_t *)dsp;
851 			dr_fill_cmp_stat(cstat, ncores, impl, psp);
852 		} else {
853 			ASSERT(ncores == 1);
854 			bcopy(cstat, dsp, sizeof (sbd_cpu_stat_t));
855 		}
856 
857 		dsp++;
858 		ncpu++;
859 	}
860 
861 	return (ncpu);
862 }
863 
864 /*
865  * Cancel previous release operation for cpu.
866  * For cpus this means simply bringing cpus that
867  * were offline back online.  Note that they had
868  * to have been online at the time there were
869  * released.
870  */
871 int
872 dr_cancel_cpu(dr_cpu_unit_t *up)
873 {
874 	int		rv = 0;
875 	static fn_t	f = "dr_cancel_cpu";
876 
877 	ASSERT(dr_cpu_unit_is_sane(up->sbc_cm.sbdev_bp, up));
878 
879 	if (cpu_flagged_active(up->sbc_cpu_flags)) {
880 		struct cpu	*cp;
881 
882 		/*
883 		 * CPU had been online, go ahead
884 		 * bring it back online.
885 		 */
886 		PR_CPU("%s: bringing cpu %d back ONLINE\n",
887 			f, up->sbc_cpu_id);
888 
889 		mutex_enter(&cpu_lock);
890 		cp = cpu[up->sbc_cpu_id];
891 
892 		if (cpu_is_poweredoff(cp)) {
893 			if (cpu_poweron(cp)) {
894 				cmn_err(CE_WARN, "%s: failed to power-on "
895 				    "cpu %d", f, up->sbc_cpu_id);
896 				rv = -1;
897 			}
898 		}
899 
900 		if (cpu_is_offline(cp)) {
901 			if (cpu_online(cp)) {
902 				cmn_err(CE_WARN, "%s: failed to online cpu %d",
903 				    f, up->sbc_cpu_id);
904 				rv = -1;
905 			}
906 		}
907 
908 		if (cpu_is_online(cp)) {
909 			if (cpu_flagged_nointr(up->sbc_cpu_flags)) {
910 				if (cpu_intr_disable(cp) != 0) {
911 					cmn_err(CE_WARN, "%s: failed to "
912 					"disable interrupts on cpu %d",
913 						f, up->sbc_cpu_id);
914 				}
915 			}
916 		}
917 
918 		mutex_exit(&cpu_lock);
919 	}
920 
921 	return (rv);
922 }
923 
924 int
925 dr_disconnect_cpu(dr_cpu_unit_t *up)
926 {
927 	sbd_error_t	*err;
928 	static fn_t	f = "dr_disconnect_cpu";
929 
930 	PR_CPU("%s...\n", f);
931 
932 	ASSERT((up->sbc_cm.sbdev_state == DR_STATE_CONNECTED) ||
933 		(up->sbc_cm.sbdev_state == DR_STATE_UNCONFIGURED));
934 
935 	ASSERT(dr_cpu_unit_is_sane(up->sbc_cm.sbdev_bp, up));
936 
937 	if (up->sbc_cm.sbdev_state == DR_STATE_CONNECTED) {
938 		/*
939 		 * Cpus were never brought in and so are still
940 		 * effectively disconnected, so nothing to do here.
941 		 */
942 		PR_CPU("%s: cpu %d never brought in\n",
943 			f, up->sbc_cpu_id);
944 		return (0);
945 	}
946 
947 	err = drmach_cpu_disconnect(up->sbc_cm.sbdev_id);
948 	if (err == NULL)
949 		return (0);
950 	else {
951 		DRERR_SET_C(&up->sbc_cm.sbdev_error, &err);
952 		return (-1);
953 	}
954 	/*NOTREACHED*/
955 }
956