pgtable.c 11 KB

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