Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 2 | /* |
| 3 | * From Coreboot file of the same name |
| 4 | * |
| 5 | * Copyright (C) 2011 The Chromium Authors. |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 10 | #include <asm/cpu.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 12 | #include <asm/msr.h> |
| 13 | #include <asm/processor.h> |
| 14 | #include <asm/turbo.h> |
| 15 | |
Simon Glass | a0c75f9 | 2017-01-16 07:04:17 -0700 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Bin Meng | 2ddb1a1 | 2017-08-17 01:10:42 -0700 | [diff] [blame] | 18 | #ifdef CONFIG_CPU_INTEL_TURBO_NOT_PACKAGE_SCOPED |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 19 | static inline int get_global_turbo_state(void) |
| 20 | { |
| 21 | return TURBO_UNKNOWN; |
| 22 | } |
| 23 | |
| 24 | static inline void set_global_turbo_state(int state) |
| 25 | { |
| 26 | } |
| 27 | #else |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 28 | static inline int get_global_turbo_state(void) |
| 29 | { |
Simon Glass | a0c75f9 | 2017-01-16 07:04:17 -0700 | [diff] [blame] | 30 | return gd->arch.turbo_state; |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static inline void set_global_turbo_state(int state) |
| 34 | { |
Simon Glass | a0c75f9 | 2017-01-16 07:04:17 -0700 | [diff] [blame] | 35 | gd->arch.turbo_state = state; |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 36 | } |
| 37 | #endif |
| 38 | |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 39 | /* gcc 7.3 does not wwant to drop strings, so use #ifdef */ |
| 40 | #ifndef CONFIG_TPL_BUILD |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 41 | static const char *const turbo_state_desc[] = { |
| 42 | [TURBO_UNKNOWN] = "unknown", |
| 43 | [TURBO_UNAVAILABLE] = "unavailable", |
| 44 | [TURBO_DISABLED] = "available but hidden", |
| 45 | [TURBO_ENABLED] = "available and visible" |
| 46 | }; |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 47 | #endif |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Determine the current state of Turbo and cache it for later. |
| 51 | * Turbo is a package level config so it does not need to be |
| 52 | * enabled on every core. |
| 53 | */ |
| 54 | int turbo_get_state(void) |
| 55 | { |
| 56 | struct cpuid_result cpuid_regs; |
| 57 | int turbo_en, turbo_cap; |
| 58 | msr_t msr; |
| 59 | int turbo_state = get_global_turbo_state(); |
| 60 | |
| 61 | /* Return cached state if available */ |
| 62 | if (turbo_state != TURBO_UNKNOWN) |
| 63 | return turbo_state; |
| 64 | |
| 65 | cpuid_regs = cpuid(CPUID_LEAF_PM); |
| 66 | turbo_cap = !!(cpuid_regs.eax & PM_CAP_TURBO_MODE); |
| 67 | |
Simon Glass | f6d00da | 2019-09-25 08:56:39 -0600 | [diff] [blame] | 68 | msr = msr_read(MSR_IA32_MISC_ENABLE); |
| 69 | turbo_en = !(msr.hi & MISC_DISABLE_TURBO); |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 70 | |
| 71 | if (!turbo_cap && turbo_en) { |
| 72 | /* Unavailable */ |
| 73 | turbo_state = TURBO_UNAVAILABLE; |
| 74 | } else if (!turbo_cap && !turbo_en) { |
| 75 | /* Available but disabled */ |
| 76 | turbo_state = TURBO_DISABLED; |
| 77 | } else if (turbo_cap && turbo_en) { |
| 78 | /* Available */ |
| 79 | turbo_state = TURBO_ENABLED; |
| 80 | } |
| 81 | |
| 82 | set_global_turbo_state(turbo_state); |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 83 | #ifndef CONFIG_TPL_BUILD |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 84 | debug("Turbo is %s\n", turbo_state_desc[turbo_state]); |
Simon Glass | 8b842be | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 85 | #endif |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 86 | return turbo_state; |
| 87 | } |
| 88 | |
| 89 | void turbo_enable(void) |
| 90 | { |
| 91 | msr_t msr; |
| 92 | |
| 93 | /* Only possible if turbo is available but hidden */ |
| 94 | if (turbo_get_state() == TURBO_DISABLED) { |
| 95 | /* Clear Turbo Disable bit in Misc Enables */ |
Simon Glass | f6d00da | 2019-09-25 08:56:39 -0600 | [diff] [blame] | 96 | msr = msr_read(MSR_IA32_MISC_ENABLE); |
| 97 | msr.hi &= ~MISC_DISABLE_TURBO; |
| 98 | msr_write(MSR_IA32_MISC_ENABLE, msr); |
Simon Glass | 18739e2 | 2014-11-14 18:18:43 -0700 | [diff] [blame] | 99 | |
| 100 | /* Update cached turbo state */ |
| 101 | set_global_turbo_state(TURBO_ENABLED); |
| 102 | debug("Turbo has been enabled\n"); |
| 103 | } |
| 104 | } |