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>
29 * Flag attributes to fetch either all or one attribute from the HCALL
30 * flag = BE(0) => fetch all attributes with firstAttributeId = 0
31 * flag = BE(1) => fetch a single attribute with firstAttributeId = id
33 #define ESI_FLAGS_ALL 0
34 #define ESI_FLAGS_SINGLE (1ull << 63)
36 #define KOBJ_MAX_ATTRS 3
38 #define ESI_HDR_SIZE sizeof(struct h_energy_scale_info_hdr)
39 #define ESI_ATTR_SIZE sizeof(struct energy_scale_attribute)
40 #define CURR_MAX_ESI_ATTRS 8
42 struct energy_scale_attribute {
49 struct h_energy_scale_info_hdr {
52 u8 data_header_version;
57 struct kobj_attribute kobj_attr;
61 struct attribute_group pg;
62 struct papr_attr pgattrs[KOBJ_MAX_ATTRS];
65 static struct papr_group *papr_groups;
66 /* /sys/firmware/papr */
67 static struct kobject *papr_kobj;
68 /* /sys/firmware/papr/energy_scale_info */
69 static struct kobject *esi_kobj;
72 * Energy modes can change dynamically hence making a new hcall each time the
73 * information needs to be retrieved
75 static int papr_get_attr(u64 id, struct energy_scale_attribute *esi)
77 int esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * ESI_ATTR_SIZE);
78 int ret, max_esi_attrs = CURR_MAX_ESI_ATTRS;
79 struct energy_scale_attribute *curr_esi;
80 struct h_energy_scale_info_hdr *hdr;
83 buf = kmalloc(esi_buf_size, GFP_KERNEL);
88 ret = plpar_hcall_norets(H_GET_ENERGY_SCALE_INFO, ESI_FLAGS_SINGLE,
89 id, virt_to_phys(buf),
93 * If the hcall fails with not enough memory for either the
94 * header or data, attempt to allocate more
96 if (ret == H_PARTIAL || ret == H_P4) {
100 esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * max_esi_attrs);
102 temp_buf = krealloc(buf, esi_buf_size, GFP_KERNEL);
111 if (ret != H_SUCCESS) {
112 pr_warn("hcall failed: H_GET_ENERGY_SCALE_INFO");
117 hdr = (struct h_energy_scale_info_hdr *) buf;
118 curr_esi = (struct energy_scale_attribute *)
119 (buf + be64_to_cpu(hdr->array_offset));
122 be64_to_cpu(hdr->array_offset) + (be64_to_cpu(hdr->num_attrs)
123 * sizeof(struct energy_scale_attribute))) {
137 * Extract and export the description of the energy scale attributes
139 static ssize_t desc_show(struct kobject *kobj,
140 struct kobj_attribute *kobj_attr,
143 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
145 struct energy_scale_attribute esi;
148 ret = papr_get_attr(pattr->id, &esi);
152 return sysfs_emit(buf, "%s\n", esi.desc);
156 * Extract and export the numeric value of the energy scale attributes
158 static ssize_t val_show(struct kobject *kobj,
159 struct kobj_attribute *kobj_attr,
162 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
164 struct energy_scale_attribute esi;
167 ret = papr_get_attr(pattr->id, &esi);
171 return sysfs_emit(buf, "%llu\n", be64_to_cpu(esi.val));
175 * Extract and export the value description in string format of the energy
178 static ssize_t val_desc_show(struct kobject *kobj,
179 struct kobj_attribute *kobj_attr,
182 struct papr_attr *pattr = container_of(kobj_attr, struct papr_attr,
184 struct energy_scale_attribute esi;
187 ret = papr_get_attr(pattr->id, &esi);
191 return sysfs_emit(buf, "%s\n", esi.value_desc);
194 static struct papr_ops_info {
195 const char *attr_name;
196 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *kobj_attr,
198 } ops_info[KOBJ_MAX_ATTRS] = {
199 { "desc", desc_show },
200 { "value", val_show },
201 { "value_desc", val_desc_show },
204 static void add_attr(u64 id, int index, struct papr_attr *attr)
207 sysfs_attr_init(&attr->kobj_attr.attr);
208 attr->kobj_attr.attr.name = ops_info[index].attr_name;
209 attr->kobj_attr.attr.mode = 0444;
210 attr->kobj_attr.show = ops_info[index].show;
213 static int add_attr_group(u64 id, struct papr_group *pg, bool show_val_desc)
217 for (i = 0; i < KOBJ_MAX_ATTRS; i++) {
218 if (!strcmp(ops_info[i].attr_name, "value_desc") &&
222 add_attr(id, i, &pg->pgattrs[i]);
223 pg->pg.attrs[i] = &pg->pgattrs[i].kobj_attr.attr;
226 return sysfs_create_group(esi_kobj, &pg->pg);
230 static int __init papr_init(void)
232 int esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * ESI_ATTR_SIZE);
233 int ret, idx, i, max_esi_attrs = CURR_MAX_ESI_ATTRS;
234 struct h_energy_scale_info_hdr *esi_hdr;
235 struct energy_scale_attribute *esi_attrs;
239 if (!firmware_has_feature(FW_FEATURE_LPAR) ||
240 !firmware_has_feature(FW_FEATURE_ENERGY_SCALE_INFO)) {
244 esi_buf = kmalloc(esi_buf_size, GFP_KERNEL);
249 * uint64 H_GET_ENERGY_SCALE_INFO, // Get energy scale info
250 * uint64 flags, // Per the flag request
251 * uint64 firstAttributeId, // The attribute id
252 * uint64 bufferAddress, // Guest physical address of the output buffer
253 * uint64 bufferSize); // The size in bytes of the output buffer
257 ret = plpar_hcall_norets(H_GET_ENERGY_SCALE_INFO, ESI_FLAGS_ALL, 0,
258 virt_to_phys(esi_buf), esi_buf_size);
261 * If the hcall fails with not enough memory for either the
262 * header or data, attempt to allocate more
264 if (ret == H_PARTIAL || ret == H_P4) {
268 esi_buf_size = ESI_HDR_SIZE + (CURR_MAX_ESI_ATTRS * max_esi_attrs);
270 temp_esi_buf = krealloc(esi_buf, esi_buf_size, GFP_KERNEL);
272 esi_buf = temp_esi_buf;
279 if (ret != H_SUCCESS) {
280 pr_warn("hcall failed: H_GET_ENERGY_SCALE_INFO, ret: %d\n", ret);
281 goto out_free_esi_buf;
284 esi_hdr = (struct h_energy_scale_info_hdr *) esi_buf;
285 num_attrs = be64_to_cpu(esi_hdr->num_attrs);
286 esi_attrs = (struct energy_scale_attribute *)
287 (esi_buf + be64_to_cpu(esi_hdr->array_offset));
290 be64_to_cpu(esi_hdr->array_offset) +
291 (num_attrs * sizeof(struct energy_scale_attribute))) {
292 goto out_free_esi_buf;
295 papr_groups = kcalloc(num_attrs, sizeof(*papr_groups), GFP_KERNEL);
297 goto out_free_esi_buf;
299 papr_kobj = kobject_create_and_add("papr", firmware_kobj);
301 pr_warn("kobject_create_and_add papr failed\n");
302 goto out_papr_groups;
305 esi_kobj = kobject_create_and_add("energy_scale_info", papr_kobj);
307 pr_warn("kobject_create_and_add energy_scale_info failed\n");
311 /* Allocate the groups before registering */
312 for (idx = 0; idx < num_attrs; idx++) {
313 papr_groups[idx].pg.attrs = kcalloc(KOBJ_MAX_ATTRS + 1,
314 sizeof(*papr_groups[idx].pg.attrs),
316 if (!papr_groups[idx].pg.attrs)
319 papr_groups[idx].pg.name = kasprintf(GFP_KERNEL, "%lld",
320 be64_to_cpu(esi_attrs[idx].id));
321 if (papr_groups[idx].pg.name == NULL)
325 for (idx = 0; idx < num_attrs; idx++) {
326 bool show_val_desc = true;
328 /* Do not add the value desc attr if it does not exist */
329 if (strnlen(esi_attrs[idx].value_desc,
330 sizeof(esi_attrs[idx].value_desc)) == 0)
331 show_val_desc = false;
333 if (add_attr_group(be64_to_cpu(esi_attrs[idx].id),
336 pr_warn("Failed to create papr attribute group %s\n",
337 papr_groups[idx].pg.name);
346 for (i = 0; i < idx ; i++) {
347 kfree(papr_groups[i].pg.attrs);
348 kfree(papr_groups[i].pg.name);
350 kobject_put(esi_kobj);
352 kobject_put(papr_kobj);
361 machine_device_initcall(pseries, papr_init);