Merge tag 'powerpc-6.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux-block.git] / arch / powerpc / platforms / pseries / lparcfg.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
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 *
1da177e4
LT
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
1da177e4
LT
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>
fff9846b 22#include <asm/papr-sysparm.h>
1da177e4 23#include <linux/seq_file.h>
5a0e3ad6 24#include <linux/slab.h>
7c0f6ba6 25#include <linux/uaccess.h>
772b039f 26#include <linux/hugetlb.h>
1da177e4 27#include <asm/lppaca.h>
1da177e4 28#include <asm/hvcall.h>
1ababe11 29#include <asm/firmware.h>
1da177e4 30#include <asm/rtas.h>
1da177e4 31#include <asm/time.h>
271c3f35 32#include <asm/vdso_datapage.h>
22e1a4dd 33#include <asm/vio.h>
46db2f86 34#include <asm/mmu.h>
f5f6cbb6 35#include <asm/machdep.h>
772b039f 36#include <asm/drmem.h>
1da177e4 37
8f272a5d 38#include "pseries.h"
2147783d 39#include "vas.h" /* pseries_vas_dlpar_cpu() */
f5f6cbb6
BH
40
41/*
42 * This isn't a module but we expose that to userspace
43 * via /proc so leave the definitions here
44 */
6fe9d1fa 45#define MODULE_VERS "1.9"
1da177e4
LT
46#define MODULE_NAME "lparcfg"
47
48/* #define LPARCFG_DEBUG */
49
1da177e4 50/*
16e9f994
SR
51 * Track sum of all purrs across all processors. This is used to further
52 * calculate usage values by different applications
1da177e4 53 */
3d3a6021
NP
54static void cpu_get_purr(void *arg)
55{
56 atomic64_t *sum = arg;
57
58 atomic64_add(mfspr(SPRN_PURR), sum);
59}
60
1da177e4
LT
61static unsigned long get_purr(void)
62{
3d3a6021 63 atomic64_t purr = ATOMIC64_INIT(0);
1da177e4 64
3d3a6021 65 on_each_cpu(cpu_get_purr, &purr, 1);
1da177e4 66
3d3a6021 67 return atomic64_read(&purr);
1da177e4
LT
68}
69
d3d2176a 70/*
1da177e4
LT
71 * Methods used to fetch LPAR data when running on a pSeries platform.
72 */
dfc3403f 73
398778f7
RJ
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;
f03cdb3a
NF
84 u16 phys_platform_procs;
85 u32 max_proc_cap_avail;
86 u32 entitled_proc_cap_avail;
398778f7
RJ
87};
88
1da177e4
LT
89/*
90 * H_GET_PPP hcall returns info in 4 parms.
91 * entitled_capacity,unallocated_capacity,
92 * aggregation, resource_capability).
93 *
d3d2176a 94 * R4 = Entitled Processor Capacity Percentage.
1da177e4
LT
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.
d3d2176a 107 * XXXX - Processors active on platform.
f03cdb3a
NF
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.
1da177e4 113 */
398778f7 114static unsigned int h_get_ppp(struct hvcall_ppp_data *ppp_data)
1da177e4 115{
6d434163
SH
116 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
117 long rc;
b9377ffc 118
f03cdb3a 119 rc = plpar_hcall9(H_GET_PPP, retbuf);
b9377ffc 120
398778f7
RJ
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;
1da177e4 132
f03cdb3a
NF
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
1da177e4
LT
137 return rc;
138}
139
5d1bc776
SC
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
9c74ecfd
SH
173static long h_pic(unsigned long *pool_idle_time,
174 unsigned long *num_procs)
1da177e4 175{
9c74ecfd
SH
176 long rc;
177 unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = {0};
b9377ffc
AB
178
179 rc = plpar_hcall(H_PIC, retbuf);
180
9c74ecfd
SH
181 if (pool_idle_time)
182 *pool_idle_time = retbuf[0];
183 if (num_procs)
184 *num_procs = retbuf[1];
11529396
NF
185
186 return rc;
187}
188
9c74ecfd
SH
189unsigned long boot_pool_idle_time;
190
11529396
NF
191/*
192 * parse_ppp_data
193 * Parse out the data returned from h_get_ppp and h_pic
194 */
195static void parse_ppp_data(struct seq_file *m)
196{
398778f7 197 struct hvcall_ppp_data ppp_data;
f03cdb3a 198 struct device_node *root;
ca5de4e6 199 const __be32 *perf_level;
6d434163 200 long rc;
11529396 201
398778f7 202 rc = h_get_ppp(&ppp_data);
11529396
NF
203 if (rc)
204 return;
205
fe333321 206 seq_printf(m, "partition_entitled_capacity=%lld\n",
398778f7
RJ
207 ppp_data.entitlement);
208 seq_printf(m, "group=%d\n", ppp_data.group_num);
209 seq_printf(m, "system_active_processors=%d\n",
210 ppp_data.active_system_procs);
11529396 211
25985edc 212 /* pool related entries are appropriate for shared configs */
eac030b2 213 if (lppaca_shared_proc()) {
11529396
NF
214 unsigned long pool_idle_time, pool_procs;
215
398778f7 216 seq_printf(m, "pool=%d\n", ppp_data.pool_num);
11529396
NF
217
218 /* report pool_capacity in percentage */
398778f7
RJ
219 seq_printf(m, "pool_capacity=%d\n",
220 ppp_data.active_procs_in_pool * 100);
11529396 221
9c74ecfd
SH
222 /* In case h_pic call is not successful, this would result in
223 * APP values being wrong in tools like lparstat.
224 */
225
226 if (h_pic(&pool_idle_time, &pool_procs) == H_SUCCESS) {
227 seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
228 seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
229 seq_printf(m, "boot_pool_idle_time=%ld\n", boot_pool_idle_time);
230 }
11529396
NF
231 }
232
398778f7
RJ
233 seq_printf(m, "unallocated_capacity_weight=%d\n",
234 ppp_data.unallocated_weight);
235 seq_printf(m, "capacity_weight=%d\n", ppp_data.weight);
236 seq_printf(m, "capped=%d\n", ppp_data.capped);
fe333321 237 seq_printf(m, "unallocated_capacity=%lld\n",
398778f7 238 ppp_data.unallocated_entitlement);
f03cdb3a
NF
239
240 /* The last bits of information returned from h_get_ppp are only
241 * valid if the ibm,partition-performance-parameters-level
242 * property is >= 1.
243 */
244 root = of_find_node_by_path("/");
245 if (root) {
246 perf_level = of_get_property(root,
247 "ibm,partition-performance-parameters-level",
248 NULL);
ca5de4e6 249 if (perf_level && (be32_to_cpup(perf_level) >= 1)) {
f03cdb3a
NF
250 seq_printf(m,
251 "physical_procs_allocated_to_virtualization=%d\n",
252 ppp_data.phys_platform_procs);
253 seq_printf(m, "max_proc_capacity_available=%d\n",
254 ppp_data.max_proc_cap_avail);
255 seq_printf(m, "entitled_proc_capacity_available=%d\n",
256 ppp_data.entitled_proc_cap_avail);
257 }
258
259 of_node_put(root);
260 }
1da177e4
LT
261}
262
dfc3403f
NF
263/**
264 * parse_mpp_data
265 * Parse out data returned from h_get_mpp
266 */
267static void parse_mpp_data(struct seq_file *m)
268{
269 struct hvcall_mpp_data mpp_data;
270 int rc;
271
272 rc = h_get_mpp(&mpp_data);
273 if (rc)
274 return;
275
276 seq_printf(m, "entitled_memory=%ld\n", mpp_data.entitled_mem);
277
278 if (mpp_data.mapped_mem != -1)
279 seq_printf(m, "mapped_entitled_memory=%ld\n",
280 mpp_data.mapped_mem);
281
282 seq_printf(m, "entitled_memory_group_number=%d\n", mpp_data.group_num);
283 seq_printf(m, "entitled_memory_pool_number=%d\n", mpp_data.pool_num);
284
285 seq_printf(m, "entitled_memory_weight=%d\n", mpp_data.mem_weight);
286 seq_printf(m, "unallocated_entitled_memory_weight=%d\n",
287 mpp_data.unallocated_mem_weight);
288 seq_printf(m, "unallocated_io_mapping_entitlement=%ld\n",
289 mpp_data.unallocated_entitlement);
290
291 if (mpp_data.pool_size != -1)
292 seq_printf(m, "entitled_memory_pool_size=%ld bytes\n",
293 mpp_data.pool_size);
294
295 seq_printf(m, "entitled_memory_loan_request=%ld\n",
296 mpp_data.loan_request);
297
298 seq_printf(m, "backing_memory=%ld bytes\n", mpp_data.backing_mem);
299}
300
9ee820fa
BK
301/**
302 * parse_mpp_x_data
303 * Parse out data returned from h_get_mpp_x
304 */
305static void parse_mpp_x_data(struct seq_file *m)
306{
307 struct hvcall_mpp_x_data mpp_x_data;
308
309 if (!firmware_has_feature(FW_FEATURE_XCMO))
310 return;
311 if (h_get_mpp_x(&mpp_x_data))
312 return;
313
314 seq_printf(m, "coalesced_bytes=%ld\n", mpp_x_data.coalesced_bytes);
315
316 if (mpp_x_data.pool_coalesced_bytes)
317 seq_printf(m, "pool_coalesced_bytes=%ld\n",
318 mpp_x_data.pool_coalesced_bytes);
319 if (mpp_x_data.pool_purr_cycles)
320 seq_printf(m, "coalesce_pool_purr=%ld\n", mpp_x_data.pool_purr_cycles);
321 if (mpp_x_data.pool_spurr_cycles)
322 seq_printf(m, "coalesce_pool_spurr=%ld\n", mpp_x_data.pool_spurr_cycles);
323}
324
eddaa9a4
LD
325/*
326 * Read the lpar name using the RTAS ibm,get-system-parameter call.
327 *
328 * The name read through this call is updated if changes are made by the end
329 * user on the hypervisor side.
330 *
331 * Some hypervisor (like Qemu) may not provide this value. In that case, a non
332 * null value is returned.
333 */
334static int read_rtas_lpar_name(struct seq_file *m)
335{
fff9846b
NL
336 struct papr_sysparm_buf *buf;
337 int err;
338
339 buf = papr_sysparm_buf_alloc();
340 if (!buf)
eddaa9a4
LD
341 return -ENOMEM;
342
fff9846b
NL
343 err = papr_sysparm_get(PAPR_SYSPARM_LPAR_NAME, buf);
344 if (!err)
345 seq_printf(m, "partition_name=%s\n", buf->val);
346
347 papr_sysparm_buf_free(buf);
348 return err;
eddaa9a4
LD
349}
350
351/*
352 * Read the LPAR name from the Device Tree.
353 *
354 * The value read in the DT is not updated if the end-user is touching the LPAR
355 * name on the hypervisor side.
356 */
357static int read_dt_lpar_name(struct seq_file *m)
358{
2a066ae1 359 struct device_node *root = of_find_node_by_path("/");
eddaa9a4 360 const char *name;
2a066ae1 361 int ret;
eddaa9a4 362
2a066ae1
CL
363 ret = of_property_read_string(root, "ibm,partition-name", &name);
364 of_node_put(root);
365 if (ret)
eddaa9a4
LD
366 return -ENOENT;
367
368 seq_printf(m, "partition_name=%s\n", name);
369 return 0;
370}
371
372static void read_lpar_name(struct seq_file *m)
373{
374 if (read_rtas_lpar_name(m) && read_dt_lpar_name(m))
375 pr_err_once("Error can't get the LPAR name");
376}
377
1da177e4
LT
378#define SPLPAR_MAXLENGTH 1026*(sizeof(char))
379
380/*
381 * parse_system_parameter_string()
382 * Retrieve the potential_processors, max_entitled_capacity and friends
383 * through the get-system-parameter rtas call. Replace keyword strings as
384 * necessary.
385 */
386static void parse_system_parameter_string(struct seq_file *m)
387{
fff9846b 388 struct papr_sysparm_buf *buf;
1da177e4 389
fff9846b
NL
390 buf = papr_sysparm_buf_alloc();
391 if (!buf)
1da177e4 392 return;
1da177e4 393
fff9846b
NL
394 if (papr_sysparm_get(PAPR_SYSPARM_SHARED_PROC_LPAR_ATTRS, buf)) {
395 goto out_free;
1da177e4 396 } else {
fff9846b 397 const char *local_buffer;
1da177e4
LT
398 int splpar_strlen;
399 int idx, w_idx;
dd00cc48 400 char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
fff9846b
NL
401
402 if (!workbuffer)
403 goto out_free;
404
405 splpar_strlen = be16_to_cpu(buf->len);
406 local_buffer = buf->val;
1da177e4 407
1da177e4
LT
408 w_idx = 0;
409 idx = 0;
410 while ((*local_buffer) && (idx < splpar_strlen)) {
411 workbuffer[w_idx++] = local_buffer[idx++];
412 if ((local_buffer[idx] == ',')
413 || (local_buffer[idx] == '\0')) {
414 workbuffer[w_idx] = '\0';
415 if (w_idx) {
416 /* avoid the empty string */
417 seq_printf(m, "%s\n", workbuffer);
418 }
419 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
420 idx++; /* skip the comma */
421 w_idx = 0;
422 } else if (local_buffer[idx] == '=') {
423 /* code here to replace workbuffer contents
424 with different keyword strings */
425 if (0 == strcmp(workbuffer, "MaxEntCap")) {
426 strcpy(workbuffer,
427 "partition_max_entitled_capacity");
428 w_idx = strlen(workbuffer);
429 }
430 if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
431 strcpy(workbuffer,
432 "system_potential_processors");
433 w_idx = strlen(workbuffer);
434 }
435 }
436 }
437 kfree(workbuffer);
438 local_buffer -= 2; /* back up over strlen value */
439 }
fff9846b
NL
440out_free:
441 papr_sysparm_buf_free(buf);
1da177e4
LT
442}
443
1da177e4
LT
444/* Return the number of processors in the system.
445 * This function reads through the device tree and counts
446 * the virtual processors, this does not include threads.
447 */
448static int lparcfg_count_active_processors(void)
449{
9625e69a 450 struct device_node *cpus_dn;
1da177e4
LT
451 int count = 0;
452
9625e69a 453 for_each_node_by_type(cpus_dn, "cpu") {
1da177e4 454#ifdef LPARCFG_DEBUG
8354be9c 455 printk(KERN_ERR "cpus_dn %p\n", cpus_dn);
1da177e4
LT
456#endif
457 count++;
458 }
459 return count;
460}
461
ffa5abbd
BK
462static void pseries_cmo_data(struct seq_file *m)
463{
464 int cpu;
465 unsigned long cmo_faults = 0;
466 unsigned long cmo_fault_time = 0;
467
ac22429d
RJ
468 seq_printf(m, "cmo_enabled=%d\n", firmware_has_feature(FW_FEATURE_CMO));
469
ffa5abbd
BK
470 if (!firmware_has_feature(FW_FEATURE_CMO))
471 return;
472
473 for_each_possible_cpu(cpu) {
7ffcf8ec
AB
474 cmo_faults += be64_to_cpu(lppaca_of(cpu).cmo_faults);
475 cmo_fault_time += be64_to_cpu(lppaca_of(cpu).cmo_fault_time);
ffa5abbd
BK
476 }
477
478 seq_printf(m, "cmo_faults=%lu\n", cmo_faults);
479 seq_printf(m, "cmo_fault_time_usec=%lu\n",
480 cmo_fault_time / tb_ticks_per_usec);
ac22429d
RJ
481 seq_printf(m, "cmo_primary_psp=%d\n", cmo_get_primary_psp());
482 seq_printf(m, "cmo_secondary_psp=%d\n", cmo_get_secondary_psp());
483 seq_printf(m, "cmo_page_size=%lu\n", cmo_get_page_size());
ffa5abbd
BK
484}
485
0559f0a7
AB
486static void splpar_dispatch_data(struct seq_file *m)
487{
488 int cpu;
489 unsigned long dispatches = 0;
490 unsigned long dispatch_dispersions = 0;
491
492 for_each_possible_cpu(cpu) {
7ffcf8ec
AB
493 dispatches += be32_to_cpu(lppaca_of(cpu).yield_count);
494 dispatch_dispersions +=
495 be32_to_cpu(lppaca_of(cpu).dispersion_count);
0559f0a7
AB
496 }
497
498 seq_printf(m, "dispatches=%lu\n", dispatches);
499 seq_printf(m, "dispatch_dispersions=%lu\n", dispatch_dispersions);
500}
501
6fe9d1fa
VS
502static void parse_em_data(struct seq_file *m)
503{
504 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
505
f5f6cbb6
BH
506 if (firmware_has_feature(FW_FEATURE_LPAR) &&
507 plpar_hcall(H_GET_EM_PARMS, retbuf) == H_SUCCESS)
6fe9d1fa
VS
508 seq_printf(m, "power_mode_data=%016lx\n", retbuf[0]);
509}
510
772b039f
AP
511static void maxmem_data(struct seq_file *m)
512{
513 unsigned long maxmem = 0;
514
f1dbc1c5 515 maxmem += (unsigned long)drmem_info->n_lmbs * drmem_info->lmb_size;
772b039f
AP
516 maxmem += hugetlb_total_pages() * PAGE_SIZE;
517
f1dbc1c5 518 seq_printf(m, "MaxMem=%lu\n", maxmem);
772b039f
AP
519}
520
16e9f994 521static int pseries_lparcfg_data(struct seq_file *m, void *v)
1da177e4
LT
522{
523 int partition_potential_processors;
524 int partition_active_processors;
1da177e4 525 struct device_node *rtas_node;
ca5de4e6 526 const __be32 *lrdrp = NULL;
1da177e4 527
8c8dc322 528 rtas_node = of_find_node_by_path("/rtas");
2b9a32ed 529 if (rtas_node)
e2eb6392 530 lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
1da177e4
LT
531
532 if (lrdrp == NULL) {
271c3f35 533 partition_potential_processors = vdso_data->processorCount;
1da177e4 534 } else {
ca5de4e6 535 partition_potential_processors = be32_to_cpup(lrdrp + 4);
1da177e4 536 }
8c8dc322 537 of_node_put(rtas_node);
1da177e4
LT
538
539 partition_active_processors = lparcfg_count_active_processors();
540
1ababe11 541 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
1da177e4 542 /* this call handles the ibm,get-system-parameter contents */
eddaa9a4 543 read_lpar_name(m);
1da177e4 544 parse_system_parameter_string(m);
11529396 545 parse_ppp_data(m);
dfc3403f 546 parse_mpp_data(m);
9ee820fa 547 parse_mpp_x_data(m);
ffa5abbd 548 pseries_cmo_data(m);
0559f0a7 549 splpar_dispatch_data(m);
1da177e4 550
11529396 551 seq_printf(m, "purr=%ld\n", get_purr());
9f3ba362 552 seq_printf(m, "tbr=%ld\n", mftb());
1da177e4
LT
553 } else { /* non SPLPAR case */
554
555 seq_printf(m, "system_active_processors=%d\n",
556 partition_potential_processors);
557
558 seq_printf(m, "system_potential_processors=%d\n",
559 partition_potential_processors);
560
561 seq_printf(m, "partition_max_entitled_capacity=%d\n",
562 partition_potential_processors * 100);
563
564 seq_printf(m, "partition_entitled_capacity=%d\n",
565 partition_active_processors * 100);
566 }
567
5d1bc776
SC
568 show_gpci_data(m);
569
1da177e4
LT
570 seq_printf(m, "partition_active_processors=%d\n",
571 partition_active_processors);
572
573 seq_printf(m, "partition_potential_processors=%d\n",
574 partition_potential_processors);
575
f13c13a0 576 seq_printf(m, "shared_processor_mode=%d\n",
eac030b2 577 lppaca_shared_proc());
1da177e4 578
387e220a 579#ifdef CONFIG_PPC_64S_HASH_MMU
3d3282fd
NP
580 if (!radix_enabled())
581 seq_printf(m, "slb_size=%d\n", mmu_slb_size);
d8c476ee 582#endif
6fe9d1fa 583 parse_em_data(m);
772b039f 584 maxmem_data(m);
6fe9d1fa 585
6ce56e1a
LD
586 seq_printf(m, "security_flavor=%u\n", pseries_security_flavor);
587
1da177e4
LT
588 return 0;
589}
590
11529396
NF
591static ssize_t update_ppp(u64 *entitlement, u8 *weight)
592{
398778f7
RJ
593 struct hvcall_ppp_data ppp_data;
594 u8 new_weight;
11529396
NF
595 u64 new_entitled;
596 ssize_t retval;
597
598 /* Get our current parameters */
398778f7 599 retval = h_get_ppp(&ppp_data);
11529396
NF
600 if (retval)
601 return retval;
602
11529396 603 if (entitlement) {
398778f7 604 new_weight = ppp_data.weight;
11529396
NF
605 new_entitled = *entitlement;
606 } else if (weight) {
607 new_weight = *weight;
398778f7 608 new_entitled = ppp_data.entitlement;
11529396
NF
609 } else
610 return -EINVAL;
611
fe333321 612 pr_debug("%s: current_entitled = %llu, current_weight = %u\n",
5df72bf3 613 __func__, ppp_data.entitlement, ppp_data.weight);
11529396 614
fe333321 615 pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
5df72bf3 616 __func__, new_entitled, new_weight);
11529396
NF
617
618 retval = plpar_hcall_norets(H_SET_PPP, new_entitled, new_weight);
619 return retval;
620}
621
dfc3403f
NF
622/**
623 * update_mpp
624 *
625 * Update the memory entitlement and weight for the partition. Caller must
626 * specify either a new entitlement or weight, not both, to be updated
627 * since the h_set_mpp call takes both entitlement and weight as parameters.
628 */
629static ssize_t update_mpp(u64 *entitlement, u8 *weight)
630{
631 struct hvcall_mpp_data mpp_data;
632 u64 new_entitled;
633 u8 new_weight;
634 ssize_t rc;
635
22e1a4dd
NF
636 if (entitlement) {
637 /* Check with vio to ensure the new memory entitlement
638 * can be handled.
639 */
640 rc = vio_cmo_entitlement_update(*entitlement);
641 if (rc)
642 return rc;
643 }
644
dfc3403f
NF
645 rc = h_get_mpp(&mpp_data);
646 if (rc)
647 return rc;
648
649 if (entitlement) {
650 new_weight = mpp_data.mem_weight;
651 new_entitled = *entitlement;
652 } else if (weight) {
653 new_weight = *weight;
654 new_entitled = mpp_data.entitled_mem;
655 } else
656 return -EINVAL;
657
658 pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
5df72bf3 659 __func__, mpp_data.entitled_mem, mpp_data.mem_weight);
dfc3403f 660
fe333321 661 pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
5df72bf3 662 __func__, new_entitled, new_weight);
dfc3403f
NF
663
664 rc = plpar_hcall_norets(H_SET_MPP, new_entitled, new_weight);
665 return rc;
666}
667
1da177e4
LT
668/*
669 * Interface for changing system parameters (variable capacity weight
670 * and entitled capacity). Format of input is "param_name=value";
671 * anything after value is ignored. Valid parameters at this time are
672 * "partition_entitled_capacity" and "capacity_weight". We use
673 * H_SET_PPP to alter parameters.
674 *
675 * This function should be invoked only on systems with
676 * FW_FEATURE_SPLPAR.
677 */
678static ssize_t lparcfg_write(struct file *file, const char __user * buf,
679 size_t count, loff_t * off)
680{
74422e2b 681 char kbuf[64];
1da177e4
LT
682 char *tmp;
683 u64 new_entitled, *new_entitled_ptr = &new_entitled;
684 u8 new_weight, *new_weight_ptr = &new_weight;
16c14b46 685 ssize_t retval;
1da177e4 686
f5339277 687 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
0524aad7
SR
688 return -EINVAL;
689
74422e2b 690 if (count > sizeof(kbuf))
16c14b46 691 return -EINVAL;
1da177e4 692
1da177e4 693 if (copy_from_user(kbuf, buf, count))
16c14b46 694 return -EFAULT;
1da177e4 695
1da177e4
LT
696 kbuf[count - 1] = '\0';
697 tmp = strchr(kbuf, '=');
698 if (!tmp)
16c14b46 699 return -EINVAL;
1da177e4
LT
700
701 *tmp++ = '\0';
702
703 if (!strcmp(kbuf, "partition_entitled_capacity")) {
704 char *endp;
705 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
706 if (endp == tmp)
16c14b46 707 return -EINVAL;
11529396
NF
708
709 retval = update_ppp(new_entitled_ptr, NULL);
2147783d
HM
710
711 if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
712 /*
713 * The hypervisor assigns VAS resources based
714 * on entitled capacity for shared mode.
715 * Reconfig VAS windows based on DLPAR CPU events.
716 */
717 if (pseries_vas_dlpar_cpu() != 0)
718 retval = H_HARDWARE;
719 }
1da177e4
LT
720 } else if (!strcmp(kbuf, "capacity_weight")) {
721 char *endp;
722 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
723 if (endp == tmp)
16c14b46 724 return -EINVAL;
1da177e4 725
11529396 726 retval = update_ppp(NULL, new_weight_ptr);
dfc3403f
NF
727 } else if (!strcmp(kbuf, "entitled_memory")) {
728 char *endp;
729 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
730 if (endp == tmp)
16c14b46 731 return -EINVAL;
dfc3403f
NF
732
733 retval = update_mpp(new_entitled_ptr, NULL);
734 } else if (!strcmp(kbuf, "entitled_memory_weight")) {
735 char *endp;
736 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
737 if (endp == tmp)
16c14b46 738 return -EINVAL;
dfc3403f
NF
739
740 retval = update_mpp(NULL, new_weight_ptr);
11529396 741 } else
16c14b46 742 return -EINVAL;
1da177e4 743
706c8c93 744 if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
1da177e4 745 retval = count;
706c8c93 746 } else if (retval == H_BUSY) {
1da177e4 747 retval = -EBUSY;
706c8c93 748 } else if (retval == H_HARDWARE) {
1da177e4 749 retval = -EIO;
706c8c93 750 } else if (retval == H_PARAMETER) {
1da177e4 751 retval = -EINVAL;
1da177e4
LT
752 }
753
1da177e4
LT
754 return retval;
755}
756
16e9f994
SR
757static int lparcfg_data(struct seq_file *m, void *v)
758{
759 struct device_node *rootdn;
760 const char *model = "";
761 const char *system_id = "";
762 const char *tmp;
ca5de4e6 763 const __be32 *lp_index_ptr;
a7f67bdf 764 unsigned int lp_index = 0;
16e9f994 765
8354be9c 766 seq_printf(m, "%s %s\n", MODULE_NAME, MODULE_VERS);
16e9f994 767
8c8dc322 768 rootdn = of_find_node_by_path("/");
16e9f994 769 if (rootdn) {
e2eb6392 770 tmp = of_get_property(rootdn, "model", NULL);
f5339277 771 if (tmp)
16e9f994 772 model = tmp;
e2eb6392 773 tmp = of_get_property(rootdn, "system-id", NULL);
f5339277 774 if (tmp)
16e9f994 775 system_id = tmp;
e2eb6392
SR
776 lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
777 NULL);
16e9f994 778 if (lp_index_ptr)
ca5de4e6 779 lp_index = be32_to_cpup(lp_index_ptr);
8c8dc322 780 of_node_put(rootdn);
16e9f994
SR
781 }
782 seq_printf(m, "serial_number=%s\n", system_id);
783 seq_printf(m, "system_type=%s\n", model);
784 seq_printf(m, "partition_id=%d\n", (int)lp_index);
785
16e9f994
SR
786 return pseries_lparcfg_data(m, v);
787}
788
1da177e4
LT
789static int lparcfg_open(struct inode *inode, struct file *file)
790{
791 return single_open(file, lparcfg_data, NULL);
792}
793
97a32539
AD
794static const struct proc_ops lparcfg_proc_ops = {
795 .proc_read = seq_read,
796 .proc_write = lparcfg_write,
797 .proc_open = lparcfg_open,
798 .proc_release = single_release,
799 .proc_lseek = seq_lseek,
1da177e4
LT
800};
801
1c21a293 802static int __init lparcfg_init(void)
1da177e4 803{
57ad583f 804 umode_t mode = 0444;
9c74ecfd 805 long retval;
1da177e4
LT
806
807 /* Allow writing if we have FW_FEATURE_SPLPAR */
f5339277 808 if (firmware_has_feature(FW_FEATURE_SPLPAR))
57ad583f 809 mode |= 0200;
1da177e4 810
97a32539 811 if (!proc_create("powerpc/lparcfg", mode, NULL, &lparcfg_proc_ops)) {
188917e1 812 printk(KERN_ERR "Failed to create powerpc/lparcfg\n");
1da177e4
LT
813 return -EIO;
814 }
9c74ecfd
SH
815
816 /* If this call fails, it would result in APP values
817 * being wrong for since boot reports of lparstat
818 */
819 retval = h_pic(&boot_pool_idle_time, NULL);
820
821 if (retval != H_SUCCESS)
822 pr_debug("H_PIC failed during lparcfg init retval: %ld\n",
823 retval);
824
1da177e4
LT
825 return 0;
826}
f5f6cbb6 827machine_device_initcall(pseries, lparcfg_init);