pgtable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Page table support for the Hexagon architecture
  4. *
  5. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  6. */
  7. #ifndef _ASM_PGTABLE_H
  8. #define _ASM_PGTABLE_H
  9. /*
  10. * Page table definitions for Qualcomm Hexagon processor.
  11. */
  12. #include <asm/page.h>
  13. #include <asm-generic/pgtable-nopmd.h>
  14. /* A handy thing to have if one has the RAM. Declared in head.S */
  15. extern unsigned long empty_zero_page;
  16. /*
  17. * The PTE model described here is that of the Hexagon Virtual Machine,
  18. * which autonomously walks 2-level page tables. At a lower level, we
  19. * also describe the RISCish software-loaded TLB entry structure of
  20. * the underlying Hexagon processor. A kernel built to run on the
  21. * virtual machine has no need to know about the underlying hardware.
  22. */
  23. #include <asm/vm_mmu.h>
  24. /*
  25. * To maximize the comfort level for the PTE manipulation macros,
  26. * define the "well known" architecture-specific bits.
  27. */
  28. #define _PAGE_READ __HVM_PTE_R
  29. #define _PAGE_WRITE __HVM_PTE_W
  30. #define _PAGE_EXECUTE __HVM_PTE_X
  31. #define _PAGE_USER __HVM_PTE_U
  32. /*
  33. * We have a total of 4 "soft" bits available in the abstract PTE.
  34. * The two mandatory software bits are Dirty and Accessed.
  35. * To make nonlinear swap work according to the more recent
  36. * model, we want a low order "Present" bit to indicate whether
  37. * the PTE describes MMU programming or swap space.
  38. */
  39. #define _PAGE_PRESENT (1<<0)
  40. #define _PAGE_DIRTY (1<<1)
  41. #define _PAGE_ACCESSED (1<<2)
  42. /*
  43. * For now, let's say that Valid and Present are the same thing.
  44. * Alternatively, we could say that it's the "or" of R, W, and X
  45. * permissions.
  46. */
  47. #define _PAGE_VALID _PAGE_PRESENT
  48. /*
  49. * We're not defining _PAGE_GLOBAL here, since there's no concept
  50. * of global pages or ASIDs exposed to the Hexagon Virtual Machine,
  51. * and we want to use the same page table structures and macros in
  52. * the native kernel as we do in the virtual machine kernel.
  53. * So we'll put up with a bit of inefficiency for now...
  54. */
  55. /* We borrow bit 6 to store the exclusive marker in swap PTEs. */
  56. #define _PAGE_SWP_EXCLUSIVE (1<<6)
  57. /*
  58. * Top "FOURTH" level (pgd), which for the Hexagon VM is really
  59. * only the second from the bottom, pgd and pud both being collapsed.
  60. * Each entry represents 4MB of virtual address space, 4K of table
  61. * thus maps the full 4GB.
  62. */
  63. #define PGDIR_SHIFT 22
  64. #define PTRS_PER_PGD 1024
  65. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  66. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  67. #ifdef CONFIG_PAGE_SIZE_4KB
  68. #define PTRS_PER_PTE 1024
  69. #endif
  70. #ifdef CONFIG_PAGE_SIZE_16KB
  71. #define PTRS_PER_PTE 256
  72. #endif
  73. #ifdef CONFIG_PAGE_SIZE_64KB
  74. #define PTRS_PER_PTE 64
  75. #endif
  76. #ifdef CONFIG_PAGE_SIZE_256KB
  77. #define PTRS_PER_PTE 16
  78. #endif
  79. #ifdef CONFIG_PAGE_SIZE_1MB
  80. #define PTRS_PER_PTE 4
  81. #endif
  82. /* Any bigger and the PTE disappears. */
  83. #define pgd_ERROR(e) \
  84. printk(KERN_ERR "%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__,\
  85. pgd_val(e))
  86. /*
  87. * Page Protection Constants. Includes (in this variant) cache attributes.
  88. */
  89. extern unsigned long _dflt_cache_att;
  90. #define PAGE_NONE __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  91. _dflt_cache_att)
  92. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  93. _PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)
  94. #define PAGE_COPY PAGE_READONLY
  95. #define PAGE_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  96. _PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)
  97. #define PAGE_COPY_EXEC PAGE_EXEC
  98. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | \
  99. _PAGE_EXECUTE | _PAGE_WRITE | _dflt_cache_att)
  100. #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_READ | \
  101. _PAGE_WRITE | _PAGE_EXECUTE | _dflt_cache_att)
  102. /*
  103. * Aliases for mapping mmap() protection bits to page protections.
  104. * These get used for static initialization, so using the _dflt_cache_att
  105. * variable for the default cache attribute isn't workable. If the
  106. * default gets changed at boot time, the boot option code has to
  107. * update data structures like the protaction_map[] array.
  108. */
  109. #define CACHEDEF (CACHE_DEFAULT << 6)
  110. extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; /* located in head.S */
  111. /* HUGETLB not working currently */
  112. #ifdef CONFIG_HUGETLB_PAGE
  113. #define pte_mkhuge(pte) __pte((pte_val(pte) & ~0x3) | HVM_HUGEPAGE_SIZE)
  114. #endif
  115. /*
  116. * For now, assume that higher-level code will do TLB/MMU invalidations
  117. * and don't insert that overhead into this low-level function.
  118. */
  119. extern void sync_icache_dcache(pte_t pte);
  120. #define pte_present_exec_user(pte) \
  121. ((pte_val(pte) & (_PAGE_EXECUTE | _PAGE_USER)) == \
  122. (_PAGE_EXECUTE | _PAGE_USER))
  123. static inline void set_pte(pte_t *ptep, pte_t pteval)
  124. {
  125. /* should really be using pte_exec, if it weren't declared later. */
  126. if (pte_present_exec_user(pteval))
  127. sync_icache_dcache(pteval);
  128. *ptep = pteval;
  129. }
  130. /*
  131. * For the Hexagon Virtual Machine MMU (or its emulation), a null/invalid
  132. * L1 PTE (PMD/PGD) has 7 in the least significant bits. For the L2 PTE
  133. * (Linux PTE), the key is to have bits 11..9 all zero. We'd use 0x7
  134. * as a universal null entry, but some of those least significant bits
  135. * are interpreted by software.
  136. */
  137. #define _NULL_PMD 0x7
  138. #define _NULL_PTE 0x0
  139. static inline void pmd_clear(pmd_t *pmd_entry_ptr)
  140. {
  141. pmd_val(*pmd_entry_ptr) = _NULL_PMD;
  142. }
  143. /*
  144. * Conveniently, a null PTE value is invalid.
  145. */
  146. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  147. pte_t *ptep)
  148. {
  149. pte_val(*ptep) = _NULL_PTE;
  150. }
  151. /**
  152. * pmd_none - check if pmd_entry is mapped
  153. * @pmd_entry: pmd entry
  154. *
  155. * MIPS checks it against that "invalid pte table" thing.
  156. */
  157. static inline int pmd_none(pmd_t pmd)
  158. {
  159. return pmd_val(pmd) == _NULL_PMD;
  160. }
  161. /**
  162. * pmd_present - is there a page table behind this?
  163. * Essentially the inverse of pmd_none. We maybe
  164. * save an inline instruction by defining it this
  165. * way, instead of simply "!pmd_none".
  166. */
  167. static inline int pmd_present(pmd_t pmd)
  168. {
  169. return pmd_val(pmd) != (unsigned long)_NULL_PMD;
  170. }
  171. /**
  172. * pmd_bad - check if a PMD entry is "bad". That might mean swapped out.
  173. * As we have no known cause of badness, it's null, as it is for many
  174. * architectures.
  175. */
  176. static inline int pmd_bad(pmd_t pmd)
  177. {
  178. return 0;
  179. }
  180. /*
  181. * pmd_pfn - converts a PMD entry to a page frame number
  182. */
  183. #define pmd_pfn(pmd) (pmd_val(pmd) >> PAGE_SHIFT)
  184. /*
  185. * pmd_page - converts a PMD entry to a page pointer
  186. */
  187. #define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
  188. /**
  189. * pte_none - check if pte is mapped
  190. * @pte: pte_t entry
  191. */
  192. static inline int pte_none(pte_t pte)
  193. {
  194. return pte_val(pte) == _NULL_PTE;
  195. };
  196. /*
  197. * pte_present - check if page is present
  198. */
  199. static inline int pte_present(pte_t pte)
  200. {
  201. return pte_val(pte) & _PAGE_PRESENT;
  202. }
  203. /* mk_pte - make a PTE out of a page pointer and protection bits */
  204. #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
  205. /* pte_page - returns a page (frame pointer/descriptor?) based on a PTE */
  206. #define pte_page(x) pfn_to_page(pte_pfn(x))
  207. /* pte_mkold - mark PTE as not recently accessed */
  208. static inline pte_t pte_mkold(pte_t pte)
  209. {
  210. pte_val(pte) &= ~_PAGE_ACCESSED;
  211. return pte;
  212. }
  213. /* pte_mkyoung - mark PTE as recently accessed */
  214. static inline pte_t pte_mkyoung(pte_t pte)
  215. {
  216. pte_val(pte) |= _PAGE_ACCESSED;
  217. return pte;
  218. }
  219. /* pte_mkclean - mark page as in sync with backing store */
  220. static inline pte_t pte_mkclean(pte_t pte)
  221. {
  222. pte_val(pte) &= ~_PAGE_DIRTY;
  223. return pte;
  224. }
  225. /* pte_mkdirty - mark page as modified */
  226. static inline pte_t pte_mkdirty(pte_t pte)
  227. {
  228. pte_val(pte) |= _PAGE_DIRTY;
  229. return pte;
  230. }
  231. /* pte_young - "is PTE marked as accessed"? */
  232. static inline int pte_young(pte_t pte)
  233. {
  234. return pte_val(pte) & _PAGE_ACCESSED;
  235. }
  236. /* pte_dirty - "is PTE dirty?" */
  237. static inline int pte_dirty(pte_t pte)
  238. {
  239. return pte_val(pte) & _PAGE_DIRTY;
  240. }
  241. /* pte_modify - set protection bits on PTE */
  242. static inline pte_t pte_modify(pte_t pte, pgprot_t prot)
  243. {
  244. pte_val(pte) &= PAGE_MASK;
  245. pte_val(pte) |= pgprot_val(prot);
  246. return pte;
  247. }
  248. /* pte_wrprotect - mark page as not writable */
  249. static inline pte_t pte_wrprotect(pte_t pte)
  250. {
  251. pte_val(pte) &= ~_PAGE_WRITE;
  252. return pte;
  253. }
  254. /* pte_mkwrite - mark page as writable */
  255. static inline pte_t pte_mkwrite_novma(pte_t pte)
  256. {
  257. pte_val(pte) |= _PAGE_WRITE;
  258. return pte;
  259. }
  260. /* pte_mkexec - mark PTE as executable */
  261. static inline pte_t pte_mkexec(pte_t pte)
  262. {
  263. pte_val(pte) |= _PAGE_EXECUTE;
  264. return pte;
  265. }
  266. /* pte_read - "is PTE marked as readable?" */
  267. static inline int pte_read(pte_t pte)
  268. {
  269. return pte_val(pte) & _PAGE_READ;
  270. }
  271. /* pte_write - "is PTE marked as writable?" */
  272. static inline int pte_write(pte_t pte)
  273. {
  274. return pte_val(pte) & _PAGE_WRITE;
  275. }
  276. /* pte_exec - "is PTE marked as executable?" */
  277. static inline int pte_exec(pte_t pte)
  278. {
  279. return pte_val(pte) & _PAGE_EXECUTE;
  280. }
  281. /* __pte_to_swp_entry - extract swap entry from PTE */
  282. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  283. /* __swp_entry_to_pte - extract PTE from swap entry */
  284. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  285. #define PFN_PTE_SHIFT PAGE_SHIFT
  286. /* pfn_pte - convert page number and protection value to page table entry */
  287. #define pfn_pte(pfn, pgprot) __pte((pfn << PAGE_SHIFT) | pgprot_val(pgprot))
  288. /* pte_pfn - convert pte to page frame number */
  289. #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
  290. #define set_pmd(pmdptr, pmdval) (*(pmdptr) = (pmdval))
  291. static inline unsigned long pmd_page_vaddr(pmd_t pmd)
  292. {
  293. return (unsigned long)__va(pmd_val(pmd) & PAGE_MASK);
  294. }
  295. /* ZERO_PAGE - returns the globally shared zero page */
  296. #define ZERO_PAGE(vaddr) (virt_to_page(&empty_zero_page))
  297. /*
  298. * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
  299. * are !pte_none() && !pte_present().
  300. *
  301. * Swap/file PTE definitions. If _PAGE_PRESENT is zero, the rest of the PTE is
  302. * interpreted as swap information. The remaining free bits are interpreted as
  303. * listed below. Rather than have the TLB fill handler test
  304. * _PAGE_PRESENT, we're going to reserve the permissions bits and set them to
  305. * all zeros for swap entries, which speeds up the miss handler at the cost of
  306. * 3 bits of offset. That trade-off can be revisited if necessary, but Hexagon
  307. * processor architecture and target applications suggest a lot of TLB misses
  308. * and not much swap space.
  309. *
  310. * Format of swap PTE:
  311. * bit 0: Present (zero)
  312. * bits 1-5: swap type (arch independent layer uses 5 bits max)
  313. * bit 6: exclusive marker
  314. * bits 7-9: bits 2:0 of offset
  315. * bits 10-12: effectively _PAGE_PROTNONE (all zero)
  316. * bits 13-31: bits 21:3 of swap offset
  317. *
  318. * The split offset makes some of the following macros a little gnarly,
  319. * but there's plenty of precedent for this sort of thing.
  320. */
  321. /* Used for swap PTEs */
  322. #define __swp_type(swp_pte) (((swp_pte).val >> 1) & 0x1f)
  323. #define __swp_offset(swp_pte) \
  324. ((((swp_pte).val >> 7) & 0x7) | (((swp_pte).val >> 10) & 0x3ffff8))
  325. #define __swp_entry(type, offset) \
  326. ((swp_entry_t) { \
  327. (((type & 0x1f) << 1) | \
  328. ((offset & 0x3ffff8) << 10) | ((offset & 0x7) << 7)) })
  329. static inline int pte_swp_exclusive(pte_t pte)
  330. {
  331. return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
  332. }
  333. static inline pte_t pte_swp_mkexclusive(pte_t pte)
  334. {
  335. pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;
  336. return pte;
  337. }
  338. static inline pte_t pte_swp_clear_exclusive(pte_t pte)
  339. {
  340. pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;
  341. return pte;
  342. }
  343. #endif