pgtable_32.c 11 KB

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