pgtable.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #if defined(CONFIG_FSL_BOOKE)
  40. #define HAVE_TLBCAM 1
  41. #endif
  42. extern char etext[], _stext[];
  43. #ifdef CONFIG_SMP
  44. extern void hash_page_sync(void);
  45. #endif
  46. #ifdef HAVE_BATS
  47. extern unsigned long v_mapped_by_bats(unsigned long va);
  48. extern unsigned long p_mapped_by_bats(unsigned long pa);
  49. void setbat(int index, unsigned long virt, unsigned long phys,
  50. unsigned int size, int flags);
  51. #else /* !HAVE_BATS */
  52. #define v_mapped_by_bats(x) (0UL)
  53. #define p_mapped_by_bats(x) (0UL)
  54. #endif /* HAVE_BATS */
  55. #ifdef HAVE_TLBCAM
  56. extern unsigned int tlbcam_index;
  57. extern unsigned long v_mapped_by_tlbcam(unsigned long va);
  58. extern unsigned long p_mapped_by_tlbcam(unsigned long pa);
  59. #else /* !HAVE_TLBCAM */
  60. #define v_mapped_by_tlbcam(x) (0UL)
  61. #define p_mapped_by_tlbcam(x) (0UL)
  62. #endif /* HAVE_TLBCAM */
  63. #ifdef CONFIG_PTE_64BIT
  64. /* 44x uses an 8kB pgdir because it has 8-byte Linux PTEs. */
  65. #define PGDIR_ORDER 1
  66. #else
  67. #define PGDIR_ORDER 0
  68. #endif
  69. pgd_t *pgd_alloc(struct mm_struct *mm)
  70. {
  71. pgd_t *ret;
  72. ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, PGDIR_ORDER);
  73. return ret;
  74. }
  75. void pgd_free(pgd_t *pgd)
  76. {
  77. free_pages((unsigned long)pgd, PGDIR_ORDER);
  78. }
  79. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  80. {
  81. pte_t *pte;
  82. extern int mem_init_done;
  83. extern void *early_get_page(void);
  84. if (mem_init_done) {
  85. pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  86. } else {
  87. pte = (pte_t *)early_get_page();
  88. if (pte)
  89. clear_page(pte);
  90. }
  91. return pte;
  92. }
  93. struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
  94. {
  95. struct page *ptepage;
  96. #ifdef CONFIG_HIGHPTE
  97. gfp_t flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT;
  98. #else
  99. gfp_t flags = GFP_KERNEL | __GFP_REPEAT;
  100. #endif
  101. ptepage = alloc_pages(flags, 0);
  102. if (ptepage)
  103. clear_highpage(ptepage);
  104. return ptepage;
  105. }
  106. void pte_free_kernel(pte_t *pte)
  107. {
  108. #ifdef CONFIG_SMP
  109. hash_page_sync();
  110. #endif
  111. free_page((unsigned long)pte);
  112. }
  113. void pte_free(struct page *ptepage)
  114. {
  115. #ifdef CONFIG_SMP
  116. hash_page_sync();
  117. #endif
  118. __free_page(ptepage);
  119. }
  120. #ifndef CONFIG_PHYS_64BIT
  121. void __iomem *
  122. ioremap(phys_addr_t addr, unsigned long size)
  123. {
  124. return __ioremap(addr, size, _PAGE_NO_CACHE);
  125. }
  126. #else /* CONFIG_PHYS_64BIT */
  127. void __iomem *
  128. ioremap64(unsigned long long addr, unsigned long size)
  129. {
  130. return __ioremap(addr, size, _PAGE_NO_CACHE);
  131. }
  132. void __iomem *
  133. ioremap(phys_addr_t addr, unsigned long size)
  134. {
  135. phys_addr_t addr64 = fixup_bigphys_addr(addr, size);
  136. return ioremap64(addr64, size);
  137. }
  138. #endif /* CONFIG_PHYS_64BIT */
  139. void __iomem *
  140. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  141. {
  142. unsigned long v, i;
  143. phys_addr_t p;
  144. int err;
  145. /*
  146. * Choose an address to map it to.
  147. * Once the vmalloc system is running, we use it.
  148. * Before then, we use space going down from ioremap_base
  149. * (ioremap_bot records where we're up to).
  150. */
  151. p = addr & PAGE_MASK;
  152. size = PAGE_ALIGN(addr + size) - p;
  153. /*
  154. * If the address lies within the first 16 MB, assume it's in ISA
  155. * memory space
  156. */
  157. if (p < 16*1024*1024)
  158. p += _ISA_MEM_BASE;
  159. /*
  160. * Don't allow anybody to remap normal RAM that we're using.
  161. * mem_init() sets high_memory so only do the check after that.
  162. */
  163. if ( mem_init_done && (p < virt_to_phys(high_memory)) )
  164. {
  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, pmd_t **pmdp)
  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. if (pmdp)
  332. *pmdp = pmd;
  333. /* XXX caller needs to do pte_unmap, yuck */
  334. }
  335. }
  336. }
  337. return(retval);
  338. }
  339. /* Find physical address for this virtual address. Normally used by
  340. * I/O functions, but anyone can call it.
  341. */
  342. unsigned long iopa(unsigned long addr)
  343. {
  344. unsigned long pa;
  345. /* I don't know why this won't work on PMacs or CHRP. It
  346. * appears there is some bug, or there is some implicit
  347. * mapping done not properly represented by BATs or in page
  348. * tables.......I am actively working on resolving this, but
  349. * can't hold up other stuff. -- Dan
  350. */
  351. pte_t *pte;
  352. struct mm_struct *mm;
  353. /* Check the BATs */
  354. pa = v_mapped_by_bats(addr);
  355. if (pa)
  356. return pa;
  357. /* Allow mapping of user addresses (within the thread)
  358. * for DMA if necessary.
  359. */
  360. if (addr < TASK_SIZE)
  361. mm = current->mm;
  362. else
  363. mm = &init_mm;
  364. pa = 0;
  365. if (get_pteptr(mm, addr, &pte, NULL)) {
  366. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  367. pte_unmap(pte);
  368. }
  369. return(pa);
  370. }
  371. /* This is will find the virtual address for a physical one....
  372. * Swiped from APUS, could be dangerous :-).
  373. * This is only a placeholder until I really find a way to make this
  374. * work. -- Dan
  375. */
  376. unsigned long
  377. mm_ptov (unsigned long paddr)
  378. {
  379. unsigned long ret;
  380. #if 0
  381. if (paddr < 16*1024*1024)
  382. ret = ZTWO_VADDR(paddr);
  383. else {
  384. int i;
  385. for (i = 0; i < kmap_chunk_count;){
  386. unsigned long phys = kmap_chunks[i++];
  387. unsigned long size = kmap_chunks[i++];
  388. unsigned long virt = kmap_chunks[i++];
  389. if (paddr >= phys
  390. && paddr < (phys + size)){
  391. ret = virt + paddr - phys;
  392. goto exit;
  393. }
  394. }
  395. ret = (unsigned long) __va(paddr);
  396. }
  397. exit:
  398. #ifdef DEBUGPV
  399. printk ("PTOV(%lx)=%lx\n", paddr, ret);
  400. #endif
  401. #else
  402. ret = (unsigned long)paddr + KERNELBASE;
  403. #endif
  404. return ret;
  405. }