blob: ec84b31c6c86b3aa70f251f0bbc701e9c6a40fa5 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 *
5 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
6 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
7 * Copyright (C) 1996 Paul Mackerras
Paul Mackerras14cf11a2005-09-26 16:04:21 +10008 *
9 * Derived from "arch/i386/mm/init.c"
10 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
11 *
12 * Dave Engebretsen <engebret@us.ibm.com>
13 * Rework for PPC64 port.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 */
21
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +100022#undef DEBUG
23
Paul Mackerras14cf11a2005-09-26 16:04:21 +100024#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/string.h>
29#include <linux/types.h>
30#include <linux/mman.h>
31#include <linux/mm.h>
32#include <linux/swap.h>
33#include <linux/stddef.h>
34#include <linux/vmalloc.h>
35#include <linux/init.h>
36#include <linux/delay.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037#include <linux/highmem.h>
38#include <linux/idr.h>
39#include <linux/nodemask.h>
40#include <linux/module.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070041#include <linux/poison.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100042#include <linux/memblock.h>
David Gibsona4fe3ce2009-10-26 19:24:31 +000043#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Paul Mackerras18569c12017-01-30 21:21:34 +110045#include <linux/of_fdt.h>
46#include <linux/libfdt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047
48#include <asm/pgalloc.h>
49#include <asm/page.h>
50#include <asm/prom.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100051#include <asm/rtas.h>
52#include <asm/io.h>
53#include <asm/mmu_context.h>
54#include <asm/pgtable.h>
55#include <asm/mmu.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080056#include <linux/uaccess.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100057#include <asm/smp.h>
58#include <asm/machdep.h>
59#include <asm/tlb.h>
60#include <asm/eeh.h>
61#include <asm/processor.h>
62#include <asm/mmzone.h>
63#include <asm/cputable.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100064#include <asm/sections.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100065#include <asm/iommu.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100066#include <asm/vdso.h>
David Gibson800fc3e2005-11-16 15:43:48 +110067
68#include "mmu_decl.h"
Paul Mackerras14cf11a2005-09-26 16:04:21 +100069
Benjamin Herrenschmidt94491682009-06-02 21:17:45 +000070#ifdef CONFIG_PPC_STD_MMU_64
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +100071#if H_PGTABLE_RANGE > USER_VSID_RANGE
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072#warning Limited user VSID range means pagetable space is wasted
73#endif
Benjamin Herrenschmidt94491682009-06-02 21:17:45 +000074#endif /* CONFIG_PPC_STD_MMU_64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100075
Kumar Gala37dd2ba2008-04-22 04:22:34 +100076phys_addr_t memstart_addr = ~0;
Sonny Rao79c30952010-08-19 18:08:09 +000077EXPORT_SYMBOL_GPL(memstart_addr);
Kumar Gala37dd2ba2008-04-22 04:22:34 +100078phys_addr_t kernstart_addr;
Sonny Rao79c30952010-08-19 18:08:09 +000079EXPORT_SYMBOL_GPL(kernstart_addr);
Kumar Galad7917ba2008-04-16 05:52:22 +100080
Andy Whitcroftd29eff72007-10-16 01:24:17 -070081#ifdef CONFIG_SPARSEMEM_VMEMMAP
82/*
83 * Given an address within the vmemmap, determine the pfn of the page that
84 * represents the start of the section it is within. Note that we have to
85 * do this by hand as the proffered address may not be correctly aligned.
86 * Subtraction of non-aligned pointers produces undefined results.
87 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +100088static unsigned long __meminit vmemmap_section_start(unsigned long page)
Andy Whitcroftd29eff72007-10-16 01:24:17 -070089{
90 unsigned long offset = page - ((unsigned long)(vmemmap));
91
92 /* Return the pfn of the start of the section. */
93 return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
94}
95
96/*
97 * Check if this vmemmap page is already initialised. If any section
98 * which overlaps this vmemmap page is initialised then this page is
99 * initialised already.
100 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000101static int __meminit vmemmap_populated(unsigned long start, int page_size)
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700102{
103 unsigned long end = start + page_size;
Li Zhong16a05bf2014-06-11 16:23:39 +0800104 start = (unsigned long)(pfn_to_page(vmemmap_section_start(start)));
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700105
106 for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
Li Zhong16a05bf2014-06-11 16:23:39 +0800107 if (pfn_valid(page_to_pfn((struct page *)start)))
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700108 return 1;
109
110 return 0;
111}
112
Mark Nelson91eea672010-04-21 16:21:03 +0000113struct vmemmap_backing *vmemmap_list;
Li Zhongbd8cb032014-06-11 16:23:36 +0800114static struct vmemmap_backing *next;
115static int num_left;
116static int num_freed;
Mark Nelson91eea672010-04-21 16:21:03 +0000117
118static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
119{
Li Zhongbd8cb032014-06-11 16:23:36 +0800120 struct vmemmap_backing *vmem_back;
121 /* get from freed entries first */
122 if (num_freed) {
123 num_freed--;
124 vmem_back = next;
125 next = next->list;
126
127 return vmem_back;
128 }
Mark Nelson91eea672010-04-21 16:21:03 +0000129
130 /* allocate a page when required and hand out chunks */
Li Zhongbd8cb032014-06-11 16:23:36 +0800131 if (!num_left) {
Mark Nelson91eea672010-04-21 16:21:03 +0000132 next = vmemmap_alloc_block(PAGE_SIZE, node);
133 if (unlikely(!next)) {
134 WARN_ON(1);
135 return NULL;
136 }
137 num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
138 }
139
140 num_left--;
141
142 return next++;
143}
144
145static __meminit void vmemmap_list_populate(unsigned long phys,
146 unsigned long start,
147 int node)
148{
149 struct vmemmap_backing *vmem_back;
150
151 vmem_back = vmemmap_list_alloc(node);
152 if (unlikely(!vmem_back)) {
153 WARN_ON(1);
154 return;
155 }
156
157 vmem_back->phys = phys;
158 vmem_back->virt_addr = start;
159 vmem_back->list = vmemmap_list;
160
161 vmemmap_list = vmem_back;
162}
163
Li Zhong71b0bfe2014-06-11 16:23:38 +0800164int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
165{
166 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
167
168 /* Align to the page size of the linear mapping. */
169 start = _ALIGN_DOWN(start, page_size);
170
171 pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
172
173 for (; start < end; start += page_size) {
174 void *p;
David Gibson1dace6c2016-02-09 13:32:42 +1000175 int rc;
Li Zhong71b0bfe2014-06-11 16:23:38 +0800176
177 if (vmemmap_populated(start, page_size))
178 continue;
179
180 p = vmemmap_alloc_block(page_size, node);
181 if (!p)
182 return -ENOMEM;
183
184 vmemmap_list_populate(__pa(p), start, node);
185
186 pr_debug(" * %016lx..%016lx allocated at %p\n",
187 start, start + page_size, p);
188
David Gibson1dace6c2016-02-09 13:32:42 +1000189 rc = vmemmap_create_mapping(start, page_size, __pa(p));
190 if (rc < 0) {
191 pr_warning(
192 "vmemmap_populate: Unable to create vmemmap mapping: %d\n",
193 rc);
194 return -EFAULT;
195 }
Li Zhong71b0bfe2014-06-11 16:23:38 +0800196 }
197
198 return 0;
199}
200
201#ifdef CONFIG_MEMORY_HOTPLUG
Li Zhongbd8cb032014-06-11 16:23:36 +0800202static unsigned long vmemmap_list_free(unsigned long start)
203{
204 struct vmemmap_backing *vmem_back, *vmem_back_prev;
205
206 vmem_back_prev = vmem_back = vmemmap_list;
207
208 /* look for it with prev pointer recorded */
209 for (; vmem_back; vmem_back = vmem_back->list) {
210 if (vmem_back->virt_addr == start)
211 break;
212 vmem_back_prev = vmem_back;
213 }
214
215 if (unlikely(!vmem_back)) {
216 WARN_ON(1);
217 return 0;
218 }
219
220 /* remove it from vmemmap_list */
221 if (vmem_back == vmemmap_list) /* remove head */
222 vmemmap_list = vmem_back->list;
223 else
224 vmem_back_prev->list = vmem_back->list;
225
226 /* next point to this freed entry */
227 vmem_back->list = next;
228 next = vmem_back;
229 num_freed++;
230
231 return vmem_back->phys;
232}
233
Li Zhong71b0bfe2014-06-11 16:23:38 +0800234void __ref vmemmap_free(unsigned long start, unsigned long end)
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700235{
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000236 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700237
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700238 start = _ALIGN_DOWN(start, page_size);
239
Li Zhong71b0bfe2014-06-11 16:23:38 +0800240 pr_debug("vmemmap_free %lx...%lx\n", start, end);
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000241
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700242 for (; start < end; start += page_size) {
Li Zhong71b0bfe2014-06-11 16:23:38 +0800243 unsigned long addr;
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700244
Li Zhong71b0bfe2014-06-11 16:23:38 +0800245 /*
246 * the section has already be marked as invalid, so
247 * vmemmap_populated() true means some other sections still
248 * in this page, so skip it.
249 */
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700250 if (vmemmap_populated(start, page_size))
251 continue;
252
Li Zhong71b0bfe2014-06-11 16:23:38 +0800253 addr = vmemmap_list_free(start);
254 if (addr) {
255 struct page *page = pfn_to_page(addr >> PAGE_SHIFT);
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700256
Li Zhong71b0bfe2014-06-11 16:23:38 +0800257 if (PageReserved(page)) {
258 /* allocated from bootmem */
259 if (page_size < PAGE_SIZE) {
260 /*
261 * this shouldn't happen, but if it is
262 * the case, leave the memory there
263 */
264 WARN_ON_ONCE(1);
265 } else {
266 unsigned int nr_pages =
267 1 << get_order(page_size);
268 while (nr_pages--)
269 free_reserved_page(page++);
270 }
271 } else
272 free_pages((unsigned long)(__va(addr)),
273 get_order(page_size));
Mark Nelson91eea672010-04-21 16:21:03 +0000274
Li Zhong71b0bfe2014-06-11 16:23:38 +0800275 vmemmap_remove_mapping(start, page_size);
276 }
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700277 }
Andy Whitcroftd29eff72007-10-16 01:24:17 -0700278}
Li Zhong71b0bfe2014-06-11 16:23:38 +0800279#endif
Nathan Fontenotf7e33342013-09-27 10:18:09 -0500280void register_page_bootmem_memmap(unsigned long section_nr,
281 struct page *start_page, unsigned long size)
282{
283}
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700284
Alexey Kardashevskiy8e0861f2013-08-28 18:37:42 +1000285/*
286 * We do not have access to the sparsemem vmemmap, so we fallback to
287 * walking the list of sparsemem blocks which we already maintain for
288 * the sake of crashdump. In the long run, we might want to maintain
289 * a tree if performance of that linear walk becomes a problem.
290 *
291 * realmode_pfn_to_page functions can fail due to:
292 * 1) As real sparsemem blocks do not lay in RAM continously (they
293 * are in virtual address space which is not available in the real mode),
294 * the requested page struct can be split between blocks so get_page/put_page
295 * may fail.
296 * 2) When huge pages are used, the get_page/put_page API will fail
297 * in real mode as the linked addresses in the page struct are virtual
298 * too.
299 */
300struct page *realmode_pfn_to_page(unsigned long pfn)
301{
302 struct vmemmap_backing *vmem_back;
303 struct page *page;
304 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
305 unsigned long pg_va = (unsigned long) pfn_to_page(pfn);
306
307 for (vmem_back = vmemmap_list; vmem_back; vmem_back = vmem_back->list) {
308 if (pg_va < vmem_back->virt_addr)
309 continue;
310
Li Zhongbd8cb032014-06-11 16:23:36 +0800311 /* After vmemmap_list entry free is possible, need check all */
312 if ((pg_va + sizeof(struct page)) <=
313 (vmem_back->virt_addr + page_size)) {
314 page = (struct page *) (vmem_back->phys + pg_va -
Alexey Kardashevskiy8e0861f2013-08-28 18:37:42 +1000315 vmem_back->virt_addr);
Li Zhongbd8cb032014-06-11 16:23:36 +0800316 return page;
317 }
Alexey Kardashevskiy8e0861f2013-08-28 18:37:42 +1000318 }
319
Li Zhongbd8cb032014-06-11 16:23:36 +0800320 /* Probably that page struct is split between real pages */
Alexey Kardashevskiy8e0861f2013-08-28 18:37:42 +1000321 return NULL;
322}
323EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
324
325#elif defined(CONFIG_FLATMEM)
326
327struct page *realmode_pfn_to_page(unsigned long pfn)
328{
329 struct page *page = pfn_to_page(pfn);
330 return page;
331}
332EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
333
334#endif /* CONFIG_SPARSEMEM_VMEMMAP/CONFIG_FLATMEM */
Michael Ellerman1a01dc82016-07-26 20:09:30 +1000335
336#ifdef CONFIG_PPC_STD_MMU_64
Michael Ellermanc610ec62016-07-26 21:29:30 +1000337static bool disable_radix;
338static int __init parse_disable_radix(char *p)
339{
340 disable_radix = true;
341 return 0;
342}
343early_param("disable_radix", parse_disable_radix);
344
Paul Mackerras18569c12017-01-30 21:21:34 +1100345/*
Paul Mackerrascc3d2942017-01-30 21:21:36 +1100346 * If we're running under a hypervisor, we need to check the contents of
347 * /chosen/ibm,architecture-vec-5 to see if the hypervisor is willing to do
348 * radix. If not, we clear the radix feature bit so we fall back to hash.
Paul Mackerras18569c12017-01-30 21:21:34 +1100349 */
350static void early_check_vec5(void)
351{
352 unsigned long root, chosen;
353 int size;
354 const u8 *vec5;
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100355 u8 mmu_supported;
Paul Mackerras18569c12017-01-30 21:21:34 +1100356
357 root = of_get_flat_dt_root();
358 chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100359 if (chosen == -FDT_ERR_NOTFOUND) {
Paul Mackerrascc3d2942017-01-30 21:21:36 +1100360 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100361 return;
362 }
363 vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
364 if (!vec5) {
365 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
366 return;
367 }
368 if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
369 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
370 return;
371 }
372
373 /* Check for supported configuration */
374 mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
375 OV5_FEAT(OV5_MMU_SUPPORT);
376 if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
377 /* Hypervisor only supports radix - check enabled && GTSE */
378 if (!early_radix_enabled()) {
379 pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
380 }
381 if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
382 OV5_FEAT(OV5_RADIX_GTSE))) {
383 pr_warn("WARNING: Hypervisor doesn't support RADIX with GTSE\n");
384 }
385 /* Do radix anyway - the hypervisor said we had to */
386 cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
387 } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
388 /* Hypervisor only supports hash - disable radix */
389 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
390 }
Paul Mackerras18569c12017-01-30 21:21:34 +1100391}
392
Michael Ellerman1a01dc82016-07-26 20:09:30 +1000393void __init mmu_early_init_devtree(void)
394{
Michael Ellermanc610ec62016-07-26 21:29:30 +1000395 /* Disable radix mode based on kernel command line. */
Paul Mackerrasfc36a902017-03-21 12:38:02 +1100396 if (disable_radix)
Aneesh Kumar K.V5a25b6f2016-07-27 13:19:01 +1000397 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000398
Paul Mackerras18569c12017-01-30 21:21:34 +1100399 /*
400 * Check /chosen/ibm,architecture-vec-5 if running as a guest.
401 * When running bare-metal, we can use radix if we like
402 * even though the ibm,architecture-vec-5 property created by
403 * skiboot doesn't have the necessary bits set.
404 */
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100405 if (!(mfmsr() & MSR_HV))
Paul Mackerras18569c12017-01-30 21:21:34 +1100406 early_check_vec5();
407
Aneesh Kumar K.Vb8f1b4f2016-07-23 14:42:35 +0530408 if (early_radix_enabled())
Michael Ellerman2537b092016-07-26 21:55:27 +1000409 radix__early_init_devtree();
410 else
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000411 hash__early_init_devtree();
Michael Ellerman1a01dc82016-07-26 20:09:30 +1000412}
413#endif /* CONFIG_PPC_STD_MMU_64 */