page.h 10.0 KB

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