blob: 6bc8b4aeba5c3cc00a6b5ace179698eda08d8948 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PowerPC64 SLB support.
3 *
4 * Copyright (C) 2004 David Gibson <dwg@au.ibm.com>, IBM
Sankar P5cdcd9d2009-05-12 12:41:13 +05305 * Based on earlier code written by:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
7 * Copyright (c) 2001 Dave Engebretsen
8 * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
9 *
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/pgtable.h>
18#include <asm/mmu.h>
19#include <asm/mmu_context.h>
20#include <asm/paca.h>
21#include <asm/cputable.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110022#include <asm/cacheflush.h>
Michael Neuling2f6093c2006-08-07 16:19:19 +100023#include <asm/smp.h>
Stephen Rothwell56291e12006-11-14 12:57:38 +110024#include <asm/firmware.h>
Michael Neuling2f6093c2006-08-07 16:19:19 +100025#include <linux/compiler.h>
will schmidtaa39be02007-10-30 06:24:19 +110026#include <asm/udbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110028
29extern void slb_allocate_realmode(unsigned long ea);
30extern void slb_allocate_user(unsigned long ea);
31
32static void slb_allocate(unsigned long ea)
33{
34 /* Currently, we do real mode for all SLBs including user, but
35 * that will change if we bring back dynamic VSIDs
36 */
37 slb_allocate_realmode(ea);
38}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Paul Mackerras3b575062008-05-02 14:29:12 +100040#define slb_esid_mask(ssize) \
41 (((ssize) == MMU_SEGSIZE_256M)? ESID_MASK: ESID_MASK_1T)
42
Paul Mackerras1189be62007-10-11 20:37:10 +100043static inline unsigned long mk_esid_data(unsigned long ea, int ssize,
44 unsigned long slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Paul Mackerras3b575062008-05-02 14:29:12 +100046 return (ea & slb_esid_mask(ssize)) | SLB_ESID_V | slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Paul Mackerras1189be62007-10-11 20:37:10 +100049#define slb_vsid_shift(ssize) \
50 ((ssize) == MMU_SEGSIZE_256M? SLB_VSID_SHIFT: SLB_VSID_SHIFT_1T)
51
52static inline unsigned long mk_vsid_data(unsigned long ea, int ssize,
53 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Paul Mackerras1189be62007-10-11 20:37:10 +100055 return (get_kernel_vsid(ea, ssize) << slb_vsid_shift(ssize)) | flags |
56 ((unsigned long) ssize << SLB_VSID_SSIZE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Paul Mackerras1189be62007-10-11 20:37:10 +100059static inline void slb_shadow_update(unsigned long ea, int ssize,
Michael Neuling67439b72007-08-03 11:55:39 +100060 unsigned long flags,
Michael Neuling2f6093c2006-08-07 16:19:19 +100061 unsigned long entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Michael Neuling2f6093c2006-08-07 16:19:19 +100063 /*
64 * Clear the ESID first so the entry is not valid while we are
Michael Neuling00efee72007-08-24 16:58:37 +100065 * updating it. No write barriers are needed here, provided
66 * we only update the current CPU's SLB shadow buffer.
Michael Neuling2f6093c2006-08-07 16:19:19 +100067 */
68 get_slb_shadow()->save_area[entry].esid = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +100069 get_slb_shadow()->save_area[entry].vsid = mk_vsid_data(ea, ssize, flags);
70 get_slb_shadow()->save_area[entry].esid = mk_esid_data(ea, ssize, entry);
Michael Neuling2f6093c2006-08-07 16:19:19 +100071}
72
Paul Mackerrasedd06222007-08-10 21:04:07 +100073static inline void slb_shadow_clear(unsigned long entry)
Michael Neuling2f6093c2006-08-07 16:19:19 +100074{
Paul Mackerrasedd06222007-08-10 21:04:07 +100075 get_slb_shadow()->save_area[entry].esid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Paul Mackerras1189be62007-10-11 20:37:10 +100078static inline void create_shadowed_slbe(unsigned long ea, int ssize,
79 unsigned long flags,
Paul Mackerras175587c2007-08-25 13:14:28 +100080 unsigned long entry)
81{
82 /*
83 * Updating the shadow buffer before writing the SLB ensures
84 * we don't get a stale entry here if we get preempted by PHYP
85 * between these two statements.
86 */
Paul Mackerras1189be62007-10-11 20:37:10 +100087 slb_shadow_update(ea, ssize, flags, entry);
Paul Mackerras175587c2007-08-25 13:14:28 +100088
89 asm volatile("slbmte %0,%1" :
Paul Mackerras1189be62007-10-11 20:37:10 +100090 : "r" (mk_vsid_data(ea, ssize, flags)),
91 "r" (mk_esid_data(ea, ssize, entry))
Paul Mackerras175587c2007-08-25 13:14:28 +100092 : "memory" );
93}
94
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100095void slb_flush_and_rebolt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 /* If you change this make sure you change SLB_NUM_BOLTED
98 * appropriately too. */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100099 unsigned long linear_llp, vmalloc_llp, lflags, vflags;
Paul Mackerras1189be62007-10-11 20:37:10 +1000100 unsigned long ksp_esid_data, ksp_vsid_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 WARN_ON(!irqs_disabled());
103
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100104 linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000105 vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100106 lflags = SLB_VSID_KERNEL | linear_llp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000107 vflags = SLB_VSID_KERNEL | vmalloc_llp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Paul Mackerras1189be62007-10-11 20:37:10 +1000109 ksp_esid_data = mk_esid_data(get_paca()->kstack, mmu_kernel_ssize, 2);
110 if ((ksp_esid_data & ~0xfffffffUL) <= PAGE_OFFSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 ksp_esid_data &= ~SLB_ESID_V;
Paul Mackerras1189be62007-10-11 20:37:10 +1000112 ksp_vsid_data = 0;
Paul Mackerrasedd06222007-08-10 21:04:07 +1000113 slb_shadow_clear(2);
114 } else {
115 /* Update stack entry; others don't change */
Paul Mackerras1189be62007-10-11 20:37:10 +1000116 slb_shadow_update(get_paca()->kstack, mmu_kernel_ssize, lflags, 2);
117 ksp_vsid_data = get_slb_shadow()->save_area[2].vsid;
Paul Mackerrasedd06222007-08-10 21:04:07 +1000118 }
Michael Neuling2f6093c2006-08-07 16:19:19 +1000119
Anton Blanchard44387e92008-03-17 15:27:09 +1100120 /*
121 * We can't take a PMU exception in the following code, so hard
122 * disable interrupts.
123 */
124 hard_irq_disable();
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /* We need to do this all in asm, so we're sure we don't touch
127 * the stack between the slbia and rebolting it. */
128 asm volatile("isync\n"
129 "slbia\n"
130 /* Slot 1 - first VMALLOC segment */
131 "slbmte %0,%1\n"
132 /* Slot 2 - kernel stack */
133 "slbmte %2,%3\n"
134 "isync"
Paul Mackerras1189be62007-10-11 20:37:10 +1000135 :: "r"(mk_vsid_data(VMALLOC_START, mmu_kernel_ssize, vflags)),
136 "r"(mk_esid_data(VMALLOC_START, mmu_kernel_ssize, 1)),
137 "r"(ksp_vsid_data),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 "r"(ksp_esid_data)
139 : "memory");
140}
141
Michael Neuling67439b72007-08-03 11:55:39 +1000142void slb_vmalloc_update(void)
143{
144 unsigned long vflags;
145
146 vflags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmalloc_psize].sllp;
Paul Mackerras1189be62007-10-11 20:37:10 +1000147 slb_shadow_update(VMALLOC_START, mmu_kernel_ssize, vflags, 1);
Michael Neuling67439b72007-08-03 11:55:39 +1000148 slb_flush_and_rebolt();
149}
150
will schmidt465ccab2007-10-31 05:59:33 +1100151/* Helper function to compare esids. There are four cases to handle.
152 * 1. The system is not 1T segment size capable. Use the GET_ESID compare.
153 * 2. The system is 1T capable, both addresses are < 1T, use the GET_ESID compare.
154 * 3. The system is 1T capable, only one of the two addresses is > 1T. This is not a match.
155 * 4. The system is 1T capable, both addresses are > 1T, use the GET_ESID_1T macro to compare.
156 */
157static inline int esids_match(unsigned long addr1, unsigned long addr2)
158{
159 int esid_1t_count;
160
161 /* System is not 1T segment size capable. */
162 if (!cpu_has_feature(CPU_FTR_1T_SEGMENT))
163 return (GET_ESID(addr1) == GET_ESID(addr2));
164
165 esid_1t_count = (((addr1 >> SID_SHIFT_1T) != 0) +
166 ((addr2 >> SID_SHIFT_1T) != 0));
167
168 /* both addresses are < 1T */
169 if (esid_1t_count == 0)
170 return (GET_ESID(addr1) == GET_ESID(addr2));
171
172 /* One address < 1T, the other > 1T. Not a match */
173 if (esid_1t_count == 1)
174 return 0;
175
176 /* Both addresses are > 1T. */
177 return (GET_ESID_1T(addr1) == GET_ESID_1T(addr2));
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* Flush all user entries from the segment table of the current processor. */
181void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
182{
183 unsigned long offset = get_paca()->slb_cache_ptr;
Paul Mackerras1189be62007-10-11 20:37:10 +1000184 unsigned long slbie_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned long pc = KSTK_EIP(tsk);
186 unsigned long stack = KSTK_ESP(tsk);
Anton Blanchardde4376c2009-07-13 20:53:53 +0000187 unsigned long exec_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Olof Johanssonf66bce52007-10-16 00:58:59 +1000189 if (!cpu_has_feature(CPU_FTR_NO_SLBIE_B) &&
190 offset <= SLB_CACHE_ENTRIES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int i;
192 asm volatile("isync" : : : "memory");
193 for (i = 0; i < offset; i++) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000194 slbie_data = (unsigned long)get_paca()->slb_cache[i]
195 << SID_SHIFT; /* EA */
196 slbie_data |= user_segment_size(slbie_data)
197 << SLBIE_SSIZE_SHIFT;
198 slbie_data |= SLBIE_C; /* C set for user addresses */
199 asm volatile("slbie %0" : : "r" (slbie_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 asm volatile("isync" : : : "memory");
202 } else {
203 slb_flush_and_rebolt();
204 }
205
206 /* Workaround POWER5 < DD2.1 issue */
207 if (offset == 1 || offset > SLB_CACHE_ENTRIES)
Paul Mackerras1189be62007-10-11 20:37:10 +1000208 asm volatile("slbie %0" : : "r" (slbie_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 get_paca()->slb_cache_ptr = 0;
211 get_paca()->context = mm->context;
212
213 /*
214 * preload some userspace segments into the SLB.
Anton Blanchardde4376c2009-07-13 20:53:53 +0000215 * Almost all 32 and 64bit PowerPC executables are linked at
216 * 0x10000000 so it makes sense to preload this segment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 */
Anton Blanchardde4376c2009-07-13 20:53:53 +0000218 exec_base = 0x10000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Anton Blanchard5eb9bac2009-07-13 20:53:52 +0000220 if (is_kernel_addr(pc) || is_kernel_addr(stack) ||
Anton Blanchardde4376c2009-07-13 20:53:53 +0000221 is_kernel_addr(exec_base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return;
Anton Blanchard5eb9bac2009-07-13 20:53:52 +0000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 slb_allocate(pc);
225
Anton Blanchard5eb9bac2009-07-13 20:53:52 +0000226 if (!esids_match(pc, stack))
227 slb_allocate(stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Anton Blanchardde4376c2009-07-13 20:53:53 +0000229 if (!esids_match(pc, exec_base) &&
230 !esids_match(stack, exec_base))
231 slb_allocate(exec_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100234static inline void patch_slb_encoding(unsigned int *insn_addr,
235 unsigned int immed)
236{
237 /* Assume the instruction had a "0" immediate value, just
238 * "or" in the new value
239 */
240 *insn_addr |= immed;
241 flush_icache_range((unsigned long)insn_addr, 4+
242 (unsigned long)insn_addr);
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245void slb_initialize(void)
246{
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000247 unsigned long linear_llp, vmalloc_llp, io_llp;
Stephen Rothwell56291e12006-11-14 12:57:38 +1100248 unsigned long lflags, vflags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100249 static int slb_encoding_inited;
250 extern unsigned int *slb_miss_kernel_load_linear;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000251 extern unsigned int *slb_miss_kernel_load_io;
Michael Neuling584f8b72007-12-06 17:24:48 +1100252 extern unsigned int *slb_compare_rr_to_size;
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000253#ifdef CONFIG_SPARSEMEM_VMEMMAP
254 extern unsigned int *slb_miss_kernel_load_vmemmap;
255 unsigned long vmemmap_llp;
256#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100257
258 /* Prepare our SLB miss handler based on our page size */
259 linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000260 io_llp = mmu_psize_defs[mmu_io_psize].sllp;
261 vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
262 get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000263#ifdef CONFIG_SPARSEMEM_VMEMMAP
264 vmemmap_llp = mmu_psize_defs[mmu_vmemmap_psize].sllp;
265#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100266 if (!slb_encoding_inited) {
267 slb_encoding_inited = 1;
268 patch_slb_encoding(slb_miss_kernel_load_linear,
269 SLB_VSID_KERNEL | linear_llp);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000270 patch_slb_encoding(slb_miss_kernel_load_io,
271 SLB_VSID_KERNEL | io_llp);
Michael Neuling584f8b72007-12-06 17:24:48 +1100272 patch_slb_encoding(slb_compare_rr_to_size,
273 mmu_slb_size);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100274
Michael Ellerman651e2dd2009-06-17 18:13:51 +0000275 pr_devel("SLB: linear LLP = %04lx\n", linear_llp);
276 pr_devel("SLB: io LLP = %04lx\n", io_llp);
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000277
278#ifdef CONFIG_SPARSEMEM_VMEMMAP
279 patch_slb_encoding(slb_miss_kernel_load_vmemmap,
280 SLB_VSID_KERNEL | vmemmap_llp);
Michael Ellerman651e2dd2009-06-17 18:13:51 +0000281 pr_devel("SLB: vmemmap LLP = %04lx\n", vmemmap_llp);
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000282#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100283 }
284
Stephen Rothwell56291e12006-11-14 12:57:38 +1100285 get_paca()->stab_rr = SLB_NUM_BOLTED;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* On iSeries the bolted entries have already been set up by
288 * the hypervisor from the lparMap data in head.S */
Stephen Rothwell56291e12006-11-14 12:57:38 +1100289 if (firmware_has_feature(FW_FEATURE_ISERIES))
290 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100292 lflags = SLB_VSID_KERNEL | linear_llp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000293 vflags = SLB_VSID_KERNEL | vmalloc_llp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100295 /* Invalidate the entire SLB (even slot 0) & all the ERATS */
Paul Mackerras175587c2007-08-25 13:14:28 +1000296 asm volatile("isync":::"memory");
297 asm volatile("slbmte %0,%0"::"r" (0) : "memory");
298 asm volatile("isync; slbia; isync":::"memory");
Paul Mackerras1189be62007-10-11 20:37:10 +1000299 create_shadowed_slbe(PAGE_OFFSET, mmu_kernel_ssize, lflags, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100300
Paul Mackerras1189be62007-10-11 20:37:10 +1000301 create_shadowed_slbe(VMALLOC_START, mmu_kernel_ssize, vflags, 1);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100302
Paul Mackerras3b575062008-05-02 14:29:12 +1000303 /* For the boot cpu, we're running on the stack in init_thread_union,
304 * which is in the first segment of the linear mapping, and also
305 * get_paca()->kstack hasn't been initialized yet.
306 * For secondary cpus, we need to bolt the kernel stack entry now.
307 */
Paul Mackerrasdfbe0d32008-01-15 17:29:33 +1100308 slb_shadow_clear(2);
Paul Mackerras3b575062008-05-02 14:29:12 +1000309 if (raw_smp_processor_id() != boot_cpuid &&
310 (get_paca()->kstack & slb_esid_mask(mmu_kernel_ssize)) > PAGE_OFFSET)
311 create_shadowed_slbe(get_paca()->kstack,
312 mmu_kernel_ssize, lflags, 2);
Paul Mackerrasdfbe0d32008-01-15 17:29:33 +1100313
Paul Mackerras175587c2007-08-25 13:14:28 +1000314 asm volatile("isync":::"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}