page.h 10 KB

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