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>
35 #include <asm/iseries/it_exp_vpd_panel.h>
37 #include <asm/vdso_datapage.h>
39 #define MODULE_VERS "1.7"
40 #define MODULE_NAME "lparcfg"
42 /* #define LPARCFG_DEBUG */
44 static struct proc_dir_entry *proc_ppc64_lparcfg;
45 #define LPARCFG_BUFF_SIZE 4096
48 * Track sum of all purrs across all processors. This is used to further
49 * calculate usage values by different applications
51 static unsigned long get_purr(void)
53 unsigned long sum_purr = 0;
56 for_each_possible_cpu(cpu) {
57 if (firmware_has_feature(FW_FEATURE_ISERIES))
58 sum_purr += lppaca[cpu].emulated_time_base;
62 cu = &per_cpu(cpu_usage_array, cpu);
63 sum_purr += cu->current_tb;
69 #ifdef CONFIG_PPC_ISERIES
72 * Methods used to fetch LPAR data when running on an iSeries platform.
74 static int iseries_lparcfg_data(struct seq_file *m, void *v)
76 unsigned long pool_id;
77 int shared, entitled_capacity, max_entitled_capacity;
78 int processors, max_processors;
79 unsigned long purr = get_purr();
81 shared = (int)(get_lppaca()->shared_proc);
83 seq_printf(m, "system_active_processors=%d\n",
84 (int)HvLpConfig_getSystemPhysicalProcessors());
86 seq_printf(m, "system_potential_processors=%d\n",
87 (int)HvLpConfig_getSystemPhysicalProcessors());
89 processors = (int)HvLpConfig_getPhysicalProcessors();
90 seq_printf(m, "partition_active_processors=%d\n", processors);
92 max_processors = (int)HvLpConfig_getMaxPhysicalProcessors();
93 seq_printf(m, "partition_potential_processors=%d\n", max_processors);
96 entitled_capacity = HvLpConfig_getSharedProcUnits();
97 max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits();
99 entitled_capacity = processors * 100;
100 max_entitled_capacity = max_processors * 100;
102 seq_printf(m, "partition_entitled_capacity=%d\n", entitled_capacity);
104 seq_printf(m, "partition_max_entitled_capacity=%d\n",
105 max_entitled_capacity);
108 pool_id = HvLpConfig_getSharedPoolIndex();
109 seq_printf(m, "pool=%d\n", (int)pool_id);
110 seq_printf(m, "pool_capacity=%d\n",
111 (int)(HvLpConfig_getNumProcsInSharedPool(pool_id) *
113 seq_printf(m, "purr=%ld\n", purr);
116 seq_printf(m, "shared_processor_mode=%d\n", shared);
121 #else /* CONFIG_PPC_ISERIES */
123 static int iseries_lparcfg_data(struct seq_file *m, void *v)
128 #endif /* CONFIG_PPC_ISERIES */
130 #ifdef CONFIG_PPC_PSERIES
132 * Methods used to fetch LPAR data when running on a pSeries platform.
134 /* find a better place for this function... */
135 static void log_plpar_hcall_return(unsigned long rc, char *tag)
137 if (rc == 0) /* success, return */
139 /* check for null tag ? */
140 if (rc == H_HARDWARE)
142 "plpar-hcall (%s) failed with hardware fault\n", tag);
143 else if (rc == H_FUNCTION)
145 "plpar-hcall (%s) failed; function not allowed\n", tag);
146 else if (rc == H_AUTHORITY)
148 "plpar-hcall (%s) failed; not authorized to this"
150 else if (rc == H_PARAMETER)
151 printk(KERN_INFO "plpar-hcall (%s) failed; Bad parameter(s)\n",
155 "plpar-hcall (%s) failed with unexpected rc(0x%lx)\n",
161 * H_GET_PPP hcall returns info in 4 parms.
162 * entitled_capacity,unallocated_capacity,
163 * aggregation, resource_capability).
165 * R4 = Entitled Processor Capacity Percentage.
166 * R5 = Unallocated Processor Capacity Percentage.
167 * R6 (AABBCCDDEEFFGGHH).
168 * XXXX - reserved (0)
169 * XXXX - reserved (0)
170 * XXXX - Group Number
171 * XXXX - Pool Number.
172 * R7 (IIJJKKLLMMNNOOPP).
174 * XX - bit 0-6 reserved (0). bit 7 is Capped indicator.
175 * XX - variable processor Capacity Weight
176 * XX - Unallocated Variable Processor Capacity Weight.
177 * XXXX - Active processors in Physical Processor Pool.
178 * XXXX - Processors active on platform.
180 static unsigned int h_get_ppp(unsigned long *entitled,
181 unsigned long *unallocated,
182 unsigned long *aggregation,
183 unsigned long *resource)
186 rc = plpar_hcall_4out(H_GET_PPP, 0, 0, 0, 0, entitled, unallocated,
187 aggregation, resource);
189 log_plpar_hcall_return(rc, "H_GET_PPP");
194 static void h_pic(unsigned long *pool_idle_time, unsigned long *num_procs)
198 rc = plpar_hcall(H_PIC, 0, 0, 0, 0, pool_idle_time, num_procs, &dummy);
200 if (rc != H_AUTHORITY)
201 log_plpar_hcall_return(rc, "H_PIC");
204 #define SPLPAR_CHARACTERISTICS_TOKEN 20
205 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
208 * parse_system_parameter_string()
209 * Retrieve the potential_processors, max_entitled_capacity and friends
210 * through the get-system-parameter rtas call. Replace keyword strings as
213 static void parse_system_parameter_string(struct seq_file *m)
217 unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
219 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
220 __FILE__, __FUNCTION__, __LINE__);
224 spin_lock(&rtas_data_buf_lock);
225 memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
226 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
228 SPLPAR_CHARACTERISTICS_TOKEN,
231 memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
232 spin_unlock(&rtas_data_buf_lock);
234 if (call_status != 0) {
236 "%s %s Error calling get-system-parameter (0x%x)\n",
237 __FILE__, __FUNCTION__, call_status);
241 char *workbuffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
243 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
244 __FILE__, __FUNCTION__, __LINE__);
249 printk(KERN_INFO "success calling get-system-parameter \n");
251 splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
252 local_buffer += 2; /* step over strlen value */
254 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
257 while ((*local_buffer) && (idx < splpar_strlen)) {
258 workbuffer[w_idx++] = local_buffer[idx++];
259 if ((local_buffer[idx] == ',')
260 || (local_buffer[idx] == '\0')) {
261 workbuffer[w_idx] = '\0';
263 /* avoid the empty string */
264 seq_printf(m, "%s\n", workbuffer);
266 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
267 idx++; /* skip the comma */
269 } else if (local_buffer[idx] == '=') {
270 /* code here to replace workbuffer contents
271 with different keyword strings */
272 if (0 == strcmp(workbuffer, "MaxEntCap")) {
274 "partition_max_entitled_capacity");
275 w_idx = strlen(workbuffer);
277 if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
279 "system_potential_processors");
280 w_idx = strlen(workbuffer);
285 local_buffer -= 2; /* back up over strlen value */
290 /* Return the number of processors in the system.
291 * This function reads through the device tree and counts
292 * the virtual processors, this does not include threads.
294 static int lparcfg_count_active_processors(void)
296 struct device_node *cpus_dn = NULL;
299 while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
301 printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
308 static int pseries_lparcfg_data(struct seq_file *m, void *v)
310 int partition_potential_processors;
311 int partition_active_processors;
312 struct device_node *rtas_node;
315 rtas_node = find_path_device("/rtas");
317 lrdrp = (int *)get_property(rtas_node, "ibm,lrdr-capacity",
321 partition_potential_processors = vdso_data->processorCount;
323 partition_potential_processors = *(lrdrp + 4);
326 partition_active_processors = lparcfg_count_active_processors();
328 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
329 unsigned long h_entitled, h_unallocated;
330 unsigned long h_aggregation, h_resource;
331 unsigned long pool_idle_time, pool_procs;
334 h_get_ppp(&h_entitled, &h_unallocated, &h_aggregation,
337 seq_printf(m, "R4=0x%lx\n", h_entitled);
338 seq_printf(m, "R5=0x%lx\n", h_unallocated);
339 seq_printf(m, "R6=0x%lx\n", h_aggregation);
340 seq_printf(m, "R7=0x%lx\n", h_resource);
344 /* this call handles the ibm,get-system-parameter contents */
345 parse_system_parameter_string(m);
347 seq_printf(m, "partition_entitled_capacity=%ld\n", h_entitled);
349 seq_printf(m, "group=%ld\n", (h_aggregation >> 2 * 8) & 0xffff);
351 seq_printf(m, "system_active_processors=%ld\n",
352 (h_resource >> 0 * 8) & 0xffff);
354 /* pool related entries are apropriate for shared configs */
355 if (lppaca[0].shared_proc) {
357 h_pic(&pool_idle_time, &pool_procs);
359 seq_printf(m, "pool=%ld\n",
360 (h_aggregation >> 0 * 8) & 0xffff);
362 /* report pool_capacity in percentage */
363 seq_printf(m, "pool_capacity=%ld\n",
364 ((h_resource >> 2 * 8) & 0xffff) * 100);
366 seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
368 seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
371 seq_printf(m, "unallocated_capacity_weight=%ld\n",
372 (h_resource >> 4 * 8) & 0xFF);
374 seq_printf(m, "capacity_weight=%ld\n",
375 (h_resource >> 5 * 8) & 0xFF);
377 seq_printf(m, "capped=%ld\n", (h_resource >> 6 * 8) & 0x01);
379 seq_printf(m, "unallocated_capacity=%ld\n", h_unallocated);
381 seq_printf(m, "purr=%ld\n", purr);
383 } else { /* non SPLPAR case */
385 seq_printf(m, "system_active_processors=%d\n",
386 partition_potential_processors);
388 seq_printf(m, "system_potential_processors=%d\n",
389 partition_potential_processors);
391 seq_printf(m, "partition_max_entitled_capacity=%d\n",
392 partition_potential_processors * 100);
394 seq_printf(m, "partition_entitled_capacity=%d\n",
395 partition_active_processors * 100);
398 seq_printf(m, "partition_active_processors=%d\n",
399 partition_active_processors);
401 seq_printf(m, "partition_potential_processors=%d\n",
402 partition_potential_processors);
404 seq_printf(m, "shared_processor_mode=%d\n", lppaca[0].shared_proc);
410 * Interface for changing system parameters (variable capacity weight
411 * and entitled capacity). Format of input is "param_name=value";
412 * anything after value is ignored. Valid parameters at this time are
413 * "partition_entitled_capacity" and "capacity_weight". We use
414 * H_SET_PPP to alter parameters.
416 * This function should be invoked only on systems with
419 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
420 size_t count, loff_t * off)
424 u64 new_entitled, *new_entitled_ptr = &new_entitled;
425 u8 new_weight, *new_weight_ptr = &new_weight;
427 unsigned long current_entitled; /* parameters for h_get_ppp */
429 unsigned long resource;
432 ssize_t retval = -ENOMEM;
434 kbuf = kmalloc(count, GFP_KERNEL);
439 if (copy_from_user(kbuf, buf, count))
443 kbuf[count - 1] = '\0';
444 tmp = strchr(kbuf, '=');
450 if (!strcmp(kbuf, "partition_entitled_capacity")) {
452 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
455 new_weight_ptr = ¤t_weight;
456 } else if (!strcmp(kbuf, "capacity_weight")) {
458 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
461 new_entitled_ptr = ¤t_entitled;
465 /* Get our current parameters */
466 retval = h_get_ppp(¤t_entitled, &dummy, &dummy, &resource);
472 current_weight = (resource >> 5 * 8) & 0xFF;
474 pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
475 __FUNCTION__, current_entitled, current_weight);
477 pr_debug("%s: new_entitled = %lu, new_weight = %u\n",
478 __FUNCTION__, *new_entitled_ptr, *new_weight_ptr);
480 retval = plpar_hcall_norets(H_SET_PPP, *new_entitled_ptr,
483 if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
485 } else if (retval == H_BUSY) {
487 } else if (retval == H_HARDWARE) {
489 } else if (retval == H_PARAMETER) {
492 printk(KERN_WARNING "%s: received unknown hv return code %ld",
493 __FUNCTION__, retval);
502 #else /* CONFIG_PPC_PSERIES */
504 static int pseries_lparcfg_data(struct seq_file *m, void *v)
509 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
510 size_t count, loff_t * off)
515 #endif /* CONFIG_PPC_PSERIES */
517 static int lparcfg_data(struct seq_file *m, void *v)
519 struct device_node *rootdn;
520 const char *model = "";
521 const char *system_id = "";
523 unsigned int *lp_index_ptr, lp_index = 0;
525 seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
527 rootdn = find_path_device("/");
529 tmp = get_property(rootdn, "model", NULL);
532 /* Skip "IBM," - see platforms/iseries/dt.c */
533 if (firmware_has_feature(FW_FEATURE_ISERIES))
536 tmp = get_property(rootdn, "system-id", NULL);
539 /* Skip "IBM," - see platforms/iseries/dt.c */
540 if (firmware_has_feature(FW_FEATURE_ISERIES))
543 lp_index_ptr = (unsigned int *)
544 get_property(rootdn, "ibm,partition-no", NULL);
546 lp_index = *lp_index_ptr;
548 seq_printf(m, "serial_number=%s\n", system_id);
549 seq_printf(m, "system_type=%s\n", model);
550 seq_printf(m, "partition_id=%d\n", (int)lp_index);
552 if (firmware_has_feature(FW_FEATURE_ISERIES))
553 return iseries_lparcfg_data(m, v);
554 return pseries_lparcfg_data(m, v);
557 static int lparcfg_open(struct inode *inode, struct file *file)
559 return single_open(file, lparcfg_data, NULL);
562 struct file_operations lparcfg_fops = {
563 .owner = THIS_MODULE,
565 .open = lparcfg_open,
566 .release = single_release,
569 int __init lparcfg_init(void)
571 struct proc_dir_entry *ent;
572 mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
574 /* Allow writing if we have FW_FEATURE_SPLPAR */
575 if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
576 !firmware_has_feature(FW_FEATURE_ISERIES)) {
577 lparcfg_fops.write = lparcfg_write;
581 ent = create_proc_entry("ppc64/lparcfg", mode, NULL);
583 ent->proc_fops = &lparcfg_fops;
584 ent->data = kmalloc(LPARCFG_BUFF_SIZE, GFP_KERNEL);
587 "Failed to allocate buffer for lparcfg\n");
588 remove_proc_entry("lparcfg", ent->parent);
592 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
596 proc_ppc64_lparcfg = ent;
600 void __exit lparcfg_cleanup(void)
602 if (proc_ppc64_lparcfg) {
603 kfree(proc_ppc64_lparcfg->data);
604 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
608 module_init(lparcfg_init);
609 module_exit(lparcfg_cleanup);
610 MODULE_DESCRIPTION("Interface for LPAR configuration data");
611 MODULE_AUTHOR("Dave Engebretsen");
612 MODULE_LICENSE("GPL");