numa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/config.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/swap.h>
  14. #include <linux/initrd.h>
  15. #include <linux/pfn.h>
  16. #include <asm/hwrpb.h>
  17. #include <asm/pgalloc.h>
  18. pg_data_t node_data[MAX_NUMNODES];
  19. bootmem_data_t node_bdata[MAX_NUMNODES];
  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 = &node_bdata[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 contigous 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), bootmap_size);
  203. printk(" reserving pages %ld:%ld\n", bootmap_start, bootmap_start+PFN_UP(bootmap_size));
  204. node_set_online(nid);
  205. }
  206. void __init
  207. setup_memory(void *kernel_end)
  208. {
  209. int nid;
  210. show_mem_layout();
  211. nodes_clear(node_online_map);
  212. min_low_pfn = ~0UL;
  213. max_low_pfn = 0UL;
  214. for (nid = 0; nid < MAX_NUMNODES; nid++)
  215. setup_memory_node(nid, kernel_end);
  216. #ifdef CONFIG_BLK_DEV_INITRD
  217. initrd_start = INITRD_START;
  218. if (initrd_start) {
  219. extern void *move_initrd(unsigned long);
  220. initrd_end = initrd_start+INITRD_SIZE;
  221. printk("Initial ramdisk at: 0x%p (%lu bytes)\n",
  222. (void *) initrd_start, INITRD_SIZE);
  223. if ((void *)initrd_end > phys_to_virt(PFN_PHYS(max_low_pfn))) {
  224. if (!move_initrd(PFN_PHYS(max_low_pfn)))
  225. printk("initrd extends beyond end of memory "
  226. "(0x%08lx > 0x%p)\ndisabling initrd\n",
  227. initrd_end,
  228. phys_to_virt(PFN_PHYS(max_low_pfn)));
  229. } else {
  230. nid = kvaddr_to_nid(initrd_start);
  231. reserve_bootmem_node(NODE_DATA(nid),
  232. virt_to_phys((void *)initrd_start),
  233. INITRD_SIZE);
  234. }
  235. }
  236. #endif /* CONFIG_BLK_DEV_INITRD */
  237. }
  238. void __init paging_init(void)
  239. {
  240. unsigned int nid;
  241. unsigned long zones_size[MAX_NR_ZONES] = {0, };
  242. unsigned long dma_local_pfn;
  243. /*
  244. * The old global MAX_DMA_ADDRESS per-arch API doesn't fit
  245. * in the NUMA model, for now we convert it to a pfn and
  246. * we interpret this pfn as a local per-node information.
  247. * This issue isn't very important since none of these machines
  248. * have legacy ISA slots anyways.
  249. */
  250. dma_local_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  251. for_each_online_node(nid) {
  252. unsigned long start_pfn = node_bdata[nid].node_boot_start >> PAGE_SHIFT;
  253. unsigned long end_pfn = node_bdata[nid].node_low_pfn;
  254. if (dma_local_pfn >= end_pfn - start_pfn)
  255. zones_size[ZONE_DMA] = end_pfn - start_pfn;
  256. else {
  257. zones_size[ZONE_DMA] = dma_local_pfn;
  258. zones_size[ZONE_NORMAL] = (end_pfn - start_pfn) - dma_local_pfn;
  259. }
  260. free_area_init_node(nid, NODE_DATA(nid), zones_size, start_pfn, NULL);
  261. }
  262. /* Initialize the kernel's ZERO_PGE. */
  263. memset((void *)ZERO_PGE, 0, PAGE_SIZE);
  264. }
  265. void __init mem_init(void)
  266. {
  267. unsigned long codesize, reservedpages, datasize, initsize, pfn;
  268. extern int page_is_ram(unsigned long) __init;
  269. extern char _text, _etext, _data, _edata;
  270. extern char __init_begin, __init_end;
  271. unsigned long nid, i;
  272. high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
  273. reservedpages = 0;
  274. for_each_online_node(nid) {
  275. /*
  276. * This will free up the bootmem, ie, slot 0 memory
  277. */
  278. totalram_pages += free_all_bootmem_node(NODE_DATA(nid));
  279. pfn = NODE_DATA(nid)->node_start_pfn;
  280. for (i = 0; i < node_spanned_pages(nid); i++, pfn++)
  281. if (page_is_ram(pfn) &&
  282. PageReserved(nid_page_nr(nid, i)))
  283. reservedpages++;
  284. }
  285. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  286. datasize = (unsigned long) &_edata - (unsigned long) &_data;
  287. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  288. printk("Memory: %luk/%luk available (%luk kernel code, %luk reserved, "
  289. "%luk data, %luk init)\n",
  290. (unsigned long)nr_free_pages() << (PAGE_SHIFT-10),
  291. num_physpages << (PAGE_SHIFT-10),
  292. codesize >> 10,
  293. reservedpages << (PAGE_SHIFT-10),
  294. datasize >> 10,
  295. initsize >> 10);
  296. #if 0
  297. mem_stress();
  298. #endif
  299. }
  300. void
  301. show_mem(void)
  302. {
  303. long i,free = 0,total = 0,reserved = 0;
  304. long shared = 0, cached = 0;
  305. int nid;
  306. printk("\nMem-info:\n");
  307. show_free_areas();
  308. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  309. for_each_online_node(nid) {
  310. unsigned long flags;
  311. pgdat_resize_lock(NODE_DATA(nid), &flags);
  312. i = node_spanned_pages(nid);
  313. while (i-- > 0) {
  314. struct page *page = nid_page_nr(nid, i);
  315. total++;
  316. if (PageReserved(page))
  317. reserved++;
  318. else if (PageSwapCache(page))
  319. cached++;
  320. else if (!page_count(page))
  321. free++;
  322. else
  323. shared += page_count(page) - 1;
  324. }
  325. pgdat_resize_unlock(NODE_DATA(nid), &flags);
  326. }
  327. printk("%ld pages of RAM\n",total);
  328. printk("%ld free pages\n",free);
  329. printk("%ld reserved pages\n",reserved);
  330. printk("%ld pages shared\n",shared);
  331. printk("%ld pages swap cached\n",cached);
  332. }