numa.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include <asm/sections.h>
  19. pg_data_t node_data[MAX_NUMNODES];
  20. EXPORT_SYMBOL(node_data);
  21. #undef DEBUG_DISCONTIG
  22. #ifdef DEBUG_DISCONTIG
  23. #define DBGDCONT(args...) printk(args)
  24. #else
  25. #define DBGDCONT(args...)
  26. #endif
  27. #define for_each_mem_cluster(memdesc, _cluster, i) \
  28. for ((_cluster) = (memdesc)->cluster, (i) = 0; \
  29. (i) < (memdesc)->numclusters; (i)++, (_cluster)++)
  30. static void __init show_mem_layout(void)
  31. {
  32. struct memclust_struct * cluster;
  33. struct memdesc_struct * memdesc;
  34. int i;
  35. /* Find free clusters, and init and free the bootmem accordingly. */
  36. memdesc = (struct memdesc_struct *)
  37. (hwrpb->mddt_offset + (unsigned long) hwrpb);
  38. printk("Raw memory layout:\n");
  39. for_each_mem_cluster(memdesc, cluster, i) {
  40. printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
  41. i, cluster->usage, cluster->start_pfn,
  42. cluster->start_pfn + cluster->numpages);
  43. }
  44. }
  45. static void __init
  46. setup_memory_node(int nid, void *kernel_end)
  47. {
  48. extern unsigned long mem_size_limit;
  49. struct memclust_struct * cluster;
  50. struct memdesc_struct * memdesc;
  51. unsigned long start_kernel_pfn, end_kernel_pfn;
  52. unsigned long bootmap_size, bootmap_pages, bootmap_start;
  53. unsigned long start, end;
  54. unsigned long node_pfn_start, node_pfn_end;
  55. unsigned long node_min_pfn, node_max_pfn;
  56. int i;
  57. unsigned long node_datasz = PFN_UP(sizeof(pg_data_t));
  58. int show_init = 0;
  59. /* Find the bounds of current node */
  60. node_pfn_start = (node_mem_start(nid)) >> PAGE_SHIFT;
  61. node_pfn_end = node_pfn_start + (node_mem_size(nid) >> PAGE_SHIFT);
  62. /* Find free clusters, and init and free the bootmem accordingly. */
  63. memdesc = (struct memdesc_struct *)
  64. (hwrpb->mddt_offset + (unsigned long) hwrpb);
  65. /* find the bounds of this node (node_min_pfn/node_max_pfn) */
  66. node_min_pfn = ~0UL;
  67. node_max_pfn = 0UL;
  68. for_each_mem_cluster(memdesc, cluster, i) {
  69. /* Bit 0 is console/PALcode reserved. Bit 1 is
  70. non-volatile memory -- we might want to mark
  71. this for later. */
  72. if (cluster->usage & 3)
  73. continue;
  74. start = cluster->start_pfn;
  75. end = start + cluster->numpages;
  76. if (start >= node_pfn_end || end <= node_pfn_start)
  77. continue;
  78. if (!show_init) {
  79. show_init = 1;
  80. printk("Initializing bootmem allocator on Node ID %d\n", nid);
  81. }
  82. printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
  83. i, cluster->usage, cluster->start_pfn,
  84. cluster->start_pfn + cluster->numpages);
  85. if (start < node_pfn_start)
  86. start = node_pfn_start;
  87. if (end > node_pfn_end)
  88. end = node_pfn_end;
  89. if (start < node_min_pfn)
  90. node_min_pfn = start;
  91. if (end > node_max_pfn)
  92. node_max_pfn = end;
  93. }
  94. if (mem_size_limit && node_max_pfn > mem_size_limit) {
  95. static int msg_shown = 0;
  96. if (!msg_shown) {
  97. msg_shown = 1;
  98. printk("setup: forcing memory size to %ldK (from %ldK).\n",
  99. mem_size_limit << (PAGE_SHIFT - 10),
  100. node_max_pfn << (PAGE_SHIFT - 10));
  101. }
  102. node_max_pfn = mem_size_limit;
  103. }
  104. if (node_min_pfn >= node_max_pfn)
  105. return;
  106. /* Update global {min,max}_low_pfn from node information. */
  107. if (node_min_pfn < min_low_pfn)
  108. min_low_pfn = node_min_pfn;
  109. if (node_max_pfn > max_low_pfn)
  110. max_pfn = max_low_pfn = node_max_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. }