xref: /linux/include/asm-generic/memory_model.h (revision d4b996f9ef1fe83d9ce9ad5c1ca0bd8231638ce5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_MEMORY_MODEL_H
3 #define __ASM_MEMORY_MODEL_H
4 
5 #include <linux/pfn.h>
6 
7 #ifndef __ASSEMBLY__
8 
9 /*
10  * supports 3 memory models.
11  */
12 #if defined(CONFIG_FLATMEM)
13 
14 #ifndef ARCH_PFN_OFFSET
15 #define ARCH_PFN_OFFSET		(0UL)
16 #endif
17 
18 #define __pfn_to_page(pfn)	(mem_map + ((pfn) - ARCH_PFN_OFFSET))
19 #define __page_to_pfn(page)	((unsigned long)((page) - mem_map) + \
20 				 ARCH_PFN_OFFSET)
21 
22 #elif defined(CONFIG_SPARSEMEM_VMEMMAP)
23 
24 /* memmap is virtually contiguous.  */
25 #define __pfn_to_page(pfn)	(vmemmap + (pfn))
26 #define __page_to_pfn(page)	(unsigned long)((page) - vmemmap)
27 
28 #elif defined(CONFIG_SPARSEMEM)
29 /*
30  * Note: section's mem_map is encoded to reflect its start_pfn.
31  * section[i].section_mem_map == mem_map's address - start_pfn;
32  */
33 #define __page_to_pfn(pg)					\
34 ({	const struct page *__pg = (pg);				\
35 	int __sec = page_to_section(__pg);			\
36 	(unsigned long)(__pg - __section_mem_map_addr(__nr_to_section(__sec)));	\
37 })
38 
39 #define __pfn_to_page(pfn)				\
40 ({	unsigned long __pfn = (pfn);			\
41 	struct mem_section *__sec = __pfn_to_section(__pfn);	\
42 	__section_mem_map_addr(__sec) + __pfn;		\
43 })
44 #endif /* CONFIG_FLATMEM/SPARSEMEM */
45 
46 /*
47  * Convert a physical address to a Page Frame Number and back
48  */
49 #define	__phys_to_pfn(paddr)	PHYS_PFN(paddr)
50 #define	__pfn_to_phys(pfn)	PFN_PHYS(pfn)
51 
52 #define page_to_pfn __page_to_pfn
53 #define pfn_to_page __pfn_to_page
54 
55 #endif /* __ASSEMBLY__ */
56 
57 #endif
58