162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Based on arch/arm/include/asm/memory.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2000-2002 Russell King
662306a36Sopenharmony_ci * Copyright (C) 2012 ARM Ltd.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Note: this file should not be included by non-asm/.h files
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci#ifndef __ASM_MEMORY_H
1162306a36Sopenharmony_ci#define __ASM_MEMORY_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/const.h>
1462306a36Sopenharmony_ci#include <linux/sizes.h>
1562306a36Sopenharmony_ci#include <asm/page-def.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/*
1862306a36Sopenharmony_ci * Size of the PCI I/O space. This must remain a power of two so that
1962306a36Sopenharmony_ci * IO_SPACE_LIMIT acts as a mask for the low bits of I/O addresses.
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_ci#define PCI_IO_SIZE		SZ_16M
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/*
2462306a36Sopenharmony_ci * VMEMMAP_SIZE - allows the whole linear region to be covered by
2562306a36Sopenharmony_ci *                a struct page array
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * If we are configured with a 52-bit kernel VA then our VMEMMAP_SIZE
2862306a36Sopenharmony_ci * needs to cover the memory region from the beginning of the 52-bit
2962306a36Sopenharmony_ci * PAGE_OFFSET all the way to PAGE_END for 48-bit. This allows us to
3062306a36Sopenharmony_ci * keep a constant PAGE_OFFSET and "fallback" to using the higher end
3162306a36Sopenharmony_ci * of the VMEMMAP where 52-bit support is not available in hardware.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci#define VMEMMAP_SHIFT	(PAGE_SHIFT - STRUCT_PAGE_MAX_SHIFT)
3462306a36Sopenharmony_ci#define VMEMMAP_SIZE	((_PAGE_END(VA_BITS_MIN) - PAGE_OFFSET) >> VMEMMAP_SHIFT)
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/*
3762306a36Sopenharmony_ci * PAGE_OFFSET - the virtual address of the start of the linear map, at the
3862306a36Sopenharmony_ci *               start of the TTBR1 address space.
3962306a36Sopenharmony_ci * PAGE_END - the end of the linear map, where all other kernel mappings begin.
4062306a36Sopenharmony_ci * KIMAGE_VADDR - the virtual address of the start of the kernel image.
4162306a36Sopenharmony_ci * VA_BITS - the maximum number of bits for virtual addresses.
4262306a36Sopenharmony_ci */
4362306a36Sopenharmony_ci#define VA_BITS			(CONFIG_ARM64_VA_BITS)
4462306a36Sopenharmony_ci#define _PAGE_OFFSET(va)	(-(UL(1) << (va)))
4562306a36Sopenharmony_ci#define PAGE_OFFSET		(_PAGE_OFFSET(VA_BITS))
4662306a36Sopenharmony_ci#define KIMAGE_VADDR		(MODULES_END)
4762306a36Sopenharmony_ci#define MODULES_END		(MODULES_VADDR + MODULES_VSIZE)
4862306a36Sopenharmony_ci#define MODULES_VADDR		(_PAGE_END(VA_BITS_MIN))
4962306a36Sopenharmony_ci#define MODULES_VSIZE		(SZ_2G)
5062306a36Sopenharmony_ci#define VMEMMAP_START		(-(UL(1) << (VA_BITS - VMEMMAP_SHIFT)))
5162306a36Sopenharmony_ci#define VMEMMAP_END		(VMEMMAP_START + VMEMMAP_SIZE)
5262306a36Sopenharmony_ci#define PCI_IO_END		(VMEMMAP_START - SZ_8M)
5362306a36Sopenharmony_ci#define PCI_IO_START		(PCI_IO_END - PCI_IO_SIZE)
5462306a36Sopenharmony_ci#define FIXADDR_TOP		(VMEMMAP_START - SZ_32M)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#if VA_BITS > 48
5762306a36Sopenharmony_ci#define VA_BITS_MIN		(48)
5862306a36Sopenharmony_ci#else
5962306a36Sopenharmony_ci#define VA_BITS_MIN		(VA_BITS)
6062306a36Sopenharmony_ci#endif
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define _PAGE_END(va)		(-(UL(1) << ((va) - 1)))
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define KERNEL_START		_text
6562306a36Sopenharmony_ci#define KERNEL_END		_end
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/*
6862306a36Sopenharmony_ci * Generic and tag-based KASAN require 1/8th and 1/16th of the kernel virtual
6962306a36Sopenharmony_ci * address space for the shadow region respectively. They can bloat the stack
7062306a36Sopenharmony_ci * significantly, so double the (minimum) stack size when they are in use.
7162306a36Sopenharmony_ci */
7262306a36Sopenharmony_ci#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
7362306a36Sopenharmony_ci#define KASAN_SHADOW_OFFSET	_AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
7462306a36Sopenharmony_ci#define KASAN_SHADOW_END	((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) \
7562306a36Sopenharmony_ci					+ KASAN_SHADOW_OFFSET)
7662306a36Sopenharmony_ci#define PAGE_END		(KASAN_SHADOW_END - (1UL << (vabits_actual - KASAN_SHADOW_SCALE_SHIFT)))
7762306a36Sopenharmony_ci#define KASAN_THREAD_SHIFT	1
7862306a36Sopenharmony_ci#else
7962306a36Sopenharmony_ci#define KASAN_THREAD_SHIFT	0
8062306a36Sopenharmony_ci#define PAGE_END		(_PAGE_END(VA_BITS_MIN))
8162306a36Sopenharmony_ci#endif /* CONFIG_KASAN */
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci#define MIN_THREAD_SHIFT	(14 + KASAN_THREAD_SHIFT)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/*
8662306a36Sopenharmony_ci * VMAP'd stacks are allocated at page granularity, so we must ensure that such
8762306a36Sopenharmony_ci * stacks are a multiple of page size.
8862306a36Sopenharmony_ci */
8962306a36Sopenharmony_ci#if defined(CONFIG_VMAP_STACK) && (MIN_THREAD_SHIFT < PAGE_SHIFT)
9062306a36Sopenharmony_ci#define THREAD_SHIFT		PAGE_SHIFT
9162306a36Sopenharmony_ci#else
9262306a36Sopenharmony_ci#define THREAD_SHIFT		MIN_THREAD_SHIFT
9362306a36Sopenharmony_ci#endif
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci#if THREAD_SHIFT >= PAGE_SHIFT
9662306a36Sopenharmony_ci#define THREAD_SIZE_ORDER	(THREAD_SHIFT - PAGE_SHIFT)
9762306a36Sopenharmony_ci#endif
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci#define THREAD_SIZE		(UL(1) << THREAD_SHIFT)
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/*
10262306a36Sopenharmony_ci * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
10362306a36Sopenharmony_ci * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry
10462306a36Sopenharmony_ci * assembly.
10562306a36Sopenharmony_ci */
10662306a36Sopenharmony_ci#ifdef CONFIG_VMAP_STACK
10762306a36Sopenharmony_ci#define THREAD_ALIGN		(2 * THREAD_SIZE)
10862306a36Sopenharmony_ci#else
10962306a36Sopenharmony_ci#define THREAD_ALIGN		THREAD_SIZE
11062306a36Sopenharmony_ci#endif
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define IRQ_STACK_SIZE		THREAD_SIZE
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci#define OVERFLOW_STACK_SIZE	SZ_4K
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci/*
11762306a36Sopenharmony_ci * With the minimum frame size of [x29, x30], exactly half the combined
11862306a36Sopenharmony_ci * sizes of the hyp and overflow stacks is the maximum size needed to
11962306a36Sopenharmony_ci * save the unwinded stacktrace; plus an additional entry to delimit the
12062306a36Sopenharmony_ci * end.
12162306a36Sopenharmony_ci */
12262306a36Sopenharmony_ci#define NVHE_STACKTRACE_SIZE	((OVERFLOW_STACK_SIZE + PAGE_SIZE) / 2 + sizeof(long))
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci/*
12562306a36Sopenharmony_ci * Alignment of kernel segments (e.g. .text, .data).
12662306a36Sopenharmony_ci *
12762306a36Sopenharmony_ci *  4 KB granule:  16 level 3 entries, with contiguous bit
12862306a36Sopenharmony_ci * 16 KB granule:   4 level 3 entries, without contiguous bit
12962306a36Sopenharmony_ci * 64 KB granule:   1 level 3 entry
13062306a36Sopenharmony_ci */
13162306a36Sopenharmony_ci#define SEGMENT_ALIGN		SZ_64K
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci/*
13462306a36Sopenharmony_ci * Memory types available.
13562306a36Sopenharmony_ci *
13662306a36Sopenharmony_ci * IMPORTANT: MT_NORMAL must be index 0 since vm_get_page_prot() may 'or' in
13762306a36Sopenharmony_ci *	      the MT_NORMAL_TAGGED memory type for PROT_MTE mappings. Note
13862306a36Sopenharmony_ci *	      that protection_map[] only contains MT_NORMAL attributes.
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_ci#define MT_NORMAL		0
14162306a36Sopenharmony_ci#define MT_NORMAL_TAGGED	1
14262306a36Sopenharmony_ci#define MT_NORMAL_NC		2
14362306a36Sopenharmony_ci#define MT_DEVICE_nGnRnE	3
14462306a36Sopenharmony_ci#define MT_DEVICE_nGnRE		4
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci/*
14762306a36Sopenharmony_ci * Memory types for Stage-2 translation
14862306a36Sopenharmony_ci */
14962306a36Sopenharmony_ci#define MT_S2_NORMAL		0xf
15062306a36Sopenharmony_ci#define MT_S2_DEVICE_nGnRE	0x1
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci/*
15362306a36Sopenharmony_ci * Memory types for Stage-2 translation when ID_AA64MMFR2_EL1.FWB is 0001
15462306a36Sopenharmony_ci * Stage-2 enforces Normal-WB and Device-nGnRE
15562306a36Sopenharmony_ci */
15662306a36Sopenharmony_ci#define MT_S2_FWB_NORMAL	6
15762306a36Sopenharmony_ci#define MT_S2_FWB_DEVICE_nGnRE	1
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci#ifdef CONFIG_ARM64_4K_PAGES
16062306a36Sopenharmony_ci#define IOREMAP_MAX_ORDER	(PUD_SHIFT)
16162306a36Sopenharmony_ci#else
16262306a36Sopenharmony_ci#define IOREMAP_MAX_ORDER	(PMD_SHIFT)
16362306a36Sopenharmony_ci#endif
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/*
16662306a36Sopenharmony_ci *  Open-coded (swapper_pg_dir - reserved_pg_dir) as this cannot be calculated
16762306a36Sopenharmony_ci *  until link time.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_ci#define RESERVED_SWAPPER_OFFSET	(PAGE_SIZE)
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/*
17262306a36Sopenharmony_ci *  Open-coded (swapper_pg_dir - tramp_pg_dir) as this cannot be calculated
17362306a36Sopenharmony_ci *  until link time.
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_ci#define TRAMP_SWAPPER_OFFSET	(2 * PAGE_SIZE)
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#ifndef __ASSEMBLY__
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci#include <linux/bitops.h>
18062306a36Sopenharmony_ci#include <linux/compiler.h>
18162306a36Sopenharmony_ci#include <linux/mmdebug.h>
18262306a36Sopenharmony_ci#include <linux/types.h>
18362306a36Sopenharmony_ci#include <asm/boot.h>
18462306a36Sopenharmony_ci#include <asm/bug.h>
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci#if VA_BITS > 48
18762306a36Sopenharmony_ciextern u64			vabits_actual;
18862306a36Sopenharmony_ci#else
18962306a36Sopenharmony_ci#define vabits_actual		((u64)VA_BITS)
19062306a36Sopenharmony_ci#endif
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciextern s64			memstart_addr;
19362306a36Sopenharmony_ci/* PHYS_OFFSET - the physical address of the start of memory. */
19462306a36Sopenharmony_ci#define PHYS_OFFSET		({ VM_BUG_ON(memstart_addr & 1); memstart_addr; })
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci/* the virtual base of the kernel image */
19762306a36Sopenharmony_ciextern u64			kimage_vaddr;
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci/* the offset between the kernel virtual and physical mappings */
20062306a36Sopenharmony_ciextern u64			kimage_voffset;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_cistatic inline unsigned long kaslr_offset(void)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	return kimage_vaddr - KIMAGE_VADDR;
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci#ifdef CONFIG_RANDOMIZE_BASE
20862306a36Sopenharmony_civoid kaslr_init(void);
20962306a36Sopenharmony_cistatic inline bool kaslr_enabled(void)
21062306a36Sopenharmony_ci{
21162306a36Sopenharmony_ci	extern bool __kaslr_is_enabled;
21262306a36Sopenharmony_ci	return __kaslr_is_enabled;
21362306a36Sopenharmony_ci}
21462306a36Sopenharmony_ci#else
21562306a36Sopenharmony_cistatic inline void kaslr_init(void) { }
21662306a36Sopenharmony_cistatic inline bool kaslr_enabled(void) { return false; }
21762306a36Sopenharmony_ci#endif
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/*
22062306a36Sopenharmony_ci * Allow all memory at the discovery stage. We will clip it later.
22162306a36Sopenharmony_ci */
22262306a36Sopenharmony_ci#define MIN_MEMBLOCK_ADDR	0
22362306a36Sopenharmony_ci#define MAX_MEMBLOCK_ADDR	U64_MAX
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci/*
22662306a36Sopenharmony_ci * PFNs are used to describe any physical page; this means
22762306a36Sopenharmony_ci * PFN 0 == physical address 0.
22862306a36Sopenharmony_ci *
22962306a36Sopenharmony_ci * This is the PFN of the first RAM page in the kernel
23062306a36Sopenharmony_ci * direct-mapped view.  We assume this is the first page
23162306a36Sopenharmony_ci * of RAM in the mem_map as well.
23262306a36Sopenharmony_ci */
23362306a36Sopenharmony_ci#define PHYS_PFN_OFFSET	(PHYS_OFFSET >> PAGE_SHIFT)
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci/*
23662306a36Sopenharmony_ci * When dealing with data aborts, watchpoints, or instruction traps we may end
23762306a36Sopenharmony_ci * up with a tagged userland pointer. Clear the tag to get a sane pointer to
23862306a36Sopenharmony_ci * pass on to access_ok(), for instance.
23962306a36Sopenharmony_ci */
24062306a36Sopenharmony_ci#define __untagged_addr(addr)	\
24162306a36Sopenharmony_ci	((__force __typeof__(addr))sign_extend64((__force u64)(addr), 55))
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci#define untagged_addr(addr)	({					\
24462306a36Sopenharmony_ci	u64 __addr = (__force u64)(addr);					\
24562306a36Sopenharmony_ci	__addr &= __untagged_addr(__addr);				\
24662306a36Sopenharmony_ci	(__force __typeof__(addr))__addr;				\
24762306a36Sopenharmony_ci})
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
25062306a36Sopenharmony_ci#define __tag_shifted(tag)	((u64)(tag) << 56)
25162306a36Sopenharmony_ci#define __tag_reset(addr)	__untagged_addr(addr)
25262306a36Sopenharmony_ci#define __tag_get(addr)		(__u8)((u64)(addr) >> 56)
25362306a36Sopenharmony_ci#else
25462306a36Sopenharmony_ci#define __tag_shifted(tag)	0UL
25562306a36Sopenharmony_ci#define __tag_reset(addr)	(addr)
25662306a36Sopenharmony_ci#define __tag_get(addr)		0
25762306a36Sopenharmony_ci#endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic inline const void *__tag_set(const void *addr, u8 tag)
26062306a36Sopenharmony_ci{
26162306a36Sopenharmony_ci	u64 __addr = (u64)addr & ~__tag_shifted(0xff);
26262306a36Sopenharmony_ci	return (const void *)(__addr | __tag_shifted(tag));
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci#ifdef CONFIG_KASAN_HW_TAGS
26662306a36Sopenharmony_ci#define arch_enable_tag_checks_sync()		mte_enable_kernel_sync()
26762306a36Sopenharmony_ci#define arch_enable_tag_checks_async()		mte_enable_kernel_async()
26862306a36Sopenharmony_ci#define arch_enable_tag_checks_asymm()		mte_enable_kernel_asymm()
26962306a36Sopenharmony_ci#define arch_suppress_tag_checks_start()	mte_enable_tco()
27062306a36Sopenharmony_ci#define arch_suppress_tag_checks_stop()		mte_disable_tco()
27162306a36Sopenharmony_ci#define arch_force_async_tag_fault()		mte_check_tfsr_exit()
27262306a36Sopenharmony_ci#define arch_get_random_tag()			mte_get_random_tag()
27362306a36Sopenharmony_ci#define arch_get_mem_tag(addr)			mte_get_mem_tag(addr)
27462306a36Sopenharmony_ci#define arch_set_mem_tag_range(addr, size, tag, init)	\
27562306a36Sopenharmony_ci			mte_set_mem_tag_range((addr), (size), (tag), (init))
27662306a36Sopenharmony_ci#endif /* CONFIG_KASAN_HW_TAGS */
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci/*
27962306a36Sopenharmony_ci * Physical vs virtual RAM address space conversion.  These are
28062306a36Sopenharmony_ci * private definitions which should NOT be used outside memory.h
28162306a36Sopenharmony_ci * files.  Use virt_to_phys/phys_to_virt/__pa/__va instead.
28262306a36Sopenharmony_ci */
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci/*
28662306a36Sopenharmony_ci * Check whether an arbitrary address is within the linear map, which
28762306a36Sopenharmony_ci * lives in the [PAGE_OFFSET, PAGE_END) interval at the bottom of the
28862306a36Sopenharmony_ci * kernel's TTBR1 address range.
28962306a36Sopenharmony_ci */
29062306a36Sopenharmony_ci#define __is_lm_address(addr)	(((u64)(addr) - PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci#define __lm_to_phys(addr)	(((addr) - PAGE_OFFSET) + PHYS_OFFSET)
29362306a36Sopenharmony_ci#define __kimg_to_phys(addr)	((addr) - kimage_voffset)
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci#define __virt_to_phys_nodebug(x) ({					\
29662306a36Sopenharmony_ci	phys_addr_t __x = (phys_addr_t)(__tag_reset(x));		\
29762306a36Sopenharmony_ci	__is_lm_address(__x) ? __lm_to_phys(__x) : __kimg_to_phys(__x);	\
29862306a36Sopenharmony_ci})
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci#define __pa_symbol_nodebug(x)	__kimg_to_phys((phys_addr_t)(x))
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_VIRTUAL
30362306a36Sopenharmony_ciextern phys_addr_t __virt_to_phys(unsigned long x);
30462306a36Sopenharmony_ciextern phys_addr_t __phys_addr_symbol(unsigned long x);
30562306a36Sopenharmony_ci#else
30662306a36Sopenharmony_ci#define __virt_to_phys(x)	__virt_to_phys_nodebug(x)
30762306a36Sopenharmony_ci#define __phys_addr_symbol(x)	__pa_symbol_nodebug(x)
30862306a36Sopenharmony_ci#endif /* CONFIG_DEBUG_VIRTUAL */
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci#define __phys_to_virt(x)	((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)
31162306a36Sopenharmony_ci#define __phys_to_kimg(x)	((unsigned long)((x) + kimage_voffset))
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci/*
31462306a36Sopenharmony_ci * Convert a page to/from a physical address
31562306a36Sopenharmony_ci */
31662306a36Sopenharmony_ci#define page_to_phys(page)	(__pfn_to_phys(page_to_pfn(page)))
31762306a36Sopenharmony_ci#define phys_to_page(phys)	(pfn_to_page(__phys_to_pfn(phys)))
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci/*
32062306a36Sopenharmony_ci * Note: Drivers should NOT use these.  They are the wrong
32162306a36Sopenharmony_ci * translation for translating DMA addresses.  Use the driver
32262306a36Sopenharmony_ci * DMA support - see dma-mapping.h.
32362306a36Sopenharmony_ci */
32462306a36Sopenharmony_ci#define virt_to_phys virt_to_phys
32562306a36Sopenharmony_cistatic inline phys_addr_t virt_to_phys(const volatile void *x)
32662306a36Sopenharmony_ci{
32762306a36Sopenharmony_ci	return __virt_to_phys((unsigned long)(x));
32862306a36Sopenharmony_ci}
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci#define phys_to_virt phys_to_virt
33162306a36Sopenharmony_cistatic inline void *phys_to_virt(phys_addr_t x)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	return (void *)(__phys_to_virt(x));
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci/* Needed already here for resolving __phys_to_pfn() in virt_to_pfn() */
33762306a36Sopenharmony_ci#include <asm-generic/memory_model.h>
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_cistatic inline unsigned long virt_to_pfn(const void *kaddr)
34062306a36Sopenharmony_ci{
34162306a36Sopenharmony_ci	return __phys_to_pfn(virt_to_phys(kaddr));
34262306a36Sopenharmony_ci}
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci/*
34562306a36Sopenharmony_ci * Drivers should NOT use these either.
34662306a36Sopenharmony_ci */
34762306a36Sopenharmony_ci#define __pa(x)			__virt_to_phys((unsigned long)(x))
34862306a36Sopenharmony_ci#define __pa_symbol(x)		__phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
34962306a36Sopenharmony_ci#define __pa_nodebug(x)		__virt_to_phys_nodebug((unsigned long)(x))
35062306a36Sopenharmony_ci#define __va(x)			((void *)__phys_to_virt((phys_addr_t)(x)))
35162306a36Sopenharmony_ci#define pfn_to_kaddr(pfn)	__va((pfn) << PAGE_SHIFT)
35262306a36Sopenharmony_ci#define sym_to_pfn(x)		__phys_to_pfn(__pa_symbol(x))
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci/*
35562306a36Sopenharmony_ci *  virt_to_page(x)	convert a _valid_ virtual address to struct page *
35662306a36Sopenharmony_ci *  virt_addr_valid(x)	indicates whether a virtual address is valid
35762306a36Sopenharmony_ci */
35862306a36Sopenharmony_ci#define ARCH_PFN_OFFSET		((unsigned long)PHYS_PFN_OFFSET)
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_VIRTUAL)
36162306a36Sopenharmony_ci#define page_to_virt(x)	({						\
36262306a36Sopenharmony_ci	__typeof__(x) __page = x;					\
36362306a36Sopenharmony_ci	void *__addr = __va(page_to_phys(__page));			\
36462306a36Sopenharmony_ci	(void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
36562306a36Sopenharmony_ci})
36662306a36Sopenharmony_ci#define virt_to_page(x)		pfn_to_page(virt_to_pfn(x))
36762306a36Sopenharmony_ci#else
36862306a36Sopenharmony_ci#define page_to_virt(x)	({						\
36962306a36Sopenharmony_ci	__typeof__(x) __page = x;					\
37062306a36Sopenharmony_ci	u64 __idx = ((u64)__page - VMEMMAP_START) / sizeof(struct page);\
37162306a36Sopenharmony_ci	u64 __addr = PAGE_OFFSET + (__idx * PAGE_SIZE);			\
37262306a36Sopenharmony_ci	(void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
37362306a36Sopenharmony_ci})
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci#define virt_to_page(x)	({						\
37662306a36Sopenharmony_ci	u64 __idx = (__tag_reset((u64)x) - PAGE_OFFSET) / PAGE_SIZE;	\
37762306a36Sopenharmony_ci	u64 __addr = VMEMMAP_START + (__idx * sizeof(struct page));	\
37862306a36Sopenharmony_ci	(struct page *)__addr;						\
37962306a36Sopenharmony_ci})
38062306a36Sopenharmony_ci#endif /* CONFIG_DEBUG_VIRTUAL */
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci#define virt_addr_valid(addr)	({					\
38362306a36Sopenharmony_ci	__typeof__(addr) __addr = __tag_reset(addr);			\
38462306a36Sopenharmony_ci	__is_lm_address(__addr) && pfn_is_map_memory(virt_to_pfn(__addr));	\
38562306a36Sopenharmony_ci})
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_civoid dump_mem_limit(void);
38862306a36Sopenharmony_ci#endif /* !ASSEMBLY */
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci/*
39162306a36Sopenharmony_ci * Given that the GIC architecture permits ITS implementations that can only be
39262306a36Sopenharmony_ci * configured with a LPI table address once, GICv3 systems with many CPUs may
39362306a36Sopenharmony_ci * end up reserving a lot of different regions after a kexec for their LPI
39462306a36Sopenharmony_ci * tables (one per CPU), as we are forced to reuse the same memory after kexec
39562306a36Sopenharmony_ci * (and thus reserve it persistently with EFI beforehand)
39662306a36Sopenharmony_ci */
39762306a36Sopenharmony_ci#if defined(CONFIG_EFI) && defined(CONFIG_ARM_GIC_V3_ITS)
39862306a36Sopenharmony_ci# define INIT_MEMBLOCK_RESERVED_REGIONS	(INIT_MEMBLOCK_REGIONS + NR_CPUS + 1)
39962306a36Sopenharmony_ci#endif
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci/*
40262306a36Sopenharmony_ci * memory regions which marked with flag MEMBLOCK_NOMAP(for example, the memory
40362306a36Sopenharmony_ci * of the EFI_UNUSABLE_MEMORY type) may divide a continuous memory block into
40462306a36Sopenharmony_ci * multiple parts. As a result, the number of memory regions is large.
40562306a36Sopenharmony_ci */
40662306a36Sopenharmony_ci#ifdef CONFIG_EFI
40762306a36Sopenharmony_ci#define INIT_MEMBLOCK_MEMORY_REGIONS	(INIT_MEMBLOCK_REGIONS * 8)
40862306a36Sopenharmony_ci#endif
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci#include <asm-generic/memory_model.h>
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci#endif /* __ASM_MEMORY_H */
413