summaryrefslogtreecommitdiffstats
path: root/src/detection/cpu/cpu_haiku.c
blob: 9a8acb58793b51be8da2f3b5001c3724a70389a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "cpu.h"
#include "common/mallocHelper.h"

#include <OS.h>
#include <private/shared/cpu_type.h>

const char* ffDetectCPUImpl(FF_A_UNUSED const FFCPUOptions* options, FFCPUResult* cpu) {
    system_info sysInfo;
    if (get_system_info(&sysInfo) != B_OK) {
        return "get_system_info() failed";
    }

    uint32 topoNodeCount = 0;
    get_cpu_topology_info(NULL, &topoNodeCount);
    if (topoNodeCount == 0) {
        return "get_cpu_topology_info(NULL) failed";
    }

    FF_AUTO_FREE cpu_topology_node_info* topology = malloc(sizeof(*topology) * topoNodeCount);
    if (get_cpu_topology_info(topology, &topoNodeCount) != B_OK) {
        return "get_cpu_topology_info(topology) failed";
    }

    enum cpu_platform platform = B_CPU_UNKNOWN;
    enum cpu_vendor cpuVendor = B_CPU_VENDOR_UNKNOWN;
    uint32 cpuModel = 0, frequency = 0;
    uint16_t packages = 0, cores = 0;

    for (uint32 i = 0; i < topoNodeCount; i++) {
        switch (topology[i].type) {
            case B_TOPOLOGY_ROOT:
                platform = topology[i].data.root.platform;
                break;

            case B_TOPOLOGY_PACKAGE:
                cpuVendor = topology[i].data.package.vendor;
                ++packages;
                break;

            case B_TOPOLOGY_CORE:
                cpuModel = topology[i].data.core.model;
                uint32_t freq = (uint32_t) (topology[i].data.core.default_frequency / 1000000);
                frequency = freq > frequency ? freq : frequency;
                ++cores;
                break;

            default:
                break;
        }
    }

    const char* model = get_cpu_model_string(platform, cpuVendor, cpuModel);
    if (model) {
        ffStrbufSetS(&cpu->name, model);
    } else {
        ffStrbufSetF(&cpu->name, "(Unknown %" B_PRIx32 ")", cpuModel);
    }
    ffStrbufSetS(&cpu->vendor, get_cpu_vendor_string(cpuVendor));

    ffCPUDetectByCpuid(cpu);
    if (cpu->frequencyBase < frequency) {
        cpu->frequencyBase = frequency;
    }
    cpu->packages = packages;
    cpu->coresPhysical = cores;
    cpu->coresOnline = cpu->coresLogical = (uint16_t) sysInfo.cpu_count;
    return NULL;
}