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