Sridahr Siricilla | cd1cd8d | 2021-11-11 01:29:40 +0530 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 2 | #include <acpi/acpi.h> |
Sridahr Siricilla | cd1cd8d | 2021-11-11 01:29:40 +0530 | [diff] [blame] | 3 | #include <acpi/acpigen.h> |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 4 | #include <bootstate.h> |
| 5 | #include <commonlib/sort.h> |
Sridahr Siricilla | cd1cd8d | 2021-11-11 01:29:40 +0530 | [diff] [blame] | 6 | #include <cpu/x86/lapic.h> |
| 7 | #include <cpu/x86/mp.h> |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 8 | #include <cpu/cpu.h> |
| 9 | #include <device/path.h> |
| 10 | #include <intelblocks/acpi.h> |
| 11 | #include <soc/cpu.h> |
| 12 | #include <types.h> |
Sridahr Siricilla | cd1cd8d | 2021-11-11 01:29:40 +0530 | [diff] [blame] | 13 | |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 14 | #define CPPC_NOM_FREQ_IDX 22 |
| 15 | #define CPPC_NOM_PERF_IDX 3 |
| 16 | |
Sridhar Siricilla | 51c75ac | 2022-01-29 14:26:22 +0530 | [diff] [blame] | 17 | enum cpu_perf_eff_type { |
Sridhar Siricilla | 4c3014f | 2023-01-03 11:29:08 +0530 | [diff] [blame] | 18 | CPU_TYPE_EFF, |
| 19 | CPU_TYPE_PERF, |
Sridhar Siricilla | 51c75ac | 2022-01-29 14:26:22 +0530 | [diff] [blame] | 20 | }; |
| 21 | |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 22 | struct cpu_apic_info_type { |
| 23 | /* |
| 24 | * Ordered APIC IDs based on core type. |
| 25 | * Array begins with Performance Cores' APIC IDs, |
| 26 | * then followed by Efficeint Cores's APIC IDs. |
| 27 | */ |
| 28 | int32_t apic_ids[CONFIG_MAX_CPUS]; |
Sridahr Siricilla | cd1cd8d | 2021-11-11 01:29:40 +0530 | [diff] [blame] | 29 | |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 30 | /* Total CPU count */ |
Sridhar Siricilla | 02b39ef | 2023-04-09 17:40:43 +0530 | [diff] [blame] | 31 | uint16_t total_cpu_cnt; |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Total Performance core count. This will be used |
| 35 | * to identify the start of Efficient Cores's |
| 36 | * APIC ID list |
| 37 | */ |
Sridhar Siricilla | 02b39ef | 2023-04-09 17:40:43 +0530 | [diff] [blame] | 38 | uint16_t perf_cpu_cnt; |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static struct cpu_apic_info_type cpu_apic_info; |
| 42 | |
| 43 | /* |
| 44 | * The function orders APIC IDs such that orders first Performance cores and then |
| 45 | * Efficient cores' APIC IDs in ascending order. Also calculates total number of |
| 46 | * Performance cores and all cores count in the system and populates the information |
| 47 | * in the cpu_apic_info sturct. |
| 48 | */ |
| 49 | static void acpi_set_hybrid_cpu_apicid_order(void *unused) |
Sridahr Siricilla | cd1cd8d | 2021-11-11 01:29:40 +0530 | [diff] [blame] | 50 | { |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 51 | size_t perf_core_cnt = 0, eff_core_cnt = 0; |
| 52 | int32_t eff_apic_ids[CONFIG_MAX_CPUS] = {0}; |
| 53 | extern struct cpu_info cpu_infos[]; |
| 54 | uint32_t i, j = 0; |
| 55 | |
| 56 | for (i = 0; i < ARRAY_SIZE(cpu_apic_info.apic_ids); i++) { |
| 57 | if (cpu_infos[i].cpu->path.apic.core_type == CPU_TYPE_PERF) |
| 58 | cpu_apic_info.apic_ids[perf_core_cnt++] = |
| 59 | cpu_infos[i].cpu->path.apic.apic_id; |
| 60 | else |
| 61 | eff_apic_ids[eff_core_cnt++] = |
| 62 | cpu_infos[i].cpu->path.apic.apic_id; |
| 63 | } |
| 64 | |
| 65 | if (perf_core_cnt > 1) |
| 66 | bubblesort(cpu_apic_info.apic_ids, perf_core_cnt, NUM_ASCENDING); |
| 67 | |
| 68 | for (i = perf_core_cnt; j < eff_core_cnt; i++, j++) |
| 69 | cpu_apic_info.apic_ids[i] = eff_apic_ids[j]; |
| 70 | |
| 71 | if (eff_core_cnt > 1) |
| 72 | bubblesort(&cpu_apic_info.apic_ids[perf_core_cnt], eff_core_cnt, NUM_ASCENDING); |
| 73 | |
| 74 | /* Populate total core count */ |
| 75 | cpu_apic_info.total_cpu_cnt = perf_core_cnt + eff_core_cnt; |
| 76 | |
| 77 | cpu_apic_info.perf_cpu_cnt = perf_core_cnt; |
Sridahr Siricilla | cd1cd8d | 2021-11-11 01:29:40 +0530 | [diff] [blame] | 78 | } |
| 79 | |
Sridhar Siricilla | 7301cfa | 2023-01-19 18:50:09 +0530 | [diff] [blame] | 80 | static unsigned long acpi_create_madt_lapics_hybrid(unsigned long current) |
| 81 | { |
| 82 | size_t index; |
| 83 | |
Kyösti Mälkki | 2e9f0d3 | 2023-04-07 23:05:46 +0300 | [diff] [blame] | 84 | for (index = 0; index < cpu_apic_info.total_cpu_cnt; index++) |
| 85 | current = acpi_create_madt_one_lapic(current, index, |
| 86 | cpu_apic_info.apic_ids[index]); |
Sridhar Siricilla | 7301cfa | 2023-01-19 18:50:09 +0530 | [diff] [blame] | 87 | |
| 88 | return current; |
| 89 | } |
| 90 | |
| 91 | unsigned long acpi_create_madt_lapics_with_nmis_hybrid(unsigned long current) |
| 92 | { |
Sridhar Siricilla | 7301cfa | 2023-01-19 18:50:09 +0530 | [diff] [blame] | 93 | current = acpi_create_madt_lapics_hybrid(current); |
Kyösti Mälkki | 9ac1fb7 | 2023-04-07 22:39:53 +0300 | [diff] [blame] | 94 | current = acpi_create_madt_lapic_nmis(current); |
Sridhar Siricilla | 7301cfa | 2023-01-19 18:50:09 +0530 | [diff] [blame] | 95 | return current; |
| 96 | } |
| 97 | |
Sridhar Siricilla | 1b04e81 | 2023-03-30 09:51:28 +0530 | [diff] [blame] | 98 | static enum cpu_perf_eff_type get_core_type(void) |
| 99 | { |
| 100 | return (get_soc_cpu_type() == CPUID_CORE_TYPE_INTEL_CORE) ? |
| 101 | CPU_TYPE_PERF : CPU_TYPE_EFF; |
| 102 | } |
| 103 | |
| 104 | void set_dev_core_type(void) |
| 105 | { |
| 106 | struct cpu_info *info = cpu_info(); |
| 107 | info->cpu->path.apic.core_type = get_core_type(); |
| 108 | } |
| 109 | |
Sridhar Siricilla | 4c3014f | 2023-01-03 11:29:08 +0530 | [diff] [blame] | 110 | static void acpi_get_cpu_nomi_perf(u16 *eff_core_nom_perf, u16 *perf_core_nom_perf) |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 111 | { |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 112 | u8 max_non_turbo_ratio = cpu_get_max_non_turbo_ratio(); |
| 113 | |
Sridhar Siricilla | d9c8269 | 2023-01-05 17:08:17 +0530 | [diff] [blame] | 114 | _Static_assert(CONFIG_SOC_INTEL_PERFORMANCE_CORE_SCALE_FACTOR != 0, |
| 115 | "CONFIG_SOC_INTEL_PERFORMANCE_CORE_SCALE_FACTOR must not be zero"); |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 116 | |
Sridhar Siricilla | d9c8269 | 2023-01-05 17:08:17 +0530 | [diff] [blame] | 117 | _Static_assert(CONFIG_SOC_INTEL_EFFICIENT_CORE_SCALE_FACTOR != 0, |
| 118 | "CONFIG_SOC_INTEL_EFFICIENT_CORE_SCALE_FACTOR must not be zero"); |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 119 | |
Sridhar Siricilla | d9c8269 | 2023-01-05 17:08:17 +0530 | [diff] [blame] | 120 | *perf_core_nom_perf = (u16)((max_non_turbo_ratio * |
| 121 | CONFIG_SOC_INTEL_PERFORMANCE_CORE_SCALE_FACTOR) / 100); |
| 122 | |
| 123 | *eff_core_nom_perf = (u16)((max_non_turbo_ratio * |
| 124 | CONFIG_SOC_INTEL_EFFICIENT_CORE_SCALE_FACTOR) / 100); |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static u16 acpi_get_cpu_nominal_freq(void) |
| 128 | { |
| 129 | return cpu_get_max_non_turbo_ratio() * cpu_get_bus_frequency(); |
| 130 | } |
| 131 | |
| 132 | /* Updates Nominal Frequency and Nominal Performance */ |
| 133 | static void acpigen_cppc_update_nominal_freq_perf(const char *pkg_path, s32 core_id) |
| 134 | { |
Sridhar Siricilla | 4c3014f | 2023-01-03 11:29:08 +0530 | [diff] [blame] | 135 | u16 eff_core_nom_perf, perf_core_nom_perf; |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 136 | |
| 137 | if (!soc_is_nominal_freq_supported()) |
| 138 | return; |
| 139 | |
Sridhar Siricilla | 4c3014f | 2023-01-03 11:29:08 +0530 | [diff] [blame] | 140 | acpi_get_cpu_nomi_perf(&eff_core_nom_perf, &perf_core_nom_perf); |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 141 | |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 142 | if (core_id < cpu_apic_info.perf_cpu_cnt) |
Sridhar Siricilla | 4c3014f | 2023-01-03 11:29:08 +0530 | [diff] [blame] | 143 | acpigen_set_package_element_int(pkg_path, CPPC_NOM_PERF_IDX, perf_core_nom_perf); |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 144 | else |
| 145 | acpigen_set_package_element_int(pkg_path, CPPC_NOM_PERF_IDX, |
Sridhar Siricilla | 4c3014f | 2023-01-03 11:29:08 +0530 | [diff] [blame] | 146 | eff_core_nom_perf); |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 147 | |
| 148 | /* Update CPU's nominal frequency */ |
| 149 | acpigen_set_package_element_int(pkg_path, CPPC_NOM_FREQ_IDX, |
| 150 | acpi_get_cpu_nominal_freq()); |
| 151 | } |
| 152 | |
| 153 | void acpigen_write_CPPC_hybrid_method(s32 core_id) |
| 154 | { |
| 155 | char pkg_path[16]; |
| 156 | |
| 157 | if (core_id == 0) |
| 158 | snprintf(pkg_path, sizeof(pkg_path), CPPC_PACKAGE_NAME, 0); |
| 159 | else |
| 160 | snprintf(pkg_path, sizeof(pkg_path), |
Felix Held | f0a8b04 | 2023-05-12 15:55:06 +0200 | [diff] [blame^] | 161 | "\\_SB." CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0); |
Sridhar Siricilla | 1173612 | 2021-11-15 17:12:49 +0530 | [diff] [blame] | 162 | |
| 163 | acpigen_write_method("_CPC", 0); |
| 164 | |
| 165 | /* Update nominal performance and nominal frequency */ |
| 166 | acpigen_cppc_update_nominal_freq_perf(pkg_path, core_id); |
| 167 | acpigen_emit_byte(RETURN_OP); |
| 168 | acpigen_emit_namestring(pkg_path); |
| 169 | acpigen_pop_len(); |
| 170 | } |
| 171 | |
Sridhar Siricilla | f8ac3dd | 2023-03-30 11:25:36 +0530 | [diff] [blame] | 172 | BOOT_STATE_INIT_ENTRY(BS_DEV_INIT_CHIPS, BS_ON_EXIT, acpi_set_hybrid_cpu_apicid_order, NULL); |