p4tcc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2005 Nate Lawson
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * Throttle clock frequency by using the thermal control circuit. This
  30. * operates independently of SpeedStep and ACPI throttling and is supported
  31. * on Pentium 4 and later models (feature TM).
  32. *
  33. * Reference: Intel Developer's manual v.3 #245472-012
  34. *
  35. * The original version of this driver was written by Ted Unangst for
  36. * OpenBSD and imported by Maxim Sobolev. It was rewritten by Nate Lawson
  37. * for use with the cpufreq framework.
  38. */
  39. #include <sys/param.h>
  40. #include <sys/systm.h>
  41. #include <sys/bus.h>
  42. #include <sys/cpu.h>
  43. #include <sys/kernel.h>
  44. #include <sys/module.h>
  45. #include <machine/md_var.h>
  46. #include <machine/specialreg.h>
  47. #include "cpufreq_if.h"
  48. #include <contrib/dev/acpica/include/acpi.h>
  49. #include <dev/acpica/acpivar.h>
  50. #include "acpi_if.h"
  51. struct p4tcc_softc {
  52. device_t dev;
  53. int set_count;
  54. int lowest_val;
  55. int auto_mode;
  56. };
  57. #define TCC_NUM_SETTINGS 8
  58. #define TCC_ENABLE_ONDEMAND (1<<4)
  59. #define TCC_REG_OFFSET 1
  60. #define TCC_SPEED_PERCENT(x) ((10000 * (x)) / TCC_NUM_SETTINGS)
  61. static int p4tcc_features(driver_t *driver, u_int *features);
  62. static void p4tcc_identify(driver_t *driver, device_t parent);
  63. static int p4tcc_probe(device_t dev);
  64. static int p4tcc_attach(device_t dev);
  65. static int p4tcc_detach(device_t dev);
  66. static int p4tcc_settings(device_t dev, struct cf_setting *sets,
  67. int *count);
  68. static int p4tcc_set(device_t dev, const struct cf_setting *set);
  69. static int p4tcc_get(device_t dev, struct cf_setting *set);
  70. static int p4tcc_type(device_t dev, int *type);
  71. static device_method_t p4tcc_methods[] = {
  72. /* Device interface */
  73. DEVMETHOD(device_identify, p4tcc_identify),
  74. DEVMETHOD(device_probe, p4tcc_probe),
  75. DEVMETHOD(device_attach, p4tcc_attach),
  76. DEVMETHOD(device_detach, p4tcc_detach),
  77. /* cpufreq interface */
  78. DEVMETHOD(cpufreq_drv_set, p4tcc_set),
  79. DEVMETHOD(cpufreq_drv_get, p4tcc_get),
  80. DEVMETHOD(cpufreq_drv_type, p4tcc_type),
  81. DEVMETHOD(cpufreq_drv_settings, p4tcc_settings),
  82. /* ACPI interface */
  83. DEVMETHOD(acpi_get_features, p4tcc_features),
  84. {0, 0}
  85. };
  86. static driver_t p4tcc_driver = {
  87. "p4tcc",
  88. p4tcc_methods,
  89. sizeof(struct p4tcc_softc),
  90. };
  91. DRIVER_MODULE(p4tcc, cpu, p4tcc_driver, 0, 0);
  92. static int
  93. p4tcc_features(driver_t *driver, u_int *features)
  94. {
  95. /* Notify the ACPI CPU that we support direct access to MSRs */
  96. *features = ACPI_CAP_THR_MSRS;
  97. return (0);
  98. }
  99. static void
  100. p4tcc_identify(driver_t *driver, device_t parent)
  101. {
  102. if ((cpu_feature & (CPUID_ACPI | CPUID_TM)) != (CPUID_ACPI | CPUID_TM))
  103. return;
  104. /* Make sure we're not being doubly invoked. */
  105. if (device_find_child(parent, "p4tcc", -1) != NULL)
  106. return;
  107. /*
  108. * We attach a p4tcc child for every CPU since settings need to
  109. * be performed on every CPU in the SMP case. See section 13.15.3
  110. * of the IA32 Intel Architecture Software Developer's Manual,
  111. * Volume 3, for more info.
  112. */
  113. if (BUS_ADD_CHILD(parent, 10, "p4tcc", device_get_unit(parent))
  114. == NULL)
  115. device_printf(parent, "add p4tcc child failed\n");
  116. }
  117. static int
  118. p4tcc_probe(device_t dev)
  119. {
  120. if (resource_disabled("p4tcc", 0))
  121. return (ENXIO);
  122. device_set_desc(dev, "CPU Frequency Thermal Control");
  123. return (0);
  124. }
  125. static int
  126. p4tcc_attach(device_t dev)
  127. {
  128. struct p4tcc_softc *sc;
  129. struct cf_setting set;
  130. sc = device_get_softc(dev);
  131. sc->dev = dev;
  132. sc->set_count = TCC_NUM_SETTINGS;
  133. /*
  134. * On boot, the TCC is usually in Automatic mode where reading the
  135. * current performance level is likely to produce bogus results.
  136. * We record that state here and don't trust the contents of the
  137. * status MSR until we've set it ourselves.
  138. */
  139. sc->auto_mode = TRUE;
  140. /*
  141. * XXX: After a cursory glance at various Intel specification
  142. * XXX: updates it seems like these tests for errata is bogus.
  143. * XXX: As far as I can tell, the failure mode is benign, in
  144. * XXX: that cpus with no errata will have their bottom two
  145. * XXX: STPCLK# rates disabled, so rather than waste more time
  146. * XXX: hunting down intel docs, just document it and punt. /phk
  147. */
  148. switch (cpu_id & 0xff) {
  149. case 0x22:
  150. case 0x24:
  151. case 0x25:
  152. case 0x27:
  153. case 0x29:
  154. /*
  155. * These CPU models hang when set to 12.5%.
  156. * See Errata O50, P44, and Z21.
  157. */
  158. sc->set_count -= 1;
  159. break;
  160. case 0x07: /* errata N44 and P18 */
  161. case 0x0a:
  162. case 0x12:
  163. case 0x13:
  164. case 0x62: /* Pentium D B1: errata AA21 */
  165. case 0x64: /* Pentium D C1: errata AA21 */
  166. case 0x65: /* Pentium D D0: errata AA21 */
  167. /*
  168. * These CPU models hang when set to 12.5% or 25%.
  169. * See Errata N44, P18l and AA21.
  170. */
  171. sc->set_count -= 2;
  172. break;
  173. }
  174. sc->lowest_val = TCC_NUM_SETTINGS - sc->set_count + 1;
  175. /*
  176. * Before we finish attach, switch to 100%. It's possible the BIOS
  177. * set us to a lower rate. The user can override this after boot.
  178. */
  179. set.freq = 10000;
  180. p4tcc_set(dev, &set);
  181. cpufreq_register(dev);
  182. return (0);
  183. }
  184. static int
  185. p4tcc_detach(device_t dev)
  186. {
  187. struct cf_setting set;
  188. int error;
  189. error = cpufreq_unregister(dev);
  190. if (error)
  191. return (error);
  192. /*
  193. * Before we finish detach, switch to Automatic mode.
  194. */
  195. set.freq = 10000;
  196. p4tcc_set(dev, &set);
  197. return(0);
  198. }
  199. static int
  200. p4tcc_settings(device_t dev, struct cf_setting *sets, int *count)
  201. {
  202. struct p4tcc_softc *sc;
  203. int i, val;
  204. sc = device_get_softc(dev);
  205. if (sets == NULL || count == NULL)
  206. return (EINVAL);
  207. if (*count < sc->set_count)
  208. return (E2BIG);
  209. /* Return a list of valid settings for this driver. */
  210. memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * sc->set_count);
  211. val = TCC_NUM_SETTINGS;
  212. for (i = 0; i < sc->set_count; i++, val--) {
  213. sets[i].freq = TCC_SPEED_PERCENT(val);
  214. sets[i].dev = dev;
  215. }
  216. *count = sc->set_count;
  217. return (0);
  218. }
  219. static int
  220. p4tcc_set(device_t dev, const struct cf_setting *set)
  221. {
  222. struct p4tcc_softc *sc;
  223. uint64_t mask, msr;
  224. int val;
  225. if (set == NULL)
  226. return (EINVAL);
  227. sc = device_get_softc(dev);
  228. /*
  229. * Validate requested state converts to a setting that is an integer
  230. * from [sc->lowest_val .. TCC_NUM_SETTINGS].
  231. */
  232. val = set->freq * TCC_NUM_SETTINGS / 10000;
  233. if (val * 10000 != set->freq * TCC_NUM_SETTINGS ||
  234. val < sc->lowest_val || val > TCC_NUM_SETTINGS)
  235. return (EINVAL);
  236. /*
  237. * Read the current register and mask off the old setting and
  238. * On-Demand bit. If the new val is < 100%, set it and the On-Demand
  239. * bit, otherwise just return to Automatic mode.
  240. */
  241. msr = rdmsr(MSR_THERM_CONTROL);
  242. mask = (TCC_NUM_SETTINGS - 1) << TCC_REG_OFFSET;
  243. msr &= ~(mask | TCC_ENABLE_ONDEMAND);
  244. if (val < TCC_NUM_SETTINGS)
  245. msr |= (val << TCC_REG_OFFSET) | TCC_ENABLE_ONDEMAND;
  246. wrmsr(MSR_THERM_CONTROL, msr);
  247. /*
  248. * Record whether we're now in Automatic or On-Demand mode. We have
  249. * to cache this since there is no reliable way to check if TCC is in
  250. * Automatic mode (i.e., at 100% or possibly 50%). Reading bit 4 of
  251. * the ACPI Thermal Monitor Control Register produces 0 no matter
  252. * what the current mode.
  253. */
  254. if (msr & TCC_ENABLE_ONDEMAND)
  255. sc->auto_mode = FALSE;
  256. else
  257. sc->auto_mode = TRUE;
  258. return (0);
  259. }
  260. static int
  261. p4tcc_get(device_t dev, struct cf_setting *set)
  262. {
  263. struct p4tcc_softc *sc;
  264. uint64_t msr;
  265. int val;
  266. if (set == NULL)
  267. return (EINVAL);
  268. sc = device_get_softc(dev);
  269. /*
  270. * Read the current register and extract the current setting. If
  271. * in automatic mode, assume we're at TCC_NUM_SETTINGS (100%).
  272. *
  273. * XXX This is not completely reliable since at high temperatures
  274. * the CPU may be automatically throttling to 50% but it's the best
  275. * we can do.
  276. */
  277. if (!sc->auto_mode) {
  278. msr = rdmsr(MSR_THERM_CONTROL);
  279. val = (msr >> TCC_REG_OFFSET) & (TCC_NUM_SETTINGS - 1);
  280. } else
  281. val = TCC_NUM_SETTINGS;
  282. memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
  283. set->freq = TCC_SPEED_PERCENT(val);
  284. set->dev = dev;
  285. return (0);
  286. }
  287. static int
  288. p4tcc_type(device_t dev, int *type)
  289. {
  290. if (type == NULL)
  291. return (EINVAL);
  292. *type = CPUFREQ_TYPE_RELATIVE;
  293. return (0);
  294. }