pgtable.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
  3. * Copyright (C) 2009 Wind River Systems Inc
  4. *
  5. * Based on asm/pgtable-32.h from mips which is:
  6. *
  7. * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 2003 Ralf Baechle
  8. * Copyright (C) 1999, 2000, 2001 Silicon Graphics, Inc.
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #ifndef _ASM_NIOS2_PGTABLE_H
  15. #define _ASM_NIOS2_PGTABLE_H
  16. #include <linux/io.h>
  17. #include <linux/bug.h>
  18. #include <asm/page.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/pgtable-bits.h>
  22. #include <asm-generic/pgtable-nopmd.h>
  23. #define FIRST_USER_ADDRESS 0
  24. #define VMALLOC_START CONFIG_NIOS2_KERNEL_MMU_REGION_BASE
  25. #define VMALLOC_END (CONFIG_NIOS2_KERNEL_REGION_BASE - 1)
  26. struct mm_struct;
  27. /* Helper macro */
  28. #define MKP(x, w, r) __pgprot(_PAGE_PRESENT | _PAGE_CACHED | \
  29. ((x) ? _PAGE_EXEC : 0) | \
  30. ((r) ? _PAGE_READ : 0) | \
  31. ((w) ? _PAGE_WRITE : 0))
  32. /*
  33. * These are the macros that generic kernel code needs
  34. * (to populate protection_map[])
  35. */
  36. /* Remove W bit on private pages for COW support */
  37. #define __P000 MKP(0, 0, 0)
  38. #define __P001 MKP(0, 0, 1)
  39. #define __P010 MKP(0, 0, 0) /* COW */
  40. #define __P011 MKP(0, 0, 1) /* COW */
  41. #define __P100 MKP(1, 0, 0)
  42. #define __P101 MKP(1, 0, 1)
  43. #define __P110 MKP(1, 0, 0) /* COW */
  44. #define __P111 MKP(1, 0, 1) /* COW */
  45. /* Shared pages can have exact HW mapping */
  46. #define __S000 MKP(0, 0, 0)
  47. #define __S001 MKP(0, 0, 1)
  48. #define __S010 MKP(0, 1, 0)
  49. #define __S011 MKP(0, 1, 1)
  50. #define __S100 MKP(1, 0, 0)
  51. #define __S101 MKP(1, 0, 1)
  52. #define __S110 MKP(1, 1, 0)
  53. #define __S111 MKP(1, 1, 1)
  54. /* Used all over the kernel */
  55. #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_CACHED | _PAGE_READ | \
  56. _PAGE_WRITE | _PAGE_EXEC | _PAGE_GLOBAL)
  57. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_CACHED | _PAGE_READ | \
  58. _PAGE_WRITE | _PAGE_ACCESSED)
  59. #define PAGE_COPY MKP(0, 0, 1)
  60. #define PGD_ORDER 0
  61. #define PTE_ORDER 0
  62. #define PTRS_PER_PGD ((PAGE_SIZE << PGD_ORDER) / sizeof(pgd_t))
  63. #define PTRS_PER_PTE ((PAGE_SIZE << PTE_ORDER) / sizeof(pte_t))
  64. #define USER_PTRS_PER_PGD \
  65. (CONFIG_NIOS2_KERNEL_MMU_REGION_BASE / PGDIR_SIZE)
  66. #define PGDIR_SHIFT 22
  67. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  68. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  69. /*
  70. * ZERO_PAGE is a global shared page that is always zero: used
  71. * for zero-mapped memory areas etc..
  72. */
  73. extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
  74. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  75. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  76. extern pte_t invalid_pte_table[PAGE_SIZE/sizeof(pte_t)];
  77. /*
  78. * (pmds are folded into puds so this doesn't get actually called,
  79. * but the define is needed for a generic inline function.)
  80. */
  81. static inline void set_pmd(pmd_t *pmdptr, pmd_t pmdval)
  82. {
  83. pmdptr->pud.pgd.pgd = pmdval.pud.pgd.pgd;
  84. }
  85. /* to find an entry in a page-table-directory */
  86. #define pgd_index(addr) (((addr) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
  87. #define pgd_offset(mm, addr) ((mm)->pgd + pgd_index(addr))
  88. static inline int pte_write(pte_t pte) \
  89. { return pte_val(pte) & _PAGE_WRITE; }
  90. static inline int pte_dirty(pte_t pte) \
  91. { return pte_val(pte) & _PAGE_DIRTY; }
  92. static inline int pte_young(pte_t pte) \
  93. { return pte_val(pte) & _PAGE_ACCESSED; }
  94. static inline int pte_file(pte_t pte) \
  95. { return pte_val(pte) & _PAGE_FILE; }
  96. static inline int pte_special(pte_t pte) { return 0; }
  97. #define pgprot_noncached pgprot_noncached
  98. static inline pgprot_t pgprot_noncached(pgprot_t _prot)
  99. {
  100. unsigned long prot = pgprot_val(_prot);
  101. prot &= ~_PAGE_CACHED;
  102. return __pgprot(prot);
  103. }
  104. static inline int pte_none(pte_t pte)
  105. {
  106. return !(pte_val(pte) & ~(_PAGE_GLOBAL|0xf));
  107. }
  108. static inline int pte_present(pte_t pte) \
  109. { return pte_val(pte) & _PAGE_PRESENT; }
  110. /*
  111. * The following only work if pte_present() is true.
  112. * Undefined behaviour if not..
  113. */
  114. static inline pte_t pte_wrprotect(pte_t pte)
  115. {
  116. pte_val(pte) &= ~_PAGE_WRITE;
  117. return pte;
  118. }
  119. static inline pte_t pte_mkclean(pte_t pte)
  120. {
  121. pte_val(pte) &= ~_PAGE_DIRTY;
  122. return pte;
  123. }
  124. static inline pte_t pte_mkold(pte_t pte)
  125. {
  126. pte_val(pte) &= ~_PAGE_ACCESSED;
  127. return pte;
  128. }
  129. static inline pte_t pte_mkwrite(pte_t pte)
  130. {
  131. pte_val(pte) |= _PAGE_WRITE;
  132. return pte;
  133. }
  134. static inline pte_t pte_mkdirty(pte_t pte)
  135. {
  136. pte_val(pte) |= _PAGE_DIRTY;
  137. return pte;
  138. }
  139. static inline pte_t pte_mkspecial(pte_t pte) { return pte; }
  140. static inline pte_t pte_mkyoung(pte_t pte)
  141. {
  142. pte_val(pte) |= _PAGE_ACCESSED;
  143. return pte;
  144. }
  145. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  146. {
  147. const unsigned long mask = _PAGE_READ | _PAGE_WRITE | _PAGE_EXEC;
  148. pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
  149. return pte;
  150. }
  151. static inline int pmd_present(pmd_t pmd)
  152. {
  153. return (pmd_val(pmd) != (unsigned long) invalid_pte_table)
  154. && (pmd_val(pmd) != 0UL);
  155. }
  156. static inline void pmd_clear(pmd_t *pmdp)
  157. {
  158. pmd_val(*pmdp) = (unsigned long) invalid_pte_table;
  159. }
  160. #define pte_pfn(pte) (pte_val(pte) & 0xfffff)
  161. #define pfn_pte(pfn, prot) (__pte(pfn | pgprot_val(prot)))
  162. #define pte_page(pte) (pfn_to_page(pte_pfn(pte)))
  163. /*
  164. * Store a linux PTE into the linux page table.
  165. */
  166. static inline void set_pte(pte_t *ptep, pte_t pteval)
  167. {
  168. *ptep = pteval;
  169. }
  170. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  171. pte_t *ptep, pte_t pteval)
  172. {
  173. unsigned long paddr = page_to_virt(pte_page(pteval));
  174. flush_dcache_range(paddr, paddr + PAGE_SIZE);
  175. set_pte(ptep, pteval);
  176. }
  177. static inline int pmd_none(pmd_t pmd)
  178. {
  179. return (pmd_val(pmd) ==
  180. (unsigned long) invalid_pte_table) || (pmd_val(pmd) == 0UL);
  181. }
  182. #define pmd_bad(pmd) (pmd_val(pmd) & ~PAGE_MASK)
  183. static inline void pte_clear(struct mm_struct *mm,
  184. unsigned long addr, pte_t *ptep)
  185. {
  186. pte_t null;
  187. pte_val(null) = (addr >> PAGE_SHIFT) & 0xf;
  188. set_pte_at(mm, addr, ptep, null);
  189. flush_tlb_one(addr);
  190. }
  191. /*
  192. * Conversion functions: convert a page and protection to a page entry,
  193. * and a page entry and page directory to the page they refer to.
  194. */
  195. #define mk_pte(page, prot) (pfn_pte(page_to_pfn(page), prot))
  196. #define pte_unmap(pte) do { } while (0)
  197. /*
  198. * Conversion functions: convert a page and protection to a page entry,
  199. * and a page entry and page directory to the page they refer to.
  200. */
  201. #define pmd_phys(pmd) virt_to_phys((void *)pmd_val(pmd))
  202. #define pmd_page(pmd) (pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT))
  203. #define pmd_page_vaddr(pmd) pmd_val(pmd)
  204. #define pte_offset_map(dir, addr) \
  205. ((pte_t *) page_address(pmd_page(*dir)) + \
  206. (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)))
  207. /* to find an entry in a kernel page-table-directory */
  208. #define pgd_offset_k(addr) pgd_offset(&init_mm, addr)
  209. /* Get the address to the PTE for a vaddr in specific directory */
  210. #define pte_offset_kernel(dir, addr) \
  211. ((pte_t *) pmd_page_vaddr(*(dir)) + \
  212. (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)))
  213. #define pte_ERROR(e) \
  214. pr_err("%s:%d: bad pte %08lx.\n", \
  215. __FILE__, __LINE__, pte_val(e))
  216. #define pgd_ERROR(e) \
  217. pr_err("%s:%d: bad pgd %08lx.\n", \
  218. __FILE__, __LINE__, pgd_val(e))
  219. /*
  220. * Encode and decode a swap entry (must be !pte_none(pte) && !pte_present(pte)
  221. * && !pte_file(pte)):
  222. *
  223. * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 ... 1 0
  224. * 0 0 0 0 type. 0 0 0 0 0 0 offset.........
  225. *
  226. * This gives us up to 2**2 = 4 swap files and 2**20 * 4K = 4G per swap file.
  227. *
  228. * Note that the offset field is always non-zero, thus !pte_none(pte) is always
  229. * true.
  230. */
  231. #define __swp_type(swp) (((swp).val >> 26) & 0x3)
  232. #define __swp_offset(swp) ((swp).val & 0xfffff)
  233. #define __swp_entry(type, off) ((swp_entry_t) { (((type) & 0x3) << 26) \
  234. | ((off) & 0xfffff) })
  235. #define __swp_entry_to_pte(swp) ((pte_t) { (swp).val })
  236. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  237. /* Encode and decode a nonlinear file mapping entry */
  238. #define PTE_FILE_MAX_BITS 25
  239. #define pte_to_pgoff(pte) (pte_val(pte) & 0x1ffffff)
  240. #define pgoff_to_pte(off) __pte(((off) & 0x1ffffff) | _PAGE_FILE)
  241. #define kern_addr_valid(addr) (1)
  242. #include <asm-generic/pgtable.h>
  243. #define pgtable_cache_init() do { } while (0)
  244. extern void __init paging_init(void);
  245. extern void __init mmu_init(void);
  246. extern void update_mmu_cache(struct vm_area_struct *vma,
  247. unsigned long address, pte_t *pte);
  248. #endif /* _ASM_NIOS2_PGTABLE_H */