xref: /OK3568_Linux_fs/kernel/arch/csky/mm/asid.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Generic ASID allocator.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Based on arch/arm/mm/context.c
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
8*4882a593Smuzhiyun  * Copyright (C) 2012 ARM Ltd.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/mm_types.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <asm/asid.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define reserved_asid(info, cpu) *per_cpu_ptr((info)->reserved, cpu)
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define ASID_MASK(info)			(~GENMASK((info)->bits - 1, 0))
19*4882a593Smuzhiyun #define ASID_FIRST_VERSION(info)	(1UL << ((info)->bits))
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define asid2idx(info, asid)		(((asid) & ~ASID_MASK(info)) >> (info)->ctxt_shift)
22*4882a593Smuzhiyun #define idx2asid(info, idx)		(((idx) << (info)->ctxt_shift) & ~ASID_MASK(info))
23*4882a593Smuzhiyun 
flush_context(struct asid_info * info)24*4882a593Smuzhiyun static void flush_context(struct asid_info *info)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	int i;
27*4882a593Smuzhiyun 	u64 asid;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	/* Update the list of reserved ASIDs and the ASID bitmap. */
30*4882a593Smuzhiyun 	bitmap_clear(info->map, 0, NUM_CTXT_ASIDS(info));
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	for_each_possible_cpu(i) {
33*4882a593Smuzhiyun 		asid = atomic64_xchg_relaxed(&active_asid(info, i), 0);
34*4882a593Smuzhiyun 		/*
35*4882a593Smuzhiyun 		 * If this CPU has already been through a
36*4882a593Smuzhiyun 		 * rollover, but hasn't run another task in
37*4882a593Smuzhiyun 		 * the meantime, we must preserve its reserved
38*4882a593Smuzhiyun 		 * ASID, as this is the only trace we have of
39*4882a593Smuzhiyun 		 * the process it is still running.
40*4882a593Smuzhiyun 		 */
41*4882a593Smuzhiyun 		if (asid == 0)
42*4882a593Smuzhiyun 			asid = reserved_asid(info, i);
43*4882a593Smuzhiyun 		__set_bit(asid2idx(info, asid), info->map);
44*4882a593Smuzhiyun 		reserved_asid(info, i) = asid;
45*4882a593Smuzhiyun 	}
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	/*
48*4882a593Smuzhiyun 	 * Queue a TLB invalidation for each CPU to perform on next
49*4882a593Smuzhiyun 	 * context-switch
50*4882a593Smuzhiyun 	 */
51*4882a593Smuzhiyun 	cpumask_setall(&info->flush_pending);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
check_update_reserved_asid(struct asid_info * info,u64 asid,u64 newasid)54*4882a593Smuzhiyun static bool check_update_reserved_asid(struct asid_info *info, u64 asid,
55*4882a593Smuzhiyun 				       u64 newasid)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	int cpu;
58*4882a593Smuzhiyun 	bool hit = false;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/*
61*4882a593Smuzhiyun 	 * Iterate over the set of reserved ASIDs looking for a match.
62*4882a593Smuzhiyun 	 * If we find one, then we can update our mm to use newasid
63*4882a593Smuzhiyun 	 * (i.e. the same ASID in the current generation) but we can't
64*4882a593Smuzhiyun 	 * exit the loop early, since we need to ensure that all copies
65*4882a593Smuzhiyun 	 * of the old ASID are updated to reflect the mm. Failure to do
66*4882a593Smuzhiyun 	 * so could result in us missing the reserved ASID in a future
67*4882a593Smuzhiyun 	 * generation.
68*4882a593Smuzhiyun 	 */
69*4882a593Smuzhiyun 	for_each_possible_cpu(cpu) {
70*4882a593Smuzhiyun 		if (reserved_asid(info, cpu) == asid) {
71*4882a593Smuzhiyun 			hit = true;
72*4882a593Smuzhiyun 			reserved_asid(info, cpu) = newasid;
73*4882a593Smuzhiyun 		}
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return hit;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
new_context(struct asid_info * info,atomic64_t * pasid,struct mm_struct * mm)79*4882a593Smuzhiyun static u64 new_context(struct asid_info *info, atomic64_t *pasid,
80*4882a593Smuzhiyun 		       struct mm_struct *mm)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	static u32 cur_idx = 1;
83*4882a593Smuzhiyun 	u64 asid = atomic64_read(pasid);
84*4882a593Smuzhiyun 	u64 generation = atomic64_read(&info->generation);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	if (asid != 0) {
87*4882a593Smuzhiyun 		u64 newasid = generation | (asid & ~ASID_MASK(info));
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 		/*
90*4882a593Smuzhiyun 		 * If our current ASID was active during a rollover, we
91*4882a593Smuzhiyun 		 * can continue to use it and this was just a false alarm.
92*4882a593Smuzhiyun 		 */
93*4882a593Smuzhiyun 		if (check_update_reserved_asid(info, asid, newasid))
94*4882a593Smuzhiyun 			return newasid;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 		/*
97*4882a593Smuzhiyun 		 * We had a valid ASID in a previous life, so try to re-use
98*4882a593Smuzhiyun 		 * it if possible.
99*4882a593Smuzhiyun 		 */
100*4882a593Smuzhiyun 		if (!__test_and_set_bit(asid2idx(info, asid), info->map))
101*4882a593Smuzhiyun 			return newasid;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	/*
105*4882a593Smuzhiyun 	 * Allocate a free ASID. If we can't find one, take a note of the
106*4882a593Smuzhiyun 	 * currently active ASIDs and mark the TLBs as requiring flushes.  We
107*4882a593Smuzhiyun 	 * always count from ASID #2 (index 1), as we use ASID #0 when setting
108*4882a593Smuzhiyun 	 * a reserved TTBR0 for the init_mm and we allocate ASIDs in even/odd
109*4882a593Smuzhiyun 	 * pairs.
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun 	asid = find_next_zero_bit(info->map, NUM_CTXT_ASIDS(info), cur_idx);
112*4882a593Smuzhiyun 	if (asid != NUM_CTXT_ASIDS(info))
113*4882a593Smuzhiyun 		goto set_asid;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	/* We're out of ASIDs, so increment the global generation count */
116*4882a593Smuzhiyun 	generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION(info),
117*4882a593Smuzhiyun 						 &info->generation);
118*4882a593Smuzhiyun 	flush_context(info);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/* We have more ASIDs than CPUs, so this will always succeed */
121*4882a593Smuzhiyun 	asid = find_next_zero_bit(info->map, NUM_CTXT_ASIDS(info), 1);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun set_asid:
124*4882a593Smuzhiyun 	__set_bit(asid, info->map);
125*4882a593Smuzhiyun 	cur_idx = asid;
126*4882a593Smuzhiyun 	cpumask_clear(mm_cpumask(mm));
127*4882a593Smuzhiyun 	return idx2asid(info, asid) | generation;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun  * Generate a new ASID for the context.
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * @pasid: Pointer to the current ASID batch allocated. It will be updated
134*4882a593Smuzhiyun  * with the new ASID batch.
135*4882a593Smuzhiyun  * @cpu: current CPU ID. Must have been acquired through get_cpu()
136*4882a593Smuzhiyun  */
asid_new_context(struct asid_info * info,atomic64_t * pasid,unsigned int cpu,struct mm_struct * mm)137*4882a593Smuzhiyun void asid_new_context(struct asid_info *info, atomic64_t *pasid,
138*4882a593Smuzhiyun 		      unsigned int cpu, struct mm_struct *mm)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	unsigned long flags;
141*4882a593Smuzhiyun 	u64 asid;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&info->lock, flags);
144*4882a593Smuzhiyun 	/* Check that our ASID belongs to the current generation. */
145*4882a593Smuzhiyun 	asid = atomic64_read(pasid);
146*4882a593Smuzhiyun 	if ((asid ^ atomic64_read(&info->generation)) >> info->bits) {
147*4882a593Smuzhiyun 		asid = new_context(info, pasid, mm);
148*4882a593Smuzhiyun 		atomic64_set(pasid, asid);
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (cpumask_test_and_clear_cpu(cpu, &info->flush_pending))
152*4882a593Smuzhiyun 		info->flush_cpu_ctxt_cb();
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	atomic64_set(&active_asid(info, cpu), asid);
155*4882a593Smuzhiyun 	cpumask_set_cpu(cpu, mm_cpumask(mm));
156*4882a593Smuzhiyun 	raw_spin_unlock_irqrestore(&info->lock, flags);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /*
160*4882a593Smuzhiyun  * Initialize the ASID allocator
161*4882a593Smuzhiyun  *
162*4882a593Smuzhiyun  * @info: Pointer to the asid allocator structure
163*4882a593Smuzhiyun  * @bits: Number of ASIDs available
164*4882a593Smuzhiyun  * @asid_per_ctxt: Number of ASIDs to allocate per-context. ASIDs are
165*4882a593Smuzhiyun  * allocated contiguously for a given context. This value should be a power of
166*4882a593Smuzhiyun  * 2.
167*4882a593Smuzhiyun  */
asid_allocator_init(struct asid_info * info,u32 bits,unsigned int asid_per_ctxt,void (* flush_cpu_ctxt_cb)(void))168*4882a593Smuzhiyun int asid_allocator_init(struct asid_info *info,
169*4882a593Smuzhiyun 			u32 bits, unsigned int asid_per_ctxt,
170*4882a593Smuzhiyun 			void (*flush_cpu_ctxt_cb)(void))
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	info->bits = bits;
173*4882a593Smuzhiyun 	info->ctxt_shift = ilog2(asid_per_ctxt);
174*4882a593Smuzhiyun 	info->flush_cpu_ctxt_cb = flush_cpu_ctxt_cb;
175*4882a593Smuzhiyun 	/*
176*4882a593Smuzhiyun 	 * Expect allocation after rollover to fail if we don't have at least
177*4882a593Smuzhiyun 	 * one more ASID than CPUs. ASID #0 is always reserved.
178*4882a593Smuzhiyun 	 */
179*4882a593Smuzhiyun 	WARN_ON(NUM_CTXT_ASIDS(info) - 1 <= num_possible_cpus());
180*4882a593Smuzhiyun 	atomic64_set(&info->generation, ASID_FIRST_VERSION(info));
181*4882a593Smuzhiyun 	info->map = kcalloc(BITS_TO_LONGS(NUM_CTXT_ASIDS(info)),
182*4882a593Smuzhiyun 			    sizeof(*info->map), GFP_KERNEL);
183*4882a593Smuzhiyun 	if (!info->map)
184*4882a593Smuzhiyun 		return -ENOMEM;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	raw_spin_lock_init(&info->lock);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	return 0;
189*4882a593Smuzhiyun }
190