blob: f0be956cd517d499fcca98ad7f7eb78415768140 [file] [log] [blame]
Benjamin Doron69bc2cc2023-06-14 19:03:04 -04001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <arch/cache.h>
4#include <arch/lib_helpers.h>
5#include <arch/smc.h>
6#include <console/console.h>
7#include <smbios.h>
Elyes Haouasbdd03c22024-05-27 11:20:07 +02008#include <stdio.h>
Benjamin Doron69bc2cc2023-06-14 19:03:04 -04009
10static void smbios_processor_id(u32 *processor_id)
11{
12 uint32_t jep106code, soc_revision;
13 uint64_t midr_el1;
14
15 if (smccc_supports_arch_soc_id()) {
16 smccc_arch_soc_id(&jep106code, &soc_revision);
17 processor_id[0] = jep106code;
18 processor_id[1] = soc_revision;
19 } else {
20 midr_el1 = raw_read_midr_el1();
21 processor_id[0] = midr_el1;
22 processor_id[1] = 0;
23 }
24}
25
26static int smbios_processor_manufacturer(u8 *start)
27{
28 char midr_el1_implementer;
29 char buf[32];
30
31 // [31:24] - Implementer code
32 midr_el1_implementer = (raw_read_midr_el1() & 0xff000000) >> 24;
33
34 snprintf(buf, sizeof(buf), "CPU implementer %x", midr_el1_implementer);
35 return smbios_add_string(start, buf);
36}
37
38static int smbios_processor_name(u8 *start)
39{
40 uint16_t midr_el1_partnumber;
41 char buf[32];
42
43 // [15:4] - PartNum
44 midr_el1_partnumber = (raw_read_midr_el1() & 0xfff0) >> 4;
45
46 snprintf(buf, sizeof(buf), "ARMv8 Processor rev %d", midr_el1_partnumber);
47 return smbios_add_string(start, buf);
48}
49
50#define MAX_CPUS_ENABLED(cpus) (cpus > 0xff ? 0xff : cpus)
51
52/* NOTE: Not handling big.LITTLE clusters. Consider using MP services (not yet) or the DSU. */
53int smbios_write_type4(unsigned long *current, int handle)
54{
55 static unsigned int cnt = 0;
56 char buf[8];
57 uint16_t characteristics = 0;
58 unsigned int cpu_voltage;
59
60 struct smbios_type4 *t = smbios_carve_table(*current, SMBIOS_PROCESSOR_INFORMATION,
61 sizeof(*t), handle);
62
63 snprintf(buf, sizeof(buf), "CPU%d", cnt++);
64 t->socket_designation = smbios_add_string(t->eos, buf);
65
66 smbios_processor_id(t->processor_id);
67 t->processor_manufacturer = smbios_processor_manufacturer(t->eos);
68 t->processor_version = smbios_processor_name(t->eos);
Elyes Haouasf38c9402024-05-25 11:59:13 +020069 t->processor_family = SMBIOS_PROCESSOR_FAMILY_FROM_FAMILY2;
70 t->processor_family2 = SMBIOS_PROCESSOR_FAMILY2_ARMV8;
Elyes Haouasb72f5942024-05-25 11:17:15 +020071 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Benjamin Doron69bc2cc2023-06-14 19:03:04 -040072
73 smbios_cpu_get_core_counts(&t->core_count2, &t->thread_count2);
74 t->core_count = MAX_CPUS_ENABLED(t->core_count2);
75 t->thread_count = MAX_CPUS_ENABLED(t->thread_count2);
76 /* Assume we always enable all cores */
77 t->core_enabled = t->core_count;
78 t->core_enabled2 = t->core_count2;
79 t->l1_cache_handle = 0xffff;
80 t->l2_cache_handle = 0xffff;
81 t->l3_cache_handle = 0xffff;
82 t->serial_number = smbios_add_string(t->eos, smbios_processor_serial_number());
83 t->status = SMBIOS_PROCESSOR_STATUS_CPU_ENABLED | SMBIOS_PROCESSOR_STATUS_POPULATED;
84 t->processor_upgrade = PROCESSOR_UPGRADE_UNKNOWN;
85
86 t->external_clock = smbios_processor_external_clock();
87 if (t->external_clock == 0)
88 t->external_clock = (raw_read_cntfrq_el0() / 1000 / 1000);
89
90 t->current_speed = smbios_cpu_get_current_speed_mhz();
91
92 /* This field identifies a capability for the system, not the processor itself. */
93 t->max_speed = smbios_cpu_get_max_speed_mhz();
94
95 /* TODO: Are "Enhanced Virtualization" (by EL2) and "Power/Performance Control" supported? */
96 characteristics |= PROCESSOR_64BIT_CAPABLE;
97 characteristics |= BIT(5); /* Execute Protection */
98
99 if (t->core_count > 1)
100 characteristics |= PROCESSOR_MULTI_CORE;
101 if (t->thread_count > 1)
102 characteristics |= BIT(4); /* BIT4: Hardware Thread */
103 if (smccc_supports_arch_soc_id())
104 characteristics |= BIT(9); /* Arm64 SoC ID */
105
106 t->processor_characteristics = characteristics | smbios_processor_characteristics();
107
108 cpu_voltage = smbios_cpu_get_voltage();
109 if (cpu_voltage > 0)
110 t->voltage = 0x80 | cpu_voltage;
111
112 const int len = smbios_full_table_len(&t->header, t->eos);
113 *current += len;
114 return len;
115}
116
117int smbios_write_type7_cache_parameters(unsigned long *current,
118 int *handle,
119 int *max_struct_size,
120 struct smbios_type4 *type4)
121{
122 enum cache_level level = CACHE_L1;
123 int h;
124 int len = 0;
125
126 while (1) {
127 enum smbios_cache_type type;
128 struct cache_info info;
129
130 const u8 cache_type = cpu_get_cache_type(level);
131 /* No more caches in the system */
132 if (!cache_type)
133 break;
134
135 switch (cache_type) {
136 case CACHE_INSTRUCTION:
137 type = SMBIOS_CACHE_TYPE_INSTRUCTION;
138 cpu_get_cache_info(level, cache_type, &info);
139 break;
140 case CACHE_DATA:
141 type = SMBIOS_CACHE_TYPE_DATA;
142 cpu_get_cache_info(level, cache_type, &info);
143 break;
144 case CACHE_SEPARATE:
145 type = SMBIOS_CACHE_TYPE_DATA;
146 cpu_get_cache_info(level, CACHE_DATA, &info);
147 h = (*handle)++;
148 update_max(len, *max_struct_size, smbios_write_type7(current, h,
149 level, smbios_cache_sram_type(), smbios_cache_associativity(info.associativity),
150 type, info.size, info.size));
151
152 type = SMBIOS_CACHE_TYPE_INSTRUCTION;
153 cpu_get_cache_info(level, CACHE_INSTRUCTION, &info);
154 break;
155 case CACHE_UNIFIED:
156 type = SMBIOS_CACHE_TYPE_UNIFIED;
157 cpu_get_cache_info(level, cache_type, &info);
158 break;
159 default:
160 type = SMBIOS_CACHE_TYPE_UNKNOWN;
161 info.size = info.associativity = 0;
162 break;
163 }
164
165 h = (*handle)++;
166 update_max(len, *max_struct_size, smbios_write_type7(current, h,
167 level, smbios_cache_sram_type(), smbios_cache_associativity(info.associativity),
168 type, info.size, info.size));
169
170 if (type4) {
171 switch (level) {
172 case 1:
173 type4->l1_cache_handle = h;
174 break;
175 case 2:
176 type4->l2_cache_handle = h;
177 break;
178 case 3:
179 type4->l3_cache_handle = h;
180 break;
181 default:
182 break;
183 }
184 }
185
186 level++;
187 }
188
189 return len;
190}