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/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. int flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT;
  99. #else
  100. int 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. spin_lock(&init_mm.page_table_lock);
  240. /* Use upper 10 bits of VA to index the first level map */
  241. pd = pmd_offset(pgd_offset_k(va), va);
  242. /* Use middle 10 bits of VA to index the second-level map */
  243. pg = pte_alloc_kernel(&init_mm, pd, va);
  244. if (pg != 0) {
  245. err = 0;
  246. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, __pgprot(flags)));
  247. if (mem_init_done)
  248. flush_HPTE(0, va, pmd_val(*pd));
  249. }
  250. spin_unlock(&init_mm.page_table_lock);
  251. return err;
  252. }
  253. /*
  254. * Map in all of physical memory starting at KERNELBASE.
  255. */
  256. void __init mapin_ram(void)
  257. {
  258. unsigned long v, p, s, f;
  259. s = mmu_mapin_ram();
  260. v = KERNELBASE + s;
  261. p = PPC_MEMSTART + s;
  262. for (; s < total_lowmem; s += PAGE_SIZE) {
  263. if ((char *) v >= _stext && (char *) v < etext)
  264. f = _PAGE_RAM_TEXT;
  265. else
  266. f = _PAGE_RAM;
  267. map_page(v, p, f);
  268. v += PAGE_SIZE;
  269. p += PAGE_SIZE;
  270. }
  271. }
  272. /* is x a power of 2? */
  273. #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
  274. /* is x a power of 4? */
  275. #define is_power_of_4(x) ((x) != 0 && (((x) & (x-1)) == 0) && (ffs(x) & 1))
  276. /*
  277. * Set up a mapping for a block of I/O.
  278. * virt, phys, size must all be page-aligned.
  279. * This should only be called before ioremap is called.
  280. */
  281. void __init io_block_mapping(unsigned long virt, phys_addr_t phys,
  282. unsigned int size, int flags)
  283. {
  284. int i;
  285. if (virt > KERNELBASE && virt < ioremap_bot)
  286. ioremap_bot = ioremap_base = virt;
  287. #ifdef HAVE_BATS
  288. /*
  289. * Use a BAT for this if possible...
  290. */
  291. if (io_bat_index < 2 && is_power_of_2(size)
  292. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  293. setbat(io_bat_index, virt, phys, size, flags);
  294. ++io_bat_index;
  295. return;
  296. }
  297. #endif /* HAVE_BATS */
  298. #ifdef HAVE_TLBCAM
  299. /*
  300. * Use a CAM for this if possible...
  301. */
  302. if (tlbcam_index < num_tlbcam_entries && is_power_of_4(size)
  303. && (virt & (size - 1)) == 0 && (phys & (size - 1)) == 0) {
  304. settlbcam(tlbcam_index, virt, phys, size, flags, 0);
  305. ++tlbcam_index;
  306. return;
  307. }
  308. #endif /* HAVE_TLBCAM */
  309. /* No BATs available, put it in the page tables. */
  310. for (i = 0; i < size; i += PAGE_SIZE)
  311. map_page(virt + i, phys + i, flags);
  312. }
  313. /* Scan the real Linux page tables and return a PTE pointer for
  314. * a virtual address in a context.
  315. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  316. * the PTE pointer is unmodified if PTE is not found.
  317. */
  318. int
  319. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
  320. {
  321. pgd_t *pgd;
  322. pmd_t *pmd;
  323. pte_t *pte;
  324. int retval = 0;
  325. pgd = pgd_offset(mm, addr & PAGE_MASK);
  326. if (pgd) {
  327. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  328. if (pmd_present(*pmd)) {
  329. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  330. if (pte) {
  331. retval = 1;
  332. *ptep = pte;
  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)) {
  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. }