model_206ax.c 11 KB

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