fb04f10a78ab35c462e251d0bacce3a05da9be32
[linux-2.6-block.git] / arch / arm64 / include / asm / memory.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Based on arch/arm/include/asm/memory.h
4  *
5  * Copyright (C) 2000-2002 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  *
8  * Note: this file should not be included by non-asm/.h files
9  */
10 #ifndef __ASM_MEMORY_H
11 #define __ASM_MEMORY_H
12
13 #include <linux/compiler.h>
14 #include <linux/const.h>
15 #include <linux/types.h>
16 #include <asm/bug.h>
17 #include <asm/page-def.h>
18 #include <linux/sizes.h>
19
20 /*
21  * Size of the PCI I/O space. This must remain a power of two so that
22  * IO_SPACE_LIMIT acts as a mask for the low bits of I/O addresses.
23  */
24 #define PCI_IO_SIZE             SZ_16M
25
26 /*
27  * VMEMMAP_SIZE - allows the whole linear region to be covered by
28  *                a struct page array
29  */
30 #define VMEMMAP_SIZE (UL(1) << (VA_BITS - PAGE_SHIFT - 1 + STRUCT_PAGE_MAX_SHIFT))
31
32 /*
33  * PAGE_OFFSET - the virtual address of the start of the linear map (top
34  *               (VA_BITS - 1))
35  * KIMAGE_VADDR - the virtual address of the start of the kernel image
36  * VA_BITS - the maximum number of bits for virtual addresses.
37  * VA_START - the first kernel virtual address.
38  */
39 #define VA_BITS                 (CONFIG_ARM64_VA_BITS)
40 #define VA_START                (UL(0xffffffffffffffff) - \
41         (UL(1) << VA_BITS) + 1)
42 #define PAGE_OFFSET             (UL(0xffffffffffffffff) - \
43         (UL(1) << (VA_BITS - 1)) + 1)
44 #define KIMAGE_VADDR            (MODULES_END)
45 #define BPF_JIT_REGION_START    (VA_START + KASAN_SHADOW_SIZE)
46 #define BPF_JIT_REGION_SIZE     (SZ_128M)
47 #define BPF_JIT_REGION_END      (BPF_JIT_REGION_START + BPF_JIT_REGION_SIZE)
48 #define MODULES_END             (MODULES_VADDR + MODULES_VSIZE)
49 #define MODULES_VADDR           (BPF_JIT_REGION_END)
50 #define MODULES_VSIZE           (SZ_128M)
51 #define VMEMMAP_START           (PAGE_OFFSET - VMEMMAP_SIZE)
52 #define PCI_IO_END              (VMEMMAP_START - SZ_2M)
53 #define PCI_IO_START            (PCI_IO_END - PCI_IO_SIZE)
54 #define FIXADDR_TOP             (PCI_IO_START - SZ_2M)
55
56 #define KERNEL_START      _text
57 #define KERNEL_END        _end
58
59 #ifdef CONFIG_ARM64_USER_VA_BITS_52
60 #define MAX_USER_VA_BITS        52
61 #else
62 #define MAX_USER_VA_BITS        VA_BITS
63 #endif
64
65 /*
66  * Generic and tag-based KASAN require 1/8th and 1/16th of the kernel virtual
67  * address space for the shadow region respectively. They can bloat the stack
68  * significantly, so double the (minimum) stack size when they are in use.
69  */
70 #ifdef CONFIG_KASAN
71 #define KASAN_SHADOW_SIZE       (UL(1) << (VA_BITS - KASAN_SHADOW_SCALE_SHIFT))
72 #define KASAN_THREAD_SHIFT      1
73 #else
74 #define KASAN_SHADOW_SIZE       (0)
75 #define KASAN_THREAD_SHIFT      0
76 #endif
77
78 #define MIN_THREAD_SHIFT        (14 + KASAN_THREAD_SHIFT)
79
80 /*
81  * VMAP'd stacks are allocated at page granularity, so we must ensure that such
82  * stacks are a multiple of page size.
83  */
84 #if defined(CONFIG_VMAP_STACK) && (MIN_THREAD_SHIFT < PAGE_SHIFT)
85 #define THREAD_SHIFT            PAGE_SHIFT
86 #else
87 #define THREAD_SHIFT            MIN_THREAD_SHIFT
88 #endif
89
90 #if THREAD_SHIFT >= PAGE_SHIFT
91 #define THREAD_SIZE_ORDER       (THREAD_SHIFT - PAGE_SHIFT)
92 #endif
93
94 #define THREAD_SIZE             (UL(1) << THREAD_SHIFT)
95
96 /*
97  * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
98  * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry
99  * assembly.
100  */
101 #ifdef CONFIG_VMAP_STACK
102 #define THREAD_ALIGN            (2 * THREAD_SIZE)
103 #else
104 #define THREAD_ALIGN            THREAD_SIZE
105 #endif
106
107 #define IRQ_STACK_SIZE          THREAD_SIZE
108
109 #define OVERFLOW_STACK_SIZE     SZ_4K
110
111 /*
112  * Alignment of kernel segments (e.g. .text, .data).
113  */
114 #if defined(CONFIG_DEBUG_ALIGN_RODATA)
115 /*
116  *  4 KB granule:   1 level 2 entry
117  * 16 KB granule: 128 level 3 entries, with contiguous bit
118  * 64 KB granule:  32 level 3 entries, with contiguous bit
119  */
120 #define SEGMENT_ALIGN                   SZ_2M
121 #else
122 /*
123  *  4 KB granule:  16 level 3 entries, with contiguous bit
124  * 16 KB granule:   4 level 3 entries, without contiguous bit
125  * 64 KB granule:   1 level 3 entry
126  */
127 #define SEGMENT_ALIGN                   SZ_64K
128 #endif
129
130 /*
131  * Memory types available.
132  */
133 #define MT_DEVICE_nGnRnE        0
134 #define MT_DEVICE_nGnRE         1
135 #define MT_DEVICE_GRE           2
136 #define MT_NORMAL_NC            3
137 #define MT_NORMAL               4
138 #define MT_NORMAL_WT            5
139
140 /*
141  * Memory types for Stage-2 translation
142  */
143 #define MT_S2_NORMAL            0xf
144 #define MT_S2_DEVICE_nGnRE      0x1
145
146 /*
147  * Memory types for Stage-2 translation when ID_AA64MMFR2_EL1.FWB is 0001
148  * Stage-2 enforces Normal-WB and Device-nGnRE
149  */
150 #define MT_S2_FWB_NORMAL        6
151 #define MT_S2_FWB_DEVICE_nGnRE  1
152
153 #ifdef CONFIG_ARM64_4K_PAGES
154 #define IOREMAP_MAX_ORDER       (PUD_SHIFT)
155 #else
156 #define IOREMAP_MAX_ORDER       (PMD_SHIFT)
157 #endif
158
159 #ifndef __ASSEMBLY__
160
161 #include <linux/bitops.h>
162 #include <linux/mmdebug.h>
163
164 extern s64                      memstart_addr;
165 /* PHYS_OFFSET - the physical address of the start of memory. */
166 #define PHYS_OFFSET             ({ VM_BUG_ON(memstart_addr & 1); memstart_addr; })
167
168 /* the virtual base of the kernel image (minus TEXT_OFFSET) */
169 extern u64                      kimage_vaddr;
170
171 /* the offset between the kernel virtual and physical mappings */
172 extern u64                      kimage_voffset;
173
174 static inline unsigned long kaslr_offset(void)
175 {
176         return kimage_vaddr - KIMAGE_VADDR;
177 }
178
179 /* the actual size of a user virtual address */
180 extern u64                      vabits_user;
181
182 /*
183  * Allow all memory at the discovery stage. We will clip it later.
184  */
185 #define MIN_MEMBLOCK_ADDR       0
186 #define MAX_MEMBLOCK_ADDR       U64_MAX
187
188 /*
189  * PFNs are used to describe any physical page; this means
190  * PFN 0 == physical address 0.
191  *
192  * This is the PFN of the first RAM page in the kernel
193  * direct-mapped view.  We assume this is the first page
194  * of RAM in the mem_map as well.
195  */
196 #define PHYS_PFN_OFFSET (PHYS_OFFSET >> PAGE_SHIFT)
197
198 /*
199  * When dealing with data aborts, watchpoints, or instruction traps we may end
200  * up with a tagged userland pointer. Clear the tag to get a sane pointer to
201  * pass on to access_ok(), for instance.
202  */
203 #define untagged_addr(addr)     \
204         ((__typeof__(addr))sign_extend64((u64)(addr), 55))
205
206 #ifdef CONFIG_KASAN_SW_TAGS
207 #define __tag_shifted(tag)      ((u64)(tag) << 56)
208 #define __tag_set(addr, tag)    (__typeof__(addr))( \
209                 ((u64)(addr) & ~__tag_shifted(0xff)) | __tag_shifted(tag))
210 #define __tag_reset(addr)       untagged_addr(addr)
211 #define __tag_get(addr)         (__u8)((u64)(addr) >> 56)
212 #else
213 static inline const void *__tag_set(const void *addr, u8 tag)
214 {
215         return addr;
216 }
217
218 #define __tag_reset(addr)       (addr)
219 #define __tag_get(addr)         0
220 #endif
221
222 /*
223  * Physical vs virtual RAM address space conversion.  These are
224  * private definitions which should NOT be used outside memory.h
225  * files.  Use virt_to_phys/phys_to_virt/__pa/__va instead.
226  */
227
228
229 /*
230  * The linear kernel range starts in the middle of the virtual adddress
231  * space. Testing the top bit for the start of the region is a
232  * sufficient check.
233  */
234 #define __is_lm_address(addr)   (!!((addr) & BIT(VA_BITS - 1)))
235
236 #define __lm_to_phys(addr)      (((addr) & ~PAGE_OFFSET) + PHYS_OFFSET)
237 #define __kimg_to_phys(addr)    ((addr) - kimage_voffset)
238
239 #define __virt_to_phys_nodebug(x) ({                                    \
240         phys_addr_t __x = (phys_addr_t)(x);                             \
241         __is_lm_address(__x) ? __lm_to_phys(__x) :                      \
242                                __kimg_to_phys(__x);                     \
243 })
244
245 #define __pa_symbol_nodebug(x)  __kimg_to_phys((phys_addr_t)(x))
246
247 #ifdef CONFIG_DEBUG_VIRTUAL
248 extern phys_addr_t __virt_to_phys(unsigned long x);
249 extern phys_addr_t __phys_addr_symbol(unsigned long x);
250 #else
251 #define __virt_to_phys(x)       __virt_to_phys_nodebug(x)
252 #define __phys_addr_symbol(x)   __pa_symbol_nodebug(x)
253 #endif
254
255 #define __phys_to_virt(x)       ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)
256 #define __phys_to_kimg(x)       ((unsigned long)((x) + kimage_voffset))
257
258 /*
259  * Convert a page to/from a physical address
260  */
261 #define page_to_phys(page)      (__pfn_to_phys(page_to_pfn(page)))
262 #define phys_to_page(phys)      (pfn_to_page(__phys_to_pfn(phys)))
263
264 /*
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
275 #define phys_to_virt phys_to_virt
276 static inline void *phys_to_virt(phys_addr_t x)
277 {
278         return (void *)(__phys_to_virt(x));
279 }
280
281 /*
282  * Drivers should NOT use these either.
283  */
284 #define __pa(x)                 __virt_to_phys((unsigned long)(x))
285 #define __pa_symbol(x)          __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
286 #define __pa_nodebug(x)         __virt_to_phys_nodebug((unsigned long)(x))
287 #define __va(x)                 ((void *)__phys_to_virt((phys_addr_t)(x)))
288 #define pfn_to_kaddr(pfn)       __va((pfn) << PAGE_SHIFT)
289 #define virt_to_pfn(x)      __phys_to_pfn(__virt_to_phys((unsigned long)(x)))
290 #define sym_to_pfn(x)       __phys_to_pfn(__pa_symbol(x))
291
292 /*
293  *  virt_to_page(k)     convert a _valid_ virtual address to struct page *
294  *  virt_addr_valid(k)  indicates whether a virtual address is valid
295  */
296 #define ARCH_PFN_OFFSET         ((unsigned long)PHYS_PFN_OFFSET)
297
298 #if !defined(CONFIG_SPARSEMEM_VMEMMAP) || defined(CONFIG_DEBUG_VIRTUAL)
299 #define virt_to_page(kaddr)     pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
300 #define _virt_addr_valid(kaddr) pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
301 #else
302 #define __virt_to_pgoff(kaddr)  (((u64)(kaddr) & ~PAGE_OFFSET) / PAGE_SIZE * sizeof(struct page))
303 #define __page_to_voff(kaddr)   (((u64)(kaddr) & ~VMEMMAP_START) * PAGE_SIZE / sizeof(struct page))
304
305 #define page_to_virt(page)      ({                                      \
306         unsigned long __addr =                                          \
307                 ((__page_to_voff(page)) | PAGE_OFFSET);                 \
308         const void *__addr_tag =                                        \
309                 __tag_set((void *)__addr, page_kasan_tag(page));        \
310         ((void *)__addr_tag);                                           \
311 })
312
313 #define virt_to_page(vaddr)     ((struct page *)((__virt_to_pgoff(vaddr)) | VMEMMAP_START))
314
315 #define _virt_addr_valid(kaddr) pfn_valid((((u64)(kaddr) & ~PAGE_OFFSET) \
316                                            + PHYS_OFFSET) >> PAGE_SHIFT)
317 #endif
318 #endif
319
320 #define _virt_addr_is_linear(kaddr)     \
321         (__tag_reset((u64)(kaddr)) >= PAGE_OFFSET)
322 #define virt_addr_valid(kaddr)          \
323         (_virt_addr_is_linear(kaddr) && _virt_addr_valid(kaddr))
324
325 /*
326  * Given that the GIC architecture permits ITS implementations that can only be
327  * configured with a LPI table address once, GICv3 systems with many CPUs may
328  * end up reserving a lot of different regions after a kexec for their LPI
329  * tables (one per CPU), as we are forced to reuse the same memory after kexec
330  * (and thus reserve it persistently with EFI beforehand)
331  */
332 #if defined(CONFIG_EFI) && defined(CONFIG_ARM_GIC_V3_ITS)
333 # define INIT_MEMBLOCK_RESERVED_REGIONS (INIT_MEMBLOCK_REGIONS + NR_CPUS + 1)
334 #endif
335
336 #include <asm-generic/memory_model.h>
337
338 #endif