xref: /titanic_44/usr/src/uts/sun4u/opl/os/opl.c (revision cc7a88b54b4969574f03e1a1225bb13be487f5db)
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 	{ "IKKAKU", OPL_MAX_BOARDS_IKKAKU, IKKAKU, STD_DISPATCH_TABLE },
90 };
91 static	int	opl_num_models = sizeof (opl_models)/sizeof (opl_model_info_t);
92 
93 /*
94  * opl_cur_model
95  */
96 static	opl_model_info_t *opl_cur_model = NULL;
97 
98 static struct memlist *opl_memlist_per_board(struct memlist *ml);
99 static void post_xscf_msg(char *, int);
100 static void pass2xscf_thread();
101 
102 /*
103  * Note FF/DC out-of-order instruction engine takes only a
104  * single cycle to execute each spin loop
105  * for comparison, Panther takes 6 cycles for same loop
106  * OPL_BOFF_SPIN = base spin loop, roughly one memory reference time
107  * OPL_BOFF_TM = approx nsec for OPL sleep instruction (1600 for OPL-C)
108  * OPL_BOFF_SLEEP = approx number of SPIN iterations to equal one sleep
109  * OPL_BOFF_MAX_SCALE - scaling factor for max backoff based on active cpus
110  * Listed values tuned for 2.15GHz to 2.64GHz systems
111  * Value may change for future systems
112  */
113 #define	OPL_BOFF_SPIN 7
114 #define	OPL_BOFF_SLEEP 4
115 #define	OPL_BOFF_TM 1600
116 #define	OPL_BOFF_MAX_SCALE 8
117 
118 #define	OPL_CLOCK_TICK_THRESHOLD	128
119 #define	OPL_CLOCK_TICK_NCPUS		64
120 
121 extern int	clock_tick_threshold;
122 extern int	clock_tick_ncpus;
123 
124 int
125 set_platform_max_ncpus(void)
126 {
127 	return (OPL_MAX_CPU_PER_BOARD * OPL_MAX_BOARDS);
128 }
129 
130 int
131 set_platform_tsb_spares(void)
132 {
133 	return (MIN(opl_tsb_spares, MAX_UPA));
134 }
135 
136 static void
137 set_model_info()
138 {
139 	extern int ts_dispatch_extended;
140 	char	name[MAXSYSNAME];
141 	int	i;
142 
143 	/*
144 	 * Get model name from the root node.
145 	 *
146 	 * We are using the prom device tree since, at this point,
147 	 * the Solaris device tree is not yet setup.
148 	 */
149 	(void) prom_getprop(prom_rootnode(), "model", (caddr_t)name);
150 
151 	for (i = 0; i < opl_num_models; i++) {
152 		if (strncmp(name, opl_models[i].model_name, MAXSYSNAME) == 0) {
153 			opl_cur_model = &opl_models[i];
154 			break;
155 		}
156 	}
157 
158 	/*
159 	 * If model not matched, it's an unknown model.
160 	 * Just return.  It will default to standard dispatch tables.
161 	 */
162 	if (i == opl_num_models)
163 		return;
164 
165 	if ((opl_cur_model->model_cmds & EXT_DISPATCH_TABLE) &&
166 	    (ts_dispatch_extended == -1)) {
167 		/*
168 		 * Based on a platform model, select a dispatch table.
169 		 * Only DC2 and DC3 systems uses the alternate/extended
170 		 * TS dispatch table.
171 		 * IKKAKU, FF1, FF2 and DC1 systems use standard dispatch
172 		 * 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 	case IKKAKU:
894 		plen = snprintf(buf, buflen, "/%s", "MBU_A");
895 		break;
896 
897 	default:
898 		/* This should never happen */
899 		return (ENODEV);
900 	}
901 
902 	if (plen >= buflen) {
903 		ret = ENOSPC;
904 	} else {
905 		if (lenp)
906 			*lenp = strlen(buf);
907 	}
908 	return (ret);
909 }
910 
911 void
912 plat_nodename_set(void)
913 {
914 	post_xscf_msg((char *)&utsname, sizeof (struct utsname));
915 }
916 
917 caddr_t	efcode_vaddr = NULL;
918 
919 /*
920  * Preallocate enough memory for fcode claims.
921  */
922 
923 caddr_t
924 efcode_alloc(caddr_t alloc_base)
925 {
926 	caddr_t efcode_alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
927 	    MMU_PAGESIZE);
928 	caddr_t vaddr;
929 
930 	/*
931 	 * allocate the physical memory for the Oberon fcode.
932 	 */
933 	if ((vaddr = (caddr_t)BOP_ALLOC(bootops, efcode_alloc_base,
934 	    efcode_size, MMU_PAGESIZE)) == NULL)
935 		cmn_err(CE_PANIC, "Cannot allocate Efcode Memory");
936 
937 	efcode_vaddr = vaddr;
938 
939 	return (efcode_alloc_base + efcode_size);
940 }
941 
942 caddr_t
943 plat_startup_memlist(caddr_t alloc_base)
944 {
945 	caddr_t tmp_alloc_base;
946 
947 	tmp_alloc_base = efcode_alloc(alloc_base);
948 	tmp_alloc_base =
949 	    (caddr_t)roundup((uintptr_t)tmp_alloc_base, ecache_alignsize);
950 	return (tmp_alloc_base);
951 }
952 
953 /* need to forward declare these */
954 static void plat_lock_delay(uint_t);
955 
956 void
957 startup_platform(void)
958 {
959 	if (clock_tick_threshold == 0)
960 		clock_tick_threshold = OPL_CLOCK_TICK_THRESHOLD;
961 	if (clock_tick_ncpus == 0)
962 		clock_tick_ncpus = OPL_CLOCK_TICK_NCPUS;
963 	mutex_lock_delay = plat_lock_delay;
964 	mutex_cap_factor = OPL_BOFF_MAX_SCALE;
965 }
966 
967 static uint_t
968 get_mmu_id(processorid_t cpuid)
969 {
970 	int pb = opl_get_physical_board(LSB_ID(cpuid));
971 
972 	if (pb == -1) {
973 		cmn_err(CE_PANIC,
974 		    "opl_get_physical_board failed (cpu %d LSB %u)",
975 		    cpuid, LSB_ID(cpuid));
976 	}
977 	return (pb * OPL_MAX_COREID_PER_BOARD) + (CHIP_ID(cpuid) *
978 	    OPL_MAX_COREID_PER_CMP) + CORE_ID(cpuid);
979 }
980 
981 void
982 plat_cpuid_to_mmu_ctx_info(processorid_t cpuid, mmu_ctx_info_t *info)
983 {
984 	int	impl;
985 
986 	impl = cpunodes[cpuid].implementation;
987 	if (IS_OLYMPUS_C(impl) || IS_JUPITER(impl)) {
988 		info->mmu_idx = get_mmu_id(cpuid);
989 		info->mmu_nctxs = 8192;
990 	} else {
991 		cmn_err(CE_PANIC, "Unknown processor %d", impl);
992 	}
993 }
994 
995 int
996 plat_get_mem_sid(char *unum, char *buf, int buflen, int *lenp)
997 {
998 	if (opl_get_mem_sid == NULL) {
999 		return (ENOTSUP);
1000 	}
1001 	return (opl_get_mem_sid(unum, buf, buflen, lenp));
1002 }
1003 
1004 int
1005 plat_get_mem_offset(uint64_t paddr, uint64_t *offp)
1006 {
1007 	if (opl_get_mem_offset == NULL) {
1008 		return (ENOTSUP);
1009 	}
1010 	return (opl_get_mem_offset(paddr, offp));
1011 }
1012 
1013 int
1014 plat_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *addrp)
1015 {
1016 	if (opl_get_mem_addr == NULL) {
1017 		return (ENOTSUP);
1018 	}
1019 	return (opl_get_mem_addr(unum, sid, offset, addrp));
1020 }
1021 
1022 void
1023 plat_lock_delay(uint_t backoff)
1024 {
1025 	int i;
1026 	uint_t cnt, remcnt;
1027 	int ctr;
1028 	hrtime_t delay_start, rem_delay;
1029 	/*
1030 	 * Platform specific lock delay code for OPL
1031 	 *
1032 	 * Using staged linear increases in the delay.
1033 	 * The sleep instruction is the preferred method of delay,
1034 	 * but is too large of granularity for the initial backoff.
1035 	 */
1036 
1037 	if (backoff < 100) {
1038 		/*
1039 		 * If desired backoff is long enough,
1040 		 * use sleep for most of it
1041 		 */
1042 		for (cnt = backoff;
1043 		    cnt >= OPL_BOFF_SLEEP;
1044 		    cnt -= OPL_BOFF_SLEEP) {
1045 			cpu_smt_pause();
1046 		}
1047 		/*
1048 		 * spin for small remainder of backoff
1049 		 */
1050 		for (ctr = cnt * OPL_BOFF_SPIN; ctr; ctr--) {
1051 			mutex_delay_default();
1052 		}
1053 	} else {
1054 		/* backoff is large.  Fill it by sleeping */
1055 		delay_start = gethrtime();
1056 		cnt = backoff / OPL_BOFF_SLEEP;
1057 		/*
1058 		 * use sleep instructions for delay
1059 		 */
1060 		for (i = 0; i < cnt; i++) {
1061 			cpu_smt_pause();
1062 		}
1063 
1064 		/*
1065 		 * Note: if the other strand executes a sleep instruction,
1066 		 * then the sleep ends immediately with a minimum time of
1067 		 * 42 clocks.  We check gethrtime to insure we have
1068 		 * waited long enough.  And we include both a short
1069 		 * spin loop and a sleep for repeated delay times.
1070 		 */
1071 
1072 		rem_delay = gethrtime() - delay_start;
1073 		while (rem_delay < cnt * OPL_BOFF_TM) {
1074 			remcnt = cnt - (rem_delay / OPL_BOFF_TM);
1075 			for (i = 0; i < remcnt; i++) {
1076 				cpu_smt_pause();
1077 				for (ctr = OPL_BOFF_SPIN; ctr; ctr--) {
1078 					mutex_delay_default();
1079 				}
1080 			}
1081 			rem_delay = gethrtime() - delay_start;
1082 		}
1083 	}
1084 }
1085 
1086 /*
1087  * The following code implements asynchronous call to XSCF to setup the
1088  * domain node name.
1089  */
1090 
1091 #define	FREE_MSG(m)		kmem_free((m), NM_LEN((m)->len))
1092 
1093 /*
1094  * The following three macros define the all operations on the request
1095  * list we are using here, and hide the details of the list
1096  * implementation from the code.
1097  */
1098 #define	PUSH(m) \
1099 	{ \
1100 		(m)->next = ctl_msg.head; \
1101 		(m)->prev = NULL; \
1102 		if ((m)->next != NULL) \
1103 			(m)->next->prev = (m); \
1104 		ctl_msg.head = (m); \
1105 	}
1106 
1107 #define	REMOVE(m) \
1108 	{ \
1109 		if ((m)->prev != NULL) \
1110 			(m)->prev->next = (m)->next; \
1111 		else \
1112 			ctl_msg.head = (m)->next; \
1113 		if ((m)->next != NULL) \
1114 			(m)->next->prev = (m)->prev; \
1115 	}
1116 
1117 #define	FREE_THE_TAIL(head) \
1118 	{ \
1119 		nm_msg_t *n_msg, *m; \
1120 		m = (head)->next; \
1121 		(head)->next = NULL; \
1122 		while (m != NULL) { \
1123 			n_msg = m->next; \
1124 			FREE_MSG(m); \
1125 			m = n_msg; \
1126 		} \
1127 	}
1128 
1129 #define	SCF_PUTINFO(f, s, p) \
1130 	f(KEY_ESCF, 0x01, 0, s, p)
1131 
1132 #define	PASS2XSCF(m, r)	((r = SCF_PUTINFO(ctl_msg.scf_service_function, \
1133 					    (m)->len, (m)->data)) == 0)
1134 
1135 /*
1136  * The value of the following macro loosely depends on the
1137  * value of the "device busy" timeout used in the SCF driver.
1138  * (See pass2xscf_thread()).
1139  */
1140 #define	SCF_DEVBUSY_DELAY	10
1141 
1142 /*
1143  * The default number of attempts to contact the scf driver
1144  * if we cannot fetch any information about the timeout value
1145  * it uses.
1146  */
1147 
1148 #define	REPEATS		4
1149 
1150 typedef struct nm_msg {
1151 	struct nm_msg *next;
1152 	struct nm_msg *prev;
1153 	int len;
1154 	char data[1];
1155 } nm_msg_t;
1156 
1157 #define	NM_LEN(len)		(sizeof (nm_msg_t) + (len) - 1)
1158 
1159 static struct ctlmsg {
1160 	nm_msg_t	*head;
1161 	nm_msg_t	*now_serving;
1162 	kmutex_t	nm_lock;
1163 	kthread_t	*nmt;
1164 	int		cnt;
1165 	int (*scf_service_function)(uint32_t, uint8_t,
1166 				    uint32_t, uint32_t, void *);
1167 } ctl_msg;
1168 
1169 static void
1170 post_xscf_msg(char *dp, int len)
1171 {
1172 	nm_msg_t *msg;
1173 
1174 	msg = (nm_msg_t *)kmem_zalloc(NM_LEN(len), KM_SLEEP);
1175 
1176 	bcopy(dp, msg->data, len);
1177 	msg->len = len;
1178 
1179 	mutex_enter(&ctl_msg.nm_lock);
1180 	if (ctl_msg.nmt == NULL) {
1181 		ctl_msg.nmt =  thread_create(NULL, 0, pass2xscf_thread,
1182 		    NULL, 0, &p0, TS_RUN, minclsyspri);
1183 	}
1184 
1185 	PUSH(msg);
1186 	ctl_msg.cnt++;
1187 	mutex_exit(&ctl_msg.nm_lock);
1188 }
1189 
1190 static void
1191 pass2xscf_thread()
1192 {
1193 	nm_msg_t *msg;
1194 	int ret;
1195 	uint_t i, msg_sent, xscf_driver_delay;
1196 	static uint_t repeat_cnt;
1197 	uint_t *scf_wait_cnt;
1198 
1199 	mutex_enter(&ctl_msg.nm_lock);
1200 
1201 	/*
1202 	 * Find the address of the SCF put routine if it's not done yet.
1203 	 */
1204 	if (ctl_msg.scf_service_function == NULL) {
1205 		if ((ctl_msg.scf_service_function =
1206 		    (int (*)(uint32_t, uint8_t, uint32_t, uint32_t, void *))
1207 		    modgetsymvalue("scf_service_putinfo", 0)) == NULL) {
1208 			cmn_err(CE_NOTE, "pass2xscf_thread: "
1209 			    "scf_service_putinfo not found\n");
1210 			ctl_msg.nmt = NULL;
1211 			mutex_exit(&ctl_msg.nm_lock);
1212 			return;
1213 		}
1214 	}
1215 
1216 	/*
1217 	 * Calculate the number of attempts to connect XSCF based on the
1218 	 * scf driver delay (which is
1219 	 * SCF_DEVBUSY_DELAY*scf_online_wait_rcnt seconds) and the value
1220 	 * of xscf_connect_delay (the total number of seconds to wait
1221 	 * till xscf get ready.)
1222 	 */
1223 	if (repeat_cnt == 0) {
1224 		if ((scf_wait_cnt =
1225 		    (uint_t *)
1226 		    modgetsymvalue("scf_online_wait_rcnt", 0)) == NULL) {
1227 			repeat_cnt = REPEATS;
1228 		} else {
1229 
1230 			xscf_driver_delay = *scf_wait_cnt *
1231 			    SCF_DEVBUSY_DELAY;
1232 			repeat_cnt = (xscf_connect_delay/xscf_driver_delay) + 1;
1233 		}
1234 	}
1235 
1236 	while (ctl_msg.cnt != 0) {
1237 
1238 		/*
1239 		 * Take the very last request from the queue,
1240 		 */
1241 		ctl_msg.now_serving = ctl_msg.head;
1242 		ASSERT(ctl_msg.now_serving != NULL);
1243 
1244 		/*
1245 		 * and discard all the others if any.
1246 		 */
1247 		FREE_THE_TAIL(ctl_msg.now_serving);
1248 		ctl_msg.cnt = 1;
1249 		mutex_exit(&ctl_msg.nm_lock);
1250 
1251 		/*
1252 		 * Pass the name to XSCF. Note please, we do not hold the
1253 		 * mutex while we are doing this.
1254 		 */
1255 		msg_sent = 0;
1256 		for (i = 0; i < repeat_cnt; i++) {
1257 			if (PASS2XSCF(ctl_msg.now_serving, ret)) {
1258 				msg_sent = 1;
1259 				break;
1260 			} else {
1261 				if (ret != EBUSY) {
1262 					cmn_err(CE_NOTE, "pass2xscf_thread:"
1263 					    " unexpected return code"
1264 					    " from scf_service_putinfo():"
1265 					    " %d\n", ret);
1266 				}
1267 			}
1268 		}
1269 
1270 		if (msg_sent) {
1271 
1272 			/*
1273 			 * Remove the request from the list
1274 			 */
1275 			mutex_enter(&ctl_msg.nm_lock);
1276 			msg = ctl_msg.now_serving;
1277 			ctl_msg.now_serving = NULL;
1278 			REMOVE(msg);
1279 			ctl_msg.cnt--;
1280 			mutex_exit(&ctl_msg.nm_lock);
1281 			FREE_MSG(msg);
1282 		} else {
1283 
1284 			/*
1285 			 * If while we have tried to communicate with
1286 			 * XSCF there were any other requests we are
1287 			 * going to drop this one and take the latest
1288 			 * one.  Otherwise we will try to pass this one
1289 			 * again.
1290 			 */
1291 			cmn_err(CE_NOTE,
1292 			    "pass2xscf_thread: "
1293 			    "scf_service_putinfo "
1294 			    "not responding\n");
1295 		}
1296 		mutex_enter(&ctl_msg.nm_lock);
1297 	}
1298 
1299 	/*
1300 	 * The request queue is empty, exit.
1301 	 */
1302 	ctl_msg.nmt = NULL;
1303 	mutex_exit(&ctl_msg.nm_lock);
1304 }
1305