pgtable.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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(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. struct page *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. return ptepage;
  94. }
  95. void pte_free_kernel(pte_t *pte)
  96. {
  97. #ifdef CONFIG_SMP
  98. hash_page_sync();
  99. #endif
  100. free_page((unsigned long)pte);
  101. }
  102. void pte_free(struct page *ptepage)
  103. {
  104. #ifdef CONFIG_SMP
  105. hash_page_sync();
  106. #endif
  107. __free_page(ptepage);
  108. }
  109. #ifndef CONFIG_PHYS_64BIT
  110. void __iomem *
  111. ioremap(phys_addr_t addr, unsigned long size)
  112. {
  113. return __ioremap(addr, size, _PAGE_NO_CACHE);
  114. }
  115. #else /* CONFIG_PHYS_64BIT */
  116. void __iomem *
  117. ioremap64(unsigned long long addr, unsigned long size)
  118. {
  119. return __ioremap(addr, size, _PAGE_NO_CACHE);
  120. }
  121. void __iomem *
  122. ioremap(phys_addr_t addr, unsigned long size)
  123. {
  124. phys_addr_t addr64 = fixup_bigphys_addr(addr, size);
  125. return ioremap64(addr64, size);
  126. }
  127. #endif /* CONFIG_PHYS_64BIT */
  128. void __iomem *
  129. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  130. {
  131. unsigned long v, i;
  132. phys_addr_t p;
  133. int err;
  134. /*
  135. * Choose an address to map it to.
  136. * Once the vmalloc system is running, we use it.
  137. * Before then, we use space going down from ioremap_base
  138. * (ioremap_bot records where we're up to).
  139. */
  140. p = addr & PAGE_MASK;
  141. size = PAGE_ALIGN(addr + size) - p;
  142. /*
  143. * If the address lies within the first 16 MB, assume it's in ISA
  144. * memory space
  145. */
  146. if (p < 16*1024*1024)
  147. p += _ISA_MEM_BASE;
  148. /*
  149. * Don't allow anybody to remap normal RAM that we're using.
  150. * mem_init() sets high_memory so only do the check after that.
  151. */
  152. if ( mem_init_done && (p < virt_to_phys(high_memory)) )
  153. {
  154. printk("__ioremap(): phys addr "PHYS_FMT" is RAM lr %p\n", p,
  155. __builtin_return_address(0));
  156. return NULL;
  157. }
  158. if (size == 0)
  159. return NULL;
  160. /*
  161. * Is it already mapped? Perhaps overlapped by a previous
  162. * BAT mapping. If the whole area is mapped then we're done,
  163. * otherwise remap it since we want to keep the virt addrs for
  164. * each request contiguous.
  165. *
  166. * We make the assumption here that if the bottom and top
  167. * of the range we want are mapped then it's mapped to the
  168. * same virt address (and this is contiguous).
  169. * -- Cort
  170. */
  171. if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
  172. goto out;
  173. if (mem_init_done) {
  174. struct vm_struct *area;
  175. area = get_vm_area(size, VM_IOREMAP);
  176. if (area == 0)
  177. return NULL;
  178. v = (unsigned long) area->addr;
  179. } else {
  180. v = (ioremap_bot -= size);
  181. }
  182. if ((flags & _PAGE_PRESENT) == 0)
  183. flags |= _PAGE_KERNEL;
  184. if (flags & _PAGE_NO_CACHE)
  185. flags |= _PAGE_GUARDED;
  186. /*
  187. * Should check if it is a candidate for a BAT mapping
  188. */
  189. err = 0;
  190. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  191. err = map_page(v+i, p+i, flags);
  192. if (err) {
  193. if (mem_init_done)
  194. vunmap((void *)v);
  195. return NULL;
  196. }
  197. out:
  198. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  199. }
  200. void iounmap(volatile void __iomem *addr)
  201. {
  202. /*
  203. * If mapped by BATs then there is nothing to do.
  204. * Calling vfree() generates a benign warning.
  205. */
  206. if (v_mapped_by_bats((unsigned long)addr)) return;
  207. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  208. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  209. }
  210. void __iomem *ioport_map(unsigned long port, unsigned int len)
  211. {
  212. return (void __iomem *) (port + _IO_BASE);
  213. }
  214. void ioport_unmap(void __iomem *addr)
  215. {
  216. /* Nothing to do */
  217. }
  218. EXPORT_SYMBOL(ioport_map);
  219. EXPORT_SYMBOL(ioport_unmap);
  220. int
  221. map_page(unsigned long va, phys_addr_t pa, int flags)
  222. {
  223. pmd_t *pd;
  224. pte_t *pg;
  225. int err = -ENOMEM;
  226. /* Use upper 10 bits of VA to index the first level map */
  227. pd = pmd_offset(pgd_offset_k(va), va);
  228. /* Use middle 10 bits of VA to index the second-level map */
  229. pg = pte_alloc_kernel(pd, va);
  230. if (pg != 0) {
  231. err = 0;
  232. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, __pgprot(flags)));
  233. if (mem_init_done)
  234. flush_HPTE(0, va, pmd_val(*pd));
  235. }
  236. return err;
  237. }
  238. /*
  239. * Map in all of physical memory starting at KERNELBASE.
  240. */
  241. void __init mapin_ram(void)
  242. {
  243. unsigned long v, p, s, f;
  244. s = mmu_mapin_ram();
  245. v = KERNELBASE + s;
  246. p = PPC_MEMSTART + s;
  247. for (; s < total_lowmem; s += PAGE_SIZE) {
  248. if ((char *) v >= _stext && (char *) v < etext)
  249. f = _PAGE_RAM_TEXT;
  250. else
  251. f = _PAGE_RAM;
  252. map_page(v, p, f);
  253. v += PAGE_SIZE;
  254. p += PAGE_SIZE;
  255. }
  256. }
  257. /* is x a power of 4? */
  258. #define is_power_of_4(x) is_power_of_2(x) && (ffs(x) & 1)
  259. /*
  260. * Set up a mapping for a block of I/O.
  261. * virt, phys, size must all be page-aligned.
  262. * This should only be called before ioremap is called.
  263. */
  264. void __init io_block_mapping(unsigned long virt, phys_addr_t phys,
  265. unsigned int size, int flags)
  266. {
  267. int i;
  268. if (virt > KERNELBASE && virt < ioremap_bot)
  269. ioremap_bot = ioremap_base = virt;
  270. #ifdef HAVE_BATS
  271. /*
  272. * Use a BAT for this if possible...
  273. */
  274. if (io_bat_index < 2 && is_power_of_2(size)
  275. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  276. setbat(io_bat_index, virt, phys, size, flags);
  277. ++io_bat_index;
  278. return;
  279. }
  280. #endif /* HAVE_BATS */
  281. /* No BATs available, put it in the page tables. */
  282. for (i = 0; i < size; i += PAGE_SIZE)
  283. map_page(virt + i, phys + i, flags);
  284. }
  285. /* Scan the real Linux page tables and return a PTE pointer for
  286. * a virtual address in a context.
  287. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  288. * the PTE pointer is unmodified if PTE is not found.
  289. */
  290. int
  291. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
  292. {
  293. pgd_t *pgd;
  294. pmd_t *pmd;
  295. pte_t *pte;
  296. int retval = 0;
  297. pgd = pgd_offset(mm, addr & PAGE_MASK);
  298. if (pgd) {
  299. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  300. if (pmd_present(*pmd)) {
  301. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  302. if (pte) {
  303. retval = 1;
  304. *ptep = pte;
  305. if (pmdp)
  306. *pmdp = pmd;
  307. /* XXX caller needs to do pte_unmap, yuck */
  308. }
  309. }
  310. }
  311. return(retval);
  312. }
  313. /* Find physical address for this virtual address. Normally used by
  314. * I/O functions, but anyone can call it.
  315. */
  316. unsigned long iopa(unsigned long addr)
  317. {
  318. unsigned long pa;
  319. /* I don't know why this won't work on PMacs or CHRP. It
  320. * appears there is some bug, or there is some implicit
  321. * mapping done not properly represented by BATs or in page
  322. * tables.......I am actively working on resolving this, but
  323. * can't hold up other stuff. -- Dan
  324. */
  325. pte_t *pte;
  326. struct mm_struct *mm;
  327. /* Check the BATs */
  328. pa = v_mapped_by_bats(addr);
  329. if (pa)
  330. return pa;
  331. /* Allow mapping of user addresses (within the thread)
  332. * for DMA if necessary.
  333. */
  334. if (addr < TASK_SIZE)
  335. mm = current->mm;
  336. else
  337. mm = &init_mm;
  338. pa = 0;
  339. if (get_pteptr(mm, addr, &pte, NULL)) {
  340. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  341. pte_unmap(pte);
  342. }
  343. return(pa);
  344. }