1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of get_cpuid().
5 * Copyright IBM Corp. 2014, 2018
6 * Author(s): Alexander Yarygin <yarygin@linux.vnet.ibm.com>
7 * Thomas Richter <tmricht@linux.vnet.ibm.com>
10 #include <sys/types.h>
16 #include "../../util/header.h"
17 #include "../../util/util.h"
19 #define SYSINFO_MANU "Manufacturer:"
20 #define SYSINFO_TYPE "Type:"
21 #define SYSINFO_MODEL "Model:"
22 #define SRVLVL_CPUMF "CPU-MF:"
23 #define SRVLVL_VERSION "version="
24 #define SRVLVL_AUTHORIZATION "authorization="
25 #define SYSINFO "/proc/sysinfo"
26 #define SRVLVL "/proc/service_levels"
28 int get_cpuid(char *buffer, size_t sz)
30 char *cp, *line = NULL, *line2;
31 char type[8], model[33], version[8], manufacturer[32], authorization[8];
32 int tpsize = 0, mdsize = 0, vssize = 0, mfsize = 0, atsize = 0;
34 unsigned long line_sz;
39 * Scan /proc/sysinfo line by line and read out values for
40 * Manufacturer:, Type: and Model:, for example:
44 * The first word is the Model Capacity and the second word is
45 * Model (can be omitted). Both words have a maximum size of 16
48 memset(manufacturer, 0, sizeof(manufacturer));
49 memset(type, 0, sizeof(type));
50 memset(model, 0, sizeof(model));
51 memset(version, 0, sizeof(version));
52 memset(authorization, 0, sizeof(authorization));
54 sysinfo = fopen(SYSINFO, "r");
58 while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
59 if (!strncmp(line, SYSINFO_MANU, strlen(SYSINFO_MANU))) {
60 line2 = line + strlen(SYSINFO_MANU);
62 while ((cp = strtok_r(line2, "\n ", &line2))) {
63 mfsize += scnprintf(manufacturer + mfsize,
64 sizeof(manufacturer) - mfsize, "%s", cp);
68 if (!strncmp(line, SYSINFO_TYPE, strlen(SYSINFO_TYPE))) {
69 line2 = line + strlen(SYSINFO_TYPE);
71 while ((cp = strtok_r(line2, "\n ", &line2))) {
72 tpsize += scnprintf(type + tpsize,
73 sizeof(type) - tpsize, "%s", cp);
77 if (!strncmp(line, SYSINFO_MODEL, strlen(SYSINFO_MODEL))) {
78 line2 = line + strlen(SYSINFO_MODEL);
80 while ((cp = strtok_r(line2, "\n ", &line2))) {
81 mdsize += scnprintf(model + mdsize, sizeof(model) - mdsize,
82 "%s%s", model[0] ? "," : "", cp);
89 /* Missing manufacturer, type or model information should not happen */
90 if (!manufacturer[0] || !type[0] || !model[0])
94 * Scan /proc/service_levels and return the CPU-MF counter facility
95 * version number and authorization level.
96 * Optional, does not exist on z/VM guests.
98 sysinfo = fopen(SRVLVL, "r");
101 while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
102 if (strncmp(line, SRVLVL_CPUMF, strlen(SRVLVL_CPUMF)))
105 line2 = line + strlen(SRVLVL_CPUMF);
106 while ((cp = strtok_r(line2, "\n ", &line2))) {
107 if (!strncmp(cp, SRVLVL_VERSION,
108 strlen(SRVLVL_VERSION))) {
109 char *sep = strchr(cp, '=');
111 vssize += scnprintf(version + vssize,
112 sizeof(version) - vssize, "%s", sep + 1);
114 if (!strncmp(cp, SRVLVL_AUTHORIZATION,
115 strlen(SRVLVL_AUTHORIZATION))) {
116 char *sep = strchr(cp, '=');
118 atsize += scnprintf(authorization + atsize,
119 sizeof(authorization) - atsize, "%s", sep + 1);
128 if (version[0] && authorization[0] )
129 nbytes = snprintf(buffer, sz, "%s,%s,%s,%s,%s",
130 manufacturer, type, model, version,
133 nbytes = snprintf(buffer, sz, "%s,%s,%s", manufacturer, type,
135 return (nbytes >= sz) ? -1 : 0;
138 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
140 char *buf = malloc(128);
142 if (buf && get_cpuid(buf, 128) < 0)