| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include <kai/ops/newgemm_lib.hpp> |
| 8 | |
| 9 | unsigned int get_cpu_impl() { |
| 10 | #ifdef __linux__ |
| 11 | int fd=open("/proc/cpuinfo", 0); |
| 12 | char buff[3000]; |
| 13 | char *pos; |
| 14 | char *end; |
| 15 | int foundid=0; |
| 16 | int variant=0; |
| 17 | |
| 18 | int cpu = sched_getcpu(); |
| 19 | |
| 20 | if (!fd) { |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | int charsread=read(fd, buff, 3000); |
| 25 | pos = buff; |
| 26 | end = buff + charsread; |
| 27 | |
| 28 | close(fd); |
| 29 | |
| 30 | /* So, to date I've encountered two formats for /proc/cpuinfo. |
| 31 | * |
| 32 | * One of them just lists processor : n for each processor (with no |
| 33 | * other info), then at the end lists part information for the current |
| 34 | * CPU. |
| 35 | * |
| 36 | * The other has an entire clause (including part number info) for each |
| 37 | * CPU in the system, with "processor : n" headers. |
| 38 | * |
| 39 | * We can cope with either of these formats by waiting to see |
| 40 | * "processor: n" (where n = our CPU ID), and then looking for the next |
| 41 | * "CPU part" field. |
| 42 | */ |
| 43 | while (pos < end) { |
| 44 | if (foundid && !strncmp(pos, "CPU variant", 11)) { |
| 45 | pos += 13; |
| 46 | char *resume = end; // Need to continue scanning after this |
| 47 | |
| 48 | for (char *ch=pos; ch<end; ch++) { |
| 49 | if (*ch=='\n') { |
| 50 | *ch='\0'; |
| 51 | resume = ch+1; |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | variant = strtoul(pos, NULL, 0); |
| 57 | |
| 58 | pos = resume; |
| 59 | } |
| 60 | |
| 61 | if (foundid && !strncmp(pos, "CPU part", 8)) { |
| 62 | /* Found part number */ |
| 63 | pos += 11; |
| 64 | unsigned int num; |
| 65 | |
| 66 | for (char *ch=pos; ch<end; ch++) { |
| 67 | if (*ch=='\n') { |
| 68 | *ch='\0'; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | num = strtoul(pos, NULL, 0); |
| 74 | |
| 75 | return (num << 4) | (variant << 20); |
| 76 | } |
| 77 | |
| 78 | if (!strncmp(pos, "processor", 9)) { |
| 79 | /* Found processor ID, see if it's ours. */ |
| 80 | pos += 11; |
| 81 | int num; |
| 82 | |
| 83 | for (char *ch=pos; ch<end; ch++) { |
| 84 | if (*ch=='\n') { |
| 85 | *ch='\0'; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | num = strtol(pos, NULL, 0); |
| 91 | |
| 92 | if (num == cpu) { |
| 93 | foundid=1; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | while (pos < end) { |
| 98 | char ch = *pos++; |
| 99 | if (ch == '\n' || ch == '\0') { |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | #endif // __linux__ |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | CPUInfo *get_CPUInfo() { |
| 110 | static CPUInfo ci; |
| 111 | |
| 112 | return &ci; |
| 113 | } |