blob: e1f22700fb169941b869c1c085c7b284df9d078a [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>
Balbir Singh8cd6d3c2016-07-13 15:05:20 +053015#include <asm/ppc-opcode.h>
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100016
17#include <asm/tlb.h>
18#include <asm/tlbflush.h>
19
20static DEFINE_RAW_SPINLOCK(native_tlbie_lock);
21
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
37 asm volatile("ptesync": : :"memory");
Balbir Singh8cd6d3c2016-07-13 15:05:20 +053038 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100039 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
40 asm volatile("ptesync": : :"memory");
41}
42
43/*
44 * We use 128 set in radix mode and 256 set in hpt mode.
45 */
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053046static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100047{
48 int set;
49
50 for (set = 0; set < POWER9_TLB_SETS_RADIX ; set++) {
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053051 __tlbiel_pid(pid, set, ric);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100052 }
53 return;
54}
55
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053056static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100057{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053058 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100059
60 rb = PPC_BIT(53); /* IS = 1 */
61 rs = pid << PPC_BITLSHIFT(31);
62 prs = 1; /* process scoped */
63 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100064
65 asm volatile("ptesync": : :"memory");
Balbir Singh8cd6d3c2016-07-13 15:05:20 +053066 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100067 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
68 asm volatile("eieio; tlbsync; ptesync": : :"memory");
69}
70
71static inline void _tlbiel_va(unsigned long va, unsigned long pid,
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053072 unsigned long ap, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100073{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053074 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100075
76 rb = va & ~(PPC_BITMASK(52, 63));
77 rb |= ap << PPC_BITLSHIFT(58);
78 rs = pid << PPC_BITLSHIFT(31);
79 prs = 1; /* process scoped */
80 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100081
82 asm volatile("ptesync": : :"memory");
Balbir Singh8cd6d3c2016-07-13 15:05:20 +053083 asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100084 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
85 asm volatile("ptesync": : :"memory");
86}
87
88static inline void _tlbie_va(unsigned long va, unsigned long pid,
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053089 unsigned long ap, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100090{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053091 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100092
93 rb = va & ~(PPC_BITMASK(52, 63));
94 rb |= ap << PPC_BITLSHIFT(58);
95 rs = pid << PPC_BITLSHIFT(31);
96 prs = 1; /* process scoped */
97 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100098
99 asm volatile("ptesync": : :"memory");
Balbir Singh8cd6d3c2016-07-13 15:05:20 +0530100 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000101 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
102 asm volatile("eieio; tlbsync; ptesync": : :"memory");
103}
104
105/*
106 * Base TLB flushing operations:
107 *
108 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
109 * - flush_tlb_page(vma, vmaddr) flushes one page
110 * - flush_tlb_range(vma, start, end) flushes a range of pages
111 * - flush_tlb_kernel_range(start, end) flushes kernel pages
112 *
113 * - local_* variants of page and mm only apply to the current
114 * processor
115 */
116void radix__local_flush_tlb_mm(struct mm_struct *mm)
117{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530118 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000119
120 preempt_disable();
121 pid = mm->context.id;
122 if (pid != MMU_NO_CONTEXT)
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530123 _tlbiel_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000124 preempt_enable();
125}
126EXPORT_SYMBOL(radix__local_flush_tlb_mm);
127
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530128void radix__local_flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
129{
130 unsigned long pid;
131 struct mm_struct *mm = tlb->mm;
132
133 preempt_disable();
134
135 pid = mm->context.id;
136 if (pid != MMU_NO_CONTEXT)
137 _tlbiel_pid(pid, RIC_FLUSH_PWC);
138
139 preempt_enable();
140}
141EXPORT_SYMBOL(radix__local_flush_tlb_pwc);
142
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000143void radix___local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
144 unsigned long ap, int nid)
145{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530146 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000147
148 preempt_disable();
149 pid = mm ? mm->context.id : 0;
150 if (pid != MMU_NO_CONTEXT)
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530151 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000152 preempt_enable();
153}
154
155void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
156{
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000157#ifdef CONFIG_HUGETLB_PAGE
158 /* need the return fix for nohash.c */
159 if (vma && is_vm_hugetlb_page(vma))
160 return __local_flush_hugetlb_page(vma, vmaddr);
161#endif
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000162 radix___local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
163 mmu_get_ap(mmu_virtual_psize), 0);
164}
165EXPORT_SYMBOL(radix__local_flush_tlb_page);
166
167#ifdef CONFIG_SMP
168static int mm_is_core_local(struct mm_struct *mm)
169{
170 return cpumask_subset(mm_cpumask(mm),
171 topology_sibling_cpumask(smp_processor_id()));
172}
173
174void radix__flush_tlb_mm(struct mm_struct *mm)
175{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530176 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000177
178 preempt_disable();
179 pid = mm->context.id;
180 if (unlikely(pid == MMU_NO_CONTEXT))
181 goto no_context;
182
183 if (!mm_is_core_local(mm)) {
184 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
185
186 if (lock_tlbie)
187 raw_spin_lock(&native_tlbie_lock);
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530188 _tlbie_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000189 if (lock_tlbie)
190 raw_spin_unlock(&native_tlbie_lock);
191 } else
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530192 _tlbiel_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000193no_context:
194 preempt_enable();
195}
196EXPORT_SYMBOL(radix__flush_tlb_mm);
197
Aneesh Kumar K.Va145abf2016-06-08 19:55:51 +0530198void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
199{
200 unsigned long pid;
201 struct mm_struct *mm = tlb->mm;
202
203 preempt_disable();
204
205 pid = mm->context.id;
206 if (unlikely(pid == MMU_NO_CONTEXT))
207 goto no_context;
208
209 if (!mm_is_core_local(mm)) {
210 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
211
212 if (lock_tlbie)
213 raw_spin_lock(&native_tlbie_lock);
214 _tlbie_pid(pid, RIC_FLUSH_PWC);
215 if (lock_tlbie)
216 raw_spin_unlock(&native_tlbie_lock);
217 } else
218 _tlbiel_pid(pid, RIC_FLUSH_PWC);
219no_context:
220 preempt_enable();
221}
222EXPORT_SYMBOL(radix__flush_tlb_pwc);
223
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000224void radix___flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
225 unsigned long ap, int nid)
226{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530227 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000228
229 preempt_disable();
230 pid = mm ? mm->context.id : 0;
231 if (unlikely(pid == MMU_NO_CONTEXT))
232 goto bail;
233 if (!mm_is_core_local(mm)) {
234 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
235
236 if (lock_tlbie)
237 raw_spin_lock(&native_tlbie_lock);
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530238 _tlbie_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000239 if (lock_tlbie)
240 raw_spin_unlock(&native_tlbie_lock);
241 } else
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530242 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000243bail:
244 preempt_enable();
245}
246
247void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
248{
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000249#ifdef CONFIG_HUGETLB_PAGE
250 if (vma && is_vm_hugetlb_page(vma))
251 return flush_hugetlb_page(vma, vmaddr);
252#endif
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000253 radix___flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
254 mmu_get_ap(mmu_virtual_psize), 0);
255}
256EXPORT_SYMBOL(radix__flush_tlb_page);
257
258#endif /* CONFIG_SMP */
259
260void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
261{
262 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
263
264 if (lock_tlbie)
265 raw_spin_lock(&native_tlbie_lock);
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530266 _tlbie_pid(0, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000267 if (lock_tlbie)
268 raw_spin_unlock(&native_tlbie_lock);
269}
270EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
271
272/*
273 * Currently, for range flushing, we just do a full mm flush. Because
274 * we use this in code path where we don' track the page size.
275 */
276void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
277 unsigned long end)
278
279{
280 struct mm_struct *mm = vma->vm_mm;
281 radix__flush_tlb_mm(mm);
282}
283EXPORT_SYMBOL(radix__flush_tlb_range);
284
Aneesh Kumar K.V912cc872016-07-13 15:05:29 +0530285static int radix_get_mmu_psize(int page_size)
286{
287 int psize;
288
289 if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
290 psize = mmu_virtual_psize;
291 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
292 psize = MMU_PAGE_2M;
293 else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
294 psize = MMU_PAGE_1G;
295 else
296 return -1;
297 return psize;
298}
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000299
300void radix__tlb_flush(struct mmu_gather *tlb)
301{
302 struct mm_struct *mm = tlb->mm;
303 radix__flush_tlb_mm(mm);
304}
Aneesh Kumar K.V912cc872016-07-13 15:05:29 +0530305
306void radix__flush_tlb_lpid_va(unsigned long lpid, unsigned long gpa,
307 unsigned long page_size)
308{
309 unsigned long rb,rs,prs,r;
310 unsigned long ap;
311 unsigned long ric = RIC_FLUSH_TLB;
312
313 ap = mmu_get_ap(radix_get_mmu_psize(page_size));
314 rb = gpa & ~(PPC_BITMASK(52, 63));
315 rb |= ap << PPC_BITLSHIFT(58);
316 rs = lpid & ((1UL << 32) - 1);
317 prs = 0; /* process scoped */
318 r = 1; /* raidx format */
319
320 asm volatile("ptesync": : :"memory");
321 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
322 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
323 asm volatile("eieio; tlbsync; ptesync": : :"memory");
324}
325EXPORT_SYMBOL(radix__flush_tlb_lpid_va);
326
327void radix__flush_tlb_lpid(unsigned long lpid)
328{
329 unsigned long rb,rs,prs,r;
330 unsigned long ric = RIC_FLUSH_ALL;
331
332 rb = 0x2 << PPC_BITLSHIFT(53); /* IS = 2 */
333 rs = lpid & ((1UL << 32) - 1);
334 prs = 0; /* partition scoped */
335 r = 1; /* raidx format */
336
337 asm volatile("ptesync": : :"memory");
338 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
339 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
340 asm volatile("eieio; tlbsync; ptesync": : :"memory");
341}
342EXPORT_SYMBOL(radix__flush_tlb_lpid);