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