xref: /openbmc/linux/arch/arm/include/asm/memory.h (revision 7d281b62)
14baa9922SRussell King /*
24baa9922SRussell King  *  arch/arm/include/asm/memory.h
34baa9922SRussell King  *
44baa9922SRussell King  *  Copyright (C) 2000-2002 Russell King
54baa9922SRussell King  *  modification for nommu, Hyok S. Choi, 2004
64baa9922SRussell King  *
74baa9922SRussell King  * This program is free software; you can redistribute it and/or modify
84baa9922SRussell King  * it under the terms of the GNU General Public License version 2 as
94baa9922SRussell King  * published by the Free Software Foundation.
104baa9922SRussell King  *
114baa9922SRussell King  *  Note: this file should not be included by non-asm/.h files
124baa9922SRussell King  */
134baa9922SRussell King #ifndef __ASM_ARM_MEMORY_H
144baa9922SRussell King #define __ASM_ARM_MEMORY_H
154baa9922SRussell King 
168d5796d2SLennert Buytenhek #include <linux/compiler.h>
178d5796d2SLennert Buytenhek #include <linux/const.h>
183a6b1676SWill Deacon #include <linux/types.h>
19158e8bfeSAlessandro Rubini #include <linux/sizes.h>
208d5796d2SLennert Buytenhek 
210cdc8b92SNicolas Pitre #ifdef CONFIG_NEED_MACH_MEMORY_H
221b9f95f8SNicolas Pitre #include <mach/memory.h>
231b9f95f8SNicolas Pitre #endif
241b9f95f8SNicolas Pitre 
254baa9922SRussell King /*
264baa9922SRussell King  * Allow for constants defined here to be used from assembly code
274baa9922SRussell King  * by prepending the UL suffix only with actual C code compilation.
284baa9922SRussell King  */
298d5796d2SLennert Buytenhek #define UL(x) _AC(x, UL)
304baa9922SRussell King 
31006fa259SRussell King /* PAGE_OFFSET - the virtual address of the start of the kernel image */
32006fa259SRussell King #define PAGE_OFFSET		UL(CONFIG_PAGE_OFFSET)
33006fa259SRussell King 
344baa9922SRussell King #ifdef CONFIG_MMU
354baa9922SRussell King 
364baa9922SRussell King /*
374baa9922SRussell King  * TASK_SIZE - the maximum size of a user space task.
384baa9922SRussell King  * TASK_UNMAPPED_BASE - the lower boundary of the mmap VM area
394baa9922SRussell King  */
405d1c20bcSWill Deacon #define TASK_SIZE		(UL(CONFIG_PAGE_OFFSET) - UL(SZ_16M))
4179d1f5c9SWill Deacon #define TASK_UNMAPPED_BASE	ALIGN(TASK_SIZE / 3, SZ_16M)
424baa9922SRussell King 
434baa9922SRussell King /*
444baa9922SRussell King  * The maximum size of a 26-bit user space task.
454baa9922SRussell King  */
465d1c20bcSWill Deacon #define TASK_SIZE_26		(UL(1) << 26)
474baa9922SRussell King 
484baa9922SRussell King /*
494baa9922SRussell King  * The module space lives between the addresses given by TASK_SIZE
504baa9922SRussell King  * and PAGE_OFFSET - it must be within 32MB of the kernel text.
514baa9922SRussell King  */
52adca6dc2SCatalin Marinas #ifndef CONFIG_THUMB2_KERNEL
535d1c20bcSWill Deacon #define MODULES_VADDR		(PAGE_OFFSET - SZ_16M)
54adca6dc2SCatalin Marinas #else
55adca6dc2SCatalin Marinas /* smaller range for Thumb-2 symbols relocation (2^24)*/
565d1c20bcSWill Deacon #define MODULES_VADDR		(PAGE_OFFSET - SZ_8M)
57adca6dc2SCatalin Marinas #endif
58adca6dc2SCatalin Marinas 
59ab4f2ee1SRussell King #if TASK_SIZE > MODULES_VADDR
604baa9922SRussell King #error Top of user space clashes with start of module space
614baa9922SRussell King #endif
624baa9922SRussell King 
634baa9922SRussell King /*
64d73cd428SNicolas Pitre  * The highmem pkmap virtual space shares the end of the module area.
65d73cd428SNicolas Pitre  */
66d73cd428SNicolas Pitre #ifdef CONFIG_HIGHMEM
67d73cd428SNicolas Pitre #define MODULES_END		(PAGE_OFFSET - PMD_SIZE)
68d73cd428SNicolas Pitre #else
69d73cd428SNicolas Pitre #define MODULES_END		(PAGE_OFFSET)
70d73cd428SNicolas Pitre #endif
71d73cd428SNicolas Pitre 
72d73cd428SNicolas Pitre /*
734baa9922SRussell King  * The XIP kernel gets mapped at the bottom of the module vm area.
744baa9922SRussell King  * Since we use sections to map it, this macro replaces the physical address
754baa9922SRussell King  * with its virtual address while keeping offset from the base section.
764baa9922SRussell King  */
77ab4f2ee1SRussell King #define XIP_VIRT_ADDR(physaddr)  (MODULES_VADDR + ((physaddr) & 0x000fffff))
784baa9922SRussell King 
79803e3dbcSSergey Dyasly #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
804baa9922SRussell King /*
814baa9922SRussell King  * Allow 16MB-aligned ioremap pages
824baa9922SRussell King  */
834baa9922SRussell King #define IOREMAP_MAX_ORDER	24
84803e3dbcSSergey Dyasly #endif
854baa9922SRussell King 
864baa9922SRussell King #else /* CONFIG_MMU */
874baa9922SRussell King 
884baa9922SRussell King /*
894baa9922SRussell King  * The limitation of user task size can grow up to the end of free ram region.
904baa9922SRussell King  * It is difficult to define and perhaps will never meet the original meaning
914baa9922SRussell King  * of this define that was meant to.
924baa9922SRussell King  * Fortunately, there is no reference for this in noMMU mode, for now.
934baa9922SRussell King  */
9403eca200SUwe Kleine-König #define TASK_SIZE		UL(0xffffffff)
954baa9922SRussell King 
964baa9922SRussell King #ifndef TASK_UNMAPPED_BASE
974baa9922SRussell King #define TASK_UNMAPPED_BASE	UL(0x00000000)
984baa9922SRussell King #endif
994baa9922SRussell King 
1004baa9922SRussell King #ifndef END_MEM
101c931b4f6SFenkart/Bostandzhyan #define END_MEM     		(UL(CONFIG_DRAM_BASE) + CONFIG_DRAM_SIZE)
1024baa9922SRussell King #endif
1034baa9922SRussell King 
1044baa9922SRussell King /*
1054baa9922SRussell King  * The module can be at any place in ram in nommu mode.
1064baa9922SRussell King  */
107ab4f2ee1SRussell King #define MODULES_END		(END_MEM)
108b713aa0bSRussell King #define MODULES_VADDR		PAGE_OFFSET
1094baa9922SRussell King 
11038b4205aSUwe Kleine-König #define XIP_VIRT_ADDR(physaddr)  (physaddr)
11138b4205aSUwe Kleine-König 
1124baa9922SRussell King #endif /* !CONFIG_MMU */
1134baa9922SRussell King 
1144baa9922SRussell King /*
1151dbd30e9SLinus Walleij  * We fix the TCM memories max 32 KiB ITCM resp DTCM at these
1161dbd30e9SLinus Walleij  * locations
1171dbd30e9SLinus Walleij  */
1181dbd30e9SLinus Walleij #ifdef CONFIG_HAVE_TCM
1191dbd30e9SLinus Walleij #define ITCM_OFFSET	UL(0xfffe0000)
1201dbd30e9SLinus Walleij #define DTCM_OFFSET	UL(0xfffe8000)
1211dbd30e9SLinus Walleij #endif
1221dbd30e9SLinus Walleij 
1231dbd30e9SLinus Walleij /*
124719301ffSRussell King  * Convert a page to/from a physical address
125719301ffSRussell King  */
126719301ffSRussell King #define page_to_phys(page)	(__pfn_to_phys(page_to_pfn(page)))
127719301ffSRussell King #define phys_to_page(phys)	(pfn_to_page(__phys_to_pfn(phys)))
128719301ffSRussell King 
1294756dcbfSCyril Chemparathy /*
130b713aa0bSRussell King  * PLAT_PHYS_OFFSET is the offset (from zero) of the start of physical
131c6f54a9bSUwe Kleine-König  * memory.  This is used for XIP and NoMMU kernels, and on platforms that don't
132c6f54a9bSUwe Kleine-König  * have CONFIG_ARM_PATCH_PHYS_VIRT. Assembly code must always use
133b713aa0bSRussell King  * PLAT_PHYS_OFFSET and not PHYS_OFFSET.
134b713aa0bSRussell King  */
135b713aa0bSRussell King #define PLAT_PHYS_OFFSET	UL(CONFIG_PHYS_OFFSET)
136b713aa0bSRussell King 
137d7811455SNicolas Pitre #ifdef CONFIG_XIP_KERNEL
1388ff97fa3SRussell King /*
1398ff97fa3SRussell King  * When referencing data in RAM from the XIP region in a relative manner
1408ff97fa3SRussell King  * with the MMU off, we need the relative offset between the two physical
1418ff97fa3SRussell King  * addresses.  The macro below achieves this, which is:
1428ff97fa3SRussell King  *    __pa(v_data) - __xip_pa(v_text)
1438ff97fa3SRussell King  */
1448ff97fa3SRussell King #define PHYS_RELATIVE(v_data, v_text) \
1458ff97fa3SRussell King 	(((v_data) - PAGE_OFFSET + PLAT_PHYS_OFFSET) - \
1468ff97fa3SRussell King 	 ((v_text) - XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR) + \
1478ff97fa3SRussell King           CONFIG_XIP_PHYS_ADDR))
148d7811455SNicolas Pitre #else
1498ff97fa3SRussell King #define PHYS_RELATIVE(v_data, v_text) ((v_data) - (v_text))
150d7811455SNicolas Pitre #endif
151d7811455SNicolas Pitre 
1524baa9922SRussell King #ifndef __ASSEMBLY__
1534baa9922SRussell King 
1544baa9922SRussell King /*
155dc21af99SRussell King  * Physical vs virtual RAM address space conversion.  These are
156dc21af99SRussell King  * private definitions which should NOT be used outside memory.h
157dc21af99SRussell King  * files.  Use virt_to_phys/phys_to_virt/__pa/__va instead.
158e26a9e00SRussell King  *
159e26a9e00SRussell King  * PFNs are used to describe any physical page; this means
160e26a9e00SRussell King  * PFN 0 == physical address 0.
161dc21af99SRussell King  */
162e26a9e00SRussell King 
1637d281b62SNicolas Pitre #if defined(CONFIG_ARM_PATCH_PHYS_VIRT)
164dc21af99SRussell King 
165cada3c08SRussell King /*
166cada3c08SRussell King  * Constants used to force the right instruction encodings and shifts
167cada3c08SRussell King  * so that all we need to do is modify the 8-bit constant field.
168cada3c08SRussell King  */
169cada3c08SRussell King #define __PV_BITS_31_24	0x81000000
170f52bb722SSricharan R #define __PV_BITS_7_0	0x81
171cada3c08SRussell King 
172e26a9e00SRussell King extern unsigned long __pv_phys_pfn_offset;
173f52bb722SSricharan R extern u64 __pv_offset;
174f52bb722SSricharan R extern void fixup_pv_table(const void *, unsigned long);
175f52bb722SSricharan R extern const void *__pv_table_begin, *__pv_table_end;
176f52bb722SSricharan R 
177e26a9e00SRussell King #define PHYS_OFFSET	((phys_addr_t)__pv_phys_pfn_offset << PAGE_SHIFT)
178e26a9e00SRussell King #define PHYS_PFN_OFFSET	(__pv_phys_pfn_offset)
179e26a9e00SRussell King 
180cada3c08SRussell King #define __pv_stub(from,to,instr,type)			\
181dc21af99SRussell King 	__asm__("@ __pv_stub\n"				\
182dc21af99SRussell King 	"1:	" instr "	%0, %1, %2\n"		\
183dc21af99SRussell King 	"	.pushsection .pv_table,\"a\"\n"		\
184dc21af99SRussell King 	"	.long	1b\n"				\
185dc21af99SRussell King 	"	.popsection\n"				\
186dc21af99SRussell King 	: "=r" (to)					\
187cada3c08SRussell King 	: "r" (from), "I" (type))
188dc21af99SRussell King 
189f52bb722SSricharan R #define __pv_stub_mov_hi(t)				\
190f52bb722SSricharan R 	__asm__ volatile("@ __pv_stub_mov\n"		\
191f52bb722SSricharan R 	"1:	mov	%R0, %1\n"			\
192f52bb722SSricharan R 	"	.pushsection .pv_table,\"a\"\n"		\
193f52bb722SSricharan R 	"	.long	1b\n"				\
194f52bb722SSricharan R 	"	.popsection\n"				\
195f52bb722SSricharan R 	: "=r" (t)					\
196f52bb722SSricharan R 	: "I" (__PV_BITS_7_0))
197f52bb722SSricharan R 
198f52bb722SSricharan R #define __pv_add_carry_stub(x, y)			\
199f52bb722SSricharan R 	__asm__ volatile("@ __pv_add_carry_stub\n"	\
200f52bb722SSricharan R 	"1:	adds	%Q0, %1, %2\n"			\
201f52bb722SSricharan R 	"	adc	%R0, %R0, #0\n"			\
202f52bb722SSricharan R 	"	.pushsection .pv_table,\"a\"\n"		\
203f52bb722SSricharan R 	"	.long	1b\n"				\
204f52bb722SSricharan R 	"	.popsection\n"				\
205f52bb722SSricharan R 	: "+r" (y)					\
206f52bb722SSricharan R 	: "r" (x), "I" (__PV_BITS_31_24)		\
207f52bb722SSricharan R 	: "cc")
208f52bb722SSricharan R 
209ca5a45c0SSantosh Shilimkar static inline phys_addr_t __virt_to_phys(unsigned long x)
210dc21af99SRussell King {
211f52bb722SSricharan R 	phys_addr_t t;
212f52bb722SSricharan R 
213f52bb722SSricharan R 	if (sizeof(phys_addr_t) == 4) {
214cada3c08SRussell King 		__pv_stub(x, t, "add", __PV_BITS_31_24);
215f52bb722SSricharan R 	} else {
216f52bb722SSricharan R 		__pv_stub_mov_hi(t);
217f52bb722SSricharan R 		__pv_add_carry_stub(x, t);
218f52bb722SSricharan R 	}
219dc21af99SRussell King 	return t;
220dc21af99SRussell King }
221dc21af99SRussell King 
222ca5a45c0SSantosh Shilimkar static inline unsigned long __phys_to_virt(phys_addr_t x)
223dc21af99SRussell King {
224dc21af99SRussell King 	unsigned long t;
225139cc2baSVictor Kamensky 
226139cc2baSVictor Kamensky 	/*
227139cc2baSVictor Kamensky 	 * 'unsigned long' cast discard upper word when
228139cc2baSVictor Kamensky 	 * phys_addr_t is 64 bit, and makes sure that inline
229139cc2baSVictor Kamensky 	 * assembler expression receives 32 bit argument
230139cc2baSVictor Kamensky 	 * in place where 'r' 32 bit operand is expected.
231139cc2baSVictor Kamensky 	 */
232139cc2baSVictor Kamensky 	__pv_stub((unsigned long) x, t, "sub", __PV_BITS_31_24);
233dc21af99SRussell King 	return t;
234dc21af99SRussell King }
235ca5a45c0SSantosh Shilimkar 
236dc21af99SRussell King #else
237ca5a45c0SSantosh Shilimkar 
238b713aa0bSRussell King #define PHYS_OFFSET	PLAT_PHYS_OFFSET
239e26a9e00SRussell King #define PHYS_PFN_OFFSET	((unsigned long)(PHYS_OFFSET >> PAGE_SHIFT))
240b713aa0bSRussell King 
241ca5a45c0SSantosh Shilimkar static inline phys_addr_t __virt_to_phys(unsigned long x)
242ca5a45c0SSantosh Shilimkar {
243ca5a45c0SSantosh Shilimkar 	return (phys_addr_t)x - PAGE_OFFSET + PHYS_OFFSET;
244ca5a45c0SSantosh Shilimkar }
245ca5a45c0SSantosh Shilimkar 
246ca5a45c0SSantosh Shilimkar static inline unsigned long __phys_to_virt(phys_addr_t x)
247ca5a45c0SSantosh Shilimkar {
248ca5a45c0SSantosh Shilimkar 	return x - PHYS_OFFSET + PAGE_OFFSET;
249ca5a45c0SSantosh Shilimkar }
250ca5a45c0SSantosh Shilimkar 
2517d281b62SNicolas Pitre #endif
2527d281b62SNicolas Pitre 
253e26a9e00SRussell King #define virt_to_pfn(kaddr) \
254e26a9e00SRussell King 	((((unsigned long)(kaddr) - PAGE_OFFSET) >> PAGE_SHIFT) + \
255e26a9e00SRussell King 	 PHYS_PFN_OFFSET)
256b4ad5155SStephen Boyd 
2574baa9922SRussell King /*
2584baa9922SRussell King  * These are *only* valid on the kernel direct mapped RAM memory.
2594baa9922SRussell King  * Note: Drivers should NOT use these.  They are the wrong
2604baa9922SRussell King  * translation for translating DMA addresses.  Use the driver
2614baa9922SRussell King  * DMA support - see dma-mapping.h.
2624baa9922SRussell King  */
26384c4d3a6SThierry Reding #define virt_to_phys virt_to_phys
2643a6b1676SWill Deacon static inline phys_addr_t virt_to_phys(const volatile void *x)
2654baa9922SRussell King {
2664baa9922SRussell King 	return __virt_to_phys((unsigned long)(x));
2674baa9922SRussell King }
2684baa9922SRussell King 
26984c4d3a6SThierry Reding #define phys_to_virt phys_to_virt
2703a6b1676SWill Deacon static inline void *phys_to_virt(phys_addr_t x)
2714baa9922SRussell King {
272ca5a45c0SSantosh Shilimkar 	return (void *)__phys_to_virt(x);
2734baa9922SRussell King }
2744baa9922SRussell King 
2754baa9922SRussell King /*
2764baa9922SRussell King  * Drivers should NOT use these either.
2774baa9922SRussell King  */
2784baa9922SRussell King #define __pa(x)			__virt_to_phys((unsigned long)(x))
279ca5a45c0SSantosh Shilimkar #define __va(x)			((void *)__phys_to_virt((phys_addr_t)(x)))
280e4886664SVitaly Andrianov #define pfn_to_kaddr(pfn)	__va((phys_addr_t)(pfn) << PAGE_SHIFT)
2814baa9922SRussell King 
282981b6714SRussell King extern long long arch_phys_to_idmap_offset;
2835e4432d3SRussell King 
2844baa9922SRussell King /*
285981b6714SRussell King  * These are for systems that have a hardware interconnect supported alias
286981b6714SRussell King  * of physical memory for idmap purposes.  Most cases should leave these
28728410293SRussell King  * untouched.  Note: this can only return addresses less than 4GiB.
2884dc9a817SSantosh Shilimkar  */
28907a7056cSRussell King static inline bool arm_has_idmap_alias(void)
29007a7056cSRussell King {
29107a7056cSRussell King 	return IS_ENABLED(CONFIG_MMU) && arch_phys_to_idmap_offset != 0;
29207a7056cSRussell King }
29307a7056cSRussell King 
294981b6714SRussell King #define IDMAP_INVALID_ADDR ((u32)~0)
295981b6714SRussell King 
296981b6714SRussell King static inline unsigned long phys_to_idmap(phys_addr_t addr)
297981b6714SRussell King {
298981b6714SRussell King 	if (IS_ENABLED(CONFIG_MMU) && arch_phys_to_idmap_offset) {
299981b6714SRussell King 		addr += arch_phys_to_idmap_offset;
300981b6714SRussell King 		if (addr > (u32)~0)
301981b6714SRussell King 			addr = IDMAP_INVALID_ADDR;
302981b6714SRussell King 	}
303981b6714SRussell King 	return addr;
304981b6714SRussell King }
305981b6714SRussell King 
306981b6714SRussell King static inline phys_addr_t idmap_to_phys(unsigned long idmap)
307981b6714SRussell King {
308981b6714SRussell King 	phys_addr_t addr = idmap;
309981b6714SRussell King 
310981b6714SRussell King 	if (IS_ENABLED(CONFIG_MMU) && arch_phys_to_idmap_offset)
311981b6714SRussell King 		addr -= arch_phys_to_idmap_offset;
312981b6714SRussell King 
313981b6714SRussell King 	return addr;
314981b6714SRussell King }
315981b6714SRussell King 
31628410293SRussell King static inline unsigned long __virt_to_idmap(unsigned long x)
3174dc9a817SSantosh Shilimkar {
318981b6714SRussell King 	return phys_to_idmap(__virt_to_phys(x));
3194dc9a817SSantosh Shilimkar }
3204dc9a817SSantosh Shilimkar 
3214dc9a817SSantosh Shilimkar #define virt_to_idmap(x)	__virt_to_idmap((unsigned long)(x))
3224dc9a817SSantosh Shilimkar 
3234dc9a817SSantosh Shilimkar /*
3244baa9922SRussell King  * Virtual <-> DMA view memory address translations
3254baa9922SRussell King  * Again, these are *only* valid on the kernel direct mapped RAM
3264baa9922SRussell King  * memory.  Use of these is *deprecated* (and that doesn't mean
3274baa9922SRussell King  * use the __ prefixed forms instead.)  See dma-mapping.h.
3284baa9922SRussell King  */
329b5ee9002SNicolas Pitre #ifndef __virt_to_bus
330b5ee9002SNicolas Pitre #define __virt_to_bus	__virt_to_phys
331b5ee9002SNicolas Pitre #define __bus_to_virt	__phys_to_virt
3321c4a4f48SRussell King #define __pfn_to_bus(x)	__pfn_to_phys(x)
3331c4a4f48SRussell King #define __bus_to_pfn(x)	__phys_to_pfn(x)
334b5ee9002SNicolas Pitre #endif
335b5ee9002SNicolas Pitre 
3364baa9922SRussell King /*
3374baa9922SRussell King  * Conversion between a struct page and a physical address.
3384baa9922SRussell King  *
3394baa9922SRussell King  *  page_to_pfn(page)	convert a struct page * to a PFN number
3404baa9922SRussell King  *  pfn_to_page(pfn)	convert a _valid_ PFN number to struct page *
3414baa9922SRussell King  *
3424baa9922SRussell King  *  virt_to_page(k)	convert a _valid_ virtual address to struct page *
3434baa9922SRussell King  *  virt_addr_valid(k)	indicates whether a virtual address is valid
3444baa9922SRussell King  */
3454baa9922SRussell King #define ARCH_PFN_OFFSET		PHYS_PFN_OFFSET
3464baa9922SRussell King 
347e26a9e00SRussell King #define virt_to_page(kaddr)	pfn_to_page(virt_to_pfn(kaddr))
348efea3403SLaura Abbott #define virt_addr_valid(kaddr)	(((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory) \
349e26a9e00SRussell King 					&& pfn_valid(virt_to_pfn(kaddr)))
3504baa9922SRussell King 
3514baa9922SRussell King #endif
3524baa9922SRussell King 
3534baa9922SRussell King #include <asm-generic/memory_model.h>
3544baa9922SRussell King 
3554baa9922SRussell King #endif
356