page.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #ifndef _ASM_POWERPC_PAGE_H
  2. #define _ASM_POWERPC_PAGE_H
  3. /*
  4. * Copyright (C) 2001,2005 IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #ifndef __ASSEMBLY__
  12. #include <linux/types.h>
  13. #else
  14. #include <asm/types.h>
  15. #endif
  16. #include <asm/asm-compat.h>
  17. #include <asm/kdump.h>
  18. /*
  19. * On regular PPC32 page size is 4K (but we support 4K/16K/64K/256K pages
  20. * on PPC44x). For PPC64 we support either 4K or 64K software
  21. * page size. When using 64K pages however, whether we are really supporting
  22. * 64K pages in HW or not is irrelevant to those definitions.
  23. */
  24. #if defined(CONFIG_PPC_256K_PAGES)
  25. #define PAGE_SHIFT 18
  26. #elif defined(CONFIG_PPC_64K_PAGES)
  27. #define PAGE_SHIFT 16
  28. #elif defined(CONFIG_PPC_16K_PAGES)
  29. #define PAGE_SHIFT 14
  30. #else
  31. #define PAGE_SHIFT 12
  32. #endif
  33. #define PAGE_SIZE (ASM_CONST(1) << PAGE_SHIFT)
  34. #ifndef __ASSEMBLY__
  35. #ifdef CONFIG_HUGETLB_PAGE
  36. extern unsigned int HPAGE_SHIFT;
  37. #else
  38. #define HPAGE_SHIFT PAGE_SHIFT
  39. #endif
  40. #define HPAGE_SIZE ((1UL) << HPAGE_SHIFT)
  41. #define HPAGE_MASK (~(HPAGE_SIZE - 1))
  42. #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT)
  43. #define HUGE_MAX_HSTATE (MMU_PAGE_COUNT-1)
  44. #endif
  45. /* We do define AT_SYSINFO_EHDR but don't use the gate mechanism */
  46. #define __HAVE_ARCH_GATE_AREA 1
  47. /*
  48. * Subtle: (1 << PAGE_SHIFT) is an int, not an unsigned long. So if we
  49. * assign PAGE_MASK to a larger type it gets extended the way we want
  50. * (i.e. with 1s in the high bits)
  51. */
  52. #define PAGE_MASK (~((1 << PAGE_SHIFT) - 1))
  53. /*
  54. * KERNELBASE is the virtual address of the start of the kernel, it's often
  55. * the same as PAGE_OFFSET, but _might not be_.
  56. *
  57. * The kdump dump kernel is one example where KERNELBASE != PAGE_OFFSET.
  58. *
  59. * PAGE_OFFSET is the virtual address of the start of lowmem.
  60. *
  61. * PHYSICAL_START is the physical address of the start of the kernel.
  62. *
  63. * MEMORY_START is the physical address of the start of lowmem.
  64. *
  65. * KERNELBASE, PAGE_OFFSET, and PHYSICAL_START are all configurable on
  66. * ppc32 and based on how they are set we determine MEMORY_START.
  67. *
  68. * For the linear mapping the following equation should be true:
  69. * KERNELBASE - PAGE_OFFSET = PHYSICAL_START - MEMORY_START
  70. *
  71. * Also, KERNELBASE >= PAGE_OFFSET and PHYSICAL_START >= MEMORY_START
  72. *
  73. * There are two was to determine a physical address from a virtual one:
  74. * va = pa + PAGE_OFFSET - MEMORY_START
  75. * va = pa + KERNELBASE - PHYSICAL_START
  76. *
  77. * If you want to know something's offset from the start of the kernel you
  78. * should subtract KERNELBASE.
  79. *
  80. * If you want to test if something's a kernel address, use is_kernel_addr().
  81. */
  82. #define KERNELBASE ASM_CONST(CONFIG_KERNEL_START)
  83. #define PAGE_OFFSET ASM_CONST(CONFIG_PAGE_OFFSET)
  84. #define LOAD_OFFSET ASM_CONST((CONFIG_KERNEL_START-CONFIG_PHYSICAL_START))
  85. #if defined(CONFIG_NONSTATIC_KERNEL)
  86. #ifndef __ASSEMBLY__
  87. extern phys_addr_t memstart_addr;
  88. extern phys_addr_t kernstart_addr;
  89. #ifdef CONFIG_RELOCATABLE_PPC32
  90. extern long long virt_phys_offset;
  91. #endif
  92. #endif /* __ASSEMBLY__ */
  93. #define PHYSICAL_START kernstart_addr
  94. #else /* !CONFIG_NONSTATIC_KERNEL */
  95. #define PHYSICAL_START ASM_CONST(CONFIG_PHYSICAL_START)
  96. #endif
  97. /* See Description below for VIRT_PHYS_OFFSET */
  98. #ifdef CONFIG_RELOCATABLE_PPC32
  99. #define VIRT_PHYS_OFFSET virt_phys_offset
  100. #else
  101. #define VIRT_PHYS_OFFSET (KERNELBASE - PHYSICAL_START)
  102. #endif
  103. #ifdef CONFIG_PPC64
  104. #define MEMORY_START 0UL
  105. #elif defined(CONFIG_NONSTATIC_KERNEL)
  106. #define MEMORY_START memstart_addr
  107. #else
  108. #define MEMORY_START (PHYSICAL_START + PAGE_OFFSET - KERNELBASE)
  109. #endif
  110. #ifdef CONFIG_FLATMEM
  111. #define ARCH_PFN_OFFSET ((unsigned long)(MEMORY_START >> PAGE_SHIFT))
  112. #define pfn_valid(pfn) ((pfn) >= ARCH_PFN_OFFSET && (pfn) < max_mapnr)
  113. #endif
  114. #define virt_to_page(kaddr) pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
  115. #define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT)
  116. #define virt_addr_valid(kaddr) pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
  117. /*
  118. * On Book-E parts we need __va to parse the device tree and we can't
  119. * determine MEMORY_START until then. However we can determine PHYSICAL_START
  120. * from information at hand (program counter, TLB lookup).
  121. *
  122. * On BookE with RELOCATABLE (RELOCATABLE_PPC32)
  123. *
  124. * With RELOCATABLE_PPC32, we support loading the kernel at any physical
  125. * address without any restriction on the page alignment.
  126. *
  127. * We find the runtime address of _stext and relocate ourselves based on
  128. * the following calculation:
  129. *
  130. * virtual_base = ALIGN_DOWN(KERNELBASE,256M) +
  131. * MODULO(_stext.run,256M)
  132. * and create the following mapping:
  133. *
  134. * ALIGN_DOWN(_stext.run,256M) => ALIGN_DOWN(KERNELBASE,256M)
  135. *
  136. * When we process relocations, we cannot depend on the
  137. * existing equation for the __va()/__pa() translations:
  138. *
  139. * __va(x) = (x) - PHYSICAL_START + KERNELBASE
  140. *
  141. * Where:
  142. * PHYSICAL_START = kernstart_addr = Physical address of _stext
  143. * KERNELBASE = Compiled virtual address of _stext.
  144. *
  145. * This formula holds true iff, kernel load address is TLB page aligned.
  146. *
  147. * In our case, we need to also account for the shift in the kernel Virtual
  148. * address.
  149. *
  150. * E.g.,
  151. *
  152. * Let the kernel be loaded at 64MB and KERNELBASE be 0xc0000000 (same as PAGE_OFFSET).
  153. * In this case, we would be mapping 0 to 0xc0000000, and kernstart_addr = 64M
  154. *
  155. * Now __va(1MB) = (0x100000) - (0x4000000) + 0xc0000000
  156. * = 0xbc100000 , which is wrong.
  157. *
  158. * Rather, it should be : 0xc0000000 + 0x100000 = 0xc0100000
  159. * according to our mapping.
  160. *
  161. * Hence we use the following formula to get the translations right:
  162. *
  163. * __va(x) = (x) - [ PHYSICAL_START - Effective KERNELBASE ]
  164. *
  165. * Where :
  166. * PHYSICAL_START = dynamic load address.(kernstart_addr variable)
  167. * Effective KERNELBASE = virtual_base =
  168. * = ALIGN_DOWN(KERNELBASE,256M) +
  169. * MODULO(PHYSICAL_START,256M)
  170. *
  171. * To make the cost of __va() / __pa() more light weight, we introduce
  172. * a new variable virt_phys_offset, which will hold :
  173. *
  174. * virt_phys_offset = Effective KERNELBASE - PHYSICAL_START
  175. * = ALIGN_DOWN(KERNELBASE,256M) -
  176. * ALIGN_DOWN(PHYSICALSTART,256M)
  177. *
  178. * Hence :
  179. *
  180. * __va(x) = x - PHYSICAL_START + Effective KERNELBASE
  181. * = x + virt_phys_offset
  182. *
  183. * and
  184. * __pa(x) = x + PHYSICAL_START - Effective KERNELBASE
  185. * = x - virt_phys_offset
  186. *
  187. * On non-Book-E PPC64 PAGE_OFFSET and MEMORY_START are constants so use
  188. * the other definitions for __va & __pa.
  189. */
  190. #ifdef CONFIG_BOOKE
  191. #define __va(x) ((void *)(unsigned long)((phys_addr_t)(x) + VIRT_PHYS_OFFSET))
  192. #define __pa(x) ((unsigned long)(x) - VIRT_PHYS_OFFSET)
  193. #else
  194. #ifdef CONFIG_PPC64
  195. /*
  196. * gcc miscompiles (unsigned long)(&static_var) - PAGE_OFFSET
  197. * with -mcmodel=medium, so we use & and | instead of - and + on 64-bit.
  198. */
  199. #define __va(x) ((void *)(unsigned long)((phys_addr_t)(x) | PAGE_OFFSET))
  200. #define __pa(x) ((unsigned long)(x) & 0x0fffffffffffffffUL)
  201. #else /* 32-bit, non book E */
  202. #define __va(x) ((void *)(unsigned long)((phys_addr_t)(x) + PAGE_OFFSET - MEMORY_START))
  203. #define __pa(x) ((unsigned long)(x) - PAGE_OFFSET + MEMORY_START)
  204. #endif
  205. #endif
  206. /*
  207. * Unfortunately the PLT is in the BSS in the PPC32 ELF ABI,
  208. * and needs to be executable. This means the whole heap ends
  209. * up being executable.
  210. */
  211. #define VM_DATA_DEFAULT_FLAGS32 (VM_READ | VM_WRITE | VM_EXEC | \
  212. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
  213. #define VM_DATA_DEFAULT_FLAGS64 (VM_READ | VM_WRITE | \
  214. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
  215. #ifdef __powerpc64__
  216. #include <asm/page_64.h>
  217. #else
  218. #include <asm/page_32.h>
  219. #endif
  220. /* align addr on a size boundary - adjust address up/down if needed */
  221. #define _ALIGN_UP(addr,size) (((addr)+((size)-1))&(~((size)-1)))
  222. #define _ALIGN_DOWN(addr,size) ((addr)&(~((size)-1)))
  223. /* align addr on a size boundary - adjust address up if needed */
  224. #define _ALIGN(addr,size) _ALIGN_UP(addr,size)
  225. /*
  226. * Don't compare things with KERNELBASE or PAGE_OFFSET to test for
  227. * "kernelness", use is_kernel_addr() - it should do what you want.
  228. */
  229. #ifdef CONFIG_PPC_BOOK3E_64
  230. #define is_kernel_addr(x) ((x) >= 0x8000000000000000ul)
  231. #else
  232. #define is_kernel_addr(x) ((x) >= PAGE_OFFSET)
  233. #endif
  234. #ifndef CONFIG_PPC_BOOK3S_64
  235. /*
  236. * Use the top bit of the higher-level page table entries to indicate whether
  237. * the entries we point to contain hugepages. This works because we know that
  238. * the page tables live in kernel space. If we ever decide to support having
  239. * page tables at arbitrary addresses, this breaks and will have to change.
  240. */
  241. #ifdef CONFIG_PPC64
  242. #define PD_HUGE 0x8000000000000000
  243. #else
  244. #define PD_HUGE 0x80000000
  245. #endif
  246. #endif /* CONFIG_PPC_BOOK3S_64 */
  247. /*
  248. * Some number of bits at the level of the page table that points to
  249. * a hugepte are used to encode the size. This masks those bits.
  250. */
  251. #define HUGEPD_SHIFT_MASK 0x3f
  252. #ifndef __ASSEMBLY__
  253. #undef STRICT_MM_TYPECHECKS
  254. #ifdef STRICT_MM_TYPECHECKS
  255. /* These are used to make use of C type-checking. */
  256. /* PTE level */
  257. typedef struct { pte_basic_t pte; } pte_t;
  258. #define pte_val(x) ((x).pte)
  259. #define __pte(x) ((pte_t) { (x) })
  260. /* 64k pages additionally define a bigger "real PTE" type that gathers
  261. * the "second half" part of the PTE for pseudo 64k pages
  262. */
  263. #if defined(CONFIG_PPC_64K_PAGES) && defined(CONFIG_PPC_STD_MMU_64)
  264. typedef struct { pte_t pte; unsigned long hidx; } real_pte_t;
  265. #else
  266. typedef struct { pte_t pte; } real_pte_t;
  267. #endif
  268. /* PMD level */
  269. #ifdef CONFIG_PPC64
  270. typedef struct { unsigned long pmd; } pmd_t;
  271. #define pmd_val(x) ((x).pmd)
  272. #define __pmd(x) ((pmd_t) { (x) })
  273. /* PUD level exusts only on 4k pages */
  274. #ifndef CONFIG_PPC_64K_PAGES
  275. typedef struct { unsigned long pud; } pud_t;
  276. #define pud_val(x) ((x).pud)
  277. #define __pud(x) ((pud_t) { (x) })
  278. #endif /* !CONFIG_PPC_64K_PAGES */
  279. #endif /* CONFIG_PPC64 */
  280. /* PGD level */
  281. typedef struct { unsigned long pgd; } pgd_t;
  282. #define pgd_val(x) ((x).pgd)
  283. #define __pgd(x) ((pgd_t) { (x) })
  284. /* Page protection bits */
  285. typedef struct { unsigned long pgprot; } pgprot_t;
  286. #define pgprot_val(x) ((x).pgprot)
  287. #define __pgprot(x) ((pgprot_t) { (x) })
  288. #else
  289. /*
  290. * .. while these make it easier on the compiler
  291. */
  292. typedef pte_basic_t pte_t;
  293. #define pte_val(x) (x)
  294. #define __pte(x) (x)
  295. #if defined(CONFIG_PPC_64K_PAGES) && defined(CONFIG_PPC_STD_MMU_64)
  296. typedef struct { pte_t pte; unsigned long hidx; } real_pte_t;
  297. #else
  298. typedef pte_t real_pte_t;
  299. #endif
  300. #ifdef CONFIG_PPC64
  301. typedef unsigned long pmd_t;
  302. #define pmd_val(x) (x)
  303. #define __pmd(x) (x)
  304. #ifndef CONFIG_PPC_64K_PAGES
  305. typedef unsigned long pud_t;
  306. #define pud_val(x) (x)
  307. #define __pud(x) (x)
  308. #endif /* !CONFIG_PPC_64K_PAGES */
  309. #endif /* CONFIG_PPC64 */
  310. typedef unsigned long pgd_t;
  311. #define pgd_val(x) (x)
  312. #define pgprot_val(x) (x)
  313. typedef unsigned long pgprot_t;
  314. #define __pgd(x) (x)
  315. #define __pgprot(x) (x)
  316. #endif
  317. typedef struct { signed long pd; } hugepd_t;
  318. #ifdef CONFIG_HUGETLB_PAGE
  319. #ifdef CONFIG_PPC_BOOK3S_64
  320. static inline int hugepd_ok(hugepd_t hpd)
  321. {
  322. /*
  323. * hugepd pointer, bottom two bits == 00 and next 4 bits
  324. * indicate size of table
  325. */
  326. return (((hpd.pd & 0x3) == 0x0) && ((hpd.pd & HUGEPD_SHIFT_MASK) != 0));
  327. }
  328. #else
  329. static inline int hugepd_ok(hugepd_t hpd)
  330. {
  331. return (hpd.pd > 0);
  332. }
  333. #endif
  334. #define is_hugepd(pdep) (hugepd_ok(*((hugepd_t *)(pdep))))
  335. int pgd_huge(pgd_t pgd);
  336. #else /* CONFIG_HUGETLB_PAGE */
  337. #define is_hugepd(pdep) 0
  338. #define pgd_huge(pgd) 0
  339. #endif /* CONFIG_HUGETLB_PAGE */
  340. struct page;
  341. extern void clear_user_page(void *page, unsigned long vaddr, struct page *pg);
  342. extern void copy_user_page(void *to, void *from, unsigned long vaddr,
  343. struct page *p);
  344. extern int page_is_ram(unsigned long pfn);
  345. extern int devmem_is_allowed(unsigned long pfn);
  346. #ifdef CONFIG_PPC_SMLPAR
  347. void arch_free_page(struct page *page, int order);
  348. #define HAVE_ARCH_FREE_PAGE
  349. #endif
  350. struct vm_area_struct;
  351. #ifdef CONFIG_PPC_64K_PAGES
  352. typedef pte_t *pgtable_t;
  353. #else
  354. typedef struct page *pgtable_t;
  355. #endif
  356. #include <asm-generic/memory_model.h>
  357. #endif /* __ASSEMBLY__ */
  358. #endif /* _ASM_POWERPC_PAGE_H */