page.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef _ASM_TILE_PAGE_H
  15. #define _ASM_TILE_PAGE_H
  16. #include <linux/const.h>
  17. /* PAGE_SHIFT and HPAGE_SHIFT determine the page sizes. */
  18. #define PAGE_SHIFT 16
  19. #define HPAGE_SHIFT 24
  20. #define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT)
  21. #define HPAGE_SIZE (_AC(1, UL) << HPAGE_SHIFT)
  22. #define PAGE_MASK (~(PAGE_SIZE - 1))
  23. #define HPAGE_MASK (~(HPAGE_SIZE - 1))
  24. #ifdef __KERNEL__
  25. #include <hv/hypervisor.h>
  26. #include <arch/chip.h>
  27. /*
  28. * The {,H}PAGE_SHIFT values must match the HV_LOG2_PAGE_SIZE_xxx
  29. * definitions in <hv/hypervisor.h>. We validate this at build time
  30. * here, and again at runtime during early boot. We provide a
  31. * separate definition since userspace doesn't have <hv/hypervisor.h>.
  32. *
  33. * Be careful to distinguish PAGE_SHIFT from HV_PTE_INDEX_PFN, since
  34. * they are the same on i386 but not TILE.
  35. */
  36. #if HV_LOG2_PAGE_SIZE_SMALL != PAGE_SHIFT
  37. # error Small page size mismatch in Linux
  38. #endif
  39. #if HV_LOG2_PAGE_SIZE_LARGE != HPAGE_SHIFT
  40. # error Huge page size mismatch in Linux
  41. #endif
  42. #ifndef __ASSEMBLY__
  43. #include <linux/types.h>
  44. #include <linux/string.h>
  45. struct page;
  46. static inline void clear_page(void *page)
  47. {
  48. memset(page, 0, PAGE_SIZE);
  49. }
  50. static inline void copy_page(void *to, void *from)
  51. {
  52. memcpy(to, from, PAGE_SIZE);
  53. }
  54. static inline void clear_user_page(void *page, unsigned long vaddr,
  55. struct page *pg)
  56. {
  57. clear_page(page);
  58. }
  59. static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
  60. struct page *topage)
  61. {
  62. copy_page(to, from);
  63. }
  64. /*
  65. * Hypervisor page tables are made of the same basic structure.
  66. */
  67. typedef __u64 pteval_t;
  68. typedef __u64 pmdval_t;
  69. typedef __u64 pudval_t;
  70. typedef __u64 pgdval_t;
  71. typedef __u64 pgprotval_t;
  72. typedef HV_PTE pte_t;
  73. typedef HV_PTE pgd_t;
  74. typedef HV_PTE pgprot_t;
  75. /*
  76. * User L2 page tables are managed as one L2 page table per page,
  77. * because we use the page allocator for them. This keeps the allocation
  78. * simple and makes it potentially useful to implement HIGHPTE at some point.
  79. * However, it's also inefficient, since L2 page tables are much smaller
  80. * than pages (currently 2KB vs 64KB). So we should revisit this.
  81. */
  82. typedef struct page *pgtable_t;
  83. /* Must be a macro since it is used to create constants. */
  84. #define __pgprot(val) hv_pte(val)
  85. static inline u64 pgprot_val(pgprot_t pgprot)
  86. {
  87. return hv_pte_val(pgprot);
  88. }
  89. static inline u64 pte_val(pte_t pte)
  90. {
  91. return hv_pte_val(pte);
  92. }
  93. static inline u64 pgd_val(pgd_t pgd)
  94. {
  95. return hv_pte_val(pgd);
  96. }
  97. #ifdef __tilegx__
  98. typedef HV_PTE pmd_t;
  99. static inline u64 pmd_val(pmd_t pmd)
  100. {
  101. return hv_pte_val(pmd);
  102. }
  103. #endif
  104. #endif /* !__ASSEMBLY__ */
  105. #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT)
  106. #define HUGE_MAX_HSTATE 2
  107. #ifdef CONFIG_HUGETLB_PAGE
  108. #define HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  109. #endif
  110. /* Each memory controller has PAs distinct in their high bits. */
  111. #define NR_PA_HIGHBIT_SHIFT (CHIP_PA_WIDTH() - CHIP_LOG_NUM_MSHIMS())
  112. #define NR_PA_HIGHBIT_VALUES (1 << CHIP_LOG_NUM_MSHIMS())
  113. #define __pa_to_highbits(pa) ((phys_addr_t)(pa) >> NR_PA_HIGHBIT_SHIFT)
  114. #define __pfn_to_highbits(pfn) ((pfn) >> (NR_PA_HIGHBIT_SHIFT - PAGE_SHIFT))
  115. #ifdef __tilegx__
  116. /*
  117. * We reserve the lower half of memory for user-space programs, and the
  118. * upper half for system code. We re-map all of physical memory in the
  119. * upper half, which takes a quarter of our VA space. Then we have
  120. * the vmalloc regions. The supervisor code lives at 0xfffffff700000000,
  121. * with the hypervisor above that.
  122. *
  123. * Loadable kernel modules are placed immediately after the static
  124. * supervisor code, with each being allocated a 256MB region of
  125. * address space, so we don't have to worry about the range of "jal"
  126. * and other branch instructions.
  127. *
  128. * For now we keep life simple and just allocate one pmd (4GB) for vmalloc.
  129. * Similarly, for now we don't play any struct page mapping games.
  130. */
  131. #if CHIP_PA_WIDTH() + 2 > CHIP_VA_WIDTH()
  132. # error Too much PA to map with the VA available!
  133. #endif
  134. #define HALF_VA_SPACE (_AC(1, UL) << (CHIP_VA_WIDTH() - 1))
  135. #define MEM_LOW_END (HALF_VA_SPACE - 1) /* low half */
  136. #define MEM_HIGH_START (-HALF_VA_SPACE) /* high half */
  137. #define PAGE_OFFSET MEM_HIGH_START
  138. #define _VMALLOC_START _AC(0xfffffff500000000, UL) /* 4 GB */
  139. #define HUGE_VMAP_BASE _AC(0xfffffff600000000, UL) /* 4 GB */
  140. #define MEM_SV_START _AC(0xfffffff700000000, UL) /* 256 MB */
  141. #define MEM_SV_INTRPT MEM_SV_START
  142. #define MEM_MODULE_START _AC(0xfffffff710000000, UL) /* 256 MB */
  143. #define MEM_MODULE_END (MEM_MODULE_START + (256*1024*1024))
  144. #define MEM_HV_START _AC(0xfffffff800000000, UL) /* 32 GB */
  145. /* Highest DTLB address we will use */
  146. #define KERNEL_HIGH_VADDR MEM_SV_START
  147. /* Since we don't currently provide any fixmaps, we use an impossible VA. */
  148. #define FIXADDR_TOP MEM_HV_START
  149. #else /* !__tilegx__ */
  150. /*
  151. * A PAGE_OFFSET of 0xC0000000 means that the kernel has
  152. * a virtual address space of one gigabyte, which limits the
  153. * amount of physical memory you can use to about 768MB.
  154. * If you want more physical memory than this then see the CONFIG_HIGHMEM
  155. * option in the kernel configuration.
  156. *
  157. * The top two 16MB chunks in the table below (VIRT and HV) are
  158. * unavailable to Linux. Since the kernel interrupt vectors must live
  159. * at 0xfd000000, we map all of the bottom of RAM at this address with
  160. * a huge page table entry to minimize its ITLB footprint (as well as
  161. * at PAGE_OFFSET). The last architected requirement is that user
  162. * interrupt vectors live at 0xfc000000, so we make that range of
  163. * memory available to user processes. The remaining regions are sized
  164. * as shown; after the first four addresses, we show "typical" values,
  165. * since the actual addresses depend on kernel #defines.
  166. *
  167. * MEM_VIRT_INTRPT 0xff000000
  168. * MEM_HV_INTRPT 0xfe000000
  169. * MEM_SV_INTRPT (kernel code) 0xfd000000
  170. * MEM_USER_INTRPT (user vector) 0xfc000000
  171. * FIX_KMAP_xxx 0xf8000000 (via NR_CPUS * KM_TYPE_NR)
  172. * PKMAP_BASE 0xf7000000 (via LAST_PKMAP)
  173. * HUGE_VMAP 0xf3000000 (via CONFIG_NR_HUGE_VMAPS)
  174. * VMALLOC_START 0xf0000000 (via __VMALLOC_RESERVE)
  175. * mapped LOWMEM 0xc0000000
  176. */
  177. #define MEM_USER_INTRPT _AC(0xfc000000, UL)
  178. #define MEM_SV_INTRPT _AC(0xfd000000, UL)
  179. #define MEM_HV_INTRPT _AC(0xfe000000, UL)
  180. #define MEM_VIRT_INTRPT _AC(0xff000000, UL)
  181. #define INTRPT_SIZE 0x4000
  182. /* Tolerate page size larger than the architecture interrupt region size. */
  183. #if PAGE_SIZE > INTRPT_SIZE
  184. #undef INTRPT_SIZE
  185. #define INTRPT_SIZE PAGE_SIZE
  186. #endif
  187. #define KERNEL_HIGH_VADDR MEM_USER_INTRPT
  188. #define FIXADDR_TOP (KERNEL_HIGH_VADDR - PAGE_SIZE)
  189. #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)
  190. /* On 32-bit architectures we mix kernel modules in with other vmaps. */
  191. #define MEM_MODULE_START VMALLOC_START
  192. #define MEM_MODULE_END VMALLOC_END
  193. #endif /* __tilegx__ */
  194. #ifndef __ASSEMBLY__
  195. #ifdef CONFIG_HIGHMEM
  196. /* Map kernel virtual addresses to page frames, in HPAGE_SIZE chunks. */
  197. extern unsigned long pbase_map[];
  198. extern void *vbase_map[];
  199. static inline unsigned long kaddr_to_pfn(const volatile void *_kaddr)
  200. {
  201. unsigned long kaddr = (unsigned long)_kaddr;
  202. return pbase_map[kaddr >> HPAGE_SHIFT] +
  203. ((kaddr & (HPAGE_SIZE - 1)) >> PAGE_SHIFT);
  204. }
  205. static inline void *pfn_to_kaddr(unsigned long pfn)
  206. {
  207. return vbase_map[__pfn_to_highbits(pfn)] + (pfn << PAGE_SHIFT);
  208. }
  209. static inline phys_addr_t virt_to_phys(const volatile void *kaddr)
  210. {
  211. unsigned long pfn = kaddr_to_pfn(kaddr);
  212. return ((phys_addr_t)pfn << PAGE_SHIFT) +
  213. ((unsigned long)kaddr & (PAGE_SIZE-1));
  214. }
  215. static inline void *phys_to_virt(phys_addr_t paddr)
  216. {
  217. return pfn_to_kaddr(paddr >> PAGE_SHIFT) + (paddr & (PAGE_SIZE-1));
  218. }
  219. /* With HIGHMEM, we pack PAGE_OFFSET through high_memory with all valid VAs. */
  220. static inline int virt_addr_valid(const volatile void *kaddr)
  221. {
  222. extern void *high_memory; /* copied from <linux/mm.h> */
  223. return ((unsigned long)kaddr >= PAGE_OFFSET && kaddr < high_memory);
  224. }
  225. #else /* !CONFIG_HIGHMEM */
  226. static inline unsigned long kaddr_to_pfn(const volatile void *kaddr)
  227. {
  228. return ((unsigned long)kaddr - PAGE_OFFSET) >> PAGE_SHIFT;
  229. }
  230. static inline void *pfn_to_kaddr(unsigned long pfn)
  231. {
  232. return (void *)((pfn << PAGE_SHIFT) + PAGE_OFFSET);
  233. }
  234. static inline phys_addr_t virt_to_phys(const volatile void *kaddr)
  235. {
  236. return (phys_addr_t)((unsigned long)kaddr - PAGE_OFFSET);
  237. }
  238. static inline void *phys_to_virt(phys_addr_t paddr)
  239. {
  240. return (void *)((unsigned long)paddr + PAGE_OFFSET);
  241. }
  242. /* Check that the given address is within some mapped range of PAs. */
  243. #define virt_addr_valid(kaddr) pfn_valid(kaddr_to_pfn(kaddr))
  244. #endif /* !CONFIG_HIGHMEM */
  245. /* All callers are not consistent in how they call these functions. */
  246. #define __pa(kaddr) virt_to_phys((void *)(unsigned long)(kaddr))
  247. #define __va(paddr) phys_to_virt((phys_addr_t)(paddr))
  248. extern int devmem_is_allowed(unsigned long pagenr);
  249. #ifdef CONFIG_FLATMEM
  250. static inline int pfn_valid(unsigned long pfn)
  251. {
  252. return pfn < max_mapnr;
  253. }
  254. #endif
  255. /* Provide as macros since these require some other headers included. */
  256. #define page_to_pa(page) ((phys_addr_t)(page_to_pfn(page)) << PAGE_SHIFT)
  257. #define virt_to_page(kaddr) pfn_to_page(kaddr_to_pfn(kaddr))
  258. #define page_to_virt(page) pfn_to_kaddr(page_to_pfn(page))
  259. struct mm_struct;
  260. extern pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr);
  261. #endif /* !__ASSEMBLY__ */
  262. #define VM_DATA_DEFAULT_FLAGS \
  263. (VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
  264. #include <asm-generic/memory_model.h>
  265. #include <asm-generic/getorder.h>
  266. #endif /* __KERNEL__ */
  267. #endif /* _ASM_TILE_PAGE_H */