pgtable_32.c 11 KB

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