pgtable_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
  36. int io_bat_index;
  37. #if defined(CONFIG_6xx) || defined(CONFIG_POWER3)
  38. #define HAVE_BATS 1
  39. #endif
  40. #if defined(CONFIG_FSL_BOOKE)
  41. #define HAVE_TLBCAM 1
  42. #endif
  43. extern char etext[], _stext[];
  44. #ifdef CONFIG_SMP
  45. extern void hash_page_sync(void);
  46. #endif
  47. #ifdef HAVE_BATS
  48. extern unsigned long v_mapped_by_bats(unsigned long va);
  49. extern unsigned long p_mapped_by_bats(unsigned long pa);
  50. void setbat(int index, unsigned long virt, unsigned long phys,
  51. unsigned int size, int flags);
  52. #else /* !HAVE_BATS */
  53. #define v_mapped_by_bats(x) (0UL)
  54. #define p_mapped_by_bats(x) (0UL)
  55. #endif /* HAVE_BATS */
  56. #ifdef HAVE_TLBCAM
  57. extern unsigned int tlbcam_index;
  58. extern unsigned long v_mapped_by_tlbcam(unsigned long va);
  59. extern unsigned long p_mapped_by_tlbcam(unsigned long pa);
  60. #else /* !HAVE_TLBCAM */
  61. #define v_mapped_by_tlbcam(x) (0UL)
  62. #define p_mapped_by_tlbcam(x) (0UL)
  63. #endif /* HAVE_TLBCAM */
  64. #ifdef CONFIG_PTE_64BIT
  65. /* 44x uses an 8kB pgdir because it has 8-byte Linux PTEs. */
  66. #define PGDIR_ORDER 1
  67. #else
  68. #define PGDIR_ORDER 0
  69. #endif
  70. pgd_t *pgd_alloc(struct mm_struct *mm)
  71. {
  72. pgd_t *ret;
  73. ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, PGDIR_ORDER);
  74. return ret;
  75. }
  76. void pgd_free(pgd_t *pgd)
  77. {
  78. free_pages((unsigned long)pgd, PGDIR_ORDER);
  79. }
  80. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  81. {
  82. pte_t *pte;
  83. extern int mem_init_done;
  84. extern void *early_get_page(void);
  85. if (mem_init_done) {
  86. pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  87. } else {
  88. pte = (pte_t *)early_get_page();
  89. if (pte)
  90. clear_page(pte);
  91. }
  92. return pte;
  93. }
  94. struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
  95. {
  96. struct page *ptepage;
  97. #ifdef CONFIG_HIGHPTE
  98. gfp_t flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT;
  99. #else
  100. gfp_t flags = GFP_KERNEL | __GFP_REPEAT;
  101. #endif
  102. ptepage = alloc_pages(flags, 0);
  103. if (ptepage)
  104. clear_highpage(ptepage);
  105. return ptepage;
  106. }
  107. void pte_free_kernel(pte_t *pte)
  108. {
  109. #ifdef CONFIG_SMP
  110. hash_page_sync();
  111. #endif
  112. free_page((unsigned long)pte);
  113. }
  114. void pte_free(struct page *ptepage)
  115. {
  116. #ifdef CONFIG_SMP
  117. hash_page_sync();
  118. #endif
  119. __free_page(ptepage);
  120. }
  121. void __iomem *
  122. ioremap(phys_addr_t addr, unsigned long size)
  123. {
  124. return __ioremap(addr, size, _PAGE_NO_CACHE);
  125. }
  126. EXPORT_SYMBOL(ioremap);
  127. void __iomem *
  128. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  129. {
  130. unsigned long v, i;
  131. phys_addr_t p;
  132. int err;
  133. /*
  134. * Choose an address to map it to.
  135. * Once the vmalloc system is running, we use it.
  136. * Before then, we use space going down from ioremap_base
  137. * (ioremap_bot records where we're up to).
  138. */
  139. p = addr & PAGE_MASK;
  140. size = PAGE_ALIGN(addr + size) - p;
  141. /*
  142. * If the address lies within the first 16 MB, assume it's in ISA
  143. * memory space
  144. */
  145. if (p < 16*1024*1024)
  146. p += _ISA_MEM_BASE;
  147. /*
  148. * Don't allow anybody to remap normal RAM that we're using.
  149. * mem_init() sets high_memory so only do the check after that.
  150. */
  151. if (mem_init_done && (p < virt_to_phys(high_memory))) {
  152. printk("__ioremap(): phys addr "PHYS_FMT" is RAM lr %p\n", p,
  153. __builtin_return_address(0));
  154. return NULL;
  155. }
  156. if (size == 0)
  157. return NULL;
  158. /*
  159. * Is it already mapped? Perhaps overlapped by a previous
  160. * BAT mapping. If the whole area is mapped then we're done,
  161. * otherwise remap it since we want to keep the virt addrs for
  162. * each request contiguous.
  163. *
  164. * We make the assumption here that if the bottom and top
  165. * of the range we want are mapped then it's mapped to the
  166. * same virt address (and this is contiguous).
  167. * -- Cort
  168. */
  169. if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
  170. goto out;
  171. if ((v = p_mapped_by_tlbcam(p)))
  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. EXPORT_SYMBOL(__ioremap);
  201. void iounmap(volatile void __iomem *addr)
  202. {
  203. /*
  204. * If mapped by BATs then there is nothing to do.
  205. * Calling vfree() generates a benign warning.
  206. */
  207. if (v_mapped_by_bats((unsigned long)addr)) return;
  208. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  209. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  210. }
  211. EXPORT_SYMBOL(iounmap);
  212. void __iomem *ioport_map(unsigned long port, unsigned int len)
  213. {
  214. return (void __iomem *) (port + _IO_BASE);
  215. }
  216. void ioport_unmap(void __iomem *addr)
  217. {
  218. /* Nothing to do */
  219. }
  220. EXPORT_SYMBOL(ioport_map);
  221. EXPORT_SYMBOL(ioport_unmap);
  222. int
  223. map_page(unsigned long va, phys_addr_t pa, int flags)
  224. {
  225. pmd_t *pd;
  226. pte_t *pg;
  227. int err = -ENOMEM;
  228. /* Use upper 10 bits of VA to index the first level map */
  229. pd = pmd_offset(pgd_offset_k(va), va);
  230. /* Use middle 10 bits of VA to index the second-level map */
  231. pg = pte_alloc_kernel(pd, va);
  232. if (pg != 0) {
  233. err = 0;
  234. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, __pgprot(flags)));
  235. if (mem_init_done)
  236. flush_HPTE(0, va, pmd_val(*pd));
  237. }
  238. return err;
  239. }
  240. /*
  241. * Map in all of physical memory starting at KERNELBASE.
  242. */
  243. void __init mapin_ram(void)
  244. {
  245. unsigned long v, p, s, f;
  246. s = mmu_mapin_ram();
  247. v = KERNELBASE + s;
  248. p = PPC_MEMSTART + s;
  249. for (; s < total_lowmem; s += PAGE_SIZE) {
  250. if ((char *) v >= _stext && (char *) v < etext)
  251. f = _PAGE_RAM_TEXT;
  252. else
  253. f = _PAGE_RAM;
  254. map_page(v, p, f);
  255. v += PAGE_SIZE;
  256. p += PAGE_SIZE;
  257. }
  258. }
  259. /* is x a power of 2? */
  260. #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
  261. /* is x a power of 4? */
  262. #define is_power_of_4(x) ((x) != 0 && (((x) & (x-1)) == 0) && (ffs(x) & 1))
  263. /*
  264. * Set up a mapping for a block of I/O.
  265. * virt, phys, size must all be page-aligned.
  266. * This should only be called before ioremap is called.
  267. */
  268. void __init io_block_mapping(unsigned long virt, phys_addr_t phys,
  269. unsigned int size, int flags)
  270. {
  271. int i;
  272. if (virt > KERNELBASE && virt < ioremap_bot)
  273. ioremap_bot = ioremap_base = virt;
  274. #ifdef HAVE_BATS
  275. /*
  276. * Use a BAT for this if possible...
  277. */
  278. if (io_bat_index < 2 && is_power_of_2(size)
  279. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  280. setbat(io_bat_index, virt, phys, size, flags);
  281. ++io_bat_index;
  282. return;
  283. }
  284. #endif /* HAVE_BATS */
  285. #ifdef HAVE_TLBCAM
  286. /*
  287. * Use a CAM for this if possible...
  288. */
  289. if (tlbcam_index < num_tlbcam_entries && is_power_of_4(size)
  290. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  291. settlbcam(tlbcam_index, virt, phys, size, flags, 0);
  292. ++tlbcam_index;
  293. return;
  294. }
  295. #endif /* HAVE_TLBCAM */
  296. /* No BATs available, put it in the page tables. */
  297. for (i = 0; i < size; i += PAGE_SIZE)
  298. map_page(virt + i, phys + i, flags);
  299. }
  300. /* Scan the real Linux page tables and return a PTE pointer for
  301. * a virtual address in a context.
  302. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  303. * the PTE pointer is unmodified if PTE is not found.
  304. */
  305. int
  306. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
  307. {
  308. pgd_t *pgd;
  309. pmd_t *pmd;
  310. pte_t *pte;
  311. int retval = 0;
  312. pgd = pgd_offset(mm, addr & PAGE_MASK);
  313. if (pgd) {
  314. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  315. if (pmd_present(*pmd)) {
  316. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  317. if (pte) {
  318. retval = 1;
  319. *ptep = pte;
  320. if (pmdp)
  321. *pmdp = pmd;
  322. /* XXX caller needs to do pte_unmap, yuck */
  323. }
  324. }
  325. }
  326. return(retval);
  327. }
  328. /* Find physical address for this virtual address. Normally used by
  329. * I/O functions, but anyone can call it.
  330. */
  331. unsigned long iopa(unsigned long addr)
  332. {
  333. unsigned long pa;
  334. /* I don't know why this won't work on PMacs or CHRP. It
  335. * appears there is some bug, or there is some implicit
  336. * mapping done not properly represented by BATs or in page
  337. * tables.......I am actively working on resolving this, but
  338. * can't hold up other stuff. -- Dan
  339. */
  340. pte_t *pte;
  341. struct mm_struct *mm;
  342. /* Check the BATs */
  343. pa = v_mapped_by_bats(addr);
  344. if (pa)
  345. return pa;
  346. /* Allow mapping of user addresses (within the thread)
  347. * for DMA if necessary.
  348. */
  349. if (addr < TASK_SIZE)
  350. mm = current->mm;
  351. else
  352. mm = &init_mm;
  353. pa = 0;
  354. if (get_pteptr(mm, addr, &pte, NULL)) {
  355. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  356. pte_unmap(pte);
  357. }
  358. return(pa);
  359. }
  360. /* This is will find the virtual address for a physical one....
  361. * Swiped from APUS, could be dangerous :-).
  362. * This is only a placeholder until I really find a way to make this
  363. * work. -- Dan
  364. */
  365. unsigned long
  366. mm_ptov (unsigned long paddr)
  367. {
  368. unsigned long ret;
  369. #if 0
  370. if (paddr < 16*1024*1024)
  371. ret = ZTWO_VADDR(paddr);
  372. else {
  373. int i;
  374. for (i = 0; i < kmap_chunk_count;){
  375. unsigned long phys = kmap_chunks[i++];
  376. unsigned long size = kmap_chunks[i++];
  377. unsigned long virt = kmap_chunks[i++];
  378. if (paddr >= phys
  379. && paddr < (phys + size)){
  380. ret = virt + paddr - phys;
  381. goto exit;
  382. }
  383. }
  384. ret = (unsigned long) __va(paddr);
  385. }
  386. exit:
  387. #ifdef DEBUGPV
  388. printk ("PTOV(%lx)=%lx\n", paddr, ret);
  389. #endif
  390. #else
  391. ret = (unsigned long)paddr + KERNELBASE;
  392. #endif
  393. return ret;
  394. }