vserver 2.0 rc7
[linux-2.6.git] / include / asm-ia64 / tlb.h
1 #ifndef _ASM_IA64_TLB_H
2 #define _ASM_IA64_TLB_H
3 /*
4  * Based on <asm-generic/tlb.h>.
5  *
6  * Copyright (C) 2002-2003 Hewlett-Packard Co
7  *      David Mosberger-Tang <davidm@hpl.hp.com>
8  */
9 /*
10  * Removing a translation from a page table (including TLB-shootdown) is a four-step
11  * procedure:
12  *
13  *      (1) Flush (virtual) caches --- ensures virtual memory is coherent with kernel memory
14  *          (this is a no-op on ia64).
15  *      (2) Clear the relevant portions of the page-table
16  *      (3) Flush the TLBs --- ensures that stale content is gone from CPU TLBs
17  *      (4) Release the pages that were freed up in step (2).
18  *
19  * Note that the ordering of these steps is crucial to avoid races on MP machines.
20  *
21  * The Linux kernel defines several platform-specific hooks for TLB-shootdown.  When
22  * unmapping a portion of the virtual address space, these hooks are called according to
23  * the following template:
24  *
25  *      tlb <- tlb_gather_mmu(mm, full_mm_flush);       // start unmap for address space MM
26  *      {
27  *        for each vma that needs a shootdown do {
28  *          tlb_start_vma(tlb, vma);
29  *            for each page-table-entry PTE that needs to be removed do {
30  *              tlb_remove_tlb_entry(tlb, pte, address);
31  *              if (pte refers to a normal page) {
32  *                tlb_remove_page(tlb, page);
33  *              }
34  *            }
35  *          tlb_end_vma(tlb, vma);
36  *        }
37  *      }
38  *      tlb_finish_mmu(tlb, start, end);        // finish unmap for address space MM
39  */
40 #include <linux/config.h>
41 #include <linux/mm.h>
42 #include <linux/pagemap.h>
43 #include <linux/swap.h>
44 #include <linux/vs_memory.h>
45
46 #include <asm/pgalloc.h>
47 #include <asm/processor.h>
48 #include <asm/tlbflush.h>
49 #include <asm/machvec.h>
50
51 #ifdef CONFIG_SMP
52 # define FREE_PTE_NR            2048
53 # define tlb_fast_mode(tlb)     ((tlb)->nr == ~0U)
54 #else
55 # define FREE_PTE_NR            0
56 # define tlb_fast_mode(tlb)     (1)
57 #endif
58
59 struct mmu_gather {
60         struct mm_struct        *mm;
61         unsigned int            nr;             /* == ~0U => fast mode */
62         unsigned char           fullmm;         /* non-zero means full mm flush */
63         unsigned char           need_flush;     /* really unmapped some PTEs? */
64         unsigned long           freed;          /* number of pages freed */
65         unsigned long           start_addr;
66         unsigned long           end_addr;
67         struct page             *pages[FREE_PTE_NR];
68 };
69
70 /* Users of the generic TLB shootdown code must declare this storage space. */
71 DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
72
73 /*
74  * Flush the TLB for address range START to END and, if not in fast mode, release the
75  * freed pages that where gathered up to this point.
76  */
77 static inline void
78 ia64_tlb_flush_mmu (struct mmu_gather *tlb, unsigned long start, unsigned long end)
79 {
80         unsigned int nr;
81
82         if (!tlb->need_flush)
83                 return;
84         tlb->need_flush = 0;
85
86         if (tlb->fullmm) {
87                 /*
88                  * Tearing down the entire address space.  This happens both as a result
89                  * of exit() and execve().  The latter case necessitates the call to
90                  * flush_tlb_mm() here.
91                  */
92                 flush_tlb_mm(tlb->mm);
93         } else if (unlikely (end - start >= 1024*1024*1024*1024UL
94                              || REGION_NUMBER(start) != REGION_NUMBER(end - 1)))
95         {
96                 /*
97                  * If we flush more than a tera-byte or across regions, we're probably
98                  * better off just flushing the entire TLB(s).  This should be very rare
99                  * and is not worth optimizing for.
100                  */
101                 flush_tlb_all();
102         } else {
103                 /*
104                  * XXX fix me: flush_tlb_range() should take an mm pointer instead of a
105                  * vma pointer.
106                  */
107                 struct vm_area_struct vma;
108
109                 vma.vm_mm = tlb->mm;
110                 /* flush the address range from the tlb: */
111                 flush_tlb_range(&vma, start, end);
112                 /* now flush the virt. page-table area mapping the address range: */
113                 flush_tlb_range(&vma, ia64_thash(start), ia64_thash(end));
114         }
115
116         /* lastly, release the freed pages */
117         nr = tlb->nr;
118         if (!tlb_fast_mode(tlb)) {
119                 unsigned long i;
120                 tlb->nr = 0;
121                 tlb->start_addr = ~0UL;
122                 for (i = 0; i < nr; ++i)
123                         free_page_and_swap_cache(tlb->pages[i]);
124         }
125 }
126
127 /*
128  * Return a pointer to an initialized struct mmu_gather.
129  */
130 static inline struct mmu_gather *
131 tlb_gather_mmu (struct mm_struct *mm, unsigned int full_mm_flush)
132 {
133         struct mmu_gather *tlb = &__get_cpu_var(mmu_gathers);
134
135         tlb->mm = mm;
136         /*
137          * Use fast mode if only 1 CPU is online.
138          *
139          * It would be tempting to turn on fast-mode for full_mm_flush as well.  But this
140          * doesn't work because of speculative accesses and software prefetching: the page
141          * table of "mm" may (and usually is) the currently active page table and even
142          * though the kernel won't do any user-space accesses during the TLB shoot down, a
143          * compiler might use speculation or lfetch.fault on what happens to be a valid
144          * user-space address.  This in turn could trigger a TLB miss fault (or a VHPT
145          * walk) and re-insert a TLB entry we just removed.  Slow mode avoids such
146          * problems.  (We could make fast-mode work by switching the current task to a
147          * different "mm" during the shootdown.) --davidm 08/02/2002
148          */
149         tlb->nr = (num_online_cpus() == 1) ? ~0U : 0;
150         tlb->fullmm = full_mm_flush;
151         tlb->freed = 0;
152         tlb->start_addr = ~0UL;
153         return tlb;
154 }
155
156 /*
157  * Called at the end of the shootdown operation to free up any resources that were
158  * collected.  The page table lock is still held at this point.
159  */
160 static inline void
161 tlb_finish_mmu (struct mmu_gather *tlb, unsigned long start, unsigned long end)
162 {
163         unsigned long freed = tlb->freed;
164         struct mm_struct *mm = tlb->mm;
165         unsigned long rss = get_mm_counter(mm, rss);
166
167         if (rss < freed)
168                 freed = rss;
169         add_mm_counter(mm, rss, -freed);
170         /*
171          * Note: tlb->nr may be 0 at this point, so we can't rely on tlb->start_addr and
172          * tlb->end_addr.
173          */
174         ia64_tlb_flush_mmu(tlb, start, end);
175
176         /* keep the page table cache within bounds */
177         check_pgt_cache();
178 }
179
180 static inline unsigned int
181 tlb_is_full_mm(struct mmu_gather *tlb)
182 {
183      return tlb->fullmm;
184 }
185
186 /*
187  * Logically, this routine frees PAGE.  On MP machines, the actual freeing of the page
188  * must be delayed until after the TLB has been flushed (see comments at the beginning of
189  * this file).
190  */
191 static inline void
192 tlb_remove_page (struct mmu_gather *tlb, struct page *page)
193 {
194         tlb->need_flush = 1;
195
196         if (tlb_fast_mode(tlb)) {
197                 free_page_and_swap_cache(page);
198                 return;
199         }
200         tlb->pages[tlb->nr++] = page;
201         if (tlb->nr >= FREE_PTE_NR)
202                 ia64_tlb_flush_mmu(tlb, tlb->start_addr, tlb->end_addr);
203 }
204
205 /*
206  * Remove TLB entry for PTE mapped at virtual address ADDRESS.  This is called for any
207  * PTE, not just those pointing to (normal) physical memory.
208  */
209 static inline void
210 __tlb_remove_tlb_entry (struct mmu_gather *tlb, pte_t *ptep, unsigned long address)
211 {
212         if (tlb->start_addr == ~0UL)
213                 tlb->start_addr = address;
214         tlb->end_addr = address + PAGE_SIZE;
215 }
216
217 #define tlb_migrate_finish(mm)  platform_tlb_migrate_finish(mm)
218
219 #define tlb_start_vma(tlb, vma)                 do { } while (0)
220 #define tlb_end_vma(tlb, vma)                   do { } while (0)
221
222 #define tlb_remove_tlb_entry(tlb, ptep, addr)           \
223 do {                                                    \
224         tlb->need_flush = 1;                            \
225         __tlb_remove_tlb_entry(tlb, ptep, addr);        \
226 } while (0)
227
228 #define pte_free_tlb(tlb, ptep)                         \
229 do {                                                    \
230         tlb->need_flush = 1;                            \
231         __pte_free_tlb(tlb, ptep);                      \
232 } while (0)
233
234 #define pmd_free_tlb(tlb, ptep)                         \
235 do {                                                    \
236         tlb->need_flush = 1;                            \
237         __pmd_free_tlb(tlb, ptep);                      \
238 } while (0)
239
240 #define pud_free_tlb(tlb, pudp)                         \
241 do {                                                    \
242         tlb->need_flush = 1;                            \
243         __pud_free_tlb(tlb, pudp);                      \
244 } while (0)
245
246 #endif /* _ASM_IA64_TLB_H */