mips-cps.h 6.2 KB

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