Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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. Miller | c9f2946 | 2006-04-30 22:54:27 -0700 | [diff] [blame] | 11 | #include <linux/preempt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 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 Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 22 | static DEFINE_PER_CPU(struct tlb_batch, tlb_batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | void flush_tlb_pending(void) |
| 25 | { |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 26 | struct tlb_batch *tb = &get_cpu_var(tlb_batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 28 | if (tb->tlb_nr) { |
| 29 | flush_tsb_user(tb); |
David S. Miller | 74bf431 | 2006-01-31 18:29:18 -0800 | [diff] [blame] | 30 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 31 | if (CTX_VALID(tb->mm->context)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #ifdef CONFIG_SMP |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 33 | smp_flush_tlb_pending(tb->mm, tb->tlb_nr, |
| 34 | &tb->vaddrs[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #else |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 36 | __flush_tlb_pending(CTX_HWBITS(tb->mm->context), |
| 37 | tb->tlb_nr, &tb->vaddrs[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #endif |
| 39 | } |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 40 | tb->tlb_nr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
David S. Miller | c9f2946 | 2006-04-30 22:54:27 -0700 | [diff] [blame] | 42 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 43 | put_cpu_var(tlb_batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 46 | void tlb_batch_add(struct mm_struct *mm, unsigned long vaddr, |
| 47 | pte_t *ptep, pte_t orig, int fullmm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 49 | struct tlb_batch *tb = &get_cpu_var(tlb_batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | unsigned long nr; |
| 51 | |
| 52 | vaddr &= PAGE_MASK; |
| 53 | if (pte_exec(orig)) |
| 54 | vaddr |= 0x1UL; |
| 55 | |
David S. Miller | 7a591cf | 2006-02-26 19:44:50 -0800 | [diff] [blame] | 56 | if (tlb_type != hypervisor && |
| 57 | pte_dirty(orig)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | 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 | |
| 79 | no_cache_flush: |
| 80 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 81 | if (fullmm) { |
| 82 | put_cpu_var(tlb_batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return; |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 86 | nr = tb->tlb_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 88 | if (unlikely(nr != 0 && mm != tb->mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | flush_tlb_pending(); |
| 90 | nr = 0; |
| 91 | } |
| 92 | |
| 93 | if (nr == 0) |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 94 | tb->mm = mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 96 | tb->vaddrs[nr] = vaddr; |
| 97 | tb->tlb_nr = ++nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | if (nr >= TLB_BATCH_NR) |
| 99 | flush_tlb_pending(); |
Peter Zijlstra | 90f08e3 | 2011-05-24 17:11:50 -0700 | [diff] [blame] | 100 | |
| 101 | put_cpu_var(tlb_batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |