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