pgtable_64.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. *
  11. * Derived from "arch/i386/mm/init.c"
  12. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  13. *
  14. * Dave Engebretsen <engebret@us.ibm.com>
  15. * Rework for PPC64 port.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. *
  22. */
  23. #include <linux/signal.h>
  24. #include <linux/sched.h>
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/string.h>
  28. #include <linux/export.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 <linux/bootmem.h>
  37. #include <linux/memblock.h>
  38. #include <linux/slab.h>
  39. #include <asm/pgalloc.h>
  40. #include <asm/page.h>
  41. #include <asm/prom.h>
  42. #include <asm/io.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/mmu.h>
  46. #include <asm/smp.h>
  47. #include <asm/machdep.h>
  48. #include <asm/tlb.h>
  49. #include <asm/processor.h>
  50. #include <asm/cputable.h>
  51. #include <asm/sections.h>
  52. #include <asm/system.h>
  53. #include <asm/abs_addr.h>
  54. #include <asm/firmware.h>
  55. #include "mmu_decl.h"
  56. unsigned long ioremap_bot = IOREMAP_BASE;
  57. #ifdef CONFIG_PPC_MMU_NOHASH
  58. static void *early_alloc_pgtable(unsigned long size)
  59. {
  60. void *pt;
  61. if (init_bootmem_done)
  62. pt = __alloc_bootmem(size, size, __pa(MAX_DMA_ADDRESS));
  63. else
  64. pt = __va(memblock_alloc_base(size, size,
  65. __pa(MAX_DMA_ADDRESS)));
  66. memset(pt, 0, size);
  67. return pt;
  68. }
  69. #endif /* CONFIG_PPC_MMU_NOHASH */
  70. /*
  71. * map_kernel_page currently only called by __ioremap
  72. * map_kernel_page adds an entry to the ioremap page table
  73. * and adds an entry to the HPT, possibly bolting it
  74. */
  75. int map_kernel_page(unsigned long ea, unsigned long pa, int flags)
  76. {
  77. pgd_t *pgdp;
  78. pud_t *pudp;
  79. pmd_t *pmdp;
  80. pte_t *ptep;
  81. if (slab_is_available()) {
  82. pgdp = pgd_offset_k(ea);
  83. pudp = pud_alloc(&init_mm, pgdp, ea);
  84. if (!pudp)
  85. return -ENOMEM;
  86. pmdp = pmd_alloc(&init_mm, pudp, ea);
  87. if (!pmdp)
  88. return -ENOMEM;
  89. ptep = pte_alloc_kernel(pmdp, ea);
  90. if (!ptep)
  91. return -ENOMEM;
  92. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  93. __pgprot(flags)));
  94. } else {
  95. #ifdef CONFIG_PPC_MMU_NOHASH
  96. /* Warning ! This will blow up if bootmem is not initialized
  97. * which our ppc64 code is keen to do that, we'll need to
  98. * fix it and/or be more careful
  99. */
  100. pgdp = pgd_offset_k(ea);
  101. #ifdef PUD_TABLE_SIZE
  102. if (pgd_none(*pgdp)) {
  103. pudp = early_alloc_pgtable(PUD_TABLE_SIZE);
  104. BUG_ON(pudp == NULL);
  105. pgd_populate(&init_mm, pgdp, pudp);
  106. }
  107. #endif /* PUD_TABLE_SIZE */
  108. pudp = pud_offset(pgdp, ea);
  109. if (pud_none(*pudp)) {
  110. pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
  111. BUG_ON(pmdp == NULL);
  112. pud_populate(&init_mm, pudp, pmdp);
  113. }
  114. pmdp = pmd_offset(pudp, ea);
  115. if (!pmd_present(*pmdp)) {
  116. ptep = early_alloc_pgtable(PAGE_SIZE);
  117. BUG_ON(ptep == NULL);
  118. pmd_populate_kernel(&init_mm, pmdp, ptep);
  119. }
  120. ptep = pte_offset_kernel(pmdp, ea);
  121. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  122. __pgprot(flags)));
  123. #else /* CONFIG_PPC_MMU_NOHASH */
  124. /*
  125. * If the mm subsystem is not fully up, we cannot create a
  126. * linux page table entry for this mapping. Simply bolt an
  127. * entry in the hardware page table.
  128. *
  129. */
  130. if (htab_bolt_mapping(ea, ea + PAGE_SIZE, pa, flags,
  131. mmu_io_psize, mmu_kernel_ssize)) {
  132. printk(KERN_ERR "Failed to do bolted mapping IO "
  133. "memory at %016lx !\n", pa);
  134. return -ENOMEM;
  135. }
  136. #endif /* !CONFIG_PPC_MMU_NOHASH */
  137. }
  138. return 0;
  139. }
  140. /**
  141. * __ioremap_at - Low level function to establish the page tables
  142. * for an IO mapping
  143. */
  144. void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size,
  145. unsigned long flags)
  146. {
  147. unsigned long i;
  148. /* Make sure we have the base flags */
  149. if ((flags & _PAGE_PRESENT) == 0)
  150. flags |= pgprot_val(PAGE_KERNEL);
  151. /* Non-cacheable page cannot be coherent */
  152. if (flags & _PAGE_NO_CACHE)
  153. flags &= ~_PAGE_COHERENT;
  154. /* We don't support the 4K PFN hack with ioremap */
  155. if (flags & _PAGE_4K_PFN)
  156. return NULL;
  157. WARN_ON(pa & ~PAGE_MASK);
  158. WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
  159. WARN_ON(size & ~PAGE_MASK);
  160. for (i = 0; i < size; i += PAGE_SIZE)
  161. if (map_kernel_page((unsigned long)ea+i, pa+i, flags))
  162. return NULL;
  163. return (void __iomem *)ea;
  164. }
  165. /**
  166. * __iounmap_from - Low level function to tear down the page tables
  167. * for an IO mapping. This is used for mappings that
  168. * are manipulated manually, like partial unmapping of
  169. * PCI IOs or ISA space.
  170. */
  171. void __iounmap_at(void *ea, unsigned long size)
  172. {
  173. WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
  174. WARN_ON(size & ~PAGE_MASK);
  175. unmap_kernel_range((unsigned long)ea, size);
  176. }
  177. void __iomem * __ioremap_caller(phys_addr_t addr, unsigned long size,
  178. unsigned long flags, void *caller)
  179. {
  180. phys_addr_t paligned;
  181. void __iomem *ret;
  182. /*
  183. * Choose an address to map it to.
  184. * Once the imalloc system is running, we use it.
  185. * Before that, we map using addresses going
  186. * up from ioremap_bot. imalloc will use
  187. * the addresses from ioremap_bot through
  188. * IMALLOC_END
  189. *
  190. */
  191. paligned = addr & PAGE_MASK;
  192. size = PAGE_ALIGN(addr + size) - paligned;
  193. if ((size == 0) || (paligned == 0))
  194. return NULL;
  195. if (mem_init_done) {
  196. struct vm_struct *area;
  197. area = __get_vm_area_caller(size, VM_IOREMAP,
  198. ioremap_bot, IOREMAP_END,
  199. caller);
  200. if (area == NULL)
  201. return NULL;
  202. area->phys_addr = paligned;
  203. ret = __ioremap_at(paligned, area->addr, size, flags);
  204. if (!ret)
  205. vunmap(area->addr);
  206. } else {
  207. ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags);
  208. if (ret)
  209. ioremap_bot += size;
  210. }
  211. if (ret)
  212. ret += addr & ~PAGE_MASK;
  213. return ret;
  214. }
  215. void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
  216. unsigned long flags)
  217. {
  218. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  219. }
  220. void __iomem * ioremap(phys_addr_t addr, unsigned long size)
  221. {
  222. unsigned long flags = _PAGE_NO_CACHE | _PAGE_GUARDED;
  223. void *caller = __builtin_return_address(0);
  224. if (ppc_md.ioremap)
  225. return ppc_md.ioremap(addr, size, flags, caller);
  226. return __ioremap_caller(addr, size, flags, caller);
  227. }
  228. void __iomem * ioremap_wc(phys_addr_t addr, unsigned long size)
  229. {
  230. unsigned long flags = _PAGE_NO_CACHE;
  231. void *caller = __builtin_return_address(0);
  232. if (ppc_md.ioremap)
  233. return ppc_md.ioremap(addr, size, flags, caller);
  234. return __ioremap_caller(addr, size, flags, caller);
  235. }
  236. void __iomem * ioremap_prot(phys_addr_t addr, unsigned long size,
  237. unsigned long flags)
  238. {
  239. void *caller = __builtin_return_address(0);
  240. /* writeable implies dirty for kernel addresses */
  241. if (flags & _PAGE_RW)
  242. flags |= _PAGE_DIRTY;
  243. /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
  244. flags &= ~(_PAGE_USER | _PAGE_EXEC);
  245. #ifdef _PAGE_BAP_SR
  246. /* _PAGE_USER contains _PAGE_BAP_SR on BookE using the new PTE format
  247. * which means that we just cleared supervisor access... oops ;-) This
  248. * restores it
  249. */
  250. flags |= _PAGE_BAP_SR;
  251. #endif
  252. if (ppc_md.ioremap)
  253. return ppc_md.ioremap(addr, size, flags, caller);
  254. return __ioremap_caller(addr, size, flags, caller);
  255. }
  256. /*
  257. * Unmap an IO region and remove it from imalloc'd list.
  258. * Access to IO memory should be serialized by driver.
  259. */
  260. void __iounmap(volatile void __iomem *token)
  261. {
  262. void *addr;
  263. if (!mem_init_done)
  264. return;
  265. addr = (void *) ((unsigned long __force)
  266. PCI_FIX_ADDR(token) & PAGE_MASK);
  267. if ((unsigned long)addr < ioremap_bot) {
  268. printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
  269. " at 0x%p\n", addr);
  270. return;
  271. }
  272. vunmap(addr);
  273. }
  274. void iounmap(volatile void __iomem *token)
  275. {
  276. if (ppc_md.iounmap)
  277. ppc_md.iounmap(token);
  278. else
  279. __iounmap(token);
  280. }
  281. EXPORT_SYMBOL(ioremap);
  282. EXPORT_SYMBOL(ioremap_wc);
  283. EXPORT_SYMBOL(ioremap_prot);
  284. EXPORT_SYMBOL(__ioremap);
  285. EXPORT_SYMBOL(__ioremap_at);
  286. EXPORT_SYMBOL(iounmap);
  287. EXPORT_SYMBOL(__iounmap);
  288. EXPORT_SYMBOL(__iounmap_at);