1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Platform energy and frequency attributes driver
5 * This driver creates a sys file at /sys/firmware/papr/ which encapsulates a
6 * directory structure containing files in keyword - value pairs that specify
7 * energy and frequency configuration of the system.
9 * The format of exposing the sysfs information is as follows:
10 * /sys/firmware/papr/energy_scale_info/
14 * |-- value_desc (if exists)
18 * |-- value_desc (if exists)
20 * Copyright 2022 IBM Corp.
23 #include <asm/hvcall.h>
24 #include <asm/machdep.h>
25 #include <asm/firmware.h>
30 * Flag attributes to fetch either all or one attribute from the HCALL
31 * flag = BE(0) => fetch all attributes with firstAttributeId = 0
32 * flag = BE(1) => fetch a single attribute with firstAttributeId = id
34 #define ESI_FLAGS_ALL 0
35 #define ESI_FLAGS_SINGLE (1ull << 63)
37 #define KOBJ_MAX_ATTRS 3
39 #define ESI_HDR_SIZE sizeof(struct h_energy_scale_info_hdr)
40 #define ESI_ATTR_SIZE sizeof(struct energy_scale_attribute)
41 #define CURR_MAX_ESI_ATTRS 8
43 struct energy_scale_attribute {
50 struct h_energy_scale_info_hdr {
53 u8 data_header_version;
58 struct kobj_attribute kobj_attr;
62 struct attribute_group pg;
63 struct papr_attr pgattrs[KOBJ_MAX_ATTRS];
66 static struct papr_group *papr_groups;
67 /* /sys/firmware/papr */
68 static struct kobject *papr_kobj;
69 /* /sys/firmware/papr/energy_scale_info */
70 static struct kobject *esi_kobj;
73 * Energy modes can change dynamically hence making a new hcall each time the
74 * information needs to be retrieved
76 static int papr_get_attr(u64 id, struct energy_scale_attribute *esi)
78 int esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * ESI_ATTR_SIZE);
79 int ret, max_esi_attrs = CURR_MAX_ESI_ATTRS;
80 struct energy_scale_attribute *curr_esi;
81 struct h_energy_scale_info_hdr *hdr;
84 buf = kmalloc(esi_buf_size, GFP_KERNEL);
89 ret = plpar_hcall_norets(H_GET_ENERGY_SCALE_INFO, ESI_FLAGS_SINGLE,
90 id, virt_to_phys(buf),
94 * If the hcall fails with not enough memory for either the
95 * header or data, attempt to allocate more
97 if (ret == H_PARTIAL || ret == H_P4) {
101 esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * max_esi_attrs);
103 temp_buf = krealloc(buf, esi_buf_size, GFP_KERNEL);
114 if (ret != H_SUCCESS) {
115 pr_warn("hcall failed: H_GET_ENERGY_SCALE_INFO");
120 hdr = (struct h_energy_scale_info_hdr *) buf;
121 curr_esi = (struct energy_scale_attribute *)
122 (buf + be64_to_cpu(hdr->array_offset));
125 be64_to_cpu(hdr->array_offset) + (be64_to_cpu(hdr->num_attrs)
126 * sizeof(struct energy_scale_attribute))) {
140 * Extract and export the description of the energy scale attributes
142 static ssize_t desc_show(struct kobject *kobj,
143 struct kobj_attribute *kobj_attr,
146 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
148 struct energy_scale_attribute esi;
151 ret = papr_get_attr(pattr->id, &esi);
155 return sysfs_emit(buf, "%s\n", esi.desc);
159 * Extract and export the numeric value of the energy scale attributes
161 static ssize_t val_show(struct kobject *kobj,
162 struct kobj_attribute *kobj_attr,
165 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
167 struct energy_scale_attribute esi;
170 ret = papr_get_attr(pattr->id, &esi);
174 return sysfs_emit(buf, "%llu\n", be64_to_cpu(esi.val));
178 * Extract and export the value description in string format of the energy
181 static ssize_t val_desc_show(struct kobject *kobj,
182 struct kobj_attribute *kobj_attr,
185 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
187 struct energy_scale_attribute esi;
190 ret = papr_get_attr(pattr->id, &esi);
194 return sysfs_emit(buf, "%s\n", esi.value_desc);
197 static struct papr_ops_info {
198 const char *attr_name;
199 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *kobj_attr,
201 } ops_info[KOBJ_MAX_ATTRS] = {
202 { "desc", desc_show },
203 { "value", val_show },
204 { "value_desc", val_desc_show },
207 static void add_attr(u64 id, int index, struct papr_attr *attr)
210 sysfs_attr_init(&attr->kobj_attr.attr);
211 attr->kobj_attr.attr.name = ops_info[index].attr_name;
212 attr->kobj_attr.attr.mode = 0444;
213 attr->kobj_attr.show = ops_info[index].show;
216 static int add_attr_group(u64 id, struct papr_group *pg, bool show_val_desc)
220 for (i = 0; i < KOBJ_MAX_ATTRS; i++) {
221 if (!strcmp(ops_info[i].attr_name, "value_desc") &&
225 add_attr(id, i, &pg->pgattrs[i]);
226 pg->pg.attrs[i] = &pg->pgattrs[i].kobj_attr.attr;
229 return sysfs_create_group(esi_kobj, &pg->pg);
233 static int __init papr_init(void)
235 int esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * ESI_ATTR_SIZE);
236 int ret, idx, i, max_esi_attrs = CURR_MAX_ESI_ATTRS;
237 struct h_energy_scale_info_hdr *esi_hdr;
238 struct energy_scale_attribute *esi_attrs;
242 if (!firmware_has_feature(FW_FEATURE_LPAR) ||
243 !firmware_has_feature(FW_FEATURE_ENERGY_SCALE_INFO)) {
247 esi_buf = kmalloc(esi_buf_size, GFP_KERNEL);
252 * uint64 H_GET_ENERGY_SCALE_INFO, // Get energy scale info
253 * uint64 flags, // Per the flag request
254 * uint64 firstAttributeId, // The attribute id
255 * uint64 bufferAddress, // Guest physical address of the output buffer
256 * uint64 bufferSize); // The size in bytes of the output buffer
260 ret = plpar_hcall_norets(H_GET_ENERGY_SCALE_INFO, ESI_FLAGS_ALL, 0,
261 virt_to_phys(esi_buf), esi_buf_size);
264 * If the hcall fails with not enough memory for either the
265 * header or data, attempt to allocate more
267 if (ret == H_PARTIAL || ret == H_P4) {
271 esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * max_esi_attrs);
273 temp_esi_buf = krealloc(esi_buf, esi_buf_size, GFP_KERNEL);
275 esi_buf = temp_esi_buf;
282 if (ret != H_SUCCESS) {
283 pr_warn("hcall failed: H_GET_ENERGY_SCALE_INFO, ret: %d\n", ret);
284 goto out_free_esi_buf;
287 esi_hdr = (struct h_energy_scale_info_hdr *) esi_buf;
288 num_attrs = be64_to_cpu(esi_hdr->num_attrs);
289 esi_attrs = (struct energy_scale_attribute *)
290 (esi_buf + be64_to_cpu(esi_hdr->array_offset));
293 be64_to_cpu(esi_hdr->array_offset) +
294 (num_attrs * sizeof(struct energy_scale_attribute))) {
295 goto out_free_esi_buf;
298 papr_groups = kcalloc(num_attrs, sizeof(*papr_groups), GFP_KERNEL);
300 goto out_free_esi_buf;
302 papr_kobj = kobject_create_and_add("papr", firmware_kobj);
304 pr_warn("kobject_create_and_add papr failed\n");
305 goto out_papr_groups;
308 esi_kobj = kobject_create_and_add("energy_scale_info", papr_kobj);
310 pr_warn("kobject_create_and_add energy_scale_info failed\n");
314 /* Allocate the groups before registering */
315 for (idx = 0; idx < num_attrs; idx++) {
316 papr_groups[idx].pg.attrs = kcalloc(KOBJ_MAX_ATTRS + 1,
317 sizeof(*papr_groups[idx].pg.attrs),
319 if (!papr_groups[idx].pg.attrs)
322 papr_groups[idx].pg.name = kasprintf(GFP_KERNEL, "%lld",
323 be64_to_cpu(esi_attrs[idx].id));
324 if (papr_groups[idx].pg.name == NULL)
328 for (idx = 0; idx < num_attrs; idx++) {
329 bool show_val_desc = true;
331 /* Do not add the value desc attr if it does not exist */
332 if (strnlen(esi_attrs[idx].value_desc,
333 sizeof(esi_attrs[idx].value_desc)) == 0)
334 show_val_desc = false;
336 if (add_attr_group(be64_to_cpu(esi_attrs[idx].id),
339 pr_warn("Failed to create papr attribute group %s\n",
340 papr_groups[idx].pg.name);
349 for (i = 0; i < idx ; i++) {
350 kfree(papr_groups[i].pg.attrs);
351 kfree(papr_groups[i].pg.name);
353 kobject_put(esi_kobj);
355 kobject_put(papr_kobj);
364 machine_device_initcall(pseries, papr_init);