1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#undef DEBUG
23
24#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>
37#include <linux/highmem.h>
38#include <linux/idr.h>
39#include <linux/nodemask.h>
40#include <linux/module.h>
41#include <linux/poison.h>
42#include <linux/memblock.h>
43#include <linux/hugetlb.h>
44#include <linux/slab.h>
45#include <linux/of_fdt.h>
46#include <linux/libfdt.h>
47#include <linux/memremap.h>
48
49#include <asm/pgalloc.h>
50#include <asm/page.h>
51#include <asm/prom.h>
52#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>
57#include <linux/uaccess.h>
58#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>
65#include <asm/sections.h>
66#include <asm/iommu.h>
67#include <asm/vdso.h>
68
69#include <mm/mmu_decl.h>
70
71phys_addr_t memstart_addr = ~0;
72EXPORT_SYMBOL_GPL(memstart_addr);
73phys_addr_t kernstart_addr;
74EXPORT_SYMBOL_GPL(kernstart_addr);
75
76#ifdef CONFIG_SPARSEMEM_VMEMMAP
77
78
79
80
81
82
83static unsigned long __meminit vmemmap_section_start(unsigned long page)
84{
85 unsigned long offset = page - ((unsigned long)(vmemmap));
86
87
88 return (offset / sizeof(struct page)) & PAGE_SECTION_MASK;
89}
90
91
92
93
94
95
96static int __meminit vmemmap_populated(unsigned long start, int page_size)
97{
98 unsigned long end = start + page_size;
99 start = (unsigned long)(pfn_to_page(vmemmap_section_start(start)));
100
101 for (; start < end; start += (PAGES_PER_SECTION * sizeof(struct page)))
102 if (pfn_valid(page_to_pfn((struct page *)start)))
103 return 1;
104
105 return 0;
106}
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121struct vmemmap_backing *vmemmap_list;
122static struct vmemmap_backing *next;
123
124
125
126
127
128
129
130
131static int num_left;
132static int num_freed;
133
134static __meminit struct vmemmap_backing * vmemmap_list_alloc(int node)
135{
136 struct vmemmap_backing *vmem_back;
137
138 if (num_freed) {
139 num_freed--;
140 vmem_back = next;
141 next = next->list;
142
143 return vmem_back;
144 }
145
146
147 if (!num_left) {
148 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
180static bool altmap_cross_boundary(struct vmem_altmap *altmap, unsigned long start,
181 unsigned long page_size)
182{
183 unsigned long nr_pfn = page_size / sizeof(struct page);
184 unsigned long start_pfn = page_to_pfn((struct page *)start);
185
186 if ((start_pfn + nr_pfn) > altmap->end_pfn)
187 return true;
188
189 if (start_pfn < altmap->base_pfn)
190 return true;
191
192 return false;
193}
194
195int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
196 struct vmem_altmap *altmap)
197{
198 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
199
200
201 start = ALIGN_DOWN(start, page_size);
202
203 pr_debug("vmemmap_populate %lx..%lx, node %d\n", start, end, node);
204
205 for (; start < end; start += page_size) {
206 void *p = NULL;
207 int rc;
208
209 if (vmemmap_populated(start, page_size))
210 continue;
211
212
213
214
215
216
217 if (altmap && !altmap_cross_boundary(altmap, start, page_size)) {
218 p = altmap_alloc_block_buf(page_size, altmap);
219 if (!p)
220 pr_debug("altmap block allocation failed, falling back to system memory");
221 }
222 if (!p)
223 p = vmemmap_alloc_block_buf(page_size, node);
224 if (!p)
225 return -ENOMEM;
226
227 vmemmap_list_populate(__pa(p), start, node);
228
229 pr_debug(" * %016lx..%016lx allocated at %p\n",
230 start, start + page_size, p);
231
232 rc = vmemmap_create_mapping(start, page_size, __pa(p));
233 if (rc < 0) {
234 pr_warn("%s: Unable to create vmemmap mapping: %d\n",
235 __func__, rc);
236 return -EFAULT;
237 }
238 }
239
240 return 0;
241}
242
243#ifdef CONFIG_MEMORY_HOTPLUG
244static unsigned long vmemmap_list_free(unsigned long start)
245{
246 struct vmemmap_backing *vmem_back, *vmem_back_prev;
247
248 vmem_back_prev = vmem_back = vmemmap_list;
249
250
251 for (; vmem_back; vmem_back = vmem_back->list) {
252 if (vmem_back->virt_addr == start)
253 break;
254 vmem_back_prev = vmem_back;
255 }
256
257 if (unlikely(!vmem_back)) {
258 WARN_ON(1);
259 return 0;
260 }
261
262
263 if (vmem_back == vmemmap_list)
264 vmemmap_list = vmem_back->list;
265 else
266 vmem_back_prev->list = vmem_back->list;
267
268
269 vmem_back->list = next;
270 next = vmem_back;
271 num_freed++;
272
273 return vmem_back->phys;
274}
275
276void __ref vmemmap_free(unsigned long start, unsigned long end,
277 struct vmem_altmap *altmap)
278{
279 unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
280 unsigned long page_order = get_order(page_size);
281 unsigned long alt_start = ~0, alt_end = ~0;
282 unsigned long base_pfn;
283
284 start = ALIGN_DOWN(start, page_size);
285 if (altmap) {
286 alt_start = altmap->base_pfn;
287 alt_end = altmap->base_pfn + altmap->reserve +
288 altmap->free + altmap->alloc + altmap->align;
289 }
290
291 pr_debug("vmemmap_free %lx...%lx\n", start, end);
292
293 for (; start < end; start += page_size) {
294 unsigned long nr_pages, addr;
295 struct page *section_base;
296 struct page *page;
297
298
299
300
301
302
303 if (vmemmap_populated(start, page_size))
304 continue;
305
306 addr = vmemmap_list_free(start);
307 if (!addr)
308 continue;
309
310 page = pfn_to_page(addr >> PAGE_SHIFT);
311 section_base = pfn_to_page(vmemmap_section_start(start));
312 nr_pages = 1 << page_order;
313 base_pfn = PHYS_PFN(addr);
314
315 if (base_pfn >= alt_start && base_pfn < alt_end) {
316 vmem_altmap_free(altmap, nr_pages);
317 } else if (PageReserved(page)) {
318
319 if (page_size < PAGE_SIZE) {
320
321
322
323
324 WARN_ON_ONCE(1);
325 } else {
326 while (nr_pages--)
327 free_reserved_page(page++);
328 }
329 } else {
330 free_pages((unsigned long)(__va(addr)), page_order);
331 }
332
333 vmemmap_remove_mapping(start, page_size);
334 }
335}
336#endif
337void register_page_bootmem_memmap(unsigned long section_nr,
338 struct page *start_page, unsigned long size)
339{
340}
341
342#endif
343
344#ifdef CONFIG_PPC_BOOK3S_64
345static bool disable_radix = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
346
347static int __init parse_disable_radix(char *p)
348{
349 bool val;
350
351 if (!p)
352 val = true;
353 else if (kstrtobool(p, &val))
354 return -EINVAL;
355
356 disable_radix = val;
357
358 return 0;
359}
360early_param("disable_radix", parse_disable_radix);
361
362
363
364
365
366
367static void __init early_check_vec5(void)
368{
369 unsigned long root, chosen;
370 int size;
371 const u8 *vec5;
372 u8 mmu_supported;
373
374 root = of_get_flat_dt_root();
375 chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
376 if (chosen == -FDT_ERR_NOTFOUND) {
377 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
378 return;
379 }
380 vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
381 if (!vec5) {
382 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
383 return;
384 }
385 if (size <= OV5_INDX(OV5_MMU_SUPPORT)) {
386 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
387 return;
388 }
389
390
391 mmu_supported = vec5[OV5_INDX(OV5_MMU_SUPPORT)] &
392 OV5_FEAT(OV5_MMU_SUPPORT);
393 if (mmu_supported == OV5_FEAT(OV5_MMU_RADIX)) {
394
395 if (!early_radix_enabled()) {
396 pr_warn("WARNING: Ignoring cmdline option disable_radix\n");
397 }
398 if (!(vec5[OV5_INDX(OV5_RADIX_GTSE)] &
399 OV5_FEAT(OV5_RADIX_GTSE))) {
400 cur_cpu_spec->mmu_features &= ~MMU_FTR_GTSE;
401 } else
402 cur_cpu_spec->mmu_features |= MMU_FTR_GTSE;
403
404 cur_cpu_spec->mmu_features |= MMU_FTR_TYPE_RADIX;
405 } else if (mmu_supported == OV5_FEAT(OV5_MMU_HASH)) {
406
407 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
408 cur_cpu_spec->mmu_features &= ~MMU_FTR_GTSE;
409 }
410}
411
412void __init mmu_early_init_devtree(void)
413{
414
415 if (disable_radix)
416 cur_cpu_spec->mmu_features &= ~MMU_FTR_TYPE_RADIX;
417
418
419
420
421
422
423
424 if (!(mfmsr() & MSR_HV))
425 early_check_vec5();
426
427 if (early_radix_enabled()) {
428 radix__early_init_devtree();
429
430
431
432
433
434 ppc64_rma_size = ULONG_MAX;
435 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
436 } else
437 hash__early_init_devtree();
438}
439#endif
440