1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/arch/alpha/mm/numa.c
4 *
5 * DISCONTIGMEM NUMA alpha support.
6 *
7 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
8 */
9
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/bootmem.h>
14 #include <linux/swap.h>
15 #include <linux/initrd.h>
16 #include <linux/pfn.h>
17 #include <linux/module.h>
18
19 #include <asm/hwrpb.h>
20 #include <asm/pgalloc.h>
21 #include <asm/sections.h>
22
23 pg_data_t node_data[MAX_NUMNODES];
24 EXPORT_SYMBOL(node_data);
25
26 #undef DEBUG_DISCONTIG
27 #ifdef DEBUG_DISCONTIG
28 #define DBGDCONT(args...) printk(args)
29 #else
30 #define DBGDCONT(args...)
31 #endif
32
33 #define for_each_mem_cluster(memdesc, _cluster, i) \
34 for ((_cluster) = (memdesc)->cluster, (i) = 0; \
35 (i) < (memdesc)->numclusters; (i)++, (_cluster)++)
36
show_mem_layout(void)37 static void __init show_mem_layout(void)
38 {
39 struct memclust_struct * cluster;
40 struct memdesc_struct * memdesc;
41 int i;
42
43 /* Find free clusters, and init and free the bootmem accordingly. */
44 memdesc = (struct memdesc_struct *)
45 (hwrpb->mddt_offset + (unsigned long) hwrpb);
46
47 printk("Raw memory layout:\n");
48 for_each_mem_cluster(memdesc, cluster, i) {
49 printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
50 i, cluster->usage, cluster->start_pfn,
51 cluster->start_pfn + cluster->numpages);
52 }
53 }
54
55 static void __init
setup_memory_node(int nid,void * kernel_end)56 setup_memory_node(int nid, void *kernel_end)
57 {
58 extern unsigned long mem_size_limit;
59 struct memclust_struct * cluster;
60 struct memdesc_struct * memdesc;
61 unsigned long start_kernel_pfn, end_kernel_pfn;
62 unsigned long bootmap_size, bootmap_pages, bootmap_start;
63 unsigned long start, end;
64 unsigned long node_pfn_start, node_pfn_end;
65 unsigned long node_min_pfn, node_max_pfn;
66 int i;
67 unsigned long node_datasz = PFN_UP(sizeof(pg_data_t));
68 int show_init = 0;
69
70 /* Find the bounds of current node */
71 node_pfn_start = (node_mem_start(nid)) >> PAGE_SHIFT;
72 node_pfn_end = node_pfn_start + (node_mem_size(nid) >> PAGE_SHIFT);
73
74 /* Find free clusters, and init and free the bootmem accordingly. */
75 memdesc = (struct memdesc_struct *)
76 (hwrpb->mddt_offset + (unsigned long) hwrpb);
77
78 /* find the bounds of this node (node_min_pfn/node_max_pfn) */
79 node_min_pfn = ~0UL;
80 node_max_pfn = 0UL;
81 for_each_mem_cluster(memdesc, cluster, i) {
82 /* Bit 0 is console/PALcode reserved. Bit 1 is
83 non-volatile memory -- we might want to mark
84 this for later. */
85 if (cluster->usage & 3)
86 continue;
87
88 start = cluster->start_pfn;
89 end = start + cluster->numpages;
90
91 if (start >= node_pfn_end || end <= node_pfn_start)
92 continue;
93
94 if (!show_init) {
95 show_init = 1;
96 printk("Initializing bootmem allocator on Node ID %d\n", nid);
97 }
98 printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
99 i, cluster->usage, cluster->start_pfn,
100 cluster->start_pfn + cluster->numpages);
101
102 if (start < node_pfn_start)
103 start = node_pfn_start;
104 if (end > node_pfn_end)
105 end = node_pfn_end;
106
107 if (start < node_min_pfn)
108 node_min_pfn = start;
109 if (end > node_max_pfn)
110 node_max_pfn = end;
111 }
112
113 if (mem_size_limit && node_max_pfn > mem_size_limit) {
114 static int msg_shown = 0;
115 if (!msg_shown) {
116 msg_shown = 1;
117 printk("setup: forcing memory size to %ldK (from %ldK).\n",
118 mem_size_limit << (PAGE_SHIFT - 10),
119 node_max_pfn << (PAGE_SHIFT - 10));
120 }
121 node_max_pfn = mem_size_limit;
122 }
123
124 if (node_min_pfn >= node_max_pfn)
125 return;
126
127 /* Update global {min,max}_low_pfn from node information. */
128 if (node_min_pfn < min_low_pfn)
129 min_low_pfn = node_min_pfn;
130 if (node_max_pfn > max_low_pfn)
131 max_pfn = max_low_pfn = node_max_pfn;
132
133 #if 0 /* we'll try this one again in a little while */
134 /* Cute trick to make sure our local node data is on local memory */
135 node_data[nid] = (pg_data_t *)(__va(node_min_pfn << PAGE_SHIFT));
136 #endif
137 /* Quasi-mark the pg_data_t as in-use */
138 node_min_pfn += node_datasz;
139 if (node_min_pfn >= node_max_pfn) {
140 printk(" not enough mem to reserve NODE_DATA");
141 return;
142 }
143 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
144
145 printk(" Detected node memory: start %8lu, end %8lu\n",
146 node_min_pfn, node_max_pfn);
147
148 DBGDCONT(" DISCONTIG: node_data[%d] is at 0x%p\n", nid, NODE_DATA(nid));
149 DBGDCONT(" DISCONTIG: NODE_DATA(%d)->bdata is at 0x%p\n", nid, NODE_DATA(nid)->bdata);
150
151 /* Find the bounds of kernel memory. */
152 start_kernel_pfn = PFN_DOWN(KERNEL_START_PHYS);
153 end_kernel_pfn = PFN_UP(virt_to_phys(kernel_end));
154 bootmap_start = -1;
155
156 if (!nid && (node_max_pfn < end_kernel_pfn || node_min_pfn > start_kernel_pfn))
157 panic("kernel loaded out of ram");
158
159 /* Zone start phys-addr must be 2^(MAX_ORDER-1) aligned.
160 Note that we round this down, not up - node memory
161 has much larger alignment than 8Mb, so it's safe. */
162 node_min_pfn &= ~((1UL << (MAX_ORDER-1))-1);
163
164 /* We need to know how many physically contiguous pages
165 we'll need for the bootmap. */
166 bootmap_pages = bootmem_bootmap_pages(node_max_pfn-node_min_pfn);
167
168 /* Now find a good region where to allocate the bootmap. */
169 for_each_mem_cluster(memdesc, cluster, i) {
170 if (cluster->usage & 3)
171 continue;
172
173 start = cluster->start_pfn;
174 end = start + cluster->numpages;
175
176 if (start >= node_max_pfn || end <= node_min_pfn)
177 continue;
178
179 if (end > node_max_pfn)
180 end = node_max_pfn;
181 if (start < node_min_pfn)
182 start = node_min_pfn;
183
184 if (start < start_kernel_pfn) {
185 if (end > end_kernel_pfn
186 && end - end_kernel_pfn >= bootmap_pages) {
187 bootmap_start = end_kernel_pfn;
188 break;
189 } else if (end > start_kernel_pfn)
190 end = start_kernel_pfn;
191 } else if (start < end_kernel_pfn)
192 start = end_kernel_pfn;
193 if (end - start >= bootmap_pages) {
194 bootmap_start = start;
195 break;
196 }
197 }
198
199 if (bootmap_start == -1)
200 panic("couldn't find a contiguous place for the bootmap");
201
202 /* Allocate the bootmap and mark the whole MM as reserved. */
203 bootmap_size = init_bootmem_node(NODE_DATA(nid), bootmap_start,
204 node_min_pfn, node_max_pfn);
205 DBGDCONT(" bootmap_start %lu, bootmap_size %lu, bootmap_pages %lu\n",
206 bootmap_start, bootmap_size, bootmap_pages);
207
208 /* Mark the free regions. */
209 for_each_mem_cluster(memdesc, cluster, i) {
210 if (cluster->usage & 3)
211 continue;
212
213 start = cluster->start_pfn;
214 end = cluster->start_pfn + cluster->numpages;
215
216 if (start >= node_max_pfn || end <= node_min_pfn)
217 continue;
218
219 if (end > node_max_pfn)
220 end = node_max_pfn;
221 if (start < node_min_pfn)
222 start = node_min_pfn;
223
224 if (start < start_kernel_pfn) {
225 if (end > end_kernel_pfn) {
226 free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start),
227 (PFN_PHYS(start_kernel_pfn)
228 - PFN_PHYS(start)));
229 printk(" freeing pages %ld:%ld\n",
230 start, start_kernel_pfn);
231 start = end_kernel_pfn;
232 } else if (end > start_kernel_pfn)
233 end = start_kernel_pfn;
234 } else if (start < end_kernel_pfn)
235 start = end_kernel_pfn;
236 if (start >= end)
237 continue;
238
239 free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start), PFN_PHYS(end) - PFN_PHYS(start));
240 printk(" freeing pages %ld:%ld\n", start, end);
241 }
242
243 /* Reserve the bootmap memory. */
244 reserve_bootmem_node(NODE_DATA(nid), PFN_PHYS(bootmap_start),
245 bootmap_size, BOOTMEM_DEFAULT);
246 printk(" reserving pages %ld:%ld\n", bootmap_start, bootmap_start+PFN_UP(bootmap_size));
247
248 node_set_online(nid);
249 }
250
251 void __init
setup_memory(void * kernel_end)252 setup_memory(void *kernel_end)
253 {
254 int nid;
255
256 show_mem_layout();
257
258 nodes_clear(node_online_map);
259
260 min_low_pfn = ~0UL;
261 max_low_pfn = 0UL;
262 for (nid = 0; nid < MAX_NUMNODES; nid++)
263 setup_memory_node(nid, kernel_end);
264
265 #ifdef CONFIG_BLK_DEV_INITRD
266 initrd_start = INITRD_START;
267 if (initrd_start) {
268 extern void *move_initrd(unsigned long);
269
270 initrd_end = initrd_start+INITRD_SIZE;
271 printk("Initial ramdisk at: 0x%p (%lu bytes)\n",
272 (void *) initrd_start, INITRD_SIZE);
273
274 if ((void *)initrd_end > phys_to_virt(PFN_PHYS(max_low_pfn))) {
275 if (!move_initrd(PFN_PHYS(max_low_pfn)))
276 printk("initrd extends beyond end of memory "
277 "(0x%08lx > 0x%p)\ndisabling initrd\n",
278 initrd_end,
279 phys_to_virt(PFN_PHYS(max_low_pfn)));
280 } else {
281 nid = kvaddr_to_nid(initrd_start);
282 reserve_bootmem_node(NODE_DATA(nid),
283 virt_to_phys((void *)initrd_start),
284 INITRD_SIZE, BOOTMEM_DEFAULT);
285 }
286 }
287 #endif /* CONFIG_BLK_DEV_INITRD */
288 }
289
paging_init(void)290 void __init paging_init(void)
291 {
292 unsigned int nid;
293 unsigned long zones_size[MAX_NR_ZONES] = {0, };
294 unsigned long dma_local_pfn;
295
296 /*
297 * The old global MAX_DMA_ADDRESS per-arch API doesn't fit
298 * in the NUMA model, for now we convert it to a pfn and
299 * we interpret this pfn as a local per-node information.
300 * This issue isn't very important since none of these machines
301 * have legacy ISA slots anyways.
302 */
303 dma_local_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
304
305 for_each_online_node(nid) {
306 bootmem_data_t *bdata = &bootmem_node_data[nid];
307 unsigned long start_pfn = bdata->node_min_pfn;
308 unsigned long end_pfn = bdata->node_low_pfn;
309
310 if (dma_local_pfn >= end_pfn - start_pfn)
311 zones_size[ZONE_DMA] = end_pfn - start_pfn;
312 else {
313 zones_size[ZONE_DMA] = dma_local_pfn;
314 zones_size[ZONE_NORMAL] = (end_pfn - start_pfn) - dma_local_pfn;
315 }
316 node_set_state(nid, N_NORMAL_MEMORY);
317 free_area_init_node(nid, zones_size, start_pfn, NULL);
318 }
319
320 /* Initialize the kernel's ZERO_PGE. */
321 memset((void *)ZERO_PGE, 0, PAGE_SIZE);
322 }
323