pgtable.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <asm/pgtable.h>
  34. #include <asm/pgalloc.h>
  35. #include <linux/io.h>
  36. #include <asm/mmu.h>
  37. #include <asm/sections.h>
  38. #include <asm/fixmap.h>
  39. unsigned long ioremap_base;
  40. unsigned long ioremap_bot;
  41. EXPORT_SYMBOL(ioremap_bot);
  42. #ifndef CONFIG_SMP
  43. struct pgtable_cache_struct quicklists;
  44. #endif
  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 %pf\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(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. pmd_t *pd;
  123. pte_t *pg;
  124. int err = -ENOMEM;
  125. /* Use upper 10 bits of VA to index the first level map */
  126. pd = pmd_offset(pgd_offset_k(va), va);
  127. /* Use middle 10 bits of VA to index the second-level map */
  128. pg = pte_alloc_kernel(pd, va); /* from powerpc - pgtable.c */
  129. /* pg = pte_alloc_kernel(&init_mm, pd, va); */
  130. if (pg != NULL) {
  131. err = 0;
  132. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
  133. __pgprot(flags)));
  134. if (unlikely(mem_init_done))
  135. _tlbie(va);
  136. }
  137. return err;
  138. }
  139. /*
  140. * Map in all of physical memory starting at CONFIG_KERNEL_START.
  141. */
  142. void __init mapin_ram(void)
  143. {
  144. unsigned long v, p, s, f;
  145. v = CONFIG_KERNEL_START;
  146. p = memory_start;
  147. for (s = 0; s < lowmem_size; s += PAGE_SIZE) {
  148. f = _PAGE_PRESENT | _PAGE_ACCESSED |
  149. _PAGE_SHARED | _PAGE_HWEXEC;
  150. if ((char *) v < _stext || (char *) v >= _etext)
  151. f |= _PAGE_WRENABLE;
  152. else
  153. /* On the MicroBlaze, no user access
  154. forces R/W kernel access */
  155. f |= _PAGE_USER;
  156. map_page(v, p, f);
  157. v += PAGE_SIZE;
  158. p += PAGE_SIZE;
  159. }
  160. }
  161. /* is x a power of 2? */
  162. #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
  163. /* Scan the real Linux page tables and return a PTE pointer for
  164. * a virtual address in a context.
  165. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  166. * the PTE pointer is unmodified if PTE is not found.
  167. */
  168. static int get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
  169. {
  170. pgd_t *pgd;
  171. pmd_t *pmd;
  172. pte_t *pte;
  173. int retval = 0;
  174. pgd = pgd_offset(mm, addr & PAGE_MASK);
  175. if (pgd) {
  176. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  177. if (pmd_present(*pmd)) {
  178. pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
  179. if (pte) {
  180. retval = 1;
  181. *ptep = pte;
  182. }
  183. }
  184. }
  185. return retval;
  186. }
  187. /* Find physical address for this virtual address. Normally used by
  188. * I/O functions, but anyone can call it.
  189. */
  190. unsigned long iopa(unsigned long addr)
  191. {
  192. unsigned long pa;
  193. pte_t *pte;
  194. struct mm_struct *mm;
  195. /* Allow mapping of user addresses (within the thread)
  196. * for DMA if necessary.
  197. */
  198. if (addr < TASK_SIZE)
  199. mm = current->mm;
  200. else
  201. mm = &init_mm;
  202. pa = 0;
  203. if (get_pteptr(mm, addr, &pte))
  204. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  205. return pa;
  206. }
  207. __ref pte_t *pte_alloc_one_kernel(struct mm_struct *mm,
  208. unsigned long address)
  209. {
  210. pte_t *pte;
  211. if (mem_init_done) {
  212. pte = (pte_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  213. } else {
  214. pte = (pte_t *)early_get_page();
  215. if (pte)
  216. clear_page(pte);
  217. }
  218. return pte;
  219. }
  220. void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
  221. {
  222. unsigned long address = __fix_to_virt(idx);
  223. if (idx >= __end_of_fixed_addresses)
  224. BUG();
  225. map_page(address, phys, pgprot_val(flags));
  226. }