162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2009 Freescale Semiconductor, Inc.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * provides masks and opcode images for use by code generation, emulation
662306a36Sopenharmony_ci * and for instructions that older assemblers might not know about
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#ifndef _ASM_POWERPC_DBELL_H
962306a36Sopenharmony_ci#define _ASM_POWERPC_DBELL_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/smp.h>
1262306a36Sopenharmony_ci#include <linux/threads.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <asm/cputhreads.h>
1562306a36Sopenharmony_ci#include <asm/ppc-opcode.h>
1662306a36Sopenharmony_ci#include <asm/feature-fixups.h>
1762306a36Sopenharmony_ci#include <asm/kvm_ppc.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define PPC_DBELL_MSG_BRDCAST	(0x04000000)
2062306a36Sopenharmony_ci#define PPC_DBELL_TYPE(x)	(((x) & 0xf) << (63-36))
2162306a36Sopenharmony_ci#define PPC_DBELL_TYPE_MASK	PPC_DBELL_TYPE(0xf)
2262306a36Sopenharmony_ci#define PPC_DBELL_LPID(x)	((x) << (63 - 49))
2362306a36Sopenharmony_ci#define PPC_DBELL_PIR_MASK	0x3fff
2462306a36Sopenharmony_cienum ppc_dbell {
2562306a36Sopenharmony_ci	PPC_DBELL = 0,		/* doorbell */
2662306a36Sopenharmony_ci	PPC_DBELL_CRIT = 1,	/* critical doorbell */
2762306a36Sopenharmony_ci	PPC_G_DBELL = 2,	/* guest doorbell */
2862306a36Sopenharmony_ci	PPC_G_DBELL_CRIT = 3,	/* guest critical doorbell */
2962306a36Sopenharmony_ci	PPC_G_DBELL_MC = 4,	/* guest mcheck doorbell */
3062306a36Sopenharmony_ci	PPC_DBELL_SERVER = 5,	/* doorbell on server */
3162306a36Sopenharmony_ci};
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#define PPC_DBELL_MSGTYPE		PPC_DBELL_SERVER
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistatic inline void _ppc_msgsnd(u32 msg)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	__asm__ __volatile__ (ASM_FTR_IFSET(PPC_MSGSND(%1), PPC_MSGSNDP(%1), %0)
4062306a36Sopenharmony_ci				: : "i" (CPU_FTR_HVMODE), "r" (msg));
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* sync before sending message */
4462306a36Sopenharmony_cistatic inline void ppc_msgsnd_sync(void)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	__asm__ __volatile__ ("sync" : : : "memory");
4762306a36Sopenharmony_ci}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/* sync after taking message interrupt */
5062306a36Sopenharmony_cistatic inline void ppc_msgsync(void)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	/* sync is not required when taking messages from the same core */
5362306a36Sopenharmony_ci	__asm__ __volatile__ (ASM_FTR_IFSET(PPC_MSGSYNC " ; lwsync", "", %0)
5462306a36Sopenharmony_ci				: : "i" (CPU_FTR_HVMODE|CPU_FTR_ARCH_300));
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic inline void _ppc_msgclr(u32 msg)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	__asm__ __volatile__ (ASM_FTR_IFSET(PPC_MSGCLR(%1), PPC_MSGCLRP(%1), %0)
6062306a36Sopenharmony_ci				: : "i" (CPU_FTR_HVMODE), "r" (msg));
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic inline void ppc_msgclr(enum ppc_dbell type)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	u32 msg = PPC_DBELL_TYPE(type);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	_ppc_msgclr(msg);
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#else /* CONFIG_PPC_BOOK3S */
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define PPC_DBELL_MSGTYPE		PPC_DBELL
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic inline void _ppc_msgsnd(u32 msg)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	__asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/* sync before sending message */
8062306a36Sopenharmony_cistatic inline void ppc_msgsnd_sync(void)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	__asm__ __volatile__ ("sync" : : : "memory");
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/* sync after taking message interrupt */
8662306a36Sopenharmony_cistatic inline void ppc_msgsync(void)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci#endif /* CONFIG_PPC_BOOK3S */
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciextern void doorbell_exception(struct pt_regs *regs);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic inline void ppc_msgsnd(enum ppc_dbell type, u32 flags, u32 tag)
9562306a36Sopenharmony_ci{
9662306a36Sopenharmony_ci	u32 msg = PPC_DBELL_TYPE(type) | (flags & PPC_DBELL_MSG_BRDCAST) |
9762306a36Sopenharmony_ci			(tag & 0x07ffffff);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	_ppc_msgsnd(msg);
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#ifdef CONFIG_SMP
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/*
10562306a36Sopenharmony_ci * Doorbells must only be used if CPU_FTR_DBELL is available.
10662306a36Sopenharmony_ci * msgsnd is used in HV, and msgsndp is used in !HV.
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * These should be used by platform code that is aware of restrictions.
10962306a36Sopenharmony_ci * Other arch code should use ->cause_ipi.
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci * doorbell_global_ipi() sends a dbell to any target CPU.
11262306a36Sopenharmony_ci * Must be used only by architectures that address msgsnd target
11362306a36Sopenharmony_ci * by PIR/get_hard_smp_processor_id.
11462306a36Sopenharmony_ci */
11562306a36Sopenharmony_cistatic inline void doorbell_global_ipi(int cpu)
11662306a36Sopenharmony_ci{
11762306a36Sopenharmony_ci	u32 tag = get_hard_smp_processor_id(cpu);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	kvmppc_set_host_ipi(cpu);
12062306a36Sopenharmony_ci	/* Order previous accesses vs. msgsnd, which is treated as a store */
12162306a36Sopenharmony_ci	ppc_msgsnd_sync();
12262306a36Sopenharmony_ci	ppc_msgsnd(PPC_DBELL_MSGTYPE, 0, tag);
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * doorbell_core_ipi() sends a dbell to a target CPU in the same core.
12762306a36Sopenharmony_ci * Must be used only by architectures that address msgsnd target
12862306a36Sopenharmony_ci * by TIR/cpu_thread_in_core.
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_cistatic inline void doorbell_core_ipi(int cpu)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	u32 tag = cpu_thread_in_core(cpu);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	kvmppc_set_host_ipi(cpu);
13562306a36Sopenharmony_ci	/* Order previous accesses vs. msgsnd, which is treated as a store */
13662306a36Sopenharmony_ci	ppc_msgsnd_sync();
13762306a36Sopenharmony_ci	ppc_msgsnd(PPC_DBELL_MSGTYPE, 0, tag);
13862306a36Sopenharmony_ci}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci/*
14162306a36Sopenharmony_ci * Attempt to cause a core doorbell if destination is on the same core.
14262306a36Sopenharmony_ci * Returns 1 on success, 0 on failure.
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_cistatic inline int doorbell_try_core_ipi(int cpu)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	int this_cpu = get_cpu();
14762306a36Sopenharmony_ci	int ret = 0;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	if (cpumask_test_cpu(cpu, cpu_sibling_mask(this_cpu))) {
15062306a36Sopenharmony_ci		doorbell_core_ipi(cpu);
15162306a36Sopenharmony_ci		ret = 1;
15262306a36Sopenharmony_ci	}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	put_cpu();
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	return ret;
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci#endif /* CONFIG_SMP */
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci#endif /* _ASM_POWERPC_DBELL_H */
162