18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Copyright (c) 2018, The Linux Foundation. All rights reserved.
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
58c2ecf20Sopenharmony_ci#include <linux/export.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <asm/barrier.h>
88c2ecf20Sopenharmony_ci#include <asm/krait-l2-accessors.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic DEFINE_RAW_SPINLOCK(krait_l2_lock);
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_civoid krait_set_l2_indirect_reg(u32 addr, u32 val)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	unsigned long flags;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&krait_l2_lock, flags);
178c2ecf20Sopenharmony_ci	/*
188c2ecf20Sopenharmony_ci	 * Select the L2 window by poking l2cpselr, then write to the window
198c2ecf20Sopenharmony_ci	 * via l2cpdr.
208c2ecf20Sopenharmony_ci	 */
218c2ecf20Sopenharmony_ci	asm volatile ("mcr p15, 3, %0, c15, c0, 6 @ l2cpselr" : : "r" (addr));
228c2ecf20Sopenharmony_ci	isb();
238c2ecf20Sopenharmony_ci	asm volatile ("mcr p15, 3, %0, c15, c0, 7 @ l2cpdr" : : "r" (val));
248c2ecf20Sopenharmony_ci	isb();
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&krait_l2_lock, flags);
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(krait_set_l2_indirect_reg);
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciu32 krait_get_l2_indirect_reg(u32 addr)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	u32 val;
338c2ecf20Sopenharmony_ci	unsigned long flags;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&krait_l2_lock, flags);
368c2ecf20Sopenharmony_ci	/*
378c2ecf20Sopenharmony_ci	 * Select the L2 window by poking l2cpselr, then read from the window
388c2ecf20Sopenharmony_ci	 * via l2cpdr.
398c2ecf20Sopenharmony_ci	 */
408c2ecf20Sopenharmony_ci	asm volatile ("mcr p15, 3, %0, c15, c0, 6 @ l2cpselr" : : "r" (addr));
418c2ecf20Sopenharmony_ci	isb();
428c2ecf20Sopenharmony_ci	asm volatile ("mrc p15, 3, %0, c15, c0, 7 @ l2cpdr" : "=r" (val));
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&krait_l2_lock, flags);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	return val;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ciEXPORT_SYMBOL(krait_get_l2_indirect_reg);
49