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/sysmacros.h> 300Sstevel@tonic-gate #include <sys/bootconf.h> 310Sstevel@tonic-gate #include <sys/atomic.h> 320Sstevel@tonic-gate #include <sys/lgrp.h> 330Sstevel@tonic-gate #include <sys/memlist.h> 340Sstevel@tonic-gate #include <sys/memnode.h> 350Sstevel@tonic-gate #include <sys/platform_module.h> 36414Skchow #include <vm/vm_dep.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate int max_mem_nodes = 1; 390Sstevel@tonic-gate 400Sstevel@tonic-gate struct mem_node_conf mem_node_config[MAX_MEM_NODES]; 410Sstevel@tonic-gate int mem_node_pfn_shift; 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * num_memnodes should be updated atomically and always >= 440Sstevel@tonic-gate * the number of bits in memnodes_mask or the algorithm may fail. 450Sstevel@tonic-gate */ 460Sstevel@tonic-gate uint16_t num_memnodes; 470Sstevel@tonic-gate mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */ 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * If set, mem_node_physalign should be a power of two, and 510Sstevel@tonic-gate * should reflect the minimum address alignment of each node. 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate uint64_t mem_node_physalign; 540Sstevel@tonic-gate 550Sstevel@tonic-gate /* 560Sstevel@tonic-gate * Platform hooks we will need. 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #pragma weak plat_build_mem_nodes 600Sstevel@tonic-gate #pragma weak plat_slice_add 610Sstevel@tonic-gate #pragma weak plat_slice_del 620Sstevel@tonic-gate 630Sstevel@tonic-gate /* 640Sstevel@tonic-gate * Adjust the memnode config after a DR operation. 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * It is rather tricky to do these updates since we can't 670Sstevel@tonic-gate * protect the memnode structures with locks, so we must 680Sstevel@tonic-gate * be mindful of the order in which updates and reads to 690Sstevel@tonic-gate * these values can occur. 700Sstevel@tonic-gate */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate void 730Sstevel@tonic-gate mem_node_add_slice(pfn_t start, pfn_t end) 740Sstevel@tonic-gate { 750Sstevel@tonic-gate int mnode; 760Sstevel@tonic-gate mnodeset_t newmask, oldmask; 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * DR will pass us the first pfn that is allocatable. 800Sstevel@tonic-gate * We need to round down to get the real start of 810Sstevel@tonic-gate * the slice. 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate if (mem_node_physalign) { 840Sstevel@tonic-gate start &= ~(btop(mem_node_physalign) - 1); 850Sstevel@tonic-gate end = roundup(end, btop(mem_node_physalign)) - 1; 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate if (&plat_slice_add) 890Sstevel@tonic-gate plat_slice_add(start, end); 900Sstevel@tonic-gate 910Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(start); 920Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 930Sstevel@tonic-gate 940Sstevel@tonic-gate if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) { 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * Add slice to existing node. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate if (start < mem_node_config[mnode].physbase) 990Sstevel@tonic-gate mem_node_config[mnode].physbase = start; 1000Sstevel@tonic-gate if (end > mem_node_config[mnode].physmax) 1010Sstevel@tonic-gate mem_node_config[mnode].physmax = end; 1020Sstevel@tonic-gate } else { 1030Sstevel@tonic-gate mem_node_config[mnode].physbase = start; 1040Sstevel@tonic-gate mem_node_config[mnode].physmax = end; 1050Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 1060Sstevel@tonic-gate do { 1070Sstevel@tonic-gate oldmask = memnodes_mask; 1080Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 1090Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Inform the common lgrp framework about the new memory 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode)); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* ARGSUSED */ 1190Sstevel@tonic-gate void 1200Sstevel@tonic-gate mem_node_pre_del_slice(pfn_t start, pfn_t end) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate int mnode = PFN_2_MEM_NODE(start); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 1250Sstevel@tonic-gate ASSERT(mem_node_config[mnode].exists == 1); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * Remove a PFN range from a memnode. On some platforms, 1300Sstevel@tonic-gate * the memnode will be created with physbase at the first 1310Sstevel@tonic-gate * allocatable PFN, but later deleted with the MC slice 1320Sstevel@tonic-gate * base address converted to a PFN, in which case we need 1330Sstevel@tonic-gate * to assume physbase and up. 1340Sstevel@tonic-gate */ 1350Sstevel@tonic-gate void 1360Sstevel@tonic-gate mem_node_post_del_slice(pfn_t start, pfn_t end, int cancelled) 1370Sstevel@tonic-gate { 1380Sstevel@tonic-gate int mnode; 1390Sstevel@tonic-gate pgcnt_t delta_pgcnt, node_size; 1400Sstevel@tonic-gate mnodeset_t omask, nmask; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate if (mem_node_physalign) { 1430Sstevel@tonic-gate start &= ~(btop(mem_node_physalign) - 1); 1440Sstevel@tonic-gate end = roundup(end, btop(mem_node_physalign)) - 1; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(start); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 1490Sstevel@tonic-gate ASSERT(mem_node_config[mnode].exists == 1); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate if (!cancelled) { 1520Sstevel@tonic-gate delta_pgcnt = end - start; 1530Sstevel@tonic-gate node_size = mem_node_config[mnode].physmax - 154*4769Sdp78419 mem_node_config[mnode].physbase; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate if (node_size > delta_pgcnt) { 1570Sstevel@tonic-gate /* 1580Sstevel@tonic-gate * Subtract the slice from the memnode. 1590Sstevel@tonic-gate */ 1600Sstevel@tonic-gate if (start <= mem_node_config[mnode].physbase) 1610Sstevel@tonic-gate mem_node_config[mnode].physbase = end + 1; 1620Sstevel@tonic-gate ASSERT(end <= mem_node_config[mnode].physmax); 1630Sstevel@tonic-gate if (end == mem_node_config[mnode].physmax) 1640Sstevel@tonic-gate mem_node_config[mnode].physmax = start - 1; 1650Sstevel@tonic-gate } else { 1660Sstevel@tonic-gate /* 1670Sstevel@tonic-gate * Let the common lgrp framework know this mnode is 1680Sstevel@tonic-gate * leaving 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_DEL, 1710Sstevel@tonic-gate mnode, MEM_NODE_2_LGRPHAND(mnode)); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * Delete the whole node. 1750Sstevel@tonic-gate */ 176414Skchow ASSERT(MNODE_PGCNT(mnode) == 0); 1770Sstevel@tonic-gate do { 1780Sstevel@tonic-gate omask = memnodes_mask; 1790Sstevel@tonic-gate nmask = omask & ~(1ull << mnode); 1800Sstevel@tonic-gate } while (cas64(&memnodes_mask, omask, nmask) != omask); 1810Sstevel@tonic-gate atomic_add_16(&num_memnodes, -1); 1820Sstevel@tonic-gate mem_node_config[mnode].exists = 0; 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate if (&plat_slice_del) 1860Sstevel@tonic-gate plat_slice_del(start, end); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate void 1910Sstevel@tonic-gate startup_build_mem_nodes(struct memlist *list) 1920Sstevel@tonic-gate { 1930Sstevel@tonic-gate pfn_t start, end; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* LINTED: ASSERT will always true or false */ 1960Sstevel@tonic-gate ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate if (&plat_build_mem_nodes) { 1990Sstevel@tonic-gate plat_build_mem_nodes(list); 2000Sstevel@tonic-gate } else { 2010Sstevel@tonic-gate /* 2020Sstevel@tonic-gate * Boot install lists are arranged <addr, len>, ... 2030Sstevel@tonic-gate */ 2040Sstevel@tonic-gate while (list) { 2050Sstevel@tonic-gate start = list->address >> PAGESHIFT; 2060Sstevel@tonic-gate if (start > physmax) 2070Sstevel@tonic-gate continue; 2080Sstevel@tonic-gate end = (list->address + list->size - 1) >> PAGESHIFT; 2090Sstevel@tonic-gate if (end > physmax) 2100Sstevel@tonic-gate end = physmax; 2110Sstevel@tonic-gate mem_node_add_slice(start, end); 2120Sstevel@tonic-gate list = list->next; 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate mem_node_physalign = 0; 2150Sstevel@tonic-gate mem_node_pfn_shift = 0; 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * Allocate an unassigned memnode. 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate int 2230Sstevel@tonic-gate mem_node_alloc() 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate int mnode; 2260Sstevel@tonic-gate mnodeset_t newmask, oldmask; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * Find an unused memnode. Update it atomically to prevent 2300Sstevel@tonic-gate * a first time memnode creation race. 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate for (mnode = 0; mnode < max_mem_nodes; mnode++) 2330Sstevel@tonic-gate if (cas32((uint32_t *)&mem_node_config[mnode].exists, 234*4769Sdp78419 0, 1) == 0) 2350Sstevel@tonic-gate break; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate if (mnode >= max_mem_nodes) 2380Sstevel@tonic-gate panic("Out of free memnodes\n"); 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate mem_node_config[mnode].physbase = (pfn_t)-1l; 2410Sstevel@tonic-gate mem_node_config[mnode].physmax = 0; 2420Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 2430Sstevel@tonic-gate do { 2440Sstevel@tonic-gate oldmask = memnodes_mask; 2450Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 2460Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate return (mnode); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * Find the intersection between a memnode and a memlist 2530Sstevel@tonic-gate * and returns the number of pages that overlap. 2540Sstevel@tonic-gate * 2550Sstevel@tonic-gate * Assumes the list is protected from DR operations by 2560Sstevel@tonic-gate * the memlist lock. 2570Sstevel@tonic-gate */ 2580Sstevel@tonic-gate pgcnt_t 2590Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist) 2600Sstevel@tonic-gate { 2610Sstevel@tonic-gate pfn_t base, end; 2620Sstevel@tonic-gate pfn_t cur_base, cur_end; 2630Sstevel@tonic-gate pgcnt_t npgs; 2640Sstevel@tonic-gate struct memlist *pmem; 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate base = mem_node_config[mnode].physbase; 2670Sstevel@tonic-gate end = mem_node_config[mnode].physmax; 2680Sstevel@tonic-gate npgs = 0; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate memlist_read_lock(); 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate for (pmem = mlist; pmem; pmem = pmem->next) { 2730Sstevel@tonic-gate cur_base = btop(pmem->address); 2740Sstevel@tonic-gate cur_end = cur_base + btop(pmem->size) - 1; 275*4769Sdp78419 if (end < cur_base || base > cur_end) 2760Sstevel@tonic-gate continue; 2770Sstevel@tonic-gate npgs = npgs + (MIN(cur_end, end) - 2780Sstevel@tonic-gate MAX(cur_base, base)) + 1; 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate memlist_read_unlock(); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate return (npgs); 2840Sstevel@tonic-gate } 285