1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/systm.h> 30*0Sstevel@tonic-gate #include <sys/platform_module.h> 31*0Sstevel@tonic-gate #include <sys/sysmacros.h> 32*0Sstevel@tonic-gate #include <sys/atomic.h> 33*0Sstevel@tonic-gate #include <sys/memlist.h> 34*0Sstevel@tonic-gate #include <sys/memnode.h> 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate int max_mem_nodes = 1; /* max memory nodes on this system */ 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate struct mem_node_conf mem_node_config[MAX_MEM_NODES]; 39*0Sstevel@tonic-gate int mem_node_pfn_shift; 40*0Sstevel@tonic-gate /* 41*0Sstevel@tonic-gate * num_memnodes should be updated atomically and always >= 42*0Sstevel@tonic-gate * the number of bits in memnodes_mask or the algorithm may fail. 43*0Sstevel@tonic-gate */ 44*0Sstevel@tonic-gate uint16_t num_memnodes; 45*0Sstevel@tonic-gate mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */ 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate /* 48*0Sstevel@tonic-gate * If set, mem_node_physalign should be a power of two, and 49*0Sstevel@tonic-gate * should reflect the minimum address alignment of each node. 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate uint64_t mem_node_physalign; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate /* 54*0Sstevel@tonic-gate * Platform hooks we will need. 55*0Sstevel@tonic-gate */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate #pragma weak plat_build_mem_nodes 58*0Sstevel@tonic-gate #pragma weak plat_slice_add 59*0Sstevel@tonic-gate #pragma weak plat_slice_del 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate /* 62*0Sstevel@tonic-gate * Adjust the memnode config after a DR operation. 63*0Sstevel@tonic-gate * 64*0Sstevel@tonic-gate * It is rather tricky to do these updates since we can't 65*0Sstevel@tonic-gate * protect the memnode structures with locks, so we must 66*0Sstevel@tonic-gate * be mindful of the order in which updates and reads to 67*0Sstevel@tonic-gate * these values can occur. 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate void 70*0Sstevel@tonic-gate mem_node_add_slice(pfn_t start, pfn_t end) 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate int mnode; 73*0Sstevel@tonic-gate mnodeset_t newmask, oldmask; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate /* 76*0Sstevel@tonic-gate * DR will pass us the first pfn that is allocatable. 77*0Sstevel@tonic-gate * We need to round down to get the real start of 78*0Sstevel@tonic-gate * the slice. 79*0Sstevel@tonic-gate */ 80*0Sstevel@tonic-gate if (mem_node_physalign) { 81*0Sstevel@tonic-gate start &= ~(btop(mem_node_physalign) - 1); 82*0Sstevel@tonic-gate end = roundup(end, btop(mem_node_physalign)) - 1; 83*0Sstevel@tonic-gate } 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate if (&plat_slice_add) 86*0Sstevel@tonic-gate plat_slice_add(start, end); 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(start); 89*0Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) { 92*0Sstevel@tonic-gate /* 93*0Sstevel@tonic-gate * Add slice to existing node. 94*0Sstevel@tonic-gate */ 95*0Sstevel@tonic-gate if (start < mem_node_config[mnode].physbase) 96*0Sstevel@tonic-gate mem_node_config[mnode].physbase = start; 97*0Sstevel@tonic-gate if (end > mem_node_config[mnode].physmax) 98*0Sstevel@tonic-gate mem_node_config[mnode].physmax = end; 99*0Sstevel@tonic-gate } else { 100*0Sstevel@tonic-gate mem_node_config[mnode].physbase = start; 101*0Sstevel@tonic-gate mem_node_config[mnode].physmax = end; 102*0Sstevel@tonic-gate mem_node_config[mnode].cursize = 0; 103*0Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 104*0Sstevel@tonic-gate do { 105*0Sstevel@tonic-gate oldmask = memnodes_mask; 106*0Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 107*0Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate /* 110*0Sstevel@tonic-gate * Let the common lgrp framework know about the new memory 111*0Sstevel@tonic-gate */ 112*0Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode)); 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate /* ARGSUSED */ 116*0Sstevel@tonic-gate void 117*0Sstevel@tonic-gate mem_node_pre_del_slice(pfn_t start, pfn_t end) 118*0Sstevel@tonic-gate { 119*0Sstevel@tonic-gate int mnode = PFN_2_MEM_NODE(start); 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 122*0Sstevel@tonic-gate ASSERT(mem_node_config[mnode].exists == 1); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate /* 126*0Sstevel@tonic-gate * Remove a PFN range from a memnode. On some platforms, 127*0Sstevel@tonic-gate * the memnode will be created with physbase at the first 128*0Sstevel@tonic-gate * allocatable PFN, but later deleted with the MC slice 129*0Sstevel@tonic-gate * base address converted to a PFN, in which case we need 130*0Sstevel@tonic-gate * to assume physbase and up. 131*0Sstevel@tonic-gate */ 132*0Sstevel@tonic-gate void 133*0Sstevel@tonic-gate mem_node_post_del_slice(pfn_t start, pfn_t end, int cancelled) 134*0Sstevel@tonic-gate { 135*0Sstevel@tonic-gate int mnode; 136*0Sstevel@tonic-gate pgcnt_t delta_pgcnt, node_size; 137*0Sstevel@tonic-gate mnodeset_t omask, nmask; 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate if (mem_node_physalign) { 140*0Sstevel@tonic-gate start &= ~(btop(mem_node_physalign) - 1); 141*0Sstevel@tonic-gate end = roundup(end, btop(mem_node_physalign)) - 1; 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate mnode = PFN_2_MEM_NODE(start); 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate ASSERT(mnode < max_mem_nodes); 146*0Sstevel@tonic-gate ASSERT(mem_node_config[mnode].exists == 1); 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate if (!cancelled) { 149*0Sstevel@tonic-gate delta_pgcnt = end - start; 150*0Sstevel@tonic-gate node_size = mem_node_config[mnode].physmax - 151*0Sstevel@tonic-gate mem_node_config[mnode].physbase; 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate if (node_size > delta_pgcnt) { 154*0Sstevel@tonic-gate /* 155*0Sstevel@tonic-gate * Subtract the slice from the memnode. 156*0Sstevel@tonic-gate */ 157*0Sstevel@tonic-gate if (start <= mem_node_config[mnode].physbase) 158*0Sstevel@tonic-gate mem_node_config[mnode].physbase = end + 1; 159*0Sstevel@tonic-gate ASSERT(end <= mem_node_config[mnode].physmax); 160*0Sstevel@tonic-gate if (end == mem_node_config[mnode].physmax) 161*0Sstevel@tonic-gate mem_node_config[mnode].physmax = start - 1; 162*0Sstevel@tonic-gate } else { 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate /* 165*0Sstevel@tonic-gate * Let the common lgrp framework know the mnode is 166*0Sstevel@tonic-gate * leaving 167*0Sstevel@tonic-gate */ 168*0Sstevel@tonic-gate lgrp_config(LGRP_CONFIG_MEM_DEL, mnode, 169*0Sstevel@tonic-gate MEM_NODE_2_LGRPHAND(mnode)); 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate /* 172*0Sstevel@tonic-gate * Delete the whole node. 173*0Sstevel@tonic-gate */ 174*0Sstevel@tonic-gate ASSERT(mem_node_config[mnode].cursize == 0); 175*0Sstevel@tonic-gate do { 176*0Sstevel@tonic-gate omask = memnodes_mask; 177*0Sstevel@tonic-gate nmask = omask & ~(1ull << mnode); 178*0Sstevel@tonic-gate } while (cas64(&memnodes_mask, omask, nmask) != omask); 179*0Sstevel@tonic-gate atomic_add_16(&num_memnodes, -1); 180*0Sstevel@tonic-gate mem_node_config[mnode].exists = 0; 181*0Sstevel@tonic-gate } 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate if (&plat_slice_del) 184*0Sstevel@tonic-gate plat_slice_del(start, end); 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate void 189*0Sstevel@tonic-gate startup_build_mem_nodes(u_longlong_t *list, size_t nelems) 190*0Sstevel@tonic-gate { 191*0Sstevel@tonic-gate size_t elem; 192*0Sstevel@tonic-gate pfn_t basepfn; 193*0Sstevel@tonic-gate pgcnt_t npgs; 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate /* LINTED: ASSERT will always true or false */ 196*0Sstevel@tonic-gate ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes); 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate if (&plat_build_mem_nodes) { 199*0Sstevel@tonic-gate plat_build_mem_nodes(list, nelems); 200*0Sstevel@tonic-gate } else { 201*0Sstevel@tonic-gate /* 202*0Sstevel@tonic-gate * Boot install lists are arranged <addr, len>, ... 203*0Sstevel@tonic-gate */ 204*0Sstevel@tonic-gate for (elem = 0; elem < nelems; elem += 2) { 205*0Sstevel@tonic-gate basepfn = btop(list[elem]); 206*0Sstevel@tonic-gate npgs = btop(list[elem+1]); 207*0Sstevel@tonic-gate mem_node_add_slice(basepfn, basepfn + npgs - 1); 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate mem_node_physalign = 0; 210*0Sstevel@tonic-gate mem_node_pfn_shift = 0; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* 215*0Sstevel@tonic-gate * Allocate an unassigned memnode. 216*0Sstevel@tonic-gate */ 217*0Sstevel@tonic-gate int 218*0Sstevel@tonic-gate mem_node_alloc() 219*0Sstevel@tonic-gate { 220*0Sstevel@tonic-gate int mnode; 221*0Sstevel@tonic-gate mnodeset_t newmask, oldmask; 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate /* 224*0Sstevel@tonic-gate * Find an unused memnode. Update it atomically to prevent 225*0Sstevel@tonic-gate * a first time memnode creation race. 226*0Sstevel@tonic-gate */ 227*0Sstevel@tonic-gate for (mnode = 0; mnode < max_mem_nodes; mnode++) 228*0Sstevel@tonic-gate if (cas32((uint32_t *)&mem_node_config[mnode].exists, 229*0Sstevel@tonic-gate 0, 1) == 0) 230*0Sstevel@tonic-gate break; 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate if (mnode >= max_mem_nodes) 233*0Sstevel@tonic-gate panic("Out of free memnodes\n"); 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate mem_node_config[mnode].physbase = (uint64_t)-1; 236*0Sstevel@tonic-gate mem_node_config[mnode].physmax = 0; 237*0Sstevel@tonic-gate mem_node_config[mnode].cursize = 0; 238*0Sstevel@tonic-gate atomic_add_16(&num_memnodes, 1); 239*0Sstevel@tonic-gate do { 240*0Sstevel@tonic-gate oldmask = memnodes_mask; 241*0Sstevel@tonic-gate newmask = memnodes_mask | (1ull << mnode); 242*0Sstevel@tonic-gate } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate return (mnode); 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate /* 248*0Sstevel@tonic-gate * Find the intersection between a memnode and a memlist 249*0Sstevel@tonic-gate * and returns the number of pages that overlap. 250*0Sstevel@tonic-gate * 251*0Sstevel@tonic-gate * Assumes the list is protected from DR operations by 252*0Sstevel@tonic-gate * the memlist lock. 253*0Sstevel@tonic-gate */ 254*0Sstevel@tonic-gate pgcnt_t 255*0Sstevel@tonic-gate mem_node_memlist_pages(int mnode, struct memlist *mlist) 256*0Sstevel@tonic-gate { 257*0Sstevel@tonic-gate pfn_t base, end; 258*0Sstevel@tonic-gate pfn_t cur_base, cur_end; 259*0Sstevel@tonic-gate pgcnt_t npgs; 260*0Sstevel@tonic-gate struct memlist *pmem; 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate base = mem_node_config[mnode].physbase; 263*0Sstevel@tonic-gate end = mem_node_config[mnode].physmax; 264*0Sstevel@tonic-gate npgs = 0; 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate memlist_read_lock(); 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate for (pmem = mlist; pmem; pmem = pmem->next) { 269*0Sstevel@tonic-gate cur_base = btop(pmem->address); 270*0Sstevel@tonic-gate cur_end = cur_base + btop(pmem->size) - 1; 271*0Sstevel@tonic-gate if (end <= cur_base || base >= cur_end) 272*0Sstevel@tonic-gate continue; 273*0Sstevel@tonic-gate npgs = npgs + (MIN(cur_end, end) - 274*0Sstevel@tonic-gate MAX(cur_base, base)) + 1; 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate memlist_read_unlock(); 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate return (npgs); 280*0Sstevel@tonic-gate } 281