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);
112 if (ret != H_SUCCESS) {
113 pr_warn("hcall failed: H_GET_ENERGY_SCALE_INFO");
118 hdr = (struct h_energy_scale_info_hdr *) buf;
119 curr_esi = (struct energy_scale_attribute *)
120 (buf + be64_to_cpu(hdr->array_offset));
123 be64_to_cpu(hdr->array_offset) + (be64_to_cpu(hdr->num_attrs)
124 * sizeof(struct energy_scale_attribute))) {
138 * Extract and export the description of the energy scale attributes
140 static ssize_t desc_show(struct kobject *kobj,
141 struct kobj_attribute *kobj_attr,
144 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
146 struct energy_scale_attribute esi;
149 ret = papr_get_attr(pattr->id, &esi);
153 return sysfs_emit(buf, "%s\n", esi.desc);
157 * Extract and export the numeric value of the energy scale attributes
159 static ssize_t val_show(struct kobject *kobj,
160 struct kobj_attribute *kobj_attr,
163 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
165 struct energy_scale_attribute esi;
168 ret = papr_get_attr(pattr->id, &esi);
172 return sysfs_emit(buf, "%llu\n", be64_to_cpu(esi.val));
176 * Extract and export the value description in string format of the energy
179 static ssize_t val_desc_show(struct kobject *kobj,
180 struct kobj_attribute *kobj_attr,
183 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
185 struct energy_scale_attribute esi;
188 ret = papr_get_attr(pattr->id, &esi);
192 return sysfs_emit(buf, "%s\n", esi.value_desc);
195 static struct papr_ops_info {
196 const char *attr_name;
197 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *kobj_attr,
199 } ops_info[KOBJ_MAX_ATTRS] = {
200 { "desc", desc_show },
201 { "value", val_show },
202 { "value_desc", val_desc_show },
205 static void add_attr(u64 id, int index, struct papr_attr *attr)
208 sysfs_attr_init(&attr->kobj_attr.attr);
209 attr->kobj_attr.attr.name = ops_info[index].attr_name;
210 attr->kobj_attr.attr.mode = 0444;
211 attr->kobj_attr.show = ops_info[index].show;
214 static int add_attr_group(u64 id, struct papr_group *pg, bool show_val_desc)
218 for (i = 0; i < KOBJ_MAX_ATTRS; i++) {
219 if (!strcmp(ops_info[i].attr_name, "value_desc") &&
223 add_attr(id, i, &pg->pgattrs[i]);
224 pg->pg.attrs[i] = &pg->pgattrs[i].kobj_attr.attr;
227 return sysfs_create_group(esi_kobj, &pg->pg);
231 static int __init papr_init(void)
233 int esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * ESI_ATTR_SIZE);
234 int ret, idx, i, max_esi_attrs = CURR_MAX_ESI_ATTRS;
235 struct h_energy_scale_info_hdr *esi_hdr;
236 struct energy_scale_attribute *esi_attrs;
240 if (!firmware_has_feature(FW_FEATURE_LPAR) ||
241 !firmware_has_feature(FW_FEATURE_ENERGY_SCALE_INFO)) {
245 esi_buf = kmalloc(esi_buf_size, GFP_KERNEL);
250 * uint64 H_GET_ENERGY_SCALE_INFO, // Get energy scale info
251 * uint64 flags, // Per the flag request
252 * uint64 firstAttributeId, // The attribute id
253 * uint64 bufferAddress, // Guest physical address of the output buffer
254 * uint64 bufferSize); // The size in bytes of the output buffer
258 ret = plpar_hcall_norets(H_GET_ENERGY_SCALE_INFO, ESI_FLAGS_ALL, 0,
259 virt_to_phys(esi_buf), esi_buf_size);
262 * If the hcall fails with not enough memory for either the
263 * header or data, attempt to allocate more
265 if (ret == H_PARTIAL || ret == H_P4) {
269 esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * max_esi_attrs);
271 temp_esi_buf = krealloc(esi_buf, esi_buf_size, GFP_KERNEL);
273 esi_buf = temp_esi_buf;
280 if (ret != H_SUCCESS) {
281 pr_warn("hcall failed: H_GET_ENERGY_SCALE_INFO, ret: %d\n", ret);
282 goto out_free_esi_buf;
285 esi_hdr = (struct h_energy_scale_info_hdr *) esi_buf;
286 num_attrs = be64_to_cpu(esi_hdr->num_attrs);
287 esi_attrs = (struct energy_scale_attribute *)
288 (esi_buf + be64_to_cpu(esi_hdr->array_offset));
291 be64_to_cpu(esi_hdr->array_offset) +
292 (num_attrs * sizeof(struct energy_scale_attribute))) {
293 goto out_free_esi_buf;
296 papr_groups = kcalloc(num_attrs, sizeof(*papr_groups), GFP_KERNEL);
298 goto out_free_esi_buf;
300 papr_kobj = kobject_create_and_add("papr", firmware_kobj);
302 pr_warn("kobject_create_and_add papr failed\n");
303 goto out_papr_groups;
306 esi_kobj = kobject_create_and_add("energy_scale_info", papr_kobj);
308 pr_warn("kobject_create_and_add energy_scale_info failed\n");
312 /* Allocate the groups before registering */
313 for (idx = 0; idx < num_attrs; idx++) {
314 papr_groups[idx].pg.attrs = kcalloc(KOBJ_MAX_ATTRS + 1,
315 sizeof(*papr_groups[idx].pg.attrs),
317 if (!papr_groups[idx].pg.attrs)
320 papr_groups[idx].pg.name = kasprintf(GFP_KERNEL, "%lld",
321 be64_to_cpu(esi_attrs[idx].id));
322 if (papr_groups[idx].pg.name == NULL)
326 for (idx = 0; idx < num_attrs; idx++) {
327 bool show_val_desc = true;
329 /* Do not add the value desc attr if it does not exist */
330 if (strnlen(esi_attrs[idx].value_desc,
331 sizeof(esi_attrs[idx].value_desc)) == 0)
332 show_val_desc = false;
334 if (add_attr_group(be64_to_cpu(esi_attrs[idx].id),
337 pr_warn("Failed to create papr attribute group %s\n",
338 papr_groups[idx].pg.name);
347 for (i = 0; i < idx ; i++) {
348 kfree(papr_groups[i].pg.attrs);
349 kfree(papr_groups[i].pg.name);
351 kobject_put(esi_kobj);
353 kobject_put(papr_kobj);
362 machine_device_initcall(pseries, papr_init);