pgtable.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * This file contains the routines setting up the linux page tables.
  3. * -- paulus
  4. *
  5. * Derived from arch/ppc/mm/init.c:
  6. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  7. *
  8. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  9. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  10. * Copyright (C) 1996 Paul Mackerras
  11. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  12. *
  13. * Derived from "arch/i386/mm/init.c"
  14. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/mm.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/init.h>
  28. #include <linux/highmem.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/io.h>
  32. #include "mmu_decl.h"
  33. unsigned long ioremap_base;
  34. unsigned long ioremap_bot;
  35. int io_bat_index;
  36. #if defined(CONFIG_6xx)
  37. #define HAVE_BATS 1
  38. #endif
  39. extern char etext[], _stext[];
  40. #ifdef CONFIG_SMP
  41. extern void hash_page_sync(void);
  42. #endif
  43. #ifdef HAVE_BATS
  44. extern unsigned long v_mapped_by_bats(unsigned long va);
  45. extern unsigned long p_mapped_by_bats(unsigned long pa);
  46. void setbat(int index, unsigned long virt, unsigned long phys,
  47. unsigned int size, int flags);
  48. #else /* !HAVE_BATS */
  49. #define v_mapped_by_bats(x) (0UL)
  50. #define p_mapped_by_bats(x) (0UL)
  51. #endif /* HAVE_BATS */
  52. #ifdef CONFIG_PTE_64BIT
  53. /* 44x uses an 8kB pgdir because it has 8-byte Linux PTEs. */
  54. #define PGDIR_ORDER 1
  55. #else
  56. #define PGDIR_ORDER 0
  57. #endif
  58. pgd_t *pgd_alloc(struct mm_struct *mm)
  59. {
  60. pgd_t *ret;
  61. ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, PGDIR_ORDER);
  62. return ret;
  63. }
  64. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  65. {
  66. free_pages((unsigned long)pgd, PGDIR_ORDER);
  67. }
  68. __init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  69. {
  70. pte_t *pte;
  71. extern int mem_init_done;
  72. extern void *early_get_page(void);
  73. if (mem_init_done) {
  74. pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  75. } else {
  76. pte = (pte_t *)early_get_page();
  77. if (pte)
  78. clear_page(pte);
  79. }
  80. return pte;
  81. }
  82. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  83. {
  84. struct page *ptepage;
  85. #ifdef CONFIG_HIGHPTE
  86. gfp_t flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT;
  87. #else
  88. gfp_t flags = GFP_KERNEL | __GFP_REPEAT;
  89. #endif
  90. ptepage = alloc_pages(flags, 0);
  91. if (ptepage) {
  92. clear_highpage(ptepage);
  93. pgtable_page_ctor(ptepage);
  94. }
  95. return ptepage;
  96. }
  97. void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
  98. {
  99. #ifdef CONFIG_SMP
  100. hash_page_sync();
  101. #endif
  102. free_page((unsigned long)pte);
  103. }
  104. void pte_free(struct mm_struct *mm, pgtable_t ptepage)
  105. {
  106. #ifdef CONFIG_SMP
  107. hash_page_sync();
  108. #endif
  109. pgtable_page_dtor(ptepage);
  110. __free_page(ptepage);
  111. }
  112. #ifndef CONFIG_PHYS_64BIT
  113. void __iomem *
  114. ioremap(phys_addr_t addr, unsigned long size)
  115. {
  116. return __ioremap(addr, size, _PAGE_NO_CACHE);
  117. }
  118. #else /* CONFIG_PHYS_64BIT */
  119. void __iomem *
  120. ioremap64(unsigned long long addr, unsigned long size)
  121. {
  122. return __ioremap(addr, size, _PAGE_NO_CACHE);
  123. }
  124. void __iomem *
  125. ioremap(phys_addr_t addr, unsigned long size)
  126. {
  127. phys_addr_t addr64 = fixup_bigphys_addr(addr, size);
  128. return ioremap64(addr64, size);
  129. }
  130. #endif /* CONFIG_PHYS_64BIT */
  131. void __iomem *
  132. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  133. {
  134. unsigned long v, i;
  135. phys_addr_t p;
  136. int err;
  137. /*
  138. * Choose an address to map it to.
  139. * Once the vmalloc system is running, we use it.
  140. * Before then, we use space going down from ioremap_base
  141. * (ioremap_bot records where we're up to).
  142. */
  143. p = addr & PAGE_MASK;
  144. size = PAGE_ALIGN(addr + size) - p;
  145. /*
  146. * If the address lies within the first 16 MB, assume it's in ISA
  147. * memory space
  148. */
  149. if (p < 16*1024*1024)
  150. p += _ISA_MEM_BASE;
  151. /*
  152. * Don't allow anybody to remap normal RAM that we're using.
  153. * mem_init() sets high_memory so only do the check after that.
  154. */
  155. if ( mem_init_done && (p < virt_to_phys(high_memory)) )
  156. {
  157. printk("__ioremap(): phys addr "PHYS_FMT" is RAM lr %p\n", p,
  158. __builtin_return_address(0));
  159. return NULL;
  160. }
  161. if (size == 0)
  162. return NULL;
  163. /*
  164. * Is it already mapped? Perhaps overlapped by a previous
  165. * BAT mapping. If the whole area is mapped then we're done,
  166. * otherwise remap it since we want to keep the virt addrs for
  167. * each request contiguous.
  168. *
  169. * We make the assumption here that if the bottom and top
  170. * of the range we want are mapped then it's mapped to the
  171. * same virt address (and this is contiguous).
  172. * -- Cort
  173. */
  174. if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
  175. goto out;
  176. if (mem_init_done) {
  177. struct vm_struct *area;
  178. area = get_vm_area(size, VM_IOREMAP);
  179. if (area == 0)
  180. return NULL;
  181. v = (unsigned long) area->addr;
  182. } else {
  183. v = (ioremap_bot -= size);
  184. }
  185. if ((flags & _PAGE_PRESENT) == 0)
  186. flags |= _PAGE_KERNEL;
  187. if (flags & _PAGE_NO_CACHE)
  188. flags |= _PAGE_GUARDED;
  189. /*
  190. * Should check if it is a candidate for a BAT mapping
  191. */
  192. err = 0;
  193. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  194. err = map_page(v+i, p+i, flags);
  195. if (err) {
  196. if (mem_init_done)
  197. vunmap((void *)v);
  198. return NULL;
  199. }
  200. out:
  201. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  202. }
  203. void iounmap(volatile void __iomem *addr)
  204. {
  205. /*
  206. * If mapped by BATs then there is nothing to do.
  207. * Calling vfree() generates a benign warning.
  208. */
  209. if (v_mapped_by_bats((unsigned long)addr)) return;
  210. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  211. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  212. }
  213. void __iomem *ioport_map(unsigned long port, unsigned int len)
  214. {
  215. return (void __iomem *) (port + _IO_BASE);
  216. }
  217. void ioport_unmap(void __iomem *addr)
  218. {
  219. /* Nothing to do */
  220. }
  221. EXPORT_SYMBOL(ioport_map);
  222. EXPORT_SYMBOL(ioport_unmap);
  223. int
  224. map_page(unsigned long va, phys_addr_t pa, int flags)
  225. {
  226. pmd_t *pd;
  227. pte_t *pg;
  228. int err = -ENOMEM;
  229. /* Use upper 10 bits of VA to index the first level map */
  230. pd = pmd_offset(pgd_offset_k(va), va);
  231. /* Use middle 10 bits of VA to index the second-level map */
  232. pg = pte_alloc_kernel(pd, va);
  233. if (pg != 0) {
  234. err = 0;
  235. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, __pgprot(flags)));
  236. if (mem_init_done)
  237. flush_HPTE(0, va, pmd_val(*pd));
  238. }
  239. return err;
  240. }
  241. /*
  242. * Map in all of physical memory starting at KERNELBASE.
  243. */
  244. void __init mapin_ram(void)
  245. {
  246. unsigned long v, p, s, f;
  247. s = mmu_mapin_ram();
  248. v = KERNELBASE + s;
  249. p = PPC_MEMSTART + s;
  250. for (; s < total_lowmem; s += PAGE_SIZE) {
  251. if ((char *) v >= _stext && (char *) v < etext)
  252. f = _PAGE_RAM_TEXT;
  253. else
  254. f = _PAGE_RAM;
  255. map_page(v, p, f);
  256. v += PAGE_SIZE;
  257. p += PAGE_SIZE;
  258. }
  259. }
  260. /* is x a power of 4? */
  261. #define is_power_of_4(x) is_power_of_2(x) && (ffs(x) & 1)
  262. /*
  263. * Set up a mapping for a block of I/O.
  264. * virt, phys, size must all be page-aligned.
  265. * This should only be called before ioremap is called.
  266. */
  267. void __init io_block_mapping(unsigned long virt, phys_addr_t phys,
  268. unsigned int size, int flags)
  269. {
  270. int i;
  271. if (virt > KERNELBASE && virt < ioremap_bot)
  272. ioremap_bot = ioremap_base = virt;
  273. #ifdef HAVE_BATS
  274. /*
  275. * Use a BAT for this if possible...
  276. */
  277. if (io_bat_index < 2 && is_power_of_2(size)
  278. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  279. setbat(io_bat_index, virt, phys, size, flags);
  280. ++io_bat_index;
  281. return;
  282. }
  283. #endif /* HAVE_BATS */
  284. /* No BATs available, put it in the page tables. */
  285. for (i = 0; i < size; i += PAGE_SIZE)
  286. map_page(virt + i, phys + i, flags);
  287. }
  288. /* Scan the real Linux page tables and return a PTE pointer for
  289. * a virtual address in a context.
  290. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  291. * the PTE pointer is unmodified if PTE is not found.
  292. */
  293. int
  294. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
  295. {
  296. pgd_t *pgd;
  297. pmd_t *pmd;
  298. pte_t *pte;
  299. int retval = 0;
  300. pgd = pgd_offset(mm, addr & PAGE_MASK);
  301. if (pgd) {
  302. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  303. if (pmd_present(*pmd)) {
  304. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  305. if (pte) {
  306. retval = 1;
  307. *ptep = pte;
  308. if (pmdp)
  309. *pmdp = pmd;
  310. /* XXX caller needs to do pte_unmap, yuck */
  311. }
  312. }
  313. }
  314. return(retval);
  315. }
  316. /* Find physical address for this virtual address. Normally used by
  317. * I/O functions, but anyone can call it.
  318. */
  319. unsigned long iopa(unsigned long addr)
  320. {
  321. unsigned long pa;
  322. /* I don't know why this won't work on PMacs or CHRP. It
  323. * appears there is some bug, or there is some implicit
  324. * mapping done not properly represented by BATs or in page
  325. * tables.......I am actively working on resolving this, but
  326. * can't hold up other stuff. -- Dan
  327. */
  328. pte_t *pte;
  329. struct mm_struct *mm;
  330. /* Check the BATs */
  331. pa = v_mapped_by_bats(addr);
  332. if (pa)
  333. return pa;
  334. /* Allow mapping of user addresses (within the thread)
  335. * for DMA if necessary.
  336. */
  337. if (addr < TASK_SIZE)
  338. mm = current->mm;
  339. else
  340. mm = &init_mm;
  341. pa = 0;
  342. if (get_pteptr(mm, addr, &pte, NULL)) {
  343. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  344. pte_unmap(pte);
  345. }
  346. return(pa);
  347. }