pgtable_32.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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_bot;
  37. EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
  38. extern char etext[], _stext[], _sinittext[], _einittext[];
  39. #define PGDIR_ORDER (32 + PGD_T_LOG2 - PGDIR_SHIFT)
  40. #ifndef CONFIG_PPC_4K_PAGES
  41. static struct kmem_cache *pgtable_cache;
  42. void pgtable_cache_init(void)
  43. {
  44. pgtable_cache = kmem_cache_create("PGDIR cache", 1 << PGDIR_ORDER,
  45. 1 << PGDIR_ORDER, 0, NULL);
  46. if (pgtable_cache == NULL)
  47. panic("Couldn't allocate pgtable caches");
  48. }
  49. #endif
  50. pgd_t *pgd_alloc(struct mm_struct *mm)
  51. {
  52. pgd_t *ret;
  53. /* pgdir take page or two with 4K pages and a page fraction otherwise */
  54. #ifndef CONFIG_PPC_4K_PAGES
  55. ret = kmem_cache_alloc(pgtable_cache, GFP_KERNEL | __GFP_ZERO);
  56. #else
  57. ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
  58. PGDIR_ORDER - PAGE_SHIFT);
  59. #endif
  60. return ret;
  61. }
  62. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  63. {
  64. #ifndef CONFIG_PPC_4K_PAGES
  65. kmem_cache_free(pgtable_cache, (void *)pgd);
  66. #else
  67. free_pages((unsigned long)pgd, PGDIR_ORDER - PAGE_SHIFT);
  68. #endif
  69. }
  70. __ref pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  71. {
  72. pte_t *pte;
  73. if (slab_is_available()) {
  74. pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  75. } else {
  76. pte = __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  77. if (pte)
  78. clear_page(pte);
  79. }
  80. return pte;
  81. }
  82. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  83. {
  84. struct page *ptepage;
  85. gfp_t flags = GFP_KERNEL | __GFP_ZERO;
  86. ptepage = alloc_pages(flags, 0);
  87. if (!ptepage)
  88. return NULL;
  89. if (!pgtable_page_ctor(ptepage)) {
  90. __free_page(ptepage);
  91. return NULL;
  92. }
  93. return ptepage;
  94. }
  95. void __iomem *
  96. ioremap(phys_addr_t addr, unsigned long size)
  97. {
  98. return __ioremap_caller(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED,
  99. __builtin_return_address(0));
  100. }
  101. EXPORT_SYMBOL(ioremap);
  102. void __iomem *
  103. ioremap_wc(phys_addr_t addr, unsigned long size)
  104. {
  105. return __ioremap_caller(addr, size, _PAGE_NO_CACHE,
  106. __builtin_return_address(0));
  107. }
  108. EXPORT_SYMBOL(ioremap_wc);
  109. void __iomem *
  110. ioremap_prot(phys_addr_t addr, unsigned long size, unsigned long flags)
  111. {
  112. /* writeable implies dirty for kernel addresses */
  113. if ((flags & (_PAGE_RW | _PAGE_RO)) != _PAGE_RO)
  114. flags |= _PAGE_DIRTY | _PAGE_HWWRITE;
  115. /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
  116. flags &= ~(_PAGE_USER | _PAGE_EXEC);
  117. #ifdef _PAGE_BAP_SR
  118. /* _PAGE_USER contains _PAGE_BAP_SR on BookE using the new PTE format
  119. * which means that we just cleared supervisor access... oops ;-) This
  120. * restores it
  121. */
  122. flags |= _PAGE_BAP_SR;
  123. #endif
  124. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  125. }
  126. EXPORT_SYMBOL(ioremap_prot);
  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 |= pgprot_val(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_TOP
  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 (slab_is_available() && (p < virt_to_phys(high_memory)) &&
  165. !(__allow_ioremap_reserved && memblock_is_region_reserved(p, size))) {
  166. printk("__ioremap(): phys addr 0x%llx is RAM lr %ps\n",
  167. (unsigned long long)p, __builtin_return_address(0));
  168. return NULL;
  169. }
  170. #endif
  171. if (size == 0)
  172. return NULL;
  173. /*
  174. * Is it already mapped? Perhaps overlapped by a previous
  175. * mapping.
  176. */
  177. v = p_block_mapped(p);
  178. if (v)
  179. goto out;
  180. if (slab_is_available()) {
  181. struct vm_struct *area;
  182. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  183. if (area == 0)
  184. return NULL;
  185. area->phys_addr = p;
  186. v = (unsigned long) area->addr;
  187. } else {
  188. v = (ioremap_bot -= size);
  189. }
  190. /*
  191. * Should check if it is a candidate for a BAT mapping
  192. */
  193. err = 0;
  194. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  195. err = map_page(v+i, p+i, flags);
  196. if (err) {
  197. if (slab_is_available())
  198. vunmap((void *)v);
  199. return NULL;
  200. }
  201. out:
  202. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  203. }
  204. EXPORT_SYMBOL(__ioremap);
  205. void iounmap(volatile void __iomem *addr)
  206. {
  207. /*
  208. * If mapped by BATs then there is nothing to do.
  209. * Calling vfree() generates a benign warning.
  210. */
  211. if (v_block_mapped((unsigned long)addr))
  212. return;
  213. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  214. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  215. }
  216. EXPORT_SYMBOL(iounmap);
  217. int map_page(unsigned long va, phys_addr_t pa, int flags)
  218. {
  219. pmd_t *pd;
  220. pte_t *pg;
  221. int err = -ENOMEM;
  222. /* Use upper 10 bits of VA to index the first level map */
  223. pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
  224. /* Use middle 10 bits of VA to index the second-level map */
  225. pg = pte_alloc_kernel(pd, va);
  226. if (pg != 0) {
  227. err = 0;
  228. /* The PTE should never be already set nor present in the
  229. * hash table
  230. */
  231. BUG_ON((pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE)) &&
  232. flags);
  233. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
  234. __pgprot(flags)));
  235. }
  236. smp_wmb();
  237. return err;
  238. }
  239. /*
  240. * Map in a chunk of physical memory starting at start.
  241. */
  242. void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
  243. {
  244. unsigned long v, s, f;
  245. phys_addr_t p;
  246. int ktext;
  247. s = offset;
  248. v = PAGE_OFFSET + s;
  249. p = memstart_addr + s;
  250. for (; s < top; s += PAGE_SIZE) {
  251. ktext = ((char *)v >= _stext && (char *)v < etext) ||
  252. ((char *)v >= _sinittext && (char *)v < _einittext);
  253. f = ktext ? pgprot_val(PAGE_KERNEL_TEXT) : pgprot_val(PAGE_KERNEL);
  254. map_page(v, p, f);
  255. #ifdef CONFIG_PPC_STD_MMU_32
  256. if (ktext)
  257. hash_preload(&init_mm, v, 0, 0x300);
  258. #endif
  259. v += PAGE_SIZE;
  260. p += PAGE_SIZE;
  261. }
  262. }
  263. void __init mapin_ram(void)
  264. {
  265. unsigned long s, top;
  266. #ifndef CONFIG_WII
  267. top = total_lowmem;
  268. s = mmu_mapin_ram(top);
  269. __mapin_ram_chunk(s, top);
  270. #else
  271. if (!wii_hole_size) {
  272. s = mmu_mapin_ram(total_lowmem);
  273. __mapin_ram_chunk(s, total_lowmem);
  274. } else {
  275. top = wii_hole_start;
  276. s = mmu_mapin_ram(top);
  277. __mapin_ram_chunk(s, top);
  278. top = memblock_end_of_DRAM();
  279. s = wii_mmu_mapin_mem2(top);
  280. __mapin_ram_chunk(s, top);
  281. }
  282. #endif
  283. }
  284. /* Scan the real Linux page tables and return a PTE pointer for
  285. * a virtual address in a context.
  286. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  287. * the PTE pointer is unmodified if PTE is not found.
  288. */
  289. int
  290. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
  291. {
  292. pgd_t *pgd;
  293. pud_t *pud;
  294. pmd_t *pmd;
  295. pte_t *pte;
  296. int retval = 0;
  297. pgd = pgd_offset(mm, addr & PAGE_MASK);
  298. if (pgd) {
  299. pud = pud_offset(pgd, addr & PAGE_MASK);
  300. if (pud && pud_present(*pud)) {
  301. pmd = pmd_offset(pud, addr & PAGE_MASK);
  302. if (pmd_present(*pmd)) {
  303. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  304. if (pte) {
  305. retval = 1;
  306. *ptep = pte;
  307. if (pmdp)
  308. *pmdp = pmd;
  309. /* XXX caller needs to do pte_unmap, yuck */
  310. }
  311. }
  312. }
  313. }
  314. return(retval);
  315. }
  316. #ifdef CONFIG_DEBUG_PAGEALLOC
  317. static int __change_page_attr(struct page *page, pgprot_t prot)
  318. {
  319. pte_t *kpte;
  320. pmd_t *kpmd;
  321. unsigned long address;
  322. BUG_ON(PageHighMem(page));
  323. address = (unsigned long)page_address(page);
  324. if (v_block_mapped(address))
  325. return 0;
  326. if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
  327. return -EINVAL;
  328. __set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
  329. wmb();
  330. flush_tlb_page(NULL, address);
  331. pte_unmap(kpte);
  332. return 0;
  333. }
  334. /*
  335. * Change the page attributes of an page in the linear mapping.
  336. *
  337. * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY
  338. */
  339. static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  340. {
  341. int i, err = 0;
  342. unsigned long flags;
  343. local_irq_save(flags);
  344. for (i = 0; i < numpages; i++, page++) {
  345. err = __change_page_attr(page, prot);
  346. if (err)
  347. break;
  348. }
  349. local_irq_restore(flags);
  350. return err;
  351. }
  352. void __kernel_map_pages(struct page *page, int numpages, int enable)
  353. {
  354. if (PageHighMem(page))
  355. return;
  356. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  357. }
  358. #endif /* CONFIG_DEBUG_PAGEALLOC */
  359. static int fixmaps;
  360. void __set_fixmap (enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
  361. {
  362. unsigned long address = __fix_to_virt(idx);
  363. if (idx >= __end_of_fixed_addresses) {
  364. BUG();
  365. return;
  366. }
  367. map_page(address, phys, pgprot_val(flags));
  368. fixmaps++;
  369. }
  370. void __this_fixmap_does_not_exist(void)
  371. {
  372. WARN_ON(1);
  373. }