sparse-vmemmap.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <linux/sched.h>
  28. #include <asm/dma.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/pgtable.h>
  31. /*
  32. * Allocate a block of memory to be used to back the virtual memory map
  33. * or to back the page tables that are used to create the mapping.
  34. * Uses the main allocators if they are available, else bootmem.
  35. */
  36. void * __meminit vmemmap_alloc_block(unsigned long size, int node)
  37. {
  38. /* If the main allocator is up use that, fallback to bootmem. */
  39. if (slab_is_available()) {
  40. struct page *page = alloc_pages_node(node,
  41. GFP_KERNEL | __GFP_ZERO, get_order(size));
  42. if (page)
  43. return page_address(page);
  44. return NULL;
  45. } else
  46. return __alloc_bootmem_node(NODE_DATA(node), size, size,
  47. __pa(MAX_DMA_ADDRESS));
  48. }
  49. void __meminit vmemmap_verify(pte_t *pte, int node,
  50. unsigned long start, unsigned long end)
  51. {
  52. unsigned long pfn = pte_pfn(*pte);
  53. int actual_node = early_pfn_to_nid(pfn);
  54. if (actual_node != node)
  55. printk(KERN_WARNING "[%lx-%lx] potential offnode "
  56. "page_structs\n", start, end - 1);
  57. }
  58. pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node)
  59. {
  60. pte_t *pte = pte_offset_kernel(pmd, addr);
  61. if (pte_none(*pte)) {
  62. pte_t entry;
  63. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  64. if (!p)
  65. return 0;
  66. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
  67. set_pte_at(&init_mm, addr, pte, entry);
  68. }
  69. return pte;
  70. }
  71. pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
  72. {
  73. pmd_t *pmd = pmd_offset(pud, addr);
  74. if (pmd_none(*pmd)) {
  75. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  76. if (!p)
  77. return 0;
  78. pmd_populate_kernel(&init_mm, pmd, p);
  79. }
  80. return pmd;
  81. }
  82. pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node)
  83. {
  84. pud_t *pud = pud_offset(pgd, addr);
  85. if (pud_none(*pud)) {
  86. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  87. if (!p)
  88. return 0;
  89. pud_populate(&init_mm, pud, p);
  90. }
  91. return pud;
  92. }
  93. pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
  94. {
  95. pgd_t *pgd = pgd_offset_k(addr);
  96. if (pgd_none(*pgd)) {
  97. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  98. if (!p)
  99. return 0;
  100. pgd_populate(&init_mm, pgd, p);
  101. }
  102. return pgd;
  103. }
  104. int __meminit vmemmap_populate_basepages(struct page *start_page,
  105. unsigned long size, int node)
  106. {
  107. unsigned long addr = (unsigned long)start_page;
  108. unsigned long end = (unsigned long)(start_page + size);
  109. pgd_t *pgd;
  110. pud_t *pud;
  111. pmd_t *pmd;
  112. pte_t *pte;
  113. for (; addr < end; addr += PAGE_SIZE) {
  114. pgd = vmemmap_pgd_populate(addr, node);
  115. if (!pgd)
  116. return -ENOMEM;
  117. pud = vmemmap_pud_populate(pgd, addr, node);
  118. if (!pud)
  119. return -ENOMEM;
  120. pmd = vmemmap_pmd_populate(pud, addr, node);
  121. if (!pmd)
  122. return -ENOMEM;
  123. pte = vmemmap_pte_populate(pmd, addr, node);
  124. if (!pte)
  125. return -ENOMEM;
  126. vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
  127. }
  128. return 0;
  129. }
  130. struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid)
  131. {
  132. struct page *map = pfn_to_page(pnum * PAGES_PER_SECTION);
  133. int error = vmemmap_populate(map, PAGES_PER_SECTION, nid);
  134. if (error)
  135. return NULL;
  136. return map;
  137. }