pgtable_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. *
  12. * Derived from "arch/i386/mm/init.c"
  13. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/mm.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/init.h>
  27. #include <linux/highmem.h>
  28. #include <linux/memblock.h>
  29. #include <linux/slab.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/fixmap.h>
  33. #include <asm/io.h>
  34. #include <asm/setup.h>
  35. #include "mmu_decl.h"
  36. unsigned long ioremap_base;
  37. unsigned long ioremap_bot;
  38. EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
  39. #ifdef CONFIG_6xx
  40. #define HAVE_BATS 1
  41. #endif
  42. #if defined(CONFIG_FSL_BOOKE)
  43. #define HAVE_TLBCAM 1
  44. #endif
  45. extern char etext[], _stext[];
  46. #ifdef HAVE_BATS
  47. extern phys_addr_t v_mapped_by_bats(unsigned long va);
  48. extern unsigned long p_mapped_by_bats(phys_addr_t pa);
  49. #else /* !HAVE_BATS */
  50. #define v_mapped_by_bats(x) (0UL)
  51. #define p_mapped_by_bats(x) (0UL)
  52. #endif /* HAVE_BATS */
  53. #ifdef HAVE_TLBCAM
  54. extern phys_addr_t v_mapped_by_tlbcam(unsigned long va);
  55. extern unsigned long p_mapped_by_tlbcam(phys_addr_t pa);
  56. #else /* !HAVE_TLBCAM */
  57. #define v_mapped_by_tlbcam(x) (0UL)
  58. #define p_mapped_by_tlbcam(x) (0UL)
  59. #endif /* HAVE_TLBCAM */
  60. #define PGDIR_ORDER (32 + PGD_T_LOG2 - PGDIR_SHIFT)
  61. #ifndef CONFIG_PPC_4K_PAGES
  62. static struct kmem_cache *pgtable_cache;
  63. void pgtable_cache_init(void)
  64. {
  65. pgtable_cache = kmem_cache_create("PGDIR cache", 1 << PGDIR_ORDER,
  66. 1 << PGDIR_ORDER, 0, NULL);
  67. if (pgtable_cache == NULL)
  68. panic("Couldn't allocate pgtable caches");
  69. }
  70. #endif
  71. pgd_t *pgd_alloc(struct mm_struct *mm)
  72. {
  73. pgd_t *ret;
  74. /* pgdir take page or two with 4K pages and a page fraction otherwise */
  75. #ifndef CONFIG_PPC_4K_PAGES
  76. ret = kmem_cache_alloc(pgtable_cache, GFP_KERNEL | __GFP_ZERO);
  77. #else
  78. ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
  79. PGDIR_ORDER - PAGE_SHIFT);
  80. #endif
  81. return ret;
  82. }
  83. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  84. {
  85. #ifndef CONFIG_PPC_4K_PAGES
  86. kmem_cache_free(pgtable_cache, (void *)pgd);
  87. #else
  88. free_pages((unsigned long)pgd, PGDIR_ORDER - PAGE_SHIFT);
  89. #endif
  90. }
  91. __init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  92. {
  93. pte_t *pte;
  94. if (slab_is_available()) {
  95. pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  96. } else {
  97. pte = __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  98. if (pte)
  99. clear_page(pte);
  100. }
  101. return pte;
  102. }
  103. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  104. {
  105. struct page *ptepage;
  106. gfp_t flags = GFP_KERNEL | __GFP_REPEAT | __GFP_ZERO;
  107. ptepage = alloc_pages(flags, 0);
  108. if (!ptepage)
  109. return NULL;
  110. if (!pgtable_page_ctor(ptepage)) {
  111. __free_page(ptepage);
  112. return NULL;
  113. }
  114. return ptepage;
  115. }
  116. void __iomem *
  117. ioremap(phys_addr_t addr, unsigned long size)
  118. {
  119. return __ioremap_caller(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED,
  120. __builtin_return_address(0));
  121. }
  122. EXPORT_SYMBOL(ioremap);
  123. void __iomem *
  124. ioremap_wc(phys_addr_t addr, unsigned long size)
  125. {
  126. return __ioremap_caller(addr, size, _PAGE_NO_CACHE,
  127. __builtin_return_address(0));
  128. }
  129. EXPORT_SYMBOL(ioremap_wc);
  130. void __iomem *
  131. ioremap_prot(phys_addr_t addr, unsigned long size, unsigned long flags)
  132. {
  133. /* writeable implies dirty for kernel addresses */
  134. if ((flags & (_PAGE_RW | _PAGE_RO)) != _PAGE_RO)
  135. flags |= _PAGE_DIRTY | _PAGE_HWWRITE;
  136. /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
  137. flags &= ~(_PAGE_USER | _PAGE_EXEC);
  138. #ifdef _PAGE_BAP_SR
  139. /* _PAGE_USER contains _PAGE_BAP_SR on BookE using the new PTE format
  140. * which means that we just cleared supervisor access... oops ;-) This
  141. * restores it
  142. */
  143. flags |= _PAGE_BAP_SR;
  144. #endif
  145. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  146. }
  147. EXPORT_SYMBOL(ioremap_prot);
  148. void __iomem *
  149. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  150. {
  151. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  152. }
  153. void __iomem *
  154. __ioremap_caller(phys_addr_t addr, unsigned long size, unsigned long flags,
  155. void *caller)
  156. {
  157. unsigned long v, i;
  158. phys_addr_t p;
  159. int err;
  160. /* Make sure we have the base flags */
  161. if ((flags & _PAGE_PRESENT) == 0)
  162. flags |= pgprot_val(PAGE_KERNEL);
  163. /* Non-cacheable page cannot be coherent */
  164. if (flags & _PAGE_NO_CACHE)
  165. flags &= ~_PAGE_COHERENT;
  166. /*
  167. * Choose an address to map it to.
  168. * Once the vmalloc system is running, we use it.
  169. * Before then, we use space going down from ioremap_base
  170. * (ioremap_bot records where we're up to).
  171. */
  172. p = addr & PAGE_MASK;
  173. size = PAGE_ALIGN(addr + size) - p;
  174. /*
  175. * If the address lies within the first 16 MB, assume it's in ISA
  176. * memory space
  177. */
  178. if (p < 16*1024*1024)
  179. p += _ISA_MEM_BASE;
  180. #ifndef CONFIG_CRASH_DUMP
  181. /*
  182. * Don't allow anybody to remap normal RAM that we're using.
  183. * mem_init() sets high_memory so only do the check after that.
  184. */
  185. if (slab_is_available() && (p < virt_to_phys(high_memory)) &&
  186. !(__allow_ioremap_reserved && memblock_is_region_reserved(p, size))) {
  187. printk("__ioremap(): phys addr 0x%llx is RAM lr %ps\n",
  188. (unsigned long long)p, __builtin_return_address(0));
  189. return NULL;
  190. }
  191. #endif
  192. if (size == 0)
  193. return NULL;
  194. /*
  195. * Is it already mapped? Perhaps overlapped by a previous
  196. * BAT mapping. If the whole area is mapped then we're done,
  197. * otherwise remap it since we want to keep the virt addrs for
  198. * each request contiguous.
  199. *
  200. * We make the assumption here that if the bottom and top
  201. * of the range we want are mapped then it's mapped to the
  202. * same virt address (and this is contiguous).
  203. * -- Cort
  204. */
  205. if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
  206. goto out;
  207. if ((v = p_mapped_by_tlbcam(p)))
  208. goto out;
  209. if (slab_is_available()) {
  210. struct vm_struct *area;
  211. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  212. if (area == 0)
  213. return NULL;
  214. area->phys_addr = p;
  215. v = (unsigned long) area->addr;
  216. } else {
  217. v = (ioremap_bot -= size);
  218. }
  219. /*
  220. * Should check if it is a candidate for a BAT mapping
  221. */
  222. err = 0;
  223. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  224. err = map_page(v+i, p+i, flags);
  225. if (err) {
  226. if (slab_is_available())
  227. vunmap((void *)v);
  228. return NULL;
  229. }
  230. out:
  231. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  232. }
  233. EXPORT_SYMBOL(__ioremap);
  234. void iounmap(volatile void __iomem *addr)
  235. {
  236. /*
  237. * If mapped by BATs then there is nothing to do.
  238. * Calling vfree() generates a benign warning.
  239. */
  240. if (v_mapped_by_bats((unsigned long)addr)) return;
  241. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  242. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  243. }
  244. EXPORT_SYMBOL(iounmap);
  245. int map_page(unsigned long va, phys_addr_t pa, int flags)
  246. {
  247. pmd_t *pd;
  248. pte_t *pg;
  249. int err = -ENOMEM;
  250. /* Use upper 10 bits of VA to index the first level map */
  251. pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
  252. /* Use middle 10 bits of VA to index the second-level map */
  253. pg = pte_alloc_kernel(pd, va);
  254. if (pg != 0) {
  255. err = 0;
  256. /* The PTE should never be already set nor present in the
  257. * hash table
  258. */
  259. BUG_ON((pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE)) &&
  260. flags);
  261. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
  262. __pgprot(flags)));
  263. }
  264. smp_wmb();
  265. return err;
  266. }
  267. /*
  268. * Map in a chunk of physical memory starting at start.
  269. */
  270. void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
  271. {
  272. unsigned long v, s, f;
  273. phys_addr_t p;
  274. int ktext;
  275. s = offset;
  276. v = PAGE_OFFSET + s;
  277. p = memstart_addr + s;
  278. for (; s < top; s += PAGE_SIZE) {
  279. ktext = ((char *) v >= _stext && (char *) v < etext);
  280. f = ktext ? pgprot_val(PAGE_KERNEL_TEXT) : pgprot_val(PAGE_KERNEL);
  281. map_page(v, p, f);
  282. #ifdef CONFIG_PPC_STD_MMU_32
  283. if (ktext)
  284. hash_preload(&init_mm, v, 0, 0x300);
  285. #endif
  286. v += PAGE_SIZE;
  287. p += PAGE_SIZE;
  288. }
  289. }
  290. void __init mapin_ram(void)
  291. {
  292. unsigned long s, top;
  293. #ifndef CONFIG_WII
  294. top = total_lowmem;
  295. s = mmu_mapin_ram(top);
  296. __mapin_ram_chunk(s, top);
  297. #else
  298. if (!wii_hole_size) {
  299. s = mmu_mapin_ram(total_lowmem);
  300. __mapin_ram_chunk(s, total_lowmem);
  301. } else {
  302. top = wii_hole_start;
  303. s = mmu_mapin_ram(top);
  304. __mapin_ram_chunk(s, top);
  305. top = memblock_end_of_DRAM();
  306. s = wii_mmu_mapin_mem2(top);
  307. __mapin_ram_chunk(s, top);
  308. }
  309. #endif
  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. pud_t *pud;
  321. pmd_t *pmd;
  322. pte_t *pte;
  323. int retval = 0;
  324. pgd = pgd_offset(mm, addr & PAGE_MASK);
  325. if (pgd) {
  326. pud = pud_offset(pgd, addr & PAGE_MASK);
  327. if (pud && pud_present(*pud)) {
  328. pmd = pmd_offset(pud, addr & PAGE_MASK);
  329. if (pmd_present(*pmd)) {
  330. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  331. if (pte) {
  332. retval = 1;
  333. *ptep = pte;
  334. if (pmdp)
  335. *pmdp = pmd;
  336. /* XXX caller needs to do pte_unmap, yuck */
  337. }
  338. }
  339. }
  340. }
  341. return(retval);
  342. }
  343. #ifdef CONFIG_DEBUG_PAGEALLOC
  344. static int __change_page_attr(struct page *page, pgprot_t prot)
  345. {
  346. pte_t *kpte;
  347. pmd_t *kpmd;
  348. unsigned long address;
  349. BUG_ON(PageHighMem(page));
  350. address = (unsigned long)page_address(page);
  351. if (v_mapped_by_bats(address) || v_mapped_by_tlbcam(address))
  352. return 0;
  353. if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
  354. return -EINVAL;
  355. __set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
  356. wmb();
  357. flush_tlb_page(NULL, address);
  358. pte_unmap(kpte);
  359. return 0;
  360. }
  361. /*
  362. * Change the page attributes of an page in the linear mapping.
  363. *
  364. * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY
  365. */
  366. static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  367. {
  368. int i, err = 0;
  369. unsigned long flags;
  370. local_irq_save(flags);
  371. for (i = 0; i < numpages; i++, page++) {
  372. err = __change_page_attr(page, prot);
  373. if (err)
  374. break;
  375. }
  376. local_irq_restore(flags);
  377. return err;
  378. }
  379. void __kernel_map_pages(struct page *page, int numpages, int enable)
  380. {
  381. if (PageHighMem(page))
  382. return;
  383. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  384. }
  385. #endif /* CONFIG_DEBUG_PAGEALLOC */
  386. static int fixmaps;
  387. void __set_fixmap (enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
  388. {
  389. unsigned long address = __fix_to_virt(idx);
  390. if (idx >= __end_of_fixed_addresses) {
  391. BUG();
  392. return;
  393. }
  394. map_page(address, phys, pgprot_val(flags));
  395. fixmaps++;
  396. }
  397. void __this_fixmap_does_not_exist(void)
  398. {
  399. WARN_ON(1);
  400. }