blob: 51ce091914f9760b85853d7f197bceb5ef73e02a [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>
Oliver O'Halloranb584c252017-06-28 11:32:33 +100047#include <linux/memremap.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048
49#include <asm/pgalloc.h>
50#include <asm/page.h>
51#include <asm/prom.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100052#include <asm/rtas.h>
53#include <asm/io.h>
54#include <asm/mmu_context.h>
55#include <asm/pgtable.h>
56#include <asm/mmu.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080057#include <linux/uaccess.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100058#include <asm/smp.h>
59#include <asm/machdep.h>
60#include <asm/tlb.h>
61#include <asm/eeh.h>
62#include <asm/processor.h>
63#include <asm/mmzone.h>
64#include <asm/cputable.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100065#include <asm/sections.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100066#include <asm/iommu.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100067#include <asm/vdso.h>
David Gibson800fc3e2005-11-16 15:43:48 +110068
69#include "mmu_decl.h"
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070
Kumar Gala37dd2ba2008-04-22 04:22:34 +100071phys_addr_t memstart_addr = ~0;
Sonny Rao79c30952010-08-19 18:08:09 +000072EXPORT_SYMBOL_GPL(memstart_addr);
Kumar Gala37dd2ba2008-04-22 04:22:34 +100073phys_addr_t kernstart_addr;
Sonny Rao79c30952010-08-19 18:08:09 +000074EXPORT_SYMBOL_GPL(kernstart_addr);
Kumar Galad7917ba2008-04-16 05:52:22 +100075
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -070076#ifdef CONFIG_SPARSEMEM_VMEMMAP
77/*
78 * Given an address within the vmemmap, determine the pfn of the page that
79 * represents the start of the section it is within. Note that we have to
80 * do this by hand as the proffered address may not be correctly aligned.
81 * Subtraction of non-aligned pointers produces undefined results.
82 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +100083static unsigned long __meminit vmemmap_section_start(unsigned long page)
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -070084{
85 unsigned long offset = page - ((unsigned long)(vmemmap));
86
87 /* Return the pfn of the start of the section. */
88 return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
89}
90
91/*
92 * Check if this vmemmap page is already initialised. If any section
93 * which overlaps this vmemmap page is initialised then this page is
94 * initialised already.
95 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +100096static int __meminit vmemmap_populated(unsigned long start, int page_size)
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -070097{
98 unsigned long end = start + page_size;
Li Zhong16a05bf2014-06-11 16:23:39 +080099 start = (unsigned long)(pfn_to_page(vmemmap_section_start(start)));
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700100
101 for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
Li Zhong16a05bf2014-06-11 16:23:39 +0800102 if (pfn_valid(page_to_pfn((struct page *)start)))
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700103 return 1;
104
105 return 0;
106}
107
Anshuman Khandual39e46752017-04-06 19:44:50 +0530108/*
109 * vmemmap virtual address space management does not have a traditonal page
110 * table to track which virtual struct pages are backed by physical mapping.
111 * The virtual to physical mappings are tracked in a simple linked list
112 * format. 'vmemmap_list' maintains the entire vmemmap physical mapping at
113 * all times where as the 'next' list maintains the available
114 * vmemmap_backing structures which have been deleted from the
115 * 'vmemmap_global' list during system runtime (memory hotplug remove
116 * operation). The freed 'vmemmap_backing' structures are reused later when
117 * new requests come in without allocating fresh memory. This pointer also
118 * tracks the allocated 'vmemmap_backing' structures as we allocate one
119 * full page memory at a time when we dont have any.
120 */
Mark Nelson91eea672010-04-21 16:21:03 +0000121struct vmemmap_backing *vmemmap_list;
Li Zhongbd8cb032014-06-11 16:23:36 +0800122static struct vmemmap_backing *next;
Anshuman Khandual39e46752017-04-06 19:44:50 +0530123
124/*
125 * The same pointer 'next' tracks individual chunks inside the allocated
126 * full page during the boot time and again tracks the freeed nodes during
127 * runtime. It is racy but it does not happen as they are separated by the
128 * boot process. Will create problem if some how we have memory hotplug
129 * operation during boot !!
130 */
Li Zhongbd8cb032014-06-11 16:23:36 +0800131static int num_left;
132static int num_freed;
Mark Nelson91eea672010-04-21 16:21:03 +0000133
134static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
135{
Li Zhongbd8cb032014-06-11 16:23:36 +0800136 struct vmemmap_backing *vmem_back;
137 /* get from freed entries first */
138 if (num_freed) {
139 num_freed--;
140 vmem_back = next;
141 next = next->list;
142
143 return vmem_back;
144 }
Mark Nelson91eea672010-04-21 16:21:03 +0000145
146 /* allocate a page when required and hand out chunks */
Li Zhongbd8cb032014-06-11 16:23:36 +0800147 if (!num_left) {
Mark Nelson91eea672010-04-21 16:21:03 +0000148 next = vmemmap_alloc_block(PAGE_SIZE, node);
149 if (unlikely(!next)) {
150 WARN_ON(1);
151 return NULL;
152 }
153 num_left = PAGE_SIZE / sizeof(struct vmemmap_backing);
154 }
155
156 num_left--;
157
158 return next++;
159}
160
161static __meminit void vmemmap_list_populate(unsigned long phys,
162 unsigned long start,
163 int node)
164{
165 struct vmemmap_backing *vmem_back;
166
167 vmem_back = vmemmap_list_alloc(node);
168 if (unlikely(!vmem_back)) {
169 WARN_ON(1);
170 return;
171 }
172
173 vmem_back->phys = phys;
174 vmem_back->virt_addr = start;
175 vmem_back->list = vmemmap_list;
176
177 vmemmap_list = vmem_back;
178}
179
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100180int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
181 struct vmem_altmap *altmap)
Li Zhong71b0bfe2014-06-11 16:23:38 +0800182{
183 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
184
185 /* Align to the page size of the linear mapping. */
186 start = _ALIGN_DOWN(start, page_size);
187
188 pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
189
190 for (; start < end; start += page_size) {
191 void *p;
David Gibson1dace6c2016-02-09 13:32:42 +1000192 int rc;
Li Zhong71b0bfe2014-06-11 16:23:38 +0800193
194 if (vmemmap_populated(start, page_size))
195 continue;
196
Christoph Hellwiga8fc3572017-12-29 08:53:58 +0100197 if (altmap)
198 p = altmap_alloc_block_buf(page_size, altmap);
199 else
200 p = vmemmap_alloc_block_buf(page_size, node);
Li Zhong71b0bfe2014-06-11 16:23:38 +0800201 if (!p)
202 return -ENOMEM;
203
204 vmemmap_list_populate(__pa(p), start, node);
205
206 pr_debug(" * %016lx..%016lx allocated at %p\n",
207 start, start + page_size, p);
208
David Gibson1dace6c2016-02-09 13:32:42 +1000209 rc = vmemmap_create_mapping(start, page_size, __pa(p));
210 if (rc < 0) {
Joe Perchesf2c2cbc2016-10-24 21:00:08 -0700211 pr_warn("%s: Unable to create vmemmap mapping: %d\n",
212 __func__, rc);
David Gibson1dace6c2016-02-09 13:32:42 +1000213 return -EFAULT;
214 }
Li Zhong71b0bfe2014-06-11 16:23:38 +0800215 }
216
217 return 0;
218}
219
220#ifdef CONFIG_MEMORY_HOTPLUG
Li Zhongbd8cb032014-06-11 16:23:36 +0800221static unsigned long vmemmap_list_free(unsigned long start)
222{
223 struct vmemmap_backing *vmem_back, *vmem_back_prev;
224
225 vmem_back_prev = vmem_back = vmemmap_list;
226
227 /* look for it with prev pointer recorded */
228 for (; vmem_back; vmem_back = vmem_back->list) {
229 if (vmem_back->virt_addr == start)
230 break;
231 vmem_back_prev = vmem_back;
232 }
233
234 if (unlikely(!vmem_back)) {
235 WARN_ON(1);
236 return 0;
237 }
238
239 /* remove it from vmemmap_list */
240 if (vmem_back == vmemmap_list) /* remove head */
241 vmemmap_list = vmem_back->list;
242 else
243 vmem_back_prev->list = vmem_back->list;
244
245 /* next point to this freed entry */
246 vmem_back->list = next;
247 next = vmem_back;
248 num_freed++;
249
250 return vmem_back->phys;
251}
252
Christoph Hellwig24b6d412017-12-29 08:53:56 +0100253void __ref vmemmap_free(unsigned long start, unsigned long end,
254 struct vmem_altmap *altmap)
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700255{
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000256 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000257 unsigned long page_order = get_order(page_size);
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700258
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700259 start = _ALIGN_DOWN(start, page_size);
260
Li Zhong71b0bfe2014-06-11 16:23:38 +0800261 pr_debug("vmemmap_free %lx...%lx\n", start, end);
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000262
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700263 for (; start < end; start += page_size) {
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000264 unsigned long nr_pages, addr;
Oliver O'Halloranb584c252017-06-28 11:32:33 +1000265 struct page *section_base;
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000266 struct page *page;
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700267
Li Zhong71b0bfe2014-06-11 16:23:38 +0800268 /*
269 * the section has already be marked as invalid, so
270 * vmemmap_populated() true means some other sections still
271 * in this page, so skip it.
272 */
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700273 if (vmemmap_populated(start, page_size))
274 continue;
275
Li Zhong71b0bfe2014-06-11 16:23:38 +0800276 addr = vmemmap_list_free(start);
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000277 if (!addr)
278 continue;
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700279
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000280 page = pfn_to_page(addr >> PAGE_SHIFT);
Oliver O'Halloranb584c252017-06-28 11:32:33 +1000281 section_base = pfn_to_page(vmemmap_section_start(start));
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000282 nr_pages = 1 << page_order;
Mark Nelson91eea672010-04-21 16:21:03 +0000283
Oliver O'Halloranb584c252017-06-28 11:32:33 +1000284 if (altmap) {
285 vmem_altmap_free(altmap, nr_pages);
286 } else if (PageReserved(page)) {
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000287 /* allocated from bootmem */
288 if (page_size < PAGE_SIZE) {
289 /*
290 * this shouldn't happen, but if it is
291 * the case, leave the memory there
292 */
293 WARN_ON_ONCE(1);
294 } else {
295 while (nr_pages--)
296 free_reserved_page(page++);
297 }
298 } else {
299 free_pages((unsigned long)(__va(addr)), page_order);
Li Zhong71b0bfe2014-06-11 16:23:38 +0800300 }
Oliver O'Hallorand7d9b612017-06-28 11:32:32 +1000301
302 vmemmap_remove_mapping(start, page_size);
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700303 }
Andy Whitcroftd29eff7bc2007-10-16 01:24:17 -0700304}
Li Zhong71b0bfe2014-06-11 16:23:38 +0800305#endif
Nathan Fontenotf7e33342013-09-27 10:18:09 -0500306void register_page_bootmem_memmap(unsigned long section_nr,
307 struct page *start_page, unsigned long size)
308{
309}
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700310
Alexey Kardashevskiy8e0861fa2013-08-28 18:37:42 +1000311/*
312 * We do not have access to the sparsemem vmemmap, so we fallback to
313 * walking the list of sparsemem blocks which we already maintain for
314 * the sake of crashdump. In the long run, we might want to maintain
315 * a tree if performance of that linear walk becomes a problem.
316 *
317 * realmode_pfn_to_page functions can fail due to:
318 * 1) As real sparsemem blocks do not lay in RAM continously (they
319 * are in virtual address space which is not available in the real mode),
320 * the requested page struct can be split between blocks so get_page/put_page
321 * may fail.
322 * 2) When huge pages are used, the get_page/put_page API will fail
323 * in real mode as the linked addresses in the page struct are virtual
324 * too.
325 */
326struct page *realmode_pfn_to_page(unsigned long pfn)
327{
328 struct vmemmap_backing *vmem_back;
329 struct page *page;
330 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
331 unsigned long pg_va = (unsigned long) pfn_to_page(pfn);
332
333 for (vmem_back = vmemmap_list; vmem_back; vmem_back = vmem_back->list) {
334 if (pg_va < vmem_back->virt_addr)
335 continue;
336
Li Zhongbd8cb032014-06-11 16:23:36 +0800337 /* After vmemmap_list entry free is possible, need check all */
338 if ((pg_va + sizeof(struct page)) <=
339 (vmem_back->virt_addr + page_size)) {
340 page = (struct page *) (vmem_back->phys + pg_va -
Alexey Kardashevskiy8e0861fa2013-08-28 18:37:42 +1000341 vmem_back->virt_addr);
Li Zhongbd8cb032014-06-11 16:23:36 +0800342 return page;
343 }
Alexey Kardashevskiy8e0861fa2013-08-28 18:37:42 +1000344 }
345
Li Zhongbd8cb032014-06-11 16:23:36 +0800346 /* Probably that page struct is split between real pages */
Alexey Kardashevskiy8e0861fa2013-08-28 18:37:42 +1000347 return NULL;
348}
349EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
350
Aneesh Kumar K.V7e7dc662017-06-28 11:39:28 +0530351#else
Alexey Kardashevskiy8e0861fa2013-08-28 18:37:42 +1000352
353struct page *realmode_pfn_to_page(unsigned long pfn)
354{
355 struct page *page = pfn_to_page(pfn);
356 return page;
357}
358EXPORT_SYMBOL_GPL(realmode_pfn_to_page);
359
Aneesh Kumar K.V7e7dc662017-06-28 11:39:28 +0530360#endif /* CONFIG_SPARSEMEM_VMEMMAP */
Michael Ellerman1a01dc82016-07-26 20:09:30 +1000361
Michael Ellerman4e003742017-10-19 15:08:43 +1100362#ifdef CONFIG_PPC_BOOK3S_64
Michael Ellerman1fd6c022017-10-24 17:48:49 +0200363static bool disable_radix = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
364
Michael Ellermanc610ec62016-07-26 21:29:30 +1000365static int __init parse_disable_radix(char *p)
366{
Michael Ellerman1fd6c022017-10-24 17:48:49 +0200367 bool val;
368
Aneesh Kumar K.Vcec4e9b2018-03-30 17:39:01 +0530369 if (!p)
Michael Ellerman1fd6c022017-10-24 17:48:49 +0200370 val = true;
371 else if (kstrtobool(p, &val))
372 return -EINVAL;
373
374 disable_radix = val;
375
Michael Ellermanc610ec62016-07-26 21:29:30 +1000376 return 0;
377}
378early_param("disable_radix", parse_disable_radix);
379
Paul Mackerras18569c12017-01-30 21:21:34 +1100380/*
Paul Mackerrascc3d2942017-01-30 21:21:36 +1100381 * If we're running under a hypervisor, we need to check the contents of
382 * /chosen/ibm,architecture-vec-5 to see if the hypervisor is willing to do
383 * radix. If not, we clear the radix feature bit so we fall back to hash.
Paul Mackerras18569c12017-01-30 21:21:34 +1100384 */
Michael Ellerman75599522017-08-08 21:44:08 +1000385static void __init early_check_vec5(void)
Paul Mackerras18569c12017-01-30 21:21:34 +1100386{
387 unsigned long root, chosen;
388 int size;
389 const u8 *vec5;
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100390 u8 mmu_supported;
Paul Mackerras18569c12017-01-30 21:21:34 +1100391
392 root = of_get_flat_dt_root();
393 chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100394 if (chosen == -FDT_ERR_NOTFOUND) {
Paul Mackerrascc3d2942017-01-30 21:21:36 +1100395 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100396 return;
397 }
398 vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
399 if (!vec5) {
400 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
401 return;
402 }
403 if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
404 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
405 return;
406 }
407
408 /* Check for supported configuration */
409 mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
410 OV5_FEAT(OV5_MMU_SUPPORT);
411 if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
412 /* Hypervisor only supports radix - check enabled && GTSE */
413 if (!early_radix_enabled()) {
414 pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
415 }
416 if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
417 OV5_FEAT(OV5_RADIX_GTSE))) {
418 pr_warn("WARNING: Hypervisor doesn't support RADIX with GTSE\n");
419 }
420 /* Do radix anyway - the hypervisor said we had to */
421 cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
422 } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
423 /* Hypervisor only supports hash - disable radix */
424 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
425 }
Paul Mackerras18569c12017-01-30 21:21:34 +1100426}
427
Michael Ellerman1a01dc82016-07-26 20:09:30 +1000428void __init mmu_early_init_devtree(void)
429{
Michael Ellermanc610ec62016-07-26 21:29:30 +1000430 /* Disable radix mode based on kernel command line. */
Paul Mackerrasfc36a902017-03-21 12:38:02 +1100431 if (disable_radix)
Aneesh Kumar K.V5a25b6f2016-07-27 13:19:01 +1000432 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000433
Paul Mackerras18569c12017-01-30 21:21:34 +1100434 /*
435 * Check /chosen/ibm,architecture-vec-5 if running as a guest.
436 * When running bare-metal, we can use radix if we like
437 * even though the ibm,architecture-vec-5 property created by
438 * skiboot doesn't have the necessary bits set.
439 */
Suraj Jitindar Singh014d02c2017-02-28 17:03:48 +1100440 if (!(mfmsr() & MSR_HV))
Paul Mackerras18569c12017-01-30 21:21:34 +1100441 early_check_vec5();
442
Aneesh Kumar K.Vb8f1b4f2016-07-23 14:42:35 +0530443 if (early_radix_enabled())
Michael Ellerman2537b092016-07-26 21:55:27 +1000444 radix__early_init_devtree();
445 else
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000446 hash__early_init_devtree();
Michael Ellerman1a01dc82016-07-26 20:09:30 +1000447}
Michael Ellerman4e003742017-10-19 15:08:43 +1100448#endif /* CONFIG_PPC_BOOK3S_64 */