blob: b1f279cd00bfd96afb039e917a0692ce440beaeb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* arch/sparc64/mm/tlb.c
2 *
3 * Copyright (C) 2004 David S. Miller <davem@redhat.com>
4 */
5
6#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/percpu.h>
9#include <linux/mm.h>
10#include <linux/swap.h>
David S. Millerc9f29462006-04-30 22:54:27 -070011#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include <asm/pgtable.h>
14#include <asm/pgalloc.h>
15#include <asm/tlbflush.h>
16#include <asm/cacheflush.h>
17#include <asm/mmu_context.h>
18#include <asm/tlb.h>
19
20/* Heavily inspired by the ppc64 code. */
21
Peter Zijlstra90f08e32011-05-24 17:11:50 -070022static DEFINE_PER_CPU(struct tlb_batch, tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24void flush_tlb_pending(void)
25{
Peter Zijlstra90f08e32011-05-24 17:11:50 -070026 struct tlb_batch *tb = &get_cpu_var(tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Peter Zijlstra90f08e32011-05-24 17:11:50 -070028 if (tb->tlb_nr) {
29 flush_tsb_user(tb);
David S. Miller74bf4312006-01-31 18:29:18 -080030
Peter Zijlstra90f08e32011-05-24 17:11:50 -070031 if (CTX_VALID(tb->mm->context)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#ifdef CONFIG_SMP
Peter Zijlstra90f08e32011-05-24 17:11:50 -070033 smp_flush_tlb_pending(tb->mm, tb->tlb_nr,
34 &tb->vaddrs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#else
Peter Zijlstra90f08e32011-05-24 17:11:50 -070036 __flush_tlb_pending(CTX_HWBITS(tb->mm->context),
37 tb->tlb_nr, &tb->vaddrs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#endif
39 }
Peter Zijlstra90f08e32011-05-24 17:11:50 -070040 tb->tlb_nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
David S. Millerc9f29462006-04-30 22:54:27 -070042
Peter Zijlstra90f08e32011-05-24 17:11:50 -070043 put_cpu_var(tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Peter Zijlstra90f08e32011-05-24 17:11:50 -070046void tlb_batch_add(struct mm_struct *mm, unsigned long vaddr,
47 pte_t *ptep, pte_t orig, int fullmm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Peter Zijlstra90f08e32011-05-24 17:11:50 -070049 struct tlb_batch *tb = &get_cpu_var(tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned long nr;
51
52 vaddr &= PAGE_MASK;
53 if (pte_exec(orig))
54 vaddr |= 0x1UL;
55
David S. Miller7a591cf2006-02-26 19:44:50 -080056 if (tlb_type != hypervisor &&
57 pte_dirty(orig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned long paddr, pfn = pte_pfn(orig);
59 struct address_space *mapping;
60 struct page *page;
61
62 if (!pfn_valid(pfn))
63 goto no_cache_flush;
64
65 page = pfn_to_page(pfn);
66 if (PageReserved(page))
67 goto no_cache_flush;
68
69 /* A real file page? */
70 mapping = page_mapping(page);
71 if (!mapping)
72 goto no_cache_flush;
73
74 paddr = (unsigned long) page_address(page);
75 if ((paddr ^ vaddr) & (1 << 13))
76 flush_dcache_page_all(mm, page);
77 }
78
79no_cache_flush:
80
Peter Zijlstra90f08e32011-05-24 17:11:50 -070081 if (fullmm) {
82 put_cpu_var(tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return;
Peter Zijlstra90f08e32011-05-24 17:11:50 -070084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Peter Zijlstra90f08e32011-05-24 17:11:50 -070086 nr = tb->tlb_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Peter Zijlstra90f08e32011-05-24 17:11:50 -070088 if (unlikely(nr != 0 && mm != tb->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 flush_tlb_pending();
90 nr = 0;
91 }
92
93 if (nr == 0)
Peter Zijlstra90f08e32011-05-24 17:11:50 -070094 tb->mm = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Peter Zijlstra90f08e32011-05-24 17:11:50 -070096 tb->vaddrs[nr] = vaddr;
97 tb->tlb_nr = ++nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (nr >= TLB_BATCH_NR)
99 flush_tlb_pending();
Peter Zijlstra90f08e32011-05-24 17:11:50 -0700100
101 put_cpu_var(tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}