pgtable.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2008-2009 Michal Simek <[email protected]>
  4. * Copyright (C) 2008-2009 PetaLogix
  5. * Copyright (C) 2006 Atmark Techno, Inc.
  6. */
  7. #ifndef _ASM_MICROBLAZE_PGTABLE_H
  8. #define _ASM_MICROBLAZE_PGTABLE_H
  9. #include <asm/setup.h>
  10. #ifndef __ASSEMBLY__
  11. extern int mem_init_done;
  12. #endif
  13. #include <asm-generic/pgtable-nopmd.h>
  14. #ifdef __KERNEL__
  15. #ifndef __ASSEMBLY__
  16. #include <linux/sched.h>
  17. #include <linux/threads.h>
  18. #include <asm/processor.h> /* For TASK_SIZE */
  19. #include <asm/mmu.h>
  20. #include <asm/page.h>
  21. extern unsigned long va_to_phys(unsigned long address);
  22. extern pte_t *va_to_pte(unsigned long address);
  23. /*
  24. * The following only work if pte_present() is true.
  25. * Undefined behaviour if not..
  26. */
  27. /* Start and end of the vmalloc area. */
  28. /* Make sure to map the vmalloc area above the pinned kernel memory area
  29. of 32Mb. */
  30. #define VMALLOC_START (CONFIG_KERNEL_START + CONFIG_LOWMEM_SIZE)
  31. #define VMALLOC_END ioremap_bot
  32. #endif /* __ASSEMBLY__ */
  33. /*
  34. * Macro to mark a page protection value as "uncacheable".
  35. */
  36. #define _PAGE_CACHE_CTL (_PAGE_GUARDED | _PAGE_NO_CACHE | \
  37. _PAGE_WRITETHRU)
  38. #define pgprot_noncached(prot) \
  39. (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  40. _PAGE_NO_CACHE | _PAGE_GUARDED))
  41. #define pgprot_noncached_wc(prot) \
  42. (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  43. _PAGE_NO_CACHE))
  44. /*
  45. * The MicroBlaze MMU is identical to the PPC-40x MMU, and uses a hash
  46. * table containing PTEs, together with a set of 16 segment registers, to
  47. * define the virtual to physical address mapping.
  48. *
  49. * We use the hash table as an extended TLB, i.e. a cache of currently
  50. * active mappings. We maintain a two-level page table tree, much
  51. * like that used by the i386, for the sake of the Linux memory
  52. * management code. Low-level assembler code in hashtable.S
  53. * (procedure hash_page) is responsible for extracting ptes from the
  54. * tree and putting them into the hash table when necessary, and
  55. * updating the accessed and modified bits in the page table tree.
  56. */
  57. /*
  58. * The MicroBlaze processor has a TLB architecture identical to PPC-40x. The
  59. * instruction and data sides share a unified, 64-entry, semi-associative
  60. * TLB which is maintained totally under software control. In addition, the
  61. * instruction side has a hardware-managed, 2,4, or 8-entry, fully-associative
  62. * TLB which serves as a first level to the shared TLB. These two TLBs are
  63. * known as the UTLB and ITLB, respectively (see "mmu.h" for definitions).
  64. */
  65. /*
  66. * The normal case is that PTEs are 32-bits and we have a 1-page
  67. * 1024-entry pgdir pointing to 1-page 1024-entry PTE pages. -- paulus
  68. *
  69. */
  70. /* PGDIR_SHIFT determines what a top-level page table entry can map */
  71. #define PGDIR_SHIFT (PAGE_SHIFT + PTE_SHIFT)
  72. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  73. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  74. /*
  75. * entries per page directory level: our page-table tree is two-level, so
  76. * we don't really have any PMD directory.
  77. */
  78. #define PTRS_PER_PTE (1 << PTE_SHIFT)
  79. #define PTRS_PER_PMD 1
  80. #define PTRS_PER_PGD (1 << (32 - PGDIR_SHIFT))
  81. #define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
  82. #define FIRST_USER_PGD_NR 0
  83. #define USER_PGD_PTRS (PAGE_OFFSET >> PGDIR_SHIFT)
  84. #define KERNEL_PGD_PTRS (PTRS_PER_PGD-USER_PGD_PTRS)
  85. #define pte_ERROR(e) \
  86. printk(KERN_ERR "%s:%d: bad pte "PTE_FMT".\n", \
  87. __FILE__, __LINE__, pte_val(e))
  88. #define pgd_ERROR(e) \
  89. printk(KERN_ERR "%s:%d: bad pgd %08lx.\n", \
  90. __FILE__, __LINE__, pgd_val(e))
  91. /*
  92. * Bits in a linux-style PTE. These match the bits in the
  93. * (hardware-defined) PTE as closely as possible.
  94. */
  95. /* There are several potential gotchas here. The hardware TLBLO
  96. * field looks like this:
  97. *
  98. * 0 1 2 3 4 ... 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  99. * RPN..................... 0 0 EX WR ZSEL....... W I M G
  100. *
  101. * Where possible we make the Linux PTE bits match up with this
  102. *
  103. * - bits 20 and 21 must be cleared, because we use 4k pages (4xx can
  104. * support down to 1k pages), this is done in the TLBMiss exception
  105. * handler.
  106. * - We use only zones 0 (for kernel pages) and 1 (for user pages)
  107. * of the 16 available. Bit 24-26 of the TLB are cleared in the TLB
  108. * miss handler. Bit 27 is PAGE_USER, thus selecting the correct
  109. * zone.
  110. * - PRESENT *must* be in the bottom two bits because swap cache
  111. * entries use the top 30 bits. Because 4xx doesn't support SMP
  112. * anyway, M is irrelevant so we borrow it for PAGE_PRESENT. Bit 30
  113. * is cleared in the TLB miss handler before the TLB entry is loaded.
  114. * - All other bits of the PTE are loaded into TLBLO without
  115. * * modification, leaving us only the bits 20, 21, 24, 25, 26, 30 for
  116. * software PTE bits. We actually use bits 21, 24, 25, and
  117. * 30 respectively for the software bits: ACCESSED, DIRTY, RW, and
  118. * PRESENT.
  119. */
  120. /* Definitions for MicroBlaze. */
  121. #define _PAGE_GUARDED 0x001 /* G: page is guarded from prefetch */
  122. #define _PAGE_PRESENT 0x002 /* software: PTE contains a translation */
  123. #define _PAGE_NO_CACHE 0x004 /* I: caching is inhibited */
  124. #define _PAGE_WRITETHRU 0x008 /* W: caching is write-through */
  125. #define _PAGE_USER 0x010 /* matches one of the zone permission bits */
  126. #define _PAGE_RW 0x040 /* software: Writes permitted */
  127. #define _PAGE_DIRTY 0x080 /* software: dirty page */
  128. #define _PAGE_HWWRITE 0x100 /* hardware: Dirty & RW, set in exception */
  129. #define _PAGE_HWEXEC 0x200 /* hardware: EX permission */
  130. #define _PAGE_ACCESSED 0x400 /* software: R: page referenced */
  131. #define _PMD_PRESENT PAGE_MASK
  132. /*
  133. * Some bits are unused...
  134. */
  135. #ifndef _PAGE_HASHPTE
  136. #define _PAGE_HASHPTE 0
  137. #endif
  138. #ifndef _PTE_NONE_MASK
  139. #define _PTE_NONE_MASK 0
  140. #endif
  141. #ifndef _PAGE_SHARED
  142. #define _PAGE_SHARED 0
  143. #endif
  144. #ifndef _PAGE_EXEC
  145. #define _PAGE_EXEC 0
  146. #endif
  147. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  148. /*
  149. * Note: the _PAGE_COHERENT bit automatically gets set in the hardware
  150. * PTE if CONFIG_SMP is defined (hash_page does this); there is no need
  151. * to have it in the Linux PTE, and in fact the bit could be reused for
  152. * another purpose. -- paulus.
  153. */
  154. #define _PAGE_BASE (_PAGE_PRESENT | _PAGE_ACCESSED)
  155. #define _PAGE_WRENABLE (_PAGE_RW | _PAGE_DIRTY | _PAGE_HWWRITE)
  156. #define _PAGE_KERNEL \
  157. (_PAGE_BASE | _PAGE_WRENABLE | _PAGE_SHARED | _PAGE_HWEXEC)
  158. #define _PAGE_IO (_PAGE_KERNEL | _PAGE_NO_CACHE | _PAGE_GUARDED)
  159. #define PAGE_NONE __pgprot(_PAGE_BASE)
  160. #define PAGE_READONLY __pgprot(_PAGE_BASE | _PAGE_USER)
  161. #define PAGE_READONLY_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC)
  162. #define PAGE_SHARED __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW)
  163. #define PAGE_SHARED_X \
  164. __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_RW | _PAGE_EXEC)
  165. #define PAGE_COPY __pgprot(_PAGE_BASE | _PAGE_USER)
  166. #define PAGE_COPY_X __pgprot(_PAGE_BASE | _PAGE_USER | _PAGE_EXEC)
  167. #define PAGE_KERNEL __pgprot(_PAGE_KERNEL)
  168. #define PAGE_KERNEL_RO __pgprot(_PAGE_BASE | _PAGE_SHARED)
  169. #define PAGE_KERNEL_CI __pgprot(_PAGE_IO)
  170. /*
  171. * We consider execute permission the same as read.
  172. * Also, write permissions imply read permissions.
  173. */
  174. #ifndef __ASSEMBLY__
  175. /*
  176. * ZERO_PAGE is a global shared page that is always zero: used
  177. * for zero-mapped memory areas etc..
  178. */
  179. extern unsigned long empty_zero_page[1024];
  180. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  181. #endif /* __ASSEMBLY__ */
  182. #define pte_none(pte) ((pte_val(pte) & ~_PTE_NONE_MASK) == 0)
  183. #define pte_present(pte) (pte_val(pte) & _PAGE_PRESENT)
  184. #define pte_clear(mm, addr, ptep) \
  185. do { set_pte_at((mm), (addr), (ptep), __pte(0)); } while (0)
  186. #define pmd_none(pmd) (!pmd_val(pmd))
  187. #define pmd_bad(pmd) ((pmd_val(pmd) & _PMD_PRESENT) == 0)
  188. #define pmd_present(pmd) ((pmd_val(pmd) & _PMD_PRESENT) != 0)
  189. #define pmd_clear(pmdp) do { pmd_val(*(pmdp)) = 0; } while (0)
  190. #define pte_page(x) (mem_map + (unsigned long) \
  191. ((pte_val(x) - memory_start) >> PAGE_SHIFT))
  192. #define PFN_SHIFT_OFFSET (PAGE_SHIFT)
  193. #define pte_pfn(x) (pte_val(x) >> PFN_SHIFT_OFFSET)
  194. #define pfn_pte(pfn, prot) \
  195. __pte(((pte_basic_t)(pfn) << PFN_SHIFT_OFFSET) | pgprot_val(prot))
  196. #ifndef __ASSEMBLY__
  197. /*
  198. * The following only work if pte_present() is true.
  199. * Undefined behaviour if not..
  200. */
  201. static inline int pte_read(pte_t pte) { return pte_val(pte) & _PAGE_USER; }
  202. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_RW; }
  203. static inline int pte_exec(pte_t pte) { return pte_val(pte) & _PAGE_EXEC; }
  204. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  205. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  206. static inline void pte_uncache(pte_t pte) { pte_val(pte) |= _PAGE_NO_CACHE; }
  207. static inline void pte_cache(pte_t pte) { pte_val(pte) &= ~_PAGE_NO_CACHE; }
  208. static inline pte_t pte_rdprotect(pte_t pte) \
  209. { pte_val(pte) &= ~_PAGE_USER; return pte; }
  210. static inline pte_t pte_wrprotect(pte_t pte) \
  211. { pte_val(pte) &= ~(_PAGE_RW | _PAGE_HWWRITE); return pte; }
  212. static inline pte_t pte_exprotect(pte_t pte) \
  213. { pte_val(pte) &= ~_PAGE_EXEC; return pte; }
  214. static inline pte_t pte_mkclean(pte_t pte) \
  215. { pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HWWRITE); return pte; }
  216. static inline pte_t pte_mkold(pte_t pte) \
  217. { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
  218. static inline pte_t pte_mkread(pte_t pte) \
  219. { pte_val(pte) |= _PAGE_USER; return pte; }
  220. static inline pte_t pte_mkexec(pte_t pte) \
  221. { pte_val(pte) |= _PAGE_USER | _PAGE_EXEC; return pte; }
  222. static inline pte_t pte_mkwrite(pte_t pte) \
  223. { pte_val(pte) |= _PAGE_RW; return pte; }
  224. static inline pte_t pte_mkdirty(pte_t pte) \
  225. { pte_val(pte) |= _PAGE_DIRTY; return pte; }
  226. static inline pte_t pte_mkyoung(pte_t pte) \
  227. { pte_val(pte) |= _PAGE_ACCESSED; return pte; }
  228. /*
  229. * Conversion functions: convert a page and protection to a page entry,
  230. * and a page entry and page directory to the page they refer to.
  231. */
  232. static inline pte_t mk_pte_phys(phys_addr_t physpage, pgprot_t pgprot)
  233. {
  234. pte_t pte;
  235. pte_val(pte) = physpage | pgprot_val(pgprot);
  236. return pte;
  237. }
  238. #define mk_pte(page, pgprot) \
  239. ({ \
  240. pte_t pte; \
  241. pte_val(pte) = (((page - mem_map) << PAGE_SHIFT) + memory_start) | \
  242. pgprot_val(pgprot); \
  243. pte; \
  244. })
  245. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  246. {
  247. pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot);
  248. return pte;
  249. }
  250. /*
  251. * Atomic PTE updates.
  252. *
  253. * pte_update clears and sets bit atomically, and returns
  254. * the old pte value.
  255. * The ((unsigned long)(p+1) - 4) hack is to get to the least-significant
  256. * 32 bits of the PTE regardless of whether PTEs are 32 or 64 bits.
  257. */
  258. static inline unsigned long pte_update(pte_t *p, unsigned long clr,
  259. unsigned long set)
  260. {
  261. unsigned long flags, old, tmp;
  262. raw_local_irq_save(flags);
  263. __asm__ __volatile__( "lw %0, %2, r0 \n"
  264. "andn %1, %0, %3 \n"
  265. "or %1, %1, %4 \n"
  266. "sw %1, %2, r0 \n"
  267. : "=&r" (old), "=&r" (tmp)
  268. : "r" ((unsigned long)(p + 1) - 4), "r" (clr), "r" (set)
  269. : "cc");
  270. raw_local_irq_restore(flags);
  271. return old;
  272. }
  273. /*
  274. * set_pte stores a linux PTE into the linux page table.
  275. */
  276. static inline void set_pte(struct mm_struct *mm, unsigned long addr,
  277. pte_t *ptep, pte_t pte)
  278. {
  279. *ptep = pte;
  280. }
  281. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  282. pte_t *ptep, pte_t pte)
  283. {
  284. *ptep = pte;
  285. }
  286. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  287. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  288. unsigned long address, pte_t *ptep)
  289. {
  290. return (pte_update(ptep, _PAGE_ACCESSED, 0) & _PAGE_ACCESSED) != 0;
  291. }
  292. static inline int ptep_test_and_clear_dirty(struct mm_struct *mm,
  293. unsigned long addr, pte_t *ptep)
  294. {
  295. return (pte_update(ptep, \
  296. (_PAGE_DIRTY | _PAGE_HWWRITE), 0) & _PAGE_DIRTY) != 0;
  297. }
  298. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  299. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  300. unsigned long addr, pte_t *ptep)
  301. {
  302. return __pte(pte_update(ptep, ~_PAGE_HASHPTE, 0));
  303. }
  304. /*static inline void ptep_set_wrprotect(struct mm_struct *mm,
  305. unsigned long addr, pte_t *ptep)
  306. {
  307. pte_update(ptep, (_PAGE_RW | _PAGE_HWWRITE), 0);
  308. }*/
  309. static inline void ptep_mkdirty(struct mm_struct *mm,
  310. unsigned long addr, pte_t *ptep)
  311. {
  312. pte_update(ptep, 0, _PAGE_DIRTY);
  313. }
  314. /*#define pte_same(A,B) (((pte_val(A) ^ pte_val(B)) & ~_PAGE_HASHPTE) == 0)*/
  315. /* Convert pmd entry to page */
  316. /* our pmd entry is an effective address of pte table*/
  317. /* returns effective address of the pmd entry*/
  318. static inline unsigned long pmd_page_vaddr(pmd_t pmd)
  319. {
  320. return ((unsigned long) (pmd_val(pmd) & PAGE_MASK));
  321. }
  322. /* returns pfn of the pmd entry*/
  323. #define pmd_pfn(pmd) (__pa(pmd_val(pmd)) >> PAGE_SHIFT)
  324. /* returns struct *page of the pmd entry*/
  325. #define pmd_page(pmd) (pfn_to_page(__pa(pmd_val(pmd)) >> PAGE_SHIFT))
  326. /* Find an entry in the third-level page table.. */
  327. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  328. /*
  329. * Encode and decode a swap entry.
  330. * Note that the bits we use in a PTE for representing a swap entry
  331. * must not include the _PAGE_PRESENT bit, or the _PAGE_HASHPTE bit
  332. * (if used). -- paulus
  333. */
  334. #define __swp_type(entry) ((entry).val & 0x3f)
  335. #define __swp_offset(entry) ((entry).val >> 6)
  336. #define __swp_entry(type, offset) \
  337. ((swp_entry_t) { (type) | ((offset) << 6) })
  338. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) >> 2 })
  339. #define __swp_entry_to_pte(x) ((pte_t) { (x).val << 2 })
  340. extern unsigned long iopa(unsigned long addr);
  341. /* Values for nocacheflag and cmode */
  342. /* These are not used by the APUS kernel_map, but prevents
  343. * compilation errors.
  344. */
  345. #define IOMAP_FULL_CACHING 0
  346. #define IOMAP_NOCACHE_SER 1
  347. #define IOMAP_NOCACHE_NONSER 2
  348. #define IOMAP_NO_COPYBACK 3
  349. /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
  350. #define kern_addr_valid(addr) (1)
  351. void do_page_fault(struct pt_regs *regs, unsigned long address,
  352. unsigned long error_code);
  353. void mapin_ram(void);
  354. int map_page(unsigned long va, phys_addr_t pa, int flags);
  355. extern int mem_init_done;
  356. asmlinkage void __init mmu_init(void);
  357. #endif /* __ASSEMBLY__ */
  358. #endif /* __KERNEL__ */
  359. #ifndef __ASSEMBLY__
  360. extern unsigned long ioremap_bot, ioremap_base;
  361. void setup_memory(void);
  362. #endif /* __ASSEMBLY__ */
  363. #endif /* _ASM_MICROBLAZE_PGTABLE_H */