| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <cpu/cpu.h> |
| 5 |
#include <cpu/x86/topology.h> |
| 6 |
#include <device/device.h> |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#define CPUID_EXTENDED_CPU_TOPOLOGY 0x0b |
| 17 |
#define CPUID_EXTENDED_CPU_TOPOLOGY2 0x1f |
| 18 |
|
| 19 |
enum { |
| 20 |
LEVEL_TYPE_PACKAGE, |
| 21 |
LEVEL_TYPE_SMT, |
| 22 |
LEVEL_TYPE_CORE, |
| 23 |
LEVEL_TYPE_MODULE, |
| 24 |
LEVEL_TYPE_TILE, |
| 25 |
LEVEL_TYPE_DIE, |
| 26 |
LEVEL_TYPE_DIEGRP, |
| 27 |
LEVEL_TYPE_MAX |
| 28 |
}; |
| 29 |
|
| 30 |
#define CPUID_CPU_TOPOLOGY(x, val) \ |
| 31 |
(((val) >> CPUID_CPU_TOPOLOGY_##x##_SHIFT) & CPUID_CPU_TOPOLOGY_##x##_MASK) |
| 32 |
|
| 33 |
#define CPUID_CPU_TOPOLOGY_LEVEL_TYPE_SHIFT 0x8 |
| 34 |
#define CPUID_CPU_TOPOLOGY_LEVEL_TYPE_MASK 0xff |
| 35 |
#define CPUID_CPU_TOPOLOGY_LEVEL(res) CPUID_CPU_TOPOLOGY(LEVEL_TYPE, (res).ecx) |
| 36 |
|
| 37 |
#define CPUID_CPU_TOPOLOGY_LEVEL_BITS_SHIFT 0x0 |
| 38 |
#define CPUID_CPU_TOPOLOGY_LEVEL_BITS_MASK 0x1f |
| 39 |
|
| 40 |
struct bitfield_descriptor { |
| 41 |
uint8_t first_bit; |
| 42 |
uint8_t num_bits; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static enum cb_err read_topology_structure(struct bitfield_descriptor *topology) |
| 61 |
{ |
| 62 |
const uint32_t cpuid_max_func = cpuid_get_max_func(); |
| 63 |
struct cpuid_result cpuid_regs; |
| 64 |
int level_num = 0, cpu_id_op; |
| 65 |
unsigned int level; |
| 66 |
uint8_t next_level_first_bit, first_bit = 0; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if (cpuid_max_func >= CPUID_EXTENDED_CPU_TOPOLOGY2) |
| 73 |
cpu_id_op = CPUID_EXTENDED_CPU_TOPOLOGY2; |
| 74 |
else if (cpuid_max_func >= CPUID_EXTENDED_CPU_TOPOLOGY) |
| 75 |
cpu_id_op = CPUID_EXTENDED_CPU_TOPOLOGY; |
| 76 |
else |
| 77 |
return CB_ERR; |
| 78 |
|
| 79 |
cpuid_regs = cpuid_ext(cpu_id_op, level_num); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if (CPUID_CPU_TOPOLOGY_LEVEL(cpuid_regs) != LEVEL_TYPE_SMT) |
| 86 |
return CB_ERR; |
| 87 |
|
| 88 |
do { |
| 89 |
level = CPUID_CPU_TOPOLOGY_LEVEL(cpuid_regs); |
| 90 |
next_level_first_bit = CPUID_CPU_TOPOLOGY(LEVEL_BITS, cpuid_regs.eax); |
| 91 |
if (level < LEVEL_TYPE_MAX) { |
| 92 |
topology[level].num_bits = next_level_first_bit - first_bit; |
| 93 |
topology[level].first_bit = first_bit; |
| 94 |
} else { |
| 95 |
printk(BIOS_ERR, "Unknown processor topology level %d\n", level); |
| 96 |
} |
| 97 |
first_bit = next_level_first_bit; |
| 98 |
level_num++; |
| 99 |
cpuid_regs = cpuid_ext(cpu_id_op, level_num); |
| 100 |
|
| 101 |
} while (CPUID_CPU_TOPOLOGY_LEVEL(cpuid_regs)); |
| 102 |
|
| 103 |
topology[LEVEL_TYPE_PACKAGE].first_bit = next_level_first_bit; |
| 104 |
return CB_SUCCESS; |
| 105 |
} |
| 106 |
|
| 107 |
void set_cpu_topology(struct device *cpu) |
| 108 |
{ |
| 109 |
const uint32_t apicid = cpu->path.apic.initial_lapicid; |
| 110 |
static struct bitfield_descriptor topology[LEVEL_TYPE_MAX]; |
| 111 |
static enum cb_err ret; |
| 112 |
static bool done; |
| 113 |
unsigned int core_id_within_package; |
| 114 |
struct { |
| 115 |
unsigned int level; |
| 116 |
unsigned int *field; |
| 117 |
} apic_fields[] = { |
| 118 |
{ LEVEL_TYPE_SMT, &cpu->path.apic.thread_id }, |
| 119 |
{ LEVEL_TYPE_CORE, &cpu->path.apic.core_id }, |
| 120 |
{ LEVEL_TYPE_MODULE, &cpu->path.apic.module_id }, |
| 121 |
{ LEVEL_TYPE_PACKAGE, &cpu->path.apic.package_id }, |
| 122 |
{ LEVEL_TYPE_PACKAGE, &cpu->path.apic.node_id } |
| 123 |
}; |
| 124 |
|
| 125 |
if (!done) { |
| 126 |
ret = read_topology_structure(topology); |
| 127 |
done = true; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if (ret != CB_SUCCESS) { |
| 139 |
cpu->path.apic.package_id = 0; |
| 140 |
cpu->path.apic.core_id = apicid; |
| 141 |
cpu->path.apic.thread_id = 0; |
| 142 |
cpu->path.apic.node_id = 0; |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
for (size_t i = 0; i < ARRAY_SIZE(apic_fields); i++) { |
| 147 |
struct bitfield_descriptor *desc = &topology[apic_fields[i].level]; |
| 148 |
if (desc->first_bit || desc->num_bits) { |
| 149 |
unsigned int value = apicid >> desc->first_bit; |
| 150 |
if (desc->num_bits) |
| 151 |
value &= (1 << (desc->num_bits)) - 1; |
| 152 |
*apic_fields[i].field = value; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
core_id_within_package = apicid & ((1 << topology[LEVEL_TYPE_PACKAGE].first_bit) - 1); |
| 157 |
core_id_within_package >>= topology[LEVEL_TYPE_CORE].first_bit; |
| 158 |
cpu->path.apic.core_id_within_package = core_id_within_package; |
| 159 |
} |