xref: /linux/arch/s390/include/asm/mmu_context.h (revision 527618abb92793b9d4dba548d55822dcebd95317)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c6557e7fSMartin Schwidefsky /*
3c6557e7fSMartin Schwidefsky  *  S390 version
4c6557e7fSMartin Schwidefsky  *
5c6557e7fSMartin Schwidefsky  *  Derived from "include/asm-i386/mmu_context.h"
6c6557e7fSMartin Schwidefsky  */
7c6557e7fSMartin Schwidefsky 
8c6557e7fSMartin Schwidefsky #ifndef __S390_MMU_CONTEXT_H
9c6557e7fSMartin Schwidefsky #define __S390_MMU_CONTEXT_H
10c6557e7fSMartin Schwidefsky 
11c6557e7fSMartin Schwidefsky #include <asm/pgalloc.h>
127c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
13589ee628SIngo Molnar #include <linux/mm_types.h>
14050eef36SMartin Schwidefsky #include <asm/tlbflush.h>
15ebe1cd53SHeiko Carstens #include <asm/ctlreg.h>
168e58ab5cSMartin Schwidefsky #include <asm-generic/mm_hooks.h>
17c6557e7fSMartin Schwidefsky 
1893e2dfd3SNicholas Piggin #define init_new_context init_new_context
19c6557e7fSMartin Schwidefsky static inline int init_new_context(struct task_struct *tsk,
20c6557e7fSMartin Schwidefsky 				   struct mm_struct *mm)
21c6557e7fSMartin Schwidefsky {
221058c163SAlexander Gordeev 	unsigned long asce_type, init_entry;
231058c163SAlexander Gordeev 
2460f07c8eSMartin Schwidefsky 	spin_lock_init(&mm->context.lock);
253446c13bSMartin Schwidefsky 	INIT_LIST_HEAD(&mm->context.pgtable_list);
263446c13bSMartin Schwidefsky 	INIT_LIST_HEAD(&mm->context.gmap_list);
271b948d6cSMartin Schwidefsky 	cpumask_clear(&mm->context.cpu_attach_mask);
2864f31d58SMartin Schwidefsky 	atomic_set(&mm->context.flush_count, 0);
2907fbdf7fSClaudio Imbrenda 	atomic_set(&mm->context.protected_count, 0);
3044b6cc81SMartin Schwidefsky 	mm->context.gmap_asce = 0;
31050eef36SMartin Schwidefsky 	mm->context.flush_mm = 0;
320b46e0a3SMartin Schwidefsky #ifdef CONFIG_PGSTE
3323fefe11SMartin Schwidefsky 	mm->context.alloc_pgste = page_table_allocate_pgste ||
3423fefe11SMartin Schwidefsky 		test_thread_flag(TIF_PGSTE) ||
3553c4ab70SMartin Schwidefsky 		(current->mm && current->mm->context.alloc_pgste);
36250cf776SChristian Borntraeger 	mm->context.has_pgste = 0;
3755531b74SJanosch Frank 	mm->context.uses_skeys = 0;
38c9f0a2b8SJanosch Frank 	mm->context.uses_cmm = 0;
39a9e00d83SJanosch Frank 	mm->context.allow_gmap_hpage_1m = 0;
400b46e0a3SMartin Schwidefsky #endif
41723cacbdSGerald Schaefer 	switch (mm->context.asce_limit) {
421058c163SAlexander Gordeev 	default:
43723cacbdSGerald Schaefer 		/*
441058c163SAlexander Gordeev 		 * context created by exec, the value of asce_limit can
451058c163SAlexander Gordeev 		 * only be zero in this case
46723cacbdSGerald Schaefer 		 */
471058c163SAlexander Gordeev 		VM_BUG_ON(mm->context.asce_limit);
481058c163SAlexander Gordeev 		/* continue as 3-level task */
49f7555608SAlexander Gordeev 		mm->context.asce_limit = _REGION2_SIZE;
501058c163SAlexander Gordeev 		fallthrough;
511058c163SAlexander Gordeev 	case _REGION2_SIZE:
521058c163SAlexander Gordeev 		/* forked 3-level task */
531058c163SAlexander Gordeev 		init_entry = _REGION3_ENTRY_EMPTY;
541058c163SAlexander Gordeev 		asce_type = _ASCE_TYPE_REGION3;
55723cacbdSGerald Schaefer 		break;
56f7555608SAlexander Gordeev 	case TASK_SIZE_MAX:
571058c163SAlexander Gordeev 		/* forked 5-level task */
581058c163SAlexander Gordeev 		init_entry = _REGION1_ENTRY_EMPTY;
591058c163SAlexander Gordeev 		asce_type = _ASCE_TYPE_REGION1;
600b89ede6SMartin Schwidefsky 		break;
61f1c1174fSHeiko Carstens 	case _REGION1_SIZE:
621058c163SAlexander Gordeev 		/* forked 4-level task */
631058c163SAlexander Gordeev 		init_entry = _REGION2_ENTRY_EMPTY;
641058c163SAlexander Gordeev 		asce_type = _ASCE_TYPE_REGION2;
65723cacbdSGerald Schaefer 		break;
663446c13bSMartin Schwidefsky 	}
671058c163SAlexander Gordeev 	mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
681058c163SAlexander Gordeev 			   _ASCE_USER_BITS | asce_type;
691058c163SAlexander Gordeev 	crst_table_init((unsigned long *) mm->pgd, init_entry);
70c6557e7fSMartin Schwidefsky 	return 0;
71c6557e7fSMartin Schwidefsky }
72c6557e7fSMartin Schwidefsky 
73bdb8c935SAlexander Gordeev static inline void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
74c6557e7fSMartin Schwidefsky 				      struct task_struct *tsk)
75c6557e7fSMartin Schwidefsky {
7653e857f3SMartin Schwidefsky 	int cpu = smp_processor_id();
7753e857f3SMartin Schwidefsky 
78b4d70a61SHeiko Carstens 	if (next == &init_mm)
79b4d70a61SHeiko Carstens 		S390_lowcore.user_asce = s390_invalid_asce;
80b4d70a61SHeiko Carstens 	else
81*527618abSHeiko Carstens 		S390_lowcore.user_asce.val = next->context.asce;
821b948d6cSMartin Schwidefsky 	cpumask_set_cpu(cpu, &next->context.cpu_attach_mask);
8387d59863SHeiko Carstens 	/* Clear previous user-ASCE from CR7 */
842372d391SHeiko Carstens 	local_ctl_load(7, &s390_invalid_asce);
85a3866208SMartin Schwidefsky 	if (prev != next)
861b948d6cSMartin Schwidefsky 		cpumask_clear_cpu(cpu, &prev->context.cpu_attach_mask);
8753e857f3SMartin Schwidefsky }
88bdb8c935SAlexander Gordeev #define switch_mm_irqs_off switch_mm_irqs_off
89bdb8c935SAlexander Gordeev 
90bdb8c935SAlexander Gordeev static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
91bdb8c935SAlexander Gordeev 			     struct task_struct *tsk)
92bdb8c935SAlexander Gordeev {
93bdb8c935SAlexander Gordeev 	unsigned long flags;
94bdb8c935SAlexander Gordeev 
95bdb8c935SAlexander Gordeev 	local_irq_save(flags);
96bdb8c935SAlexander Gordeev 	switch_mm_irqs_off(prev, next, tsk);
97bdb8c935SAlexander Gordeev 	local_irq_restore(flags);
98bdb8c935SAlexander Gordeev }
9953e857f3SMartin Schwidefsky 
10053e857f3SMartin Schwidefsky #define finish_arch_post_lock_switch finish_arch_post_lock_switch
10153e857f3SMartin Schwidefsky static inline void finish_arch_post_lock_switch(void)
10253e857f3SMartin Schwidefsky {
10353e857f3SMartin Schwidefsky 	struct task_struct *tsk = current;
10453e857f3SMartin Schwidefsky 	struct mm_struct *mm = tsk->mm;
10553e857f3SMartin Schwidefsky 
106f8b13505SMartin Schwidefsky 	if (mm) {
10753e857f3SMartin Schwidefsky 		preempt_disable();
10864f31d58SMartin Schwidefsky 		while (atomic_read(&mm->context.flush_count))
10953e857f3SMartin Schwidefsky 			cpu_relax();
110b3e5dc45SMartin Schwidefsky 		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
11160f07c8eSMartin Schwidefsky 		__tlb_flush_mm_lazy(mm);
11253e857f3SMartin Schwidefsky 		preempt_enable();
113c6557e7fSMartin Schwidefsky 	}
1142372d391SHeiko Carstens 	local_ctl_load(7, &S390_lowcore.user_asce);
115f8b13505SMartin Schwidefsky }
116c6557e7fSMartin Schwidefsky 
11793e2dfd3SNicholas Piggin #define activate_mm activate_mm
118c6557e7fSMartin Schwidefsky static inline void activate_mm(struct mm_struct *prev,
119c6557e7fSMartin Schwidefsky                                struct mm_struct *next)
120c6557e7fSMartin Schwidefsky {
121c6557e7fSMartin Schwidefsky 	switch_mm(prev, next, current);
122b3e5dc45SMartin Schwidefsky 	cpumask_set_cpu(smp_processor_id(), mm_cpumask(next));
1232372d391SHeiko Carstens 	local_ctl_load(7, &S390_lowcore.user_asce);
124c6557e7fSMartin Schwidefsky }
125c6557e7fSMartin Schwidefsky 
12693e2dfd3SNicholas Piggin #include <asm-generic/mmu_context.h>
12793e2dfd3SNicholas Piggin 
128c6557e7fSMartin Schwidefsky #endif /* __S390_MMU_CONTEXT_H */
129