xref: /illumos-gate/usr/src/uts/sun4v/os/fillsysinfo.c (revision c2aa8c918a0c67f7fd93724a31efac84968fc12c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/errno.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/cpu.h>
32 #include <sys/cpuvar.h>
33 #include <sys/clock.h>
34 #include <sys/promif.h>
35 #include <sys/promimpl.h>
36 #include <sys/systm.h>
37 #include <sys/machsystm.h>
38 #include <sys/debug.h>
39 #include <sys/sunddi.h>
40 #include <sys/modctl.h>
41 #include <sys/cpu_module.h>
42 #include <sys/kobj.h>
43 #include <sys/cmp.h>
44 #include <sys/async.h>
45 #include <vm/page.h>
46 #include <vm/hat_sfmmu.h>
47 #include <sys/sysmacros.h>
48 #include <sys/mach_descrip.h>
49 #include <sys/mdesc.h>
50 #include <sys/archsystm.h>
51 #include <sys/error.h>
52 #include <sys/mmu.h>
53 #include <sys/bitmap.h>
54 #include <sys/intreg.h>
55 
56 struct cpu_node cpunodes[NCPU];
57 
58 uint64_t cpu_q_entries;
59 uint64_t dev_q_entries;
60 uint64_t cpu_rq_entries;
61 uint64_t cpu_nrq_entries;
62 uint64_t ncpu_guest_max;
63 
64 void fill_cpu(md_t *, mde_cookie_t);
65 
66 static uint64_t get_mmu_ctx_bits(md_t *, mde_cookie_t);
67 static uint64_t get_mmu_tsbs(md_t *, mde_cookie_t);
68 static uint64_t	get_mmu_shcontexts(md_t *, mde_cookie_t);
69 static uint64_t get_cpu_pagesizes(md_t *, mde_cookie_t);
70 static char *construct_isalist(md_t *, mde_cookie_t, char **);
71 static void init_md_broken(md_t *, mde_cookie_t *);
72 static int get_l2_cache_info(md_t *, mde_cookie_t, uint64_t *, uint64_t *,
73     uint64_t *);
74 static void get_q_sizes(md_t *, mde_cookie_t);
75 static void get_va_bits(md_t *, mde_cookie_t);
76 static size_t get_ra_limit(md_t *);
77 
78 uint64_t	system_clock_freq;
79 uint_t		niommu_tsbs = 0;
80 
81 /* prevent compilation with VAC defined */
82 #ifdef VAC
83 #error "The sun4v architecture does not support VAC"
84 #endif
85 
86 #define	S_VAC_SIZE	MMU_PAGESIZE
87 #define	S_VAC_SHIFT	MMU_PAGESHIFT
88 
89 int		vac_size = S_VAC_SIZE;
90 uint_t		vac_mask = MMU_PAGEMASK & (S_VAC_SIZE - 1);
91 int		vac_shift = S_VAC_SHIFT;
92 uintptr_t	shm_alignment = S_VAC_SIZE;
93 
94 void
95 map_wellknown_devices()
96 {
97 }
98 
99 void
100 fill_cpu(md_t *mdp, mde_cookie_t cpuc)
101 {
102 	struct cpu_node *cpunode;
103 	uint64_t cpuid;
104 	uint64_t clk_freq;
105 	char *namebuf;
106 	char *namebufp;
107 	int namelen;
108 	uint64_t associativity = 0, linesize = 0, size = 0;
109 
110 	if (md_get_prop_val(mdp, cpuc, "id", &cpuid)) {
111 		return;
112 	}
113 
114 	/* All out-of-range cpus will be stopped later. */
115 	if (cpuid >= NCPU) {
116 		cmn_err(CE_CONT, "fill_cpu: out of range cpuid %ld - "
117 		    "cpu excluded from configuration\n", cpuid);
118 
119 		return;
120 	}
121 
122 	cpunode = &cpunodes[cpuid];
123 	cpunode->cpuid = (int)cpuid;
124 	cpunode->device_id = cpuid;
125 
126 	if (sizeof (cpunode->fru_fmri) > strlen(CPU_FRU_FMRI))
127 		(void) strcpy(cpunode->fru_fmri, CPU_FRU_FMRI);
128 
129 	if (md_get_prop_data(mdp, cpuc,
130 	    "compatible", (uint8_t **)&namebuf, &namelen)) {
131 		cmn_err(CE_PANIC, "fill_cpu: Cannot read compatible "
132 		    "property");
133 	}
134 	namebufp = namebuf;
135 	if (strncmp(namebufp, "SUNW,", 5) == 0)
136 		namebufp += 5;
137 	if (strlen(namebufp) > sizeof (cpunode->name))
138 		cmn_err(CE_PANIC, "Compatible property too big to "
139 		    "fit into the cpunode name buffer");
140 	(void) strcpy(cpunode->name, namebufp);
141 
142 	if (md_get_prop_val(mdp, cpuc,
143 	    "clock-frequency", &clk_freq)) {
144 			clk_freq = 0;
145 	}
146 	cpunode->clock_freq = clk_freq;
147 
148 	ASSERT(cpunode->clock_freq != 0);
149 	/*
150 	 * Compute scaling factor based on rate of %tick. This is used
151 	 * to convert from ticks derived from %tick to nanoseconds. See
152 	 * comment in sun4u/sys/clock.h for details.
153 	 */
154 	cpunode->tick_nsec_scale = (uint_t)(((uint64_t)NANOSEC <<
155 	    (32 - TICK_NSEC_SHIFT)) / cpunode->clock_freq);
156 
157 	/*
158 	 * The nodeid is not used in sun4v at all. Setting it
159 	 * to positive value to make starting of slave CPUs
160 	 * code happy.
161 	 */
162 	cpunode->nodeid = cpuid + 1;
163 
164 	/*
165 	 * Obtain the L2 cache information from MD.
166 	 * If "Cache" node exists, then set L2 cache properties
167 	 * as read from MD.
168 	 * If node does not exists, then set the L2 cache properties
169 	 * in individual CPU module.
170 	 */
171 	if ((!get_l2_cache_info(mdp, cpuc,
172 	    &associativity, &size, &linesize)) ||
173 	    associativity == 0 || size == 0 || linesize == 0) {
174 		cpu_fiximp(cpunode);
175 	} else {
176 		/*
177 		 * Do not expect L2 cache properties to be bigger
178 		 * than 32-bit quantity.
179 		 */
180 		cpunode->ecache_associativity = (int)associativity;
181 		cpunode->ecache_size = (int)size;
182 		cpunode->ecache_linesize = (int)linesize;
183 	}
184 
185 	cpunode->ecache_setsize =
186 	    cpunode->ecache_size / cpunode->ecache_associativity;
187 
188 		/*
189 		 * Start off by assigning the cpu id as the default
190 		 * mapping index.
191 		 */
192 
193 	cpunode->exec_unit_mapping = NO_EU_MAPPING_FOUND;
194 
195 	if (ecache_setsize == 0)
196 		ecache_setsize = cpunode->ecache_setsize;
197 	if (ecache_alignsize == 0)
198 		ecache_alignsize = cpunode->ecache_linesize;
199 
200 }
201 
202 void
203 empty_cpu(int cpuid)
204 {
205 	bzero(&cpunodes[cpuid], sizeof (struct cpu_node));
206 }
207 
208 void
209 setup_exec_unit_mappings(md_t *mdp)
210 {
211 	uint64_t num, num_eunits;
212 	mde_cookie_t cpus_node;
213 	mde_cookie_t *node, *eunit;
214 	int idx, i, j;
215 	processorid_t cpuid;
216 	char *eunit_name = broken_md_flag ? "exec_unit" : "exec-unit";
217 	enum eu_type { INTEGER, FPU } etype;
218 
219 	/*
220 	 * Find the cpu integer exec units - and
221 	 * setup the mappings appropriately.
222 	 */
223 	num = md_alloc_scan_dag(mdp, md_root_node(mdp), "cpus", "fwd", &node);
224 	if (num < 1)
225 		cmn_err(CE_PANIC, "No cpus node in machine description");
226 	if (num > 1)
227 		cmn_err(CE_PANIC, "More than 1 cpus node in machine"
228 		    " description");
229 
230 	cpus_node = node[0];
231 	md_free_scan_dag(mdp, &node);
232 
233 	num_eunits = md_alloc_scan_dag(mdp, cpus_node, eunit_name,
234 	    "fwd", &eunit);
235 	if (num_eunits > 0) {
236 		char *int_str = broken_md_flag ? "int" : "integer";
237 		char *fpu_str = "fp";
238 
239 		/* Spin through and find all the integer exec units */
240 		for (i = 0; i < num_eunits; i++) {
241 			char *p;
242 			char *val;
243 			int vallen;
244 			uint64_t lcpuid;
245 
246 			/* ignore nodes with no type */
247 			if (md_get_prop_data(mdp, eunit[i], "type",
248 			    (uint8_t **)&val, &vallen))
249 				continue;
250 
251 			for (p = val; *p != '\0'; p += strlen(p) + 1) {
252 				if (strcmp(p, int_str) == 0) {
253 					etype = INTEGER;
254 					goto found;
255 				}
256 				if (strcmp(p, fpu_str) == 0) {
257 					etype = FPU;
258 					goto found;
259 				}
260 			}
261 
262 			continue;
263 found:
264 			idx = NCPU + i;
265 			/*
266 			 * find the cpus attached to this EU and
267 			 * update their mapping indices
268 			 */
269 			num = md_alloc_scan_dag(mdp, eunit[i], "cpu",
270 			    "back", &node);
271 
272 			if (num < 1)
273 				cmn_err(CE_PANIC, "exec-unit node in MD"
274 				    " not attached to a cpu node");
275 
276 			for (j = 0; j < num; j++) {
277 				if (md_get_prop_val(mdp, node[j], "id",
278 				    &lcpuid))
279 					continue;
280 				if (lcpuid >= NCPU)
281 					continue;
282 				cpuid = (processorid_t)lcpuid;
283 				switch (etype) {
284 				case INTEGER:
285 					cpunodes[cpuid].exec_unit_mapping = idx;
286 					break;
287 				case FPU:
288 					cpunodes[cpuid].fpu_mapping = idx;
289 					break;
290 				}
291 			}
292 			md_free_scan_dag(mdp, &node);
293 		}
294 
295 
296 		md_free_scan_dag(mdp, &eunit);
297 	}
298 }
299 
300 /*
301  * All the common setup of sun4v CPU modules is done by this routine.
302  */
303 void
304 cpu_setup_common(char **cpu_module_isa_set)
305 {
306 	extern int mmu_exported_pagesize_mask;
307 	int nocpus, i;
308 	size_t ra_limit;
309 	mde_cookie_t *cpulist;
310 	md_t *mdp;
311 
312 	if ((mdp = md_get_handle()) == NULL)
313 		cmn_err(CE_PANIC, "Unable to initialize machine description");
314 
315 	nocpus = md_alloc_scan_dag(mdp,
316 	    md_root_node(mdp), "cpu", "fwd", &cpulist);
317 	if (nocpus < 1) {
318 		cmn_err(CE_PANIC, "cpu_common_setup: cpulist allocation "
319 		    "failed or incorrect number of CPUs in MD");
320 	}
321 
322 	init_md_broken(mdp, cpulist);
323 
324 	if (use_page_coloring) {
325 		do_pg_coloring = 1;
326 	}
327 
328 	/*
329 	 * Get the valid mmu page sizes mask, Q sizes and isalist/r
330 	 * from the MD for the first available CPU in cpulist.
331 	 *
332 	 * Do not expect the MMU page sizes mask to be more than 32-bit.
333 	 */
334 	mmu_exported_pagesize_mask = (int)get_cpu_pagesizes(mdp, cpulist[0]);
335 
336 	/*
337 	 * Get the number of contexts and tsbs supported.
338 	 */
339 	if (get_mmu_shcontexts(mdp, cpulist[0]) >= MIN_NSHCONTEXTS &&
340 	    get_mmu_tsbs(mdp, cpulist[0]) >= MIN_NTSBS) {
341 		shctx_on = 1;
342 	}
343 
344 	for (i = 0; i < nocpus; i++)
345 		fill_cpu(mdp, cpulist[i]);
346 
347 	setup_exec_unit_mappings(mdp);
348 
349 	/*
350 	 * If MD is broken then append the passed ISA set,
351 	 * otherwise trust the MD.
352 	 */
353 
354 	if (broken_md_flag)
355 		isa_list = construct_isalist(mdp, cpulist[0],
356 		    cpu_module_isa_set);
357 	else
358 		isa_list = construct_isalist(mdp, cpulist[0], NULL);
359 
360 	get_q_sizes(mdp, cpulist[0]);
361 
362 	get_va_bits(mdp, cpulist[0]);
363 
364 	/*
365 	 * ra_limit is the highest real address in the machine.
366 	 */
367 	ra_limit = get_ra_limit(mdp);
368 
369 	md_free_scan_dag(mdp, &cpulist);
370 
371 	(void) md_fini_handle(mdp);
372 
373 	/*
374 	 * Block stores invalidate all pages of the d$ so pagecopy
375 	 * et. al. do not need virtual translations with virtual
376 	 * coloring taken into consideration.
377 	 */
378 	pp_consistent_coloring = 0;
379 
380 	/*
381 	 * The kpm mapping window.
382 	 * kpm_size:
383 	 *	The size of a single kpm range.
384 	 *	The overall size will be: kpm_size * vac_colors.
385 	 * kpm_vbase:
386 	 *	The virtual start address of the kpm range within the kernel
387 	 *	virtual address space. kpm_vbase has to be kpm_size aligned.
388 	 */
389 
390 	/*
391 	 * Make kpm_vbase, kpm_size aligned to kpm_size_shift.
392 	 * To do this find the nearest power of 2 size that the
393 	 * actual ra_limit fits within.
394 	 * If it is an even power of two use that, otherwise use the
395 	 * next power of two larger than ra_limit.
396 	 */
397 
398 	ASSERT(ra_limit != 0);
399 
400 	kpm_size_shift = (ra_limit & (ra_limit - 1)) != 0 ?
401 	    highbit(ra_limit) : highbit(ra_limit) - 1;
402 
403 	/*
404 	 * No virtual caches on sun4v so size matches size shift
405 	 */
406 	kpm_size = 1ul << kpm_size_shift;
407 
408 	if (va_bits < VA_ADDRESS_SPACE_BITS) {
409 		/*
410 		 * In case of VA hole
411 		 * kpm_base = hole_end + 1TB
412 		 * Starting 1TB beyond where VA hole ends because on Niagara
413 		 * processor software must not use pages within 4GB of the
414 		 * VA hole as instruction pages to avoid problems with
415 		 * prefetching into the VA hole.
416 		 */
417 		kpm_vbase = (caddr_t)((0ull - (1ull << (va_bits - 1))) +
418 		    (1ull << 40));
419 	} else {		/* Number of VA bits 64 ... no VA hole */
420 		kpm_vbase = (caddr_t)0x8000000000000000ull;	/* 8 EB */
421 	}
422 
423 	/*
424 	 * The traptrace code uses either %tick or %stick for
425 	 * timestamping.  The sun4v require use of %stick.
426 	 */
427 	traptrace_use_stick = 1;
428 }
429 
430 /*
431  * Get the nctxs from MD. If absent panic.
432  */
433 static uint64_t
434 get_mmu_ctx_bits(md_t *mdp, mde_cookie_t cpu_node_cookie)
435 {
436 	uint64_t ctx_bits;
437 
438 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-#context-bits",
439 	    &ctx_bits))
440 		ctx_bits = 0;
441 
442 	if (ctx_bits < MIN_NCTXS_BITS || ctx_bits > MAX_NCTXS_BITS)
443 		cmn_err(CE_PANIC, "Incorrect %ld number of contexts bits "
444 		    "returned by MD", ctx_bits);
445 
446 	return (ctx_bits);
447 }
448 
449 /*
450  * Get the number of tsbs from MD. If absent the default value is 0.
451  */
452 static uint64_t
453 get_mmu_tsbs(md_t *mdp, mde_cookie_t cpu_node_cookie)
454 {
455 	uint64_t number_tsbs;
456 
457 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-max-#tsbs",
458 	    &number_tsbs))
459 		number_tsbs = 0;
460 
461 	return (number_tsbs);
462 }
463 
464 /*
465  * Get the number of shared contexts from MD. This property more accurately
466  * describes the total number of contexts available, not just "shared contexts".
467  * If absent the default value is 1,
468  *
469  */
470 static uint64_t
471 get_mmu_shcontexts(md_t *mdp, mde_cookie_t cpu_node_cookie)
472 {
473 	uint64_t number_contexts;
474 
475 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-#shared-contexts",
476 	    &number_contexts))
477 		number_contexts = 0;
478 
479 	return (number_contexts);
480 }
481 
482 /*
483  * Initalize supported page sizes information.
484  * Set to 0, if the page sizes mask information is absent in MD.
485  */
486 static uint64_t
487 get_cpu_pagesizes(md_t *mdp, mde_cookie_t cpu_node_cookie)
488 {
489 	uint64_t mmu_page_size_list;
490 
491 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-page-size-list",
492 	    &mmu_page_size_list))
493 		mmu_page_size_list = 0;
494 
495 	if (mmu_page_size_list == 0 || mmu_page_size_list > MAX_PAGESIZE_MASK)
496 		cmn_err(CE_PANIC, "Incorrect 0x%lx pagesize mask returned"
497 		    "by MD", mmu_page_size_list);
498 
499 	return (mmu_page_size_list);
500 }
501 
502 /*
503  * This routine gets the isalist information from MD and appends
504  * the CPU module ISA set if required.
505  */
506 static char *
507 construct_isalist(md_t *mdp, mde_cookie_t cpu_node_cookie,
508     char **cpu_module_isa_set)
509 {
510 	extern int at_flags;
511 	char *md_isalist;
512 	int md_isalen;
513 	char *isabuf;
514 	int isalen;
515 	char **isa_set;
516 	char *p, *q;
517 	int cpu_module_isalen = 0, found = 0;
518 
519 	(void) md_get_prop_data(mdp, cpu_node_cookie,
520 	    "isalist", (uint8_t **)&isabuf, &isalen);
521 
522 	/*
523 	 * We support binaries for all the cpus that have shipped so far.
524 	 * The kernel emulates instructions that are not supported by hardware.
525 	 */
526 	at_flags = EF_SPARC_SUN_US3 | EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
527 
528 	/*
529 	 * Construct the space separated isa_list.
530 	 */
531 	if (cpu_module_isa_set != NULL) {
532 		for (isa_set = cpu_module_isa_set; *isa_set != NULL;
533 		    isa_set++) {
534 			cpu_module_isalen += strlen(*isa_set);
535 			cpu_module_isalen++;	/* for space character */
536 		}
537 	}
538 
539 	/*
540 	 * Allocate the buffer of MD isa buffer length + CPU module
541 	 * isa buffer length.
542 	 */
543 	md_isalen = isalen + cpu_module_isalen + 2;
544 	md_isalist = (char *)prom_alloc((caddr_t)0, md_isalen, 0);
545 	if (md_isalist == NULL)
546 		cmn_err(CE_PANIC, "construct_isalist: Allocation failed for "
547 		    "md_isalist");
548 
549 	md_isalist[0] = '\0'; /* create an empty string to start */
550 	for (p = isabuf, q = p + isalen; p < q; p += strlen(p) + 1) {
551 		(void) strlcat(md_isalist, p, md_isalen);
552 		(void) strcat(md_isalist, " ");
553 	}
554 
555 	/*
556 	 * Check if the isa_set is present in isalist returned by MD.
557 	 * If yes, then no need to append it, if no then append it to
558 	 * isalist returned by MD.
559 	 */
560 	if (cpu_module_isa_set != NULL) {
561 		for (isa_set = cpu_module_isa_set; *isa_set != NULL;
562 		    isa_set++) {
563 			found = 0;
564 			for (p = isabuf, q = p + isalen; p < q;
565 			    p += strlen(p) + 1) {
566 				if (strcmp(p, *isa_set) == 0) {
567 					found = 1;
568 					break;
569 				}
570 			}
571 			if (!found) {
572 				(void) strlcat(md_isalist, *isa_set, md_isalen);
573 				(void) strcat(md_isalist, " ");
574 			}
575 		}
576 	}
577 
578 	/* Get rid of any trailing white spaces */
579 	md_isalist[strlen(md_isalist) - 1] = '\0';
580 
581 	return (md_isalist);
582 }
583 
584 uint64_t
585 get_ra_limit(md_t *mdp)
586 {
587 	mde_cookie_t *mem_list;
588 	mde_cookie_t *mblock_list;
589 	int i;
590 	int memnodes;
591 	int nmblock;
592 	uint64_t base;
593 	uint64_t size;
594 	uint64_t ra_limit = 0, new_limit = 0;
595 
596 	memnodes = md_alloc_scan_dag(mdp,
597 	    md_root_node(mdp), "memory", "fwd", &mem_list);
598 
599 	ASSERT(memnodes == 1);
600 
601 	nmblock = md_alloc_scan_dag(mdp,
602 	    mem_list[0], "mblock", "fwd", &mblock_list);
603 	if (nmblock < 1)
604 		cmn_err(CE_PANIC, "cannot find mblock nodes in MD");
605 
606 	for (i = 0; i < nmblock; i++) {
607 		if (md_get_prop_val(mdp, mblock_list[i], "base", &base))
608 			cmn_err(CE_PANIC, "base property missing from MD"
609 			    " mblock node");
610 		if (md_get_prop_val(mdp, mblock_list[i], "size", &size))
611 			cmn_err(CE_PANIC, "size property missing from MD"
612 			    " mblock node");
613 
614 		ASSERT(size != 0);
615 
616 		new_limit = base + size;
617 
618 		if (base > new_limit)
619 			cmn_err(CE_PANIC, "mblock in MD wrapped around");
620 
621 		if (new_limit > ra_limit)
622 			ra_limit = new_limit;
623 	}
624 
625 	ASSERT(ra_limit != 0);
626 
627 	if (ra_limit > MAX_REAL_ADDRESS) {
628 		cmn_err(CE_WARN, "Highest real address in MD too large"
629 		    " clipping to %llx\n", MAX_REAL_ADDRESS);
630 		ra_limit = MAX_REAL_ADDRESS;
631 	}
632 
633 	md_free_scan_dag(mdp, &mblock_list);
634 
635 	md_free_scan_dag(mdp, &mem_list);
636 
637 	return (ra_limit);
638 }
639 
640 /*
641  * This routine sets the globals for CPU and DEV mondo queue entries and
642  * resumable and non-resumable error queue entries.
643  *
644  * First, look up the number of bits available to pass an entry number.
645  * This can vary by platform and may result in allocating an unreasonably
646  * (or impossibly) large amount of memory for the corresponding table,
647  * so we clamp it by 'max_entries'.  If the prop is missing, use
648  * 'default_entries'.
649  */
650 static uint64_t
651 get_single_q_size(md_t *mdp, mde_cookie_t cpu_node_cookie,
652     char *qnamep, uint64_t default_entries, uint64_t max_entries)
653 {
654 	uint64_t entries;
655 
656 	if (default_entries > max_entries)
657 		cmn_err(CE_CONT, "!get_single_q_size: dflt %ld > "
658 		    "max %ld for %s\n", default_entries, max_entries, qnamep);
659 
660 	if (md_get_prop_val(mdp, cpu_node_cookie, qnamep, &entries)) {
661 		if (!broken_md_flag)
662 			cmn_err(CE_PANIC, "Missing %s property in MD cpu node",
663 			    qnamep);
664 		entries = default_entries;
665 	} else {
666 		entries = 1 << entries;
667 	}
668 
669 	entries = MIN(entries, max_entries);
670 
671 	return (entries);
672 }
673 
674 /* Scaling constant used to compute size of cpu mondo queue */
675 #define	CPU_MONDO_Q_MULTIPLIER	8
676 
677 static void
678 get_q_sizes(md_t *mdp, mde_cookie_t cpu_node_cookie)
679 {
680 	uint64_t max_qsize;
681 	mde_cookie_t *platlist;
682 	int nrnode;
683 
684 	/*
685 	 * Compute the maximum number of entries for the cpu mondo queue.
686 	 * Use the appropriate property in the platform node, if it is
687 	 * available.  Else, base it on NCPU.
688 	 */
689 	nrnode = md_alloc_scan_dag(mdp,
690 	    md_root_node(mdp), "platform", "fwd", &platlist);
691 
692 	ASSERT(nrnode == 1);
693 
694 	ncpu_guest_max = NCPU;
695 	(void) md_get_prop_val(mdp, platlist[0], "max-cpus", &ncpu_guest_max);
696 	max_qsize = ncpu_guest_max * CPU_MONDO_Q_MULTIPLIER;
697 
698 	md_free_scan_dag(mdp, &platlist);
699 
700 	cpu_q_entries = get_single_q_size(mdp, cpu_node_cookie,
701 	    "q-cpu-mondo-#bits", DEFAULT_CPU_Q_ENTRIES, max_qsize);
702 
703 	dev_q_entries = get_single_q_size(mdp, cpu_node_cookie,
704 	    "q-dev-mondo-#bits", DEFAULT_DEV_Q_ENTRIES, MAXIVNUM);
705 
706 	cpu_rq_entries = get_single_q_size(mdp, cpu_node_cookie,
707 	    "q-resumable-#bits", CPU_RQ_ENTRIES, MAX_CPU_RQ_ENTRIES);
708 
709 	cpu_nrq_entries = get_single_q_size(mdp, cpu_node_cookie,
710 	    "q-nonresumable-#bits", CPU_NRQ_ENTRIES, MAX_CPU_NRQ_ENTRIES);
711 }
712 
713 
714 static void
715 get_va_bits(md_t *mdp, mde_cookie_t cpu_node_cookie)
716 {
717 	uint64_t value = VA_ADDRESS_SPACE_BITS;
718 
719 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-#va-bits", &value))
720 		cmn_err(CE_PANIC, "mmu-#va-bits property  not found in MD");
721 
722 
723 	if (value == 0 || value > VA_ADDRESS_SPACE_BITS)
724 		cmn_err(CE_PANIC, "Incorrect number of va bits in MD");
725 
726 	/* Do not expect number of VA bits to be more than 32-bit quantity */
727 
728 	va_bits = (int)value;
729 
730 	/*
731 	 * Correct the value for VA bits on UltraSPARC-T1 based systems
732 	 * in case of broken MD.
733 	 */
734 	if (broken_md_flag)
735 		va_bits = DEFAULT_VA_ADDRESS_SPACE_BITS;
736 }
737 
738 /*
739  * This routine returns the L2 cache information such as -- associativity,
740  * size and linesize.
741  */
742 static int
743 get_l2_cache_info(md_t *mdp, mde_cookie_t cpu_node_cookie,
744 	    uint64_t *associativity, uint64_t *size, uint64_t *linesize)
745 {
746 	mde_cookie_t *cachelist;
747 	int ncaches, i;
748 	uint64_t max_level;
749 
750 	ncaches = md_alloc_scan_dag(mdp, cpu_node_cookie, "cache",
751 	    "fwd", &cachelist);
752 	/*
753 	 * The "cache" node is optional in MD, therefore ncaches can be 0.
754 	 */
755 	if (ncaches < 1) {
756 		return (0);
757 	}
758 
759 	max_level = 0;
760 	for (i = 0; i < ncaches; i++) {
761 		uint64_t cache_level;
762 		uint64_t local_assoc;
763 		uint64_t local_size;
764 		uint64_t local_lsize;
765 
766 		if (md_get_prop_val(mdp, cachelist[i], "level", &cache_level))
767 			continue;
768 
769 		if (cache_level <= max_level) continue;
770 
771 		/* If properties are missing from this cache ignore it */
772 
773 		if ((md_get_prop_val(mdp, cachelist[i],
774 		    "associativity", &local_assoc))) {
775 			continue;
776 		}
777 
778 		if ((md_get_prop_val(mdp, cachelist[i],
779 		    "size", &local_size))) {
780 			continue;
781 		}
782 
783 		if ((md_get_prop_val(mdp, cachelist[i],
784 		    "line-size", &local_lsize))) {
785 			continue;
786 		}
787 
788 		max_level = cache_level;
789 		*associativity = local_assoc;
790 		*size = local_size;
791 		*linesize = local_lsize;
792 	}
793 
794 	md_free_scan_dag(mdp, &cachelist);
795 
796 	return ((max_level > 0) ? 1 : 0);
797 }
798 
799 
800 /*
801  * Set the broken_md_flag to 1 if the MD doesn't have
802  * the domaining-enabled property in the platform node and the
803  * platform uses the UltraSPARC-T1 cpu. This flag is used to
804  * workaround some of the incorrect MD properties.
805  */
806 static void
807 init_md_broken(md_t *mdp, mde_cookie_t *cpulist)
808 {
809 	int nrnode;
810 	mde_cookie_t *platlist, rootnode;
811 	uint64_t val = 0;
812 	char *namebuf;
813 	int namelen;
814 
815 	rootnode = md_root_node(mdp);
816 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
817 	ASSERT(cpulist);
818 
819 	nrnode = md_alloc_scan_dag(mdp, rootnode, "platform", "fwd",
820 	    &platlist);
821 
822 	if (nrnode < 1)
823 		cmn_err(CE_PANIC, "init_md_broken: platform node missing");
824 
825 	if (md_get_prop_data(mdp, cpulist[0],
826 	    "compatible", (uint8_t **)&namebuf, &namelen)) {
827 		cmn_err(CE_PANIC, "init_md_broken: "
828 		    "Cannot read 'compatible' property of 'cpu' node");
829 	}
830 
831 	if (md_get_prop_val(mdp, platlist[0],
832 	    "domaining-enabled", &val) == -1 &&
833 	    strcmp(namebuf, "SUNW,UltraSPARC-T1") == 0)
834 		broken_md_flag = 1;
835 
836 	md_free_scan_dag(mdp, &platlist);
837 }
838