xref: /titanic_50/usr/src/uts/sun4v/os/fillsysinfo.c (revision 44743693dce3212f5edba623e0cb0327bd4337a3)
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 	 * Initialize the mapping for exec unit, chip and core.
190 	 */
191 	cpunode->exec_unit_mapping = NO_EU_MAPPING_FOUND;
192 	cpunode->l2_cache_mapping = NO_MAPPING_FOUND;
193 	cpunode->core_mapping = NO_CORE_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 /*
209  * Use L2 cache node to derive the chip mapping.
210  */
211 void
212 setup_chip_mappings(md_t *mdp)
213 {
214 	uint64_t ncache, ncpu;
215 	mde_cookie_t *node, *cachelist;
216 	int i, j;
217 	processorid_t cpuid;
218 	int idx = 0;
219 
220 	ncache = md_alloc_scan_dag(mdp, md_root_node(mdp), "cache",
221 	    "fwd", &cachelist);
222 
223 	/*
224 	 * The "cache" node is optional in MD, therefore ncaches can be 0.
225 	 */
226 	if (ncache < 1) {
227 		return;
228 	}
229 
230 	for (i = 0; i < ncache; i++) {
231 		uint64_t cache_level;
232 		uint64_t lcpuid;
233 
234 		if (md_get_prop_val(mdp, cachelist[i], "level", &cache_level))
235 			continue;
236 
237 		if (cache_level != 2)
238 			continue;
239 
240 		/*
241 		 * Found a l2 cache node. Find out the cpu nodes it
242 		 * points to.
243 		 */
244 		ncpu = md_alloc_scan_dag(mdp, cachelist[i], "cpu",
245 		    "back", &node);
246 
247 		if (ncpu < 1)
248 			continue;
249 
250 		for (j = 0; j < ncpu; j++) {
251 			if (md_get_prop_val(mdp, node[j], "id", &lcpuid))
252 				continue;
253 			if (lcpuid >= NCPU)
254 				continue;
255 			cpuid = (processorid_t)lcpuid;
256 			cpunodes[cpuid].l2_cache_mapping = idx;
257 		}
258 		md_free_scan_dag(mdp, &node);
259 
260 		idx++;
261 	}
262 
263 	md_free_scan_dag(mdp, &cachelist);
264 }
265 
266 void
267 setup_exec_unit_mappings(md_t *mdp)
268 {
269 	uint64_t num, num_eunits;
270 	mde_cookie_t cpus_node;
271 	mde_cookie_t *node, *eunit;
272 	int idx, i, j;
273 	processorid_t cpuid;
274 	char *eunit_name = broken_md_flag ? "exec_unit" : "exec-unit";
275 	enum eu_type { INTEGER, FPU } etype;
276 
277 	/*
278 	 * Find the cpu integer exec units - and
279 	 * setup the mappings appropriately.
280 	 */
281 	num = md_alloc_scan_dag(mdp, md_root_node(mdp), "cpus", "fwd", &node);
282 	if (num < 1)
283 		cmn_err(CE_PANIC, "No cpus node in machine description");
284 	if (num > 1)
285 		cmn_err(CE_PANIC, "More than 1 cpus node in machine"
286 		    " description");
287 
288 	cpus_node = node[0];
289 	md_free_scan_dag(mdp, &node);
290 
291 	num_eunits = md_alloc_scan_dag(mdp, cpus_node, eunit_name,
292 	    "fwd", &eunit);
293 	if (num_eunits > 0) {
294 		char *int_str = broken_md_flag ? "int" : "integer";
295 		char *fpu_str = "fp";
296 
297 		/* Spin through and find all the integer exec units */
298 		for (i = 0; i < num_eunits; i++) {
299 			char *p;
300 			char *val;
301 			int vallen;
302 			uint64_t lcpuid;
303 
304 			/* ignore nodes with no type */
305 			if (md_get_prop_data(mdp, eunit[i], "type",
306 			    (uint8_t **)&val, &vallen))
307 				continue;
308 
309 			for (p = val; *p != '\0'; p += strlen(p) + 1) {
310 				if (strcmp(p, int_str) == 0) {
311 					etype = INTEGER;
312 					goto found;
313 				}
314 				if (strcmp(p, fpu_str) == 0) {
315 					etype = FPU;
316 					goto found;
317 				}
318 			}
319 
320 			continue;
321 found:
322 			idx = NCPU + i;
323 			/*
324 			 * find the cpus attached to this EU and
325 			 * update their mapping indices
326 			 */
327 			num = md_alloc_scan_dag(mdp, eunit[i], "cpu",
328 			    "back", &node);
329 
330 			if (num < 1)
331 				cmn_err(CE_PANIC, "exec-unit node in MD"
332 				    " not attached to a cpu node");
333 
334 			for (j = 0; j < num; j++) {
335 				if (md_get_prop_val(mdp, node[j], "id",
336 				    &lcpuid))
337 					continue;
338 				if (lcpuid >= NCPU)
339 					continue;
340 				cpuid = (processorid_t)lcpuid;
341 				switch (etype) {
342 				case INTEGER:
343 					cpunodes[cpuid].exec_unit_mapping = idx;
344 					break;
345 				case FPU:
346 					cpunodes[cpuid].fpu_mapping = idx;
347 					break;
348 				}
349 			}
350 			md_free_scan_dag(mdp, &node);
351 		}
352 
353 
354 		md_free_scan_dag(mdp, &eunit);
355 	}
356 }
357 
358 /*
359  * All the common setup of sun4v CPU modules is done by this routine.
360  */
361 void
362 cpu_setup_common(char **cpu_module_isa_set)
363 {
364 	extern int mmu_exported_pagesize_mask;
365 	int nocpus, i;
366 	size_t ra_limit;
367 	mde_cookie_t *cpulist;
368 	md_t *mdp;
369 
370 	if ((mdp = md_get_handle()) == NULL)
371 		cmn_err(CE_PANIC, "Unable to initialize machine description");
372 
373 	nocpus = md_alloc_scan_dag(mdp,
374 	    md_root_node(mdp), "cpu", "fwd", &cpulist);
375 	if (nocpus < 1) {
376 		cmn_err(CE_PANIC, "cpu_common_setup: cpulist allocation "
377 		    "failed or incorrect number of CPUs in MD");
378 	}
379 
380 	init_md_broken(mdp, cpulist);
381 
382 	if (use_page_coloring) {
383 		do_pg_coloring = 1;
384 	}
385 
386 	/*
387 	 * Get the valid mmu page sizes mask, Q sizes and isalist/r
388 	 * from the MD for the first available CPU in cpulist.
389 	 *
390 	 * Do not expect the MMU page sizes mask to be more than 32-bit.
391 	 */
392 	mmu_exported_pagesize_mask = (int)get_cpu_pagesizes(mdp, cpulist[0]);
393 
394 	/*
395 	 * Get the number of contexts and tsbs supported.
396 	 */
397 	if (get_mmu_shcontexts(mdp, cpulist[0]) >= MIN_NSHCONTEXTS &&
398 	    get_mmu_tsbs(mdp, cpulist[0]) >= MIN_NTSBS) {
399 		shctx_on = 1;
400 	}
401 
402 	for (i = 0; i < nocpus; i++)
403 		fill_cpu(mdp, cpulist[i]);
404 
405 	setup_chip_mappings(mdp);
406 	setup_exec_unit_mappings(mdp);
407 
408 	/*
409 	 * If MD is broken then append the passed ISA set,
410 	 * otherwise trust the MD.
411 	 */
412 
413 	if (broken_md_flag)
414 		isa_list = construct_isalist(mdp, cpulist[0],
415 		    cpu_module_isa_set);
416 	else
417 		isa_list = construct_isalist(mdp, cpulist[0], NULL);
418 
419 	get_q_sizes(mdp, cpulist[0]);
420 
421 	get_va_bits(mdp, cpulist[0]);
422 
423 	/*
424 	 * ra_limit is the highest real address in the machine.
425 	 */
426 	ra_limit = get_ra_limit(mdp);
427 
428 	md_free_scan_dag(mdp, &cpulist);
429 
430 	(void) md_fini_handle(mdp);
431 
432 	/*
433 	 * Block stores invalidate all pages of the d$ so pagecopy
434 	 * et. al. do not need virtual translations with virtual
435 	 * coloring taken into consideration.
436 	 */
437 	pp_consistent_coloring = 0;
438 
439 	/*
440 	 * The kpm mapping window.
441 	 * kpm_size:
442 	 *	The size of a single kpm range.
443 	 *	The overall size will be: kpm_size * vac_colors.
444 	 * kpm_vbase:
445 	 *	The virtual start address of the kpm range within the kernel
446 	 *	virtual address space. kpm_vbase has to be kpm_size aligned.
447 	 */
448 
449 	/*
450 	 * Make kpm_vbase, kpm_size aligned to kpm_size_shift.
451 	 * To do this find the nearest power of 2 size that the
452 	 * actual ra_limit fits within.
453 	 * If it is an even power of two use that, otherwise use the
454 	 * next power of two larger than ra_limit.
455 	 */
456 
457 	ASSERT(ra_limit != 0);
458 
459 	kpm_size_shift = (ra_limit & (ra_limit - 1)) != 0 ?
460 	    highbit(ra_limit) : highbit(ra_limit) - 1;
461 
462 	/*
463 	 * No virtual caches on sun4v so size matches size shift
464 	 */
465 	kpm_size = 1ul << kpm_size_shift;
466 
467 	if (va_bits < VA_ADDRESS_SPACE_BITS) {
468 		/*
469 		 * In case of VA hole
470 		 * kpm_base = hole_end + 1TB
471 		 * Starting 1TB beyond where VA hole ends because on Niagara
472 		 * processor software must not use pages within 4GB of the
473 		 * VA hole as instruction pages to avoid problems with
474 		 * prefetching into the VA hole.
475 		 */
476 		kpm_vbase = (caddr_t)((0ull - (1ull << (va_bits - 1))) +
477 		    (1ull << 40));
478 	} else {		/* Number of VA bits 64 ... no VA hole */
479 		kpm_vbase = (caddr_t)0x8000000000000000ull;	/* 8 EB */
480 	}
481 
482 	/*
483 	 * The traptrace code uses either %tick or %stick for
484 	 * timestamping.  The sun4v require use of %stick.
485 	 */
486 	traptrace_use_stick = 1;
487 }
488 
489 /*
490  * Get the nctxs from MD. If absent panic.
491  */
492 static uint64_t
493 get_mmu_ctx_bits(md_t *mdp, mde_cookie_t cpu_node_cookie)
494 {
495 	uint64_t ctx_bits;
496 
497 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-#context-bits",
498 	    &ctx_bits))
499 		ctx_bits = 0;
500 
501 	if (ctx_bits < MIN_NCTXS_BITS || ctx_bits > MAX_NCTXS_BITS)
502 		cmn_err(CE_PANIC, "Incorrect %ld number of contexts bits "
503 		    "returned by MD", ctx_bits);
504 
505 	return (ctx_bits);
506 }
507 
508 /*
509  * Get the number of tsbs from MD. If absent the default value is 0.
510  */
511 static uint64_t
512 get_mmu_tsbs(md_t *mdp, mde_cookie_t cpu_node_cookie)
513 {
514 	uint64_t number_tsbs;
515 
516 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-max-#tsbs",
517 	    &number_tsbs))
518 		number_tsbs = 0;
519 
520 	return (number_tsbs);
521 }
522 
523 /*
524  * Get the number of shared contexts from MD. If absent the default value is 0.
525  *
526  */
527 static uint64_t
528 get_mmu_shcontexts(md_t *mdp, mde_cookie_t cpu_node_cookie)
529 {
530 	uint64_t number_contexts;
531 
532 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-#shared-contexts",
533 	    &number_contexts))
534 		number_contexts = 0;
535 
536 	return (number_contexts);
537 }
538 
539 /*
540  * Initalize supported page sizes information.
541  * Set to 0, if the page sizes mask information is absent in MD.
542  */
543 static uint64_t
544 get_cpu_pagesizes(md_t *mdp, mde_cookie_t cpu_node_cookie)
545 {
546 	uint64_t mmu_page_size_list;
547 
548 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-page-size-list",
549 	    &mmu_page_size_list))
550 		mmu_page_size_list = 0;
551 
552 	if (mmu_page_size_list == 0 || mmu_page_size_list > MAX_PAGESIZE_MASK)
553 		cmn_err(CE_PANIC, "Incorrect 0x%lx pagesize mask returned"
554 		    "by MD", mmu_page_size_list);
555 
556 	return (mmu_page_size_list);
557 }
558 
559 /*
560  * This routine gets the isalist information from MD and appends
561  * the CPU module ISA set if required.
562  */
563 static char *
564 construct_isalist(md_t *mdp, mde_cookie_t cpu_node_cookie,
565     char **cpu_module_isa_set)
566 {
567 	extern int at_flags;
568 	char *md_isalist;
569 	int md_isalen;
570 	char *isabuf;
571 	int isalen;
572 	char **isa_set;
573 	char *p, *q;
574 	int cpu_module_isalen = 0, found = 0;
575 
576 	(void) md_get_prop_data(mdp, cpu_node_cookie,
577 	    "isalist", (uint8_t **)&isabuf, &isalen);
578 
579 	/*
580 	 * We support binaries for all the cpus that have shipped so far.
581 	 * The kernel emulates instructions that are not supported by hardware.
582 	 */
583 	at_flags = EF_SPARC_SUN_US3 | EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
584 
585 	/*
586 	 * Construct the space separated isa_list.
587 	 */
588 	if (cpu_module_isa_set != NULL) {
589 		for (isa_set = cpu_module_isa_set; *isa_set != NULL;
590 		    isa_set++) {
591 			cpu_module_isalen += strlen(*isa_set);
592 			cpu_module_isalen++;	/* for space character */
593 		}
594 	}
595 
596 	/*
597 	 * Allocate the buffer of MD isa buffer length + CPU module
598 	 * isa buffer length.
599 	 */
600 	md_isalen = isalen + cpu_module_isalen + 2;
601 	md_isalist = (char *)prom_alloc((caddr_t)0, md_isalen, 0);
602 	if (md_isalist == NULL)
603 		cmn_err(CE_PANIC, "construct_isalist: Allocation failed for "
604 		    "md_isalist");
605 
606 	md_isalist[0] = '\0'; /* create an empty string to start */
607 	for (p = isabuf, q = p + isalen; p < q; p += strlen(p) + 1) {
608 		(void) strlcat(md_isalist, p, md_isalen);
609 		(void) strcat(md_isalist, " ");
610 	}
611 
612 	/*
613 	 * Check if the isa_set is present in isalist returned by MD.
614 	 * If yes, then no need to append it, if no then append it to
615 	 * isalist returned by MD.
616 	 */
617 	if (cpu_module_isa_set != NULL) {
618 		for (isa_set = cpu_module_isa_set; *isa_set != NULL;
619 		    isa_set++) {
620 			found = 0;
621 			for (p = isabuf, q = p + isalen; p < q;
622 			    p += strlen(p) + 1) {
623 				if (strcmp(p, *isa_set) == 0) {
624 					found = 1;
625 					break;
626 				}
627 			}
628 			if (!found) {
629 				(void) strlcat(md_isalist, *isa_set, md_isalen);
630 				(void) strcat(md_isalist, " ");
631 			}
632 		}
633 	}
634 
635 	/* Get rid of any trailing white spaces */
636 	md_isalist[strlen(md_isalist) - 1] = '\0';
637 
638 	return (md_isalist);
639 }
640 
641 uint64_t
642 get_ra_limit(md_t *mdp)
643 {
644 	mde_cookie_t *mem_list;
645 	mde_cookie_t *mblock_list;
646 	int i;
647 	int memnodes;
648 	int nmblock;
649 	uint64_t base;
650 	uint64_t size;
651 	uint64_t ra_limit = 0, new_limit = 0;
652 
653 	memnodes = md_alloc_scan_dag(mdp,
654 	    md_root_node(mdp), "memory", "fwd", &mem_list);
655 
656 	ASSERT(memnodes == 1);
657 
658 	nmblock = md_alloc_scan_dag(mdp,
659 	    mem_list[0], "mblock", "fwd", &mblock_list);
660 	if (nmblock < 1)
661 		cmn_err(CE_PANIC, "cannot find mblock nodes in MD");
662 
663 	for (i = 0; i < nmblock; i++) {
664 		if (md_get_prop_val(mdp, mblock_list[i], "base", &base))
665 			cmn_err(CE_PANIC, "base property missing from MD"
666 			    " mblock node");
667 		if (md_get_prop_val(mdp, mblock_list[i], "size", &size))
668 			cmn_err(CE_PANIC, "size property missing from MD"
669 			    " mblock node");
670 
671 		ASSERT(size != 0);
672 
673 		new_limit = base + size;
674 
675 		if (base > new_limit)
676 			cmn_err(CE_PANIC, "mblock in MD wrapped around");
677 
678 		if (new_limit > ra_limit)
679 			ra_limit = new_limit;
680 	}
681 
682 	ASSERT(ra_limit != 0);
683 
684 	if (ra_limit > MAX_REAL_ADDRESS) {
685 		cmn_err(CE_WARN, "Highest real address in MD too large"
686 		    " clipping to %llx\n", MAX_REAL_ADDRESS);
687 		ra_limit = MAX_REAL_ADDRESS;
688 	}
689 
690 	md_free_scan_dag(mdp, &mblock_list);
691 
692 	md_free_scan_dag(mdp, &mem_list);
693 
694 	return (ra_limit);
695 }
696 
697 /*
698  * This routine sets the globals for CPU and DEV mondo queue entries and
699  * resumable and non-resumable error queue entries.
700  *
701  * First, look up the number of bits available to pass an entry number.
702  * This can vary by platform and may result in allocating an unreasonably
703  * (or impossibly) large amount of memory for the corresponding table,
704  * so we clamp it by 'max_entries'.  If the prop is missing, use
705  * 'default_entries'.
706  */
707 static uint64_t
708 get_single_q_size(md_t *mdp, mde_cookie_t cpu_node_cookie,
709     char *qnamep, uint64_t default_entries, uint64_t max_entries)
710 {
711 	uint64_t entries;
712 
713 	if (default_entries > max_entries)
714 		cmn_err(CE_CONT, "!get_single_q_size: dflt %ld > "
715 		    "max %ld for %s\n", default_entries, max_entries, qnamep);
716 
717 	if (md_get_prop_val(mdp, cpu_node_cookie, qnamep, &entries)) {
718 		if (!broken_md_flag)
719 			cmn_err(CE_PANIC, "Missing %s property in MD cpu node",
720 			    qnamep);
721 		entries = default_entries;
722 	} else {
723 		entries = 1 << entries;
724 	}
725 
726 	entries = MIN(entries, max_entries);
727 
728 	return (entries);
729 }
730 
731 /* Scaling constant used to compute size of cpu mondo queue */
732 #define	CPU_MONDO_Q_MULTIPLIER	8
733 
734 static void
735 get_q_sizes(md_t *mdp, mde_cookie_t cpu_node_cookie)
736 {
737 	uint64_t max_qsize;
738 	mde_cookie_t *platlist;
739 	int nrnode;
740 
741 	/*
742 	 * Compute the maximum number of entries for the cpu mondo queue.
743 	 * Use the appropriate property in the platform node, if it is
744 	 * available.  Else, base it on NCPU.
745 	 */
746 	nrnode = md_alloc_scan_dag(mdp,
747 	    md_root_node(mdp), "platform", "fwd", &platlist);
748 
749 	ASSERT(nrnode == 1);
750 
751 	ncpu_guest_max = NCPU;
752 	(void) md_get_prop_val(mdp, platlist[0], "max-cpus", &ncpu_guest_max);
753 	max_qsize = ncpu_guest_max * CPU_MONDO_Q_MULTIPLIER;
754 
755 	md_free_scan_dag(mdp, &platlist);
756 
757 	cpu_q_entries = get_single_q_size(mdp, cpu_node_cookie,
758 	    "q-cpu-mondo-#bits", DEFAULT_CPU_Q_ENTRIES, max_qsize);
759 
760 	dev_q_entries = get_single_q_size(mdp, cpu_node_cookie,
761 	    "q-dev-mondo-#bits", DEFAULT_DEV_Q_ENTRIES, MAXIVNUM);
762 
763 	cpu_rq_entries = get_single_q_size(mdp, cpu_node_cookie,
764 	    "q-resumable-#bits", CPU_RQ_ENTRIES, MAX_CPU_RQ_ENTRIES);
765 
766 	cpu_nrq_entries = get_single_q_size(mdp, cpu_node_cookie,
767 	    "q-nonresumable-#bits", CPU_NRQ_ENTRIES, MAX_CPU_NRQ_ENTRIES);
768 }
769 
770 
771 static void
772 get_va_bits(md_t *mdp, mde_cookie_t cpu_node_cookie)
773 {
774 	uint64_t value = VA_ADDRESS_SPACE_BITS;
775 
776 	if (md_get_prop_val(mdp, cpu_node_cookie, "mmu-#va-bits", &value))
777 		cmn_err(CE_PANIC, "mmu-#va-bits property  not found in MD");
778 
779 
780 	if (value == 0 || value > VA_ADDRESS_SPACE_BITS)
781 		cmn_err(CE_PANIC, "Incorrect number of va bits in MD");
782 
783 	/* Do not expect number of VA bits to be more than 32-bit quantity */
784 
785 	va_bits = (int)value;
786 
787 	/*
788 	 * Correct the value for VA bits on UltraSPARC-T1 based systems
789 	 * in case of broken MD.
790 	 */
791 	if (broken_md_flag)
792 		va_bits = DEFAULT_VA_ADDRESS_SPACE_BITS;
793 }
794 
795 /*
796  * This routine returns the L2 cache information such as -- associativity,
797  * size and linesize.
798  */
799 static int
800 get_l2_cache_info(md_t *mdp, mde_cookie_t cpu_node_cookie,
801 	    uint64_t *associativity, uint64_t *size, uint64_t *linesize)
802 {
803 	mde_cookie_t *cachelist;
804 	int ncaches, i;
805 	uint64_t cache_level = 0;
806 
807 	ncaches = md_alloc_scan_dag(mdp, cpu_node_cookie, "cache",
808 	    "fwd", &cachelist);
809 	/*
810 	 * The "cache" node is optional in MD, therefore ncaches can be 0.
811 	 */
812 	if (ncaches < 1) {
813 		return (0);
814 	}
815 
816 	for (i = 0; i < ncaches; i++) {
817 		uint64_t local_assoc;
818 		uint64_t local_size;
819 		uint64_t local_lsize;
820 
821 		if (md_get_prop_val(mdp, cachelist[i], "level", &cache_level))
822 			continue;
823 
824 		if (cache_level != 2) continue;
825 
826 		/* If properties are missing from this cache ignore it */
827 
828 		if ((md_get_prop_val(mdp, cachelist[i],
829 		    "associativity", &local_assoc))) {
830 			continue;
831 		}
832 
833 		if ((md_get_prop_val(mdp, cachelist[i],
834 		    "size", &local_size))) {
835 			continue;
836 		}
837 
838 		if ((md_get_prop_val(mdp, cachelist[i],
839 		    "line-size", &local_lsize))) {
840 			continue;
841 		}
842 
843 		*associativity = local_assoc;
844 		*size = local_size;
845 		*linesize = local_lsize;
846 		break;
847 	}
848 
849 	md_free_scan_dag(mdp, &cachelist);
850 
851 	return ((cache_level == 2) ? 1 : 0);
852 }
853 
854 
855 /*
856  * Set the broken_md_flag to 1 if the MD doesn't have
857  * the domaining-enabled property in the platform node and the
858  * platform uses the UltraSPARC-T1 cpu. This flag is used to
859  * workaround some of the incorrect MD properties.
860  */
861 static void
862 init_md_broken(md_t *mdp, mde_cookie_t *cpulist)
863 {
864 	int nrnode;
865 	mde_cookie_t *platlist, rootnode;
866 	uint64_t val = 0;
867 	char *namebuf;
868 	int namelen;
869 
870 	rootnode = md_root_node(mdp);
871 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
872 	ASSERT(cpulist);
873 
874 	nrnode = md_alloc_scan_dag(mdp, rootnode, "platform", "fwd",
875 	    &platlist);
876 
877 	if (nrnode < 1)
878 		cmn_err(CE_PANIC, "init_md_broken: platform node missing");
879 
880 	if (md_get_prop_data(mdp, cpulist[0],
881 	    "compatible", (uint8_t **)&namebuf, &namelen)) {
882 		cmn_err(CE_PANIC, "init_md_broken: "
883 		    "Cannot read 'compatible' property of 'cpu' node");
884 	}
885 
886 	if (md_get_prop_val(mdp, platlist[0],
887 	    "domaining-enabled", &val) == -1 &&
888 	    strcmp(namebuf, "SUNW,UltraSPARC-T1") == 0)
889 		broken_md_flag = 1;
890 
891 	md_free_scan_dag(mdp, &platlist);
892 }
893