blob: f1a0dace3efee423e7727e143550aae06f081fd5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King4baa9922008-08-02 10:55:55 +01002 * arch/arm/include/asm/tlb.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2002 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Experimentation shows that on a StrongARM, it appears to be faster
11 * to use the "invalidate whole tlb" rather than "invalidate single
12 * tlb" for this.
13 *
14 * This appears true for both the process fork+exit case, as well as
15 * the munmap-large-area case.
16 */
17#ifndef __ASMARM_TLB_H
18#define __ASMARM_TLB_H
19
20#include <asm/cacheflush.h>
Hyok S. Choi01579032006-02-24 21:41:25 +000021
22#ifndef CONFIG_MMU
23
24#include <linux/pagemap.h>
Russell King58e9c472011-02-20 12:27:49 +000025
26#define tlb_flush(tlb) ((void) tlb)
27
Hyok S. Choi01579032006-02-24 21:41:25 +000028#include <asm-generic/tlb.h>
29
30#else /* !CONFIG_MMU */
31
Russell King06824ba2011-02-20 12:16:45 +000032#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/pgalloc.h>
Russell King06824ba2011-02-20 12:16:45 +000034#include <asm/tlbflush.h>
35
Peter Zijlstra9e14f672011-05-24 17:11:53 -070036#define MMU_GATHER_BUNDLE 8
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * TLB handling. This allows us to remove pages from the page
40 * tables, and efficiently handle the TLB issues.
41 */
42struct mmu_gather {
43 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 unsigned int fullmm;
Russell King06824ba2011-02-20 12:16:45 +000045 struct vm_area_struct *vma;
Linus Torvalds2b047252013-08-15 11:42:25 -070046 unsigned long start, end;
Aaro Koskinen7fccfc02009-04-14 13:07:35 +010047 unsigned long range_start;
48 unsigned long range_end;
Russell King06824ba2011-02-20 12:16:45 +000049 unsigned int nr;
Peter Zijlstra9e14f672011-05-24 17:11:53 -070050 unsigned int max;
51 struct page **pages;
52 struct page *local[MMU_GATHER_BUNDLE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
56
Russell King06824ba2011-02-20 12:16:45 +000057/*
58 * This is unnecessarily complex. There's three ways the TLB shootdown
59 * code is used:
60 * 1. Unmapping a range of vmas. See zap_page_range(), unmap_region().
61 * tlb->fullmm = 0, and tlb_start_vma/tlb_end_vma will be called.
62 * tlb->vma will be non-NULL.
63 * 2. Unmapping all vmas. See exit_mmap().
64 * tlb->fullmm = 1, and tlb_start_vma/tlb_end_vma will be called.
65 * tlb->vma will be non-NULL. Additionally, page tables will be freed.
66 * 3. Unmapping argument pages. See shift_arg_pages().
67 * tlb->fullmm = 0, but tlb_start_vma/tlb_end_vma will not be called.
68 * tlb->vma will be NULL.
69 */
70static inline void tlb_flush(struct mmu_gather *tlb)
71{
72 if (tlb->fullmm || !tlb->vma)
73 flush_tlb_mm(tlb->mm);
74 else if (tlb->range_end > 0) {
75 flush_tlb_range(tlb->vma, tlb->range_start, tlb->range_end);
76 tlb->range_start = TASK_SIZE;
77 tlb->range_end = 0;
78 }
79}
80
81static inline void tlb_add_flush(struct mmu_gather *tlb, unsigned long addr)
82{
83 if (!tlb->fullmm) {
84 if (addr < tlb->range_start)
85 tlb->range_start = addr;
86 if (addr + PAGE_SIZE > tlb->range_end)
87 tlb->range_end = addr + PAGE_SIZE;
88 }
89}
90
Peter Zijlstra9e14f672011-05-24 17:11:53 -070091static inline void __tlb_alloc_page(struct mmu_gather *tlb)
92{
93 unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
94
95 if (addr) {
96 tlb->pages = (void *)addr;
97 tlb->max = PAGE_SIZE / sizeof(struct page *);
98 }
99}
100
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700101static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
Russell King06824ba2011-02-20 12:16:45 +0000102{
103 tlb_flush(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700104}
105
106static inline void tlb_flush_mmu_free(struct mmu_gather *tlb)
107{
Peter Zijlstra29eb7782013-06-05 12:26:50 +0200108 free_pages_and_swap_cache(tlb->pages, tlb->nr);
109 tlb->nr = 0;
110 if (tlb->pages == tlb->local)
111 __tlb_alloc_page(tlb);
Russell King06824ba2011-02-20 12:16:45 +0000112}
113
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700114static inline void tlb_flush_mmu(struct mmu_gather *tlb)
115{
116 tlb_flush_mmu_tlbonly(tlb);
117 tlb_flush_mmu_free(tlb);
118}
119
Peter Zijlstra9e14f672011-05-24 17:11:53 -0700120static inline void
Linus Torvalds2b047252013-08-15 11:42:25 -0700121tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 tlb->mm = mm;
Linus Torvalds2b047252013-08-15 11:42:25 -0700124 tlb->fullmm = !(start | (end+1));
125 tlb->start = start;
126 tlb->end = end;
Russell King06824ba2011-02-20 12:16:45 +0000127 tlb->vma = NULL;
Peter Zijlstra9e14f672011-05-24 17:11:53 -0700128 tlb->max = ARRAY_SIZE(tlb->local);
129 tlb->pages = tlb->local;
Russell King06824ba2011-02-20 12:16:45 +0000130 tlb->nr = 0;
Peter Zijlstra9e14f672011-05-24 17:11:53 -0700131 __tlb_alloc_page(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static inline void
135tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
136{
Russell King06824ba2011-02-20 12:16:45 +0000137 tlb_flush_mmu(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* keep the page table cache within bounds */
140 check_pgt_cache();
Hugh Dickins15a23ff2005-10-29 18:16:01 -0700141
Peter Zijlstra9e14f672011-05-24 17:11:53 -0700142 if (tlb->pages != tlb->local)
143 free_pages((unsigned long)tlb->pages, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Aaro Koskinen7fccfc02009-04-14 13:07:35 +0100146/*
147 * Memorize the range for the TLB flush.
148 */
149static inline void
150tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long addr)
151{
Russell King06824ba2011-02-20 12:16:45 +0000152 tlb_add_flush(tlb, addr);
Aaro Koskinen7fccfc02009-04-14 13:07:35 +0100153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/*
156 * In the case of tlb vma handling, we can optimise these away in the
157 * case where we're doing a full MM flush. When we're doing a munmap,
158 * the vmas are adjusted to only cover the region to be torn down.
159 */
160static inline void
161tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
162{
Aaro Koskinen7fccfc02009-04-14 13:07:35 +0100163 if (!tlb->fullmm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 flush_cache_range(vma, vma->vm_start, vma->vm_end);
Russell King06824ba2011-02-20 12:16:45 +0000165 tlb->vma = vma;
Aaro Koskinen7fccfc02009-04-14 13:07:35 +0100166 tlb->range_start = TASK_SIZE;
167 tlb->range_end = 0;
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static inline void
172tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
173{
Russell King06824ba2011-02-20 12:16:45 +0000174 if (!tlb->fullmm)
175 tlb_flush(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Peter Zijlstra9e14f672011-05-24 17:11:53 -0700178static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
Russell King06824ba2011-02-20 12:16:45 +0000179{
Peter Zijlstra9e14f672011-05-24 17:11:53 -0700180 tlb->pages[tlb->nr++] = page;
181 VM_BUG_ON(tlb->nr > tlb->max);
182 return tlb->max - tlb->nr;
183}
184
185static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
186{
187 if (!__tlb_remove_page(tlb, page))
188 tlb_flush_mmu(tlb);
Russell King06824ba2011-02-20 12:16:45 +0000189}
190
191static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
192 unsigned long addr)
193{
194 pgtable_page_dtor(pte);
Catalin Marinas6d3ec1a2012-01-25 11:54:22 +0100195
Will Deacondf547e02012-08-24 15:23:06 +0100196#ifdef CONFIG_ARM_LPAE
197 tlb_add_flush(tlb, addr);
198#else
Catalin Marinas6d3ec1a2012-01-25 11:54:22 +0100199 /*
200 * With the classic ARM MMU, a pte page has two corresponding pmd
201 * entries, each covering 1MB.
202 */
203 addr &= PMD_MASK;
204 tlb_add_flush(tlb, addr + SZ_1M - PAGE_SIZE);
205 tlb_add_flush(tlb, addr + SZ_1M);
Will Deacondf547e02012-08-24 15:23:06 +0100206#endif
Catalin Marinas6d3ec1a2012-01-25 11:54:22 +0100207
Russell King06824ba2011-02-20 12:16:45 +0000208 tlb_remove_page(tlb, pte);
209}
210
Catalin Marinasc9f27f12011-11-22 17:30:29 +0000211static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
212 unsigned long addr)
213{
214#ifdef CONFIG_ARM_LPAE
215 tlb_add_flush(tlb, addr);
216 tlb_remove_page(tlb, virt_to_page(pmdp));
217#endif
218}
219
Catalin Marinas8d962502012-07-25 14:39:26 +0100220static inline void
221tlb_remove_pmd_tlb_entry(struct mmu_gather *tlb, pmd_t *pmdp, unsigned long addr)
222{
223 tlb_add_flush(tlb, addr);
224}
225
Russell King06824ba2011-02-20 12:16:45 +0000226#define pte_free_tlb(tlb, ptep, addr) __pte_free_tlb(tlb, ptep, addr)
Catalin Marinasc9f27f12011-11-22 17:30:29 +0000227#define pmd_free_tlb(tlb, pmdp, addr) __pmd_free_tlb(tlb, pmdp, addr)
Russell Kinga32618d2011-11-22 17:30:28 +0000228#define pud_free_tlb(tlb, pudp, addr) pud_free((tlb)->mm, pudp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230#define tlb_migrate_finish(mm) do { } while (0)
231
Hyok S. Choi01579032006-02-24 21:41:25 +0000232#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#endif