pgtable.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Page table support for the Hexagon architecture
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #ifndef _ASM_PGTABLE_H
  21. #define _ASM_PGTABLE_H
  22. /*
  23. * Page table definitions for Qualcomm Hexagon processor.
  24. */
  25. #include <linux/swap.h>
  26. #include <asm/page.h>
  27. #include <asm-generic/pgtable-nopmd.h>
  28. /* A handy thing to have if one has the RAM. Declared in head.S */
  29. extern unsigned long empty_zero_page;
  30. extern unsigned long zero_page_mask;
  31. /*
  32. * The PTE model described here is that of the Hexagon Virtual Machine,
  33. * which autonomously walks 2-level page tables. At a lower level, we
  34. * also describe the RISCish software-loaded TLB entry structure of
  35. * the underlying Hexagon processor. A kernel built to run on the
  36. * virtual machine has no need to know about the underlying hardware.
  37. */
  38. #include <asm/vm_mmu.h>
  39. /*
  40. * To maximize the comfort level for the PTE manipulation macros,
  41. * define the "well known" architecture-specific bits.
  42. */
  43. #define _PAGE_READ __HVM_PTE_R
  44. #define _PAGE_WRITE __HVM_PTE_W
  45. #define _PAGE_EXECUTE __HVM_PTE_X
  46. #define _PAGE_USER __HVM_PTE_U
  47. /*
  48. * We have a total of 4 "soft" bits available in the abstract PTE.
  49. * The two mandatory software bits are Dirty and Accessed.
  50. * To make nonlinear swap work according to the more recent
  51. * model, we want a low order "Present" bit to indicate whether
  52. * the PTE describes MMU programming or swap space.
  53. */
  54. #define _PAGE_PRESENT (1<<0)
  55. #define _PAGE_DIRTY (1<<1)
  56. #define _PAGE_ACCESSED (1<<2)
  57. /*
  58. * _PAGE_FILE is only meaningful if _PAGE_PRESENT is false, while
  59. * _PAGE_DIRTY is only meaningful if _PAGE_PRESENT is true.
  60. * So we can overload the bit...
  61. */
  62. #define _PAGE_FILE _PAGE_DIRTY /* set: pagecache, unset = swap */
  63. /*
  64. * For now, let's say that Valid and Present are the same thing.
  65. * Alternatively, we could say that it's the "or" of R, W, and X
  66. * permissions.
  67. */
  68. #define _PAGE_VALID _PAGE_PRESENT
  69. /*
  70. * We're not defining _PAGE_GLOBAL here, since there's no concept
  71. * of global pages or ASIDs exposed to the Hexagon Virtual Machine,
  72. * and we want to use the same page table structures and macros in
  73. * the native kernel as we do in the virtual machine kernel.
  74. * So we'll put up with a bit of inefficiency for now...
  75. */
  76. /*
  77. * Top "FOURTH" level (pgd), which for the Hexagon VM is really
  78. * only the second from the bottom, pgd and pud both being collapsed.
  79. * Each entry represents 4MB of virtual address space, 4K of table
  80. * thus maps the full 4GB.
  81. */
  82. #define PGDIR_SHIFT 22
  83. #define PTRS_PER_PGD 1024
  84. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  85. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  86. #ifdef CONFIG_PAGE_SIZE_4KB
  87. #define PTRS_PER_PTE 1024
  88. #endif
  89. #ifdef CONFIG_PAGE_SIZE_16KB
  90. #define PTRS_PER_PTE 256
  91. #endif
  92. #ifdef CONFIG_PAGE_SIZE_64KB
  93. #define PTRS_PER_PTE 64
  94. #endif
  95. #ifdef CONFIG_PAGE_SIZE_256KB
  96. #define PTRS_PER_PTE 16
  97. #endif
  98. #ifdef CONFIG_PAGE_SIZE_1MB
  99. #define PTRS_PER_PTE 4
  100. #endif
  101. /* Any bigger and the PTE disappears. */
  102. #define pgd_ERROR(e) \
  103. printk(KERN_ERR "%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__,\
  104. pgd_val(e))
  105. /*
  106. * Page Protection Constants. Includes (in this variant) cache attributes.
  107. */
  108. extern unsigned long _dflt_cache_att;
  109. #define PAGE_NONE __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  110. _dflt_cache_att)
  111. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  112. _PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)
  113. #define PAGE_COPY PAGE_READONLY
  114. #define PAGE_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  115. _PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)
  116. #define PAGE_COPY_EXEC PAGE_EXEC
  117. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | \
  118. _PAGE_EXECUTE | _PAGE_WRITE | _dflt_cache_att)
  119. #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_READ | \
  120. _PAGE_WRITE | _PAGE_EXECUTE | _dflt_cache_att)
  121. /*
  122. * Aliases for mapping mmap() protection bits to page protections.
  123. * These get used for static initialization, so using the _dflt_cache_att
  124. * variable for the default cache attribute isn't workable. If the
  125. * default gets changed at boot time, the boot option code has to
  126. * update data structures like the protaction_map[] array.
  127. */
  128. #define CACHEDEF (CACHE_DEFAULT << 6)
  129. /* Private (copy-on-write) page protections. */
  130. #define __P000 __pgprot(_PAGE_PRESENT | _PAGE_USER | CACHEDEF)
  131. #define __P001 __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | CACHEDEF)
  132. #define __P010 __P000 /* Write-only copy-on-write */
  133. #define __P011 __P001 /* Read/Write copy-on-write */
  134. #define __P100 __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  135. _PAGE_EXECUTE | CACHEDEF)
  136. #define __P101 __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_EXECUTE | \
  137. _PAGE_READ | CACHEDEF)
  138. #define __P110 __P100 /* Write/execute copy-on-write */
  139. #define __P111 __P101 /* Read/Write/Execute, copy-on-write */
  140. /* Shared page protections. */
  141. #define __S000 __P000
  142. #define __S001 __P001
  143. #define __S010 __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  144. _PAGE_WRITE | CACHEDEF)
  145. #define __S011 __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | \
  146. _PAGE_WRITE | CACHEDEF)
  147. #define __S100 __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  148. _PAGE_EXECUTE | CACHEDEF)
  149. #define __S101 __P101
  150. #define __S110 __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  151. _PAGE_EXECUTE | _PAGE_WRITE | CACHEDEF)
  152. #define __S111 __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | \
  153. _PAGE_EXECUTE | _PAGE_WRITE | CACHEDEF)
  154. extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; /* located in head.S */
  155. /* Seems to be zero even in architectures where the zero page is firewalled? */
  156. #define FIRST_USER_ADDRESS 0
  157. #define pte_special(pte) 0
  158. #define pte_mkspecial(pte) (pte)
  159. /* HUGETLB not working currently */
  160. #ifdef CONFIG_HUGETLB_PAGE
  161. #define pte_mkhuge(pte) __pte((pte_val(pte) & ~0x3) | HVM_HUGEPAGE_SIZE)
  162. #endif
  163. /*
  164. * For now, assume that higher-level code will do TLB/MMU invalidations
  165. * and don't insert that overhead into this low-level function.
  166. */
  167. extern void sync_icache_dcache(pte_t pte);
  168. #define pte_present_exec_user(pte) \
  169. ((pte_val(pte) & (_PAGE_EXECUTE | _PAGE_USER)) == \
  170. (_PAGE_EXECUTE | _PAGE_USER))
  171. static inline void set_pte(pte_t *ptep, pte_t pteval)
  172. {
  173. /* should really be using pte_exec, if it weren't declared later. */
  174. if (pte_present_exec_user(pteval))
  175. sync_icache_dcache(pteval);
  176. *ptep = pteval;
  177. }
  178. /*
  179. * For the Hexagon Virtual Machine MMU (or its emulation), a null/invalid
  180. * L1 PTE (PMD/PGD) has 7 in the least significant bits. For the L2 PTE
  181. * (Linux PTE), the key is to have bits 11..9 all zero. We'd use 0x7
  182. * as a universal null entry, but some of those least significant bits
  183. * are interpreted by software.
  184. */
  185. #define _NULL_PMD 0x7
  186. #define _NULL_PTE 0x0
  187. static inline void pmd_clear(pmd_t *pmd_entry_ptr)
  188. {
  189. pmd_val(*pmd_entry_ptr) = _NULL_PMD;
  190. }
  191. /*
  192. * Conveniently, a null PTE value is invalid.
  193. */
  194. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  195. pte_t *ptep)
  196. {
  197. pte_val(*ptep) = _NULL_PTE;
  198. }
  199. #ifdef NEED_PMD_INDEX_DESPITE_BEING_2_LEVEL
  200. /**
  201. * pmd_index - returns the index of the entry in the PMD page
  202. * which would control the given virtual address
  203. */
  204. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  205. #endif
  206. /**
  207. * pgd_index - returns the index of the entry in the PGD page
  208. * which would control the given virtual address
  209. *
  210. * This returns the *index* for the address in the pgd_t
  211. */
  212. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  213. /*
  214. * pgd_offset - find an offset in a page-table-directory
  215. */
  216. #define pgd_offset(mm, addr) ((mm)->pgd + pgd_index(addr))
  217. /*
  218. * pgd_offset_k - get kernel (init_mm) pgd entry pointer for addr
  219. */
  220. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  221. /**
  222. * pmd_none - check if pmd_entry is mapped
  223. * @pmd_entry: pmd entry
  224. *
  225. * MIPS checks it against that "invalid pte table" thing.
  226. */
  227. static inline int pmd_none(pmd_t pmd)
  228. {
  229. return pmd_val(pmd) == _NULL_PMD;
  230. }
  231. /**
  232. * pmd_present - is there a page table behind this?
  233. * Essentially the inverse of pmd_none. We maybe
  234. * save an inline instruction by defining it this
  235. * way, instead of simply "!pmd_none".
  236. */
  237. static inline int pmd_present(pmd_t pmd)
  238. {
  239. return pmd_val(pmd) != (unsigned long)_NULL_PMD;
  240. }
  241. /**
  242. * pmd_bad - check if a PMD entry is "bad". That might mean swapped out.
  243. * As we have no known cause of badness, it's null, as it is for many
  244. * architectures.
  245. */
  246. static inline int pmd_bad(pmd_t pmd)
  247. {
  248. return 0;
  249. }
  250. /*
  251. * pmd_page - converts a PMD entry to a page pointer
  252. */
  253. #define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
  254. #define pmd_pgtable(pmd) pmd_page(pmd)
  255. /**
  256. * pte_none - check if pte is mapped
  257. * @pte: pte_t entry
  258. */
  259. static inline int pte_none(pte_t pte)
  260. {
  261. return pte_val(pte) == _NULL_PTE;
  262. };
  263. /*
  264. * pte_present - check if page is present
  265. */
  266. static inline int pte_present(pte_t pte)
  267. {
  268. return pte_val(pte) & _PAGE_PRESENT;
  269. }
  270. /* mk_pte - make a PTE out of a page pointer and protection bits */
  271. #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
  272. /* pte_page - returns a page (frame pointer/descriptor?) based on a PTE */
  273. #define pte_page(x) pfn_to_page(pte_pfn(x))
  274. /* pte_mkold - mark PTE as not recently accessed */
  275. static inline pte_t pte_mkold(pte_t pte)
  276. {
  277. pte_val(pte) &= ~_PAGE_ACCESSED;
  278. return pte;
  279. }
  280. /* pte_mkyoung - mark PTE as recently accessed */
  281. static inline pte_t pte_mkyoung(pte_t pte)
  282. {
  283. pte_val(pte) |= _PAGE_ACCESSED;
  284. return pte;
  285. }
  286. /* pte_mkclean - mark page as in sync with backing store */
  287. static inline pte_t pte_mkclean(pte_t pte)
  288. {
  289. pte_val(pte) &= ~_PAGE_DIRTY;
  290. return pte;
  291. }
  292. /* pte_mkdirty - mark page as modified */
  293. static inline pte_t pte_mkdirty(pte_t pte)
  294. {
  295. pte_val(pte) |= _PAGE_DIRTY;
  296. return pte;
  297. }
  298. /* pte_young - "is PTE marked as accessed"? */
  299. static inline int pte_young(pte_t pte)
  300. {
  301. return pte_val(pte) & _PAGE_ACCESSED;
  302. }
  303. /* pte_dirty - "is PTE dirty?" */
  304. static inline int pte_dirty(pte_t pte)
  305. {
  306. return pte_val(pte) & _PAGE_DIRTY;
  307. }
  308. /* pte_modify - set protection bits on PTE */
  309. static inline pte_t pte_modify(pte_t pte, pgprot_t prot)
  310. {
  311. pte_val(pte) &= PAGE_MASK;
  312. pte_val(pte) |= pgprot_val(prot);
  313. return pte;
  314. }
  315. /* pte_wrprotect - mark page as not writable */
  316. static inline pte_t pte_wrprotect(pte_t pte)
  317. {
  318. pte_val(pte) &= ~_PAGE_WRITE;
  319. return pte;
  320. }
  321. /* pte_mkwrite - mark page as writable */
  322. static inline pte_t pte_mkwrite(pte_t pte)
  323. {
  324. pte_val(pte) |= _PAGE_WRITE;
  325. return pte;
  326. }
  327. /* pte_mkexec - mark PTE as executable */
  328. static inline pte_t pte_mkexec(pte_t pte)
  329. {
  330. pte_val(pte) |= _PAGE_EXECUTE;
  331. return pte;
  332. }
  333. /* pte_read - "is PTE marked as readable?" */
  334. static inline int pte_read(pte_t pte)
  335. {
  336. return pte_val(pte) & _PAGE_READ;
  337. }
  338. /* pte_write - "is PTE marked as writable?" */
  339. static inline int pte_write(pte_t pte)
  340. {
  341. return pte_val(pte) & _PAGE_WRITE;
  342. }
  343. /* pte_exec - "is PTE marked as executable?" */
  344. static inline int pte_exec(pte_t pte)
  345. {
  346. return pte_val(pte) & _PAGE_EXECUTE;
  347. }
  348. /* __pte_to_swp_entry - extract swap entry from PTE */
  349. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  350. /* __swp_entry_to_pte - extract PTE from swap entry */
  351. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  352. /* pfn_pte - convert page number and protection value to page table entry */
  353. #define pfn_pte(pfn, pgprot) __pte((pfn << PAGE_SHIFT) | pgprot_val(pgprot))
  354. /* pte_pfn - convert pte to page frame number */
  355. #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
  356. #define set_pmd(pmdptr, pmdval) (*(pmdptr) = (pmdval))
  357. /*
  358. * set_pte_at - update page table and do whatever magic may be
  359. * necessary to make the underlying hardware/firmware take note.
  360. *
  361. * VM may require a virtual instruction to alert the MMU.
  362. */
  363. #define set_pte_at(mm, addr, ptep, pte) set_pte(ptep, pte)
  364. /*
  365. * May need to invoke the virtual machine as well...
  366. */
  367. #define pte_unmap(pte) do { } while (0)
  368. #define pte_unmap_nested(pte) do { } while (0)
  369. /*
  370. * pte_offset_map - returns the linear address of the page table entry
  371. * corresponding to an address
  372. */
  373. #define pte_offset_map(dir, address) \
  374. ((pte_t *)page_address(pmd_page(*(dir))) + __pte_offset(address))
  375. #define pte_offset_map_nested(pmd, addr) pte_offset_map(pmd, addr)
  376. /* pte_offset_kernel - kernel version of pte_offset */
  377. #define pte_offset_kernel(dir, address) \
  378. ((pte_t *) (unsigned long) __va(pmd_val(*dir) & PAGE_MASK) \
  379. + __pte_offset(address))
  380. /* ZERO_PAGE - returns the globally shared zero page */
  381. #define ZERO_PAGE(vaddr) (virt_to_page(&empty_zero_page))
  382. #define __pte_offset(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  383. /* Nothing special about IO remapping at this point */
  384. #define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
  385. remap_pfn_range(vma, vaddr, pfn, size, prot)
  386. /* I think this is in case we have page table caches; needed by init/main.c */
  387. #define pgtable_cache_init() do { } while (0)
  388. /*
  389. * Swap/file PTE definitions. If _PAGE_PRESENT is zero, the rest of the
  390. * PTE is interpreted as swap information. Depending on the _PAGE_FILE
  391. * bit, the remaining free bits are eitehr interpreted as a file offset
  392. * or a swap type/offset tuple. Rather than have the TLB fill handler
  393. * test _PAGE_PRESENT, we're going to reserve the permissions bits
  394. * and set them to all zeros for swap entries, which speeds up the
  395. * miss handler at the cost of 3 bits of offset. That trade-off can
  396. * be revisited if necessary, but Hexagon processor architecture and
  397. * target applications suggest a lot of TLB misses and not much swap space.
  398. *
  399. * Format of swap PTE:
  400. * bit 0: Present (zero)
  401. * bit 1: _PAGE_FILE (zero)
  402. * bits 2-6: swap type (arch independent layer uses 5 bits max)
  403. * bits 7-9: bits 2:0 of offset
  404. * bits 10-12: effectively _PAGE_PROTNONE (all zero)
  405. * bits 13-31: bits 21:3 of swap offset
  406. *
  407. * Format of file PTE:
  408. * bit 0: Present (zero)
  409. * bit 1: _PAGE_FILE (zero)
  410. * bits 2-9: bits 7:0 of offset
  411. * bits 10-12: effectively _PAGE_PROTNONE (all zero)
  412. * bits 13-31: bits 26:8 of swap offset
  413. *
  414. * The split offset makes some of the following macros a little gnarly,
  415. * but there's plenty of precedent for this sort of thing.
  416. */
  417. #define PTE_FILE_MAX_BITS 27
  418. /* Used for swap PTEs */
  419. #define __swp_type(swp_pte) (((swp_pte).val >> 2) & 0x1f)
  420. #define __swp_offset(swp_pte) \
  421. ((((swp_pte).val >> 7) & 0x7) | (((swp_pte).val >> 10) & 0x003ffff8))
  422. #define __swp_entry(type, offset) \
  423. ((swp_entry_t) { \
  424. ((type << 2) | \
  425. ((offset & 0x3ffff8) << 10) | ((offset & 0x7) << 7)) })
  426. /* Used for file PTEs */
  427. #define pte_file(pte) \
  428. ((pte_val(pte) & (_PAGE_FILE | _PAGE_PRESENT)) == _PAGE_FILE)
  429. #define pte_to_pgoff(pte) \
  430. (((pte_val(pte) >> 2) & 0xff) | ((pte_val(pte) >> 5) & 0x07ffff00))
  431. #define pgoff_to_pte(off) \
  432. ((pte_t) { ((((off) & 0x7ffff00) << 5) | (((off) & 0xff) << 2)\
  433. | _PAGE_FILE) })
  434. /* Oh boy. There are a lot of possible arch overrides found in this file. */
  435. #include <asm-generic/pgtable.h>
  436. #endif