18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Page table support for the Hexagon architecture
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _ASM_PGALLOC_H
98c2ecf20Sopenharmony_ci#define _ASM_PGALLOC_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/mem-layout.h>
128c2ecf20Sopenharmony_ci#include <asm/atomic.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm-generic/pgalloc.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciextern unsigned long long kmap_generation;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * Page table creation interface
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cistatic inline pgd_t *pgd_alloc(struct mm_struct *mm)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	pgd_t *pgd;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	/*
288c2ecf20Sopenharmony_ci	 * There may be better ways to do this, but to ensure
298c2ecf20Sopenharmony_ci	 * that new address spaces always contain the kernel
308c2ecf20Sopenharmony_ci	 * base mapping, and to ensure that the user area is
318c2ecf20Sopenharmony_ci	 * initially marked invalid, initialize the new map
328c2ecf20Sopenharmony_ci	 * map with a copy of the kernel's persistent map.
338c2ecf20Sopenharmony_ci	 */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	memcpy(pgd, swapper_pg_dir, PTRS_PER_PGD*sizeof(pgd_t));
368c2ecf20Sopenharmony_ci	mm->context.generation = kmap_generation;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* Physical version is what is passed to virtual machine on switch */
398c2ecf20Sopenharmony_ci	mm->context.ptbase = __pa(pgd);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	return pgd;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
458c2ecf20Sopenharmony_ci				pgtable_t pte)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	/*
488c2ecf20Sopenharmony_ci	 * Conveniently, zero in 3 LSB means indirect 4K page table.
498c2ecf20Sopenharmony_ci	 * Not so convenient when you're trying to vary the page size.
508c2ecf20Sopenharmony_ci	 */
518c2ecf20Sopenharmony_ci	set_pmd(pmd, __pmd(((unsigned long)page_to_pfn(pte) << PAGE_SHIFT) |
528c2ecf20Sopenharmony_ci		HEXAGON_L1_PTE_SIZE));
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * Other architectures seem to have ways of making all processes
578c2ecf20Sopenharmony_ci * share the same pmd's for their kernel mappings, but the v0.3
588c2ecf20Sopenharmony_ci * Hexagon VM spec has a "monolithic" L1 table for user and kernel
598c2ecf20Sopenharmony_ci * segments.  We track "generations" of the kernel map to minimize
608c2ecf20Sopenharmony_ci * overhead, and update the "slave" copies of the kernel mappings
618c2ecf20Sopenharmony_ci * as part of switch_mm.  However, we still need to update the
628c2ecf20Sopenharmony_ci * kernel map of the active thread who's calling pmd_populate_kernel...
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_cistatic inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
658c2ecf20Sopenharmony_ci				       pte_t *pte)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	extern spinlock_t kmap_gen_lock;
688c2ecf20Sopenharmony_ci	pmd_t *ppmd;
698c2ecf20Sopenharmony_ci	int pmdindex;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	spin_lock(&kmap_gen_lock);
728c2ecf20Sopenharmony_ci	kmap_generation++;
738c2ecf20Sopenharmony_ci	mm->context.generation = kmap_generation;
748c2ecf20Sopenharmony_ci	current->active_mm->context.generation = kmap_generation;
758c2ecf20Sopenharmony_ci	spin_unlock(&kmap_gen_lock);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	set_pmd(pmd, __pmd(((unsigned long)__pa(pte)) | HEXAGON_L1_PTE_SIZE));
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/*
808c2ecf20Sopenharmony_ci	 * Now the "slave" copy of the current thread.
818c2ecf20Sopenharmony_ci	 * This is pointer arithmetic, not byte addresses!
828c2ecf20Sopenharmony_ci	 */
838c2ecf20Sopenharmony_ci	pmdindex = (pgd_t *)pmd - mm->pgd;
848c2ecf20Sopenharmony_ci	ppmd = (pmd_t *)current->active_mm->pgd + pmdindex;
858c2ecf20Sopenharmony_ci	set_pmd(ppmd, __pmd(((unsigned long)__pa(pte)) | HEXAGON_L1_PTE_SIZE));
868c2ecf20Sopenharmony_ci	if (pmdindex > max_kernel_seg)
878c2ecf20Sopenharmony_ci		max_kernel_seg = pmdindex;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define __pte_free_tlb(tlb, pte, addr)		\
918c2ecf20Sopenharmony_cido {						\
928c2ecf20Sopenharmony_ci	pgtable_pte_page_dtor((pte));		\
938c2ecf20Sopenharmony_ci	tlb_remove_page((tlb), (pte));		\
948c2ecf20Sopenharmony_ci} while (0)
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#endif
97