xref: /OK3568_Linux_fs/kernel/arch/mips/include/asm/mips-cps.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2017 Imagination Technologies
4*4882a593Smuzhiyun  * Author: Paul Burton <paul.burton@mips.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef __MIPS_ASM_MIPS_CPS_H__
8*4882a593Smuzhiyun #define __MIPS_ASM_MIPS_CPS_H__
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/io.h>
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun extern unsigned long __cps_access_bad_size(void)
14*4882a593Smuzhiyun 	__compiletime_error("Bad size for CPS accessor");
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define CPS_ACCESSOR_A(unit, off, name)					\
17*4882a593Smuzhiyun static inline void *addr_##unit##_##name(void)				\
18*4882a593Smuzhiyun {									\
19*4882a593Smuzhiyun 	return mips_##unit##_base + (off);				\
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define CPS_ACCESSOR_R(unit, sz, name)					\
23*4882a593Smuzhiyun static inline uint##sz##_t read_##unit##_##name(void)			\
24*4882a593Smuzhiyun {									\
25*4882a593Smuzhiyun 	uint64_t val64;							\
26*4882a593Smuzhiyun 									\
27*4882a593Smuzhiyun 	switch (sz) {							\
28*4882a593Smuzhiyun 	case 32:							\
29*4882a593Smuzhiyun 		return __raw_readl(addr_##unit##_##name());		\
30*4882a593Smuzhiyun 									\
31*4882a593Smuzhiyun 	case 64:							\
32*4882a593Smuzhiyun 		if (mips_cm_is64)					\
33*4882a593Smuzhiyun 			return __raw_readq(addr_##unit##_##name());	\
34*4882a593Smuzhiyun 									\
35*4882a593Smuzhiyun 		val64 = __raw_readl(addr_##unit##_##name() + 4);	\
36*4882a593Smuzhiyun 		val64 <<= 32;						\
37*4882a593Smuzhiyun 		val64 |= __raw_readl(addr_##unit##_##name());		\
38*4882a593Smuzhiyun 		return val64;						\
39*4882a593Smuzhiyun 									\
40*4882a593Smuzhiyun 	default:							\
41*4882a593Smuzhiyun 		return __cps_access_bad_size();				\
42*4882a593Smuzhiyun 	}								\
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #define CPS_ACCESSOR_W(unit, sz, name)					\
46*4882a593Smuzhiyun static inline void write_##unit##_##name(uint##sz##_t val)		\
47*4882a593Smuzhiyun {									\
48*4882a593Smuzhiyun 	switch (sz) {							\
49*4882a593Smuzhiyun 	case 32:							\
50*4882a593Smuzhiyun 		__raw_writel(val, addr_##unit##_##name());		\
51*4882a593Smuzhiyun 		break;							\
52*4882a593Smuzhiyun 									\
53*4882a593Smuzhiyun 	case 64:							\
54*4882a593Smuzhiyun 		if (mips_cm_is64) {					\
55*4882a593Smuzhiyun 			__raw_writeq(val, addr_##unit##_##name());	\
56*4882a593Smuzhiyun 			break;						\
57*4882a593Smuzhiyun 		}							\
58*4882a593Smuzhiyun 									\
59*4882a593Smuzhiyun 		__raw_writel((uint64_t)val >> 32,			\
60*4882a593Smuzhiyun 			     addr_##unit##_##name() + 4);		\
61*4882a593Smuzhiyun 		__raw_writel(val, addr_##unit##_##name());		\
62*4882a593Smuzhiyun 		break;							\
63*4882a593Smuzhiyun 									\
64*4882a593Smuzhiyun 	default:							\
65*4882a593Smuzhiyun 		__cps_access_bad_size();				\
66*4882a593Smuzhiyun 		break;							\
67*4882a593Smuzhiyun 	}								\
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define CPS_ACCESSOR_M(unit, sz, name)					\
71*4882a593Smuzhiyun static inline void change_##unit##_##name(uint##sz##_t mask,		\
72*4882a593Smuzhiyun 					  uint##sz##_t val)		\
73*4882a593Smuzhiyun {									\
74*4882a593Smuzhiyun 	uint##sz##_t reg_val = read_##unit##_##name();			\
75*4882a593Smuzhiyun 	reg_val &= ~mask;						\
76*4882a593Smuzhiyun 	reg_val |= val;							\
77*4882a593Smuzhiyun 	write_##unit##_##name(reg_val);					\
78*4882a593Smuzhiyun }									\
79*4882a593Smuzhiyun 									\
80*4882a593Smuzhiyun static inline void set_##unit##_##name(uint##sz##_t val)		\
81*4882a593Smuzhiyun {									\
82*4882a593Smuzhiyun 	change_##unit##_##name(val, val);				\
83*4882a593Smuzhiyun }									\
84*4882a593Smuzhiyun 									\
85*4882a593Smuzhiyun static inline void clear_##unit##_##name(uint##sz##_t val)		\
86*4882a593Smuzhiyun {									\
87*4882a593Smuzhiyun 	change_##unit##_##name(val, 0);					\
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #define CPS_ACCESSOR_RO(unit, sz, off, name)				\
91*4882a593Smuzhiyun 	CPS_ACCESSOR_A(unit, off, name)					\
92*4882a593Smuzhiyun 	CPS_ACCESSOR_R(unit, sz, name)
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #define CPS_ACCESSOR_WO(unit, sz, off, name)				\
95*4882a593Smuzhiyun 	CPS_ACCESSOR_A(unit, off, name)					\
96*4882a593Smuzhiyun 	CPS_ACCESSOR_W(unit, sz, name)
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun #define CPS_ACCESSOR_RW(unit, sz, off, name)				\
99*4882a593Smuzhiyun 	CPS_ACCESSOR_A(unit, off, name)					\
100*4882a593Smuzhiyun 	CPS_ACCESSOR_R(unit, sz, name)					\
101*4882a593Smuzhiyun 	CPS_ACCESSOR_W(unit, sz, name)					\
102*4882a593Smuzhiyun 	CPS_ACCESSOR_M(unit, sz, name)
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #include <asm/mips-cm.h>
105*4882a593Smuzhiyun #include <asm/mips-cpc.h>
106*4882a593Smuzhiyun #include <asm/mips-gic.h>
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun  * mips_cps_numclusters - return the number of clusters present in the system
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  * Returns the number of clusters in the system.
112*4882a593Smuzhiyun  */
mips_cps_numclusters(void)113*4882a593Smuzhiyun static inline unsigned int mips_cps_numclusters(void)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	unsigned int num_clusters;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if (mips_cm_revision() < CM_REV_CM3_5)
118*4882a593Smuzhiyun 		return 1;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	num_clusters = read_gcr_config() & CM_GCR_CONFIG_NUM_CLUSTERS;
121*4882a593Smuzhiyun 	num_clusters >>= __ffs(CM_GCR_CONFIG_NUM_CLUSTERS);
122*4882a593Smuzhiyun 	return num_clusters;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /**
126*4882a593Smuzhiyun  * mips_cps_cluster_config - return (GCR|CPC)_CONFIG from a cluster
127*4882a593Smuzhiyun  * @cluster: the ID of the cluster whose config we want
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * Read the value of GCR_CONFIG (or its CPC_CONFIG mirror) from a @cluster.
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * Returns the value of GCR_CONFIG.
132*4882a593Smuzhiyun  */
mips_cps_cluster_config(unsigned int cluster)133*4882a593Smuzhiyun static inline uint64_t mips_cps_cluster_config(unsigned int cluster)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	uint64_t config;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (mips_cm_revision() < CM_REV_CM3_5) {
138*4882a593Smuzhiyun 		/*
139*4882a593Smuzhiyun 		 * Prior to CM 3.5 we don't have the notion of multiple
140*4882a593Smuzhiyun 		 * clusters so we can trivially read the GCR_CONFIG register
141*4882a593Smuzhiyun 		 * within this cluster.
142*4882a593Smuzhiyun 		 */
143*4882a593Smuzhiyun 		WARN_ON(cluster != 0);
144*4882a593Smuzhiyun 		config = read_gcr_config();
145*4882a593Smuzhiyun 	} else {
146*4882a593Smuzhiyun 		/*
147*4882a593Smuzhiyun 		 * From CM 3.5 onwards we read the CPC_CONFIG mirror of
148*4882a593Smuzhiyun 		 * GCR_CONFIG via the redirect region, since the CPC is always
149*4882a593Smuzhiyun 		 * powered up allowing us not to need to power up the CM.
150*4882a593Smuzhiyun 		 */
151*4882a593Smuzhiyun 		mips_cm_lock_other(cluster, 0, 0, CM_GCR_Cx_OTHER_BLOCK_GLOBAL);
152*4882a593Smuzhiyun 		config = read_cpc_redir_config();
153*4882a593Smuzhiyun 		mips_cm_unlock_other();
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	return config;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun  * mips_cps_numcores - return the number of cores present in a cluster
161*4882a593Smuzhiyun  * @cluster: the ID of the cluster whose core count we want
162*4882a593Smuzhiyun  *
163*4882a593Smuzhiyun  * Returns the value of the PCORES field of the GCR_CONFIG register plus 1, or
164*4882a593Smuzhiyun  * zero if no Coherence Manager is present.
165*4882a593Smuzhiyun  */
mips_cps_numcores(unsigned int cluster)166*4882a593Smuzhiyun static inline unsigned int mips_cps_numcores(unsigned int cluster)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	if (!mips_cm_present())
169*4882a593Smuzhiyun 		return 0;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* Add one before masking to handle 0xff indicating no cores */
172*4882a593Smuzhiyun 	return (mips_cps_cluster_config(cluster) + 1) & CM_GCR_CONFIG_PCORES;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /**
176*4882a593Smuzhiyun  * mips_cps_numiocu - return the number of IOCUs present in a cluster
177*4882a593Smuzhiyun  * @cluster: the ID of the cluster whose IOCU count we want
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * Returns the value of the NUMIOCU field of the GCR_CONFIG register, or zero
180*4882a593Smuzhiyun  * if no Coherence Manager is present.
181*4882a593Smuzhiyun  */
mips_cps_numiocu(unsigned int cluster)182*4882a593Smuzhiyun static inline unsigned int mips_cps_numiocu(unsigned int cluster)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	unsigned int num_iocu;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if (!mips_cm_present())
187*4882a593Smuzhiyun 		return 0;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	num_iocu = mips_cps_cluster_config(cluster) & CM_GCR_CONFIG_NUMIOCU;
190*4882a593Smuzhiyun 	num_iocu >>= __ffs(CM_GCR_CONFIG_NUMIOCU);
191*4882a593Smuzhiyun 	return num_iocu;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /**
195*4882a593Smuzhiyun  * mips_cps_numvps - return the number of VPs (threads) supported by a core
196*4882a593Smuzhiyun  * @cluster: the ID of the cluster containing the core we want to examine
197*4882a593Smuzhiyun  * @core: the ID of the core whose VP count we want
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * Returns the number of Virtual Processors (VPs, ie. hardware threads) that
200*4882a593Smuzhiyun  * are supported by the given @core in the given @cluster. If the core or the
201*4882a593Smuzhiyun  * kernel do not support hardware mutlti-threading this returns 1.
202*4882a593Smuzhiyun  */
mips_cps_numvps(unsigned int cluster,unsigned int core)203*4882a593Smuzhiyun static inline unsigned int mips_cps_numvps(unsigned int cluster, unsigned int core)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	unsigned int cfg;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (!mips_cm_present())
208*4882a593Smuzhiyun 		return 1;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	if ((!IS_ENABLED(CONFIG_MIPS_MT_SMP) || !cpu_has_mipsmt)
211*4882a593Smuzhiyun 		&& (!IS_ENABLED(CONFIG_CPU_MIPSR6) || !cpu_has_vp))
212*4882a593Smuzhiyun 		return 1;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	mips_cm_lock_other(cluster, core, 0, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (mips_cm_revision() < CM_REV_CM3_5) {
217*4882a593Smuzhiyun 		/*
218*4882a593Smuzhiyun 		 * Prior to CM 3.5 we can only have one cluster & don't have
219*4882a593Smuzhiyun 		 * CPC_Cx_CONFIG, so we read GCR_Cx_CONFIG.
220*4882a593Smuzhiyun 		 */
221*4882a593Smuzhiyun 		cfg = read_gcr_co_config();
222*4882a593Smuzhiyun 	} else {
223*4882a593Smuzhiyun 		/*
224*4882a593Smuzhiyun 		 * From CM 3.5 onwards we read CPC_Cx_CONFIG because the CPC is
225*4882a593Smuzhiyun 		 * always powered, which allows us to not worry about powering
226*4882a593Smuzhiyun 		 * up the cluster's CM here.
227*4882a593Smuzhiyun 		 */
228*4882a593Smuzhiyun 		cfg = read_cpc_co_config();
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	mips_cm_unlock_other();
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	return (cfg + 1) & CM_GCR_Cx_CONFIG_PVPE;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun #endif /* __MIPS_ASM_MIPS_CPS_H__ */
237