patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-ppc64 / mmu.h
1 /*
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>
18
19 #ifndef __ASSEMBLY__
20
21 /* Time to allow for more things here */
22 typedef unsigned long mm_context_id_t;
23 typedef struct {
24         mm_context_id_t id;
25 #ifdef CONFIG_HUGETLB_PAGE
26         u16 htlb_segs; /* bitmask */
27 #endif
28 } mm_context_t;
29
30 #ifdef CONFIG_HUGETLB_PAGE
31 #define KERNEL_LOW_HPAGES       .htlb_segs = 0,
32 #else
33 #define KERNEL_LOW_HPAGES
34 #endif
35
36 #define KERNEL_CONTEXT(ea) ({ \
37                 mm_context_t ctx = { .id = REGION_ID(ea), KERNEL_LOW_HPAGES}; \
38                 ctx; })
39
40 /*
41  * Hardware Segment Lookaside Buffer Entry
42  * This structure has been padded out to two 64b doublewords (actual SLBE's are
43  * 94 bits).  This padding facilites use by the segment management
44  * instructions.
45  */
46 typedef struct {
47         unsigned long esid: 36; /* Effective segment ID */
48         unsigned long resv0:20; /* Reserved */
49         unsigned long v:     1; /* Entry valid (v=1) or invalid */
50         unsigned long resv1: 1; /* Reserved */
51         unsigned long ks:    1; /* Supervisor (privileged) state storage key */
52         unsigned long kp:    1; /* Problem state storage key */
53         unsigned long n:     1; /* No-execute if n=1 */
54         unsigned long resv2: 3; /* padding to a 64b boundary */
55 } ste_dword0;
56
57 typedef struct {
58         unsigned long vsid: 52; /* Virtual segment ID */
59         unsigned long resv0:12; /* Padding to a 64b boundary */
60 } ste_dword1;
61
62 typedef struct _STE {
63         union {
64                 unsigned long dword0;
65                 ste_dword0    dw0;
66         } dw0;
67
68         union {
69                 unsigned long dword1;
70                 ste_dword1    dw1;
71         } dw1;
72 } STE;
73
74 typedef struct {
75         unsigned long esid: 36; /* Effective segment ID */
76         unsigned long v:     1; /* Entry valid (v=1) or invalid */
77         unsigned long null1:15; /* padding to a 64b boundary */
78         unsigned long index:12; /* Index to select SLB entry. Used by slbmte */
79 } slb_dword0;
80
81 typedef struct {
82         unsigned long vsid: 52; /* Virtual segment ID */
83         unsigned long ks:    1; /* Supervisor (privileged) state storage key */
84         unsigned long kp:    1; /* Problem state storage key */
85         unsigned long n:     1; /* No-execute if n=1 */
86         unsigned long l:     1; /* Virt pages are large (l=1) or 4KB (l=0) */
87         unsigned long c:     1; /* Class */
88         unsigned long resv0: 7; /* Padding to a 64b boundary */
89 } slb_dword1;
90
91 typedef struct {
92         union {
93                 unsigned long dword0;
94                 slb_dword0    dw0;
95         } dw0;
96
97         union {
98                 unsigned long dword1;
99                 slb_dword1    dw1;
100         } dw1;
101 } SLBE;
102
103 /*
104  * This structure is used in paca.h where the layout depends on the 
105  * size being 24B.
106  */
107 typedef struct {
108         unsigned long   real;
109         unsigned long   virt;
110         unsigned long   next_round_robin;
111 } STAB;
112
113 /* Hardware Page Table Entry */
114
115 #define HPTES_PER_GROUP 8
116
117 typedef struct {
118         unsigned long avpn:57; /* vsid | api == avpn  */
119         unsigned long :     2; /* Software use */
120         unsigned long bolted: 1; /* HPTE is "bolted" */
121         unsigned long lock: 1; /* lock on pSeries SMP */
122         unsigned long l:    1; /* Virtual page is large (L=1) or 4 KB (L=0) */
123         unsigned long h:    1; /* Hash function identifier */
124         unsigned long v:    1; /* Valid (v=1) or invalid (v=0) */
125 } Hpte_dword0;
126
127 typedef struct {
128         unsigned long pp0:  1; /* Page protection bit 0 */
129         unsigned long ts:   1; /* Tag set bit */
130         unsigned long rpn: 50; /* Real page number */
131         unsigned long :     2; /* Reserved */
132         unsigned long ac:   1; /* Address compare */ 
133         unsigned long r:    1; /* Referenced */
134         unsigned long c:    1; /* Changed */
135         unsigned long w:    1; /* Write-thru cache mode */
136         unsigned long i:    1; /* Cache inhibited */
137         unsigned long m:    1; /* Memory coherence required */
138         unsigned long g:    1; /* Guarded */
139         unsigned long n:    1; /* No-execute */
140         unsigned long pp:   2; /* Page protection bits 1:2 */
141 } Hpte_dword1;
142
143 typedef struct {
144         char padding[6];                /* padding */
145         unsigned long :       6;        /* padding */ 
146         unsigned long flags: 10;        /* HPTE flags */
147 } Hpte_dword1_flags;
148
149 typedef struct {
150         union {
151                 unsigned long dword0;
152                 Hpte_dword0   dw0;
153         } dw0;
154
155         union {
156                 unsigned long dword1;
157                 Hpte_dword1 dw1;
158                 Hpte_dword1_flags flags;
159         } dw1;
160 } HPTE; 
161
162 /* Values for PP (assumes Ks=0, Kp=1) */
163 /* pp0 will always be 0 for linux     */
164 #define PP_RWXX 0       /* Supervisor read/write, User none */
165 #define PP_RWRX 1       /* Supervisor read/write, User read */
166 #define PP_RWRW 2       /* Supervisor read/write, User read/write */
167 #define PP_RXRX 3       /* Supervisor read,       User read */
168
169
170 typedef struct {
171         HPTE *          htab;
172         unsigned long   htab_num_ptegs;
173         unsigned long   htab_hash_mask;
174         unsigned long   next_round_robin;
175         unsigned long   last_kernel_address;
176 } HTAB;
177
178 extern HTAB htab_data;
179
180 void invalidate_hpte( unsigned long slot );
181 long select_hpte_slot( unsigned long vpn );
182 void create_valid_hpte( unsigned long slot, unsigned long vpn,
183                         unsigned long prpn, unsigned hash,
184                         void * ptep, unsigned hpteflags,
185                         unsigned bolted );
186
187 #define PD_SHIFT (10+12)                /* Page directory */
188 #define PD_MASK  0x02FF
189 #define PT_SHIFT (12)                   /* Page Table */
190 #define PT_MASK  0x02FF
191
192 #define LARGE_PAGE_SHIFT 24
193
194 static inline unsigned long hpt_hash(unsigned long vpn, int large)
195 {
196         unsigned long vsid;
197         unsigned long page;
198
199         if (large) {
200                 vsid = vpn >> 4;
201                 page = vpn & 0xf;
202         } else {
203                 vsid = vpn >> 16;
204                 page = vpn & 0xffff;
205         }
206
207         return (vsid & 0x7fffffffffUL) ^ page;
208 }
209
210 static inline void __tlbie(unsigned long va, int large)
211 {
212         /* clear top 16 bits, non SLS segment */
213         va &= ~(0xffffULL << 48);
214
215         if (large)
216                 asm volatile("tlbie %0,1" : : "r"(va) : "memory");
217         else
218                 asm volatile("tlbie %0,0" : : "r"(va) : "memory");
219 }
220
221 static inline void tlbie(unsigned long va, int large)
222 {
223         asm volatile("ptesync": : :"memory");
224         __tlbie(va, large);
225         asm volatile("eieio; tlbsync; ptesync": : :"memory");
226 }
227
228 static inline void __tlbiel(unsigned long va)
229 {
230         /* clear top 16 bits, non SLS segment */
231         va &= ~(0xffffULL << 48);
232
233         /* 
234          * Thanks to Alan Modra we are now able to use machine specific 
235          * assembly instructions (like tlbiel) by using the gas -many flag.
236          * However we have to support older toolchains so for the moment 
237          * we hardwire it.
238          */
239 #if 0
240         asm volatile("tlbiel %0" : : "r"(va) : "memory");
241 #else
242         asm volatile(".long 0x7c000224 | (%0 << 11)" : : "r"(va) : "memory");
243 #endif
244 }
245
246 static inline void tlbiel(unsigned long va)
247 {
248         asm volatile("ptesync": : :"memory");
249         __tlbiel(va);
250         asm volatile("ptesync": : :"memory");
251 }
252
253 /*
254  * Handle a fault by adding an HPTE. If the address can't be determined
255  * to be valid via Linux page tables, return 1. If handled return 0
256  */
257 extern int __hash_page(unsigned long ea, unsigned long access,
258                        unsigned long vsid, pte_t *ptep, unsigned long trap,
259                        int local);
260
261 extern void htab_finish_init(void);
262
263 #endif /* __ASSEMBLY__ */
264
265 /*
266  * Location of cpu0's segment table
267  */
268 #define STAB0_PAGE      0x9
269 #define STAB0_PHYS_ADDR (STAB0_PAGE<<PAGE_SHIFT)
270 #define STAB0_VIRT_ADDR (KERNELBASE+STAB0_PHYS_ADDR)
271
272 /* Block size masks */
273 #define BL_128K 0x000
274 #define BL_256K 0x001
275 #define BL_512K 0x003
276 #define BL_1M   0x007
277 #define BL_2M   0x00F
278 #define BL_4M   0x01F
279 #define BL_8M   0x03F
280 #define BL_16M  0x07F
281 #define BL_32M  0x0FF
282 #define BL_64M  0x1FF
283 #define BL_128M 0x3FF
284 #define BL_256M 0x7FF
285
286 /* Used to set up SDR1 register */
287 #define HASH_TABLE_SIZE_64K     0x00010000
288 #define HASH_TABLE_SIZE_128K    0x00020000
289 #define HASH_TABLE_SIZE_256K    0x00040000
290 #define HASH_TABLE_SIZE_512K    0x00080000
291 #define HASH_TABLE_SIZE_1M      0x00100000
292 #define HASH_TABLE_SIZE_2M      0x00200000
293 #define HASH_TABLE_SIZE_4M      0x00400000
294 #define HASH_TABLE_MASK_64K     0x000   
295 #define HASH_TABLE_MASK_128K    0x001   
296 #define HASH_TABLE_MASK_256K    0x003   
297 #define HASH_TABLE_MASK_512K    0x007
298 #define HASH_TABLE_MASK_1M      0x00F   
299 #define HASH_TABLE_MASK_2M      0x01F   
300 #define HASH_TABLE_MASK_4M      0x03F   
301
302 /* These are the Ks and Kp from the PowerPC books.  For proper operation,
303  * Ks = 0, Kp = 1.
304  */
305 #define MI_AP           786
306 #define MI_Ks           0x80000000      /* Should not be set */
307 #define MI_Kp           0x40000000      /* Should always be set */
308
309 /* The effective page number register.  When read, contains the information
310  * about the last instruction TLB miss.  When MI_RPN is written, bits in
311  * this register are used to create the TLB entry.
312  */
313 #define MI_EPN          787
314 #define MI_EPNMASK      0xfffff000      /* Effective page number for entry */
315 #define MI_EVALID       0x00000200      /* Entry is valid */
316 #define MI_ASIDMASK     0x0000000f      /* ASID match value */
317                                         /* Reset value is undefined */
318
319 /* A "level 1" or "segment" or whatever you want to call it register.
320  * For the instruction TLB, it contains bits that get loaded into the
321  * TLB entry when the MI_RPN is written.
322  */
323 #define MI_TWC          789
324 #define MI_APG          0x000001e0      /* Access protection group (0) */
325 #define MI_GUARDED      0x00000010      /* Guarded storage */
326 #define MI_PSMASK       0x0000000c      /* Mask of page size bits */
327 #define MI_PS8MEG       0x0000000c      /* 8M page size */
328 #define MI_PS512K       0x00000004      /* 512K page size */
329 #define MI_PS4K_16K     0x00000000      /* 4K or 16K page size */
330 #define MI_SVALID       0x00000001      /* Segment entry is valid */
331                                         /* Reset value is undefined */
332
333 /* Real page number.  Defined by the pte.  Writing this register
334  * causes a TLB entry to be created for the instruction TLB, using
335  * additional information from the MI_EPN, and MI_TWC registers.
336  */
337 #define MI_RPN          790
338
339 /* Define an RPN value for mapping kernel memory to large virtual
340  * pages for boot initialization.  This has real page number of 0,
341  * large page size, shared page, cache enabled, and valid.
342  * Also mark all subpages valid and write access.
343  */
344 #define MI_BOOTINIT     0x000001fd
345
346 #define MD_CTR          792     /* Data TLB control register */
347 #define MD_GPM          0x80000000      /* Set domain manager mode */
348 #define MD_PPM          0x40000000      /* Set subpage protection */
349 #define MD_CIDEF        0x20000000      /* Set cache inhibit when MMU dis */
350 #define MD_WTDEF        0x10000000      /* Set writethrough when MMU dis */
351 #define MD_RSV4I        0x08000000      /* Reserve 4 TLB entries */
352 #define MD_TWAM         0x04000000      /* Use 4K page hardware assist */
353 #define MD_PPCS         0x02000000      /* Use MI_RPN prob/priv state */
354 #define MD_IDXMASK      0x00001f00      /* TLB index to be loaded */
355 #define MD_RESETVAL     0x04000000      /* Value of register at reset */
356
357 #define M_CASID         793     /* Address space ID (context) to match */
358 #define MC_ASIDMASK     0x0000000f      /* Bits used for ASID value */
359
360
361 /* These are the Ks and Kp from the PowerPC books.  For proper operation,
362  * Ks = 0, Kp = 1.
363  */
364 #define MD_AP           794
365 #define MD_Ks           0x80000000      /* Should not be set */
366 #define MD_Kp           0x40000000      /* Should always be set */
367
368 /* The effective page number register.  When read, contains the information
369  * about the last instruction TLB miss.  When MD_RPN is written, bits in
370  * this register are used to create the TLB entry.
371  */
372 #define MD_EPN          795
373 #define MD_EPNMASK      0xfffff000      /* Effective page number for entry */
374 #define MD_EVALID       0x00000200      /* Entry is valid */
375 #define MD_ASIDMASK     0x0000000f      /* ASID match value */
376                                         /* Reset value is undefined */
377
378 /* The pointer to the base address of the first level page table.
379  * During a software tablewalk, reading this register provides the address
380  * of the entry associated with MD_EPN.
381  */
382 #define M_TWB           796
383 #define M_L1TB          0xfffff000      /* Level 1 table base address */
384 #define M_L1INDX        0x00000ffc      /* Level 1 index, when read */
385                                         /* Reset value is undefined */
386
387 /* A "level 1" or "segment" or whatever you want to call it register.
388  * For the data TLB, it contains bits that get loaded into the TLB entry
389  * when the MD_RPN is written.  It is also provides the hardware assist
390  * for finding the PTE address during software tablewalk.
391  */
392 #define MD_TWC          797
393 #define MD_L2TB         0xfffff000      /* Level 2 table base address */
394 #define MD_L2INDX       0xfffffe00      /* Level 2 index (*pte), when read */
395 #define MD_APG          0x000001e0      /* Access protection group (0) */
396 #define MD_GUARDED      0x00000010      /* Guarded storage */
397 #define MD_PSMASK       0x0000000c      /* Mask of page size bits */
398 #define MD_PS8MEG       0x0000000c      /* 8M page size */
399 #define MD_PS512K       0x00000004      /* 512K page size */
400 #define MD_PS4K_16K     0x00000000      /* 4K or 16K page size */
401 #define MD_WT           0x00000002      /* Use writethrough page attribute */
402 #define MD_SVALID       0x00000001      /* Segment entry is valid */
403                                         /* Reset value is undefined */
404
405
406 /* Real page number.  Defined by the pte.  Writing this register
407  * causes a TLB entry to be created for the data TLB, using
408  * additional information from the MD_EPN, and MD_TWC registers.
409  */
410 #define MD_RPN          798
411
412 /* This is a temporary storage register that could be used to save
413  * a processor working register during a tablewalk.
414  */
415 #define M_TW            799
416
417 #endif /* _PPC64_MMU_H_ */