numa.c 11 KB

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