xref: /OK3568_Linux_fs/kernel/arch/hexagon/include/asm/pgalloc.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Page table support for the Hexagon architecture
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef _ASM_PGALLOC_H
9*4882a593Smuzhiyun #define _ASM_PGALLOC_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <asm/mem-layout.h>
12*4882a593Smuzhiyun #include <asm/atomic.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <asm-generic/pgalloc.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun extern unsigned long long kmap_generation;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * Page table creation interface
20*4882a593Smuzhiyun  */
pgd_alloc(struct mm_struct * mm)21*4882a593Smuzhiyun static inline pgd_t *pgd_alloc(struct mm_struct *mm)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun 	pgd_t *pgd;
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 	pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	/*
28*4882a593Smuzhiyun 	 * There may be better ways to do this, but to ensure
29*4882a593Smuzhiyun 	 * that new address spaces always contain the kernel
30*4882a593Smuzhiyun 	 * base mapping, and to ensure that the user area is
31*4882a593Smuzhiyun 	 * initially marked invalid, initialize the new map
32*4882a593Smuzhiyun 	 * map with a copy of the kernel's persistent map.
33*4882a593Smuzhiyun 	 */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	memcpy(pgd, swapper_pg_dir, PTRS_PER_PGD*sizeof(pgd_t));
36*4882a593Smuzhiyun 	mm->context.generation = kmap_generation;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	/* Physical version is what is passed to virtual machine on switch */
39*4882a593Smuzhiyun 	mm->context.ptbase = __pa(pgd);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	return pgd;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
pmd_populate(struct mm_struct * mm,pmd_t * pmd,pgtable_t pte)44*4882a593Smuzhiyun static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
45*4882a593Smuzhiyun 				pgtable_t pte)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	/*
48*4882a593Smuzhiyun 	 * Conveniently, zero in 3 LSB means indirect 4K page table.
49*4882a593Smuzhiyun 	 * Not so convenient when you're trying to vary the page size.
50*4882a593Smuzhiyun 	 */
51*4882a593Smuzhiyun 	set_pmd(pmd, __pmd(((unsigned long)page_to_pfn(pte) << PAGE_SHIFT) |
52*4882a593Smuzhiyun 		HEXAGON_L1_PTE_SIZE));
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * Other architectures seem to have ways of making all processes
57*4882a593Smuzhiyun  * share the same pmd's for their kernel mappings, but the v0.3
58*4882a593Smuzhiyun  * Hexagon VM spec has a "monolithic" L1 table for user and kernel
59*4882a593Smuzhiyun  * segments.  We track "generations" of the kernel map to minimize
60*4882a593Smuzhiyun  * overhead, and update the "slave" copies of the kernel mappings
61*4882a593Smuzhiyun  * as part of switch_mm.  However, we still need to update the
62*4882a593Smuzhiyun  * kernel map of the active thread who's calling pmd_populate_kernel...
63*4882a593Smuzhiyun  */
pmd_populate_kernel(struct mm_struct * mm,pmd_t * pmd,pte_t * pte)64*4882a593Smuzhiyun static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
65*4882a593Smuzhiyun 				       pte_t *pte)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	extern spinlock_t kmap_gen_lock;
68*4882a593Smuzhiyun 	pmd_t *ppmd;
69*4882a593Smuzhiyun 	int pmdindex;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	spin_lock(&kmap_gen_lock);
72*4882a593Smuzhiyun 	kmap_generation++;
73*4882a593Smuzhiyun 	mm->context.generation = kmap_generation;
74*4882a593Smuzhiyun 	current->active_mm->context.generation = kmap_generation;
75*4882a593Smuzhiyun 	spin_unlock(&kmap_gen_lock);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	set_pmd(pmd, __pmd(((unsigned long)__pa(pte)) | HEXAGON_L1_PTE_SIZE));
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/*
80*4882a593Smuzhiyun 	 * Now the "slave" copy of the current thread.
81*4882a593Smuzhiyun 	 * This is pointer arithmetic, not byte addresses!
82*4882a593Smuzhiyun 	 */
83*4882a593Smuzhiyun 	pmdindex = (pgd_t *)pmd - mm->pgd;
84*4882a593Smuzhiyun 	ppmd = (pmd_t *)current->active_mm->pgd + pmdindex;
85*4882a593Smuzhiyun 	set_pmd(ppmd, __pmd(((unsigned long)__pa(pte)) | HEXAGON_L1_PTE_SIZE));
86*4882a593Smuzhiyun 	if (pmdindex > max_kernel_seg)
87*4882a593Smuzhiyun 		max_kernel_seg = pmdindex;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #define __pte_free_tlb(tlb, pte, addr)		\
91*4882a593Smuzhiyun do {						\
92*4882a593Smuzhiyun 	pgtable_pte_page_dtor((pte));		\
93*4882a593Smuzhiyun 	tlb_remove_page((tlb), (pte));		\
94*4882a593Smuzhiyun } while (0)
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun #endif
97