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>
14 #include <linux/ctype.h>
15 #include <linux/kernel.h>
16 #include <linux/zalloc.h>
18 #include "../../util/header.h"
20 #define SYSINFO_MANU "Manufacturer:"
21 #define SYSINFO_TYPE "Type:"
22 #define SYSINFO_MODEL "Model:"
23 #define SRVLVL_CPUMF "CPU-MF:"
24 #define SRVLVL_VERSION "version="
25 #define SRVLVL_AUTHORIZATION "authorization="
26 #define SYSINFO "/proc/sysinfo"
27 #define SRVLVL "/proc/service_levels"
29 int get_cpuid(char *buffer, size_t sz)
31 char *cp, *line = NULL, *line2;
32 char type[8], model[33], version[8], manufacturer[32], authorization[8];
33 int tpsize = 0, mdsize = 0, vssize = 0, mfsize = 0, atsize = 0;
35 unsigned long line_sz;
40 * Scan /proc/sysinfo line by line and read out values for
41 * Manufacturer:, Type: and Model:, for example:
45 * The first word is the Model Capacity and the second word is
46 * Model (can be omitted). Both words have a maximum size of 16
49 memset(manufacturer, 0, sizeof(manufacturer));
50 memset(type, 0, sizeof(type));
51 memset(model, 0, sizeof(model));
52 memset(version, 0, sizeof(version));
53 memset(authorization, 0, sizeof(authorization));
55 sysinfo = fopen(SYSINFO, "r");
59 while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
60 if (!strncmp(line, SYSINFO_MANU, strlen(SYSINFO_MANU))) {
61 line2 = line + strlen(SYSINFO_MANU);
63 while ((cp = strtok_r(line2, "\n ", &line2))) {
64 mfsize += scnprintf(manufacturer + mfsize,
65 sizeof(manufacturer) - mfsize, "%s", cp);
69 if (!strncmp(line, SYSINFO_TYPE, strlen(SYSINFO_TYPE))) {
70 line2 = line + strlen(SYSINFO_TYPE);
72 while ((cp = strtok_r(line2, "\n ", &line2))) {
73 tpsize += scnprintf(type + tpsize,
74 sizeof(type) - tpsize, "%s", cp);
78 if (!strncmp(line, SYSINFO_MODEL, strlen(SYSINFO_MODEL))) {
79 line2 = line + strlen(SYSINFO_MODEL);
81 while ((cp = strtok_r(line2, "\n ", &line2))) {
82 mdsize += scnprintf(model + mdsize, sizeof(model) - mdsize,
83 "%s%s", model[0] ? "," : "", cp);
90 /* Missing manufacturer, type or model information should not happen */
91 if (!manufacturer[0] || !type[0] || !model[0])
95 * Scan /proc/service_levels and return the CPU-MF counter facility
96 * version number and authorization level.
97 * Optional, does not exist on z/VM guests.
99 sysinfo = fopen(SRVLVL, "r");
102 while ((read = getline(&line, &line_sz, sysinfo)) != -1) {
103 if (strncmp(line, SRVLVL_CPUMF, strlen(SRVLVL_CPUMF)))
106 line2 = line + strlen(SRVLVL_CPUMF);
107 while ((cp = strtok_r(line2, "\n ", &line2))) {
108 if (!strncmp(cp, SRVLVL_VERSION,
109 strlen(SRVLVL_VERSION))) {
110 char *sep = strchr(cp, '=');
112 vssize += scnprintf(version + vssize,
113 sizeof(version) - vssize, "%s", sep + 1);
115 if (!strncmp(cp, SRVLVL_AUTHORIZATION,
116 strlen(SRVLVL_AUTHORIZATION))) {
117 char *sep = strchr(cp, '=');
119 atsize += scnprintf(authorization + atsize,
120 sizeof(authorization) - atsize, "%s", sep + 1);
129 if (version[0] && authorization[0] )
130 nbytes = snprintf(buffer, sz, "%s,%s,%s,%s,%s",
131 manufacturer, type, model, version,
134 nbytes = snprintf(buffer, sz, "%s,%s,%s", manufacturer, type,
136 return (nbytes >= sz) ? -1 : 0;
139 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
141 char *buf = malloc(128);
143 if (buf && get_cpuid(buf, 128) < 0)