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 */
54static void cpu_get_purr(void *arg)
55{
56	atomic64_t *sum = arg;
57
58	atomic64_add(mfspr(SPRN_PURR), sum);
59}
60
61static 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
74struct 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 */
114static unsigned int h_get_ppp(struct hvcall_ppp_data *ppp_data)
115{
116	unsigned long rc;
117	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
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
140static 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);
169out:
170	kfree(buf);
171}
172
173static 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 */
191static 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	int 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 */
257static 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 */
295static 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 */
324static 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 */
347static int read_dt_lpar_name(struct seq_file *m)
348{
349	struct device_node *root = of_find_node_by_path("/");
350	const char *name;
351	int ret;
352
353	ret = of_property_read_string(root, "ibm,partition-name", &name);
354	of_node_put(root);
355	if (ret)
356		return -ENOENT;
357
358	seq_printf(m, "partition_name=%s\n", name);
359	return 0;
360}
361
362static void read_lpar_name(struct seq_file *m)
363{
364	if (read_rtas_lpar_name(m) && read_dt_lpar_name(m))
365		pr_err_once("Error can't get the LPAR name");
366}
367
368#define SPLPAR_MAXLENGTH 1026*(sizeof(char))
369
370/*
371 * parse_system_parameter_string()
372 * Retrieve the potential_processors, max_entitled_capacity and friends
373 * through the get-system-parameter rtas call.  Replace keyword strings as
374 * necessary.
375 */
376static void parse_system_parameter_string(struct seq_file *m)
377{
378	struct papr_sysparm_buf *buf;
379
380	buf = papr_sysparm_buf_alloc();
381	if (!buf)
382		return;
383
384	if (papr_sysparm_get(PAPR_SYSPARM_SHARED_PROC_LPAR_ATTRS, buf)) {
385		goto out_free;
386	} else {
387		const char *local_buffer;
388		int splpar_strlen;
389		int idx, w_idx;
390		char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
391
392		if (!workbuffer)
393			goto out_free;
394
395		splpar_strlen = be16_to_cpu(buf->len);
396		local_buffer = buf->val;
397
398		w_idx = 0;
399		idx = 0;
400		while ((*local_buffer) && (idx < splpar_strlen)) {
401			workbuffer[w_idx++] = local_buffer[idx++];
402			if ((local_buffer[idx] == ',')
403			    || (local_buffer[idx] == '\0')) {
404				workbuffer[w_idx] = '\0';
405				if (w_idx) {
406					/* avoid the empty string */
407					seq_printf(m, "%s\n", workbuffer);
408				}
409				memset(workbuffer, 0, SPLPAR_MAXLENGTH);
410				idx++;	/* skip the comma */
411				w_idx = 0;
412			} else if (local_buffer[idx] == '=') {
413				/* code here to replace workbuffer contents
414				   with different keyword strings */
415				if (0 == strcmp(workbuffer, "MaxEntCap")) {
416					strcpy(workbuffer,
417					       "partition_max_entitled_capacity");
418					w_idx = strlen(workbuffer);
419				}
420				if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
421					strcpy(workbuffer,
422					       "system_potential_processors");
423					w_idx = strlen(workbuffer);
424				}
425			}
426		}
427		kfree(workbuffer);
428		local_buffer -= 2;	/* back up over strlen value */
429	}
430out_free:
431	papr_sysparm_buf_free(buf);
432}
433
434/* Return the number of processors in the system.
435 * This function reads through the device tree and counts
436 * the virtual processors, this does not include threads.
437 */
438static int lparcfg_count_active_processors(void)
439{
440	struct device_node *cpus_dn;
441	int count = 0;
442
443	for_each_node_by_type(cpus_dn, "cpu") {
444#ifdef LPARCFG_DEBUG
445		printk(KERN_ERR "cpus_dn %p\n", cpus_dn);
446#endif
447		count++;
448	}
449	return count;
450}
451
452static void pseries_cmo_data(struct seq_file *m)
453{
454	int cpu;
455	unsigned long cmo_faults = 0;
456	unsigned long cmo_fault_time = 0;
457
458	seq_printf(m, "cmo_enabled=%d\n", firmware_has_feature(FW_FEATURE_CMO));
459
460	if (!firmware_has_feature(FW_FEATURE_CMO))
461		return;
462
463	for_each_possible_cpu(cpu) {
464		cmo_faults += be64_to_cpu(lppaca_of(cpu).cmo_faults);
465		cmo_fault_time += be64_to_cpu(lppaca_of(cpu).cmo_fault_time);
466	}
467
468	seq_printf(m, "cmo_faults=%lu\n", cmo_faults);
469	seq_printf(m, "cmo_fault_time_usec=%lu\n",
470		   cmo_fault_time / tb_ticks_per_usec);
471	seq_printf(m, "cmo_primary_psp=%d\n", cmo_get_primary_psp());
472	seq_printf(m, "cmo_secondary_psp=%d\n", cmo_get_secondary_psp());
473	seq_printf(m, "cmo_page_size=%lu\n", cmo_get_page_size());
474}
475
476static void splpar_dispatch_data(struct seq_file *m)
477{
478	int cpu;
479	unsigned long dispatches = 0;
480	unsigned long dispatch_dispersions = 0;
481
482	for_each_possible_cpu(cpu) {
483		dispatches += be32_to_cpu(lppaca_of(cpu).yield_count);
484		dispatch_dispersions +=
485			be32_to_cpu(lppaca_of(cpu).dispersion_count);
486	}
487
488	seq_printf(m, "dispatches=%lu\n", dispatches);
489	seq_printf(m, "dispatch_dispersions=%lu\n", dispatch_dispersions);
490}
491
492static void parse_em_data(struct seq_file *m)
493{
494	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
495
496	if (firmware_has_feature(FW_FEATURE_LPAR) &&
497	    plpar_hcall(H_GET_EM_PARMS, retbuf) == H_SUCCESS)
498		seq_printf(m, "power_mode_data=%016lx\n", retbuf[0]);
499}
500
501static void maxmem_data(struct seq_file *m)
502{
503	unsigned long maxmem = 0;
504
505	maxmem += (unsigned long)drmem_info->n_lmbs * drmem_info->lmb_size;
506	maxmem += hugetlb_total_pages() * PAGE_SIZE;
507
508	seq_printf(m, "MaxMem=%lu\n", maxmem);
509}
510
511static int pseries_lparcfg_data(struct seq_file *m, void *v)
512{
513	int partition_potential_processors;
514	int partition_active_processors;
515	struct device_node *rtas_node;
516	const __be32 *lrdrp = NULL;
517
518	rtas_node = of_find_node_by_path("/rtas");
519	if (rtas_node)
520		lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
521
522	if (lrdrp == NULL) {
523		partition_potential_processors = vdso_data->processorCount;
524	} else {
525		partition_potential_processors = be32_to_cpup(lrdrp + 4);
526	}
527	of_node_put(rtas_node);
528
529	partition_active_processors = lparcfg_count_active_processors();
530
531	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
532		/* this call handles the ibm,get-system-parameter contents */
533		read_lpar_name(m);
534		parse_system_parameter_string(m);
535		parse_ppp_data(m);
536		parse_mpp_data(m);
537		parse_mpp_x_data(m);
538		pseries_cmo_data(m);
539		splpar_dispatch_data(m);
540
541		seq_printf(m, "purr=%ld\n", get_purr());
542		seq_printf(m, "tbr=%ld\n", mftb());
543	} else {		/* non SPLPAR case */
544
545		seq_printf(m, "system_active_processors=%d\n",
546			   partition_potential_processors);
547
548		seq_printf(m, "system_potential_processors=%d\n",
549			   partition_potential_processors);
550
551		seq_printf(m, "partition_max_entitled_capacity=%d\n",
552			   partition_potential_processors * 100);
553
554		seq_printf(m, "partition_entitled_capacity=%d\n",
555			   partition_active_processors * 100);
556	}
557
558	show_gpci_data(m);
559
560	seq_printf(m, "partition_active_processors=%d\n",
561		   partition_active_processors);
562
563	seq_printf(m, "partition_potential_processors=%d\n",
564		   partition_potential_processors);
565
566	seq_printf(m, "shared_processor_mode=%d\n",
567		   lppaca_shared_proc());
568
569#ifdef CONFIG_PPC_64S_HASH_MMU
570	if (!radix_enabled())
571		seq_printf(m, "slb_size=%d\n", mmu_slb_size);
572#endif
573	parse_em_data(m);
574	maxmem_data(m);
575
576	seq_printf(m, "security_flavor=%u\n", pseries_security_flavor);
577
578	return 0;
579}
580
581static ssize_t update_ppp(u64 *entitlement, u8 *weight)
582{
583	struct hvcall_ppp_data ppp_data;
584	u8 new_weight;
585	u64 new_entitled;
586	ssize_t retval;
587
588	/* Get our current parameters */
589	retval = h_get_ppp(&ppp_data);
590	if (retval)
591		return retval;
592
593	if (entitlement) {
594		new_weight = ppp_data.weight;
595		new_entitled = *entitlement;
596	} else if (weight) {
597		new_weight = *weight;
598		new_entitled = ppp_data.entitlement;
599	} else
600		return -EINVAL;
601
602	pr_debug("%s: current_entitled = %llu, current_weight = %u\n",
603		 __func__, ppp_data.entitlement, ppp_data.weight);
604
605	pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
606		 __func__, new_entitled, new_weight);
607
608	retval = plpar_hcall_norets(H_SET_PPP, new_entitled, new_weight);
609	return retval;
610}
611
612/**
613 * update_mpp
614 *
615 * Update the memory entitlement and weight for the partition.  Caller must
616 * specify either a new entitlement or weight, not both, to be updated
617 * since the h_set_mpp call takes both entitlement and weight as parameters.
618 */
619static ssize_t update_mpp(u64 *entitlement, u8 *weight)
620{
621	struct hvcall_mpp_data mpp_data;
622	u64 new_entitled;
623	u8 new_weight;
624	ssize_t rc;
625
626	if (entitlement) {
627		/* Check with vio to ensure the new memory entitlement
628		 * can be handled.
629		 */
630		rc = vio_cmo_entitlement_update(*entitlement);
631		if (rc)
632			return rc;
633	}
634
635	rc = h_get_mpp(&mpp_data);
636	if (rc)
637		return rc;
638
639	if (entitlement) {
640		new_weight = mpp_data.mem_weight;
641		new_entitled = *entitlement;
642	} else if (weight) {
643		new_weight = *weight;
644		new_entitled = mpp_data.entitled_mem;
645	} else
646		return -EINVAL;
647
648	pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
649	         __func__, mpp_data.entitled_mem, mpp_data.mem_weight);
650
651	pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
652		 __func__, new_entitled, new_weight);
653
654	rc = plpar_hcall_norets(H_SET_MPP, new_entitled, new_weight);
655	return rc;
656}
657
658/*
659 * Interface for changing system parameters (variable capacity weight
660 * and entitled capacity).  Format of input is "param_name=value";
661 * anything after value is ignored.  Valid parameters at this time are
662 * "partition_entitled_capacity" and "capacity_weight".  We use
663 * H_SET_PPP to alter parameters.
664 *
665 * This function should be invoked only on systems with
666 * FW_FEATURE_SPLPAR.
667 */
668static ssize_t lparcfg_write(struct file *file, const char __user * buf,
669			     size_t count, loff_t * off)
670{
671	char kbuf[64];
672	char *tmp;
673	u64 new_entitled, *new_entitled_ptr = &new_entitled;
674	u8 new_weight, *new_weight_ptr = &new_weight;
675	ssize_t retval;
676
677	if (!firmware_has_feature(FW_FEATURE_SPLPAR))
678		return -EINVAL;
679
680	if (count > sizeof(kbuf))
681		return -EINVAL;
682
683	if (copy_from_user(kbuf, buf, count))
684		return -EFAULT;
685
686	kbuf[count - 1] = '\0';
687	tmp = strchr(kbuf, '=');
688	if (!tmp)
689		return -EINVAL;
690
691	*tmp++ = '\0';
692
693	if (!strcmp(kbuf, "partition_entitled_capacity")) {
694		char *endp;
695		*new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
696		if (endp == tmp)
697			return -EINVAL;
698
699		retval = update_ppp(new_entitled_ptr, NULL);
700
701		if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
702			/*
703			 * The hypervisor assigns VAS resources based
704			 * on entitled capacity for shared mode.
705			 * Reconfig VAS windows based on DLPAR CPU events.
706			 */
707			if (pseries_vas_dlpar_cpu() != 0)
708				retval = H_HARDWARE;
709		}
710	} else if (!strcmp(kbuf, "capacity_weight")) {
711		char *endp;
712		*new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
713		if (endp == tmp)
714			return -EINVAL;
715
716		retval = update_ppp(NULL, new_weight_ptr);
717	} else if (!strcmp(kbuf, "entitled_memory")) {
718		char *endp;
719		*new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
720		if (endp == tmp)
721			return -EINVAL;
722
723		retval = update_mpp(new_entitled_ptr, NULL);
724	} else if (!strcmp(kbuf, "entitled_memory_weight")) {
725		char *endp;
726		*new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
727		if (endp == tmp)
728			return -EINVAL;
729
730		retval = update_mpp(NULL, new_weight_ptr);
731	} else
732		return -EINVAL;
733
734	if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
735		retval = count;
736	} else if (retval == H_BUSY) {
737		retval = -EBUSY;
738	} else if (retval == H_HARDWARE) {
739		retval = -EIO;
740	} else if (retval == H_PARAMETER) {
741		retval = -EINVAL;
742	}
743
744	return retval;
745}
746
747static int lparcfg_data(struct seq_file *m, void *v)
748{
749	struct device_node *rootdn;
750	const char *model = "";
751	const char *system_id = "";
752	const char *tmp;
753	const __be32 *lp_index_ptr;
754	unsigned int lp_index = 0;
755
756	seq_printf(m, "%s %s\n", MODULE_NAME, MODULE_VERS);
757
758	rootdn = of_find_node_by_path("/");
759	if (rootdn) {
760		tmp = of_get_property(rootdn, "model", NULL);
761		if (tmp)
762			model = tmp;
763		tmp = of_get_property(rootdn, "system-id", NULL);
764		if (tmp)
765			system_id = tmp;
766		lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
767					NULL);
768		if (lp_index_ptr)
769			lp_index = be32_to_cpup(lp_index_ptr);
770		of_node_put(rootdn);
771	}
772	seq_printf(m, "serial_number=%s\n", system_id);
773	seq_printf(m, "system_type=%s\n", model);
774	seq_printf(m, "partition_id=%d\n", (int)lp_index);
775
776	return pseries_lparcfg_data(m, v);
777}
778
779static int lparcfg_open(struct inode *inode, struct file *file)
780{
781	return single_open(file, lparcfg_data, NULL);
782}
783
784static const struct proc_ops lparcfg_proc_ops = {
785	.proc_read	= seq_read,
786	.proc_write	= lparcfg_write,
787	.proc_open	= lparcfg_open,
788	.proc_release	= single_release,
789	.proc_lseek	= seq_lseek,
790};
791
792static int __init lparcfg_init(void)
793{
794	umode_t mode = 0444;
795
796	/* Allow writing if we have FW_FEATURE_SPLPAR */
797	if (firmware_has_feature(FW_FEATURE_SPLPAR))
798		mode |= 0200;
799
800	if (!proc_create("powerpc/lparcfg", mode, NULL, &lparcfg_proc_ops)) {
801		printk(KERN_ERR "Failed to create powerpc/lparcfg\n");
802		return -EIO;
803	}
804	return 0;
805}
806machine_device_initcall(pseries, lparcfg_init);
807