pgtable.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * This file contains the routines setting up the linux page tables.
  3. *
  4. * Copyright (C) 2008 Michal Simek
  5. * Copyright (C) 2008 PetaLogix
  6. *
  7. * Copyright (C) 2007 Xilinx, Inc. All rights reserved.
  8. *
  9. * Derived from arch/ppc/mm/pgtable.c:
  10. * -- paulus
  11. *
  12. * Derived from arch/ppc/mm/init.c:
  13. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  14. *
  15. * Modifications by Paul Mackerras (PowerMac) ([email protected])
  16. * and Cort Dougan (PReP) ([email protected])
  17. * Copyright (C) 1996 Paul Mackerras
  18. * Amiga/APUS changes by Jesper Skov ([email protected]).
  19. *
  20. * Derived from "arch/i386/mm/init.c"
  21. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  22. *
  23. * This file is subject to the terms and conditions of the GNU General
  24. * Public License. See the file COPYING in the main directory of this
  25. * archive for more details.
  26. *
  27. */
  28. #include <linux/export.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/init.h>
  33. #include <linux/mm_types.h>
  34. #include <linux/pgtable.h>
  35. #include <linux/memblock.h>
  36. #include <linux/kallsyms.h>
  37. #include <asm/pgalloc.h>
  38. #include <linux/io.h>
  39. #include <asm/mmu.h>
  40. #include <asm/sections.h>
  41. #include <asm/fixmap.h>
  42. unsigned long ioremap_base;
  43. unsigned long ioremap_bot;
  44. EXPORT_SYMBOL(ioremap_bot);
  45. static void __iomem *__ioremap(phys_addr_t addr, unsigned long size,
  46. unsigned long flags)
  47. {
  48. unsigned long v, i;
  49. phys_addr_t p;
  50. int err;
  51. /*
  52. * Choose an address to map it to.
  53. * Once the vmalloc system is running, we use it.
  54. * Before then, we use space going down from ioremap_base
  55. * (ioremap_bot records where we're up to).
  56. */
  57. p = addr & PAGE_MASK;
  58. size = PAGE_ALIGN(addr + size) - p;
  59. /*
  60. * Don't allow anybody to remap normal RAM that we're using.
  61. * mem_init() sets high_memory so only do the check after that.
  62. *
  63. * However, allow remap of rootfs: TBD
  64. */
  65. if (mem_init_done &&
  66. p >= memory_start && p < virt_to_phys(high_memory) &&
  67. !(p >= __virt_to_phys((phys_addr_t)__bss_stop) &&
  68. p < __virt_to_phys((phys_addr_t)__bss_stop))) {
  69. pr_warn("__ioremap(): phys addr "PTE_FMT" is RAM lr %ps\n",
  70. (unsigned long)p, __builtin_return_address(0));
  71. return NULL;
  72. }
  73. if (size == 0)
  74. return NULL;
  75. /*
  76. * Is it already mapped? If the whole area is mapped then we're
  77. * done, otherwise remap it since we want to keep the virt addrs for
  78. * each request contiguous.
  79. *
  80. * We make the assumption here that if the bottom and top
  81. * of the range we want are mapped then it's mapped to the
  82. * same virt address (and this is contiguous).
  83. * -- Cort
  84. */
  85. if (mem_init_done) {
  86. struct vm_struct *area;
  87. area = get_vm_area(size, VM_IOREMAP);
  88. if (area == NULL)
  89. return NULL;
  90. v = (unsigned long) area->addr;
  91. } else {
  92. v = (ioremap_bot -= size);
  93. }
  94. if ((flags & _PAGE_PRESENT) == 0)
  95. flags |= _PAGE_KERNEL;
  96. if (flags & _PAGE_NO_CACHE)
  97. flags |= _PAGE_GUARDED;
  98. err = 0;
  99. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  100. err = map_page(v + i, p + i, flags);
  101. if (err) {
  102. if (mem_init_done)
  103. vfree((void *)v);
  104. return NULL;
  105. }
  106. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  107. }
  108. void __iomem *ioremap(phys_addr_t addr, unsigned long size)
  109. {
  110. return __ioremap(addr, size, _PAGE_NO_CACHE);
  111. }
  112. EXPORT_SYMBOL(ioremap);
  113. void iounmap(volatile void __iomem *addr)
  114. {
  115. if ((__force void *)addr > high_memory &&
  116. (unsigned long) addr < ioremap_bot)
  117. vfree((void *) (PAGE_MASK & (unsigned long) addr));
  118. }
  119. EXPORT_SYMBOL(iounmap);
  120. int map_page(unsigned long va, phys_addr_t pa, int flags)
  121. {
  122. p4d_t *p4d;
  123. pud_t *pud;
  124. pmd_t *pd;
  125. pte_t *pg;
  126. int err = -ENOMEM;
  127. /* Use upper 10 bits of VA to index the first level map */
  128. p4d = p4d_offset(pgd_offset_k(va), va);
  129. pud = pud_offset(p4d, va);
  130. pd = pmd_offset(pud, va);
  131. /* Use middle 10 bits of VA to index the second-level map */
  132. pg = pte_alloc_kernel(pd, va); /* from powerpc - pgtable.c */
  133. /* pg = pte_alloc_kernel(&init_mm, pd, va); */
  134. if (pg != NULL) {
  135. err = 0;
  136. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
  137. __pgprot(flags)));
  138. if (unlikely(mem_init_done))
  139. _tlbie(va);
  140. }
  141. return err;
  142. }
  143. /*
  144. * Map in all of physical memory starting at CONFIG_KERNEL_START.
  145. */
  146. void __init mapin_ram(void)
  147. {
  148. unsigned long v, p, s, f;
  149. v = CONFIG_KERNEL_START;
  150. p = memory_start;
  151. for (s = 0; s < lowmem_size; s += PAGE_SIZE) {
  152. f = _PAGE_PRESENT | _PAGE_ACCESSED |
  153. _PAGE_SHARED | _PAGE_HWEXEC;
  154. if (!is_kernel_text(v))
  155. f |= _PAGE_WRENABLE;
  156. else
  157. /* On the MicroBlaze, no user access
  158. forces R/W kernel access */
  159. f |= _PAGE_USER;
  160. map_page(v, p, f);
  161. v += PAGE_SIZE;
  162. p += PAGE_SIZE;
  163. }
  164. }
  165. /* is x a power of 2? */
  166. #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
  167. /* Scan the real Linux page tables and return a PTE pointer for
  168. * a virtual address in a context.
  169. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  170. * the PTE pointer is unmodified if PTE is not found.
  171. */
  172. static int get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
  173. {
  174. pgd_t *pgd;
  175. p4d_t *p4d;
  176. pud_t *pud;
  177. pmd_t *pmd;
  178. pte_t *pte;
  179. int retval = 0;
  180. pgd = pgd_offset(mm, addr & PAGE_MASK);
  181. if (pgd) {
  182. p4d = p4d_offset(pgd, addr & PAGE_MASK);
  183. pud = pud_offset(p4d, addr & PAGE_MASK);
  184. pmd = pmd_offset(pud, addr & PAGE_MASK);
  185. if (pmd_present(*pmd)) {
  186. pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
  187. if (pte) {
  188. retval = 1;
  189. *ptep = pte;
  190. }
  191. }
  192. }
  193. return retval;
  194. }
  195. /* Find physical address for this virtual address. Normally used by
  196. * I/O functions, but anyone can call it.
  197. */
  198. unsigned long iopa(unsigned long addr)
  199. {
  200. unsigned long pa;
  201. pte_t *pte;
  202. struct mm_struct *mm;
  203. /* Allow mapping of user addresses (within the thread)
  204. * for DMA if necessary.
  205. */
  206. if (addr < TASK_SIZE)
  207. mm = current->mm;
  208. else
  209. mm = &init_mm;
  210. pa = 0;
  211. if (get_pteptr(mm, addr, &pte))
  212. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  213. return pa;
  214. }
  215. __ref pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
  216. {
  217. if (mem_init_done)
  218. return (pte_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  219. else
  220. return memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
  221. MEMBLOCK_LOW_LIMIT,
  222. memory_start + kernel_tlb,
  223. NUMA_NO_NODE);
  224. }
  225. void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
  226. {
  227. unsigned long address = __fix_to_virt(idx);
  228. if (idx >= __end_of_fixed_addresses)
  229. BUG();
  230. map_page(address, phys, pgprot_val(flags));
  231. }