mmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * linux/arch/unicore32/mm/mmu.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/mman.h>
  17. #include <linux/nodemask.h>
  18. #include <linux/memblock.h>
  19. #include <linux/fs.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/io.h>
  22. #include <asm/cputype.h>
  23. #include <asm/sections.h>
  24. #include <asm/setup.h>
  25. #include <asm/sizes.h>
  26. #include <asm/tlb.h>
  27. #include <mach/map.h>
  28. #include "mm.h"
  29. /*
  30. * empty_zero_page is a special page that is used for
  31. * zero-initialized data and COW.
  32. */
  33. struct page *empty_zero_page;
  34. EXPORT_SYMBOL(empty_zero_page);
  35. /*
  36. * The pmd table for the upper-most set of pages.
  37. */
  38. pmd_t *top_pmd;
  39. pgprot_t pgprot_user;
  40. EXPORT_SYMBOL(pgprot_user);
  41. pgprot_t pgprot_kernel;
  42. EXPORT_SYMBOL(pgprot_kernel);
  43. static int __init noalign_setup(char *__unused)
  44. {
  45. cr_alignment &= ~CR_A;
  46. cr_no_alignment &= ~CR_A;
  47. set_cr(cr_alignment);
  48. return 1;
  49. }
  50. __setup("noalign", noalign_setup);
  51. void adjust_cr(unsigned long mask, unsigned long set)
  52. {
  53. unsigned long flags;
  54. mask &= ~CR_A;
  55. set &= mask;
  56. local_irq_save(flags);
  57. cr_no_alignment = (cr_no_alignment & ~mask) | set;
  58. cr_alignment = (cr_alignment & ~mask) | set;
  59. set_cr((get_cr() & ~mask) | set);
  60. local_irq_restore(flags);
  61. }
  62. struct map_desc {
  63. unsigned long virtual;
  64. unsigned long pfn;
  65. unsigned long length;
  66. unsigned int type;
  67. };
  68. #define PROT_PTE_DEVICE (PTE_PRESENT | PTE_YOUNG | \
  69. PTE_DIRTY | PTE_READ | PTE_WRITE)
  70. #define PROT_SECT_DEVICE (PMD_TYPE_SECT | PMD_PRESENT | \
  71. PMD_SECT_READ | PMD_SECT_WRITE)
  72. static struct mem_type mem_types[] = {
  73. [MT_DEVICE] = { /* Strongly ordered */
  74. .prot_pte = PROT_PTE_DEVICE,
  75. .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
  76. .prot_sect = PROT_SECT_DEVICE,
  77. },
  78. /*
  79. * MT_KUSER: pte for vecpage -- cacheable,
  80. * and sect for unigfx mmap -- noncacheable
  81. */
  82. [MT_KUSER] = {
  83. .prot_pte = PTE_PRESENT | PTE_YOUNG | PTE_DIRTY |
  84. PTE_CACHEABLE | PTE_READ | PTE_EXEC,
  85. .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
  86. .prot_sect = PROT_SECT_DEVICE,
  87. },
  88. [MT_HIGH_VECTORS] = {
  89. .prot_pte = PTE_PRESENT | PTE_YOUNG | PTE_DIRTY |
  90. PTE_CACHEABLE | PTE_READ | PTE_WRITE |
  91. PTE_EXEC,
  92. .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
  93. },
  94. [MT_MEMORY] = {
  95. .prot_pte = PTE_PRESENT | PTE_YOUNG | PTE_DIRTY |
  96. PTE_WRITE | PTE_EXEC,
  97. .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
  98. .prot_sect = PMD_TYPE_SECT | PMD_PRESENT | PMD_SECT_CACHEABLE |
  99. PMD_SECT_READ | PMD_SECT_WRITE | PMD_SECT_EXEC,
  100. },
  101. [MT_ROM] = {
  102. .prot_sect = PMD_TYPE_SECT | PMD_PRESENT | PMD_SECT_CACHEABLE |
  103. PMD_SECT_READ,
  104. },
  105. };
  106. const struct mem_type *get_mem_type(unsigned int type)
  107. {
  108. return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
  109. }
  110. EXPORT_SYMBOL(get_mem_type);
  111. /*
  112. * Adjust the PMD section entries according to the CPU in use.
  113. */
  114. static void __init build_mem_type_table(void)
  115. {
  116. pgprot_user = __pgprot(PTE_PRESENT | PTE_YOUNG | PTE_CACHEABLE);
  117. pgprot_kernel = __pgprot(PTE_PRESENT | PTE_YOUNG |
  118. PTE_DIRTY | PTE_READ | PTE_WRITE |
  119. PTE_EXEC | PTE_CACHEABLE);
  120. }
  121. #define vectors_base() (vectors_high() ? 0xffff0000 : 0)
  122. static void __init *early_alloc(unsigned long sz)
  123. {
  124. void *ptr = __va(memblock_alloc(sz, sz));
  125. memset(ptr, 0, sz);
  126. return ptr;
  127. }
  128. static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,
  129. unsigned long prot)
  130. {
  131. if (pmd_none(*pmd)) {
  132. pte_t *pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
  133. __pmd_populate(pmd, __pa(pte) | prot);
  134. }
  135. BUG_ON(pmd_bad(*pmd));
  136. return pte_offset_kernel(pmd, addr);
  137. }
  138. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  139. unsigned long end, unsigned long pfn,
  140. const struct mem_type *type)
  141. {
  142. pte_t *pte = early_pte_alloc(pmd, addr, type->prot_l1);
  143. do {
  144. set_pte(pte, pfn_pte(pfn, __pgprot(type->prot_pte)));
  145. pfn++;
  146. } while (pte++, addr += PAGE_SIZE, addr != end);
  147. }
  148. static void __init alloc_init_section(pgd_t *pgd, unsigned long addr,
  149. unsigned long end, unsigned long phys,
  150. const struct mem_type *type)
  151. {
  152. pmd_t *pmd = pmd_offset((pud_t *)pgd, addr);
  153. /*
  154. * Try a section mapping - end, addr and phys must all be aligned
  155. * to a section boundary.
  156. */
  157. if (((addr | end | phys) & ~SECTION_MASK) == 0) {
  158. pmd_t *p = pmd;
  159. do {
  160. set_pmd(pmd, __pmd(phys | type->prot_sect));
  161. phys += SECTION_SIZE;
  162. } while (pmd++, addr += SECTION_SIZE, addr != end);
  163. flush_pmd_entry(p);
  164. } else {
  165. /*
  166. * No need to loop; pte's aren't interested in the
  167. * individual L1 entries.
  168. */
  169. alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
  170. }
  171. }
  172. /*
  173. * Create the page directory entries and any necessary
  174. * page tables for the mapping specified by `md'. We
  175. * are able to cope here with varying sizes and address
  176. * offsets, and we take full advantage of sections.
  177. */
  178. static void __init create_mapping(struct map_desc *md)
  179. {
  180. unsigned long phys, addr, length, end;
  181. const struct mem_type *type;
  182. pgd_t *pgd;
  183. if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
  184. printk(KERN_WARNING "BUG: not creating mapping for "
  185. "0x%08llx at 0x%08lx in user region\n",
  186. __pfn_to_phys((u64)md->pfn), md->virtual);
  187. return;
  188. }
  189. if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
  190. md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
  191. printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
  192. "overlaps vmalloc space\n",
  193. __pfn_to_phys((u64)md->pfn), md->virtual);
  194. }
  195. type = &mem_types[md->type];
  196. addr = md->virtual & PAGE_MASK;
  197. phys = (unsigned long)__pfn_to_phys(md->pfn);
  198. length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
  199. if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
  200. printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
  201. "be mapped using pages, ignoring.\n",
  202. __pfn_to_phys(md->pfn), addr);
  203. return;
  204. }
  205. pgd = pgd_offset_k(addr);
  206. end = addr + length;
  207. do {
  208. unsigned long next = pgd_addr_end(addr, end);
  209. alloc_init_section(pgd, addr, next, phys, type);
  210. phys += next - addr;
  211. addr = next;
  212. } while (pgd++, addr != end);
  213. }
  214. static void * __initdata vmalloc_min = (void *)(VMALLOC_END - SZ_128M);
  215. /*
  216. * vmalloc=size forces the vmalloc area to be exactly 'size'
  217. * bytes. This can be used to increase (or decrease) the vmalloc
  218. * area - the default is 128m.
  219. */
  220. static int __init early_vmalloc(char *arg)
  221. {
  222. unsigned long vmalloc_reserve = memparse(arg, NULL);
  223. if (vmalloc_reserve < SZ_16M) {
  224. vmalloc_reserve = SZ_16M;
  225. printk(KERN_WARNING
  226. "vmalloc area too small, limiting to %luMB\n",
  227. vmalloc_reserve >> 20);
  228. }
  229. if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
  230. vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
  231. printk(KERN_WARNING
  232. "vmalloc area is too big, limiting to %luMB\n",
  233. vmalloc_reserve >> 20);
  234. }
  235. vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
  236. return 0;
  237. }
  238. early_param("vmalloc", early_vmalloc);
  239. static phys_addr_t lowmem_limit __initdata = SZ_1G;
  240. static void __init sanity_check_meminfo(void)
  241. {
  242. int i, j;
  243. lowmem_limit = __pa(vmalloc_min - 1) + 1;
  244. memblock_set_current_limit(lowmem_limit);
  245. for (i = 0, j = 0; i < meminfo.nr_banks; i++) {
  246. struct membank *bank = &meminfo.bank[j];
  247. *bank = meminfo.bank[i];
  248. j++;
  249. }
  250. meminfo.nr_banks = j;
  251. }
  252. static inline void prepare_page_table(void)
  253. {
  254. unsigned long addr;
  255. phys_addr_t end;
  256. /*
  257. * Clear out all the mappings below the kernel image.
  258. */
  259. for (addr = 0; addr < MODULES_VADDR; addr += PGDIR_SIZE)
  260. pmd_clear(pmd_off_k(addr));
  261. for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
  262. pmd_clear(pmd_off_k(addr));
  263. /*
  264. * Find the end of the first block of lowmem.
  265. */
  266. end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
  267. if (end >= lowmem_limit)
  268. end = lowmem_limit;
  269. /*
  270. * Clear out all the kernel space mappings, except for the first
  271. * memory bank, up to the end of the vmalloc region.
  272. */
  273. for (addr = __phys_to_virt(end);
  274. addr < VMALLOC_END; addr += PGDIR_SIZE)
  275. pmd_clear(pmd_off_k(addr));
  276. }
  277. /*
  278. * Reserve the special regions of memory
  279. */
  280. void __init uc32_mm_memblock_reserve(void)
  281. {
  282. /*
  283. * Reserve the page tables. These are already in use,
  284. * and can only be in node 0.
  285. */
  286. memblock_reserve(__pa(swapper_pg_dir), PTRS_PER_PGD * sizeof(pgd_t));
  287. }
  288. /*
  289. * Set up device the mappings. Since we clear out the page tables for all
  290. * mappings above VMALLOC_END, we will remove any debug device mappings.
  291. * This means you have to be careful how you debug this function, or any
  292. * called function. This means you can't use any function or debugging
  293. * method which may touch any device, otherwise the kernel _will_ crash.
  294. */
  295. static void __init devicemaps_init(void)
  296. {
  297. struct map_desc map;
  298. unsigned long addr;
  299. void *vectors;
  300. /*
  301. * Allocate the vector page early.
  302. */
  303. vectors = early_alloc(PAGE_SIZE);
  304. for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
  305. pmd_clear(pmd_off_k(addr));
  306. /*
  307. * Create a mapping for the machine vectors at the high-vectors
  308. * location (0xffff0000). If we aren't using high-vectors, also
  309. * create a mapping at the low-vectors virtual address.
  310. */
  311. map.pfn = __phys_to_pfn(virt_to_phys(vectors));
  312. map.virtual = VECTORS_BASE;
  313. map.length = PAGE_SIZE;
  314. map.type = MT_HIGH_VECTORS;
  315. create_mapping(&map);
  316. /*
  317. * Create a mapping for the kuser page at the special
  318. * location (0xbfff0000) to the same vectors location.
  319. */
  320. map.pfn = __phys_to_pfn(virt_to_phys(vectors));
  321. map.virtual = KUSER_VECPAGE_BASE;
  322. map.length = PAGE_SIZE;
  323. map.type = MT_KUSER;
  324. create_mapping(&map);
  325. /*
  326. * Finally flush the caches and tlb to ensure that we're in a
  327. * consistent state wrt the writebuffer. This also ensures that
  328. * any write-allocated cache lines in the vector page are written
  329. * back. After this point, we can start to touch devices again.
  330. */
  331. local_flush_tlb_all();
  332. flush_cache_all();
  333. }
  334. static void __init map_lowmem(void)
  335. {
  336. struct memblock_region *reg;
  337. /* Map all the lowmem memory banks. */
  338. for_each_memblock(memory, reg) {
  339. phys_addr_t start = reg->base;
  340. phys_addr_t end = start + reg->size;
  341. struct map_desc map;
  342. if (end > lowmem_limit)
  343. end = lowmem_limit;
  344. if (start >= end)
  345. break;
  346. map.pfn = __phys_to_pfn(start);
  347. map.virtual = __phys_to_virt(start);
  348. map.length = end - start;
  349. map.type = MT_MEMORY;
  350. create_mapping(&map);
  351. }
  352. }
  353. /*
  354. * paging_init() sets up the page tables, initialises the zone memory
  355. * maps, and sets up the zero page, bad page and bad page tables.
  356. */
  357. void __init paging_init(void)
  358. {
  359. void *zero_page;
  360. build_mem_type_table();
  361. sanity_check_meminfo();
  362. prepare_page_table();
  363. map_lowmem();
  364. devicemaps_init();
  365. top_pmd = pmd_off_k(0xffff0000);
  366. /* allocate the zero page. */
  367. zero_page = early_alloc(PAGE_SIZE);
  368. bootmem_init();
  369. empty_zero_page = virt_to_page(zero_page);
  370. __flush_dcache_page(NULL, empty_zero_page);
  371. }
  372. /*
  373. * In order to soft-boot, we need to insert a 1:1 mapping in place of
  374. * the user-mode pages. This will then ensure that we have predictable
  375. * results when turning the mmu off
  376. */
  377. void setup_mm_for_reboot(char mode)
  378. {
  379. unsigned long base_pmdval;
  380. pgd_t *pgd;
  381. int i;
  382. /*
  383. * We need to access to user-mode page tables here. For kernel threads
  384. * we don't have any user-mode mappings so we use the context that we
  385. * "borrowed".
  386. */
  387. pgd = current->active_mm->pgd;
  388. base_pmdval = PMD_SECT_WRITE | PMD_SECT_READ | PMD_TYPE_SECT;
  389. for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
  390. unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
  391. pmd_t *pmd;
  392. pmd = pmd_off(pgd, i << PGDIR_SHIFT);
  393. set_pmd(pmd, __pmd(pmdval));
  394. flush_pmd_entry(pmd);
  395. }
  396. local_flush_tlb_all();
  397. }
  398. /*
  399. * Take care of architecture specific things when placing a new PTE into
  400. * a page table, or changing an existing PTE. Basically, there are two
  401. * things that we need to take care of:
  402. *
  403. * 1. If PG_dcache_clean is not set for the page, we need to ensure
  404. * that any cache entries for the kernels virtual memory
  405. * range are written back to the page.
  406. * 2. If we have multiple shared mappings of the same space in
  407. * an object, we need to deal with the cache aliasing issues.
  408. *
  409. * Note that the pte lock will be held.
  410. */
  411. void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr,
  412. pte_t *ptep)
  413. {
  414. unsigned long pfn = pte_pfn(*ptep);
  415. struct address_space *mapping;
  416. struct page *page;
  417. if (!pfn_valid(pfn))
  418. return;
  419. /*
  420. * The zero page is never written to, so never has any dirty
  421. * cache lines, and therefore never needs to be flushed.
  422. */
  423. page = pfn_to_page(pfn);
  424. if (page == ZERO_PAGE(0))
  425. return;
  426. mapping = page_mapping(page);
  427. if (!test_and_set_bit(PG_dcache_clean, &page->flags))
  428. __flush_dcache_page(mapping, page);
  429. if (mapping)
  430. if (vma->vm_flags & VM_EXEC)
  431. __flush_icache_all();
  432. }