blob: d16100c9416ac3d647c4ec72fada972a0ec411a1 [file] [log] [blame]
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +00001/*
2 * This file contains the routines for TLB flushing.
3 * On machines where the MMU does not use a hash table to store virtual to
4 * physical translations (ie, SW loaded TLBs or Book3E compilant processors,
5 * this does -not- include 603 however which shares the implementation with
6 * hash based processors)
7 *
8 * -- BenH
9 *
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000010 * Copyright 2008,2009 Ben Herrenschmidt <benh@kernel.crashing.org>
11 * IBM Corp.
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000012 *
13 * Derived from arch/ppc/mm/init.c:
14 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
15 *
16 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
17 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
18 * Copyright (C) 1996 Paul Mackerras
19 *
20 * Derived from "arch/i386/mm/init.c"
21 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version
26 * 2 of the License, or (at your option) any later version.
27 *
28 */
29
30#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/init.h>
33#include <linux/highmem.h>
34#include <linux/pagemap.h>
35#include <linux/preempt.h>
36#include <linux/spinlock.h>
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000037#include <linux/lmb.h>
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000038
39#include <asm/tlbflush.h>
40#include <asm/tlb.h>
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000041#include <asm/code-patching.h>
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000042
43#include "mmu_decl.h"
44
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000045#ifdef CONFIG_PPC_BOOK3E
46struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
47 [MMU_PAGE_4K] = {
48 .shift = 12,
49 .enc = BOOK3E_PAGESZ_4K,
50 },
51 [MMU_PAGE_16K] = {
52 .shift = 14,
53 .enc = BOOK3E_PAGESZ_16K,
54 },
55 [MMU_PAGE_64K] = {
56 .shift = 16,
57 .enc = BOOK3E_PAGESZ_64K,
58 },
59 [MMU_PAGE_1M] = {
60 .shift = 20,
61 .enc = BOOK3E_PAGESZ_1M,
62 },
63 [MMU_PAGE_16M] = {
64 .shift = 24,
65 .enc = BOOK3E_PAGESZ_16M,
66 },
67 [MMU_PAGE_256M] = {
68 .shift = 28,
69 .enc = BOOK3E_PAGESZ_256M,
70 },
71 [MMU_PAGE_1G] = {
72 .shift = 30,
73 .enc = BOOK3E_PAGESZ_1GB,
74 },
75};
76static inline int mmu_get_tsize(int psize)
77{
78 return mmu_psize_defs[psize].enc;
79}
80#else
81static inline int mmu_get_tsize(int psize)
82{
83 /* This isn't used on !Book3E for now */
84 return 0;
85}
86#endif
87
88/* The variables below are currently only used on 64-bit Book3E
89 * though this will probably be made common with other nohash
90 * implementations at some point
91 */
92#ifdef CONFIG_PPC64
93
94int mmu_linear_psize; /* Page size used for the linear mapping */
95int mmu_pte_psize; /* Page size used for PTE pages */
96int book3e_htw_enabled; /* Is HW tablewalk enabled ? */
97unsigned long linear_map_top; /* Top of linear mapping */
98
99#endif /* CONFIG_PPC64 */
100
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000101/*
102 * Base TLB flushing operations:
103 *
104 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
105 * - flush_tlb_page(vma, vmaddr) flushes one page
106 * - flush_tlb_range(vma, start, end) flushes a range of pages
107 * - flush_tlb_kernel_range(start, end) flushes kernel pages
108 *
109 * - local_* variants of page and mm only apply to the current
110 * processor
111 */
112
113/*
114 * These are the base non-SMP variants of page and mm flushing
115 */
116void local_flush_tlb_mm(struct mm_struct *mm)
117{
118 unsigned int pid;
119
120 preempt_disable();
121 pid = mm->context.id;
122 if (pid != MMU_NO_CONTEXT)
123 _tlbil_pid(pid);
124 preempt_enable();
125}
126EXPORT_SYMBOL(local_flush_tlb_mm);
127
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000128void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
129 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000130{
131 unsigned int pid;
132
133 preempt_disable();
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000134 pid = mm ? mm->context.id : 0;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000135 if (pid != MMU_NO_CONTEXT)
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000136 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000137 preempt_enable();
138}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000139
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000140void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
141{
142 __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000143 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000144}
145EXPORT_SYMBOL(local_flush_tlb_page);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000146
147/*
148 * And here are the SMP non-local implementations
149 */
150#ifdef CONFIG_SMP
151
152static DEFINE_SPINLOCK(tlbivax_lock);
153
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000154static int mm_is_core_local(struct mm_struct *mm)
155{
156 return cpumask_subset(mm_cpumask(mm),
157 topology_thread_cpumask(smp_processor_id()));
158}
159
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000160struct tlb_flush_param {
161 unsigned long addr;
162 unsigned int pid;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000163 unsigned int tsize;
164 unsigned int ind;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000165};
166
167static void do_flush_tlb_mm_ipi(void *param)
168{
169 struct tlb_flush_param *p = param;
170
171 _tlbil_pid(p ? p->pid : 0);
172}
173
174static void do_flush_tlb_page_ipi(void *param)
175{
176 struct tlb_flush_param *p = param;
177
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000178 _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000179}
180
181
182/* Note on invalidations and PID:
183 *
184 * We snapshot the PID with preempt disabled. At this point, it can still
185 * change either because:
186 * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
187 * - we are invaliating some target that isn't currently running here
188 * and is concurrently acquiring a new PID on another CPU
189 * - some other CPU is re-acquiring a lost PID for this mm
190 * etc...
191 *
192 * However, this shouldn't be a problem as we only guarantee
193 * invalidation of TLB entries present prior to this call, so we
194 * don't care about the PID changing, and invalidating a stale PID
195 * is generally harmless.
196 */
197
198void flush_tlb_mm(struct mm_struct *mm)
199{
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000200 unsigned int pid;
201
202 preempt_disable();
203 pid = mm->context.id;
204 if (unlikely(pid == MMU_NO_CONTEXT))
205 goto no_context;
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000206 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000207 struct tlb_flush_param p = { .pid = pid };
Rusty Russell56aa4122009-03-15 18:16:43 +0000208 /* Ignores smp_processor_id() even if set. */
209 smp_call_function_many(mm_cpumask(mm),
210 do_flush_tlb_mm_ipi, &p, 1);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000211 }
212 _tlbil_pid(pid);
213 no_context:
214 preempt_enable();
215}
216EXPORT_SYMBOL(flush_tlb_mm);
217
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000218void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
219 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000220{
Rusty Russell56aa4122009-03-15 18:16:43 +0000221 struct cpumask *cpu_mask;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000222 unsigned int pid;
223
224 preempt_disable();
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000225 pid = mm ? mm->context.id : 0;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000226 if (unlikely(pid == MMU_NO_CONTEXT))
227 goto bail;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000228 cpu_mask = mm_cpumask(mm);
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000229 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000230 /* If broadcast tlbivax is supported, use it */
231 if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
232 int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
233 if (lock)
234 spin_lock(&tlbivax_lock);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000235 _tlbivax_bcast(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000236 if (lock)
237 spin_unlock(&tlbivax_lock);
238 goto bail;
239 } else {
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000240 struct tlb_flush_param p = {
241 .pid = pid,
242 .addr = vmaddr,
243 .tsize = tsize,
244 .ind = ind,
245 };
Rusty Russell56aa4122009-03-15 18:16:43 +0000246 /* Ignores smp_processor_id() even if set in cpu_mask */
247 smp_call_function_many(cpu_mask,
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000248 do_flush_tlb_page_ipi, &p, 1);
249 }
250 }
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000251 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000252 bail:
253 preempt_enable();
254}
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000255
256void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
257{
258 __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000259 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000260}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000261EXPORT_SYMBOL(flush_tlb_page);
262
263#endif /* CONFIG_SMP */
264
265/*
266 * Flush kernel TLB entries in the given range
267 */
268void flush_tlb_kernel_range(unsigned long start, unsigned long end)
269{
270#ifdef CONFIG_SMP
271 preempt_disable();
272 smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
273 _tlbil_pid(0);
274 preempt_enable();
Dave Liud6a09e02008-12-30 23:42:55 +0000275#else
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000276 _tlbil_pid(0);
Dave Liud6a09e02008-12-30 23:42:55 +0000277#endif
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000278}
279EXPORT_SYMBOL(flush_tlb_kernel_range);
280
281/*
282 * Currently, for range flushing, we just do a full mm flush. This should
283 * be optimized based on a threshold on the size of the range, since
284 * some implementation can stack multiple tlbivax before a tlbsync but
285 * for now, we keep it that way
286 */
287void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
288 unsigned long end)
289
290{
291 flush_tlb_mm(vma->vm_mm);
292}
293EXPORT_SYMBOL(flush_tlb_range);
Benjamin Herrenschmidtc7cc58a12009-07-23 23:15:28 +0000294
295void tlb_flush(struct mmu_gather *tlb)
296{
297 flush_tlb_mm(tlb->mm);
298
299 /* Push out batch of freed page tables */
300 pte_free_finish();
301}
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000302
303/*
304 * Below are functions specific to the 64-bit variant of Book3E though that
305 * may change in the future
306 */
307
308#ifdef CONFIG_PPC64
309
310/*
311 * Handling of virtual linear page tables or indirect TLB entries
312 * flushing when PTE pages are freed
313 */
314void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
315{
316 int tsize = mmu_psize_defs[mmu_pte_psize].enc;
317
318 if (book3e_htw_enabled) {
319 unsigned long start = address & PMD_MASK;
320 unsigned long end = address + PMD_SIZE;
321 unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
322
323 /* This isn't the most optimal, ideally we would factor out the
324 * while preempt & CPU mask mucking around, or even the IPI but
325 * it will do for now
326 */
327 while (start < end) {
328 __flush_tlb_page(tlb->mm, start, tsize, 1);
329 start += size;
330 }
331 } else {
332 unsigned long rmask = 0xf000000000000000ul;
333 unsigned long rid = (address & rmask) | 0x1000000000000000ul;
334 unsigned long vpte = address & ~rmask;
335
336#ifdef CONFIG_PPC_64K_PAGES
337 vpte = (vpte >> (PAGE_SHIFT - 4)) & ~0xfffful;
338#else
339 vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
340#endif
341 vpte |= rid;
342 __flush_tlb_page(tlb->mm, vpte, tsize, 0);
343 }
344}
345
346/*
347 * Early initialization of the MMU TLB code
348 */
349static void __early_init_mmu(int boot_cpu)
350{
351 extern unsigned int interrupt_base_book3e;
352 extern unsigned int exc_data_tlb_miss_htw_book3e;
353 extern unsigned int exc_instruction_tlb_miss_htw_book3e;
354
355 unsigned int *ibase = &interrupt_base_book3e;
356 unsigned int mas4;
357
358 /* XXX This will have to be decided at runtime, but right
359 * now our boot and TLB miss code hard wires it
360 */
361 mmu_linear_psize = MMU_PAGE_1G;
362
363
364 /* Check if HW tablewalk is present, and if yes, enable it by:
365 *
366 * - patching the TLB miss handlers to branch to the
367 * one dedicates to it
368 *
369 * - setting the global book3e_htw_enabled
370 *
371 * - Set MAS4:INDD and default page size
372 */
373
374 /* XXX This code only checks for TLB 0 capabilities and doesn't
375 * check what page size combos are supported by the HW. It
376 * also doesn't handle the case where a separate array holds
377 * the IND entries from the array loaded by the PT.
378 */
379 if (boot_cpu) {
380 unsigned int tlb0cfg = mfspr(SPRN_TLB0CFG);
381
382 /* Check if HW loader is supported */
383 if ((tlb0cfg & TLBnCFG_IND) &&
384 (tlb0cfg & TLBnCFG_PT)) {
385 patch_branch(ibase + (0x1c0 / 4),
386 (unsigned long)&exc_data_tlb_miss_htw_book3e, 0);
387 patch_branch(ibase + (0x1e0 / 4),
388 (unsigned long)&exc_instruction_tlb_miss_htw_book3e, 0);
389 book3e_htw_enabled = 1;
390 }
391 pr_info("MMU: Book3E Page Tables %s\n",
392 book3e_htw_enabled ? "Enabled" : "Disabled");
393 }
394
395 /* Set MAS4 based on page table setting */
396
397 mas4 = 0x4 << MAS4_WIMGED_SHIFT;
398 if (book3e_htw_enabled) {
399 mas4 |= mas4 | MAS4_INDD;
400#ifdef CONFIG_PPC_64K_PAGES
401 mas4 |= BOOK3E_PAGESZ_256M << MAS4_TSIZED_SHIFT;
402 mmu_pte_psize = MMU_PAGE_256M;
403#else
404 mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
405 mmu_pte_psize = MMU_PAGE_1M;
406#endif
407 } else {
408#ifdef CONFIG_PPC_64K_PAGES
409 mas4 |= BOOK3E_PAGESZ_64K << MAS4_TSIZED_SHIFT;
410#else
411 mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
412#endif
413 mmu_pte_psize = mmu_virtual_psize;
414 }
415 mtspr(SPRN_MAS4, mas4);
416
417 /* Set the global containing the top of the linear mapping
418 * for use by the TLB miss code
419 */
420 linear_map_top = lmb_end_of_DRAM();
421
422 /* A sync won't hurt us after mucking around with
423 * the MMU configuration
424 */
425 mb();
426}
427
428void __init early_init_mmu(void)
429{
430 __early_init_mmu(1);
431}
432
433void __cpuinit early_init_mmu_secondary(void)
434{
435 __early_init_mmu(0);
436}
437
438#endif /* CONFIG_PPC64 */