Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 2 | #ifndef __ASM_MEMORY_MODEL_H |
| 3 | #define __ASM_MEMORY_MODEL_H |
| 4 | |
Chen Gang | 8f235d1 | 2016-01-14 15:18:33 -0800 | [diff] [blame] | 5 | #include <linux/pfn.h> |
| 6 | |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 7 | #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 Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 28 | /* |
| 29 | * supports 3 memory models. |
| 30 | */ |
| 31 | #if defined(CONFIG_FLATMEM) |
| 32 | |
Andy Whitcroft | 67de648 | 2006-06-23 02:03:12 -0700 | [diff] [blame] | 33 | #define __pfn_to_page(pfn) (mem_map + ((pfn) - ARCH_PFN_OFFSET)) |
| 34 | #define __page_to_pfn(page) ((unsigned long)((page) - mem_map) + \ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 35 | ARCH_PFN_OFFSET) |
| 36 | #elif defined(CONFIG_DISCONTIGMEM) |
| 37 | |
Andy Whitcroft | 67de648 | 2006-06-23 02:03:12 -0700 | [diff] [blame] | 38 | #define __pfn_to_page(pfn) \ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 39 | ({ unsigned long __pfn = (pfn); \ |
Rafael J. Wysocki | c5d7124 | 2008-11-08 13:53:33 +0100 | [diff] [blame] | 40 | unsigned long __nid = arch_pfn_to_nid(__pfn); \ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 41 | NODE_DATA(__nid)->node_mem_map + arch_local_page_offset(__pfn, __nid);\ |
| 42 | }) |
| 43 | |
Andy Whitcroft | 67de648 | 2006-06-23 02:03:12 -0700 | [diff] [blame] | 44 | #define __page_to_pfn(pg) \ |
Ian Campbell | aa462ab | 2011-08-17 17:40:33 +0100 | [diff] [blame] | 45 | ({ const struct page *__pg = (pg); \ |
KAMEZAWA Hiroyuki | a0140c1 | 2006-03-27 01:15:55 -0800 | [diff] [blame] | 46 | struct pglist_data *__pgdat = NODE_DATA(page_to_nid(__pg)); \ |
| 47 | (unsigned long)(__pg - __pgdat->node_mem_map) + \ |
| 48 | __pgdat->node_start_pfn; \ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 49 | }) |
| 50 | |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 51 | #elif defined(CONFIG_SPARSEMEM_VMEMMAP) |
| 52 | |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 53 | /* memmap is virtually contiguous. */ |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 54 | #define __pfn_to_page(pfn) (vmemmap + (pfn)) |
Martin Schwidefsky | 32272a2 | 2008-12-25 13:38:59 +0100 | [diff] [blame] | 55 | #define __page_to_pfn(page) (unsigned long)((page) - vmemmap) |
Christoph Lameter | 8f6aac4 | 2007-10-16 01:24:13 -0700 | [diff] [blame] | 56 | |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 57 | #elif defined(CONFIG_SPARSEMEM) |
| 58 | /* |
Zhang Yanfei | 1a49123 | 2013-10-03 19:38:14 +0800 | [diff] [blame] | 59 | * Note: section's mem_map is encoded to reflect its start_pfn. |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 60 | * section[i].section_mem_map == mem_map's address - start_pfn; |
| 61 | */ |
Andy Whitcroft | 67de648 | 2006-06-23 02:03:12 -0700 | [diff] [blame] | 62 | #define __page_to_pfn(pg) \ |
Ian Campbell | aa462ab | 2011-08-17 17:40:33 +0100 | [diff] [blame] | 63 | ({ const struct page *__pg = (pg); \ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 64 | int __sec = page_to_section(__pg); \ |
Randy Dunlap | f05b628 | 2007-02-10 01:42:59 -0800 | [diff] [blame] | 65 | (unsigned long)(__pg - __section_mem_map_addr(__nr_to_section(__sec))); \ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 66 | }) |
| 67 | |
Andy Whitcroft | 67de648 | 2006-06-23 02:03:12 -0700 | [diff] [blame] | 68 | #define __pfn_to_page(pfn) \ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 69 | ({ 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 Whitcroft | 67de648 | 2006-06-23 02:03:12 -0700 | [diff] [blame] | 74 | |
Christoph Hellwig | 012dcef | 2015-08-07 17:41:01 -0400 | [diff] [blame] | 75 | /* |
| 76 | * Convert a physical address to a Page Frame Number and back |
| 77 | */ |
Chen Gang | 8f235d1 | 2016-01-14 15:18:33 -0800 | [diff] [blame] | 78 | #define __phys_to_pfn(paddr) PHYS_PFN(paddr) |
Tyler Baker | ae4f976 | 2015-09-19 03:58:10 -0400 | [diff] [blame] | 79 | #define __pfn_to_phys(pfn) PFN_PHYS(pfn) |
Christoph Hellwig | 012dcef | 2015-08-07 17:41:01 -0400 | [diff] [blame] | 80 | |
Andy Whitcroft | 67de648 | 2006-06-23 02:03:12 -0700 | [diff] [blame] | 81 | #define page_to_pfn __page_to_pfn |
| 82 | #define pfn_to_page __pfn_to_page |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 83 | |
| 84 | #endif /* __ASSEMBLY__ */ |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 85 | |
| 86 | #endif |