xref: /titanic_51/usr/src/uts/sun4/os/memnode.c (revision 9853d9e82e7a067a2b88dae2fd257207e6be5f94)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * 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.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*9853d9e8SJason Beloro  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/systm.h>
277c478bd9Sstevel@tonic-gate #include <sys/platform_module.h>
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
307c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
317c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
32affbd3ccSkchow #include <vm/vm_dep.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate int max_mem_nodes = 1;		/* max memory nodes on this system */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate struct mem_node_conf mem_node_config[MAX_MEM_NODES];
377c478bd9Sstevel@tonic-gate int mem_node_pfn_shift;
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * num_memnodes should be updated atomically and always >=
407c478bd9Sstevel@tonic-gate  * the number of bits in memnodes_mask or the algorithm may fail.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate uint16_t num_memnodes;
437c478bd9Sstevel@tonic-gate mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * If set, mem_node_physalign should be a power of two, and
477c478bd9Sstevel@tonic-gate  * should reflect the minimum address alignment of each node.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate uint64_t mem_node_physalign;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Platform hooks we will need.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #pragma weak plat_build_mem_nodes
567c478bd9Sstevel@tonic-gate #pragma weak plat_slice_add
577c478bd9Sstevel@tonic-gate #pragma weak plat_slice_del
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Adjust the memnode config after a DR operation.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * It is rather tricky to do these updates since we can't
637c478bd9Sstevel@tonic-gate  * protect the memnode structures with locks, so we must
647c478bd9Sstevel@tonic-gate  * be mindful of the order in which updates and reads to
657c478bd9Sstevel@tonic-gate  * these values can occur.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate void
687c478bd9Sstevel@tonic-gate mem_node_add_slice(pfn_t start, pfn_t end)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	int mnode;
717c478bd9Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * DR will pass us the first pfn that is allocatable.
757c478bd9Sstevel@tonic-gate 	 * We need to round down to get the real start of
767c478bd9Sstevel@tonic-gate 	 * the slice.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	if (mem_node_physalign) {
797c478bd9Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
807c478bd9Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
847c478bd9Sstevel@tonic-gate 	ASSERT(mnode < max_mem_nodes);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) {
877c478bd9Sstevel@tonic-gate 		/*
887c478bd9Sstevel@tonic-gate 		 * Add slice to existing node.
897c478bd9Sstevel@tonic-gate 		 */
907c478bd9Sstevel@tonic-gate 		if (start < mem_node_config[mnode].physbase)
917c478bd9Sstevel@tonic-gate 			mem_node_config[mnode].physbase = start;
927c478bd9Sstevel@tonic-gate 		if (end > mem_node_config[mnode].physmax)
937c478bd9Sstevel@tonic-gate 			mem_node_config[mnode].physmax = end;
947c478bd9Sstevel@tonic-gate 	} else {
957c478bd9Sstevel@tonic-gate 		mem_node_config[mnode].physbase = start;
967c478bd9Sstevel@tonic-gate 		mem_node_config[mnode].physmax = end;
977c478bd9Sstevel@tonic-gate 		atomic_add_16(&num_memnodes, 1);
987c478bd9Sstevel@tonic-gate 		do {
997c478bd9Sstevel@tonic-gate 			oldmask = memnodes_mask;
1007c478bd9Sstevel@tonic-gate 			newmask = memnodes_mask | (1ull << mnode);
1017c478bd9Sstevel@tonic-gate 		} while (cas64(&memnodes_mask, oldmask, newmask) != oldmask);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * Let the common lgrp framework know about the new memory
1057c478bd9Sstevel@tonic-gate 	 */
1067c478bd9Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode));
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Remove a PFN range from a memnode.  On some platforms,
1117c478bd9Sstevel@tonic-gate  * the memnode will be created with physbase at the first
1127c478bd9Sstevel@tonic-gate  * allocatable PFN, but later deleted with the MC slice
1137c478bd9Sstevel@tonic-gate  * base address converted to a PFN, in which case we need
1147c478bd9Sstevel@tonic-gate  * to assume physbase and up.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate void
117*9853d9e8SJason Beloro mem_node_del_slice(pfn_t start, pfn_t end)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	int mnode;
1207c478bd9Sstevel@tonic-gate 	pgcnt_t delta_pgcnt, node_size;
1217c478bd9Sstevel@tonic-gate 	mnodeset_t omask, nmask;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (mem_node_physalign) {
1247c478bd9Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
1257c478bd9Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	ASSERT(mnode < max_mem_nodes);
1307c478bd9Sstevel@tonic-gate 	ASSERT(mem_node_config[mnode].exists == 1);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	delta_pgcnt = end - start;
1337c478bd9Sstevel@tonic-gate 	node_size = mem_node_config[mnode].physmax -
1347c478bd9Sstevel@tonic-gate 	    mem_node_config[mnode].physbase;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (node_size > delta_pgcnt) {
1377c478bd9Sstevel@tonic-gate 		/*
1387c478bd9Sstevel@tonic-gate 		 * Subtract the slice from the memnode.
1397c478bd9Sstevel@tonic-gate 		 */
1407c478bd9Sstevel@tonic-gate 		if (start <= mem_node_config[mnode].physbase)
1417c478bd9Sstevel@tonic-gate 			mem_node_config[mnode].physbase = end + 1;
1427c478bd9Sstevel@tonic-gate 		ASSERT(end <= mem_node_config[mnode].physmax);
1437c478bd9Sstevel@tonic-gate 		if (end == mem_node_config[mnode].physmax)
1447c478bd9Sstevel@tonic-gate 			mem_node_config[mnode].physmax = start - 1;
1457c478bd9Sstevel@tonic-gate 	} else {
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		/*
1487c478bd9Sstevel@tonic-gate 		 * Let the common lgrp framework know the mnode is
1497c478bd9Sstevel@tonic-gate 		 * leaving
1507c478bd9Sstevel@tonic-gate 		 */
1517c478bd9Sstevel@tonic-gate 		lgrp_config(LGRP_CONFIG_MEM_DEL, mnode,
1527c478bd9Sstevel@tonic-gate 		    MEM_NODE_2_LGRPHAND(mnode));
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		/*
1557c478bd9Sstevel@tonic-gate 		 * Delete the whole node.
1567c478bd9Sstevel@tonic-gate 		 */
157affbd3ccSkchow 		ASSERT(MNODE_PGCNT(mnode) == 0);
1587c478bd9Sstevel@tonic-gate 		do {
1597c478bd9Sstevel@tonic-gate 			omask = memnodes_mask;
1607c478bd9Sstevel@tonic-gate 			nmask = omask & ~(1ull << mnode);
1617c478bd9Sstevel@tonic-gate 		} while (cas64(&memnodes_mask, omask, nmask) != omask);
1627c478bd9Sstevel@tonic-gate 		atomic_add_16(&num_memnodes, -1);
1637c478bd9Sstevel@tonic-gate 		mem_node_config[mnode].exists = 0;
1647c478bd9Sstevel@tonic-gate 	}
165*9853d9e8SJason Beloro }
1667c478bd9Sstevel@tonic-gate 
167*9853d9e8SJason Beloro void
168*9853d9e8SJason Beloro mem_node_add_range(pfn_t start, pfn_t end)
169*9853d9e8SJason Beloro {
170*9853d9e8SJason Beloro 	if (&plat_slice_add != NULL)
171*9853d9e8SJason Beloro 		plat_slice_add(start, end);
172*9853d9e8SJason Beloro 	else
173*9853d9e8SJason Beloro 		mem_node_add_slice(start, end);
174*9853d9e8SJason Beloro }
175*9853d9e8SJason Beloro 
176*9853d9e8SJason Beloro void
177*9853d9e8SJason Beloro mem_node_del_range(pfn_t start, pfn_t end)
178*9853d9e8SJason Beloro {
179ce8eb11aSdp78419 	if (&plat_slice_del != NULL)
1807c478bd9Sstevel@tonic-gate 		plat_slice_del(start, end);
181*9853d9e8SJason Beloro 	else
182*9853d9e8SJason Beloro 		mem_node_del_slice(start, end);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate void
186986fd29aSsetje startup_build_mem_nodes(prom_memlist_t *list, size_t nelems)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	size_t	elem;
1897c478bd9Sstevel@tonic-gate 	pfn_t	basepfn;
1907c478bd9Sstevel@tonic-gate 	pgcnt_t	npgs;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* LINTED: ASSERT will always true or false */
1937c478bd9Sstevel@tonic-gate 	ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes);
1947c478bd9Sstevel@tonic-gate 
195ce8eb11aSdp78419 	if (&plat_build_mem_nodes != NULL) {
1967c478bd9Sstevel@tonic-gate 		plat_build_mem_nodes(list, nelems);
1977c478bd9Sstevel@tonic-gate 	} else {
1987c478bd9Sstevel@tonic-gate 		/*
1997c478bd9Sstevel@tonic-gate 		 * Boot install lists are arranged <addr, len>, ...
2007c478bd9Sstevel@tonic-gate 		 */
201986fd29aSsetje 		for (elem = 0; elem < nelems; list++, elem++) {
202986fd29aSsetje 			basepfn = btop(list->addr);
203986fd29aSsetje 			npgs = btop(list->size);
204*9853d9e8SJason Beloro 			mem_node_add_range(basepfn, basepfn + npgs - 1);
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Allocate an unassigned memnode.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate int
2137c478bd9Sstevel@tonic-gate mem_node_alloc()
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	int mnode;
2167c478bd9Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Find an unused memnode.  Update it atomically to prevent
2207c478bd9Sstevel@tonic-gate 	 * a first time memnode creation race.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	for (mnode = 0; mnode < max_mem_nodes; mnode++)
2237c478bd9Sstevel@tonic-gate 		if (cas32((uint32_t *)&mem_node_config[mnode].exists,
2247c478bd9Sstevel@tonic-gate 		    0, 1) == 0)
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (mnode >= max_mem_nodes)
2287c478bd9Sstevel@tonic-gate 			panic("Out of free memnodes\n");
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	mem_node_config[mnode].physbase = (uint64_t)-1;
2317c478bd9Sstevel@tonic-gate 	mem_node_config[mnode].physmax = 0;
2327c478bd9Sstevel@tonic-gate 	atomic_add_16(&num_memnodes, 1);
2337c478bd9Sstevel@tonic-gate 	do {
2347c478bd9Sstevel@tonic-gate 		oldmask = memnodes_mask;
2357c478bd9Sstevel@tonic-gate 		newmask = memnodes_mask | (1ull << mnode);
2367c478bd9Sstevel@tonic-gate 	} while (cas64(&memnodes_mask, oldmask, newmask) != oldmask);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (mnode);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * Find the intersection between a memnode and a memlist
2437c478bd9Sstevel@tonic-gate  * and returns the number of pages that overlap.
2447c478bd9Sstevel@tonic-gate  *
245ce8eb11aSdp78419  * Grab the memlist lock to protect the list from DR operations.
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate pgcnt_t
2487c478bd9Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	pfn_t		base, end;
2517c478bd9Sstevel@tonic-gate 	pfn_t		cur_base, cur_end;
252ce8eb11aSdp78419 	pgcnt_t		npgs = 0;
253ce8eb11aSdp78419 	pgcnt_t		pages;
2547c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
2557c478bd9Sstevel@tonic-gate 
256ce8eb11aSdp78419 	if (&plat_mem_node_intersect_range != NULL) {
257ce8eb11aSdp78419 		memlist_read_lock();
258ce8eb11aSdp78419 
259ce8eb11aSdp78419 		for (pmem = mlist; pmem; pmem = pmem->next) {
260ce8eb11aSdp78419 			plat_mem_node_intersect_range(btop(pmem->address),
261ce8eb11aSdp78419 			    btop(pmem->size), mnode, &pages);
262ce8eb11aSdp78419 			npgs += pages;
263ce8eb11aSdp78419 		}
264ce8eb11aSdp78419 
265ce8eb11aSdp78419 		memlist_read_unlock();
266ce8eb11aSdp78419 		return (npgs);
267ce8eb11aSdp78419 	}
268ce8eb11aSdp78419 
2697c478bd9Sstevel@tonic-gate 	base = mem_node_config[mnode].physbase;
2707c478bd9Sstevel@tonic-gate 	end = mem_node_config[mnode].physmax;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	memlist_read_lock();
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	for (pmem = mlist; pmem; pmem = pmem->next) {
2757c478bd9Sstevel@tonic-gate 		cur_base = btop(pmem->address);
2767c478bd9Sstevel@tonic-gate 		cur_end = cur_base + btop(pmem->size) - 1;
277ce8eb11aSdp78419 		if (end < cur_base || base > cur_end)
2787c478bd9Sstevel@tonic-gate 			continue;
2797c478bd9Sstevel@tonic-gate 		npgs = npgs + (MIN(cur_end, end) -
2807c478bd9Sstevel@tonic-gate 		    MAX(cur_base, base)) + 1;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	return (npgs);
2867c478bd9Sstevel@tonic-gate }
287ce8eb11aSdp78419 
288ce8eb11aSdp78419 /*
289ce8eb11aSdp78419  * Find MIN(physbase) and MAX(physmax) over all mnodes
290ce8eb11aSdp78419  *
291ce8eb11aSdp78419  * Called during startup and DR to find hpm_counters limits when
292ce8eb11aSdp78419  * interleaved_mnodes is set.
293ce8eb11aSdp78419  * NOTE: there is a race condition with DR if it tries to change more than
294ce8eb11aSdp78419  * one mnode in parallel. Sizing shared hpm_counters depends on finding the
295ce8eb11aSdp78419  * min(physbase) and max(physmax) across all mnodes. Therefore, the caller of
296ce8eb11aSdp78419  * page_ctrs_adjust must ensure that mem_node_config does not change while it
297ce8eb11aSdp78419  * is running.
298ce8eb11aSdp78419  */
299ce8eb11aSdp78419 void
300ce8eb11aSdp78419 mem_node_max_range(pfn_t *basep, pfn_t *maxp)
301ce8eb11aSdp78419 {
302ce8eb11aSdp78419 	int mnode;
303ce8eb11aSdp78419 	pfn_t max = 0;
304ce8eb11aSdp78419 	pfn_t base = (pfn_t)-1;
305ce8eb11aSdp78419 
306ce8eb11aSdp78419 	for (mnode = 0; mnode < max_mem_nodes; mnode++) {
307ce8eb11aSdp78419 		if (mem_node_config[mnode].exists == 0)
308ce8eb11aSdp78419 			continue;
309ce8eb11aSdp78419 		if (max < mem_node_config[mnode].physmax)
310ce8eb11aSdp78419 			max = mem_node_config[mnode].physmax;
311ce8eb11aSdp78419 		if (base > mem_node_config[mnode].physbase)
312ce8eb11aSdp78419 			base = mem_node_config[mnode].physbase;
313ce8eb11aSdp78419 	}
314ce8eb11aSdp78419 	ASSERT(base != (pfn_t)-1 && max != 0);
315ce8eb11aSdp78419 	*basep = base;
316ce8eb11aSdp78419 	*maxp = max;
317ce8eb11aSdp78419 }
318