numa.c 10 KB

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