blob: 71083621884ebeb49e731cb931ba446d53f02fa6 [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
16#include <asm/tlb.h>
17#include <asm/tlbflush.h>
18
19static DEFINE_RAW_SPINLOCK(native_tlbie_lock);
20
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053021#define RIC_FLUSH_TLB 0
22#define RIC_FLUSH_PWC 1
23#define RIC_FLUSH_ALL 2
24
25static inline void __tlbiel_pid(unsigned long pid, int set,
26 unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100027{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053028 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100029
30 rb = PPC_BIT(53); /* IS = 1 */
31 rb |= set << PPC_BITLSHIFT(51);
32 rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
33 prs = 1; /* process scoped */
34 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100035
36 asm volatile("ptesync": : :"memory");
37 asm volatile(".long 0x7c000224 | (%0 << 11) | (%1 << 16) |"
38 "(%2 << 17) | (%3 << 18) | (%4 << 21)"
39 : : "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");
66 asm volatile(".long 0x7c000264 | (%0 << 11) | (%1 << 16) |"
67 "(%2 << 17) | (%3 << 18) | (%4 << 21)"
68 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
69 asm volatile("eieio; tlbsync; ptesync": : :"memory");
70}
71
72static inline void _tlbiel_va(unsigned long va, unsigned long pid,
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053073 unsigned long ap, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100074{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053075 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100076
77 rb = va & ~(PPC_BITMASK(52, 63));
78 rb |= ap << PPC_BITLSHIFT(58);
79 rs = pid << PPC_BITLSHIFT(31);
80 prs = 1; /* process scoped */
81 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100082
83 asm volatile("ptesync": : :"memory");
84 asm volatile(".long 0x7c000224 | (%0 << 11) | (%1 << 16) |"
85 "(%2 << 17) | (%3 << 18) | (%4 << 21)"
86 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
87 asm volatile("ptesync": : :"memory");
88}
89
90static inline void _tlbie_va(unsigned long va, unsigned long pid,
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053091 unsigned long ap, unsigned long ric)
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100092{
Aneesh Kumar K.V36194812016-06-08 19:55:50 +053093 unsigned long rb,rs,prs,r;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +100094
95 rb = va & ~(PPC_BITMASK(52, 63));
96 rb |= ap << PPC_BITLSHIFT(58);
97 rs = pid << PPC_BITLSHIFT(31);
98 prs = 1; /* process scoped */
99 r = 1; /* raidx format */
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000100
101 asm volatile("ptesync": : :"memory");
102 asm volatile(".long 0x7c000264 | (%0 << 11) | (%1 << 16) |"
103 "(%2 << 17) | (%3 << 18) | (%4 << 21)"
104 : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
105 asm volatile("eieio; tlbsync; ptesync": : :"memory");
106}
107
108/*
109 * Base TLB flushing operations:
110 *
111 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
112 * - flush_tlb_page(vma, vmaddr) flushes one page
113 * - flush_tlb_range(vma, start, end) flushes a range of pages
114 * - flush_tlb_kernel_range(start, end) flushes kernel pages
115 *
116 * - local_* variants of page and mm only apply to the current
117 * processor
118 */
119void radix__local_flush_tlb_mm(struct mm_struct *mm)
120{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530121 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000122
123 preempt_disable();
124 pid = mm->context.id;
125 if (pid != MMU_NO_CONTEXT)
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530126 _tlbiel_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000127 preempt_enable();
128}
129EXPORT_SYMBOL(radix__local_flush_tlb_mm);
130
131void radix___local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
132 unsigned long ap, int nid)
133{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530134 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000135
136 preempt_disable();
137 pid = mm ? mm->context.id : 0;
138 if (pid != MMU_NO_CONTEXT)
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530139 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000140 preempt_enable();
141}
142
143void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
144{
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000145#ifdef CONFIG_HUGETLB_PAGE
146 /* need the return fix for nohash.c */
147 if (vma && is_vm_hugetlb_page(vma))
148 return __local_flush_hugetlb_page(vma, vmaddr);
149#endif
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000150 radix___local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
151 mmu_get_ap(mmu_virtual_psize), 0);
152}
153EXPORT_SYMBOL(radix__local_flush_tlb_page);
154
155#ifdef CONFIG_SMP
156static int mm_is_core_local(struct mm_struct *mm)
157{
158 return cpumask_subset(mm_cpumask(mm),
159 topology_sibling_cpumask(smp_processor_id()));
160}
161
162void radix__flush_tlb_mm(struct mm_struct *mm)
163{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530164 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000165
166 preempt_disable();
167 pid = mm->context.id;
168 if (unlikely(pid == MMU_NO_CONTEXT))
169 goto no_context;
170
171 if (!mm_is_core_local(mm)) {
172 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
173
174 if (lock_tlbie)
175 raw_spin_lock(&native_tlbie_lock);
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530176 _tlbie_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000177 if (lock_tlbie)
178 raw_spin_unlock(&native_tlbie_lock);
179 } else
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530180 _tlbiel_pid(pid, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000181no_context:
182 preempt_enable();
183}
184EXPORT_SYMBOL(radix__flush_tlb_mm);
185
186void radix___flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
187 unsigned long ap, int nid)
188{
Aneesh Kumar K.V9690c152016-06-02 15:14:48 +0530189 unsigned long pid;
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000190
191 preempt_disable();
192 pid = mm ? mm->context.id : 0;
193 if (unlikely(pid == MMU_NO_CONTEXT))
194 goto bail;
195 if (!mm_is_core_local(mm)) {
196 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
197
198 if (lock_tlbie)
199 raw_spin_lock(&native_tlbie_lock);
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530200 _tlbie_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000201 if (lock_tlbie)
202 raw_spin_unlock(&native_tlbie_lock);
203 } else
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530204 _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000205bail:
206 preempt_enable();
207}
208
209void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
210{
Aneesh Kumar K.V48483762016-04-29 23:26:25 +1000211#ifdef CONFIG_HUGETLB_PAGE
212 if (vma && is_vm_hugetlb_page(vma))
213 return flush_hugetlb_page(vma, vmaddr);
214#endif
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000215 radix___flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
216 mmu_get_ap(mmu_virtual_psize), 0);
217}
218EXPORT_SYMBOL(radix__flush_tlb_page);
219
220#endif /* CONFIG_SMP */
221
222void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
223{
224 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
225
226 if (lock_tlbie)
227 raw_spin_lock(&native_tlbie_lock);
Aneesh Kumar K.V36194812016-06-08 19:55:50 +0530228 _tlbie_pid(0, RIC_FLUSH_ALL);
Aneesh Kumar K.V1a472c92016-04-29 23:26:05 +1000229 if (lock_tlbie)
230 raw_spin_unlock(&native_tlbie_lock);
231}
232EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
233
234/*
235 * Currently, for range flushing, we just do a full mm flush. Because
236 * we use this in code path where we don' track the page size.
237 */
238void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
239 unsigned long end)
240
241{
242 struct mm_struct *mm = vma->vm_mm;
243 radix__flush_tlb_mm(mm);
244}
245EXPORT_SYMBOL(radix__flush_tlb_range);
246
247
248void radix__tlb_flush(struct mmu_gather *tlb)
249{
250 struct mm_struct *mm = tlb->mm;
251 radix__flush_tlb_mm(mm);
252}