page.h 12 KB

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