memory.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm/include/asm/memory.h
  4. *
  5. * Copyright (C) 2000-2002 Russell King
  6. * modification for nommu, Hyok S. Choi, 2004
  7. *
  8. * Note: this file should not be included explicitly, include <asm/page.h>
  9. * to get access to these definitions.
  10. */
  11. #ifndef __ASM_ARM_MEMORY_H
  12. #define __ASM_ARM_MEMORY_H
  13. #ifndef _ASMARM_PAGE_H
  14. #error "Do not include <asm/memory.h> directly"
  15. #endif
  16. #include <linux/compiler.h>
  17. #include <linux/const.h>
  18. #include <linux/types.h>
  19. #include <linux/sizes.h>
  20. #ifdef CONFIG_NEED_MACH_MEMORY_H
  21. #include <mach/memory.h>
  22. #endif
  23. #include <asm/kasan_def.h>
  24. /*
  25. * PAGE_OFFSET: the virtual address of the start of lowmem, memory above
  26. * the virtual address range for userspace.
  27. * KERNEL_OFFSET: the virtual address of the start of the kernel image.
  28. * we may further offset this with TEXT_OFFSET in practice.
  29. */
  30. #define PAGE_OFFSET UL(CONFIG_PAGE_OFFSET)
  31. #define KERNEL_OFFSET (PAGE_OFFSET)
  32. #ifdef CONFIG_MMU
  33. /*
  34. * TASK_SIZE - the maximum size of a user space task.
  35. * TASK_UNMAPPED_BASE - the lower boundary of the mmap VM area
  36. */
  37. #ifndef CONFIG_KASAN
  38. #define TASK_SIZE (UL(CONFIG_PAGE_OFFSET) - UL(SZ_16M))
  39. #else
  40. #define TASK_SIZE (KASAN_SHADOW_START)
  41. #endif
  42. #define TASK_UNMAPPED_BASE ALIGN(TASK_SIZE / 3, SZ_16M)
  43. /*
  44. * The maximum size of a 26-bit user space task.
  45. */
  46. #define TASK_SIZE_26 (UL(1) << 26)
  47. /*
  48. * The module space lives between the addresses given by TASK_SIZE
  49. * and PAGE_OFFSET - it must be within 32MB of the kernel text.
  50. */
  51. #ifndef CONFIG_THUMB2_KERNEL
  52. #define MODULES_VADDR (PAGE_OFFSET - SZ_16M)
  53. #else
  54. /* smaller range for Thumb-2 symbols relocation (2^24)*/
  55. #define MODULES_VADDR (PAGE_OFFSET - SZ_8M)
  56. #endif
  57. #if TASK_SIZE > MODULES_VADDR
  58. #error Top of user space clashes with start of module space
  59. #endif
  60. /*
  61. * The highmem pkmap virtual space shares the end of the module area.
  62. */
  63. #ifdef CONFIG_HIGHMEM
  64. #define MODULES_END (PAGE_OFFSET - PMD_SIZE)
  65. #else
  66. #define MODULES_END (PAGE_OFFSET)
  67. #endif
  68. /*
  69. * The XIP kernel gets mapped at the bottom of the module vm area.
  70. * Since we use sections to map it, this macro replaces the physical address
  71. * with its virtual address while keeping offset from the base section.
  72. */
  73. #define XIP_VIRT_ADDR(physaddr) (MODULES_VADDR + ((physaddr) & 0x000fffff))
  74. #define FDT_FIXED_BASE UL(0xff800000)
  75. #define FDT_FIXED_SIZE (2 * SECTION_SIZE)
  76. #define FDT_VIRT_BASE(physbase) ((void *)(FDT_FIXED_BASE | (physbase) % SECTION_SIZE))
  77. #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
  78. /*
  79. * Allow 16MB-aligned ioremap pages
  80. */
  81. #define IOREMAP_MAX_ORDER 24
  82. #endif
  83. #define VECTORS_BASE UL(0xffff0000)
  84. #else /* CONFIG_MMU */
  85. #ifndef __ASSEMBLY__
  86. extern unsigned long setup_vectors_base(void);
  87. extern unsigned long vectors_base;
  88. #define VECTORS_BASE vectors_base
  89. #endif
  90. /*
  91. * The limitation of user task size can grow up to the end of free ram region.
  92. * It is difficult to define and perhaps will never meet the original meaning
  93. * of this define that was meant to.
  94. * Fortunately, there is no reference for this in noMMU mode, for now.
  95. */
  96. #define TASK_SIZE UL(0xffffffff)
  97. #ifndef TASK_UNMAPPED_BASE
  98. #define TASK_UNMAPPED_BASE UL(0x00000000)
  99. #endif
  100. #ifndef END_MEM
  101. #define END_MEM (UL(CONFIG_DRAM_BASE) + CONFIG_DRAM_SIZE)
  102. #endif
  103. /*
  104. * The module can be at any place in ram in nommu mode.
  105. */
  106. #define MODULES_END (END_MEM)
  107. #define MODULES_VADDR PAGE_OFFSET
  108. #define XIP_VIRT_ADDR(physaddr) (physaddr)
  109. #define FDT_VIRT_BASE(physbase) ((void *)(physbase))
  110. #endif /* !CONFIG_MMU */
  111. #ifdef CONFIG_XIP_KERNEL
  112. #define KERNEL_START _sdata
  113. #else
  114. #define KERNEL_START _stext
  115. #endif
  116. #define KERNEL_END _end
  117. /*
  118. * We fix the TCM memories max 32 KiB ITCM resp DTCM at these
  119. * locations
  120. */
  121. #ifdef CONFIG_HAVE_TCM
  122. #define ITCM_OFFSET UL(0xfffe0000)
  123. #define DTCM_OFFSET UL(0xfffe8000)
  124. #endif
  125. /*
  126. * Convert a page to/from a physical address
  127. */
  128. #define page_to_phys(page) (__pfn_to_phys(page_to_pfn(page)))
  129. #define phys_to_page(phys) (pfn_to_page(__phys_to_pfn(phys)))
  130. /*
  131. * PLAT_PHYS_OFFSET is the offset (from zero) of the start of physical
  132. * memory. This is used for XIP and NoMMU kernels, and on platforms that don't
  133. * have CONFIG_ARM_PATCH_PHYS_VIRT. Assembly code must always use
  134. * PLAT_PHYS_OFFSET and not PHYS_OFFSET.
  135. */
  136. #define PLAT_PHYS_OFFSET UL(CONFIG_PHYS_OFFSET)
  137. #ifndef __ASSEMBLY__
  138. /*
  139. * Physical start and end address of the kernel sections. These addresses are
  140. * 2MB-aligned to match the section mappings placed over the kernel. We use
  141. * u64 so that LPAE mappings beyond the 32bit limit will work out as well.
  142. */
  143. extern u64 kernel_sec_start;
  144. extern u64 kernel_sec_end;
  145. /*
  146. * Physical vs virtual RAM address space conversion. These are
  147. * private definitions which should NOT be used outside memory.h
  148. * files. Use virt_to_phys/phys_to_virt/__pa/__va instead.
  149. *
  150. * PFNs are used to describe any physical page; this means
  151. * PFN 0 == physical address 0.
  152. */
  153. #if defined(CONFIG_ARM_PATCH_PHYS_VIRT)
  154. /*
  155. * Constants used to force the right instruction encodings and shifts
  156. * so that all we need to do is modify the 8-bit constant field.
  157. */
  158. #define __PV_BITS_31_24 0x81000000
  159. #define __PV_BITS_23_16 0x810000
  160. #define __PV_BITS_7_0 0x81
  161. extern unsigned long __pv_phys_pfn_offset;
  162. extern u64 __pv_offset;
  163. extern void fixup_pv_table(const void *, unsigned long);
  164. extern const void *__pv_table_begin, *__pv_table_end;
  165. #define PHYS_OFFSET ((phys_addr_t)__pv_phys_pfn_offset << PAGE_SHIFT)
  166. #define PHYS_PFN_OFFSET (__pv_phys_pfn_offset)
  167. #ifndef CONFIG_THUMB2_KERNEL
  168. #define __pv_stub(from,to,instr) \
  169. __asm__("@ __pv_stub\n" \
  170. "1: " instr " %0, %1, %2\n" \
  171. "2: " instr " %0, %0, %3\n" \
  172. " .pushsection .pv_table,\"a\"\n" \
  173. " .long 1b - ., 2b - .\n" \
  174. " .popsection\n" \
  175. : "=r" (to) \
  176. : "r" (from), "I" (__PV_BITS_31_24), \
  177. "I"(__PV_BITS_23_16))
  178. #define __pv_add_carry_stub(x, y) \
  179. __asm__("@ __pv_add_carry_stub\n" \
  180. "0: movw %R0, #0\n" \
  181. " adds %Q0, %1, %R0, lsl #20\n" \
  182. "1: mov %R0, %2\n" \
  183. " adc %R0, %R0, #0\n" \
  184. " .pushsection .pv_table,\"a\"\n" \
  185. " .long 0b - ., 1b - .\n" \
  186. " .popsection\n" \
  187. : "=&r" (y) \
  188. : "r" (x), "I" (__PV_BITS_7_0) \
  189. : "cc")
  190. #else
  191. #define __pv_stub(from,to,instr) \
  192. __asm__("@ __pv_stub\n" \
  193. "0: movw %0, #0\n" \
  194. " lsl %0, #21\n" \
  195. " " instr " %0, %1, %0\n" \
  196. " .pushsection .pv_table,\"a\"\n" \
  197. " .long 0b - .\n" \
  198. " .popsection\n" \
  199. : "=&r" (to) \
  200. : "r" (from))
  201. #define __pv_add_carry_stub(x, y) \
  202. __asm__("@ __pv_add_carry_stub\n" \
  203. "0: movw %R0, #0\n" \
  204. " lsls %R0, #21\n" \
  205. " adds %Q0, %1, %R0\n" \
  206. "1: mvn %R0, #0\n" \
  207. " adc %R0, %R0, #0\n" \
  208. " .pushsection .pv_table,\"a\"\n" \
  209. " .long 0b - ., 1b - .\n" \
  210. " .popsection\n" \
  211. : "=&r" (y) \
  212. : "r" (x) \
  213. : "cc")
  214. #endif
  215. static inline phys_addr_t __virt_to_phys_nodebug(unsigned long x)
  216. {
  217. phys_addr_t t;
  218. if (sizeof(phys_addr_t) == 4) {
  219. __pv_stub(x, t, "add");
  220. } else {
  221. __pv_add_carry_stub(x, t);
  222. }
  223. return t;
  224. }
  225. static inline unsigned long __phys_to_virt(phys_addr_t x)
  226. {
  227. unsigned long t;
  228. /*
  229. * 'unsigned long' cast discard upper word when
  230. * phys_addr_t is 64 bit, and makes sure that inline
  231. * assembler expression receives 32 bit argument
  232. * in place where 'r' 32 bit operand is expected.
  233. */
  234. __pv_stub((unsigned long) x, t, "sub");
  235. return t;
  236. }
  237. #else
  238. #define PHYS_OFFSET PLAT_PHYS_OFFSET
  239. #define PHYS_PFN_OFFSET ((unsigned long)(PHYS_OFFSET >> PAGE_SHIFT))
  240. static inline phys_addr_t __virt_to_phys_nodebug(unsigned long x)
  241. {
  242. return (phys_addr_t)x - PAGE_OFFSET + PHYS_OFFSET;
  243. }
  244. static inline unsigned long __phys_to_virt(phys_addr_t x)
  245. {
  246. return x - PHYS_OFFSET + PAGE_OFFSET;
  247. }
  248. #endif
  249. static inline unsigned long virt_to_pfn(const void *p)
  250. {
  251. unsigned long kaddr = (unsigned long)p;
  252. return (((kaddr - PAGE_OFFSET) >> PAGE_SHIFT) +
  253. PHYS_PFN_OFFSET);
  254. }
  255. #define __pa_symbol_nodebug(x) __virt_to_phys_nodebug((x))
  256. #ifdef CONFIG_DEBUG_VIRTUAL
  257. extern phys_addr_t __virt_to_phys(unsigned long x);
  258. extern phys_addr_t __phys_addr_symbol(unsigned long x);
  259. #else
  260. #define __virt_to_phys(x) __virt_to_phys_nodebug(x)
  261. #define __phys_addr_symbol(x) __pa_symbol_nodebug(x)
  262. #endif
  263. /*
  264. * These are *only* valid on the kernel direct mapped RAM memory.
  265. * Note: Drivers should NOT use these. They are the wrong
  266. * translation for translating DMA addresses. Use the driver
  267. * DMA support - see dma-mapping.h.
  268. */
  269. #define virt_to_phys virt_to_phys
  270. static inline phys_addr_t virt_to_phys(const volatile void *x)
  271. {
  272. return __virt_to_phys((unsigned long)(x));
  273. }
  274. #define phys_to_virt phys_to_virt
  275. static inline void *phys_to_virt(phys_addr_t x)
  276. {
  277. return (void *)__phys_to_virt(x);
  278. }
  279. /*
  280. * Drivers should NOT use these either.
  281. */
  282. #define __pa(x) __virt_to_phys((unsigned long)(x))
  283. #define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
  284. #define __va(x) ((void *)__phys_to_virt((phys_addr_t)(x)))
  285. #define pfn_to_kaddr(pfn) __va((phys_addr_t)(pfn) << PAGE_SHIFT)
  286. extern long long arch_phys_to_idmap_offset;
  287. /*
  288. * These are for systems that have a hardware interconnect supported alias
  289. * of physical memory for idmap purposes. Most cases should leave these
  290. * untouched. Note: this can only return addresses less than 4GiB.
  291. */
  292. static inline bool arm_has_idmap_alias(void)
  293. {
  294. return IS_ENABLED(CONFIG_MMU) && arch_phys_to_idmap_offset != 0;
  295. }
  296. #define IDMAP_INVALID_ADDR ((u32)~0)
  297. static inline unsigned long phys_to_idmap(phys_addr_t addr)
  298. {
  299. if (IS_ENABLED(CONFIG_MMU) && arch_phys_to_idmap_offset) {
  300. addr += arch_phys_to_idmap_offset;
  301. if (addr > (u32)~0)
  302. addr = IDMAP_INVALID_ADDR;
  303. }
  304. return addr;
  305. }
  306. static inline phys_addr_t idmap_to_phys(unsigned long idmap)
  307. {
  308. phys_addr_t addr = idmap;
  309. if (IS_ENABLED(CONFIG_MMU) && arch_phys_to_idmap_offset)
  310. addr -= arch_phys_to_idmap_offset;
  311. return addr;
  312. }
  313. static inline unsigned long __virt_to_idmap(unsigned long x)
  314. {
  315. return phys_to_idmap(__virt_to_phys(x));
  316. }
  317. #define virt_to_idmap(x) __virt_to_idmap((unsigned long)(x))
  318. /*
  319. * Conversion between a struct page and a physical address.
  320. *
  321. * page_to_pfn(page) convert a struct page * to a PFN number
  322. * pfn_to_page(pfn) convert a _valid_ PFN number to struct page *
  323. *
  324. * virt_to_page(k) convert a _valid_ virtual address to struct page *
  325. * virt_addr_valid(k) indicates whether a virtual address is valid
  326. */
  327. #define ARCH_PFN_OFFSET PHYS_PFN_OFFSET
  328. #define virt_to_page(kaddr) pfn_to_page(virt_to_pfn(kaddr))
  329. #define virt_addr_valid(kaddr) (((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory) \
  330. && pfn_valid(virt_to_pfn(kaddr)))
  331. #endif
  332. #endif