discontig.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright (c) 2000, 2003 Silicon Graphics, Inc. All rights reserved.
  3. * Copyright (c) 2001 Intel Corp.
  4. * Copyright (c) 2001 Tony Luck <tony.luck@intel.com>
  5. * Copyright (c) 2002 NEC Corp.
  6. * Copyright (c) 2002 Kimio Suganuma <k-suganuma@da.jp.nec.com>
  7. * Copyright (c) 2004 Silicon Graphics, Inc
  8. * Russ Anderson <rja@sgi.com>
  9. * Jesse Barnes <jbarnes@sgi.com>
  10. * Jack Steiner <steiner@sgi.com>
  11. */
  12. /*
  13. * Platform initialization for Discontig Memory
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/swap.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/acpi.h>
  20. #include <linux/efi.h>
  21. #include <linux/nodemask.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlb.h>
  24. #include <asm/meminit.h>
  25. #include <asm/numa.h>
  26. #include <asm/sections.h>
  27. /*
  28. * Track per-node information needed to setup the boot memory allocator, the
  29. * per-node areas, and the real VM.
  30. */
  31. struct early_node_data {
  32. struct ia64_node_data *node_data;
  33. pg_data_t *pgdat;
  34. unsigned long pernode_addr;
  35. unsigned long pernode_size;
  36. struct bootmem_data bootmem_data;
  37. unsigned long num_physpages;
  38. unsigned long num_dma_physpages;
  39. unsigned long min_pfn;
  40. unsigned long max_pfn;
  41. };
  42. static struct early_node_data mem_data[MAX_NUMNODES] __initdata;
  43. /**
  44. * reassign_cpu_only_nodes - called from find_memory to move CPU-only nodes to a memory node
  45. *
  46. * This function will move nodes with only CPUs (no memory)
  47. * to a node with memory which is at the minimum numa_slit distance.
  48. * Any reassigments will result in the compression of the nodes
  49. * and renumbering the nid values where appropriate.
  50. * The static declarations below are to avoid large stack size which
  51. * makes the code not re-entrant.
  52. */
  53. static void __init reassign_cpu_only_nodes(void)
  54. {
  55. struct node_memblk_s *p;
  56. int i, j, k, nnode, nid, cpu, cpunid, pxm;
  57. u8 cslit, slit;
  58. static DECLARE_BITMAP(nodes_with_mem, MAX_NUMNODES) __initdata;
  59. static u8 numa_slit_fix[MAX_NUMNODES * MAX_NUMNODES] __initdata;
  60. static int node_flip[MAX_NUMNODES] __initdata;
  61. static int old_nid_map[NR_CPUS] __initdata;
  62. for (nnode = 0, p = &node_memblk[0]; p < &node_memblk[num_node_memblks]; p++)
  63. if (!test_bit(p->nid, (void *) nodes_with_mem)) {
  64. set_bit(p->nid, (void *) nodes_with_mem);
  65. nnode++;
  66. }
  67. /*
  68. * All nids with memory.
  69. */
  70. if (nnode == num_online_nodes())
  71. return;
  72. /*
  73. * Change nids and attempt to migrate CPU-only nodes
  74. * to the best numa_slit (closest neighbor) possible.
  75. * For reassigned CPU nodes a nid can't be arrived at
  76. * until after this loop because the target nid's new
  77. * identity might not have been established yet. So
  78. * new nid values are fabricated above num_online_nodes() and
  79. * mapped back later to their true value.
  80. */
  81. /* MCD - This code is a bit complicated, but may be unnecessary now.
  82. * We can now handle much more interesting node-numbering.
  83. * The old requirement that 0 <= nid <= numnodes <= MAX_NUMNODES
  84. * and that there be no holes in the numbering 0..numnodes
  85. * has become simply 0 <= nid <= MAX_NUMNODES.
  86. */
  87. nid = 0;
  88. for_each_online_node(i) {
  89. if (test_bit(i, (void *) nodes_with_mem)) {
  90. /*
  91. * Save original nid value for numa_slit
  92. * fixup and node_cpuid reassignments.
  93. */
  94. node_flip[nid] = i;
  95. if (i == nid) {
  96. nid++;
  97. continue;
  98. }
  99. for (p = &node_memblk[0]; p < &node_memblk[num_node_memblks]; p++)
  100. if (p->nid == i)
  101. p->nid = nid;
  102. cpunid = nid;
  103. nid++;
  104. } else
  105. cpunid = MAX_NUMNODES;
  106. for (cpu = 0; cpu < NR_CPUS; cpu++)
  107. if (node_cpuid[cpu].nid == i) {
  108. /*
  109. * For nodes not being reassigned just
  110. * fix the cpu's nid and reverse pxm map
  111. */
  112. if (cpunid < MAX_NUMNODES) {
  113. pxm = nid_to_pxm_map[i];
  114. pxm_to_nid_map[pxm] =
  115. node_cpuid[cpu].nid = cpunid;
  116. continue;
  117. }
  118. /*
  119. * For nodes being reassigned, find best node by
  120. * numa_slit information and then make a temporary
  121. * nid value based on current nid and num_online_nodes().
  122. */
  123. slit = 0xff;
  124. k = 2*num_online_nodes();
  125. for_each_online_node(j) {
  126. if (i == j)
  127. continue;
  128. else if (test_bit(j, (void *) nodes_with_mem)) {
  129. cslit = numa_slit[i * num_online_nodes() + j];
  130. if (cslit < slit) {
  131. k = num_online_nodes() + j;
  132. slit = cslit;
  133. }
  134. }
  135. }
  136. /* save old nid map so we can update the pxm */
  137. old_nid_map[cpu] = node_cpuid[cpu].nid;
  138. node_cpuid[cpu].nid = k;
  139. }
  140. }
  141. /*
  142. * Fixup temporary nid values for CPU-only nodes.
  143. */
  144. for (cpu = 0; cpu < NR_CPUS; cpu++)
  145. if (node_cpuid[cpu].nid == (2*num_online_nodes())) {
  146. pxm = nid_to_pxm_map[old_nid_map[cpu]];
  147. pxm_to_nid_map[pxm] = node_cpuid[cpu].nid = nnode - 1;
  148. } else {
  149. for (i = 0; i < nnode; i++) {
  150. if (node_flip[i] != (node_cpuid[cpu].nid - num_online_nodes()))
  151. continue;
  152. pxm = nid_to_pxm_map[old_nid_map[cpu]];
  153. pxm_to_nid_map[pxm] = node_cpuid[cpu].nid = i;
  154. break;
  155. }
  156. }
  157. /*
  158. * Fix numa_slit by compressing from larger
  159. * nid array to reduced nid array.
  160. */
  161. for (i = 0; i < nnode; i++)
  162. for (j = 0; j < nnode; j++)
  163. numa_slit_fix[i * nnode + j] =
  164. numa_slit[node_flip[i] * num_online_nodes() + node_flip[j]];
  165. memcpy(numa_slit, numa_slit_fix, sizeof (numa_slit));
  166. nodes_clear(node_online_map);
  167. for (i = 0; i < nnode; i++)
  168. node_set_online(i);
  169. return;
  170. }
  171. /*
  172. * To prevent cache aliasing effects, align per-node structures so that they
  173. * start at addresses that are strided by node number.
  174. */
  175. #define NODEDATA_ALIGN(addr, node) \
  176. ((((addr) + 1024*1024-1) & ~(1024*1024-1)) + (node)*PERCPU_PAGE_SIZE)
  177. /**
  178. * build_node_maps - callback to setup bootmem structs for each node
  179. * @start: physical start of range
  180. * @len: length of range
  181. * @node: node where this range resides
  182. *
  183. * We allocate a struct bootmem_data for each piece of memory that we wish to
  184. * treat as a virtually contiguous block (i.e. each node). Each such block
  185. * must start on an %IA64_GRANULE_SIZE boundary, so we round the address down
  186. * if necessary. Any non-existent pages will simply be part of the virtual
  187. * memmap. We also update min_low_pfn and max_low_pfn here as we receive
  188. * memory ranges from the caller.
  189. */
  190. static int __init build_node_maps(unsigned long start, unsigned long len,
  191. int node)
  192. {
  193. unsigned long cstart, epfn, end = start + len;
  194. struct bootmem_data *bdp = &mem_data[node].bootmem_data;
  195. epfn = GRANULEROUNDUP(end) >> PAGE_SHIFT;
  196. cstart = GRANULEROUNDDOWN(start);
  197. if (!bdp->node_low_pfn) {
  198. bdp->node_boot_start = cstart;
  199. bdp->node_low_pfn = epfn;
  200. } else {
  201. bdp->node_boot_start = min(cstart, bdp->node_boot_start);
  202. bdp->node_low_pfn = max(epfn, bdp->node_low_pfn);
  203. }
  204. min_low_pfn = min(min_low_pfn, bdp->node_boot_start>>PAGE_SHIFT);
  205. max_low_pfn = max(max_low_pfn, bdp->node_low_pfn);
  206. return 0;
  207. }
  208. /**
  209. * early_nr_phys_cpus_node - return number of physical cpus on a given node
  210. * @node: node to check
  211. *
  212. * Count the number of physical cpus on @node. These are cpus that actually
  213. * exist. We can't use nr_cpus_node() yet because
  214. * acpi_boot_init() (which builds the node_to_cpu_mask array) hasn't been
  215. * called yet.
  216. */
  217. static int early_nr_phys_cpus_node(int node)
  218. {
  219. int cpu, n = 0;
  220. for (cpu = 0; cpu < NR_CPUS; cpu++)
  221. if (node == node_cpuid[cpu].nid)
  222. if ((cpu == 0) || node_cpuid[cpu].phys_id)
  223. n++;
  224. return n;
  225. }
  226. /**
  227. * early_nr_cpus_node - return number of cpus on a given node
  228. * @node: node to check
  229. *
  230. * Count the number of cpus on @node. We can't use nr_cpus_node() yet because
  231. * acpi_boot_init() (which builds the node_to_cpu_mask array) hasn't been
  232. * called yet. Note that node 0 will also count all non-existent cpus.
  233. */
  234. static int early_nr_cpus_node(int node)
  235. {
  236. int cpu, n = 0;
  237. for (cpu = 0; cpu < NR_CPUS; cpu++)
  238. if (node == node_cpuid[cpu].nid)
  239. n++;
  240. return n;
  241. }
  242. /**
  243. * find_pernode_space - allocate memory for memory map and per-node structures
  244. * @start: physical start of range
  245. * @len: length of range
  246. * @node: node where this range resides
  247. *
  248. * This routine reserves space for the per-cpu data struct, the list of
  249. * pg_data_ts and the per-node data struct. Each node will have something like
  250. * the following in the first chunk of addr. space large enough to hold it.
  251. *
  252. * ________________________
  253. * | |
  254. * |~~~~~~~~~~~~~~~~~~~~~~~~| <-- NODEDATA_ALIGN(start, node) for the first
  255. * | PERCPU_PAGE_SIZE * | start and length big enough
  256. * | cpus_on_this_node | Node 0 will also have entries for all non-existent cpus.
  257. * |------------------------|
  258. * | local pg_data_t * |
  259. * |------------------------|
  260. * | local ia64_node_data |
  261. * |------------------------|
  262. * | ??? |
  263. * |________________________|
  264. *
  265. * Once this space has been set aside, the bootmem maps are initialized. We
  266. * could probably move the allocation of the per-cpu and ia64_node_data space
  267. * outside of this function and use alloc_bootmem_node(), but doing it here
  268. * is straightforward and we get the alignments we want so...
  269. */
  270. static int __init find_pernode_space(unsigned long start, unsigned long len,
  271. int node)
  272. {
  273. unsigned long epfn, cpu, cpus, phys_cpus;
  274. unsigned long pernodesize = 0, pernode, pages, mapsize;
  275. void *cpu_data;
  276. struct bootmem_data *bdp = &mem_data[node].bootmem_data;
  277. epfn = (start + len) >> PAGE_SHIFT;
  278. pages = bdp->node_low_pfn - (bdp->node_boot_start >> PAGE_SHIFT);
  279. mapsize = bootmem_bootmap_pages(pages) << PAGE_SHIFT;
  280. /*
  281. * Make sure this memory falls within this node's usable memory
  282. * since we may have thrown some away in build_maps().
  283. */
  284. if (start < bdp->node_boot_start || epfn > bdp->node_low_pfn)
  285. return 0;
  286. /* Don't setup this node's local space twice... */
  287. if (mem_data[node].pernode_addr)
  288. return 0;
  289. /*
  290. * Calculate total size needed, incl. what's necessary
  291. * for good alignment and alias prevention.
  292. */
  293. cpus = early_nr_cpus_node(node);
  294. phys_cpus = early_nr_phys_cpus_node(node);
  295. pernodesize += PERCPU_PAGE_SIZE * cpus;
  296. pernodesize += node * L1_CACHE_BYTES;
  297. pernodesize += L1_CACHE_ALIGN(sizeof(pg_data_t));
  298. pernodesize += L1_CACHE_ALIGN(sizeof(struct ia64_node_data));
  299. pernodesize = PAGE_ALIGN(pernodesize);
  300. pernode = NODEDATA_ALIGN(start, node);
  301. /* Is this range big enough for what we want to store here? */
  302. if (start + len > (pernode + pernodesize + mapsize)) {
  303. mem_data[node].pernode_addr = pernode;
  304. mem_data[node].pernode_size = pernodesize;
  305. memset(__va(pernode), 0, pernodesize);
  306. cpu_data = (void *)pernode;
  307. pernode += PERCPU_PAGE_SIZE * cpus;
  308. pernode += node * L1_CACHE_BYTES;
  309. mem_data[node].pgdat = __va(pernode);
  310. pernode += L1_CACHE_ALIGN(sizeof(pg_data_t));
  311. mem_data[node].node_data = __va(pernode);
  312. pernode += L1_CACHE_ALIGN(sizeof(struct ia64_node_data));
  313. mem_data[node].pgdat->bdata = bdp;
  314. pernode += L1_CACHE_ALIGN(sizeof(pg_data_t));
  315. /*
  316. * Copy the static per-cpu data into the region we
  317. * just set aside and then setup __per_cpu_offset
  318. * for each CPU on this node.
  319. */
  320. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  321. if (node == node_cpuid[cpu].nid) {
  322. memcpy(__va(cpu_data), __phys_per_cpu_start,
  323. __per_cpu_end - __per_cpu_start);
  324. __per_cpu_offset[cpu] = (char*)__va(cpu_data) -
  325. __per_cpu_start;
  326. cpu_data += PERCPU_PAGE_SIZE;
  327. }
  328. }
  329. }
  330. return 0;
  331. }
  332. /**
  333. * free_node_bootmem - free bootmem allocator memory for use
  334. * @start: physical start of range
  335. * @len: length of range
  336. * @node: node where this range resides
  337. *
  338. * Simply calls the bootmem allocator to free the specified ranged from
  339. * the given pg_data_t's bdata struct. After this function has been called
  340. * for all the entries in the EFI memory map, the bootmem allocator will
  341. * be ready to service allocation requests.
  342. */
  343. static int __init free_node_bootmem(unsigned long start, unsigned long len,
  344. int node)
  345. {
  346. free_bootmem_node(mem_data[node].pgdat, start, len);
  347. return 0;
  348. }
  349. /**
  350. * reserve_pernode_space - reserve memory for per-node space
  351. *
  352. * Reserve the space used by the bootmem maps & per-node space in the boot
  353. * allocator so that when we actually create the real mem maps we don't
  354. * use their memory.
  355. */
  356. static void __init reserve_pernode_space(void)
  357. {
  358. unsigned long base, size, pages;
  359. struct bootmem_data *bdp;
  360. int node;
  361. for_each_online_node(node) {
  362. pg_data_t *pdp = mem_data[node].pgdat;
  363. bdp = pdp->bdata;
  364. /* First the bootmem_map itself */
  365. pages = bdp->node_low_pfn - (bdp->node_boot_start>>PAGE_SHIFT);
  366. size = bootmem_bootmap_pages(pages) << PAGE_SHIFT;
  367. base = __pa(bdp->node_bootmem_map);
  368. reserve_bootmem_node(pdp, base, size);
  369. /* Now the per-node space */
  370. size = mem_data[node].pernode_size;
  371. base = __pa(mem_data[node].pernode_addr);
  372. reserve_bootmem_node(pdp, base, size);
  373. }
  374. }
  375. /**
  376. * initialize_pernode_data - fixup per-cpu & per-node pointers
  377. *
  378. * Each node's per-node area has a copy of the global pg_data_t list, so
  379. * we copy that to each node here, as well as setting the per-cpu pointer
  380. * to the local node data structure. The active_cpus field of the per-node
  381. * structure gets setup by the platform_cpu_init() function later.
  382. */
  383. static void __init initialize_pernode_data(void)
  384. {
  385. int cpu, node;
  386. pg_data_t *pgdat_list[MAX_NUMNODES];
  387. for_each_online_node(node)
  388. pgdat_list[node] = mem_data[node].pgdat;
  389. /* Copy the pg_data_t list to each node and init the node field */
  390. for_each_online_node(node) {
  391. memcpy(mem_data[node].node_data->pg_data_ptrs, pgdat_list,
  392. sizeof(pgdat_list));
  393. }
  394. /* Set the node_data pointer for each per-cpu struct */
  395. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  396. node = node_cpuid[cpu].nid;
  397. per_cpu(cpu_info, cpu).node_data = mem_data[node].node_data;
  398. }
  399. }
  400. /**
  401. * find_memory - walk the EFI memory map and setup the bootmem allocator
  402. *
  403. * Called early in boot to setup the bootmem allocator, and to
  404. * allocate the per-cpu and per-node structures.
  405. */
  406. void __init find_memory(void)
  407. {
  408. int node;
  409. reserve_memory();
  410. if (num_online_nodes() == 0) {
  411. printk(KERN_ERR "node info missing!\n");
  412. node_set_online(0);
  413. }
  414. min_low_pfn = -1;
  415. max_low_pfn = 0;
  416. if (num_online_nodes() > 1)
  417. reassign_cpu_only_nodes();
  418. /* These actually end up getting called by call_pernode_memory() */
  419. efi_memmap_walk(filter_rsvd_memory, build_node_maps);
  420. efi_memmap_walk(filter_rsvd_memory, find_pernode_space);
  421. /*
  422. * Initialize the boot memory maps in reverse order since that's
  423. * what the bootmem allocator expects
  424. */
  425. for (node = MAX_NUMNODES - 1; node >= 0; node--) {
  426. unsigned long pernode, pernodesize, map;
  427. struct bootmem_data *bdp;
  428. if (!node_online(node))
  429. continue;
  430. bdp = &mem_data[node].bootmem_data;
  431. pernode = mem_data[node].pernode_addr;
  432. pernodesize = mem_data[node].pernode_size;
  433. map = pernode + pernodesize;
  434. /* Sanity check... */
  435. if (!pernode)
  436. panic("pernode space for node %d "
  437. "could not be allocated!", node);
  438. init_bootmem_node(mem_data[node].pgdat,
  439. map>>PAGE_SHIFT,
  440. bdp->node_boot_start>>PAGE_SHIFT,
  441. bdp->node_low_pfn);
  442. }
  443. efi_memmap_walk(filter_rsvd_memory, free_node_bootmem);
  444. reserve_pernode_space();
  445. initialize_pernode_data();
  446. max_pfn = max_low_pfn;
  447. find_initrd();
  448. }
  449. /**
  450. * per_cpu_init - setup per-cpu variables
  451. *
  452. * find_pernode_space() does most of this already, we just need to set
  453. * local_per_cpu_offset
  454. */
  455. void *per_cpu_init(void)
  456. {
  457. int cpu;
  458. if (smp_processor_id() == 0) {
  459. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  460. per_cpu(local_per_cpu_offset, cpu) =
  461. __per_cpu_offset[cpu];
  462. }
  463. }
  464. return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
  465. }
  466. /**
  467. * show_mem - give short summary of memory stats
  468. *
  469. * Shows a simple page count of reserved and used pages in the system.
  470. * For discontig machines, it does this on a per-pgdat basis.
  471. */
  472. void show_mem(void)
  473. {
  474. int i, total_reserved = 0;
  475. int total_shared = 0, total_cached = 0;
  476. unsigned long total_present = 0;
  477. pg_data_t *pgdat;
  478. printk("Mem-info:\n");
  479. show_free_areas();
  480. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  481. for_each_pgdat(pgdat) {
  482. unsigned long present = pgdat->node_present_pages;
  483. int shared = 0, cached = 0, reserved = 0;
  484. printk("Node ID: %d\n", pgdat->node_id);
  485. for(i = 0; i < pgdat->node_spanned_pages; i++) {
  486. struct page *page = pgdat_page_nr(pgdat, i);
  487. if (!ia64_pfn_valid(pgdat->node_start_pfn+i))
  488. continue;
  489. if (PageReserved(page))
  490. reserved++;
  491. else if (PageSwapCache(page))
  492. cached++;
  493. else if (page_count(page))
  494. shared += page_count(page)-1;
  495. }
  496. total_present += present;
  497. total_reserved += reserved;
  498. total_cached += cached;
  499. total_shared += shared;
  500. printk("\t%ld pages of RAM\n", present);
  501. printk("\t%d reserved pages\n", reserved);
  502. printk("\t%d pages shared\n", shared);
  503. printk("\t%d pages swap cached\n", cached);
  504. }
  505. printk("%ld pages of RAM\n", total_present);
  506. printk("%d reserved pages\n", total_reserved);
  507. printk("%d pages shared\n", total_shared);
  508. printk("%d pages swap cached\n", total_cached);
  509. printk("Total of %ld pages in page table cache\n",
  510. pgtable_quicklist_total_size());
  511. printk("%d free buffer pages\n", nr_free_buffer_pages());
  512. }
  513. /**
  514. * call_pernode_memory - use SRAT to call callback functions with node info
  515. * @start: physical start of range
  516. * @len: length of range
  517. * @arg: function to call for each range
  518. *
  519. * efi_memmap_walk() knows nothing about layout of memory across nodes. Find
  520. * out to which node a block of memory belongs. Ignore memory that we cannot
  521. * identify, and split blocks that run across multiple nodes.
  522. *
  523. * Take this opportunity to round the start address up and the end address
  524. * down to page boundaries.
  525. */
  526. void call_pernode_memory(unsigned long start, unsigned long len, void *arg)
  527. {
  528. unsigned long rs, re, end = start + len;
  529. void (*func)(unsigned long, unsigned long, int);
  530. int i;
  531. start = PAGE_ALIGN(start);
  532. end &= PAGE_MASK;
  533. if (start >= end)
  534. return;
  535. func = arg;
  536. if (!num_node_memblks) {
  537. /* No SRAT table, so assume one node (node 0) */
  538. if (start < end)
  539. (*func)(start, end - start, 0);
  540. return;
  541. }
  542. for (i = 0; i < num_node_memblks; i++) {
  543. rs = max(start, node_memblk[i].start_paddr);
  544. re = min(end, node_memblk[i].start_paddr +
  545. node_memblk[i].size);
  546. if (rs < re)
  547. (*func)(rs, re - rs, node_memblk[i].nid);
  548. if (re == end)
  549. break;
  550. }
  551. }
  552. /**
  553. * count_node_pages - callback to build per-node memory info structures
  554. * @start: physical start of range
  555. * @len: length of range
  556. * @node: node where this range resides
  557. *
  558. * Each node has it's own number of physical pages, DMAable pages, start, and
  559. * end page frame number. This routine will be called by call_pernode_memory()
  560. * for each piece of usable memory and will setup these values for each node.
  561. * Very similar to build_maps().
  562. */
  563. static __init int count_node_pages(unsigned long start, unsigned long len, int node)
  564. {
  565. unsigned long end = start + len;
  566. mem_data[node].num_physpages += len >> PAGE_SHIFT;
  567. if (start <= __pa(MAX_DMA_ADDRESS))
  568. mem_data[node].num_dma_physpages +=
  569. (min(end, __pa(MAX_DMA_ADDRESS)) - start) >>PAGE_SHIFT;
  570. start = GRANULEROUNDDOWN(start);
  571. start = ORDERROUNDDOWN(start);
  572. end = GRANULEROUNDUP(end);
  573. mem_data[node].max_pfn = max(mem_data[node].max_pfn,
  574. end >> PAGE_SHIFT);
  575. mem_data[node].min_pfn = min(mem_data[node].min_pfn,
  576. start >> PAGE_SHIFT);
  577. return 0;
  578. }
  579. /**
  580. * paging_init - setup page tables
  581. *
  582. * paging_init() sets up the page tables for each node of the system and frees
  583. * the bootmem allocator memory for general use.
  584. */
  585. void __init paging_init(void)
  586. {
  587. unsigned long max_dma;
  588. unsigned long zones_size[MAX_NR_ZONES];
  589. unsigned long zholes_size[MAX_NR_ZONES];
  590. unsigned long pfn_offset = 0;
  591. int node;
  592. max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  593. /* so min() will work in count_node_pages */
  594. for_each_online_node(node)
  595. mem_data[node].min_pfn = ~0UL;
  596. efi_memmap_walk(filter_rsvd_memory, count_node_pages);
  597. for_each_online_node(node) {
  598. memset(zones_size, 0, sizeof(zones_size));
  599. memset(zholes_size, 0, sizeof(zholes_size));
  600. num_physpages += mem_data[node].num_physpages;
  601. if (mem_data[node].min_pfn >= max_dma) {
  602. /* All of this node's memory is above ZONE_DMA */
  603. zones_size[ZONE_NORMAL] = mem_data[node].max_pfn -
  604. mem_data[node].min_pfn;
  605. zholes_size[ZONE_NORMAL] = mem_data[node].max_pfn -
  606. mem_data[node].min_pfn -
  607. mem_data[node].num_physpages;
  608. } else if (mem_data[node].max_pfn < max_dma) {
  609. /* All of this node's memory is in ZONE_DMA */
  610. zones_size[ZONE_DMA] = mem_data[node].max_pfn -
  611. mem_data[node].min_pfn;
  612. zholes_size[ZONE_DMA] = mem_data[node].max_pfn -
  613. mem_data[node].min_pfn -
  614. mem_data[node].num_dma_physpages;
  615. } else {
  616. /* This node has memory in both zones */
  617. zones_size[ZONE_DMA] = max_dma -
  618. mem_data[node].min_pfn;
  619. zholes_size[ZONE_DMA] = zones_size[ZONE_DMA] -
  620. mem_data[node].num_dma_physpages;
  621. zones_size[ZONE_NORMAL] = mem_data[node].max_pfn -
  622. max_dma;
  623. zholes_size[ZONE_NORMAL] = zones_size[ZONE_NORMAL] -
  624. (mem_data[node].num_physpages -
  625. mem_data[node].num_dma_physpages);
  626. }
  627. if (node == 0) {
  628. vmalloc_end -=
  629. PAGE_ALIGN(max_low_pfn * sizeof(struct page));
  630. vmem_map = (struct page *) vmalloc_end;
  631. efi_memmap_walk(create_mem_map_page_table, NULL);
  632. printk("Virtual mem_map starts at 0x%p\n", vmem_map);
  633. }
  634. pfn_offset = mem_data[node].min_pfn;
  635. NODE_DATA(node)->node_mem_map = vmem_map + pfn_offset;
  636. free_area_init_node(node, NODE_DATA(node), zones_size,
  637. pfn_offset, zholes_size);
  638. }
  639. zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
  640. }