18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __ASM_ASM_ASID_H
38c2ecf20Sopenharmony_ci#define __ASM_ASM_ASID_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/atomic.h>
68c2ecf20Sopenharmony_ci#include <linux/compiler.h>
78c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
88c2ecf20Sopenharmony_ci#include <linux/percpu.h>
98c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistruct asid_info
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	atomic64_t	generation;
148c2ecf20Sopenharmony_ci	unsigned long	*map;
158c2ecf20Sopenharmony_ci	atomic64_t __percpu	*active;
168c2ecf20Sopenharmony_ci	u64 __percpu		*reserved;
178c2ecf20Sopenharmony_ci	u32			bits;
188c2ecf20Sopenharmony_ci	/* Lock protecting the structure */
198c2ecf20Sopenharmony_ci	raw_spinlock_t		lock;
208c2ecf20Sopenharmony_ci	/* Which CPU requires context flush on next call */
218c2ecf20Sopenharmony_ci	cpumask_t		flush_pending;
228c2ecf20Sopenharmony_ci	/* Number of ASID allocated by context (shift value) */
238c2ecf20Sopenharmony_ci	unsigned int		ctxt_shift;
248c2ecf20Sopenharmony_ci	/* Callback to locally flush the context. */
258c2ecf20Sopenharmony_ci	void			(*flush_cpu_ctxt_cb)(void);
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define NUM_ASIDS(info)			(1UL << ((info)->bits))
298c2ecf20Sopenharmony_ci#define NUM_CTXT_ASIDS(info)		(NUM_ASIDS(info) >> (info)->ctxt_shift)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define active_asid(info, cpu)	*per_cpu_ptr((info)->active, cpu)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_civoid asid_new_context(struct asid_info *info, atomic64_t *pasid,
348c2ecf20Sopenharmony_ci		      unsigned int cpu, struct mm_struct *mm);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Check the ASID is still valid for the context. If not generate a new ASID.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * @pasid: Pointer to the current ASID batch
408c2ecf20Sopenharmony_ci * @cpu: current CPU ID. Must have been acquired throught get_cpu()
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_cistatic inline void asid_check_context(struct asid_info *info,
438c2ecf20Sopenharmony_ci				      atomic64_t *pasid, unsigned int cpu,
448c2ecf20Sopenharmony_ci				      struct mm_struct *mm)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	u64 asid, old_active_asid;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	asid = atomic64_read(pasid);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	/*
518c2ecf20Sopenharmony_ci	 * The memory ordering here is subtle.
528c2ecf20Sopenharmony_ci	 * If our active_asid is non-zero and the ASID matches the current
538c2ecf20Sopenharmony_ci	 * generation, then we update the active_asid entry with a relaxed
548c2ecf20Sopenharmony_ci	 * cmpxchg. Racing with a concurrent rollover means that either:
558c2ecf20Sopenharmony_ci	 *
568c2ecf20Sopenharmony_ci	 * - We get a zero back from the cmpxchg and end up waiting on the
578c2ecf20Sopenharmony_ci	 *   lock. Taking the lock synchronises with the rollover and so
588c2ecf20Sopenharmony_ci	 *   we are forced to see the updated generation.
598c2ecf20Sopenharmony_ci	 *
608c2ecf20Sopenharmony_ci	 * - We get a valid ASID back from the cmpxchg, which means the
618c2ecf20Sopenharmony_ci	 *   relaxed xchg in flush_context will treat us as reserved
628c2ecf20Sopenharmony_ci	 *   because atomic RmWs are totally ordered for a given location.
638c2ecf20Sopenharmony_ci	 */
648c2ecf20Sopenharmony_ci	old_active_asid = atomic64_read(&active_asid(info, cpu));
658c2ecf20Sopenharmony_ci	if (old_active_asid &&
668c2ecf20Sopenharmony_ci	    !((asid ^ atomic64_read(&info->generation)) >> info->bits) &&
678c2ecf20Sopenharmony_ci	    atomic64_cmpxchg_relaxed(&active_asid(info, cpu),
688c2ecf20Sopenharmony_ci				     old_active_asid, asid))
698c2ecf20Sopenharmony_ci		return;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	asid_new_context(info, pasid, cpu, mm);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciint asid_allocator_init(struct asid_info *info,
758c2ecf20Sopenharmony_ci			u32 bits, unsigned int asid_per_ctxt,
768c2ecf20Sopenharmony_ci			void (*flush_cpu_ctxt_cb)(void));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#endif
79