2 * PowerPC64 LPAR Configuration Information Driver
4 * Dave Engebretsen engebret@us.ibm.com
5 * Copyright (c) 2003 Dave Engebretsen
6 * Will Schmidt willschm@us.ibm.com
7 * SPLPAR updates, Copyright (c) 2003 Will Schmidt IBM Corporation.
8 * seq_file updates, Copyright (c) 2004 Will Schmidt IBM Corporation.
9 * Nathan Lynch nathanl@austin.ibm.com
10 * Added lparcfg_write, Copyright (C) 2004 Nathan Lynch IBM Corporation.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * This driver creates a proc file at /proc/ppc64/lparcfg which contains
18 * keyword - value pairs that specify the configuration of the partition.
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/proc_fs.h>
25 #include <linux/init.h>
26 #include <linux/seq_file.h>
27 #include <asm/uaccess.h>
28 #include <asm/iseries/hv_lp_config.h>
29 #include <asm/lppaca.h>
30 #include <asm/hvcall.h>
31 #include <asm/firmware.h>
33 #include <asm/system.h>
36 #include <asm/vdso_datapage.h>
38 #define MODULE_VERS "1.7"
39 #define MODULE_NAME "lparcfg"
41 /* #define LPARCFG_DEBUG */
43 static struct proc_dir_entry *proc_ppc64_lparcfg;
46 * Track sum of all purrs across all processors. This is used to further
47 * calculate usage values by different applications
49 static unsigned long get_purr(void)
51 unsigned long sum_purr = 0;
54 for_each_possible_cpu(cpu) {
55 if (firmware_has_feature(FW_FEATURE_ISERIES))
56 sum_purr += lppaca[cpu].emulated_time_base;
60 cu = &per_cpu(cpu_usage_array, cpu);
61 sum_purr += cu->current_tb;
67 #ifdef CONFIG_PPC_ISERIES
70 * Methods used to fetch LPAR data when running on an iSeries platform.
72 static int iseries_lparcfg_data(struct seq_file *m, void *v)
74 unsigned long pool_id;
75 int shared, entitled_capacity, max_entitled_capacity;
76 int processors, max_processors;
77 unsigned long purr = get_purr();
79 shared = (int)(local_paca->lppaca_ptr->shared_proc);
81 seq_printf(m, "system_active_processors=%d\n",
82 (int)HvLpConfig_getSystemPhysicalProcessors());
84 seq_printf(m, "system_potential_processors=%d\n",
85 (int)HvLpConfig_getSystemPhysicalProcessors());
87 processors = (int)HvLpConfig_getPhysicalProcessors();
88 seq_printf(m, "partition_active_processors=%d\n", processors);
90 max_processors = (int)HvLpConfig_getMaxPhysicalProcessors();
91 seq_printf(m, "partition_potential_processors=%d\n", max_processors);
94 entitled_capacity = HvLpConfig_getSharedProcUnits();
95 max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits();
97 entitled_capacity = processors * 100;
98 max_entitled_capacity = max_processors * 100;
100 seq_printf(m, "partition_entitled_capacity=%d\n", entitled_capacity);
102 seq_printf(m, "partition_max_entitled_capacity=%d\n",
103 max_entitled_capacity);
106 pool_id = HvLpConfig_getSharedPoolIndex();
107 seq_printf(m, "pool=%d\n", (int)pool_id);
108 seq_printf(m, "pool_capacity=%d\n",
109 (int)(HvLpConfig_getNumProcsInSharedPool(pool_id) *
111 seq_printf(m, "purr=%ld\n", purr);
114 seq_printf(m, "shared_processor_mode=%d\n", shared);
119 #else /* CONFIG_PPC_ISERIES */
121 static int iseries_lparcfg_data(struct seq_file *m, void *v)
126 #endif /* CONFIG_PPC_ISERIES */
128 #ifdef CONFIG_PPC_PSERIES
130 * Methods used to fetch LPAR data when running on a pSeries platform.
133 * H_GET_PPP hcall returns info in 4 parms.
134 * entitled_capacity,unallocated_capacity,
135 * aggregation, resource_capability).
137 * R4 = Entitled Processor Capacity Percentage.
138 * R5 = Unallocated Processor Capacity Percentage.
139 * R6 (AABBCCDDEEFFGGHH).
140 * XXXX - reserved (0)
141 * XXXX - reserved (0)
142 * XXXX - Group Number
143 * XXXX - Pool Number.
144 * R7 (IIJJKKLLMMNNOOPP).
146 * XX - bit 0-6 reserved (0). bit 7 is Capped indicator.
147 * XX - variable processor Capacity Weight
148 * XX - Unallocated Variable Processor Capacity Weight.
149 * XXXX - Active processors in Physical Processor Pool.
150 * XXXX - Processors active on platform.
152 static unsigned int h_get_ppp(unsigned long *entitled,
153 unsigned long *unallocated,
154 unsigned long *aggregation,
155 unsigned long *resource)
158 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
160 rc = plpar_hcall(H_GET_PPP, retbuf);
162 *entitled = retbuf[0];
163 *unallocated = retbuf[1];
164 *aggregation = retbuf[2];
165 *resource = retbuf[3];
170 static void h_pic(unsigned long *pool_idle_time, unsigned long *num_procs)
173 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
175 rc = plpar_hcall(H_PIC, retbuf);
177 *pool_idle_time = retbuf[0];
178 *num_procs = retbuf[1];
181 #define SPLPAR_CHARACTERISTICS_TOKEN 20
182 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
185 * parse_system_parameter_string()
186 * Retrieve the potential_processors, max_entitled_capacity and friends
187 * through the get-system-parameter rtas call. Replace keyword strings as
190 static void parse_system_parameter_string(struct seq_file *m)
194 unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
196 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
197 __FILE__, __func__, __LINE__);
201 spin_lock(&rtas_data_buf_lock);
202 memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
203 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
205 SPLPAR_CHARACTERISTICS_TOKEN,
208 memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
209 spin_unlock(&rtas_data_buf_lock);
211 if (call_status != 0) {
213 "%s %s Error calling get-system-parameter (0x%x)\n",
214 __FILE__, __func__, call_status);
218 char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
220 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
221 __FILE__, __func__, __LINE__);
226 printk(KERN_INFO "success calling get-system-parameter \n");
228 splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
229 local_buffer += 2; /* step over strlen value */
233 while ((*local_buffer) && (idx < splpar_strlen)) {
234 workbuffer[w_idx++] = local_buffer[idx++];
235 if ((local_buffer[idx] == ',')
236 || (local_buffer[idx] == '\0')) {
237 workbuffer[w_idx] = '\0';
239 /* avoid the empty string */
240 seq_printf(m, "%s\n", workbuffer);
242 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
243 idx++; /* skip the comma */
245 } else if (local_buffer[idx] == '=') {
246 /* code here to replace workbuffer contents
247 with different keyword strings */
248 if (0 == strcmp(workbuffer, "MaxEntCap")) {
250 "partition_max_entitled_capacity");
251 w_idx = strlen(workbuffer);
253 if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
255 "system_potential_processors");
256 w_idx = strlen(workbuffer);
261 local_buffer -= 2; /* back up over strlen value */
266 /* Return the number of processors in the system.
267 * This function reads through the device tree and counts
268 * the virtual processors, this does not include threads.
270 static int lparcfg_count_active_processors(void)
272 struct device_node *cpus_dn = NULL;
275 while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
277 printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
284 static int pseries_lparcfg_data(struct seq_file *m, void *v)
286 int partition_potential_processors;
287 int partition_active_processors;
288 struct device_node *rtas_node;
289 const int *lrdrp = NULL;
291 rtas_node = of_find_node_by_path("/rtas");
293 lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
296 partition_potential_processors = vdso_data->processorCount;
298 partition_potential_processors = *(lrdrp + 4);
300 of_node_put(rtas_node);
302 partition_active_processors = lparcfg_count_active_processors();
304 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
305 unsigned long h_entitled, h_unallocated;
306 unsigned long h_aggregation, h_resource;
307 unsigned long pool_idle_time, pool_procs;
310 h_get_ppp(&h_entitled, &h_unallocated, &h_aggregation,
313 seq_printf(m, "R4=0x%lx\n", h_entitled);
314 seq_printf(m, "R5=0x%lx\n", h_unallocated);
315 seq_printf(m, "R6=0x%lx\n", h_aggregation);
316 seq_printf(m, "R7=0x%lx\n", h_resource);
320 /* this call handles the ibm,get-system-parameter contents */
321 parse_system_parameter_string(m);
323 seq_printf(m, "partition_entitled_capacity=%ld\n", h_entitled);
325 seq_printf(m, "group=%ld\n", (h_aggregation >> 2 * 8) & 0xffff);
327 seq_printf(m, "system_active_processors=%ld\n",
328 (h_resource >> 0 * 8) & 0xffff);
330 /* pool related entries are apropriate for shared configs */
331 if (lppaca[0].shared_proc) {
333 h_pic(&pool_idle_time, &pool_procs);
335 seq_printf(m, "pool=%ld\n",
336 (h_aggregation >> 0 * 8) & 0xffff);
338 /* report pool_capacity in percentage */
339 seq_printf(m, "pool_capacity=%ld\n",
340 ((h_resource >> 2 * 8) & 0xffff) * 100);
342 seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
344 seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
347 seq_printf(m, "unallocated_capacity_weight=%ld\n",
348 (h_resource >> 4 * 8) & 0xFF);
350 seq_printf(m, "capacity_weight=%ld\n",
351 (h_resource >> 5 * 8) & 0xFF);
353 seq_printf(m, "capped=%ld\n", (h_resource >> 6 * 8) & 0x01);
355 seq_printf(m, "unallocated_capacity=%ld\n", h_unallocated);
357 seq_printf(m, "purr=%ld\n", purr);
359 } else { /* non SPLPAR case */
361 seq_printf(m, "system_active_processors=%d\n",
362 partition_potential_processors);
364 seq_printf(m, "system_potential_processors=%d\n",
365 partition_potential_processors);
367 seq_printf(m, "partition_max_entitled_capacity=%d\n",
368 partition_potential_processors * 100);
370 seq_printf(m, "partition_entitled_capacity=%d\n",
371 partition_active_processors * 100);
374 seq_printf(m, "partition_active_processors=%d\n",
375 partition_active_processors);
377 seq_printf(m, "partition_potential_processors=%d\n",
378 partition_potential_processors);
380 seq_printf(m, "shared_processor_mode=%d\n", lppaca[0].shared_proc);
386 * Interface for changing system parameters (variable capacity weight
387 * and entitled capacity). Format of input is "param_name=value";
388 * anything after value is ignored. Valid parameters at this time are
389 * "partition_entitled_capacity" and "capacity_weight". We use
390 * H_SET_PPP to alter parameters.
392 * This function should be invoked only on systems with
395 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
396 size_t count, loff_t * off)
400 u64 new_entitled, *new_entitled_ptr = &new_entitled;
401 u8 new_weight, *new_weight_ptr = &new_weight;
403 unsigned long current_entitled; /* parameters for h_get_ppp */
405 unsigned long resource;
408 ssize_t retval = -ENOMEM;
410 if (!firmware_has_feature(FW_FEATURE_SPLPAR) ||
411 firmware_has_feature(FW_FEATURE_ISERIES))
414 kbuf = kmalloc(count, GFP_KERNEL);
419 if (copy_from_user(kbuf, buf, count))
423 kbuf[count - 1] = '\0';
424 tmp = strchr(kbuf, '=');
430 if (!strcmp(kbuf, "partition_entitled_capacity")) {
432 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
435 new_weight_ptr = ¤t_weight;
436 } else if (!strcmp(kbuf, "capacity_weight")) {
438 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
441 new_entitled_ptr = ¤t_entitled;
445 /* Get our current parameters */
446 retval = h_get_ppp(¤t_entitled, &dummy, &dummy, &resource);
452 current_weight = (resource >> 5 * 8) & 0xFF;
454 pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
455 __func__, current_entitled, current_weight);
457 pr_debug("%s: new_entitled = %lu, new_weight = %u\n",
458 __func__, *new_entitled_ptr, *new_weight_ptr);
460 retval = plpar_hcall_norets(H_SET_PPP, *new_entitled_ptr,
463 if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
465 } else if (retval == H_BUSY) {
467 } else if (retval == H_HARDWARE) {
469 } else if (retval == H_PARAMETER) {
472 printk(KERN_WARNING "%s: received unknown hv return code %ld",
482 #else /* CONFIG_PPC_PSERIES */
484 static int pseries_lparcfg_data(struct seq_file *m, void *v)
489 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
490 size_t count, loff_t * off)
495 #endif /* CONFIG_PPC_PSERIES */
497 static int lparcfg_data(struct seq_file *m, void *v)
499 struct device_node *rootdn;
500 const char *model = "";
501 const char *system_id = "";
503 const unsigned int *lp_index_ptr;
504 unsigned int lp_index = 0;
506 seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
508 rootdn = of_find_node_by_path("/");
510 tmp = of_get_property(rootdn, "model", NULL);
513 /* Skip "IBM," - see platforms/iseries/dt.c */
514 if (firmware_has_feature(FW_FEATURE_ISERIES))
517 tmp = of_get_property(rootdn, "system-id", NULL);
520 /* Skip "IBM," - see platforms/iseries/dt.c */
521 if (firmware_has_feature(FW_FEATURE_ISERIES))
524 lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
527 lp_index = *lp_index_ptr;
530 seq_printf(m, "serial_number=%s\n", system_id);
531 seq_printf(m, "system_type=%s\n", model);
532 seq_printf(m, "partition_id=%d\n", (int)lp_index);
534 if (firmware_has_feature(FW_FEATURE_ISERIES))
535 return iseries_lparcfg_data(m, v);
536 return pseries_lparcfg_data(m, v);
539 static int lparcfg_open(struct inode *inode, struct file *file)
541 return single_open(file, lparcfg_data, NULL);
544 static const struct file_operations lparcfg_fops = {
545 .owner = THIS_MODULE,
547 .write = lparcfg_write,
548 .open = lparcfg_open,
549 .release = single_release,
552 static int __init lparcfg_init(void)
554 struct proc_dir_entry *ent;
555 mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
557 /* Allow writing if we have FW_FEATURE_SPLPAR */
558 if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
559 !firmware_has_feature(FW_FEATURE_ISERIES))
562 ent = proc_create("ppc64/lparcfg", mode, NULL, &lparcfg_fops);
564 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
568 proc_ppc64_lparcfg = ent;
572 static void __exit lparcfg_cleanup(void)
574 if (proc_ppc64_lparcfg)
575 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
578 module_init(lparcfg_init);
579 module_exit(lparcfg_cleanup);
580 MODULE_DESCRIPTION("Interface for LPAR configuration data");
581 MODULE_AUTHOR("Dave Engebretsen");
582 MODULE_LICENSE("GPL");