pgtable.c 6.1 KB

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