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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/systm.h> 27 #include <sys/sysmacros.h> 28 #include <sys/bootconf.h> 29 #include <sys/atomic.h> 30 #include <sys/lgrp.h> 31 #include <sys/memlist.h> 32 #include <sys/memnode.h> 33 #include <sys/platform_module.h> 34 #include <vm/vm_dep.h> 35 36 int max_mem_nodes = 1; 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 70 void 71 mem_node_add_slice(pfn_t start, pfn_t end) 72 { 73 int mnode; 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 mnode = PFN_2_MEM_NODE(start); 86 ASSERT(mnode >= 0 && mnode < max_mem_nodes); 87 88 if (atomic_cas_32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) { 89 /* 90 * Add slice to existing node. 91 */ 92 if (start < mem_node_config[mnode].physbase) 93 mem_node_config[mnode].physbase = start; 94 if (end > mem_node_config[mnode].physmax) 95 mem_node_config[mnode].physmax = end; 96 } else { 97 mem_node_config[mnode].physbase = start; 98 mem_node_config[mnode].physmax = end; 99 atomic_inc_16(&num_memnodes); 100 atomic_or_64(&memnodes_mask, 1ull << mnode); 101 } 102 103 /* 104 * Inform the common lgrp framework about the new memory 105 */ 106 lgrp_config(LGRP_CONFIG_MEM_ADD, mnode, MEM_NODE_2_LGRPHAND(mnode)); 107 } 108 109 /* 110 * Remove a PFN range from a memnode. On some platforms, 111 * the memnode will be created with physbase at the first 112 * allocatable PFN, but later deleted with the MC slice 113 * base address converted to a PFN, in which case we need 114 * to assume physbase and up. 115 */ 116 void 117 mem_node_del_slice(pfn_t start, pfn_t end) 118 { 119 int mnode; 120 pgcnt_t delta_pgcnt, node_size; 121 122 if (mem_node_physalign) { 123 start &= ~(btop(mem_node_physalign) - 1); 124 end = roundup(end, btop(mem_node_physalign)) - 1; 125 } 126 mnode = PFN_2_MEM_NODE(start); 127 128 ASSERT(mnode >= 0 && mnode < max_mem_nodes); 129 ASSERT(mem_node_config[mnode].exists == 1); 130 131 delta_pgcnt = end - start; 132 node_size = mem_node_config[mnode].physmax - 133 mem_node_config[mnode].physbase; 134 135 if (node_size > delta_pgcnt) { 136 /* 137 * Subtract the slice from the memnode. 138 */ 139 if (start <= mem_node_config[mnode].physbase) 140 mem_node_config[mnode].physbase = end + 1; 141 ASSERT(end <= mem_node_config[mnode].physmax); 142 if (end == mem_node_config[mnode].physmax) 143 mem_node_config[mnode].physmax = start - 1; 144 } else { 145 /* 146 * Let the common lgrp framework know this mnode is 147 * leaving 148 */ 149 lgrp_config(LGRP_CONFIG_MEM_DEL, 150 mnode, MEM_NODE_2_LGRPHAND(mnode)); 151 152 /* 153 * Delete the whole node. 154 */ 155 ASSERT(MNODE_PGCNT(mnode) == 0); 156 atomic_and_64(&memnodes_mask, ~(1ull << mnode)); 157 atomic_dec_16(&num_memnodes); 158 mem_node_config[mnode].exists = 0; 159 } 160 } 161 162 void 163 mem_node_add_range(pfn_t start, pfn_t end) 164 { 165 if (&plat_slice_add) 166 plat_slice_add(start, end); 167 else 168 mem_node_add_slice(start, end); 169 } 170 171 void 172 mem_node_del_range(pfn_t start, pfn_t end) 173 { 174 if (&plat_slice_del) 175 plat_slice_del(start, end); 176 else 177 mem_node_del_slice(start, end); 178 } 179 180 void 181 startup_build_mem_nodes(struct memlist *list) 182 { 183 pfn_t start, end; 184 185 /* LINTED: ASSERT will always true or false */ 186 ASSERT(NBBY * sizeof (mnodeset_t) >= max_mem_nodes); 187 188 if (&plat_build_mem_nodes) { 189 plat_build_mem_nodes(list); 190 } else { 191 /* 192 * Boot install lists are arranged <addr, len>, ... 193 */ 194 while (list) { 195 start = list->ml_address >> PAGESHIFT; 196 if (start > physmax) 197 continue; 198 end = 199 (list->ml_address + list->ml_size - 1) >> PAGESHIFT; 200 if (end > physmax) 201 end = physmax; 202 mem_node_add_range(start, end); 203 list = list->ml_next; 204 } 205 mem_node_physalign = 0; 206 mem_node_pfn_shift = 0; 207 } 208 } 209 210 /* 211 * Allocate an unassigned memnode. 212 */ 213 int 214 mem_node_alloc() 215 { 216 int mnode; 217 218 /* 219 * Find an unused memnode. Update it atomically to prevent 220 * a first time memnode creation race. 221 */ 222 for (mnode = 0; mnode < max_mem_nodes; mnode++) 223 if (atomic_cas_32((uint32_t *)&mem_node_config[mnode].exists, 224 0, 1) == 0) 225 break; 226 227 if (mnode >= max_mem_nodes) 228 panic("Out of free memnodes\n"); 229 230 mem_node_config[mnode].physbase = (pfn_t)-1l; 231 mem_node_config[mnode].physmax = 0; 232 atomic_inc_16(&num_memnodes); 233 atomic_or_64(&memnodes_mask, 1ull << mnode); 234 235 return (mnode); 236 } 237 238 /* 239 * Find the intersection between a memnode and a memlist 240 * and returns the number of pages that overlap. 241 * 242 * Assumes the list is protected from DR operations by 243 * the memlist lock. 244 */ 245 pgcnt_t 246 mem_node_memlist_pages(int mnode, struct memlist *mlist) 247 { 248 pfn_t base, end; 249 pfn_t cur_base, cur_end; 250 pgcnt_t npgs; 251 struct memlist *pmem; 252 253 base = mem_node_config[mnode].physbase; 254 end = mem_node_config[mnode].physmax; 255 npgs = 0; 256 257 memlist_read_lock(); 258 259 for (pmem = mlist; pmem; pmem = pmem->ml_next) { 260 cur_base = btop(pmem->ml_address); 261 cur_end = cur_base + btop(pmem->ml_size) - 1; 262 if (end < cur_base || base > cur_end) 263 continue; 264 npgs = npgs + (MIN(cur_end, end) - 265 MAX(cur_base, base)) + 1; 266 } 267 268 memlist_read_unlock(); 269 270 return (npgs); 271 }