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