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 2005 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/sysmacros.h> 31 #include <sys/bootconf.h> 32 #include <sys/atomic.h> 33 #include <sys/lgrp.h> 34 #include <sys/memlist.h> 35 #include <sys/memnode.h> 36 #include <sys/platform_module.h> 37 38 int max_mem_nodes = 1; 39 40 struct mem_node_conf mem_node_config[MAX_MEM_NODES]; 41 int mem_node_pfn_shift; 42 /* 43 * num_memnodes should be updated atomically and always >= 44 * the number of bits in memnodes_mask or the algorithm may fail. 45 */ 46 uint16_t num_memnodes; 47 mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */ 48 49 /* 50 * If set, mem_node_physalign should be a power of two, and 51 * should reflect the minimum address alignment of each node. 52 */ 53 uint64_t mem_node_physalign; 54 55 /* 56 * Platform hooks we will need. 57 */ 58 59 #pragma weak plat_build_mem_nodes 60 #pragma weak plat_slice_add 61 #pragma weak plat_slice_del 62 63 /* 64 * Adjust the memnode config after a DR operation. 65 * 66 * It is rather tricky to do these updates since we can't 67 * protect the memnode structures with locks, so we must 68 * be mindful of the order in which updates and reads to 69 * these values can occur. 70 */ 71 72 void 73 mem_node_add_slice(pfn_t start, pfn_t end) 74 { 75 int mnode; 76 mnodeset_t newmask, oldmask; 77 78 /* 79 * DR will pass us the first pfn that is allocatable. 80 * We need to round down to get the real start of 81 * the slice. 82 */ 83 if (mem_node_physalign) { 84 start &= ~(btop(mem_node_physalign) - 1); 85 end = roundup(end, btop(mem_node_physalign)) - 1; 86 } 87 88 if (&plat_slice_add) 89 plat_slice_add(start, end); 90 91 mnode = PFN_2_MEM_NODE(start); 92 ASSERT(mnode < max_mem_nodes); 93 94 if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) { 95 /* 96 * Add slice to existing node. 97 */ 98 if (start < mem_node_config[mnode].physbase) 99 mem_node_config[mnode].physbase = start; 100 if (end > mem_node_config[mnode].physmax) 101 mem_node_config[mnode].physmax = end; 102 } else { 103 mem_node_config[mnode].physbase = start; 104 mem_node_config[mnode].physmax = end; 105 mem_node_config[mnode].cursize = 0; 106 atomic_add_16(&num_memnodes, 1); 107 do { 108 oldmask = memnodes_mask; 109 newmask = memnodes_mask | (1ull << mnode); 110 } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 111 } 112 113 /* 114 * Inform the common lgrp framework about the new memory 115 */ 116 lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode)); 117 } 118 119 /* ARGSUSED */ 120 void 121 mem_node_pre_del_slice(pfn_t start, pfn_t end) 122 { 123 int mnode = PFN_2_MEM_NODE(start); 124 125 ASSERT(mnode < max_mem_nodes); 126 ASSERT(mem_node_config[mnode].exists == 1); 127 } 128 129 /* 130 * Remove a PFN range from a memnode. On some platforms, 131 * the memnode will be created with physbase at the first 132 * allocatable PFN, but later deleted with the MC slice 133 * base address converted to a PFN, in which case we need 134 * to assume physbase and up. 135 */ 136 void 137 mem_node_post_del_slice(pfn_t start, pfn_t end, int cancelled) 138 { 139 int mnode; 140 pgcnt_t delta_pgcnt, node_size; 141 mnodeset_t omask, nmask; 142 143 if (mem_node_physalign) { 144 start &= ~(btop(mem_node_physalign) - 1); 145 end = roundup(end, btop(mem_node_physalign)) - 1; 146 } 147 mnode = PFN_2_MEM_NODE(start); 148 149 ASSERT(mnode < max_mem_nodes); 150 ASSERT(mem_node_config[mnode].exists == 1); 151 152 if (!cancelled) { 153 delta_pgcnt = end - start; 154 node_size = mem_node_config[mnode].physmax - 155 mem_node_config[mnode].physbase; 156 157 if (node_size > delta_pgcnt) { 158 /* 159 * Subtract the slice from the memnode. 160 */ 161 if (start <= mem_node_config[mnode].physbase) 162 mem_node_config[mnode].physbase = end + 1; 163 ASSERT(end <= mem_node_config[mnode].physmax); 164 if (end == mem_node_config[mnode].physmax) 165 mem_node_config[mnode].physmax = start - 1; 166 } else { 167 /* 168 * Let the common lgrp framework know this mnode is 169 * leaving 170 */ 171 lgrp_config(LGRP_CONFIG_MEM_DEL, 172 mnode, MEM_NODE_2_LGRPHAND(mnode)); 173 174 /* 175 * Delete the whole node. 176 */ 177 ASSERT(mem_node_config[mnode].cursize == 0); 178 do { 179 omask = memnodes_mask; 180 nmask = omask & ~(1ull << mnode); 181 } while (cas64(&memnodes_mask, omask, nmask) != omask); 182 atomic_add_16(&num_memnodes, -1); 183 mem_node_config[mnode].exists = 0; 184 } 185 186 if (&plat_slice_del) 187 plat_slice_del(start, end); 188 } 189 } 190 191 void 192 startup_build_mem_nodes(struct memlist *list) 193 { 194 pfn_t start, end; 195 196 /* LINTED: ASSERT will always true or false */ 197 ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes); 198 199 if (&plat_build_mem_nodes) { 200 plat_build_mem_nodes(list); 201 } else { 202 /* 203 * Boot install lists are arranged <addr, len>, ... 204 */ 205 while (list) { 206 start = list->address >> PAGESHIFT; 207 if (start > physmax) 208 continue; 209 end = (list->address + list->size - 1) >> PAGESHIFT; 210 if (end > physmax) 211 end = physmax; 212 mem_node_add_slice(start, end); 213 list = list->next; 214 } 215 mem_node_physalign = 0; 216 mem_node_pfn_shift = 0; 217 } 218 } 219 220 /* 221 * Allocate an unassigned memnode. 222 */ 223 int 224 mem_node_alloc() 225 { 226 int mnode; 227 mnodeset_t newmask, oldmask; 228 229 /* 230 * Find an unused memnode. Update it atomically to prevent 231 * a first time memnode creation race. 232 */ 233 for (mnode = 0; mnode < max_mem_nodes; mnode++) 234 if (cas32((uint32_t *)&mem_node_config[mnode].exists, 235 0, 1) == 0) 236 break; 237 238 if (mnode >= max_mem_nodes) 239 panic("Out of free memnodes\n"); 240 241 mem_node_config[mnode].physbase = (pfn_t)-1l; 242 mem_node_config[mnode].physmax = 0; 243 mem_node_config[mnode].cursize = 0; 244 atomic_add_16(&num_memnodes, 1); 245 do { 246 oldmask = memnodes_mask; 247 newmask = memnodes_mask | (1ull << mnode); 248 } while (cas64(&memnodes_mask, oldmask, newmask) != oldmask); 249 250 return (mnode); 251 } 252 253 /* 254 * Find the intersection between a memnode and a memlist 255 * and returns the number of pages that overlap. 256 * 257 * Assumes the list is protected from DR operations by 258 * the memlist lock. 259 */ 260 pgcnt_t 261 mem_node_memlist_pages(int mnode, struct memlist *mlist) 262 { 263 pfn_t base, end; 264 pfn_t cur_base, cur_end; 265 pgcnt_t npgs; 266 struct memlist *pmem; 267 268 base = mem_node_config[mnode].physbase; 269 end = mem_node_config[mnode].physmax; 270 npgs = 0; 271 272 memlist_read_lock(); 273 274 for (pmem = mlist; pmem; pmem = pmem->next) { 275 cur_base = btop(pmem->address); 276 cur_end = cur_base + btop(pmem->size) - 1; 277 if (end <= cur_base || base >= cur_end) 278 continue; 279 npgs = npgs + (MIN(cur_end, end) - 280 MAX(cur_base, base)) + 1; 281 } 282 283 memlist_read_unlock(); 284 285 return (npgs); 286 } 287