Skip to content

Commit 74cd177

Browse files
SiFiveHollandpalmer-dabbelt
authored andcommitted
riscv: mm: Introduce cntx2asid/cntx2version helper macros
When using the ASID allocator, the MM context ID contains two values: the ASID in the lower bits, and the allocator version number in the remaining bits. Use macros to make this separation more obvious. Reviewed-by: Alexandre Ghiti <[email protected]> Signed-off-by: Samuel Holland <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent d6dcdab commit 74cd177

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

arch/riscv/include/asm/mmu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ typedef struct {
2626
#endif
2727
} mm_context_t;
2828

29+
#define cntx2asid(cntx) ((cntx) & asid_mask)
30+
#define cntx2version(cntx) ((cntx) & ~asid_mask)
31+
2932
void __init create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa,
3033
phys_addr_t sz, pgprot_t prot);
3134
#endif /* __ASSEMBLY__ */

arch/riscv/mm/context.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void __flush_context(void)
8181
if (cntx == 0)
8282
cntx = per_cpu(reserved_context, i);
8383

84-
__set_bit(cntx & asid_mask, context_asid_map);
84+
__set_bit(cntx2asid(cntx), context_asid_map);
8585
per_cpu(reserved_context, i) = cntx;
8686
}
8787

@@ -102,7 +102,7 @@ static unsigned long __new_context(struct mm_struct *mm)
102102
lockdep_assert_held(&context_lock);
103103

104104
if (cntx != 0) {
105-
unsigned long newcntx = ver | (cntx & asid_mask);
105+
unsigned long newcntx = ver | cntx2asid(cntx);
106106

107107
/*
108108
* If our current CONTEXT was active during a rollover, we
@@ -115,7 +115,7 @@ static unsigned long __new_context(struct mm_struct *mm)
115115
* We had a valid CONTEXT in a previous life, so try to
116116
* re-use it if possible.
117117
*/
118-
if (!__test_and_set_bit(cntx & asid_mask, context_asid_map))
118+
if (!__test_and_set_bit(cntx2asid(cntx), context_asid_map))
119119
return newcntx;
120120
}
121121

@@ -168,7 +168,7 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)
168168
*/
169169
old_active_cntx = atomic_long_read(&per_cpu(active_context, cpu));
170170
if (old_active_cntx &&
171-
((cntx & ~asid_mask) == atomic_long_read(&current_version)) &&
171+
(cntx2version(cntx) == atomic_long_read(&current_version)) &&
172172
atomic_long_cmpxchg_relaxed(&per_cpu(active_context, cpu),
173173
old_active_cntx, cntx))
174174
goto switch_mm_fast;
@@ -177,7 +177,7 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)
177177

178178
/* Check that our ASID belongs to the current_version. */
179179
cntx = atomic_long_read(&mm->context.id);
180-
if ((cntx & ~asid_mask) != atomic_long_read(&current_version)) {
180+
if (cntx2version(cntx) != atomic_long_read(&current_version)) {
181181
cntx = __new_context(mm);
182182
atomic_long_set(&mm->context.id, cntx);
183183
}
@@ -191,7 +191,7 @@ static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)
191191

192192
switch_mm_fast:
193193
csr_write(CSR_SATP, virt_to_pfn(mm->pgd) |
194-
((cntx & asid_mask) << SATP_ASID_SHIFT) |
194+
(cntx2asid(cntx) << SATP_ASID_SHIFT) |
195195
satp_mode);
196196

197197
if (need_flush_tlb)

arch/riscv/mm/tlbflush.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static void __flush_tlb_range(struct cpumask *cmask, unsigned long asid,
110110
static inline unsigned long get_mm_asid(struct mm_struct *mm)
111111
{
112112
return static_branch_unlikely(&use_asid_allocator) ?
113-
atomic_long_read(&mm->context.id) & asid_mask : FLUSH_TLB_NO_ASID;
113+
cntx2asid(atomic_long_read(&mm->context.id)) : FLUSH_TLB_NO_ASID;
114114
}
115115

116116
void flush_tlb_mm(struct mm_struct *mm)

0 commit comments

Comments
 (0)