pgtable_32.c 11 KB

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