pgtable_64.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/config.h>
  25. #include <linux/signal.h>
  26. #include <linux/sched.h>
  27. #include <linux/kernel.h>
  28. #include <linux/errno.h>
  29. #include <linux/string.h>
  30. #include <linux/types.h>
  31. #include <linux/mman.h>
  32. #include <linux/mm.h>
  33. #include <linux/swap.h>
  34. #include <linux/stddef.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/init.h>
  37. #include <linux/delay.h>
  38. #include <linux/bootmem.h>
  39. #include <linux/highmem.h>
  40. #include <linux/idr.h>
  41. #include <linux/nodemask.h>
  42. #include <linux/module.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/page.h>
  45. #include <asm/prom.h>
  46. #include <asm/lmb.h>
  47. #include <asm/rtas.h>
  48. #include <asm/io.h>
  49. #include <asm/mmu_context.h>
  50. #include <asm/pgtable.h>
  51. #include <asm/mmu.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/smp.h>
  54. #include <asm/machdep.h>
  55. #include <asm/tlb.h>
  56. #include <asm/eeh.h>
  57. #include <asm/processor.h>
  58. #include <asm/mmzone.h>
  59. #include <asm/cputable.h>
  60. #include <asm/ppcdebug.h>
  61. #include <asm/sections.h>
  62. #include <asm/system.h>
  63. #include <asm/iommu.h>
  64. #include <asm/abs_addr.h>
  65. #include <asm/vdso.h>
  66. #include <asm/imalloc.h>
  67. #if PGTABLE_RANGE > USER_VSID_RANGE
  68. #warning Limited user VSID range means pagetable space is wasted
  69. #endif
  70. #if (TASK_SIZE_USER64 < PGTABLE_RANGE) && (TASK_SIZE_USER64 < USER_VSID_RANGE)
  71. #warning TASK_SIZE is smaller than it needs to be.
  72. #endif
  73. int mem_init_done;
  74. unsigned long ioremap_bot = IMALLOC_BASE;
  75. static unsigned long phbs_io_bot = PHBS_IO_BASE;
  76. extern pgd_t swapper_pg_dir[];
  77. extern struct task_struct *current_set[NR_CPUS];
  78. unsigned long klimit = (unsigned long)_end;
  79. /* max amount of RAM to use */
  80. unsigned long __max_memory;
  81. /* info on what we think the IO hole is */
  82. unsigned long io_hole_start;
  83. unsigned long io_hole_size;
  84. #ifdef CONFIG_PPC_ISERIES
  85. void __iomem *ioremap(unsigned long addr, unsigned long size)
  86. {
  87. return (void __iomem *)addr;
  88. }
  89. extern void __iomem *__ioremap(unsigned long addr, unsigned long size,
  90. unsigned long flags)
  91. {
  92. return (void __iomem *)addr;
  93. }
  94. void iounmap(volatile void __iomem *addr)
  95. {
  96. return;
  97. }
  98. #else
  99. /*
  100. * map_io_page currently only called by __ioremap
  101. * map_io_page adds an entry to the ioremap page table
  102. * and adds an entry to the HPT, possibly bolting it
  103. */
  104. static int map_io_page(unsigned long ea, unsigned long pa, int flags)
  105. {
  106. pgd_t *pgdp;
  107. pud_t *pudp;
  108. pmd_t *pmdp;
  109. pte_t *ptep;
  110. unsigned long vsid;
  111. if (mem_init_done) {
  112. spin_lock(&init_mm.page_table_lock);
  113. pgdp = pgd_offset_k(ea);
  114. pudp = pud_alloc(&init_mm, pgdp, ea);
  115. if (!pudp)
  116. return -ENOMEM;
  117. pmdp = pmd_alloc(&init_mm, pudp, ea);
  118. if (!pmdp)
  119. return -ENOMEM;
  120. ptep = pte_alloc_kernel(&init_mm, pmdp, ea);
  121. if (!ptep)
  122. return -ENOMEM;
  123. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  124. __pgprot(flags)));
  125. spin_unlock(&init_mm.page_table_lock);
  126. } else {
  127. unsigned long va, vpn, hash, hpteg;
  128. /*
  129. * If the mm subsystem is not fully up, we cannot create a
  130. * linux page table entry for this mapping. Simply bolt an
  131. * entry in the hardware page table.
  132. */
  133. vsid = get_kernel_vsid(ea);
  134. va = (vsid << 28) | (ea & 0xFFFFFFF);
  135. vpn = va >> PAGE_SHIFT;
  136. hash = hpt_hash(vpn, 0);
  137. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  138. /* Panic if a pte grpup is full */
  139. if (ppc_md.hpte_insert(hpteg, va, pa >> PAGE_SHIFT,
  140. HPTE_V_BOLTED,
  141. _PAGE_NO_CACHE|_PAGE_GUARDED|PP_RWXX)
  142. == -1) {
  143. panic("map_io_page: could not insert mapping");
  144. }
  145. }
  146. return 0;
  147. }
  148. static void __iomem * __ioremap_com(unsigned long addr, unsigned long pa,
  149. unsigned long ea, unsigned long size,
  150. unsigned long flags)
  151. {
  152. unsigned long i;
  153. if ((flags & _PAGE_PRESENT) == 0)
  154. flags |= pgprot_val(PAGE_KERNEL);
  155. for (i = 0; i < size; i += PAGE_SIZE)
  156. if (map_io_page(ea+i, pa+i, flags))
  157. return NULL;
  158. return (void __iomem *) (ea + (addr & ~PAGE_MASK));
  159. }
  160. void __iomem *
  161. ioremap(unsigned long addr, unsigned long size)
  162. {
  163. return __ioremap(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  164. }
  165. void __iomem * __ioremap(unsigned long addr, unsigned long size,
  166. unsigned long flags)
  167. {
  168. unsigned long pa, ea;
  169. void __iomem *ret;
  170. /*
  171. * Choose an address to map it to.
  172. * Once the imalloc system is running, we use it.
  173. * Before that, we map using addresses going
  174. * up from ioremap_bot. imalloc will use
  175. * the addresses from ioremap_bot through
  176. * IMALLOC_END
  177. *
  178. */
  179. pa = addr & PAGE_MASK;
  180. size = PAGE_ALIGN(addr + size) - pa;
  181. if (size == 0)
  182. return NULL;
  183. if (mem_init_done) {
  184. struct vm_struct *area;
  185. area = im_get_free_area(size);
  186. if (area == NULL)
  187. return NULL;
  188. ea = (unsigned long)(area->addr);
  189. ret = __ioremap_com(addr, pa, ea, size, flags);
  190. if (!ret)
  191. im_free(area->addr);
  192. } else {
  193. ea = ioremap_bot;
  194. ret = __ioremap_com(addr, pa, ea, size, flags);
  195. if (ret)
  196. ioremap_bot += size;
  197. }
  198. return ret;
  199. }
  200. #define IS_PAGE_ALIGNED(_val) ((_val) == ((_val) & PAGE_MASK))
  201. int __ioremap_explicit(unsigned long pa, unsigned long ea,
  202. unsigned long size, unsigned long flags)
  203. {
  204. struct vm_struct *area;
  205. void __iomem *ret;
  206. /* For now, require page-aligned values for pa, ea, and size */
  207. if (!IS_PAGE_ALIGNED(pa) || !IS_PAGE_ALIGNED(ea) ||
  208. !IS_PAGE_ALIGNED(size)) {
  209. printk(KERN_ERR "unaligned value in %s\n", __FUNCTION__);
  210. return 1;
  211. }
  212. if (!mem_init_done) {
  213. /* Two things to consider in this case:
  214. * 1) No records will be kept (imalloc, etc) that the region
  215. * has been remapped
  216. * 2) It won't be easy to iounmap() the region later (because
  217. * of 1)
  218. */
  219. ;
  220. } else {
  221. area = im_get_area(ea, size,
  222. IM_REGION_UNUSED|IM_REGION_SUBSET|IM_REGION_EXISTS);
  223. if (area == NULL) {
  224. /* Expected when PHB-dlpar is in play */
  225. return 1;
  226. }
  227. if (ea != (unsigned long) area->addr) {
  228. printk(KERN_ERR "unexpected addr return from "
  229. "im_get_area\n");
  230. return 1;
  231. }
  232. }
  233. ret = __ioremap_com(pa, pa, ea, size, flags);
  234. if (ret == NULL) {
  235. printk(KERN_ERR "ioremap_explicit() allocation failure !\n");
  236. return 1;
  237. }
  238. if (ret != (void *) ea) {
  239. printk(KERN_ERR "__ioremap_com() returned unexpected addr\n");
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. /*
  245. * Unmap an IO region and remove it from imalloc'd list.
  246. * Access to IO memory should be serialized by driver.
  247. * This code is modeled after vmalloc code - unmap_vm_area()
  248. *
  249. * XXX what about calls before mem_init_done (ie python_countermeasures())
  250. */
  251. void iounmap(volatile void __iomem *token)
  252. {
  253. void *addr;
  254. if (!mem_init_done)
  255. return;
  256. addr = (void *) ((unsigned long __force) token & PAGE_MASK);
  257. im_free(addr);
  258. }
  259. static int iounmap_subset_regions(unsigned long addr, unsigned long size)
  260. {
  261. struct vm_struct *area;
  262. /* Check whether subsets of this region exist */
  263. area = im_get_area(addr, size, IM_REGION_SUPERSET);
  264. if (area == NULL)
  265. return 1;
  266. while (area) {
  267. iounmap((void __iomem *) area->addr);
  268. area = im_get_area(addr, size,
  269. IM_REGION_SUPERSET);
  270. }
  271. return 0;
  272. }
  273. int iounmap_explicit(volatile void __iomem *start, unsigned long size)
  274. {
  275. struct vm_struct *area;
  276. unsigned long addr;
  277. int rc;
  278. addr = (unsigned long __force) start & PAGE_MASK;
  279. /* Verify that the region either exists or is a subset of an existing
  280. * region. In the latter case, split the parent region to create
  281. * the exact region
  282. */
  283. area = im_get_area(addr, size,
  284. IM_REGION_EXISTS | IM_REGION_SUBSET);
  285. if (area == NULL) {
  286. /* Determine whether subset regions exist. If so, unmap */
  287. rc = iounmap_subset_regions(addr, size);
  288. if (rc) {
  289. printk(KERN_ERR
  290. "%s() cannot unmap nonexistent range 0x%lx\n",
  291. __FUNCTION__, addr);
  292. return 1;
  293. }
  294. } else {
  295. iounmap((void __iomem *) area->addr);
  296. }
  297. /*
  298. * FIXME! This can't be right:
  299. iounmap(area->addr);
  300. * Maybe it should be "iounmap(area);"
  301. */
  302. return 0;
  303. }
  304. #endif
  305. EXPORT_SYMBOL(ioremap);
  306. EXPORT_SYMBOL(__ioremap);
  307. EXPORT_SYMBOL(iounmap);