pgtable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #ifndef _ASMNDS32_PGTABLE_H
  4. #define _ASMNDS32_PGTABLE_H
  5. #define __PAGETABLE_PMD_FOLDED 1
  6. #include <asm-generic/4level-fixup.h>
  7. #include <asm-generic/sizes.h>
  8. #include <asm/memory.h>
  9. #include <asm/nds32.h>
  10. #ifndef __ASSEMBLY__
  11. #include <asm/fixmap.h>
  12. #include <asm/io.h>
  13. #include <nds32_intrinsic.h>
  14. #endif
  15. #ifdef CONFIG_ANDES_PAGE_SIZE_4KB
  16. #define PGDIR_SHIFT 22
  17. #define PTRS_PER_PGD 1024
  18. #define PMD_SHIFT 22
  19. #define PTRS_PER_PMD 1
  20. #define PTRS_PER_PTE 1024
  21. #endif
  22. #ifdef CONFIG_ANDES_PAGE_SIZE_8KB
  23. #define PGDIR_SHIFT 24
  24. #define PTRS_PER_PGD 256
  25. #define PMD_SHIFT 24
  26. #define PTRS_PER_PMD 1
  27. #define PTRS_PER_PTE 2048
  28. #endif
  29. #ifndef __ASSEMBLY__
  30. extern void __pte_error(const char *file, int line, unsigned long val);
  31. extern void __pmd_error(const char *file, int line, unsigned long val);
  32. extern void __pgd_error(const char *file, int line, unsigned long val);
  33. #define pte_ERROR(pte) __pte_error(__FILE__, __LINE__, pte_val(pte))
  34. #define pmd_ERROR(pmd) __pmd_error(__FILE__, __LINE__, pmd_val(pmd))
  35. #define pgd_ERROR(pgd) __pgd_error(__FILE__, __LINE__, pgd_val(pgd))
  36. #endif /* !__ASSEMBLY__ */
  37. #define PMD_SIZE (1UL << PMD_SHIFT)
  38. #define PMD_MASK (~(PMD_SIZE-1))
  39. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  40. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  41. /*
  42. * This is the lowest virtual address we can permit any user space
  43. * mapping to be mapped at. This is particularly important for
  44. * non-high vector CPUs.
  45. */
  46. #define FIRST_USER_ADDRESS 0x8000
  47. #ifdef CONFIG_HIGHMEM
  48. #define CONSISTENT_BASE ((PKMAP_BASE) - (SZ_2M))
  49. #define CONSISTENT_END (PKMAP_BASE)
  50. #else
  51. #define CONSISTENT_BASE (FIXADDR_START - SZ_2M)
  52. #define CONSISTENT_END (FIXADDR_START)
  53. #endif
  54. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
  55. #ifdef CONFIG_HIGHMEM
  56. #ifndef __ASSEMBLY__
  57. #include <asm/highmem.h>
  58. #endif
  59. #endif
  60. #define VMALLOC_RESERVE SZ_128M
  61. #define VMALLOC_END (CONSISTENT_BASE - PAGE_SIZE)
  62. #define VMALLOC_START ((VMALLOC_END) - VMALLOC_RESERVE)
  63. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  64. #define MAXMEM __pa(VMALLOC_START)
  65. #define MAXMEM_PFN PFN_DOWN(MAXMEM)
  66. #define FIRST_USER_PGD_NR 0
  67. #define USER_PTRS_PER_PGD ((TASK_SIZE/PGDIR_SIZE) + FIRST_USER_PGD_NR)
  68. /* L2 PTE */
  69. #define _PAGE_V (1UL << 0)
  70. #define _PAGE_M_XKRW (0UL << 1)
  71. #define _PAGE_M_UR_KR (1UL << 1)
  72. #define _PAGE_M_UR_KRW (2UL << 1)
  73. #define _PAGE_M_URW_KRW (3UL << 1)
  74. #define _PAGE_M_KR (5UL << 1)
  75. #define _PAGE_M_KRW (7UL << 1)
  76. #define _PAGE_D (1UL << 4)
  77. #define _PAGE_E (1UL << 5)
  78. #define _PAGE_A (1UL << 6)
  79. #define _PAGE_G (1UL << 7)
  80. #define _PAGE_C_DEV (0UL << 8)
  81. #define _PAGE_C_DEV_WB (1UL << 8)
  82. #define _PAGE_C_MEM (2UL << 8)
  83. #define _PAGE_C_MEM_SHRD_WB (4UL << 8)
  84. #define _PAGE_C_MEM_SHRD_WT (5UL << 8)
  85. #define _PAGE_C_MEM_WB (6UL << 8)
  86. #define _PAGE_C_MEM_WT (7UL << 8)
  87. #define _PAGE_L (1UL << 11)
  88. #define _HAVE_PAGE_L (_PAGE_L)
  89. #define _PAGE_FILE (1UL << 1)
  90. #define _PAGE_YOUNG 0
  91. #define _PAGE_M_MASK _PAGE_M_KRW
  92. #define _PAGE_C_MASK _PAGE_C_MEM_WT
  93. #ifdef CONFIG_SMP
  94. #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
  95. #define _PAGE_CACHE_SHRD _PAGE_C_MEM_SHRD_WT
  96. #else
  97. #define _PAGE_CACHE_SHRD _PAGE_C_MEM_SHRD_WB
  98. #endif
  99. #else
  100. #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
  101. #define _PAGE_CACHE_SHRD _PAGE_C_MEM_WT
  102. #else
  103. #define _PAGE_CACHE_SHRD _PAGE_C_MEM_WB
  104. #endif
  105. #endif
  106. #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
  107. #define _PAGE_CACHE _PAGE_C_MEM_WT
  108. #else
  109. #define _PAGE_CACHE _PAGE_C_MEM_WB
  110. #endif
  111. /*
  112. * + Level 1 descriptor (PMD)
  113. */
  114. #define PMD_TYPE_TABLE 0
  115. #ifndef __ASSEMBLY__
  116. #define _PAGE_USER_TABLE PMD_TYPE_TABLE
  117. #define _PAGE_KERNEL_TABLE PMD_TYPE_TABLE
  118. #define PAGE_EXEC __pgprot(_PAGE_V | _PAGE_M_XKRW | _PAGE_E)
  119. #define PAGE_NONE __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_A)
  120. #define PAGE_READ __pgprot(_PAGE_V | _PAGE_M_UR_KR)
  121. #define PAGE_RDWR __pgprot(_PAGE_V | _PAGE_M_URW_KRW | _PAGE_D)
  122. #define PAGE_COPY __pgprot(_PAGE_V | _PAGE_M_UR_KR)
  123. #define PAGE_UXKRWX_V1 __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
  124. #define PAGE_UXKRWX_V2 __pgprot(_PAGE_V | _PAGE_M_XKRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
  125. #define PAGE_URXKRWX_V2 __pgprot(_PAGE_V | _PAGE_M_UR_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
  126. #define PAGE_CACHE_L1 __pgprot(_HAVE_PAGE_L | _PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE)
  127. #define PAGE_MEMORY __pgprot(_HAVE_PAGE_L | _PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
  128. #define PAGE_KERNEL __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_E | _PAGE_G | _PAGE_CACHE_SHRD)
  129. #define PAGE_SHARED __pgprot(_PAGE_V | _PAGE_M_URW_KRW | _PAGE_D | _PAGE_CACHE_SHRD)
  130. #define PAGE_DEVICE __pgprot(_PAGE_V | _PAGE_M_KRW | _PAGE_D | _PAGE_G | _PAGE_C_DEV)
  131. #endif /* __ASSEMBLY__ */
  132. /* xwr */
  133. #define __P000 (PAGE_NONE | _PAGE_CACHE_SHRD)
  134. #define __P001 (PAGE_READ | _PAGE_CACHE_SHRD)
  135. #define __P010 (PAGE_COPY | _PAGE_CACHE_SHRD)
  136. #define __P011 (PAGE_COPY | _PAGE_CACHE_SHRD)
  137. #define __P100 (PAGE_EXEC | _PAGE_CACHE_SHRD)
  138. #define __P101 (PAGE_READ | _PAGE_E | _PAGE_CACHE_SHRD)
  139. #define __P110 (PAGE_COPY | _PAGE_E | _PAGE_CACHE_SHRD)
  140. #define __P111 (PAGE_COPY | _PAGE_E | _PAGE_CACHE_SHRD)
  141. #define __S000 (PAGE_NONE | _PAGE_CACHE_SHRD)
  142. #define __S001 (PAGE_READ | _PAGE_CACHE_SHRD)
  143. #define __S010 (PAGE_RDWR | _PAGE_CACHE_SHRD)
  144. #define __S011 (PAGE_RDWR | _PAGE_CACHE_SHRD)
  145. #define __S100 (PAGE_EXEC | _PAGE_CACHE_SHRD)
  146. #define __S101 (PAGE_READ | _PAGE_E | _PAGE_CACHE_SHRD)
  147. #define __S110 (PAGE_RDWR | _PAGE_E | _PAGE_CACHE_SHRD)
  148. #define __S111 (PAGE_RDWR | _PAGE_E | _PAGE_CACHE_SHRD)
  149. #ifndef __ASSEMBLY__
  150. /*
  151. * ZERO_PAGE is a global shared page that is always zero: used
  152. * for zero-mapped memory areas etc..
  153. */
  154. extern struct page *empty_zero_page;
  155. extern void paging_init(void);
  156. #define ZERO_PAGE(vaddr) (empty_zero_page)
  157. #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
  158. #define pfn_pte(pfn,prot) (__pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot)))
  159. #define pte_none(pte) !(pte_val(pte))
  160. #define pte_clear(mm,addr,ptep) set_pte_at((mm),(addr),(ptep), __pte(0))
  161. #define pte_page(pte) (pfn_to_page(pte_pfn(pte)))
  162. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  163. #define pte_offset_kernel(dir, address) ((pte_t *)pmd_page_kernel(*(dir)) + pte_index(address))
  164. #define pte_offset_map(dir, address) ((pte_t *)page_address(pmd_page(*(dir))) + pte_index(address))
  165. #define pte_offset_map_nested(dir, address) pte_offset_map(dir, address)
  166. #define pmd_page_kernel(pmd) ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
  167. #define pte_unmap(pte) do { } while (0)
  168. #define pte_unmap_nested(pte) do { } while (0)
  169. #define pmd_off_k(address) pmd_offset(pgd_offset_k(address), address)
  170. #define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
  171. /*
  172. * Set a level 1 translation table entry, and clean it out of
  173. * any caches such that the MMUs can load it correctly.
  174. */
  175. static inline void set_pmd(pmd_t * pmdp, pmd_t pmd)
  176. {
  177. *pmdp = pmd;
  178. #if !defined(CONFIG_CPU_DCACHE_DISABLE) && !defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
  179. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (pmdp):"memory");
  180. __nds32__msync_all();
  181. __nds32__dsb();
  182. #endif
  183. }
  184. /*
  185. * Set a PTE and flush it out
  186. */
  187. static inline void set_pte(pte_t * ptep, pte_t pte)
  188. {
  189. *ptep = pte;
  190. #if !defined(CONFIG_CPU_DCACHE_DISABLE) && !defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
  191. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (ptep):"memory");
  192. __nds32__msync_all();
  193. __nds32__dsb();
  194. #endif
  195. }
  196. /*
  197. * The following only work if pte_present() is true.
  198. * Undefined behaviour if not..
  199. */
  200. /*
  201. * pte_write: this page is writeable for user mode
  202. * pte_read: this page is readable for user mode
  203. * pte_kernel_write: this page is writeable for kernel mode
  204. *
  205. * We don't have pte_kernel_read because kernel always can read.
  206. *
  207. * */
  208. #define pte_present(pte) (pte_val(pte) & _PAGE_V)
  209. #define pte_write(pte) ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_URW_KRW)
  210. #define pte_read(pte) (((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_UR_KR) || \
  211. ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_UR_KRW) || \
  212. ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_URW_KRW))
  213. #define pte_kernel_write(pte) (((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_URW_KRW) || \
  214. ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_UR_KRW) || \
  215. ((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_KRW) || \
  216. (((pte_val(pte) & _PAGE_M_MASK) == _PAGE_M_XKRW) && pte_exec(pte)))
  217. #define pte_exec(pte) (pte_val(pte) & _PAGE_E)
  218. #define pte_dirty(pte) (pte_val(pte) & _PAGE_D)
  219. #define pte_young(pte) (pte_val(pte) & _PAGE_YOUNG)
  220. /*
  221. * The following only works if pte_present() is not true.
  222. */
  223. #define pte_file(pte) (pte_val(pte) & _PAGE_FILE)
  224. #define pte_to_pgoff(x) (pte_val(x) >> 2)
  225. #define pgoff_to_pte(x) __pte(((x) << 2) | _PAGE_FILE)
  226. #define PTE_FILE_MAX_BITS 29
  227. #define PTE_BIT_FUNC(fn,op) \
  228. static inline pte_t pte_##fn(pte_t pte) { pte_val(pte) op; return pte; }
  229. static inline pte_t pte_wrprotect(pte_t pte)
  230. {
  231. pte_val(pte) = pte_val(pte) & ~_PAGE_M_MASK;
  232. pte_val(pte) = pte_val(pte) | _PAGE_M_UR_KR;
  233. return pte;
  234. }
  235. static inline pte_t pte_mkwrite(pte_t pte)
  236. {
  237. pte_val(pte) = pte_val(pte) & ~_PAGE_M_MASK;
  238. pte_val(pte) = pte_val(pte) | _PAGE_M_URW_KRW;
  239. return pte;
  240. }
  241. PTE_BIT_FUNC(exprotect, &=~_PAGE_E);
  242. PTE_BIT_FUNC(mkexec, |=_PAGE_E);
  243. PTE_BIT_FUNC(mkclean, &=~_PAGE_D);
  244. PTE_BIT_FUNC(mkdirty, |=_PAGE_D);
  245. PTE_BIT_FUNC(mkold, &=~_PAGE_YOUNG);
  246. PTE_BIT_FUNC(mkyoung, |=_PAGE_YOUNG);
  247. static inline int pte_special(pte_t pte)
  248. {
  249. return 0;
  250. }
  251. static inline pte_t pte_mkspecial(pte_t pte)
  252. {
  253. return pte;
  254. }
  255. /*
  256. * Mark the prot value as uncacheable and unbufferable.
  257. */
  258. #define pgprot_noncached(prot) __pgprot((pgprot_val(prot)&~_PAGE_C_MASK) | _PAGE_C_DEV)
  259. #define pgprot_writecombine(prot) __pgprot((pgprot_val(prot)&~_PAGE_C_MASK) | _PAGE_C_DEV_WB)
  260. #define pmd_none(pmd) (pmd_val(pmd)&0x1)
  261. #define pmd_present(pmd) (!pmd_none(pmd))
  262. #define pmd_bad(pmd) pmd_none(pmd)
  263. #define copy_pmd(pmdpd,pmdps) set_pmd((pmdpd), *(pmdps))
  264. #define pmd_clear(pmdp) set_pmd((pmdp), __pmd(1))
  265. static inline pmd_t __mk_pmd(pte_t * ptep, unsigned long prot)
  266. {
  267. unsigned long ptr = (unsigned long)ptep;
  268. pmd_t pmd;
  269. /*
  270. * The pmd must be loaded with the physical
  271. * address of the PTE table
  272. */
  273. pmd_val(pmd) = __virt_to_phys(ptr) | prot;
  274. return pmd;
  275. }
  276. #define pmd_page(pmd) virt_to_page(__va(pmd_val(pmd)))
  277. /*
  278. * Permanent address of a page. We never have highmem, so this is trivial.
  279. */
  280. #define pages_to_mb(x) ((x) >> (20 - PAGE_SHIFT))
  281. /*
  282. * Conversion functions: convert a page and protection to a page entry,
  283. * and a page entry and page directory to the page they refer to.
  284. */
  285. #define mk_pte(page,prot) pfn_pte(page_to_pfn(page),prot)
  286. /*
  287. * The "pgd_xxx()" functions here are trivial for a folded two-level
  288. * setup: the pgd is never bad, and a pmd always exists (as it's folded
  289. * into the pgd entry)
  290. */
  291. #define pgd_none(pgd) (0)
  292. #define pgd_bad(pgd) (0)
  293. #define pgd_present(pgd) (1)
  294. #define pgd_clear(pgdp) do { } while (0)
  295. #define page_pte_prot(page,prot) mk_pte(page, prot)
  296. #define page_pte(page) mk_pte(page, __pgprot(0))
  297. /*
  298. * L1PTE = $mr1 + ((virt >> PMD_SHIFT) << 2);
  299. * L2PTE = (((virt >> PAGE_SHIFT) & (PTRS_PER_PTE -1 )) << 2);
  300. * PPN = (phys & 0xfffff000);
  301. *
  302. */
  303. /* to find an entry in a page-table-directory */
  304. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
  305. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  306. /* to find an entry in a kernel page-table-directory */
  307. #define pgd_offset_k(addr) pgd_offset(&init_mm, addr)
  308. /* Find an entry in the second-level page table.. */
  309. #define pmd_offset(dir, addr) ((pmd_t *)(dir))
  310. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  311. {
  312. const unsigned long mask = 0xfff;
  313. pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
  314. return pte;
  315. }
  316. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  317. /* Encode and decode a swap entry.
  318. *
  319. * We support up to 32GB of swap on 4k machines
  320. */
  321. #define __swp_type(x) (((x).val >> 2) & 0x7f)
  322. #define __swp_offset(x) ((x).val >> 9)
  323. #define __swp_entry(type,offset) ((swp_entry_t) { ((type) << 2) | ((offset) << 9) })
  324. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  325. #define __swp_entry_to_pte(swp) ((pte_t) { (swp).val })
  326. /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
  327. #define kern_addr_valid(addr) (1)
  328. #include <asm-generic/pgtable.h>
  329. /*
  330. * We provide our own arch_get_unmapped_area to cope with VIPT caches.
  331. */
  332. #define HAVE_ARCH_UNMAPPED_AREA
  333. /*
  334. * remap a physical address `phys' of size `size' with page protection `prot'
  335. * into virtual address `from'
  336. */
  337. #define pgtable_cache_init() do { } while (0)
  338. #endif /* !__ASSEMBLY__ */
  339. #endif /* _ASMNDS32_PGTABLE_H */