cbe_thermal.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * thermal support for the cell processor
  3. *
  4. * This module adds some sysfs attributes to cpu and spu nodes.
  5. * Base for measurements are the digital thermal sensors (DTS)
  6. * located on the chip.
  7. * The accuracy is 2 degrees, starting from 65 up to 125 degrees celsius
  8. * The attributes can be found under
  9. * /sys/devices/system/cpu/cpuX/thermal
  10. * /sys/devices/system/spu/spuX/thermal
  11. *
  12. * The following attributes are added for each node:
  13. * temperature:
  14. * contains the current temperature measured by the DTS
  15. * throttle_begin:
  16. * throttling begins when temperature is greater or equal to
  17. * throttle_begin. Setting this value to 125 prevents throttling.
  18. * throttle_end:
  19. * throttling is being ceased, if the temperature is lower than
  20. * throttle_end. Due to a delay between applying throttling and
  21. * a reduced temperature this value should be less than throttle_begin.
  22. * A value equal to throttle_begin provides only a very little hysteresis.
  23. * throttle_full_stop:
  24. * If the temperatrue is greater or equal to throttle_full_stop,
  25. * full throttling is applied to the cpu or spu. This value should be
  26. * greater than throttle_begin and throttle_end. Setting this value to
  27. * 65 prevents the unit from running code at all.
  28. *
  29. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  30. *
  31. * Author: Christian Krafft <krafft@de.ibm.com>
  32. *
  33. * This program is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2, or (at your option)
  36. * any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program; if not, write to the Free Software
  45. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  46. */
  47. #include <linux/module.h>
  48. #include <linux/sysdev.h>
  49. #include <linux/kernel.h>
  50. #include <linux/cpu.h>
  51. #include <asm/spu.h>
  52. #include <asm/io.h>
  53. #include <asm/prom.h>
  54. #include <asm/cell-regs.h>
  55. #include "spu_priv1_mmio.h"
  56. #define TEMP_MIN 65
  57. #define TEMP_MAX 125
  58. #define SYSDEV_PREFIX_ATTR(_prefix,_name,_mode) \
  59. struct sysdev_attribute attr_ ## _prefix ## _ ## _name = { \
  60. .attr = { .name = __stringify(_name), .mode = _mode }, \
  61. .show = _prefix ## _show_ ## _name, \
  62. .store = _prefix ## _store_ ## _name, \
  63. };
  64. static inline u8 reg_to_temp(u8 reg_value)
  65. {
  66. return ((reg_value & 0x3f) << 1) + TEMP_MIN;
  67. }
  68. static inline u8 temp_to_reg(u8 temp)
  69. {
  70. return ((temp - TEMP_MIN) >> 1) & 0x3f;
  71. }
  72. static struct cbe_pmd_regs __iomem *get_pmd_regs(struct sys_device *sysdev)
  73. {
  74. struct spu *spu;
  75. spu = container_of(sysdev, struct spu, sysdev);
  76. return cbe_get_pmd_regs(spu_devnode(spu));
  77. }
  78. /* returns the value for a given spu in a given register */
  79. static u8 spu_read_register_value(struct sys_device *sysdev, union spe_reg __iomem *reg)
  80. {
  81. union spe_reg value;
  82. struct spu *spu;
  83. spu = container_of(sysdev, struct spu, sysdev);
  84. value.val = in_be64(&reg->val);
  85. return value.spe[spu->spe_id];
  86. }
  87. static ssize_t spu_show_temp(struct sys_device *sysdev, char *buf)
  88. {
  89. u8 value;
  90. struct cbe_pmd_regs __iomem *pmd_regs;
  91. pmd_regs = get_pmd_regs(sysdev);
  92. value = spu_read_register_value(sysdev, &pmd_regs->ts_ctsr1);
  93. return sprintf(buf, "%d\n", reg_to_temp(value));
  94. }
  95. static ssize_t show_throttle(struct cbe_pmd_regs __iomem *pmd_regs, char *buf, int pos)
  96. {
  97. u64 value;
  98. value = in_be64(&pmd_regs->tm_tpr.val);
  99. /* access the corresponding byte */
  100. value >>= pos;
  101. value &= 0x3F;
  102. return sprintf(buf, "%d\n", reg_to_temp(value));
  103. }
  104. static ssize_t store_throttle(struct cbe_pmd_regs __iomem *pmd_regs, const char *buf, size_t size, int pos)
  105. {
  106. u64 reg_value;
  107. int temp;
  108. u64 new_value;
  109. int ret;
  110. ret = sscanf(buf, "%u", &temp);
  111. if (ret != 1 || temp < TEMP_MIN || temp > TEMP_MAX)
  112. return -EINVAL;
  113. new_value = temp_to_reg(temp);
  114. reg_value = in_be64(&pmd_regs->tm_tpr.val);
  115. /* zero out bits for new value */
  116. reg_value &= ~(0xffull << pos);
  117. /* set bits to new value */
  118. reg_value |= new_value << pos;
  119. out_be64(&pmd_regs->tm_tpr.val, reg_value);
  120. return size;
  121. }
  122. static ssize_t spu_show_throttle_end(struct sys_device *sysdev, char *buf)
  123. {
  124. return show_throttle(get_pmd_regs(sysdev), buf, 0);
  125. }
  126. static ssize_t spu_show_throttle_begin(struct sys_device *sysdev, char *buf)
  127. {
  128. return show_throttle(get_pmd_regs(sysdev), buf, 8);
  129. }
  130. static ssize_t spu_show_throttle_full_stop(struct sys_device *sysdev, char *buf)
  131. {
  132. return show_throttle(get_pmd_regs(sysdev), buf, 16);
  133. }
  134. static ssize_t spu_store_throttle_end(struct sys_device *sysdev, const char *buf, size_t size)
  135. {
  136. return store_throttle(get_pmd_regs(sysdev), buf, size, 0);
  137. }
  138. static ssize_t spu_store_throttle_begin(struct sys_device *sysdev, const char *buf, size_t size)
  139. {
  140. return store_throttle(get_pmd_regs(sysdev), buf, size, 8);
  141. }
  142. static ssize_t spu_store_throttle_full_stop(struct sys_device *sysdev, const char *buf, size_t size)
  143. {
  144. return store_throttle(get_pmd_regs(sysdev), buf, size, 16);
  145. }
  146. static ssize_t ppe_show_temp(struct sys_device *sysdev, char *buf, int pos)
  147. {
  148. struct cbe_pmd_regs __iomem *pmd_regs;
  149. u64 value;
  150. pmd_regs = cbe_get_cpu_pmd_regs(sysdev->id);
  151. value = in_be64(&pmd_regs->ts_ctsr2);
  152. value = (value >> pos) & 0x3f;
  153. return sprintf(buf, "%d\n", reg_to_temp(value));
  154. }
  155. /* shows the temperature of the DTS on the PPE,
  156. * located near the linear thermal sensor */
  157. static ssize_t ppe_show_temp0(struct sys_device *sysdev, char *buf)
  158. {
  159. return ppe_show_temp(sysdev, buf, 32);
  160. }
  161. /* shows the temperature of the second DTS on the PPE */
  162. static ssize_t ppe_show_temp1(struct sys_device *sysdev, char *buf)
  163. {
  164. return ppe_show_temp(sysdev, buf, 0);
  165. }
  166. static ssize_t ppe_show_throttle_end(struct sys_device *sysdev, char *buf)
  167. {
  168. return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 32);
  169. }
  170. static ssize_t ppe_show_throttle_begin(struct sys_device *sysdev, char *buf)
  171. {
  172. return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 40);
  173. }
  174. static ssize_t ppe_show_throttle_full_stop(struct sys_device *sysdev, char *buf)
  175. {
  176. return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 48);
  177. }
  178. static ssize_t ppe_store_throttle_end(struct sys_device *sysdev, const char *buf, size_t size)
  179. {
  180. return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 32);
  181. }
  182. static ssize_t ppe_store_throttle_begin(struct sys_device *sysdev, const char *buf, size_t size)
  183. {
  184. return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 40);
  185. }
  186. static ssize_t ppe_store_throttle_full_stop(struct sys_device *sysdev, const char *buf, size_t size)
  187. {
  188. return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 48);
  189. }
  190. static struct sysdev_attribute attr_spu_temperature = {
  191. .attr = {.name = "temperature", .mode = 0400 },
  192. .show = spu_show_temp,
  193. };
  194. static SYSDEV_PREFIX_ATTR(spu, throttle_end, 0600);
  195. static SYSDEV_PREFIX_ATTR(spu, throttle_begin, 0600);
  196. static SYSDEV_PREFIX_ATTR(spu, throttle_full_stop, 0600);
  197. static struct attribute *spu_attributes[] = {
  198. &attr_spu_temperature.attr,
  199. &attr_spu_throttle_end.attr,
  200. &attr_spu_throttle_begin.attr,
  201. &attr_spu_throttle_full_stop.attr,
  202. NULL,
  203. };
  204. static struct attribute_group spu_attribute_group = {
  205. .name = "thermal",
  206. .attrs = spu_attributes,
  207. };
  208. static struct sysdev_attribute attr_ppe_temperature0 = {
  209. .attr = {.name = "temperature0", .mode = 0400 },
  210. .show = ppe_show_temp0,
  211. };
  212. static struct sysdev_attribute attr_ppe_temperature1 = {
  213. .attr = {.name = "temperature1", .mode = 0400 },
  214. .show = ppe_show_temp1,
  215. };
  216. static SYSDEV_PREFIX_ATTR(ppe, throttle_end, 0600);
  217. static SYSDEV_PREFIX_ATTR(ppe, throttle_begin, 0600);
  218. static SYSDEV_PREFIX_ATTR(ppe, throttle_full_stop, 0600);
  219. static struct attribute *ppe_attributes[] = {
  220. &attr_ppe_temperature0.attr,
  221. &attr_ppe_temperature1.attr,
  222. &attr_ppe_throttle_end.attr,
  223. &attr_ppe_throttle_begin.attr,
  224. &attr_ppe_throttle_full_stop.attr,
  225. NULL,
  226. };
  227. static struct attribute_group ppe_attribute_group = {
  228. .name = "thermal",
  229. .attrs = ppe_attributes,
  230. };
  231. /*
  232. * initialize throttling with default values
  233. */
  234. static int __init init_default_values(void)
  235. {
  236. int cpu;
  237. struct cbe_pmd_regs __iomem *pmd_regs;
  238. struct sys_device *sysdev;
  239. union ppe_spe_reg tpr;
  240. union spe_reg str1;
  241. u64 str2;
  242. union spe_reg cr1;
  243. u64 cr2;
  244. /* TPR defaults */
  245. /* ppe
  246. * 1F - no full stop
  247. * 08 - dynamic throttling starts if over 80 degrees
  248. * 03 - dynamic throttling ceases if below 70 degrees */
  249. tpr.ppe = 0x1F0803;
  250. /* spe
  251. * 10 - full stopped when over 96 degrees
  252. * 08 - dynamic throttling starts if over 80 degrees
  253. * 03 - dynamic throttling ceases if below 70 degrees
  254. */
  255. tpr.spe = 0x100803;
  256. /* STR defaults */
  257. /* str1
  258. * 10 - stop 16 of 32 cycles
  259. */
  260. str1.val = 0x1010101010101010ull;
  261. /* str2
  262. * 10 - stop 16 of 32 cycles
  263. */
  264. str2 = 0x10;
  265. /* CR defaults */
  266. /* cr1
  267. * 4 - normal operation
  268. */
  269. cr1.val = 0x0404040404040404ull;
  270. /* cr2
  271. * 4 - normal operation
  272. */
  273. cr2 = 0x04;
  274. for_each_possible_cpu (cpu) {
  275. pr_debug("processing cpu %d\n", cpu);
  276. sysdev = get_cpu_sysdev(cpu);
  277. if (!sysdev) {
  278. pr_info("invalid sysdev pointer for cbe_thermal\n");
  279. return -EINVAL;
  280. }
  281. pmd_regs = cbe_get_cpu_pmd_regs(sysdev->id);
  282. if (!pmd_regs) {
  283. pr_info("invalid CBE regs pointer for cbe_thermal\n");
  284. return -EINVAL;
  285. }
  286. out_be64(&pmd_regs->tm_str2, str2);
  287. out_be64(&pmd_regs->tm_str1.val, str1.val);
  288. out_be64(&pmd_regs->tm_tpr.val, tpr.val);
  289. out_be64(&pmd_regs->tm_cr1.val, cr1.val);
  290. out_be64(&pmd_regs->tm_cr2, cr2);
  291. }
  292. return 0;
  293. }
  294. static int __init thermal_init(void)
  295. {
  296. int rc = init_default_values();
  297. if (rc == 0) {
  298. spu_add_sysdev_attr_group(&spu_attribute_group);
  299. cpu_add_sysdev_attr_group(&ppe_attribute_group);
  300. }
  301. return rc;
  302. }
  303. module_init(thermal_init);
  304. static void __exit thermal_exit(void)
  305. {
  306. spu_remove_sysdev_attr_group(&spu_attribute_group);
  307. cpu_remove_sysdev_attr_group(&ppe_attribute_group);
  308. }
  309. module_exit(thermal_exit);
  310. MODULE_LICENSE("GPL");
  311. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");