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.
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.
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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/systm.h>
27 #include <sys/sysmacros.h>
28 #include <sys/bootconf.h>
29 #include <sys/atomic.h>
31 #include <sys/memlist.h>
32 #include <sys/memnode.h>
33 #include <sys/platform_module.h>
34 #include <vm/vm_dep.h>
36 int max_mem_nodes
= 1;
38 struct mem_node_conf mem_node_config
[MAX_MEM_NODES
];
39 int mem_node_pfn_shift
;
41 * num_memnodes should be updated atomically and always >=
42 * the number of bits in memnodes_mask or the algorithm may fail.
44 uint16_t num_memnodes
;
45 mnodeset_t memnodes_mask
; /* assumes 8*(sizeof(mnodeset_t)) >= MAX_MEM_NODES */
48 * If set, mem_node_physalign should be a power of two, and
49 * should reflect the minimum address alignment of each node.
51 uint64_t mem_node_physalign
;
54 * Platform hooks we will need.
57 #pragma weak plat_build_mem_nodes
58 #pragma weak plat_slice_add
59 #pragma weak plat_slice_del
62 * Adjust the memnode config after a DR operation.
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.
71 mem_node_add_slice(pfn_t start
, pfn_t end
)
76 * DR will pass us the first pfn that is allocatable.
77 * We need to round down to get the real start of
80 if (mem_node_physalign
) {
81 start
&= ~(btop(mem_node_physalign
) - 1);
82 end
= roundup(end
, btop(mem_node_physalign
)) - 1;
85 mnode
= PFN_2_MEM_NODE(start
);
86 ASSERT(mnode
>= 0 && mnode
< max_mem_nodes
);
88 if (atomic_cas_32((uint32_t *)&mem_node_config
[mnode
].exists
, 0, 1)) {
90 * Add slice to existing node.
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
;
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
);
104 * Inform the common lgrp framework about the new memory
106 lgrp_config(LGRP_CONFIG_MEM_ADD
, mnode
, MEM_NODE_2_LGRPHAND(mnode
));
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.
117 mem_node_del_slice(pfn_t start
, pfn_t end
)
120 pgcnt_t delta_pgcnt
, node_size
;
122 if (mem_node_physalign
) {
123 start
&= ~(btop(mem_node_physalign
) - 1);
124 end
= roundup(end
, btop(mem_node_physalign
)) - 1;
126 mnode
= PFN_2_MEM_NODE(start
);
128 ASSERT(mnode
>= 0 && mnode
< max_mem_nodes
);
129 ASSERT(mem_node_config
[mnode
].exists
== 1);
131 delta_pgcnt
= end
- start
;
132 node_size
= mem_node_config
[mnode
].physmax
-
133 mem_node_config
[mnode
].physbase
;
135 if (node_size
> delta_pgcnt
) {
137 * Subtract the slice from the memnode.
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;
146 * Let the common lgrp framework know this mnode is
149 lgrp_config(LGRP_CONFIG_MEM_DEL
,
150 mnode
, MEM_NODE_2_LGRPHAND(mnode
));
153 * Delete the whole node.
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;
163 mem_node_add_range(pfn_t start
, pfn_t end
)
166 plat_slice_add(start
, end
);
168 mem_node_add_slice(start
, end
);
172 mem_node_del_range(pfn_t start
, pfn_t end
)
175 plat_slice_del(start
, end
);
177 mem_node_del_slice(start
, end
);
181 startup_build_mem_nodes(struct memlist
*list
)
185 /* LINTED: ASSERT will always true or false */
186 ASSERT(NBBY
* sizeof (mnodeset_t
) >= max_mem_nodes
);
188 if (&plat_build_mem_nodes
) {
189 plat_build_mem_nodes(list
);
192 * Boot install lists are arranged <addr, len>, ...
195 start
= list
->ml_address
>> PAGESHIFT
;
199 (list
->ml_address
+ list
->ml_size
- 1) >> PAGESHIFT
;
202 mem_node_add_range(start
, end
);
203 list
= list
->ml_next
;
205 mem_node_physalign
= 0;
206 mem_node_pfn_shift
= 0;
211 * Allocate an unassigned memnode.
219 * Find an unused memnode. Update it atomically to prevent
220 * a first time memnode creation race.
222 for (mnode
= 0; mnode
< max_mem_nodes
; mnode
++)
223 if (atomic_cas_32((uint32_t *)&mem_node_config
[mnode
].exists
,
227 if (mnode
>= max_mem_nodes
)
228 panic("Out of free memnodes\n");
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
);
239 * Find the intersection between a memnode and a memlist
240 * and returns the number of pages that overlap.
242 * Assumes the list is protected from DR operations by
246 mem_node_memlist_pages(int mnode
, struct memlist
*mlist
)
249 pfn_t cur_base
, cur_end
;
251 struct memlist
*pmem
;
253 base
= mem_node_config
[mnode
].physbase
;
254 end
= mem_node_config
[mnode
].physmax
;
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
)
264 npgs
= npgs
+ (MIN(cur_end
, end
) -
265 MAX(cur_base
, base
)) + 1;
268 memlist_read_unlock();