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