Debian Codegraph @

src/soc/intel/common/block/acpi/cpu_hybrid.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #include <acpi/acpi.h>
3 #include <acpi/acpigen.h>
4 #include <bootstate.h>
5 #include <commonlib/sort.h>
6 #include <cpu/x86/lapic.h>
7 #include <cpu/x86/mp.h>
8 #include <cpu/cpu.h>
9 #include <device/path.h>
10 #include <intelblocks/acpi.h>
11 #include <soc/cpu.h>
12 #include <stdio.h>
13 #include <types.h>
14
15 #define CPPC_NOM_FREQ_IDX 22
16 #define CPPC_NOM_PERF_IDX 3
17
18 struct cpu_apic_info_type {
19 /*
20 * Ordered APIC IDs based on core type.
21 * Array begins with BSP, then come all Performance Cores' APIC IDs,
22 * then followed by Efficient Cores's APIC IDs.
23 */
24 int32_t apic_ids[CONFIG_MAX_CPUS];
25
26 /* Total CPU count */
27 uint16_t total_cpu_cnt;
28
29 /*
30 * This will be used to identify the start of Efficient Cores's
31 * APIC ID list
32 */
33 uint16_t eff_cores_start;
34
35 /*
36 * Set to true if bsp is a performance core
37 */
38 bool is_bsp_perf;
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)
50 {
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 u32 bsp_lapicid = lapicid();
56
57 /* As per spec first comes BSP.
58 See 5.2.12.1 MADT Processor Local APIC / SAPIC Structure Entry Order
59 " platform firmware should list the boot processor as the first
60 processor entry in the MADT. "
61 */
62 cpu_apic_info.apic_ids[0] = bsp_lapicid;
63
64 for (i = 0; i < ARRAY_SIZE(cpu_apic_info.apic_ids); i++) {
65 if (!cpu_infos[i].cpu)
66 continue;
67 if (cpu_infos[i].cpu->path.apic.apic_id == bsp_lapicid) {
68 cpu_apic_info.is_bsp_perf =
69 cpu_infos[i].cpu->path.apic.core_type == CPU_TYPE_PERF;
70 continue;
71 }
72 if (cpu_infos[i].cpu->path.apic.core_type == CPU_TYPE_PERF)
73 cpu_apic_info.apic_ids[1 + perf_core_cnt++] =
74 cpu_infos[i].cpu->path.apic.apic_id;
75 else
76 eff_apic_ids[eff_core_cnt++] =
77 cpu_infos[i].cpu->path.apic.apic_id;
78 }
79
80 if (perf_core_cnt > 1)
81 bubblesort(cpu_apic_info.apic_ids + 1, perf_core_cnt, NUM_ASCENDING);
82
83 for (i = perf_core_cnt + 1; j < eff_core_cnt; i++, j++)
84 cpu_apic_info.apic_ids[i] = eff_apic_ids[j];
85
86 if (eff_core_cnt > 1)
87 bubblesort(&cpu_apic_info.apic_ids[perf_core_cnt + 1], eff_core_cnt, NUM_ASCENDING);
88
89 /* Populate total core count */
90 cpu_apic_info.total_cpu_cnt = perf_core_cnt + eff_core_cnt + 1;
91
92 cpu_apic_info.eff_cores_start = perf_core_cnt + 1;
93 }
94
95 static unsigned long acpi_create_madt_lapics_hybrid(unsigned long current)
96 {
97 size_t index;
98
99 for (index = 0; index < cpu_apic_info.total_cpu_cnt; index++)
100 current = acpi_create_madt_one_lapic(current, index,
101 cpu_apic_info.apic_ids[index]);
102
103 return current;
104 }
105
106 unsigned long acpi_create_madt_lapics_with_nmis_hybrid(unsigned long current)
107 {
108 current = acpi_create_madt_lapics_hybrid(current);
109 current = acpi_create_madt_lapic_nmis(current);
110 return current;
111 }
112
113 static enum cpu_perf_eff_type get_core_type(void)
114 {
115 return (get_soc_cpu_type() == CPUID_CORE_TYPE_INTEL_CORE) ?
116 CPU_TYPE_PERF : CPU_TYPE_EFF;
117 }
118
119 void set_dev_core_type(void)
120 {
121 struct cpu_info *info = cpu_info();
122 info->cpu->path.apic.core_type = get_core_type();
123 }
124
125 static void acpi_get_cpu_nomi_perf(u16 *eff_core_nom_perf, u16 *perf_core_nom_perf)
126 {
127 u8 max_non_turbo_ratio = cpu_get_max_non_turbo_ratio();
128 static u16 performance, efficient;
129
130 if (!performance)
131 soc_read_core_scaling_factors(&performance, &efficient);
132
133 *perf_core_nom_perf = (u16)((max_non_turbo_ratio * performance) / 100);
134 *eff_core_nom_perf = (u16)((max_non_turbo_ratio * efficient) / 100);
135 }
136
137 static u16 acpi_get_cpu_nominal_freq(void)
138 {
139 return cpu_get_max_non_turbo_ratio() * cpu_get_bus_frequency();
140 }
141
142 /* Updates Nominal Frequency and Nominal Performance */
143 static void acpigen_cppc_update_nominal_freq_perf(const char *pkg_path, s32 core_id)
144 {
145 u16 eff_core_nom_perf, perf_core_nom_perf;
146
147 if (!soc_is_nominal_freq_supported())
148 return;
149
150 acpi_get_cpu_nomi_perf(&eff_core_nom_perf, &perf_core_nom_perf);
151
152 if (core_id == 0 ? cpu_apic_info.is_bsp_perf : (core_id < cpu_apic_info.eff_cores_start))
153 acpigen_set_package_element_int(pkg_path, CPPC_NOM_PERF_IDX, perf_core_nom_perf);
154 else
155 acpigen_set_package_element_int(pkg_path, CPPC_NOM_PERF_IDX,
156 eff_core_nom_perf);
157
158 /* Update CPU's nominal frequency */
159 acpigen_set_package_element_int(pkg_path, CPPC_NOM_FREQ_IDX,
160 acpi_get_cpu_nominal_freq());
161 }
162
163 void acpigen_write_CPPC_hybrid_method(s32 core_id)
164 {
165 char pkg_path[16];
166
167 if (core_id == 0)
168 snprintf(pkg_path, sizeof(pkg_path), CPPC_PACKAGE_NAME, 0);
169 else
170 snprintf(pkg_path, sizeof(pkg_path),
171 "\\_SB." CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
172
173 acpigen_write_method("_CPC", 0);
174
175 /* Update nominal performance and nominal frequency */
176 acpigen_cppc_update_nominal_freq_perf(pkg_path, core_id);
177 acpigen_emit_byte(RETURN_OP);
178 acpigen_emit_namestring(pkg_path);
179 acpigen_pop_len();
180 }
181
182 BOOT_STATE_INIT_ENTRY(BS_DEV_INIT_CHIPS, BS_ON_EXIT, acpi_set_hybrid_cpu_apicid_order, NULL);