xref: /illumos-gate/usr/src/uts/sun4u/opl/os/opl.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/cpuvar.h>
27 #include <sys/systm.h>
28 #include <sys/sysmacros.h>
29 #include <sys/promif.h>
30 #include <sys/platform_module.h>
31 #include <sys/cmn_err.h>
32 #include <sys/errno.h>
33 #include <sys/machsystm.h>
34 #include <sys/bootconf.h>
35 #include <sys/nvpair.h>
36 #include <sys/kobj.h>
37 #include <sys/mem_cage.h>
38 #include <sys/opl.h>
39 #include <sys/scfd/scfostoescf.h>
40 #include <sys/cpu_sgnblk_defs.h>
41 #include <sys/utsname.h>
42 #include <sys/ddi.h>
43 #include <sys/sunndi.h>
44 #include <sys/lgrp.h>
45 #include <sys/memnode.h>
46 #include <sys/sysmacros.h>
47 #include <sys/time.h>
48 #include <sys/cpu.h>
49 #include <vm/vm_dep.h>
50 
51 int (*opl_get_mem_unum)(int, uint64_t, char *, int, int *);
52 int (*opl_get_mem_sid)(char *unum, char *buf, int buflen, int *lenp);
53 int (*opl_get_mem_offset)(uint64_t paddr, uint64_t *offp);
54 int (*opl_get_mem_addr)(char *unum, char *sid,
55     uint64_t offset, uint64_t *paddr);
56 
57 /* Memory for fcode claims.  16k times # maximum possible IO units */
58 #define	EFCODE_SIZE	(OPL_MAX_BOARDS * OPL_MAX_IO_UNITS_PER_BOARD * 0x4000)
59 int efcode_size = EFCODE_SIZE;
60 
61 #define	OPL_MC_MEMBOARD_SHIFT 38	/* Boards on 256BG boundary */
62 
63 /* Set the maximum number of boards for DR */
64 int opl_boards = OPL_MAX_BOARDS;
65 
66 void sgn_update_all_cpus(ushort_t, uchar_t, uchar_t);
67 
68 extern int tsb_lgrp_affinity;
69 
70 int opl_tsb_spares = (OPL_MAX_BOARDS) * (OPL_MAX_PCICH_UNITS_PER_BOARD) *
71 	(OPL_MAX_TSBS_PER_PCICH);
72 
73 pgcnt_t opl_startup_cage_size = 0;
74 
75 /*
76  * The length of the delay in seconds in communication with XSCF after
77  * which the warning message will be logged.
78  */
79 uint_t	xscf_connect_delay = 60 * 15;
80 
81 static opl_model_info_t opl_models[] = {
82 	{ "FF1", OPL_MAX_BOARDS_FF1, FF1, STD_DISPATCH_TABLE },
83 	{ "FF2", OPL_MAX_BOARDS_FF2, FF2, STD_DISPATCH_TABLE },
84 	{ "DC1", OPL_MAX_BOARDS_DC1, DC1, STD_DISPATCH_TABLE },
85 	{ "DC2", OPL_MAX_BOARDS_DC2, DC2, EXT_DISPATCH_TABLE },
86 	{ "DC3", OPL_MAX_BOARDS_DC3, DC3, EXT_DISPATCH_TABLE },
87 	{ "IKKAKU", OPL_MAX_BOARDS_IKKAKU, IKKAKU, STD_DISPATCH_TABLE },
88 };
89 static	int	opl_num_models = sizeof (opl_models)/sizeof (opl_model_info_t);
90 
91 /*
92  * opl_cur_model
93  */
94 static	opl_model_info_t *opl_cur_model = NULL;
95 
96 static struct memlist *opl_memlist_per_board(struct memlist *ml);
97 static void post_xscf_msg(char *, int);
98 static void pass2xscf_thread();
99 
100 /*
101  * Note FF/DC out-of-order instruction engine takes only a
102  * single cycle to execute each spin loop
103  * for comparison, Panther takes 6 cycles for same loop
104  * OPL_BOFF_SPIN = base spin loop, roughly one memory reference time
105  * OPL_BOFF_TM = approx nsec for OPL sleep instruction (1600 for OPL-C)
106  * OPL_BOFF_SLEEP = approx number of SPIN iterations to equal one sleep
107  * OPL_BOFF_MAX_SCALE - scaling factor for max backoff based on active cpus
108  * Listed values tuned for 2.15GHz to 2.64GHz systems
109  * Value may change for future systems
110  */
111 #define	OPL_BOFF_SPIN 7
112 #define	OPL_BOFF_SLEEP 4
113 #define	OPL_BOFF_TM 1600
114 #define	OPL_BOFF_MAX_SCALE 8
115 
116 #define	OPL_CLOCK_TICK_THRESHOLD	128
117 #define	OPL_CLOCK_TICK_NCPUS		64
118 
119 extern int	clock_tick_threshold;
120 extern int	clock_tick_ncpus;
121 
122 int
123 set_platform_max_ncpus(void)
124 {
125 	return (OPL_MAX_CPU_PER_BOARD * OPL_MAX_BOARDS);
126 }
127 
128 int
129 set_platform_tsb_spares(void)
130 {
131 	return (MIN(opl_tsb_spares, MAX_UPA));
132 }
133 
134 static void
135 set_model_info()
136 {
137 	extern int ts_dispatch_extended;
138 	char	name[MAXSYSNAME];
139 	int	i;
140 
141 	/*
142 	 * Get model name from the root node.
143 	 *
144 	 * We are using the prom device tree since, at this point,
145 	 * the Solaris device tree is not yet setup.
146 	 */
147 	(void) prom_getprop(prom_rootnode(), "model", (caddr_t)name);
148 
149 	for (i = 0; i < opl_num_models; i++) {
150 		if (strncmp(name, opl_models[i].model_name, MAXSYSNAME) == 0) {
151 			opl_cur_model = &opl_models[i];
152 			break;
153 		}
154 	}
155 
156 	/*
157 	 * If model not matched, it's an unknown model.
158 	 * Just return.  It will default to standard dispatch tables.
159 	 */
160 	if (i == opl_num_models)
161 		return;
162 
163 	if ((opl_cur_model->model_cmds & EXT_DISPATCH_TABLE) &&
164 	    (ts_dispatch_extended == -1)) {
165 		/*
166 		 * Based on a platform model, select a dispatch table.
167 		 * Only DC2 and DC3 systems uses the alternate/extended
168 		 * TS dispatch table.
169 		 * IKKAKU, FF1, FF2 and DC1 systems use standard dispatch
170 		 * tables.
171 		 */
172 		ts_dispatch_extended = 1;
173 	}
174 
175 }
176 
177 static void
178 set_max_mmu_ctxdoms()
179 {
180 	extern uint_t	max_mmu_ctxdoms;
181 	int		max_boards;
182 
183 	/*
184 	 * From the model, get the maximum number of boards
185 	 * supported and set the value accordingly. If the model
186 	 * could not be determined or recognized, we assume the max value.
187 	 */
188 	if (opl_cur_model == NULL)
189 		max_boards = OPL_MAX_BOARDS;
190 	else
191 		max_boards = opl_cur_model->model_max_boards;
192 
193 	/*
194 	 * On OPL, cores and MMUs are one-to-one.
195 	 */
196 	max_mmu_ctxdoms = OPL_MAX_CORE_UNITS_PER_BOARD * max_boards;
197 }
198 
199 #pragma weak mmu_init_large_pages
200 
201 void
202 set_platform_defaults(void)
203 {
204 	extern char *tod_module_name;
205 	extern void cpu_sgn_update(ushort_t, uchar_t, uchar_t, int);
206 	extern void mmu_init_large_pages(size_t);
207 
208 	/* Set the CPU signature function pointer */
209 	cpu_sgn_func = cpu_sgn_update;
210 
211 	/* Set appropriate tod module for OPL platform */
212 	ASSERT(tod_module_name == NULL);
213 	tod_module_name = "todopl";
214 
215 	if ((mmu_page_sizes == max_mmu_page_sizes) &&
216 	    (mmu_ism_pagesize != DEFAULT_ISM_PAGESIZE)) {
217 		if (&mmu_init_large_pages)
218 			mmu_init_large_pages(mmu_ism_pagesize);
219 	}
220 
221 	tsb_lgrp_affinity = 1;
222 
223 	set_max_mmu_ctxdoms();
224 }
225 
226 /*
227  * Convert logical a board number to a physical one.
228  */
229 
230 #define	LSBPROP		"board#"
231 #define	PSBPROP		"physical-board#"
232 
233 int
234 opl_get_physical_board(int id)
235 {
236 	dev_info_t	*root_dip, *dip = NULL;
237 	char		*dname = NULL;
238 	int		circ;
239 
240 	pnode_t		pnode;
241 	char		pname[MAXSYSNAME] = {0};
242 
243 	int		lsb_id;	/* Logical System Board ID */
244 	int		psb_id;	/* Physical System Board ID */
245 
246 
247 	/*
248 	 * This function is called on early stage of bootup when the
249 	 * kernel device tree is not initialized yet, and also
250 	 * later on when the device tree is up. We want to try
251 	 * the fast track first.
252 	 */
253 	root_dip = ddi_root_node();
254 	if (root_dip) {
255 		/* Get from devinfo node */
256 		ndi_devi_enter(root_dip, &circ);
257 		for (dip = ddi_get_child(root_dip); dip;
258 		    dip = ddi_get_next_sibling(dip)) {
259 
260 			dname = ddi_node_name(dip);
261 			if (strncmp(dname, "pseudo-mc", 9) != 0)
262 				continue;
263 
264 			if ((lsb_id = (int)ddi_getprop(DDI_DEV_T_ANY, dip,
265 			    DDI_PROP_DONTPASS, LSBPROP, -1)) == -1)
266 				continue;
267 
268 			if (id == lsb_id) {
269 				if ((psb_id = (int)ddi_getprop(DDI_DEV_T_ANY,
270 				    dip, DDI_PROP_DONTPASS, PSBPROP, -1))
271 				    == -1) {
272 					ndi_devi_exit(root_dip, circ);
273 					return (-1);
274 				} else {
275 					ndi_devi_exit(root_dip, circ);
276 					return (psb_id);
277 				}
278 			}
279 		}
280 		ndi_devi_exit(root_dip, circ);
281 	}
282 
283 	/*
284 	 * We do not have the kernel device tree, or we did not
285 	 * find the node for some reason (let's say the kernel
286 	 * device tree was modified), let's try the OBP tree.
287 	 */
288 	pnode = prom_rootnode();
289 	for (pnode = prom_childnode(pnode); pnode;
290 	    pnode = prom_nextnode(pnode)) {
291 
292 		if ((prom_getprop(pnode, "name", (caddr_t)pname) == -1) ||
293 		    (strncmp(pname, "pseudo-mc", 9) != 0))
294 			continue;
295 
296 		if (prom_getprop(pnode, LSBPROP, (caddr_t)&lsb_id) == -1)
297 			continue;
298 
299 		if (id == lsb_id) {
300 			if (prom_getprop(pnode, PSBPROP,
301 			    (caddr_t)&psb_id) == -1) {
302 				return (-1);
303 			} else {
304 				return (psb_id);
305 			}
306 		}
307 	}
308 
309 	return (-1);
310 }
311 
312 /*
313  * For OPL it's possible that memory from two or more successive boards
314  * will be contiguous across the boards, and therefore represented as a
315  * single chunk.
316  * This function splits such chunks down the board boundaries.
317  */
318 static struct memlist *
319 opl_memlist_per_board(struct memlist *ml)
320 {
321 	uint64_t ssize, low, high, boundary;
322 	struct memlist *head, *tail, *new;
323 
324 	ssize = (1ull << OPL_MC_MEMBOARD_SHIFT);
325 
326 	head = tail = NULL;
327 
328 	for (; ml; ml = ml->next) {
329 		low  = (uint64_t)ml->address;
330 		high = low+(uint64_t)(ml->size);
331 		while (low < high) {
332 			boundary = roundup(low+1, ssize);
333 			boundary = MIN(high, boundary);
334 			new = kmem_zalloc(sizeof (struct memlist), KM_SLEEP);
335 			new->address = low;
336 			new->size = boundary - low;
337 			if (head == NULL)
338 				head = new;
339 			if (tail) {
340 				tail->next = new;
341 				new->prev = tail;
342 			}
343 			tail = new;
344 			low = boundary;
345 		}
346 	}
347 	return (head);
348 }
349 
350 void
351 set_platform_cage_params(void)
352 {
353 	extern pgcnt_t total_pages;
354 	extern struct memlist *phys_avail;
355 	struct memlist *ml, *tml;
356 
357 	if (kernel_cage_enable) {
358 		pgcnt_t preferred_cage_size;
359 
360 		preferred_cage_size = MAX(opl_startup_cage_size,
361 		    total_pages / 256);
362 
363 		ml = opl_memlist_per_board(phys_avail);
364 
365 		/*
366 		 * Note: we are assuming that post has load the
367 		 * whole show in to the high end of memory. Having
368 		 * taken this leap, we copy the whole of phys_avail
369 		 * the glist and arrange for the cage to grow
370 		 * downward (descending pfns).
371 		 */
372 		kcage_range_init(ml, KCAGE_DOWN, preferred_cage_size);
373 
374 		/* free the memlist */
375 		do {
376 			tml = ml->next;
377 			kmem_free(ml, sizeof (struct memlist));
378 			ml = tml;
379 		} while (ml != NULL);
380 	}
381 
382 	if (kcage_on)
383 		cmn_err(CE_NOTE, "!DR Kernel Cage is ENABLED");
384 	else
385 		cmn_err(CE_NOTE, "!DR Kernel Cage is DISABLED");
386 }
387 
388 /*ARGSUSED*/
389 int
390 plat_cpu_poweron(struct cpu *cp)
391 {
392 	int (*opl_cpu_poweron)(struct cpu *) = NULL;
393 
394 	opl_cpu_poweron =
395 	    (int (*)(struct cpu *))kobj_getsymvalue("drmach_cpu_poweron", 0);
396 
397 	if (opl_cpu_poweron == NULL)
398 		return (ENOTSUP);
399 	else
400 		return ((opl_cpu_poweron)(cp));
401 
402 }
403 
404 /*ARGSUSED*/
405 int
406 plat_cpu_poweroff(struct cpu *cp)
407 {
408 	int (*opl_cpu_poweroff)(struct cpu *) = NULL;
409 
410 	opl_cpu_poweroff =
411 	    (int (*)(struct cpu *))kobj_getsymvalue("drmach_cpu_poweroff", 0);
412 
413 	if (opl_cpu_poweroff == NULL)
414 		return (ENOTSUP);
415 	else
416 		return ((opl_cpu_poweroff)(cp));
417 
418 }
419 
420 int
421 plat_max_boards(void)
422 {
423 	/*
424 	 * If the model cannot be determined, default to the max value.
425 	 * Otherwise, Ikkaku model only supports 1 system board.
426 	 */
427 	if ((opl_cur_model != NULL) && (opl_cur_model->model_type == IKKAKU))
428 		return (OPL_MAX_BOARDS_IKKAKU);
429 	else
430 		return (OPL_MAX_BOARDS);
431 }
432 
433 int
434 plat_max_cpu_units_per_board(void)
435 {
436 	return (OPL_MAX_CPU_PER_BOARD);
437 }
438 
439 int
440 plat_max_mem_units_per_board(void)
441 {
442 	return (OPL_MAX_MEM_UNITS_PER_BOARD);
443 }
444 
445 int
446 plat_max_io_units_per_board(void)
447 {
448 	return (OPL_MAX_IO_UNITS_PER_BOARD);
449 }
450 
451 int
452 plat_max_cmp_units_per_board(void)
453 {
454 	return (OPL_MAX_CMP_UNITS_PER_BOARD);
455 }
456 
457 int
458 plat_max_core_units_per_board(void)
459 {
460 	return (OPL_MAX_CORE_UNITS_PER_BOARD);
461 }
462 
463 int
464 plat_pfn_to_mem_node(pfn_t pfn)
465 {
466 	return (pfn >> mem_node_pfn_shift);
467 }
468 
469 /* ARGSUSED */
470 void
471 plat_build_mem_nodes(prom_memlist_t *list, size_t nelems)
472 {
473 	size_t	elem;
474 	pfn_t	basepfn;
475 	pgcnt_t	npgs;
476 	uint64_t	boundary, ssize;
477 	uint64_t	low, high;
478 
479 	/*
480 	 * OPL mem slices are always aligned on a 256GB boundary.
481 	 */
482 	mem_node_pfn_shift = OPL_MC_MEMBOARD_SHIFT - MMU_PAGESHIFT;
483 	mem_node_physalign = 0;
484 
485 	/*
486 	 * Boot install lists are arranged <addr, len>, <addr, len>, ...
487 	 */
488 	ssize = (1ull << OPL_MC_MEMBOARD_SHIFT);
489 	for (elem = 0; elem < nelems; list++, elem++) {
490 		low  = list->addr;
491 		high = low + list->size;
492 		while (low < high) {
493 			boundary = roundup(low+1, ssize);
494 			boundary = MIN(high, boundary);
495 			basepfn = btop(low);
496 			npgs = btop(boundary - low);
497 			mem_node_add_slice(basepfn, basepfn + npgs - 1);
498 			low = boundary;
499 		}
500 	}
501 }
502 
503 /*
504  * Find the CPU associated with a slice at boot-time.
505  */
506 void
507 plat_fill_mc(pnode_t nodeid)
508 {
509 	int board;
510 	int memnode;
511 	struct {
512 		uint64_t	addr;
513 		uint64_t	size;
514 	} mem_range;
515 
516 	if (prom_getprop(nodeid, "board#", (caddr_t)&board) < 0) {
517 		panic("Can not find board# property in mc node %x", nodeid);
518 	}
519 	if (prom_getprop(nodeid, "sb-mem-ranges", (caddr_t)&mem_range) < 0) {
520 		panic("Can not find sb-mem-ranges property in mc node %x",
521 		    nodeid);
522 	}
523 	memnode = mem_range.addr >> OPL_MC_MEMBOARD_SHIFT;
524 	plat_assign_lgrphand_to_mem_node(board, memnode);
525 }
526 
527 /*
528  * Return the platform handle for the lgroup containing the given CPU
529  *
530  * For OPL, lgroup platform handle == board #.
531  */
532 
533 extern int mpo_disabled;
534 extern lgrp_handle_t lgrp_default_handle;
535 
536 lgrp_handle_t
537 plat_lgrp_cpu_to_hand(processorid_t id)
538 {
539 	lgrp_handle_t plathand;
540 
541 	/*
542 	 * Return the real platform handle for the CPU until
543 	 * such time as we know that MPO should be disabled.
544 	 * At that point, we set the "mpo_disabled" flag to true,
545 	 * and from that point on, return the default handle.
546 	 *
547 	 * By the time we know that MPO should be disabled, the
548 	 * first CPU will have already been added to a leaf
549 	 * lgroup, but that's ok. The common lgroup code will
550 	 * double check that the boot CPU is in the correct place,
551 	 * and in the case where mpo should be disabled, will move
552 	 * it to the root if necessary.
553 	 */
554 	if (mpo_disabled) {
555 		/* If MPO is disabled, return the default (UMA) handle */
556 		plathand = lgrp_default_handle;
557 	} else
558 		plathand = (lgrp_handle_t)LSB_ID(id);
559 	return (plathand);
560 }
561 
562 /*
563  * Platform specific lgroup initialization
564  */
565 void
566 plat_lgrp_init(void)
567 {
568 	extern uint32_t lgrp_expand_proc_thresh;
569 	extern uint32_t lgrp_expand_proc_diff;
570 	const uint_t m = LGRP_LOADAVG_THREAD_MAX;
571 
572 	/*
573 	 * Set tuneables for the OPL architecture
574 	 *
575 	 * lgrp_expand_proc_thresh is the threshold load on the set of
576 	 * lgroups a process is currently using on before considering
577 	 * adding another lgroup to the set.  For Oly-C and Jupiter
578 	 * systems, there are four sockets per lgroup. Setting
579 	 * lgrp_expand_proc_thresh to add lgroups when the load reaches
580 	 * four threads will spread the load when it exceeds one thread
581 	 * per socket, optimizing memory bandwidth and L2 cache space.
582 	 *
583 	 * lgrp_expand_proc_diff determines how much less another lgroup
584 	 * must be loaded before shifting the start location of a thread
585 	 * to it.
586 	 *
587 	 * lgrp_loadavg_tolerance is the threshold where two lgroups are
588 	 * considered to have different loads.  It is set to be less than
589 	 * 1% so that even a small residual load will be considered different
590 	 * from no residual load.
591 	 *
592 	 * We note loadavg values are not precise.
593 	 * Every 1/10 of a second loadavg values are reduced by 5%.
594 	 * This adjustment can come in the middle of the lgroup selection
595 	 * process, and for larger parallel apps with many threads can
596 	 * frequently occur between the start of the second thread
597 	 * placement and the finish of the last thread placement.
598 	 * We also must be careful to not use too small of a threshold
599 	 * since the cumulative decay for 1 second idle time is 40%.
600 	 * That is, the residual load from completed threads will still
601 	 * be 60% one second after the proc goes idle or 8% after 5 seconds.
602 	 *
603 	 * To allow for lag time in loadavg calculations
604 	 * remote thresh = 3.75 * LGRP_LOADAVG_THREAD_MAX
605 	 * local thresh  = 0.75 * LGRP_LOADAVG_THREAD_MAX
606 	 * tolerance	 = 0.0078 * LGRP_LOADAVG_THREAD_MAX
607 	 *
608 	 * The load placement algorithms consider LGRP_LOADAVG_THREAD_MAX
609 	 * as the equivalent of a load of 1. To make the code more compact,
610 	 * we set m = LGRP_LOADAVG_THREAD_MAX.
611 	 */
612 	lgrp_expand_proc_thresh = (m * 3) + (m >> 1) + (m >> 2);
613 	lgrp_expand_proc_diff = (m >> 1) + (m >> 2);
614 	lgrp_loadavg_tolerance = (m >> 7);
615 }
616 
617 /*
618  * Platform notification of lgroup (re)configuration changes
619  */
620 /*ARGSUSED*/
621 void
622 plat_lgrp_config(lgrp_config_flag_t evt, uintptr_t arg)
623 {
624 	update_membounds_t *umb;
625 	lgrp_config_mem_rename_t lmr;
626 	int sbd, tbd;
627 	lgrp_handle_t hand, shand, thand;
628 	int mnode, snode, tnode;
629 	pfn_t start, end;
630 
631 	if (mpo_disabled)
632 		return;
633 
634 	switch (evt) {
635 
636 	case LGRP_CONFIG_MEM_ADD:
637 		/*
638 		 * Establish the lgroup handle to memnode translation.
639 		 */
640 		umb = (update_membounds_t *)arg;
641 
642 		hand = umb->u_board;
643 		mnode = plat_pfn_to_mem_node(umb->u_base >> MMU_PAGESHIFT);
644 		plat_assign_lgrphand_to_mem_node(hand, mnode);
645 
646 		break;
647 
648 	case LGRP_CONFIG_MEM_DEL:
649 		/*
650 		 * Special handling for possible memory holes.
651 		 */
652 		umb = (update_membounds_t *)arg;
653 		hand = umb->u_board;
654 		if ((mnode = plat_lgrphand_to_mem_node(hand)) != -1) {
655 			if (mem_node_config[mnode].exists) {
656 				start = mem_node_config[mnode].physbase;
657 				end = mem_node_config[mnode].physmax;
658 				mem_node_del_slice(start, end);
659 			}
660 		}
661 
662 		break;
663 
664 	case LGRP_CONFIG_MEM_RENAME:
665 		/*
666 		 * During a DR copy-rename operation, all of the memory
667 		 * on one board is moved to another board -- but the
668 		 * addresses/pfns and memnodes don't change. This means
669 		 * the memory has changed locations without changing identity.
670 		 *
671 		 * Source is where we are copying from and target is where we
672 		 * are copying to.  After source memnode is copied to target
673 		 * memnode, the physical addresses of the target memnode are
674 		 * renamed to match what the source memnode had.  Then target
675 		 * memnode can be removed and source memnode can take its
676 		 * place.
677 		 *
678 		 * To do this, swap the lgroup handle to memnode mappings for
679 		 * the boards, so target lgroup will have source memnode and
680 		 * source lgroup will have empty target memnode which is where
681 		 * its memory will go (if any is added to it later).
682 		 *
683 		 * Then source memnode needs to be removed from its lgroup
684 		 * and added to the target lgroup where the memory was living
685 		 * but under a different name/memnode.  The memory was in the
686 		 * target memnode and now lives in the source memnode with
687 		 * different physical addresses even though it is the same
688 		 * memory.
689 		 */
690 		sbd = arg & 0xffff;
691 		tbd = (arg & 0xffff0000) >> 16;
692 		shand = sbd;
693 		thand = tbd;
694 		snode = plat_lgrphand_to_mem_node(shand);
695 		tnode = plat_lgrphand_to_mem_node(thand);
696 
697 		/*
698 		 * Special handling for possible memory holes.
699 		 */
700 		if (tnode != -1 && mem_node_config[tnode].exists) {
701 			start = mem_node_config[tnode].physbase;
702 			end = mem_node_config[tnode].physmax;
703 			mem_node_del_slice(start, end);
704 		}
705 
706 		plat_assign_lgrphand_to_mem_node(thand, snode);
707 		plat_assign_lgrphand_to_mem_node(shand, tnode);
708 
709 		lmr.lmem_rename_from = shand;
710 		lmr.lmem_rename_to = thand;
711 
712 		/*
713 		 * Remove source memnode of copy rename from its lgroup
714 		 * and add it to its new target lgroup
715 		 */
716 		lgrp_config(LGRP_CONFIG_MEM_RENAME, (uintptr_t)snode,
717 		    (uintptr_t)&lmr);
718 
719 		break;
720 
721 	default:
722 		break;
723 	}
724 }
725 
726 /*
727  * Return latency between "from" and "to" lgroups
728  *
729  * This latency number can only be used for relative comparison
730  * between lgroups on the running system, cannot be used across platforms,
731  * and may not reflect the actual latency.  It is platform and implementation
732  * specific, so platform gets to decide its value.  It would be nice if the
733  * number was at least proportional to make comparisons more meaningful though.
734  * NOTE: The numbers below are supposed to be load latencies for uncached
735  * memory divided by 10.
736  *
737  */
738 int
739 plat_lgrp_latency(lgrp_handle_t from, lgrp_handle_t to)
740 {
741 	/*
742 	 * Return min remote latency when there are more than two lgroups
743 	 * (root and child) and getting latency between two different lgroups
744 	 * or root is involved
745 	 */
746 	if (lgrp_optimizations() && (from != to ||
747 	    from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE))
748 		return (42);
749 	else
750 		return (35);
751 }
752 
753 /*
754  * Return platform handle for root lgroup
755  */
756 lgrp_handle_t
757 plat_lgrp_root_hand(void)
758 {
759 	if (mpo_disabled)
760 		return (lgrp_default_handle);
761 
762 	return (LGRP_DEFAULT_HANDLE);
763 }
764 
765 /*ARGSUSED*/
766 void
767 plat_freelist_process(int mnode)
768 {
769 }
770 
771 void
772 load_platform_drivers(void)
773 {
774 	(void) i_ddi_attach_pseudo_node("dr");
775 }
776 
777 /*
778  * No platform drivers on this platform
779  */
780 char *platform_module_list[] = {
781 	(char *)0
782 };
783 
784 /*ARGSUSED*/
785 void
786 plat_tod_fault(enum tod_fault_type tod_bad)
787 {
788 }
789 
790 /*ARGSUSED*/
791 void
792 cpu_sgn_update(ushort_t sgn, uchar_t state, uchar_t sub_state, int cpuid)
793 {
794 	static void (*scf_panic_callback)(int);
795 	static void (*scf_shutdown_callback)(int);
796 
797 	/*
798 	 * This is for notifing system panic/shutdown to SCF.
799 	 * In case of shutdown and panic, SCF call back
800 	 * function should be called.
801 	 *  <SCF call back functions>
802 	 *   scf_panic_callb()   : panicsys()->panic_quiesce_hw()
803 	 *   scf_shutdown_callb(): halt() or power_down() or reboot_machine()
804 	 * cpuid should be -1 and state should be SIGST_EXIT.
805 	 */
806 	if (state == SIGST_EXIT && cpuid == -1) {
807 
808 		/*
809 		 * find the symbol for the SCF panic callback routine in driver
810 		 */
811 		if (scf_panic_callback == NULL)
812 			scf_panic_callback = (void (*)(int))
813 			    modgetsymvalue("scf_panic_callb", 0);
814 		if (scf_shutdown_callback == NULL)
815 			scf_shutdown_callback = (void (*)(int))
816 			    modgetsymvalue("scf_shutdown_callb", 0);
817 
818 		switch (sub_state) {
819 		case SIGSUBST_PANIC:
820 			if (scf_panic_callback == NULL) {
821 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
822 				    "scf_panic_callb not found\n");
823 				return;
824 			}
825 			scf_panic_callback(SIGSUBST_PANIC);
826 			break;
827 
828 		case SIGSUBST_HALT:
829 			if (scf_shutdown_callback == NULL) {
830 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
831 				    "scf_shutdown_callb not found\n");
832 				return;
833 			}
834 			scf_shutdown_callback(SIGSUBST_HALT);
835 			break;
836 
837 		case SIGSUBST_ENVIRON:
838 			if (scf_shutdown_callback == NULL) {
839 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
840 				    "scf_shutdown_callb not found\n");
841 				return;
842 			}
843 			scf_shutdown_callback(SIGSUBST_ENVIRON);
844 			break;
845 
846 		case SIGSUBST_REBOOT:
847 			if (scf_shutdown_callback == NULL) {
848 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
849 				    "scf_shutdown_callb not found\n");
850 				return;
851 			}
852 			scf_shutdown_callback(SIGSUBST_REBOOT);
853 			break;
854 		}
855 	}
856 }
857 
858 /*ARGSUSED*/
859 int
860 plat_get_mem_unum(int synd_code, uint64_t flt_addr, int flt_bus_id,
861 	int flt_in_memory, ushort_t flt_status,
862 	char *buf, int buflen, int *lenp)
863 {
864 	/*
865 	 * check if it's a Memory error.
866 	 */
867 	if (flt_in_memory) {
868 		if (opl_get_mem_unum != NULL) {
869 			return (opl_get_mem_unum(synd_code, flt_addr, buf,
870 			    buflen, lenp));
871 		} else {
872 			return (ENOTSUP);
873 		}
874 	} else {
875 		return (ENOTSUP);
876 	}
877 }
878 
879 /*ARGSUSED*/
880 int
881 plat_get_cpu_unum(int cpuid, char *buf, int buflen, int *lenp)
882 {
883 	int	ret = 0;
884 	int	sb;
885 	int	plen;
886 
887 	sb = opl_get_physical_board(LSB_ID(cpuid));
888 	if (sb == -1) {
889 		return (ENXIO);
890 	}
891 
892 	/*
893 	 * opl_cur_model is assigned here
894 	 */
895 	if (opl_cur_model == NULL) {
896 		set_model_info();
897 
898 		/*
899 		 * if not matched, return
900 		 */
901 		if (opl_cur_model == NULL)
902 			return (ENODEV);
903 	}
904 
905 	ASSERT((opl_cur_model - opl_models) == (opl_cur_model->model_type));
906 
907 	switch (opl_cur_model->model_type) {
908 	case FF1:
909 		plen = snprintf(buf, buflen, "/%s/CPUM%d", "MBU_A",
910 		    CHIP_ID(cpuid) / 2);
911 		break;
912 
913 	case FF2:
914 		plen = snprintf(buf, buflen, "/%s/CPUM%d", "MBU_B",
915 		    (CHIP_ID(cpuid) / 2) + (sb * 2));
916 		break;
917 
918 	case DC1:
919 	case DC2:
920 	case DC3:
921 		plen = snprintf(buf, buflen, "/%s%02d/CPUM%d", "CMU", sb,
922 		    CHIP_ID(cpuid));
923 		break;
924 
925 	case IKKAKU:
926 		plen = snprintf(buf, buflen, "/%s", "MBU_A");
927 		break;
928 
929 	default:
930 		/* This should never happen */
931 		return (ENODEV);
932 	}
933 
934 	if (plen >= buflen) {
935 		ret = ENOSPC;
936 	} else {
937 		if (lenp)
938 			*lenp = strlen(buf);
939 	}
940 	return (ret);
941 }
942 
943 void
944 plat_nodename_set(void)
945 {
946 	post_xscf_msg((char *)&utsname, sizeof (struct utsname));
947 }
948 
949 caddr_t	efcode_vaddr = NULL;
950 
951 /*
952  * Preallocate enough memory for fcode claims.
953  */
954 
955 caddr_t
956 efcode_alloc(caddr_t alloc_base)
957 {
958 	caddr_t efcode_alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
959 	    MMU_PAGESIZE);
960 	caddr_t vaddr;
961 
962 	/*
963 	 * allocate the physical memory for the Oberon fcode.
964 	 */
965 	if ((vaddr = (caddr_t)BOP_ALLOC(bootops, efcode_alloc_base,
966 	    efcode_size, MMU_PAGESIZE)) == NULL)
967 		cmn_err(CE_PANIC, "Cannot allocate Efcode Memory");
968 
969 	efcode_vaddr = vaddr;
970 
971 	return (efcode_alloc_base + efcode_size);
972 }
973 
974 caddr_t
975 plat_startup_memlist(caddr_t alloc_base)
976 {
977 	caddr_t tmp_alloc_base;
978 
979 	tmp_alloc_base = efcode_alloc(alloc_base);
980 	tmp_alloc_base =
981 	    (caddr_t)roundup((uintptr_t)tmp_alloc_base, ecache_alignsize);
982 	return (tmp_alloc_base);
983 }
984 
985 /* need to forward declare these */
986 static void plat_lock_delay(uint_t);
987 
988 void
989 startup_platform(void)
990 {
991 	if (clock_tick_threshold == 0)
992 		clock_tick_threshold = OPL_CLOCK_TICK_THRESHOLD;
993 	if (clock_tick_ncpus == 0)
994 		clock_tick_ncpus = OPL_CLOCK_TICK_NCPUS;
995 	mutex_lock_delay = plat_lock_delay;
996 	mutex_cap_factor = OPL_BOFF_MAX_SCALE;
997 }
998 
999 static uint_t
1000 get_mmu_id(processorid_t cpuid)
1001 {
1002 	int pb = opl_get_physical_board(LSB_ID(cpuid));
1003 
1004 	if (pb == -1) {
1005 		cmn_err(CE_PANIC,
1006 		    "opl_get_physical_board failed (cpu %d LSB %u)",
1007 		    cpuid, LSB_ID(cpuid));
1008 	}
1009 	return (pb * OPL_MAX_COREID_PER_BOARD) + (CHIP_ID(cpuid) *
1010 	    OPL_MAX_COREID_PER_CMP) + CORE_ID(cpuid);
1011 }
1012 
1013 void
1014 plat_cpuid_to_mmu_ctx_info(processorid_t cpuid, mmu_ctx_info_t *info)
1015 {
1016 	int	impl;
1017 
1018 	impl = cpunodes[cpuid].implementation;
1019 	if (IS_OLYMPUS_C(impl) || IS_JUPITER(impl)) {
1020 		info->mmu_idx = get_mmu_id(cpuid);
1021 		info->mmu_nctxs = 8192;
1022 	} else {
1023 		cmn_err(CE_PANIC, "Unknown processor %d", impl);
1024 	}
1025 }
1026 
1027 int
1028 plat_get_mem_sid(char *unum, char *buf, int buflen, int *lenp)
1029 {
1030 	if (opl_get_mem_sid == NULL) {
1031 		return (ENOTSUP);
1032 	}
1033 	return (opl_get_mem_sid(unum, buf, buflen, lenp));
1034 }
1035 
1036 int
1037 plat_get_mem_offset(uint64_t paddr, uint64_t *offp)
1038 {
1039 	if (opl_get_mem_offset == NULL) {
1040 		return (ENOTSUP);
1041 	}
1042 	return (opl_get_mem_offset(paddr, offp));
1043 }
1044 
1045 int
1046 plat_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *addrp)
1047 {
1048 	if (opl_get_mem_addr == NULL) {
1049 		return (ENOTSUP);
1050 	}
1051 	return (opl_get_mem_addr(unum, sid, offset, addrp));
1052 }
1053 
1054 void
1055 plat_lock_delay(uint_t backoff)
1056 {
1057 	int i;
1058 	uint_t cnt, remcnt;
1059 	int ctr;
1060 	hrtime_t delay_start, rem_delay;
1061 	/*
1062 	 * Platform specific lock delay code for OPL
1063 	 *
1064 	 * Using staged linear increases in the delay.
1065 	 * The sleep instruction is the preferred method of delay,
1066 	 * but is too large of granularity for the initial backoff.
1067 	 */
1068 
1069 	if (backoff < 100) {
1070 		/*
1071 		 * If desired backoff is long enough,
1072 		 * use sleep for most of it
1073 		 */
1074 		for (cnt = backoff;
1075 		    cnt >= OPL_BOFF_SLEEP;
1076 		    cnt -= OPL_BOFF_SLEEP) {
1077 			cpu_smt_pause();
1078 		}
1079 		/*
1080 		 * spin for small remainder of backoff
1081 		 */
1082 		for (ctr = cnt * OPL_BOFF_SPIN; ctr; ctr--) {
1083 			mutex_delay_default();
1084 		}
1085 	} else {
1086 		/* backoff is large.  Fill it by sleeping */
1087 		delay_start = gethrtime_waitfree();
1088 		cnt = backoff / OPL_BOFF_SLEEP;
1089 		/*
1090 		 * use sleep instructions for delay
1091 		 */
1092 		for (i = 0; i < cnt; i++) {
1093 			cpu_smt_pause();
1094 		}
1095 
1096 		/*
1097 		 * Note: if the other strand executes a sleep instruction,
1098 		 * then the sleep ends immediately with a minimum time of
1099 		 * 42 clocks.  We check gethrtime to insure we have
1100 		 * waited long enough.  And we include both a short
1101 		 * spin loop and a sleep for repeated delay times.
1102 		 */
1103 
1104 		rem_delay = gethrtime_waitfree() - delay_start;
1105 		while (rem_delay < cnt * OPL_BOFF_TM) {
1106 			remcnt = cnt - (rem_delay / OPL_BOFF_TM);
1107 			for (i = 0; i < remcnt; i++) {
1108 				cpu_smt_pause();
1109 				for (ctr = OPL_BOFF_SPIN; ctr; ctr--) {
1110 					mutex_delay_default();
1111 				}
1112 			}
1113 			rem_delay = gethrtime_waitfree() - delay_start;
1114 		}
1115 	}
1116 }
1117 
1118 /*
1119  * The following code implements asynchronous call to XSCF to setup the
1120  * domain node name.
1121  */
1122 
1123 #define	FREE_MSG(m)		kmem_free((m), NM_LEN((m)->len))
1124 
1125 /*
1126  * The following three macros define the all operations on the request
1127  * list we are using here, and hide the details of the list
1128  * implementation from the code.
1129  */
1130 #define	PUSH(m) \
1131 	{ \
1132 		(m)->next = ctl_msg.head; \
1133 		(m)->prev = NULL; \
1134 		if ((m)->next != NULL) \
1135 			(m)->next->prev = (m); \
1136 		ctl_msg.head = (m); \
1137 	}
1138 
1139 #define	REMOVE(m) \
1140 	{ \
1141 		if ((m)->prev != NULL) \
1142 			(m)->prev->next = (m)->next; \
1143 		else \
1144 			ctl_msg.head = (m)->next; \
1145 		if ((m)->next != NULL) \
1146 			(m)->next->prev = (m)->prev; \
1147 	}
1148 
1149 #define	FREE_THE_TAIL(head) \
1150 	{ \
1151 		nm_msg_t *n_msg, *m; \
1152 		m = (head)->next; \
1153 		(head)->next = NULL; \
1154 		while (m != NULL) { \
1155 			n_msg = m->next; \
1156 			FREE_MSG(m); \
1157 			m = n_msg; \
1158 		} \
1159 	}
1160 
1161 #define	SCF_PUTINFO(f, s, p) \
1162 	f(KEY_ESCF, 0x01, 0, s, p)
1163 
1164 #define	PASS2XSCF(m, r)	((r = SCF_PUTINFO(ctl_msg.scf_service_function, \
1165 					    (m)->len, (m)->data)) == 0)
1166 
1167 /*
1168  * The value of the following macro loosely depends on the
1169  * value of the "device busy" timeout used in the SCF driver.
1170  * (See pass2xscf_thread()).
1171  */
1172 #define	SCF_DEVBUSY_DELAY	10
1173 
1174 /*
1175  * The default number of attempts to contact the scf driver
1176  * if we cannot fetch any information about the timeout value
1177  * it uses.
1178  */
1179 
1180 #define	REPEATS		4
1181 
1182 typedef struct nm_msg {
1183 	struct nm_msg *next;
1184 	struct nm_msg *prev;
1185 	int len;
1186 	char data[1];
1187 } nm_msg_t;
1188 
1189 #define	NM_LEN(len)		(sizeof (nm_msg_t) + (len) - 1)
1190 
1191 static struct ctlmsg {
1192 	nm_msg_t	*head;
1193 	nm_msg_t	*now_serving;
1194 	kmutex_t	nm_lock;
1195 	kthread_t	*nmt;
1196 	int		cnt;
1197 	int (*scf_service_function)(uint32_t, uint8_t,
1198 				    uint32_t, uint32_t, void *);
1199 } ctl_msg;
1200 
1201 static void
1202 post_xscf_msg(char *dp, int len)
1203 {
1204 	nm_msg_t *msg;
1205 
1206 	msg = (nm_msg_t *)kmem_zalloc(NM_LEN(len), KM_SLEEP);
1207 
1208 	bcopy(dp, msg->data, len);
1209 	msg->len = len;
1210 
1211 	mutex_enter(&ctl_msg.nm_lock);
1212 	if (ctl_msg.nmt == NULL) {
1213 		ctl_msg.nmt =  thread_create(NULL, 0, pass2xscf_thread,
1214 		    NULL, 0, &p0, TS_RUN, minclsyspri);
1215 	}
1216 
1217 	PUSH(msg);
1218 	ctl_msg.cnt++;
1219 	mutex_exit(&ctl_msg.nm_lock);
1220 }
1221 
1222 static void
1223 pass2xscf_thread()
1224 {
1225 	nm_msg_t *msg;
1226 	int ret;
1227 	uint_t i, msg_sent, xscf_driver_delay;
1228 	static uint_t repeat_cnt;
1229 	uint_t *scf_wait_cnt;
1230 
1231 	mutex_enter(&ctl_msg.nm_lock);
1232 
1233 	/*
1234 	 * Find the address of the SCF put routine if it's not done yet.
1235 	 */
1236 	if (ctl_msg.scf_service_function == NULL) {
1237 		if ((ctl_msg.scf_service_function =
1238 		    (int (*)(uint32_t, uint8_t, uint32_t, uint32_t, void *))
1239 		    modgetsymvalue("scf_service_putinfo", 0)) == NULL) {
1240 			cmn_err(CE_NOTE, "pass2xscf_thread: "
1241 			    "scf_service_putinfo not found\n");
1242 			ctl_msg.nmt = NULL;
1243 			mutex_exit(&ctl_msg.nm_lock);
1244 			return;
1245 		}
1246 	}
1247 
1248 	/*
1249 	 * Calculate the number of attempts to connect XSCF based on the
1250 	 * scf driver delay (which is
1251 	 * SCF_DEVBUSY_DELAY*scf_online_wait_rcnt seconds) and the value
1252 	 * of xscf_connect_delay (the total number of seconds to wait
1253 	 * till xscf get ready.)
1254 	 */
1255 	if (repeat_cnt == 0) {
1256 		if ((scf_wait_cnt =
1257 		    (uint_t *)
1258 		    modgetsymvalue("scf_online_wait_rcnt", 0)) == NULL) {
1259 			repeat_cnt = REPEATS;
1260 		} else {
1261 
1262 			xscf_driver_delay = *scf_wait_cnt *
1263 			    SCF_DEVBUSY_DELAY;
1264 			repeat_cnt = (xscf_connect_delay/xscf_driver_delay) + 1;
1265 		}
1266 	}
1267 
1268 	while (ctl_msg.cnt != 0) {
1269 
1270 		/*
1271 		 * Take the very last request from the queue,
1272 		 */
1273 		ctl_msg.now_serving = ctl_msg.head;
1274 		ASSERT(ctl_msg.now_serving != NULL);
1275 
1276 		/*
1277 		 * and discard all the others if any.
1278 		 */
1279 		FREE_THE_TAIL(ctl_msg.now_serving);
1280 		ctl_msg.cnt = 1;
1281 		mutex_exit(&ctl_msg.nm_lock);
1282 
1283 		/*
1284 		 * Pass the name to XSCF. Note please, we do not hold the
1285 		 * mutex while we are doing this.
1286 		 */
1287 		msg_sent = 0;
1288 		for (i = 0; i < repeat_cnt; i++) {
1289 			if (PASS2XSCF(ctl_msg.now_serving, ret)) {
1290 				msg_sent = 1;
1291 				break;
1292 			} else {
1293 				if (ret != EBUSY) {
1294 					cmn_err(CE_NOTE, "pass2xscf_thread:"
1295 					    " unexpected return code"
1296 					    " from scf_service_putinfo():"
1297 					    " %d\n", ret);
1298 				}
1299 			}
1300 		}
1301 
1302 		if (msg_sent) {
1303 
1304 			/*
1305 			 * Remove the request from the list
1306 			 */
1307 			mutex_enter(&ctl_msg.nm_lock);
1308 			msg = ctl_msg.now_serving;
1309 			ctl_msg.now_serving = NULL;
1310 			REMOVE(msg);
1311 			ctl_msg.cnt--;
1312 			mutex_exit(&ctl_msg.nm_lock);
1313 			FREE_MSG(msg);
1314 		} else {
1315 
1316 			/*
1317 			 * If while we have tried to communicate with
1318 			 * XSCF there were any other requests we are
1319 			 * going to drop this one and take the latest
1320 			 * one.  Otherwise we will try to pass this one
1321 			 * again.
1322 			 */
1323 			cmn_err(CE_NOTE,
1324 			    "pass2xscf_thread: "
1325 			    "scf_service_putinfo "
1326 			    "not responding\n");
1327 		}
1328 		mutex_enter(&ctl_msg.nm_lock);
1329 	}
1330 
1331 	/*
1332 	 * The request queue is empty, exit.
1333 	 */
1334 	ctl_msg.nmt = NULL;
1335 	mutex_exit(&ctl_msg.nm_lock);
1336 }
1337