xref: /titanic_51/usr/src/uts/sun4u/starfire/io/drmach.c (revision a776e90690d98f629ab57f6493ff5949af9d65bd)
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 2007 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/debug.h>
29 #include <sys/types.h>
30 #include <sys/varargs.h>
31 #include <sys/errno.h>
32 #include <sys/cred.h>
33 #include <sys/dditypes.h>
34 #include <sys/devops.h>
35 #include <sys/modctl.h>
36 #include <sys/poll.h>
37 #include <sys/conf.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/sunndi.h>
41 #include <sys/ndi_impldefs.h>
42 #include <sys/stat.h>
43 #include <sys/kmem.h>
44 #include <sys/vmem.h>
45 #include <sys/processor.h>
46 #include <sys/spitregs.h>
47 #include <sys/cpuvar.h>
48 #include <sys/cpupart.h>
49 #include <sys/mem_config.h>
50 #include <sys/ddi_impldefs.h>
51 #include <sys/systm.h>
52 #include <sys/machsystm.h>
53 #include <sys/autoconf.h>
54 #include <sys/cmn_err.h>
55 #include <sys/sysmacros.h>
56 #include <sys/x_call.h>
57 #include <sys/promif.h>
58 #include <sys/prom_plat.h>
59 #include <sys/membar.h>
60 #include <vm/seg_kmem.h>
61 #include <sys/mem_cage.h>
62 #include <sys/stack.h>
63 #include <sys/archsystm.h>
64 #include <vm/hat_sfmmu.h>
65 #include <sys/pte.h>
66 #include <sys/mmu.h>
67 #include <sys/cpu_module.h>
68 #include <sys/obpdefs.h>
69 #include <sys/note.h>
70 
71 #include <sys/starfire.h>	/* plat_max_... decls */
72 #include <sys/cvc.h>
73 #include <sys/cpu_sgnblk_defs.h>
74 #include <sys/drmach.h>
75 #include <sys/dr_util.h>
76 #include <sys/pda.h>
77 
78 #include <sys/sysevent.h>
79 #include <sys/sysevent/dr.h>
80 #include <sys/sysevent/eventdefs.h>
81 
82 
83 extern void		bcopy32_il(uint64_t, uint64_t);
84 extern void		flush_ecache_il(
85 				uint64_t physaddr, int size, int linesz);
86 extern uint_t		ldphysio_il(uint64_t physaddr);
87 extern void		stphysio_il(uint64_t physaddr, uint_t value);
88 
89 extern uint64_t		mc_get_mem_alignment(void);
90 extern uint64_t		mc_get_asr_addr(pnode_t);
91 extern uint64_t		mc_get_idle_addr(pnode_t);
92 extern uint64_t		mc_get_alignment_mask(pnode_t);
93 extern int		mc_read_asr(pnode_t, uint_t *);
94 extern int		mc_write_asr(pnode_t, uint_t);
95 extern uint64_t		mc_asr_to_pa(uint_t);
96 extern uint_t		mc_pa_to_asr(uint_t, uint64_t);
97 
98 extern int		pc_madr_add(int, int, int, int);
99 
100 typedef struct {
101 	struct drmach_node	*node;
102 	void			*data;
103 } drmach_node_walk_args_t;
104 
105 typedef struct drmach_node {
106 	void		*here;
107 
108 	pnode_t		 (*get_dnode)(struct drmach_node *node);
109 	int		 (*walk)(struct drmach_node *node, void *data,
110 				int (*cb)(drmach_node_walk_args_t *args));
111 } drmach_node_t;
112 
113 typedef struct {
114 	int		 min_index;
115 	int		 max_index;
116 	int		 arr_sz;
117 	drmachid_t	*arr;
118 } drmach_array_t;
119 
120 typedef struct {
121 	void		*isa;
122 
123 	sbd_error_t	*(*release)(drmachid_t);
124 	sbd_error_t	*(*status)(drmachid_t, drmach_status_t *);
125 
126 	char		 name[MAXNAMELEN];
127 } drmach_common_t;
128 
129 typedef struct {
130 	drmach_common_t	 cm;
131 	int		 bnum;
132 	int		 assigned;
133 	int		 powered;
134 	int		 connect_cpuid;
135 	int		 cond;
136 	drmach_node_t	*tree;
137 	drmach_array_t	*devices;
138 } drmach_board_t;
139 
140 typedef struct {
141 	drmach_common_t	 cm;
142 	drmach_board_t	*bp;
143 	int		 unum;
144 	int		 busy;
145 	int		 powered;
146 	const char	*type;
147 	drmach_node_t	*node;
148 } drmach_device_t;
149 
150 typedef struct {
151 	int		 flags;
152 	drmach_device_t	*dp;
153 	sbd_error_t	*err;
154 	dev_info_t	*dip;
155 } drmach_config_args_t;
156 
157 typedef struct {
158 	uint64_t	 idle_addr;
159 	drmach_device_t	*mem;
160 } drmach_mc_idle_script_t;
161 
162 typedef struct {
163 	uint64_t	masr_addr;
164 	uint_t		masr;
165 	uint_t		_filler;
166 } drmach_rename_script_t;
167 
168 typedef struct {
169 	void		(*run)(void *arg);
170 	caddr_t		data;
171 	pda_handle_t	*ph;
172 	struct memlist	*c_ml;
173 	uint64_t	s_copybasepa;
174 	uint64_t	t_copybasepa;
175 	drmach_device_t	*restless_mc;	/* diagnostic output */
176 } drmach_copy_rename_program_t;
177 
178 typedef enum {
179 	DO_IDLE,
180 	DO_UNIDLE,
181 	DO_PAUSE,
182 	DO_UNPAUSE
183 } drmach_iopc_op_t;
184 
185 typedef struct {
186 	drmach_board_t	*obj;
187 	int		 ndevs;
188 	void		*a;
189 	sbd_error_t	*(*found)(void *a, const char *, int, drmachid_t);
190 	sbd_error_t	*err;
191 } drmach_board_cb_data_t;
192 
193 static caddr_t		 drmach_shutdown_va;
194 
195 static int		 drmach_initialized;
196 static drmach_array_t	*drmach_boards;
197 
198 static int		 drmach_cpu_delay = 100;
199 static int		 drmach_cpu_ntries = 50000;
200 
201 volatile uchar_t	*drmach_xt_mb;
202 
203 /*
204  * Do not change the drmach_shutdown_mbox structure without
205  * considering the drmach_shutdown_asm assembly language code.
206  */
207 struct drmach_shutdown_mbox {
208 	uint64_t	estack;
209 	uint64_t	flushaddr;
210 	int		size;
211 	int		linesize;
212 	uint64_t	physaddr;
213 };
214 struct drmach_shutdown_mbox	*drmach_shutdown_asm_mbox;
215 static sbd_error_t	*drmach_device_new(drmach_node_t *,
216 				drmach_board_t *, drmach_device_t **);
217 static sbd_error_t	*drmach_cpu_new(drmach_device_t *);
218 static sbd_error_t	*drmach_mem_new(drmach_device_t *);
219 static sbd_error_t	*drmach_io_new(drmach_device_t *);
220 
221 extern struct cpu	*SIGBCPU;
222 
223 #ifdef DEBUG
224 
225 #define	DRMACH_PR		if (drmach_debug) printf
226 int drmach_debug = 0;		 /* set to non-zero to enable debug messages */
227 #else
228 
229 #define	DRMACH_PR		_NOTE(CONSTANTCONDITION) if (0) printf
230 #endif /* DEBUG */
231 
232 #define	DRMACH_OBJ(id)		((drmach_common_t *)id)
233 
234 #define	DRMACH_IS_BOARD_ID(id)	\
235 	((id != 0) &&		\
236 	(DRMACH_OBJ(id)->isa == (void *)drmach_board_new))
237 
238 #define	DRMACH_IS_CPU_ID(id)	\
239 	((id != 0) &&		\
240 	(DRMACH_OBJ(id)->isa == (void *)drmach_cpu_new))
241 
242 #define	DRMACH_IS_MEM_ID(id)	\
243 	((id != 0) &&		\
244 	(DRMACH_OBJ(id)->isa == (void *)drmach_mem_new))
245 
246 #define	DRMACH_IS_IO_ID(id)	\
247 	((id != 0) &&		\
248 	(DRMACH_OBJ(id)->isa == (void *)drmach_io_new))
249 
250 #define	DRMACH_IS_DEVICE_ID(id)					\
251 	((id != 0) &&						\
252 	(DRMACH_OBJ(id)->isa == (void *)drmach_cpu_new ||	\
253 	    DRMACH_OBJ(id)->isa == (void *)drmach_mem_new ||	\
254 	    DRMACH_OBJ(id)->isa == (void *)drmach_io_new))
255 
256 #define	DRMACH_IS_ID(id)					\
257 	((id != 0) &&						\
258 	(DRMACH_OBJ(id)->isa == (void *)drmach_board_new ||	\
259 	    DRMACH_OBJ(id)->isa == (void *)drmach_cpu_new ||	\
260 	    DRMACH_OBJ(id)->isa == (void *)drmach_mem_new ||	\
261 	    DRMACH_OBJ(id)->isa == (void *)drmach_io_new))
262 
263 #define	DRMACH_CPUID2BNUM(cpuid) \
264 	((cpuid) / MAX_CPU_UNITS_PER_BOARD)
265 
266 #define	DRMACH_INTERNAL_ERROR() \
267 	drerr_new(1, ESTF_INTERNAL, drmach_ie_fmt, __LINE__)
268 static char		*drmach_ie_fmt = "drmach.c %d";
269 
270 static struct {
271 	const char	 *name;
272 	const char	 *type;
273 	sbd_error_t	 *(*new)(drmach_device_t *);
274 } name2type[] = {
275 	{ "SUNW,UltraSPARC",	DRMACH_DEVTYPE_CPU,  drmach_cpu_new },
276 	{ "mem-unit",		DRMACH_DEVTYPE_MEM,  drmach_mem_new },
277 	{ "pci",		DRMACH_DEVTYPE_PCI,  drmach_io_new  },
278 	{ "sbus",		DRMACH_DEVTYPE_SBUS, drmach_io_new  },
279 };
280 
281 /* node types to cleanup when a board is unconfigured */
282 #define	MISC_COUNTER_TIMER_DEVNAME	"counter-timer"
283 #define	MISC_PERF_COUNTER_DEVNAME	"perf-counter"
284 
285 /* utility */
286 #define	MBYTE	(1048576ull)
287 
288 /*
289  * This is necessary because the CPU support needs
290  * to call cvc_assign_iocpu.
291  */
292 #ifndef lint
293 char _depends_on[] = "drv/cvc";
294 #endif  /* lint */
295 
296 /*
297  * drmach autoconfiguration data structures and interfaces
298  */
299 
300 extern struct mod_ops mod_miscops;
301 
302 static struct modlmisc modlmisc = {
303 	&mod_miscops,
304 	"Sun Enterprise 10000 DR %I%"
305 };
306 
307 static struct modlinkage modlinkage = {
308 	MODREV_1,
309 	(void *)&modlmisc,
310 	NULL
311 };
312 
313 static kmutex_t drmach_i_lock;
314 
315 int
316 _init(void)
317 {
318 	int err;
319 
320 	/* check that we have the correct version of obp */
321 	if (prom_test("SUNW,UE10000,add-brd") != 0) {
322 
323 		cmn_err(CE_WARN, "!OBP/SSP upgrade is required to enable "
324 		    "DR Functionality");
325 
326 		return (-1);
327 	}
328 
329 	mutex_init(&drmach_i_lock, NULL, MUTEX_DRIVER, NULL);
330 
331 	drmach_xt_mb = (uchar_t *)vmem_alloc(static_alloc_arena,
332 	    NCPU * sizeof (uchar_t), VM_SLEEP);
333 	drmach_shutdown_asm_mbox = (struct drmach_shutdown_mbox *)
334 	    vmem_alloc(static_alloc_arena, sizeof (struct drmach_shutdown_mbox),
335 	    VM_SLEEP);
336 
337 	if ((err = mod_install(&modlinkage)) != 0) {
338 		mutex_destroy(&drmach_i_lock);
339 		vmem_free(static_alloc_arena, (void *)drmach_xt_mb,
340 		    NCPU * sizeof (uchar_t));
341 		vmem_free(static_alloc_arena, (void *)drmach_shutdown_asm_mbox,
342 		    sizeof (struct drmach_shutdown_mbox));
343 	}
344 
345 	return (err);
346 }
347 
348 int
349 _fini(void)
350 {
351 	static int drmach_fini(void);
352 
353 	if (drmach_fini())
354 		return (DDI_FAILURE);
355 	else
356 		return (mod_remove(&modlinkage));
357 }
358 
359 int
360 _info(struct modinfo *modinfop)
361 {
362 	return (mod_info(&modlinkage, modinfop));
363 }
364 
365 static pnode_t
366 drmach_node_obp_get_dnode(drmach_node_t *np)
367 {
368 	return ((pnode_t)(uintptr_t)np->here);
369 }
370 
371 static int
372 drmach_node_obp_walk(drmach_node_t *np, void *data,
373 		int (*cb)(drmach_node_walk_args_t *args))
374 {
375 	pnode_t			nodeid;
376 	int			rv;
377 	drmach_node_walk_args_t	args;
378 
379 	/* initialized args structure for callback */
380 	args.node = np;
381 	args.data = data;
382 
383 	nodeid = prom_childnode(prom_rootnode());
384 
385 	/* save our new position with in the tree */
386 	np->here = (void *)(uintptr_t)nodeid;
387 
388 	rv = 0;
389 	while (nodeid != OBP_NONODE) {
390 		rv = (*cb)(&args);
391 		if (rv)
392 			break;
393 
394 		nodeid = prom_nextnode(nodeid);
395 
396 		/* save our new position with in the tree */
397 		np->here = (void *)(uintptr_t)nodeid;
398 	}
399 
400 	return (rv);
401 }
402 
403 static drmach_node_t *
404 drmach_node_new(void)
405 {
406 	drmach_node_t *np;
407 
408 	np = kmem_zalloc(sizeof (drmach_node_t), KM_SLEEP);
409 
410 	np->get_dnode = drmach_node_obp_get_dnode;
411 	np->walk = drmach_node_obp_walk;
412 
413 	return (np);
414 }
415 
416 static void
417 drmach_node_dispose(drmach_node_t *np)
418 {
419 	kmem_free(np, sizeof (*np));
420 }
421 
422 static dev_info_t *
423 drmach_node_get_dip(drmach_node_t *np)
424 {
425 	pnode_t nodeid;
426 
427 	nodeid = np->get_dnode(np);
428 	if (nodeid == OBP_NONODE)
429 		return (NULL);
430 	else {
431 		dev_info_t *dip;
432 
433 		/* The root node doesn't have to be held */
434 		dip = e_ddi_nodeid_to_dip(nodeid);
435 		if (dip) {
436 			/*
437 			 * Branch rooted at dip is already held, so release
438 			 * hold acquired in e_ddi_nodeid_to_dip()
439 			 */
440 			ddi_release_devi(dip);
441 			ASSERT(e_ddi_branch_held(dip));
442 		}
443 
444 		return (dip);
445 	}
446 	/*NOTREACHED*/
447 }
448 
449 static pnode_t
450 drmach_node_get_dnode(drmach_node_t *np)
451 {
452 	return (np->get_dnode(np));
453 }
454 
455 static int
456 drmach_node_walk(drmach_node_t *np, void *param,
457 		int (*cb)(drmach_node_walk_args_t *args))
458 {
459 	return (np->walk(np, param, cb));
460 }
461 
462 static int
463 drmach_node_get_prop(drmach_node_t *np, char *name, void *buf)
464 {
465 	pnode_t	nodeid;
466 	int	rv;
467 
468 	nodeid = np->get_dnode(np);
469 	if (nodeid == OBP_NONODE)
470 		rv = -1;
471 	else if (prom_getproplen(nodeid, (caddr_t)name) < 0)
472 		rv = -1;
473 	else {
474 		(void) prom_getprop(nodeid, (caddr_t)name, (caddr_t)buf);
475 		rv = 0;
476 	}
477 
478 	return (rv);
479 }
480 
481 static int
482 drmach_node_get_proplen(drmach_node_t *np, char *name, int *len)
483 {
484 	pnode_t	 nodeid;
485 	int	 rv;
486 
487 	nodeid = np->get_dnode(np);
488 	if (nodeid == OBP_NONODE)
489 		rv = -1;
490 	else {
491 		*len = prom_getproplen(nodeid, (caddr_t)name);
492 		rv = (*len < 0 ? -1 : 0);
493 	}
494 
495 	return (rv);
496 }
497 
498 static drmachid_t
499 drmach_node_dup(drmach_node_t *np)
500 {
501 	drmach_node_t *dup;
502 
503 	dup = drmach_node_new();
504 	dup->here = np->here;
505 
506 	return (dup);
507 }
508 
509 /*
510  * drmach_array provides convenient array construction, access,
511  * bounds checking and array destruction logic.
512  */
513 
514 static drmach_array_t *
515 drmach_array_new(int min_index, int max_index)
516 {
517 	drmach_array_t *arr;
518 
519 	arr = kmem_zalloc(sizeof (drmach_array_t), KM_SLEEP);
520 
521 	arr->arr_sz = (max_index - min_index + 1) * sizeof (void *);
522 	if (arr->arr_sz > 0) {
523 		arr->min_index = min_index;
524 		arr->max_index = max_index;
525 
526 		arr->arr = kmem_zalloc(arr->arr_sz, KM_SLEEP);
527 		return (arr);
528 	} else {
529 		kmem_free(arr, sizeof (*arr));
530 		return (0);
531 	}
532 }
533 
534 static int
535 drmach_array_set(drmach_array_t *arr, int idx, drmachid_t val)
536 {
537 	if (idx < arr->min_index || idx > arr->max_index)
538 		return (-1);
539 	else {
540 		arr->arr[idx - arr->min_index] = val;
541 		return (0);
542 	}
543 	/*NOTREACHED*/
544 }
545 
546 static int
547 drmach_array_get(drmach_array_t *arr, int idx, drmachid_t *val)
548 {
549 	if (idx < arr->min_index || idx > arr->max_index)
550 		return (-1);
551 	else {
552 		*val = arr->arr[idx - arr->min_index];
553 		return (0);
554 	}
555 	/*NOTREACHED*/
556 }
557 
558 static int
559 drmach_array_first(drmach_array_t *arr, int *idx, drmachid_t *val)
560 {
561 	int rv;
562 
563 	*idx = arr->min_index;
564 	while ((rv = drmach_array_get(arr, *idx, val)) == 0 && *val == NULL)
565 		*idx += 1;
566 
567 	return (rv);
568 }
569 
570 static int
571 drmach_array_next(drmach_array_t *arr, int *idx, drmachid_t *val)
572 {
573 	int rv;
574 
575 	*idx += 1;
576 	while ((rv = drmach_array_get(arr, *idx, val)) == 0 && *val == NULL)
577 		*idx += 1;
578 
579 	return (rv);
580 }
581 
582 static void
583 drmach_array_dispose(drmach_array_t *arr, void (*disposer)(drmachid_t))
584 {
585 	drmachid_t	val;
586 	int		idx;
587 	int		rv;
588 
589 	rv = drmach_array_first(arr, &idx, &val);
590 	while (rv == 0) {
591 		(*disposer)(val);
592 		rv = drmach_array_next(arr, &idx, &val);
593 	}
594 
595 	kmem_free(arr->arr, arr->arr_sz);
596 	kmem_free(arr, sizeof (*arr));
597 }
598 
599 /*ARGSUSED*/
600 static int
601 drmach_prom_select(pnode_t nodeid, void *arg, uint_t flags)
602 {
603 	int			rprop[64];
604 	pnode_t			saved;
605 	drmach_config_args_t	*ap = (drmach_config_args_t *)arg;
606 	drmach_device_t		*dp = ap->dp;
607 	sbd_error_t		*err;
608 
609 	saved = drmach_node_get_dnode(dp->node);
610 
611 	if (nodeid != saved)
612 		return (DDI_FAILURE);
613 
614 	if (saved == OBP_NONODE) {
615 		err = DRMACH_INTERNAL_ERROR();
616 		DRERR_SET_C(&ap->err, &err);
617 		return (DDI_FAILURE);
618 	}
619 
620 	if (prom_getprop(nodeid, OBP_REG, (caddr_t)rprop) <= 0) {
621 		return (DDI_FAILURE);
622 	}
623 
624 	return (DDI_SUCCESS);
625 }
626 
627 /*ARGSUSED*/
628 static void
629 drmach_branch_callback(dev_info_t *rdip, void *arg, uint_t flags)
630 {
631 	drmach_config_args_t	*ap = (drmach_config_args_t *)arg;
632 
633 	ASSERT(ap->dip == NULL);
634 
635 	ap->dip = rdip;
636 }
637 
638 sbd_error_t *
639 drmach_configure(drmachid_t id, int flags)
640 {
641 	drmach_device_t		*dp;
642 	sbd_error_t		*err;
643 	drmach_config_args_t	ca;
644 	devi_branch_t		b = {0};
645 	dev_info_t		*fdip = NULL;
646 
647 	if (!DRMACH_IS_DEVICE_ID(id))
648 		return (drerr_new(0, ESTF_INAPPROP, NULL));
649 	dp = id;
650 
651 	ca.dp = dp;
652 	ca.flags = flags;
653 	ca.err = NULL;		/* will be set if error detected */
654 	ca.dip = NULL;
655 
656 	b.arg = &ca;
657 	b.type = DEVI_BRANCH_PROM;
658 	b.create.prom_branch_select = drmach_prom_select;
659 	b.devi_branch_callback = drmach_branch_callback;
660 
661 	if (e_ddi_branch_create(ddi_root_node(), &b, &fdip,
662 	    DEVI_BRANCH_CHILD | DEVI_BRANCH_CONFIGURE) != 0) {
663 		char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
664 
665 		/*
666 		 * If non-NULL, fdip is returned held and must be released.
667 		 */
668 		if (fdip != NULL) {
669 			(void) ddi_pathname(fdip, path);
670 			ddi_release_devi(fdip);
671 		} else if (ca.dip != NULL) {
672 			/* safe to call ddi_pathname as dip already held */
673 			(void) ddi_pathname(ca.dip, path);
674 		} else {
675 			(void) strcpy(path, "<none>");
676 		}
677 
678 		err = drerr_new(1, ESTF_DRVFAIL, path);
679 		DRERR_SET_C(&ca.err, &err);
680 		kmem_free(path, MAXPATHLEN);
681 	}
682 
683 	return (ca.err);
684 }
685 
686 static sbd_error_t *
687 drmach_device_new(drmach_node_t *node,
688 	drmach_board_t *bp, drmach_device_t **dpp)
689 {
690 	int		 i;
691 	int		 rv;
692 	drmach_device_t	*dp;
693 	sbd_error_t	*err;
694 	char		 name[OBP_MAXDRVNAME];
695 
696 	rv = drmach_node_get_prop(node, OBP_NAME, name);
697 	if (rv) {
698 		/* every node is expected to have a name */
699 		err = drerr_new(1, ESTF_GETPROP,
700 			"PROM Node 0x%x: property %s",
701 			(uint_t)node->get_dnode(node), OBP_NAME);
702 
703 		return (err);
704 	}
705 
706 	/*
707 	 * The node currently being examined is not listed in the name2type[]
708 	 * array.  In this case, the node is no interest to drmach.  Both
709 	 * dp and err are initialized here to yield nothing (no device or
710 	 * error structure) for this case.
711 	 */
712 	for (i = 0; i < sizeof (name2type) / sizeof (name2type[0]); i++)
713 		if (strcmp(name2type[i].name, name) == 0)
714 			break;
715 
716 	if (i < sizeof (name2type) / sizeof (name2type[0])) {
717 		dp = kmem_zalloc(sizeof (drmach_device_t), KM_SLEEP);
718 
719 		dp->bp = bp;
720 		dp->unum = -1;
721 		dp->node = drmach_node_dup(node);
722 		dp->type = name2type[i].type;
723 
724 		err = (name2type[i].new)(dp);
725 		if (err) {
726 			drmach_node_dispose(node);
727 			kmem_free(dp, sizeof (*dp));
728 			dp = NULL;
729 		}
730 
731 		*dpp = dp;
732 		return (err);
733 	}
734 
735 	/*
736 	 * The node currently being examined is not listed in the name2type[]
737 	 * array.  In this case, the node is no interest to drmach.  Both
738 	 * dp and err are initialized here to yield nothing (no device or
739 	 * error structure) for this case.
740 	 */
741 	*dpp = NULL;
742 	return (NULL);
743 }
744 
745 static void
746 drmach_device_dispose(drmachid_t id)
747 {
748 	drmach_device_t *self = id;
749 
750 	if (self->node)
751 		drmach_node_dispose(self->node);
752 
753 	kmem_free(self, sizeof (*self));
754 }
755 
756 static sbd_error_t *
757 drmach_device_get_prop(drmach_device_t *dp, char *name, void *buf)
758 {
759 	sbd_error_t	*err = NULL;
760 	int		 rv;
761 
762 	rv = drmach_node_get_prop(dp->node, name, buf);
763 	if (rv) {
764 		err = drerr_new(1, ESTF_GETPROP,
765 			"%s::%s: property %s",
766 			dp->bp->cm.name, dp->cm.name, name);
767 	}
768 
769 	return (err);
770 }
771 
772 static sbd_error_t *
773 drmach_device_get_proplen(drmach_device_t *dp, char *name, int *len)
774 {
775 	sbd_error_t	*err = NULL;
776 	int		 rv;
777 
778 	rv = drmach_node_get_proplen(dp->node, name, len);
779 	if (rv) {
780 		err = drerr_new(1, ESTF_GETPROPLEN,
781 			"%s::%s: property %s",
782 			dp->bp->cm.name, dp->cm.name, name);
783 	}
784 
785 	return (err);
786 }
787 
788 static drmach_board_t *
789 drmach_board_new(int bnum)
790 {
791 	static sbd_error_t *drmach_board_release(drmachid_t);
792 	static sbd_error_t *drmach_board_status(drmachid_t, drmach_status_t *);
793 
794 	drmach_board_t	*bp;
795 
796 	bp = kmem_zalloc(sizeof (drmach_board_t), KM_SLEEP);
797 
798 	bp->cm.isa = (void *)drmach_board_new;
799 	bp->cm.release = drmach_board_release;
800 	bp->cm.status = drmach_board_status;
801 
802 	(void) drmach_board_name(bnum, bp->cm.name, sizeof (bp->cm.name));
803 
804 	bp->bnum = bnum;
805 	bp->devices = NULL;
806 	bp->connect_cpuid = -1;
807 	bp->tree = drmach_node_new();
808 	bp->assigned = !drmach_initialized;
809 	bp->powered = !drmach_initialized;
810 
811 	drmach_array_set(drmach_boards, bnum, bp);
812 	return (bp);
813 }
814 
815 static void
816 drmach_board_dispose(drmachid_t id)
817 {
818 	drmach_board_t *bp;
819 
820 	ASSERT(DRMACH_IS_BOARD_ID(id));
821 	bp = id;
822 
823 	if (bp->tree)
824 		drmach_node_dispose(bp->tree);
825 
826 	if (bp->devices)
827 		drmach_array_dispose(bp->devices, drmach_device_dispose);
828 
829 	kmem_free(bp, sizeof (*bp));
830 }
831 
832 static sbd_error_t *
833 drmach_board_status(drmachid_t id, drmach_status_t *stat)
834 {
835 	sbd_error_t	*err = NULL;
836 	drmach_board_t	*bp;
837 
838 	if (!DRMACH_IS_BOARD_ID(id))
839 		return (drerr_new(0, ESTF_INAPPROP, NULL));
840 	bp = id;
841 
842 	stat->assigned = bp->assigned;
843 	stat->powered = bp->powered;
844 	stat->busy = 0;			/* assume not busy */
845 	stat->configured = 0;		/* assume not configured */
846 	stat->empty = 0;
847 	stat->cond = bp->cond = SBD_COND_OK;
848 	strncpy(stat->type, "System Brd", sizeof (stat->type));
849 	stat->info[0] = '\0';
850 
851 	if (bp->devices) {
852 		int		 rv;
853 		int		 d_idx;
854 		drmachid_t	 d_id;
855 
856 		rv = drmach_array_first(bp->devices, &d_idx, &d_id);
857 		while (rv == 0) {
858 			drmach_status_t	d_stat;
859 
860 			err = drmach_status(d_id, &d_stat);
861 			if (err)
862 				break;
863 
864 			stat->busy |= d_stat.busy;
865 			stat->configured |= d_stat.configured;
866 
867 			rv = drmach_array_next(bp->devices, &d_idx, &d_id);
868 		}
869 	}
870 
871 	return (err);
872 }
873 
874 /* a simple routine to reduce redundancy of this common logic */
875 static pda_handle_t
876 drmach_pda_open(void)
877 {
878 	pda_handle_t ph;
879 
880 	ph = pda_open();
881 	if (ph == NULL) {
882 		/* catch in debug kernels */
883 		ASSERT(0);
884 		cmn_err(CE_WARN, "pda_open failed");
885 	}
886 
887 	return (ph);
888 }
889 
890 #ifdef DEBUG
891 int drmach_init_break = 0;
892 #endif
893 
894 static int
895 hold_rele_branch(dev_info_t *rdip, void *arg)
896 {
897 	int	i;
898 	int	*holdp = (int *)arg;
899 	char	*name = ddi_node_name(rdip);
900 
901 	/*
902 	 * For Starfire, we must be children of the root devinfo node
903 	 */
904 	ASSERT(ddi_get_parent(rdip) == ddi_root_node());
905 
906 	for (i = 0; i < sizeof (name2type) / sizeof (name2type[0]); i++)
907 		if (strcmp(name2type[i].name, name) == 0)
908 			break;
909 
910 	if (i == sizeof (name2type) / sizeof (name2type[0])) {
911 		/* Not of interest to us */
912 		return (DDI_WALK_PRUNECHILD);
913 	}
914 
915 	if (*holdp) {
916 		ASSERT(!e_ddi_branch_held(rdip));
917 		e_ddi_branch_hold(rdip);
918 	} else {
919 		ASSERT(e_ddi_branch_held(rdip));
920 		e_ddi_branch_rele(rdip);
921 	}
922 
923 	return (DDI_WALK_PRUNECHILD);
924 }
925 
926 static int
927 drmach_init(void)
928 {
929 	pnode_t		nodeid;
930 	dev_info_t	*rdip;
931 	int		hold, circ;
932 
933 #ifdef DEBUG
934 	if (drmach_init_break)
935 		debug_enter("drmach_init: drmach_init_break set\n");
936 #endif
937 	mutex_enter(&drmach_i_lock);
938 	if (drmach_initialized) {
939 		mutex_exit(&drmach_i_lock);
940 		return (0);
941 	}
942 
943 	drmach_boards = drmach_array_new(0, MAX_BOARDS - 1);
944 
945 	nodeid = prom_childnode(prom_rootnode());
946 	do {
947 		int		 bnum;
948 		drmachid_t	 id;
949 
950 		bnum = -1;
951 		(void) prom_getprop(nodeid, OBP_BOARDNUM, (caddr_t)&bnum);
952 		if (bnum == -1)
953 			continue;
954 
955 		if (drmach_array_get(drmach_boards, bnum, &id) == -1) {
956 			cmn_err(CE_WARN, "OBP node 0x%x has"
957 				" invalid property value, %s=%d",
958 				nodeid, OBP_BOARDNUM, bnum);
959 
960 			/* clean up */
961 			drmach_array_dispose(
962 				drmach_boards, drmach_board_dispose);
963 
964 			mutex_exit(&drmach_i_lock);
965 			return (-1);
966 		} else if (id == NULL)
967 			(void) drmach_board_new(bnum);
968 	} while ((nodeid = prom_nextnode(nodeid)) != OBP_NONODE);
969 
970 	drmach_shutdown_va = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
971 
972 	/*
973 	 * Walk immediate children of devinfo root node and hold
974 	 * all devinfo branches of interest.
975 	 */
976 	hold = 1;
977 	rdip = ddi_root_node();
978 
979 	ndi_devi_enter(rdip, &circ);
980 	ddi_walk_devs(ddi_get_child(rdip), hold_rele_branch, &hold);
981 	ndi_devi_exit(rdip, circ);
982 
983 	drmach_initialized = 1;
984 
985 	mutex_exit(&drmach_i_lock);
986 
987 	return (0);
988 }
989 
990 static int
991 drmach_fini(void)
992 {
993 	dev_info_t	*rdip;
994 	int		hold, circ;
995 
996 	if (drmach_initialized) {
997 		int		busy = 0;
998 		int		rv;
999 		int		idx;
1000 		drmachid_t	id;
1001 
1002 		ASSERT(drmach_boards != NULL);
1003 
1004 		rv = drmach_array_first(drmach_boards, &idx, &id);
1005 		while (rv == 0) {
1006 			sbd_error_t	*err;
1007 			drmach_status_t stat;
1008 
1009 			err = drmach_board_status(id, &stat);
1010 			if (err) {
1011 				/* catch in debug kernels */
1012 				ASSERT(0);
1013 				sbd_err_clear(&err);
1014 				busy = 1;
1015 			} else
1016 				busy |= stat.busy;
1017 
1018 			rv = drmach_array_next(drmach_boards, &idx, &id);
1019 		}
1020 
1021 		if (busy)
1022 			return (-1);
1023 
1024 		drmach_array_dispose(drmach_boards, drmach_board_dispose);
1025 		drmach_boards = NULL;
1026 
1027 		vmem_free(heap_arena, drmach_shutdown_va, PAGESIZE);
1028 
1029 		/*
1030 		 * Walk immediate children of the root devinfo node
1031 		 * releasing holds acquired on branches in drmach_init()
1032 		 */
1033 		hold = 0;
1034 		rdip = ddi_root_node();
1035 
1036 		ndi_devi_enter(rdip, &circ);
1037 		ddi_walk_devs(ddi_get_child(rdip), hold_rele_branch, &hold);
1038 		ndi_devi_exit(rdip, circ);
1039 
1040 		mutex_destroy(&drmach_i_lock);
1041 
1042 		drmach_initialized = 0;
1043 	}
1044 	if (drmach_xt_mb != NULL) {
1045 		vmem_free(static_alloc_arena, (void *)drmach_xt_mb,
1046 		    NCPU * sizeof (uchar_t));
1047 	}
1048 	if (drmach_shutdown_asm_mbox != NULL) {
1049 		vmem_free(static_alloc_arena, (void *)drmach_shutdown_asm_mbox,
1050 		    sizeof (struct drmach_shutdown_mbox));
1051 	}
1052 	return (0);
1053 }
1054 
1055 static sbd_error_t *
1056 drmach_get_mc_asr_addr(drmachid_t id, uint64_t *pa)
1057 {
1058 	drmach_device_t	*dp;
1059 	pnode_t		nodeid;
1060 	uint64_t	addr;
1061 
1062 	if (!DRMACH_IS_MEM_ID(id))
1063 		return (drerr_new(0, ESTF_INAPPROP, NULL));
1064 	dp = id;
1065 
1066 	nodeid = drmach_node_get_dnode(dp->node);
1067 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE)
1068 		return (DRMACH_INTERNAL_ERROR());
1069 
1070 	addr = mc_get_asr_addr(nodeid);
1071 	if (addr == (uint64_t)-1)
1072 		return (DRMACH_INTERNAL_ERROR());
1073 
1074 	*pa = addr;
1075 	return (NULL);
1076 }
1077 
1078 static sbd_error_t *
1079 drmach_get_mc_idle_addr(drmachid_t id, uint64_t *pa)
1080 {
1081 	drmach_device_t	*dp;
1082 	pnode_t		nodeid;
1083 	uint64_t	addr;
1084 
1085 	if (!DRMACH_IS_MEM_ID(id))
1086 		return (drerr_new(0, ESTF_INAPPROP, NULL));
1087 	dp = id;
1088 
1089 	nodeid = drmach_node_get_dnode(dp->node);
1090 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE)
1091 		return (DRMACH_INTERNAL_ERROR());
1092 
1093 	addr = mc_get_idle_addr(nodeid);
1094 	if (addr == (uint64_t)-1)
1095 		return (DRMACH_INTERNAL_ERROR());
1096 
1097 	*pa = addr;
1098 	return (NULL);
1099 }
1100 
1101 static sbd_error_t *
1102 drmach_read_mc_asr(drmachid_t id, uint_t *mcregp)
1103 {
1104 	drmach_device_t	*dp;
1105 	pnode_t		 nodeid;
1106 	sbd_error_t	*err;
1107 
1108 	if (!DRMACH_IS_MEM_ID(id))
1109 		return (drerr_new(0, ESTF_INAPPROP, NULL));
1110 	dp = id;
1111 
1112 	nodeid = drmach_node_get_dnode(dp->node);
1113 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE)
1114 		err = DRMACH_INTERNAL_ERROR();
1115 	else if (mc_read_asr(nodeid, mcregp) == -1)
1116 		err = DRMACH_INTERNAL_ERROR();
1117 	else
1118 		err = NULL;
1119 
1120 	return (err);
1121 }
1122 
1123 static sbd_error_t *
1124 drmach_write_mc_asr(drmachid_t id, uint_t mcreg)
1125 {
1126 	drmach_device_t	*dp;
1127 	pnode_t		 nodeid;
1128 	sbd_error_t	*err;
1129 
1130 	if (!DRMACH_IS_MEM_ID(id))
1131 		return (drerr_new(0, ESTF_INAPPROP, NULL));
1132 	dp = id;
1133 
1134 	nodeid = drmach_node_get_dnode(dp->node);
1135 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE)
1136 		err = DRMACH_INTERNAL_ERROR();
1137 	else if (mc_write_asr(nodeid, mcreg) == -1)
1138 		err = DRMACH_INTERNAL_ERROR();
1139 	else
1140 		err = NULL;
1141 
1142 	return (err);
1143 }
1144 
1145 static sbd_error_t *
1146 drmach_prep_rename_script(drmach_device_t *s_mem, drmach_device_t *t_mem,
1147 	uint64_t t_slice_offset, caddr_t buf, int buflen)
1148 {
1149 	int			i, b, m;
1150 	drmach_mc_idle_script_t	*isp;
1151 	drmach_rename_script_t	*rsp;
1152 	int			s_bd, t_bd;
1153 	uint_t			s_masr, t_masr;
1154 	uint64_t		s_new_basepa, t_new_basepa;
1155 	int			b_idx, rv;
1156 	sbd_error_t		*err;
1157 	drmachid_t		 b_id;
1158 	drmach_board_t		*brd;
1159 
1160 #ifdef DEBUG
1161 	/*
1162 	 * Starfire CPU/MEM/IO boards have only one MC per board.
1163 	 * This function has been coded with that fact in mind.
1164 	 */
1165 	ASSERT(MAX_MEM_UNITS_PER_BOARD == 1);
1166 
1167 	/*
1168 	 * calculate the maximum space that could be consumed,
1169 	 * then verify the available buffer space is adequate.
1170 	 */
1171 	m  = sizeof (drmach_mc_idle_script_t *) * 2; /* two MCs */
1172 	b  = sizeof (drmach_rename_script_t *) * 3 * MAX_CPU_UNITS_PER_BOARD;
1173 	b += sizeof (drmach_rename_script_t *) * 3 * MAX_IO_UNITS_PER_BOARD;
1174 	b *= MAX_BOARDS;
1175 	b += sizeof (drmach_rename_script_t *) * 3;
1176 	b += sizeof (drmach_rename_script_t *) * 1;
1177 	ASSERT(m + b < buflen);
1178 #endif
1179 
1180 	/*
1181 	 * construct an array of MC idle register addresses of
1182 	 * both MCs.  The array is zero terminated -- as expected
1183 	 * by drmach_copy_rename_prog__relocatable().
1184 	 */
1185 	isp = (drmach_mc_idle_script_t *)buf;
1186 
1187 	/* source mc */
1188 	err = drmach_get_mc_idle_addr(s_mem, &isp->idle_addr);
1189 	if (err)
1190 		return (err);
1191 	isp->mem = s_mem;
1192 	isp += 1;
1193 
1194 	/* target mc */
1195 	err = drmach_get_mc_idle_addr(t_mem, &isp->idle_addr);
1196 	if (err)
1197 		return (err);
1198 	isp->mem = t_mem;
1199 	isp += 1;
1200 
1201 	/* terminator */
1202 	isp->idle_addr = 0;
1203 	isp->mem = NULL;
1204 	isp += 1;
1205 
1206 	/* fetch source mc asr register value */
1207 	err = drmach_read_mc_asr(s_mem, &s_masr);
1208 	if (err)
1209 		return (err);
1210 	else if (s_masr & STARFIRE_MC_INTERLEAVE_MASK) {
1211 		return (drerr_new(1, ESTF_INTERBOARD, "%s::%s",
1212 				s_mem->bp->cm.name, s_mem->cm.name));
1213 	}
1214 
1215 	/* fetch target mc asr register value */
1216 	err = drmach_read_mc_asr(t_mem, &t_masr);
1217 	if (err)
1218 		return (err);
1219 	else if (t_masr & STARFIRE_MC_INTERLEAVE_MASK) {
1220 		return (drerr_new(1, ESTF_INTERBOARD, "%s::%s",
1221 				t_mem->bp->cm.name, t_mem->cm.name));
1222 	}
1223 
1224 	/* get new source base pa from target's masr */
1225 	s_new_basepa = mc_asr_to_pa(t_masr);
1226 
1227 	/*
1228 	 * remove any existing slice offset to realign
1229 	 * memory with board's slice boundary
1230 	 */
1231 	s_new_basepa &= ~ (mc_get_mem_alignment() - 1);
1232 
1233 	/* get new target base pa from source's masr */
1234 	t_new_basepa  = mc_asr_to_pa(s_masr);
1235 
1236 	/* remove any existing slice offset, then apply new offset */
1237 	t_new_basepa &= ~ (mc_get_mem_alignment() - 1);
1238 	t_new_basepa += t_slice_offset;
1239 
1240 	/* encode new base pa into s_masr.  turn off mem present bit */
1241 	s_masr  = mc_pa_to_asr(s_masr, s_new_basepa);
1242 	s_masr &= ~STARFIRE_MC_MEM_PRESENT_MASK;
1243 
1244 	/* encode new base pa into t_masr.  turn on mem present bit */
1245 	t_masr  = mc_pa_to_asr(t_masr, t_new_basepa);
1246 	t_masr |= STARFIRE_MC_MEM_PRESENT_MASK;
1247 
1248 	/*
1249 	 * Step 0:	Mark source memory as not present.
1250 	 */
1251 	m = 0;
1252 	rsp = (drmach_rename_script_t *)isp;
1253 	err = drmach_get_mc_asr_addr(s_mem, &rsp[m].masr_addr);
1254 	if (err)
1255 		return (err);
1256 	rsp[m].masr = s_masr;
1257 	m++;
1258 
1259 	/*
1260 	 * Step 1:	Write source base address to target MC
1261 	 *		with present bit off.
1262 	 */
1263 	err = drmach_get_mc_asr_addr(t_mem, &rsp[m].masr_addr);
1264 	if (err)
1265 		return (err);
1266 	rsp[m].masr = t_masr & ~STARFIRE_MC_MEM_PRESENT_MASK;
1267 	m++;
1268 
1269 	/*
1270 	 * Step 2:	Now rewrite target reg with present bit on.
1271 	 */
1272 	rsp[m].masr_addr = rsp[m-1].masr_addr;
1273 	rsp[m].masr = t_masr;
1274 	m++;
1275 
1276 	s_bd = s_mem->bp->bnum;
1277 	t_bd = t_mem->bp->bnum;
1278 
1279 	DRMACH_PR("preparing script for CPU and IO units:\n");
1280 
1281 	rv = drmach_array_first(drmach_boards, &b_idx, &b_id);
1282 	if (rv) {
1283 		/* catch this in debug kernels */
1284 		ASSERT(0);
1285 		return (DRMACH_INTERNAL_ERROR());
1286 	}
1287 
1288 	do {
1289 		int			 d_idx;
1290 		drmachid_t		 d_id;
1291 		drmach_device_t		*device;
1292 
1293 		ASSERT(DRMACH_IS_BOARD_ID(b_id));
1294 		brd = b_id;
1295 		b = brd->bnum;
1296 
1297 		/*
1298 		 * Step 3:	Update PC MADR tables for CPUs.
1299 		 */
1300 		if (brd->devices == NULL) {
1301 			/* devices not initialized */
1302 			continue;
1303 		}
1304 
1305 		rv = drmach_array_first(brd->devices, &d_idx, &d_id);
1306 		if (rv) {
1307 			/* must mean no devices on this board */
1308 			break;
1309 		}
1310 
1311 		DRMACH_PR("\t%s\n", brd->cm.name);
1312 
1313 		do {
1314 			ASSERT(DRMACH_IS_DEVICE_ID(d_id));
1315 
1316 			if (!DRMACH_IS_CPU_ID(d_id))
1317 				continue;
1318 
1319 			device = d_id;
1320 			i = device->unum;
1321 
1322 			DRMACH_PR("\t\t%s\n", device->cm.name);
1323 
1324 			/*
1325 			 * Disabled detaching mem node.
1326 			 */
1327 			rsp[m].masr_addr = STARFIRE_PC_MADR_ADDR(b, s_bd, i);
1328 			rsp[m].masr = s_masr;
1329 			m++;
1330 			/*
1331 			 * Always write masr with present bit
1332 			 * off and then again with it on.
1333 			 */
1334 			rsp[m].masr_addr = STARFIRE_PC_MADR_ADDR(b, t_bd, i);
1335 			rsp[m].masr = t_masr & ~STARFIRE_MC_MEM_PRESENT_MASK;
1336 			m++;
1337 			rsp[m].masr_addr = rsp[m-1].masr_addr;
1338 			rsp[m].masr = t_masr;
1339 			m++;
1340 
1341 		} while (drmach_array_next(brd->devices, &d_idx, &d_id) == 0);
1342 
1343 		/*
1344 		 * Step 4:	Update PC MADR tables for IOs.
1345 		 */
1346 		rv = drmach_array_first(brd->devices, &d_idx, &d_id);
1347 		/* this worked for previous loop, must work here too */
1348 		ASSERT(rv == 0);
1349 
1350 		do {
1351 			ASSERT(DRMACH_IS_DEVICE_ID(d_id));
1352 
1353 			if (!DRMACH_IS_IO_ID(d_id))
1354 				continue;
1355 
1356 			device = d_id;
1357 			i = device->unum;
1358 
1359 			DRMACH_PR("\t\t%s\n", device->cm.name);
1360 
1361 			/*
1362 			 * Disabled detaching mem node.
1363 			 */
1364 			rsp[m].masr_addr = STARFIRE_PC_MADR_ADDR(b, s_bd, i+4);
1365 			rsp[m].masr = s_masr;
1366 			m++;
1367 			/*
1368 			 * Always write masr with present bit
1369 			 * off and then again with it on.
1370 			 */
1371 			rsp[m].masr_addr = STARFIRE_PC_MADR_ADDR(b, t_bd, i+4);
1372 			rsp[m].masr = t_masr & ~STARFIRE_MC_MEM_PRESENT_MASK;
1373 			m++;
1374 			rsp[m].masr_addr = rsp[m-1].masr_addr;
1375 			rsp[m].masr = t_masr;
1376 			m++;
1377 
1378 		} while (drmach_array_next(brd->devices, &d_idx, &d_id) == 0);
1379 	} while (drmach_array_next(drmach_boards, &b_idx, &b_id) == 0);
1380 
1381 	/*
1382 	 * Zero masr_addr value indicates the END.
1383 	 */
1384 	rsp[m].masr_addr = 0ull;
1385 	rsp[m].masr = 0;
1386 	DRMACH_PR("number of steps in rename script = %d\n", m);
1387 	m++;
1388 
1389 	/* paranoia */
1390 	ASSERT((caddr_t)&rsp[m] <= buf + buflen);
1391 
1392 #ifdef DEBUG
1393 	{
1394 		int	j;
1395 
1396 		DRMACH_PR("mc idle register address list:");
1397 		isp = (drmach_mc_idle_script_t *)buf;
1398 		DRMACH_PR("source mc idle addr 0x%lx, mem id %p",
1399 			isp[0].idle_addr, isp[0].mem);
1400 		DRMACH_PR("target mc idle addr 0x%lx, mem id %p",
1401 			isp[1].idle_addr, isp[1].mem);
1402 		ASSERT(isp[2].idle_addr == 0);
1403 
1404 		DRMACH_PR("copy-rename script:");
1405 		for (j = 0; j < m; j++) {
1406 			DRMACH_PR("0x%lx = 0x%08x",
1407 				rsp[j].masr_addr, rsp[j].masr);
1408 		}
1409 
1410 		DELAY(1000000);
1411 	}
1412 #endif
1413 
1414 	/* return number of bytes consumed */
1415 	b = (caddr_t)&rsp[m] - buf;
1416 	DRMACH_PR("total number of bytes consumed is %d\n", b);
1417 	ASSERT(b <= buflen);
1418 
1419 #ifdef lint
1420 	buflen = buflen;
1421 #endif
1422 
1423 	return (NULL);
1424 }
1425 
1426 /*
1427  * The routine performs the necessary memory COPY and MC adr SWITCH.
1428  * Both operations MUST be at the same "level" so that the stack is
1429  * maintained correctly between the copy and switch.  The switch
1430  * portion implements a caching mechanism to guarantee the code text
1431  * is cached prior to execution.  This is to guard against possible
1432  * memory access while the MC adr's are being modified.
1433  *
1434  * IMPORTANT: The _drmach_copy_rename_end() function must immediately
1435  * follow drmach_copy_rename_prog__relocatable() so that the correct
1436  * "length" of the drmach_copy_rename_prog__relocatable can be
1437  * calculated.  This routine MUST be a LEAF function, i.e. it can
1438  * make NO function calls, primarily for two reasons:
1439  *
1440  *	1. We must keep the stack consistent across the "switch".
1441  *	2. Function calls are compiled to relative offsets, and
1442  *	   we execute this function we'll be executing it from
1443  *	   a copied version in a different area of memory, thus
1444  *	   the relative offsets will be bogus.
1445  *
1446  * Moreover, it must have the "__relocatable" suffix to inform DTrace
1447  * providers (and anything else, for that matter) that this
1448  * function's text is manually relocated elsewhere before it is
1449  * executed.  That is, it cannot be safely instrumented with any
1450  * methodology that is PC-relative.
1451  */
1452 static void
1453 drmach_copy_rename_prog__relocatable(drmach_copy_rename_program_t *prog)
1454 {
1455 	extern void drmach_exec_script_il(drmach_rename_script_t *rsp);
1456 
1457 	drmach_mc_idle_script_t		*isp;
1458 	struct memlist			*ml;
1459 	int				csize;
1460 	int				lnsize;
1461 	uint64_t			caddr;
1462 
1463 	isp = (drmach_mc_idle_script_t *)prog->data;
1464 
1465 	caddr = ecache_flushaddr;
1466 	csize = (cpunodes[CPU->cpu_id].ecache_size << 1);
1467 	lnsize = cpunodes[CPU->cpu_id].ecache_linesize;
1468 
1469 	/*
1470 	 * DO COPY.
1471 	 */
1472 	for (ml = prog->c_ml; ml; ml = ml->next) {
1473 		uint64_t	s_pa, t_pa;
1474 		uint64_t	nbytes;
1475 
1476 		s_pa = prog->s_copybasepa + ml->address;
1477 		t_pa = prog->t_copybasepa + ml->address;
1478 		nbytes = ml->size;
1479 
1480 		while (nbytes != 0ull) {
1481 			/*
1482 			 * This copy does NOT use an ASI
1483 			 * that avoids the Ecache, therefore
1484 			 * the dst_pa addresses may remain
1485 			 * in our Ecache after the dst_pa
1486 			 * has been removed from the system.
1487 			 * A subsequent write-back to memory
1488 			 * will cause an ARB-stop because the
1489 			 * physical address no longer exists
1490 			 * in the system. Therefore we must
1491 			 * flush out local Ecache after we
1492 			 * finish the copy.
1493 			 */
1494 
1495 			/* copy 32 bytes at src_pa to dst_pa */
1496 			bcopy32_il(s_pa, t_pa);
1497 
1498 			/* increment by 32 bytes */
1499 			s_pa += (4 * sizeof (uint64_t));
1500 			t_pa += (4 * sizeof (uint64_t));
1501 
1502 			/* decrement by 32 bytes */
1503 			nbytes -= (4 * sizeof (uint64_t));
1504 		}
1505 	}
1506 
1507 	/*
1508 	 * Since bcopy32_il() does NOT use an ASI to bypass
1509 	 * the Ecache, we need to flush our Ecache after
1510 	 * the copy is complete.
1511 	 */
1512 	flush_ecache_il(caddr, csize, lnsize);		/* inline version */
1513 
1514 	/*
1515 	 * Wait for MCs to go idle.
1516 	 */
1517 	do {
1518 		register int	t = 10;
1519 		register uint_t	v;
1520 
1521 		/* loop t cycles waiting for each mc to indicate it's idle */
1522 		do {
1523 			v = ldphysio_il(isp->idle_addr)
1524 				& STARFIRE_MC_IDLE_MASK;
1525 
1526 		} while (v != STARFIRE_MC_IDLE_MASK && t-- > 0);
1527 
1528 		/* bailout if timedout */
1529 		if (t <= 0) {
1530 			prog->restless_mc = isp->mem;
1531 			return;
1532 		}
1533 
1534 		isp += 1;
1535 
1536 		/* stop if terminating zero has been reached */
1537 	} while (isp->idle_addr != 0);
1538 
1539 	/* advance passed terminating zero */
1540 	isp += 1;
1541 
1542 	/*
1543 	 * The following inline assembly routine caches
1544 	 * the rename script and then caches the code that
1545 	 * will do the rename.  This is necessary
1546 	 * so that we don't have any memory references during
1547 	 * the reprogramming.  We accomplish this by first
1548 	 * jumping through the code to guarantee it's cached
1549 	 * before we actually execute it.
1550 	 */
1551 	drmach_exec_script_il((drmach_rename_script_t *)isp);
1552 }
1553 
1554 static void
1555 drmach_copy_rename_end(void)
1556 {
1557 	/*
1558 	 * IMPORTANT:	This function's location MUST be located immediately
1559 	 *		following drmach_copy_rename_prog__relocatable to
1560 	 *		accurately estimate its size.  Note that this assumes
1561 	 *		the compiler keeps these functions in the order in
1562 	 *		which they appear :-o
1563 	 */
1564 }
1565 
1566 sbd_error_t *
1567 drmach_copy_rename_init(drmachid_t t_id, uint64_t t_slice_offset,
1568 	drmachid_t s_id, struct memlist *c_ml, drmachid_t *pgm_id)
1569 {
1570 	drmach_device_t	*s_mem;
1571 	drmach_device_t	*t_mem;
1572 	struct memlist	*x_ml;
1573 	uint64_t	off_mask, s_copybasepa, t_copybasepa, t_basepa;
1574 	int		len;
1575 	caddr_t		bp, wp;
1576 	pda_handle_t	ph;
1577 	sbd_error_t	*err;
1578 	drmach_copy_rename_program_t *prog;
1579 
1580 	if (!DRMACH_IS_MEM_ID(s_id))
1581 		return (drerr_new(0, ESTF_INAPPROP, NULL));
1582 	if (!DRMACH_IS_MEM_ID(t_id))
1583 		return (drerr_new(0, ESTF_INAPPROP, NULL));
1584 	s_mem = s_id;
1585 	t_mem = t_id;
1586 
1587 	/* get starting physical address of target memory */
1588 	err = drmach_mem_get_base_physaddr(t_id, &t_basepa);
1589 	if (err)
1590 		return (err);
1591 
1592 	/* calculate slice offset mask from slice size */
1593 	off_mask = mc_get_mem_alignment() - 1;
1594 
1595 	/* calculate source and target base pa */
1596 	s_copybasepa = c_ml->address;
1597 	t_copybasepa = t_basepa + ((c_ml->address & off_mask) - t_slice_offset);
1598 
1599 	/* paranoia */
1600 	ASSERT((c_ml->address & off_mask) >= t_slice_offset);
1601 
1602 	/* adjust copy memlist addresses to be relative to copy base pa */
1603 	x_ml = c_ml;
1604 	while (x_ml != NULL) {
1605 		x_ml->address -= s_copybasepa;
1606 		x_ml = x_ml->next;
1607 	}
1608 
1609 #ifdef DEBUG
1610 	{
1611 	uint64_t s_basepa, s_size, t_size;
1612 
1613 	x_ml = c_ml;
1614 	while (x_ml->next != NULL)
1615 		x_ml = x_ml->next;
1616 
1617 	DRMACH_PR("source copy span: base pa 0x%lx, end pa 0x%lx\n",
1618 		s_copybasepa,
1619 		s_copybasepa + x_ml->address + x_ml->size);
1620 
1621 	DRMACH_PR("target copy span: base pa 0x%lx, end pa 0x%lx\n",
1622 		t_copybasepa,
1623 		t_copybasepa + x_ml->address + x_ml->size);
1624 
1625 	DRMACH_PR("copy memlist (relative to copy base pa):\n");
1626 	MEMLIST_DUMP(c_ml);
1627 
1628 	err = drmach_mem_get_base_physaddr(s_id, &s_basepa);
1629 	ASSERT(err == NULL);
1630 
1631 	err = drmach_mem_get_size(s_id, &s_size);
1632 	ASSERT(err == NULL);
1633 
1634 	err = drmach_mem_get_size(t_id, &t_size);
1635 	ASSERT(err == NULL);
1636 
1637 	DRMACH_PR("current source base pa 0x%lx, size 0x%lx\n",
1638 		s_basepa, s_size);
1639 	DRMACH_PR("current target base pa 0x%lx, size 0x%lx\n",
1640 		t_basepa, t_size);
1641 
1642 	ASSERT(s_copybasepa + x_ml->address + x_ml->size <= s_basepa + s_size);
1643 	ASSERT(t_copybasepa + x_ml->address + x_ml->size <= t_basepa + t_size);
1644 	}
1645 #endif
1646 
1647 	ph = drmach_pda_open();
1648 	if (ph == NULL)
1649 		return (DRMACH_INTERNAL_ERROR());
1650 
1651 	/*
1652 	 * bp will be page aligned, since we're calling
1653 	 * kmem_zalloc() with an exact multiple of PAGESIZE.
1654 	 */
1655 	wp = bp = kmem_zalloc(PAGESIZE, KM_SLEEP);
1656 
1657 	/* allocate space for copy rename struct */
1658 	len = sizeof (drmach_copy_rename_program_t);
1659 	DRMACH_PR("prog = 0x%p, header len %d\n", wp, len);
1660 	prog = (drmach_copy_rename_program_t *)wp;
1661 	wp += (len + ecache_alignsize - 1) & ~ (ecache_alignsize - 1);
1662 
1663 	/*
1664 	 * Copy the code for the copy-rename routine into
1665 	 * a page aligned piece of memory.  We do this to guarantee
1666 	 * that we're executing within the same page and thus reduce
1667 	 * the possibility of cache collisions between different
1668 	 * pages.
1669 	 */
1670 	len = (int)((ulong_t)drmach_copy_rename_end -
1671 		    (ulong_t)drmach_copy_rename_prog__relocatable);
1672 	ASSERT(wp + len < bp + PAGESIZE);
1673 	bcopy((caddr_t)drmach_copy_rename_prog__relocatable, wp, len);
1674 
1675 	DRMACH_PR("copy-rename function 0x%p, len %d\n", wp, len);
1676 	prog->run = (void (*)())wp;
1677 	wp += (len + ecache_alignsize - 1) & ~ (ecache_alignsize - 1);
1678 
1679 	/*
1680 	 * Prepare data page that will contain script of
1681 	 * operations to perform during copy-rename.
1682 	 * Allocate temporary buffer to hold script.
1683 	 */
1684 	err = drmach_prep_rename_script(s_mem, t_mem, t_slice_offset,
1685 		wp, PAGESIZE - (wp - bp));
1686 	if (err) {
1687 		(void) drmach_copy_rename_fini(prog);
1688 		return (err);
1689 	}
1690 
1691 	DRMACH_PR("copy-rename script 0x%p, len %d\n", wp, len);
1692 	prog->data = wp;
1693 	wp += (len + ecache_alignsize - 1) & ~ (ecache_alignsize - 1);
1694 
1695 	prog->ph = ph;
1696 	prog->s_copybasepa = s_copybasepa;
1697 	prog->t_copybasepa = t_copybasepa;
1698 	prog->c_ml = c_ml;
1699 	*pgm_id = prog;
1700 
1701 	return (NULL);
1702 }
1703 
1704 sbd_error_t *
1705 drmach_copy_rename_fini(drmachid_t id)
1706 {
1707 	drmach_copy_rename_program_t	*prog = id;
1708 	sbd_error_t			*err = NULL;
1709 
1710 	if (prog->c_ml != NULL)
1711 		memlist_delete(prog->c_ml);
1712 
1713 	if (prog->ph != NULL)
1714 		pda_close(prog->ph);
1715 
1716 	if (prog->restless_mc != 0) {
1717 		cmn_err(CE_WARN, "MC did not idle; OBP Node 0x%x",
1718 			(uint_t)drmach_node_get_dnode(prog->restless_mc->node));
1719 
1720 		err = DRMACH_INTERNAL_ERROR();
1721 	}
1722 
1723 	kmem_free(prog, PAGESIZE);
1724 
1725 	return (err);
1726 }
1727 
1728 static sbd_error_t *
1729 drmach_io_new(drmach_device_t *dp)
1730 {
1731 	static sbd_error_t *drmach_io_release(drmachid_t);
1732 	static sbd_error_t *drmach_io_status(drmachid_t, drmach_status_t *);
1733 
1734 	sbd_error_t	*err;
1735 	int		 portid;
1736 
1737 	err = drmach_device_get_prop(dp, "upa-portid", &portid);
1738 	if (err == NULL) {
1739 		ASSERT(portid & 0x40);
1740 		dp->unum = portid & 1;
1741 	}
1742 
1743 	dp->cm.isa = (void *)drmach_io_new;
1744 	dp->cm.release = drmach_io_release;
1745 	dp->cm.status = drmach_io_status;
1746 
1747 	snprintf(dp->cm.name, sizeof (dp->cm.name), "%s%d", dp->type, dp->unum);
1748 
1749 	return (err);
1750 }
1751 
1752 static void
1753 drmach_iopc_op(pda_handle_t ph, drmach_iopc_op_t op)
1754 {
1755 	register int b;
1756 
1757 	for (b = 0; b < MAX_BOARDS; b++) {
1758 		int		p;
1759 		ushort_t	bda_ioc;
1760 		board_desc_t	*bdesc;
1761 
1762 		if (pda_board_present(ph, b) == 0)
1763 			continue;
1764 
1765 		bdesc = (board_desc_t *)pda_get_board_info(ph, b);
1766 		/*
1767 		 * Update PCs for IOCs.
1768 		 */
1769 		bda_ioc = bdesc->bda_ioc;
1770 		for (p = 0; p < MAX_IOCS; p++) {
1771 			u_longlong_t	idle_addr;
1772 			uchar_t		value;
1773 
1774 			if (BDA_NBL(bda_ioc, p) != BDAN_GOOD)
1775 				continue;
1776 
1777 			idle_addr = STARFIRE_BB_PC_ADDR(b, p, 1);
1778 
1779 			switch (op) {
1780 			case DO_PAUSE:
1781 				value = STARFIRE_BB_PC_PAUSE(p);
1782 				break;
1783 
1784 			case DO_IDLE:
1785 				value = STARFIRE_BB_PC_IDLE(p);
1786 				break;
1787 
1788 			case DO_UNPAUSE:
1789 				value = ldbphysio(idle_addr);
1790 				value &= ~STARFIRE_BB_PC_PAUSE(p);
1791 				break;
1792 
1793 			case DO_UNIDLE:
1794 				value = ldbphysio(idle_addr);
1795 				value &= ~STARFIRE_BB_PC_IDLE(p);
1796 				break;
1797 
1798 			default:
1799 				cmn_err(CE_PANIC,
1800 					"drmach_iopc_op: unknown op (%d)",
1801 					(int)op);
1802 				/*NOTREACHED*/
1803 			}
1804 			stbphysio(idle_addr, value);
1805 		}
1806 	}
1807 }
1808 
1809 void
1810 drmach_copy_rename(drmachid_t id)
1811 {
1812 	drmach_copy_rename_program_t	*prog = id;
1813 	uint64_t			neer;
1814 
1815 	/*
1816 	 * UPA IDLE
1817 	 * Protocol = PAUSE -> IDLE -> UNPAUSE
1818 	 * In reality since we only "idle" the IOPCs it's sufficient
1819 	 * to just issue the IDLE operation since (in theory) all IOPCs
1820 	 * in the field are PC6.  However, we'll be robust and do the
1821 	 * proper workaround protocol so that we never have to worry!
1822 	 */
1823 	drmach_iopc_op(prog->ph, DO_PAUSE);
1824 	drmach_iopc_op(prog->ph, DO_IDLE);
1825 	DELAY(100);
1826 	drmach_iopc_op(prog->ph, DO_UNPAUSE);
1827 	DELAY(100);
1828 
1829 	/* disable CE reporting */
1830 	neer = get_error_enable();
1831 	set_error_enable(neer & ~EER_CEEN);
1832 
1833 	/* run the copy/rename program */
1834 	prog->run(prog);
1835 
1836 	/* enable CE reporting */
1837 	set_error_enable(neer);
1838 
1839 	/*
1840 	 * UPA UNIDLE
1841 	 * Protocol = UNIDLE
1842 	 */
1843 	drmach_iopc_op(prog->ph, DO_UNIDLE);
1844 	DELAY(100);
1845 }
1846 
1847 /*
1848  * The counter-timer and perf-counter nodes are not being cleaned
1849  * up after a board that was present at start of day is detached.
1850  * If the board has become unconfigured with this operation, walk
1851  * the prom tree and find all counter-timer and perf-counter nodes
1852  * that have the same board number as the board that was just
1853  * unconfigured and remove them.
1854  */
1855 static sbd_error_t *
1856 drmach_remove_counter_nodes(drmachid_t id)
1857 {
1858 	int		num;
1859 	char		name[OBP_MAXDRVNAME];
1860 	pnode_t		child;
1861 	dev_info_t	*dip;
1862 	sbd_error_t	*err;
1863 	drmach_status_t	stat;
1864 	drmach_board_t	*bp;
1865 
1866 	if (!DRMACH_IS_BOARD_ID(id)) {
1867 		return (drerr_new(0, ESTF_INAPPROP, NULL));
1868 	}
1869 
1870 	if ((err = drmach_board_status(id, &stat)) != NULL) {
1871 		return (err);
1872 	}
1873 
1874 	/*
1875 	 * Only clean up the counter-timer and perf-counter
1876 	 * nodes when the entire board is unconfigured.
1877 	 */
1878 	if (stat.configured) {
1879 		return (NULL);
1880 	}
1881 
1882 	bp = (drmach_board_t *)id;
1883 
1884 	err = NULL;
1885 
1886 	for (child = prom_childnode(prom_rootnode()); child != OBP_NONODE;
1887 	    child = prom_nextnode(child)) {
1888 
1889 		if (prom_getprop(child, OBP_BOARDNUM, (caddr_t)&num) == -1) {
1890 			continue;
1891 		}
1892 
1893 		if (bp->bnum != num) {
1894 			continue;
1895 		}
1896 
1897 		if (prom_getprop(child, OBP_NAME, (caddr_t)name) == -1) {
1898 			continue;
1899 		}
1900 
1901 		if (strncmp(name, MISC_COUNTER_TIMER_DEVNAME, OBP_MAXDRVNAME) &&
1902 		    strncmp(name, MISC_PERF_COUNTER_DEVNAME, OBP_MAXDRVNAME)) {
1903 				continue;
1904 		}
1905 
1906 		/* Root node doesn't have to be held */
1907 		dip = e_ddi_nodeid_to_dip(child);
1908 
1909 		/*
1910 		 * If the node is only in the OBP tree, then
1911 		 * we don't have to remove it.
1912 		 */
1913 		if (dip) {
1914 			dev_info_t *fdip = NULL;
1915 
1916 			DRMACH_PR("removing %s devinfo node\n", name);
1917 
1918 			e_ddi_branch_hold(dip);
1919 			ddi_release_devi(dip); /* held in e_ddi_nodeid_to_dip */
1920 
1921 			if (e_ddi_branch_destroy(dip, &fdip, 0)) {
1922 				char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1923 
1924 				/*
1925 				 * If non-NULL, fdip is held and must be
1926 				 * released.
1927 				 */
1928 				if (fdip != NULL) {
1929 					(void) ddi_pathname(fdip, path);
1930 					ddi_release_devi(fdip);
1931 				} else {
1932 					(void) ddi_pathname(dip, path);
1933 				}
1934 
1935 				err = drerr_new(1, ESTF_DRVFAIL, path);
1936 				kmem_free(path, MAXPATHLEN);
1937 				e_ddi_branch_rele(dip);
1938 				break;
1939 			}
1940 		}
1941 	}
1942 
1943 	return (err);
1944 }
1945 
1946 /*ARGSUSED*/
1947 sbd_error_t *
1948 drmach_pre_op(int cmd, drmachid_t id, drmach_opts_t *opts)
1949 {
1950 	/* allow status and ncm operations to always succeed */
1951 	if ((cmd == SBD_CMD_STATUS) || (cmd == SBD_CMD_GETNCM)) {
1952 		return (NULL);
1953 	}
1954 
1955 	/* check all other commands for the required option string */
1956 	if ((opts->size > 0) && (opts->copts != NULL)) {
1957 
1958 		DRMACH_PR("platform options: %s\n", opts->copts);
1959 
1960 		if (strstr(opts->copts, "xfdr") != NULL) {
1961 			return (NULL);
1962 		}
1963 	}
1964 
1965 	return (drerr_new(0, ESTF_SUPPORT, NULL));
1966 }
1967 
1968 /*ARGSUSED*/
1969 sbd_error_t *
1970 drmach_post_op(int cmd, drmachid_t id, drmach_opts_t *opts)
1971 {
1972 	sbd_error_t	*err = NULL;
1973 
1974 	switch (cmd) {
1975 	case SBD_CMD_UNCONFIGURE:
1976 
1977 		err = drmach_remove_counter_nodes(id);
1978 		break;
1979 
1980 	case SBD_CMD_CONFIGURE:
1981 	case SBD_CMD_DISCONNECT:
1982 	case SBD_CMD_CONNECT:
1983 	case SBD_CMD_GETNCM:
1984 	case SBD_CMD_STATUS:
1985 		break;
1986 
1987 	default:
1988 		break;
1989 	}
1990 
1991 	return (err);
1992 }
1993 
1994 sbd_error_t *
1995 drmach_board_assign(int bnum, drmachid_t *id)
1996 {
1997 	sbd_error_t	*err;
1998 
1999 	if (!drmach_initialized && drmach_init() == -1) {
2000 		err = DRMACH_INTERNAL_ERROR();
2001 	} else if (drmach_array_get(drmach_boards, bnum, id) == -1) {
2002 		err = drerr_new(1, ESTF_BNUM, "%d", bnum);
2003 	} else if (*id != NULL) {
2004 		err = NULL;
2005 	} else {
2006 		drmach_board_t	*bp;
2007 
2008 		*id  = (drmachid_t)drmach_board_new(bnum);
2009 		bp = *id;
2010 		bp->assigned = 1;
2011 		err = NULL;
2012 	}
2013 
2014 	return (err);
2015 }
2016 
2017 static int
2018 drmach_attach_board(void *arg)
2019 {
2020 	drmach_board_t	*obj = (drmach_board_t *)arg;
2021 	cpuset_t	cset;
2022 	int		retval;
2023 
2024 	/*
2025 	 * OBP disables traps during the board probe.
2026 	 * So, in order to prevent cross-call/cross-trap timeouts,
2027 	 * and thus panics, we effectively block anybody from
2028 	 * issuing xc's/xt's by doing a promsafe_xc_attention.
2029 	 * In the previous version of Starfire DR (2.6), a timeout
2030 	 * suspension mechanism was implemented in the send-mondo
2031 	 * assembly.  That mechanism is unnecessary with the
2032 	 * existence of xc_attention/xc_dismissed.
2033 	 */
2034 	cset = cpu_ready_set;
2035 	promsafe_xc_attention(cset);
2036 
2037 	retval = prom_starfire_add_brd(obj->connect_cpuid);
2038 
2039 	xc_dismissed(cset);
2040 
2041 	return (retval);
2042 }
2043 
2044 sbd_error_t *
2045 drmach_board_connect(drmachid_t id, drmach_opts_t *opts)
2046 {
2047 	drmach_board_t	*obj = (drmach_board_t *)id;
2048 	int		retval;
2049 	sbd_error_t	*err;
2050 	char		*cptr, *copts;
2051 
2052 	if (!DRMACH_IS_BOARD_ID(id))
2053 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2054 
2055 	if (opts->size > 0)
2056 		copts = opts->copts;
2057 
2058 	if ((cptr = strstr(copts, "cpuid=")) != NULL) {
2059 		int cpuid;
2060 
2061 		cptr += strlen("cpuid=");
2062 		cpuid = stoi(&cptr);
2063 
2064 		if (DRMACH_CPUID2BNUM(cpuid) == obj->bnum) {
2065 			obj->connect_cpuid = cpuid;
2066 			obj->assigned = 1;
2067 		} else
2068 			return (drerr_new(1, ESTF_SETCPUVAL, "%d", cpuid));
2069 	} else {
2070 		/* cpuid was not specified */
2071 		obj->connect_cpuid = -1;
2072 	}
2073 
2074 	if (obj->connect_cpuid == -1) {
2075 		err =  drerr_new(1, ESTF_NOCPUID, obj->cm.name);
2076 		return (err);
2077 	}
2078 
2079 	cmn_err(CE_CONT, "DRMACH: PROM attach %s CPU %d\n",
2080 		obj->cm.name, obj->connect_cpuid);
2081 
2082 	retval = prom_tree_update(drmach_attach_board, obj);
2083 
2084 	if (retval == 0)
2085 		err = NULL;
2086 	else {
2087 		cmn_err(CE_WARN, "prom error: prom_starfire_add_brd(%d) "
2088 			"returned %d", obj->connect_cpuid, retval);
2089 
2090 		err = drerr_new(1, ESTF_PROBE, obj->cm.name);
2091 	}
2092 
2093 	obj->connect_cpuid = -1;
2094 
2095 	return (err);
2096 }
2097 
2098 /*ARGSUSED*/
2099 sbd_error_t *
2100 drmach_board_disconnect(drmachid_t id, drmach_opts_t *opts)
2101 {
2102 	drmach_board_t		*bp;
2103 	int			rv;
2104 	int			d_idx;	/* device index */
2105 	drmachid_t		d_id;	/* device ID */
2106 	sbd_error_t		*err;
2107 
2108 	if (!DRMACH_IS_BOARD_ID(id))
2109 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2110 
2111 	bp = id;
2112 
2113 	/*
2114 	 * We need to make sure all of the board's device nodes
2115 	 * have been removed from the Solaris device tree before
2116 	 * continuing with the disconnect. Otherwise, we could
2117 	 * disconnect the board and remove the OBP device tree
2118 	 * nodes with Solaris device tree nodes remaining.
2119 	 *
2120 	 * On Starfire, Solaris device tree nodes are deleted
2121 	 * during unconfigure by drmach_unconfigure(). It's
2122 	 * necessary to do this here because drmach_unconfigure()
2123 	 * failures are not handled during unconfigure.
2124 	 */
2125 	if (bp->devices) {
2126 		rv = drmach_array_first(bp->devices, &d_idx, &d_id);
2127 		while (rv == 0) {
2128 			err = drmach_unconfigure(d_id, DRMACH_DEVI_REMOVE);
2129 			if (err)
2130 				return (err);
2131 
2132 			rv = drmach_array_next(bp->devices, &d_idx, &d_id);
2133 		}
2134 	}
2135 
2136 	/*
2137 	 * Starfire board Solaris device tree counter nodes,
2138 	 * which are only present on start-of-day boards, are
2139 	 * removed in the dr_post_op() code flow after the
2140 	 * board is unconfigured. We call the counter node
2141 	 * removal function here because unconfigure errors
2142 	 * can cause the dr_post_op() function to be skipped
2143 	 * after an unconfigure operation even though all of
2144 	 * the board's devices have been transitioned to the
2145 	 * unconfigured state.
2146 	 */
2147 	err = drmach_remove_counter_nodes(id);
2148 	if (err)
2149 		return (err);
2150 
2151 	return (NULL);
2152 }
2153 
2154 static int
2155 drmach_board_find_devices_cb(drmach_node_walk_args_t *args)
2156 {
2157 	drmach_node_t			*node = args->node;
2158 	drmach_board_cb_data_t		*data = args->data;
2159 	drmach_board_t			*obj = data->obj;
2160 
2161 	int		 rv;
2162 	int		 bnum;
2163 	drmach_device_t	*device;
2164 
2165 	rv = drmach_node_get_prop(node, OBP_BOARDNUM, &bnum);
2166 	if (rv) {
2167 		/*
2168 		 * if the node does not have a board# property, then
2169 		 * by that information alone it is known that drmach
2170 		 * is not interested in it.
2171 		 */
2172 		return (0);
2173 	} else if (bnum != obj->bnum)
2174 		return (0);
2175 
2176 	/*
2177 	 * Create a device data structure from this node data.
2178 	 * The call may yield nothing if the node is not of interest
2179 	 * to drmach.
2180 	 */
2181 	data->err = drmach_device_new(node, obj, &device);
2182 	if (data->err)
2183 		return (-1);
2184 	else if (device == NULL) {
2185 		/*
2186 		 * drmach_device_new examined the node we passed in
2187 		 * and determined that it was one not of interest to
2188 		 * drmach.  So, it is skipped.
2189 		 */
2190 		return (0);
2191 	}
2192 
2193 	rv = drmach_array_set(obj->devices, data->ndevs++, device);
2194 	if (rv) {
2195 		drmach_device_dispose(device);
2196 		data->err = DRMACH_INTERNAL_ERROR();
2197 		return (-1);
2198 	}
2199 
2200 	data->err = (*data->found)(data->a, device->type, device->unum, device);
2201 	return (data->err == NULL ? 0 : -1);
2202 }
2203 
2204 sbd_error_t *
2205 drmach_board_find_devices(drmachid_t id, void *a,
2206 	sbd_error_t *(*found)(void *a, const char *, int, drmachid_t))
2207 {
2208 	extern int		 plat_max_cpu_units_per_board();
2209 	extern int		 plat_max_mem_units_per_board();
2210 	extern int		 plat_max_io_units_per_board();
2211 
2212 	drmach_board_t		*obj = (drmach_board_t *)id;
2213 	sbd_error_t		*err;
2214 	int			 max_devices;
2215 	int			 rv;
2216 	drmach_board_cb_data_t	data;
2217 
2218 	max_devices  = plat_max_cpu_units_per_board();
2219 	max_devices += plat_max_mem_units_per_board();
2220 	max_devices += plat_max_io_units_per_board();
2221 
2222 	obj->devices = drmach_array_new(0, max_devices);
2223 
2224 	data.obj = obj;
2225 	data.ndevs = 0;
2226 	data.found = found;
2227 	data.a = a;
2228 	data.err = NULL;
2229 
2230 	rv = drmach_node_walk(obj->tree, &data, drmach_board_find_devices_cb);
2231 	if (rv == 0)
2232 		err = NULL;
2233 	else {
2234 		drmach_array_dispose(obj->devices, drmach_device_dispose);
2235 		obj->devices = NULL;
2236 
2237 		if (data.err)
2238 			err = data.err;
2239 		else
2240 			err = DRMACH_INTERNAL_ERROR();
2241 	}
2242 
2243 	return (err);
2244 }
2245 
2246 int
2247 drmach_board_lookup(int bnum, drmachid_t *id)
2248 {
2249 	int	rv = 0;
2250 
2251 	if (!drmach_initialized && drmach_init() == -1) {
2252 		*id = 0;
2253 		rv = -1;
2254 	} else if (drmach_array_get(drmach_boards, bnum, id)) {
2255 		*id = 0;
2256 		rv = -1;
2257 	}
2258 	return (rv);
2259 }
2260 
2261 sbd_error_t *
2262 drmach_board_name(int bnum, char *buf, int buflen)
2263 {
2264 	snprintf(buf, buflen, "SB%d", bnum);
2265 	return (NULL);
2266 }
2267 
2268 sbd_error_t *
2269 drmach_board_poweroff(drmachid_t id)
2270 {
2271 	drmach_board_t	*bp;
2272 	sbd_error_t	*err;
2273 	drmach_status_t	 stat;
2274 
2275 	if (!DRMACH_IS_BOARD_ID(id))
2276 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2277 	bp = id;
2278 
2279 	err = drmach_board_status(id, &stat);
2280 	if (err)
2281 		return (err);
2282 	else if (stat.configured || stat.busy)
2283 		return (drerr_new(0, ESTF_CONFIGBUSY, bp->cm.name));
2284 	else {
2285 		/* board power off is essentially a noop for Starfire */
2286 		bp->powered = 0;
2287 		return (NULL);
2288 	}
2289 	/*NOTREACHED*/
2290 }
2291 
2292 sbd_error_t *
2293 drmach_board_poweron(drmachid_t id)
2294 {
2295 	drmach_board_t	*bp;
2296 
2297 	if (!DRMACH_IS_BOARD_ID(id))
2298 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2299 	bp = id;
2300 
2301 	/* board power on is essentially a noop for Starfire */
2302 	bp->powered = 1;
2303 
2304 	return (NULL);
2305 }
2306 
2307 static sbd_error_t *
2308 drmach_board_release(drmachid_t id)
2309 {
2310 	if (!DRMACH_IS_BOARD_ID(id))
2311 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2312 	return (NULL);
2313 }
2314 
2315 /*ARGSUSED*/
2316 sbd_error_t *
2317 drmach_board_test(drmachid_t id, drmach_opts_t *opts, int force)
2318 {
2319 	return (NULL);
2320 }
2321 
2322 sbd_error_t *
2323 drmach_board_unassign(drmachid_t id)
2324 {
2325 	drmach_board_t	*bp;
2326 	sbd_error_t	*err;
2327 	drmach_status_t	 stat;
2328 
2329 	if (!DRMACH_IS_BOARD_ID(id))
2330 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2331 	bp = id;
2332 
2333 	err = drmach_board_status(id, &stat);
2334 	if (err)
2335 		return (err);
2336 	else if (stat.configured || stat.busy)
2337 		return (drerr_new(0, ESTF_CONFIGBUSY, bp->cm.name));
2338 	else if (drmach_array_set(drmach_boards, bp->bnum, 0) != 0)
2339 		return (DRMACH_INTERNAL_ERROR());
2340 	else {
2341 		drmach_board_dispose(bp);
2342 		return (NULL);
2343 	}
2344 	/*NOTREACHED*/
2345 }
2346 
2347 static sbd_error_t *
2348 drmach_cpu_new(drmach_device_t *dp)
2349 {
2350 	static sbd_error_t *drmach_cpu_release(drmachid_t);
2351 	static sbd_error_t *drmach_cpu_status(drmachid_t, drmach_status_t *);
2352 
2353 	sbd_error_t	*err;
2354 	int		 portid;
2355 
2356 	err = drmach_device_get_prop(dp, "upa-portid", &portid);
2357 	if (err == NULL)
2358 		dp->unum = portid & 3;
2359 
2360 	dp->cm.isa = (void *)drmach_cpu_new;
2361 	dp->cm.release = drmach_cpu_release;
2362 	dp->cm.status = drmach_cpu_status;
2363 
2364 	snprintf(dp->cm.name, sizeof (dp->cm.name), "%s%d", dp->type, dp->unum);
2365 
2366 	return (err);
2367 }
2368 
2369 /*
2370  * drmach_cpu_obp_detach()
2371  *  This requires two steps, first, we must put the cpuid into the OBP
2372  *  idle loop (Idle in Program) state.  Then we call OBP to place the CPU
2373  *  into the "Detached" state, which does any special processing to
2374  *  actually detach the cpu, such as flushing ecache, and also ensures
2375  *  that a subsequent breakpoint won't restart the cpu (if it was just in
2376  *  Idle in Program state).
2377  */
2378 static void
2379 drmach_cpu_obp_detach(int cpuid)
2380 {
2381 	/*
2382 	 * Cpu may not be under OBP's control. Eg, if cpu exited to download
2383 	 * helper on a prior attach.
2384 	 */
2385 	if (CPU_SGN_EXISTS(cpuid) &&
2386 			!SGN_CPU_IS_OS(cpuid) &&
2387 			!SGN_CPU_IS_OBP(cpuid)) {
2388 		cmn_err(CE_WARN,
2389 			"unexpected signature (0x%x) for cpu %d",
2390 			get_cpu_sgn(cpuid), cpuid);
2391 	}
2392 
2393 	/*
2394 	 * Now we place the CPU into the "Detached" idle loop in OBP.
2395 	 * This is so that the CPU won't be restarted if we break into
2396 	 * OBP with a breakpoint or BREAK key from the console, and also
2397 	 * if we need to do any special processing, such as flushing the
2398 	 * cpu's ecache, disabling interrupts (by turning of the ET bit in
2399 	 * the PSR) and/or spinning in BBSRAM rather than global memory.
2400 	 */
2401 	DRMACH_PR("prom_starfire_rm_cpu(%d)\n", cpuid);
2402 	prom_starfire_rm_cpu(cpuid);
2403 }
2404 
2405 /*
2406  * drmach_cpu_obp_is_detached() returns TRUE if the cpu sigblock signature state
2407  * is SIGBST_DETACHED; otherwise it returns FALSE. This routine should only
2408  * be called after we have asked OBP to detach the CPU. It should NOT be
2409  * called as a check during any other flow.
2410  */
2411 static int
2412 drmach_cpu_obp_is_detached(int cpuid)
2413 {
2414 	if (!CPU_SGN_EXISTS(cpuid) ||
2415 		(SGN_CPU_IS_OS(cpuid) && SGN_CPU_STATE_IS_DETACHED(cpuid)))
2416 		return (1);
2417 	else
2418 		return (0);
2419 }
2420 
2421 static int
2422 drmach_cpu_start(struct cpu *cp)
2423 {
2424 	int		cpuid = cp->cpu_id;
2425 	int		ntries = drmach_cpu_ntries;
2426 	extern void	restart_other_cpu(int);
2427 
2428 	ASSERT(MUTEX_HELD(&cpu_lock));
2429 	ASSERT(cpunodes[cpuid].nodeid != (pnode_t)0);
2430 
2431 	cp->cpu_flags &= ~CPU_POWEROFF;
2432 
2433 	/*
2434 	 * NOTE: restart_other_cpu pauses cpus during the
2435 	 *	 slave cpu start.  This helps to quiesce the
2436 	 *	 bus traffic a bit which makes the tick sync
2437 	 *	 routine in the prom more robust.
2438 	 */
2439 	DRMACH_PR("COLD START for cpu (%d)\n", cpuid);
2440 
2441 	prom_starfire_add_cpu(cpuid);
2442 
2443 	restart_other_cpu(cpuid);
2444 
2445 	/*
2446 	 * Wait for the cpu to reach its idle thread before
2447 	 * we zap him with a request to blow away the mappings
2448 	 * he (might) have for the drmach_shutdown_asm code
2449 	 * he may have executed on unconfigure.
2450 	 */
2451 	while ((cp->cpu_thread != cp->cpu_idle_thread) && (ntries > 0)) {
2452 		DELAY(drmach_cpu_delay);
2453 		ntries--;
2454 	}
2455 
2456 	DRMACH_PR("waited %d out of %d loops for cpu %d\n",
2457 		drmach_cpu_ntries - ntries, drmach_cpu_ntries, cpuid);
2458 
2459 	xt_one(cpuid, vtag_flushpage_tl1,
2460 		(uint64_t)drmach_shutdown_va, (uint64_t)ksfmmup);
2461 
2462 	return (0);
2463 }
2464 
2465 /*
2466  * A detaching CPU is xcalled with an xtrap to drmach_cpu_stop_self() after
2467  * it has been offlined. The function of this routine is to get the cpu
2468  * spinning in a safe place. The requirement is that the system will not
2469  * reference anything on the detaching board (memory and i/o is detached
2470  * elsewhere) and that the CPU not reference anything on any other board
2471  * in the system.  This isolation is required during and after the writes
2472  * to the domain masks to remove the board from the domain.
2473  *
2474  * To accomplish this isolation the following is done:
2475  *	1) Create a locked mapping to a location in BBSRAM where
2476  *	   the cpu will execute.
2477  *	2) Copy the target function (drmach_shutdown_asm) in which
2478  *	   the cpu will execute into BBSRAM.
2479  *	3) Jump into function with BBSRAM.
2480  *	   Function will:
2481  *	   3.1) Flush its Ecache (displacement).
2482  *	   3.2) Flush its Dcache with HW mechanism.
2483  *	   3.3) Flush its Icache with HW mechanism.
2484  *	   3.4) Flush all valid and _unlocked_ D-TLB entries.
2485  *	   3.5) Flush all valid and _unlocked_ I-TLB entries.
2486  *	   3.6) Clear xt_mb to signal completion. Note: cache line is
2487  *		recovered by drmach_cpu_poweroff().
2488  *	4) Jump into a tight loop.
2489  */
2490 #define	DRMACH_BBSRAM_OFFSET	0x1000
2491 
2492 static void
2493 drmach_cpu_stop_self(void)
2494 {
2495 	int		cpuid = (int)CPU->cpu_id;
2496 	tte_t		tte;
2497 	volatile uint_t	*src, *dst;
2498 	uint_t		funclen;
2499 	uint64_t	bbsram_pa, bbsram_offset;
2500 	uint_t		bbsram_pfn;
2501 	uint64_t	bbsram_addr;
2502 	void		(*bbsram_func)(uint64_t);
2503 	extern void	drmach_shutdown_asm(uint64_t);
2504 	extern void	drmach_shutdown_asm_end(void);
2505 
2506 	funclen = (uint_t)drmach_shutdown_asm_end - (uint_t)drmach_shutdown_asm;
2507 	ASSERT(funclen <= MMU_PAGESIZE);
2508 	/*
2509 	 * We'll start from the 0th's base.
2510 	 */
2511 	bbsram_pa = STARFIRE_UPAID2UPS(cpuid) | STARFIRE_PSI_BASE;
2512 	bbsram_offset = bbsram_pa | 0xfe0ULL;
2513 	bbsram_pa += ldphysio(bbsram_offset) + DRMACH_BBSRAM_OFFSET;
2514 
2515 	bbsram_pfn = (uint_t)(bbsram_pa >> MMU_PAGESHIFT);
2516 
2517 	bbsram_addr = (uint64_t)drmach_shutdown_va;
2518 	drmach_shutdown_asm_mbox->estack = bbsram_addr + (uint64_t)funclen;
2519 
2520 	tte.tte_inthi = TTE_VALID_INT | TTE_SZ_INT(TTE8K) |
2521 			TTE_PFN_INTHI(bbsram_pfn);
2522 	tte.tte_intlo = TTE_PFN_INTLO(bbsram_pfn) |
2523 			TTE_HWWR_INT | TTE_PRIV_INT | TTE_LCK_INT;
2524 	sfmmu_dtlb_ld_kva(drmach_shutdown_va, &tte);	/* load dtlb */
2525 	sfmmu_itlb_ld_kva(drmach_shutdown_va, &tte);	/* load itlb */
2526 
2527 	for (src = (uint_t *)drmach_shutdown_asm, dst = (uint_t *)bbsram_addr;
2528 		src < (uint_t *)drmach_shutdown_asm_end; src++, dst++)
2529 		*dst = *src;
2530 
2531 	bbsram_func = (void (*)())bbsram_addr;
2532 	drmach_shutdown_asm_mbox->flushaddr = ecache_flushaddr;
2533 	drmach_shutdown_asm_mbox->size = (cpunodes[cpuid].ecache_size << 1);
2534 	drmach_shutdown_asm_mbox->linesize = cpunodes[cpuid].ecache_linesize;
2535 	drmach_shutdown_asm_mbox->physaddr
2536 				    = va_to_pa((void *)&drmach_xt_mb[cpuid]);
2537 
2538 	/*
2539 	 * Signal to drmach_cpu_poweroff() is via drmach_xt_mb cleared
2540 	 * by asm code
2541 	 */
2542 
2543 	(*bbsram_func)(va_to_pa((void *)drmach_shutdown_asm_mbox));
2544 }
2545 
2546 static void
2547 drmach_cpu_shutdown_self(void)
2548 {
2549 	cpu_t		*cp = CPU;
2550 	int		cpuid = cp->cpu_id;
2551 	extern void	flush_windows(void);
2552 
2553 	flush_windows();
2554 
2555 	(void) spl8();
2556 
2557 	ASSERT(cp->cpu_intr_actv == 0);
2558 	ASSERT(cp->cpu_thread == cp->cpu_idle_thread ||
2559 	    cp->cpu_thread == cp->cpu_startup_thread);
2560 
2561 	cp->cpu_flags = CPU_OFFLINE | CPU_QUIESCED | CPU_POWEROFF;
2562 
2563 	drmach_cpu_stop_self();
2564 
2565 	cmn_err(CE_PANIC, "CPU %d FAILED TO SHUTDOWN", cpuid);
2566 }
2567 
2568 /* a helper routine to keep the math in one place */
2569 static processorid_t
2570 drmach_cpu_calc_id(drmach_device_t *dp)
2571 {
2572 	return (dp->bp->bnum * MAX_CPU_UNITS_PER_BOARD + dp->unum);
2573 }
2574 
2575 /*
2576  * Move bootproc (SIGBCPU) to another cpu.  If dst_cpu is NULL, a
2577  * destination cpu is chosen from the set of cpus not located on the
2578  * same board as the current bootproc cpu.
2579  */
2580 static sbd_error_t *
2581 drmach_cpu_juggle_bootproc(drmach_device_t *dst_cpu)
2582 {
2583 	processorid_t	 cpuid;
2584 	struct cpu	*cp;
2585 	sbd_error_t	*err;
2586 	int		 rv;
2587 
2588 	ASSERT(MUTEX_HELD(&cpu_lock));
2589 
2590 	/* dst_cpu is NULL when target cpu is unspecified. So, pick one. */
2591 	if (dst_cpu == NULL) {
2592 		int avoid_board = DRMACH_CPUID2BNUM(SIGBCPU->cpu_id);
2593 		int max_cpuid = MAX_BOARDS * MAX_CPU_UNITS_PER_BOARD;
2594 
2595 		for (cpuid = 0; cpuid < max_cpuid; cpuid++)
2596 			if (DRMACH_CPUID2BNUM(cpuid) != avoid_board) {
2597 				cp = cpu_get(cpuid);
2598 				if (cp != NULL && cpu_is_online(cp))
2599 					break;
2600 			}
2601 
2602 		if (cpuid == max_cpuid) {
2603 			err = drerr_new(1, ESTF_JUGGLE, NULL);
2604 			return (err);
2605 		}
2606 
2607 		/* else, cp points to the selected target cpu */
2608 	} else {
2609 		cpuid = drmach_cpu_calc_id(dst_cpu);
2610 
2611 		if ((cp = cpu_get(cpuid)) == NULL) {
2612 			err = drerr_new(1, ESTF_NODEV, "%s::%s",
2613 				dst_cpu->bp->cm.name, dst_cpu->cm.name);
2614 			return (err);
2615 		}
2616 
2617 		if (cpuid == SIGBCPU->cpu_id) {
2618 			cmn_err(CE_WARN,
2619 				"SIGBCPU(%d) same as new selection(%d)",
2620 				SIGBCPU->cpu_id, cpuid);
2621 
2622 			/* technically not an error, but a no-op */
2623 			return (NULL);
2624 		}
2625 	}
2626 
2627 	cmn_err(CE_NOTE, "?relocating SIGBCPU from %d to %d",
2628 		SIGBCPU->cpu_id, cpuid);
2629 
2630 	DRMACH_PR("moving SIGBCPU to CPU %d\n", cpuid);
2631 
2632 	/*
2633 	 * Tell OBP to initialize cvc-offset field of new CPU0
2634 	 * so that it's in sync with OBP and cvc_server
2635 	 */
2636 	prom_starfire_init_console(cpuid);
2637 
2638 	/*
2639 	 * Assign cvc to new cpu0's bbsram for I/O.  This has to be
2640 	 * done BEFORE cpu0 is moved via obp, since this logic
2641 	 * will cause obp_helper to switch to a different bbsram for
2642 	 * cvc I/O.  We don't want cvc writing to a buffer from which
2643 	 * nobody will pick up the data!
2644 	 */
2645 	cvc_assign_iocpu(cpuid);
2646 
2647 	rv = prom_starfire_move_cpu0(cpuid);
2648 
2649 	if (rv == 0) {
2650 		SIGBCPU = cp;
2651 
2652 		DRMACH_PR("successfully juggled to CPU %d\n", cpuid);
2653 		return (NULL);
2654 	} else {
2655 		DRMACH_PR("prom error: prom_starfire_move_cpu0(%d) "
2656 			"returned %d\n", cpuid, rv);
2657 
2658 		/*
2659 		 * The move failed, hopefully obp_helper is still back
2660 		 * at the old bootproc.  Move cvc back there.
2661 		 */
2662 		cvc_assign_iocpu(SIGBCPU->cpu_id);
2663 
2664 
2665 		err = drerr_new(1, ESTF_MOVESIGB, "CPU %d", cpuid);
2666 		return (err);
2667 	}
2668 	/*NOTREACHED*/
2669 }
2670 
2671 static sbd_error_t *
2672 drmach_cpu_release(drmachid_t id)
2673 {
2674 	drmach_device_t	*dp;
2675 	processorid_t	 cpuid;
2676 	struct cpu	*cp;
2677 	sbd_error_t	*err;
2678 
2679 	if (!DRMACH_IS_CPU_ID(id))
2680 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2681 	dp = id;
2682 	cpuid = drmach_cpu_calc_id(dp);
2683 
2684 	ASSERT(MUTEX_HELD(&cpu_lock));
2685 
2686 	cp = cpu_get(cpuid);
2687 	if (cp == NULL)
2688 		err = DRMACH_INTERNAL_ERROR();
2689 	else if (SIGBCPU->cpu_id == cp->cpu_id)
2690 		err = drmach_cpu_juggle_bootproc(NULL);
2691 	else
2692 		err = NULL;
2693 
2694 	return (err);
2695 }
2696 
2697 static sbd_error_t *
2698 drmach_cpu_status(drmachid_t id, drmach_status_t *stat)
2699 {
2700 	drmach_device_t *dp;
2701 
2702 	ASSERT(DRMACH_IS_CPU_ID(id));
2703 	dp = id;
2704 
2705 	stat->assigned = dp->bp->assigned;
2706 	stat->powered = dp->bp->powered;
2707 	mutex_enter(&cpu_lock);
2708 	stat->configured = (cpu_get(drmach_cpu_calc_id(dp)) != NULL);
2709 	mutex_exit(&cpu_lock);
2710 	stat->busy = dp->busy;
2711 	strncpy(stat->type, dp->type, sizeof (stat->type));
2712 	stat->info[0] = '\0';
2713 
2714 	return (NULL);
2715 }
2716 
2717 sbd_error_t *
2718 drmach_cpu_disconnect(drmachid_t id)
2719 {
2720 	drmach_device_t	*cpu;
2721 	int		 cpuid;
2722 	int		 ntries;
2723 	int		 p;
2724 	u_longlong_t	 pc_addr;
2725 	uchar_t		 rvalue;
2726 
2727 	if (!DRMACH_IS_CPU_ID(id))
2728 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2729 	cpu = id;
2730 
2731 	cpuid = drmach_cpu_calc_id(cpu);
2732 	if (SIGBCPU->cpu_id == cpuid) {
2733 		/* this cpu is SIGBCPU, can't disconnect */
2734 		return (drerr_new(1, ESTF_HASSIGB, "%s::%s",
2735 				cpu->bp->cm.name, cpu->cm.name));
2736 	}
2737 
2738 	/*
2739 	 * Make sure SIGBST_DETACHED is set before
2740 	 * mapping out the sig block.
2741 	 */
2742 	ntries = drmach_cpu_ntries;
2743 	while (!drmach_cpu_obp_is_detached(cpuid) && ntries) {
2744 		DELAY(drmach_cpu_delay);
2745 		ntries--;
2746 	}
2747 	if (!drmach_cpu_obp_is_detached(cpuid)) {
2748 		cmn_err(CE_WARN, "failed to mark cpu %d detached in sigblock",
2749 			cpuid);
2750 	}
2751 
2752 	/* map out signature block */
2753 	if (CPU_SGN_EXISTS(cpuid)) {
2754 		CPU_SGN_MAPOUT(cpuid);
2755 	}
2756 
2757 	/*
2758 	 * We now PC IDLE the processor to guarantee we
2759 	 * stop any transactions from coming from it.
2760 	 */
2761 	p = cpu->unum & 1;
2762 	pc_addr = STARFIRE_BB_PC_ADDR(cpu->bp->bnum, cpu->unum, 0);
2763 
2764 	DRMACH_PR("PC idle cpu %d (addr = 0x%llx, port = %d, p = %d)",
2765 		drmach_cpu_calc_id(cpu), pc_addr, cpu->unum, p);
2766 
2767 	rvalue = ldbphysio(pc_addr);
2768 	rvalue |= STARFIRE_BB_PC_IDLE(p);
2769 	stbphysio(pc_addr, rvalue);
2770 	DELAY(50000);
2771 
2772 	return (NULL);
2773 }
2774 
2775 sbd_error_t *
2776 drmach_cpu_get_id(drmachid_t id, processorid_t *cpuid)
2777 {
2778 	drmach_device_t *cpu;
2779 
2780 	if (!DRMACH_IS_CPU_ID(id))
2781 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2782 	cpu = id;
2783 
2784 	*cpuid = drmach_cpu_calc_id(cpu);
2785 	return (NULL);
2786 }
2787 
2788 sbd_error_t *
2789 drmach_cpu_get_impl(drmachid_t id, int *ip)
2790 {
2791 	drmach_device_t *cpu;
2792 	int		impl;
2793 
2794 	if (!DRMACH_IS_CPU_ID(id))
2795 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2796 
2797 	cpu = id;
2798 
2799 	if (drmach_node_get_prop(cpu->node, "implementation#", &impl) == -1) {
2800 		return (DRMACH_INTERNAL_ERROR());
2801 	}
2802 
2803 	*ip = impl;
2804 
2805 	return (NULL);
2806 }
2807 
2808 void
2809 drmach_cpu_flush_ecache_sync(void)
2810 {
2811 	ASSERT(curthread->t_bound_cpu == CPU);
2812 
2813 	/*
2814 	 * Now let's flush our ecache thereby removing all references
2815 	 * to the target (detaching) memory from all ecache's in
2816 	 * system.
2817 	 */
2818 	cpu_flush_ecache();
2819 
2820 	/*
2821 	 * Delay 100 usec out of paranoia to insure everything
2822 	 * (hardware queues) has drained before we start reprogramming
2823 	 * the hardware.
2824 	 */
2825 	DELAY(100);
2826 }
2827 
2828 sbd_error_t *
2829 drmach_get_dip(drmachid_t id, dev_info_t **dip)
2830 {
2831 	drmach_device_t	*dp;
2832 
2833 	if (!DRMACH_IS_DEVICE_ID(id))
2834 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2835 	dp = id;
2836 
2837 	*dip = drmach_node_get_dip(dp->node);
2838 	return (NULL);
2839 }
2840 
2841 sbd_error_t *
2842 drmach_io_is_attached(drmachid_t id, int *yes)
2843 {
2844 	drmach_device_t *dp;
2845 	dev_info_t	*dip;
2846 	int		state;
2847 
2848 	if (!DRMACH_IS_IO_ID(id))
2849 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2850 	dp = id;
2851 
2852 	dip = drmach_node_get_dip(dp->node);
2853 	if (dip == NULL) {
2854 		*yes = 0;
2855 		return (NULL);
2856 	}
2857 
2858 	state = ddi_get_devstate(dip);
2859 	*yes = (i_ddi_devi_attached(dip) || (state == DDI_DEVSTATE_UP));
2860 
2861 	return (NULL);
2862 }
2863 
2864 sbd_error_t *
2865 drmach_io_pre_release(drmachid_t id)
2866 {
2867 	if (!DRMACH_IS_IO_ID(id))
2868 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2869 	return (NULL);
2870 }
2871 
2872 static sbd_error_t *
2873 drmach_io_release(drmachid_t id)
2874 {
2875 	if (!DRMACH_IS_IO_ID(id))
2876 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2877 	return (NULL);
2878 }
2879 
2880 sbd_error_t *
2881 drmach_io_unrelease(drmachid_t id)
2882 {
2883 	if (!DRMACH_IS_IO_ID(id))
2884 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2885 	return (NULL);
2886 }
2887 
2888 /*ARGSUSED*/
2889 sbd_error_t *
2890 drmach_io_post_release(drmachid_t id)
2891 {
2892 	return (NULL);
2893 }
2894 
2895 /*ARGSUSED*/
2896 sbd_error_t *
2897 drmach_io_post_attach(drmachid_t id)
2898 {
2899 	return (NULL);
2900 }
2901 
2902 static sbd_error_t *
2903 drmach_io_status(drmachid_t id, drmach_status_t *stat)
2904 {
2905 	drmach_device_t *dp;
2906 	sbd_error_t	*err;
2907 	int		 configured;
2908 
2909 	ASSERT(DRMACH_IS_IO_ID(id));
2910 	dp = id;
2911 
2912 	err = drmach_io_is_attached(id, &configured);
2913 	if (err)
2914 		return (err);
2915 
2916 	stat->assigned = dp->bp->assigned;
2917 	stat->powered = dp->bp->powered;
2918 	stat->configured = (configured != 0);
2919 	stat->busy = dp->busy;
2920 	strncpy(stat->type, dp->type, sizeof (stat->type));
2921 	stat->info[0] = '\0';
2922 
2923 	return (NULL);
2924 }
2925 
2926 static sbd_error_t *
2927 drmach_mem_new(drmach_device_t *dp)
2928 {
2929 	static sbd_error_t *drmach_mem_release(drmachid_t);
2930 	static sbd_error_t *drmach_mem_status(drmachid_t, drmach_status_t *);
2931 
2932 	dp->unum = 0;
2933 	dp->cm.isa = (void *)drmach_mem_new;
2934 	dp->cm.release = drmach_mem_release;
2935 	dp->cm.status = drmach_mem_status;
2936 
2937 	snprintf(dp->cm.name, sizeof (dp->cm.name), "%s", dp->type);
2938 
2939 	return (NULL);
2940 }
2941 
2942 sbd_error_t *
2943 drmach_mem_add_span(drmachid_t id, uint64_t basepa, uint64_t size)
2944 {
2945 	pfn_t		basepfn = (pfn_t)(basepa >> PAGESHIFT);
2946 	pgcnt_t		npages = (pgcnt_t)(size >> PAGESHIFT);
2947 	pda_handle_t	ph;
2948 	int		rv;
2949 
2950 	ASSERT(size != 0);
2951 
2952 	if (!DRMACH_IS_MEM_ID(id))
2953 		return (drerr_new(0, ESTF_INAPPROP, NULL));
2954 
2955 	rv = kcage_range_add(basepfn, npages, KCAGE_DOWN);
2956 	if (rv == ENOMEM) {
2957 		cmn_err(CE_WARN, "%ld megabytes not available to kernel cage",
2958 			(size == 0 ? 0 : size / MBYTE));
2959 	} else if (rv != 0) {
2960 		/* catch this in debug kernels */
2961 		ASSERT(0);
2962 
2963 		cmn_err(CE_WARN, "unexpected kcage_range_add"
2964 			" return value %d", rv);
2965 	}
2966 
2967 	/*
2968 	 * Update the PDA (post2obp) structure with the
2969 	 * range of the newly added memory.
2970 	 */
2971 	ph = drmach_pda_open();
2972 	if (ph != NULL) {
2973 		pda_mem_add_span(ph, basepa, size);
2974 		pda_close(ph);
2975 	}
2976 
2977 	return (NULL);
2978 }
2979 
2980 sbd_error_t *
2981 drmach_mem_del_span(drmachid_t id, uint64_t basepa, uint64_t size)
2982 {
2983 	drmach_device_t	*mem = id;
2984 	pfn_t		basepfn = (pfn_t)(basepa >> PAGESHIFT);
2985 	pgcnt_t		npages = (pgcnt_t)(size >> PAGESHIFT);
2986 	uint_t		mcreg;
2987 	sbd_error_t	*err;
2988 	pda_handle_t	ph;
2989 	int		rv;
2990 
2991 	err = drmach_read_mc_asr(id, &mcreg);
2992 	if (err)
2993 		return (err);
2994 	else if (mcreg & STARFIRE_MC_INTERLEAVE_MASK) {
2995 		return (drerr_new(1, ESTF_INTERBOARD, "%s::%s",
2996 				mem->bp->cm.name, mem->cm.name));
2997 	}
2998 
2999 	if (size > 0) {
3000 		rv = kcage_range_delete_post_mem_del(basepfn, npages);
3001 		if (rv != 0) {
3002 			cmn_err(CE_WARN,
3003 			    "unexpected kcage_range_delete_post_mem_del"
3004 			    " return value %d", rv);
3005 			return (DRMACH_INTERNAL_ERROR());
3006 		}
3007 	}
3008 
3009 	/*
3010 	 * Update the PDA (post2obp) structure with the
3011 	 * range of removed memory.
3012 	 */
3013 	ph = drmach_pda_open();
3014 	if (ph != NULL) {
3015 		if (size > 0)
3016 			pda_mem_del_span(ph, basepa, size);
3017 
3018 		/* update PDA to board's new mc register settings */
3019 		pda_mem_sync(ph, mem->bp->bnum, 0);
3020 
3021 		pda_close(ph);
3022 	}
3023 
3024 	return (NULL);
3025 }
3026 
3027 /* support routine for enable and disable */
3028 static sbd_error_t *
3029 drmach_mem_update_interconnect(drmachid_t id, uint_t mcreg)
3030 {
3031 	drmach_device_t	*dp;
3032 	pda_handle_t	 ph;
3033 	int		 b;
3034 
3035 	if (!DRMACH_IS_MEM_ID(id))
3036 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3037 	dp = id;
3038 
3039 	ph = drmach_pda_open();
3040 	if (ph == NULL)
3041 		return (DRMACH_INTERNAL_ERROR());
3042 
3043 	for (b = 0; b < MAX_BOARDS; b++) {
3044 		int		p;
3045 		int		rv;
3046 		ushort_t	bda_proc, bda_ioc;
3047 		board_desc_t	*bdesc;
3048 
3049 		if (pda_board_present(ph, b) == 0)
3050 			continue;
3051 
3052 		bdesc = (board_desc_t *)pda_get_board_info(ph, b);
3053 
3054 		/*
3055 		 * Update PCs for CPUs.
3056 		 */
3057 
3058 		/* make sure definition in platmod is in sync with pda */
3059 		ASSERT(MAX_PROCMODS == MAX_CPU_UNITS_PER_BOARD);
3060 
3061 		bda_proc = bdesc->bda_proc;
3062 		for (p = 0; p < MAX_PROCMODS; p++) {
3063 			if (BDA_NBL(bda_proc, p) != BDAN_GOOD)
3064 				continue;
3065 
3066 			rv = pc_madr_add(b, dp->bp->bnum, p, mcreg);
3067 			if (rv) {
3068 				pda_close(ph);
3069 				return (DRMACH_INTERNAL_ERROR());
3070 			}
3071 		}
3072 
3073 		/*
3074 		 * Update PCs for IOCs.
3075 		 */
3076 
3077 		/* make sure definition in platmod is in sync with pda */
3078 		ASSERT(MAX_IOCS == MAX_IO_UNITS_PER_BOARD);
3079 
3080 		bda_ioc = bdesc->bda_ioc;
3081 		for (p = 0; p < MAX_IOCS; p++) {
3082 			if (BDA_NBL(bda_ioc, p) != BDAN_GOOD)
3083 				continue;
3084 
3085 			rv = pc_madr_add(b, dp->bp->bnum, p + 4, mcreg);
3086 			if (rv) {
3087 				pda_close(ph);
3088 				return (DRMACH_INTERNAL_ERROR());
3089 			}
3090 		}
3091 	}
3092 
3093 	pda_close(ph);
3094 	return (NULL);
3095 }
3096 
3097 sbd_error_t *
3098 drmach_mem_disable(drmachid_t id)
3099 {
3100 	sbd_error_t	*err;
3101 	uint_t		 mcreg;
3102 
3103 	err = drmach_read_mc_asr(id, &mcreg);
3104 	if (err == NULL) {
3105 		ASSERT(mcreg & STARFIRE_MC_MEM_PRESENT_MASK);
3106 
3107 		/* Turn off presence bit. */
3108 		mcreg &= ~STARFIRE_MC_MEM_PRESENT_MASK;
3109 
3110 		err = drmach_mem_update_interconnect(id, mcreg);
3111 		if (err == NULL)
3112 			err = drmach_write_mc_asr(id, mcreg);
3113 	}
3114 
3115 	return (err);
3116 }
3117 
3118 sbd_error_t *
3119 drmach_mem_enable(drmachid_t id)
3120 {
3121 	sbd_error_t	*err;
3122 	uint_t		 mcreg;
3123 
3124 	err = drmach_read_mc_asr(id, &mcreg);
3125 	if (err == NULL) {
3126 		mcreg |= STARFIRE_MC_MEM_PRESENT_MASK;
3127 
3128 		err = drmach_write_mc_asr(id, mcreg);
3129 		if (err == NULL)
3130 			err = drmach_mem_update_interconnect(id, mcreg);
3131 	}
3132 
3133 	return (err);
3134 }
3135 
3136 sbd_error_t *
3137 drmach_mem_get_alignment(drmachid_t id, uint64_t *mask)
3138 {
3139 	drmach_device_t	*mem;
3140 	sbd_error_t	*err;
3141 	pnode_t		 nodeid;
3142 
3143 	if (!DRMACH_IS_MEM_ID(id))
3144 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3145 	mem = id;
3146 
3147 	nodeid = drmach_node_get_dnode(mem->node);
3148 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE)
3149 		err = DRMACH_INTERNAL_ERROR();
3150 	else {
3151 		uint64_t size;
3152 
3153 		size = mc_get_alignment_mask(nodeid);
3154 		if (size == (uint64_t)-1)
3155 			err = DRMACH_INTERNAL_ERROR();
3156 		else {
3157 			*mask = size - 1;
3158 			err = NULL;
3159 		}
3160 	}
3161 
3162 	return (err);
3163 }
3164 
3165 sbd_error_t *
3166 drmach_mem_get_base_physaddr(drmachid_t id, uint64_t *pa)
3167 {
3168 	sbd_error_t	*err;
3169 	uint_t		 mcreg;
3170 
3171 	err = drmach_read_mc_asr(id, &mcreg);
3172 	if (err == NULL)
3173 		*pa = mc_asr_to_pa(mcreg);
3174 
3175 	return (err);
3176 }
3177 
3178 /*
3179  * Use of this routine after copy/rename will yield incorrect results,
3180  * because the OBP MEMAVAIL property will not correctly reflect the
3181  * programming of the MCs.
3182  */
3183 sbd_error_t *
3184 drmach_mem_get_memlist(drmachid_t id, struct memlist **ml)
3185 {
3186 	drmach_device_t	*mem;
3187 	int		rv, i, rlen, rblks;
3188 	sbd_error_t	*err;
3189 	struct memlist	*mlist;
3190 	struct sf_memunit_regspec *rlist;
3191 
3192 	if (!DRMACH_IS_MEM_ID(id))
3193 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3194 	mem = id;
3195 
3196 	err = drmach_device_get_proplen(mem, "dr-available", &rlen);
3197 	if (err)
3198 		return (err);
3199 
3200 	rlist = kmem_zalloc(rlen, KM_SLEEP);
3201 
3202 	err = drmach_device_get_prop(mem, "dr-available", rlist);
3203 	if (err) {
3204 		kmem_free(rlist, rlen);
3205 		return (err);
3206 	}
3207 
3208 	mlist = NULL;
3209 	rblks = rlen / sizeof (struct sf_memunit_regspec);
3210 	for (i = 0; i < rblks; i++) {
3211 		uint64_t	addr, size;
3212 
3213 		addr  = (uint64_t)rlist[i].regspec_addr_hi << 32;
3214 		addr |= (uint64_t)rlist[i].regspec_addr_lo;
3215 		size  = (uint64_t)rlist[i].regspec_size_hi << 32;
3216 		size |= (uint64_t)rlist[i].regspec_size_lo;
3217 
3218 		mlist = memlist_add_span(mlist, addr, size);
3219 	}
3220 
3221 	kmem_free(rlist, rlen);
3222 
3223 	/*
3224 	 * Make sure the incoming memlist doesn't already
3225 	 * intersect with what's present in the system (phys_install).
3226 	 */
3227 	memlist_read_lock();
3228 	rv = memlist_intersect(phys_install, mlist);
3229 	memlist_read_unlock();
3230 	if (rv) {
3231 #ifdef DEBUG
3232 		DRMACH_PR("OBP derived memlist intersects"
3233 			" with phys_install\n");
3234 		memlist_dump(mlist);
3235 
3236 		DRMACH_PR("phys_install memlist:\n");
3237 		memlist_dump(phys_install);
3238 #endif
3239 
3240 		memlist_delete(mlist);
3241 		return (DRMACH_INTERNAL_ERROR());
3242 	}
3243 
3244 #ifdef DEBUG
3245 	DRMACH_PR("OBP derived memlist:");
3246 	memlist_dump(mlist);
3247 #endif
3248 
3249 	*ml = mlist;
3250 	return (NULL);
3251 }
3252 
3253 sbd_error_t *
3254 drmach_mem_get_size(drmachid_t id, uint64_t *bytes)
3255 {
3256 	drmach_device_t	*mem;
3257 	pda_handle_t	ph;
3258 	pgcnt_t		npages;
3259 
3260 	if (!DRMACH_IS_MEM_ID(id))
3261 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3262 	mem = id;
3263 
3264 	ph = drmach_pda_open();
3265 	if (ph == NULL)
3266 		return (DRMACH_INTERNAL_ERROR());
3267 
3268 	npages = pda_get_mem_size(ph, mem->bp->bnum);
3269 	*bytes = (uint64_t)npages << PAGESHIFT;
3270 
3271 	pda_close(ph);
3272 	return (NULL);
3273 }
3274 
3275 sbd_error_t *
3276 drmach_mem_get_slice_size(drmachid_t id, uint64_t *bytes)
3277 {
3278 	if (!DRMACH_IS_MEM_ID(id))
3279 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3280 
3281 	*bytes = mc_get_mem_alignment();
3282 	return (NULL);
3283 }
3284 
3285 /* field debugging tool */
3286 processorid_t drmach_mem_cpu_affinity_nail = 0;
3287 
3288 processorid_t
3289 drmach_mem_cpu_affinity(drmachid_t id)
3290 {
3291 	drmach_device_t	*mp;
3292 	drmach_board_t	*bp;
3293 	processorid_t	 cpuid;
3294 
3295 	if (!DRMACH_IS_MEM_ID(id))
3296 		return (CPU_CURRENT);
3297 
3298 	if (drmach_mem_cpu_affinity_nail) {
3299 		cpuid = drmach_mem_cpu_affinity_nail;
3300 
3301 		if (cpuid < 0 || cpuid > NCPU)
3302 			return (CPU_CURRENT);
3303 
3304 		mutex_enter(&cpu_lock);
3305 		if (cpu[cpuid] == NULL || !CPU_ACTIVE(cpu[cpuid]))
3306 			cpuid = CPU_CURRENT;
3307 		mutex_exit(&cpu_lock);
3308 
3309 		return (cpuid);
3310 	}
3311 
3312 	/* try to choose a proc on the target board */
3313 	mp = id;
3314 	bp = mp->bp;
3315 	if (bp->devices) {
3316 		int		rv;
3317 		int		d_idx;
3318 		drmachid_t	d_id;
3319 
3320 		rv = drmach_array_first(bp->devices, &d_idx, &d_id);
3321 		while (rv == 0) {
3322 			if (DRMACH_IS_CPU_ID(d_id)) {
3323 				cpuid = drmach_cpu_calc_id(d_id);
3324 
3325 				mutex_enter(&cpu_lock);
3326 				if (cpu[cpuid] && CPU_ACTIVE(cpu[cpuid])) {
3327 					mutex_exit(&cpu_lock);
3328 					DRMACH_PR("drmach_mem_cpu_affinity: "
3329 					    "selected cpuid=%d\n", cpuid);
3330 					return (cpuid);
3331 				} else {
3332 					mutex_exit(&cpu_lock);
3333 				}
3334 			}
3335 
3336 			rv = drmach_array_next(bp->devices, &d_idx, &d_id);
3337 		}
3338 	}
3339 
3340 	/* otherwise, this proc, wherever it is */
3341 	DRMACH_PR("drmach_mem_cpu_affinity: using default CPU_CURRENT\n");
3342 
3343 	return (CPU_CURRENT);
3344 }
3345 
3346 static sbd_error_t *
3347 drmach_mem_release(drmachid_t id)
3348 {
3349 	if (!DRMACH_IS_MEM_ID(id))
3350 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3351 	return (NULL);
3352 }
3353 
3354 static sbd_error_t *
3355 drmach_mem_status(drmachid_t id, drmach_status_t *stat)
3356 {
3357 	drmach_device_t *dp;
3358 	sbd_error_t	*err;
3359 	uint64_t	 pa, slice_size;
3360 	struct memlist	*ml;
3361 
3362 	ASSERT(DRMACH_IS_MEM_ID(id));
3363 	dp = id;
3364 
3365 	/* get starting physical address of target memory */
3366 	err = drmach_mem_get_base_physaddr(id, &pa);
3367 	if (err)
3368 		return (err);
3369 
3370 	/* round down to slice boundary */
3371 	slice_size = mc_get_mem_alignment();
3372 	pa &= ~ (slice_size - 1);
3373 
3374 	/* stop at first span that is in slice */
3375 	memlist_read_lock();
3376 	for (ml = phys_install; ml; ml = ml->next)
3377 		if (ml->address >= pa && ml->address < pa + slice_size)
3378 			break;
3379 	memlist_read_unlock();
3380 
3381 	stat->assigned = dp->bp->assigned;
3382 	stat->powered = dp->bp->powered;
3383 	stat->configured = (ml != NULL);
3384 	stat->busy = dp->busy;
3385 	strncpy(stat->type, dp->type, sizeof (stat->type));
3386 	stat->info[0] = '\0';
3387 
3388 	return (NULL);
3389 }
3390 
3391 static int
3392 drmach_detach_board(void *arg)
3393 {
3394 	cpuset_t	cset;
3395 	int		retval;
3396 	drmach_board_t	*bp = (drmach_board_t *)arg;
3397 
3398 	cset = cpu_ready_set;
3399 	promsafe_xc_attention(cset);
3400 
3401 	retval = prom_starfire_rm_brd(bp->bnum);
3402 
3403 	xc_dismissed(cset);
3404 
3405 	return (retval);
3406 }
3407 
3408 sbd_error_t *
3409 drmach_board_deprobe(drmachid_t id)
3410 {
3411 	drmach_board_t	*bp;
3412 	int		 retval;
3413 
3414 	if (!DRMACH_IS_BOARD_ID(id))
3415 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3416 	bp = id;
3417 
3418 	cmn_err(CE_CONT, "DR: PROM detach board %d\n", bp->bnum);
3419 
3420 	retval = prom_tree_update(drmach_detach_board, bp);
3421 
3422 	if (retval == 0)
3423 		return (NULL);
3424 	else {
3425 		cmn_err(CE_WARN, "prom error: prom_starfire_rm_brd(%d) "
3426 			"returned %d", bp->bnum, retval);
3427 		return (drerr_new(1, ESTF_DEPROBE, "%s", bp->cm.name));
3428 	}
3429 }
3430 
3431 /*ARGSUSED*/
3432 static sbd_error_t *
3433 drmach_pt_juggle_bootproc(drmachid_t id, drmach_opts_t *opts)
3434 {
3435 	drmach_device_t	*cpu;
3436 	sbd_error_t	*err;
3437 
3438 	if (!DRMACH_IS_CPU_ID(id))
3439 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3440 	cpu = id;
3441 
3442 	mutex_enter(&cpu_lock);
3443 
3444 	err = drmach_cpu_juggle_bootproc(cpu);
3445 
3446 	mutex_exit(&cpu_lock);
3447 
3448 	return (err);
3449 }
3450 
3451 /*ARGSUSED*/
3452 static sbd_error_t *
3453 drmach_pt_dump_pdainfo(drmachid_t id, drmach_opts_t *opts)
3454 {
3455 	drmach_board_t	*bp;
3456 	int		board;
3457 	int		i;
3458 	pda_handle_t	ph;
3459 	board_desc_t	*bdesc;
3460 
3461 	if (!DRMACH_IS_BOARD_ID(id))
3462 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3463 	bp = id;
3464 	board = bp->bnum;
3465 
3466 	ph = drmach_pda_open();
3467 	if (ph == NULL)
3468 		return (DRMACH_INTERNAL_ERROR());
3469 
3470 	if (pda_board_present(ph, board) == 0) {
3471 		cmn_err(CE_CONT, "board %d is MISSING\n", board);
3472 		pda_close(ph);
3473 		return (DRMACH_INTERNAL_ERROR());
3474 	}
3475 
3476 	cmn_err(CE_CONT, "board %d is PRESENT\n", board);
3477 
3478 	bdesc = (board_desc_t *)pda_get_board_info(ph, board);
3479 	if (bdesc == NULL) {
3480 		cmn_err(CE_CONT,
3481 			"no board descriptor found for board %d\n",
3482 			board);
3483 		pda_close(ph);
3484 		return (DRMACH_INTERNAL_ERROR());
3485 	}
3486 
3487 	/* make sure definition in platmod is in sync with pda */
3488 	ASSERT(MAX_PROCMODS == MAX_CPU_UNITS_PER_BOARD);
3489 
3490 	for (i = 0; i < MAX_PROCMODS; i++) {
3491 		if (BDA_NBL(bdesc->bda_proc, i) == BDAN_GOOD)
3492 			cmn_err(CE_CONT,
3493 				"proc %d.%d PRESENT\n", board, i);
3494 		else
3495 			cmn_err(CE_CONT,
3496 				"proc %d.%d MISSING\n", board, i);
3497 	}
3498 
3499 	for (i = 0; i < MAX_MGROUPS; i++) {
3500 		if (BDA_NBL(bdesc->bda_mgroup, i) == BDAN_GOOD)
3501 			cmn_err(CE_CONT,
3502 				"mgroup %d.%d PRESENT\n", board, i);
3503 		else
3504 			cmn_err(CE_CONT,
3505 				"mgroup %d.%d MISSING\n", board, i);
3506 	}
3507 
3508 	/* make sure definition in platmod is in sync with pda */
3509 	ASSERT(MAX_IOCS == MAX_IO_UNITS_PER_BOARD);
3510 
3511 	for (i = 0; i < MAX_IOCS; i++) {
3512 		int	s;
3513 
3514 		if (BDA_NBL(bdesc->bda_ioc, i) == BDAN_GOOD) {
3515 			cmn_err(CE_CONT,
3516 				"ioc %d.%d PRESENT\n", board, i);
3517 			for (s = 0; s < MAX_SLOTS_PER_IOC; s++) {
3518 				if (BDA_NBL(bdesc->bda_ios[i], s) != BDAN_GOOD)
3519 					continue;
3520 				cmn_err(CE_CONT,
3521 					"..scard %d.%d.%d PRESENT\n",
3522 					board, i, s);
3523 			}
3524 		} else {
3525 			cmn_err(CE_CONT,
3526 				"ioc %d.%d MISSING\n",
3527 				board, i);
3528 		}
3529 	}
3530 
3531 	cmn_err(CE_CONT,
3532 		"board %d memsize = %d pages\n",
3533 		board, pda_get_mem_size(ph, board));
3534 
3535 	pda_close(ph);
3536 
3537 	return (NULL);
3538 }
3539 
3540 /*ARGSUSED*/
3541 sbd_error_t *
3542 drmach_pt_readmem(drmachid_t id, drmach_opts_t *opts)
3543 {
3544 	struct memlist	*ml;
3545 	uint64_t	src_pa;
3546 	uint64_t	dst_pa;
3547 	uint64_t	dst;
3548 
3549 	dst_pa = va_to_pa(&dst);
3550 
3551 	memlist_read_lock();
3552 	for (ml = phys_install; ml; ml = ml->next) {
3553 		uint64_t	nbytes;
3554 
3555 		src_pa = ml->address;
3556 		nbytes = ml->size;
3557 
3558 		while (nbytes != 0ull) {
3559 
3560 			/* copy 32 bytes at arc_pa to dst_pa */
3561 			bcopy32_il(src_pa, dst_pa);
3562 
3563 			/* increment by 32 bytes */
3564 			src_pa += (4 * sizeof (uint64_t));
3565 
3566 			/* decrement by 32 bytes */
3567 			nbytes -= (4 * sizeof (uint64_t));
3568 		}
3569 	}
3570 	memlist_read_unlock();
3571 
3572 	return (NULL);
3573 }
3574 
3575 static struct {
3576 	const char	*name;
3577 	sbd_error_t	*(*handler)(drmachid_t id, drmach_opts_t *opts);
3578 } drmach_pt_arr[] = {
3579 	{ "juggle",		drmach_pt_juggle_bootproc	},
3580 	{ "pda",		drmach_pt_dump_pdainfo		},
3581 	{ "readmem",		drmach_pt_readmem		},
3582 
3583 	/* the following line must always be last */
3584 	{ NULL,			NULL				}
3585 };
3586 
3587 /*ARGSUSED*/
3588 sbd_error_t *
3589 drmach_passthru(drmachid_t id, drmach_opts_t *opts)
3590 {
3591 	int		i;
3592 	sbd_error_t	*err;
3593 
3594 	i = 0;
3595 	while (drmach_pt_arr[i].name != NULL) {
3596 		int len = strlen(drmach_pt_arr[i].name);
3597 
3598 		if (strncmp(drmach_pt_arr[i].name, opts->copts, len) == 0)
3599 			break;
3600 
3601 		i += 1;
3602 	}
3603 
3604 	if (drmach_pt_arr[i].name == NULL)
3605 		err = drerr_new(0, ESTF_UNKPTCMD, opts->copts);
3606 	else
3607 		err = (*drmach_pt_arr[i].handler)(id, opts);
3608 
3609 	return (err);
3610 }
3611 
3612 sbd_error_t *
3613 drmach_release(drmachid_t id)
3614 {
3615 	drmach_common_t *cp;
3616 	if (!DRMACH_IS_DEVICE_ID(id))
3617 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3618 	cp = id;
3619 
3620 	return (cp->release(id));
3621 }
3622 
3623 sbd_error_t *
3624 drmach_status(drmachid_t id, drmach_status_t *stat)
3625 {
3626 	drmach_common_t *cp;
3627 
3628 	if (!DRMACH_IS_ID(id))
3629 		return (drerr_new(0, ESTF_NOTID, NULL));
3630 	cp = id;
3631 
3632 	return (cp->status(id, stat));
3633 }
3634 
3635 sbd_error_t *
3636 drmach_unconfigure(drmachid_t id, int flags)
3637 {
3638 	drmach_device_t	*dp;
3639 	pnode_t		 nodeid;
3640 	dev_info_t	*dip, *fdip = NULL;
3641 
3642 	if (!DRMACH_IS_DEVICE_ID(id))
3643 		return (drerr_new(0, ESTF_INAPPROP, NULL));
3644 
3645 	dp = id;
3646 
3647 	nodeid = drmach_node_get_dnode(dp->node);
3648 	if (nodeid == OBP_NONODE)
3649 		return (DRMACH_INTERNAL_ERROR());
3650 
3651 	dip = e_ddi_nodeid_to_dip(nodeid);
3652 	if (dip == NULL)
3653 		return (NULL);
3654 
3655 	/*
3656 	 * Branch already held, so hold acquired in
3657 	 * e_ddi_nodeid_to_dip() can be released
3658 	 */
3659 	ddi_release_devi(dip);
3660 
3661 	if (flags & DEVI_BRANCH_DESTROY)
3662 		flags |= DEVI_BRANCH_EVENT;
3663 
3664 	/*
3665 	 * Force flag is no longer necessary. See starcat/io/drmach.c
3666 	 * for details.
3667 	 */
3668 	ASSERT(e_ddi_branch_held(dip));
3669 	if (e_ddi_branch_unconfigure(dip, &fdip, flags)) {
3670 		sbd_error_t	*err;
3671 		char		*path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3672 
3673 		/*
3674 		 * If non-NULL, fdip is returned held and must be released.
3675 		 */
3676 		if (fdip != NULL) {
3677 			(void) ddi_pathname(fdip, path);
3678 			ndi_rele_devi(fdip);
3679 		} else {
3680 			(void) ddi_pathname(dip, path);
3681 		}
3682 
3683 		err = drerr_new(1, ESTF_DRVFAIL, path);
3684 
3685 		kmem_free(path, MAXPATHLEN);
3686 
3687 		return (err);
3688 	}
3689 
3690 	return (NULL);
3691 }
3692 
3693 /*
3694  * drmach interfaces to legacy Starfire platmod logic
3695  * linkage via runtime symbol look up, called from plat_cpu_power*
3696  */
3697 
3698 /*
3699  * Start up a cpu.  It is possible that we're attempting to restart
3700  * the cpu after an UNCONFIGURE in which case the cpu will be
3701  * spinning in its cache.  So, all we have to do is wakeup him up.
3702  * Under normal circumstances the cpu will be coming from a previous
3703  * CONNECT and thus will be spinning in OBP.  In both cases, the
3704  * startup sequence is the same.
3705  */
3706 int
3707 drmach_cpu_poweron(struct cpu *cp)
3708 {
3709 	DRMACH_PR("drmach_cpu_poweron: starting cpuid %d\n", cp->cpu_id);
3710 
3711 	ASSERT(MUTEX_HELD(&cpu_lock));
3712 
3713 	if (drmach_cpu_start(cp) != 0)
3714 		return (EBUSY);
3715 	else
3716 		return (0);
3717 }
3718 
3719 int
3720 drmach_cpu_poweroff(struct cpu *cp)
3721 {
3722 	int		ntries, cnt;
3723 	processorid_t	cpuid = cp->cpu_id;
3724 	void		drmach_cpu_shutdown_self(void);
3725 
3726 	DRMACH_PR("drmach_cpu_poweroff: stopping cpuid %d\n", cp->cpu_id);
3727 
3728 	ASSERT(MUTEX_HELD(&cpu_lock));
3729 
3730 	/*
3731 	 * Capture all CPUs (except for detaching proc) to prevent
3732 	 * crosscalls to the detaching proc until it has cleared its
3733 	 * bit in cpu_ready_set.
3734 	 *
3735 	 * The CPU's remain paused and the prom_mutex is known to be free.
3736 	 * This prevents the x-trap victim from blocking when doing prom
3737 	 * IEEE-1275 calls at a high PIL level.
3738 	 */
3739 	promsafe_pause_cpus();
3740 
3741 	/*
3742 	 * Quiesce interrupts on the target CPU. We do this by setting
3743 	 * the CPU 'not ready'- (i.e. removing the CPU from cpu_ready_set) to
3744 	 * prevent it from receiving cross calls and cross traps.
3745 	 * This prevents the processor from receiving any new soft interrupts.
3746 	 */
3747 	mp_cpu_quiesce(cp);
3748 
3749 	/* setup xt_mb, will be cleared by drmach_shutdown_asm when ready */
3750 	drmach_xt_mb[cpuid] = 0x80;
3751 
3752 	xt_one_unchecked(cpuid, (xcfunc_t *)idle_stop_xcall,
3753 		(uint64_t)drmach_cpu_shutdown_self, NULL);
3754 
3755 	ntries = drmach_cpu_ntries;
3756 	cnt = 0;
3757 	while (drmach_xt_mb[cpuid] && ntries) {
3758 		DELAY(drmach_cpu_delay);
3759 		ntries--;
3760 		cnt++;
3761 	}
3762 
3763 	drmach_xt_mb[cpuid] = 0;	/* steal the cache line back */
3764 
3765 	start_cpus();
3766 
3767 	DRMACH_PR("waited %d out of %d tries for "
3768 		"drmach_cpu_shutdown_self on cpu%d",
3769 		drmach_cpu_ntries - ntries, drmach_cpu_ntries, cp->cpu_id);
3770 
3771 	drmach_cpu_obp_detach(cpuid);
3772 
3773 	CPU_SIGNATURE(OS_SIG, SIGST_DETACHED, SIGSUBST_NULL, cpuid);
3774 
3775 	return (0);
3776 }
3777 
3778 /*ARGSUSED*/
3779 int
3780 drmach_verify_sr(dev_info_t *dip, int sflag)
3781 {
3782 	return (0);
3783 }
3784 
3785 void
3786 drmach_suspend_last(void)
3787 {
3788 }
3789 
3790 void
3791 drmach_resume_first(void)
3792 {
3793 }
3794 
3795 /*
3796  * Log a DR sysevent.
3797  * Return value: 0 success, non-zero failure.
3798  */
3799 int
3800 drmach_log_sysevent(int board, char *hint, int flag, int verbose)
3801 {
3802 	sysevent_t			*ev;
3803 	sysevent_id_t			eid;
3804 	int				rv, km_flag;
3805 	sysevent_value_t		evnt_val;
3806 	sysevent_attr_list_t		*evnt_attr_list = NULL;
3807 	char				attach_pnt[MAXNAMELEN];
3808 
3809 	km_flag = (flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
3810 	attach_pnt[0] = '\0';
3811 	if (drmach_board_name(board, attach_pnt, MAXNAMELEN)) {
3812 		rv = -1;
3813 		goto logexit;
3814 	}
3815 	if (verbose)
3816 		DRMACH_PR("drmach_log_sysevent: %s %s, flag: %d, verbose: %d\n",
3817 			    attach_pnt, hint, flag, verbose);
3818 
3819 	if ((ev = sysevent_alloc(EC_DR, ESC_DR_AP_STATE_CHANGE,
3820 				    SUNW_KERN_PUB"dr", km_flag)) == NULL) {
3821 		rv = -2;
3822 		goto logexit;
3823 	}
3824 	evnt_val.value_type = SE_DATA_TYPE_STRING;
3825 	evnt_val.value.sv_string = attach_pnt;
3826 	if ((rv = sysevent_add_attr(&evnt_attr_list, DR_AP_ID,
3827 				    &evnt_val, km_flag)) != 0)
3828 		goto logexit;
3829 
3830 	evnt_val.value_type = SE_DATA_TYPE_STRING;
3831 	evnt_val.value.sv_string = hint;
3832 	if ((rv = sysevent_add_attr(&evnt_attr_list, DR_HINT,
3833 				    &evnt_val, km_flag)) != 0) {
3834 		sysevent_free_attr(evnt_attr_list);
3835 		goto logexit;
3836 	}
3837 
3838 	(void) sysevent_attach_attributes(ev, evnt_attr_list);
3839 
3840 	/*
3841 	 * Log the event but do not sleep waiting for its
3842 	 * delivery. This provides insulation from syseventd.
3843 	 */
3844 	rv = log_sysevent(ev, SE_NOSLEEP, &eid);
3845 
3846 logexit:
3847 	if (ev)
3848 		sysevent_free(ev);
3849 	if ((rv != 0) && verbose)
3850 		cmn_err(CE_WARN,
3851 			    "drmach_log_sysevent failed (rv %d) for %s  %s\n",
3852 			    rv, attach_pnt, hint);
3853 
3854 	return (rv);
3855 }
3856 
3857 /*ARGSUSED*/
3858 int
3859 drmach_allow_memrange_modify(drmachid_t id)
3860 {
3861 	return (1);	/* TRUE */
3862 }
3863