blob: 7637fb46ba4f49edb81985c7baf7fff1aadc8675 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08002#ifndef __ASM_MEMORY_MODEL_H
3#define __ASM_MEMORY_MODEL_H
4
Chen Gang8f235d12016-01-14 15:18:33 -08005#include <linux/pfn.h>
6
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -08007#ifndef __ASSEMBLY__
8
9#if defined(CONFIG_FLATMEM)
10
11#ifndef ARCH_PFN_OFFSET
12#define ARCH_PFN_OFFSET (0UL)
13#endif
14
15#elif defined(CONFIG_DISCONTIGMEM)
16
17#ifndef arch_pfn_to_nid
18#define arch_pfn_to_nid(pfn) pfn_to_nid(pfn)
19#endif
20
21#ifndef arch_local_page_offset
22#define arch_local_page_offset(pfn, nid) \
23 ((pfn) - NODE_DATA(nid)->node_start_pfn)
24#endif
25
26#endif /* CONFIG_DISCONTIGMEM */
27
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080028/*
29 * supports 3 memory models.
30 */
31#if defined(CONFIG_FLATMEM)
32
Andy Whitcroft67de6482006-06-23 02:03:12 -070033#define __pfn_to_page(pfn) (mem_map + ((pfn) - ARCH_PFN_OFFSET))
34#define __page_to_pfn(page) ((unsigned long)((page) - mem_map) + \
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080035 ARCH_PFN_OFFSET)
36#elif defined(CONFIG_DISCONTIGMEM)
37
Andy Whitcroft67de6482006-06-23 02:03:12 -070038#define __pfn_to_page(pfn) \
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080039({ unsigned long __pfn = (pfn); \
Rafael J. Wysockic5d71242008-11-08 13:53:33 +010040 unsigned long __nid = arch_pfn_to_nid(__pfn); \
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080041 NODE_DATA(__nid)->node_mem_map + arch_local_page_offset(__pfn, __nid);\
42})
43
Andy Whitcroft67de6482006-06-23 02:03:12 -070044#define __page_to_pfn(pg) \
Ian Campbellaa462ab2011-08-17 17:40:33 +010045({ const struct page *__pg = (pg); \
KAMEZAWA Hiroyukia0140c12006-03-27 01:15:55 -080046 struct pglist_data *__pgdat = NODE_DATA(page_to_nid(__pg)); \
47 (unsigned long)(__pg - __pgdat->node_mem_map) + \
48 __pgdat->node_start_pfn; \
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080049})
50
Christoph Lameter8f6aac42007-10-16 01:24:13 -070051#elif defined(CONFIG_SPARSEMEM_VMEMMAP)
52
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020053/* memmap is virtually contiguous. */
Christoph Lameter8f6aac42007-10-16 01:24:13 -070054#define __pfn_to_page(pfn) (vmemmap + (pfn))
Martin Schwidefsky32272a22008-12-25 13:38:59 +010055#define __page_to_pfn(page) (unsigned long)((page) - vmemmap)
Christoph Lameter8f6aac42007-10-16 01:24:13 -070056
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080057#elif defined(CONFIG_SPARSEMEM)
58/*
Zhang Yanfei1a491232013-10-03 19:38:14 +080059 * Note: section's mem_map is encoded to reflect its start_pfn.
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080060 * section[i].section_mem_map == mem_map's address - start_pfn;
61 */
Andy Whitcroft67de6482006-06-23 02:03:12 -070062#define __page_to_pfn(pg) \
Ian Campbellaa462ab2011-08-17 17:40:33 +010063({ const struct page *__pg = (pg); \
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080064 int __sec = page_to_section(__pg); \
Randy Dunlapf05b6282007-02-10 01:42:59 -080065 (unsigned long)(__pg - __section_mem_map_addr(__nr_to_section(__sec))); \
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080066})
67
Andy Whitcroft67de6482006-06-23 02:03:12 -070068#define __pfn_to_page(pfn) \
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080069({ unsigned long __pfn = (pfn); \
70 struct mem_section *__sec = __pfn_to_section(__pfn); \
71 __section_mem_map_addr(__sec) + __pfn; \
72})
73#endif /* CONFIG_FLATMEM/DISCONTIGMEM/SPARSEMEM */
Andy Whitcroft67de6482006-06-23 02:03:12 -070074
Christoph Hellwig012dcef2015-08-07 17:41:01 -040075/*
76 * Convert a physical address to a Page Frame Number and back
77 */
Chen Gang8f235d12016-01-14 15:18:33 -080078#define __phys_to_pfn(paddr) PHYS_PFN(paddr)
Tyler Bakerae4f9762015-09-19 03:58:10 -040079#define __pfn_to_phys(pfn) PFN_PHYS(pfn)
Christoph Hellwig012dcef2015-08-07 17:41:01 -040080
Andy Whitcroft67de6482006-06-23 02:03:12 -070081#define page_to_pfn __page_to_pfn
82#define pfn_to_page __pfn_to_page
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080083
84#endif /* __ASSEMBLY__ */
KAMEZAWA Hiroyukia117e662006-03-27 01:15:25 -080085
86#endif