1 /* 2 * POWER platform energy management driver 3 * Copyright (C) 2010 IBM Corporation 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * version 2 as published by the Free Software Foundation. 8 * 9 * This pseries platform device driver provides access to 10 * platform energy management capabilities. 11 */ 12 13 #include <linux/module.h> 14 #include <linux/types.h> 15 #include <linux/errno.h> 16 #include <linux/init.h> 17 #include <linux/seq_file.h> 18 #include <linux/device.h> 19 #include <linux/cpu.h> 20 #include <linux/of.h> 21 #include <asm/cputhreads.h> 22 #include <asm/page.h> 23 #include <asm/hvcall.h> 24 #include <asm/firmware.h> 25 #include <asm/prom.h> 26 27 28 #define MODULE_VERS "1.0" 29 #define MODULE_NAME "pseries_energy" 30 31 /* Driver flags */ 32 33 static int sysfs_entries; 34 35 /* Helper routines */ 36 37 /* Helper Routines to convert between drc_index to cpu numbers */ 38 39 static u32 cpu_to_drc_index(int cpu) 40 { 41 struct device_node *dn = NULL; 42 int thread_index; 43 int rc = 1; 44 u32 ret = 0; 45 46 dn = of_find_node_by_path("/cpus"); 47 if (dn == NULL) 48 goto err; 49 50 /* Convert logical cpu number to core number */ 51 thread_index = cpu_core_index_of_thread(cpu); 52 53 if (firmware_has_feature(FW_FEATURE_DRC_INFO)) { 54 struct property *info = NULL; 55 struct of_drc_info drc; 56 int j; 57 u32 num_set_entries; 58 const __be32 *value; 59 60 info = of_find_property(dn, "ibm,drc-info", NULL); 61 if (info == NULL) 62 goto err_of_node_put; 63 64 value = of_prop_next_u32(info, NULL, &num_set_entries); 65 if (!value) 66 goto err_of_node_put; 67 68 for (j = 0; j < num_set_entries; j++) { 69 70 of_read_drc_info_cell(&info, &value, &drc); 71 if (strncmp(drc.drc_type, "CPU", 3)) 72 goto err; 73 74 if (thread_index < drc.last_drc_index) 75 break; 76 } 77 78 ret = drc.drc_index_start + (thread_index * drc.sequential_inc); 79 } else { 80 u32 nr_drc_indexes, thread_drc_index; 81 82 /* 83 * The first element of ibm,drc-indexes array is the 84 * number of drc_indexes returned in the list. Hence 85 * thread_index+1 will get the drc_index corresponding 86 * to core number thread_index. 87 */ 88 rc = of_property_read_u32_index(dn, "ibm,drc-indexes", 89 0, &nr_drc_indexes); 90 if (rc) 91 goto err_of_node_put; 92 93 WARN_ON_ONCE(thread_index > nr_drc_indexes); 94 rc = of_property_read_u32_index(dn, "ibm,drc-indexes", 95 thread_index + 1, 96 &thread_drc_index); 97 if (rc) 98 goto err_of_node_put; 99 100 ret = thread_drc_index; 101 } 102 103 rc = 0; 104 105 err_of_node_put: 106 of_node_put(dn); 107 err: 108 if (rc) 109 printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu); 110 return ret; 111 } 112 113 static int drc_index_to_cpu(u32 drc_index) 114 { 115 struct device_node *dn = NULL; 116 const int *indexes; 117 int thread_index = 0, cpu = 0; 118 int rc = 1; 119 120 dn = of_find_node_by_path("/cpus"); 121 if (dn == NULL) 122 goto err; 123 124 if (firmware_has_feature(FW_FEATURE_DRC_INFO)) { 125 struct property *info = NULL; 126 struct of_drc_info drc; 127 int j; 128 u32 num_set_entries; 129 const __be32 *value; 130 131 info = of_find_property(dn, "ibm,drc-info", NULL); 132 if (info == NULL) 133 goto err_of_node_put; 134 135 value = of_prop_next_u32(info, NULL, &num_set_entries); 136 if (!value) 137 goto err_of_node_put; 138 139 for (j = 0; j < num_set_entries; j++) { 140 141 of_read_drc_info_cell(&info, &value, &drc); 142 if (strncmp(drc.drc_type, "CPU", 3)) 143 goto err; 144 145 if (drc_index > drc.last_drc_index) { 146 cpu += drc.num_sequential_elems; 147 continue; 148 } 149 cpu += ((drc_index - drc.drc_index_start) / 150 drc.sequential_inc); 151 152 thread_index = cpu_first_thread_of_core(cpu); 153 rc = 0; 154 break; 155 } 156 } else { 157 unsigned long int i; 158 159 indexes = of_get_property(dn, "ibm,drc-indexes", NULL); 160 if (indexes == NULL) 161 goto err_of_node_put; 162 /* 163 * First element in the array is the number of drc_indexes 164 * returned. Search through the list to find the matching 165 * drc_index and get the core number 166 */ 167 for (i = 0; i < indexes[0]; i++) { 168 if (indexes[i + 1] == drc_index) 169 break; 170 } 171 /* Convert core number to logical cpu number */ 172 thread_index = cpu_first_thread_of_core(i); 173 rc = 0; 174 } 175 176 err_of_node_put: 177 of_node_put(dn); 178 err: 179 if (rc) 180 printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index); 181 return thread_index; 182 } 183 184 /* 185 * pseries hypervisor call H_BEST_ENERGY provides hints to OS on 186 * preferred logical cpus to activate or deactivate for optimized 187 * energy consumption. 188 */ 189 190 #define FLAGS_MODE1 0x004E200000080E01UL 191 #define FLAGS_MODE2 0x004E200000080401UL 192 #define FLAGS_ACTIVATE 0x100 193 194 static ssize_t get_best_energy_list(char *page, int activate) 195 { 196 int rc, cnt, i, cpu; 197 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE]; 198 unsigned long flags = 0; 199 u32 *buf_page; 200 char *s = page; 201 202 buf_page = (u32 *) get_zeroed_page(GFP_KERNEL); 203 if (!buf_page) 204 return -ENOMEM; 205 206 flags = FLAGS_MODE1; 207 if (activate) 208 flags |= FLAGS_ACTIVATE; 209 210 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page), 211 0, 0, 0, 0, 0, 0); 212 if (rc != H_SUCCESS) { 213 free_page((unsigned long) buf_page); 214 return -EINVAL; 215 } 216 217 cnt = retbuf[0]; 218 for (i = 0; i < cnt; i++) { 219 cpu = drc_index_to_cpu(buf_page[2*i+1]); 220 if ((cpu_online(cpu) && !activate) || 221 (!cpu_online(cpu) && activate)) 222 s += sprintf(s, "%d,", cpu); 223 } 224 if (s > page) { /* Something to show */ 225 s--; /* Suppress last comma */ 226 s += sprintf(s, "\n"); 227 } 228 229 free_page((unsigned long) buf_page); 230 return s-page; 231 } 232 233 static ssize_t get_best_energy_data(struct device *dev, 234 char *page, int activate) 235 { 236 int rc; 237 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE]; 238 unsigned long flags = 0; 239 240 flags = FLAGS_MODE2; 241 if (activate) 242 flags |= FLAGS_ACTIVATE; 243 244 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 245 cpu_to_drc_index(dev->id), 246 0, 0, 0, 0, 0, 0, 0); 247 248 if (rc != H_SUCCESS) 249 return -EINVAL; 250 251 return sprintf(page, "%lu\n", retbuf[1] >> 32); 252 } 253 254 /* Wrapper functions */ 255 256 static ssize_t cpu_activate_hint_list_show(struct device *dev, 257 struct device_attribute *attr, char *page) 258 { 259 return get_best_energy_list(page, 1); 260 } 261 262 static ssize_t cpu_deactivate_hint_list_show(struct device *dev, 263 struct device_attribute *attr, char *page) 264 { 265 return get_best_energy_list(page, 0); 266 } 267 268 static ssize_t percpu_activate_hint_show(struct device *dev, 269 struct device_attribute *attr, char *page) 270 { 271 return get_best_energy_data(dev, page, 1); 272 } 273 274 static ssize_t percpu_deactivate_hint_show(struct device *dev, 275 struct device_attribute *attr, char *page) 276 { 277 return get_best_energy_data(dev, page, 0); 278 } 279 280 /* 281 * Create sysfs interface: 282 * /sys/devices/system/cpu/pseries_activate_hint_list 283 * /sys/devices/system/cpu/pseries_deactivate_hint_list 284 * Comma separated list of cpus to activate or deactivate 285 * /sys/devices/system/cpu/cpuN/pseries_activate_hint 286 * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint 287 * Per-cpu value of the hint 288 */ 289 290 static struct device_attribute attr_cpu_activate_hint_list = 291 __ATTR(pseries_activate_hint_list, 0444, 292 cpu_activate_hint_list_show, NULL); 293 294 static struct device_attribute attr_cpu_deactivate_hint_list = 295 __ATTR(pseries_deactivate_hint_list, 0444, 296 cpu_deactivate_hint_list_show, NULL); 297 298 static struct device_attribute attr_percpu_activate_hint = 299 __ATTR(pseries_activate_hint, 0444, 300 percpu_activate_hint_show, NULL); 301 302 static struct device_attribute attr_percpu_deactivate_hint = 303 __ATTR(pseries_deactivate_hint, 0444, 304 percpu_deactivate_hint_show, NULL); 305 306 static int __init pseries_energy_init(void) 307 { 308 int cpu, err; 309 struct device *cpu_dev; 310 311 if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY)) 312 return 0; /* H_BEST_ENERGY hcall not supported */ 313 314 /* Create the sysfs files */ 315 err = device_create_file(cpu_subsys.dev_root, 316 &attr_cpu_activate_hint_list); 317 if (!err) 318 err = device_create_file(cpu_subsys.dev_root, 319 &attr_cpu_deactivate_hint_list); 320 321 if (err) 322 return err; 323 for_each_possible_cpu(cpu) { 324 cpu_dev = get_cpu_device(cpu); 325 err = device_create_file(cpu_dev, 326 &attr_percpu_activate_hint); 327 if (err) 328 break; 329 err = device_create_file(cpu_dev, 330 &attr_percpu_deactivate_hint); 331 if (err) 332 break; 333 } 334 335 if (err) 336 return err; 337 338 sysfs_entries = 1; /* Removed entries on cleanup */ 339 return 0; 340 341 } 342 343 static void __exit pseries_energy_cleanup(void) 344 { 345 int cpu; 346 struct device *cpu_dev; 347 348 if (!sysfs_entries) 349 return; 350 351 /* Remove the sysfs files */ 352 device_remove_file(cpu_subsys.dev_root, &attr_cpu_activate_hint_list); 353 device_remove_file(cpu_subsys.dev_root, &attr_cpu_deactivate_hint_list); 354 355 for_each_possible_cpu(cpu) { 356 cpu_dev = get_cpu_device(cpu); 357 sysfs_remove_file(&cpu_dev->kobj, 358 &attr_percpu_activate_hint.attr); 359 sysfs_remove_file(&cpu_dev->kobj, 360 &attr_percpu_deactivate_hint.attr); 361 } 362 } 363 364 module_init(pseries_energy_init); 365 module_exit(pseries_energy_cleanup); 366 MODULE_DESCRIPTION("Driver for pSeries platform energy management"); 367 MODULE_AUTHOR("Vaidyanathan Srinivasan"); 368 MODULE_LICENSE("GPL"); 369