ohmu
·
2026-10-15
1#include "cpu.h"
2#include "common/debug.h"
3#include "common/mallocHelper.h"
4
5#include <OS.h>
6#include <private/shared/cpu_type.h>
7
8const char* ffDetectCPUImpl([[maybe_unused]] const FFCPUOptions* options, FFCPUResult* cpu) {
9 system_info sysInfo;
10 status_t status = get_system_info(&sysInfo);
11 if (status != B_OK) {
12 FF_DEBUG("get_system_info() failed: %d", (int) status);
13 return "get_system_info() failed";
14 }
15
16 uint32 topoNodeCount = 0;
17 get_cpu_topology_info(nullptr, &topoNodeCount);
18 if (topoNodeCount == 0) {
19 FF_DEBUG("get_cpu_topology_info(nullptr) failed");
20 return "get_cpu_topology_info(nullptr) failed";
21 }
22
23 FF_AUTO_FREE cpu_topology_node_info* topology = malloc(sizeof(*topology) * topoNodeCount);
24 status_t topoStatus = get_cpu_topology_info(topology, &topoNodeCount);
25 if (topoStatus != B_OK) {
26 FF_DEBUG("get_cpu_topology_info(topology) failed: %d", (int) topoStatus);
27 return "get_cpu_topology_info(topology) failed";
28 }
29
30 enum cpu_platform platform = B_CPU_UNKNOWN;
31 enum cpu_vendor cpuVendor = B_CPU_VENDOR_UNKNOWN;
32 uint32 cpuModel = 0, frequency = 0;
33 uint16_t packages = 0, cores = 0;
34
35 for (uint32 i = 0; i < topoNodeCount; i++) {
36 switch (topology[i].type) {
37 case B_TOPOLOGY_ROOT:
38 platform = topology[i].data.root.platform;
39 break;
40
41 case B_TOPOLOGY_PACKAGE:
42 cpuVendor = topology[i].data.package.vendor;
43 ++packages;
44 break;
45
46 case B_TOPOLOGY_CORE:
47 cpuModel = topology[i].data.core.model;
48 uint32_t freq = (uint32_t) (topology[i].data.core.default_frequency / 1000000);
49 frequency = freq > frequency ? freq : frequency;
50 ++cores;
51 break;
52
53 default:
54 break;
55 }
56 }
57
58 const char* model = get_cpu_model_string(platform, cpuVendor, cpuModel);
59 if (model) {
60 ffStrbufSetS(&cpu->name, model);
61 } else {
62 ffStrbufSetF(&cpu->name, "(Unknown %" B_PRIx32 ")", cpuModel);
63 }
64 ffStrbufSetS(&cpu->vendor, get_cpu_vendor_string(cpuVendor));
65
66 ffCPUDetectByCpuid(cpu);
67 if (cpu->frequencyBase < frequency) {
68 cpu->frequencyBase = frequency;
69 }
70 cpu->packages = packages;
71 cpu->coresPhysical = cores;
72 cpu->coresOnline = cpu->coresLogical = (uint16_t) sysInfo.cpu_count;
73 return nullptr;
74}