1#include "ggml-backend-impl.h"
 2
 3#if defined(__s390x__)
 4#include <sys/auxv.h>
 5
 6// find hwcap bits in asm/elf.h
 7#ifndef HWCAP_VXRS_EXT2
 8#define HWCAP_VXRS_EXT2 (1 << 15)
 9#endif
10
11#ifndef HWCAP_NNPA
12#define HWCAP_NNPA (1 << 20)
13#endif
14
15struct s390x_features {
16    bool has_vxe2 = false;
17    bool has_nnpa = false;
18
19    s390x_features() {
20        uint32_t hwcap = getauxval(AT_HWCAP);
21        // NOTE: use hwcap2 with DFLT for z17 and later
22        // uint32_t hwcap2 = getauxval(AT_HWCAP2);
23
24        has_vxe2 = !!(hwcap & HWCAP_VXRS_EXT2);
25        has_nnpa = !!(hwcap & HWCAP_NNPA);
26    }
27};
28
29static int ggml_backend_cpu_s390x_score() {
30    int score = 1;
31    s390x_features sf;
32
33// IBM z15 / LinuxONE 3
34#ifdef GGML_USE_VXE2
35    if (!sf.has_vxe2) { return 0; }
36    score += 1 << 1;
37#endif
38
39// IBM z16 / LinuxONE 4 and z17 / LinuxONE 5
40#ifdef GGML_USE_NNPA
41    if (!sf.has_nnpa) { return 0; }
42    score += 1 << 2;
43#endif
44
45    return score;
46}
47
48GGML_BACKEND_DL_SCORE_IMPL(ggml_backend_cpu_s390x_score)
49
50#endif  // __s390x__