blob: 63e277b6e60ccabd540646ce86dfcdb89b9d5990 [file] [log] [blame]
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +10001/*
2 * TLB flush routines for radix kernels.
3 *
4 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/mm.h>
13#include <linux/hugetlb.h>
14#include <linux/memblock.h>
15
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +100016#include <asm/ppc-opcode.h>
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100017#include <asm/tlb.h>
18#include <asm/tlbflush.h>
Balbir Singh04284912017-04-11 15:23:25 +100019#include <asm/trace.h>
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +100020#include <asm/cputhreads.h>
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100021
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053022#define RIC_FLUSH_TLB 0
23#define RIC_FLUSH_PWC 1
24#define RIC_FLUSH_ALL 2
25
26static inline void __tlbiel_pid(unsigned long pid, int set,
27 unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100028{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053029 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100030
31 rb = PPC_BIT(53); /* IS = 1 */
32 rb |= set << PPC_BITLSHIFT(51);
33 rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
34 prs = 1; /* process scoped */
35 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100036
Balbir Singh8cd6d3c2016-07-13 15:05:20 +053037 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100038 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
Balbir Singh04284912017-04-11 15:23:25 +100039 trace_tlbie(0, 1, rb, rs, ric, prs, r);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100040}
41
42/*
43 * We use 128 set in radix mode and 256 set in hpt mode.
44 */
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053045static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100046{
47 int set;
48
Aneesh Kumar K.Vf7327e02017-04-01 20:11:48 +053049 asm volatile("ptesync": : :"memory");
Aneesh Kumar K.Va5998fc2017-04-26 21:38:17 +100050
51 /*
52 * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
53 * also flush the entire Page Walk Cache.
54 */
55 __tlbiel_pid(pid, 0, ric);
56
Benjamin Herrenschmidt5ce5fe12017-07-19 14:49:04 +100057 /* For PWC, only one flush is needed */
58 if (ric == RIC_FLUSH_PWC) {
59 asm volatile("ptesync": : :"memory");
60 return;
61 }
Aneesh Kumar K.Va5998fc2017-04-26 21:38:17 +100062
Benjamin Herrenschmidt5ce5fe12017-07-19 14:49:04 +100063 /* For the remaining sets, just flush the TLB */
Aneesh Kumar K.Va5998fc2017-04-26 21:38:17 +100064 for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
Benjamin Herrenschmidt5ce5fe12017-07-19 14:49:04 +100065 __tlbiel_pid(pid, set, RIC_FLUSH_TLB);
Aneesh Kumar K.Va5998fc2017-04-26 21:38:17 +100066
Aneesh Kumar K.Vf7327e02017-04-01 20:11:48 +053067 asm volatile("ptesync": : :"memory");
Benjamin Herrenschmidt90c1e3c2017-02-06 13:05:16 +110068 asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100069}
70
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053071static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100072{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053073 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100074
75 rb = PPC_BIT(53); /* IS = 1 */
76 rs = pid << PPC_BITLSHIFT(31);
77 prs = 1; /* process scoped */
78 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100079
80 asm volatile("ptesync": : :"memory");
Balbir Singh8cd6d3c2016-07-13 15:05:20 +053081 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100082 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
83 asm volatile("eieio; tlbsync; ptesync": : :"memory");
Balbir Singh04284912017-04-11 15:23:25 +100084 trace_tlbie(0, 0, rb, rs, ric, prs, r);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100085}
86
87static inline void _tlbiel_va(unsigned long va, unsigned long pid,
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053088 unsigned long ap, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100089{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053090 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100091
92 rb = va & ~(PPC_BITMASK(52, 63));
93 rb |= ap << PPC_BITLSHIFT(58);
94 rs = pid << PPC_BITLSHIFT(31);
95 prs = 1; /* process scoped */
96 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100097
98 asm volatile("ptesync": : :"memory");
Balbir Singh8cd6d3c2016-07-13 15:05:20 +053099 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000100 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
101 asm volatile("ptesync": : :"memory");
Balbir Singh04284912017-04-11 15:23:25 +1000102 trace_tlbie(0, 1, rb, rs, ric, prs, r);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000103}
104
105static inline void _tlbie_va(unsigned long va, unsigned long pid,
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530106 unsigned long ap, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000107{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530108 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000109
110 rb = va & ~(PPC_BITMASK(52, 63));
111 rb |= ap << PPC_BITLSHIFT(58);
112 rs = pid << PPC_BITLSHIFT(31);
113 prs = 1; /* process scoped */
114 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000115
116 asm volatile("ptesync": : :"memory");
Balbir Singh8cd6d3c2016-07-13 15:05:20 +0530117 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000118 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
119 asm volatile("eieio; tlbsync; ptesync": : :"memory");
Balbir Singh04284912017-04-11 15:23:25 +1000120 trace_tlbie(0, 0, rb, rs, ric, prs, r);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000121}
122
123/*
124 * Base TLB flushing operations:
125 *
126 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
127 * - flush_tlb_page(vma, vmaddr) flushes one page
128 * - flush_tlb_range(vma, start, end) flushes a range of pages
129 * - flush_tlb_kernel_range(start, end) flushes kernel pages
130 *
131 * - local_* variants of page and mm only apply to the current
132 * processor
133 */
134void radix__local_flush_tlb_mm(struct mm_struct *mm)
135{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530136 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000137
138 preempt_disable();
139 pid = mm->context.id;
140 if (pid != MMU_NO_CONTEXT)
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000141 _tlbiel_pid(pid, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000142 preempt_enable();
143}
144EXPORT_SYMBOL(radix__local_flush_tlb_mm);
145
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000146#ifndef CONFIG_SMP
Frederic Barrat61102362017-09-03 20:15:12 +0200147void radix__local_flush_all_mm(struct mm_struct *mm)
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530148{
149 unsigned long pid;
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530150
151 preempt_disable();
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530152 pid = mm->context.id;
153 if (pid != MMU_NO_CONTEXT)
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000154 _tlbiel_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530155 preempt_enable();
156}
Frederic Barrat61102362017-09-03 20:15:12 +0200157EXPORT_SYMBOL(radix__local_flush_all_mm);
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000158#endif /* CONFIG_SMP */
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530159
Aneesh Kumar K.Vf22dfc92016-07-13 15:06:41 +0530160void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
Aneesh Kumar K.Vfbfa26d2016-07-13 15:06:42 +0530161 int psize)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000162{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530163 unsigned long pid;
Aneesh Kumar K.Vfbfa26d2016-07-13 15:06:42 +0530164 unsigned long ap = mmu_get_ap(psize);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000165
166 preempt_disable();
Michael Ellerman67730272017-10-16 12:41:00 +0530167 pid = mm->context.id;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000168 if (pid != MMU_NO_CONTEXT)
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530169 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000170 preempt_enable();
171}
172
173void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
174{
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000175#ifdef CONFIG_HUGETLB_PAGE
176 /* need the return fix for nohash.c */
Michael Ellerman67730272017-10-16 12:41:00 +0530177 if (is_vm_hugetlb_page(vma))
178 return radix__local_flush_hugetlb_page(vma, vmaddr);
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000179#endif
Michael Ellerman67730272017-10-16 12:41:00 +0530180 radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000181}
182EXPORT_SYMBOL(radix__local_flush_tlb_page);
183
184#ifdef CONFIG_SMP
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000185void radix__flush_tlb_mm(struct mm_struct *mm)
186{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530187 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000188
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000189 pid = mm->context.id;
190 if (unlikely(pid == MMU_NO_CONTEXT))
Nicholas Piggindffe8442017-10-24 23:06:53 +1000191 return;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000192
Nicholas Piggindffe8442017-10-24 23:06:53 +1000193 preempt_disable();
Michael Ellerman3c9ac2b2017-05-02 21:00:14 +1000194 if (!mm_is_thread_local(mm))
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000195 _tlbie_pid(pid, RIC_FLUSH_TLB);
196 else
197 _tlbiel_pid(pid, RIC_FLUSH_TLB);
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000198 preempt_enable();
199}
200EXPORT_SYMBOL(radix__flush_tlb_mm);
201
Frederic Barrat61102362017-09-03 20:15:12 +0200202void radix__flush_all_mm(struct mm_struct *mm)
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000203{
204 unsigned long pid;
205
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000206 pid = mm->context.id;
207 if (unlikely(pid == MMU_NO_CONTEXT))
Nicholas Piggindffe8442017-10-24 23:06:53 +1000208 return;
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000209
Nicholas Piggindffe8442017-10-24 23:06:53 +1000210 preempt_disable();
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000211 if (!mm_is_thread_local(mm))
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530212 _tlbie_pid(pid, RIC_FLUSH_ALL);
Michael Ellerman3c9ac2b2017-05-02 21:00:14 +1000213 else
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530214 _tlbiel_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000215 preempt_enable();
216}
Frederic Barrat61102362017-09-03 20:15:12 +0200217EXPORT_SYMBOL(radix__flush_all_mm);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000218
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530219void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
220{
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000221 tlb->need_flush_all = 1;
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530222}
223EXPORT_SYMBOL(radix__flush_tlb_pwc);
224
Aneesh Kumar K.Vf22dfc92016-07-13 15:06:41 +0530225void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
Aneesh Kumar K.Vfbfa26d2016-07-13 15:06:42 +0530226 int psize)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000227{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530228 unsigned long pid;
Aneesh Kumar K.Vfbfa26d2016-07-13 15:06:42 +0530229 unsigned long ap = mmu_get_ap(psize);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000230
Michael Ellerman67730272017-10-16 12:41:00 +0530231 pid = mm->context.id;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000232 if (unlikely(pid == MMU_NO_CONTEXT))
Nicholas Piggindffe8442017-10-24 23:06:53 +1000233 return;
234
235 preempt_disable();
Michael Ellerman3c9ac2b2017-05-02 21:00:14 +1000236 if (!mm_is_thread_local(mm))
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530237 _tlbie_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Michael Ellerman3c9ac2b2017-05-02 21:00:14 +1000238 else
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530239 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000240 preempt_enable();
241}
242
243void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
244{
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000245#ifdef CONFIG_HUGETLB_PAGE
Michael Ellerman67730272017-10-16 12:41:00 +0530246 if (is_vm_hugetlb_page(vma))
247 return radix__flush_hugetlb_page(vma, vmaddr);
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000248#endif
Michael Ellerman67730272017-10-16 12:41:00 +0530249 radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000250}
251EXPORT_SYMBOL(radix__flush_tlb_page);
252
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000253#else /* CONFIG_SMP */
254#define radix__flush_all_mm radix__local_flush_all_mm
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000255#endif /* CONFIG_SMP */
256
257void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
258{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530259 _tlbie_pid(0, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000260}
261EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
262
263/*
264 * Currently, for range flushing, we just do a full mm flush. Because
265 * we use this in code path where we don' track the page size.
266 */
267void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
268 unsigned long end)
269
270{
271 struct mm_struct *mm = vma->vm_mm;
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000272
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000273 radix__flush_tlb_mm(mm);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000274}
275EXPORT_SYMBOL(radix__flush_tlb_range);
276
Aneesh Kumar K.V912cc872016-07-13 15:05:29 +0530277static int radix_get_mmu_psize(int page_size)
278{
279 int psize;
280
281 if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
282 psize = mmu_virtual_psize;
283 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
284 psize = MMU_PAGE_2M;
285 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
286 psize = MMU_PAGE_1G;
287 else
288 return -1;
289 return psize;
290}
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000291
292void radix__tlb_flush(struct mmu_gather *tlb)
293{
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530294 int psize = 0;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000295 struct mm_struct *mm = tlb->mm;
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530296 int page_size = tlb->page_size;
297
298 psize = radix_get_mmu_psize(page_size);
299 /*
300 * if page size is not something we understand, do a full mm flush
Nicholas Piggin30b49ec2017-10-24 23:06:54 +1000301 *
302 * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
303 * that flushes the process table entry cache upon process teardown.
304 * See the comment for radix in arch_exit_mmap().
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530305 */
306 if (psize != -1 && !tlb->fullmm && !tlb->need_flush_all)
307 radix__flush_tlb_range_psize(mm, tlb->start, tlb->end, psize);
Nicholas Piggin30b49ec2017-10-24 23:06:54 +1000308 else if (tlb->fullmm || tlb->need_flush_all) {
Benjamin Herrenschmidta46cc7a2017-07-19 14:49:05 +1000309 tlb->need_flush_all = 0;
310 radix__flush_all_mm(mm);
311 } else
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530312 radix__flush_tlb_mm(mm);
313}
314
315#define TLB_FLUSH_ALL -1UL
316/*
317 * Number of pages above which we will do a bcast tlbie. Just a
318 * number at this point copied from x86
319 */
320static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
321
322void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
323 unsigned long end, int psize)
324{
325 unsigned long pid;
326 unsigned long addr;
Nicholas Piggindffe8442017-10-24 23:06:53 +1000327 bool local;
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530328 unsigned long ap = mmu_get_ap(psize);
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530329 unsigned long page_size = 1UL << mmu_psize_defs[psize].shift;
330
Michael Ellerman67730272017-10-16 12:41:00 +0530331 pid = mm->context.id;
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530332 if (unlikely(pid == MMU_NO_CONTEXT))
Nicholas Piggindffe8442017-10-24 23:06:53 +1000333 return;
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530334
Nicholas Piggindffe8442017-10-24 23:06:53 +1000335 preempt_disable();
336 local = mm_is_thread_local(mm);
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530337 if (end == TLB_FLUSH_ALL ||
338 (end - start) > tlb_single_page_flush_ceiling * page_size) {
339 if (local)
340 _tlbiel_pid(pid, RIC_FLUSH_TLB);
341 else
342 _tlbie_pid(pid, RIC_FLUSH_TLB);
Nicholas Piggindffe8442017-10-24 23:06:53 +1000343 } else {
344 for (addr = start; addr < end; addr += page_size) {
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530345
Nicholas Piggindffe8442017-10-24 23:06:53 +1000346 if (local)
347 _tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
348 else
349 _tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
350 }
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530351 }
Aneesh Kumar K.V8cb81402016-07-13 15:06:35 +0530352 preempt_enable();
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000353}
Aneesh Kumar K.V912cc872016-07-13 15:05:29 +0530354
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000355#ifdef CONFIG_TRANSPARENT_HUGEPAGE
356void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
357{
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000358 unsigned long ap = mmu_get_ap(mmu_virtual_psize);
359 unsigned long pid, end;
Nicholas Piggindffe8442017-10-24 23:06:53 +1000360 bool local;
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000361
Michael Ellerman67730272017-10-16 12:41:00 +0530362 pid = mm->context.id;
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000363 if (unlikely(pid == MMU_NO_CONTEXT))
Nicholas Piggindffe8442017-10-24 23:06:53 +1000364 return;
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000365
366 /* 4k page size, just blow the world */
367 if (PAGE_SIZE == 0x1000) {
368 radix__flush_all_mm(mm);
369 return;
370 }
371
Nicholas Piggindffe8442017-10-24 23:06:53 +1000372 preempt_disable();
373 local = mm_is_thread_local(mm);
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000374 /* Otherwise first do the PWC */
375 if (local)
376 _tlbiel_pid(pid, RIC_FLUSH_PWC);
377 else
378 _tlbie_pid(pid, RIC_FLUSH_PWC);
379
380 /* Then iterate the pages */
381 end = addr + HPAGE_PMD_SIZE;
382 for (; addr < end; addr += PAGE_SIZE) {
383 if (local)
384 _tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
385 else
386 _tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
387 }
Nicholas Piggindffe8442017-10-24 23:06:53 +1000388
Benjamin Herrenschmidt424de9c2017-07-19 14:49:06 +1000389 preempt_enable();
390}
391#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
392
Aneesh Kumar K.V912cc872016-07-13 15:05:29 +0530393void radix__flush_tlb_lpid_va(unsigned long lpid, unsigned long gpa,
394 unsigned long page_size)
395{
396 unsigned long rb,rs,prs,r;
397 unsigned long ap;
398 unsigned long ric = RIC_FLUSH_TLB;
399
400 ap = mmu_get_ap(radix_get_mmu_psize(page_size));
401 rb = gpa & ~(PPC_BITMASK(52, 63));
402 rb |= ap << PPC_BITLSHIFT(58);
403 rs = lpid & ((1UL << 32) - 1);
404 prs = 0; /* process scoped */
405 r = 1; /* raidx format */
406
407 asm volatile("ptesync": : :"memory");
408 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
409 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
410 asm volatile("eieio; tlbsync; ptesync": : :"memory");
Balbir Singh04284912017-04-11 15:23:25 +1000411 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
Aneesh Kumar K.V912cc872016-07-13 15:05:29 +0530412}
413EXPORT_SYMBOL(radix__flush_tlb_lpid_va);
414
415void radix__flush_tlb_lpid(unsigned long lpid)
416{
417 unsigned long rb,rs,prs,r;
418 unsigned long ric = RIC_FLUSH_ALL;
419
420 rb = 0x2 << PPC_BITLSHIFT(53); /* IS = 2 */
421 rs = lpid & ((1UL << 32) - 1);
422 prs = 0; /* partition scoped */
423 r = 1; /* raidx format */
424
425 asm volatile("ptesync": : :"memory");
426 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
427 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
428 asm volatile("eieio; tlbsync; ptesync": : :"memory");
Balbir Singh04284912017-04-11 15:23:25 +1000429 trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
Aneesh Kumar K.V912cc872016-07-13 15:05:29 +0530430}
431EXPORT_SYMBOL(radix__flush_tlb_lpid);
Aneesh Kumar K.Vd8e91e92016-07-13 15:06:40 +0530432
433void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
434 unsigned long start, unsigned long end)
435{
436 radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
437}
438EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
Aneesh Kumar K.Vbe34d302016-08-23 16:27:48 +0530439
440void radix__flush_tlb_all(void)
441{
442 unsigned long rb,prs,r,rs;
443 unsigned long ric = RIC_FLUSH_ALL;
444
445 rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
446 prs = 0; /* partition scoped */
447 r = 1; /* raidx format */
448 rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
449
450 asm volatile("ptesync": : :"memory");
451 /*
452 * now flush guest entries by passing PRS = 1 and LPID != 0
453 */
454 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
455 : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
Balbir Singh04284912017-04-11 15:23:25 +1000456 trace_tlbie(0, 0, rb, rs, ric, prs, r);
Aneesh Kumar K.Vbe34d302016-08-23 16:27:48 +0530457 /*
458 * now flush host entires by passing PRS = 0 and LPID == 0
459 */
460 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
461 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
462 asm volatile("eieio; tlbsync; ptesync": : :"memory");
Balbir Singh04284912017-04-11 15:23:25 +1000463 trace_tlbie(0, 0, rb, 0, ric, prs, r);
Aneesh Kumar K.Vbe34d302016-08-23 16:27:48 +0530464}
Aneesh Kumar K.V6d3a0372016-11-28 11:47:01 +0530465
466void radix__flush_tlb_pte_p9_dd1(unsigned long old_pte, struct mm_struct *mm,
467 unsigned long address)
468{
469 /*
470 * We track page size in pte only for DD1, So we can
471 * call this only on DD1.
472 */
473 if (!cpu_has_feature(CPU_FTR_POWER9_DD1)) {
474 VM_WARN_ON(1);
475 return;
476 }
477
Aneesh Kumar K.Vddb014b2017-03-21 22:59:54 +0530478 if (old_pte & R_PAGE_LARGE)
Aneesh Kumar K.V6d3a0372016-11-28 11:47:01 +0530479 radix__flush_tlb_page_psize(mm, address, MMU_PAGE_2M);
480 else
481 radix__flush_tlb_page_psize(mm, address, mmu_virtual_psize);
482}
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +1000483
484#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
485extern void radix_kvm_prefetch_workaround(struct mm_struct *mm)
486{
487 unsigned int pid = mm->context.id;
488
489 if (unlikely(pid == MMU_NO_CONTEXT))
490 return;
491
492 /*
493 * If this context hasn't run on that CPU before and KVM is
494 * around, there's a slim chance that the guest on another
495 * CPU just brought in obsolete translation into the TLB of
496 * this CPU due to a bad prefetch using the guest PID on
497 * the way into the hypervisor.
498 *
499 * We work around this here. If KVM is possible, we check if
500 * any sibling thread is in KVM. If it is, the window may exist
501 * and thus we flush that PID from the core.
502 *
503 * A potential future improvement would be to mark which PIDs
504 * have never been used on the system and avoid it if the PID
505 * is new and the process has no other cpumask bit set.
506 */
507 if (cpu_has_feature(CPU_FTR_HVMODE) && radix_enabled()) {
508 int cpu = smp_processor_id();
509 int sib = cpu_first_thread_sibling(cpu);
510 bool flush = false;
511
512 for (; sib <= cpu_last_thread_sibling(cpu) && !flush; sib++) {
513 if (sib == cpu)
514 continue;
515 if (paca[sib].kvm_hstate.kvm_vcpu)
516 flush = true;
517 }
518 if (flush)
519 _tlbiel_pid(pid, RIC_FLUSH_ALL);
520 }
521}
522EXPORT_SYMBOL_GPL(radix_kvm_prefetch_workaround);
523#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */