pgtable.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #define flush_HPTE(X, va, pg) _tlbie(va)
  39. unsigned long ioremap_base;
  40. unsigned long ioremap_bot;
  41. /* The maximum lowmem defaults to 768Mb, but this can be configured to
  42. * another value.
  43. */
  44. #define MAX_LOW_MEM CONFIG_LOWMEM_SIZE
  45. #ifndef CONFIG_SMP
  46. struct pgtable_cache_struct quicklists;
  47. #endif
  48. static void __iomem *__ioremap(phys_addr_t addr, unsigned long size,
  49. unsigned long flags)
  50. {
  51. unsigned long v, i;
  52. phys_addr_t p;
  53. int err;
  54. /*
  55. * Choose an address to map it to.
  56. * Once the vmalloc system is running, we use it.
  57. * Before then, we use space going down from ioremap_base
  58. * (ioremap_bot records where we're up to).
  59. */
  60. p = addr & PAGE_MASK;
  61. size = PAGE_ALIGN(addr + size) - p;
  62. /*
  63. * Don't allow anybody to remap normal RAM that we're using.
  64. * mem_init() sets high_memory so only do the check after that.
  65. *
  66. * However, allow remap of rootfs: TBD
  67. */
  68. if (mem_init_done &&
  69. p >= memory_start && p < virt_to_phys(high_memory) &&
  70. !(p >= virt_to_phys((unsigned long)&__bss_stop) &&
  71. p < virt_to_phys((unsigned long)__bss_stop))) {
  72. printk(KERN_WARNING "__ioremap(): phys addr "PTE_FMT
  73. " is RAM lr %p\n", (unsigned long)p,
  74. __builtin_return_address(0));
  75. return NULL;
  76. }
  77. if (size == 0)
  78. return NULL;
  79. /*
  80. * Is it already mapped? If the whole area is mapped then we're
  81. * done, otherwise remap it since we want to keep the virt addrs for
  82. * each request contiguous.
  83. *
  84. * We make the assumption here that if the bottom and top
  85. * of the range we want are mapped then it's mapped to the
  86. * same virt address (and this is contiguous).
  87. * -- Cort
  88. */
  89. if (mem_init_done) {
  90. struct vm_struct *area;
  91. area = get_vm_area(size, VM_IOREMAP);
  92. if (area == NULL)
  93. return NULL;
  94. v = VMALLOC_VMADDR(area->addr);
  95. } else {
  96. v = (ioremap_bot -= size);
  97. }
  98. if ((flags & _PAGE_PRESENT) == 0)
  99. flags |= _PAGE_KERNEL;
  100. if (flags & _PAGE_NO_CACHE)
  101. flags |= _PAGE_GUARDED;
  102. err = 0;
  103. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  104. err = map_page(v + i, p + i, flags);
  105. if (err) {
  106. if (mem_init_done)
  107. vfree((void *)v);
  108. return NULL;
  109. }
  110. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  111. }
  112. void __iomem *ioremap(phys_addr_t addr, unsigned long size)
  113. {
  114. return __ioremap(addr, size, _PAGE_NO_CACHE);
  115. }
  116. EXPORT_SYMBOL(ioremap);
  117. void iounmap(void *addr)
  118. {
  119. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  120. vfree((void *) (PAGE_MASK & (unsigned long) addr));
  121. }
  122. EXPORT_SYMBOL(iounmap);
  123. int map_page(unsigned long va, phys_addr_t pa, int flags)
  124. {
  125. pmd_t *pd;
  126. pte_t *pg;
  127. int err = -ENOMEM;
  128. /* spin_lock(&init_mm.page_table_lock); */
  129. /* Use upper 10 bits of VA to index the first level map */
  130. pd = pmd_offset(pgd_offset_k(va), 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 (mem_init_done)
  139. flush_HPTE(0, va, pmd_val(*pd));
  140. /* flush_HPTE(0, va, pg); */
  141. }
  142. /* spin_unlock(&init_mm.page_table_lock); */
  143. return err;
  144. }
  145. void __init adjust_total_lowmem(void)
  146. {
  147. /* TBD */
  148. #if 0
  149. unsigned long max_low_mem = MAX_LOW_MEM;
  150. if (total_lowmem > max_low_mem) {
  151. total_lowmem = max_low_mem;
  152. #ifndef CONFIG_HIGHMEM
  153. printk(KERN_INFO "Warning, memory limited to %ld Mb, use "
  154. "CONFIG_HIGHMEM to reach %ld Mb\n",
  155. max_low_mem >> 20, total_memory >> 20);
  156. total_memory = total_lowmem;
  157. #endif /* CONFIG_HIGHMEM */
  158. }
  159. #endif
  160. }
  161. static void show_tmem(unsigned long tmem)
  162. {
  163. volatile unsigned long a;
  164. a = a + tmem;
  165. }
  166. /*
  167. * Map in all of physical memory starting at CONFIG_KERNEL_START.
  168. */
  169. void __init mapin_ram(void)
  170. {
  171. unsigned long v, p, s, f;
  172. v = CONFIG_KERNEL_START;
  173. p = memory_start;
  174. show_tmem(memory_size);
  175. for (s = 0; s < memory_size; s += PAGE_SIZE) {
  176. f = _PAGE_PRESENT | _PAGE_ACCESSED |
  177. _PAGE_SHARED | _PAGE_HWEXEC;
  178. if ((char *) v < _stext || (char *) v >= _etext)
  179. f |= _PAGE_WRENABLE;
  180. else
  181. /* On the MicroBlaze, no user access
  182. forces R/W kernel access */
  183. f |= _PAGE_USER;
  184. map_page(v, p, f);
  185. v += PAGE_SIZE;
  186. p += PAGE_SIZE;
  187. }
  188. }
  189. /* is x a power of 2? */
  190. #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
  191. /*
  192. * Set up a mapping for a block of I/O.
  193. * virt, phys, size must all be page-aligned.
  194. * This should only be called before ioremap is called.
  195. */
  196. void __init io_block_mapping(unsigned long virt, phys_addr_t phys,
  197. unsigned int size, int flags)
  198. {
  199. int i;
  200. if (virt > CONFIG_KERNEL_START && virt < ioremap_bot)
  201. ioremap_bot = ioremap_base = virt;
  202. /* Put it in the page tables. */
  203. for (i = 0; i < size; i += PAGE_SIZE)
  204. map_page(virt + i, phys + i, flags);
  205. }
  206. /* Scan the real Linux page tables and return a PTE pointer for
  207. * a virtual address in a context.
  208. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  209. * the PTE pointer is unmodified if PTE is not found.
  210. */
  211. static int get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
  212. {
  213. pgd_t *pgd;
  214. pmd_t *pmd;
  215. pte_t *pte;
  216. int retval = 0;
  217. pgd = pgd_offset(mm, addr & PAGE_MASK);
  218. if (pgd) {
  219. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  220. if (pmd_present(*pmd)) {
  221. pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
  222. if (pte) {
  223. retval = 1;
  224. *ptep = pte;
  225. }
  226. }
  227. }
  228. return retval;
  229. }
  230. /* Find physical address for this virtual address. Normally used by
  231. * I/O functions, but anyone can call it.
  232. */
  233. unsigned long iopa(unsigned long addr)
  234. {
  235. unsigned long pa;
  236. pte_t *pte;
  237. struct mm_struct *mm;
  238. /* Allow mapping of user addresses (within the thread)
  239. * for DMA if necessary.
  240. */
  241. if (addr < TASK_SIZE)
  242. mm = current->mm;
  243. else
  244. mm = &init_mm;
  245. pa = 0;
  246. if (get_pteptr(mm, addr, &pte))
  247. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  248. return pa;
  249. }