pgtable.h 20.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifdef __KERNEL__
#ifndef _PPC_PGTABLE_H
#define _PPC_PGTABLE_H

#include <linux/config.h>

#ifndef __ASSEMBLY__
#include <linux/sched.h>
#include <linux/threads.h>
#include <asm/processor.h>		/* For TASK_SIZE */
#include <asm/mmu.h>
#include <asm/page.h>

extern unsigned long va_to_phys(unsigned long address);
extern pte_t *va_to_pte(unsigned long address);
extern unsigned long ioremap_bot, ioremap_base;
#endif /* __ASSEMBLY__ */

/*
 * The PowerPC MMU uses a hash table containing PTEs, together with
 * a set of 16 segment registers (on 32-bit implementations), to define
 * the virtual to physical address mapping.
 *
 * We use the hash table as an extended TLB, i.e. a cache of currently
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28 29 30
 * active mappings.  We maintain a two-level page table tree, much
 * like that used by the i386, for the sake of the Linux memory
 * management code.  Low-level assembler code in hashtable.S
 * (procedure hash_page) is responsible for extracting ptes from the
 * tree and putting them into the hash table when necessary, and
 * updating the accessed and modified bits in the page table tree.
Linus Torvalds's avatar
Linus Torvalds committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 */

/*
 * The PowerPC MPC8xx uses a TLB with hardware assisted, software tablewalk.
 * We also use the two level tables, but we can put the real bits in them
 * needed for the TLB and tablewalk.  These definitions require Mx_CTR.PPM = 0,
 * Mx_CTR.PPCS = 0, and MD_CTR.TWAM = 1.  The level 2 descriptor has
 * additional page protection (when Mx_CTR.PPCS = 1) that allows TLB hit
 * based upon user/super access.  The TLB does not have accessed nor write
 * protect.  We assume that if the TLB get loaded with an entry it is
 * accessed, and overload the changed bit for write protect.  We use
 * two bits in the software pte that are supposed to be set to zero in
 * the TLB entry (24 and 25) for these indicators.  Although the level 1
 * descriptor contains the guarded and writethrough/copyback bits, we can
 * set these at the page level since they get copied from the Mx_TWC
 * register when the TLB entry is loaded.  We will use bit 27 for guard, since
 * that is where it exists in the MD_TWC, and bit 26 for writethrough.
 * These will get masked from the level 2 descriptor at TLB load time, and
 * copied to the MD_TWC before it gets loaded.
50 51 52 53 54 55
 * Large page sizes added.  We currently support two sizes, 4K and 8M.
 * This also allows a TLB hander optimization because we can directly
 * load the PMD into MD_TWC.  The 8M pages are only used for kernel
 * mapping of well known areas.  The PMD (PGD) entries contain control
 * flags in addition to the address, so care must be taken that the
 * software no longer assumes these are only pointers.
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 */

/*
 * At present, all PowerPC 400-class processors share a similar TLB
 * architecture. The instruction and data sides share a unified,
 * 64-entry, fully-associative TLB which is maintained totally under
 * software control. In addition, the instruction side has a
 * hardware-managed, 4-entry, fully-associative TLB which serves as a
 * first level to the shared TLB. These two TLBs are known as the UTLB
 * and ITLB, respectively (see "mmu.h" for definitions).
 */

/* PMD_SHIFT determines the size of the area mapped by the second-level page tables */
#define PMD_SHIFT	22
#define PMD_SIZE	(1UL << PMD_SHIFT)
#define PMD_MASK	(~(PMD_SIZE-1))

/* PGDIR_SHIFT determines what a third-level page table entry can map */
#define PGDIR_SHIFT	22
#define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
#define PGDIR_MASK	(~(PGDIR_SIZE-1))

/*
 * entries per page directory level: our page-table tree is two-level, so
 * we don't really have any PMD directory.
 */
#define PTRS_PER_PTE	1024
#define PTRS_PER_PMD	1
#define PTRS_PER_PGD	1024
#define USER_PTRS_PER_PGD	(TASK_SIZE / PGDIR_SIZE)
#define FIRST_USER_PGD_NR	0

#define USER_PGD_PTRS (PAGE_OFFSET >> PGDIR_SHIFT)
#define KERNEL_PGD_PTRS (PTRS_PER_PGD-USER_PGD_PTRS)

#define pte_ERROR(e) \
	printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
#define pmd_ERROR(e) \
	printk("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, pmd_val(e))
#define pgd_ERROR(e) \
	printk("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))

/*
 * Just any arbitrary offset to the start of the vmalloc VM area: the
 * current 64MB value just means that there will be a 64MB "hole" after the
 * physical memory until the kernel virtual memory starts.  That means that
 * any out-of-bounds memory accesses will hopefully be caught.
 * The vmalloc() routines leaves a hole of 4kB between each vmalloced
 * area for the same reason. ;)
 *
 * We no longer map larger than phys RAM with the BATs so we don't have
 * to worry about the VMALLOC_OFFSET causing problems.  We do have to worry
 * about clashes between our early calls to ioremap() that start growing down
 * from ioremap_base being run into the VM area allocations (growing upwards
 * from VMALLOC_START).  For this reason we have ioremap_bot to check when
 * we actually run into our mappings setup in the early boot with the VM
 * system.  This really does become a problem for machines with good amounts
 * of RAM.  -- Cort
 */
#define VMALLOC_OFFSET (0x1000000) /* 16M */
#define VMALLOC_START ((((long)high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1)))
#define VMALLOC_VMADDR(x) ((unsigned long)(x))
#define VMALLOC_END	ioremap_bot

/*
 * Bits in a linux-style PTE.  These match the bits in the
 * (hardware-defined) PowerPC PTE as closely as possible.
 */

125
#if defined(CONFIG_40x)
126

127
/* There are several potential gotchas here.  The 40x hardware TLBLO
128 129 130 131 132 133 134
   field looks like this:

   0  1  2  3  4  ... 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   RPN.....................  0  0 EX WR ZSEL.......  W  I  M  G

   Where possible we make the Linux PTE bits match up with this

135
   - bits 20 and 21 must be cleared, because we use 4k pages (40x can
136 137 138 139 140 141 142
     support down to 1k pages), this is done in the TLBMiss exception
     handler.
   - We use only zones 0 (for kernel pages) and 1 (for user pages)
     of the 16 available.  Bit 24-26 of the TLB are cleared in the TLB
     miss handler.  Bit 27 is PAGE_USER, thus selecting the correct
     zone.
   - PRESENT *must* be in the bottom two bits because swap cache
143
     entries use the top 30 bits.  Because 40x doesn't support SMP
144 145 146 147
     anyway, M is irrelevant so we borrow it for PAGE_PRESENT.  Bit 30
     is cleared in the TLB miss handler before the TLB entry is loaded.
   - All other bits of the PTE are loaded into TLBLO without
     modification, leaving us only the bits 20, 21, 24, 25, 26, 30 for
148 149
     software PTE bits.  We actually use use bits 21, 24, 25, and
     30 respectively for the software bits: ACCESSED, DIRTY, RW, and
150 151 152
     PRESENT.
*/

153
/* Definitions for 40x embedded chips. */
Linus Torvalds's avatar
Linus Torvalds committed
154
#define	_PAGE_GUARDED	0x001	/* G: page is guarded from prefetch */
155
#define _PAGE_PRESENT	0x002	/* software: PTE contains a translation */
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158
#define	_PAGE_NO_CACHE	0x004	/* I: caching is inhibited */
#define	_PAGE_WRITETHRU	0x008	/* W: caching is write-through */
#define	_PAGE_USER	0x010	/* matches one of the zone permission bits */
159 160 161 162 163
#define	_PAGE_RW	0x040	/* software: Writes permitted */
#define	_PAGE_DIRTY	0x080	/* software: dirty page */
#define _PAGE_HWWRITE	0x100	/* hardware: Dirty & RW, set in exception */
#define _PAGE_HWEXEC	0x200	/* hardware: EX permission */
#define _PAGE_ACCESSED	0x400	/* software: R: page referenced */
Linus Torvalds's avatar
Linus Torvalds committed
164

165 166 167 168 169 170 171
#define _PMD_PRESENT	0x400	/* PMD points to page of PTEs */
#define _PMD_BAD	0x802
#define _PMD_SIZE	0x0e0	/* size field, != 0 for large-page PMD entry */
#define _PMD_SIZE_4M	0x0c0
#define _PMD_SIZE_16M	0x0e0
#define PMD_PAGE_SIZE(pmdval)	(1024 << (((pmdval) & _PMD_SIZE) >> 4))

Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179 180
#elif defined(CONFIG_8xx)
/* Definitions for 8xx embedded chips. */
#define _PAGE_PRESENT	0x0001	/* Page is valid */
#define _PAGE_NO_CACHE	0x0002	/* I: cache inhibit */
#define _PAGE_SHARED	0x0004	/* No ASID (context) compare */

/* These five software bits must be masked out when the entry is loaded
 * into the TLB.
 */
Linus Torvalds's avatar
Linus Torvalds committed
181
#define _PAGE_EXEC	0x0008	/* software: i-cache coherency required */
Linus Torvalds's avatar
Linus Torvalds committed
182
#define _PAGE_GUARDED	0x0010	/* software: guarded access */
183
#define _PAGE_DIRTY	0x0020	/* software: page changed */
Linus Torvalds's avatar
Linus Torvalds committed
184 185 186
#define _PAGE_RW	0x0040	/* software: user write access allowed */
#define _PAGE_ACCESSED	0x0080	/* software: page referenced */

187 188 189 190
/* Setting any bits in the nibble with the follow two controls will
 * require a TLB exception handler change.  It is assumed unused bits
 * are always zero.
 */
Linus Torvalds's avatar
Linus Torvalds committed
191
#define _PAGE_HWWRITE	0x0100	/* h/w write enable: never set in Linux PTE */
Linus Torvalds's avatar
Linus Torvalds committed
192
#define _PAGE_USER	0x0800	/* One of the PP bits, the other is USER&~RW */
Linus Torvalds's avatar
Linus Torvalds committed
193

194 195
#define _PMD_PRESENT	0x0001
#define _PMD_BAD	0x0ff0
196 197 198
#define _PMD_PAGE_MASK	0x000c
#define _PMD_PAGE_8M	0x000c

199 200 201 202 203 204 205 206 207 208
/*
 * The 8xx TLB miss handler allegedly sets _PAGE_ACCESSED in the PTE
 * for an address even if _PAGE_PRESENT is not set, as a performance
 * optimization.  This is a bug if you ever want to use swap unless
 * _PAGE_ACCESSED is 2, which it isn't, or unless you have 8xx-specific
 * definitions for __swp_entry etc. below, which would be gross.
 *  -- paulus
 */
#define _PTE_NONE_MASK _PAGE_ACCESSED

Linus Torvalds's avatar
Linus Torvalds committed
209 210 211
#else /* CONFIG_6xx */
/* Definitions for 60x, 740/750, etc. */
#define _PAGE_PRESENT	0x001	/* software: pte contains a translation */
Linus Torvalds's avatar
Linus Torvalds committed
212 213 214
#define _PAGE_HASHPTE	0x002	/* hash_page has made an HPTE for this pte */
#define _PAGE_USER	0x004	/* usermode access allowed */
#define _PAGE_GUARDED	0x008	/* G: prohibit speculative access */
Linus Torvalds's avatar
Linus Torvalds committed
215 216 217 218 219
#define _PAGE_COHERENT	0x010	/* M: enforce memory coherence (SMP systems) */
#define _PAGE_NO_CACHE	0x020	/* I: cache inhibit */
#define _PAGE_WRITETHRU	0x040	/* W: cache write-through */
#define _PAGE_DIRTY	0x080	/* C: page changed */
#define _PAGE_ACCESSED	0x100	/* R: page referenced */
Linus Torvalds's avatar
Linus Torvalds committed
220 221
#define _PAGE_EXEC	0x200	/* software: i-cache coherency required */
#define _PAGE_RW	0x400	/* software: user write access allowed */
222 223 224 225 226 227

#define _PTE_NONE_MASK	_PAGE_HASHPTE

#define _PMD_PRESENT	0
#define _PMD_PRESENT_MASK (PAGE_MASK)
#define _PMD_BAD	(~PAGE_MASK)
Linus Torvalds's avatar
Linus Torvalds committed
228 229
#endif

230 231
/*
 * Some bits are only used on some cpu families...
Linus Torvalds's avatar
Linus Torvalds committed
232
 */
Linus Torvalds's avatar
Linus Torvalds committed
233 234
#ifndef _PAGE_HASHPTE
#define _PAGE_HASHPTE	0
235 236 237
#endif
#ifndef _PTE_NONE_MASK
#define _PTE_NONE_MASK 0
Linus Torvalds's avatar
Linus Torvalds committed
238 239
#endif
#ifndef _PAGE_SHARED
Linus Torvalds's avatar
Linus Torvalds committed
240 241
#define _PAGE_SHARED	0
#endif
Linus Torvalds's avatar
Linus Torvalds committed
242 243 244
#ifndef _PAGE_HWWRITE
#define _PAGE_HWWRITE	0
#endif
245 246 247 248 249
#ifndef _PAGE_HWEXEC
#define _PAGE_HWEXEC	0
#endif
#ifndef _PAGE_EXEC
#define _PAGE_EXEC	0
Linus Torvalds's avatar
Linus Torvalds committed
250
#endif
251 252 253 254 255 256 257
#ifndef _PMD_PRESENT_MASK
#define _PMD_PRESENT_MASK	_PMD_PRESENT
#endif
#ifndef _PMD_SIZE
#define _PMD_SIZE	0
#define PMD_PAGE_SIZE(pmd)	bad_call_to_PMD_PAGE_SIZE()
#endif
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260

#define _PAGE_CHG_MASK	(PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)

Linus Torvalds's avatar
Linus Torvalds committed
261 262 263 264 265 266
/*
 * Note: the _PAGE_COHERENT bit automatically gets set in the hardware
 * PTE if CONFIG_SMP is defined (hash_page does this); there is no need
 * to have it in the Linux PTE, and in fact the bit could be reused for
 * another purpose.  -- paulus.
 */
Linus Torvalds's avatar
Linus Torvalds committed
267

268 269 270 271 272 273 274 275 276 277 278
#define _PAGE_BASE	(_PAGE_PRESENT | _PAGE_ACCESSED)
#define _PAGE_WRENABLE	(_PAGE_RW | _PAGE_DIRTY | _PAGE_HWWRITE)
#define _PAGE_KERNEL	(_PAGE_BASE | _PAGE_SHARED | _PAGE_WRENABLE)

#ifdef CONFIG_PPC_STD_MMU
/* On standard PPC MMU, no user access implies kernel read/write access,
 * so to write-protect kernel memory we must turn on user access */
#define _PAGE_KERNEL_RO	(_PAGE_BASE | _PAGE_SHARED | _PAGE_USER)
#else
#define _PAGE_KERNEL_RO	(_PAGE_BASE | _PAGE_SHARED)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
279

280 281
#define _PAGE_IO	(_PAGE_KERNEL | _PAGE_NO_CACHE | _PAGE_GUARDED)
#define _PAGE_RAM	(_PAGE_KERNEL | _PAGE_HWEXEC)
282 283 284 285 286 287

#if defined(CONFIG_KGDB) || defined(CONFIG_XMON)
/* We want the debuggers to be able to set breakpoints anywhere, so
 * don't write protect the kernel text */
#define _PAGE_RAM_TEXT	_PAGE_RAM
#else
288
#define _PAGE_RAM_TEXT	(_PAGE_KERNEL_RO | _PAGE_HWEXEC)
289 290
#endif

Linus Torvalds's avatar
Linus Torvalds committed
291
#define PAGE_NONE	__pgprot(_PAGE_BASE)
Linus Torvalds's avatar
Linus Torvalds committed
292
#define PAGE_READONLY	__pgprot(_PAGE_BASE | _PAGE_USER)
Linus Torvalds's avatar
Linus Torvalds committed
293 294 295 296 297 298
#define PAGE_READONLY_X	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC)
#define PAGE_SHARED	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW)
#define PAGE_SHARED_X	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW | _PAGE_EXEC)
#define PAGE_COPY	__pgprot(_PAGE_BASE | _PAGE_USER)
#define PAGE_COPY_X	__pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC)

299
#define PAGE_KERNEL	__pgprot(_PAGE_RAM)
Linus Torvalds's avatar
Linus Torvalds committed
300 301 302 303 304 305 306 307

/*
 * The PowerPC can only do execute protection on a segment (256MB) basis,
 * not on a page basis.  So we consider execute permission the same as read.
 * Also, write permissions imply read permissions.
 * This is the closest we can get..
 */
#define __P000	PAGE_NONE
Linus Torvalds's avatar
Linus Torvalds committed
308
#define __P001	PAGE_READONLY_X
Linus Torvalds's avatar
Linus Torvalds committed
309
#define __P010	PAGE_COPY
Linus Torvalds's avatar
Linus Torvalds committed
310
#define __P011	PAGE_COPY_X
Linus Torvalds's avatar
Linus Torvalds committed
311
#define __P100	PAGE_READONLY
Linus Torvalds's avatar
Linus Torvalds committed
312
#define __P101	PAGE_READONLY_X
Linus Torvalds's avatar
Linus Torvalds committed
313
#define __P110	PAGE_COPY
Linus Torvalds's avatar
Linus Torvalds committed
314
#define __P111	PAGE_COPY_X
Linus Torvalds's avatar
Linus Torvalds committed
315 316

#define __S000	PAGE_NONE
Linus Torvalds's avatar
Linus Torvalds committed
317
#define __S001	PAGE_READONLY_X
Linus Torvalds's avatar
Linus Torvalds committed
318
#define __S010	PAGE_SHARED
Linus Torvalds's avatar
Linus Torvalds committed
319
#define __S011	PAGE_SHARED_X
Linus Torvalds's avatar
Linus Torvalds committed
320
#define __S100	PAGE_READONLY
Linus Torvalds's avatar
Linus Torvalds committed
321
#define __S101	PAGE_READONLY_X
Linus Torvalds's avatar
Linus Torvalds committed
322
#define __S110	PAGE_SHARED
Linus Torvalds's avatar
Linus Torvalds committed
323
#define __S111	PAGE_SHARED_X
Linus Torvalds's avatar
Linus Torvalds committed
324 325

#ifndef __ASSEMBLY__
326 327 328 329
/* Make sure we get a link error if PMD_PAGE_SIZE is ever called on a
 * kernel without large page PMD support */
extern unsigned long bad_call_to_PMD_PAGE_SIZE(void);

330 331 332 333 334 335 336 337 338 339
/*
 * Conversions between PTE values and page frame numbers.
 */

#define pte_pfn(x)		(pte_val(x) >> PAGE_SHIFT)
#define pte_page(x)		pfn_to_page(pte_pfn(x))

#define pfn_pte(pfn, prot)	__pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
#define mk_pte(page, prot)	pfn_pte(page_to_pfn(page), prot)

Linus Torvalds's avatar
Linus Torvalds committed
340 341 342 343 344 345 346 347 348
/*
 * ZERO_PAGE is a global shared page that is always zero: used
 * for zero-mapped memory areas etc..
 */
extern unsigned long empty_zero_page[1024];
#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))

#endif /* __ASSEMBLY__ */

Linus Torvalds's avatar
Linus Torvalds committed
349
#define pte_none(pte)		((pte_val(pte) & ~_PTE_NONE_MASK) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
350
#define pte_present(pte)	(pte_val(pte) & _PAGE_PRESENT)
Linus Torvalds's avatar
Linus Torvalds committed
351
#define pte_clear(ptep)		do { set_pte((ptep), __pte(0)); } while (0)
Linus Torvalds's avatar
Linus Torvalds committed
352 353

#define pmd_none(pmd)		(!pmd_val(pmd))
354 355
#define	pmd_bad(pmd)		(pmd_val(pmd) & _PMD_BAD)
#define	pmd_present(pmd)	(pmd_val(pmd) & _PMD_PRESENT_MASK)
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360 361 362 363
#define	pmd_clear(pmdp)		do { pmd_val(*(pmdp)) = 0; } while (0)

#ifndef __ASSEMBLY__
/*
 * The "pgd_xxx()" functions here are trivial for a folded two-level
 * setup: the pgd is never bad, and a pmd always exists (as it's folded
 * into the pgd entry)
 */
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366
static inline int pgd_none(pgd_t pgd)		{ return 0; }
static inline int pgd_bad(pgd_t pgd)		{ return 0; }
static inline int pgd_present(pgd_t pgd)	{ return 1; }
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370 371 372 373 374 375
#define pgd_clear(xp)				do { } while (0)

#define pgd_page(pgd) \
	((unsigned long) __va(pgd_val(pgd) & PAGE_MASK))

/*
 * The following only work if pte_present() is true.
 * Undefined behaviour if not..
 */
Linus Torvalds's avatar
Linus Torvalds committed
376 377
static inline int pte_read(pte_t pte)		{ return pte_val(pte) & _PAGE_USER; }
static inline int pte_write(pte_t pte)		{ return pte_val(pte) & _PAGE_RW; }
Linus Torvalds's avatar
Linus Torvalds committed
378
static inline int pte_exec(pte_t pte)		{ return pte_val(pte) & _PAGE_EXEC; }
Linus Torvalds's avatar
Linus Torvalds committed
379 380
static inline int pte_dirty(pte_t pte)		{ return pte_val(pte) & _PAGE_DIRTY; }
static inline int pte_young(pte_t pte)		{ return pte_val(pte) & _PAGE_ACCESSED; }
Linus Torvalds's avatar
Linus Torvalds committed
381

Linus Torvalds's avatar
Linus Torvalds committed
382 383
static inline void pte_uncache(pte_t pte)       { pte_val(pte) |= _PAGE_NO_CACHE; }
static inline void pte_cache(pte_t pte)         { pte_val(pte) &= ~_PAGE_NO_CACHE; }
Linus Torvalds's avatar
Linus Torvalds committed
384

Linus Torvalds's avatar
Linus Torvalds committed
385
static inline pte_t pte_rdprotect(pte_t pte) {
Linus Torvalds's avatar
Linus Torvalds committed
386
	pte_val(pte) &= ~_PAGE_USER; return pte; }
Linus Torvalds's avatar
Linus Torvalds committed
387
static inline pte_t pte_wrprotect(pte_t pte) {
Linus Torvalds's avatar
Linus Torvalds committed
388
	pte_val(pte) &= ~(_PAGE_RW | _PAGE_HWWRITE); return pte; }
Linus Torvalds's avatar
Linus Torvalds committed
389 390
static inline pte_t pte_exprotect(pte_t pte) {
	pte_val(pte) &= ~_PAGE_EXEC; return pte; }
Linus Torvalds's avatar
Linus Torvalds committed
391
static inline pte_t pte_mkclean(pte_t pte) {
Linus Torvalds's avatar
Linus Torvalds committed
392
	pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HWWRITE); return pte; }
Linus Torvalds's avatar
Linus Torvalds committed
393
static inline pte_t pte_mkold(pte_t pte) {
Linus Torvalds's avatar
Linus Torvalds committed
394 395
	pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }

Linus Torvalds's avatar
Linus Torvalds committed
396
static inline pte_t pte_mkread(pte_t pte) {
Linus Torvalds's avatar
Linus Torvalds committed
397
	pte_val(pte) |= _PAGE_USER; return pte; }
Linus Torvalds's avatar
Linus Torvalds committed
398
static inline pte_t pte_mkexec(pte_t pte) {
Linus Torvalds's avatar
Linus Torvalds committed
399 400 401 402 403
	pte_val(pte) |= _PAGE_USER | _PAGE_EXEC; return pte; }
static inline pte_t pte_mkwrite(pte_t pte) {
	pte_val(pte) |= _PAGE_RW; return pte; }
static inline pte_t pte_mkdirty(pte_t pte) {
	pte_val(pte) |= _PAGE_DIRTY; return pte; }
Linus Torvalds's avatar
Linus Torvalds committed
404
static inline pte_t pte_mkyoung(pte_t pte) {
Linus Torvalds's avatar
Linus Torvalds committed
405 406
	pte_val(pte) |= _PAGE_ACCESSED; return pte; }

Linus Torvalds's avatar
Linus Torvalds committed
407
static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
Linus Torvalds's avatar
Linus Torvalds committed
408 409 410 411 412
{
	pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot);
	return pte;
}

Linus Torvalds's avatar
Linus Torvalds committed
413 414 415 416 417 418 419 420 421 422 423
/*
 * Atomic PTE updates.
 *
 * pte_update clears and sets bit atomically, and returns
 * the old pte value.
 */
static inline unsigned long pte_update(pte_t *p, unsigned long clr,
				       unsigned long set)
{
	unsigned long old, tmp;
	
Linus Torvalds's avatar
Linus Torvalds committed
424 425 426
	__asm__ __volatile__("\
1:	lwarx	%0,0,%3\n\
	andc	%1,%0,%4\n\
427 428 429
	or	%1,%1,%5\n"
	PPC405_ERR77(0,%3)
"	stwcx.	%1,0,%3\n\
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433 434 435 436
	bne-	1b"
	: "=&r" (old), "=&r" (tmp), "=m" (*p)
	: "r" (p), "r" (clr), "r" (set), "m" (*p)
	: "cc" );
	return old;
}

Linus Torvalds's avatar
Linus Torvalds committed
437
/*
438 439 440
 * set_pte stores a linux PTE into the linux page table.
 * On machines which use an MMU hash table we avoid changing the
 * _PAGE_HASHPTE bit.
Linus Torvalds's avatar
Linus Torvalds committed
441
 */
442 443 444 445 446 447 448 449
static inline void set_pte(pte_t *ptep, pte_t pte)
{
#if _PAGE_HASHPTE != 0
	pte_update(ptep, ~_PAGE_HASHPTE, pte_val(pte) & ~_PAGE_HASHPTE);
#else
	*ptep = pte;
#endif
}
Linus Torvalds's avatar
Linus Torvalds committed
450

Linus Torvalds's avatar
Linus Torvalds committed
451 452 453 454 455 456 457
static inline int ptep_test_and_clear_young(pte_t *ptep)
{
	return (pte_update(ptep, _PAGE_ACCESSED, 0) & _PAGE_ACCESSED) != 0;
}

static inline int ptep_test_and_clear_dirty(pte_t *ptep)
{
Linus Torvalds's avatar
Linus Torvalds committed
458
	return (pte_update(ptep, (_PAGE_DIRTY | _PAGE_HWWRITE), 0) & _PAGE_DIRTY) != 0;
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461 462
}

static inline pte_t ptep_get_and_clear(pte_t *ptep)
{
Linus Torvalds's avatar
Linus Torvalds committed
463
	return __pte(pte_update(ptep, ~_PAGE_HASHPTE, 0));
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466 467
}

static inline void ptep_set_wrprotect(pte_t *ptep)
{
Linus Torvalds's avatar
Linus Torvalds committed
468
	pte_update(ptep, (_PAGE_RW | _PAGE_HWWRITE), 0);
Linus Torvalds's avatar
Linus Torvalds committed
469 470 471 472 473 474 475
}

static inline void ptep_mkdirty(pte_t *ptep)
{
	pte_update(ptep, 0, _PAGE_DIRTY);
}

Linus Torvalds's avatar
Linus Torvalds committed
476
#define pte_same(A,B)	(((pte_val(A) ^ pte_val(B)) & ~_PAGE_HASHPTE) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
477

478 479 480 481
#define pmd_page_kernel(pmd)	\
	((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
#define pmd_page(pmd)		\
	(mem_map + (pmd_val(pmd) >> PAGE_SHIFT))
Linus Torvalds's avatar
Linus Torvalds committed
482 483 484 485 486 487 488 489 490

/* to find an entry in a kernel page-table-directory */
#define pgd_offset_k(address) pgd_offset(&init_mm, address)

/* to find an entry in a page-table-directory */
#define pgd_index(address)	 ((address) >> PGDIR_SHIFT)
#define pgd_offset(mm, address)	 ((mm)->pgd + pgd_index(address))

/* Find an entry in the second-level page table.. */
Linus Torvalds's avatar
Linus Torvalds committed
491
static inline pmd_t * pmd_offset(pgd_t * dir, unsigned long address)
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494 495
{
	return (pmd_t *) dir;
}

496 497
/* Find an entry in the third-level page table.. */
#define __pte_offset(address)		\
498
	(((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
499 500 501 502 503 504 505 506 507
#define pte_offset_kernel(dir, addr)	\
	((pte_t *) pmd_page_kernel(*(dir)) + __pte_offset(addr))
#define pte_offset_map(dir, addr)		\
	((pte_t *) kmap_atomic(pmd_page(*(dir)), KM_PTE0) + __pte_offset(addr))
#define pte_offset_map_nested(dir, addr)	\
	((pte_t *) kmap_atomic(pmd_page(*(dir)), KM_PTE1) + __pte_offset(addr))

#define pte_unmap(pte)		kunmap_atomic(pte, KM_PTE0)
#define pte_unmap_nested(pte)	kunmap_atomic(pte, KM_PTE1)
Linus Torvalds's avatar
Linus Torvalds committed
508 509 510 511 512

extern pgd_t swapper_pg_dir[1024];
extern void paging_init(void);

/*
Linus Torvalds's avatar
Linus Torvalds committed
513 514
 * When flushing the tlb entry for a page, we also need to flush the hash
 * table entry.  flush_hash_page is assembler (for speed) in hashtable.S.
Linus Torvalds's avatar
Linus Torvalds committed
515
 */
516 517
extern int flush_hash_pages(unsigned context, unsigned long va,
			    unsigned long pmdval, int count);
Linus Torvalds's avatar
Linus Torvalds committed
518 519

/* Add an HPTE to the hash table */
520 521
extern void add_hash_page(unsigned context, unsigned long va,
			  unsigned long pmdval);
Linus Torvalds's avatar
Linus Torvalds committed
522 523

/*
Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527
 * Encode and decode a swap entry.
 * Note that the bits we use in a PTE for representing a swap entry
 * must not include the _PAGE_PRESENT bit, or the _PAGE_HASHPTE bit
 * (if used).  -- paulus
Linus Torvalds's avatar
Linus Torvalds committed
528
 */
529 530 531 532 533
#define __swp_type(entry)		((entry).val & 0x3f)
#define __swp_offset(entry)		((entry).val >> 6)
#define __swp_entry(type, offset)	((swp_entry_t) { (type) | ((offset) << 6) })
#define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) >> 2 })
#define __swp_entry_to_pte(x)		((pte_t) { (x).val << 2 })
Linus Torvalds's avatar
Linus Torvalds committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

/* CONFIG_APUS */
/* For virtual address to physical address conversion */
extern void cache_clear(__u32 addr, int length);
extern void cache_push(__u32 addr, int length);
extern int mm_end_of_chunk (unsigned long addr, int len);
extern unsigned long iopa(unsigned long addr);
extern unsigned long mm_ptov(unsigned long addr) __attribute__ ((const));

/* Values for nocacheflag and cmode */
/* These are not used by the APUS kernel_map, but prevents
   compilation errors. */
#define	KERNELMAP_FULL_CACHING		0
#define	KERNELMAP_NOCACHE_SER		1
#define	KERNELMAP_NOCACHE_NONSER	2
#define	KERNELMAP_NO_COPYBACK		3

/*
 * Map some physical address range into the kernel address space.
 */
extern unsigned long kernel_map(unsigned long paddr, unsigned long size,
				int nocacheflag, unsigned long *memavailp );

/*
 * Set cache mode of (kernel space) address range. 
 */
extern void kernel_set_cachemode (unsigned long address, unsigned long size,
                                 unsigned int cmode);

/* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
#define kern_addr_valid(addr)	(1)

#define io_remap_page_range remap_page_range 

Linus Torvalds's avatar
Linus Torvalds committed
568 569 570 571 572
/*
 * No page table caches to initialise
 */
#define pgtable_cache_init()	do { } while (0)

573 574 575
typedef pte_t *pte_addr_t;

#endif /* !__ASSEMBLY__ */
Linus Torvalds's avatar
Linus Torvalds committed
576 577
#endif /* _PPC_PGTABLE_H */
#endif /* __KERNEL__ */