1 /*
2  * PowerPC64 LPAR Configuration Information Driver
3  *
4  * Dave Engebretsen engebret@us.ibm.com
5  *    Copyright (c) 2003 Dave Engebretsen
6  * Will Schmidt willschm@us.ibm.com
7  *    SPLPAR updates, Copyright (c) 2003 Will Schmidt IBM Corporation.
8  *    seq_file updates, Copyright (c) 2004 Will Schmidt IBM Corporation.
9  * Nathan Lynch nathanl@austin.ibm.com
10  *    Added lparcfg_write, Copyright (C) 2004 Nathan Lynch IBM Corporation.
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  *
17  * This driver creates a proc file at /proc/ppc64/lparcfg which contains
18  * keyword - value pairs that specify the configuration of the partition.
19  */
20 
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/proc_fs.h>
25 #include <linux/init.h>
26 #include <linux/seq_file.h>
27 #include <linux/slab.h>
28 #include <asm/uaccess.h>
29 #include <asm/iseries/hv_lp_config.h>
30 #include <asm/lppaca.h>
31 #include <asm/hvcall.h>
32 #include <asm/firmware.h>
33 #include <asm/rtas.h>
34 #include <asm/system.h>
35 #include <asm/time.h>
36 #include <asm/prom.h>
37 #include <asm/vdso_datapage.h>
38 #include <asm/vio.h>
39 #include <asm/mmu.h>
40 
41 #define MODULE_VERS "1.9"
42 #define MODULE_NAME "lparcfg"
43 
44 /* #define LPARCFG_DEBUG */
45 
46 static struct proc_dir_entry *proc_ppc64_lparcfg;
47 
48 /*
49  * Track sum of all purrs across all processors. This is used to further
50  * calculate usage values by different applications
51  */
get_purr(void)52 static unsigned long get_purr(void)
53 {
54 	unsigned long sum_purr = 0;
55 	int cpu;
56 
57 	for_each_possible_cpu(cpu) {
58 		if (firmware_has_feature(FW_FEATURE_ISERIES))
59 			sum_purr += lppaca_of(cpu).emulated_time_base;
60 		else {
61 			struct cpu_usage *cu;
62 
63 			cu = &per_cpu(cpu_usage_array, cpu);
64 			sum_purr += cu->current_tb;
65 		}
66 	}
67 	return sum_purr;
68 }
69 
70 #ifdef CONFIG_PPC_ISERIES
71 
72 /*
73  * Methods used to fetch LPAR data when running on an iSeries platform.
74  */
iseries_lparcfg_data(struct seq_file * m,void * v)75 static int iseries_lparcfg_data(struct seq_file *m, void *v)
76 {
77 	unsigned long pool_id;
78 	int shared, entitled_capacity, max_entitled_capacity;
79 	int processors, max_processors;
80 	unsigned long purr = get_purr();
81 
82 	shared = (int)(local_paca->lppaca_ptr->shared_proc);
83 
84 	seq_printf(m, "system_active_processors=%d\n",
85 		   (int)HvLpConfig_getSystemPhysicalProcessors());
86 
87 	seq_printf(m, "system_potential_processors=%d\n",
88 		   (int)HvLpConfig_getSystemPhysicalProcessors());
89 
90 	processors = (int)HvLpConfig_getPhysicalProcessors();
91 	seq_printf(m, "partition_active_processors=%d\n", processors);
92 
93 	max_processors = (int)HvLpConfig_getMaxPhysicalProcessors();
94 	seq_printf(m, "partition_potential_processors=%d\n", max_processors);
95 
96 	if (shared) {
97 		entitled_capacity = HvLpConfig_getSharedProcUnits();
98 		max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits();
99 	} else {
100 		entitled_capacity = processors * 100;
101 		max_entitled_capacity = max_processors * 100;
102 	}
103 	seq_printf(m, "partition_entitled_capacity=%d\n", entitled_capacity);
104 
105 	seq_printf(m, "partition_max_entitled_capacity=%d\n",
106 		   max_entitled_capacity);
107 
108 	if (shared) {
109 		pool_id = HvLpConfig_getSharedPoolIndex();
110 		seq_printf(m, "pool=%d\n", (int)pool_id);
111 		seq_printf(m, "pool_capacity=%d\n",
112 			   (int)(HvLpConfig_getNumProcsInSharedPool(pool_id) *
113 				 100));
114 		seq_printf(m, "purr=%ld\n", purr);
115 	}
116 
117 	seq_printf(m, "shared_processor_mode=%d\n", shared);
118 
119 	return 0;
120 }
121 
122 #else				/* CONFIG_PPC_ISERIES */
123 
iseries_lparcfg_data(struct seq_file * m,void * v)124 static int iseries_lparcfg_data(struct seq_file *m, void *v)
125 {
126 	return 0;
127 }
128 
129 #endif				/* CONFIG_PPC_ISERIES */
130 
131 #ifdef CONFIG_PPC_PSERIES
132 /*
133  * Methods used to fetch LPAR data when running on a pSeries platform.
134  */
135 /**
136  * h_get_mpp
137  * H_GET_MPP hcall returns info in 7 parms
138  */
h_get_mpp(struct hvcall_mpp_data * mpp_data)139 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
140 {
141 	int rc;
142 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
143 
144 	rc = plpar_hcall9(H_GET_MPP, retbuf);
145 
146 	mpp_data->entitled_mem = retbuf[0];
147 	mpp_data->mapped_mem = retbuf[1];
148 
149 	mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
150 	mpp_data->pool_num = retbuf[2] & 0xffff;
151 
152 	mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
153 	mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
154 	mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffff;
155 
156 	mpp_data->pool_size = retbuf[4];
157 	mpp_data->loan_request = retbuf[5];
158 	mpp_data->backing_mem = retbuf[6];
159 
160 	return rc;
161 }
162 EXPORT_SYMBOL(h_get_mpp);
163 
164 struct hvcall_ppp_data {
165 	u64	entitlement;
166 	u64	unallocated_entitlement;
167 	u16	group_num;
168 	u16	pool_num;
169 	u8	capped;
170 	u8	weight;
171 	u8	unallocated_weight;
172 	u16	active_procs_in_pool;
173 	u16	active_system_procs;
174 	u16	phys_platform_procs;
175 	u32	max_proc_cap_avail;
176 	u32	entitled_proc_cap_avail;
177 };
178 
179 /*
180  * H_GET_PPP hcall returns info in 4 parms.
181  *  entitled_capacity,unallocated_capacity,
182  *  aggregation, resource_capability).
183  *
184  *  R4 = Entitled Processor Capacity Percentage.
185  *  R5 = Unallocated Processor Capacity Percentage.
186  *  R6 (AABBCCDDEEFFGGHH).
187  *      XXXX - reserved (0)
188  *          XXXX - reserved (0)
189  *              XXXX - Group Number
190  *                  XXXX - Pool Number.
191  *  R7 (IIJJKKLLMMNNOOPP).
192  *      XX - reserved. (0)
193  *        XX - bit 0-6 reserved (0).   bit 7 is Capped indicator.
194  *          XX - variable processor Capacity Weight
195  *            XX - Unallocated Variable Processor Capacity Weight.
196  *              XXXX - Active processors in Physical Processor Pool.
197  *                  XXXX  - Processors active on platform.
198  *  R8 (QQQQRRRRRRSSSSSS). if ibm,partition-performance-parameters-level >= 1
199  *	XXXX - Physical platform procs allocated to virtualization.
200  *	    XXXXXX - Max procs capacity % available to the partitions pool.
201  *	          XXXXXX - Entitled procs capacity % available to the
202  *			   partitions pool.
203  */
h_get_ppp(struct hvcall_ppp_data * ppp_data)204 static unsigned int h_get_ppp(struct hvcall_ppp_data *ppp_data)
205 {
206 	unsigned long rc;
207 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
208 
209 	rc = plpar_hcall9(H_GET_PPP, retbuf);
210 
211 	ppp_data->entitlement = retbuf[0];
212 	ppp_data->unallocated_entitlement = retbuf[1];
213 
214 	ppp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
215 	ppp_data->pool_num = retbuf[2] & 0xffff;
216 
217 	ppp_data->capped = (retbuf[3] >> 6 * 8) & 0x01;
218 	ppp_data->weight = (retbuf[3] >> 5 * 8) & 0xff;
219 	ppp_data->unallocated_weight = (retbuf[3] >> 4 * 8) & 0xff;
220 	ppp_data->active_procs_in_pool = (retbuf[3] >> 2 * 8) & 0xffff;
221 	ppp_data->active_system_procs = retbuf[3] & 0xffff;
222 
223 	ppp_data->phys_platform_procs = retbuf[4] >> 6 * 8;
224 	ppp_data->max_proc_cap_avail = (retbuf[4] >> 3 * 8) & 0xffffff;
225 	ppp_data->entitled_proc_cap_avail = retbuf[4] & 0xffffff;
226 
227 	return rc;
228 }
229 
h_pic(unsigned long * pool_idle_time,unsigned long * num_procs)230 static unsigned h_pic(unsigned long *pool_idle_time,
231 		      unsigned long *num_procs)
232 {
233 	unsigned long rc;
234 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
235 
236 	rc = plpar_hcall(H_PIC, retbuf);
237 
238 	*pool_idle_time = retbuf[0];
239 	*num_procs = retbuf[1];
240 
241 	return rc;
242 }
243 
244 /*
245  * parse_ppp_data
246  * Parse out the data returned from h_get_ppp and h_pic
247  */
parse_ppp_data(struct seq_file * m)248 static void parse_ppp_data(struct seq_file *m)
249 {
250 	struct hvcall_ppp_data ppp_data;
251 	struct device_node *root;
252 	const int *perf_level;
253 	int rc;
254 
255 	rc = h_get_ppp(&ppp_data);
256 	if (rc)
257 		return;
258 
259 	seq_printf(m, "partition_entitled_capacity=%lld\n",
260 	           ppp_data.entitlement);
261 	seq_printf(m, "group=%d\n", ppp_data.group_num);
262 	seq_printf(m, "system_active_processors=%d\n",
263 	           ppp_data.active_system_procs);
264 
265 	/* pool related entries are appropriate for shared configs */
266 	if (lppaca_of(0).shared_proc) {
267 		unsigned long pool_idle_time, pool_procs;
268 
269 		seq_printf(m, "pool=%d\n", ppp_data.pool_num);
270 
271 		/* report pool_capacity in percentage */
272 		seq_printf(m, "pool_capacity=%d\n",
273 			   ppp_data.active_procs_in_pool * 100);
274 
275 		h_pic(&pool_idle_time, &pool_procs);
276 		seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
277 		seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
278 	}
279 
280 	seq_printf(m, "unallocated_capacity_weight=%d\n",
281 		   ppp_data.unallocated_weight);
282 	seq_printf(m, "capacity_weight=%d\n", ppp_data.weight);
283 	seq_printf(m, "capped=%d\n", ppp_data.capped);
284 	seq_printf(m, "unallocated_capacity=%lld\n",
285 		   ppp_data.unallocated_entitlement);
286 
287 	/* The last bits of information returned from h_get_ppp are only
288 	 * valid if the ibm,partition-performance-parameters-level
289 	 * property is >= 1.
290 	 */
291 	root = of_find_node_by_path("/");
292 	if (root) {
293 		perf_level = of_get_property(root,
294 				"ibm,partition-performance-parameters-level",
295 					     NULL);
296 		if (perf_level && (*perf_level >= 1)) {
297 			seq_printf(m,
298 			    "physical_procs_allocated_to_virtualization=%d\n",
299 				   ppp_data.phys_platform_procs);
300 			seq_printf(m, "max_proc_capacity_available=%d\n",
301 				   ppp_data.max_proc_cap_avail);
302 			seq_printf(m, "entitled_proc_capacity_available=%d\n",
303 				   ppp_data.entitled_proc_cap_avail);
304 		}
305 
306 		of_node_put(root);
307 	}
308 }
309 
310 /**
311  * parse_mpp_data
312  * Parse out data returned from h_get_mpp
313  */
parse_mpp_data(struct seq_file * m)314 static void parse_mpp_data(struct seq_file *m)
315 {
316 	struct hvcall_mpp_data mpp_data;
317 	int rc;
318 
319 	rc = h_get_mpp(&mpp_data);
320 	if (rc)
321 		return;
322 
323 	seq_printf(m, "entitled_memory=%ld\n", mpp_data.entitled_mem);
324 
325 	if (mpp_data.mapped_mem != -1)
326 		seq_printf(m, "mapped_entitled_memory=%ld\n",
327 		           mpp_data.mapped_mem);
328 
329 	seq_printf(m, "entitled_memory_group_number=%d\n", mpp_data.group_num);
330 	seq_printf(m, "entitled_memory_pool_number=%d\n", mpp_data.pool_num);
331 
332 	seq_printf(m, "entitled_memory_weight=%d\n", mpp_data.mem_weight);
333 	seq_printf(m, "unallocated_entitled_memory_weight=%d\n",
334 	           mpp_data.unallocated_mem_weight);
335 	seq_printf(m, "unallocated_io_mapping_entitlement=%ld\n",
336 	           mpp_data.unallocated_entitlement);
337 
338 	if (mpp_data.pool_size != -1)
339 		seq_printf(m, "entitled_memory_pool_size=%ld bytes\n",
340 		           mpp_data.pool_size);
341 
342 	seq_printf(m, "entitled_memory_loan_request=%ld\n",
343 	           mpp_data.loan_request);
344 
345 	seq_printf(m, "backing_memory=%ld bytes\n", mpp_data.backing_mem);
346 }
347 
348 #define SPLPAR_CHARACTERISTICS_TOKEN 20
349 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
350 
351 /*
352  * parse_system_parameter_string()
353  * Retrieve the potential_processors, max_entitled_capacity and friends
354  * through the get-system-parameter rtas call.  Replace keyword strings as
355  * necessary.
356  */
parse_system_parameter_string(struct seq_file * m)357 static void parse_system_parameter_string(struct seq_file *m)
358 {
359 	int call_status;
360 
361 	unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
362 	if (!local_buffer) {
363 		printk(KERN_ERR "%s %s kmalloc failure at line %d\n",
364 		       __FILE__, __func__, __LINE__);
365 		return;
366 	}
367 
368 	spin_lock(&rtas_data_buf_lock);
369 	memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
370 	call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
371 				NULL,
372 				SPLPAR_CHARACTERISTICS_TOKEN,
373 				__pa(rtas_data_buf),
374 				RTAS_DATA_BUF_SIZE);
375 	memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
376 	spin_unlock(&rtas_data_buf_lock);
377 
378 	if (call_status != 0) {
379 		printk(KERN_INFO
380 		       "%s %s Error calling get-system-parameter (0x%x)\n",
381 		       __FILE__, __func__, call_status);
382 	} else {
383 		int splpar_strlen;
384 		int idx, w_idx;
385 		char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
386 		if (!workbuffer) {
387 			printk(KERN_ERR "%s %s kmalloc failure at line %d\n",
388 			       __FILE__, __func__, __LINE__);
389 			kfree(local_buffer);
390 			return;
391 		}
392 #ifdef LPARCFG_DEBUG
393 		printk(KERN_INFO "success calling get-system-parameter\n");
394 #endif
395 		splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
396 		local_buffer += 2;	/* step over strlen value */
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 	}
430 	kfree(local_buffer);
431 }
432 
433 /* Return the number of processors in the system.
434  * This function reads through the device tree and counts
435  * the virtual processors, this does not include threads.
436  */
lparcfg_count_active_processors(void)437 static int lparcfg_count_active_processors(void)
438 {
439 	struct device_node *cpus_dn = NULL;
440 	int count = 0;
441 
442 	while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
443 #ifdef LPARCFG_DEBUG
444 		printk(KERN_ERR "cpus_dn %p\n", cpus_dn);
445 #endif
446 		count++;
447 	}
448 	return count;
449 }
450 
pseries_cmo_data(struct seq_file * m)451 static void pseries_cmo_data(struct seq_file *m)
452 {
453 	int cpu;
454 	unsigned long cmo_faults = 0;
455 	unsigned long cmo_fault_time = 0;
456 
457 	seq_printf(m, "cmo_enabled=%d\n", firmware_has_feature(FW_FEATURE_CMO));
458 
459 	if (!firmware_has_feature(FW_FEATURE_CMO))
460 		return;
461 
462 	for_each_possible_cpu(cpu) {
463 		cmo_faults += lppaca_of(cpu).cmo_faults;
464 		cmo_fault_time += lppaca_of(cpu).cmo_fault_time;
465 	}
466 
467 	seq_printf(m, "cmo_faults=%lu\n", cmo_faults);
468 	seq_printf(m, "cmo_fault_time_usec=%lu\n",
469 		   cmo_fault_time / tb_ticks_per_usec);
470 	seq_printf(m, "cmo_primary_psp=%d\n", cmo_get_primary_psp());
471 	seq_printf(m, "cmo_secondary_psp=%d\n", cmo_get_secondary_psp());
472 	seq_printf(m, "cmo_page_size=%lu\n", cmo_get_page_size());
473 }
474 
splpar_dispatch_data(struct seq_file * m)475 static void splpar_dispatch_data(struct seq_file *m)
476 {
477 	int cpu;
478 	unsigned long dispatches = 0;
479 	unsigned long dispatch_dispersions = 0;
480 
481 	for_each_possible_cpu(cpu) {
482 		dispatches += lppaca_of(cpu).yield_count;
483 		dispatch_dispersions += lppaca_of(cpu).dispersion_count;
484 	}
485 
486 	seq_printf(m, "dispatches=%lu\n", dispatches);
487 	seq_printf(m, "dispatch_dispersions=%lu\n", dispatch_dispersions);
488 }
489 
parse_em_data(struct seq_file * m)490 static void parse_em_data(struct seq_file *m)
491 {
492 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
493 
494 	if (plpar_hcall(H_GET_EM_PARMS, retbuf) == H_SUCCESS)
495 		seq_printf(m, "power_mode_data=%016lx\n", retbuf[0]);
496 }
497 
pseries_lparcfg_data(struct seq_file * m,void * v)498 static int pseries_lparcfg_data(struct seq_file *m, void *v)
499 {
500 	int partition_potential_processors;
501 	int partition_active_processors;
502 	struct device_node *rtas_node;
503 	const int *lrdrp = NULL;
504 
505 	rtas_node = of_find_node_by_path("/rtas");
506 	if (rtas_node)
507 		lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
508 
509 	if (lrdrp == NULL) {
510 		partition_potential_processors = vdso_data->processorCount;
511 	} else {
512 		partition_potential_processors = *(lrdrp + 4);
513 	}
514 	of_node_put(rtas_node);
515 
516 	partition_active_processors = lparcfg_count_active_processors();
517 
518 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
519 		/* this call handles the ibm,get-system-parameter contents */
520 		parse_system_parameter_string(m);
521 		parse_ppp_data(m);
522 		parse_mpp_data(m);
523 		pseries_cmo_data(m);
524 		splpar_dispatch_data(m);
525 
526 		seq_printf(m, "purr=%ld\n", get_purr());
527 	} else {		/* non SPLPAR case */
528 
529 		seq_printf(m, "system_active_processors=%d\n",
530 			   partition_potential_processors);
531 
532 		seq_printf(m, "system_potential_processors=%d\n",
533 			   partition_potential_processors);
534 
535 		seq_printf(m, "partition_max_entitled_capacity=%d\n",
536 			   partition_potential_processors * 100);
537 
538 		seq_printf(m, "partition_entitled_capacity=%d\n",
539 			   partition_active_processors * 100);
540 	}
541 
542 	seq_printf(m, "partition_active_processors=%d\n",
543 		   partition_active_processors);
544 
545 	seq_printf(m, "partition_potential_processors=%d\n",
546 		   partition_potential_processors);
547 
548 	seq_printf(m, "shared_processor_mode=%d\n", lppaca_of(0).shared_proc);
549 
550 	seq_printf(m, "slb_size=%d\n", mmu_slb_size);
551 
552 	parse_em_data(m);
553 
554 	return 0;
555 }
556 
update_ppp(u64 * entitlement,u8 * weight)557 static ssize_t update_ppp(u64 *entitlement, u8 *weight)
558 {
559 	struct hvcall_ppp_data ppp_data;
560 	u8 new_weight;
561 	u64 new_entitled;
562 	ssize_t retval;
563 
564 	/* Get our current parameters */
565 	retval = h_get_ppp(&ppp_data);
566 	if (retval)
567 		return retval;
568 
569 	if (entitlement) {
570 		new_weight = ppp_data.weight;
571 		new_entitled = *entitlement;
572 	} else if (weight) {
573 		new_weight = *weight;
574 		new_entitled = ppp_data.entitlement;
575 	} else
576 		return -EINVAL;
577 
578 	pr_debug("%s: current_entitled = %llu, current_weight = %u\n",
579 		 __func__, ppp_data.entitlement, ppp_data.weight);
580 
581 	pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
582 		 __func__, new_entitled, new_weight);
583 
584 	retval = plpar_hcall_norets(H_SET_PPP, new_entitled, new_weight);
585 	return retval;
586 }
587 
588 /**
589  * update_mpp
590  *
591  * Update the memory entitlement and weight for the partition.  Caller must
592  * specify either a new entitlement or weight, not both, to be updated
593  * since the h_set_mpp call takes both entitlement and weight as parameters.
594  */
update_mpp(u64 * entitlement,u8 * weight)595 static ssize_t update_mpp(u64 *entitlement, u8 *weight)
596 {
597 	struct hvcall_mpp_data mpp_data;
598 	u64 new_entitled;
599 	u8 new_weight;
600 	ssize_t rc;
601 
602 	if (entitlement) {
603 		/* Check with vio to ensure the new memory entitlement
604 		 * can be handled.
605 		 */
606 		rc = vio_cmo_entitlement_update(*entitlement);
607 		if (rc)
608 			return rc;
609 	}
610 
611 	rc = h_get_mpp(&mpp_data);
612 	if (rc)
613 		return rc;
614 
615 	if (entitlement) {
616 		new_weight = mpp_data.mem_weight;
617 		new_entitled = *entitlement;
618 	} else if (weight) {
619 		new_weight = *weight;
620 		new_entitled = mpp_data.entitled_mem;
621 	} else
622 		return -EINVAL;
623 
624 	pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
625 	         __func__, mpp_data.entitled_mem, mpp_data.mem_weight);
626 
627 	pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
628 		 __func__, new_entitled, new_weight);
629 
630 	rc = plpar_hcall_norets(H_SET_MPP, new_entitled, new_weight);
631 	return rc;
632 }
633 
634 /*
635  * Interface for changing system parameters (variable capacity weight
636  * and entitled capacity).  Format of input is "param_name=value";
637  * anything after value is ignored.  Valid parameters at this time are
638  * "partition_entitled_capacity" and "capacity_weight".  We use
639  * H_SET_PPP to alter parameters.
640  *
641  * This function should be invoked only on systems with
642  * FW_FEATURE_SPLPAR.
643  */
lparcfg_write(struct file * file,const char __user * buf,size_t count,loff_t * off)644 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
645 			     size_t count, loff_t * off)
646 {
647 	int kbuf_sz = 64;
648 	char kbuf[kbuf_sz];
649 	char *tmp;
650 	u64 new_entitled, *new_entitled_ptr = &new_entitled;
651 	u8 new_weight, *new_weight_ptr = &new_weight;
652 	ssize_t retval;
653 
654 	if (!firmware_has_feature(FW_FEATURE_SPLPAR) ||
655 			firmware_has_feature(FW_FEATURE_ISERIES))
656 		return -EINVAL;
657 
658 	if (count > kbuf_sz)
659 		return -EINVAL;
660 
661 	if (copy_from_user(kbuf, buf, count))
662 		return -EFAULT;
663 
664 	kbuf[count - 1] = '\0';
665 	tmp = strchr(kbuf, '=');
666 	if (!tmp)
667 		return -EINVAL;
668 
669 	*tmp++ = '\0';
670 
671 	if (!strcmp(kbuf, "partition_entitled_capacity")) {
672 		char *endp;
673 		*new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
674 		if (endp == tmp)
675 			return -EINVAL;
676 
677 		retval = update_ppp(new_entitled_ptr, NULL);
678 	} else if (!strcmp(kbuf, "capacity_weight")) {
679 		char *endp;
680 		*new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
681 		if (endp == tmp)
682 			return -EINVAL;
683 
684 		retval = update_ppp(NULL, new_weight_ptr);
685 	} else if (!strcmp(kbuf, "entitled_memory")) {
686 		char *endp;
687 		*new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
688 		if (endp == tmp)
689 			return -EINVAL;
690 
691 		retval = update_mpp(new_entitled_ptr, NULL);
692 	} else if (!strcmp(kbuf, "entitled_memory_weight")) {
693 		char *endp;
694 		*new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
695 		if (endp == tmp)
696 			return -EINVAL;
697 
698 		retval = update_mpp(NULL, new_weight_ptr);
699 	} else
700 		return -EINVAL;
701 
702 	if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
703 		retval = count;
704 	} else if (retval == H_BUSY) {
705 		retval = -EBUSY;
706 	} else if (retval == H_HARDWARE) {
707 		retval = -EIO;
708 	} else if (retval == H_PARAMETER) {
709 		retval = -EINVAL;
710 	}
711 
712 	return retval;
713 }
714 
715 #else				/* CONFIG_PPC_PSERIES */
716 
pseries_lparcfg_data(struct seq_file * m,void * v)717 static int pseries_lparcfg_data(struct seq_file *m, void *v)
718 {
719 	return 0;
720 }
721 
lparcfg_write(struct file * file,const char __user * buf,size_t count,loff_t * off)722 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
723 			     size_t count, loff_t * off)
724 {
725 	return -EINVAL;
726 }
727 
728 #endif				/* CONFIG_PPC_PSERIES */
729 
lparcfg_data(struct seq_file * m,void * v)730 static int lparcfg_data(struct seq_file *m, void *v)
731 {
732 	struct device_node *rootdn;
733 	const char *model = "";
734 	const char *system_id = "";
735 	const char *tmp;
736 	const unsigned int *lp_index_ptr;
737 	unsigned int lp_index = 0;
738 
739 	seq_printf(m, "%s %s\n", MODULE_NAME, MODULE_VERS);
740 
741 	rootdn = of_find_node_by_path("/");
742 	if (rootdn) {
743 		tmp = of_get_property(rootdn, "model", NULL);
744 		if (tmp) {
745 			model = tmp;
746 			/* Skip "IBM," - see platforms/iseries/dt.c */
747 			if (firmware_has_feature(FW_FEATURE_ISERIES))
748 				model += 4;
749 		}
750 		tmp = of_get_property(rootdn, "system-id", NULL);
751 		if (tmp) {
752 			system_id = tmp;
753 			/* Skip "IBM," - see platforms/iseries/dt.c */
754 			if (firmware_has_feature(FW_FEATURE_ISERIES))
755 				system_id += 4;
756 		}
757 		lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
758 					NULL);
759 		if (lp_index_ptr)
760 			lp_index = *lp_index_ptr;
761 		of_node_put(rootdn);
762 	}
763 	seq_printf(m, "serial_number=%s\n", system_id);
764 	seq_printf(m, "system_type=%s\n", model);
765 	seq_printf(m, "partition_id=%d\n", (int)lp_index);
766 
767 	if (firmware_has_feature(FW_FEATURE_ISERIES))
768 		return iseries_lparcfg_data(m, v);
769 	return pseries_lparcfg_data(m, v);
770 }
771 
lparcfg_open(struct inode * inode,struct file * file)772 static int lparcfg_open(struct inode *inode, struct file *file)
773 {
774 	return single_open(file, lparcfg_data, NULL);
775 }
776 
777 static const struct file_operations lparcfg_fops = {
778 	.owner		= THIS_MODULE,
779 	.read		= seq_read,
780 	.write		= lparcfg_write,
781 	.open		= lparcfg_open,
782 	.release	= single_release,
783 	.llseek		= seq_lseek,
784 };
785 
lparcfg_init(void)786 static int __init lparcfg_init(void)
787 {
788 	struct proc_dir_entry *ent;
789 	mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
790 
791 	/* Allow writing if we have FW_FEATURE_SPLPAR */
792 	if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
793 			!firmware_has_feature(FW_FEATURE_ISERIES))
794 		mode |= S_IWUSR;
795 
796 	ent = proc_create("powerpc/lparcfg", mode, NULL, &lparcfg_fops);
797 	if (!ent) {
798 		printk(KERN_ERR "Failed to create powerpc/lparcfg\n");
799 		return -EIO;
800 	}
801 
802 	proc_ppc64_lparcfg = ent;
803 	return 0;
804 }
805 
lparcfg_cleanup(void)806 static void __exit lparcfg_cleanup(void)
807 {
808 	if (proc_ppc64_lparcfg)
809 		remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
810 }
811 
812 module_init(lparcfg_init);
813 module_exit(lparcfg_cleanup);
814 MODULE_DESCRIPTION("Interface for LPAR configuration data");
815 MODULE_AUTHOR("Dave Engebretsen");
816 MODULE_LICENSE("GPL");
817