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*56f33205SJonathan 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 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); 877c478bd9Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if (cas32((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; 1007c478bd9Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 1017c478bd9Sstevel@tonic-gate do { 1027c478bd9Sstevel@tonic-gate oldmask = memnodes_mask; 1037c478bd9Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 1047c478bd9Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * Inform the common lgrp framework about the new memory 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode)); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* 1147c478bd9Sstevel@tonic-gate * Remove a PFN range from a memnode. On some platforms, 1157c478bd9Sstevel@tonic-gate * the memnode will be created with physbase at the first 1167c478bd9Sstevel@tonic-gate * allocatable PFN, but later deleted with the MC slice 1177c478bd9Sstevel@tonic-gate * base address converted to a PFN, in which case we need 1187c478bd9Sstevel@tonic-gate * to assume physbase and up. 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate void 1219853d9e8SJason Beloro mem_node_del_slice(pfn_t start, pfn_t end) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate int mnode; 1247c478bd9Sstevel@tonic-gate pgcnt_t delta_pgcnt, node_size; 1257c478bd9Sstevel@tonic-gate mnodeset_t omask, nmask; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (mem_node_physalign) { 1287c478bd9Sstevel@tonic-gate start &= ~(btop(mem_node_physalign) - 1); 1297c478bd9Sstevel@tonic-gate end = roundup(end, btop(mem_node_physalign)) - 1; 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(start); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 1347c478bd9Sstevel@tonic-gate ASSERT(mem_node_config[mnode].exists == 1); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate delta_pgcnt = end - start; 1377c478bd9Sstevel@tonic-gate node_size = mem_node_config[mnode].physmax - 1387c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if (node_size > delta_pgcnt) { 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Subtract the slice from the memnode. 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate if (start <= mem_node_config[mnode].physbase) 1457c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase = end + 1; 1467c478bd9Sstevel@tonic-gate ASSERT(end <= mem_node_config[mnode].physmax); 1477c478bd9Sstevel@tonic-gate if (end == mem_node_config[mnode].physmax) 1487c478bd9Sstevel@tonic-gate mem_node_config[mnode].physmax = start - 1; 1497c478bd9Sstevel@tonic-gate } else { 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * Let the common lgrp framework know this mnode is 1527c478bd9Sstevel@tonic-gate * leaving 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_DEL, 1557c478bd9Sstevel@tonic-gate mnode, MEM_NODE_2_LGRPHAND(mnode)); 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* 1587c478bd9Sstevel@tonic-gate * Delete the whole node. 1597c478bd9Sstevel@tonic-gate */ 160affbd3ccSkchow ASSERT(MNODE_PGCNT(mnode) == 0); 1617c478bd9Sstevel@tonic-gate do { 1627c478bd9Sstevel@tonic-gate omask = memnodes_mask; 1637c478bd9Sstevel@tonic-gate nmask = omask & ~(1ull << mnode); 1647c478bd9Sstevel@tonic-gate } while (cas64(&memnodes_mask, omask, nmask) != omask); 1657c478bd9Sstevel@tonic-gate atomic_add_16(&num_memnodes, -1); 1667c478bd9Sstevel@tonic-gate mem_node_config[mnode].exists = 0; 1677c478bd9Sstevel@tonic-gate } 1689853d9e8SJason Beloro } 1697c478bd9Sstevel@tonic-gate 1709853d9e8SJason Beloro void 1719853d9e8SJason Beloro mem_node_add_range(pfn_t start, pfn_t end) 1729853d9e8SJason Beloro { 1739853d9e8SJason Beloro if (&plat_slice_add) 1749853d9e8SJason Beloro plat_slice_add(start, end); 1759853d9e8SJason Beloro else 1769853d9e8SJason Beloro mem_node_add_slice(start, end); 1779853d9e8SJason Beloro } 1789853d9e8SJason Beloro 1799853d9e8SJason Beloro void 1809853d9e8SJason Beloro mem_node_del_range(pfn_t start, pfn_t end) 1819853d9e8SJason Beloro { 1827c478bd9Sstevel@tonic-gate if (&plat_slice_del) 1837c478bd9Sstevel@tonic-gate plat_slice_del(start, end); 1849853d9e8SJason Beloro else 1859853d9e8SJason Beloro mem_node_del_slice(start, end); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate void 1897c478bd9Sstevel@tonic-gate startup_build_mem_nodes(struct memlist *list) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate pfn_t start, end; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* LINTED: ASSERT will always true or false */ 1947c478bd9Sstevel@tonic-gate ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes); 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate if (&plat_build_mem_nodes) { 1977c478bd9Sstevel@tonic-gate plat_build_mem_nodes(list); 1987c478bd9Sstevel@tonic-gate } else { 1997c478bd9Sstevel@tonic-gate /* 2007c478bd9Sstevel@tonic-gate * Boot install lists are arranged <addr, len>, ... 2017c478bd9Sstevel@tonic-gate */ 2027c478bd9Sstevel@tonic-gate while (list) { 203*56f33205SJonathan Adams start = list->ml_address >> PAGESHIFT; 2047c478bd9Sstevel@tonic-gate if (start > physmax) 2057c478bd9Sstevel@tonic-gate continue; 206*56f33205SJonathan Adams end = 207*56f33205SJonathan Adams (list->ml_address + list->ml_size - 1) >> PAGESHIFT; 2087c478bd9Sstevel@tonic-gate if (end > physmax) 2097c478bd9Sstevel@tonic-gate end = physmax; 2109853d9e8SJason Beloro mem_node_add_range(start, end); 211*56f33205SJonathan Adams list = list->ml_next; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate mem_node_physalign = 0; 2147c478bd9Sstevel@tonic-gate mem_node_pfn_shift = 0; 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate /* 2197c478bd9Sstevel@tonic-gate * Allocate an unassigned memnode. 2207c478bd9Sstevel@tonic-gate */ 2217c478bd9Sstevel@tonic-gate int 2227c478bd9Sstevel@tonic-gate mem_node_alloc() 2237c478bd9Sstevel@tonic-gate { 2247c478bd9Sstevel@tonic-gate int mnode; 2257c478bd9Sstevel@tonic-gate mnodeset_t newmask, oldmask; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* 2287c478bd9Sstevel@tonic-gate * Find an unused memnode. Update it atomically to prevent 2297c478bd9Sstevel@tonic-gate * a first time memnode creation race. 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate for (mnode = 0; mnode < max_mem_nodes; mnode++) 2327c478bd9Sstevel@tonic-gate if (cas32((uint32_t *)&mem_node_config[mnode].exists, 2337c478bd9Sstevel@tonic-gate 0, 1) == 0) 2347c478bd9Sstevel@tonic-gate break; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate if (mnode >= max_mem_nodes) 2377c478bd9Sstevel@tonic-gate panic("Out of free memnodes\n"); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate mem_node_config[mnode].physbase = (pfn_t)-1l; 2407c478bd9Sstevel@tonic-gate mem_node_config[mnode].physmax = 0; 2417c478bd9Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 2427c478bd9Sstevel@tonic-gate do { 2437c478bd9Sstevel@tonic-gate oldmask = memnodes_mask; 2447c478bd9Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 2457c478bd9Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate return (mnode); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* 2517c478bd9Sstevel@tonic-gate * Find the intersection between a memnode and a memlist 2527c478bd9Sstevel@tonic-gate * and returns the number of pages that overlap. 2537c478bd9Sstevel@tonic-gate * 2547c478bd9Sstevel@tonic-gate * Assumes the list is protected from DR operations by 2557c478bd9Sstevel@tonic-gate * the memlist lock. 2567c478bd9Sstevel@tonic-gate */ 2577c478bd9Sstevel@tonic-gate pgcnt_t 2587c478bd9Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist) 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate pfn_t base, end; 2617c478bd9Sstevel@tonic-gate pfn_t cur_base, cur_end; 2627c478bd9Sstevel@tonic-gate pgcnt_t npgs; 2637c478bd9Sstevel@tonic-gate struct memlist *pmem; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate base = mem_node_config[mnode].physbase; 2667c478bd9Sstevel@tonic-gate end = mem_node_config[mnode].physmax; 2677c478bd9Sstevel@tonic-gate npgs = 0; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate memlist_read_lock(); 2707c478bd9Sstevel@tonic-gate 271*56f33205SJonathan Adams for (pmem = mlist; pmem; pmem = pmem->ml_next) { 272*56f33205SJonathan Adams cur_base = btop(pmem->ml_address); 273*56f33205SJonathan Adams cur_end = cur_base + btop(pmem->ml_size) - 1; 274ce8eb11aSdp78419 if (end < cur_base || base > cur_end) 2757c478bd9Sstevel@tonic-gate continue; 2767c478bd9Sstevel@tonic-gate npgs = npgs + (MIN(cur_end, end) - 2777c478bd9Sstevel@tonic-gate MAX(cur_base, base)) + 1; 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate memlist_read_unlock(); 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate return (npgs); 2837c478bd9Sstevel@tonic-gate } 284