pgtable_64.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/slab.h>
  36. #include <linux/hugetlb.h>
  37. #include <asm/pgalloc.h>
  38. #include <asm/page.h>
  39. #include <asm/prom.h>
  40. #include <asm/io.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/mmu.h>
  44. #include <asm/smp.h>
  45. #include <asm/machdep.h>
  46. #include <asm/tlb.h>
  47. #include <asm/processor.h>
  48. #include <asm/cputable.h>
  49. #include <asm/sections.h>
  50. #include <asm/firmware.h>
  51. #include <asm/dma.h>
  52. #include "mmu_decl.h"
  53. #ifdef CONFIG_PPC_BOOK3S_64
  54. /*
  55. * partition table and process table for ISA 3.0
  56. */
  57. struct prtb_entry *process_tb;
  58. struct patb_entry *partition_tb;
  59. /*
  60. * page table size
  61. */
  62. unsigned long __pte_index_size;
  63. EXPORT_SYMBOL(__pte_index_size);
  64. unsigned long __pmd_index_size;
  65. EXPORT_SYMBOL(__pmd_index_size);
  66. unsigned long __pud_index_size;
  67. EXPORT_SYMBOL(__pud_index_size);
  68. unsigned long __pgd_index_size;
  69. EXPORT_SYMBOL(__pgd_index_size);
  70. unsigned long __pud_cache_index;
  71. EXPORT_SYMBOL(__pud_cache_index);
  72. unsigned long __pte_table_size;
  73. EXPORT_SYMBOL(__pte_table_size);
  74. unsigned long __pmd_table_size;
  75. EXPORT_SYMBOL(__pmd_table_size);
  76. unsigned long __pud_table_size;
  77. EXPORT_SYMBOL(__pud_table_size);
  78. unsigned long __pgd_table_size;
  79. EXPORT_SYMBOL(__pgd_table_size);
  80. unsigned long __pmd_val_bits;
  81. EXPORT_SYMBOL(__pmd_val_bits);
  82. unsigned long __pud_val_bits;
  83. EXPORT_SYMBOL(__pud_val_bits);
  84. unsigned long __pgd_val_bits;
  85. EXPORT_SYMBOL(__pgd_val_bits);
  86. unsigned long __kernel_virt_start;
  87. EXPORT_SYMBOL(__kernel_virt_start);
  88. unsigned long __kernel_virt_size;
  89. EXPORT_SYMBOL(__kernel_virt_size);
  90. unsigned long __vmalloc_start;
  91. EXPORT_SYMBOL(__vmalloc_start);
  92. unsigned long __vmalloc_end;
  93. EXPORT_SYMBOL(__vmalloc_end);
  94. unsigned long __kernel_io_start;
  95. EXPORT_SYMBOL(__kernel_io_start);
  96. struct page *vmemmap;
  97. EXPORT_SYMBOL(vmemmap);
  98. unsigned long __pte_frag_nr;
  99. EXPORT_SYMBOL(__pte_frag_nr);
  100. unsigned long __pte_frag_size_shift;
  101. EXPORT_SYMBOL(__pte_frag_size_shift);
  102. unsigned long ioremap_bot;
  103. #else /* !CONFIG_PPC_BOOK3S_64 */
  104. unsigned long ioremap_bot = IOREMAP_BASE;
  105. #endif
  106. /**
  107. * __ioremap_at - Low level function to establish the page tables
  108. * for an IO mapping
  109. */
  110. void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size,
  111. unsigned long flags)
  112. {
  113. unsigned long i;
  114. /* Make sure we have the base flags */
  115. if ((flags & _PAGE_PRESENT) == 0)
  116. flags |= pgprot_val(PAGE_KERNEL);
  117. /* We don't support the 4K PFN hack with ioremap */
  118. if (flags & H_PAGE_4K_PFN)
  119. return NULL;
  120. WARN_ON(pa & ~PAGE_MASK);
  121. WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
  122. WARN_ON(size & ~PAGE_MASK);
  123. for (i = 0; i < size; i += PAGE_SIZE)
  124. if (map_kernel_page((unsigned long)ea+i, pa+i, flags))
  125. return NULL;
  126. return (void __iomem *)ea;
  127. }
  128. /**
  129. * __iounmap_from - Low level function to tear down the page tables
  130. * for an IO mapping. This is used for mappings that
  131. * are manipulated manually, like partial unmapping of
  132. * PCI IOs or ISA space.
  133. */
  134. void __iounmap_at(void *ea, unsigned long size)
  135. {
  136. WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
  137. WARN_ON(size & ~PAGE_MASK);
  138. unmap_kernel_range((unsigned long)ea, size);
  139. }
  140. void __iomem * __ioremap_caller(phys_addr_t addr, unsigned long size,
  141. unsigned long flags, void *caller)
  142. {
  143. phys_addr_t paligned;
  144. void __iomem *ret;
  145. /*
  146. * Choose an address to map it to.
  147. * Once the imalloc system is running, we use it.
  148. * Before that, we map using addresses going
  149. * up from ioremap_bot. imalloc will use
  150. * the addresses from ioremap_bot through
  151. * IMALLOC_END
  152. *
  153. */
  154. paligned = addr & PAGE_MASK;
  155. size = PAGE_ALIGN(addr + size) - paligned;
  156. if ((size == 0) || (paligned == 0))
  157. return NULL;
  158. if (slab_is_available()) {
  159. struct vm_struct *area;
  160. area = __get_vm_area_caller(size, VM_IOREMAP,
  161. ioremap_bot, IOREMAP_END,
  162. caller);
  163. if (area == NULL)
  164. return NULL;
  165. area->phys_addr = paligned;
  166. ret = __ioremap_at(paligned, area->addr, size, flags);
  167. if (!ret)
  168. vunmap(area->addr);
  169. } else {
  170. ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags);
  171. if (ret)
  172. ioremap_bot += size;
  173. }
  174. if (ret)
  175. ret += addr & ~PAGE_MASK;
  176. return ret;
  177. }
  178. void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
  179. unsigned long flags)
  180. {
  181. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  182. }
  183. void __iomem * ioremap(phys_addr_t addr, unsigned long size)
  184. {
  185. unsigned long flags = pgprot_val(pgprot_noncached(__pgprot(0)));
  186. void *caller = __builtin_return_address(0);
  187. if (ppc_md.ioremap)
  188. return ppc_md.ioremap(addr, size, flags, caller);
  189. return __ioremap_caller(addr, size, flags, caller);
  190. }
  191. void __iomem * ioremap_wc(phys_addr_t addr, unsigned long size)
  192. {
  193. unsigned long flags = pgprot_val(pgprot_noncached_wc(__pgprot(0)));
  194. void *caller = __builtin_return_address(0);
  195. if (ppc_md.ioremap)
  196. return ppc_md.ioremap(addr, size, flags, caller);
  197. return __ioremap_caller(addr, size, flags, caller);
  198. }
  199. void __iomem * ioremap_prot(phys_addr_t addr, unsigned long size,
  200. unsigned long flags)
  201. {
  202. void *caller = __builtin_return_address(0);
  203. /* writeable implies dirty for kernel addresses */
  204. if (flags & _PAGE_WRITE)
  205. flags |= _PAGE_DIRTY;
  206. /* we don't want to let _PAGE_EXEC leak out */
  207. flags &= ~_PAGE_EXEC;
  208. /*
  209. * Force kernel mapping.
  210. */
  211. flags &= ~_PAGE_USER;
  212. flags |= _PAGE_PRIVILEGED;
  213. if (ppc_md.ioremap)
  214. return ppc_md.ioremap(addr, size, flags, caller);
  215. return __ioremap_caller(addr, size, flags, caller);
  216. }
  217. /*
  218. * Unmap an IO region and remove it from imalloc'd list.
  219. * Access to IO memory should be serialized by driver.
  220. */
  221. void __iounmap(volatile void __iomem *token)
  222. {
  223. void *addr;
  224. if (!slab_is_available())
  225. return;
  226. addr = (void *) ((unsigned long __force)
  227. PCI_FIX_ADDR(token) & PAGE_MASK);
  228. if ((unsigned long)addr < ioremap_bot) {
  229. printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
  230. " at 0x%p\n", addr);
  231. return;
  232. }
  233. vunmap(addr);
  234. }
  235. void iounmap(volatile void __iomem *token)
  236. {
  237. if (ppc_md.iounmap)
  238. ppc_md.iounmap(token);
  239. else
  240. __iounmap(token);
  241. }
  242. EXPORT_SYMBOL(ioremap);
  243. EXPORT_SYMBOL(ioremap_wc);
  244. EXPORT_SYMBOL(ioremap_prot);
  245. EXPORT_SYMBOL(__ioremap);
  246. EXPORT_SYMBOL(__ioremap_at);
  247. EXPORT_SYMBOL(iounmap);
  248. EXPORT_SYMBOL(__iounmap);
  249. EXPORT_SYMBOL(__iounmap_at);
  250. #ifndef __PAGETABLE_PUD_FOLDED
  251. /* 4 level page table */
  252. struct page *pgd_page(pgd_t pgd)
  253. {
  254. if (pgd_huge(pgd))
  255. return pte_page(pgd_pte(pgd));
  256. return virt_to_page(pgd_page_vaddr(pgd));
  257. }
  258. #endif
  259. struct page *pud_page(pud_t pud)
  260. {
  261. if (pud_huge(pud))
  262. return pte_page(pud_pte(pud));
  263. return virt_to_page(pud_page_vaddr(pud));
  264. }
  265. /*
  266. * For hugepage we have pfn in the pmd, we use PTE_RPN_SHIFT bits for flags
  267. * For PTE page, we have a PTE_FRAG_SIZE (4K) aligned virtual address.
  268. */
  269. struct page *pmd_page(pmd_t pmd)
  270. {
  271. if (pmd_trans_huge(pmd) || pmd_huge(pmd) || pmd_devmap(pmd))
  272. return pte_page(pmd_pte(pmd));
  273. return virt_to_page(pmd_page_vaddr(pmd));
  274. }
  275. #ifdef CONFIG_STRICT_KERNEL_RWX
  276. void mark_rodata_ro(void)
  277. {
  278. if (!mmu_has_feature(MMU_FTR_KERNEL_RO)) {
  279. pr_warn("Warning: Unable to mark rodata read only on this CPU.\n");
  280. return;
  281. }
  282. if (radix_enabled())
  283. radix__mark_rodata_ro();
  284. else
  285. hash__mark_rodata_ro();
  286. }
  287. void mark_initmem_nx(void)
  288. {
  289. if (radix_enabled())
  290. radix__mark_initmem_nx();
  291. else
  292. hash__mark_initmem_nx();
  293. }
  294. #endif