blob: 227118577a9429d32481be4011ed29adad4f7614 [file] [log] [blame]
Sam Ravnborga439fe52008-07-27 23:00:59 +02001#ifndef _SPARC_PGTABLE_H
2#define _SPARC_PGTABLE_H
3
4/* asm/pgtable.h: Defines and functions used to work
5 * with Sparc page tables.
6 *
7 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
8 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 */
10
Sam Ravnborgeb485d62011-04-21 15:48:39 -070011#include <linux/const.h>
12
Sam Ravnborga439fe52008-07-27 23:00:59 +020013#ifndef __ASSEMBLY__
14#include <asm-generic/4level-fixup.h>
15
16#include <linux/spinlock.h>
17#include <linux/swap.h>
18#include <asm/types.h>
Sam Ravnborga439fe52008-07-27 23:00:59 +020019#include <asm/pgtsrmmu.h>
Sam Ravnborga439fe52008-07-27 23:00:59 +020020#include <asm/oplib.h>
21#include <asm/btfixup.h>
David Howellsd550bbd2012-03-28 18:30:03 +010022#include <asm/cpu_type.h>
Sam Ravnborga439fe52008-07-27 23:00:59 +020023
24
25struct vm_area_struct;
26struct page;
27
28extern void load_mmu(void);
29extern unsigned long calc_highpages(void);
30
Sam Ravnborga439fe52008-07-27 23:00:59 +020031#define pte_ERROR(e) __builtin_trap()
32#define pmd_ERROR(e) __builtin_trap()
33#define pgd_ERROR(e) __builtin_trap()
34
35BTFIXUPDEF_INT(page_none)
36BTFIXUPDEF_INT(page_copy)
37BTFIXUPDEF_INT(page_readonly)
38BTFIXUPDEF_INT(page_kernel)
39
Sam Ravnborg1ee0e142012-05-10 23:12:10 +020040#define PMD_SHIFT 22
Sam Ravnborga439fe52008-07-27 23:00:59 +020041#define PMD_SIZE (1UL << PMD_SHIFT)
42#define PMD_MASK (~(PMD_SIZE-1))
43#define PMD_ALIGN(__addr) (((__addr) + ~PMD_MASK) & PMD_MASK)
David S. Miller3d386c02012-05-12 12:02:02 -070044#define PGDIR_SHIFT SRMMU_PGDIR_SHIFT
45#define PGDIR_SIZE SRMMU_PGDIR_SIZE
46#define PGDIR_MASK SRMMU_PGDIR_MASK
Sam Ravnborga439fe52008-07-27 23:00:59 +020047#define PTRS_PER_PTE 1024
David S. Miller3d386c02012-05-12 12:02:02 -070048#define PTRS_PER_PMD SRMMU_PTRS_PER_PMD
49#define PTRS_PER_PGD SRMMU_PTRS_PER_PGD
50#define USER_PTRS_PER_PGD PAGE_OFFSET / SRMMU_PGDIR_SIZE
Sam Ravnborga439fe52008-07-27 23:00:59 +020051#define FIRST_USER_ADDRESS 0
52#define PTE_SIZE (PTRS_PER_PTE*4)
53
54#define PAGE_NONE __pgprot(BTFIXUP_INT(page_none))
55extern pgprot_t PAGE_SHARED;
56#define PAGE_COPY __pgprot(BTFIXUP_INT(page_copy))
57#define PAGE_READONLY __pgprot(BTFIXUP_INT(page_readonly))
58
59extern unsigned long page_kernel;
60
61#ifdef MODULE
62#define PAGE_KERNEL page_kernel
63#else
64#define PAGE_KERNEL __pgprot(BTFIXUP_INT(page_kernel))
65#endif
66
67/* Top-level page directory */
68extern pgd_t swapper_pg_dir[1024];
69
70extern void paging_init(void);
71
Sam Ravnborga439fe52008-07-27 23:00:59 +020072extern unsigned long ptr_in_current_pgd;
73
74/* Here is a trick, since mmap.c need the initializer elements for
75 * protection_map[] to be constant at compile time, I set the following
76 * to all zeros. I set it to the real values after I link in the
77 * appropriate MMU page table routines at boot time.
78 */
79#define __P000 __pgprot(0)
80#define __P001 __pgprot(0)
81#define __P010 __pgprot(0)
82#define __P011 __pgprot(0)
83#define __P100 __pgprot(0)
84#define __P101 __pgprot(0)
85#define __P110 __pgprot(0)
86#define __P111 __pgprot(0)
87
88#define __S000 __pgprot(0)
89#define __S001 __pgprot(0)
90#define __S010 __pgprot(0)
91#define __S011 __pgprot(0)
92#define __S100 __pgprot(0)
93#define __S101 __pgprot(0)
94#define __S110 __pgprot(0)
95#define __S111 __pgprot(0)
96
97extern int num_contexts;
98
99/* First physical page can be anywhere, the following is needed so that
100 * va-->pa and vice versa conversions work properly without performance
101 * hit for all __pa()/__va() operations.
102 */
103extern unsigned long phys_base;
104extern unsigned long pfn_base;
105
106/*
107 * BAD_PAGETABLE is used when we need a bogus page-table, while
108 * BAD_PAGE is used for a bogus page.
109 *
110 * ZERO_PAGE is a global shared page that is always zero: used
111 * for zero-mapped memory areas etc..
112 */
113extern pte_t * __bad_pagetable(void);
114extern pte_t __bad_page(void);
115extern unsigned long empty_zero_page;
116
117#define BAD_PAGETABLE __bad_pagetable()
118#define BAD_PAGE __bad_page()
119#define ZERO_PAGE(vaddr) (virt_to_page(&empty_zero_page))
120
121/*
122 */
123BTFIXUPDEF_CALL_CONST(struct page *, pmd_page, pmd_t)
124BTFIXUPDEF_CALL_CONST(unsigned long, pgd_page_vaddr, pgd_t)
125
126#define pmd_page(pmd) BTFIXUP_CALL(pmd_page)(pmd)
127#define pgd_page_vaddr(pgd) BTFIXUP_CALL(pgd_page_vaddr)(pgd)
128
Sam Ravnborga439fe52008-07-27 23:00:59 +0200129BTFIXUPDEF_CALL_CONST(int, pte_present, pte_t)
130BTFIXUPDEF_CALL(void, pte_clear, pte_t *)
131
132static inline int pte_none(pte_t pte)
133{
David S. Millerc87fe1c2010-05-25 23:36:31 -0700134 return !pte_val(pte);
Sam Ravnborga439fe52008-07-27 23:00:59 +0200135}
136
137#define pte_present(pte) BTFIXUP_CALL(pte_present)(pte)
138#define pte_clear(mm,addr,pte) BTFIXUP_CALL(pte_clear)(pte)
139
140BTFIXUPDEF_CALL_CONST(int, pmd_bad, pmd_t)
141BTFIXUPDEF_CALL_CONST(int, pmd_present, pmd_t)
142BTFIXUPDEF_CALL(void, pmd_clear, pmd_t *)
143
144static inline int pmd_none(pmd_t pmd)
145{
David S. Millerc87fe1c2010-05-25 23:36:31 -0700146 return !pmd_val(pmd);
Sam Ravnborga439fe52008-07-27 23:00:59 +0200147}
148
149#define pmd_bad(pmd) BTFIXUP_CALL(pmd_bad)(pmd)
150#define pmd_present(pmd) BTFIXUP_CALL(pmd_present)(pmd)
151#define pmd_clear(pmd) BTFIXUP_CALL(pmd_clear)(pmd)
152
153BTFIXUPDEF_CALL_CONST(int, pgd_none, pgd_t)
154BTFIXUPDEF_CALL_CONST(int, pgd_bad, pgd_t)
155BTFIXUPDEF_CALL_CONST(int, pgd_present, pgd_t)
156BTFIXUPDEF_CALL(void, pgd_clear, pgd_t *)
157
158#define pgd_none(pgd) BTFIXUP_CALL(pgd_none)(pgd)
159#define pgd_bad(pgd) BTFIXUP_CALL(pgd_bad)(pgd)
160#define pgd_present(pgd) BTFIXUP_CALL(pgd_present)(pgd)
161#define pgd_clear(pgd) BTFIXUP_CALL(pgd_clear)(pgd)
162
163/*
164 * The following only work if pte_present() is true.
165 * Undefined behaviour if not..
166 */
167BTFIXUPDEF_HALF(pte_writei)
168BTFIXUPDEF_HALF(pte_dirtyi)
169BTFIXUPDEF_HALF(pte_youngi)
170
171static int pte_write(pte_t pte) __attribute_const__;
172static inline int pte_write(pte_t pte)
173{
174 return pte_val(pte) & BTFIXUP_HALF(pte_writei);
175}
176
177static int pte_dirty(pte_t pte) __attribute_const__;
178static inline int pte_dirty(pte_t pte)
179{
180 return pte_val(pte) & BTFIXUP_HALF(pte_dirtyi);
181}
182
183static int pte_young(pte_t pte) __attribute_const__;
184static inline int pte_young(pte_t pte)
185{
186 return pte_val(pte) & BTFIXUP_HALF(pte_youngi);
187}
188
189/*
190 * The following only work if pte_present() is not true.
191 */
192BTFIXUPDEF_HALF(pte_filei)
193
194static int pte_file(pte_t pte) __attribute_const__;
195static inline int pte_file(pte_t pte)
196{
197 return pte_val(pte) & BTFIXUP_HALF(pte_filei);
198}
199
200static inline int pte_special(pte_t pte)
201{
202 return 0;
203}
204
205/*
206 */
207BTFIXUPDEF_HALF(pte_wrprotecti)
208BTFIXUPDEF_HALF(pte_mkcleani)
209BTFIXUPDEF_HALF(pte_mkoldi)
210
211static pte_t pte_wrprotect(pte_t pte) __attribute_const__;
212static inline pte_t pte_wrprotect(pte_t pte)
213{
214 return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_wrprotecti));
215}
216
217static pte_t pte_mkclean(pte_t pte) __attribute_const__;
218static inline pte_t pte_mkclean(pte_t pte)
219{
220 return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_mkcleani));
221}
222
223static pte_t pte_mkold(pte_t pte) __attribute_const__;
224static inline pte_t pte_mkold(pte_t pte)
225{
226 return __pte(pte_val(pte) & ~BTFIXUP_HALF(pte_mkoldi));
227}
228
229BTFIXUPDEF_CALL_CONST(pte_t, pte_mkwrite, pte_t)
230BTFIXUPDEF_CALL_CONST(pte_t, pte_mkdirty, pte_t)
231BTFIXUPDEF_CALL_CONST(pte_t, pte_mkyoung, pte_t)
232
233#define pte_mkwrite(pte) BTFIXUP_CALL(pte_mkwrite)(pte)
234#define pte_mkdirty(pte) BTFIXUP_CALL(pte_mkdirty)(pte)
235#define pte_mkyoung(pte) BTFIXUP_CALL(pte_mkyoung)(pte)
236
237#define pte_mkspecial(pte) (pte)
238
239#define pfn_pte(pfn, prot) mk_pte(pfn_to_page(pfn), prot)
240
241BTFIXUPDEF_CALL(unsigned long, pte_pfn, pte_t)
242#define pte_pfn(pte) BTFIXUP_CALL(pte_pfn)(pte)
243#define pte_page(pte) pfn_to_page(pte_pfn(pte))
244
245/*
246 * Conversion functions: convert a page and protection to a page entry,
247 * and a page entry and page directory to the page they refer to.
248 */
249BTFIXUPDEF_CALL_CONST(pte_t, mk_pte, struct page *, pgprot_t)
250
251BTFIXUPDEF_CALL_CONST(pte_t, mk_pte_phys, unsigned long, pgprot_t)
252BTFIXUPDEF_CALL_CONST(pte_t, mk_pte_io, unsigned long, pgprot_t, int)
Sam Ravnborga439fe52008-07-27 23:00:59 +0200253
254#define mk_pte(page,pgprot) BTFIXUP_CALL(mk_pte)(page,pgprot)
255#define mk_pte_phys(page,pgprot) BTFIXUP_CALL(mk_pte_phys)(page,pgprot)
256#define mk_pte_io(page,pgprot,space) BTFIXUP_CALL(mk_pte_io)(page,pgprot,space)
257
Sam Ravnborgafaedde2012-05-11 11:35:17 +0000258#define pgprot_noncached pgprot_noncached
259static inline pgprot_t pgprot_noncached(pgprot_t prot)
260{
261 prot &= ~__pgprot(SRMMU_CACHE);
262 return prot;
263}
Sam Ravnborga439fe52008-07-27 23:00:59 +0200264
265BTFIXUPDEF_INT(pte_modify_mask)
266
267static pte_t pte_modify(pte_t pte, pgprot_t newprot) __attribute_const__;
268static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
269{
270 return __pte((pte_val(pte) & BTFIXUP_INT(pte_modify_mask)) |
271 pgprot_val(newprot));
272}
273
274#define pgd_index(address) ((address) >> PGDIR_SHIFT)
275
276/* to find an entry in a page-table-directory */
277#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
278
279/* to find an entry in a kernel page-table-directory */
280#define pgd_offset_k(address) pgd_offset(&init_mm, address)
281
282/* Find an entry in the second-level page table.. */
283BTFIXUPDEF_CALL(pmd_t *, pmd_offset, pgd_t *, unsigned long)
284#define pmd_offset(dir,addr) BTFIXUP_CALL(pmd_offset)(dir,addr)
285
286/* Find an entry in the third-level page table.. */
287BTFIXUPDEF_CALL(pte_t *, pte_offset_kernel, pmd_t *, unsigned long)
288#define pte_offset_kernel(dir,addr) BTFIXUP_CALL(pte_offset_kernel)(dir,addr)
289
290/*
David S. Milleree906c92012-05-12 00:35:45 -0700291 * This shortcut works on sun4m (and sun4d) because the nocache area is static.
Sam Ravnborga439fe52008-07-27 23:00:59 +0200292 */
293#define pte_offset_map(d, a) pte_offset_kernel(d,a)
Sam Ravnborga439fe52008-07-27 23:00:59 +0200294#define pte_unmap(pte) do{}while(0)
Sam Ravnborga439fe52008-07-27 23:00:59 +0200295
296/* Certain architectures need to do special things when pte's
297 * within a page table are directly modified. Thus, the following
298 * hook is made available.
299 */
300
301BTFIXUPDEF_CALL(void, set_pte, pte_t *, pte_t)
302
303#define set_pte(ptep,pteval) BTFIXUP_CALL(set_pte)(ptep,pteval)
304#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
305
306struct seq_file;
307BTFIXUPDEF_CALL(void, mmu_info, struct seq_file *)
308
309#define mmu_info(p) BTFIXUP_CALL(mmu_info)(p)
310
311/* Fault handler stuff... */
312#define FAULT_CODE_PROT 0x1
313#define FAULT_CODE_WRITE 0x2
314#define FAULT_CODE_USER 0x4
315
Russell King4b3073e2009-12-18 16:40:18 +0000316BTFIXUPDEF_CALL(void, update_mmu_cache, struct vm_area_struct *, unsigned long, pte_t *)
Sam Ravnborga439fe52008-07-27 23:00:59 +0200317
Russell King4b3073e2009-12-18 16:40:18 +0000318#define update_mmu_cache(vma,addr,ptep) BTFIXUP_CALL(update_mmu_cache)(vma,addr,ptep)
Sam Ravnborga439fe52008-07-27 23:00:59 +0200319
320BTFIXUPDEF_CALL(void, sparc_mapiorange, unsigned int, unsigned long,
321 unsigned long, unsigned int)
322BTFIXUPDEF_CALL(void, sparc_unmapiorange, unsigned long, unsigned int)
323#define sparc_mapiorange(bus,pa,va,len) BTFIXUP_CALL(sparc_mapiorange)(bus,pa,va,len)
324#define sparc_unmapiorange(va,len) BTFIXUP_CALL(sparc_unmapiorange)(va,len)
325
326extern int invalid_segment;
327
328/* Encode and de-code a swap entry */
329BTFIXUPDEF_CALL(unsigned long, __swp_type, swp_entry_t)
330BTFIXUPDEF_CALL(unsigned long, __swp_offset, swp_entry_t)
331BTFIXUPDEF_CALL(swp_entry_t, __swp_entry, unsigned long, unsigned long)
332
333#define __swp_type(__x) BTFIXUP_CALL(__swp_type)(__x)
334#define __swp_offset(__x) BTFIXUP_CALL(__swp_offset)(__x)
335#define __swp_entry(__type,__off) BTFIXUP_CALL(__swp_entry)(__type,__off)
336
337#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
338#define __swp_entry_to_pte(x) ((pte_t) { (x).val })
339
340/* file-offset-in-pte helpers */
Sam Ravnborgafaedde2012-05-11 11:35:17 +0000341static inline unsigned long pte_to_pgoff(pte_t pte)
342{
343 return pte_val(pte) >> SRMMU_PTE_FILE_SHIFT;
344}
Sam Ravnborga439fe52008-07-27 23:00:59 +0200345
Sam Ravnborgafaedde2012-05-11 11:35:17 +0000346static inline pte_t pgoff_to_pte(unsigned long pgoff)
347{
348 return __pte((pgoff << SRMMU_PTE_FILE_SHIFT) | SRMMU_FILE);
349}
Sam Ravnborga439fe52008-07-27 23:00:59 +0200350
351/*
352 * This is made a constant because mm/fremap.c required a constant.
Sam Ravnborga439fe52008-07-27 23:00:59 +0200353 */
354#define PTE_FILE_MAX_BITS 24
355
356/*
357 */
358struct ctx_list {
359 struct ctx_list *next;
360 struct ctx_list *prev;
361 unsigned int ctx_number;
362 struct mm_struct *ctx_mm;
363};
364
365extern struct ctx_list *ctx_list_pool; /* Dynamically allocated */
366extern struct ctx_list ctx_free; /* Head of free list */
367extern struct ctx_list ctx_used; /* Head of used contexts list */
368
369#define NO_CONTEXT -1
370
371static inline void remove_from_ctx_list(struct ctx_list *entry)
372{
373 entry->next->prev = entry->prev;
374 entry->prev->next = entry->next;
375}
376
377static inline void add_to_ctx_list(struct ctx_list *head, struct ctx_list *entry)
378{
379 entry->next = head;
380 (entry->prev = head->prev)->next = entry;
381 head->prev = entry;
382}
383#define add_to_free_ctxlist(entry) add_to_ctx_list(&ctx_free, entry)
384#define add_to_used_ctxlist(entry) add_to_ctx_list(&ctx_used, entry)
385
386static inline unsigned long
387__get_phys (unsigned long addr)
388{
389 switch (sparc_cpu_model){
Sam Ravnborga439fe52008-07-27 23:00:59 +0200390 case sun4m:
391 case sun4d:
392 return ((srmmu_get_pte (addr) & 0xffffff00) << 4);
393 default:
394 return 0;
395 }
396}
397
398static inline int
399__get_iospace (unsigned long addr)
400{
401 switch (sparc_cpu_model){
Sam Ravnborga439fe52008-07-27 23:00:59 +0200402 case sun4m:
403 case sun4d:
404 return (srmmu_get_pte (addr) >> 28);
405 default:
406 return -1;
407 }
408}
409
410extern unsigned long *sparc_valid_addr_bitmap;
411
412/* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
413#define kern_addr_valid(addr) \
414 (test_bit(__pa((unsigned long)(addr))>>20, sparc_valid_addr_bitmap))
415
Sam Ravnborga439fe52008-07-27 23:00:59 +0200416/*
417 * For sparc32&64, the pfn in io_remap_pfn_range() carries <iospace> in
418 * its high 4 bits. These macros/functions put it there or get it from there.
419 */
420#define MK_IOSPACE_PFN(space, pfn) (pfn | (space << (BITS_PER_LONG - 4)))
421#define GET_IOSPACE(pfn) (pfn >> (BITS_PER_LONG - 4))
422#define GET_PFN(pfn) (pfn & 0x0fffffffUL)
423
David S. Miller3e37fd32011-11-17 18:17:59 -0800424extern int remap_pfn_range(struct vm_area_struct *, unsigned long, unsigned long,
425 unsigned long, pgprot_t);
426
427static inline int io_remap_pfn_range(struct vm_area_struct *vma,
428 unsigned long from, unsigned long pfn,
429 unsigned long size, pgprot_t prot)
430{
431 unsigned long long offset, space, phys_base;
432
433 offset = ((unsigned long long) GET_PFN(pfn)) << PAGE_SHIFT;
434 space = GET_IOSPACE(pfn);
435 phys_base = offset | (space << 32ULL);
436
437 return remap_pfn_range(vma, from, phys_base >> PAGE_SHIFT, size, prot);
438}
439
Sam Ravnborga439fe52008-07-27 23:00:59 +0200440#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
441#define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
442({ \
443 int __changed = !pte_same(*(__ptep), __entry); \
444 if (__changed) { \
445 set_pte_at((__vma)->vm_mm, (__address), __ptep, __entry); \
446 flush_tlb_page(__vma, __address); \
447 } \
Sam Ravnborg1ee0e142012-05-10 23:12:10 +0200448 __changed; \
Sam Ravnborga439fe52008-07-27 23:00:59 +0200449})
450
451#include <asm-generic/pgtable.h>
452
453#endif /* !(__ASSEMBLY__) */
454
Sam Ravnborgeb485d62011-04-21 15:48:39 -0700455#define VMALLOC_START _AC(0xfe600000,UL)
Sam Ravnborgeb485d62011-04-21 15:48:39 -0700456#define VMALLOC_END _AC(0xffc00000,UL)
Sam Ravnborga439fe52008-07-27 23:00:59 +0200457
Sam Ravnborga439fe52008-07-27 23:00:59 +0200458/* We provide our own get_unmapped_area to cope with VA holes for userland */
459#define HAVE_ARCH_UNMAPPED_AREA
460
461/*
462 * No page table caches to initialise
463 */
464#define pgtable_cache_init() do { } while (0)
465
466#endif /* !(_SPARC_PGTABLE_H) */