1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PowerPC64 LPAR Configuration Information Driver
5 * Dave Engebretsen engebret@us.ibm.com
6 * Copyright (c) 2003 Dave Engebretsen
7 * Will Schmidt willschm@us.ibm.com
8 * SPLPAR updates, Copyright (c) 2003 Will Schmidt IBM Corporation.
9 * seq_file updates, Copyright (c) 2004 Will Schmidt IBM Corporation.
10 * Nathan Lynch nathanl@austin.ibm.com
11 * Added lparcfg_write, Copyright (C) 2004 Nathan Lynch IBM Corporation.
13 * This driver creates a proc file at /proc/ppc64/lparcfg which contains
14 * keyword - value pairs that specify the configuration of the partition.
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/proc_fs.h>
21 #include <linux/init.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
25 #include <linux/hugetlb.h>
26 #include <asm/lppaca.h>
27 #include <asm/hvcall.h>
28 #include <asm/firmware.h>
32 #include <asm/vdso_datapage.h>
35 #include <asm/machdep.h>
36 #include <asm/drmem.h>
41 * This isn't a module but we expose that to userspace
42 * via /proc so leave the definitions here
44 #define MODULE_VERS "1.9"
45 #define MODULE_NAME "lparcfg"
47 /* #define LPARCFG_DEBUG */
50 * Track sum of all purrs across all processors. This is used to further
51 * calculate usage values by different applications
53 static void cpu_get_purr(void *arg
)
55 atomic64_t
*sum
= arg
;
57 atomic64_add(mfspr(SPRN_PURR
), sum
);
60 static unsigned long get_purr(void)
62 atomic64_t purr
= ATOMIC64_INIT(0);
64 on_each_cpu(cpu_get_purr
, &purr
, 1);
66 return atomic64_read(&purr
);
70 * Methods used to fetch LPAR data when running on a pSeries platform.
73 struct hvcall_ppp_data
{
75 u64 unallocated_entitlement
;
80 u8 unallocated_weight
;
81 u16 active_procs_in_pool
;
82 u16 active_system_procs
;
83 u16 phys_platform_procs
;
84 u32 max_proc_cap_avail
;
85 u32 entitled_proc_cap_avail
;
89 * H_GET_PPP hcall returns info in 4 parms.
90 * entitled_capacity,unallocated_capacity,
91 * aggregation, resource_capability).
93 * R4 = Entitled Processor Capacity Percentage.
94 * R5 = Unallocated Processor Capacity Percentage.
95 * R6 (AABBCCDDEEFFGGHH).
100 * R7 (IIJJKKLLMMNNOOPP).
102 * XX - bit 0-6 reserved (0). bit 7 is Capped indicator.
103 * XX - variable processor Capacity Weight
104 * XX - Unallocated Variable Processor Capacity Weight.
105 * XXXX - Active processors in Physical Processor Pool.
106 * XXXX - Processors active on platform.
107 * R8 (QQQQRRRRRRSSSSSS). if ibm,partition-performance-parameters-level >= 1
108 * XXXX - Physical platform procs allocated to virtualization.
109 * XXXXXX - Max procs capacity % available to the partitions pool.
110 * XXXXXX - Entitled procs capacity % available to the
113 static unsigned int h_get_ppp(struct hvcall_ppp_data
*ppp_data
)
116 unsigned long retbuf
[PLPAR_HCALL9_BUFSIZE
];
118 rc
= plpar_hcall9(H_GET_PPP
, retbuf
);
120 ppp_data
->entitlement
= retbuf
[0];
121 ppp_data
->unallocated_entitlement
= retbuf
[1];
123 ppp_data
->group_num
= (retbuf
[2] >> 2 * 8) & 0xffff;
124 ppp_data
->pool_num
= retbuf
[2] & 0xffff;
126 ppp_data
->capped
= (retbuf
[3] >> 6 * 8) & 0x01;
127 ppp_data
->weight
= (retbuf
[3] >> 5 * 8) & 0xff;
128 ppp_data
->unallocated_weight
= (retbuf
[3] >> 4 * 8) & 0xff;
129 ppp_data
->active_procs_in_pool
= (retbuf
[3] >> 2 * 8) & 0xffff;
130 ppp_data
->active_system_procs
= retbuf
[3] & 0xffff;
132 ppp_data
->phys_platform_procs
= retbuf
[4] >> 6 * 8;
133 ppp_data
->max_proc_cap_avail
= (retbuf
[4] >> 3 * 8) & 0xffffff;
134 ppp_data
->entitled_proc_cap_avail
= retbuf
[4] & 0xffffff;
139 static void show_gpci_data(struct seq_file
*m
)
141 struct hv_gpci_request_buffer
*buf
;
142 unsigned int affinity_score
;
145 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
150 * Show the local LPAR's affinity score.
152 * 0xB1 selects the Affinity_Domain_Info_By_Partition subcall.
153 * The score is at byte 0xB in the output buffer.
155 memset(&buf
->params
, 0, sizeof(buf
->params
));
156 buf
->params
.counter_request
= cpu_to_be32(0xB1);
157 buf
->params
.starting_index
= cpu_to_be32(-1); /* local LPAR */
158 buf
->params
.counter_info_version_in
= 0x5; /* v5+ for score */
159 ret
= plpar_hcall_norets(H_GET_PERF_COUNTER_INFO
, virt_to_phys(buf
),
161 if (ret
!= H_SUCCESS
) {
162 pr_debug("hcall failed: H_GET_PERF_COUNTER_INFO: %ld, %x\n",
163 ret
, be32_to_cpu(buf
->params
.detail_rc
));
166 affinity_score
= buf
->bytes
[0xB];
167 seq_printf(m
, "partition_affinity_score=%u\n", affinity_score
);
172 static unsigned h_pic(unsigned long *pool_idle_time
,
173 unsigned long *num_procs
)
176 unsigned long retbuf
[PLPAR_HCALL_BUFSIZE
];
178 rc
= plpar_hcall(H_PIC
, retbuf
);
180 *pool_idle_time
= retbuf
[0];
181 *num_procs
= retbuf
[1];
188 * Parse out the data returned from h_get_ppp and h_pic
190 static void parse_ppp_data(struct seq_file
*m
)
192 struct hvcall_ppp_data ppp_data
;
193 struct device_node
*root
;
194 const __be32
*perf_level
;
197 rc
= h_get_ppp(&ppp_data
);
201 seq_printf(m
, "partition_entitled_capacity=%lld\n",
202 ppp_data
.entitlement
);
203 seq_printf(m
, "group=%d\n", ppp_data
.group_num
);
204 seq_printf(m
, "system_active_processors=%d\n",
205 ppp_data
.active_system_procs
);
207 /* pool related entries are appropriate for shared configs */
208 if (lppaca_shared_proc(get_lppaca())) {
209 unsigned long pool_idle_time
, pool_procs
;
211 seq_printf(m
, "pool=%d\n", ppp_data
.pool_num
);
213 /* report pool_capacity in percentage */
214 seq_printf(m
, "pool_capacity=%d\n",
215 ppp_data
.active_procs_in_pool
* 100);
217 h_pic(&pool_idle_time
, &pool_procs
);
218 seq_printf(m
, "pool_idle_time=%ld\n", pool_idle_time
);
219 seq_printf(m
, "pool_num_procs=%ld\n", pool_procs
);
222 seq_printf(m
, "unallocated_capacity_weight=%d\n",
223 ppp_data
.unallocated_weight
);
224 seq_printf(m
, "capacity_weight=%d\n", ppp_data
.weight
);
225 seq_printf(m
, "capped=%d\n", ppp_data
.capped
);
226 seq_printf(m
, "unallocated_capacity=%lld\n",
227 ppp_data
.unallocated_entitlement
);
229 /* The last bits of information returned from h_get_ppp are only
230 * valid if the ibm,partition-performance-parameters-level
233 root
= of_find_node_by_path("/");
235 perf_level
= of_get_property(root
,
236 "ibm,partition-performance-parameters-level",
238 if (perf_level
&& (be32_to_cpup(perf_level
) >= 1)) {
240 "physical_procs_allocated_to_virtualization=%d\n",
241 ppp_data
.phys_platform_procs
);
242 seq_printf(m
, "max_proc_capacity_available=%d\n",
243 ppp_data
.max_proc_cap_avail
);
244 seq_printf(m
, "entitled_proc_capacity_available=%d\n",
245 ppp_data
.entitled_proc_cap_avail
);
254 * Parse out data returned from h_get_mpp
256 static void parse_mpp_data(struct seq_file
*m
)
258 struct hvcall_mpp_data mpp_data
;
261 rc
= h_get_mpp(&mpp_data
);
265 seq_printf(m
, "entitled_memory=%ld\n", mpp_data
.entitled_mem
);
267 if (mpp_data
.mapped_mem
!= -1)
268 seq_printf(m
, "mapped_entitled_memory=%ld\n",
269 mpp_data
.mapped_mem
);
271 seq_printf(m
, "entitled_memory_group_number=%d\n", mpp_data
.group_num
);
272 seq_printf(m
, "entitled_memory_pool_number=%d\n", mpp_data
.pool_num
);
274 seq_printf(m
, "entitled_memory_weight=%d\n", mpp_data
.mem_weight
);
275 seq_printf(m
, "unallocated_entitled_memory_weight=%d\n",
276 mpp_data
.unallocated_mem_weight
);
277 seq_printf(m
, "unallocated_io_mapping_entitlement=%ld\n",
278 mpp_data
.unallocated_entitlement
);
280 if (mpp_data
.pool_size
!= -1)
281 seq_printf(m
, "entitled_memory_pool_size=%ld bytes\n",
284 seq_printf(m
, "entitled_memory_loan_request=%ld\n",
285 mpp_data
.loan_request
);
287 seq_printf(m
, "backing_memory=%ld bytes\n", mpp_data
.backing_mem
);
292 * Parse out data returned from h_get_mpp_x
294 static void parse_mpp_x_data(struct seq_file
*m
)
296 struct hvcall_mpp_x_data mpp_x_data
;
298 if (!firmware_has_feature(FW_FEATURE_XCMO
))
300 if (h_get_mpp_x(&mpp_x_data
))
303 seq_printf(m
, "coalesced_bytes=%ld\n", mpp_x_data
.coalesced_bytes
);
305 if (mpp_x_data
.pool_coalesced_bytes
)
306 seq_printf(m
, "pool_coalesced_bytes=%ld\n",
307 mpp_x_data
.pool_coalesced_bytes
);
308 if (mpp_x_data
.pool_purr_cycles
)
309 seq_printf(m
, "coalesce_pool_purr=%ld\n", mpp_x_data
.pool_purr_cycles
);
310 if (mpp_x_data
.pool_spurr_cycles
)
311 seq_printf(m
, "coalesce_pool_spurr=%ld\n", mpp_x_data
.pool_spurr_cycles
);
314 #define SPLPAR_CHARACTERISTICS_TOKEN 20
315 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
318 * parse_system_parameter_string()
319 * Retrieve the potential_processors, max_entitled_capacity and friends
320 * through the get-system-parameter rtas call. Replace keyword strings as
323 static void parse_system_parameter_string(struct seq_file
*m
)
327 unsigned char *local_buffer
= kmalloc(SPLPAR_MAXLENGTH
, GFP_KERNEL
);
329 printk(KERN_ERR
"%s %s kmalloc failure at line %d\n",
330 __FILE__
, __func__
, __LINE__
);
334 spin_lock(&rtas_data_buf_lock
);
335 memset(rtas_data_buf
, 0, SPLPAR_MAXLENGTH
);
336 call_status
= rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
338 SPLPAR_CHARACTERISTICS_TOKEN
,
341 memcpy(local_buffer
, rtas_data_buf
, SPLPAR_MAXLENGTH
);
342 local_buffer
[SPLPAR_MAXLENGTH
- 1] = '\0';
343 spin_unlock(&rtas_data_buf_lock
);
345 if (call_status
!= 0) {
347 "%s %s Error calling get-system-parameter (0x%x)\n",
348 __FILE__
, __func__
, call_status
);
352 char *workbuffer
= kzalloc(SPLPAR_MAXLENGTH
, GFP_KERNEL
);
354 printk(KERN_ERR
"%s %s kmalloc failure at line %d\n",
355 __FILE__
, __func__
, __LINE__
);
360 printk(KERN_INFO
"success calling get-system-parameter\n");
362 splpar_strlen
= local_buffer
[0] * 256 + local_buffer
[1];
363 local_buffer
+= 2; /* step over strlen value */
367 while ((*local_buffer
) && (idx
< splpar_strlen
)) {
368 workbuffer
[w_idx
++] = local_buffer
[idx
++];
369 if ((local_buffer
[idx
] == ',')
370 || (local_buffer
[idx
] == '\0')) {
371 workbuffer
[w_idx
] = '\0';
373 /* avoid the empty string */
374 seq_printf(m
, "%s\n", workbuffer
);
376 memset(workbuffer
, 0, SPLPAR_MAXLENGTH
);
377 idx
++; /* skip the comma */
379 } else if (local_buffer
[idx
] == '=') {
380 /* code here to replace workbuffer contents
381 with different keyword strings */
382 if (0 == strcmp(workbuffer
, "MaxEntCap")) {
384 "partition_max_entitled_capacity");
385 w_idx
= strlen(workbuffer
);
387 if (0 == strcmp(workbuffer
, "MaxPlatProcs")) {
389 "system_potential_processors");
390 w_idx
= strlen(workbuffer
);
395 local_buffer
-= 2; /* back up over strlen value */
400 /* Return the number of processors in the system.
401 * This function reads through the device tree and counts
402 * the virtual processors, this does not include threads.
404 static int lparcfg_count_active_processors(void)
406 struct device_node
*cpus_dn
;
409 for_each_node_by_type(cpus_dn
, "cpu") {
411 printk(KERN_ERR
"cpus_dn %p\n", cpus_dn
);
418 static void pseries_cmo_data(struct seq_file
*m
)
421 unsigned long cmo_faults
= 0;
422 unsigned long cmo_fault_time
= 0;
424 seq_printf(m
, "cmo_enabled=%d\n", firmware_has_feature(FW_FEATURE_CMO
));
426 if (!firmware_has_feature(FW_FEATURE_CMO
))
429 for_each_possible_cpu(cpu
) {
430 cmo_faults
+= be64_to_cpu(lppaca_of(cpu
).cmo_faults
);
431 cmo_fault_time
+= be64_to_cpu(lppaca_of(cpu
).cmo_fault_time
);
434 seq_printf(m
, "cmo_faults=%lu\n", cmo_faults
);
435 seq_printf(m
, "cmo_fault_time_usec=%lu\n",
436 cmo_fault_time
/ tb_ticks_per_usec
);
437 seq_printf(m
, "cmo_primary_psp=%d\n", cmo_get_primary_psp());
438 seq_printf(m
, "cmo_secondary_psp=%d\n", cmo_get_secondary_psp());
439 seq_printf(m
, "cmo_page_size=%lu\n", cmo_get_page_size());
442 static void splpar_dispatch_data(struct seq_file
*m
)
445 unsigned long dispatches
= 0;
446 unsigned long dispatch_dispersions
= 0;
448 for_each_possible_cpu(cpu
) {
449 dispatches
+= be32_to_cpu(lppaca_of(cpu
).yield_count
);
450 dispatch_dispersions
+=
451 be32_to_cpu(lppaca_of(cpu
).dispersion_count
);
454 seq_printf(m
, "dispatches=%lu\n", dispatches
);
455 seq_printf(m
, "dispatch_dispersions=%lu\n", dispatch_dispersions
);
458 static void parse_em_data(struct seq_file
*m
)
460 unsigned long retbuf
[PLPAR_HCALL_BUFSIZE
];
462 if (firmware_has_feature(FW_FEATURE_LPAR
) &&
463 plpar_hcall(H_GET_EM_PARMS
, retbuf
) == H_SUCCESS
)
464 seq_printf(m
, "power_mode_data=%016lx\n", retbuf
[0]);
467 static void maxmem_data(struct seq_file
*m
)
469 unsigned long maxmem
= 0;
471 maxmem
+= (unsigned long)drmem_info
->n_lmbs
* drmem_info
->lmb_size
;
472 maxmem
+= hugetlb_total_pages() * PAGE_SIZE
;
474 seq_printf(m
, "MaxMem=%lu\n", maxmem
);
477 static int pseries_lparcfg_data(struct seq_file
*m
, void *v
)
479 int partition_potential_processors
;
480 int partition_active_processors
;
481 struct device_node
*rtas_node
;
482 const __be32
*lrdrp
= NULL
;
484 rtas_node
= of_find_node_by_path("/rtas");
486 lrdrp
= of_get_property(rtas_node
, "ibm,lrdr-capacity", NULL
);
489 partition_potential_processors
= vdso_data
->processorCount
;
491 partition_potential_processors
= be32_to_cpup(lrdrp
+ 4);
493 of_node_put(rtas_node
);
495 partition_active_processors
= lparcfg_count_active_processors();
497 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
498 /* this call handles the ibm,get-system-parameter contents */
499 parse_system_parameter_string(m
);
504 splpar_dispatch_data(m
);
506 seq_printf(m
, "purr=%ld\n", get_purr());
507 seq_printf(m
, "tbr=%ld\n", mftb());
508 } else { /* non SPLPAR case */
510 seq_printf(m
, "system_active_processors=%d\n",
511 partition_potential_processors
);
513 seq_printf(m
, "system_potential_processors=%d\n",
514 partition_potential_processors
);
516 seq_printf(m
, "partition_max_entitled_capacity=%d\n",
517 partition_potential_processors
* 100);
519 seq_printf(m
, "partition_entitled_capacity=%d\n",
520 partition_active_processors
* 100);
525 seq_printf(m
, "partition_active_processors=%d\n",
526 partition_active_processors
);
528 seq_printf(m
, "partition_potential_processors=%d\n",
529 partition_potential_processors
);
531 seq_printf(m
, "shared_processor_mode=%d\n",
532 lppaca_shared_proc(get_lppaca()));
534 #ifdef CONFIG_PPC_BOOK3S_64
535 seq_printf(m
, "slb_size=%d\n", mmu_slb_size
);
543 static ssize_t
update_ppp(u64
*entitlement
, u8
*weight
)
545 struct hvcall_ppp_data ppp_data
;
550 /* Get our current parameters */
551 retval
= h_get_ppp(&ppp_data
);
556 new_weight
= ppp_data
.weight
;
557 new_entitled
= *entitlement
;
559 new_weight
= *weight
;
560 new_entitled
= ppp_data
.entitlement
;
564 pr_debug("%s: current_entitled = %llu, current_weight = %u\n",
565 __func__
, ppp_data
.entitlement
, ppp_data
.weight
);
567 pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
568 __func__
, new_entitled
, new_weight
);
570 retval
= plpar_hcall_norets(H_SET_PPP
, new_entitled
, new_weight
);
577 * Update the memory entitlement and weight for the partition. Caller must
578 * specify either a new entitlement or weight, not both, to be updated
579 * since the h_set_mpp call takes both entitlement and weight as parameters.
581 static ssize_t
update_mpp(u64
*entitlement
, u8
*weight
)
583 struct hvcall_mpp_data mpp_data
;
589 /* Check with vio to ensure the new memory entitlement
592 rc
= vio_cmo_entitlement_update(*entitlement
);
597 rc
= h_get_mpp(&mpp_data
);
602 new_weight
= mpp_data
.mem_weight
;
603 new_entitled
= *entitlement
;
605 new_weight
= *weight
;
606 new_entitled
= mpp_data
.entitled_mem
;
610 pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
611 __func__
, mpp_data
.entitled_mem
, mpp_data
.mem_weight
);
613 pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
614 __func__
, new_entitled
, new_weight
);
616 rc
= plpar_hcall_norets(H_SET_MPP
, new_entitled
, new_weight
);
621 * Interface for changing system parameters (variable capacity weight
622 * and entitled capacity). Format of input is "param_name=value";
623 * anything after value is ignored. Valid parameters at this time are
624 * "partition_entitled_capacity" and "capacity_weight". We use
625 * H_SET_PPP to alter parameters.
627 * This function should be invoked only on systems with
630 static ssize_t
lparcfg_write(struct file
*file
, const char __user
* buf
,
631 size_t count
, loff_t
* off
)
635 u64 new_entitled
, *new_entitled_ptr
= &new_entitled
;
636 u8 new_weight
, *new_weight_ptr
= &new_weight
;
639 if (!firmware_has_feature(FW_FEATURE_SPLPAR
))
642 if (count
> sizeof(kbuf
))
645 if (copy_from_user(kbuf
, buf
, count
))
648 kbuf
[count
- 1] = '\0';
649 tmp
= strchr(kbuf
, '=');
655 if (!strcmp(kbuf
, "partition_entitled_capacity")) {
657 *new_entitled_ptr
= (u64
) simple_strtoul(tmp
, &endp
, 10);
661 retval
= update_ppp(new_entitled_ptr
, NULL
);
662 } else if (!strcmp(kbuf
, "capacity_weight")) {
664 *new_weight_ptr
= (u8
) simple_strtoul(tmp
, &endp
, 10);
668 retval
= update_ppp(NULL
, new_weight_ptr
);
669 } else if (!strcmp(kbuf
, "entitled_memory")) {
671 *new_entitled_ptr
= (u64
) simple_strtoul(tmp
, &endp
, 10);
675 retval
= update_mpp(new_entitled_ptr
, NULL
);
676 } else if (!strcmp(kbuf
, "entitled_memory_weight")) {
678 *new_weight_ptr
= (u8
) simple_strtoul(tmp
, &endp
, 10);
682 retval
= update_mpp(NULL
, new_weight_ptr
);
686 if (retval
== H_SUCCESS
|| retval
== H_CONSTRAINED
) {
688 } else if (retval
== H_BUSY
) {
690 } else if (retval
== H_HARDWARE
) {
692 } else if (retval
== H_PARAMETER
) {
699 static int lparcfg_data(struct seq_file
*m
, void *v
)
701 struct device_node
*rootdn
;
702 const char *model
= "";
703 const char *system_id
= "";
705 const __be32
*lp_index_ptr
;
706 unsigned int lp_index
= 0;
708 seq_printf(m
, "%s %s\n", MODULE_NAME
, MODULE_VERS
);
710 rootdn
= of_find_node_by_path("/");
712 tmp
= of_get_property(rootdn
, "model", NULL
);
715 tmp
= of_get_property(rootdn
, "system-id", NULL
);
718 lp_index_ptr
= of_get_property(rootdn
, "ibm,partition-no",
721 lp_index
= be32_to_cpup(lp_index_ptr
);
724 seq_printf(m
, "serial_number=%s\n", system_id
);
725 seq_printf(m
, "system_type=%s\n", model
);
726 seq_printf(m
, "partition_id=%d\n", (int)lp_index
);
728 return pseries_lparcfg_data(m
, v
);
731 static int lparcfg_open(struct inode
*inode
, struct file
*file
)
733 return single_open(file
, lparcfg_data
, NULL
);
736 static const struct proc_ops lparcfg_proc_ops
= {
737 .proc_read
= seq_read
,
738 .proc_write
= lparcfg_write
,
739 .proc_open
= lparcfg_open
,
740 .proc_release
= single_release
,
741 .proc_lseek
= seq_lseek
,
744 static int __init
lparcfg_init(void)
748 /* Allow writing if we have FW_FEATURE_SPLPAR */
749 if (firmware_has_feature(FW_FEATURE_SPLPAR
))
752 if (!proc_create("powerpc/lparcfg", mode
, NULL
, &lparcfg_proc_ops
)) {
753 printk(KERN_ERR
"Failed to create powerpc/lparcfg\n");
758 machine_device_initcall(pseries
, lparcfg_init
);