pgtable_32.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <asm/pgtable.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/fixmap.h>
  31. #include <asm/io.h>
  32. #include "mmu_decl.h"
  33. unsigned long ioremap_base;
  34. unsigned long ioremap_bot;
  35. EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
  36. #if defined(CONFIG_6xx) || defined(CONFIG_POWER3)
  37. #define HAVE_BATS 1
  38. #endif
  39. #if defined(CONFIG_FSL_BOOKE)
  40. #define HAVE_TLBCAM 1
  41. #endif
  42. extern char etext[], _stext[];
  43. #ifdef HAVE_BATS
  44. extern phys_addr_t v_mapped_by_bats(unsigned long va);
  45. extern unsigned long p_mapped_by_bats(phys_addr_t pa);
  46. void setbat(int index, unsigned long virt, phys_addr_t phys,
  47. unsigned int size, int flags);
  48. #else /* !HAVE_BATS */
  49. #define v_mapped_by_bats(x) (0UL)
  50. #define p_mapped_by_bats(x) (0UL)
  51. #endif /* HAVE_BATS */
  52. #ifdef HAVE_TLBCAM
  53. extern unsigned int tlbcam_index;
  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. pgd_t *pgd_alloc(struct mm_struct *mm)
  62. {
  63. pgd_t *ret;
  64. /* pgdir take page or two with 4K pages and a page fraction otherwise */
  65. #ifndef CONFIG_PPC_4K_PAGES
  66. ret = (pgd_t *)kzalloc(1 << PGDIR_ORDER, GFP_KERNEL);
  67. #else
  68. ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
  69. PGDIR_ORDER - PAGE_SHIFT);
  70. #endif
  71. return ret;
  72. }
  73. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  74. {
  75. #ifndef CONFIG_PPC_4K_PAGES
  76. kfree((void *)pgd);
  77. #else
  78. free_pages((unsigned long)pgd, PGDIR_ORDER - PAGE_SHIFT);
  79. #endif
  80. }
  81. __init_refok 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. pgtable_t 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 | __GFP_ZERO;
  100. #else
  101. gfp_t flags = GFP_KERNEL | __GFP_REPEAT | __GFP_ZERO;
  102. #endif
  103. ptepage = alloc_pages(flags, 0);
  104. if (!ptepage)
  105. return NULL;
  106. pgtable_page_ctor(ptepage);
  107. return ptepage;
  108. }
  109. void __iomem *
  110. ioremap(phys_addr_t addr, unsigned long size)
  111. {
  112. return __ioremap_caller(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED,
  113. __builtin_return_address(0));
  114. }
  115. EXPORT_SYMBOL(ioremap);
  116. void __iomem *
  117. ioremap_flags(phys_addr_t addr, unsigned long size, unsigned long flags)
  118. {
  119. /* writeable implies dirty for kernel addresses */
  120. if (flags & _PAGE_RW)
  121. flags |= _PAGE_DIRTY | _PAGE_HWWRITE;
  122. /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
  123. flags &= ~(_PAGE_USER | _PAGE_EXEC | _PAGE_HWEXEC);
  124. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  125. }
  126. EXPORT_SYMBOL(ioremap_flags);
  127. void __iomem *
  128. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  129. {
  130. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  131. }
  132. void __iomem *
  133. __ioremap_caller(phys_addr_t addr, unsigned long size, unsigned long flags,
  134. void *caller)
  135. {
  136. unsigned long v, i;
  137. phys_addr_t p;
  138. int err;
  139. /* Make sure we have the base flags */
  140. if ((flags & _PAGE_PRESENT) == 0)
  141. flags |= PAGE_KERNEL;
  142. /* Non-cacheable page cannot be coherent */
  143. if (flags & _PAGE_NO_CACHE)
  144. flags &= ~_PAGE_COHERENT;
  145. /*
  146. * Choose an address to map it to.
  147. * Once the vmalloc system is running, we use it.
  148. * Before then, we use space going down from ioremap_base
  149. * (ioremap_bot records where we're up to).
  150. */
  151. p = addr & PAGE_MASK;
  152. size = PAGE_ALIGN(addr + size) - p;
  153. /*
  154. * If the address lies within the first 16 MB, assume it's in ISA
  155. * memory space
  156. */
  157. if (p < 16*1024*1024)
  158. p += _ISA_MEM_BASE;
  159. #ifndef CONFIG_CRASH_DUMP
  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 0x%llx is RAM lr %p\n",
  166. (unsigned long long)p, __builtin_return_address(0));
  167. return NULL;
  168. }
  169. #endif
  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_caller(size, VM_IOREMAP, caller);
  190. if (area == 0)
  191. return NULL;
  192. v = (unsigned long) area->addr;
  193. } else {
  194. v = (ioremap_bot -= size);
  195. }
  196. /*
  197. * Should check if it is a candidate for a BAT mapping
  198. */
  199. err = 0;
  200. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  201. err = map_page(v+i, p+i, flags);
  202. if (err) {
  203. if (mem_init_done)
  204. vunmap((void *)v);
  205. return NULL;
  206. }
  207. out:
  208. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  209. }
  210. EXPORT_SYMBOL(__ioremap);
  211. void iounmap(volatile void __iomem *addr)
  212. {
  213. /*
  214. * If mapped by BATs then there is nothing to do.
  215. * Calling vfree() generates a benign warning.
  216. */
  217. if (v_mapped_by_bats((unsigned long)addr)) return;
  218. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  219. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  220. }
  221. EXPORT_SYMBOL(iounmap);
  222. int map_page(unsigned long va, phys_addr_t pa, int flags)
  223. {
  224. pmd_t *pd;
  225. pte_t *pg;
  226. int err = -ENOMEM;
  227. /* Use upper 10 bits of VA to index the first level map */
  228. pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
  229. /* Use middle 10 bits of VA to index the second-level map */
  230. pg = pte_alloc_kernel(pd, va);
  231. if (pg != 0) {
  232. err = 0;
  233. /* The PTE should never be already set nor present in the
  234. * hash table
  235. */
  236. BUG_ON((pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE)) &&
  237. flags);
  238. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
  239. __pgprot(flags)));
  240. }
  241. return err;
  242. }
  243. /*
  244. * Map in a big chunk of physical memory starting at PAGE_OFFSET.
  245. */
  246. void __init mapin_ram(void)
  247. {
  248. unsigned long v, s, f;
  249. phys_addr_t p;
  250. int ktext;
  251. s = mmu_mapin_ram();
  252. v = PAGE_OFFSET + s;
  253. p = memstart_addr + s;
  254. for (; s < total_lowmem; s += PAGE_SIZE) {
  255. ktext = ((char *) v >= _stext && (char *) v < etext);
  256. f = ktext ? PAGE_KERNEL_TEXT : PAGE_KERNEL;
  257. map_page(v, p, f);
  258. #ifdef CONFIG_PPC_STD_MMU_32
  259. if (ktext)
  260. hash_preload(&init_mm, v, 0, 0x300);
  261. #endif
  262. v += PAGE_SIZE;
  263. p += PAGE_SIZE;
  264. }
  265. }
  266. /* Scan the real Linux page tables and return a PTE pointer for
  267. * a virtual address in a context.
  268. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  269. * the PTE pointer is unmodified if PTE is not found.
  270. */
  271. int
  272. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
  273. {
  274. pgd_t *pgd;
  275. pud_t *pud;
  276. pmd_t *pmd;
  277. pte_t *pte;
  278. int retval = 0;
  279. pgd = pgd_offset(mm, addr & PAGE_MASK);
  280. if (pgd) {
  281. pud = pud_offset(pgd, addr & PAGE_MASK);
  282. if (pud && pud_present(*pud)) {
  283. pmd = pmd_offset(pud, addr & PAGE_MASK);
  284. if (pmd_present(*pmd)) {
  285. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  286. if (pte) {
  287. retval = 1;
  288. *ptep = pte;
  289. if (pmdp)
  290. *pmdp = pmd;
  291. /* XXX caller needs to do pte_unmap, yuck */
  292. }
  293. }
  294. }
  295. }
  296. return(retval);
  297. }
  298. #ifdef CONFIG_DEBUG_PAGEALLOC
  299. static int __change_page_attr(struct page *page, pgprot_t prot)
  300. {
  301. pte_t *kpte;
  302. pmd_t *kpmd;
  303. unsigned long address;
  304. BUG_ON(PageHighMem(page));
  305. address = (unsigned long)page_address(page);
  306. if (v_mapped_by_bats(address) || v_mapped_by_tlbcam(address))
  307. return 0;
  308. if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
  309. return -EINVAL;
  310. set_pte_at(&init_mm, address, kpte, mk_pte(page, prot));
  311. wmb();
  312. #ifdef CONFIG_PPC_STD_MMU
  313. flush_hash_pages(0, address, pmd_val(*kpmd), 1);
  314. #else
  315. flush_tlb_page(NULL, address);
  316. #endif
  317. pte_unmap(kpte);
  318. return 0;
  319. }
  320. /*
  321. * Change the page attributes of an page in the linear mapping.
  322. *
  323. * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY
  324. */
  325. static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  326. {
  327. int i, err = 0;
  328. unsigned long flags;
  329. local_irq_save(flags);
  330. for (i = 0; i < numpages; i++, page++) {
  331. err = __change_page_attr(page, prot);
  332. if (err)
  333. break;
  334. }
  335. local_irq_restore(flags);
  336. return err;
  337. }
  338. void kernel_map_pages(struct page *page, int numpages, int enable)
  339. {
  340. if (PageHighMem(page))
  341. return;
  342. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  343. }
  344. #endif /* CONFIG_DEBUG_PAGEALLOC */
  345. static int fixmaps;
  346. unsigned long FIXADDR_TOP = (-PAGE_SIZE);
  347. EXPORT_SYMBOL(FIXADDR_TOP);
  348. void __set_fixmap (enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
  349. {
  350. unsigned long address = __fix_to_virt(idx);
  351. if (idx >= __end_of_fixed_addresses) {
  352. BUG();
  353. return;
  354. }
  355. map_page(address, phys, pgprot_val(flags));
  356. fixmaps++;
  357. }
  358. void __this_fixmap_does_not_exist(void)
  359. {
  360. WARN_ON(1);
  361. }