model_206ax.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * From Coreboot file of same name
  4. *
  5. * Copyright (C) 2007-2009 coresystems GmbH
  6. * Copyright (C) 2011 The Chromium Authors
  7. */
  8. #include <common.h>
  9. #include <cpu.h>
  10. #include <dm.h>
  11. #include <fdtdec.h>
  12. #include <malloc.h>
  13. #include <asm/cpu.h>
  14. #include <asm/cpu_x86.h>
  15. #include <asm/msr.h>
  16. #include <asm/msr-index.h>
  17. #include <asm/mtrr.h>
  18. #include <asm/processor.h>
  19. #include <asm/speedstep.h>
  20. #include <asm/turbo.h>
  21. #include <asm/arch/model_206ax.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. static void enable_vmx(void)
  24. {
  25. struct cpuid_result regs;
  26. #ifdef CONFIG_ENABLE_VMX
  27. int enable = true;
  28. #else
  29. int enable = false;
  30. #endif
  31. msr_t msr;
  32. regs = cpuid(1);
  33. /* Check that the VMX is supported before reading or writing the MSR. */
  34. if (!((regs.ecx & CPUID_VMX) || (regs.ecx & CPUID_SMX)))
  35. return;
  36. msr = msr_read(MSR_IA32_FEATURE_CONTROL);
  37. if (msr.lo & (1 << 0)) {
  38. debug("VMX is locked, so %s will do nothing\n", __func__);
  39. /* VMX locked. If we set it again we get an illegal
  40. * instruction
  41. */
  42. return;
  43. }
  44. /* The IA32_FEATURE_CONTROL MSR may initialize with random values.
  45. * It must be cleared regardless of VMX config setting.
  46. */
  47. msr.hi = 0;
  48. msr.lo = 0;
  49. debug("%s VMX\n", enable ? "Enabling" : "Disabling");
  50. /*
  51. * Even though the Intel manual says you must set the lock bit in
  52. * addition to the VMX bit in order for VMX to work, it is incorrect.
  53. * Thus we leave it unlocked for the OS to manage things itself.
  54. * This is good for a few reasons:
  55. * - No need to reflash the bios just to toggle the lock bit.
  56. * - The VMX bits really really should match each other across cores,
  57. * so hard locking it on one while another has the opposite setting
  58. * can easily lead to crashes as code using VMX migrates between
  59. * them.
  60. * - Vendors that want to "upsell" from a bios that disables+locks to
  61. * one that doesn't is sleazy.
  62. * By leaving this to the OS (e.g. Linux), people can do exactly what
  63. * they want on the fly, and do it correctly (e.g. across multiple
  64. * cores).
  65. */
  66. if (enable) {
  67. msr.lo |= (1 << 2);
  68. if (regs.ecx & CPUID_SMX)
  69. msr.lo |= (1 << 1);
  70. }
  71. msr_write(MSR_IA32_FEATURE_CONTROL, msr);
  72. }
  73. /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
  74. static const u8 power_limit_time_sec_to_msr[] = {
  75. [0] = 0x00,
  76. [1] = 0x0a,
  77. [2] = 0x0b,
  78. [3] = 0x4b,
  79. [4] = 0x0c,
  80. [5] = 0x2c,
  81. [6] = 0x4c,
  82. [7] = 0x6c,
  83. [8] = 0x0d,
  84. [10] = 0x2d,
  85. [12] = 0x4d,
  86. [14] = 0x6d,
  87. [16] = 0x0e,
  88. [20] = 0x2e,
  89. [24] = 0x4e,
  90. [28] = 0x6e,
  91. [32] = 0x0f,
  92. [40] = 0x2f,
  93. [48] = 0x4f,
  94. [56] = 0x6f,
  95. [64] = 0x10,
  96. [80] = 0x30,
  97. [96] = 0x50,
  98. [112] = 0x70,
  99. [128] = 0x11,
  100. };
  101. /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
  102. static const u8 power_limit_time_msr_to_sec[] = {
  103. [0x00] = 0,
  104. [0x0a] = 1,
  105. [0x0b] = 2,
  106. [0x4b] = 3,
  107. [0x0c] = 4,
  108. [0x2c] = 5,
  109. [0x4c] = 6,
  110. [0x6c] = 7,
  111. [0x0d] = 8,
  112. [0x2d] = 10,
  113. [0x4d] = 12,
  114. [0x6d] = 14,
  115. [0x0e] = 16,
  116. [0x2e] = 20,
  117. [0x4e] = 24,
  118. [0x6e] = 28,
  119. [0x0f] = 32,
  120. [0x2f] = 40,
  121. [0x4f] = 48,
  122. [0x6f] = 56,
  123. [0x10] = 64,
  124. [0x30] = 80,
  125. [0x50] = 96,
  126. [0x70] = 112,
  127. [0x11] = 128,
  128. };
  129. int cpu_config_tdp_levels(void)
  130. {
  131. struct cpuid_result result;
  132. msr_t platform_info;
  133. /* Minimum CPU revision */
  134. result = cpuid(1);
  135. if (result.eax < IVB_CONFIG_TDP_MIN_CPUID)
  136. return 0;
  137. /* Bits 34:33 indicate how many levels supported */
  138. platform_info = msr_read(MSR_PLATFORM_INFO);
  139. return (platform_info.hi >> 1) & 3;
  140. }
  141. /*
  142. * Configure processor power limits if possible
  143. * This must be done AFTER set of BIOS_RESET_CPL
  144. */
  145. void set_power_limits(u8 power_limit_1_time)
  146. {
  147. msr_t msr = msr_read(MSR_PLATFORM_INFO);
  148. msr_t limit;
  149. unsigned power_unit;
  150. unsigned tdp, min_power, max_power, max_time;
  151. u8 power_limit_1_val;
  152. if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr))
  153. return;
  154. if (!(msr.lo & PLATFORM_INFO_SET_TDP))
  155. return;
  156. /* Get units */
  157. msr = msr_read(MSR_PKG_POWER_SKU_UNIT);
  158. power_unit = 2 << ((msr.lo & 0xf) - 1);
  159. /* Get power defaults for this SKU */
  160. msr = msr_read(MSR_PKG_POWER_SKU);
  161. tdp = msr.lo & 0x7fff;
  162. min_power = (msr.lo >> 16) & 0x7fff;
  163. max_power = msr.hi & 0x7fff;
  164. max_time = (msr.hi >> 16) & 0x7f;
  165. debug("CPU TDP: %u Watts\n", tdp / power_unit);
  166. if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
  167. power_limit_1_time = power_limit_time_msr_to_sec[max_time];
  168. if (min_power > 0 && tdp < min_power)
  169. tdp = min_power;
  170. if (max_power > 0 && tdp > max_power)
  171. tdp = max_power;
  172. power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
  173. /* Set long term power limit to TDP */
  174. limit.lo = 0;
  175. limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
  176. limit.lo |= PKG_POWER_LIMIT_EN;
  177. limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
  178. PKG_POWER_LIMIT_TIME_SHIFT;
  179. /* Set short term power limit to 1.25 * TDP */
  180. limit.hi = 0;
  181. limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
  182. limit.hi |= PKG_POWER_LIMIT_EN;
  183. /* Power limit 2 time is only programmable on SNB EP/EX */
  184. msr_write(MSR_PKG_POWER_LIMIT, limit);
  185. /* Use nominal TDP values for CPUs with configurable TDP */
  186. if (cpu_config_tdp_levels()) {
  187. msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
  188. limit.hi = 0;
  189. limit.lo = msr.lo & 0xff;
  190. msr_write(MSR_TURBO_ACTIVATION_RATIO, limit);
  191. }
  192. }
  193. static void configure_c_states(void)
  194. {
  195. struct cpuid_result result;
  196. msr_t msr;
  197. msr = msr_read(MSR_PMG_CST_CONFIG_CTL);
  198. msr.lo |= (1 << 28); /* C1 Auto Undemotion Enable */
  199. msr.lo |= (1 << 27); /* C3 Auto Undemotion Enable */
  200. msr.lo |= (1 << 26); /* C1 Auto Demotion Enable */
  201. msr.lo |= (1 << 25); /* C3 Auto Demotion Enable */
  202. msr.lo &= ~(1 << 10); /* Disable IO MWAIT redirection */
  203. msr.lo |= 7; /* No package C-state limit */
  204. msr_write(MSR_PMG_CST_CONFIG_CTL, msr);
  205. msr = msr_read(MSR_PMG_IO_CAPTURE_ADR);
  206. msr.lo &= ~0x7ffff;
  207. msr.lo |= (PMB0_BASE + 4); /* LVL_2 base address */
  208. msr.lo |= (2 << 16); /* CST Range: C7 is max C-state */
  209. msr_write(MSR_PMG_IO_CAPTURE_ADR, msr);
  210. msr = msr_read(MSR_MISC_PWR_MGMT);
  211. msr.lo &= ~(1 << 0); /* Enable P-state HW_ALL coordination */
  212. msr_write(MSR_MISC_PWR_MGMT, msr);
  213. msr = msr_read(MSR_POWER_CTL);
  214. msr.lo |= (1 << 18); /* Enable Energy Perf Bias MSR 0x1b0 */
  215. msr.lo |= (1 << 1); /* C1E Enable */
  216. msr.lo |= (1 << 0); /* Bi-directional PROCHOT# */
  217. msr_write(MSR_POWER_CTL, msr);
  218. /* C3 Interrupt Response Time Limit */
  219. msr.hi = 0;
  220. msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
  221. msr_write(MSR_PKGC3_IRTL, msr);
  222. /* C6 Interrupt Response Time Limit */
  223. msr.hi = 0;
  224. msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
  225. msr_write(MSR_PKGC6_IRTL, msr);
  226. /* C7 Interrupt Response Time Limit */
  227. msr.hi = 0;
  228. msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
  229. msr_write(MSR_PKGC7_IRTL, msr);
  230. /* Primary Plane Current Limit */
  231. msr = msr_read(MSR_PP0_CURRENT_CONFIG);
  232. msr.lo &= ~0x1fff;
  233. msr.lo |= PP0_CURRENT_LIMIT;
  234. msr_write(MSR_PP0_CURRENT_CONFIG, msr);
  235. /* Secondary Plane Current Limit */
  236. msr = msr_read(MSR_PP1_CURRENT_CONFIG);
  237. msr.lo &= ~0x1fff;
  238. result = cpuid(1);
  239. if (result.eax >= 0x30600)
  240. msr.lo |= PP1_CURRENT_LIMIT_IVB;
  241. else
  242. msr.lo |= PP1_CURRENT_LIMIT_SNB;
  243. msr_write(MSR_PP1_CURRENT_CONFIG, msr);
  244. }
  245. static int configure_thermal_target(struct udevice *dev)
  246. {
  247. int tcc_offset;
  248. msr_t msr;
  249. tcc_offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  250. "tcc-offset", 0);
  251. /* Set TCC activaiton offset if supported */
  252. msr = msr_read(MSR_PLATFORM_INFO);
  253. if ((msr.lo & (1 << 30)) && tcc_offset) {
  254. msr = msr_read(MSR_TEMPERATURE_TARGET);
  255. msr.lo &= ~(0xf << 24); /* Bits 27:24 */
  256. msr.lo |= (tcc_offset & 0xf) << 24;
  257. msr_write(MSR_TEMPERATURE_TARGET, msr);
  258. }
  259. return 0;
  260. }
  261. static void configure_misc(void)
  262. {
  263. msr_t msr;
  264. msr = msr_read(IA32_MISC_ENABLE);
  265. msr.lo |= (1 << 0); /* Fast String enable */
  266. msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
  267. msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
  268. msr_write(IA32_MISC_ENABLE, msr);
  269. /* Disable Thermal interrupts */
  270. msr.lo = 0;
  271. msr.hi = 0;
  272. msr_write(IA32_THERM_INTERRUPT, msr);
  273. /* Enable package critical interrupt only */
  274. msr.lo = 1 << 4;
  275. msr.hi = 0;
  276. msr_write(IA32_PACKAGE_THERM_INTERRUPT, msr);
  277. }
  278. static void enable_lapic_tpr(void)
  279. {
  280. msr_t msr;
  281. msr = msr_read(MSR_PIC_MSG_CONTROL);
  282. msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */
  283. msr_write(MSR_PIC_MSG_CONTROL, msr);
  284. }
  285. static void configure_dca_cap(void)
  286. {
  287. struct cpuid_result cpuid_regs;
  288. msr_t msr;
  289. /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */
  290. cpuid_regs = cpuid(1);
  291. if (cpuid_regs.ecx & (1 << 18)) {
  292. msr = msr_read(IA32_PLATFORM_DCA_CAP);
  293. msr.lo |= 1;
  294. msr_write(IA32_PLATFORM_DCA_CAP, msr);
  295. }
  296. }
  297. static void set_max_ratio(void)
  298. {
  299. msr_t msr, perf_ctl;
  300. perf_ctl.hi = 0;
  301. /* Check for configurable TDP option */
  302. if (cpu_config_tdp_levels()) {
  303. /* Set to nominal TDP ratio */
  304. msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
  305. perf_ctl.lo = (msr.lo & 0xff) << 8;
  306. } else {
  307. /* Platform Info bits 15:8 give max ratio */
  308. msr = msr_read(MSR_PLATFORM_INFO);
  309. perf_ctl.lo = msr.lo & 0xff00;
  310. }
  311. msr_write(MSR_IA32_PERF_CTL, perf_ctl);
  312. debug("model_x06ax: frequency set to %d\n",
  313. ((perf_ctl.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK);
  314. }
  315. static void set_energy_perf_bias(u8 policy)
  316. {
  317. msr_t msr;
  318. /* Energy Policy is bits 3:0 */
  319. msr = msr_read(IA32_ENERGY_PERFORMANCE_BIAS);
  320. msr.lo &= ~0xf;
  321. msr.lo |= policy & 0xf;
  322. msr_write(IA32_ENERGY_PERFORMANCE_BIAS, msr);
  323. debug("model_x06ax: energy policy set to %u\n", policy);
  324. }
  325. static void configure_mca(void)
  326. {
  327. msr_t msr;
  328. int i;
  329. msr.lo = 0;
  330. msr.hi = 0;
  331. /* This should only be done on a cold boot */
  332. for (i = 0; i < 7; i++)
  333. msr_write(IA32_MC0_STATUS + (i * 4), msr);
  334. }
  335. static int model_206ax_init(struct udevice *dev)
  336. {
  337. int ret;
  338. /* Clear out pending MCEs */
  339. configure_mca();
  340. /* Enable the local cpu apics */
  341. enable_lapic_tpr();
  342. /* Enable virtualization if enabled in CMOS */
  343. enable_vmx();
  344. /* Configure C States */
  345. configure_c_states();
  346. /* Configure Enhanced SpeedStep and Thermal Sensors */
  347. configure_misc();
  348. /* Thermal throttle activation offset */
  349. ret = configure_thermal_target(dev);
  350. if (ret) {
  351. debug("Cannot set thermal target\n");
  352. return ret;
  353. }
  354. /* Enable Direct Cache Access */
  355. configure_dca_cap();
  356. /* Set energy policy */
  357. set_energy_perf_bias(ENERGY_POLICY_NORMAL);
  358. /* Set Max Ratio */
  359. set_max_ratio();
  360. /* Enable Turbo */
  361. turbo_enable();
  362. return 0;
  363. }
  364. static int model_206ax_get_info(struct udevice *dev, struct cpu_info *info)
  365. {
  366. msr_t msr;
  367. msr = msr_read(MSR_IA32_PERF_CTL);
  368. info->cpu_freq = ((msr.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK * 1000000;
  369. info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU |
  370. 1 << CPU_FEAT_UCODE;
  371. return 0;
  372. }
  373. static int model_206ax_get_count(struct udevice *dev)
  374. {
  375. return 4;
  376. }
  377. static int cpu_x86_model_206ax_probe(struct udevice *dev)
  378. {
  379. if (dev->seq == 0)
  380. model_206ax_init(dev);
  381. return 0;
  382. }
  383. static const struct cpu_ops cpu_x86_model_206ax_ops = {
  384. .get_desc = cpu_x86_get_desc,
  385. .get_info = model_206ax_get_info,
  386. .get_count = model_206ax_get_count,
  387. .get_vendor = cpu_x86_get_vendor,
  388. };
  389. static const struct udevice_id cpu_x86_model_206ax_ids[] = {
  390. { .compatible = "intel,core-gen3" },
  391. { }
  392. };
  393. U_BOOT_DRIVER(cpu_x86_model_206ax_drv) = {
  394. .name = "cpu_x86_model_206ax",
  395. .id = UCLASS_CPU,
  396. .of_match = cpu_x86_model_206ax_ids,
  397. .bind = cpu_x86_bind,
  398. .probe = cpu_x86_model_206ax_probe,
  399. .ops = &cpu_x86_model_206ax_ops,
  400. .flags = DM_FLAG_PRE_RELOC,
  401. };