memory.h 10 KB

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