/arch/ppc/mm/pgtable.c

https://bitbucket.org/evzijst/gittest · C · 471 lines · 309 code · 64 blank · 98 comment · 66 complexity · 4d547a5c99059dfa583219ca09cb1c65 MD5 · raw file

  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 int num_tlbcam_entries;
  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_44x
  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. int flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT;
  100. #else
  101. int 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_44x
  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_44x */
  129. void __iomem *
  130. ioremap64(unsigned long long addr, unsigned long size)
  131. {
  132. return __ioremap(addr, size, _PAGE_NO_CACHE);
  133. }
  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_44x */
  141. void __iomem *
  142. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  143. {
  144. unsigned long v, i;
  145. phys_addr_t p;
  146. int err;
  147. /*
  148. * Choose an address to map it to.
  149. * Once the vmalloc system is running, we use it.
  150. * Before then, we use space going down from ioremap_base
  151. * (ioremap_bot records where we're up to).
  152. */
  153. p = addr & PAGE_MASK;
  154. size = PAGE_ALIGN(addr + size) - p;
  155. /*
  156. * If the address lies within the first 16 MB, assume it's in ISA
  157. * memory space
  158. */
  159. if (p < 16*1024*1024)
  160. p += _ISA_MEM_BASE;
  161. /*
  162. * Don't allow anybody to remap normal RAM that we're using.
  163. * mem_init() sets high_memory so only do the check after that.
  164. */
  165. if ( mem_init_done && (p < virt_to_phys(high_memory)) )
  166. {
  167. printk("__ioremap(): phys addr "PTE_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. void iounmap(volatile void __iomem *addr)
  216. {
  217. /*
  218. * If mapped by BATs then there is nothing to do.
  219. * Calling vfree() generates a benign warning.
  220. */
  221. if (v_mapped_by_bats((unsigned long)addr)) return;
  222. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  223. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  224. }
  225. void __iomem *ioport_map(unsigned long port, unsigned int len)
  226. {
  227. return (void __iomem *) (port + _IO_BASE);
  228. }
  229. void ioport_unmap(void __iomem *addr)
  230. {
  231. /* Nothing to do */
  232. }
  233. EXPORT_SYMBOL(ioport_map);
  234. EXPORT_SYMBOL(ioport_unmap);
  235. int
  236. map_page(unsigned long va, phys_addr_t pa, int flags)
  237. {
  238. pmd_t *pd;
  239. pte_t *pg;
  240. int err = -ENOMEM;
  241. spin_lock(&init_mm.page_table_lock);
  242. /* Use upper 10 bits of VA to index the first level map */
  243. pd = pmd_offset(pgd_offset_k(va), va);
  244. /* Use middle 10 bits of VA to index the second-level map */
  245. pg = pte_alloc_kernel(&init_mm, pd, va);
  246. if (pg != 0) {
  247. err = 0;
  248. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, __pgprot(flags)));
  249. if (mem_init_done)
  250. flush_HPTE(0, va, pmd_val(*pd));
  251. }
  252. spin_unlock(&init_mm.page_table_lock);
  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)
  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. /* XXX caller needs to do pte_unmap, yuck */
  336. }
  337. }
  338. }
  339. return(retval);
  340. }
  341. /* Find physical address for this virtual address. Normally used by
  342. * I/O functions, but anyone can call it.
  343. */
  344. unsigned long iopa(unsigned long addr)
  345. {
  346. unsigned long pa;
  347. /* I don't know why this won't work on PMacs or CHRP. It
  348. * appears there is some bug, or there is some implicit
  349. * mapping done not properly represented by BATs or in page
  350. * tables.......I am actively working on resolving this, but
  351. * can't hold up other stuff. -- Dan
  352. */
  353. pte_t *pte;
  354. struct mm_struct *mm;
  355. /* Check the BATs */
  356. pa = v_mapped_by_bats(addr);
  357. if (pa)
  358. return pa;
  359. /* Allow mapping of user addresses (within the thread)
  360. * for DMA if necessary.
  361. */
  362. if (addr < TASK_SIZE)
  363. mm = current->mm;
  364. else
  365. mm = &init_mm;
  366. pa = 0;
  367. if (get_pteptr(mm, addr, &pte)) {
  368. pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
  369. pte_unmap(pte);
  370. }
  371. return(pa);
  372. }
  373. /* This is will find the virtual address for a physical one....
  374. * Swiped from APUS, could be dangerous :-).
  375. * This is only a placeholder until I really find a way to make this
  376. * work. -- Dan
  377. */
  378. unsigned long
  379. mm_ptov (unsigned long paddr)
  380. {
  381. unsigned long ret;
  382. #if 0
  383. if (paddr < 16*1024*1024)
  384. ret = ZTWO_VADDR(paddr);
  385. else {
  386. int i;
  387. for (i = 0; i < kmap_chunk_count;){
  388. unsigned long phys = kmap_chunks[i++];
  389. unsigned long size = kmap_chunks[i++];
  390. unsigned long virt = kmap_chunks[i++];
  391. if (paddr >= phys
  392. && paddr < (phys + size)){
  393. ret = virt + paddr - phys;
  394. goto exit;
  395. }
  396. }
  397. ret = (unsigned long) __va(paddr);
  398. }
  399. exit:
  400. #ifdef DEBUGPV
  401. printk ("PTOV(%lx)=%lx\n", paddr, ret);
  402. #endif
  403. #else
  404. ret = (unsigned long)paddr + KERNELBASE;
  405. #endif
  406. return ret;
  407. }