xref: /titanic_41/usr/src/uts/i86pc/os/memnode.c (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
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 #include <vm/vm_dep.h>
38 
39 int	max_mem_nodes = 1;
40 
41 struct mem_node_conf mem_node_config[MAX_MEM_NODES];
42 int mem_node_pfn_shift;
43 /*
44  * num_memnodes should be updated atomically and always >=
45  * the number of bits in memnodes_mask or the algorithm may fail.
46  */
47 uint16_t num_memnodes;
48 mnodeset_t memnodes_mask; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */
49 
50 /*
51  * If set, mem_node_physalign should be a power of two, and
52  * should reflect the minimum address alignment of each node.
53  */
54 uint64_t mem_node_physalign;
55 
56 /*
57  * Platform hooks we will need.
58  */
59 
60 #pragma weak plat_build_mem_nodes
61 #pragma weak plat_slice_add
62 #pragma weak plat_slice_del
63 
64 /*
65  * Adjust the memnode config after a DR operation.
66  *
67  * It is rather tricky to do these updates since we can't
68  * protect the memnode structures with locks, so we must
69  * be mindful of the order in which updates and reads to
70  * these values can occur.
71  */
72 
73 void
74 mem_node_add_slice(pfn_t start, pfn_t end)
75 {
76 	int mnode;
77 	mnodeset_t newmask, oldmask;
78 
79 	/*
80 	 * DR will pass us the first pfn that is allocatable.
81 	 * We need to round down to get the real start of
82 	 * the slice.
83 	 */
84 	if (mem_node_physalign) {
85 		start &= ~(btop(mem_node_physalign) - 1);
86 		end = roundup(end, btop(mem_node_physalign)) - 1;
87 	}
88 
89 	if (&plat_slice_add)
90 		plat_slice_add(start, end);
91 
92 	mnode = PFN_2_MEM_NODE(start);
93 	ASSERT(mnode < max_mem_nodes);
94 
95 	if (cas32((uint32_t *)&mem_node_config[mnode].exists, 0, 1)) {
96 		/*
97 		 * Add slice to existing node.
98 		 */
99 		if (start < mem_node_config[mnode].physbase)
100 			mem_node_config[mnode].physbase = start;
101 		if (end > mem_node_config[mnode].physmax)
102 			mem_node_config[mnode].physmax = end;
103 	} else {
104 		mem_node_config[mnode].physbase = start;
105 		mem_node_config[mnode].physmax = end;
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(MNODE_PGCNT(mnode) == 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 	atomic_add_16(&num_memnodes, 1);
244 	do {
245 		oldmask = memnodes_mask;
246 		newmask = memnodes_mask | (1ull << mnode);
247 	} while (cas64(&memnodes_mask, oldmask, newmask) != oldmask);
248 
249 	return (mnode);
250 }
251 
252 /*
253  * Find the intersection between a memnode and a memlist
254  * and returns the number of pages that overlap.
255  *
256  * Assumes the list is protected from DR operations by
257  * the memlist lock.
258  */
259 pgcnt_t
260 mem_node_memlist_pages(int mnode, struct memlist *mlist)
261 {
262 	pfn_t		base, end;
263 	pfn_t		cur_base, cur_end;
264 	pgcnt_t		npgs;
265 	struct memlist	*pmem;
266 
267 	base = mem_node_config[mnode].physbase;
268 	end = mem_node_config[mnode].physmax;
269 	npgs = 0;
270 
271 	memlist_read_lock();
272 
273 	for (pmem = mlist; pmem; pmem = pmem->next) {
274 		cur_base = btop(pmem->address);
275 		cur_end = cur_base + btop(pmem->size) - 1;
276 		if (end <= cur_base || base >= cur_end)
277 			continue;
278 		npgs = npgs + (MIN(cur_end, end) -
279 		    MAX(cur_base, base)) + 1;
280 	}
281 
282 	memlist_read_unlock();
283 
284 	return (npgs);
285 }
286