xref: /openbmc/linux/arch/powerpc/mm/book3s64/pgtable.c (revision f33ac92f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
4  */
5 
6 #include <linux/sched.h>
7 #include <linux/mm_types.h>
8 #include <linux/memblock.h>
9 #include <linux/debugfs.h>
10 #include <misc/cxl-base.h>
11 
12 #include <asm/pgalloc.h>
13 #include <asm/tlb.h>
14 #include <asm/trace.h>
15 #include <asm/powernv.h>
16 #include <asm/firmware.h>
17 #include <asm/ultravisor.h>
18 #include <asm/kexec.h>
19 
20 #include <mm/mmu_decl.h>
21 #include <trace/events/thp.h>
22 
23 #include "internal.h"
24 
25 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
26 EXPORT_SYMBOL_GPL(mmu_psize_defs);
27 
28 #ifdef CONFIG_SPARSEMEM_VMEMMAP
29 int mmu_vmemmap_psize = MMU_PAGE_4K;
30 #endif
31 
32 unsigned long __pmd_frag_nr;
33 EXPORT_SYMBOL(__pmd_frag_nr);
34 unsigned long __pmd_frag_size_shift;
35 EXPORT_SYMBOL(__pmd_frag_size_shift);
36 
37 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
38 /*
39  * This is called when relaxing access to a hugepage. It's also called in the page
40  * fault path when we don't hit any of the major fault cases, ie, a minor
41  * update of _PAGE_ACCESSED, _PAGE_DIRTY, etc... The generic code will have
42  * handled those two for us, we additionally deal with missing execute
43  * permission here on some processors
44  */
45 int pmdp_set_access_flags(struct vm_area_struct *vma, unsigned long address,
46 			  pmd_t *pmdp, pmd_t entry, int dirty)
47 {
48 	int changed;
49 #ifdef CONFIG_DEBUG_VM
50 	WARN_ON(!pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
51 	assert_spin_locked(pmd_lockptr(vma->vm_mm, pmdp));
52 #endif
53 	changed = !pmd_same(*(pmdp), entry);
54 	if (changed) {
55 		/*
56 		 * We can use MMU_PAGE_2M here, because only radix
57 		 * path look at the psize.
58 		 */
59 		__ptep_set_access_flags(vma, pmdp_ptep(pmdp),
60 					pmd_pte(entry), address, MMU_PAGE_2M);
61 	}
62 	return changed;
63 }
64 
65 int pmdp_test_and_clear_young(struct vm_area_struct *vma,
66 			      unsigned long address, pmd_t *pmdp)
67 {
68 	return __pmdp_test_and_clear_young(vma->vm_mm, address, pmdp);
69 }
70 /*
71  * set a new huge pmd. We should not be called for updating
72  * an existing pmd entry. That should go via pmd_hugepage_update.
73  */
74 void set_pmd_at(struct mm_struct *mm, unsigned long addr,
75 		pmd_t *pmdp, pmd_t pmd)
76 {
77 #ifdef CONFIG_DEBUG_VM
78 	/*
79 	 * Make sure hardware valid bit is not set. We don't do
80 	 * tlb flush for this update.
81 	 */
82 
83 	WARN_ON(pte_hw_valid(pmd_pte(*pmdp)) && !pte_protnone(pmd_pte(*pmdp)));
84 	assert_spin_locked(pmd_lockptr(mm, pmdp));
85 	WARN_ON(!(pmd_large(pmd)));
86 #endif
87 	trace_hugepage_set_pmd(addr, pmd_val(pmd));
88 	return set_pte_at(mm, addr, pmdp_ptep(pmdp), pmd_pte(pmd));
89 }
90 
91 static void do_serialize(void *arg)
92 {
93 	/* We've taken the IPI, so try to trim the mask while here */
94 	if (radix_enabled()) {
95 		struct mm_struct *mm = arg;
96 		exit_lazy_flush_tlb(mm, false);
97 	}
98 }
99 
100 /*
101  * Serialize against find_current_mm_pte which does lock-less
102  * lookup in page tables with local interrupts disabled. For huge pages
103  * it casts pmd_t to pte_t. Since format of pte_t is different from
104  * pmd_t we want to prevent transit from pmd pointing to page table
105  * to pmd pointing to huge page (and back) while interrupts are disabled.
106  * We clear pmd to possibly replace it with page table pointer in
107  * different code paths. So make sure we wait for the parallel
108  * find_current_mm_pte to finish.
109  */
110 void serialize_against_pte_lookup(struct mm_struct *mm)
111 {
112 	smp_mb();
113 	smp_call_function_many(mm_cpumask(mm), do_serialize, mm, 1);
114 }
115 
116 /*
117  * We use this to invalidate a pmdp entry before switching from a
118  * hugepte to regular pmd entry.
119  */
120 pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
121 		     pmd_t *pmdp)
122 {
123 	unsigned long old_pmd;
124 
125 	old_pmd = pmd_hugepage_update(vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID);
126 	flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
127 	return __pmd(old_pmd);
128 }
129 
130 pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma,
131 				   unsigned long addr, pmd_t *pmdp, int full)
132 {
133 	pmd_t pmd;
134 	VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
135 	VM_BUG_ON((pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) &&
136 		   !pmd_devmap(*pmdp)) || !pmd_present(*pmdp));
137 	pmd = pmdp_huge_get_and_clear(vma->vm_mm, addr, pmdp);
138 	/*
139 	 * if it not a fullmm flush, then we can possibly end up converting
140 	 * this PMD pte entry to a regular level 0 PTE by a parallel page fault.
141 	 * Make sure we flush the tlb in this case.
142 	 */
143 	if (!full)
144 		flush_pmd_tlb_range(vma, addr, addr + HPAGE_PMD_SIZE);
145 	return pmd;
146 }
147 
148 static pmd_t pmd_set_protbits(pmd_t pmd, pgprot_t pgprot)
149 {
150 	return __pmd(pmd_val(pmd) | pgprot_val(pgprot));
151 }
152 
153 /*
154  * At some point we should be able to get rid of
155  * pmd_mkhuge() and mk_huge_pmd() when we update all the
156  * other archs to mark the pmd huge in pfn_pmd()
157  */
158 pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot)
159 {
160 	unsigned long pmdv;
161 
162 	pmdv = (pfn << PAGE_SHIFT) & PTE_RPN_MASK;
163 
164 	return __pmd_mkhuge(pmd_set_protbits(__pmd(pmdv), pgprot));
165 }
166 
167 pmd_t mk_pmd(struct page *page, pgprot_t pgprot)
168 {
169 	return pfn_pmd(page_to_pfn(page), pgprot);
170 }
171 
172 pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
173 {
174 	unsigned long pmdv;
175 
176 	pmdv = pmd_val(pmd);
177 	pmdv &= _HPAGE_CHG_MASK;
178 	return pmd_set_protbits(__pmd(pmdv), newprot);
179 }
180 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
181 
182 /* For use by kexec, called with MMU off */
183 notrace void mmu_cleanup_all(void)
184 {
185 	if (radix_enabled())
186 		radix__mmu_cleanup_all();
187 	else if (mmu_hash_ops.hpte_clear_all)
188 		mmu_hash_ops.hpte_clear_all();
189 
190 	reset_sprs();
191 }
192 
193 #ifdef CONFIG_MEMORY_HOTPLUG
194 int __meminit create_section_mapping(unsigned long start, unsigned long end,
195 				     int nid, pgprot_t prot)
196 {
197 	if (radix_enabled())
198 		return radix__create_section_mapping(start, end, nid, prot);
199 
200 	return hash__create_section_mapping(start, end, nid, prot);
201 }
202 
203 int __meminit remove_section_mapping(unsigned long start, unsigned long end)
204 {
205 	if (radix_enabled())
206 		return radix__remove_section_mapping(start, end);
207 
208 	return hash__remove_section_mapping(start, end);
209 }
210 #endif /* CONFIG_MEMORY_HOTPLUG */
211 
212 void __init mmu_partition_table_init(void)
213 {
214 	unsigned long patb_size = 1UL << PATB_SIZE_SHIFT;
215 	unsigned long ptcr;
216 
217 	/* Initialize the Partition Table with no entries */
218 	partition_tb = memblock_alloc(patb_size, patb_size);
219 	if (!partition_tb)
220 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
221 		      __func__, patb_size, patb_size);
222 
223 	ptcr = __pa(partition_tb) | (PATB_SIZE_SHIFT - 12);
224 	set_ptcr_when_no_uv(ptcr);
225 	powernv_set_nmmu_ptcr(ptcr);
226 }
227 
228 static void flush_partition(unsigned int lpid, bool radix)
229 {
230 	if (radix) {
231 		radix__flush_all_lpid(lpid);
232 		radix__flush_all_lpid_guest(lpid);
233 	} else {
234 		asm volatile("ptesync" : : : "memory");
235 		asm volatile(PPC_TLBIE_5(%0,%1,2,0,0) : :
236 			     "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
237 		/* do we need fixup here ?*/
238 		asm volatile("eieio; tlbsync; ptesync" : : : "memory");
239 		trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 0);
240 	}
241 }
242 
243 void mmu_partition_table_set_entry(unsigned int lpid, unsigned long dw0,
244 				  unsigned long dw1, bool flush)
245 {
246 	unsigned long old = be64_to_cpu(partition_tb[lpid].patb0);
247 
248 	/*
249 	 * When ultravisor is enabled, the partition table is stored in secure
250 	 * memory and can only be accessed doing an ultravisor call. However, we
251 	 * maintain a copy of the partition table in normal memory to allow Nest
252 	 * MMU translations to occur (for normal VMs).
253 	 *
254 	 * Therefore, here we always update partition_tb, regardless of whether
255 	 * we are running under an ultravisor or not.
256 	 */
257 	partition_tb[lpid].patb0 = cpu_to_be64(dw0);
258 	partition_tb[lpid].patb1 = cpu_to_be64(dw1);
259 
260 	/*
261 	 * If ultravisor is enabled, we do an ultravisor call to register the
262 	 * partition table entry (PATE), which also do a global flush of TLBs
263 	 * and partition table caches for the lpid. Otherwise, just do the
264 	 * flush. The type of flush (hash or radix) depends on what the previous
265 	 * use of the partition ID was, not the new use.
266 	 */
267 	if (firmware_has_feature(FW_FEATURE_ULTRAVISOR)) {
268 		uv_register_pate(lpid, dw0, dw1);
269 		pr_info("PATE registered by ultravisor: dw0 = 0x%lx, dw1 = 0x%lx\n",
270 			dw0, dw1);
271 	} else if (flush) {
272 		/*
273 		 * Boot does not need to flush, because MMU is off and each
274 		 * CPU does a tlbiel_all() before switching them on, which
275 		 * flushes everything.
276 		 */
277 		flush_partition(lpid, (old & PATB_HR));
278 	}
279 }
280 EXPORT_SYMBOL_GPL(mmu_partition_table_set_entry);
281 
282 static pmd_t *get_pmd_from_cache(struct mm_struct *mm)
283 {
284 	void *pmd_frag, *ret;
285 
286 	if (PMD_FRAG_NR == 1)
287 		return NULL;
288 
289 	spin_lock(&mm->page_table_lock);
290 	ret = mm->context.pmd_frag;
291 	if (ret) {
292 		pmd_frag = ret + PMD_FRAG_SIZE;
293 		/*
294 		 * If we have taken up all the fragments mark PTE page NULL
295 		 */
296 		if (((unsigned long)pmd_frag & ~PAGE_MASK) == 0)
297 			pmd_frag = NULL;
298 		mm->context.pmd_frag = pmd_frag;
299 	}
300 	spin_unlock(&mm->page_table_lock);
301 	return (pmd_t *)ret;
302 }
303 
304 static pmd_t *__alloc_for_pmdcache(struct mm_struct *mm)
305 {
306 	void *ret = NULL;
307 	struct page *page;
308 	gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO;
309 
310 	if (mm == &init_mm)
311 		gfp &= ~__GFP_ACCOUNT;
312 	page = alloc_page(gfp);
313 	if (!page)
314 		return NULL;
315 	if (!pgtable_pmd_page_ctor(page)) {
316 		__free_pages(page, 0);
317 		return NULL;
318 	}
319 
320 	atomic_set(&page->pt_frag_refcount, 1);
321 
322 	ret = page_address(page);
323 	/*
324 	 * if we support only one fragment just return the
325 	 * allocated page.
326 	 */
327 	if (PMD_FRAG_NR == 1)
328 		return ret;
329 
330 	spin_lock(&mm->page_table_lock);
331 	/*
332 	 * If we find pgtable_page set, we return
333 	 * the allocated page with single fragement
334 	 * count.
335 	 */
336 	if (likely(!mm->context.pmd_frag)) {
337 		atomic_set(&page->pt_frag_refcount, PMD_FRAG_NR);
338 		mm->context.pmd_frag = ret + PMD_FRAG_SIZE;
339 	}
340 	spin_unlock(&mm->page_table_lock);
341 
342 	return (pmd_t *)ret;
343 }
344 
345 pmd_t *pmd_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr)
346 {
347 	pmd_t *pmd;
348 
349 	pmd = get_pmd_from_cache(mm);
350 	if (pmd)
351 		return pmd;
352 
353 	return __alloc_for_pmdcache(mm);
354 }
355 
356 void pmd_fragment_free(unsigned long *pmd)
357 {
358 	struct page *page = virt_to_page(pmd);
359 
360 	if (PageReserved(page))
361 		return free_reserved_page(page);
362 
363 	BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0);
364 	if (atomic_dec_and_test(&page->pt_frag_refcount)) {
365 		pgtable_pmd_page_dtor(page);
366 		__free_page(page);
367 	}
368 }
369 
370 static inline void pgtable_free(void *table, int index)
371 {
372 	switch (index) {
373 	case PTE_INDEX:
374 		pte_fragment_free(table, 0);
375 		break;
376 	case PMD_INDEX:
377 		pmd_fragment_free(table);
378 		break;
379 	case PUD_INDEX:
380 		__pud_free(table);
381 		break;
382 #if defined(CONFIG_PPC_4K_PAGES) && defined(CONFIG_HUGETLB_PAGE)
383 		/* 16M hugepd directory at pud level */
384 	case HTLB_16M_INDEX:
385 		BUILD_BUG_ON(H_16M_CACHE_INDEX <= 0);
386 		kmem_cache_free(PGT_CACHE(H_16M_CACHE_INDEX), table);
387 		break;
388 		/* 16G hugepd directory at the pgd level */
389 	case HTLB_16G_INDEX:
390 		BUILD_BUG_ON(H_16G_CACHE_INDEX <= 0);
391 		kmem_cache_free(PGT_CACHE(H_16G_CACHE_INDEX), table);
392 		break;
393 #endif
394 		/* We don't free pgd table via RCU callback */
395 	default:
396 		BUG();
397 	}
398 }
399 
400 void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
401 {
402 	unsigned long pgf = (unsigned long)table;
403 
404 	BUG_ON(index > MAX_PGTABLE_INDEX_SIZE);
405 	pgf |= index;
406 	tlb_remove_table(tlb, (void *)pgf);
407 }
408 
409 void __tlb_remove_table(void *_table)
410 {
411 	void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE);
412 	unsigned int index = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE;
413 
414 	return pgtable_free(table, index);
415 }
416 
417 #ifdef CONFIG_PROC_FS
418 atomic_long_t direct_pages_count[MMU_PAGE_COUNT];
419 
420 void arch_report_meminfo(struct seq_file *m)
421 {
422 	/*
423 	 * Hash maps the memory with one size mmu_linear_psize.
424 	 * So don't bother to print these on hash
425 	 */
426 	if (!radix_enabled())
427 		return;
428 	seq_printf(m, "DirectMap4k:    %8lu kB\n",
429 		   atomic_long_read(&direct_pages_count[MMU_PAGE_4K]) << 2);
430 	seq_printf(m, "DirectMap64k:    %8lu kB\n",
431 		   atomic_long_read(&direct_pages_count[MMU_PAGE_64K]) << 6);
432 	seq_printf(m, "DirectMap2M:    %8lu kB\n",
433 		   atomic_long_read(&direct_pages_count[MMU_PAGE_2M]) << 11);
434 	seq_printf(m, "DirectMap1G:    %8lu kB\n",
435 		   atomic_long_read(&direct_pages_count[MMU_PAGE_1G]) << 20);
436 }
437 #endif /* CONFIG_PROC_FS */
438 
439 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr,
440 			     pte_t *ptep)
441 {
442 	unsigned long pte_val;
443 
444 	/*
445 	 * Clear the _PAGE_PRESENT so that no hardware parallel update is
446 	 * possible. Also keep the pte_present true so that we don't take
447 	 * wrong fault.
448 	 */
449 	pte_val = pte_update(vma->vm_mm, addr, ptep, _PAGE_PRESENT, _PAGE_INVALID, 0);
450 
451 	return __pte(pte_val);
452 
453 }
454 
455 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
456 			     pte_t *ptep, pte_t old_pte, pte_t pte)
457 {
458 	if (radix_enabled())
459 		return radix__ptep_modify_prot_commit(vma, addr,
460 						      ptep, old_pte, pte);
461 	set_pte_at(vma->vm_mm, addr, ptep, pte);
462 }
463 
464 /*
465  * For hash translation mode, we use the deposited table to store hash slot
466  * information and they are stored at PTRS_PER_PMD offset from related pmd
467  * location. Hence a pmd move requires deposit and withdraw.
468  *
469  * For radix translation with split pmd ptl, we store the deposited table in the
470  * pmd page. Hence if we have different pmd page we need to withdraw during pmd
471  * move.
472  *
473  * With hash we use deposited table always irrespective of anon or not.
474  * With radix we use deposited table only for anonymous mapping.
475  */
476 int pmd_move_must_withdraw(struct spinlock *new_pmd_ptl,
477 			   struct spinlock *old_pmd_ptl,
478 			   struct vm_area_struct *vma)
479 {
480 	if (radix_enabled())
481 		return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
482 
483 	return true;
484 }
485 
486 /*
487  * Does the CPU support tlbie?
488  */
489 bool tlbie_capable __read_mostly = true;
490 EXPORT_SYMBOL(tlbie_capable);
491 
492 /*
493  * Should tlbie be used for management of CPU TLBs, for kernel and process
494  * address spaces? tlbie may still be used for nMMU accelerators, and for KVM
495  * guest address spaces.
496  */
497 bool tlbie_enabled __read_mostly = true;
498 
499 static int __init setup_disable_tlbie(char *str)
500 {
501 	if (!radix_enabled()) {
502 		pr_err("disable_tlbie: Unable to disable TLBIE with Hash MMU.\n");
503 		return 1;
504 	}
505 
506 	tlbie_capable = false;
507 	tlbie_enabled = false;
508 
509         return 1;
510 }
511 __setup("disable_tlbie", setup_disable_tlbie);
512 
513 static int __init pgtable_debugfs_setup(void)
514 {
515 	if (!tlbie_capable)
516 		return 0;
517 
518 	/*
519 	 * There is no locking vs tlb flushing when changing this value.
520 	 * The tlb flushers will see one value or another, and use either
521 	 * tlbie or tlbiel with IPIs. In both cases the TLBs will be
522 	 * invalidated as expected.
523 	 */
524 	debugfs_create_bool("tlbie_enabled", 0600,
525 			arch_debugfs_dir,
526 			&tlbie_enabled);
527 
528 	return 0;
529 }
530 arch_initcall(pgtable_debugfs_setup);
531 
532 #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_ARCH_HAS_MEMREMAP_COMPAT_ALIGN)
533 /*
534  * Override the generic version in mm/memremap.c.
535  *
536  * With hash translation, the direct-map range is mapped with just one
537  * page size selected by htab_init_page_sizes(). Consult
538  * mmu_psize_defs[] to determine the minimum page size alignment.
539 */
540 unsigned long memremap_compat_align(void)
541 {
542 	if (!radix_enabled()) {
543 		unsigned int shift = mmu_psize_defs[mmu_linear_psize].shift;
544 		return max(SUBSECTION_SIZE, 1UL << shift);
545 	}
546 
547 	return SUBSECTION_SIZE;
548 }
549 EXPORT_SYMBOL_GPL(memremap_compat_align);
550 #endif
551