xref: /linux/arch/powerpc/mm/init_64.c (revision e77553cb21adabb7680930e7b20c578cec7ae5a8)
170d64ceaSPaul Mackerras /*
270d64ceaSPaul Mackerras  *  PowerPC version
370d64ceaSPaul Mackerras  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
470d64ceaSPaul Mackerras  *
570d64ceaSPaul Mackerras  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
670d64ceaSPaul Mackerras  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
770d64ceaSPaul Mackerras  *    Copyright (C) 1996 Paul Mackerras
870d64ceaSPaul Mackerras  *
970d64ceaSPaul Mackerras  *  Derived from "arch/i386/mm/init.c"
1070d64ceaSPaul Mackerras  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
1170d64ceaSPaul Mackerras  *
1270d64ceaSPaul Mackerras  *  Dave Engebretsen <engebret@us.ibm.com>
1370d64ceaSPaul Mackerras  *      Rework for PPC64 port.
1470d64ceaSPaul Mackerras  *
1570d64ceaSPaul Mackerras  *  This program is free software; you can redistribute it and/or
1670d64ceaSPaul Mackerras  *  modify it under the terms of the GNU General Public License
1770d64ceaSPaul Mackerras  *  as published by the Free Software Foundation; either version
1870d64ceaSPaul Mackerras  *  2 of the License, or (at your option) any later version.
1970d64ceaSPaul Mackerras  *
2070d64ceaSPaul Mackerras  */
2170d64ceaSPaul Mackerras 
22cec08e7aSBenjamin Herrenschmidt #undef DEBUG
23cec08e7aSBenjamin Herrenschmidt 
2470d64ceaSPaul Mackerras #include <linux/signal.h>
2570d64ceaSPaul Mackerras #include <linux/sched.h>
2670d64ceaSPaul Mackerras #include <linux/kernel.h>
2770d64ceaSPaul Mackerras #include <linux/errno.h>
2870d64ceaSPaul Mackerras #include <linux/string.h>
2970d64ceaSPaul Mackerras #include <linux/types.h>
3070d64ceaSPaul Mackerras #include <linux/mman.h>
3170d64ceaSPaul Mackerras #include <linux/mm.h>
3270d64ceaSPaul Mackerras #include <linux/swap.h>
3370d64ceaSPaul Mackerras #include <linux/stddef.h>
3470d64ceaSPaul Mackerras #include <linux/vmalloc.h>
3570d64ceaSPaul Mackerras #include <linux/init.h>
3670d64ceaSPaul Mackerras #include <linux/delay.h>
3770d64ceaSPaul Mackerras #include <linux/highmem.h>
3870d64ceaSPaul Mackerras #include <linux/idr.h>
3970d64ceaSPaul Mackerras #include <linux/nodemask.h>
4070d64ceaSPaul Mackerras #include <linux/module.h>
41c9cf5528SRandy Dunlap #include <linux/poison.h>
4295f72d1eSYinghai Lu #include <linux/memblock.h>
43a4fe3ce7SDavid Gibson #include <linux/hugetlb.h>
445a0e3ad6STejun Heo #include <linux/slab.h>
4570d64ceaSPaul Mackerras 
4670d64ceaSPaul Mackerras #include <asm/pgalloc.h>
4770d64ceaSPaul Mackerras #include <asm/page.h>
4870d64ceaSPaul Mackerras #include <asm/prom.h>
4970d64ceaSPaul Mackerras #include <asm/rtas.h>
5070d64ceaSPaul Mackerras #include <asm/io.h>
5170d64ceaSPaul Mackerras #include <asm/mmu_context.h>
5270d64ceaSPaul Mackerras #include <asm/pgtable.h>
5370d64ceaSPaul Mackerras #include <asm/mmu.h>
5470d64ceaSPaul Mackerras #include <asm/uaccess.h>
5570d64ceaSPaul Mackerras #include <asm/smp.h>
5670d64ceaSPaul Mackerras #include <asm/machdep.h>
5770d64ceaSPaul Mackerras #include <asm/tlb.h>
5870d64ceaSPaul Mackerras #include <asm/eeh.h>
5970d64ceaSPaul Mackerras #include <asm/processor.h>
6070d64ceaSPaul Mackerras #include <asm/mmzone.h>
6170d64ceaSPaul Mackerras #include <asm/cputable.h>
6270d64ceaSPaul Mackerras #include <asm/sections.h>
6370d64ceaSPaul Mackerras #include <asm/iommu.h>
6470d64ceaSPaul Mackerras #include <asm/vdso.h>
65800fc3eeSDavid Gibson 
66800fc3eeSDavid Gibson #include "mmu_decl.h"
6770d64ceaSPaul Mackerras 
6894491685SBenjamin Herrenschmidt #ifdef CONFIG_PPC_STD_MMU_64
6970d64ceaSPaul Mackerras #if PGTABLE_RANGE > USER_VSID_RANGE
7070d64ceaSPaul Mackerras #warning Limited user VSID range means pagetable space is wasted
7170d64ceaSPaul Mackerras #endif
7270d64ceaSPaul Mackerras 
7370d64ceaSPaul Mackerras #if (TASK_SIZE_USER64 < PGTABLE_RANGE) && (TASK_SIZE_USER64 < USER_VSID_RANGE)
7470d64ceaSPaul Mackerras #warning TASK_SIZE is smaller than it needs to be.
7570d64ceaSPaul Mackerras #endif
7694491685SBenjamin Herrenschmidt #endif /* CONFIG_PPC_STD_MMU_64 */
7770d64ceaSPaul Mackerras 
7837dd2badSKumar Gala phys_addr_t memstart_addr = ~0;
7979c3095fSSonny Rao EXPORT_SYMBOL_GPL(memstart_addr);
8037dd2badSKumar Gala phys_addr_t kernstart_addr;
8179c3095fSSonny Rao EXPORT_SYMBOL_GPL(kernstart_addr);
82d7917ba7SKumar Gala 
8351cc5068SAlexey Dobriyan static void pgd_ctor(void *addr)
8470d64ceaSPaul Mackerras {
8551cc5068SAlexey Dobriyan 	memset(addr, 0, PGD_TABLE_SIZE);
8651cc5068SAlexey Dobriyan }
8751cc5068SAlexey Dobriyan 
8851cc5068SAlexey Dobriyan static void pmd_ctor(void *addr)
8951cc5068SAlexey Dobriyan {
90f940f528SAneesh Kumar K.V #ifdef CONFIG_TRANSPARENT_HUGEPAGE
91f940f528SAneesh Kumar K.V 	memset(addr, 0, PMD_TABLE_SIZE * 2);
92f940f528SAneesh Kumar K.V #else
9351cc5068SAlexey Dobriyan 	memset(addr, 0, PMD_TABLE_SIZE);
94f940f528SAneesh Kumar K.V #endif
9570d64ceaSPaul Mackerras }
9670d64ceaSPaul Mackerras 
97a0668cdcSDavid Gibson struct kmem_cache *pgtable_cache[MAX_PGTABLE_INDEX_SIZE];
9870d64ceaSPaul Mackerras 
99a0668cdcSDavid Gibson /*
100a0668cdcSDavid Gibson  * Create a kmem_cache() for pagetables.  This is not used for PTE
101a0668cdcSDavid Gibson  * pages - they're linked to struct page, come from the normal free
102a0668cdcSDavid Gibson  * pages pool and have a different entry size (see real_pte_t) to
103a0668cdcSDavid Gibson  * everything else.  Caches created by this function are used for all
104a0668cdcSDavid Gibson  * the higher level pagetables, and for hugepage pagetables.
105a0668cdcSDavid Gibson  */
106a0668cdcSDavid Gibson void pgtable_cache_add(unsigned shift, void (*ctor)(void *))
107a0668cdcSDavid Gibson {
108a0668cdcSDavid Gibson 	char *name;
109a0668cdcSDavid Gibson 	unsigned long table_size = sizeof(void *) << shift;
110a0668cdcSDavid Gibson 	unsigned long align = table_size;
111a0668cdcSDavid Gibson 
112a0668cdcSDavid Gibson 	/* When batching pgtable pointers for RCU freeing, we store
113a0668cdcSDavid Gibson 	 * the index size in the low bits.  Table alignment must be
114a4fe3ce7SDavid Gibson 	 * big enough to fit it.
115a4fe3ce7SDavid Gibson 	 *
116a4fe3ce7SDavid Gibson 	 * Likewise, hugeapge pagetable pointers contain a (different)
117a4fe3ce7SDavid Gibson 	 * shift value in the low bits.  All tables must be aligned so
118a4fe3ce7SDavid Gibson 	 * as to leave enough 0 bits in the address to contain it. */
119a4fe3ce7SDavid Gibson 	unsigned long minalign = max(MAX_PGTABLE_INDEX_SIZE + 1,
120a4fe3ce7SDavid Gibson 				     HUGEPD_SHIFT_MASK + 1);
121a0668cdcSDavid Gibson 	struct kmem_cache *new;
122a0668cdcSDavid Gibson 
123a0668cdcSDavid Gibson 	/* It would be nice if this was a BUILD_BUG_ON(), but at the
124a0668cdcSDavid Gibson 	 * moment, gcc doesn't seem to recognize is_power_of_2 as a
125a0668cdcSDavid Gibson 	 * constant expression, so so much for that. */
126a0668cdcSDavid Gibson 	BUG_ON(!is_power_of_2(minalign));
127a0668cdcSDavid Gibson 	BUG_ON((shift < 1) || (shift > MAX_PGTABLE_INDEX_SIZE));
128a0668cdcSDavid Gibson 
129a0668cdcSDavid Gibson 	if (PGT_CACHE(shift))
130a0668cdcSDavid Gibson 		return; /* Already have a cache of this size */
131a0668cdcSDavid Gibson 
132a0668cdcSDavid Gibson 	align = max_t(unsigned long, align, minalign);
133a0668cdcSDavid Gibson 	name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift);
134a0668cdcSDavid Gibson 	new = kmem_cache_create(name, table_size, align, 0, ctor);
135*e77553cbSYanjiang Jin 	kfree(name);
136cf9427b8SAneesh Kumar K.V 	pgtable_cache[shift - 1] = new;
137a0668cdcSDavid Gibson 	pr_debug("Allocated pgtable cache for order %d\n", shift);
138a0668cdcSDavid Gibson }
139a0668cdcSDavid Gibson 
14070d64ceaSPaul Mackerras 
14170d64ceaSPaul Mackerras void pgtable_cache_init(void)
14270d64ceaSPaul Mackerras {
143a0668cdcSDavid Gibson 	pgtable_cache_add(PGD_INDEX_SIZE, pgd_ctor);
144f940f528SAneesh Kumar K.V 	pgtable_cache_add(PMD_CACHE_INDEX, pmd_ctor);
145f940f528SAneesh Kumar K.V 	if (!PGT_CACHE(PGD_INDEX_SIZE) || !PGT_CACHE(PMD_CACHE_INDEX))
146a0668cdcSDavid Gibson 		panic("Couldn't allocate pgtable caches");
147a0668cdcSDavid Gibson 	/* In all current configs, when the PUD index exists it's the
148a0668cdcSDavid Gibson 	 * same size as either the pgd or pmd index.  Verify that the
149a0668cdcSDavid Gibson 	 * initialization above has also created a PUD cache.  This
150a0668cdcSDavid Gibson 	 * will need re-examiniation if we add new possibilities for
151a0668cdcSDavid Gibson 	 * the pagetable layout. */
152a0668cdcSDavid Gibson 	BUG_ON(PUD_INDEX_SIZE && !PGT_CACHE(PUD_INDEX_SIZE));
15370d64ceaSPaul Mackerras }
154d29eff7bSAndy Whitcroft 
155d29eff7bSAndy Whitcroft #ifdef CONFIG_SPARSEMEM_VMEMMAP
156d29eff7bSAndy Whitcroft /*
157d29eff7bSAndy Whitcroft  * Given an address within the vmemmap, determine the pfn of the page that
158d29eff7bSAndy Whitcroft  * represents the start of the section it is within.  Note that we have to
159d29eff7bSAndy Whitcroft  * do this by hand as the proffered address may not be correctly aligned.
160d29eff7bSAndy Whitcroft  * Subtraction of non-aligned pointers produces undefined results.
161d29eff7bSAndy Whitcroft  */
16209de9ff8SMichael Ellerman static unsigned long __meminit vmemmap_section_start(unsigned long page)
163d29eff7bSAndy Whitcroft {
164d29eff7bSAndy Whitcroft 	unsigned long offset = page - ((unsigned long)(vmemmap));
165d29eff7bSAndy Whitcroft 
166d29eff7bSAndy Whitcroft 	/* Return the pfn of the start of the section. */
167d29eff7bSAndy Whitcroft 	return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
168d29eff7bSAndy Whitcroft }
169d29eff7bSAndy Whitcroft 
170d29eff7bSAndy Whitcroft /*
171d29eff7bSAndy Whitcroft  * Check if this vmemmap page is already initialised.  If any section
172d29eff7bSAndy Whitcroft  * which overlaps this vmemmap page is initialised then this page is
173d29eff7bSAndy Whitcroft  * initialised already.
174d29eff7bSAndy Whitcroft  */
17509de9ff8SMichael Ellerman static int __meminit vmemmap_populated(unsigned long start, int page_size)
176d29eff7bSAndy Whitcroft {
177d29eff7bSAndy Whitcroft 	unsigned long end = start + page_size;
17816a05bffSLi Zhong 	start = (unsigned long)(pfn_to_page(vmemmap_section_start(start)));
179d29eff7bSAndy Whitcroft 
180d29eff7bSAndy Whitcroft 	for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
18116a05bffSLi Zhong 		if (pfn_valid(page_to_pfn((struct page *)start)))
182d29eff7bSAndy Whitcroft 			return 1;
183d29eff7bSAndy Whitcroft 
184d29eff7bSAndy Whitcroft 	return 0;
185d29eff7bSAndy Whitcroft }
186d29eff7bSAndy Whitcroft 
18732a74949SBenjamin Herrenschmidt /* On hash-based CPUs, the vmemmap is bolted in the hash table.
18832a74949SBenjamin Herrenschmidt  *
18932a74949SBenjamin Herrenschmidt  * On Book3E CPUs, the vmemmap is currently mapped in the top half of
19032a74949SBenjamin Herrenschmidt  * the vmalloc space using normal page tables, though the size of
19132a74949SBenjamin Herrenschmidt  * pages encoded in the PTEs can be different
19232a74949SBenjamin Herrenschmidt  */
19332a74949SBenjamin Herrenschmidt 
19432a74949SBenjamin Herrenschmidt #ifdef CONFIG_PPC_BOOK3E
19532a74949SBenjamin Herrenschmidt static void __meminit vmemmap_create_mapping(unsigned long start,
19632a74949SBenjamin Herrenschmidt 					     unsigned long page_size,
19732a74949SBenjamin Herrenschmidt 					     unsigned long phys)
19832a74949SBenjamin Herrenschmidt {
19932a74949SBenjamin Herrenschmidt 	/* Create a PTE encoding without page size */
20032a74949SBenjamin Herrenschmidt 	unsigned long i, flags = _PAGE_PRESENT | _PAGE_ACCESSED |
20132a74949SBenjamin Herrenschmidt 		_PAGE_KERNEL_RW;
20232a74949SBenjamin Herrenschmidt 
20332a74949SBenjamin Herrenschmidt 	/* PTEs only contain page size encodings up to 32M */
20432a74949SBenjamin Herrenschmidt 	BUG_ON(mmu_psize_defs[mmu_vmemmap_psize].enc > 0xf);
20532a74949SBenjamin Herrenschmidt 
20632a74949SBenjamin Herrenschmidt 	/* Encode the size in the PTE */
20732a74949SBenjamin Herrenschmidt 	flags |= mmu_psize_defs[mmu_vmemmap_psize].enc << 8;
20832a74949SBenjamin Herrenschmidt 
20932a74949SBenjamin Herrenschmidt 	/* For each PTE for that area, map things. Note that we don't
21032a74949SBenjamin Herrenschmidt 	 * increment phys because all PTEs are of the large size and
21132a74949SBenjamin Herrenschmidt 	 * thus must have the low bits clear
21232a74949SBenjamin Herrenschmidt 	 */
21332a74949SBenjamin Herrenschmidt 	for (i = 0; i < page_size; i += PAGE_SIZE)
21432a74949SBenjamin Herrenschmidt 		BUG_ON(map_kernel_page(start + i, phys, flags));
21532a74949SBenjamin Herrenschmidt }
216ed5694a8SLi Zhong 
217ed5694a8SLi Zhong #ifdef CONFIG_MEMORY_HOTPLUG
218ed5694a8SLi Zhong static void vmemmap_remove_mapping(unsigned long start,
219ed5694a8SLi Zhong 				   unsigned long page_size)
220ed5694a8SLi Zhong {
221ed5694a8SLi Zhong }
222ed5694a8SLi Zhong #endif
22332a74949SBenjamin Herrenschmidt #else /* CONFIG_PPC_BOOK3E */
22432a74949SBenjamin Herrenschmidt static void __meminit vmemmap_create_mapping(unsigned long start,
22532a74949SBenjamin Herrenschmidt 					     unsigned long page_size,
22632a74949SBenjamin Herrenschmidt 					     unsigned long phys)
22732a74949SBenjamin Herrenschmidt {
22832a74949SBenjamin Herrenschmidt 	int  mapped = htab_bolt_mapping(start, start + page_size, phys,
22983d5e64bSAneesh Kumar K.V 					pgprot_val(PAGE_KERNEL),
23083d5e64bSAneesh Kumar K.V 					mmu_vmemmap_psize,
23132a74949SBenjamin Herrenschmidt 					mmu_kernel_ssize);
23232a74949SBenjamin Herrenschmidt 	BUG_ON(mapped < 0);
23332a74949SBenjamin Herrenschmidt }
234ed5694a8SLi Zhong 
235ed5694a8SLi Zhong #ifdef CONFIG_MEMORY_HOTPLUG
236ed5694a8SLi Zhong static void vmemmap_remove_mapping(unsigned long start,
237ed5694a8SLi Zhong 				   unsigned long page_size)
238ed5694a8SLi Zhong {
239ed5694a8SLi Zhong 	int mapped = htab_remove_mapping(start, start + page_size,
240ed5694a8SLi Zhong 					 mmu_vmemmap_psize,
241ed5694a8SLi Zhong 					 mmu_kernel_ssize);
242ed5694a8SLi Zhong 	BUG_ON(mapped < 0);
243ed5694a8SLi Zhong }
244ed5694a8SLi Zhong #endif
245ed5694a8SLi Zhong 
24632a74949SBenjamin Herrenschmidt #endif /* CONFIG_PPC_BOOK3E */
24732a74949SBenjamin Herrenschmidt 
24891eea67cSMark Nelson struct vmemmap_backing *vmemmap_list;
249bd8cb03dSLi Zhong static struct vmemmap_backing *next;
250bd8cb03dSLi Zhong static int num_left;
251bd8cb03dSLi Zhong static int num_freed;
25291eea67cSMark Nelson 
25391eea67cSMark Nelson static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
25491eea67cSMark Nelson {
255bd8cb03dSLi Zhong 	struct vmemmap_backing *vmem_back;
256bd8cb03dSLi Zhong 	/* get from freed entries first */
257bd8cb03dSLi Zhong 	if (num_freed) {
258bd8cb03dSLi Zhong 		num_freed--;
259bd8cb03dSLi Zhong 		vmem_back = next;
260bd8cb03dSLi Zhong 		next = next->list;
261bd8cb03dSLi Zhong 
262bd8cb03dSLi Zhong 		return vmem_back;
263bd8cb03dSLi Zhong 	}
26491eea67cSMark Nelson 
26591eea67cSMark Nelson 	/* allocate a page when required and hand out chunks */
266bd8cb03dSLi Zhong 	if (!num_left) {
26791eea67cSMark Nelson 		next = vmemmap_alloc_block(PAGE_SIZE, node);
26891eea67cSMark Nelson 		if (unlikely(!next)) {
26991eea67cSMark Nelson 			WARN_ON(1);
27091eea67cSMark Nelson 			return NULL;
27191eea67cSMark Nelson 		}
27291eea67cSMark Nelson 		num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
27391eea67cSMark Nelson 	}
27491eea67cSMark Nelson 
27591eea67cSMark Nelson 	num_left--;
27691eea67cSMark Nelson 
27791eea67cSMark Nelson 	return next++;
27891eea67cSMark Nelson }
27991eea67cSMark Nelson 
28091eea67cSMark Nelson static __meminit void vmemmap_list_populate(unsigned long phys,
28191eea67cSMark Nelson 					    unsigned long start,
28291eea67cSMark Nelson 					    int node)
28391eea67cSMark Nelson {
28491eea67cSMark Nelson 	struct vmemmap_backing *vmem_back;
28591eea67cSMark Nelson 
28691eea67cSMark Nelson 	vmem_back = vmemmap_list_alloc(node);
28791eea67cSMark Nelson 	if (unlikely(!vmem_back)) {
28891eea67cSMark Nelson 		WARN_ON(1);
28991eea67cSMark Nelson 		return;
29091eea67cSMark Nelson 	}
29191eea67cSMark Nelson 
29291eea67cSMark Nelson 	vmem_back->phys = phys;
29391eea67cSMark Nelson 	vmem_back->virt_addr = start;
29491eea67cSMark Nelson 	vmem_back->list = vmemmap_list;
29591eea67cSMark Nelson 
29691eea67cSMark Nelson 	vmemmap_list = vmem_back;
29791eea67cSMark Nelson }
29891eea67cSMark Nelson 
29971b0bfe4SLi Zhong int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
30071b0bfe4SLi Zhong {
30171b0bfe4SLi Zhong 	unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
30271b0bfe4SLi Zhong 
30371b0bfe4SLi Zhong 	/* Align to the page size of the linear mapping. */
30471b0bfe4SLi Zhong 	start = _ALIGN_DOWN(start, page_size);
30571b0bfe4SLi Zhong 
30671b0bfe4SLi Zhong 	pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
30771b0bfe4SLi Zhong 
30871b0bfe4SLi Zhong 	for (; start < end; start += page_size) {
30971b0bfe4SLi Zhong 		void *p;
31071b0bfe4SLi Zhong 
31171b0bfe4SLi Zhong 		if (vmemmap_populated(start, page_size))
31271b0bfe4SLi Zhong 			continue;
31371b0bfe4SLi Zhong 
31471b0bfe4SLi Zhong 		p = vmemmap_alloc_block(page_size, node);
31571b0bfe4SLi Zhong 		if (!p)
31671b0bfe4SLi Zhong 			return -ENOMEM;
31771b0bfe4SLi Zhong 
31871b0bfe4SLi Zhong 		vmemmap_list_populate(__pa(p), start, node);
31971b0bfe4SLi Zhong 
32071b0bfe4SLi Zhong 		pr_debug("      * %016lx..%016lx allocated at %p\n",
32171b0bfe4SLi Zhong 			 start, start + page_size, p);
32271b0bfe4SLi Zhong 
32371b0bfe4SLi Zhong 		vmemmap_create_mapping(start, page_size, __pa(p));
32471b0bfe4SLi Zhong 	}
32571b0bfe4SLi Zhong 
32671b0bfe4SLi Zhong 	return 0;
32771b0bfe4SLi Zhong }
32871b0bfe4SLi Zhong 
32971b0bfe4SLi Zhong #ifdef CONFIG_MEMORY_HOTPLUG
330bd8cb03dSLi Zhong static unsigned long vmemmap_list_free(unsigned long start)
331bd8cb03dSLi Zhong {
332bd8cb03dSLi Zhong 	struct vmemmap_backing *vmem_back, *vmem_back_prev;
333bd8cb03dSLi Zhong 
334bd8cb03dSLi Zhong 	vmem_back_prev = vmem_back = vmemmap_list;
335bd8cb03dSLi Zhong 
336bd8cb03dSLi Zhong 	/* look for it with prev pointer recorded */
337bd8cb03dSLi Zhong 	for (; vmem_back; vmem_back = vmem_back->list) {
338bd8cb03dSLi Zhong 		if (vmem_back->virt_addr == start)
339bd8cb03dSLi Zhong 			break;
340bd8cb03dSLi Zhong 		vmem_back_prev = vmem_back;
341bd8cb03dSLi Zhong 	}
342bd8cb03dSLi Zhong 
343bd8cb03dSLi Zhong 	if (unlikely(!vmem_back)) {
344bd8cb03dSLi Zhong 		WARN_ON(1);
345bd8cb03dSLi Zhong 		return 0;
346bd8cb03dSLi Zhong 	}
347bd8cb03dSLi Zhong 
348bd8cb03dSLi Zhong 	/* remove it from vmemmap_list */
349bd8cb03dSLi Zhong 	if (vmem_back == vmemmap_list) /* remove head */
350bd8cb03dSLi Zhong 		vmemmap_list = vmem_back->list;
351bd8cb03dSLi Zhong 	else
352bd8cb03dSLi Zhong 		vmem_back_prev->list = vmem_back->list;
353bd8cb03dSLi Zhong 
354bd8cb03dSLi Zhong 	/* next point to this freed entry */
355bd8cb03dSLi Zhong 	vmem_back->list = next;
356bd8cb03dSLi Zhong 	next = vmem_back;
357bd8cb03dSLi Zhong 	num_freed++;
358bd8cb03dSLi Zhong 
359bd8cb03dSLi Zhong 	return vmem_back->phys;
360bd8cb03dSLi Zhong }
361bd8cb03dSLi Zhong 
36271b0bfe4SLi Zhong void __ref vmemmap_free(unsigned long start, unsigned long end)
363d29eff7bSAndy Whitcroft {
364cec08e7aSBenjamin Herrenschmidt 	unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
365d29eff7bSAndy Whitcroft 
366d29eff7bSAndy Whitcroft 	start = _ALIGN_DOWN(start, page_size);
367d29eff7bSAndy Whitcroft 
36871b0bfe4SLi Zhong 	pr_debug("vmemmap_free %lx...%lx\n", start, end);
36932a74949SBenjamin Herrenschmidt 
370d29eff7bSAndy Whitcroft 	for (; start < end; start += page_size) {
37171b0bfe4SLi Zhong 		unsigned long addr;
372d29eff7bSAndy Whitcroft 
37371b0bfe4SLi Zhong 		/*
37471b0bfe4SLi Zhong 		 * the section has already be marked as invalid, so
37571b0bfe4SLi Zhong 		 * vmemmap_populated() true means some other sections still
37671b0bfe4SLi Zhong 		 * in this page, so skip it.
37771b0bfe4SLi Zhong 		 */
378d29eff7bSAndy Whitcroft 		if (vmemmap_populated(start, page_size))
379d29eff7bSAndy Whitcroft 			continue;
380d29eff7bSAndy Whitcroft 
38171b0bfe4SLi Zhong 		addr = vmemmap_list_free(start);
38271b0bfe4SLi Zhong 		if (addr) {
38371b0bfe4SLi Zhong 			struct page *page = pfn_to_page(addr >> PAGE_SHIFT);
384d29eff7bSAndy Whitcroft 
38571b0bfe4SLi Zhong 			if (PageReserved(page)) {
38671b0bfe4SLi Zhong 				/* allocated from bootmem */
38771b0bfe4SLi Zhong 				if (page_size < PAGE_SIZE) {
38871b0bfe4SLi Zhong 					/*
38971b0bfe4SLi Zhong 					 * this shouldn't happen, but if it is
39071b0bfe4SLi Zhong 					 * the case, leave the memory there
39171b0bfe4SLi Zhong 					 */
39271b0bfe4SLi Zhong 					WARN_ON_ONCE(1);
39371b0bfe4SLi Zhong 				} else {
39471b0bfe4SLi Zhong 					unsigned int nr_pages =
39571b0bfe4SLi Zhong 						1 << get_order(page_size);
39671b0bfe4SLi Zhong 					while (nr_pages--)
39771b0bfe4SLi Zhong 						free_reserved_page(page++);
398d29eff7bSAndy Whitcroft 				}
39971b0bfe4SLi Zhong 			} else
40071b0bfe4SLi Zhong 				free_pages((unsigned long)(__va(addr)),
40171b0bfe4SLi Zhong 							get_order(page_size));
402d29eff7bSAndy Whitcroft 
40371b0bfe4SLi Zhong 			vmemmap_remove_mapping(start, page_size);
404d29eff7bSAndy Whitcroft 		}
4050197518cSTang Chen 	}
40671b0bfe4SLi Zhong }
40771b0bfe4SLi Zhong #endif
408f7e3334aSNathan Fontenot void register_page_bootmem_memmap(unsigned long section_nr,
409f7e3334aSNathan Fontenot 				  struct page *start_page, unsigned long size)
410f7e3334aSNathan Fontenot {
411f7e3334aSNathan Fontenot }
412cd3db0c4SBenjamin Herrenschmidt 
4138e0861faSAlexey Kardashevskiy /*
4148e0861faSAlexey Kardashevskiy  * We do not have access to the sparsemem vmemmap, so we fallback to
4158e0861faSAlexey Kardashevskiy  * walking the list of sparsemem blocks which we already maintain for
4168e0861faSAlexey Kardashevskiy  * the sake of crashdump. In the long run, we might want to maintain
4178e0861faSAlexey Kardashevskiy  * a tree if performance of that linear walk becomes a problem.
4188e0861faSAlexey Kardashevskiy  *
4198e0861faSAlexey Kardashevskiy  * realmode_pfn_to_page functions can fail due to:
4208e0861faSAlexey Kardashevskiy  * 1) As real sparsemem blocks do not lay in RAM continously (they
4218e0861faSAlexey Kardashevskiy  * are in virtual address space which is not available in the real mode),
4228e0861faSAlexey Kardashevskiy  * the requested page struct can be split between blocks so get_page/put_page
4238e0861faSAlexey Kardashevskiy  * may fail.
4248e0861faSAlexey Kardashevskiy  * 2) When huge pages are used, the get_page/put_page API will fail
4258e0861faSAlexey Kardashevskiy  * in real mode as the linked addresses in the page struct are virtual
4268e0861faSAlexey Kardashevskiy  * too.
4278e0861faSAlexey Kardashevskiy  */
4288e0861faSAlexey Kardashevskiy struct page *realmode_pfn_to_page(unsigned long pfn)
4298e0861faSAlexey Kardashevskiy {
4308e0861faSAlexey Kardashevskiy 	struct vmemmap_backing *vmem_back;
4318e0861faSAlexey Kardashevskiy 	struct page *page;
4328e0861faSAlexey Kardashevskiy 	unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
4338e0861faSAlexey Kardashevskiy 	unsigned long pg_va = (unsigned long) pfn_to_page(pfn);
4348e0861faSAlexey Kardashevskiy 
4358e0861faSAlexey Kardashevskiy 	for (vmem_back = vmemmap_list; vmem_back; vmem_back = vmem_back->list) {
4368e0861faSAlexey Kardashevskiy 		if (pg_va < vmem_back->virt_addr)
4378e0861faSAlexey Kardashevskiy 			continue;
4388e0861faSAlexey Kardashevskiy 
439bd8cb03dSLi Zhong 		/* After vmemmap_list entry free is possible, need check all */
440bd8cb03dSLi Zhong 		if ((pg_va + sizeof(struct page)) <=
441bd8cb03dSLi Zhong 				(vmem_back->virt_addr + page_size)) {
4428e0861faSAlexey Kardashevskiy 			page = (struct page *) (vmem_back->phys + pg_va -
4438e0861faSAlexey Kardashevskiy 				vmem_back->virt_addr);
4448e0861faSAlexey Kardashevskiy 			return page;
4458e0861faSAlexey Kardashevskiy 		}
446bd8cb03dSLi Zhong 	}
4478e0861faSAlexey Kardashevskiy 
448bd8cb03dSLi Zhong 	/* Probably that page struct is split between real pages */
4498e0861faSAlexey Kardashevskiy 	return NULL;
4508e0861faSAlexey Kardashevskiy }
4518e0861faSAlexey Kardashevskiy EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
4528e0861faSAlexey Kardashevskiy 
4538e0861faSAlexey Kardashevskiy #elif defined(CONFIG_FLATMEM)
4548e0861faSAlexey Kardashevskiy 
4558e0861faSAlexey Kardashevskiy struct page *realmode_pfn_to_page(unsigned long pfn)
4568e0861faSAlexey Kardashevskiy {
4578e0861faSAlexey Kardashevskiy 	struct page *page = pfn_to_page(pfn);
4588e0861faSAlexey Kardashevskiy 	return page;
4598e0861faSAlexey Kardashevskiy }
4608e0861faSAlexey Kardashevskiy EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
4618e0861faSAlexey Kardashevskiy 
4628e0861faSAlexey Kardashevskiy #endif /* CONFIG_SPARSEMEM_VMEMMAP/CONFIG_FLATMEM */
463