xref: /illumos-gate/usr/src/uts/i86pc/os/memnode.c (revision 1a5e258f)
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
5ce8eb11aSdp  * Common Development and Distribution License (the "License").
6ce8eb11aSdp  * 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 /*
2256f33205SJonathan Adams  * Copyright 2010 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/sysmacros.h>
287c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
297c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
307c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
317c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
327c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
337c478bd9Sstevel@tonic-gate #include <sys/platform_module.h>
34affbd3ccSkchow #include <vm/vm_dep.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate int	max_mem_nodes = 1;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate struct mem_node_conf mem_node_config[MAX_MEM_NODES];
397c478bd9Sstevel@tonic-gate int mem_node_pfn_shift;
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * num_memnodes should be updated atomically and always >=
427c478bd9Sstevel@tonic-gate  * the number of bits in memnodes_mask or the algorithm may fail.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate uint16_t num_memnodes;
457c478bd9Sstevel@tonic-gate mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * If set, mem_node_physalign should be a power of two, and
497c478bd9Sstevel@tonic-gate  * should reflect the minimum address alignment of each node.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate uint64_t mem_node_physalign;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Platform hooks we will need.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #pragma weak plat_build_mem_nodes
587c478bd9Sstevel@tonic-gate #pragma weak plat_slice_add
597c478bd9Sstevel@tonic-gate #pragma weak plat_slice_del
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Adjust the memnode config after a DR operation.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * It is rather tricky to do these updates since we can't
657c478bd9Sstevel@tonic-gate  * protect the memnode structures with locks, so we must
667c478bd9Sstevel@tonic-gate  * be mindful of the order in which updates and reads to
677c478bd9Sstevel@tonic-gate  * these values can occur.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate void
mem_node_add_slice(pfn_t start,pfn_t end)717c478bd9Sstevel@tonic-gate mem_node_add_slice(pfn_t start, pfn_t end)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	int mnode;
747c478bd9Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * DR will pass us the first pfn that is allocatable.
787c478bd9Sstevel@tonic-gate 	 * We need to round down to get the real start of
797c478bd9Sstevel@tonic-gate 	 * the slice.
807c478bd9Sstevel@tonic-gate 	 */
817c478bd9Sstevel@tonic-gate 	if (mem_node_physalign) {
827c478bd9Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
837c478bd9Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
87a3114836SGerry Liu 	ASSERT(mnode >= 0 && mnode < max_mem_nodes);
887c478bd9Sstevel@tonic-gate 
8975d94465SJosef 'Jeff' Sipek 	if (atomic_cas_32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) {
907c478bd9Sstevel@tonic-gate 		/*
917c478bd9Sstevel@tonic-gate 		 * Add slice to existing node.
927c478bd9Sstevel@tonic-gate 		 */
937c478bd9Sstevel@tonic-gate 		if (start < mem_node_config[mnode].physbase)
947c478bd9Sstevel@tonic-gate 			mem_node_config[mnode].physbase = start;
957c478bd9Sstevel@tonic-gate 		if (end > mem_node_config[mnode].physmax)
967c478bd9Sstevel@tonic-gate 			mem_node_config[mnode].physmax = end;
977c478bd9Sstevel@tonic-gate 	} else {
987c478bd9Sstevel@tonic-gate 		mem_node_config[mnode].physbase = start;
997c478bd9Sstevel@tonic-gate 		mem_node_config[mnode].physmax = end;
100*1a5e258fSJosef 'Jeff' Sipek 		atomic_inc_16(&num_memnodes);
1017c478bd9Sstevel@tonic-gate 		do {
1027c478bd9Sstevel@tonic-gate 			oldmask = memnodes_mask;
1037c478bd9Sstevel@tonic-gate 			newmask = memnodes_mask | (1ull << mnode);
10475d94465SJosef 'Jeff' Sipek 		} while (atomic_cas_64(&memnodes_mask, oldmask, newmask) !=
10575d94465SJosef 'Jeff' Sipek 		    oldmask);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * Inform the common lgrp framework about the new memory
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode));
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Remove a PFN range from a memnode.  On some platforms,
1167c478bd9Sstevel@tonic-gate  * the memnode will be created with physbase at the first
1177c478bd9Sstevel@tonic-gate  * allocatable PFN, but later deleted with the MC slice
1187c478bd9Sstevel@tonic-gate  * base address converted to a PFN, in which case we need
1197c478bd9Sstevel@tonic-gate  * to assume physbase and up.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate void
mem_node_del_slice(pfn_t start,pfn_t end)1229853d9e8SJason Beloro mem_node_del_slice(pfn_t start, pfn_t end)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	int mnode;
1257c478bd9Sstevel@tonic-gate 	pgcnt_t delta_pgcnt, node_size;
1267c478bd9Sstevel@tonic-gate 	mnodeset_t omask, nmask;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (mem_node_physalign) {
1297c478bd9Sstevel@tonic-gate 		start &= ~(btop(mem_node_physalign) - 1);
1307c478bd9Sstevel@tonic-gate 		end = roundup(end, btop(mem_node_physalign)) - 1;
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(start);
1337c478bd9Sstevel@tonic-gate 
134a3114836SGerry Liu 	ASSERT(mnode >= 0 && mnode < max_mem_nodes);
1357c478bd9Sstevel@tonic-gate 	ASSERT(mem_node_config[mnode].exists == 1);
1367c478bd9Sstevel@tonic-gate 
1379853d9e8SJason Beloro 	delta_pgcnt = end - start;
1389853d9e8SJason Beloro 	node_size = mem_node_config[mnode].physmax -
1399853d9e8SJason Beloro 	    mem_node_config[mnode].physbase;
1407c478bd9Sstevel@tonic-gate 
1419853d9e8SJason Beloro 	if (node_size > delta_pgcnt) {
1429853d9e8SJason Beloro 		/*
1439853d9e8SJason Beloro 		 * Subtract the slice from the memnode.
1449853d9e8SJason Beloro 		 */
1459853d9e8SJason Beloro 		if (start <= mem_node_config[mnode].physbase)
1469853d9e8SJason Beloro 			mem_node_config[mnode].physbase = end + 1;
1479853d9e8SJason Beloro 		ASSERT(end <= mem_node_config[mnode].physmax);
1489853d9e8SJason Beloro 		if (end == mem_node_config[mnode].physmax)
1499853d9e8SJason Beloro 			mem_node_config[mnode].physmax = start - 1;
1509853d9e8SJason Beloro 	} else {
1519853d9e8SJason Beloro 		/*
1529853d9e8SJason Beloro 		 * Let the common lgrp framework know this mnode is
1539853d9e8SJason Beloro 		 * leaving
1549853d9e8SJason Beloro 		 */
1559853d9e8SJason Beloro 		lgrp_config(LGRP_CONFIG_MEM_DEL,
1569853d9e8SJason Beloro 		    mnode, MEM_NODE_2_LGRPHAND(mnode));
1579853d9e8SJason Beloro 
1589853d9e8SJason Beloro 		/*
1599853d9e8SJason Beloro 		 * Delete the whole node.
1609853d9e8SJason Beloro 		 */
1619853d9e8SJason Beloro 		ASSERT(MNODE_PGCNT(mnode) == 0);
1629853d9e8SJason Beloro 		do {
1639853d9e8SJason Beloro 			omask = memnodes_mask;
1649853d9e8SJason Beloro 			nmask = omask & ~(1ull << mnode);
16575d94465SJosef 'Jeff' Sipek 		} while (atomic_cas_64(&memnodes_mask, omask, nmask) != omask);
166*1a5e258fSJosef 'Jeff' Sipek 		atomic_dec_16(&num_memnodes);
1679853d9e8SJason Beloro 		mem_node_config[mnode].exists = 0;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1719853d9e8SJason Beloro void
mem_node_add_range(pfn_t start,pfn_t end)1729853d9e8SJason Beloro mem_node_add_range(pfn_t start, pfn_t end)
1739853d9e8SJason Beloro {
1749853d9e8SJason Beloro 	if (&plat_slice_add)
1759853d9e8SJason Beloro 		plat_slice_add(start, end);
1769853d9e8SJason Beloro 	else
1779853d9e8SJason Beloro 		mem_node_add_slice(start, end);
1789853d9e8SJason Beloro }
1799853d9e8SJason Beloro 
1809853d9e8SJason Beloro void
mem_node_del_range(pfn_t start,pfn_t end)1819853d9e8SJason Beloro mem_node_del_range(pfn_t start, pfn_t end)
1829853d9e8SJason Beloro {
1839853d9e8SJason Beloro 	if (&plat_slice_del)
1849853d9e8SJason Beloro 		plat_slice_del(start, end);
1859853d9e8SJason Beloro 	else
1869853d9e8SJason Beloro 		mem_node_del_slice(start, end);
1879853d9e8SJason Beloro }
1889853d9e8SJason Beloro 
1897c478bd9Sstevel@tonic-gate void
startup_build_mem_nodes(struct memlist * list)1907c478bd9Sstevel@tonic-gate startup_build_mem_nodes(struct memlist *list)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	pfn_t	start, end;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* LINTED: ASSERT will always true or false */
1957c478bd9Sstevel@tonic-gate 	ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (&plat_build_mem_nodes) {
1987c478bd9Sstevel@tonic-gate 		plat_build_mem_nodes(list);
1997c478bd9Sstevel@tonic-gate 	} else {
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * Boot install lists are arranged <addr, len>, ...
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		while (list) {
20456f33205SJonathan Adams 			start = list->ml_address >> PAGESHIFT;
2057c478bd9Sstevel@tonic-gate 			if (start > physmax)
2067c478bd9Sstevel@tonic-gate 				continue;
20756f33205SJonathan Adams 			end =
20856f33205SJonathan Adams 			    (list->ml_address + list->ml_size - 1) >> PAGESHIFT;
2097c478bd9Sstevel@tonic-gate 			if (end > physmax)
2107c478bd9Sstevel@tonic-gate 				end = physmax;
2119853d9e8SJason Beloro 			mem_node_add_range(start, end);
21256f33205SJonathan Adams 			list = list->ml_next;
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		mem_node_physalign = 0;
2157c478bd9Sstevel@tonic-gate 		mem_node_pfn_shift = 0;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * Allocate an unassigned memnode.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate int
mem_node_alloc()2237c478bd9Sstevel@tonic-gate mem_node_alloc()
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	int mnode;
2267c478bd9Sstevel@tonic-gate 	mnodeset_t newmask, oldmask;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * Find an unused memnode.  Update it atomically to prevent
2307c478bd9Sstevel@tonic-gate 	 * a first time memnode creation race.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	for (mnode = 0; mnode < max_mem_nodes; mnode++)
23375d94465SJosef 'Jeff' Sipek 		if (atomic_cas_32((uint32_t *)&mem_node_config[mnode].exists,
234ce8eb11aSdp 		    0, 1) == 0)
2357c478bd9Sstevel@tonic-gate 			break;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (mnode >= max_mem_nodes)
2387c478bd9Sstevel@tonic-gate 		panic("Out of free memnodes\n");
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	mem_node_config[mnode].physbase = (pfn_t)-1l;
2417c478bd9Sstevel@tonic-gate 	mem_node_config[mnode].physmax = 0;
242*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_16(&num_memnodes);
2437c478bd9Sstevel@tonic-gate 	do {
2447c478bd9Sstevel@tonic-gate 		oldmask = memnodes_mask;
2457c478bd9Sstevel@tonic-gate 		newmask = memnodes_mask | (1ull << mnode);
24675d94465SJosef 'Jeff' Sipek 	} while (atomic_cas_64(&memnodes_mask, oldmask, newmask) != oldmask);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (mnode);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Find the intersection between a memnode and a memlist
2537c478bd9Sstevel@tonic-gate  * and returns the number of pages that overlap.
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * Assumes the list is protected from DR operations by
2567c478bd9Sstevel@tonic-gate  * the memlist lock.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate pgcnt_t
mem_node_memlist_pages(int mnode,struct memlist * mlist)2597c478bd9Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	pfn_t		base, end;
2627c478bd9Sstevel@tonic-gate 	pfn_t		cur_base, cur_end;
2637c478bd9Sstevel@tonic-gate 	pgcnt_t		npgs;
2647c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	base = mem_node_config[mnode].physbase;
2677c478bd9Sstevel@tonic-gate 	end = mem_node_config[mnode].physmax;
2687c478bd9Sstevel@tonic-gate 	npgs = 0;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	memlist_read_lock();
2717c478bd9Sstevel@tonic-gate 
27256f33205SJonathan Adams 	for (pmem = mlist; pmem; pmem = pmem->ml_next) {
27356f33205SJonathan Adams 		cur_base = btop(pmem->ml_address);
27456f33205SJonathan Adams 		cur_end = cur_base + btop(pmem->ml_size) - 1;
275ce8eb11aSdp 		if (end < cur_base || base > cur_end)
2767c478bd9Sstevel@tonic-gate 			continue;
2777c478bd9Sstevel@tonic-gate 		npgs = npgs + (MIN(cur_end, end) -
2787c478bd9Sstevel@tonic-gate 		    MAX(cur_base, base)) + 1;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	return (npgs);
2847c478bd9Sstevel@tonic-gate }
285