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