cbe_thermal.c 10 KB

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