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