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