xref: /onnv-gate/usr/src/uts/sun4/os/memnode.c (revision 11474:857f9db4ef05)
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
54769Sdp78419  * Common Development and Distribution License (the "License").
64769Sdp78419  * 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*11474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/systm.h>
270Sstevel@tonic-gate #include <sys/platform_module.h>
280Sstevel@tonic-gate #include <sys/sysmacros.h>
290Sstevel@tonic-gate #include <sys/atomic.h>
300Sstevel@tonic-gate #include <sys/memlist.h>
310Sstevel@tonic-gate #include <sys/memnode.h>
32414Skchow #include <vm/vm_dep.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate int max_mem_nodes = 1;		/* max memory nodes on this system */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate struct mem_node_conf mem_node_config[MAX_MEM_NODES];
370Sstevel@tonic-gate int mem_node_pfn_shift;
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * num_memnodes should be updated atomically and always >=
400Sstevel@tonic-gate  * the number of bits in memnodes_mask or the algorithm may fail.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate uint16_t num_memnodes;
430Sstevel@tonic-gate mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * If set, mem_node_physalign should be a power of two, and
470Sstevel@tonic-gate  * should reflect the minimum address alignment of each node.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate uint64_t mem_node_physalign;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Platform hooks we will need.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #pragma weak plat_build_mem_nodes
560Sstevel@tonic-gate #pragma weak plat_slice_add
570Sstevel@tonic-gate #pragma weak plat_slice_del
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Adjust the memnode config after a DR operation.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * It is rather tricky to do these updates since we can't
630Sstevel@tonic-gate  * protect the memnode structures with locks, so we must
640Sstevel@tonic-gate  * be mindful of the order in which updates and reads to
650Sstevel@tonic-gate  * these values can occur.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate void
mem_node_add_slice(pfn_t start,pfn_t end)680Sstevel@tonic-gate mem_node_add_slice(pfn_t start, pfn_t end)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	int mnode;
710Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/*
740Sstevel@tonic-gate 	 * DR will pass us the first pfn that is allocatable.
750Sstevel@tonic-gate 	 * We need to round down to get the real start of
760Sstevel@tonic-gate 	 * the slice.
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 	if (mem_node_physalign) {
790Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
800Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
840Sstevel@tonic-gate 	ASSERT(mnode < max_mem_nodes);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) {
870Sstevel@tonic-gate 		/*
880Sstevel@tonic-gate 		 * Add slice to existing node.
890Sstevel@tonic-gate 		 */
900Sstevel@tonic-gate 		if (start < mem_node_config[mnode].physbase)
910Sstevel@tonic-gate 			mem_node_config[mnode].physbase = start;
920Sstevel@tonic-gate 		if (end > mem_node_config[mnode].physmax)
930Sstevel@tonic-gate 			mem_node_config[mnode].physmax = end;
940Sstevel@tonic-gate 	} else {
950Sstevel@tonic-gate 		mem_node_config[mnode].physbase = start;
960Sstevel@tonic-gate 		mem_node_config[mnode].physmax = end;
970Sstevel@tonic-gate 		atomic_add_16(&num_memnodes, 1);
980Sstevel@tonic-gate 		do {
990Sstevel@tonic-gate 			oldmask = memnodes_mask;
1000Sstevel@tonic-gate 			newmask = memnodes_mask | (1ull << mnode);
1010Sstevel@tonic-gate 		} while (cas64(&memnodes_mask, oldmask, newmask) != oldmask);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	/*
1040Sstevel@tonic-gate 	 * Let the common lgrp framework know about the new memory
1050Sstevel@tonic-gate 	 */
1060Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode));
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Remove a PFN range from a memnode.  On some platforms,
1110Sstevel@tonic-gate  * the memnode will be created with physbase at the first
1120Sstevel@tonic-gate  * allocatable PFN, but later deleted with the MC slice
1130Sstevel@tonic-gate  * base address converted to a PFN, in which case we need
1140Sstevel@tonic-gate  * to assume physbase and up.
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate void
mem_node_del_slice(pfn_t start,pfn_t end)11710106SJason.Beloro@Sun.COM mem_node_del_slice(pfn_t start, pfn_t end)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	int mnode;
1200Sstevel@tonic-gate 	pgcnt_t delta_pgcnt, node_size;
1210Sstevel@tonic-gate 	mnodeset_t omask, nmask;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (mem_node_physalign) {
1240Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
1250Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	ASSERT(mnode < max_mem_nodes);
1300Sstevel@tonic-gate 	ASSERT(mem_node_config[mnode].exists == 1);
1310Sstevel@tonic-gate 
13210106SJason.Beloro@Sun.COM 	delta_pgcnt = end - start;
13310106SJason.Beloro@Sun.COM 	node_size = mem_node_config[mnode].physmax -
13410106SJason.Beloro@Sun.COM 	    mem_node_config[mnode].physbase;
1350Sstevel@tonic-gate 
13610106SJason.Beloro@Sun.COM 	if (node_size > delta_pgcnt) {
13710106SJason.Beloro@Sun.COM 		/*
13810106SJason.Beloro@Sun.COM 		 * Subtract the slice from the memnode.
13910106SJason.Beloro@Sun.COM 		 */
14010106SJason.Beloro@Sun.COM 		if (start <= mem_node_config[mnode].physbase)
14110106SJason.Beloro@Sun.COM 			mem_node_config[mnode].physbase = end + 1;
14210106SJason.Beloro@Sun.COM 		ASSERT(end <= mem_node_config[mnode].physmax);
14310106SJason.Beloro@Sun.COM 		if (end == mem_node_config[mnode].physmax)
14410106SJason.Beloro@Sun.COM 			mem_node_config[mnode].physmax = start - 1;
14510106SJason.Beloro@Sun.COM 	} else {
14610106SJason.Beloro@Sun.COM 
14710106SJason.Beloro@Sun.COM 		/*
14810106SJason.Beloro@Sun.COM 		 * Let the common lgrp framework know the mnode is
14910106SJason.Beloro@Sun.COM 		 * leaving
15010106SJason.Beloro@Sun.COM 		 */
15110106SJason.Beloro@Sun.COM 		lgrp_config(LGRP_CONFIG_MEM_DEL, mnode,
15210106SJason.Beloro@Sun.COM 		    MEM_NODE_2_LGRPHAND(mnode));
1530Sstevel@tonic-gate 
15410106SJason.Beloro@Sun.COM 		/*
15510106SJason.Beloro@Sun.COM 		 * Delete the whole node.
15610106SJason.Beloro@Sun.COM 		 */
15710106SJason.Beloro@Sun.COM 		ASSERT(MNODE_PGCNT(mnode) == 0);
15810106SJason.Beloro@Sun.COM 		do {
15910106SJason.Beloro@Sun.COM 			omask = memnodes_mask;
16010106SJason.Beloro@Sun.COM 			nmask = omask & ~(1ull << mnode);
16110106SJason.Beloro@Sun.COM 		} while (cas64(&memnodes_mask, omask, nmask) != omask);
16210106SJason.Beloro@Sun.COM 		atomic_add_16(&num_memnodes, -1);
16310106SJason.Beloro@Sun.COM 		mem_node_config[mnode].exists = 0;
16410106SJason.Beloro@Sun.COM 	}
16510106SJason.Beloro@Sun.COM }
1660Sstevel@tonic-gate 
16710106SJason.Beloro@Sun.COM void
mem_node_add_range(pfn_t start,pfn_t end)16810106SJason.Beloro@Sun.COM mem_node_add_range(pfn_t start, pfn_t end)
16910106SJason.Beloro@Sun.COM {
17010106SJason.Beloro@Sun.COM 	if (&plat_slice_add != NULL)
17110106SJason.Beloro@Sun.COM 		plat_slice_add(start, end);
17210106SJason.Beloro@Sun.COM 	else
17310106SJason.Beloro@Sun.COM 		mem_node_add_slice(start, end);
17410106SJason.Beloro@Sun.COM }
1750Sstevel@tonic-gate 
17610106SJason.Beloro@Sun.COM void
mem_node_del_range(pfn_t start,pfn_t end)17710106SJason.Beloro@Sun.COM mem_node_del_range(pfn_t start, pfn_t end)
17810106SJason.Beloro@Sun.COM {
17910106SJason.Beloro@Sun.COM 	if (&plat_slice_del != NULL)
18010106SJason.Beloro@Sun.COM 		plat_slice_del(start, end);
18110106SJason.Beloro@Sun.COM 	else
18210106SJason.Beloro@Sun.COM 		mem_node_del_slice(start, end);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate void
startup_build_mem_nodes(prom_memlist_t * list,size_t nelems)1865648Ssetje startup_build_mem_nodes(prom_memlist_t *list, size_t nelems)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	size_t	elem;
1890Sstevel@tonic-gate 	pfn_t	basepfn;
1900Sstevel@tonic-gate 	pgcnt_t	npgs;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/* LINTED: ASSERT will always true or false */
1930Sstevel@tonic-gate 	ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes);
1940Sstevel@tonic-gate 
1954769Sdp78419 	if (&plat_build_mem_nodes != NULL) {
1960Sstevel@tonic-gate 		plat_build_mem_nodes(list, nelems);
1970Sstevel@tonic-gate 	} else {
1980Sstevel@tonic-gate 		/*
1990Sstevel@tonic-gate 		 * Boot install lists are arranged <addr, len>, ...
2000Sstevel@tonic-gate 		 */
2015648Ssetje 		for (elem = 0; elem < nelems; list++, elem++) {
2025648Ssetje 			basepfn = btop(list->addr);
2035648Ssetje 			npgs = btop(list->size);
20410106SJason.Beloro@Sun.COM 			mem_node_add_range(basepfn, basepfn + npgs - 1);
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * Allocate an unassigned memnode.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate int
mem_node_alloc()2130Sstevel@tonic-gate mem_node_alloc()
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	int mnode;
2160Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Find an unused memnode.  Update it atomically to prevent
2200Sstevel@tonic-gate 	 * a first time memnode creation race.
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	for (mnode = 0; mnode < max_mem_nodes; mnode++)
2230Sstevel@tonic-gate 		if (cas32((uint32_t *)&mem_node_config[mnode].exists,
2244769Sdp78419 		    0, 1) == 0)
2250Sstevel@tonic-gate 			break;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (mnode >= max_mem_nodes)
2280Sstevel@tonic-gate 			panic("Out of free memnodes\n");
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	mem_node_config[mnode].physbase = (uint64_t)-1;
2310Sstevel@tonic-gate 	mem_node_config[mnode].physmax = 0;
2320Sstevel@tonic-gate 	atomic_add_16(&num_memnodes, 1);
2330Sstevel@tonic-gate 	do {
2340Sstevel@tonic-gate 		oldmask = memnodes_mask;
2350Sstevel@tonic-gate 		newmask = memnodes_mask | (1ull << mnode);
2360Sstevel@tonic-gate 	} while (cas64(&memnodes_mask, oldmask, newmask) != oldmask);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	return (mnode);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Find the intersection between a memnode and a memlist
2430Sstevel@tonic-gate  * and returns the number of pages that overlap.
2440Sstevel@tonic-gate  *
2454769Sdp78419  * Grab the memlist lock to protect the list from DR operations.
2460Sstevel@tonic-gate  */
2470Sstevel@tonic-gate pgcnt_t
mem_node_memlist_pages(int mnode,struct memlist * mlist)2480Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	pfn_t		base, end;
2510Sstevel@tonic-gate 	pfn_t		cur_base, cur_end;
2524769Sdp78419 	pgcnt_t		npgs = 0;
2534769Sdp78419 	pgcnt_t		pages;
2540Sstevel@tonic-gate 	struct memlist	*pmem;
2550Sstevel@tonic-gate 
2564769Sdp78419 	if (&plat_mem_node_intersect_range != NULL) {
2574769Sdp78419 		memlist_read_lock();
2584769Sdp78419 
259*11474SJonathan.Adams@Sun.COM 		for (pmem = mlist; pmem; pmem = pmem->ml_next) {
260*11474SJonathan.Adams@Sun.COM 			plat_mem_node_intersect_range(btop(pmem->ml_address),
261*11474SJonathan.Adams@Sun.COM 			    btop(pmem->ml_size), mnode, &pages);
2624769Sdp78419 			npgs += pages;
2634769Sdp78419 		}
2644769Sdp78419 
2654769Sdp78419 		memlist_read_unlock();
2664769Sdp78419 		return (npgs);
2674769Sdp78419 	}
2684769Sdp78419 
2690Sstevel@tonic-gate 	base = mem_node_config[mnode].physbase;
2700Sstevel@tonic-gate 	end = mem_node_config[mnode].physmax;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	memlist_read_lock();
2730Sstevel@tonic-gate 
274*11474SJonathan.Adams@Sun.COM 	for (pmem = mlist; pmem; pmem = pmem->ml_next) {
275*11474SJonathan.Adams@Sun.COM 		cur_base = btop(pmem->ml_address);
276*11474SJonathan.Adams@Sun.COM 		cur_end = cur_base + btop(pmem->ml_size) - 1;
2774769Sdp78419 		if (end < cur_base || base > cur_end)
2780Sstevel@tonic-gate 			continue;
2790Sstevel@tonic-gate 		npgs = npgs + (MIN(cur_end, end) -
2800Sstevel@tonic-gate 		    MAX(cur_base, base)) + 1;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	memlist_read_unlock();
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	return (npgs);
2860Sstevel@tonic-gate }
2874769Sdp78419 
2884769Sdp78419 /*
2894769Sdp78419  * Find MIN(physbase) and MAX(physmax) over all mnodes
2904769Sdp78419  *
2914769Sdp78419  * Called during startup and DR to find hpm_counters limits when
2924769Sdp78419  * interleaved_mnodes is set.
2934769Sdp78419  * NOTE: there is a race condition with DR if it tries to change more than
2944769Sdp78419  * one mnode in parallel. Sizing shared hpm_counters depends on finding the
2954769Sdp78419  * min(physbase) and max(physmax) across all mnodes. Therefore, the caller of
2964769Sdp78419  * page_ctrs_adjust must ensure that mem_node_config does not change while it
2974769Sdp78419  * is running.
2984769Sdp78419  */
2994769Sdp78419 void
mem_node_max_range(pfn_t * basep,pfn_t * maxp)3004769Sdp78419 mem_node_max_range(pfn_t *basep, pfn_t *maxp)
3014769Sdp78419 {
3024769Sdp78419 	int mnode;
3034769Sdp78419 	pfn_t max = 0;
3044769Sdp78419 	pfn_t base = (pfn_t)-1;
3054769Sdp78419 
3064769Sdp78419 	for (mnode = 0; mnode < max_mem_nodes; mnode++) {
3074769Sdp78419 		if (mem_node_config[mnode].exists == 0)
3084769Sdp78419 			continue;
3094769Sdp78419 		if (max < mem_node_config[mnode].physmax)
3104769Sdp78419 			max = mem_node_config[mnode].physmax;
3114769Sdp78419 		if (base > mem_node_config[mnode].physbase)
3124769Sdp78419 			base = mem_node_config[mnode].physbase;
3134769Sdp78419 	}
3144769Sdp78419 	ASSERT(base != (pfn_t)-1 && max != 0);
3154769Sdp78419 	*basep = base;
3164769Sdp78419 	*maxp = max;
3174769Sdp78419 }
318