blob: 2fcd321040ff3868ee9fee536d27593429bd8c51 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002/*
3 * This file contains the routines for TLB flushing.
4 * On machines where the MMU uses a hash table to store virtual to
5 * physical translations, these routines flush entries from the
6 * hash table also.
7 * -- paulus
8 *
9 * Derived from arch/ppc/mm/init.c:
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11 *
12 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
13 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
14 * Copyright (C) 1996 Paul Mackerras
Paul Mackerras14cf11a2005-09-26 16:04:21 +100015 *
16 * Derived from "arch/i386/mm/init.c"
17 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018 */
19
Paul Mackerras14cf11a2005-09-26 16:04:21 +100020#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/init.h>
23#include <linux/highmem.h>
Mariusz Kozlowski97d22d22007-07-21 04:37:44 -070024#include <linux/pagemap.h>
Paul Gortmaker93087942011-07-29 16:19:31 +100025#include <linux/export.h>
Mariusz Kozlowski97d22d22007-07-21 04:37:44 -070026
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027#include <asm/tlbflush.h>
28#include <asm/tlb.h>
29
Christophe Leroy9d9f2cc2019-03-29 09:59:59 +000030#include <mm/mmu_decl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100031
32/*
33 * Called when unmapping pages to flush entries from the TLB/hash table.
34 */
35void flush_hash_entry(struct mm_struct *mm, pte_t *ptep, unsigned long addr)
36{
37 unsigned long ptephys;
38
Mathieu Malaterred8731522018-04-13 20:41:43 +020039 if (Hash) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040 ptephys = __pa(ptep) & PAGE_MASK;
Paul Mackerras6218a762006-06-11 14:15:17 +100041 flush_hash_pages(mm->context.id, addr, ptephys, 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042 }
43}
Becky Bruce4ee70842008-09-24 11:01:24 -050044EXPORT_SYMBOL(flush_hash_entry);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100045
46/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047 * Called at the end of a mmu_gather operation to make sure the
48 * TLB flush is completely done.
49 */
50void tlb_flush(struct mmu_gather *tlb)
51{
Mathieu Malaterred8731522018-04-13 20:41:43 +020052 if (!Hash) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +100053 /*
54 * 603 needs to flush the whole TLB here since
55 * it doesn't use a hash table.
56 */
57 _tlbia();
58 }
59}
60
61/*
62 * TLB flushing:
63 *
64 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
65 * - flush_tlb_page(vma, vmaddr) flushes one page
66 * - flush_tlb_range(vma, start, end) flushes a range of pages
67 * - flush_tlb_kernel_range(start, end) flushes kernel pages
68 *
69 * since the hardware hash table functions as an extension of the
70 * tlb as far as the linux tables are concerned, flush it too.
71 * -- Cort
72 */
73
Paul Mackerras14cf11a2005-09-26 16:04:21 +100074static void flush_range(struct mm_struct *mm, unsigned long start,
75 unsigned long end)
76{
77 pmd_t *pmd;
78 unsigned long pmd_end;
79 int count;
Paul Mackerras6218a762006-06-11 14:15:17 +100080 unsigned int ctx = mm->context.id;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100081
Mathieu Malaterred8731522018-04-13 20:41:43 +020082 if (!Hash) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +100083 _tlbia();
84 return;
85 }
86 start &= PAGE_MASK;
87 if (start >= end)
88 return;
89 end = (end - 1) | ~PAGE_MASK;
David Gibsonf1a1eb22007-05-09 15:20:37 +100090 pmd = pmd_offset(pud_offset(pgd_offset(mm, start), start), start);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100091 for (;;) {
92 pmd_end = ((start + PGDIR_SIZE) & PGDIR_MASK) - 1;
93 if (pmd_end > end)
94 pmd_end = end;
95 if (!pmd_none(*pmd)) {
96 count = ((pmd_end - start) >> PAGE_SHIFT) + 1;
97 flush_hash_pages(ctx, start, pmd_val(*pmd), count);
98 }
99 if (pmd_end == end)
100 break;
101 start = pmd_end + 1;
102 ++pmd;
103 }
104}
105
106/*
107 * Flush kernel TLB entries in the given range
108 */
109void flush_tlb_kernel_range(unsigned long start, unsigned long end)
110{
111 flush_range(&init_mm, start, end);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000112}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000113EXPORT_SYMBOL(flush_tlb_kernel_range);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000114
115/*
116 * Flush all the (user) entries for the address space described by mm.
117 */
118void flush_tlb_mm(struct mm_struct *mm)
119{
120 struct vm_area_struct *mp;
121
Mathieu Malaterred8731522018-04-13 20:41:43 +0200122 if (!Hash) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000123 _tlbia();
124 return;
125 }
126
Hugh Dickins01edcd82005-11-23 13:37:39 -0800127 /*
128 * It is safe to go down the mm's list of vmas when called
129 * from dup_mmap, holding mmap_sem. It would also be safe from
130 * unmap_region or exit_mmap, but not from vmtruncate on SMP -
131 * but it seems dup_mmap is the only SMP case which gets here.
132 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000133 for (mp = mm->mmap; mp != NULL; mp = mp->vm_next)
134 flush_range(mp->vm_mm, mp->vm_start, mp->vm_end);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000135}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000136EXPORT_SYMBOL(flush_tlb_mm);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000137
138void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
139{
140 struct mm_struct *mm;
141 pmd_t *pmd;
142
Mathieu Malaterred8731522018-04-13 20:41:43 +0200143 if (!Hash) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000144 _tlbie(vmaddr);
145 return;
146 }
147 mm = (vmaddr < TASK_SIZE)? vma->vm_mm: &init_mm;
David Gibsonf1a1eb22007-05-09 15:20:37 +1000148 pmd = pmd_offset(pud_offset(pgd_offset(mm, vmaddr), vmaddr), vmaddr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000149 if (!pmd_none(*pmd))
Paul Mackerras6218a762006-06-11 14:15:17 +1000150 flush_hash_pages(mm->context.id, vmaddr, pmd_val(*pmd), 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000151}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000152EXPORT_SYMBOL(flush_tlb_page);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000153
154/*
155 * For each address in the range, find the pte for the address
156 * and check _PAGE_HASHPTE bit; if it is set, find and destroy
157 * the corresponding HPTE.
158 */
159void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
160 unsigned long end)
161{
162 flush_range(vma->vm_mm, start, end);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000163}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000164EXPORT_SYMBOL(flush_tlb_range);
Dave Kleikamp91b191c2011-07-04 18:38:03 +0000165
166void __init early_init_mmu(void)
167{
168}