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