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