model_206ax.c 11 KB

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