18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2017 Imagination Technologies
48c2ecf20Sopenharmony_ci * Author: Paul Burton <paul.burton@mips.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __MIPS_ASM_MIPS_CPS_H__
88c2ecf20Sopenharmony_ci#define __MIPS_ASM_MIPS_CPS_H__
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciextern unsigned long __cps_access_bad_size(void)
148c2ecf20Sopenharmony_ci	__compiletime_error("Bad size for CPS accessor");
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define CPS_ACCESSOR_A(unit, off, name)					\
178c2ecf20Sopenharmony_cistatic inline void *addr_##unit##_##name(void)				\
188c2ecf20Sopenharmony_ci{									\
198c2ecf20Sopenharmony_ci	return mips_##unit##_base + (off);				\
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define CPS_ACCESSOR_R(unit, sz, name)					\
238c2ecf20Sopenharmony_cistatic inline uint##sz##_t read_##unit##_##name(void)			\
248c2ecf20Sopenharmony_ci{									\
258c2ecf20Sopenharmony_ci	uint64_t val64;							\
268c2ecf20Sopenharmony_ci									\
278c2ecf20Sopenharmony_ci	switch (sz) {							\
288c2ecf20Sopenharmony_ci	case 32:							\
298c2ecf20Sopenharmony_ci		return __raw_readl(addr_##unit##_##name());		\
308c2ecf20Sopenharmony_ci									\
318c2ecf20Sopenharmony_ci	case 64:							\
328c2ecf20Sopenharmony_ci		if (mips_cm_is64)					\
338c2ecf20Sopenharmony_ci			return __raw_readq(addr_##unit##_##name());	\
348c2ecf20Sopenharmony_ci									\
358c2ecf20Sopenharmony_ci		val64 = __raw_readl(addr_##unit##_##name() + 4);	\
368c2ecf20Sopenharmony_ci		val64 <<= 32;						\
378c2ecf20Sopenharmony_ci		val64 |= __raw_readl(addr_##unit##_##name());		\
388c2ecf20Sopenharmony_ci		return val64;						\
398c2ecf20Sopenharmony_ci									\
408c2ecf20Sopenharmony_ci	default:							\
418c2ecf20Sopenharmony_ci		return __cps_access_bad_size();				\
428c2ecf20Sopenharmony_ci	}								\
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define CPS_ACCESSOR_W(unit, sz, name)					\
468c2ecf20Sopenharmony_cistatic inline void write_##unit##_##name(uint##sz##_t val)		\
478c2ecf20Sopenharmony_ci{									\
488c2ecf20Sopenharmony_ci	switch (sz) {							\
498c2ecf20Sopenharmony_ci	case 32:							\
508c2ecf20Sopenharmony_ci		__raw_writel(val, addr_##unit##_##name());		\
518c2ecf20Sopenharmony_ci		break;							\
528c2ecf20Sopenharmony_ci									\
538c2ecf20Sopenharmony_ci	case 64:							\
548c2ecf20Sopenharmony_ci		if (mips_cm_is64) {					\
558c2ecf20Sopenharmony_ci			__raw_writeq(val, addr_##unit##_##name());	\
568c2ecf20Sopenharmony_ci			break;						\
578c2ecf20Sopenharmony_ci		}							\
588c2ecf20Sopenharmony_ci									\
598c2ecf20Sopenharmony_ci		__raw_writel((uint64_t)val >> 32,			\
608c2ecf20Sopenharmony_ci			     addr_##unit##_##name() + 4);		\
618c2ecf20Sopenharmony_ci		__raw_writel(val, addr_##unit##_##name());		\
628c2ecf20Sopenharmony_ci		break;							\
638c2ecf20Sopenharmony_ci									\
648c2ecf20Sopenharmony_ci	default:							\
658c2ecf20Sopenharmony_ci		__cps_access_bad_size();				\
668c2ecf20Sopenharmony_ci		break;							\
678c2ecf20Sopenharmony_ci	}								\
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define CPS_ACCESSOR_M(unit, sz, name)					\
718c2ecf20Sopenharmony_cistatic inline void change_##unit##_##name(uint##sz##_t mask,		\
728c2ecf20Sopenharmony_ci					  uint##sz##_t val)		\
738c2ecf20Sopenharmony_ci{									\
748c2ecf20Sopenharmony_ci	uint##sz##_t reg_val = read_##unit##_##name();			\
758c2ecf20Sopenharmony_ci	reg_val &= ~mask;						\
768c2ecf20Sopenharmony_ci	reg_val |= val;							\
778c2ecf20Sopenharmony_ci	write_##unit##_##name(reg_val);					\
788c2ecf20Sopenharmony_ci}									\
798c2ecf20Sopenharmony_ci									\
808c2ecf20Sopenharmony_cistatic inline void set_##unit##_##name(uint##sz##_t val)		\
818c2ecf20Sopenharmony_ci{									\
828c2ecf20Sopenharmony_ci	change_##unit##_##name(val, val);				\
838c2ecf20Sopenharmony_ci}									\
848c2ecf20Sopenharmony_ci									\
858c2ecf20Sopenharmony_cistatic inline void clear_##unit##_##name(uint##sz##_t val)		\
868c2ecf20Sopenharmony_ci{									\
878c2ecf20Sopenharmony_ci	change_##unit##_##name(val, 0);					\
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define CPS_ACCESSOR_RO(unit, sz, off, name)				\
918c2ecf20Sopenharmony_ci	CPS_ACCESSOR_A(unit, off, name)					\
928c2ecf20Sopenharmony_ci	CPS_ACCESSOR_R(unit, sz, name)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define CPS_ACCESSOR_WO(unit, sz, off, name)				\
958c2ecf20Sopenharmony_ci	CPS_ACCESSOR_A(unit, off, name)					\
968c2ecf20Sopenharmony_ci	CPS_ACCESSOR_W(unit, sz, name)
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define CPS_ACCESSOR_RW(unit, sz, off, name)				\
998c2ecf20Sopenharmony_ci	CPS_ACCESSOR_A(unit, off, name)					\
1008c2ecf20Sopenharmony_ci	CPS_ACCESSOR_R(unit, sz, name)					\
1018c2ecf20Sopenharmony_ci	CPS_ACCESSOR_W(unit, sz, name)					\
1028c2ecf20Sopenharmony_ci	CPS_ACCESSOR_M(unit, sz, name)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#include <asm/mips-cm.h>
1058c2ecf20Sopenharmony_ci#include <asm/mips-cpc.h>
1068c2ecf20Sopenharmony_ci#include <asm/mips-gic.h>
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/**
1098c2ecf20Sopenharmony_ci * mips_cps_numclusters - return the number of clusters present in the system
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * Returns the number of clusters in the system.
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistatic inline unsigned int mips_cps_numclusters(void)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	unsigned int num_clusters;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (mips_cm_revision() < CM_REV_CM3_5)
1188c2ecf20Sopenharmony_ci		return 1;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	num_clusters = read_gcr_config() & CM_GCR_CONFIG_NUM_CLUSTERS;
1218c2ecf20Sopenharmony_ci	num_clusters >>= __ffs(CM_GCR_CONFIG_NUM_CLUSTERS);
1228c2ecf20Sopenharmony_ci	return num_clusters;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/**
1268c2ecf20Sopenharmony_ci * mips_cps_cluster_config - return (GCR|CPC)_CONFIG from a cluster
1278c2ecf20Sopenharmony_ci * @cluster: the ID of the cluster whose config we want
1288c2ecf20Sopenharmony_ci *
1298c2ecf20Sopenharmony_ci * Read the value of GCR_CONFIG (or its CPC_CONFIG mirror) from a @cluster.
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * Returns the value of GCR_CONFIG.
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_cistatic inline uint64_t mips_cps_cluster_config(unsigned int cluster)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	uint64_t config;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	if (mips_cm_revision() < CM_REV_CM3_5) {
1388c2ecf20Sopenharmony_ci		/*
1398c2ecf20Sopenharmony_ci		 * Prior to CM 3.5 we don't have the notion of multiple
1408c2ecf20Sopenharmony_ci		 * clusters so we can trivially read the GCR_CONFIG register
1418c2ecf20Sopenharmony_ci		 * within this cluster.
1428c2ecf20Sopenharmony_ci		 */
1438c2ecf20Sopenharmony_ci		WARN_ON(cluster != 0);
1448c2ecf20Sopenharmony_ci		config = read_gcr_config();
1458c2ecf20Sopenharmony_ci	} else {
1468c2ecf20Sopenharmony_ci		/*
1478c2ecf20Sopenharmony_ci		 * From CM 3.5 onwards we read the CPC_CONFIG mirror of
1488c2ecf20Sopenharmony_ci		 * GCR_CONFIG via the redirect region, since the CPC is always
1498c2ecf20Sopenharmony_ci		 * powered up allowing us not to need to power up the CM.
1508c2ecf20Sopenharmony_ci		 */
1518c2ecf20Sopenharmony_ci		mips_cm_lock_other(cluster, 0, 0, CM_GCR_Cx_OTHER_BLOCK_GLOBAL);
1528c2ecf20Sopenharmony_ci		config = read_cpc_redir_config();
1538c2ecf20Sopenharmony_ci		mips_cm_unlock_other();
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return config;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/**
1608c2ecf20Sopenharmony_ci * mips_cps_numcores - return the number of cores present in a cluster
1618c2ecf20Sopenharmony_ci * @cluster: the ID of the cluster whose core count we want
1628c2ecf20Sopenharmony_ci *
1638c2ecf20Sopenharmony_ci * Returns the value of the PCORES field of the GCR_CONFIG register plus 1, or
1648c2ecf20Sopenharmony_ci * zero if no Coherence Manager is present.
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_cistatic inline unsigned int mips_cps_numcores(unsigned int cluster)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	if (!mips_cm_present())
1698c2ecf20Sopenharmony_ci		return 0;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Add one before masking to handle 0xff indicating no cores */
1728c2ecf20Sopenharmony_ci	return (mips_cps_cluster_config(cluster) + 1) & CM_GCR_CONFIG_PCORES;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/**
1768c2ecf20Sopenharmony_ci * mips_cps_numiocu - return the number of IOCUs present in a cluster
1778c2ecf20Sopenharmony_ci * @cluster: the ID of the cluster whose IOCU count we want
1788c2ecf20Sopenharmony_ci *
1798c2ecf20Sopenharmony_ci * Returns the value of the NUMIOCU field of the GCR_CONFIG register, or zero
1808c2ecf20Sopenharmony_ci * if no Coherence Manager is present.
1818c2ecf20Sopenharmony_ci */
1828c2ecf20Sopenharmony_cistatic inline unsigned int mips_cps_numiocu(unsigned int cluster)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	unsigned int num_iocu;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	if (!mips_cm_present())
1878c2ecf20Sopenharmony_ci		return 0;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	num_iocu = mips_cps_cluster_config(cluster) & CM_GCR_CONFIG_NUMIOCU;
1908c2ecf20Sopenharmony_ci	num_iocu >>= __ffs(CM_GCR_CONFIG_NUMIOCU);
1918c2ecf20Sopenharmony_ci	return num_iocu;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/**
1958c2ecf20Sopenharmony_ci * mips_cps_numvps - return the number of VPs (threads) supported by a core
1968c2ecf20Sopenharmony_ci * @cluster: the ID of the cluster containing the core we want to examine
1978c2ecf20Sopenharmony_ci * @core: the ID of the core whose VP count we want
1988c2ecf20Sopenharmony_ci *
1998c2ecf20Sopenharmony_ci * Returns the number of Virtual Processors (VPs, ie. hardware threads) that
2008c2ecf20Sopenharmony_ci * are supported by the given @core in the given @cluster. If the core or the
2018c2ecf20Sopenharmony_ci * kernel do not support hardware mutlti-threading this returns 1.
2028c2ecf20Sopenharmony_ci */
2038c2ecf20Sopenharmony_cistatic inline unsigned int mips_cps_numvps(unsigned int cluster, unsigned int core)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	unsigned int cfg;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (!mips_cm_present())
2088c2ecf20Sopenharmony_ci		return 1;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if ((!IS_ENABLED(CONFIG_MIPS_MT_SMP) || !cpu_has_mipsmt)
2118c2ecf20Sopenharmony_ci		&& (!IS_ENABLED(CONFIG_CPU_MIPSR6) || !cpu_has_vp))
2128c2ecf20Sopenharmony_ci		return 1;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	mips_cm_lock_other(cluster, core, 0, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (mips_cm_revision() < CM_REV_CM3_5) {
2178c2ecf20Sopenharmony_ci		/*
2188c2ecf20Sopenharmony_ci		 * Prior to CM 3.5 we can only have one cluster & don't have
2198c2ecf20Sopenharmony_ci		 * CPC_Cx_CONFIG, so we read GCR_Cx_CONFIG.
2208c2ecf20Sopenharmony_ci		 */
2218c2ecf20Sopenharmony_ci		cfg = read_gcr_co_config();
2228c2ecf20Sopenharmony_ci	} else {
2238c2ecf20Sopenharmony_ci		/*
2248c2ecf20Sopenharmony_ci		 * From CM 3.5 onwards we read CPC_Cx_CONFIG because the CPC is
2258c2ecf20Sopenharmony_ci		 * always powered, which allows us to not worry about powering
2268c2ecf20Sopenharmony_ci		 * up the cluster's CM here.
2278c2ecf20Sopenharmony_ci		 */
2288c2ecf20Sopenharmony_ci		cfg = read_cpc_co_config();
2298c2ecf20Sopenharmony_ci	}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	mips_cm_unlock_other();
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return (cfg + 1) & CM_GCR_Cx_CONFIG_PVPE;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#endif /* __MIPS_ASM_MIPS_CPS_H__ */
237