sparse.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * sparse memory mappings.
  3. */
  4. #include <linux/config.h>
  5. #include <linux/mm.h>
  6. #include <linux/mmzone.h>
  7. #include <linux/bootmem.h>
  8. #include <linux/module.h>
  9. #include <asm/dma.h>
  10. /*
  11. * Permanent SPARSEMEM data:
  12. *
  13. * 1) mem_section - memory sections, mem_map's for valid memory
  14. */
  15. #ifdef CONFIG_SPARSEMEM_EXTREME
  16. struct mem_section *mem_section[NR_SECTION_ROOTS]
  17. ____cacheline_maxaligned_in_smp;
  18. #else
  19. struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
  20. ____cacheline_maxaligned_in_smp;
  21. #endif
  22. EXPORT_SYMBOL(mem_section);
  23. static void sparse_alloc_root(unsigned long root, int nid)
  24. {
  25. #ifdef CONFIG_SPARSEMEM_EXTREME
  26. mem_section[root] = alloc_bootmem_node(NODE_DATA(nid), PAGE_SIZE);
  27. #endif
  28. }
  29. static void sparse_index_init(unsigned long section, int nid)
  30. {
  31. unsigned long root = SECTION_NR_TO_ROOT(section);
  32. if (mem_section[root])
  33. return;
  34. sparse_alloc_root(root, nid);
  35. if (mem_section[root])
  36. memset(mem_section[root], 0, PAGE_SIZE);
  37. else
  38. panic("memory_present: NO MEMORY\n");
  39. }
  40. /* Record a memory area against a node. */
  41. void memory_present(int nid, unsigned long start, unsigned long end)
  42. {
  43. unsigned long pfn;
  44. start &= PAGE_SECTION_MASK;
  45. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  46. unsigned long section = pfn_to_section_nr(pfn);
  47. struct mem_section *ms;
  48. sparse_index_init(section, nid);
  49. ms = __nr_to_section(section);
  50. if (!ms->section_mem_map)
  51. ms->section_mem_map = SECTION_MARKED_PRESENT;
  52. }
  53. }
  54. /*
  55. * Only used by the i386 NUMA architecures, but relatively
  56. * generic code.
  57. */
  58. unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
  59. unsigned long end_pfn)
  60. {
  61. unsigned long pfn;
  62. unsigned long nr_pages = 0;
  63. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  64. if (nid != early_pfn_to_nid(pfn))
  65. continue;
  66. if (pfn_valid(pfn))
  67. nr_pages += PAGES_PER_SECTION;
  68. }
  69. return nr_pages * sizeof(struct page);
  70. }
  71. /*
  72. * Subtle, we encode the real pfn into the mem_map such that
  73. * the identity pfn - section_mem_map will return the actual
  74. * physical page frame number.
  75. */
  76. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  77. {
  78. return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  79. }
  80. /*
  81. * We need this if we ever free the mem_maps. While not implemented yet,
  82. * this function is included for parity with its sibling.
  83. */
  84. static __attribute((unused))
  85. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  86. {
  87. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  88. }
  89. static int sparse_init_one_section(struct mem_section *ms,
  90. unsigned long pnum, struct page *mem_map)
  91. {
  92. if (!valid_section(ms))
  93. return -EINVAL;
  94. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
  95. return 1;
  96. }
  97. static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
  98. {
  99. struct page *map;
  100. int nid = early_pfn_to_nid(section_nr_to_pfn(pnum));
  101. struct mem_section *ms = __nr_to_section(pnum);
  102. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  103. if (map)
  104. return map;
  105. map = alloc_bootmem_node(NODE_DATA(nid),
  106. sizeof(struct page) * PAGES_PER_SECTION);
  107. if (map)
  108. return map;
  109. printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
  110. ms->section_mem_map = 0;
  111. return NULL;
  112. }
  113. /*
  114. * Allocate the accumulated non-linear sections, allocate a mem_map
  115. * for each and record the physical to section mapping.
  116. */
  117. void sparse_init(void)
  118. {
  119. unsigned long pnum;
  120. struct page *map;
  121. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  122. if (!valid_section_nr(pnum))
  123. continue;
  124. map = sparse_early_mem_map_alloc(pnum);
  125. if (!map)
  126. continue;
  127. sparse_init_one_section(__nr_to_section(pnum), pnum, map);
  128. }
  129. }
  130. /*
  131. * returns the number of sections whose mem_maps were properly
  132. * set. If this is <=0, then that means that the passed-in
  133. * map was not consumed and must be freed.
  134. */
  135. int sparse_add_one_section(unsigned long start_pfn, int nr_pages, struct page *map)
  136. {
  137. struct mem_section *ms = __pfn_to_section(start_pfn);
  138. if (ms->section_mem_map & SECTION_MARKED_PRESENT)
  139. return -EEXIST;
  140. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  141. return sparse_init_one_section(ms, pfn_to_section_nr(start_pfn), map);
  142. }