cbe_thermal.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "cbe_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. const unsigned int *id;
  82. union spe_reg value;
  83. struct spu *spu;
  84. /* getting the id from the reg attribute will not work on future device-tree layouts
  85. * in future we should store the id to the spu struct and use it here */
  86. spu = container_of(sysdev, struct spu, sysdev);
  87. id = of_get_property(spu_devnode(spu), "reg", NULL);
  88. value.val = in_be64(&reg->val);
  89. return value.spe[*id];
  90. }
  91. static ssize_t spu_show_temp(struct sys_device *sysdev, char *buf)
  92. {
  93. u8 value;
  94. struct cbe_pmd_regs __iomem *pmd_regs;
  95. pmd_regs = get_pmd_regs(sysdev);
  96. value = spu_read_register_value(sysdev, &pmd_regs->ts_ctsr1);
  97. return sprintf(buf, "%d\n", reg_to_temp(value));
  98. }
  99. static ssize_t show_throttle(struct cbe_pmd_regs __iomem *pmd_regs, char *buf, int pos)
  100. {
  101. u64 value;
  102. value = in_be64(&pmd_regs->tm_tpr.val);
  103. /* access the corresponding byte */
  104. value >>= pos;
  105. value &= 0x3F;
  106. return sprintf(buf, "%d\n", reg_to_temp(value));
  107. }
  108. static ssize_t store_throttle(struct cbe_pmd_regs __iomem *pmd_regs, const char *buf, size_t size, int pos)
  109. {
  110. u64 reg_value;
  111. int temp;
  112. u64 new_value;
  113. int ret;
  114. ret = sscanf(buf, "%u", &temp);
  115. if (ret != 1 || temp < TEMP_MIN || temp > TEMP_MAX)
  116. return -EINVAL;
  117. new_value = temp_to_reg(temp);
  118. reg_value = in_be64(&pmd_regs->tm_tpr.val);
  119. /* zero out bits for new value */
  120. reg_value &= ~(0xffull << pos);
  121. /* set bits to new value */
  122. reg_value |= new_value << pos;
  123. out_be64(&pmd_regs->tm_tpr.val, reg_value);
  124. return size;
  125. }
  126. static ssize_t spu_show_throttle_end(struct sys_device *sysdev, char *buf)
  127. {
  128. return show_throttle(get_pmd_regs(sysdev), buf, 0);
  129. }
  130. static ssize_t spu_show_throttle_begin(struct sys_device *sysdev, char *buf)
  131. {
  132. return show_throttle(get_pmd_regs(sysdev), buf, 8);
  133. }
  134. static ssize_t spu_show_throttle_full_stop(struct sys_device *sysdev, char *buf)
  135. {
  136. return show_throttle(get_pmd_regs(sysdev), buf, 16);
  137. }
  138. static ssize_t spu_store_throttle_end(struct sys_device *sysdev, const char *buf, size_t size)
  139. {
  140. return store_throttle(get_pmd_regs(sysdev), buf, size, 0);
  141. }
  142. static ssize_t spu_store_throttle_begin(struct sys_device *sysdev, const char *buf, size_t size)
  143. {
  144. return store_throttle(get_pmd_regs(sysdev), buf, size, 8);
  145. }
  146. static ssize_t spu_store_throttle_full_stop(struct sys_device *sysdev, const char *buf, size_t size)
  147. {
  148. return store_throttle(get_pmd_regs(sysdev), buf, size, 16);
  149. }
  150. static ssize_t ppe_show_temp(struct sys_device *sysdev, char *buf, int pos)
  151. {
  152. struct cbe_pmd_regs __iomem *pmd_regs;
  153. u64 value;
  154. pmd_regs = cbe_get_cpu_pmd_regs(sysdev->id);
  155. value = in_be64(&pmd_regs->ts_ctsr2);
  156. value = (value >> pos) & 0x3f;
  157. return sprintf(buf, "%d\n", reg_to_temp(value));
  158. }
  159. /* shows the temperature of the DTS on the PPE,
  160. * located near the linear thermal sensor */
  161. static ssize_t ppe_show_temp0(struct sys_device *sysdev, char *buf)
  162. {
  163. return ppe_show_temp(sysdev, buf, 32);
  164. }
  165. /* shows the temperature of the second DTS on the PPE */
  166. static ssize_t ppe_show_temp1(struct sys_device *sysdev, char *buf)
  167. {
  168. return ppe_show_temp(sysdev, buf, 0);
  169. }
  170. static ssize_t ppe_show_throttle_end(struct sys_device *sysdev, char *buf)
  171. {
  172. return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 32);
  173. }
  174. static ssize_t ppe_show_throttle_begin(struct sys_device *sysdev, char *buf)
  175. {
  176. return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 40);
  177. }
  178. static ssize_t ppe_show_throttle_full_stop(struct sys_device *sysdev, char *buf)
  179. {
  180. return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 48);
  181. }
  182. static ssize_t ppe_store_throttle_end(struct sys_device *sysdev, const char *buf, size_t size)
  183. {
  184. return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 32);
  185. }
  186. static ssize_t ppe_store_throttle_begin(struct sys_device *sysdev, const char *buf, size_t size)
  187. {
  188. return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 40);
  189. }
  190. static ssize_t ppe_store_throttle_full_stop(struct sys_device *sysdev, const char *buf, size_t size)
  191. {
  192. return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 48);
  193. }
  194. static struct sysdev_attribute attr_spu_temperature = {
  195. .attr = {.name = "temperature", .mode = 0400 },
  196. .show = spu_show_temp,
  197. };
  198. static SYSDEV_PREFIX_ATTR(spu, throttle_end, 0600);
  199. static SYSDEV_PREFIX_ATTR(spu, throttle_begin, 0600);
  200. static SYSDEV_PREFIX_ATTR(spu, throttle_full_stop, 0600);
  201. static struct attribute *spu_attributes[] = {
  202. &attr_spu_temperature.attr,
  203. &attr_spu_throttle_end.attr,
  204. &attr_spu_throttle_begin.attr,
  205. &attr_spu_throttle_full_stop.attr,
  206. NULL,
  207. };
  208. static struct attribute_group spu_attribute_group = {
  209. .name = "thermal",
  210. .attrs = spu_attributes,
  211. };
  212. static struct sysdev_attribute attr_ppe_temperature0 = {
  213. .attr = {.name = "temperature0", .mode = 0400 },
  214. .show = ppe_show_temp0,
  215. };
  216. static struct sysdev_attribute attr_ppe_temperature1 = {
  217. .attr = {.name = "temperature1", .mode = 0400 },
  218. .show = ppe_show_temp1,
  219. };
  220. static SYSDEV_PREFIX_ATTR(ppe, throttle_end, 0600);
  221. static SYSDEV_PREFIX_ATTR(ppe, throttle_begin, 0600);
  222. static SYSDEV_PREFIX_ATTR(ppe, throttle_full_stop, 0600);
  223. static struct attribute *ppe_attributes[] = {
  224. &attr_ppe_temperature0.attr,
  225. &attr_ppe_temperature1.attr,
  226. &attr_ppe_throttle_end.attr,
  227. &attr_ppe_throttle_begin.attr,
  228. &attr_ppe_throttle_full_stop.attr,
  229. NULL,
  230. };
  231. static struct attribute_group ppe_attribute_group = {
  232. .name = "thermal",
  233. .attrs = ppe_attributes,
  234. };
  235. /*
  236. * initialize throttling with default values
  237. */
  238. static void __init init_default_values(void)
  239. {
  240. int cpu;
  241. struct cbe_pmd_regs __iomem *pmd_regs;
  242. struct sys_device *sysdev;
  243. union ppe_spe_reg tpr;
  244. union spe_reg str1;
  245. u64 str2;
  246. union spe_reg cr1;
  247. u64 cr2;
  248. /* TPR defaults */
  249. /* ppe
  250. * 1F - no full stop
  251. * 08 - dynamic throttling starts if over 80 degrees
  252. * 03 - dynamic throttling ceases if below 70 degrees */
  253. tpr.ppe = 0x1F0803;
  254. /* spe
  255. * 10 - full stopped when over 96 degrees
  256. * 08 - dynamic throttling starts if over 80 degrees
  257. * 03 - dynamic throttling ceases if below 70 degrees
  258. */
  259. tpr.spe = 0x100803;
  260. /* STR defaults */
  261. /* str1
  262. * 10 - stop 16 of 32 cycles
  263. */
  264. str1.val = 0x1010101010101010ull;
  265. /* str2
  266. * 10 - stop 16 of 32 cycles
  267. */
  268. str2 = 0x10;
  269. /* CR defaults */
  270. /* cr1
  271. * 4 - normal operation
  272. */
  273. cr1.val = 0x0404040404040404ull;
  274. /* cr2
  275. * 4 - normal operation
  276. */
  277. cr2 = 0x04;
  278. for_each_possible_cpu (cpu) {
  279. pr_debug("processing cpu %d\n", cpu);
  280. sysdev = get_cpu_sysdev(cpu);
  281. pmd_regs = cbe_get_cpu_pmd_regs(sysdev->id);
  282. out_be64(&pmd_regs->tm_str2, str2);
  283. out_be64(&pmd_regs->tm_str1.val, str1.val);
  284. out_be64(&pmd_regs->tm_tpr.val, tpr.val);
  285. out_be64(&pmd_regs->tm_cr1.val, cr1.val);
  286. out_be64(&pmd_regs->tm_cr2, cr2);
  287. }
  288. }
  289. static int __init thermal_init(void)
  290. {
  291. init_default_values();
  292. spu_add_sysdev_attr_group(&spu_attribute_group);
  293. cpu_add_sysdev_attr_group(&ppe_attribute_group);
  294. return 0;
  295. }
  296. module_init(thermal_init);
  297. static void __exit thermal_exit(void)
  298. {
  299. spu_remove_sysdev_attr_group(&spu_attribute_group);
  300. cpu_remove_sysdev_attr_group(&ppe_attribute_group);
  301. }
  302. module_exit(thermal_exit);
  303. MODULE_LICENSE("GPL");
  304. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");