ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / mm / tlb.c
1 /*
2  * This file contains the routines for TLB flushing.
3  * On machines where the MMU uses a hash table to store virtual to
4  * physical translations, these routines flush entries from the
5  * hash table also.
6  *  -- paulus
7  *
8  *  Derived from arch/ppc/mm/init.c:
9  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
10  *
11  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
12  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
13  *    Copyright (C) 1996 Paul Mackerras
14  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
15  *
16  *  Derived from "arch/i386/mm/init.c"
17  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
18  *
19  *  This program is free software; you can redistribute it and/or
20  *  modify it under the terms of the GNU General Public License
21  *  as published by the Free Software Foundation; either version
22  *  2 of the License, or (at your option) any later version.
23  *
24  */
25
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/init.h>
30 #include <linux/highmem.h>
31 #include <asm/tlbflush.h>
32 #include <asm/tlb.h>
33
34 #include "mmu_decl.h"
35
36 /*
37  * Called when unmapping pages to flush entries from the TLB/hash table.
38  */
39 void flush_hash_entry(struct mm_struct *mm, pte_t *ptep, unsigned long addr)
40 {
41         unsigned long ptephys;
42
43         if (Hash != 0) {
44                 ptephys = __pa(ptep) & PAGE_MASK;
45                 flush_hash_pages(mm->context, addr, ptephys, 1);
46         }
47 }
48
49 /*
50  * Called by ptep_test_and_clear_young()
51  */
52 void flush_hash_one_pte(pte_t *ptep)
53 {
54         struct page *ptepage;
55         struct mm_struct *mm;
56         unsigned long ptephys;
57         unsigned long addr;
58
59         if (Hash == 0)
60                 return;
61         
62         ptepage = virt_to_page(ptep);
63         mm = (struct mm_struct *) ptepage->mapping;
64         ptephys = __pa(ptep) & PAGE_MASK;
65         addr = ptepage->index + (((unsigned long)ptep & ~PAGE_MASK) << 9);
66         flush_hash_pages(mm->context, addr, ptephys, 1);
67 }
68
69 /*
70  * Called at the end of a mmu_gather operation to make sure the
71  * TLB flush is completely done.
72  */
73 void tlb_flush(struct mmu_gather *tlb)
74 {
75         if (Hash == 0) {
76                 /*
77                  * 603 needs to flush the whole TLB here since
78                  * it doesn't use a hash table.
79                  */
80                 _tlbia();
81         }
82 }
83
84 /*
85  * TLB flushing:
86  *
87  *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
88  *  - flush_tlb_page(vma, vmaddr) flushes one page
89  *  - flush_tlb_range(vma, start, end) flushes a range of pages
90  *  - flush_tlb_kernel_range(start, end) flushes kernel pages
91  *
92  * since the hardware hash table functions as an extension of the
93  * tlb as far as the linux tables are concerned, flush it too.
94  *    -- Cort
95  */
96
97 /*
98  * 750 SMP is a Bad Idea because the 750 doesn't broadcast all
99  * the cache operations on the bus.  Hence we need to use an IPI
100  * to get the other CPU(s) to invalidate their TLBs.
101  */
102 #ifdef CONFIG_SMP_750
103 #define FINISH_FLUSH    smp_send_tlb_invalidate(0)
104 #else
105 #define FINISH_FLUSH    do { } while (0)
106 #endif
107
108 static void flush_range(struct mm_struct *mm, unsigned long start,
109                         unsigned long end)
110 {
111         pmd_t *pmd;
112         unsigned long pmd_end;
113         int count;
114         unsigned int ctx = mm->context;
115
116         if (Hash == 0) {
117                 _tlbia();
118                 return;
119         }
120         start &= PAGE_MASK;
121         if (start >= end)
122                 return;
123         end = (end - 1) | ~PAGE_MASK;
124         pmd = pmd_offset(pgd_offset(mm, start), start);
125         for (;;) {
126                 pmd_end = ((start + PGDIR_SIZE) & PGDIR_MASK) - 1;
127                 if (pmd_end > end)
128                         pmd_end = end;
129                 if (!pmd_none(*pmd)) {
130                         count = ((pmd_end - start) >> PAGE_SHIFT) + 1;
131                         flush_hash_pages(ctx, start, pmd_val(*pmd), count);
132                 }
133                 if (pmd_end == end)
134                         break;
135                 start = pmd_end + 1;
136                 ++pmd;
137         }
138 }
139
140 /*
141  * Flush kernel TLB entries in the given range
142  */
143 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
144 {
145         flush_range(&init_mm, start, end);
146         FINISH_FLUSH;
147 }
148
149 /*
150  * Flush all the (user) entries for the address space described by mm.
151  */
152 void flush_tlb_mm(struct mm_struct *mm)
153 {
154         struct vm_area_struct *mp;
155
156         if (Hash == 0) {
157                 _tlbia();
158                 return;
159         }
160
161         for (mp = mm->mmap; mp != NULL; mp = mp->vm_next)
162                 flush_range(mp->vm_mm, mp->vm_start, mp->vm_end);
163         FINISH_FLUSH;
164 }
165
166 void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
167 {
168         struct mm_struct *mm;
169         pmd_t *pmd;
170
171         if (Hash == 0) {
172                 _tlbie(vmaddr);
173                 return;
174         }
175         mm = (vmaddr < TASK_SIZE)? vma->vm_mm: &init_mm;
176         pmd = pmd_offset(pgd_offset(mm, vmaddr), vmaddr);
177         if (!pmd_none(*pmd))
178                 flush_hash_pages(mm->context, vmaddr, pmd_val(*pmd), 1);
179         FINISH_FLUSH;
180 }
181
182 /*
183  * For each address in the range, find the pte for the address
184  * and check _PAGE_HASHPTE bit; if it is set, find and destroy
185  * the corresponding HPTE.
186  */
187 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
188                      unsigned long end)
189 {
190         flush_range(vma->vm_mm, start, end);
191         FINISH_FLUSH;
192 }