blob: 6a8bf6c6000e29428d8d69f2b559363d8f8b146a [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
5 * Based on earlier code writteh by:
6 * 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
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110017#undef DEBUG
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/config.h>
20#include <asm/pgtable.h>
21#include <asm/mmu.h>
22#include <asm/mmu_context.h>
23#include <asm/paca.h>
24#include <asm/cputable.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110025#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110027#ifdef DEBUG
28#define DBG(fmt...) udbg_printf(fmt)
29#else
30#define DBG(fmt...)
31#endif
32
33extern void slb_allocate_realmode(unsigned long ea);
34extern void slb_allocate_user(unsigned long ea);
35
36static void slb_allocate(unsigned long ea)
37{
38 /* Currently, we do real mode for all SLBs including user, but
39 * that will change if we bring back dynamic VSIDs
40 */
41 slb_allocate_realmode(ea);
42}
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static inline unsigned long mk_esid_data(unsigned long ea, unsigned long slot)
45{
46 return (ea & ESID_MASK) | SLB_ESID_V | slot;
47}
48
49static inline unsigned long mk_vsid_data(unsigned long ea, unsigned long flags)
50{
51 return (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | flags;
52}
53
Olof Johanssonbb78cb72005-05-01 08:58:44 -070054static inline void create_slbe(unsigned long ea, unsigned long flags,
55 unsigned long entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 asm volatile("slbmte %0,%1" :
58 : "r" (mk_vsid_data(ea, flags)),
59 "r" (mk_esid_data(ea, entry))
60 : "memory" );
61}
62
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100063void slb_flush_and_rebolt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 /* If you change this make sure you change SLB_NUM_BOLTED
66 * appropriately too. */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100067 unsigned long linear_llp, vmalloc_llp, lflags, vflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned long ksp_esid_data;
69
70 WARN_ON(!irqs_disabled());
71
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110072 linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100073 vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110074 lflags = SLB_VSID_KERNEL | linear_llp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100075 vflags = SLB_VSID_KERNEL | vmalloc_llp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 ksp_esid_data = mk_esid_data(get_paca()->kstack, 2);
Michael Ellermanb5666f72005-12-05 10:24:33 -060078 if ((ksp_esid_data & ESID_MASK) == PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 ksp_esid_data &= ~SLB_ESID_V;
80
81 /* We need to do this all in asm, so we're sure we don't touch
82 * the stack between the slbia and rebolting it. */
83 asm volatile("isync\n"
84 "slbia\n"
85 /* Slot 1 - first VMALLOC segment */
86 "slbmte %0,%1\n"
87 /* Slot 2 - kernel stack */
88 "slbmte %2,%3\n"
89 "isync"
David Gibson14c89e72005-12-14 16:08:40 +110090 :: "r"(mk_vsid_data(VMALLOC_START, vflags)),
91 "r"(mk_esid_data(VMALLOC_START, 1)),
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110092 "r"(mk_vsid_data(ksp_esid_data, lflags)),
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 "r"(ksp_esid_data)
94 : "memory");
95}
96
97/* Flush all user entries from the segment table of the current processor. */
98void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
99{
100 unsigned long offset = get_paca()->slb_cache_ptr;
101 unsigned long esid_data = 0;
102 unsigned long pc = KSTK_EIP(tsk);
103 unsigned long stack = KSTK_ESP(tsk);
104 unsigned long unmapped_base;
105
106 if (offset <= SLB_CACHE_ENTRIES) {
107 int i;
108 asm volatile("isync" : : : "memory");
109 for (i = 0; i < offset; i++) {
David Gibson14b34662005-09-06 14:59:47 +1000110 esid_data = ((unsigned long)get_paca()->slb_cache[i]
111 << SID_SHIFT) | SLBIE_C;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 asm volatile("slbie %0" : : "r" (esid_data));
113 }
114 asm volatile("isync" : : : "memory");
115 } else {
116 slb_flush_and_rebolt();
117 }
118
119 /* Workaround POWER5 < DD2.1 issue */
120 if (offset == 1 || offset > SLB_CACHE_ENTRIES)
121 asm volatile("slbie %0" : : "r" (esid_data));
122
123 get_paca()->slb_cache_ptr = 0;
124 get_paca()->context = mm->context;
125
126 /*
127 * preload some userspace segments into the SLB.
128 */
129 if (test_tsk_thread_flag(tsk, TIF_32BIT))
130 unmapped_base = TASK_UNMAPPED_BASE_USER32;
131 else
132 unmapped_base = TASK_UNMAPPED_BASE_USER64;
133
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100134 if (is_kernel_addr(pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
136 slb_allocate(pc);
137
138 if (GET_ESID(pc) == GET_ESID(stack))
139 return;
140
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100141 if (is_kernel_addr(stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return;
143 slb_allocate(stack);
144
145 if ((GET_ESID(pc) == GET_ESID(unmapped_base))
146 || (GET_ESID(stack) == GET_ESID(unmapped_base)))
147 return;
148
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100149 if (is_kernel_addr(unmapped_base))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return;
151 slb_allocate(unmapped_base);
152}
153
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100154static inline void patch_slb_encoding(unsigned int *insn_addr,
155 unsigned int immed)
156{
157 /* Assume the instruction had a "0" immediate value, just
158 * "or" in the new value
159 */
160 *insn_addr |= immed;
161 flush_icache_range((unsigned long)insn_addr, 4+
162 (unsigned long)insn_addr);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165void slb_initialize(void)
166{
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000167 unsigned long linear_llp, vmalloc_llp, io_llp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100168 static int slb_encoding_inited;
169 extern unsigned int *slb_miss_kernel_load_linear;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000170 extern unsigned int *slb_miss_kernel_load_io;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100171#ifdef CONFIG_HUGETLB_PAGE
172 extern unsigned int *slb_miss_user_load_huge;
173 unsigned long huge_llp;
174
175 huge_llp = mmu_psize_defs[mmu_huge_psize].sllp;
176#endif
177
178 /* Prepare our SLB miss handler based on our page size */
179 linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000180 io_llp = mmu_psize_defs[mmu_io_psize].sllp;
181 vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
182 get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
183
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100184 if (!slb_encoding_inited) {
185 slb_encoding_inited = 1;
186 patch_slb_encoding(slb_miss_kernel_load_linear,
187 SLB_VSID_KERNEL | linear_llp);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000188 patch_slb_encoding(slb_miss_kernel_load_io,
189 SLB_VSID_KERNEL | io_llp);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100190
191 DBG("SLB: linear LLP = %04x\n", linear_llp);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000192 DBG("SLB: io LLP = %04x\n", io_llp);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100193#ifdef CONFIG_HUGETLB_PAGE
194 patch_slb_encoding(slb_miss_user_load_huge,
195 SLB_VSID_USER | huge_llp);
196 DBG("SLB: huge LLP = %04x\n", huge_llp);
197#endif
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* On iSeries the bolted entries have already been set up by
201 * the hypervisor from the lparMap data in head.S */
202#ifndef CONFIG_PPC_ISERIES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100203 {
204 unsigned long lflags, vflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100206 lflags = SLB_VSID_KERNEL | linear_llp;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000207 vflags = SLB_VSID_KERNEL | vmalloc_llp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100209 /* Invalidate the entire SLB (even slot 0) & all the ERATS */
210 asm volatile("isync":::"memory");
211 asm volatile("slbmte %0,%0"::"r" (0) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 asm volatile("isync; slbia; isync":::"memory");
Michael Ellermanb5666f72005-12-05 10:24:33 -0600213 create_slbe(PAGE_OFFSET, lflags, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100214
David Gibson14c89e72005-12-14 16:08:40 +1100215 create_slbe(VMALLOC_START, vflags, 1);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* We don't bolt the stack for the time being - we're in boot,
218 * so the stack is in the bolted segment. By the time it goes
219 * elsewhere, we'll call _switch() which will bolt in the new
220 * one. */
221 asm volatile("isync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100222 }
223#endif /* CONFIG_PPC_ISERIES */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 get_paca()->stab_rr = SLB_NUM_BOLTED;
226}