xref: /titanic_53/usr/src/uts/sun4v/os/mpo.c (revision b779d3e04b6c453fc372ef1dbf8df01a8c2e10dc)
1ce8eb11aSdp78419 /*
2ce8eb11aSdp78419  * CDDL HEADER START
3ce8eb11aSdp78419  *
4ce8eb11aSdp78419  * The contents of this file are subject to the terms of the
5ce8eb11aSdp78419  * Common Development and Distribution License (the "License").
6ce8eb11aSdp78419  * You may not use this file except in compliance with the License.
7ce8eb11aSdp78419  *
8ce8eb11aSdp78419  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ce8eb11aSdp78419  * or http://www.opensolaris.org/os/licensing.
10ce8eb11aSdp78419  * See the License for the specific language governing permissions
11ce8eb11aSdp78419  * and limitations under the License.
12ce8eb11aSdp78419  *
13ce8eb11aSdp78419  * When distributing Covered Code, include this CDDL HEADER in each
14ce8eb11aSdp78419  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ce8eb11aSdp78419  * If applicable, add the following below this CDDL HEADER, with the
16ce8eb11aSdp78419  * fields enclosed by brackets "[]" replaced with your own identifying
17ce8eb11aSdp78419  * information: Portions Copyright [yyyy] [name of copyright owner]
18ce8eb11aSdp78419  *
19ce8eb11aSdp78419  * CDDL HEADER END
20ce8eb11aSdp78419  */
21ce8eb11aSdp78419 
22ce8eb11aSdp78419 /*
23*b779d3e0Sdp78419  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24ce8eb11aSdp78419  * Use is subject to license terms.
25ce8eb11aSdp78419  */
26ce8eb11aSdp78419 
27ce8eb11aSdp78419 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28ce8eb11aSdp78419 
29ce8eb11aSdp78419 #include <sys/types.h>
30ce8eb11aSdp78419 #include <sys/sysmacros.h>
31ce8eb11aSdp78419 #include <sys/machsystm.h>
32ce8eb11aSdp78419 #include <sys/machparam.h>
33ce8eb11aSdp78419 #include <sys/cmn_err.h>
34ce8eb11aSdp78419 #include <sys/stat.h>
35ce8eb11aSdp78419 #include <sys/mach_descrip.h>
36ce8eb11aSdp78419 #include <sys/memnode.h>
37ce8eb11aSdp78419 #include <sys/mdesc.h>
38ce8eb11aSdp78419 #include <sys/mpo.h>
39ce8eb11aSdp78419 #include <vm/vm_dep.h>
40e853d8c3Sjc25722 #include <vm/hat_sfmmu.h>
41bb57d1f5Sjc25722 #include <sys/promif.h>
42ce8eb11aSdp78419 
43ce8eb11aSdp78419 /*
44ce8eb11aSdp78419  * MPO and the sun4v memory representation
45ce8eb11aSdp78419  * ---------------------------------------
46ce8eb11aSdp78419  *
47ce8eb11aSdp78419  * Latency groups are defined in the sun4v achitecture by memory-latency-group
48ce8eb11aSdp78419  * nodes in the Machine Description, as specified in FWARC/2007/260.  These
49ce8eb11aSdp78419  * tie together cpu nodes and mblock nodes, and contain mask and match
50ce8eb11aSdp78419  * properties that identify the portion of an mblock that belongs to the
51ce8eb11aSdp78419  * lgroup.  Mask and match are defined in the Physical Address (PA) space,
52ce8eb11aSdp78419  * but an mblock defines Real Addresses (RA).  To translate, the mblock
53ce8eb11aSdp78419  * includes the property address-congruence-offset, hereafter referred to as
54ce8eb11aSdp78419  * ra_to_pa.  A real address ra is a member of an lgroup if
55ce8eb11aSdp78419  *
56ce8eb11aSdp78419  *	(ra + mblock.ra_to_pa) & lgroup.mask == lgroup.match
57ce8eb11aSdp78419  *
58ce8eb11aSdp78419  * The MD is traversed, and information on all mblocks is kept in the array
59ce8eb11aSdp78419  * mpo_mblock[].  Information on all CPUs, including which lgroup they map
60ce8eb11aSdp78419  * to, is kept in the array mpo_cpu[].
61ce8eb11aSdp78419  *
62ce8eb11aSdp78419  * This implementation makes (and verifies) the simplifying assumption that
63ce8eb11aSdp78419  * the mask bits are the same for all defined lgroups, and that all 1 bits in
64ce8eb11aSdp78419  * the mask are contiguous.  Thus the number of lgroups is bounded by the
65ce8eb11aSdp78419  * number of possible mask values, and the lgrp_handle_t is defined as the
66ce8eb11aSdp78419  * mask value, shifted right to eliminate the 0 bit positions in mask.  The
67ce8eb11aSdp78419  * masks and values are also referred to as "home bits" in the code.
68ce8eb11aSdp78419  *
69ce8eb11aSdp78419  * A mem_node is defined to be 1:1 with an lgrp_handle_t, thus each lgroup
70ce8eb11aSdp78419  * has exactly 1 mem_node, and plat_pfn_to_mem_node() must find the mblock
71ce8eb11aSdp78419  * containing a pfn, apply the mblock's ra_to_pa adjustment, and extract the
72ce8eb11aSdp78419  * home bits.  This yields the mem_node.
73ce8eb11aSdp78419  *
74ce8eb11aSdp78419  * Interfaces
75ce8eb11aSdp78419  * ----------
76ce8eb11aSdp78419  *
77ce8eb11aSdp78419  * This file exports the following entry points:
78ce8eb11aSdp78419  *
79ce8eb11aSdp78419  * plat_lgrp_init()
80ce8eb11aSdp78419  * plat_build_mem_nodes()
81ce8eb11aSdp78419  * plat_lgrp_cpu_to_hand()
82ce8eb11aSdp78419  * plat_lgrp_latency()
83ce8eb11aSdp78419  * plat_pfn_to_mem_node()
84ce8eb11aSdp78419  *	These implement the usual platform lgroup interfaces.
85ce8eb11aSdp78419  *
86ce8eb11aSdp78419  * plat_rapfn_to_papfn()
87ce8eb11aSdp78419  *	Recover the PA page coloring bits from an RA.
88ce8eb11aSdp78419  *
89ce8eb11aSdp78419  * plat_mem_node_iterator_init()
90ce8eb11aSdp78419  *	Initialize an iterator to efficiently step through pages in a mem_node.
91ce8eb11aSdp78419  *
92ce8eb11aSdp78419  * plat_mem_node_intersect_range()
93ce8eb11aSdp78419  *	Find the intersection with a mem_node.
94ce8eb11aSdp78419  */
95ce8eb11aSdp78419 
96ce8eb11aSdp78419 int	sun4v_mpo_enable = 1;
97ce8eb11aSdp78419 int	sun4v_mpo_debug = 0;
98ce8eb11aSdp78419 char	sun4v_mpo_status[256] = "";
99ce8eb11aSdp78419 
100ce8eb11aSdp78419 /* Save CPU info from the MD and associate CPUs with lgroups */
101ce8eb11aSdp78419 static	struct cpu_md mpo_cpu[NCPU];
102ce8eb11aSdp78419 
103ce8eb11aSdp78419 /* Save lgroup info from the MD */
104ce8eb11aSdp78419 #define	MAX_MD_LGROUPS 32
105ce8eb11aSdp78419 static	struct	lgrp_md mpo_lgroup[MAX_MD_LGROUPS];
106ce8eb11aSdp78419 static	int	n_lgrpnodes = 0;
107ce8eb11aSdp78419 static	int	n_locality_groups = 0;
108ce8eb11aSdp78419 static	int	max_locality_groups = 0;
109ce8eb11aSdp78419 
110ce8eb11aSdp78419 /* Save mblocks from the MD */
111bb57d1f5Sjc25722 #define	SMALL_MBLOCKS_COUNT	8
112bb57d1f5Sjc25722 static 	struct	mblock_md *mpo_mblock;
113bb57d1f5Sjc25722 static	struct 	mblock_md small_mpo_mblocks[SMALL_MBLOCKS_COUNT];
114ce8eb11aSdp78419 static	int	n_mblocks = 0;
115ce8eb11aSdp78419 
116ce8eb11aSdp78419 /* Save mem_node stripes calculate from mblocks and lgroups. */
117bb57d1f5Sjc25722 static mem_stripe_t *mem_stripes;
118bb57d1f5Sjc25722 static	mem_stripe_t small_mem_stripes[SMALL_MBLOCKS_COUNT * MAX_MEM_NODES];
119bb57d1f5Sjc25722 static	int 	mstripesz = 0;
120ce8eb11aSdp78419 static	int	n_mem_stripes = 0;
121ce8eb11aSdp78419 static	pfn_t	mnode_stride;	/* distance between stripes, start to start */
122ce8eb11aSdp78419 static	int	stripe_shift;	/* stride/stripes expressed as a shift */
123ce8eb11aSdp78419 static	pfn_t	mnode_pages;	/* mem_node stripe width */
124ce8eb11aSdp78419 
125ce8eb11aSdp78419 /* Save home mask and shift used to calculate lgrp_handle_t values */
126ce8eb11aSdp78419 static	uint64_t home_mask = 0;
127ce8eb11aSdp78419 static	pfn_t	home_mask_pfn = 0;
128ce8eb11aSdp78419 static	int	home_mask_shift = 0;
129ce8eb11aSdp78419 static	uint_t	home_mask_pfn_shift = 0;
130ce8eb11aSdp78419 
131ce8eb11aSdp78419 /* Save lowest and highest latencies found across all lgroups */
132ce8eb11aSdp78419 static	int	lower_latency = 0;
133ce8eb11aSdp78419 static	int	higher_latency = 0;
134ce8eb11aSdp78419 
135ce8eb11aSdp78419 static	pfn_t	base_ra_to_pa_pfn = 0;	/* ra_to_pa for single mblock memory */
136ce8eb11aSdp78419 
137ce8eb11aSdp78419 static	int	valid_pages(md_t *md, mde_cookie_t cpu0);
138ce8eb11aSdp78419 static	int	unique_home_mem_lg_count(uint64_t mem_lg_homeset);
139ce8eb11aSdp78419 static	int	fix_interleave(void);
140ce8eb11aSdp78419 
141ce8eb11aSdp78419 /* Debug support */
142ce8eb11aSdp78419 #if defined(DEBUG) && !defined(lint)
143ce8eb11aSdp78419 #define	MPO_DEBUG(args...) if (sun4v_mpo_debug) printf(args)
144ce8eb11aSdp78419 #else
145ce8eb11aSdp78419 #define	MPO_DEBUG(...)
146ce8eb11aSdp78419 #endif	/* DEBUG */
147ce8eb11aSdp78419 
148ce8eb11aSdp78419 /* Record status message, viewable from mdb */
149ce8eb11aSdp78419 #define	MPO_STATUS(args...) {						      \
150ce8eb11aSdp78419 	(void) snprintf(sun4v_mpo_status, sizeof (sun4v_mpo_status), args);   \
151ce8eb11aSdp78419 	MPO_DEBUG(sun4v_mpo_status);					      \
152ce8eb11aSdp78419 }
153ce8eb11aSdp78419 
154ce8eb11aSdp78419 /*
155ce8eb11aSdp78419  * Routine to read a uint64_t from a given md
156ce8eb11aSdp78419  */
157ce8eb11aSdp78419 static	int64_t
158ce8eb11aSdp78419 get_int(md_t md, mde_cookie_t node, char *propname, uint64_t *val)
159ce8eb11aSdp78419 {
160ce8eb11aSdp78419 	int err = md_get_prop_val(md, node, propname, val);
161ce8eb11aSdp78419 	return (err);
162ce8eb11aSdp78419 }
163ce8eb11aSdp78419 
164ce8eb11aSdp78419 static int
165ce8eb11aSdp78419 mblock_cmp(const void *a, const void *b)
166ce8eb11aSdp78419 {
167ce8eb11aSdp78419 	struct mblock_md *m1 = (struct mblock_md *)a;
168ce8eb11aSdp78419 	struct mblock_md *m2 = (struct mblock_md *)b;
169ce8eb11aSdp78419 
170ce8eb11aSdp78419 	if (m1->base < m2->base)
171ce8eb11aSdp78419 		return (-1);
172ce8eb11aSdp78419 	else if (m1->base == m2->base)
173ce8eb11aSdp78419 		return (0);
174ce8eb11aSdp78419 	else
175ce8eb11aSdp78419 		return (1);
176ce8eb11aSdp78419 }
177ce8eb11aSdp78419 
178ce8eb11aSdp78419 static void
179ce8eb11aSdp78419 mblock_sort(struct mblock_md *mblocks, int n)
180ce8eb11aSdp78419 {
181ce8eb11aSdp78419 	extern void qsort(void *, size_t, size_t,
182ce8eb11aSdp78419 	    int (*)(const void *, const void *));
183ce8eb11aSdp78419 
184ce8eb11aSdp78419 	qsort(mblocks, n, sizeof (mblocks[0]), mblock_cmp);
185ce8eb11aSdp78419 }
186ce8eb11aSdp78419 
187924db11bSjc25722 static void
188924db11bSjc25722 mpo_update_tunables(void)
189924db11bSjc25722 {
190924db11bSjc25722 	int i, ncpu_min;
191924db11bSjc25722 
192924db11bSjc25722 	/*
193924db11bSjc25722 	 * lgrp_expand_proc_thresh is the minimum load on the lgroups
194924db11bSjc25722 	 * this process is currently running on before considering
195924db11bSjc25722 	 *  expanding threads to another lgroup.
196924db11bSjc25722 	 *
197924db11bSjc25722 	 * lgrp_expand_proc_diff determines how much less the remote lgroup
198924db11bSjc25722 	 *  must be loaded before expanding to it.
199924db11bSjc25722 	 *
200924db11bSjc25722 	 * On sun4v CMT processors, threads share a core pipeline, and
201924db11bSjc25722 	 * at less than 100% utilization, best throughput is obtained by
202924db11bSjc25722 	 * spreading threads across more cores, even if some are in a
203924db11bSjc25722 	 * different lgroup.  Spread threads to a new lgroup if the
204924db11bSjc25722 	 * current group is more than 50% loaded.  Because of virtualization,
205924db11bSjc25722 	 * lgroups may have different numbers of CPUs, but the tunables
206924db11bSjc25722 	 * apply to all lgroups, so find the smallest lgroup and compute
207924db11bSjc25722 	 * 50% loading.
208924db11bSjc25722 	 */
209924db11bSjc25722 
210924db11bSjc25722 	ncpu_min = NCPU;
211924db11bSjc25722 	for (i = 0; i < n_lgrpnodes; i++) {
212924db11bSjc25722 		int ncpu = mpo_lgroup[i].ncpu;
213924db11bSjc25722 		if (ncpu != 0 && ncpu < ncpu_min)
214924db11bSjc25722 			ncpu_min = ncpu;
215924db11bSjc25722 	}
216924db11bSjc25722 	lgrp_expand_proc_thresh = ncpu_min * lgrp_loadavg_max_effect / 2;
217924db11bSjc25722 
218924db11bSjc25722 	/* new home may only be half as loaded as the existing home to use it */
219924db11bSjc25722 	lgrp_expand_proc_diff = lgrp_expand_proc_thresh / 2;
220924db11bSjc25722 
221924db11bSjc25722 	lgrp_loadavg_tolerance = lgrp_loadavg_max_effect;
222924db11bSjc25722 }
223924db11bSjc25722 
224924db11bSjc25722 static mde_cookie_t
225924db11bSjc25722 cpuid_to_cpunode(md_t *md, int cpuid)
226924db11bSjc25722 {
227924db11bSjc25722 	mde_cookie_t    rootnode, foundnode, *cpunodes;
228924db11bSjc25722 	uint64_t	cpuid_prop;
229924db11bSjc25722 	int 	n_cpunodes, i;
230924db11bSjc25722 
231924db11bSjc25722 	if (md == NULL)
232924db11bSjc25722 		return (MDE_INVAL_ELEM_COOKIE);
233924db11bSjc25722 
234924db11bSjc25722 	rootnode = md_root_node(md);
235924db11bSjc25722 	if (rootnode == MDE_INVAL_ELEM_COOKIE)
236924db11bSjc25722 		return (MDE_INVAL_ELEM_COOKIE);
237924db11bSjc25722 
238924db11bSjc25722 	n_cpunodes = md_alloc_scan_dag(md, rootnode, PROP_LG_CPU,
239924db11bSjc25722 	    "fwd", &cpunodes);
240924db11bSjc25722 	if (n_cpunodes <= 0 || n_cpunodes > NCPU)
241924db11bSjc25722 		goto cpuid_fail;
242924db11bSjc25722 
243924db11bSjc25722 	for (i = 0; i < n_cpunodes; i++) {
244924db11bSjc25722 		if (md_get_prop_val(md, cpunodes[i], PROP_LG_CPU_ID,
245924db11bSjc25722 		    &cpuid_prop))
246924db11bSjc25722 			break;
247924db11bSjc25722 		if (cpuid_prop == (uint64_t)cpuid) {
248924db11bSjc25722 			foundnode = cpunodes[i];
249924db11bSjc25722 			md_free_scan_dag(md, &cpunodes);
250924db11bSjc25722 			return (foundnode);
251924db11bSjc25722 		}
252924db11bSjc25722 	}
253924db11bSjc25722 cpuid_fail:
254924db11bSjc25722 	if (n_cpunodes > 0)
255924db11bSjc25722 		md_free_scan_dag(md, &cpunodes);
256924db11bSjc25722 	return (MDE_INVAL_ELEM_COOKIE);
257924db11bSjc25722 }
258924db11bSjc25722 
259924db11bSjc25722 static int
260924db11bSjc25722 mpo_cpu_to_lgroup(md_t *md, mde_cookie_t cpunode)
261924db11bSjc25722 {
262924db11bSjc25722 	mde_cookie_t *nodes;
263924db11bSjc25722 	uint64_t latency, lowest_latency;
264924db11bSjc25722 	uint64_t address_match, lowest_address_match;
265924db11bSjc25722 	int n_lgroups, j, result = 0;
266924db11bSjc25722 
267924db11bSjc25722 	/* Find lgroup nodes reachable from this cpu */
268924db11bSjc25722 	n_lgroups = md_alloc_scan_dag(md, cpunode, PROP_LG_MEM_LG,
269924db11bSjc25722 	    "fwd", &nodes);
270924db11bSjc25722 
271924db11bSjc25722 	lowest_latency = ~(0UL);
272924db11bSjc25722 
273924db11bSjc25722 	/* Find the lgroup node with the smallest latency */
274924db11bSjc25722 	for (j = 0; j < n_lgroups; j++) {
275924db11bSjc25722 		result = get_int(md, nodes[j], PROP_LG_LATENCY,
276924db11bSjc25722 		    &latency);
277924db11bSjc25722 		result |= get_int(md, nodes[j], PROP_LG_MATCH,
278924db11bSjc25722 		    &address_match);
279924db11bSjc25722 		if (result != 0) {
280924db11bSjc25722 			j = -1;
281924db11bSjc25722 			goto to_lgrp_done;
282924db11bSjc25722 		}
283924db11bSjc25722 		if (latency < lowest_latency) {
284924db11bSjc25722 			lowest_latency = latency;
285924db11bSjc25722 			lowest_address_match = address_match;
286924db11bSjc25722 		}
287924db11bSjc25722 	}
288924db11bSjc25722 	for (j = 0; j < n_lgrpnodes; j++) {
289924db11bSjc25722 		if ((mpo_lgroup[j].latency == lowest_latency) &&
290924db11bSjc25722 		    (mpo_lgroup[j].addr_match == lowest_address_match))
291924db11bSjc25722 			break;
292924db11bSjc25722 	}
293924db11bSjc25722 	if (j == n_lgrpnodes)
294924db11bSjc25722 		j = -1;
295924db11bSjc25722 
296924db11bSjc25722 to_lgrp_done:
297924db11bSjc25722 	if (n_lgroups > 0)
298924db11bSjc25722 		md_free_scan_dag(md, &nodes);
299924db11bSjc25722 	return (j);
300924db11bSjc25722 }
301924db11bSjc25722 
302924db11bSjc25722 /* Called when DR'ing in a CPU */
303924db11bSjc25722 void
304924db11bSjc25722 mpo_cpu_add(int cpuid)
305924db11bSjc25722 {
306924db11bSjc25722 	md_t *md;
307924db11bSjc25722 	mde_cookie_t cpunode;
308924db11bSjc25722 
309924db11bSjc25722 	int i;
310924db11bSjc25722 
311924db11bSjc25722 	if (n_lgrpnodes <= 0)
312924db11bSjc25722 		return;
313924db11bSjc25722 
314924db11bSjc25722 	md = md_get_handle();
315924db11bSjc25722 
316924db11bSjc25722 	if (md == NULL)
317924db11bSjc25722 		goto add_fail;
318924db11bSjc25722 
319924db11bSjc25722 	cpunode = cpuid_to_cpunode(md, cpuid);
320924db11bSjc25722 	if (cpunode == MDE_INVAL_ELEM_COOKIE)
321924db11bSjc25722 		goto add_fail;
322924db11bSjc25722 
323924db11bSjc25722 	i = mpo_cpu_to_lgroup(md, cpunode);
324924db11bSjc25722 	if (i == -1)
325924db11bSjc25722 		goto add_fail;
326924db11bSjc25722 
327924db11bSjc25722 	mpo_cpu[cpuid].lgrp_index = i;
328924db11bSjc25722 	mpo_cpu[cpuid].home = mpo_lgroup[i].addr_match >> home_mask_shift;
329924db11bSjc25722 	mpo_lgroup[i].ncpu++;
330924db11bSjc25722 	mpo_update_tunables();
331924db11bSjc25722 	(void) md_fini_handle(md);
332924db11bSjc25722 	return;
333924db11bSjc25722 add_fail:
334924db11bSjc25722 	panic("mpo_cpu_add: Cannot read MD");
335924db11bSjc25722 }
336924db11bSjc25722 
337924db11bSjc25722 /* Called when DR'ing out a CPU */
338924db11bSjc25722 void
339924db11bSjc25722 mpo_cpu_remove(int cpuid)
340924db11bSjc25722 {
341924db11bSjc25722 	int i;
342924db11bSjc25722 
343924db11bSjc25722 	if (n_lgrpnodes <= 0)
344924db11bSjc25722 		return;
345924db11bSjc25722 
346924db11bSjc25722 	i = mpo_cpu[cpuid].lgrp_index;
347924db11bSjc25722 	mpo_lgroup[i].ncpu--;
348924db11bSjc25722 	mpo_cpu[cpuid].home = 0;
349924db11bSjc25722 	mpo_cpu[cpuid].lgrp_index = -1;
350924db11bSjc25722 	mpo_update_tunables();
351924db11bSjc25722 }
352924db11bSjc25722 
353ce8eb11aSdp78419 /*
354ce8eb11aSdp78419  *
355ce8eb11aSdp78419  * Traverse the MD to determine:
356ce8eb11aSdp78419  *
357ce8eb11aSdp78419  *  Number of CPU nodes, lgrp_nodes, and mblocks
358ce8eb11aSdp78419  *  Then for each lgrp_node, obtain the appropriate data.
359ce8eb11aSdp78419  *  For each CPU, determine its home locality and store it.
360ce8eb11aSdp78419  *  For each mblock, retrieve its data and store it.
361ce8eb11aSdp78419  */
362ce8eb11aSdp78419 static	int
363ce8eb11aSdp78419 lgrp_traverse(md_t *md)
364ce8eb11aSdp78419 {
365ce8eb11aSdp78419 	mde_cookie_t root, *cpunodes, *lgrpnodes, *nodes, *mblocknodes;
366ce8eb11aSdp78419 	uint64_t i, j, k, o, n_nodes;
367ce8eb11aSdp78419 	uint64_t mem_lg_homeset = 0;
368ce8eb11aSdp78419 	int ret_val = 0;
369ce8eb11aSdp78419 	int result = 0;
370ce8eb11aSdp78419 	int n_cpunodes = 0;
371ce8eb11aSdp78419 	int sub_page_fix;
372bb57d1f5Sjc25722 	int mblocksz = 0;
373bb57d1f5Sjc25722 	size_t allocsz;
374ce8eb11aSdp78419 
375ce8eb11aSdp78419 	n_nodes = md_node_count(md);
376ce8eb11aSdp78419 
377ce8eb11aSdp78419 	if (n_nodes <= 0) {
378ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: No nodes in node count\n");
379ce8eb11aSdp78419 		ret_val = -1;
380ce8eb11aSdp78419 		goto fail;
381ce8eb11aSdp78419 	}
382ce8eb11aSdp78419 
383ce8eb11aSdp78419 	root = md_root_node(md);
384ce8eb11aSdp78419 
385ce8eb11aSdp78419 	if (root == MDE_INVAL_ELEM_COOKIE) {
386ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: Root node is missing\n");
387ce8eb11aSdp78419 		ret_val = -1;
388ce8eb11aSdp78419 		goto fail;
389ce8eb11aSdp78419 	}
390ce8eb11aSdp78419 
391ce8eb11aSdp78419 	/*
392ce8eb11aSdp78419 	 * Build the Memory Nodes.  Do this before any possibility of
393ce8eb11aSdp78419 	 * bailing from this routine so we obtain ra_to_pa (needed for page
394ce8eb11aSdp78419 	 * coloring) even when there are no lgroups defined.
395ce8eb11aSdp78419 	 */
396ce8eb11aSdp78419 
397ce8eb11aSdp78419 	n_mblocks = md_alloc_scan_dag(md, root, PROP_LG_MBLOCK,
398ce8eb11aSdp78419 	    "fwd", &mblocknodes);
399ce8eb11aSdp78419 
400bb57d1f5Sjc25722 	if (n_mblocks <= 0) {
401ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: No mblock "
402ce8eb11aSdp78419 		    "nodes detected in Machine Descriptor\n");
403ce8eb11aSdp78419 		n_mblocks = 0;
404ce8eb11aSdp78419 		ret_val = -1;
405ce8eb11aSdp78419 		goto fail;
406ce8eb11aSdp78419 	}
407bb57d1f5Sjc25722 	/*
408bb57d1f5Sjc25722 	 * If we have a small number of mblocks we will use the space
409bb57d1f5Sjc25722 	 * that we preallocated. Otherwise, we will dynamically
410bb57d1f5Sjc25722 	 * allocate the space
411bb57d1f5Sjc25722 	 */
412bb57d1f5Sjc25722 	mblocksz = n_mblocks * sizeof (struct mblock_md);
413bb57d1f5Sjc25722 	mstripesz = MAX_MEM_NODES * n_mblocks * sizeof (mem_stripe_t);
414bb57d1f5Sjc25722 
415bb57d1f5Sjc25722 	if (n_mblocks <= SMALL_MBLOCKS_COUNT) {
416bb57d1f5Sjc25722 		mpo_mblock = &small_mpo_mblocks[0];
417bb57d1f5Sjc25722 		mem_stripes = &small_mem_stripes[0];
418bb57d1f5Sjc25722 	} else {
419bb57d1f5Sjc25722 		allocsz = mmu_ptob(mmu_btopr(mblocksz + mstripesz));
420bb57d1f5Sjc25722 	/* Ensure that we dont request more space than reserved */
421bb57d1f5Sjc25722 		if (allocsz > MPOBUF_SIZE) {
422bb57d1f5Sjc25722 			MPO_STATUS("lgrp_traverse: Insufficient space "
423bb57d1f5Sjc25722 			    "for mblock structures \n");
424bb57d1f5Sjc25722 			ret_val = -1;
425bb57d1f5Sjc25722 			n_mblocks = 0;
426bb57d1f5Sjc25722 			goto fail;
427bb57d1f5Sjc25722 		}
428bb57d1f5Sjc25722 		mpo_mblock = (struct mblock_md *)
429bb57d1f5Sjc25722 		    prom_alloc((caddr_t)MPOBUF_BASE, allocsz, PAGESIZE);
430bb57d1f5Sjc25722 		if (mpo_mblock != (struct mblock_md *)MPOBUF_BASE) {
431bb57d1f5Sjc25722 			MPO_STATUS("lgrp_traverse: Cannot allocate space "
432bb57d1f5Sjc25722 			    "for mblocks \n");
433bb57d1f5Sjc25722 			ret_val = -1;
434bb57d1f5Sjc25722 			n_mblocks = 0;
435bb57d1f5Sjc25722 			goto fail;
436bb57d1f5Sjc25722 		}
437bb57d1f5Sjc25722 		mpo_heap32_buf = (caddr_t)MPOBUF_BASE;
438bb57d1f5Sjc25722 		mpo_heap32_bufsz = MPOBUF_SIZE;
439bb57d1f5Sjc25722 
440bb57d1f5Sjc25722 		mem_stripes = (mem_stripe_t *)(mpo_mblock + n_mblocks);
441bb57d1f5Sjc25722 	}
442*b779d3e0Sdp78419 	for (i = 0, j = 0; j < n_mblocks; j++) {
443*b779d3e0Sdp78419 		mpo_mblock[i].node = mblocknodes[j];
444ce8eb11aSdp78419 
445ce8eb11aSdp78419 		/* Without a base or size value we will fail */
446*b779d3e0Sdp78419 		result = get_int(md, mblocknodes[j], PROP_LG_BASE,
447ce8eb11aSdp78419 		    &mpo_mblock[i].base);
448ce8eb11aSdp78419 		if (result < 0) {
449ce8eb11aSdp78419 			MPO_STATUS("lgrp_traverse: "
450ce8eb11aSdp78419 			    "PROP_LG_BASE is missing\n");
451ce8eb11aSdp78419 			n_mblocks = 0;
452ce8eb11aSdp78419 			ret_val = -1;
453ce8eb11aSdp78419 			goto fail;
454ce8eb11aSdp78419 		}
455ce8eb11aSdp78419 
456*b779d3e0Sdp78419 		result = get_int(md, mblocknodes[j], PROP_LG_SIZE,
457ce8eb11aSdp78419 		    &mpo_mblock[i].size);
458ce8eb11aSdp78419 		if (result < 0) {
459ce8eb11aSdp78419 			MPO_STATUS("lgrp_traverse: "
460ce8eb11aSdp78419 			    "PROP_LG_SIZE is missing\n");
461ce8eb11aSdp78419 			n_mblocks = 0;
462ce8eb11aSdp78419 			ret_val = -1;
463ce8eb11aSdp78419 			goto fail;
464ce8eb11aSdp78419 		}
465ce8eb11aSdp78419 
466*b779d3e0Sdp78419 		result = get_int(md, mblocknodes[j],
467ce8eb11aSdp78419 		    PROP_LG_RA_PA_OFFSET, &mpo_mblock[i].ra_to_pa);
468ce8eb11aSdp78419 
469ce8eb11aSdp78419 		/* If we don't have an ra_pa_offset, just set it to 0 */
470ce8eb11aSdp78419 		if (result < 0)
471ce8eb11aSdp78419 			mpo_mblock[i].ra_to_pa = 0;
472ce8eb11aSdp78419 
473ce8eb11aSdp78419 		MPO_DEBUG("mblock[%ld]: base = %lx, size = %lx, "
474ce8eb11aSdp78419 		    "ra_to_pa = %lx\n", i,
475ce8eb11aSdp78419 		    mpo_mblock[i].base,
476ce8eb11aSdp78419 		    mpo_mblock[i].size,
477ce8eb11aSdp78419 		    mpo_mblock[i].ra_to_pa);
478*b779d3e0Sdp78419 
479*b779d3e0Sdp78419 		/* check for unsupportable values of base and size */
480*b779d3e0Sdp78419 		if (mpo_mblock[i].base >
481*b779d3e0Sdp78419 		    mpo_mblock[i].base + mpo_mblock[i].size) {
482*b779d3e0Sdp78419 			MPO_STATUS("lgrp_traverse: "
483*b779d3e0Sdp78419 			    "PROP_LG_BASE+PROP_LG_SIZE is invalid: "
484*b779d3e0Sdp78419 			    "base = %lx, size = %lx",
485*b779d3e0Sdp78419 			    mpo_mblock[i].base, mpo_mblock[i].size);
486*b779d3e0Sdp78419 			n_mblocks = 0;
487*b779d3e0Sdp78419 			ret_val = -1;
488*b779d3e0Sdp78419 			goto fail;
489ce8eb11aSdp78419 		}
490ce8eb11aSdp78419 
491*b779d3e0Sdp78419 		/* eliminate size==0 blocks */
492*b779d3e0Sdp78419 		if (mpo_mblock[i].size != 0) {
493*b779d3e0Sdp78419 			i++;
494*b779d3e0Sdp78419 		}
495*b779d3e0Sdp78419 	}
496*b779d3e0Sdp78419 
497*b779d3e0Sdp78419 	if (i == 0) {
498*b779d3e0Sdp78419 		MPO_STATUS("lgrp_traverse: "
499*b779d3e0Sdp78419 		    "No non-empty mblock nodes were found "
500*b779d3e0Sdp78419 		    "in the Machine Descriptor\n");
501*b779d3e0Sdp78419 		n_mblocks = 0;
502*b779d3e0Sdp78419 		ret_val = -1;
503*b779d3e0Sdp78419 		goto fail;
504*b779d3e0Sdp78419 	}
505*b779d3e0Sdp78419 	ASSERT(i <= n_mblocks);
506*b779d3e0Sdp78419 	n_mblocks = i;
507*b779d3e0Sdp78419 
508ce8eb11aSdp78419 	/* Must sort mblocks by address for mem_node_iterator_init() */
509ce8eb11aSdp78419 	mblock_sort(mpo_mblock, n_mblocks);
510ce8eb11aSdp78419 
511ce8eb11aSdp78419 	base_ra_to_pa_pfn = btop(mpo_mblock[0].ra_to_pa);
512ce8eb11aSdp78419 
513ce8eb11aSdp78419 	/* Page coloring hook is required so we can iterate through mnodes */
514ce8eb11aSdp78419 	if (&page_next_pfn_for_color_cpu == NULL) {
515ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: No page coloring support\n");
516ce8eb11aSdp78419 		ret_val = -1;
517ce8eb11aSdp78419 		goto fail;
518ce8eb11aSdp78419 	}
519ce8eb11aSdp78419 
520ce8eb11aSdp78419 	/* Global enable for mpo */
521ce8eb11aSdp78419 	if (sun4v_mpo_enable == 0) {
522ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: MPO feature is not enabled\n");
523ce8eb11aSdp78419 		ret_val = -1;
524ce8eb11aSdp78419 		goto fail;
525ce8eb11aSdp78419 	}
526ce8eb11aSdp78419 
527ce8eb11aSdp78419 	n_lgrpnodes = md_alloc_scan_dag(md, root, PROP_LG_MEM_LG,
528ce8eb11aSdp78419 	    "fwd", &lgrpnodes);
529ce8eb11aSdp78419 
530ce8eb11aSdp78419 	if (n_lgrpnodes <= 0 || n_lgrpnodes >= MAX_MD_LGROUPS) {
531ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: No Lgroups\n");
532ce8eb11aSdp78419 		ret_val = -1;
533ce8eb11aSdp78419 		goto fail;
534ce8eb11aSdp78419 	}
535ce8eb11aSdp78419 
536ce8eb11aSdp78419 	n_cpunodes = md_alloc_scan_dag(md, root, PROP_LG_CPU, "fwd", &cpunodes);
537ce8eb11aSdp78419 
538ce8eb11aSdp78419 	if (n_cpunodes <= 0 || n_cpunodes > NCPU) {
539ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: No CPU nodes detected "
540ce8eb11aSdp78419 		    "in MD\n");
541ce8eb11aSdp78419 		ret_val = -1;
542ce8eb11aSdp78419 		goto fail;
543ce8eb11aSdp78419 	}
544ce8eb11aSdp78419 
545ce8eb11aSdp78419 	MPO_DEBUG("lgrp_traverse: Node Count: %ld\n", n_nodes);
546ce8eb11aSdp78419 	MPO_DEBUG("lgrp_traverse: md: %p\n", md);
547ce8eb11aSdp78419 	MPO_DEBUG("lgrp_traverse: root: %lx\n", root);
548ce8eb11aSdp78419 	MPO_DEBUG("lgrp_traverse: mem_lgs: %d\n", n_lgrpnodes);
549ce8eb11aSdp78419 	MPO_DEBUG("lgrp_traverse: cpus: %d\n", n_cpunodes);
550ce8eb11aSdp78419 	MPO_DEBUG("lgrp_traverse: mblocks: %d\n", n_mblocks);
551ce8eb11aSdp78419 
552ce8eb11aSdp78419 	for (i = 0; i < n_lgrpnodes; i++) {
553ce8eb11aSdp78419 		mpo_lgroup[i].node = lgrpnodes[i];
554ce8eb11aSdp78419 		mpo_lgroup[i].id = i;
555ce8eb11aSdp78419 		mpo_lgroup[i].ncpu = 0;
556ce8eb11aSdp78419 		result = get_int(md, lgrpnodes[i], PROP_LG_MASK,
557ce8eb11aSdp78419 		    &mpo_lgroup[i].addr_mask);
558ce8eb11aSdp78419 		result |= get_int(md, lgrpnodes[i], PROP_LG_MATCH,
559ce8eb11aSdp78419 		    &mpo_lgroup[i].addr_match);
560ce8eb11aSdp78419 
561ce8eb11aSdp78419 		/*
562ce8eb11aSdp78419 		 * If either the mask or match properties are missing, set to 0
563ce8eb11aSdp78419 		 */
564ce8eb11aSdp78419 		if (result < 0) {
565ce8eb11aSdp78419 			mpo_lgroup[i].addr_mask = 0;
566ce8eb11aSdp78419 			mpo_lgroup[i].addr_match = 0;
567ce8eb11aSdp78419 		}
568ce8eb11aSdp78419 
569ce8eb11aSdp78419 		/* Set latency to 0 if property not present */
570ce8eb11aSdp78419 
571ce8eb11aSdp78419 		result = get_int(md, lgrpnodes[i], PROP_LG_LATENCY,
572ce8eb11aSdp78419 		    &mpo_lgroup[i].latency);
573ce8eb11aSdp78419 		if (result < 0)
574ce8eb11aSdp78419 			mpo_lgroup[i].latency = 0;
575ce8eb11aSdp78419 	}
576ce8eb11aSdp78419 
577ce8eb11aSdp78419 	/*
578ce8eb11aSdp78419 	 * Sub-page level interleave is not yet supported.  Check for it,
579ce8eb11aSdp78419 	 * and remove sub-page interleaved lgroups from mpo_lgroup and
580ce8eb11aSdp78419 	 * n_lgrpnodes.  If no lgroups are left, return.
581ce8eb11aSdp78419 	 */
582ce8eb11aSdp78419 
583ce8eb11aSdp78419 	sub_page_fix = fix_interleave();
584ce8eb11aSdp78419 	if (n_lgrpnodes == 0) {
585ce8eb11aSdp78419 		ret_val = -1;
586ce8eb11aSdp78419 		goto fail;
587ce8eb11aSdp78419 	}
588ce8eb11aSdp78419 
589ce8eb11aSdp78419 	/* Ensure that all of the addr_mask values are the same */
590ce8eb11aSdp78419 
591ce8eb11aSdp78419 	for (i = 0; i < n_lgrpnodes; i++) {
592ce8eb11aSdp78419 		if (mpo_lgroup[0].addr_mask != mpo_lgroup[i].addr_mask) {
593ce8eb11aSdp78419 			MPO_STATUS("lgrp_traverse: "
594ce8eb11aSdp78419 			    "addr_mask values are not the same\n");
595ce8eb11aSdp78419 			ret_val = -1;
596ce8eb11aSdp78419 			goto fail;
597ce8eb11aSdp78419 		}
598ce8eb11aSdp78419 	}
599ce8eb11aSdp78419 
600ce8eb11aSdp78419 	/*
601ce8eb11aSdp78419 	 * Ensure that all lgrp nodes see all the mblocks. However, if
602ce8eb11aSdp78419 	 * sub-page interleave is being fixed, they do not, so skip
603ce8eb11aSdp78419 	 * the check.
604ce8eb11aSdp78419 	 */
605ce8eb11aSdp78419 
606ce8eb11aSdp78419 	if (sub_page_fix == 0) {
607ce8eb11aSdp78419 		for (i = 0; i < n_lgrpnodes; i++) {
608ce8eb11aSdp78419 			j = md_alloc_scan_dag(md, mpo_lgroup[i].node,
609ce8eb11aSdp78419 			    PROP_LG_MBLOCK, "fwd", &nodes);
610ce8eb11aSdp78419 			md_free_scan_dag(md, &nodes);
611ce8eb11aSdp78419 			if (j != n_mblocks) {
612ce8eb11aSdp78419 				MPO_STATUS("lgrp_traverse: "
613ce8eb11aSdp78419 				    "sub-page interleave is being fixed\n");
614ce8eb11aSdp78419 				ret_val = -1;
615ce8eb11aSdp78419 				goto fail;
616ce8eb11aSdp78419 			}
617ce8eb11aSdp78419 		}
618ce8eb11aSdp78419 	}
619ce8eb11aSdp78419 
620ce8eb11aSdp78419 	/*
621ce8eb11aSdp78419 	 * Use the address mask from the first lgroup node
622ce8eb11aSdp78419 	 * to establish our home_mask.
623ce8eb11aSdp78419 	 */
624ce8eb11aSdp78419 	home_mask = mpo_lgroup[0].addr_mask;
625ce8eb11aSdp78419 	home_mask_pfn = btop(home_mask);
626ce8eb11aSdp78419 	home_mask_shift = lowbit(home_mask) - 1;
627ce8eb11aSdp78419 	home_mask_pfn_shift = home_mask_shift - PAGESHIFT;
628ce8eb11aSdp78419 	mnode_pages = btop(1ULL << home_mask_shift);
629ce8eb11aSdp78419 
630ce8eb11aSdp78419 	/*
631ce8eb11aSdp78419 	 * How many values are possible in home mask?  Assume the mask
632ce8eb11aSdp78419 	 * bits are contiguous.
633ce8eb11aSdp78419 	 */
634ce8eb11aSdp78419 	max_locality_groups =
635ce8eb11aSdp78419 	    1 << highbit(home_mask_pfn >> home_mask_pfn_shift);
636ce8eb11aSdp78419 
637ce8eb11aSdp78419 	/* Now verify the home mask bits are contiguous */
638ce8eb11aSdp78419 
639ce8eb11aSdp78419 	if (max_locality_groups - 1 != home_mask_pfn >> home_mask_pfn_shift) {
640ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: "
641ce8eb11aSdp78419 		    "home mask bits are not contiguous\n");
642ce8eb11aSdp78419 		ret_val = -1;
643ce8eb11aSdp78419 		goto fail;
644ce8eb11aSdp78419 	}
645ce8eb11aSdp78419 
646ce8eb11aSdp78419 	/* Record all of the home bits */
647ce8eb11aSdp78419 
648ce8eb11aSdp78419 	for (i = 0; i < n_lgrpnodes; i++) {
649ce8eb11aSdp78419 		HOMESET_ADD(mem_lg_homeset,
650ce8eb11aSdp78419 		    mpo_lgroup[i].addr_match >> home_mask_shift);
651ce8eb11aSdp78419 	}
652ce8eb11aSdp78419 
653ce8eb11aSdp78419 	/* Count the number different "home"  mem_lg's we've discovered */
654ce8eb11aSdp78419 
655ce8eb11aSdp78419 	n_locality_groups = unique_home_mem_lg_count(mem_lg_homeset);
656ce8eb11aSdp78419 
657ce8eb11aSdp78419 	/* If we have only 1 locality group then we can exit */
658ce8eb11aSdp78419 	if (n_locality_groups == 1) {
659ce8eb11aSdp78419 		MPO_STATUS("lgrp_traverse: n_locality_groups == 1\n");
660ce8eb11aSdp78419 		ret_val = -1;
661ce8eb11aSdp78419 		goto fail;
662ce8eb11aSdp78419 	}
663ce8eb11aSdp78419 
664ce8eb11aSdp78419 	/*
665ce8eb11aSdp78419 	 * Set the latencies.  A CPU's lgroup is defined by the lowest
666ce8eb11aSdp78419 	 * latency found.  All other memory is considered remote, and the
667ce8eb11aSdp78419 	 * remote latency is represented by the highest latency found.
668ce8eb11aSdp78419 	 * Thus hierarchical lgroups, if any, are approximated by a
669ce8eb11aSdp78419 	 * two level scheme.
670ce8eb11aSdp78419 	 *
671ce8eb11aSdp78419 	 * The Solaris MPO framework by convention wants to see latencies
672ce8eb11aSdp78419 	 * in units of nano-sec/10. In the MD, the units are defined to be
673ce8eb11aSdp78419 	 * pico-seconds.
674ce8eb11aSdp78419 	 */
675ce8eb11aSdp78419 
676ce8eb11aSdp78419 	lower_latency = mpo_lgroup[0].latency;
677ce8eb11aSdp78419 	higher_latency = mpo_lgroup[0].latency;
678ce8eb11aSdp78419 
679ce8eb11aSdp78419 	for (i = 1; i < n_lgrpnodes; i++) {
680ce8eb11aSdp78419 		if (mpo_lgroup[i].latency < lower_latency) {
681ce8eb11aSdp78419 			lower_latency = mpo_lgroup[i].latency;
682ce8eb11aSdp78419 		}
683ce8eb11aSdp78419 		if (mpo_lgroup[i].latency > higher_latency) {
684ce8eb11aSdp78419 			higher_latency = mpo_lgroup[i].latency;
685ce8eb11aSdp78419 		}
686ce8eb11aSdp78419 	}
687ce8eb11aSdp78419 	lower_latency /= 10000;
688ce8eb11aSdp78419 	higher_latency /= 10000;
689ce8eb11aSdp78419 
690ce8eb11aSdp78419 	/* Clear our CPU data */
691ce8eb11aSdp78419 
692ce8eb11aSdp78419 	for (i = 0; i < NCPU; i++) {
693ce8eb11aSdp78419 		mpo_cpu[i].home = 0;
694924db11bSjc25722 		mpo_cpu[i].lgrp_index = -1;
695ce8eb11aSdp78419 	}
696ce8eb11aSdp78419 
697ce8eb11aSdp78419 	/* Build the CPU nodes */
698ce8eb11aSdp78419 	for (i = 0; i < n_cpunodes; i++) {
699ce8eb11aSdp78419 
700ce8eb11aSdp78419 		/* Read in the lgroup nodes */
701ce8eb11aSdp78419 		result = get_int(md, cpunodes[i], PROP_LG_CPU_ID, &k);
702ce8eb11aSdp78419 		if (result < 0) {
703ce8eb11aSdp78419 			MPO_STATUS("lgrp_traverse: PROP_LG_CPU_ID missing\n");
704ce8eb11aSdp78419 			ret_val = -1;
705ce8eb11aSdp78419 			goto fail;
706ce8eb11aSdp78419 		}
707ce8eb11aSdp78419 
708924db11bSjc25722 		o = mpo_cpu_to_lgroup(md, cpunodes[i]);
709924db11bSjc25722 		if (o == -1) {
710ce8eb11aSdp78419 			ret_val = -1;
711ce8eb11aSdp78419 			goto fail;
712ce8eb11aSdp78419 		}
713924db11bSjc25722 		mpo_cpu[k].lgrp_index = o;
714924db11bSjc25722 		mpo_cpu[k].home = mpo_lgroup[o].addr_match >> home_mask_shift;
715ce8eb11aSdp78419 		mpo_lgroup[o].ncpu++;
716ce8eb11aSdp78419 	}
717ce8eb11aSdp78419 	/* Validate that no large pages cross mnode boundaries. */
718ce8eb11aSdp78419 	if (valid_pages(md, cpunodes[0]) == 0) {
719ce8eb11aSdp78419 		ret_val = -1;
720ce8eb11aSdp78419 		goto fail;
721ce8eb11aSdp78419 	}
722ce8eb11aSdp78419 
723ce8eb11aSdp78419 fail:
724ce8eb11aSdp78419 	/* MD cookies are no longer valid; ensure they are not used again. */
725ce8eb11aSdp78419 	for (i = 0; i < n_mblocks; i++)
726ce8eb11aSdp78419 		mpo_mblock[i].node = MDE_INVAL_ELEM_COOKIE;
727ce8eb11aSdp78419 	for (i = 0; i < n_lgrpnodes; i++)
728ce8eb11aSdp78419 		mpo_lgroup[i].node = MDE_INVAL_ELEM_COOKIE;
729ce8eb11aSdp78419 
730ce8eb11aSdp78419 	if (n_cpunodes > 0)
731ce8eb11aSdp78419 		md_free_scan_dag(md, &cpunodes);
732ce8eb11aSdp78419 	if (n_lgrpnodes > 0)
733ce8eb11aSdp78419 		md_free_scan_dag(md, &lgrpnodes);
734ce8eb11aSdp78419 	if (n_mblocks > 0)
735ce8eb11aSdp78419 		md_free_scan_dag(md, &mblocknodes);
736ce8eb11aSdp78419 	else
737ce8eb11aSdp78419 		panic("lgrp_traverse: No memory blocks found");
738ce8eb11aSdp78419 
739ce8eb11aSdp78419 	if (ret_val == 0)
740ce8eb11aSdp78419 		MPO_STATUS("MPO feature is enabled.\n");
741ce8eb11aSdp78419 
742ce8eb11aSdp78419 	return (ret_val);
743ce8eb11aSdp78419 }
744ce8eb11aSdp78419 
745ce8eb11aSdp78419 /*
746ce8eb11aSdp78419  *  Determine the number of unique mem_lg's present in our system
747ce8eb11aSdp78419  */
748ce8eb11aSdp78419 static	int
749ce8eb11aSdp78419 unique_home_mem_lg_count(uint64_t mem_lg_homeset)
750ce8eb11aSdp78419 {
751ce8eb11aSdp78419 	int homeid;
752ce8eb11aSdp78419 	int count = 0;
753ce8eb11aSdp78419 
754ce8eb11aSdp78419 	/*
755ce8eb11aSdp78419 	 * Scan the "home" bits of the mem_lgs, count
756ce8eb11aSdp78419 	 * the number that are unique.
757ce8eb11aSdp78419 	 */
758ce8eb11aSdp78419 
759ce8eb11aSdp78419 	for (homeid = 0; homeid < NLGRPS_MAX; homeid++) {
760ce8eb11aSdp78419 		if (MEM_LG_ISMEMBER(mem_lg_homeset, homeid)) {
761ce8eb11aSdp78419 			count++;
762ce8eb11aSdp78419 		}
763ce8eb11aSdp78419 	}
764ce8eb11aSdp78419 
765ce8eb11aSdp78419 	MPO_DEBUG("unique_home_mem_lg_count: homeset %lx\n",
766ce8eb11aSdp78419 	    mem_lg_homeset);
767ce8eb11aSdp78419 	MPO_DEBUG("unique_home_mem_lg_count: count: %d\n", count);
768ce8eb11aSdp78419 
769ce8eb11aSdp78419 	/* Default must be at least one */
770ce8eb11aSdp78419 	if (count == 0)
771ce8eb11aSdp78419 		count = 1;
772ce8eb11aSdp78419 
773ce8eb11aSdp78419 	return (count);
774ce8eb11aSdp78419 }
775ce8eb11aSdp78419 
776ce8eb11aSdp78419 /*
777ce8eb11aSdp78419  * Platform specific lgroup initialization
778ce8eb11aSdp78419  */
779ce8eb11aSdp78419 void
780ce8eb11aSdp78419 plat_lgrp_init(void)
781ce8eb11aSdp78419 {
782ce8eb11aSdp78419 	md_t *md;
783924db11bSjc25722 	int rc;
784ce8eb11aSdp78419 
785ce8eb11aSdp78419 	/* Get the Machine Descriptor handle */
786ce8eb11aSdp78419 
787ce8eb11aSdp78419 	md = md_get_handle();
788ce8eb11aSdp78419 
789ce8eb11aSdp78419 	/* If not, we cannot continue */
790ce8eb11aSdp78419 
791ce8eb11aSdp78419 	if (md == NULL) {
792ce8eb11aSdp78419 		panic("cannot access machine descriptor\n");
793ce8eb11aSdp78419 	} else {
794ce8eb11aSdp78419 		rc = lgrp_traverse(md);
795ce8eb11aSdp78419 		(void) md_fini_handle(md);
796ce8eb11aSdp78419 	}
797ce8eb11aSdp78419 
798ce8eb11aSdp78419 	/*
799ce8eb11aSdp78419 	 * If we can't process the MD for lgroups then at least let the
800ce8eb11aSdp78419 	 * system try to boot.  Assume we have one lgroup so that
801ce8eb11aSdp78419 	 * when plat_build_mem_nodes is called, it will attempt to init
802ce8eb11aSdp78419 	 * an mnode based on the supplied memory segment.
803ce8eb11aSdp78419 	 */
804ce8eb11aSdp78419 
805ce8eb11aSdp78419 	if (rc == -1) {
806ce8eb11aSdp78419 		home_mask_pfn = 0;
807ce8eb11aSdp78419 		max_locality_groups = 1;
808ce8eb11aSdp78419 		n_locality_groups = 1;
809ce8eb11aSdp78419 		return;
810ce8eb11aSdp78419 	}
811ce8eb11aSdp78419 
812ce8eb11aSdp78419 	mem_node_pfn_shift = 0;
813ce8eb11aSdp78419 	mem_node_physalign = 0;
814ce8eb11aSdp78419 
815ce8eb11aSdp78419 	/* Use lgroup-aware TSB allocations */
816ce8eb11aSdp78419 	tsb_lgrp_affinity = 1;
817ce8eb11aSdp78419 
818ce8eb11aSdp78419 	/* Require that a home lgroup have some memory to be chosen */
819ce8eb11aSdp78419 	lgrp_mem_free_thresh = 1;
820ce8eb11aSdp78419 
821ce8eb11aSdp78419 	/* Standard home-on-next-touch policy */
822ce8eb11aSdp78419 	lgrp_mem_policy_root = LGRP_MEM_POLICY_NEXT;
823ce8eb11aSdp78419 
824ce8eb11aSdp78419 	/* Disable option to choose root lgroup if all leaf lgroups are busy */
825ce8eb11aSdp78419 	lgrp_load_thresh = UINT32_MAX;
826924db11bSjc25722 
827924db11bSjc25722 	mpo_update_tunables();
828ce8eb11aSdp78419 }
829ce8eb11aSdp78419 
830ce8eb11aSdp78419 /*
831ce8eb11aSdp78419  *  Helper routine for debugging calls to mem_node_add_slice()
832ce8eb11aSdp78419  */
833ce8eb11aSdp78419 static	void
834ce8eb11aSdp78419 mpo_mem_node_add_slice(pfn_t basepfn, pfn_t endpfn)
835ce8eb11aSdp78419 {
836ce8eb11aSdp78419 #if defined(DEBUG) && !defined(lint)
837ce8eb11aSdp78419 	static int slice_count = 0;
838ce8eb11aSdp78419 
839ce8eb11aSdp78419 	slice_count++;
840ce8eb11aSdp78419 	MPO_DEBUG("mem_add_slice(%d): basepfn: %lx  endpfn: %lx\n",
841ce8eb11aSdp78419 	    slice_count, basepfn, endpfn);
842ce8eb11aSdp78419 #endif
843ce8eb11aSdp78419 	mem_node_add_slice(basepfn, endpfn);
844ce8eb11aSdp78419 }
845ce8eb11aSdp78419 
846ce8eb11aSdp78419 /*
847ce8eb11aSdp78419  *  Helper routine for debugging calls to plat_assign_lgrphand_to_mem_node()
848ce8eb11aSdp78419  */
849ce8eb11aSdp78419 static	void
850ce8eb11aSdp78419 mpo_plat_assign_lgrphand_to_mem_node(lgrp_handle_t plathand, int mnode)
851ce8eb11aSdp78419 {
852ce8eb11aSdp78419 	MPO_DEBUG("plat_assign_to_mem_nodes: lgroup home %ld,"
853ce8eb11aSdp78419 	    "mnode index: %d\n", plathand, mnode);
854ce8eb11aSdp78419 	plat_assign_lgrphand_to_mem_node(plathand, mnode);
855ce8eb11aSdp78419 }
856ce8eb11aSdp78419 
857ce8eb11aSdp78419 /*
858ce8eb11aSdp78419  * plat_build_mem_nodes()
859ce8eb11aSdp78419  *
860ce8eb11aSdp78419  * Define the mem_nodes based on the modified boot memory list,
861ce8eb11aSdp78419  * or based on info read from the MD in plat_lgrp_init().
862ce8eb11aSdp78419  *
863ce8eb11aSdp78419  * When the home mask lies in the middle of the address bits (as it does on
864ce8eb11aSdp78419  * Victoria Falls), then the memory in one mem_node is no longer contiguous;
865ce8eb11aSdp78419  * it is striped across an mblock in a repeating pattern of contiguous memory
866ce8eb11aSdp78419  * followed by a gap.  The stripe width is the size of the contiguous piece.
867ce8eb11aSdp78419  * The stride is the distance from the start of one contiguous piece to the
868ce8eb11aSdp78419  * start of the next.  The gap is thus stride - stripe_width.
869ce8eb11aSdp78419  *
870ce8eb11aSdp78419  * The stripe of an mnode that falls within an mblock is described by the type
871ce8eb11aSdp78419  * mem_stripe_t, and there is one mem_stripe_t per mnode per mblock.  The
872ce8eb11aSdp78419  * mem_stripe_t's are kept in a global array mem_stripes[].  The index into
873ce8eb11aSdp78419  * this array is predetermined.  The mem_stripe_t that describes mnode m
874ce8eb11aSdp78419  * within mpo_mblock[i] is stored at
875ce8eb11aSdp78419  *	 mem_stripes[ m + i * max_locality_groups ]
876ce8eb11aSdp78419  *
877ce8eb11aSdp78419  * max_locality_groups is the total number of possible locality groups,
878ce8eb11aSdp78419  * as defined by the size of the home mask, even if the memory assigned
879ce8eb11aSdp78419  * to the domain is small and does not cover all the lgroups.  Thus some
880ce8eb11aSdp78419  * mem_stripe_t's may be empty.
881ce8eb11aSdp78419  *
882ce8eb11aSdp78419  * The members of mem_stripe_t are:
883ce8eb11aSdp78419  *	physbase: First valid page in mem_node in the corresponding mblock
884ce8eb11aSdp78419  *	physmax: Last valid page in mem_node in mblock
885ce8eb11aSdp78419  *	offset:  The full stripe width starts at physbase - offset.
886ce8eb11aSdp78419  *	    Thus if offset is non-zero, this mem_node starts in the middle
887ce8eb11aSdp78419  *	    of a stripe width, and the second full stripe starts at
888ce8eb11aSdp78419  *	    physbase - offset + stride.  (even though physmax may fall in the
889ce8eb11aSdp78419  *	    middle of a stripe width, we do not save the ending fragment size
890ce8eb11aSdp78419  *	    in this data structure.)
891ce8eb11aSdp78419  *	exists: Set to 1 if the mblock has memory in this mem_node stripe.
892ce8eb11aSdp78419  *
893ce8eb11aSdp78419  *	The stripe width is kept in the global mnode_pages.
894ce8eb11aSdp78419  *	The stride is kept in the global mnode_stride.
895ce8eb11aSdp78419  *	All the above use pfn's as the unit.
896ce8eb11aSdp78419  *
897ce8eb11aSdp78419  * As an example, the memory layout for a domain with 2 mblocks and 4
898ce8eb11aSdp78419  * mem_nodes 0,1,2,3 could look like this:
899ce8eb11aSdp78419  *
900ce8eb11aSdp78419  *	123012301230 ...	012301230123 ...
901ce8eb11aSdp78419  *	  mblock 0		  mblock 1
902ce8eb11aSdp78419  */
903ce8eb11aSdp78419 
904ce8eb11aSdp78419 void
905986fd29aSsetje plat_build_mem_nodes(prom_memlist_t *list, size_t nelems)
906ce8eb11aSdp78419 {
907ce8eb11aSdp78419 	lgrp_handle_t lgrphand, lgrp_start;
908ce8eb11aSdp78419 	int i, mnode, elem;
909ce8eb11aSdp78419 	uint64_t offset, stripe_end, base, len, end, ra_to_pa, stride;
910ce8eb11aSdp78419 	uint64_t stripe, frag, remove;
911ce8eb11aSdp78419 	mem_stripe_t *ms;
912ce8eb11aSdp78419 
913e853d8c3Sjc25722 	/* Pre-reserve space for plat_assign_lgrphand_to_mem_node */
914e853d8c3Sjc25722 	max_mem_nodes = max_locality_groups;
915ce8eb11aSdp78419 
916e853d8c3Sjc25722 	/* Check for non-MPO sun4v platforms */
917ce8eb11aSdp78419 	if (n_locality_groups <= 1) {
918*b779d3e0Sdp78419 		ASSERT(n_locality_groups == 1);
919*b779d3e0Sdp78419 		ASSERT(max_locality_groups == 1 && max_mem_nodes == 1);
920e853d8c3Sjc25722 		mpo_plat_assign_lgrphand_to_mem_node(LGRP_DEFAULT_HANDLE, 0);
921986fd29aSsetje 		for (elem = 0; elem < nelems; list++, elem++) {
922986fd29aSsetje 			base = list->addr;
923986fd29aSsetje 			len = list->size;
924ce8eb11aSdp78419 
925ce8eb11aSdp78419 			mpo_mem_node_add_slice(btop(base),
926ce8eb11aSdp78419 			    btop(base + len - 1));
927ce8eb11aSdp78419 		}
928ce8eb11aSdp78419 		mem_node_pfn_shift = 0;
929ce8eb11aSdp78419 		mem_node_physalign = 0;
930*b779d3e0Sdp78419 
931*b779d3e0Sdp78419 		if (n_mblocks == 1) {
932ce8eb11aSdp78419 			n_mem_stripes = 0;
933*b779d3e0Sdp78419 		} else {
934*b779d3e0Sdp78419 			n_mem_stripes = n_mblocks;
935*b779d3e0Sdp78419 			bzero(mem_stripes, mstripesz);
936*b779d3e0Sdp78419 			for (i = 0; i < n_mblocks; i++) {
937*b779d3e0Sdp78419 				base = mpo_mblock[i].base;
938*b779d3e0Sdp78419 				end = base + mpo_mblock[i].size;
939*b779d3e0Sdp78419 				ASSERT(end > base);
940*b779d3e0Sdp78419 				mem_stripes[i].exists = 1;
941*b779d3e0Sdp78419 				mpo_mblock[i].base_pfn = btop(base);
942*b779d3e0Sdp78419 				mpo_mblock[i].end_pfn = btop(end - 1);
943*b779d3e0Sdp78419 				mem_stripes[i].physbase =
944*b779d3e0Sdp78419 				    mpo_mblock[i].base_pfn;
945*b779d3e0Sdp78419 				mem_stripes[i].physmax = mpo_mblock[i].end_pfn;
946*b779d3e0Sdp78419 			}
947*b779d3e0Sdp78419 		}
948ce8eb11aSdp78419 		return;
949ce8eb11aSdp78419 	}
950ce8eb11aSdp78419 
951bb57d1f5Sjc25722 	bzero(mem_stripes, mstripesz);
952ce8eb11aSdp78419 	stripe = ptob(mnode_pages);
953ce8eb11aSdp78419 	stride = max_locality_groups * stripe;
954ce8eb11aSdp78419 
955ce8eb11aSdp78419 	/* Save commonly used values in globals */
956ce8eb11aSdp78419 	mnode_stride = btop(stride);
957ce8eb11aSdp78419 	n_mem_stripes = max_locality_groups * n_mblocks;
958ce8eb11aSdp78419 	stripe_shift = highbit(max_locality_groups) - 1;
959ce8eb11aSdp78419 
960ce8eb11aSdp78419 	for (i = 0; i < n_mblocks; i++) {
961ce8eb11aSdp78419 		base = mpo_mblock[i].base;
962ce8eb11aSdp78419 		end = mpo_mblock[i].base + mpo_mblock[i].size;
963ce8eb11aSdp78419 		ra_to_pa = mpo_mblock[i].ra_to_pa;
964ce8eb11aSdp78419 		mpo_mblock[i].base_pfn = btop(base);
965ce8eb11aSdp78419 		mpo_mblock[i].end_pfn = btop(end - 1);
966ce8eb11aSdp78419 
967ce8eb11aSdp78419 		/* Find the offset from the prev stripe boundary in PA space. */
968ce8eb11aSdp78419 		offset = (base + ra_to_pa) & (stripe - 1);
969ce8eb11aSdp78419 
970ce8eb11aSdp78419 		/* Set the next stripe boundary. */
971ce8eb11aSdp78419 		stripe_end = base - offset + stripe;
972ce8eb11aSdp78419 
973ce8eb11aSdp78419 		lgrp_start = (((base + ra_to_pa) & home_mask) >>
974ce8eb11aSdp78419 		    home_mask_shift);
975ce8eb11aSdp78419 		lgrphand = lgrp_start;
976ce8eb11aSdp78419 
977ce8eb11aSdp78419 		/*
978ce8eb11aSdp78419 		 * Loop over all lgroups covered by the mblock, creating a
979ce8eb11aSdp78419 		 * stripe for each.  Stop when lgrp_start is visited again.
980ce8eb11aSdp78419 		 */
981ce8eb11aSdp78419 		do {
982ce8eb11aSdp78419 			/* mblock may not span all lgroups */
983ce8eb11aSdp78419 			if (base >= end)
984ce8eb11aSdp78419 				break;
985ce8eb11aSdp78419 
986ce8eb11aSdp78419 			mnode = lgrphand;
987ce8eb11aSdp78419 			ASSERT(mnode < max_mem_nodes);
988ce8eb11aSdp78419 
989ce8eb11aSdp78419 			/*
990ce8eb11aSdp78419 			 * Calculate the size of the fragment that does not
991ce8eb11aSdp78419 			 * belong to the mnode in the last partial stride.
992ce8eb11aSdp78419 			 */
993ce8eb11aSdp78419 			frag = (end - (base - offset)) & (stride - 1);
994ce8eb11aSdp78419 			if (frag == 0) {
995ce8eb11aSdp78419 				/* remove the gap */
996ce8eb11aSdp78419 				remove = stride - stripe;
997ce8eb11aSdp78419 			} else if (frag < stripe) {
998ce8eb11aSdp78419 				/* fragment fits in stripe; keep it all */
999ce8eb11aSdp78419 				remove = 0;
1000ce8eb11aSdp78419 			} else {
1001ce8eb11aSdp78419 				/* fragment is large; trim after whole stripe */
1002ce8eb11aSdp78419 				remove = frag - stripe;
1003ce8eb11aSdp78419 			}
1004ce8eb11aSdp78419 
1005ce8eb11aSdp78419 			ms = &mem_stripes[i * max_locality_groups + mnode];
1006ce8eb11aSdp78419 			ms->physbase = btop(base);
1007ce8eb11aSdp78419 			ms->physmax = btop(end - 1 - remove);
1008ce8eb11aSdp78419 			ms->offset = btop(offset);
1009ce8eb11aSdp78419 			ms->exists = 1;
1010ce8eb11aSdp78419 
1011e853d8c3Sjc25722 			/*
1012e853d8c3Sjc25722 			 * If we have only 1 lgroup and multiple mblocks,
1013e853d8c3Sjc25722 			 * then we have already established our lgrp handle
1014e853d8c3Sjc25722 			 * to mem_node and mem_node_config values above.
1015e853d8c3Sjc25722 			 */
1016e853d8c3Sjc25722 			if (n_locality_groups > 1) {
1017e853d8c3Sjc25722 				mpo_plat_assign_lgrphand_to_mem_node(lgrphand,
1018e853d8c3Sjc25722 				    mnode);
1019e853d8c3Sjc25722 				mpo_mem_node_add_slice(ms->physbase,
1020e853d8c3Sjc25722 				    ms->physmax);
1021e853d8c3Sjc25722 			}
1022ce8eb11aSdp78419 			base = stripe_end;
1023ce8eb11aSdp78419 			stripe_end += stripe;
1024ce8eb11aSdp78419 			offset = 0;
1025ce8eb11aSdp78419 			lgrphand = (((base + ra_to_pa) & home_mask) >>
1026ce8eb11aSdp78419 			    home_mask_shift);
1027ce8eb11aSdp78419 		} while (lgrphand != lgrp_start);
1028ce8eb11aSdp78419 	}
1029ce8eb11aSdp78419 
1030ce8eb11aSdp78419 	/*
1031ce8eb11aSdp78419 	 * Indicate to vm_pagelist that the hpm_counters array
1032ce8eb11aSdp78419 	 * should be shared because the ranges overlap.
1033ce8eb11aSdp78419 	 */
1034ce8eb11aSdp78419 	if (max_mem_nodes > 1) {
1035ce8eb11aSdp78419 		interleaved_mnodes = 1;
1036ce8eb11aSdp78419 	}
1037ce8eb11aSdp78419 }
1038ce8eb11aSdp78419 
1039ce8eb11aSdp78419 /*
1040ce8eb11aSdp78419  * Return the locality group value for the supplied processor
1041ce8eb11aSdp78419  */
1042ce8eb11aSdp78419 lgrp_handle_t
1043ce8eb11aSdp78419 plat_lgrp_cpu_to_hand(processorid_t id)
1044ce8eb11aSdp78419 {
1045ce8eb11aSdp78419 	if (n_locality_groups > 1) {
1046ce8eb11aSdp78419 		return ((lgrp_handle_t)mpo_cpu[(int)id].home);
1047ce8eb11aSdp78419 	} else {
1048e853d8c3Sjc25722 		return ((lgrp_handle_t)LGRP_DEFAULT_HANDLE); /* Default */
1049ce8eb11aSdp78419 	}
1050ce8eb11aSdp78419 }
1051ce8eb11aSdp78419 
1052ce8eb11aSdp78419 int
1053ce8eb11aSdp78419 plat_lgrp_latency(lgrp_handle_t from, lgrp_handle_t to)
1054ce8eb11aSdp78419 {
1055ce8eb11aSdp78419 	/*
1056ce8eb11aSdp78419 	 * Return min remote latency when there are more than two lgroups
1057ce8eb11aSdp78419 	 * (root and child) and getting latency between two different lgroups
1058ce8eb11aSdp78419 	 * or root is involved.
1059ce8eb11aSdp78419 	 */
1060ce8eb11aSdp78419 	if (lgrp_optimizations() && (from != to ||
1061ce8eb11aSdp78419 	    from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE)) {
1062ce8eb11aSdp78419 		return ((int)higher_latency);
1063ce8eb11aSdp78419 	} else {
1064ce8eb11aSdp78419 		return ((int)lower_latency);
1065ce8eb11aSdp78419 	}
1066ce8eb11aSdp78419 }
1067ce8eb11aSdp78419 
1068ce8eb11aSdp78419 int
1069ce8eb11aSdp78419 plat_pfn_to_mem_node(pfn_t pfn)
1070ce8eb11aSdp78419 {
1071ce8eb11aSdp78419 	int i, mnode;
1072ce8eb11aSdp78419 	pfn_t ra_to_pa_pfn;
1073ce8eb11aSdp78419 	struct mblock_md *mb;
1074ce8eb11aSdp78419 
1075ce8eb11aSdp78419 	if (n_locality_groups <= 1)
1076ce8eb11aSdp78419 		return (0);
1077ce8eb11aSdp78419 
1078ce8eb11aSdp78419 	/*
1079ce8eb11aSdp78419 	 * The mnode is defined to be 1:1 with the lgroup handle, which
1080ce8eb11aSdp78419 	 * is taken from from the home bits.  Find the mblock in which
1081ce8eb11aSdp78419 	 * the pfn falls to get the ra_to_pa adjustment, and extract
1082ce8eb11aSdp78419 	 * the home bits.
1083ce8eb11aSdp78419 	 */
1084ce8eb11aSdp78419 	mb = &mpo_mblock[0];
1085ce8eb11aSdp78419 	for (i = 0; i < n_mblocks; i++) {
1086ce8eb11aSdp78419 		if (pfn >= mb->base_pfn && pfn <= mb->end_pfn) {
1087ce8eb11aSdp78419 			ra_to_pa_pfn = btop(mb->ra_to_pa);
1088ce8eb11aSdp78419 			mnode = (((pfn + ra_to_pa_pfn) & home_mask_pfn) >>
1089ce8eb11aSdp78419 			    home_mask_pfn_shift);
1090ce8eb11aSdp78419 			ASSERT(mnode < max_mem_nodes);
1091ce8eb11aSdp78419 			return (mnode);
1092ce8eb11aSdp78419 		}
1093ce8eb11aSdp78419 		mb++;
1094ce8eb11aSdp78419 	}
1095ce8eb11aSdp78419 
1096ce8eb11aSdp78419 	panic("plat_pfn_to_mem_node() failed to find mblock: pfn=%lx\n", pfn);
1097ce8eb11aSdp78419 	return (pfn);
1098ce8eb11aSdp78419 }
1099ce8eb11aSdp78419 
1100ce8eb11aSdp78419 /*
1101ce8eb11aSdp78419  * plat_rapfn_to_papfn
1102ce8eb11aSdp78419  *
1103ce8eb11aSdp78419  * Convert a pfn in RA space to a pfn in PA space, in which the page coloring
1104ce8eb11aSdp78419  * and home mask bits are correct.  The upper bits do not necessarily
1105ce8eb11aSdp78419  * match the actual PA, however.
1106ce8eb11aSdp78419  */
1107ce8eb11aSdp78419 pfn_t
1108ce8eb11aSdp78419 plat_rapfn_to_papfn(pfn_t pfn)
1109ce8eb11aSdp78419 {
1110ce8eb11aSdp78419 	int i;
1111ce8eb11aSdp78419 	pfn_t ra_to_pa_pfn;
1112ce8eb11aSdp78419 	struct mblock_md *mb;
1113ce8eb11aSdp78419 
1114ce8eb11aSdp78419 	ASSERT(n_mblocks > 0);
1115ce8eb11aSdp78419 	if (n_mblocks == 1)
1116ce8eb11aSdp78419 		return (pfn + base_ra_to_pa_pfn);
1117ce8eb11aSdp78419 
1118ce8eb11aSdp78419 	/*
1119ce8eb11aSdp78419 	 * Find the mblock in which the pfn falls
1120ce8eb11aSdp78419 	 * in order to get the ra_to_pa adjustment.
1121ce8eb11aSdp78419 	 */
1122ce8eb11aSdp78419 	for (mb = &mpo_mblock[0], i = 0; i < n_mblocks; i++, mb++) {
1123ce8eb11aSdp78419 		if (pfn <= mb->end_pfn && pfn >= mb->base_pfn) {
1124ce8eb11aSdp78419 			ra_to_pa_pfn = btop(mb->ra_to_pa);
1125ce8eb11aSdp78419 			return (pfn + ra_to_pa_pfn);
1126ce8eb11aSdp78419 		}
1127ce8eb11aSdp78419 	}
1128ce8eb11aSdp78419 
1129ce8eb11aSdp78419 	panic("plat_rapfn_to_papfn() failed to find mblock: pfn=%lx\n", pfn);
1130ce8eb11aSdp78419 	return (pfn);
1131ce8eb11aSdp78419 }
1132ce8eb11aSdp78419 
1133ce8eb11aSdp78419 /*
1134ce8eb11aSdp78419  * plat_mem_node_iterator_init()
1135*b779d3e0Sdp78419  *      Initialize cookie "it" to iterate over pfn's in an mnode.  There is
1136ce8eb11aSdp78419  *      no additional iterator function.  The caller uses the info from
1137ce8eb11aSdp78419  *      the iterator structure directly.
1138ce8eb11aSdp78419  *
1139ce8eb11aSdp78419  *      pfn: starting pfn.
1140ce8eb11aSdp78419  *      mnode: desired mnode.
1141*b779d3e0Sdp78419  *	szc: desired page size.
1142*b779d3e0Sdp78419  *      init:
1143*b779d3e0Sdp78419  *          if 1, start a new traversal, initialize "it", find first
1144*b779d3e0Sdp78419  *              mblock containing pfn, and return its starting pfn
1145*b779d3e0Sdp78419  *              within the mnode.
1146*b779d3e0Sdp78419  *          if 0, continue the previous traversal using passed-in data
1147*b779d3e0Sdp78419  *              from "it", advance to the next mblock, and return its
1148*b779d3e0Sdp78419  *              starting pfn within the mnode.
1149*b779d3e0Sdp78419  *      it: returns readonly data to the caller; see below.
1150ce8eb11aSdp78419  *
1151*b779d3e0Sdp78419  *	The input pfn must be aligned for the page size szc.
1152*b779d3e0Sdp78419  *
1153*b779d3e0Sdp78419  *      Returns: starting pfn for the iteration for the mnode/mblock,
1154*b779d3e0Sdp78419  *	    which is aligned according to the page size,
1155*b779d3e0Sdp78419  *          or returns (pfn_t)(-1) if the input pfn lies past the last
1156*b779d3e0Sdp78419  *          valid pfn of the mnode.
1157*b779d3e0Sdp78419  *      Returns misc values in the "it" struct that allows the caller
1158*b779d3e0Sdp78419  *          to advance the pfn within an mblock using address arithmetic;
1159*b779d3e0Sdp78419  *          see definition of mem_node_iterator_t in vm_dep.h.
1160*b779d3e0Sdp78419  *          When the caller calculates a pfn that is greater than the
1161*b779d3e0Sdp78419  *          returned value it->mi_mblock_end, the caller should again
1162*b779d3e0Sdp78419  *          call plat_mem_node_iterator_init, passing init=0.
1163ce8eb11aSdp78419  */
1164ce8eb11aSdp78419 pfn_t
1165*b779d3e0Sdp78419 plat_mem_node_iterator_init(pfn_t pfn, int mnode, uchar_t szc,
1166ce8eb11aSdp78419     mem_node_iterator_t *it, int init)
1167ce8eb11aSdp78419 {
1168ce8eb11aSdp78419 	int i;
1169*b779d3e0Sdp78419 	pgcnt_t szcpgcnt = PNUM_SIZE(szc);
1170ce8eb11aSdp78419 	struct mblock_md *mblock;
1171ce8eb11aSdp78419 	pfn_t base, end;
1172*b779d3e0Sdp78419 	mem_stripe_t *ms;
1173*b779d3e0Sdp78419 	uint64_t szcpagesize;
1174ce8eb11aSdp78419 
1175ce8eb11aSdp78419 	ASSERT(it != NULL);
1176ce8eb11aSdp78419 	ASSERT(mnode >= 0 && mnode < max_mem_nodes);
1177ce8eb11aSdp78419 	ASSERT(n_mblocks > 0);
1178*b779d3e0Sdp78419 	ASSERT(P2PHASE(pfn, szcpgcnt) == 0);
1179ce8eb11aSdp78419 
1180ce8eb11aSdp78419 	if (init) {
1181ce8eb11aSdp78419 		it->mi_last_mblock = 0;
1182ce8eb11aSdp78419 		it->mi_init = 1;
1183ce8eb11aSdp78419 	}
1184ce8eb11aSdp78419 
1185ce8eb11aSdp78419 	/* Check if mpo is not enabled and we only have one mblock */
1186ce8eb11aSdp78419 	if (n_locality_groups == 1 && n_mblocks == 1) {
1187*b779d3e0Sdp78419 		if (P2PHASE(base_ra_to_pa_pfn, szcpgcnt))
1188*b779d3e0Sdp78419 			return ((pfn_t)-1);
1189ce8eb11aSdp78419 		it->mi_mnode = mnode;
1190ce8eb11aSdp78419 		it->mi_ra_to_pa = base_ra_to_pa_pfn;
1191ce8eb11aSdp78419 		it->mi_mnode_pfn_mask = 0;
1192ce8eb11aSdp78419 		it->mi_mnode_pfn_shift = 0;
1193ce8eb11aSdp78419 		it->mi_mnode_mask = 0;
1194ce8eb11aSdp78419 		it->mi_mblock_base = mem_node_config[mnode].physbase;
1195ce8eb11aSdp78419 		it->mi_mblock_end = mem_node_config[mnode].physmax;
1196ce8eb11aSdp78419 		if (pfn < it->mi_mblock_base)
1197*b779d3e0Sdp78419 			pfn = P2ROUNDUP(it->mi_mblock_base, szcpgcnt);
1198*b779d3e0Sdp78419 		if ((pfn + szcpgcnt - 1) > it->mi_mblock_end)
1199ce8eb11aSdp78419 			pfn = (pfn_t)-1;
1200ce8eb11aSdp78419 		return (pfn);
1201ce8eb11aSdp78419 	}
1202ce8eb11aSdp78419 
1203*b779d3e0Sdp78419 	/* init=1 means begin iterator, init=0 means continue */
1204*b779d3e0Sdp78419 	if (init == 1) {
1205*b779d3e0Sdp78419 		i = 0;
1206*b779d3e0Sdp78419 	} else {
1207ce8eb11aSdp78419 		ASSERT(it->mi_last_mblock < n_mblocks);
1208*b779d3e0Sdp78419 		i = it->mi_last_mblock;
1209*b779d3e0Sdp78419 		ASSERT(pfn >
1210*b779d3e0Sdp78419 		    mem_stripes[i * max_locality_groups + mnode].physmax);
1211*b779d3e0Sdp78419 		if (++i == n_mblocks)
1212ce8eb11aSdp78419 			return ((pfn_t)-1);
1213*b779d3e0Sdp78419 	}
1214ce8eb11aSdp78419 
1215*b779d3e0Sdp78419 	/*
1216*b779d3e0Sdp78419 	 * Find mblock that contains pfn for mnode's stripe, or first such an
1217*b779d3e0Sdp78419 	 * mblock after pfn, else pfn is out of bound and we'll return -1.
1218*b779d3e0Sdp78419 	 * mblocks and stripes are sorted in ascending address order.
1219*b779d3e0Sdp78419 	 */
1220*b779d3e0Sdp78419 	szcpagesize = szcpgcnt << PAGESHIFT;
1221ce8eb11aSdp78419 	for (; i < n_mblocks; i++) {
1222*b779d3e0Sdp78419 		if (P2PHASE(mpo_mblock[i].ra_to_pa, szcpagesize))
1223*b779d3e0Sdp78419 			continue;
1224*b779d3e0Sdp78419 		ms = &mem_stripes[i * max_locality_groups + mnode];
1225*b779d3e0Sdp78419 		if (ms->exists && (pfn + szcpgcnt - 1) <= ms->physmax &&
1226*b779d3e0Sdp78419 		    (P2ROUNDUP(ms->physbase, szcpgcnt) + szcpgcnt - 1) <=
1227*b779d3e0Sdp78419 		    ms->physmax)
1228ce8eb11aSdp78419 			break;
1229ce8eb11aSdp78419 	}
1230ce8eb11aSdp78419 	if (i == n_mblocks) {
1231ce8eb11aSdp78419 		it->mi_last_mblock = i - 1;
1232ce8eb11aSdp78419 		return ((pfn_t)-1);
1233ce8eb11aSdp78419 	}
1234*b779d3e0Sdp78419 
1235ce8eb11aSdp78419 	it->mi_last_mblock = i;
1236ce8eb11aSdp78419 
1237ce8eb11aSdp78419 	mblock = &mpo_mblock[i];
1238ce8eb11aSdp78419 	base = ms->physbase;
1239ce8eb11aSdp78419 	end = ms->physmax;
1240ce8eb11aSdp78419 
1241ce8eb11aSdp78419 	it->mi_mnode = mnode;
1242ce8eb11aSdp78419 	it->mi_ra_to_pa = btop(mblock->ra_to_pa);
1243ce8eb11aSdp78419 	it->mi_mblock_base = base;
1244ce8eb11aSdp78419 	it->mi_mblock_end = end;
1245ce8eb11aSdp78419 	it->mi_mnode_pfn_mask = home_mask_pfn;	/* is 0 for non-MPO case */
1246ce8eb11aSdp78419 	it->mi_mnode_pfn_shift = home_mask_pfn_shift;
1247ce8eb11aSdp78419 	it->mi_mnode_mask = max_locality_groups - 1;
1248*b779d3e0Sdp78419 	if (pfn < base) {
1249*b779d3e0Sdp78419 		pfn = P2ROUNDUP(base, szcpgcnt);
1250*b779d3e0Sdp78419 		ASSERT(pfn + szcpgcnt - 1 <= end);
1251*b779d3e0Sdp78419 	}
1252*b779d3e0Sdp78419 	ASSERT((pfn + szcpgcnt - 1) <= mpo_mblock[i].end_pfn);
1253ce8eb11aSdp78419 	return (pfn);
1254ce8eb11aSdp78419 }
1255ce8eb11aSdp78419 
1256ce8eb11aSdp78419 /*
1257ce8eb11aSdp78419  * plat_mem_node_intersect_range()
1258ce8eb11aSdp78419  *
1259ce8eb11aSdp78419  * Find the intersection between a memnode and a range of pfn's.
1260ce8eb11aSdp78419  */
1261ce8eb11aSdp78419 void
1262ce8eb11aSdp78419 plat_mem_node_intersect_range(pfn_t test_base, pgcnt_t test_len,
1263ce8eb11aSdp78419     int mnode, pgcnt_t *npages_out)
1264ce8eb11aSdp78419 {
1265ce8eb11aSdp78419 	pfn_t offset, len, hole, base, end, test_end, frag;
1266ce8eb11aSdp78419 	pfn_t nearest;
1267ce8eb11aSdp78419 	mem_stripe_t *ms;
1268ce8eb11aSdp78419 	int i, npages;
1269ce8eb11aSdp78419 
1270ce8eb11aSdp78419 	*npages_out = 0;
1271ce8eb11aSdp78419 
1272ce8eb11aSdp78419 	if (!mem_node_config[mnode].exists || test_len == 0)
1273ce8eb11aSdp78419 		return;
1274ce8eb11aSdp78419 
1275ce8eb11aSdp78419 	base = mem_node_config[mnode].physbase;
1276ce8eb11aSdp78419 	end = mem_node_config[mnode].physmax;
1277ce8eb11aSdp78419 
1278ce8eb11aSdp78419 	test_end = test_base + test_len - 1;
1279ce8eb11aSdp78419 	if (end < test_base || base > test_end)
1280ce8eb11aSdp78419 		return;
1281ce8eb11aSdp78419 
1282ce8eb11aSdp78419 	if (n_locality_groups == 1) {
1283ce8eb11aSdp78419 		*npages_out = MIN(test_end, end) - MAX(test_base, base) + 1;
1284ce8eb11aSdp78419 		return;
1285ce8eb11aSdp78419 	}
1286ce8eb11aSdp78419 
1287ce8eb11aSdp78419 	hole = mnode_stride - mnode_pages;
1288ce8eb11aSdp78419 	npages = 0;
1289ce8eb11aSdp78419 
1290ce8eb11aSdp78419 	/*
1291ce8eb11aSdp78419 	 * Iterate over all the stripes for this mnode (one per mblock),
1292ce8eb11aSdp78419 	 * find the intersection with each, and accumulate the intersections.
1293ce8eb11aSdp78419 	 *
1294ce8eb11aSdp78419 	 * Determing the intersection with a stripe is tricky.  If base or end
1295ce8eb11aSdp78419 	 * fall outside the mem_node bounds, round them to physbase/physmax of
1296ce8eb11aSdp78419 	 * mem_node.  If base or end fall in a gap, round them to start of
1297ce8eb11aSdp78419 	 * nearest stripe.  If they fall within a stripe, keep base or end,
1298ce8eb11aSdp78419 	 * but calculate the fragment size that should be excluded from the
1299ce8eb11aSdp78419 	 * stripe.  Calculate how many strides fall in the adjusted range,
1300ce8eb11aSdp78419 	 * multiply by stripe width, and add the start and end fragments.
1301ce8eb11aSdp78419 	 */
1302ce8eb11aSdp78419 
1303ce8eb11aSdp78419 	for (i = mnode; i < n_mem_stripes; i += max_locality_groups) {
1304ce8eb11aSdp78419 		ms = &mem_stripes[i];
1305ce8eb11aSdp78419 		if (ms->exists &&
1306ce8eb11aSdp78419 		    test_base <= (end = ms->physmax) &&
1307ce8eb11aSdp78419 		    test_end >= (base = ms->physbase)) {
1308ce8eb11aSdp78419 
1309ce8eb11aSdp78419 			offset = ms->offset;
1310ce8eb11aSdp78419 
1311ce8eb11aSdp78419 			if (test_base > base) {
1312ce8eb11aSdp78419 				/* Round test_base to next multiple of stride */
1313ce8eb11aSdp78419 				len = P2ROUNDUP(test_base - (base - offset),
1314ce8eb11aSdp78419 				    mnode_stride);
1315ce8eb11aSdp78419 				nearest = base - offset + len;
1316ce8eb11aSdp78419 				/*
1317ce8eb11aSdp78419 				 * Compute distance from test_base to the
1318ce8eb11aSdp78419 				 * stride boundary to see if test_base falls
1319ce8eb11aSdp78419 				 * in the stripe or in the hole.
1320ce8eb11aSdp78419 				 */
1321ce8eb11aSdp78419 				if (nearest - test_base > hole) {
1322ce8eb11aSdp78419 					/*
1323ce8eb11aSdp78419 					 * test_base lies in stripe,
1324ce8eb11aSdp78419 					 * and offset should be excluded.
1325ce8eb11aSdp78419 					 */
1326ce8eb11aSdp78419 					offset = test_base -
1327ce8eb11aSdp78419 					    (nearest - mnode_stride);
1328ce8eb11aSdp78419 					base = test_base;
1329ce8eb11aSdp78419 				} else {
1330ce8eb11aSdp78419 					/* round up to next stripe start */
1331ce8eb11aSdp78419 					offset = 0;
1332ce8eb11aSdp78419 					base = nearest;
1333ce8eb11aSdp78419 					if (base > end)
1334ce8eb11aSdp78419 						continue;
1335ce8eb11aSdp78419 				}
1336ce8eb11aSdp78419 
1337ce8eb11aSdp78419 			}
1338ce8eb11aSdp78419 
1339ce8eb11aSdp78419 			if (test_end < end)
1340ce8eb11aSdp78419 				end = test_end;
1341ce8eb11aSdp78419 			end++;		/* adjust to an exclusive bound */
1342ce8eb11aSdp78419 
1343ce8eb11aSdp78419 			/* Round end to next multiple of stride */
1344ce8eb11aSdp78419 			len = P2ROUNDUP(end - (base - offset), mnode_stride);
1345ce8eb11aSdp78419 			nearest = (base - offset) + len;
1346ce8eb11aSdp78419 			if (nearest - end <= hole) {
1347ce8eb11aSdp78419 				/* end falls in hole, use entire last stripe */
1348ce8eb11aSdp78419 				frag = 0;
1349ce8eb11aSdp78419 			} else {
1350ce8eb11aSdp78419 				/* end falls in stripe, compute fragment */
1351ce8eb11aSdp78419 				frag = nearest - hole - end;
1352ce8eb11aSdp78419 			}
1353ce8eb11aSdp78419 
1354ce8eb11aSdp78419 			len = (len >> stripe_shift) - offset - frag;
1355ce8eb11aSdp78419 			npages += len;
1356ce8eb11aSdp78419 		}
1357ce8eb11aSdp78419 	}
1358ce8eb11aSdp78419 
1359ce8eb11aSdp78419 	*npages_out = npages;
1360ce8eb11aSdp78419 }
1361ce8eb11aSdp78419 
1362ce8eb11aSdp78419 /*
1363ce8eb11aSdp78419  * valid_pages()
1364ce8eb11aSdp78419  *
1365ce8eb11aSdp78419  * Return 1 if pages are valid and do not cross mnode boundaries
1366ce8eb11aSdp78419  * (which would break page free list assumptions), and 0 otherwise.
1367ce8eb11aSdp78419  */
1368ce8eb11aSdp78419 
1369ce8eb11aSdp78419 #define	MNODE(pa)	\
1370ce8eb11aSdp78419 	((btop(pa) & home_mask_pfn) >> home_mask_pfn_shift)
1371ce8eb11aSdp78419 
1372ce8eb11aSdp78419 static int
1373ce8eb11aSdp78419 valid_pages(md_t *md, mde_cookie_t cpu0)
1374ce8eb11aSdp78419 {
1375ce8eb11aSdp78419 	int i, max_szc;
1376ce8eb11aSdp78419 	uint64_t last_page_base, szc_mask;
1377ce8eb11aSdp78419 	uint64_t max_page_len, max_coalesce_len;
1378ce8eb11aSdp78419 	struct mblock_md *mb = mpo_mblock;
1379ce8eb11aSdp78419 
1380ce8eb11aSdp78419 	/*
1381ce8eb11aSdp78419 	 * Find the smaller of the largest page possible and supported.
1382ce8eb11aSdp78419 	 * mmu_exported_pagesize_mask is not yet initialized, so read
1383ce8eb11aSdp78419 	 * it from the MD.  Apply minimal fixups in case of broken MDs
1384ce8eb11aSdp78419 	 * to get a sane mask.
1385ce8eb11aSdp78419 	 */
1386ce8eb11aSdp78419 
1387ce8eb11aSdp78419 	if (md_get_prop_val(md, cpu0, "mmu-page-size-list", &szc_mask))
1388ce8eb11aSdp78419 		szc_mask = 0;
1389ce8eb11aSdp78419 	szc_mask |=  (1 << TTE4M);	/* largest in sun4v default support */
1390ce8eb11aSdp78419 	max_szc = highbit(szc_mask) - 1;
1391ce8eb11aSdp78419 	if (max_szc > TTE256M)
1392ce8eb11aSdp78419 		max_szc = TTE256M;
1393ce8eb11aSdp78419 	max_page_len = TTEBYTES(max_szc);
1394ce8eb11aSdp78419 
1395ce8eb11aSdp78419 	/*
1396ce8eb11aSdp78419 	 * Page coalescing code coalesces all sizes up to 256M on sun4v, even
1397ce8eb11aSdp78419 	 * if mmu-page-size-list does not contain it, so 256M pages must fall
1398ce8eb11aSdp78419 	 * within one mnode to use MPO.
1399ce8eb11aSdp78419 	 */
1400ce8eb11aSdp78419 	max_coalesce_len = TTEBYTES(TTE256M);
1401ce8eb11aSdp78419 	ASSERT(max_coalesce_len >= max_page_len);
1402ce8eb11aSdp78419 
1403ce8eb11aSdp78419 	if (ptob(mnode_pages) < max_coalesce_len) {
1404ce8eb11aSdp78419 		MPO_STATUS("Page too large; MPO disabled: page = %lx, "
1405ce8eb11aSdp78419 		    "mnode slice = %lx\n", max_coalesce_len, ptob(mnode_pages));
1406ce8eb11aSdp78419 		return (0);
1407ce8eb11aSdp78419 	}
1408ce8eb11aSdp78419 
1409ce8eb11aSdp78419 	for (i = 0; i < n_mblocks; i++) {
1410ce8eb11aSdp78419 		uint64_t base = mb->base;
1411ce8eb11aSdp78419 		uint64_t end = mb->base + mb->size - 1;
1412ce8eb11aSdp78419 		uint64_t ra_to_pa = mb->ra_to_pa;
1413ce8eb11aSdp78419 
1414ce8eb11aSdp78419 		/*
1415ce8eb11aSdp78419 		 * If mblock is smaller than the max page size, then
1416ce8eb11aSdp78419 		 * RA = PA mod MAXPAGE is not guaranteed, but it must
1417ce8eb11aSdp78419 		 * not span mnodes.
1418ce8eb11aSdp78419 		 */
1419ce8eb11aSdp78419 		if (mb->size < max_page_len) {
1420ce8eb11aSdp78419 			if (MNODE(base + ra_to_pa) != MNODE(end + ra_to_pa)) {
1421ce8eb11aSdp78419 				MPO_STATUS("Small mblock spans mnodes; "
1422ce8eb11aSdp78419 				    "MPO disabled: base = %lx, end = %lx, "
1423ce8eb11aSdp78419 				    "ra2pa = %lx\n", base, end, ra_to_pa);
1424ce8eb11aSdp78419 				return (0);
1425ce8eb11aSdp78419 			}
1426ce8eb11aSdp78419 		} else {
1427ce8eb11aSdp78419 			/* Verify RA = PA mod MAXPAGE, using coalesce size */
1428ce8eb11aSdp78419 			uint64_t pa_base = base + ra_to_pa;
1429ce8eb11aSdp78419 			if ((base & (max_coalesce_len - 1)) !=
1430ce8eb11aSdp78419 			    (pa_base & (max_coalesce_len - 1))) {
1431ce8eb11aSdp78419 				MPO_STATUS("bad page alignment; MPO disabled: "
1432ce8eb11aSdp78419 				    "ra = %lx, pa = %lx, pagelen = %lx\n",
1433ce8eb11aSdp78419 				    base, pa_base, max_coalesce_len);
1434ce8eb11aSdp78419 				return (0);
1435ce8eb11aSdp78419 			}
1436ce8eb11aSdp78419 		}
1437ce8eb11aSdp78419 
1438ce8eb11aSdp78419 		/*
1439ce8eb11aSdp78419 		 * Find start of last large page in mblock in RA space.
1440ce8eb11aSdp78419 		 * If page extends into the next mblock, verify the
1441ce8eb11aSdp78419 		 * mnode does not change.
1442ce8eb11aSdp78419 		 */
1443ce8eb11aSdp78419 		last_page_base = P2ALIGN(end, max_coalesce_len);
1444ce8eb11aSdp78419 		if (i + 1 < n_mblocks &&
1445ce8eb11aSdp78419 		    last_page_base + max_coalesce_len > mb[1].base &&
1446ce8eb11aSdp78419 		    MNODE(last_page_base + ra_to_pa) !=
1447ce8eb11aSdp78419 		    MNODE(mb[1].base + mb[1].ra_to_pa)) {
1448ce8eb11aSdp78419 			MPO_STATUS("Large page spans mblocks; MPO disabled: "
1449ce8eb11aSdp78419 			    "end = %lx, ra2pa = %lx, base = %lx, ra2pa = %lx, "
1450ce8eb11aSdp78419 			    "pagelen = %lx\n", end, ra_to_pa, mb[1].base,
1451ce8eb11aSdp78419 			    mb[1].ra_to_pa, max_coalesce_len);
1452ce8eb11aSdp78419 			return (0);
1453ce8eb11aSdp78419 		}
1454ce8eb11aSdp78419 
1455ce8eb11aSdp78419 		mb++;
1456ce8eb11aSdp78419 	}
1457ce8eb11aSdp78419 	return (1);
1458ce8eb11aSdp78419 }
1459ce8eb11aSdp78419 
1460ce8eb11aSdp78419 
1461ce8eb11aSdp78419 /*
1462ce8eb11aSdp78419  * fix_interleave() - Find lgroups with sub-page sized memory interleave,
1463ce8eb11aSdp78419  * if any, and remove them.  This yields a config where the "coarse
1464ce8eb11aSdp78419  * grained" lgroups cover all of memory, even though part of that memory
1465ce8eb11aSdp78419  * is fine grain interleaved and does not deliver a purely local memory
1466ce8eb11aSdp78419  * latency.
1467ce8eb11aSdp78419  *
1468ce8eb11aSdp78419  * This function reads and modifies the globals:
1469ce8eb11aSdp78419  *	mpo_lgroup[], n_lgrpnodes
1470ce8eb11aSdp78419  *
1471ce8eb11aSdp78419  * Returns 1 if lgroup nodes were removed, 0 otherwise.
1472ce8eb11aSdp78419  */
1473ce8eb11aSdp78419 
1474ce8eb11aSdp78419 static int
1475ce8eb11aSdp78419 fix_interleave(void)
1476ce8eb11aSdp78419 {
1477ce8eb11aSdp78419 	int i, j;
1478ce8eb11aSdp78419 	uint64_t mask = 0;
1479ce8eb11aSdp78419 
1480ce8eb11aSdp78419 	j = 0;
1481ce8eb11aSdp78419 	for (i = 0; i < n_lgrpnodes; i++) {
1482ce8eb11aSdp78419 		if ((mpo_lgroup[i].addr_mask & PAGEOFFSET) != 0) {
1483ce8eb11aSdp78419 			/* remove this lgroup */
1484ce8eb11aSdp78419 			mask = mpo_lgroup[i].addr_mask;
1485ce8eb11aSdp78419 		} else {
1486ce8eb11aSdp78419 			mpo_lgroup[j++] = mpo_lgroup[i];
1487ce8eb11aSdp78419 		}
1488ce8eb11aSdp78419 	}
1489ce8eb11aSdp78419 	n_lgrpnodes = j;
1490ce8eb11aSdp78419 
1491ce8eb11aSdp78419 	if (mask != 0)
1492ce8eb11aSdp78419 		MPO_STATUS("sub-page interleave %lx found; "
1493ce8eb11aSdp78419 		    "removing lgroup.\n", mask);
1494ce8eb11aSdp78419 
1495ce8eb11aSdp78419 	return (mask != 0);
1496ce8eb11aSdp78419 }
1497