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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*affbd3ccSkchow * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/systm.h> 307c478bd9Sstevel@tonic-gate #include <sys/platform_module.h> 317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 327c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 337c478bd9Sstevel@tonic-gate #include <sys/memlist.h> 347c478bd9Sstevel@tonic-gate #include <sys/memnode.h> 35*affbd3ccSkchow #include <vm/vm_dep.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate int max_mem_nodes = 1; /* max memory nodes on this system */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate struct mem_node_conf mem_node_config[MAX_MEM_NODES]; 407c478bd9Sstevel@tonic-gate int mem_node_pfn_shift; 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * num_memnodes should be updated atomically and always >= 437c478bd9Sstevel@tonic-gate * the number of bits in memnodes_mask or the algorithm may fail. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate uint16_t num_memnodes; 467c478bd9Sstevel@tonic-gate mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * If set, mem_node_physalign should be a power of two, and 507c478bd9Sstevel@tonic-gate * should reflect the minimum address alignment of each node. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate uint64_t mem_node_physalign; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * Platform hooks we will need. 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #pragma weak plat_build_mem_nodes 597c478bd9Sstevel@tonic-gate #pragma weak plat_slice_add 607c478bd9Sstevel@tonic-gate #pragma weak plat_slice_del 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate /* 637c478bd9Sstevel@tonic-gate * Adjust the memnode config after a DR operation. 647c478bd9Sstevel@tonic-gate * 657c478bd9Sstevel@tonic-gate * It is rather tricky to do these updates since we can't 667c478bd9Sstevel@tonic-gate * protect the memnode structures with locks, so we must 677c478bd9Sstevel@tonic-gate * be mindful of the order in which updates and reads to 687c478bd9Sstevel@tonic-gate * these values can occur. 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate void 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 if (&plat_slice_add) 877c478bd9Sstevel@tonic-gate plat_slice_add(start, end); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(start); 907c478bd9Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) { 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * Add slice to existing node. 957c478bd9Sstevel@tonic-gate */ 967c478bd9Sstevel@tonic-gate if (start < mem_node_config[mnode].physbase) 977c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase = start; 987c478bd9Sstevel@tonic-gate if (end > mem_node_config[mnode].physmax) 997c478bd9Sstevel@tonic-gate mem_node_config[mnode].physmax = end; 1007c478bd9Sstevel@tonic-gate } else { 1017c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase = start; 1027c478bd9Sstevel@tonic-gate mem_node_config[mnode].physmax = end; 1037c478bd9Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 1047c478bd9Sstevel@tonic-gate do { 1057c478bd9Sstevel@tonic-gate oldmask = memnodes_mask; 1067c478bd9Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 1077c478bd9Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * Let the common lgrp framework know about the new memory 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode)); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1167c478bd9Sstevel@tonic-gate void 1177c478bd9Sstevel@tonic-gate mem_node_pre_del_slice(pfn_t start, pfn_t end) 1187c478bd9Sstevel@tonic-gate { 1197c478bd9Sstevel@tonic-gate int mnode = PFN_2_MEM_NODE(start); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 1227c478bd9Sstevel@tonic-gate ASSERT(mem_node_config[mnode].exists == 1); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Remove a PFN range from a memnode. On some platforms, 1277c478bd9Sstevel@tonic-gate * the memnode will be created with physbase at the first 1287c478bd9Sstevel@tonic-gate * allocatable PFN, but later deleted with the MC slice 1297c478bd9Sstevel@tonic-gate * base address converted to a PFN, in which case we need 1307c478bd9Sstevel@tonic-gate * to assume physbase and up. 1317c478bd9Sstevel@tonic-gate */ 1327c478bd9Sstevel@tonic-gate void 1337c478bd9Sstevel@tonic-gate mem_node_post_del_slice(pfn_t start, pfn_t end, int cancelled) 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate int mnode; 1367c478bd9Sstevel@tonic-gate pgcnt_t delta_pgcnt, node_size; 1377c478bd9Sstevel@tonic-gate mnodeset_t omask, nmask; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if (mem_node_physalign) { 1407c478bd9Sstevel@tonic-gate start &= ~(btop(mem_node_physalign) - 1); 1417c478bd9Sstevel@tonic-gate end = roundup(end, btop(mem_node_physalign)) - 1; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(start); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 1467c478bd9Sstevel@tonic-gate ASSERT(mem_node_config[mnode].exists == 1); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate if (!cancelled) { 1497c478bd9Sstevel@tonic-gate delta_pgcnt = end - start; 1507c478bd9Sstevel@tonic-gate node_size = mem_node_config[mnode].physmax - 1517c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate if (node_size > delta_pgcnt) { 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * Subtract the slice from the memnode. 1567c478bd9Sstevel@tonic-gate */ 1577c478bd9Sstevel@tonic-gate if (start <= mem_node_config[mnode].physbase) 1587c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase = end + 1; 1597c478bd9Sstevel@tonic-gate ASSERT(end <= mem_node_config[mnode].physmax); 1607c478bd9Sstevel@tonic-gate if (end == mem_node_config[mnode].physmax) 1617c478bd9Sstevel@tonic-gate mem_node_config[mnode].physmax = start - 1; 1627c478bd9Sstevel@tonic-gate } else { 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * Let the common lgrp framework know the mnode is 1667c478bd9Sstevel@tonic-gate * leaving 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_DEL, mnode, 1697c478bd9Sstevel@tonic-gate MEM_NODE_2_LGRPHAND(mnode)); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * Delete the whole node. 1737c478bd9Sstevel@tonic-gate */ 174*affbd3ccSkchow ASSERT(MNODE_PGCNT(mnode) == 0); 1757c478bd9Sstevel@tonic-gate do { 1767c478bd9Sstevel@tonic-gate omask = memnodes_mask; 1777c478bd9Sstevel@tonic-gate nmask = omask & ~(1ull << mnode); 1787c478bd9Sstevel@tonic-gate } while (cas64(&memnodes_mask, omask, nmask) != omask); 1797c478bd9Sstevel@tonic-gate atomic_add_16(&num_memnodes, -1); 1807c478bd9Sstevel@tonic-gate mem_node_config[mnode].exists = 0; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (&plat_slice_del) 1847c478bd9Sstevel@tonic-gate plat_slice_del(start, end); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate void 1897c478bd9Sstevel@tonic-gate startup_build_mem_nodes(u_longlong_t *list, size_t nelems) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate size_t elem; 1927c478bd9Sstevel@tonic-gate pfn_t basepfn; 1937c478bd9Sstevel@tonic-gate pgcnt_t npgs; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* LINTED: ASSERT will always true or false */ 1967c478bd9Sstevel@tonic-gate ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if (&plat_build_mem_nodes) { 1997c478bd9Sstevel@tonic-gate plat_build_mem_nodes(list, nelems); 2007c478bd9Sstevel@tonic-gate } else { 2017c478bd9Sstevel@tonic-gate /* 2027c478bd9Sstevel@tonic-gate * Boot install lists are arranged <addr, len>, ... 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate for (elem = 0; elem < nelems; elem += 2) { 2057c478bd9Sstevel@tonic-gate basepfn = btop(list[elem]); 2067c478bd9Sstevel@tonic-gate npgs = btop(list[elem+1]); 2077c478bd9Sstevel@tonic-gate mem_node_add_slice(basepfn, basepfn + npgs - 1); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate mem_node_physalign = 0; 2107c478bd9Sstevel@tonic-gate mem_node_pfn_shift = 0; 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * Allocate an unassigned memnode. 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate int 2187c478bd9Sstevel@tonic-gate mem_node_alloc() 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate int mnode; 2217c478bd9Sstevel@tonic-gate mnodeset_t newmask, oldmask; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* 2247c478bd9Sstevel@tonic-gate * Find an unused memnode. Update it atomically to prevent 2257c478bd9Sstevel@tonic-gate * a first time memnode creation race. 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate for (mnode = 0; mnode < max_mem_nodes; mnode++) 2287c478bd9Sstevel@tonic-gate if (cas32((uint32_t *)&mem_node_config[mnode].exists, 2297c478bd9Sstevel@tonic-gate 0, 1) == 0) 2307c478bd9Sstevel@tonic-gate break; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate if (mnode >= max_mem_nodes) 2337c478bd9Sstevel@tonic-gate panic("Out of free memnodes\n"); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase = (uint64_t)-1; 2367c478bd9Sstevel@tonic-gate mem_node_config[mnode].physmax = 0; 2377c478bd9Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 2387c478bd9Sstevel@tonic-gate do { 2397c478bd9Sstevel@tonic-gate oldmask = memnodes_mask; 2407c478bd9Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 2417c478bd9Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate return (mnode); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * Find the intersection between a memnode and a memlist 2487c478bd9Sstevel@tonic-gate * and returns the number of pages that overlap. 2497c478bd9Sstevel@tonic-gate * 2507c478bd9Sstevel@tonic-gate * Assumes the list is protected from DR operations by 2517c478bd9Sstevel@tonic-gate * the memlist lock. 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate pgcnt_t 2547c478bd9Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist) 2557c478bd9Sstevel@tonic-gate { 2567c478bd9Sstevel@tonic-gate pfn_t base, end; 2577c478bd9Sstevel@tonic-gate pfn_t cur_base, cur_end; 2587c478bd9Sstevel@tonic-gate pgcnt_t npgs; 2597c478bd9Sstevel@tonic-gate struct memlist *pmem; 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate base = mem_node_config[mnode].physbase; 2627c478bd9Sstevel@tonic-gate end = mem_node_config[mnode].physmax; 2637c478bd9Sstevel@tonic-gate npgs = 0; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate memlist_read_lock(); 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate for (pmem = mlist; pmem; pmem = pmem->next) { 2687c478bd9Sstevel@tonic-gate cur_base = btop(pmem->address); 2697c478bd9Sstevel@tonic-gate cur_end = cur_base + btop(pmem->size) - 1; 2707c478bd9Sstevel@tonic-gate if (end <= cur_base || base >= cur_end) 2717c478bd9Sstevel@tonic-gate continue; 2727c478bd9Sstevel@tonic-gate npgs = npgs + (MIN(cur_end, end) - 2737c478bd9Sstevel@tonic-gate MAX(cur_base, base)) + 1; 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate memlist_read_unlock(); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate return (npgs); 2797c478bd9Sstevel@tonic-gate } 280