xref: /onnv-gate/usr/src/uts/sun4/os/memnode.c (revision 4769)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4769Sdp78419  * Common Development and Distribution License (the "License").
6*4769Sdp78419  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4769Sdp78419  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/platform_module.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/atomic.h>
320Sstevel@tonic-gate #include <sys/memlist.h>
330Sstevel@tonic-gate #include <sys/memnode.h>
34414Skchow #include <vm/vm_dep.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate int max_mem_nodes = 1;		/* max memory nodes on this system */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate struct mem_node_conf mem_node_config[MAX_MEM_NODES];
390Sstevel@tonic-gate int mem_node_pfn_shift;
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * num_memnodes should be updated atomically and always >=
420Sstevel@tonic-gate  * the number of bits in memnodes_mask or the algorithm may fail.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate uint16_t num_memnodes;
450Sstevel@tonic-gate mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * If set, mem_node_physalign should be a power of two, and
490Sstevel@tonic-gate  * should reflect the minimum address alignment of each node.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate uint64_t mem_node_physalign;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Platform hooks we will need.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #pragma weak plat_build_mem_nodes
580Sstevel@tonic-gate #pragma weak plat_slice_add
590Sstevel@tonic-gate #pragma weak plat_slice_del
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Adjust the memnode config after a DR operation.
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * It is rather tricky to do these updates since we can't
650Sstevel@tonic-gate  * protect the memnode structures with locks, so we must
660Sstevel@tonic-gate  * be mindful of the order in which updates and reads to
670Sstevel@tonic-gate  * these values can occur.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate void
700Sstevel@tonic-gate mem_node_add_slice(pfn_t start, pfn_t end)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	int mnode;
730Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * DR will pass us the first pfn that is allocatable.
770Sstevel@tonic-gate 	 * We need to round down to get the real start of
780Sstevel@tonic-gate 	 * the slice.
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	if (mem_node_physalign) {
810Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
820Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
85*4769Sdp78419 	if (&plat_slice_add != NULL)
860Sstevel@tonic-gate 		plat_slice_add(start, end);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
890Sstevel@tonic-gate 	ASSERT(mnode < max_mem_nodes);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) {
920Sstevel@tonic-gate 		/*
930Sstevel@tonic-gate 		 * Add slice to existing node.
940Sstevel@tonic-gate 		 */
950Sstevel@tonic-gate 		if (start < mem_node_config[mnode].physbase)
960Sstevel@tonic-gate 			mem_node_config[mnode].physbase = start;
970Sstevel@tonic-gate 		if (end > mem_node_config[mnode].physmax)
980Sstevel@tonic-gate 			mem_node_config[mnode].physmax = end;
990Sstevel@tonic-gate 	} else {
1000Sstevel@tonic-gate 		mem_node_config[mnode].physbase = start;
1010Sstevel@tonic-gate 		mem_node_config[mnode].physmax = end;
1020Sstevel@tonic-gate 		atomic_add_16(&num_memnodes, 1);
1030Sstevel@tonic-gate 		do {
1040Sstevel@tonic-gate 			oldmask = memnodes_mask;
1050Sstevel@tonic-gate 			newmask = memnodes_mask | (1ull << mnode);
1060Sstevel@tonic-gate 		} while (cas64(&memnodes_mask, oldmask, newmask) != oldmask);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	/*
1090Sstevel@tonic-gate 	 * Let the common lgrp framework know about the new memory
1100Sstevel@tonic-gate 	 */
1110Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode));
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /* ARGSUSED */
1150Sstevel@tonic-gate void
1160Sstevel@tonic-gate mem_node_pre_del_slice(pfn_t start, pfn_t end)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	int mnode = PFN_2_MEM_NODE(start);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	ASSERT(mnode < max_mem_nodes);
1210Sstevel@tonic-gate 	ASSERT(mem_node_config[mnode].exists == 1);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * Remove a PFN range from a memnode.  On some platforms,
1260Sstevel@tonic-gate  * the memnode will be created with physbase at the first
1270Sstevel@tonic-gate  * allocatable PFN, but later deleted with the MC slice
1280Sstevel@tonic-gate  * base address converted to a PFN, in which case we need
1290Sstevel@tonic-gate  * to assume physbase and up.
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate void
1320Sstevel@tonic-gate mem_node_post_del_slice(pfn_t start, pfn_t end, int cancelled)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	int mnode;
1350Sstevel@tonic-gate 	pgcnt_t delta_pgcnt, node_size;
1360Sstevel@tonic-gate 	mnodeset_t omask, nmask;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (mem_node_physalign) {
1390Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
1400Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	ASSERT(mnode < max_mem_nodes);
1450Sstevel@tonic-gate 	ASSERT(mem_node_config[mnode].exists == 1);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (!cancelled) {
1480Sstevel@tonic-gate 		delta_pgcnt = end - start;
1490Sstevel@tonic-gate 		node_size = mem_node_config[mnode].physmax -
150*4769Sdp78419 		    mem_node_config[mnode].physbase;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		if (node_size > delta_pgcnt) {
1530Sstevel@tonic-gate 			/*
1540Sstevel@tonic-gate 			 * Subtract the slice from the memnode.
1550Sstevel@tonic-gate 			 */
1560Sstevel@tonic-gate 			if (start <= mem_node_config[mnode].physbase)
1570Sstevel@tonic-gate 				mem_node_config[mnode].physbase = end + 1;
1580Sstevel@tonic-gate 			ASSERT(end <= mem_node_config[mnode].physmax);
1590Sstevel@tonic-gate 			if (end == mem_node_config[mnode].physmax)
1600Sstevel@tonic-gate 				mem_node_config[mnode].physmax = start - 1;
1610Sstevel@tonic-gate 		} else {
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 			/*
1640Sstevel@tonic-gate 			 * Let the common lgrp framework know the mnode is
1650Sstevel@tonic-gate 			 * leaving
1660Sstevel@tonic-gate 			 */
1670Sstevel@tonic-gate 			lgrp_config(LGRP_CONFIG_MEM_DEL, mnode,
1680Sstevel@tonic-gate 			    MEM_NODE_2_LGRPHAND(mnode));
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 			/*
1710Sstevel@tonic-gate 			 * Delete the whole node.
1720Sstevel@tonic-gate 			 */
173414Skchow 			ASSERT(MNODE_PGCNT(mnode) == 0);
1740Sstevel@tonic-gate 			do {
1750Sstevel@tonic-gate 				omask = memnodes_mask;
1760Sstevel@tonic-gate 				nmask = omask & ~(1ull << mnode);
1770Sstevel@tonic-gate 			} while (cas64(&memnodes_mask, omask, nmask) != omask);
1780Sstevel@tonic-gate 			atomic_add_16(&num_memnodes, -1);
1790Sstevel@tonic-gate 			mem_node_config[mnode].exists = 0;
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 
182*4769Sdp78419 		if (&plat_slice_del != NULL)
1830Sstevel@tonic-gate 			plat_slice_del(start, end);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate void
1880Sstevel@tonic-gate startup_build_mem_nodes(u_longlong_t *list, size_t nelems)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	size_t	elem;
1910Sstevel@tonic-gate 	pfn_t	basepfn;
1920Sstevel@tonic-gate 	pgcnt_t	npgs;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/* LINTED: ASSERT will always true or false */
1950Sstevel@tonic-gate 	ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes);
1960Sstevel@tonic-gate 
197*4769Sdp78419 	if (&plat_build_mem_nodes != NULL) {
1980Sstevel@tonic-gate 		plat_build_mem_nodes(list, nelems);
1990Sstevel@tonic-gate 	} else {
2000Sstevel@tonic-gate 		/*
2010Sstevel@tonic-gate 		 * Boot install lists are arranged <addr, len>, ...
2020Sstevel@tonic-gate 		 */
2030Sstevel@tonic-gate 		for (elem = 0; elem < nelems; elem += 2) {
2040Sstevel@tonic-gate 			basepfn = btop(list[elem]);
2050Sstevel@tonic-gate 			npgs = btop(list[elem+1]);
2060Sstevel@tonic-gate 			mem_node_add_slice(basepfn, basepfn + npgs - 1);
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 		mem_node_physalign = 0;
2090Sstevel@tonic-gate 		mem_node_pfn_shift = 0;
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * Allocate an unassigned memnode.
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate int
2170Sstevel@tonic-gate mem_node_alloc()
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	int mnode;
2200Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * Find an unused memnode.  Update it atomically to prevent
2240Sstevel@tonic-gate 	 * a first time memnode creation race.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	for (mnode = 0; mnode < max_mem_nodes; mnode++)
2270Sstevel@tonic-gate 		if (cas32((uint32_t *)&mem_node_config[mnode].exists,
228*4769Sdp78419 		    0, 1) == 0)
2290Sstevel@tonic-gate 			break;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (mnode >= max_mem_nodes)
2320Sstevel@tonic-gate 			panic("Out of free memnodes\n");
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	mem_node_config[mnode].physbase = (uint64_t)-1;
2350Sstevel@tonic-gate 	mem_node_config[mnode].physmax = 0;
2360Sstevel@tonic-gate 	atomic_add_16(&num_memnodes, 1);
2370Sstevel@tonic-gate 	do {
2380Sstevel@tonic-gate 		oldmask = memnodes_mask;
2390Sstevel@tonic-gate 		newmask = memnodes_mask | (1ull << mnode);
2400Sstevel@tonic-gate 	} while (cas64(&memnodes_mask, oldmask, newmask) != oldmask);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	return (mnode);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Find the intersection between a memnode and a memlist
2470Sstevel@tonic-gate  * and returns the number of pages that overlap.
2480Sstevel@tonic-gate  *
249*4769Sdp78419  * Grab the memlist lock to protect the list from DR operations.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate pgcnt_t
2520Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	pfn_t		base, end;
2550Sstevel@tonic-gate 	pfn_t		cur_base, cur_end;
256*4769Sdp78419 	pgcnt_t		npgs = 0;
257*4769Sdp78419 	pgcnt_t		pages;
2580Sstevel@tonic-gate 	struct memlist	*pmem;
2590Sstevel@tonic-gate 
260*4769Sdp78419 	if (&plat_mem_node_intersect_range != NULL) {
261*4769Sdp78419 		memlist_read_lock();
262*4769Sdp78419 
263*4769Sdp78419 		for (pmem = mlist; pmem; pmem = pmem->next) {
264*4769Sdp78419 			plat_mem_node_intersect_range(btop(pmem->address),
265*4769Sdp78419 			    btop(pmem->size), mnode, &pages);
266*4769Sdp78419 			npgs += pages;
267*4769Sdp78419 		}
268*4769Sdp78419 
269*4769Sdp78419 		memlist_read_unlock();
270*4769Sdp78419 		return (npgs);
271*4769Sdp78419 	}
272*4769Sdp78419 
2730Sstevel@tonic-gate 	base = mem_node_config[mnode].physbase;
2740Sstevel@tonic-gate 	end = mem_node_config[mnode].physmax;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	memlist_read_lock();
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	for (pmem = mlist; pmem; pmem = pmem->next) {
2790Sstevel@tonic-gate 		cur_base = btop(pmem->address);
2800Sstevel@tonic-gate 		cur_end = cur_base + btop(pmem->size) - 1;
281*4769Sdp78419 		if (end < cur_base || base > cur_end)
2820Sstevel@tonic-gate 			continue;
2830Sstevel@tonic-gate 		npgs = npgs + (MIN(cur_end, end) -
2840Sstevel@tonic-gate 		    MAX(cur_base, base)) + 1;
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	memlist_read_unlock();
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	return (npgs);
2900Sstevel@tonic-gate }
291*4769Sdp78419 
292*4769Sdp78419 /*
293*4769Sdp78419  * Find MIN(physbase) and MAX(physmax) over all mnodes
294*4769Sdp78419  *
295*4769Sdp78419  * Called during startup and DR to find hpm_counters limits when
296*4769Sdp78419  * interleaved_mnodes is set.
297*4769Sdp78419  * NOTE: there is a race condition with DR if it tries to change more than
298*4769Sdp78419  * one mnode in parallel. Sizing shared hpm_counters depends on finding the
299*4769Sdp78419  * min(physbase) and max(physmax) across all mnodes. Therefore, the caller of
300*4769Sdp78419  * page_ctrs_adjust must ensure that mem_node_config does not change while it
301*4769Sdp78419  * is running.
302*4769Sdp78419  */
303*4769Sdp78419 void
304*4769Sdp78419 mem_node_max_range(pfn_t *basep, pfn_t *maxp)
305*4769Sdp78419 {
306*4769Sdp78419 	int mnode;
307*4769Sdp78419 	pfn_t max = 0;
308*4769Sdp78419 	pfn_t base = (pfn_t)-1;
309*4769Sdp78419 
310*4769Sdp78419 	for (mnode = 0; mnode < max_mem_nodes; mnode++) {
311*4769Sdp78419 		if (mem_node_config[mnode].exists == 0)
312*4769Sdp78419 			continue;
313*4769Sdp78419 		if (max < mem_node_config[mnode].physmax)
314*4769Sdp78419 			max = mem_node_config[mnode].physmax;
315*4769Sdp78419 		if (base > mem_node_config[mnode].physbase)
316*4769Sdp78419 			base = mem_node_config[mnode].physbase;
317*4769Sdp78419 	}
318*4769Sdp78419 	ASSERT(base != (pfn_t)-1 && max != 0);
319*4769Sdp78419 	*basep = base;
320*4769Sdp78419 	*maxp = max;
321*4769Sdp78419 }
322