xref: /titanic_52/usr/src/uts/sun4u/opl/os/opl.c (revision 1915be19b7a1f7c113b706bd54d6ca393e8d8107)
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  * OPL_BOFF_SPIN = base spin loop, roughly one memory reference time
106  * OPL_BOFF_TM = approx nsec for OPL sleep instruction (1600 for OPL-C)
107  * OPL_BOFF_SLEEP = approx number of SPIN iterations to equal one sleep
108  * OPL_BOFF_MAX_SCALE - scaling factor for max backoff based on active cpus
109  * Listed values tuned for 2.15GHz to 2.64GHz systems
110  * Value may change for future systems
111  */
112 #define	OPL_BOFF_SPIN 7
113 #define	OPL_BOFF_SLEEP 4
114 #define	OPL_BOFF_TM 1600
115 #define	OPL_BOFF_MAX_SCALE 8
116 
117 #define	OPL_CLOCK_TICK_THRESHOLD	128
118 #define	OPL_CLOCK_TICK_NCPUS		64
119 
120 extern int	clock_tick_threshold;
121 extern int	clock_tick_ncpus;
122 
123 int
124 set_platform_max_ncpus(void)
125 {
126 	return (OPL_MAX_CPU_PER_BOARD * OPL_MAX_BOARDS);
127 }
128 
129 int
130 set_platform_tsb_spares(void)
131 {
132 	return (MIN(opl_tsb_spares, MAX_UPA));
133 }
134 
135 static void
136 set_model_info()
137 {
138 	extern int ts_dispatch_extended;
139 	char	name[MAXSYSNAME];
140 	int	i;
141 
142 	/*
143 	 * Get model name from the root node.
144 	 *
145 	 * We are using the prom device tree since, at this point,
146 	 * the Solaris device tree is not yet setup.
147 	 */
148 	(void) prom_getprop(prom_rootnode(), "model", (caddr_t)name);
149 
150 	for (i = 0; i < opl_num_models; i++) {
151 		if (strncmp(name, opl_models[i].model_name, MAXSYSNAME) == 0) {
152 			opl_cur_model = &opl_models[i];
153 			break;
154 		}
155 	}
156 
157 	/*
158 	 * If model not matched, it's an unknown model.
159 	 * just return.
160 	 */
161 	if (i == opl_num_models)
162 		return;
163 
164 	if ((opl_cur_model->model_cmds & EXT_DISPATCH_TABLE) &&
165 	    (ts_dispatch_extended == -1)) {
166 		/*
167 		 * Based on a platform model, select a dispatch table.
168 		 * Only DC2 and DC3 systems uses the alternate/extended
169 		 * TS dispatch table.
170 		 * FF1, FF2 and DC1 systems used standard dispatch 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 	return (OPL_MAX_BOARDS);
424 }
425 
426 int
427 plat_max_cpu_units_per_board(void)
428 {
429 	return (OPL_MAX_CPU_PER_BOARD);
430 }
431 
432 int
433 plat_max_mem_units_per_board(void)
434 {
435 	return (OPL_MAX_MEM_UNITS_PER_BOARD);
436 }
437 
438 int
439 plat_max_io_units_per_board(void)
440 {
441 	return (OPL_MAX_IO_UNITS_PER_BOARD);
442 }
443 
444 int
445 plat_max_cmp_units_per_board(void)
446 {
447 	return (OPL_MAX_CMP_UNITS_PER_BOARD);
448 }
449 
450 int
451 plat_max_core_units_per_board(void)
452 {
453 	return (OPL_MAX_CORE_UNITS_PER_BOARD);
454 }
455 
456 int
457 plat_pfn_to_mem_node(pfn_t pfn)
458 {
459 	return (pfn >> mem_node_pfn_shift);
460 }
461 
462 /* ARGSUSED */
463 void
464 plat_build_mem_nodes(prom_memlist_t *list, size_t nelems)
465 {
466 	size_t	elem;
467 	pfn_t	basepfn;
468 	pgcnt_t	npgs;
469 	uint64_t	boundary, ssize;
470 	uint64_t	low, high;
471 
472 	/*
473 	 * OPL mem slices are always aligned on a 256GB boundary.
474 	 */
475 	mem_node_pfn_shift = OPL_MC_MEMBOARD_SHIFT - MMU_PAGESHIFT;
476 	mem_node_physalign = 0;
477 
478 	/*
479 	 * Boot install lists are arranged <addr, len>, <addr, len>, ...
480 	 */
481 	ssize = (1ull << OPL_MC_MEMBOARD_SHIFT);
482 	for (elem = 0; elem < nelems; list++, elem++) {
483 		low  = list->addr;
484 		high = low + list->size;
485 		while (low < high) {
486 			boundary = roundup(low+1, ssize);
487 			boundary = MIN(high, boundary);
488 			basepfn = btop(low);
489 			npgs = btop(boundary - low);
490 			mem_node_add_slice(basepfn, basepfn + npgs - 1);
491 			low = boundary;
492 		}
493 	}
494 }
495 
496 /*
497  * Find the CPU associated with a slice at boot-time.
498  */
499 void
500 plat_fill_mc(pnode_t nodeid)
501 {
502 	int board;
503 	int memnode;
504 	struct {
505 		uint64_t	addr;
506 		uint64_t	size;
507 	} mem_range;
508 
509 	if (prom_getprop(nodeid, "board#", (caddr_t)&board) < 0) {
510 		panic("Can not find board# property in mc node %x", nodeid);
511 	}
512 	if (prom_getprop(nodeid, "sb-mem-ranges", (caddr_t)&mem_range) < 0) {
513 		panic("Can not find sb-mem-ranges property in mc node %x",
514 		    nodeid);
515 	}
516 	memnode = mem_range.addr >> OPL_MC_MEMBOARD_SHIFT;
517 	plat_assign_lgrphand_to_mem_node(board, memnode);
518 }
519 
520 /*
521  * Return the platform handle for the lgroup containing the given CPU
522  *
523  * For OPL, lgroup platform handle == board #.
524  */
525 
526 extern int mpo_disabled;
527 extern lgrp_handle_t lgrp_default_handle;
528 
529 lgrp_handle_t
530 plat_lgrp_cpu_to_hand(processorid_t id)
531 {
532 	lgrp_handle_t plathand;
533 
534 	/*
535 	 * Return the real platform handle for the CPU until
536 	 * such time as we know that MPO should be disabled.
537 	 * At that point, we set the "mpo_disabled" flag to true,
538 	 * and from that point on, return the default handle.
539 	 *
540 	 * By the time we know that MPO should be disabled, the
541 	 * first CPU will have already been added to a leaf
542 	 * lgroup, but that's ok. The common lgroup code will
543 	 * double check that the boot CPU is in the correct place,
544 	 * and in the case where mpo should be disabled, will move
545 	 * it to the root if necessary.
546 	 */
547 	if (mpo_disabled) {
548 		/* If MPO is disabled, return the default (UMA) handle */
549 		plathand = lgrp_default_handle;
550 	} else
551 		plathand = (lgrp_handle_t)LSB_ID(id);
552 	return (plathand);
553 }
554 
555 /*
556  * Platform specific lgroup initialization
557  */
558 void
559 plat_lgrp_init(void)
560 {
561 	extern uint32_t lgrp_expand_proc_thresh;
562 	extern uint32_t lgrp_expand_proc_diff;
563 
564 	/*
565 	 * Set tuneables for the OPL architecture
566 	 *
567 	 * lgrp_expand_proc_thresh is the minimum load on the lgroups
568 	 * this process is currently running on before considering
569 	 * expanding threads to another lgroup.
570 	 *
571 	 * lgrp_expand_proc_diff determines how much less the remote lgroup
572 	 * must be loaded before expanding to it.
573 	 *
574 	 * Since remote latencies can be costly, attempt to keep 3 threads
575 	 * within the same lgroup before expanding to the next lgroup.
576 	 */
577 	lgrp_expand_proc_thresh = LGRP_LOADAVG_THREAD_MAX * 3;
578 	lgrp_expand_proc_diff = LGRP_LOADAVG_THREAD_MAX;
579 }
580 
581 /*
582  * Platform notification of lgroup (re)configuration changes
583  */
584 /*ARGSUSED*/
585 void
586 plat_lgrp_config(lgrp_config_flag_t evt, uintptr_t arg)
587 {
588 	update_membounds_t *umb;
589 	lgrp_config_mem_rename_t lmr;
590 	int sbd, tbd;
591 	lgrp_handle_t hand, shand, thand;
592 	int mnode, snode, tnode;
593 	pfn_t start, end;
594 
595 	if (mpo_disabled)
596 		return;
597 
598 	switch (evt) {
599 
600 	case LGRP_CONFIG_MEM_ADD:
601 		/*
602 		 * Establish the lgroup handle to memnode translation.
603 		 */
604 		umb = (update_membounds_t *)arg;
605 
606 		hand = umb->u_board;
607 		mnode = plat_pfn_to_mem_node(umb->u_base >> MMU_PAGESHIFT);
608 		plat_assign_lgrphand_to_mem_node(hand, mnode);
609 
610 		break;
611 
612 	case LGRP_CONFIG_MEM_DEL:
613 		/*
614 		 * Special handling for possible memory holes.
615 		 */
616 		umb = (update_membounds_t *)arg;
617 		hand = umb->u_board;
618 		if ((mnode = plat_lgrphand_to_mem_node(hand)) != -1) {
619 			if (mem_node_config[mnode].exists) {
620 				start = mem_node_config[mnode].physbase;
621 				end = mem_node_config[mnode].physmax;
622 				mem_node_pre_del_slice(start, end);
623 				mem_node_post_del_slice(start, end, 0);
624 			}
625 		}
626 
627 		break;
628 
629 	case LGRP_CONFIG_MEM_RENAME:
630 		/*
631 		 * During a DR copy-rename operation, all of the memory
632 		 * on one board is moved to another board -- but the
633 		 * addresses/pfns and memnodes don't change. This means
634 		 * the memory has changed locations without changing identity.
635 		 *
636 		 * Source is where we are copying from and target is where we
637 		 * are copying to.  After source memnode is copied to target
638 		 * memnode, the physical addresses of the target memnode are
639 		 * renamed to match what the source memnode had.  Then target
640 		 * memnode can be removed and source memnode can take its
641 		 * place.
642 		 *
643 		 * To do this, swap the lgroup handle to memnode mappings for
644 		 * the boards, so target lgroup will have source memnode and
645 		 * source lgroup will have empty target memnode which is where
646 		 * its memory will go (if any is added to it later).
647 		 *
648 		 * Then source memnode needs to be removed from its lgroup
649 		 * and added to the target lgroup where the memory was living
650 		 * but under a different name/memnode.  The memory was in the
651 		 * target memnode and now lives in the source memnode with
652 		 * different physical addresses even though it is the same
653 		 * memory.
654 		 */
655 		sbd = arg & 0xffff;
656 		tbd = (arg & 0xffff0000) >> 16;
657 		shand = sbd;
658 		thand = tbd;
659 		snode = plat_lgrphand_to_mem_node(shand);
660 		tnode = plat_lgrphand_to_mem_node(thand);
661 
662 		/*
663 		 * Special handling for possible memory holes.
664 		 */
665 		if (tnode != -1 && mem_node_config[tnode].exists) {
666 			start = mem_node_config[tnode].physbase;
667 			end = mem_node_config[tnode].physmax;
668 			mem_node_pre_del_slice(start, end);
669 			mem_node_post_del_slice(start, end, 0);
670 		}
671 
672 		plat_assign_lgrphand_to_mem_node(thand, snode);
673 		plat_assign_lgrphand_to_mem_node(shand, tnode);
674 
675 		lmr.lmem_rename_from = shand;
676 		lmr.lmem_rename_to = thand;
677 
678 		/*
679 		 * Remove source memnode of copy rename from its lgroup
680 		 * and add it to its new target lgroup
681 		 */
682 		lgrp_config(LGRP_CONFIG_MEM_RENAME, (uintptr_t)snode,
683 		    (uintptr_t)&lmr);
684 
685 		break;
686 
687 	default:
688 		break;
689 	}
690 }
691 
692 /*
693  * Return latency between "from" and "to" lgroups
694  *
695  * This latency number can only be used for relative comparison
696  * between lgroups on the running system, cannot be used across platforms,
697  * and may not reflect the actual latency.  It is platform and implementation
698  * specific, so platform gets to decide its value.  It would be nice if the
699  * number was at least proportional to make comparisons more meaningful though.
700  * NOTE: The numbers below are supposed to be load latencies for uncached
701  * memory divided by 10.
702  *
703  */
704 int
705 plat_lgrp_latency(lgrp_handle_t from, lgrp_handle_t to)
706 {
707 	/*
708 	 * Return min remote latency when there are more than two lgroups
709 	 * (root and child) and getting latency between two different lgroups
710 	 * or root is involved
711 	 */
712 	if (lgrp_optimizations() && (from != to ||
713 	    from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE))
714 		return (42);
715 	else
716 		return (35);
717 }
718 
719 /*
720  * Return platform handle for root lgroup
721  */
722 lgrp_handle_t
723 plat_lgrp_root_hand(void)
724 {
725 	if (mpo_disabled)
726 		return (lgrp_default_handle);
727 
728 	return (LGRP_DEFAULT_HANDLE);
729 }
730 
731 /*ARGSUSED*/
732 void
733 plat_freelist_process(int mnode)
734 {
735 }
736 
737 void
738 load_platform_drivers(void)
739 {
740 	(void) i_ddi_attach_pseudo_node("dr");
741 }
742 
743 /*
744  * No platform drivers on this platform
745  */
746 char *platform_module_list[] = {
747 	(char *)0
748 };
749 
750 /*ARGSUSED*/
751 void
752 plat_tod_fault(enum tod_fault_type tod_bad)
753 {
754 }
755 
756 /*ARGSUSED*/
757 void
758 cpu_sgn_update(ushort_t sgn, uchar_t state, uchar_t sub_state, int cpuid)
759 {
760 	static void (*scf_panic_callback)(int);
761 	static void (*scf_shutdown_callback)(int);
762 
763 	/*
764 	 * This is for notifing system panic/shutdown to SCF.
765 	 * In case of shutdown and panic, SCF call back
766 	 * function should be called.
767 	 *  <SCF call back functions>
768 	 *   scf_panic_callb()   : panicsys()->panic_quiesce_hw()
769 	 *   scf_shutdown_callb(): halt() or power_down() or reboot_machine()
770 	 * cpuid should be -1 and state should be SIGST_EXIT.
771 	 */
772 	if (state == SIGST_EXIT && cpuid == -1) {
773 
774 		/*
775 		 * find the symbol for the SCF panic callback routine in driver
776 		 */
777 		if (scf_panic_callback == NULL)
778 			scf_panic_callback = (void (*)(int))
779 			    modgetsymvalue("scf_panic_callb", 0);
780 		if (scf_shutdown_callback == NULL)
781 			scf_shutdown_callback = (void (*)(int))
782 			    modgetsymvalue("scf_shutdown_callb", 0);
783 
784 		switch (sub_state) {
785 		case SIGSUBST_PANIC:
786 			if (scf_panic_callback == NULL) {
787 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
788 				    "scf_panic_callb not found\n");
789 				return;
790 			}
791 			scf_panic_callback(SIGSUBST_PANIC);
792 			break;
793 
794 		case SIGSUBST_HALT:
795 			if (scf_shutdown_callback == NULL) {
796 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
797 				    "scf_shutdown_callb not found\n");
798 				return;
799 			}
800 			scf_shutdown_callback(SIGSUBST_HALT);
801 			break;
802 
803 		case SIGSUBST_ENVIRON:
804 			if (scf_shutdown_callback == NULL) {
805 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
806 				    "scf_shutdown_callb not found\n");
807 				return;
808 			}
809 			scf_shutdown_callback(SIGSUBST_ENVIRON);
810 			break;
811 
812 		case SIGSUBST_REBOOT:
813 			if (scf_shutdown_callback == NULL) {
814 				cmn_err(CE_NOTE, "!cpu_sgn_update: "
815 				    "scf_shutdown_callb not found\n");
816 				return;
817 			}
818 			scf_shutdown_callback(SIGSUBST_REBOOT);
819 			break;
820 		}
821 	}
822 }
823 
824 /*ARGSUSED*/
825 int
826 plat_get_mem_unum(int synd_code, uint64_t flt_addr, int flt_bus_id,
827 	int flt_in_memory, ushort_t flt_status,
828 	char *buf, int buflen, int *lenp)
829 {
830 	/*
831 	 * check if it's a Memory error.
832 	 */
833 	if (flt_in_memory) {
834 		if (opl_get_mem_unum != NULL) {
835 			return (opl_get_mem_unum(synd_code, flt_addr, buf,
836 			    buflen, lenp));
837 		} else {
838 			return (ENOTSUP);
839 		}
840 	} else {
841 		return (ENOTSUP);
842 	}
843 }
844 
845 /*ARGSUSED*/
846 int
847 plat_get_cpu_unum(int cpuid, char *buf, int buflen, int *lenp)
848 {
849 	int	ret = 0;
850 	int	sb;
851 	int	plen;
852 
853 	sb = opl_get_physical_board(LSB_ID(cpuid));
854 	if (sb == -1) {
855 		return (ENXIO);
856 	}
857 
858 	/*
859 	 * opl_cur_model is assigned here
860 	 */
861 	if (opl_cur_model == NULL) {
862 		set_model_info();
863 
864 		/*
865 		 * if not matched, return
866 		 */
867 		if (opl_cur_model == NULL)
868 			return (ENODEV);
869 	}
870 
871 	ASSERT((opl_cur_model - opl_models) == (opl_cur_model->model_type));
872 
873 	switch (opl_cur_model->model_type) {
874 	case FF1:
875 		plen = snprintf(buf, buflen, "/%s/CPUM%d", "MBU_A",
876 		    CHIP_ID(cpuid) / 2);
877 		break;
878 
879 	case FF2:
880 		plen = snprintf(buf, buflen, "/%s/CPUM%d", "MBU_B",
881 		    (CHIP_ID(cpuid) / 2) + (sb * 2));
882 		break;
883 
884 	case DC1:
885 	case DC2:
886 	case DC3:
887 		plen = snprintf(buf, buflen, "/%s%02d/CPUM%d", "CMU", sb,
888 		    CHIP_ID(cpuid));
889 		break;
890 
891 	default:
892 		/* This should never happen */
893 		return (ENODEV);
894 	}
895 
896 	if (plen >= buflen) {
897 		ret = ENOSPC;
898 	} else {
899 		if (lenp)
900 			*lenp = strlen(buf);
901 	}
902 	return (ret);
903 }
904 
905 void
906 plat_nodename_set(void)
907 {
908 	post_xscf_msg((char *)&utsname, sizeof (struct utsname));
909 }
910 
911 caddr_t	efcode_vaddr = NULL;
912 
913 /*
914  * Preallocate enough memory for fcode claims.
915  */
916 
917 caddr_t
918 efcode_alloc(caddr_t alloc_base)
919 {
920 	caddr_t efcode_alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
921 	    MMU_PAGESIZE);
922 	caddr_t vaddr;
923 
924 	/*
925 	 * allocate the physical memory for the Oberon fcode.
926 	 */
927 	if ((vaddr = (caddr_t)BOP_ALLOC(bootops, efcode_alloc_base,
928 	    efcode_size, MMU_PAGESIZE)) == NULL)
929 		cmn_err(CE_PANIC, "Cannot allocate Efcode Memory");
930 
931 	efcode_vaddr = vaddr;
932 
933 	return (efcode_alloc_base + efcode_size);
934 }
935 
936 caddr_t
937 plat_startup_memlist(caddr_t alloc_base)
938 {
939 	caddr_t tmp_alloc_base;
940 
941 	tmp_alloc_base = efcode_alloc(alloc_base);
942 	tmp_alloc_base =
943 	    (caddr_t)roundup((uintptr_t)tmp_alloc_base, ecache_alignsize);
944 	return (tmp_alloc_base);
945 }
946 
947 /* need to forward declare these */
948 static void plat_lock_delay(uint_t);
949 
950 void
951 startup_platform(void)
952 {
953 	if (clock_tick_threshold == 0)
954 		clock_tick_threshold = OPL_CLOCK_TICK_THRESHOLD;
955 	if (clock_tick_ncpus == 0)
956 		clock_tick_ncpus = OPL_CLOCK_TICK_NCPUS;
957 	mutex_lock_delay = plat_lock_delay;
958 	mutex_cap_factor = OPL_BOFF_MAX_SCALE;
959 }
960 
961 void
962 plat_cpuid_to_mmu_ctx_info(processorid_t cpuid, mmu_ctx_info_t *info)
963 {
964 	int	impl;
965 
966 	impl = cpunodes[cpuid].implementation;
967 	if (IS_OLYMPUS_C(impl) || IS_JUPITER(impl)) {
968 		info->mmu_idx = MMU_ID(cpuid);
969 		info->mmu_nctxs = 8192;
970 	} else {
971 		cmn_err(CE_PANIC, "Unknown processor %d", impl);
972 	}
973 }
974 
975 int
976 plat_get_mem_sid(char *unum, char *buf, int buflen, int *lenp)
977 {
978 	if (opl_get_mem_sid == NULL) {
979 		return (ENOTSUP);
980 	}
981 	return (opl_get_mem_sid(unum, buf, buflen, lenp));
982 }
983 
984 int
985 plat_get_mem_offset(uint64_t paddr, uint64_t *offp)
986 {
987 	if (opl_get_mem_offset == NULL) {
988 		return (ENOTSUP);
989 	}
990 	return (opl_get_mem_offset(paddr, offp));
991 }
992 
993 int
994 plat_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *addrp)
995 {
996 	if (opl_get_mem_addr == NULL) {
997 		return (ENOTSUP);
998 	}
999 	return (opl_get_mem_addr(unum, sid, offset, addrp));
1000 }
1001 
1002 void
1003 plat_lock_delay(uint_t backoff)
1004 {
1005 	int i;
1006 	uint_t cnt, remcnt;
1007 	int ctr;
1008 	hrtime_t delay_start, rem_delay;
1009 	/*
1010 	 * Platform specific lock delay code for OPL
1011 	 *
1012 	 * Using staged linear increases in the delay.
1013 	 * The sleep instruction is the preferred method of delay,
1014 	 * but is too large of granularity for the initial backoff.
1015 	 */
1016 
1017 	if (backoff < 100) {
1018 		/*
1019 		 * If desired backoff is long enough,
1020 		 * use sleep for most of it
1021 		 */
1022 		for (cnt = backoff;
1023 		    cnt >= OPL_BOFF_SLEEP;
1024 		    cnt -= OPL_BOFF_SLEEP) {
1025 			cpu_smt_pause();
1026 		}
1027 		/*
1028 		 * spin for small remainder of backoff
1029 		 */
1030 		for (ctr = cnt * OPL_BOFF_SPIN; ctr; ctr--) {
1031 			mutex_delay_default();
1032 		}
1033 	} else {
1034 		/* backoff is large.  Fill it by sleeping */
1035 		delay_start = gethrtime();
1036 		cnt = backoff / OPL_BOFF_SLEEP;
1037 		/*
1038 		 * use sleep instructions for delay
1039 		 */
1040 		for (i = 0; i < cnt; i++) {
1041 			cpu_smt_pause();
1042 		}
1043 
1044 		/*
1045 		 * Note: if the other strand executes a sleep instruction,
1046 		 * then the sleep ends immediately with a minimum time of
1047 		 * 42 clocks.  We check gethrtime to insure we have
1048 		 * waited long enough.  And we include both a short
1049 		 * spin loop and a sleep for repeated delay times.
1050 		 */
1051 
1052 		rem_delay = gethrtime() - delay_start;
1053 		while (rem_delay < cnt * OPL_BOFF_TM) {
1054 			remcnt = cnt - (rem_delay / OPL_BOFF_TM);
1055 			for (i = 0; i < remcnt; i++) {
1056 				cpu_smt_pause();
1057 				for (ctr = OPL_BOFF_SPIN; ctr; ctr--) {
1058 					mutex_delay_default();
1059 				}
1060 			}
1061 			rem_delay = gethrtime() - delay_start;
1062 		}
1063 	}
1064 }
1065 
1066 /*
1067  * The following code implements asynchronous call to XSCF to setup the
1068  * domain node name.
1069  */
1070 
1071 #define	FREE_MSG(m)		kmem_free((m), NM_LEN((m)->len))
1072 
1073 /*
1074  * The following three macros define the all operations on the request
1075  * list we are using here, and hide the details of the list
1076  * implementation from the code.
1077  */
1078 #define	PUSH(m) \
1079 	{ \
1080 		(m)->next = ctl_msg.head; \
1081 		(m)->prev = NULL; \
1082 		if ((m)->next != NULL) \
1083 			(m)->next->prev = (m); \
1084 		ctl_msg.head = (m); \
1085 	}
1086 
1087 #define	REMOVE(m) \
1088 	{ \
1089 		if ((m)->prev != NULL) \
1090 			(m)->prev->next = (m)->next; \
1091 		else \
1092 			ctl_msg.head = (m)->next; \
1093 		if ((m)->next != NULL) \
1094 			(m)->next->prev = (m)->prev; \
1095 	}
1096 
1097 #define	FREE_THE_TAIL(head) \
1098 	{ \
1099 		nm_msg_t *n_msg, *m; \
1100 		m = (head)->next; \
1101 		(head)->next = NULL; \
1102 		while (m != NULL) { \
1103 			n_msg = m->next; \
1104 			FREE_MSG(m); \
1105 			m = n_msg; \
1106 		} \
1107 	}
1108 
1109 #define	SCF_PUTINFO(f, s, p) \
1110 	f(KEY_ESCF, 0x01, 0, s, p)
1111 
1112 #define	PASS2XSCF(m, r)	((r = SCF_PUTINFO(ctl_msg.scf_service_function, \
1113 					    (m)->len, (m)->data)) == 0)
1114 
1115 /*
1116  * The value of the following macro loosely depends on the
1117  * value of the "device busy" timeout used in the SCF driver.
1118  * (See pass2xscf_thread()).
1119  */
1120 #define	SCF_DEVBUSY_DELAY	10
1121 
1122 /*
1123  * The default number of attempts to contact the scf driver
1124  * if we cannot fetch any information about the timeout value
1125  * it uses.
1126  */
1127 
1128 #define	REPEATS		4
1129 
1130 typedef struct nm_msg {
1131 	struct nm_msg *next;
1132 	struct nm_msg *prev;
1133 	int len;
1134 	char data[1];
1135 } nm_msg_t;
1136 
1137 #define	NM_LEN(len)		(sizeof (nm_msg_t) + (len) - 1)
1138 
1139 static struct ctlmsg {
1140 	nm_msg_t	*head;
1141 	nm_msg_t	*now_serving;
1142 	kmutex_t	nm_lock;
1143 	kthread_t	*nmt;
1144 	int		cnt;
1145 	int (*scf_service_function)(uint32_t, uint8_t,
1146 				    uint32_t, uint32_t, void *);
1147 } ctl_msg;
1148 
1149 static void
1150 post_xscf_msg(char *dp, int len)
1151 {
1152 	nm_msg_t *msg;
1153 
1154 	msg = (nm_msg_t *)kmem_zalloc(NM_LEN(len), KM_SLEEP);
1155 
1156 	bcopy(dp, msg->data, len);
1157 	msg->len = len;
1158 
1159 	mutex_enter(&ctl_msg.nm_lock);
1160 	if (ctl_msg.nmt == NULL) {
1161 		ctl_msg.nmt =  thread_create(NULL, 0, pass2xscf_thread,
1162 		    NULL, 0, &p0, TS_RUN, minclsyspri);
1163 	}
1164 
1165 	PUSH(msg);
1166 	ctl_msg.cnt++;
1167 	mutex_exit(&ctl_msg.nm_lock);
1168 }
1169 
1170 static void
1171 pass2xscf_thread()
1172 {
1173 	nm_msg_t *msg;
1174 	int ret;
1175 	uint_t i, msg_sent, xscf_driver_delay;
1176 	static uint_t repeat_cnt;
1177 	uint_t *scf_wait_cnt;
1178 
1179 	mutex_enter(&ctl_msg.nm_lock);
1180 
1181 	/*
1182 	 * Find the address of the SCF put routine if it's not done yet.
1183 	 */
1184 	if (ctl_msg.scf_service_function == NULL) {
1185 		if ((ctl_msg.scf_service_function =
1186 		    (int (*)(uint32_t, uint8_t, uint32_t, uint32_t, void *))
1187 		    modgetsymvalue("scf_service_putinfo", 0)) == NULL) {
1188 			cmn_err(CE_NOTE, "pass2xscf_thread: "
1189 			    "scf_service_putinfo not found\n");
1190 			ctl_msg.nmt = NULL;
1191 			mutex_exit(&ctl_msg.nm_lock);
1192 			return;
1193 		}
1194 	}
1195 
1196 	/*
1197 	 * Calculate the number of attempts to connect XSCF based on the
1198 	 * scf driver delay (which is
1199 	 * SCF_DEVBUSY_DELAY*scf_online_wait_rcnt seconds) and the value
1200 	 * of xscf_connect_delay (the total number of seconds to wait
1201 	 * till xscf get ready.)
1202 	 */
1203 	if (repeat_cnt == 0) {
1204 		if ((scf_wait_cnt =
1205 		    (uint_t *)
1206 		    modgetsymvalue("scf_online_wait_rcnt", 0)) == NULL) {
1207 			repeat_cnt = REPEATS;
1208 		} else {
1209 
1210 			xscf_driver_delay = *scf_wait_cnt *
1211 			    SCF_DEVBUSY_DELAY;
1212 			repeat_cnt = (xscf_connect_delay/xscf_driver_delay) + 1;
1213 		}
1214 	}
1215 
1216 	while (ctl_msg.cnt != 0) {
1217 
1218 		/*
1219 		 * Take the very last request from the queue,
1220 		 */
1221 		ctl_msg.now_serving = ctl_msg.head;
1222 		ASSERT(ctl_msg.now_serving != NULL);
1223 
1224 		/*
1225 		 * and discard all the others if any.
1226 		 */
1227 		FREE_THE_TAIL(ctl_msg.now_serving);
1228 		ctl_msg.cnt = 1;
1229 		mutex_exit(&ctl_msg.nm_lock);
1230 
1231 		/*
1232 		 * Pass the name to XSCF. Note please, we do not hold the
1233 		 * mutex while we are doing this.
1234 		 */
1235 		msg_sent = 0;
1236 		for (i = 0; i < repeat_cnt; i++) {
1237 			if (PASS2XSCF(ctl_msg.now_serving, ret)) {
1238 				msg_sent = 1;
1239 				break;
1240 			} else {
1241 				if (ret != EBUSY) {
1242 					cmn_err(CE_NOTE, "pass2xscf_thread:"
1243 					    " unexpected return code"
1244 					    " from scf_service_putinfo():"
1245 					    " %d\n", ret);
1246 				}
1247 			}
1248 		}
1249 
1250 		if (msg_sent) {
1251 
1252 			/*
1253 			 * Remove the request from the list
1254 			 */
1255 			mutex_enter(&ctl_msg.nm_lock);
1256 			msg = ctl_msg.now_serving;
1257 			ctl_msg.now_serving = NULL;
1258 			REMOVE(msg);
1259 			ctl_msg.cnt--;
1260 			mutex_exit(&ctl_msg.nm_lock);
1261 			FREE_MSG(msg);
1262 		} else {
1263 
1264 			/*
1265 			 * If while we have tried to communicate with
1266 			 * XSCF there were any other requests we are
1267 			 * going to drop this one and take the latest
1268 			 * one.  Otherwise we will try to pass this one
1269 			 * again.
1270 			 */
1271 			cmn_err(CE_NOTE,
1272 			    "pass2xscf_thread: "
1273 			    "scf_service_putinfo "
1274 			    "not responding\n");
1275 		}
1276 		mutex_enter(&ctl_msg.nm_lock);
1277 	}
1278 
1279 	/*
1280 	 * The request queue is empty, exit.
1281 	 */
1282 	ctl_msg.nmt = NULL;
1283 	mutex_exit(&ctl_msg.nm_lock);
1284 }
1285