pgtable_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/config.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/mm.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/init.h>
  29. #include <linux/highmem.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/io.h>
  33. #include "mmu_decl.h"
  34. unsigned long ioremap_base;
  35. unsigned long ioremap_bot;
  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. #ifndef CONFIG_PHYS_64BIT
  122. void __iomem *
  123. ioremap(phys_addr_t addr, unsigned long size)
  124. {
  125. return __ioremap(addr, size, _PAGE_NO_CACHE);
  126. }
  127. #else /* CONFIG_PHYS_64BIT */
  128. void __iomem *
  129. ioremap64(unsigned long long addr, unsigned long size)
  130. {
  131. return __ioremap(addr, size, _PAGE_NO_CACHE);
  132. }
  133. void __iomem *
  134. ioremap(phys_addr_t addr, unsigned long size)
  135. {
  136. phys_addr_t addr64 = fixup_bigphys_addr(addr, size);
  137. return ioremap64(addr64, size);
  138. }
  139. #endif /* CONFIG_PHYS_64BIT */
  140. void __iomem *
  141. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  142. {
  143. unsigned long v, i;
  144. phys_addr_t p;
  145. int err;
  146. /*
  147. * Choose an address to map it to.
  148. * Once the vmalloc system is running, we use it.
  149. * Before then, we use space going down from ioremap_base
  150. * (ioremap_bot records where we're up to).
  151. */
  152. p = addr & PAGE_MASK;
  153. size = PAGE_ALIGN(addr + size) - p;
  154. /*
  155. * If the address lies within the first 16 MB, assume it's in ISA
  156. * memory space
  157. */
  158. if (p < 16*1024*1024)
  159. p += _ISA_MEM_BASE;
  160. /*
  161. * Don't allow anybody to remap normal RAM that we're using.
  162. * mem_init() sets high_memory so only do the check after that.
  163. */
  164. if (mem_init_done && (p < virt_to_phys(high_memory))) {
  165. printk("__ioremap(): phys addr "PHYS_FMT" is RAM lr %p\n", p,
  166. __builtin_return_address(0));
  167. return NULL;
  168. }
  169. if (size == 0)
  170. return NULL;
  171. /*
  172. * Is it already mapped? Perhaps overlapped by a previous
  173. * BAT mapping. If the whole area is mapped then we're done,
  174. * otherwise remap it since we want to keep the virt addrs for
  175. * each request contiguous.
  176. *
  177. * We make the assumption here that if the bottom and top
  178. * of the range we want are mapped then it's mapped to the
  179. * same virt address (and this is contiguous).
  180. * -- Cort
  181. */
  182. if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
  183. goto out;
  184. if ((v = p_mapped_by_tlbcam(p)))
  185. goto out;
  186. if (mem_init_done) {
  187. struct vm_struct *area;
  188. area = get_vm_area(size, VM_IOREMAP);
  189. if (area == 0)
  190. return NULL;
  191. v = (unsigned long) area->addr;
  192. } else {
  193. v = (ioremap_bot -= size);
  194. }
  195. if ((flags & _PAGE_PRESENT) == 0)
  196. flags |= _PAGE_KERNEL;
  197. if (flags & _PAGE_NO_CACHE)
  198. flags |= _PAGE_GUARDED;
  199. /*
  200. * Should check if it is a candidate for a BAT mapping
  201. */
  202. err = 0;
  203. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  204. err = map_page(v+i, p+i, flags);
  205. if (err) {
  206. if (mem_init_done)
  207. vunmap((void *)v);
  208. return NULL;
  209. }
  210. out:
  211. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  212. }
  213. void iounmap(volatile void __iomem *addr)
  214. {
  215. /*
  216. * If mapped by BATs then there is nothing to do.
  217. * Calling vfree() generates a benign warning.
  218. */
  219. if (v_mapped_by_bats((unsigned long)addr)) return;
  220. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  221. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  222. }
  223. void __iomem *ioport_map(unsigned long port, unsigned int len)
  224. {
  225. return (void __iomem *) (port + _IO_BASE);
  226. }
  227. void ioport_unmap(void __iomem *addr)
  228. {
  229. /* Nothing to do */
  230. }
  231. EXPORT_SYMBOL(ioport_map);
  232. EXPORT_SYMBOL(ioport_unmap);
  233. int
  234. map_page(unsigned long va, phys_addr_t pa, int flags)
  235. {
  236. pmd_t *pd;
  237. pte_t *pg;
  238. int err = -ENOMEM;
  239. /* Use upper 10 bits of VA to index the first level map */
  240. pd = pmd_offset(pgd_offset_k(va), va);
  241. /* Use middle 10 bits of VA to index the second-level map */
  242. pg = pte_alloc_kernel(pd, va);
  243. if (pg != 0) {
  244. err = 0;
  245. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, __pgprot(flags)));
  246. if (mem_init_done)
  247. flush_HPTE(0, va, pmd_val(*pd));
  248. }
  249. return err;
  250. }
  251. /*
  252. * Map in all of physical memory starting at KERNELBASE.
  253. */
  254. void __init mapin_ram(void)
  255. {
  256. unsigned long v, p, s, f;
  257. s = mmu_mapin_ram();
  258. v = KERNELBASE + s;
  259. p = PPC_MEMSTART + s;
  260. for (; s < total_lowmem; s += PAGE_SIZE) {
  261. if ((char *) v >= _stext && (char *) v < etext)
  262. f = _PAGE_RAM_TEXT;
  263. else
  264. f = _PAGE_RAM;
  265. map_page(v, p, f);
  266. v += PAGE_SIZE;
  267. p += PAGE_SIZE;
  268. }
  269. }
  270. /* is x a power of 2? */
  271. #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
  272. /* is x a power of 4? */
  273. #define is_power_of_4(x) ((x) != 0 && (((x) & (x-1)) == 0) && (ffs(x) & 1))
  274. /*
  275. * Set up a mapping for a block of I/O.
  276. * virt, phys, size must all be page-aligned.
  277. * This should only be called before ioremap is called.
  278. */
  279. void __init io_block_mapping(unsigned long virt, phys_addr_t phys,
  280. unsigned int size, int flags)
  281. {
  282. int i;
  283. if (virt > KERNELBASE && virt < ioremap_bot)
  284. ioremap_bot = ioremap_base = virt;
  285. #ifdef HAVE_BATS
  286. /*
  287. * Use a BAT for this if possible...
  288. */
  289. if (io_bat_index < 2 && is_power_of_2(size)
  290. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  291. setbat(io_bat_index, virt, phys, size, flags);
  292. ++io_bat_index;
  293. return;
  294. }
  295. #endif /* HAVE_BATS */
  296. #ifdef HAVE_TLBCAM
  297. /*
  298. * Use a CAM for this if possible...
  299. */
  300. if (tlbcam_index < num_tlbcam_entries && is_power_of_4(size)
  301. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  302. settlbcam(tlbcam_index, virt, phys, size, flags, 0);
  303. ++tlbcam_index;
  304. return;
  305. }
  306. #endif /* HAVE_TLBCAM */
  307. /* No BATs available, put it in the page tables. */
  308. for (i = 0; i < size; i += PAGE_SIZE)
  309. map_page(virt + i, phys + i, flags);
  310. }
  311. /* Scan the real Linux page tables and return a PTE pointer for
  312. * a virtual address in a context.
  313. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  314. * the PTE pointer is unmodified if PTE is not found.
  315. */
  316. int
  317. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
  318. {
  319. pgd_t *pgd;
  320. pmd_t *pmd;
  321. pte_t *pte;
  322. int retval = 0;
  323. pgd = pgd_offset(mm, addr & PAGE_MASK);
  324. if (pgd) {
  325. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  326. if (pmd_present(*pmd)) {
  327. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  328. if (pte) {
  329. retval = 1;
  330. *ptep = pte;
  331. /* XXX caller needs to do pte_unmap, yuck */
  332. }
  333. }
  334. }
  335. return(retval);
  336. }
  337. /* Find physical address for this virtual address. Normally used by
  338. * I/O functions, but anyone can call it.
  339. */
  340. unsigned long iopa(unsigned long addr)
  341. {
  342. unsigned long pa;
  343. /* I don't know why this won't work on PMacs or CHRP. It
  344. * appears there is some bug, or there is some implicit
  345. * mapping done not properly represented by BATs or in page
  346. * tables.......I am actively working on resolving this, but
  347. * can't hold up other stuff. -- Dan
  348. */
  349. pte_t *pte;
  350. struct mm_struct *mm;
  351. /* Check the BATs */
  352. pa = v_mapped_by_bats(addr);
  353. if (pa)
  354. return pa;
  355. /* Allow mapping of user addresses (within the thread)
  356. * for DMA if necessary.
  357. */
  358. if (addr < TASK_SIZE)
  359. mm = current->mm;
  360. else
  361. mm = &init_mm;
  362. pa = 0;
  363. if (get_pteptr(mm, addr, &pte)) {
  364. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  365. pte_unmap(pte);
  366. }
  367. return(pa);
  368. }
  369. /* This is will find the virtual address for a physical one....
  370. * Swiped from APUS, could be dangerous :-).
  371. * This is only a placeholder until I really find a way to make this
  372. * work. -- Dan
  373. */
  374. unsigned long
  375. mm_ptov (unsigned long paddr)
  376. {
  377. unsigned long ret;
  378. #if 0
  379. if (paddr < 16*1024*1024)
  380. ret = ZTWO_VADDR(paddr);
  381. else {
  382. int i;
  383. for (i = 0; i < kmap_chunk_count;){
  384. unsigned long phys = kmap_chunks[i++];
  385. unsigned long size = kmap_chunks[i++];
  386. unsigned long virt = kmap_chunks[i++];
  387. if (paddr >= phys
  388. && paddr < (phys + size)){
  389. ret = virt + paddr - phys;
  390. goto exit;
  391. }
  392. }
  393. ret = (unsigned long) __va(paddr);
  394. }
  395. exit:
  396. #ifdef DEBUGPV
  397. printk ("PTOV(%lx)=%lx\n", paddr, ret);
  398. #endif
  399. #else
  400. ret = (unsigned long)paddr + KERNELBASE;
  401. #endif
  402. return ret;
  403. }