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.8"
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.
134 * H_GET_MPP hcall returns info in 7 parms
136 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
139 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
141 rc = plpar_hcall9(H_GET_MPP, retbuf);
143 mpp_data->entitled_mem = retbuf[0];
144 mpp_data->mapped_mem = retbuf[1];
146 mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
147 mpp_data->pool_num = retbuf[2] & 0xffff;
149 mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
150 mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
151 mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffff;
153 mpp_data->pool_size = retbuf[4];
154 mpp_data->loan_request = retbuf[5];
155 mpp_data->backing_mem = retbuf[6];
159 EXPORT_SYMBOL(h_get_mpp);
162 * H_GET_PPP hcall returns info in 4 parms.
163 * entitled_capacity,unallocated_capacity,
164 * aggregation, resource_capability).
166 * R4 = Entitled Processor Capacity Percentage.
167 * R5 = Unallocated Processor Capacity Percentage.
168 * R6 (AABBCCDDEEFFGGHH).
169 * XXXX - reserved (0)
170 * XXXX - reserved (0)
171 * XXXX - Group Number
172 * XXXX - Pool Number.
173 * R7 (IIJJKKLLMMNNOOPP).
175 * XX - bit 0-6 reserved (0). bit 7 is Capped indicator.
176 * XX - variable processor Capacity Weight
177 * XX - Unallocated Variable Processor Capacity Weight.
178 * XXXX - Active processors in Physical Processor Pool.
179 * XXXX - Processors active on platform.
181 static unsigned int h_get_ppp(unsigned long *entitled,
182 unsigned long *unallocated,
183 unsigned long *aggregation,
184 unsigned long *resource)
187 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
189 rc = plpar_hcall(H_GET_PPP, retbuf);
191 *entitled = retbuf[0];
192 *unallocated = retbuf[1];
193 *aggregation = retbuf[2];
194 *resource = retbuf[3];
199 static unsigned h_pic(unsigned long *pool_idle_time,
200 unsigned long *num_procs)
203 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
205 rc = plpar_hcall(H_PIC, retbuf);
207 *pool_idle_time = retbuf[0];
208 *num_procs = retbuf[1];
215 * Parse out the data returned from h_get_ppp and h_pic
217 static void parse_ppp_data(struct seq_file *m)
219 unsigned long h_entitled, h_unallocated;
220 unsigned long h_aggregation, h_resource;
223 rc = h_get_ppp(&h_entitled, &h_unallocated, &h_aggregation,
228 seq_printf(m, "partition_entitled_capacity=%ld\n", h_entitled);
229 seq_printf(m, "group=%ld\n", (h_aggregation >> 2 * 8) & 0xffff);
230 seq_printf(m, "system_active_processors=%ld\n",
231 (h_resource >> 0 * 8) & 0xffff);
233 /* pool related entries are apropriate for shared configs */
234 if (lppaca[0].shared_proc) {
235 unsigned long pool_idle_time, pool_procs;
237 seq_printf(m, "pool=%ld\n", (h_aggregation >> 0 * 8) & 0xffff);
239 /* report pool_capacity in percentage */
240 seq_printf(m, "pool_capacity=%ld\n",
241 ((h_resource >> 2 * 8) & 0xffff) * 100);
243 h_pic(&pool_idle_time, &pool_procs);
244 seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
245 seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
248 seq_printf(m, "unallocated_capacity_weight=%ld\n",
249 (h_resource >> 4 * 8) & 0xFF);
251 seq_printf(m, "capacity_weight=%ld\n", (h_resource >> 5 * 8) & 0xFF);
252 seq_printf(m, "capped=%ld\n", (h_resource >> 6 * 8) & 0x01);
253 seq_printf(m, "unallocated_capacity=%ld\n", h_unallocated);
258 * Parse out data returned from h_get_mpp
260 static void parse_mpp_data(struct seq_file *m)
262 struct hvcall_mpp_data mpp_data;
265 rc = h_get_mpp(&mpp_data);
269 seq_printf(m, "entitled_memory=%ld\n", mpp_data.entitled_mem);
271 if (mpp_data.mapped_mem != -1)
272 seq_printf(m, "mapped_entitled_memory=%ld\n",
273 mpp_data.mapped_mem);
275 seq_printf(m, "entitled_memory_group_number=%d\n", mpp_data.group_num);
276 seq_printf(m, "entitled_memory_pool_number=%d\n", mpp_data.pool_num);
278 seq_printf(m, "entitled_memory_weight=%d\n", mpp_data.mem_weight);
279 seq_printf(m, "unallocated_entitled_memory_weight=%d\n",
280 mpp_data.unallocated_mem_weight);
281 seq_printf(m, "unallocated_io_mapping_entitlement=%ld\n",
282 mpp_data.unallocated_entitlement);
284 if (mpp_data.pool_size != -1)
285 seq_printf(m, "entitled_memory_pool_size=%ld bytes\n",
288 seq_printf(m, "entitled_memory_loan_request=%ld\n",
289 mpp_data.loan_request);
291 seq_printf(m, "backing_memory=%ld bytes\n", mpp_data.backing_mem);
294 #define SPLPAR_CHARACTERISTICS_TOKEN 20
295 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
298 * parse_system_parameter_string()
299 * Retrieve the potential_processors, max_entitled_capacity and friends
300 * through the get-system-parameter rtas call. Replace keyword strings as
303 static void parse_system_parameter_string(struct seq_file *m)
307 unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
309 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
310 __FILE__, __func__, __LINE__);
314 spin_lock(&rtas_data_buf_lock);
315 memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
316 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
318 SPLPAR_CHARACTERISTICS_TOKEN,
321 memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
322 spin_unlock(&rtas_data_buf_lock);
324 if (call_status != 0) {
326 "%s %s Error calling get-system-parameter (0x%x)\n",
327 __FILE__, __func__, call_status);
331 char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
333 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
334 __FILE__, __func__, __LINE__);
339 printk(KERN_INFO "success calling get-system-parameter \n");
341 splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
342 local_buffer += 2; /* step over strlen value */
346 while ((*local_buffer) && (idx < splpar_strlen)) {
347 workbuffer[w_idx++] = local_buffer[idx++];
348 if ((local_buffer[idx] == ',')
349 || (local_buffer[idx] == '\0')) {
350 workbuffer[w_idx] = '\0';
352 /* avoid the empty string */
353 seq_printf(m, "%s\n", workbuffer);
355 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
356 idx++; /* skip the comma */
358 } else if (local_buffer[idx] == '=') {
359 /* code here to replace workbuffer contents
360 with different keyword strings */
361 if (0 == strcmp(workbuffer, "MaxEntCap")) {
363 "partition_max_entitled_capacity");
364 w_idx = strlen(workbuffer);
366 if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
368 "system_potential_processors");
369 w_idx = strlen(workbuffer);
374 local_buffer -= 2; /* back up over strlen value */
379 /* Return the number of processors in the system.
380 * This function reads through the device tree and counts
381 * the virtual processors, this does not include threads.
383 static int lparcfg_count_active_processors(void)
385 struct device_node *cpus_dn = NULL;
388 while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
390 printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
397 static int pseries_lparcfg_data(struct seq_file *m, void *v)
399 int partition_potential_processors;
400 int partition_active_processors;
401 struct device_node *rtas_node;
402 const int *lrdrp = NULL;
404 rtas_node = of_find_node_by_path("/rtas");
406 lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
409 partition_potential_processors = vdso_data->processorCount;
411 partition_potential_processors = *(lrdrp + 4);
413 of_node_put(rtas_node);
415 partition_active_processors = lparcfg_count_active_processors();
417 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
418 /* this call handles the ibm,get-system-parameter contents */
419 parse_system_parameter_string(m);
423 seq_printf(m, "purr=%ld\n", get_purr());
424 } else { /* non SPLPAR case */
426 seq_printf(m, "system_active_processors=%d\n",
427 partition_potential_processors);
429 seq_printf(m, "system_potential_processors=%d\n",
430 partition_potential_processors);
432 seq_printf(m, "partition_max_entitled_capacity=%d\n",
433 partition_potential_processors * 100);
435 seq_printf(m, "partition_entitled_capacity=%d\n",
436 partition_active_processors * 100);
439 seq_printf(m, "partition_active_processors=%d\n",
440 partition_active_processors);
442 seq_printf(m, "partition_potential_processors=%d\n",
443 partition_potential_processors);
445 seq_printf(m, "shared_processor_mode=%d\n", lppaca[0].shared_proc);
450 static ssize_t update_ppp(u64 *entitlement, u8 *weight)
452 unsigned long current_entitled;
454 unsigned long resource;
455 u8 current_weight, new_weight;
459 /* Get our current parameters */
460 retval = h_get_ppp(¤t_entitled, &dummy, &dummy, &resource);
464 current_weight = (resource >> 5 * 8) & 0xFF;
467 new_weight = current_weight;
468 new_entitled = *entitlement;
470 new_weight = *weight;
471 new_entitled = current_entitled;
475 pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
476 __FUNCTION__, current_entitled, current_weight);
478 pr_debug("%s: new_entitled = %lu, new_weight = %u\n",
479 __FUNCTION__, new_entitled, new_weight);
481 retval = plpar_hcall_norets(H_SET_PPP, new_entitled, new_weight);
488 * Update the memory entitlement and weight for the partition. Caller must
489 * specify either a new entitlement or weight, not both, to be updated
490 * since the h_set_mpp call takes both entitlement and weight as parameters.
492 static ssize_t update_mpp(u64 *entitlement, u8 *weight)
494 struct hvcall_mpp_data mpp_data;
499 rc = h_get_mpp(&mpp_data);
504 new_weight = mpp_data.mem_weight;
505 new_entitled = *entitlement;
507 new_weight = *weight;
508 new_entitled = mpp_data.entitled_mem;
512 pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
513 __FUNCTION__, mpp_data.entitled_mem, mpp_data.mem_weight);
515 pr_debug("%s: new_entitled = %lu, new_weight = %u\n",
516 __FUNCTION__, new_entitled, new_weight);
518 rc = plpar_hcall_norets(H_SET_MPP, new_entitled, new_weight);
523 * Interface for changing system parameters (variable capacity weight
524 * and entitled capacity). Format of input is "param_name=value";
525 * anything after value is ignored. Valid parameters at this time are
526 * "partition_entitled_capacity" and "capacity_weight". We use
527 * H_SET_PPP to alter parameters.
529 * This function should be invoked only on systems with
532 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
533 size_t count, loff_t * off)
537 u64 new_entitled, *new_entitled_ptr = &new_entitled;
538 u8 new_weight, *new_weight_ptr = &new_weight;
539 ssize_t retval = -ENOMEM;
541 if (!firmware_has_feature(FW_FEATURE_SPLPAR) ||
542 firmware_has_feature(FW_FEATURE_ISERIES))
545 kbuf = kmalloc(count, GFP_KERNEL);
550 if (copy_from_user(kbuf, buf, count))
554 kbuf[count - 1] = '\0';
555 tmp = strchr(kbuf, '=');
561 if (!strcmp(kbuf, "partition_entitled_capacity")) {
563 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
567 retval = update_ppp(new_entitled_ptr, NULL);
568 } else if (!strcmp(kbuf, "capacity_weight")) {
570 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
574 retval = update_ppp(NULL, new_weight_ptr);
575 } else if (!strcmp(kbuf, "entitled_memory")) {
577 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
581 retval = update_mpp(new_entitled_ptr, NULL);
582 } else if (!strcmp(kbuf, "entitled_memory_weight")) {
584 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
588 retval = update_mpp(NULL, new_weight_ptr);
592 if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
594 } else if (retval == H_BUSY) {
596 } else if (retval == H_HARDWARE) {
598 } else if (retval == H_PARAMETER) {
601 printk(KERN_WARNING "%s: received unknown hv return code %ld",
611 #else /* CONFIG_PPC_PSERIES */
613 static int pseries_lparcfg_data(struct seq_file *m, void *v)
618 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
619 size_t count, loff_t * off)
624 #endif /* CONFIG_PPC_PSERIES */
626 static int lparcfg_data(struct seq_file *m, void *v)
628 struct device_node *rootdn;
629 const char *model = "";
630 const char *system_id = "";
632 const unsigned int *lp_index_ptr;
633 unsigned int lp_index = 0;
635 seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
637 rootdn = of_find_node_by_path("/");
639 tmp = of_get_property(rootdn, "model", NULL);
642 /* Skip "IBM," - see platforms/iseries/dt.c */
643 if (firmware_has_feature(FW_FEATURE_ISERIES))
646 tmp = of_get_property(rootdn, "system-id", NULL);
649 /* Skip "IBM," - see platforms/iseries/dt.c */
650 if (firmware_has_feature(FW_FEATURE_ISERIES))
653 lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
656 lp_index = *lp_index_ptr;
659 seq_printf(m, "serial_number=%s\n", system_id);
660 seq_printf(m, "system_type=%s\n", model);
661 seq_printf(m, "partition_id=%d\n", (int)lp_index);
663 if (firmware_has_feature(FW_FEATURE_ISERIES))
664 return iseries_lparcfg_data(m, v);
665 return pseries_lparcfg_data(m, v);
668 static int lparcfg_open(struct inode *inode, struct file *file)
670 return single_open(file, lparcfg_data, NULL);
673 static const struct file_operations lparcfg_fops = {
674 .owner = THIS_MODULE,
676 .write = lparcfg_write,
677 .open = lparcfg_open,
678 .release = single_release,
681 static int __init lparcfg_init(void)
683 struct proc_dir_entry *ent;
684 mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
686 /* Allow writing if we have FW_FEATURE_SPLPAR */
687 if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
688 !firmware_has_feature(FW_FEATURE_ISERIES))
691 ent = proc_create("ppc64/lparcfg", mode, NULL, &lparcfg_fops);
693 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
697 proc_ppc64_lparcfg = ent;
701 static void __exit lparcfg_cleanup(void)
703 if (proc_ppc64_lparcfg)
704 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
707 module_init(lparcfg_init);
708 module_exit(lparcfg_cleanup);
709 MODULE_DESCRIPTION("Interface for LPAR configuration data");
710 MODULE_AUTHOR("Dave Engebretsen");
711 MODULE_LICENSE("GPL");