blob: 7bc42eb087adb777df01b69350ada386055c97a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PowerPC memory management structures
3 *
4 * Dave Engebretsen & Mike Corrigan <{engebret|mikejc}@us.ibm.com>
5 * PPC64 rework.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#ifndef _PPC64_MMU_H_
14#define _PPC64_MMU_H_
15
16#include <linux/config.h>
17#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
David Gibson1f8d4192005-05-05 16:15:13 -070019/*
20 * Segment table
21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define STE_ESID_V 0x80
24#define STE_ESID_KS 0x20
25#define STE_ESID_KP 0x10
26#define STE_ESID_N 0x08
27
28#define STE_VSID_SHIFT 12
29
David Gibson1f8d4192005-05-05 16:15:13 -070030/* Location of cpu0's segment table */
David Gibsonc59c4642005-08-19 14:52:31 +100031#define STAB0_PAGE 0x6
David Gibson1f8d4192005-05-05 16:15:13 -070032#define STAB0_PHYS_ADDR (STAB0_PAGE<<PAGE_SHIFT)
David Gibsonc59c4642005-08-19 14:52:31 +100033
34#ifndef __ASSEMBLY__
35extern char initial_stab[];
36#endif /* ! __ASSEMBLY */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David Gibson1f8d4192005-05-05 16:15:13 -070038/*
39 * SLB
40 */
41
42#define SLB_NUM_BOLTED 3
43#define SLB_CACHE_ENTRIES 8
44
45/* Bits in the SLB ESID word */
46#define SLB_ESID_V ASM_CONST(0x0000000008000000) /* valid */
47
48/* Bits in the SLB VSID word */
49#define SLB_VSID_SHIFT 12
50#define SLB_VSID_KS ASM_CONST(0x0000000000000800)
51#define SLB_VSID_KP ASM_CONST(0x0000000000000400)
52#define SLB_VSID_N ASM_CONST(0x0000000000000200) /* no-execute */
Arnd Bergmannfef1c772005-06-23 09:43:37 +100053#define SLB_VSID_L ASM_CONST(0x0000000000000100) /* largepage */
David Gibson1f8d4192005-05-05 16:15:13 -070054#define SLB_VSID_C ASM_CONST(0x0000000000000080) /* class */
Arnd Bergmannfef1c772005-06-23 09:43:37 +100055#define SLB_VSID_LS ASM_CONST(0x0000000000000070) /* size of largepage */
56
David Gibson14b34662005-09-06 14:59:47 +100057#define SLB_VSID_KERNEL (SLB_VSID_KP)
58#define SLB_VSID_USER (SLB_VSID_KP|SLB_VSID_KS|SLB_VSID_C)
59
60#define SLBIE_C (0x08000000)
David Gibson1f8d4192005-05-05 16:15:13 -070061
62/*
63 * Hash table
64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#define HPTES_PER_GROUP 8
67
David Gibson96e28442005-07-13 01:11:42 -070068#define HPTE_V_AVPN_SHIFT 7
69#define HPTE_V_AVPN ASM_CONST(0xffffffffffffff80)
70#define HPTE_V_AVPN_VAL(x) (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
71#define HPTE_V_BOLTED ASM_CONST(0x0000000000000010)
72#define HPTE_V_LOCK ASM_CONST(0x0000000000000008)
73#define HPTE_V_LARGE ASM_CONST(0x0000000000000004)
74#define HPTE_V_SECONDARY ASM_CONST(0x0000000000000002)
75#define HPTE_V_VALID ASM_CONST(0x0000000000000001)
76
77#define HPTE_R_PP0 ASM_CONST(0x8000000000000000)
78#define HPTE_R_TS ASM_CONST(0x4000000000000000)
79#define HPTE_R_RPN_SHIFT 12
80#define HPTE_R_RPN ASM_CONST(0x3ffffffffffff000)
81#define HPTE_R_FLAGS ASM_CONST(0x00000000000003ff)
82#define HPTE_R_PP ASM_CONST(0x0000000000000003)
83
David Gibson1f8d4192005-05-05 16:15:13 -070084/* Values for PP (assumes Ks=0, Kp=1) */
85/* pp0 will always be 0 for linux */
86#define PP_RWXX 0 /* Supervisor read/write, User none */
87#define PP_RWRX 1 /* Supervisor read/write, User read */
88#define PP_RWRW 2 /* Supervisor read/write, User read/write */
89#define PP_RXRX 3 /* Supervisor read, User read */
90
91#ifndef __ASSEMBLY__
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093typedef struct {
David Gibson96e28442005-07-13 01:11:42 -070094 unsigned long v;
95 unsigned long r;
96} hpte_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David Gibson96e28442005-07-13 01:11:42 -070098extern hpte_t *htab_address;
99extern unsigned long htab_hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static inline unsigned long hpt_hash(unsigned long vpn, int large)
102{
103 unsigned long vsid;
104 unsigned long page;
105
106 if (large) {
107 vsid = vpn >> 4;
108 page = vpn & 0xf;
109 } else {
110 vsid = vpn >> 16;
111 page = vpn & 0xffff;
112 }
113
114 return (vsid & 0x7fffffffffUL) ^ page;
115}
116
117static inline void __tlbie(unsigned long va, int large)
118{
119 /* clear top 16 bits, non SLS segment */
120 va &= ~(0xffffULL << 48);
121
122 if (large) {
123 va &= HPAGE_MASK;
124 asm volatile("tlbie %0,1" : : "r"(va) : "memory");
125 } else {
126 va &= PAGE_MASK;
127 asm volatile("tlbie %0,0" : : "r"(va) : "memory");
128 }
129}
130
131static inline void tlbie(unsigned long va, int large)
132{
133 asm volatile("ptesync": : :"memory");
134 __tlbie(va, large);
135 asm volatile("eieio; tlbsync; ptesync": : :"memory");
136}
137
138static inline void __tlbiel(unsigned long va)
139{
140 /* clear top 16 bits, non SLS segment */
141 va &= ~(0xffffULL << 48);
142 va &= PAGE_MASK;
143
144 /*
145 * Thanks to Alan Modra we are now able to use machine specific
146 * assembly instructions (like tlbiel) by using the gas -many flag.
147 * However we have to support older toolchains so for the moment
148 * we hardwire it.
149 */
150#if 0
151 asm volatile("tlbiel %0" : : "r"(va) : "memory");
152#else
153 asm volatile(".long 0x7c000224 | (%0 << 11)" : : "r"(va) : "memory");
154#endif
155}
156
157static inline void tlbiel(unsigned long va)
158{
159 asm volatile("ptesync": : :"memory");
160 __tlbiel(va);
161 asm volatile("ptesync": : :"memory");
162}
163
David Gibson96e28442005-07-13 01:11:42 -0700164static inline unsigned long slot2va(unsigned long hpte_v, unsigned long slot)
R Sharadaf4c82d52005-06-25 14:58:08 -0700165{
David Gibson96e28442005-07-13 01:11:42 -0700166 unsigned long avpn = HPTE_V_AVPN_VAL(hpte_v);
R Sharadaf4c82d52005-06-25 14:58:08 -0700167 unsigned long va;
168
169 va = avpn << 23;
170
David Gibson96e28442005-07-13 01:11:42 -0700171 if (! (hpte_v & HPTE_V_LARGE)) {
R Sharadaf4c82d52005-06-25 14:58:08 -0700172 unsigned long vpi, pteg;
173
174 pteg = slot / HPTES_PER_GROUP;
David Gibson96e28442005-07-13 01:11:42 -0700175 if (hpte_v & HPTE_V_SECONDARY)
R Sharadaf4c82d52005-06-25 14:58:08 -0700176 pteg = ~pteg;
177
178 vpi = ((va >> 28) ^ pteg) & htab_hash_mask;
179
180 va |= vpi << PAGE_SHIFT;
181 }
182
183 return va;
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * Handle a fault by adding an HPTE. If the address can't be determined
188 * to be valid via Linux page tables, return 1. If handled return 0
189 */
190extern int __hash_page(unsigned long ea, unsigned long access,
191 unsigned long vsid, pte_t *ptep, unsigned long trap,
192 int local);
193
194extern void htab_finish_init(void);
195
David Gibson1f8d4192005-05-05 16:15:13 -0700196extern void hpte_init_native(void);
197extern void hpte_init_lpar(void);
198extern void hpte_init_iSeries(void);
199
200extern long pSeries_lpar_hpte_insert(unsigned long hpte_group,
201 unsigned long va, unsigned long prpn,
David Gibson96e28442005-07-13 01:11:42 -0700202 unsigned long vflags,
203 unsigned long rflags);
David Gibson1f8d4192005-05-05 16:15:13 -0700204extern long native_hpte_insert(unsigned long hpte_group, unsigned long va,
David Gibson96e28442005-07-13 01:11:42 -0700205 unsigned long prpn,
206 unsigned long vflags, unsigned long rflags);
David Gibson1f8d4192005-05-05 16:15:13 -0700207
David Gibson533f0812005-07-27 11:44:19 -0700208extern void stabs_alloc(void);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#endif /* __ASSEMBLY__ */
211
212/*
David Gibson1f8d4192005-05-05 16:15:13 -0700213 * VSID allocation
214 *
215 * We first generate a 36-bit "proto-VSID". For kernel addresses this
216 * is equal to the ESID, for user addresses it is:
217 * (context << 15) | (esid & 0x7fff)
218 *
219 * The two forms are distinguishable because the top bit is 0 for user
220 * addresses, whereas the top two bits are 1 for kernel addresses.
221 * Proto-VSIDs with the top two bits equal to 0b10 are reserved for
222 * now.
223 *
224 * The proto-VSIDs are then scrambled into real VSIDs with the
225 * multiplicative hash:
226 *
227 * VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
228 * where VSID_MULTIPLIER = 268435399 = 0xFFFFFC7
229 * VSID_MODULUS = 2^36-1 = 0xFFFFFFFFF
230 *
231 * This scramble is only well defined for proto-VSIDs below
232 * 0xFFFFFFFFF, so both proto-VSID and actual VSID 0xFFFFFFFFF are
233 * reserved. VSID_MULTIPLIER is prime, so in particular it is
234 * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
235 * Because the modulus is 2^n-1 we can compute it efficiently without
236 * a divide or extra multiply (see below).
237 *
238 * This scheme has several advantages over older methods:
239 *
240 * - We have VSIDs allocated for every kernel address
241 * (i.e. everything above 0xC000000000000000), except the very top
242 * segment, which simplifies several things.
243 *
244 * - We allow for 15 significant bits of ESID and 20 bits of
245 * context for user addresses. i.e. 8T (43 bits) of address space for
246 * up to 1M contexts (although the page table structure and context
247 * allocation will need changes to take advantage of this).
248 *
249 * - The scramble function gives robust scattering in the hash
250 * table (at least based on some initial results). The previous
251 * method was more susceptible to pathological cases giving excessive
252 * hash collisions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
David Gibson1f8d4192005-05-05 16:15:13 -0700254/*
255 * WARNING - If you change these you must make sure the asm
256 * implementations in slb_allocate (slb_low.S), do_stab_bolted
257 * (head.S) and ASM_VSID_SCRAMBLE (below) are changed accordingly.
258 *
259 * You'll also need to change the precomputed VSID values in head.S
260 * which are used by the iSeries firmware.
261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263#define VSID_MULTIPLIER ASM_CONST(200730139) /* 28-bit prime */
264#define VSID_BITS 36
265#define VSID_MODULUS ((1UL<<VSID_BITS)-1)
266
David Gibsone28f7fa2005-08-05 19:39:06 +1000267#define CONTEXT_BITS 19
268#define USER_ESID_BITS 16
269
270#define USER_VSID_RANGE (1UL << (USER_ESID_BITS + SID_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/*
273 * This macro generates asm code to compute the VSID scramble
274 * function. Used in slb_allocate() and do_stab_bolted. The function
275 * computed is: (protovsid*VSID_MULTIPLIER) % VSID_MODULUS
276 *
277 * rt = register continaing the proto-VSID and into which the
278 * VSID will be stored
279 * rx = scratch register (clobbered)
280 *
281 * - rt and rx must be different registers
282 * - The answer will end up in the low 36 bits of rt. The higher
283 * bits may contain other garbage, so you may need to mask the
284 * result.
285 */
286#define ASM_VSID_SCRAMBLE(rt, rx) \
287 lis rx,VSID_MULTIPLIER@h; \
288 ori rx,rx,VSID_MULTIPLIER@l; \
289 mulld rt,rt,rx; /* rt = rt * MULTIPLIER */ \
290 \
291 srdi rx,rt,VSID_BITS; \
292 clrldi rt,rt,(64-VSID_BITS); \
293 add rt,rt,rx; /* add high and low bits */ \
294 /* Now, r3 == VSID (mod 2^36-1), and lies between 0 and \
295 * 2^36-1+2^28-1. That in particular means that if r3 >= \
296 * 2^36-1, then r3+1 has the 2^36 bit set. So, if r3+1 has \
297 * the bit clear, r3 already has the answer we want, if it \
298 * doesn't, the answer is the low 36 bits of r3+1. So in all \
299 * cases the answer is the low 36 bits of (r3 + ((r3+1) >> 36))*/\
300 addi rx,rt,1; \
301 srdi rx,rx,VSID_BITS; /* extract 2^36 bit */ \
302 add rt,rt,rx
303
David Gibson1f8d4192005-05-05 16:15:13 -0700304
305#ifndef __ASSEMBLY__
306
307typedef unsigned long mm_context_id_t;
308
309typedef struct {
310 mm_context_id_t id;
311#ifdef CONFIG_HUGETLB_PAGE
David Gibsonc594ada2005-08-11 16:55:21 +1000312 u16 low_htlb_areas, high_htlb_areas;
David Gibson1f8d4192005-05-05 16:15:13 -0700313#endif
314} mm_context_t;
315
316
317static inline unsigned long vsid_scramble(unsigned long protovsid)
318{
319#if 0
320 /* The code below is equivalent to this function for arguments
321 * < 2^VSID_BITS, which is all this should ever be called
322 * with. However gcc is not clever enough to compute the
323 * modulus (2^n-1) without a second multiply. */
324 return ((protovsid * VSID_MULTIPLIER) % VSID_MODULUS);
325#else /* 1 */
326 unsigned long x;
327
328 x = protovsid * VSID_MULTIPLIER;
329 x = (x >> VSID_BITS) + (x & VSID_MODULUS);
330 return (x + ((x+1) >> VSID_BITS)) & VSID_MODULUS;
331#endif /* 1 */
332}
333
334/* This is only valid for addresses >= KERNELBASE */
335static inline unsigned long get_kernel_vsid(unsigned long ea)
336{
337 return vsid_scramble(ea >> SID_SHIFT);
338}
339
340/* This is only valid for user addresses (which are below 2^41) */
341static inline unsigned long get_vsid(unsigned long context, unsigned long ea)
342{
343 return vsid_scramble((context << USER_ESID_BITS)
344 | (ea >> SID_SHIFT));
345}
346
David Gibson488f8492005-07-27 11:44:21 -0700347#define VSID_SCRAMBLE(pvsid) (((pvsid) * VSID_MULTIPLIER) % VSID_MODULUS)
348#define KERNEL_VSID(ea) VSID_SCRAMBLE(GET_ESID(ea))
349
David Gibson1f8d4192005-05-05 16:15:13 -0700350#endif /* __ASSEMBLY */
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#endif /* _PPC64_MMU_H_ */