18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __ASM_GENERIC_MMIOWB_H
38c2ecf20Sopenharmony_ci#define __ASM_GENERIC_MMIOWB_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/*
68c2ecf20Sopenharmony_ci * Generic implementation of mmiowb() tracking for spinlocks.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * If your architecture doesn't ensure that writes to an I/O peripheral
98c2ecf20Sopenharmony_ci * within two spinlocked sections on two different CPUs are seen by the
108c2ecf20Sopenharmony_ci * peripheral in the order corresponding to the lock handover, then you
118c2ecf20Sopenharmony_ci * need to follow these FIVE easy steps:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * 	1. Implement mmiowb() (and arch_mmiowb_state() if you're fancy)
148c2ecf20Sopenharmony_ci *	   in asm/mmiowb.h, then #include this file
158c2ecf20Sopenharmony_ci *	2. Ensure your I/O write accessors call mmiowb_set_pending()
168c2ecf20Sopenharmony_ci *	3. Select ARCH_HAS_MMIOWB
178c2ecf20Sopenharmony_ci *	4. Untangle the resulting mess of header files
188c2ecf20Sopenharmony_ci *	5. Complain to your architects
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci#ifdef CONFIG_MMIOWB
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/compiler.h>
238c2ecf20Sopenharmony_ci#include <asm-generic/mmiowb_types.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#ifndef arch_mmiowb_state
268c2ecf20Sopenharmony_ci#include <asm/percpu.h>
278c2ecf20Sopenharmony_ci#include <asm/smp.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciDECLARE_PER_CPU(struct mmiowb_state, __mmiowb_state);
308c2ecf20Sopenharmony_ci#define __mmiowb_state()	raw_cpu_ptr(&__mmiowb_state)
318c2ecf20Sopenharmony_ci#else
328c2ecf20Sopenharmony_ci#define __mmiowb_state()	arch_mmiowb_state()
338c2ecf20Sopenharmony_ci#endif	/* arch_mmiowb_state */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic inline void mmiowb_set_pending(void)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct mmiowb_state *ms = __mmiowb_state();
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	if (likely(ms->nesting_count))
408c2ecf20Sopenharmony_ci		ms->mmiowb_pending = ms->nesting_count;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic inline void mmiowb_spin_lock(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct mmiowb_state *ms = __mmiowb_state();
468c2ecf20Sopenharmony_ci	ms->nesting_count++;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic inline void mmiowb_spin_unlock(void)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct mmiowb_state *ms = __mmiowb_state();
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (unlikely(ms->mmiowb_pending)) {
548c2ecf20Sopenharmony_ci		ms->mmiowb_pending = 0;
558c2ecf20Sopenharmony_ci		mmiowb();
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ms->nesting_count--;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci#else
618c2ecf20Sopenharmony_ci#define mmiowb_set_pending()		do { } while (0)
628c2ecf20Sopenharmony_ci#define mmiowb_spin_lock()		do { } while (0)
638c2ecf20Sopenharmony_ci#define mmiowb_spin_unlock()		do { } while (0)
648c2ecf20Sopenharmony_ci#endif	/* CONFIG_MMIOWB */
658c2ecf20Sopenharmony_ci#endif	/* __ASM_GENERIC_MMIOWB_H */
66