xref: /linux/arch/powerpc/platforms/pseries/lparcfg.c (revision c8f80f95da32618d49c2a068ee561b85655b761d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PowerPC64 LPAR Configuration Information Driver
4  *
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.
12  *
13  * This driver creates a proc file at /proc/ppc64/lparcfg which contains
14  * keyword - value pairs that specify the configuration of the partition.
15  */
16 
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 <asm/papr-sysparm.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27 #include <linux/hugetlb.h>
28 #include <asm/lppaca.h>
29 #include <asm/hvcall.h>
30 #include <asm/firmware.h>
31 #include <asm/rtas.h>
32 #include <asm/time.h>
33 #include <asm/vio.h>
34 #include <asm/mmu.h>
35 #include <asm/machdep.h>
36 #include <asm/drmem.h>
37 
38 #include "pseries.h"
39 #include "vas.h"	/* pseries_vas_dlpar_cpu() */
40 
41 /*
42  * This isn't a module but we expose that to userspace
43  * via /proc so leave the definitions here
44  */
45 #define MODULE_VERS "1.9"
46 #define MODULE_NAME "lparcfg"
47 
48 /* #define LPARCFG_DEBUG */
49 
50 /*
51  * Track sum of all purrs across all processors. This is used to further
52  * calculate usage values by different applications
53  */
54 static void cpu_get_purr(void *arg)
55 {
56 	atomic64_t *sum = arg;
57 
58 	atomic64_add(mfspr(SPRN_PURR), sum);
59 }
60 
61 static unsigned long get_purr(void)
62 {
63 	atomic64_t purr = ATOMIC64_INIT(0);
64 
65 	on_each_cpu(cpu_get_purr, &purr, 1);
66 
67 	return atomic64_read(&purr);
68 }
69 
70 /*
71  * Methods used to fetch LPAR data when running on a pSeries platform.
72  */
73 
74 struct hvcall_ppp_data {
75 	u64	entitlement;
76 	u64	unallocated_entitlement;
77 	u16	group_num;
78 	u16	pool_num;
79 	u8	capped;
80 	u8	weight;
81 	u8	unallocated_weight;
82 	u8      resource_group_index;
83 	u16     active_procs_in_resource_group;
84 	u16	active_procs_in_pool;
85 	u16	active_system_procs;
86 	u16	phys_platform_procs;
87 	u32	max_proc_cap_avail;
88 	u32	entitled_proc_cap_avail;
89 };
90 
91 /*
92  * H_GET_PPP hcall returns info in 5 parms.
93  *  entitled_capacity,unallocated_capacity,
94  *  aggregation, resource_capability).
95  *
96  *  R4 = Entitled Processor Capacity Percentage.
97  *  R5 = Unallocated Processor Capacity Percentage.
98  *  R6 (AABBCCDDEEFFGGHH).
99  *      XXXX - reserved (0)
100  *          XXXX - Active Cores in Resource Group
101  *              XXXX - Group Number
102  *                  XXXX - Pool Number.
103  *  R7 (IIJJKKLLMMNNOOPP).
104  *      XX - Resource group Number
105  *        XX - bit 0-6 reserved (0).   bit 7 is Capped indicator.
106  *          XX - variable processor Capacity Weight
107  *            XX - Unallocated Variable Processor Capacity Weight.
108  *              XXXX - Active processors in Physical Processor Pool.
109  *                  XXXX  - Processors active on platform.
110  *  R8 (QQQQRRRRRRSSSSSS). if ibm,partition-performance-parameters-level >= 1
111  *	XXXX - Physical platform procs allocated to virtualization.
112  *	    XXXXXX - Max procs capacity % available to the partitions pool.
113  *	          XXXXXX - Entitled procs capacity % available to the
114  *			   partitions pool.
115  */
116 static unsigned int h_get_ppp(struct hvcall_ppp_data *ppp_data)
117 {
118 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
119 	long rc;
120 
121 	rc = plpar_hcall9(H_GET_PPP, retbuf);
122 
123 	ppp_data->entitlement = retbuf[0];
124 	ppp_data->unallocated_entitlement = retbuf[1];
125 
126 	ppp_data->active_procs_in_resource_group = (retbuf[2] >> 4 * 8) & 0xffff;
127 	ppp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
128 	ppp_data->pool_num = retbuf[2] & 0xffff;
129 
130 	ppp_data->resource_group_index = (retbuf[3] >> 7 *  8) & 0xff;
131 	ppp_data->capped = (retbuf[3] >> 6 * 8) & 0x01;
132 	ppp_data->weight = (retbuf[3] >> 5 * 8) & 0xff;
133 	ppp_data->unallocated_weight = (retbuf[3] >> 4 * 8) & 0xff;
134 	ppp_data->active_procs_in_pool = (retbuf[3] >> 2 * 8) & 0xffff;
135 	ppp_data->active_system_procs = retbuf[3] & 0xffff;
136 
137 	ppp_data->phys_platform_procs = retbuf[4] >> 6 * 8;
138 	ppp_data->max_proc_cap_avail = (retbuf[4] >> 3 * 8) & 0xffffff;
139 	ppp_data->entitled_proc_cap_avail = retbuf[4] & 0xffffff;
140 
141 	return rc;
142 }
143 
144 static void show_gpci_data(struct seq_file *m)
145 {
146 	struct hv_gpci_request_buffer *buf;
147 	unsigned int affinity_score;
148 	long ret;
149 
150 	buf = kmalloc_obj(*buf);
151 	if (buf == NULL)
152 		return;
153 
154 	/*
155 	 * Show the local LPAR's affinity score.
156 	 *
157 	 * 0xB1 selects the Affinity_Domain_Info_By_Partition subcall.
158 	 * The score is at byte 0xB in the output buffer.
159 	 */
160 	memset(&buf->params, 0, sizeof(buf->params));
161 	buf->params.counter_request = cpu_to_be32(0xB1);
162 	buf->params.starting_index = cpu_to_be32(-1);	/* local LPAR */
163 	buf->params.counter_info_version_in = 0x5;	/* v5+ for score */
164 	ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO, virt_to_phys(buf),
165 				 sizeof(*buf));
166 	if (ret != H_SUCCESS) {
167 		pr_debug("hcall failed: H_GET_PERF_COUNTER_INFO: %ld, %x\n",
168 			 ret, be32_to_cpu(buf->params.detail_rc));
169 		goto out;
170 	}
171 	affinity_score = buf->bytes[0xB];
172 	seq_printf(m, "partition_affinity_score=%u\n", affinity_score);
173 out:
174 	kfree(buf);
175 }
176 
177 static long h_pic(unsigned long *pool_idle_time,
178 		  unsigned long *num_procs)
179 {
180 	long rc;
181 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = {0};
182 
183 	rc = plpar_hcall(H_PIC, retbuf);
184 
185 	if (pool_idle_time)
186 		*pool_idle_time = retbuf[0];
187 	if (num_procs)
188 		*num_procs = retbuf[1];
189 
190 	return rc;
191 }
192 
193 unsigned long boot_pool_idle_time;
194 
195 /*
196  * parse_ppp_data
197  * Parse out the data returned from h_get_ppp and h_pic
198  */
199 static void parse_ppp_data(struct seq_file *m)
200 {
201 	struct hvcall_ppp_data ppp_data;
202 	struct device_node *root;
203 	const __be32 *perf_level;
204 	long rc;
205 
206 	rc = h_get_ppp(&ppp_data);
207 	if (rc)
208 		return;
209 
210 	seq_printf(m, "partition_entitled_capacity=%lld\n",
211 	           ppp_data.entitlement);
212 	seq_printf(m, "group=%d\n", ppp_data.group_num);
213 	seq_printf(m, "system_active_processors=%d\n",
214 	           ppp_data.active_system_procs);
215 
216 	/* pool related entries are appropriate for shared configs */
217 	if (lppaca_shared_proc()) {
218 		unsigned long pool_idle_time, pool_procs;
219 
220 		seq_printf(m, "pool=%d\n", ppp_data.pool_num);
221 
222 		/* report pool_capacity in percentage */
223 		seq_printf(m, "pool_capacity=%d\n",
224 			   ppp_data.active_procs_in_pool * 100);
225 
226 		/* In case h_pic call is not successful, this would result in
227 		 * APP values being wrong in tools like lparstat.
228 		 */
229 
230 		if (h_pic(&pool_idle_time, &pool_procs) == H_SUCCESS) {
231 			seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
232 			seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
233 			seq_printf(m, "boot_pool_idle_time=%ld\n", boot_pool_idle_time);
234 		}
235 	}
236 
237 	seq_printf(m, "unallocated_capacity_weight=%d\n",
238 		   ppp_data.unallocated_weight);
239 	seq_printf(m, "capacity_weight=%d\n", ppp_data.weight);
240 	seq_printf(m, "capped=%d\n", ppp_data.capped);
241 	seq_printf(m, "unallocated_capacity=%lld\n",
242 		   ppp_data.unallocated_entitlement);
243 
244 	if (ppp_data.active_procs_in_resource_group)  {
245 		seq_printf(m, "resource_group_number=%d\n",
246 				ppp_data.resource_group_index);
247 		seq_printf(m, "resource_group_active_processors=%d\n",
248 				ppp_data.active_procs_in_resource_group);
249 	}
250 
251 	/* The last bits of information returned from h_get_ppp are only
252 	 * valid if the ibm,partition-performance-parameters-level
253 	 * property is >= 1.
254 	 */
255 	root = of_find_node_by_path("/");
256 	if (root) {
257 		perf_level = of_get_property(root,
258 				"ibm,partition-performance-parameters-level",
259 					     NULL);
260 		if (perf_level && (be32_to_cpup(perf_level) >= 1)) {
261 			seq_printf(m,
262 			    "physical_procs_allocated_to_virtualization=%d\n",
263 				   ppp_data.phys_platform_procs);
264 			seq_printf(m, "max_proc_capacity_available=%d\n",
265 				   ppp_data.max_proc_cap_avail);
266 			seq_printf(m, "entitled_proc_capacity_available=%d\n",
267 				   ppp_data.entitled_proc_cap_avail);
268 		}
269 
270 		of_node_put(root);
271 	}
272 }
273 
274 /**
275  * parse_mpp_data
276  * Parse out data returned from h_get_mpp
277  */
278 static void parse_mpp_data(struct seq_file *m)
279 {
280 	struct hvcall_mpp_data mpp_data;
281 	int rc;
282 
283 	rc = h_get_mpp(&mpp_data);
284 	if (rc)
285 		return;
286 
287 	seq_printf(m, "entitled_memory=%ld\n", mpp_data.entitled_mem);
288 
289 	if (mpp_data.mapped_mem != -1)
290 		seq_printf(m, "mapped_entitled_memory=%ld\n",
291 		           mpp_data.mapped_mem);
292 
293 	seq_printf(m, "entitled_memory_group_number=%d\n", mpp_data.group_num);
294 	seq_printf(m, "entitled_memory_pool_number=%d\n", mpp_data.pool_num);
295 
296 	seq_printf(m, "entitled_memory_weight=%d\n", mpp_data.mem_weight);
297 	seq_printf(m, "unallocated_entitled_memory_weight=%d\n",
298 	           mpp_data.unallocated_mem_weight);
299 	seq_printf(m, "unallocated_io_mapping_entitlement=%ld\n",
300 	           mpp_data.unallocated_entitlement);
301 
302 	if (mpp_data.pool_size != -1)
303 		seq_printf(m, "entitled_memory_pool_size=%ld bytes\n",
304 		           mpp_data.pool_size);
305 
306 	seq_printf(m, "entitled_memory_loan_request=%ld\n",
307 	           mpp_data.loan_request);
308 
309 	seq_printf(m, "backing_memory=%ld bytes\n", mpp_data.backing_mem);
310 }
311 
312 /**
313  * parse_mpp_x_data
314  * Parse out data returned from h_get_mpp_x
315  */
316 static void parse_mpp_x_data(struct seq_file *m)
317 {
318 	struct hvcall_mpp_x_data mpp_x_data;
319 
320 	if (!firmware_has_feature(FW_FEATURE_XCMO))
321 		return;
322 	if (h_get_mpp_x(&mpp_x_data))
323 		return;
324 
325 	seq_printf(m, "coalesced_bytes=%ld\n", mpp_x_data.coalesced_bytes);
326 
327 	if (mpp_x_data.pool_coalesced_bytes)
328 		seq_printf(m, "pool_coalesced_bytes=%ld\n",
329 			   mpp_x_data.pool_coalesced_bytes);
330 	if (mpp_x_data.pool_purr_cycles)
331 		seq_printf(m, "coalesce_pool_purr=%ld\n", mpp_x_data.pool_purr_cycles);
332 	if (mpp_x_data.pool_spurr_cycles)
333 		seq_printf(m, "coalesce_pool_spurr=%ld\n", mpp_x_data.pool_spurr_cycles);
334 }
335 
336 /*
337  * Read the lpar name using the RTAS ibm,get-system-parameter call.
338  *
339  * The name read through this call is updated if changes are made by the end
340  * user on the hypervisor side.
341  *
342  * Some hypervisor (like Qemu) may not provide this value. In that case, a non
343  * null value is returned.
344  */
345 static int read_rtas_lpar_name(struct seq_file *m)
346 {
347 	struct papr_sysparm_buf *buf;
348 	int err;
349 
350 	buf = papr_sysparm_buf_alloc();
351 	if (!buf)
352 		return -ENOMEM;
353 
354 	err = papr_sysparm_get(PAPR_SYSPARM_LPAR_NAME, buf);
355 	if (!err)
356 		seq_printf(m, "partition_name=%s\n", buf->val);
357 
358 	papr_sysparm_buf_free(buf);
359 	return err;
360 }
361 
362 /*
363  * Read the LPAR name from the Device Tree.
364  *
365  * The value read in the DT is not updated if the end-user is touching the LPAR
366  * name on the hypervisor side.
367  */
368 static int read_dt_lpar_name(struct seq_file *m)
369 {
370 	struct device_node *root = of_find_node_by_path("/");
371 	const char *name;
372 	int ret;
373 
374 	ret = of_property_read_string(root, "ibm,partition-name", &name);
375 	of_node_put(root);
376 	if (ret)
377 		return -ENOENT;
378 
379 	seq_printf(m, "partition_name=%s\n", name);
380 	return 0;
381 }
382 
383 static void read_lpar_name(struct seq_file *m)
384 {
385 	if (read_rtas_lpar_name(m))
386 		read_dt_lpar_name(m);
387 }
388 
389 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
390 
391 /*
392  * parse_system_parameter_string()
393  * Retrieve the potential_processors, max_entitled_capacity and friends
394  * through the get-system-parameter rtas call.  Replace keyword strings as
395  * necessary.
396  */
397 static void parse_system_parameter_string(struct seq_file *m)
398 {
399 	struct papr_sysparm_buf *buf;
400 
401 	buf = papr_sysparm_buf_alloc();
402 	if (!buf)
403 		return;
404 
405 	if (papr_sysparm_get(PAPR_SYSPARM_SHARED_PROC_LPAR_ATTRS, buf)) {
406 		goto out_free;
407 	} else {
408 		const char *local_buffer;
409 		int splpar_strlen;
410 		int idx, w_idx;
411 		char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
412 
413 		if (!workbuffer)
414 			goto out_free;
415 
416 		splpar_strlen = be16_to_cpu(buf->len);
417 		local_buffer = buf->val;
418 
419 		w_idx = 0;
420 		idx = 0;
421 		while ((*local_buffer) && (idx < splpar_strlen)) {
422 			workbuffer[w_idx++] = local_buffer[idx++];
423 			if ((local_buffer[idx] == ',')
424 			    || (local_buffer[idx] == '\0')) {
425 				workbuffer[w_idx] = '\0';
426 				if (w_idx) {
427 					/* avoid the empty string */
428 					seq_printf(m, "%s\n", workbuffer);
429 				}
430 				memset(workbuffer, 0, SPLPAR_MAXLENGTH);
431 				idx++;	/* skip the comma */
432 				w_idx = 0;
433 			} else if (local_buffer[idx] == '=') {
434 				/* code here to replace workbuffer contents
435 				 * with different keyword strings. Truncation
436 				 * by strscpy is deliberately ignored because
437 				 * SPLPAR_MAXLENGTH >= maximum string size.
438 				 */
439 				if (!strcmp(workbuffer, "MaxEntCap"))
440 					w_idx = strscpy(workbuffer,
441 							"partition_max_entitled_capacity",
442 							SPLPAR_MAXLENGTH);
443 				if (!strcmp(workbuffer, "MaxPlatProcs"))
444 					w_idx = strscpy(workbuffer,
445 							"system_potential_processors",
446 							SPLPAR_MAXLENGTH);
447 			}
448 		}
449 		kfree(workbuffer);
450 		local_buffer -= 2;	/* back up over strlen value */
451 	}
452 out_free:
453 	papr_sysparm_buf_free(buf);
454 }
455 
456 /* Return the number of processors in the system.
457  * This function reads through the device tree and counts
458  * the virtual processors, this does not include threads.
459  */
460 static int lparcfg_count_active_processors(void)
461 {
462 	struct device_node *cpus_dn;
463 	int count = 0;
464 
465 	for_each_node_by_type(cpus_dn, "cpu") {
466 #ifdef LPARCFG_DEBUG
467 		printk(KERN_ERR "cpus_dn %p\n", cpus_dn);
468 #endif
469 		count++;
470 	}
471 	return count;
472 }
473 
474 static void pseries_cmo_data(struct seq_file *m)
475 {
476 	int cpu;
477 	unsigned long cmo_faults = 0;
478 	unsigned long cmo_fault_time = 0;
479 
480 	seq_printf(m, "cmo_enabled=%d\n", firmware_has_feature(FW_FEATURE_CMO));
481 
482 	if (!firmware_has_feature(FW_FEATURE_CMO))
483 		return;
484 
485 	for_each_possible_cpu(cpu) {
486 		cmo_faults += be64_to_cpu(lppaca_of(cpu).cmo_faults);
487 		cmo_fault_time += be64_to_cpu(lppaca_of(cpu).cmo_fault_time);
488 	}
489 
490 	seq_printf(m, "cmo_faults=%lu\n", cmo_faults);
491 	seq_printf(m, "cmo_fault_time_usec=%lu\n",
492 		   cmo_fault_time / tb_ticks_per_usec);
493 	seq_printf(m, "cmo_primary_psp=%d\n", cmo_get_primary_psp());
494 	seq_printf(m, "cmo_secondary_psp=%d\n", cmo_get_secondary_psp());
495 	seq_printf(m, "cmo_page_size=%lu\n", cmo_get_page_size());
496 }
497 
498 static void splpar_dispatch_data(struct seq_file *m)
499 {
500 	int cpu;
501 	unsigned long dispatches = 0;
502 	unsigned long dispatch_dispersions = 0;
503 
504 	for_each_possible_cpu(cpu) {
505 		dispatches += be32_to_cpu(lppaca_of(cpu).yield_count);
506 		dispatch_dispersions +=
507 			be32_to_cpu(lppaca_of(cpu).dispersion_count);
508 	}
509 
510 	seq_printf(m, "dispatches=%lu\n", dispatches);
511 	seq_printf(m, "dispatch_dispersions=%lu\n", dispatch_dispersions);
512 }
513 
514 static void parse_em_data(struct seq_file *m)
515 {
516 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
517 
518 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
519 	    plpar_hcall(H_GET_EM_PARMS, retbuf) == H_SUCCESS)
520 		seq_printf(m, "power_mode_data=%016lx\n", retbuf[0]);
521 }
522 
523 static void maxmem_data(struct seq_file *m)
524 {
525 	unsigned long maxmem = 0;
526 
527 	maxmem += (unsigned long)drmem_info->n_lmbs * drmem_info->lmb_size;
528 	maxmem += hugetlb_total_pages() * PAGE_SIZE;
529 
530 	seq_printf(m, "MaxMem=%lu\n", maxmem);
531 }
532 
533 static int pseries_lparcfg_data(struct seq_file *m, void *v)
534 {
535 	int partition_potential_processors;
536 	int partition_active_processors;
537 	struct device_node *rtas_node;
538 	const __be32 *lrdrp = NULL;
539 
540 	rtas_node = of_find_node_by_path("/rtas");
541 	if (rtas_node)
542 		lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
543 
544 	if (lrdrp == NULL) {
545 		partition_potential_processors = num_possible_cpus();
546 	} else {
547 		partition_potential_processors = be32_to_cpup(lrdrp + 4);
548 	}
549 	of_node_put(rtas_node);
550 
551 	partition_active_processors = lparcfg_count_active_processors();
552 
553 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
554 		/* this call handles the ibm,get-system-parameter contents */
555 		read_lpar_name(m);
556 		parse_system_parameter_string(m);
557 		parse_ppp_data(m);
558 		parse_mpp_data(m);
559 		parse_mpp_x_data(m);
560 		pseries_cmo_data(m);
561 		splpar_dispatch_data(m);
562 
563 		seq_printf(m, "purr=%ld\n", get_purr());
564 		seq_printf(m, "tbr=%ld\n", mftb());
565 	} else {		/* non SPLPAR case */
566 
567 		seq_printf(m, "system_active_processors=%d\n",
568 			   partition_active_processors);
569 
570 		seq_printf(m, "system_potential_processors=%d\n",
571 			   partition_potential_processors);
572 
573 		seq_printf(m, "partition_max_entitled_capacity=%d\n",
574 			   partition_potential_processors * 100);
575 
576 		seq_printf(m, "partition_entitled_capacity=%d\n",
577 			   partition_active_processors * 100);
578 	}
579 
580 	show_gpci_data(m);
581 
582 	seq_printf(m, "partition_active_processors=%d\n",
583 		   partition_active_processors);
584 
585 	seq_printf(m, "partition_potential_processors=%d\n",
586 		   partition_potential_processors);
587 
588 	seq_printf(m, "shared_processor_mode=%d\n",
589 		   lppaca_shared_proc());
590 
591 #ifdef CONFIG_PPC_64S_HASH_MMU
592 	if (!radix_enabled())
593 		seq_printf(m, "slb_size=%d\n", mmu_slb_size);
594 #endif
595 	parse_em_data(m);
596 	maxmem_data(m);
597 
598 	seq_printf(m, "security_flavor=%u\n", pseries_security_flavor);
599 
600 	return 0;
601 }
602 
603 static ssize_t update_ppp(u64 *entitlement, u8 *weight)
604 {
605 	struct hvcall_ppp_data ppp_data;
606 	u8 new_weight;
607 	u64 new_entitled;
608 	ssize_t retval;
609 
610 	/* Get our current parameters */
611 	retval = h_get_ppp(&ppp_data);
612 	if (retval)
613 		return retval;
614 
615 	if (entitlement) {
616 		new_weight = ppp_data.weight;
617 		new_entitled = *entitlement;
618 	} else if (weight) {
619 		new_weight = *weight;
620 		new_entitled = ppp_data.entitlement;
621 	} else
622 		return -EINVAL;
623 
624 	pr_debug("%s: current_entitled = %llu, current_weight = %u\n",
625 		 __func__, ppp_data.entitlement, ppp_data.weight);
626 
627 	pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
628 		 __func__, new_entitled, new_weight);
629 
630 	retval = plpar_hcall_norets(H_SET_PPP, new_entitled, new_weight);
631 	return retval;
632 }
633 
634 /**
635  * update_mpp
636  *
637  * Update the memory entitlement and weight for the partition.  Caller must
638  * specify either a new entitlement or weight, not both, to be updated
639  * since the h_set_mpp call takes both entitlement and weight as parameters.
640  */
641 static ssize_t update_mpp(u64 *entitlement, u8 *weight)
642 {
643 	struct hvcall_mpp_data mpp_data;
644 	u64 new_entitled;
645 	u8 new_weight;
646 	ssize_t rc;
647 
648 	if (entitlement) {
649 		/* Check with vio to ensure the new memory entitlement
650 		 * can be handled.
651 		 */
652 		rc = vio_cmo_entitlement_update(*entitlement);
653 		if (rc)
654 			return rc;
655 	}
656 
657 	rc = h_get_mpp(&mpp_data);
658 	if (rc)
659 		return rc;
660 
661 	if (entitlement) {
662 		new_weight = mpp_data.mem_weight;
663 		new_entitled = *entitlement;
664 	} else if (weight) {
665 		new_weight = *weight;
666 		new_entitled = mpp_data.entitled_mem;
667 	} else
668 		return -EINVAL;
669 
670 	pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
671 	         __func__, mpp_data.entitled_mem, mpp_data.mem_weight);
672 
673 	pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
674 		 __func__, new_entitled, new_weight);
675 
676 	rc = plpar_hcall_norets(H_SET_MPP, new_entitled, new_weight);
677 	return rc;
678 }
679 
680 /*
681  * Interface for changing system parameters (variable capacity weight
682  * and entitled capacity).  Format of input is "param_name=value";
683  * anything after value is ignored.  Valid parameters at this time are
684  * "partition_entitled_capacity" and "capacity_weight".  We use
685  * H_SET_PPP to alter parameters.
686  *
687  * This function should be invoked only on systems with
688  * FW_FEATURE_SPLPAR.
689  */
690 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
691 			     size_t count, loff_t * off)
692 {
693 	char kbuf[64];
694 	char *tmp;
695 	u64 new_entitled, *new_entitled_ptr = &new_entitled;
696 	u8 new_weight, *new_weight_ptr = &new_weight;
697 	ssize_t retval;
698 
699 	if (!firmware_has_feature(FW_FEATURE_SPLPAR))
700 		return -EINVAL;
701 
702 	if (count > sizeof(kbuf))
703 		return -EINVAL;
704 
705 	if (copy_from_user(kbuf, buf, count))
706 		return -EFAULT;
707 
708 	kbuf[count - 1] = '\0';
709 	tmp = strchr(kbuf, '=');
710 	if (!tmp)
711 		return -EINVAL;
712 
713 	*tmp++ = '\0';
714 
715 	if (!strcmp(kbuf, "partition_entitled_capacity")) {
716 		char *endp;
717 		*new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
718 		if (endp == tmp)
719 			return -EINVAL;
720 
721 		retval = update_ppp(new_entitled_ptr, NULL);
722 
723 		if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
724 			/*
725 			 * The hypervisor assigns VAS resources based
726 			 * on entitled capacity for shared mode.
727 			 * Reconfig VAS windows based on DLPAR CPU events.
728 			 */
729 			if (pseries_vas_dlpar_cpu() != 0)
730 				retval = H_HARDWARE;
731 		}
732 	} else if (!strcmp(kbuf, "capacity_weight")) {
733 		char *endp;
734 		*new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
735 		if (endp == tmp)
736 			return -EINVAL;
737 
738 		retval = update_ppp(NULL, new_weight_ptr);
739 	} else if (!strcmp(kbuf, "entitled_memory")) {
740 		char *endp;
741 		*new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
742 		if (endp == tmp)
743 			return -EINVAL;
744 
745 		retval = update_mpp(new_entitled_ptr, NULL);
746 	} else if (!strcmp(kbuf, "entitled_memory_weight")) {
747 		char *endp;
748 		*new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
749 		if (endp == tmp)
750 			return -EINVAL;
751 
752 		retval = update_mpp(NULL, new_weight_ptr);
753 	} else
754 		return -EINVAL;
755 
756 	if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
757 		retval = count;
758 	} else if (retval == H_BUSY) {
759 		retval = -EBUSY;
760 	} else if (retval == H_HARDWARE) {
761 		retval = -EIO;
762 	} else if (retval == H_PARAMETER) {
763 		retval = -EINVAL;
764 	}
765 
766 	return retval;
767 }
768 
769 static int lparcfg_data(struct seq_file *m, void *v)
770 {
771 	struct device_node *rootdn;
772 	const char *model = "";
773 	const char *system_id = "";
774 	const char *tmp;
775 	const __be32 *lp_index_ptr;
776 	unsigned int lp_index = 0;
777 
778 	seq_printf(m, "%s %s\n", MODULE_NAME, MODULE_VERS);
779 
780 	rootdn = of_find_node_by_path("/");
781 	if (rootdn) {
782 		tmp = of_get_property(rootdn, "model", NULL);
783 		if (tmp)
784 			model = tmp;
785 		tmp = of_get_property(rootdn, "system-id", NULL);
786 		if (tmp)
787 			system_id = tmp;
788 		lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
789 					NULL);
790 		if (lp_index_ptr)
791 			lp_index = be32_to_cpup(lp_index_ptr);
792 		of_node_put(rootdn);
793 	}
794 	seq_printf(m, "serial_number=%s\n", system_id);
795 	seq_printf(m, "system_type=%s\n", model);
796 	seq_printf(m, "partition_id=%d\n", (int)lp_index);
797 
798 	return pseries_lparcfg_data(m, v);
799 }
800 
801 static int lparcfg_open(struct inode *inode, struct file *file)
802 {
803 	return single_open(file, lparcfg_data, NULL);
804 }
805 
806 static const struct proc_ops lparcfg_proc_ops = {
807 	.proc_read	= seq_read,
808 	.proc_write	= lparcfg_write,
809 	.proc_open	= lparcfg_open,
810 	.proc_release	= single_release,
811 	.proc_lseek	= seq_lseek,
812 };
813 
814 static int __init lparcfg_init(void)
815 {
816 	umode_t mode = 0444;
817 	long retval;
818 
819 	/* Allow writing if we have FW_FEATURE_SPLPAR */
820 	if (firmware_has_feature(FW_FEATURE_SPLPAR))
821 		mode |= 0200;
822 
823 	if (!proc_create("powerpc/lparcfg", mode, NULL, &lparcfg_proc_ops)) {
824 		printk(KERN_ERR "Failed to create powerpc/lparcfg\n");
825 		return -EIO;
826 	}
827 
828 	/* If this call fails, it would result in APP values
829 	 * being wrong for since boot reports of lparstat
830 	 */
831 	retval = h_pic(&boot_pool_idle_time, NULL);
832 
833 	if (retval != H_SUCCESS)
834 		pr_debug("H_PIC failed during lparcfg init retval: %ld\n",
835 			 retval);
836 
837 	return 0;
838 }
839 machine_device_initcall(pseries, lparcfg_init);
840