pgtable.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/kernel.h>
  29. #include <linux/module.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. #define flush_HPTE(X, va, pg) _tlbie(va)
  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((unsigned long)&__bss_stop) &&
  69. p < virt_to_phys((unsigned long)__bss_stop))) {
  70. printk(KERN_WARNING "__ioremap(): phys addr "PTE_FMT
  71. " is RAM lr %pf\n", (unsigned long)p,
  72. __builtin_return_address(0));
  73. return NULL;
  74. }
  75. if (size == 0)
  76. return NULL;
  77. /*
  78. * Is it already mapped? If the whole area is mapped then we're
  79. * done, otherwise remap it since we want to keep the virt addrs for
  80. * each request contiguous.
  81. *
  82. * We make the assumption here that if the bottom and top
  83. * of the range we want are mapped then it's mapped to the
  84. * same virt address (and this is contiguous).
  85. * -- Cort
  86. */
  87. if (mem_init_done) {
  88. struct vm_struct *area;
  89. area = get_vm_area(size, VM_IOREMAP);
  90. if (area == NULL)
  91. return NULL;
  92. v = (unsigned long) area->addr;
  93. } else {
  94. v = (ioremap_bot -= size);
  95. }
  96. if ((flags & _PAGE_PRESENT) == 0)
  97. flags |= _PAGE_KERNEL;
  98. if (flags & _PAGE_NO_CACHE)
  99. flags |= _PAGE_GUARDED;
  100. err = 0;
  101. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  102. err = map_page(v + i, p + i, flags);
  103. if (err) {
  104. if (mem_init_done)
  105. vfree((void *)v);
  106. return NULL;
  107. }
  108. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  109. }
  110. void __iomem *ioremap(phys_addr_t addr, unsigned long size)
  111. {
  112. return __ioremap(addr, size, _PAGE_NO_CACHE);
  113. }
  114. EXPORT_SYMBOL(ioremap);
  115. void iounmap(void *addr)
  116. {
  117. if (addr > high_memory && (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. flush_HPTE(0, va, pmd_val(*pd));
  137. /* flush_HPTE(0, va, pg); */
  138. }
  139. return err;
  140. }
  141. /*
  142. * Map in all of physical memory starting at CONFIG_KERNEL_START.
  143. */
  144. void __init mapin_ram(void)
  145. {
  146. unsigned long v, p, s, f;
  147. v = CONFIG_KERNEL_START;
  148. p = memory_start;
  149. for (s = 0; s < lowmem_size; s += PAGE_SIZE) {
  150. f = _PAGE_PRESENT | _PAGE_ACCESSED |
  151. _PAGE_SHARED | _PAGE_HWEXEC;
  152. if ((char *) v < _stext || (char *) v >= _etext)
  153. f |= _PAGE_WRENABLE;
  154. else
  155. /* On the MicroBlaze, no user access
  156. forces R/W kernel access */
  157. f |= _PAGE_USER;
  158. map_page(v, p, f);
  159. v += PAGE_SIZE;
  160. p += PAGE_SIZE;
  161. }
  162. }
  163. /* is x a power of 2? */
  164. #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
  165. /* Scan the real Linux page tables and return a PTE pointer for
  166. * a virtual address in a context.
  167. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  168. * the PTE pointer is unmodified if PTE is not found.
  169. */
  170. static int get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
  171. {
  172. pgd_t *pgd;
  173. pmd_t *pmd;
  174. pte_t *pte;
  175. int retval = 0;
  176. pgd = pgd_offset(mm, addr & PAGE_MASK);
  177. if (pgd) {
  178. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  179. if (pmd_present(*pmd)) {
  180. pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
  181. if (pte) {
  182. retval = 1;
  183. *ptep = pte;
  184. }
  185. }
  186. }
  187. return retval;
  188. }
  189. /* Find physical address for this virtual address. Normally used by
  190. * I/O functions, but anyone can call it.
  191. */
  192. unsigned long iopa(unsigned long addr)
  193. {
  194. unsigned long pa;
  195. pte_t *pte;
  196. struct mm_struct *mm;
  197. /* Allow mapping of user addresses (within the thread)
  198. * for DMA if necessary.
  199. */
  200. if (addr < TASK_SIZE)
  201. mm = current->mm;
  202. else
  203. mm = &init_mm;
  204. pa = 0;
  205. if (get_pteptr(mm, addr, &pte))
  206. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  207. return pa;
  208. }
  209. __init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm,
  210. unsigned long address)
  211. {
  212. pte_t *pte;
  213. if (mem_init_done) {
  214. pte = (pte_t *)__get_free_page(GFP_KERNEL |
  215. __GFP_REPEAT | __GFP_ZERO);
  216. } else {
  217. pte = (pte_t *)early_get_page();
  218. if (pte)
  219. clear_page(pte);
  220. }
  221. return pte;
  222. }
  223. void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
  224. {
  225. unsigned long address = __fix_to_virt(idx);
  226. if (idx >= __end_of_fixed_addresses)
  227. BUG();
  228. map_page(address, phys, pgprot_val(flags));
  229. }