sparse-vmemmap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Virtual Memory Map support
  3. *
  4. * (C) 2007 sgi. Christoph Lameter <clameter@sgi.com>.
  5. *
  6. * Virtual memory maps allow VM primitives pfn_to_page, page_to_pfn,
  7. * virt_to_page, page_address() to be implemented as a base offset
  8. * calculation without memory access.
  9. *
  10. * However, virtual mappings need a page table and TLBs. Many Linux
  11. * architectures already map their physical space using 1-1 mappings
  12. * via TLBs. For those arches the virtual memmory map is essentially
  13. * for free if we use the same page size as the 1-1 mappings. In that
  14. * case the overhead consists of a few additional pages that are
  15. * allocated to create a view of memory for vmemmap.
  16. *
  17. * The architecture is expected to provide a vmemmap_populate() function
  18. * to instantiate the mapping.
  19. */
  20. #include <linux/mm.h>
  21. #include <linux/mmzone.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/highmem.h>
  24. #include <linux/module.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/vmalloc.h>
  27. #include <asm/dma.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/pgtable.h>
  30. /*
  31. * Allocate a block of memory to be used to back the virtual memory map
  32. * or to back the page tables that are used to create the mapping.
  33. * Uses the main allocators if they are available, else bootmem.
  34. */
  35. void * __meminit vmemmap_alloc_block(unsigned long size, int node)
  36. {
  37. /* If the main allocator is up use that, fallback to bootmem. */
  38. if (slab_is_available()) {
  39. struct page *page = alloc_pages_node(node,
  40. GFP_KERNEL | __GFP_ZERO, get_order(size));
  41. if (page)
  42. return page_address(page);
  43. return NULL;
  44. } else
  45. return __alloc_bootmem_node(NODE_DATA(node), size, size,
  46. __pa(MAX_DMA_ADDRESS));
  47. }
  48. void __meminit vmemmap_verify(pte_t *pte, int node,
  49. unsigned long start, unsigned long end)
  50. {
  51. unsigned long pfn = pte_pfn(*pte);
  52. int actual_node = early_pfn_to_nid(pfn);
  53. if (actual_node != node)
  54. printk(KERN_WARNING "[%lx-%lx] potential offnode "
  55. "page_structs\n", start, end - 1);
  56. }
  57. pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node)
  58. {
  59. pte_t *pte = pte_offset_kernel(pmd, addr);
  60. if (pte_none(*pte)) {
  61. pte_t entry;
  62. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  63. if (!p)
  64. return 0;
  65. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
  66. set_pte_at(&init_mm, addr, pte, entry);
  67. }
  68. return pte;
  69. }
  70. pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
  71. {
  72. pmd_t *pmd = pmd_offset(pud, addr);
  73. if (pmd_none(*pmd)) {
  74. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  75. if (!p)
  76. return 0;
  77. pmd_populate_kernel(&init_mm, pmd, p);
  78. }
  79. return pmd;
  80. }
  81. pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node)
  82. {
  83. pud_t *pud = pud_offset(pgd, addr);
  84. if (pud_none(*pud)) {
  85. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  86. if (!p)
  87. return 0;
  88. pud_populate(&init_mm, pud, p);
  89. }
  90. return pud;
  91. }
  92. pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
  93. {
  94. pgd_t *pgd = pgd_offset_k(addr);
  95. if (pgd_none(*pgd)) {
  96. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  97. if (!p)
  98. return 0;
  99. pgd_populate(&init_mm, pgd, p);
  100. }
  101. return pgd;
  102. }
  103. int __meminit vmemmap_populate_basepages(struct page *start_page,
  104. unsigned long size, int node)
  105. {
  106. unsigned long addr = (unsigned long)start_page;
  107. unsigned long end = (unsigned long)(start_page + size);
  108. pgd_t *pgd;
  109. pud_t *pud;
  110. pmd_t *pmd;
  111. pte_t *pte;
  112. for (; addr < end; addr += PAGE_SIZE) {
  113. pgd = vmemmap_pgd_populate(addr, node);
  114. if (!pgd)
  115. return -ENOMEM;
  116. pud = vmemmap_pud_populate(pgd, addr, node);
  117. if (!pud)
  118. return -ENOMEM;
  119. pmd = vmemmap_pmd_populate(pud, addr, node);
  120. if (!pmd)
  121. return -ENOMEM;
  122. pte = vmemmap_pte_populate(pmd, addr, node);
  123. if (!pte)
  124. return -ENOMEM;
  125. vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
  126. }
  127. return 0;
  128. }
  129. struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid)
  130. {
  131. struct page *map = pfn_to_page(pnum * PAGES_PER_SECTION);
  132. int error = vmemmap_populate(map, PAGES_PER_SECTION, nid);
  133. if (error)
  134. return NULL;
  135. return map;
  136. }