model_206ax.c 11 KB

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