xref: /openbmc/linux/arch/mips/include/asm/pgtable.h (revision e23feb16)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2003 Ralf Baechle
7  */
8 #ifndef _ASM_PGTABLE_H
9 #define _ASM_PGTABLE_H
10 
11 #include <linux/mm_types.h>
12 #include <linux/mmzone.h>
13 #ifdef CONFIG_32BIT
14 #include <asm/pgtable-32.h>
15 #endif
16 #ifdef CONFIG_64BIT
17 #include <asm/pgtable-64.h>
18 #endif
19 
20 #include <asm/io.h>
21 #include <asm/pgtable-bits.h>
22 
23 struct mm_struct;
24 struct vm_area_struct;
25 
26 #define PAGE_NONE	__pgprot(_PAGE_PRESENT | _CACHE_CACHABLE_NONCOHERENT)
27 #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_WRITE | (cpu_has_rixi ? 0 : _PAGE_READ) | \
28 				 _page_cachable_default)
29 #define PAGE_COPY	__pgprot(_PAGE_PRESENT | (cpu_has_rixi ? 0 : _PAGE_READ) | \
30 				 (cpu_has_rixi ?  _PAGE_NO_EXEC : 0) | _page_cachable_default)
31 #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | (cpu_has_rixi ? 0 : _PAGE_READ) | \
32 				 _page_cachable_default)
33 #define PAGE_KERNEL	__pgprot(_PAGE_PRESENT | __READABLE | __WRITEABLE | \
34 				 _PAGE_GLOBAL | _page_cachable_default)
35 #define PAGE_USERIO	__pgprot(_PAGE_PRESENT | (cpu_has_rixi ? 0 : _PAGE_READ) | _PAGE_WRITE | \
36 				 _page_cachable_default)
37 #define PAGE_KERNEL_UNCACHED __pgprot(_PAGE_PRESENT | __READABLE | \
38 			__WRITEABLE | _PAGE_GLOBAL | _CACHE_UNCACHED)
39 
40 /*
41  * If _PAGE_NO_EXEC is not defined, we can't do page protection for
42  * execute, and consider it to be the same as read. Also, write
43  * permissions imply read permissions. This is the closest we can get
44  * by reasonable means..
45  */
46 
47 /*
48  * Dummy values to fill the table in mmap.c
49  * The real values will be generated at runtime
50  */
51 #define __P000 __pgprot(0)
52 #define __P001 __pgprot(0)
53 #define __P010 __pgprot(0)
54 #define __P011 __pgprot(0)
55 #define __P100 __pgprot(0)
56 #define __P101 __pgprot(0)
57 #define __P110 __pgprot(0)
58 #define __P111 __pgprot(0)
59 
60 #define __S000 __pgprot(0)
61 #define __S001 __pgprot(0)
62 #define __S010 __pgprot(0)
63 #define __S011 __pgprot(0)
64 #define __S100 __pgprot(0)
65 #define __S101 __pgprot(0)
66 #define __S110 __pgprot(0)
67 #define __S111 __pgprot(0)
68 
69 extern unsigned long _page_cachable_default;
70 
71 /*
72  * ZERO_PAGE is a global shared page that is always zero; used
73  * for zero-mapped memory areas etc..
74  */
75 
76 extern unsigned long empty_zero_page;
77 extern unsigned long zero_page_mask;
78 
79 #define ZERO_PAGE(vaddr) \
80 	(virt_to_page((void *)(empty_zero_page + (((unsigned long)(vaddr)) & zero_page_mask))))
81 #define __HAVE_COLOR_ZERO_PAGE
82 
83 extern void paging_init(void);
84 
85 /*
86  * Conversion functions: convert a page and protection to a page entry,
87  * and a page entry and page directory to the page they refer to.
88  */
89 #define pmd_phys(pmd)		virt_to_phys((void *)pmd_val(pmd))
90 
91 #define __pmd_page(pmd)		(pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT))
92 #ifndef CONFIG_TRANSPARENT_HUGEPAGE
93 #define pmd_page(pmd)		__pmd_page(pmd)
94 #endif /* CONFIG_TRANSPARENT_HUGEPAGE  */
95 
96 #define pmd_page_vaddr(pmd)	pmd_val(pmd)
97 
98 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
99 
100 #define pte_none(pte)		(!(((pte).pte_low | (pte).pte_high) & ~_PAGE_GLOBAL))
101 #define pte_present(pte)	((pte).pte_low & _PAGE_PRESENT)
102 
103 static inline void set_pte(pte_t *ptep, pte_t pte)
104 {
105 	ptep->pte_high = pte.pte_high;
106 	smp_wmb();
107 	ptep->pte_low = pte.pte_low;
108 
109 	if (pte.pte_low & _PAGE_GLOBAL) {
110 		pte_t *buddy = ptep_buddy(ptep);
111 		/*
112 		 * Make sure the buddy is global too (if it's !none,
113 		 * it better already be global)
114 		 */
115 		if (pte_none(*buddy)) {
116 			buddy->pte_low	|= _PAGE_GLOBAL;
117 			buddy->pte_high |= _PAGE_GLOBAL;
118 		}
119 	}
120 }
121 #define set_pte_at(mm, addr, ptep, pteval) set_pte(ptep, pteval)
122 
123 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
124 {
125 	pte_t null = __pte(0);
126 
127 	/* Preserve global status for the pair */
128 	if (ptep_buddy(ptep)->pte_low & _PAGE_GLOBAL)
129 		null.pte_low = null.pte_high = _PAGE_GLOBAL;
130 
131 	set_pte_at(mm, addr, ptep, null);
132 }
133 #else
134 
135 #define pte_none(pte)		(!(pte_val(pte) & ~_PAGE_GLOBAL))
136 #define pte_present(pte)	(pte_val(pte) & _PAGE_PRESENT)
137 
138 /*
139  * Certain architectures need to do special things when pte's
140  * within a page table are directly modified.  Thus, the following
141  * hook is made available.
142  */
143 static inline void set_pte(pte_t *ptep, pte_t pteval)
144 {
145 	*ptep = pteval;
146 #if !defined(CONFIG_CPU_R3000) && !defined(CONFIG_CPU_TX39XX)
147 	if (pte_val(pteval) & _PAGE_GLOBAL) {
148 		pte_t *buddy = ptep_buddy(ptep);
149 		/*
150 		 * Make sure the buddy is global too (if it's !none,
151 		 * it better already be global)
152 		 */
153 		if (pte_none(*buddy))
154 			pte_val(*buddy) = pte_val(*buddy) | _PAGE_GLOBAL;
155 	}
156 #endif
157 }
158 #define set_pte_at(mm, addr, ptep, pteval) set_pte(ptep, pteval)
159 
160 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
161 {
162 #if !defined(CONFIG_CPU_R3000) && !defined(CONFIG_CPU_TX39XX)
163 	/* Preserve global status for the pair */
164 	if (pte_val(*ptep_buddy(ptep)) & _PAGE_GLOBAL)
165 		set_pte_at(mm, addr, ptep, __pte(_PAGE_GLOBAL));
166 	else
167 #endif
168 		set_pte_at(mm, addr, ptep, __pte(0));
169 }
170 #endif
171 
172 /*
173  * (pmds are folded into puds so this doesn't get actually called,
174  * but the define is needed for a generic inline function.)
175  */
176 #define set_pmd(pmdptr, pmdval) do { *(pmdptr) = (pmdval); } while(0)
177 
178 #ifndef __PAGETABLE_PMD_FOLDED
179 /*
180  * (puds are folded into pgds so this doesn't get actually called,
181  * but the define is needed for a generic inline function.)
182  */
183 #define set_pud(pudptr, pudval) do { *(pudptr) = (pudval); } while(0)
184 #endif
185 
186 #define PGD_T_LOG2	(__builtin_ffs(sizeof(pgd_t)) - 1)
187 #define PMD_T_LOG2	(__builtin_ffs(sizeof(pmd_t)) - 1)
188 #define PTE_T_LOG2	(__builtin_ffs(sizeof(pte_t)) - 1)
189 
190 /*
191  * We used to declare this array with size but gcc 3.3 and older are not able
192  * to find that this expression is a constant, so the size is dropped.
193  */
194 extern pgd_t swapper_pg_dir[];
195 
196 /*
197  * The following only work if pte_present() is true.
198  * Undefined behaviour if not..
199  */
200 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
201 static inline int pte_write(pte_t pte)	{ return pte.pte_low & _PAGE_WRITE; }
202 static inline int pte_dirty(pte_t pte)	{ return pte.pte_low & _PAGE_MODIFIED; }
203 static inline int pte_young(pte_t pte)	{ return pte.pte_low & _PAGE_ACCESSED; }
204 static inline int pte_file(pte_t pte)	{ return pte.pte_low & _PAGE_FILE; }
205 
206 static inline pte_t pte_wrprotect(pte_t pte)
207 {
208 	pte.pte_low  &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
209 	pte.pte_high &= ~_PAGE_SILENT_WRITE;
210 	return pte;
211 }
212 
213 static inline pte_t pte_mkclean(pte_t pte)
214 {
215 	pte.pte_low  &= ~(_PAGE_MODIFIED | _PAGE_SILENT_WRITE);
216 	pte.pte_high &= ~_PAGE_SILENT_WRITE;
217 	return pte;
218 }
219 
220 static inline pte_t pte_mkold(pte_t pte)
221 {
222 	pte.pte_low  &= ~(_PAGE_ACCESSED | _PAGE_SILENT_READ);
223 	pte.pte_high &= ~_PAGE_SILENT_READ;
224 	return pte;
225 }
226 
227 static inline pte_t pte_mkwrite(pte_t pte)
228 {
229 	pte.pte_low |= _PAGE_WRITE;
230 	if (pte.pte_low & _PAGE_MODIFIED) {
231 		pte.pte_low  |= _PAGE_SILENT_WRITE;
232 		pte.pte_high |= _PAGE_SILENT_WRITE;
233 	}
234 	return pte;
235 }
236 
237 static inline pte_t pte_mkdirty(pte_t pte)
238 {
239 	pte.pte_low |= _PAGE_MODIFIED;
240 	if (pte.pte_low & _PAGE_WRITE) {
241 		pte.pte_low  |= _PAGE_SILENT_WRITE;
242 		pte.pte_high |= _PAGE_SILENT_WRITE;
243 	}
244 	return pte;
245 }
246 
247 static inline pte_t pte_mkyoung(pte_t pte)
248 {
249 	pte.pte_low |= _PAGE_ACCESSED;
250 	if (pte.pte_low & _PAGE_READ) {
251 		pte.pte_low  |= _PAGE_SILENT_READ;
252 		pte.pte_high |= _PAGE_SILENT_READ;
253 	}
254 	return pte;
255 }
256 #else
257 static inline int pte_write(pte_t pte)	{ return pte_val(pte) & _PAGE_WRITE; }
258 static inline int pte_dirty(pte_t pte)	{ return pte_val(pte) & _PAGE_MODIFIED; }
259 static inline int pte_young(pte_t pte)	{ return pte_val(pte) & _PAGE_ACCESSED; }
260 static inline int pte_file(pte_t pte)	{ return pte_val(pte) & _PAGE_FILE; }
261 
262 static inline pte_t pte_wrprotect(pte_t pte)
263 {
264 	pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
265 	return pte;
266 }
267 
268 static inline pte_t pte_mkclean(pte_t pte)
269 {
270 	pte_val(pte) &= ~(_PAGE_MODIFIED|_PAGE_SILENT_WRITE);
271 	return pte;
272 }
273 
274 static inline pte_t pte_mkold(pte_t pte)
275 {
276 	pte_val(pte) &= ~(_PAGE_ACCESSED|_PAGE_SILENT_READ);
277 	return pte;
278 }
279 
280 static inline pte_t pte_mkwrite(pte_t pte)
281 {
282 	pte_val(pte) |= _PAGE_WRITE;
283 	if (pte_val(pte) & _PAGE_MODIFIED)
284 		pte_val(pte) |= _PAGE_SILENT_WRITE;
285 	return pte;
286 }
287 
288 static inline pte_t pte_mkdirty(pte_t pte)
289 {
290 	pte_val(pte) |= _PAGE_MODIFIED;
291 	if (pte_val(pte) & _PAGE_WRITE)
292 		pte_val(pte) |= _PAGE_SILENT_WRITE;
293 	return pte;
294 }
295 
296 static inline pte_t pte_mkyoung(pte_t pte)
297 {
298 	pte_val(pte) |= _PAGE_ACCESSED;
299 	if (cpu_has_rixi) {
300 		if (!(pte_val(pte) & _PAGE_NO_READ))
301 			pte_val(pte) |= _PAGE_SILENT_READ;
302 	} else {
303 		if (pte_val(pte) & _PAGE_READ)
304 			pte_val(pte) |= _PAGE_SILENT_READ;
305 	}
306 	return pte;
307 }
308 
309 #ifdef _PAGE_HUGE
310 static inline int pte_huge(pte_t pte)	{ return pte_val(pte) & _PAGE_HUGE; }
311 
312 static inline pte_t pte_mkhuge(pte_t pte)
313 {
314 	pte_val(pte) |= _PAGE_HUGE;
315 	return pte;
316 }
317 #endif /* _PAGE_HUGE */
318 #endif
319 static inline int pte_special(pte_t pte)	{ return 0; }
320 static inline pte_t pte_mkspecial(pte_t pte)	{ return pte; }
321 
322 /*
323  * Macro to make mark a page protection value as "uncacheable".	 Note
324  * that "protection" is really a misnomer here as the protection value
325  * contains the memory attribute bits, dirty bits, and various other
326  * bits as well.
327  */
328 #define pgprot_noncached pgprot_noncached
329 
330 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
331 {
332 	unsigned long prot = pgprot_val(_prot);
333 
334 	prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
335 
336 	return __pgprot(prot);
337 }
338 
339 /*
340  * Conversion functions: convert a page and protection to a page entry,
341  * and a page entry and page directory to the page they refer to.
342  */
343 #define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
344 
345 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
346 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
347 {
348 	pte.pte_low  &= _PAGE_CHG_MASK;
349 	pte.pte_high &= ~0x3f;
350 	pte.pte_low  |= pgprot_val(newprot);
351 	pte.pte_high |= pgprot_val(newprot) & 0x3f;
352 	return pte;
353 }
354 #else
355 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
356 {
357 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
358 }
359 #endif
360 
361 
362 extern void __update_tlb(struct vm_area_struct *vma, unsigned long address,
363 	pte_t pte);
364 extern void __update_cache(struct vm_area_struct *vma, unsigned long address,
365 	pte_t pte);
366 
367 static inline void update_mmu_cache(struct vm_area_struct *vma,
368 	unsigned long address, pte_t *ptep)
369 {
370 	pte_t pte = *ptep;
371 	__update_tlb(vma, address, pte);
372 	__update_cache(vma, address, pte);
373 }
374 
375 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
376 	unsigned long address, pmd_t *pmdp)
377 {
378 	pte_t pte = *(pte_t *)pmdp;
379 
380 	__update_tlb(vma, address, pte);
381 }
382 
383 #define kern_addr_valid(addr)	(1)
384 
385 #ifdef CONFIG_64BIT_PHYS_ADDR
386 extern int remap_pfn_range(struct vm_area_struct *vma, unsigned long from, unsigned long pfn, unsigned long size, pgprot_t prot);
387 
388 static inline int io_remap_pfn_range(struct vm_area_struct *vma,
389 		unsigned long vaddr,
390 		unsigned long pfn,
391 		unsigned long size,
392 		pgprot_t prot)
393 {
394 	phys_t phys_addr_high = fixup_bigphys_addr(pfn << PAGE_SHIFT, size);
395 	return remap_pfn_range(vma, vaddr, phys_addr_high >> PAGE_SHIFT, size, prot);
396 }
397 #define io_remap_pfn_range io_remap_pfn_range
398 #endif
399 
400 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
401 
402 extern int has_transparent_hugepage(void);
403 
404 static inline int pmd_trans_huge(pmd_t pmd)
405 {
406 	return !!(pmd_val(pmd) & _PAGE_HUGE);
407 }
408 
409 static inline pmd_t pmd_mkhuge(pmd_t pmd)
410 {
411 	pmd_val(pmd) |= _PAGE_HUGE;
412 
413 	return pmd;
414 }
415 
416 static inline int pmd_trans_splitting(pmd_t pmd)
417 {
418 	return !!(pmd_val(pmd) & _PAGE_SPLITTING);
419 }
420 
421 static inline pmd_t pmd_mksplitting(pmd_t pmd)
422 {
423 	pmd_val(pmd) |= _PAGE_SPLITTING;
424 
425 	return pmd;
426 }
427 
428 extern void set_pmd_at(struct mm_struct *mm, unsigned long addr,
429 		       pmd_t *pmdp, pmd_t pmd);
430 
431 #define __HAVE_ARCH_PMDP_SPLITTING_FLUSH
432 /* Extern to avoid header file madness */
433 extern void pmdp_splitting_flush(struct vm_area_struct *vma,
434 					unsigned long address,
435 					pmd_t *pmdp);
436 
437 #define __HAVE_ARCH_PMD_WRITE
438 static inline int pmd_write(pmd_t pmd)
439 {
440 	return !!(pmd_val(pmd) & _PAGE_WRITE);
441 }
442 
443 static inline pmd_t pmd_wrprotect(pmd_t pmd)
444 {
445 	pmd_val(pmd) &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
446 	return pmd;
447 }
448 
449 static inline pmd_t pmd_mkwrite(pmd_t pmd)
450 {
451 	pmd_val(pmd) |= _PAGE_WRITE;
452 	if (pmd_val(pmd) & _PAGE_MODIFIED)
453 		pmd_val(pmd) |= _PAGE_SILENT_WRITE;
454 
455 	return pmd;
456 }
457 
458 static inline int pmd_dirty(pmd_t pmd)
459 {
460 	return !!(pmd_val(pmd) & _PAGE_MODIFIED);
461 }
462 
463 static inline pmd_t pmd_mkclean(pmd_t pmd)
464 {
465 	pmd_val(pmd) &= ~(_PAGE_MODIFIED | _PAGE_SILENT_WRITE);
466 	return pmd;
467 }
468 
469 static inline pmd_t pmd_mkdirty(pmd_t pmd)
470 {
471 	pmd_val(pmd) |= _PAGE_MODIFIED;
472 	if (pmd_val(pmd) & _PAGE_WRITE)
473 		pmd_val(pmd) |= _PAGE_SILENT_WRITE;
474 
475 	return pmd;
476 }
477 
478 static inline int pmd_young(pmd_t pmd)
479 {
480 	return !!(pmd_val(pmd) & _PAGE_ACCESSED);
481 }
482 
483 static inline pmd_t pmd_mkold(pmd_t pmd)
484 {
485 	pmd_val(pmd) &= ~(_PAGE_ACCESSED|_PAGE_SILENT_READ);
486 
487 	return pmd;
488 }
489 
490 static inline pmd_t pmd_mkyoung(pmd_t pmd)
491 {
492 	pmd_val(pmd) |= _PAGE_ACCESSED;
493 
494 	if (cpu_has_rixi) {
495 		if (!(pmd_val(pmd) & _PAGE_NO_READ))
496 			pmd_val(pmd) |= _PAGE_SILENT_READ;
497 	} else {
498 		if (pmd_val(pmd) & _PAGE_READ)
499 			pmd_val(pmd) |= _PAGE_SILENT_READ;
500 	}
501 
502 	return pmd;
503 }
504 
505 /* Extern to avoid header file madness */
506 extern pmd_t mk_pmd(struct page *page, pgprot_t prot);
507 
508 static inline unsigned long pmd_pfn(pmd_t pmd)
509 {
510 	return pmd_val(pmd) >> _PFN_SHIFT;
511 }
512 
513 static inline struct page *pmd_page(pmd_t pmd)
514 {
515 	if (pmd_trans_huge(pmd))
516 		return pfn_to_page(pmd_pfn(pmd));
517 
518 	return pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT);
519 }
520 
521 static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
522 {
523 	pmd_val(pmd) = (pmd_val(pmd) & _PAGE_CHG_MASK) | pgprot_val(newprot);
524 	return pmd;
525 }
526 
527 static inline pmd_t pmd_mknotpresent(pmd_t pmd)
528 {
529 	pmd_val(pmd) &= ~(_PAGE_PRESENT | _PAGE_VALID | _PAGE_DIRTY);
530 
531 	return pmd;
532 }
533 
534 /*
535  * The generic version pmdp_get_and_clear uses a version of pmd_clear() with a
536  * different prototype.
537  */
538 #define __HAVE_ARCH_PMDP_GET_AND_CLEAR
539 static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
540 				       unsigned long address, pmd_t *pmdp)
541 {
542 	pmd_t old = *pmdp;
543 
544 	pmd_clear(pmdp);
545 
546 	return old;
547 }
548 
549 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
550 
551 #include <asm-generic/pgtable.h>
552 
553 /*
554  * uncached accelerated TLB map for video memory access
555  */
556 #ifdef CONFIG_CPU_SUPPORTS_UNCACHED_ACCELERATED
557 #define __HAVE_PHYS_MEM_ACCESS_PROT
558 
559 struct file;
560 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
561 		unsigned long size, pgprot_t vma_prot);
562 int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
563 		unsigned long size, pgprot_t *vma_prot);
564 #endif
565 
566 /*
567  * We provide our own get_unmapped area to cope with the virtual aliasing
568  * constraints placed on us by the cache architecture.
569  */
570 #define HAVE_ARCH_UNMAPPED_AREA
571 #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
572 
573 /*
574  * No page table caches to initialise
575  */
576 #define pgtable_cache_init()	do { } while (0)
577 
578 #endif /* _ASM_PGTABLE_H */
579