pgtable_64.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * This file contains ioremap and related functions for 64-bit machines.
  3. *
  4. * Derived from arch/ppc64/mm/init.c
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. *
  7. * Modifications by Paul Mackerras (PowerMac) (paulus@samba.org)
  8. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  9. * Copyright (C) 1996 Paul Mackerras
  10. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  11. *
  12. * Derived from "arch/i386/mm/init.c"
  13. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  14. *
  15. * Dave Engebretsen <engebret@us.ibm.com>
  16. * Rework for PPC64 port.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. */
  24. #include <linux/signal.h>
  25. #include <linux/sched.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/string.h>
  29. #include <linux/types.h>
  30. #include <linux/mman.h>
  31. #include <linux/mm.h>
  32. #include <linux/swap.h>
  33. #include <linux/stddef.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/init.h>
  36. #include <asm/pgalloc.h>
  37. #include <asm/page.h>
  38. #include <asm/prom.h>
  39. #include <asm/io.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/mmu.h>
  43. #include <asm/smp.h>
  44. #include <asm/machdep.h>
  45. #include <asm/tlb.h>
  46. #include <asm/processor.h>
  47. #include <asm/cputable.h>
  48. #include <asm/sections.h>
  49. #include <asm/system.h>
  50. #include <asm/abs_addr.h>
  51. #include <asm/firmware.h>
  52. #include "mmu_decl.h"
  53. unsigned long ioremap_bot = IOREMAP_BASE;
  54. /*
  55. * map_io_page currently only called by __ioremap
  56. * map_io_page adds an entry to the ioremap page table
  57. * and adds an entry to the HPT, possibly bolting it
  58. */
  59. static int map_io_page(unsigned long ea, unsigned long pa, int flags)
  60. {
  61. pgd_t *pgdp;
  62. pud_t *pudp;
  63. pmd_t *pmdp;
  64. pte_t *ptep;
  65. if (mem_init_done) {
  66. pgdp = pgd_offset_k(ea);
  67. pudp = pud_alloc(&init_mm, pgdp, ea);
  68. if (!pudp)
  69. return -ENOMEM;
  70. pmdp = pmd_alloc(&init_mm, pudp, ea);
  71. if (!pmdp)
  72. return -ENOMEM;
  73. ptep = pte_alloc_kernel(pmdp, ea);
  74. if (!ptep)
  75. return -ENOMEM;
  76. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  77. __pgprot(flags)));
  78. } else {
  79. /*
  80. * If the mm subsystem is not fully up, we cannot create a
  81. * linux page table entry for this mapping. Simply bolt an
  82. * entry in the hardware page table.
  83. *
  84. */
  85. if (htab_bolt_mapping(ea, (unsigned long)ea + PAGE_SIZE,
  86. pa, flags, mmu_io_psize)) {
  87. printk(KERN_ERR "Failed to do bolted mapping IO "
  88. "memory at %016lx !\n", pa);
  89. return -ENOMEM;
  90. }
  91. }
  92. return 0;
  93. }
  94. /**
  95. * __ioremap_at - Low level function to establish the page tables
  96. * for an IO mapping
  97. */
  98. void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size,
  99. unsigned long flags)
  100. {
  101. unsigned long i;
  102. if ((flags & _PAGE_PRESENT) == 0)
  103. flags |= pgprot_val(PAGE_KERNEL);
  104. WARN_ON(pa & ~PAGE_MASK);
  105. WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
  106. WARN_ON(size & ~PAGE_MASK);
  107. for (i = 0; i < size; i += PAGE_SIZE)
  108. if (map_io_page((unsigned long)ea+i, pa+i, flags))
  109. return NULL;
  110. return (void __iomem *)ea;
  111. }
  112. /**
  113. * __iounmap_from - Low level function to tear down the page tables
  114. * for an IO mapping. This is used for mappings that
  115. * are manipulated manually, like partial unmapping of
  116. * PCI IOs or ISA space.
  117. */
  118. void __iounmap_at(void *ea, unsigned long size)
  119. {
  120. WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
  121. WARN_ON(size & ~PAGE_MASK);
  122. unmap_kernel_range((unsigned long)ea, size);
  123. }
  124. void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
  125. unsigned long flags)
  126. {
  127. phys_addr_t paligned;
  128. void __iomem *ret;
  129. /*
  130. * Choose an address to map it to.
  131. * Once the imalloc system is running, we use it.
  132. * Before that, we map using addresses going
  133. * up from ioremap_bot. imalloc will use
  134. * the addresses from ioremap_bot through
  135. * IMALLOC_END
  136. *
  137. */
  138. paligned = addr & PAGE_MASK;
  139. size = PAGE_ALIGN(addr + size) - paligned;
  140. if ((size == 0) || (paligned == 0))
  141. return NULL;
  142. if (mem_init_done) {
  143. struct vm_struct *area;
  144. area = __get_vm_area(size, VM_IOREMAP,
  145. ioremap_bot, IOREMAP_END);
  146. if (area == NULL)
  147. return NULL;
  148. ret = __ioremap_at(paligned, area->addr, size, flags);
  149. if (!ret)
  150. vunmap(area->addr);
  151. } else {
  152. ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags);
  153. if (ret)
  154. ioremap_bot += size;
  155. }
  156. if (ret)
  157. ret += addr & ~PAGE_MASK;
  158. return ret;
  159. }
  160. void __iomem * ioremap(phys_addr_t addr, unsigned long size)
  161. {
  162. unsigned long flags = _PAGE_NO_CACHE | _PAGE_GUARDED;
  163. if (ppc_md.ioremap)
  164. return ppc_md.ioremap(addr, size, flags);
  165. return __ioremap(addr, size, flags);
  166. }
  167. void __iomem * ioremap_flags(phys_addr_t addr, unsigned long size,
  168. unsigned long flags)
  169. {
  170. if (ppc_md.ioremap)
  171. return ppc_md.ioremap(addr, size, flags);
  172. return __ioremap(addr, size, flags);
  173. }
  174. /*
  175. * Unmap an IO region and remove it from imalloc'd list.
  176. * Access to IO memory should be serialized by driver.
  177. */
  178. void __iounmap(volatile void __iomem *token)
  179. {
  180. void *addr;
  181. if (!mem_init_done)
  182. return;
  183. addr = (void *) ((unsigned long __force)
  184. PCI_FIX_ADDR(token) & PAGE_MASK);
  185. if ((unsigned long)addr < ioremap_bot) {
  186. printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
  187. " at 0x%p\n", addr);
  188. return;
  189. }
  190. vunmap(addr);
  191. }
  192. void iounmap(volatile void __iomem *token)
  193. {
  194. if (ppc_md.iounmap)
  195. ppc_md.iounmap(token);
  196. else
  197. __iounmap(token);
  198. }
  199. EXPORT_SYMBOL(ioremap);
  200. EXPORT_SYMBOL(ioremap_flags);
  201. EXPORT_SYMBOL(__ioremap);
  202. EXPORT_SYMBOL(iounmap);
  203. EXPORT_SYMBOL(__iounmap);