1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 <asm/uaccess.h>
28#include <asm/iseries/hv_lp_config.h>
29#include <asm/lppaca.h>
30#include <asm/hvcall.h>
31#include <asm/firmware.h>
32#include <asm/rtas.h>
33#include <asm/system.h>
34#include <asm/time.h>
35#include <asm/prom.h>
36#include <asm/vdso_datapage.h>
37
38#define MODULE_VERS "1.7"
39#define MODULE_NAME "lparcfg"
40
41
42
43static struct proc_dir_entry *proc_ppc64_lparcfg;
44#define LPARCFG_BUFF_SIZE 4096
45
46
47
48
49
50static unsigned long get_purr(void)
51{
52 unsigned long sum_purr = 0;
53 int cpu;
54
55 for_each_possible_cpu(cpu) {
56 if (firmware_has_feature(FW_FEATURE_ISERIES))
57 sum_purr += lppaca[cpu].emulated_time_base;
58 else {
59 struct cpu_usage *cu;
60
61 cu = &per_cpu(cpu_usage_array, cpu);
62 sum_purr += cu->current_tb;
63 }
64 }
65 return sum_purr;
66}
67
68#ifdef CONFIG_PPC_ISERIES
69
70
71
72
73static int iseries_lparcfg_data(struct seq_file *m, void *v)
74{
75 unsigned long pool_id;
76 int shared, entitled_capacity, max_entitled_capacity;
77 int processors, max_processors;
78 unsigned long purr = get_purr();
79
80 shared = (int)(local_paca->lppaca_ptr->shared_proc);
81
82 seq_printf(m, "system_active_processors=%d\n",
83 (int)HvLpConfig_getSystemPhysicalProcessors());
84
85 seq_printf(m, "system_potential_processors=%d\n",
86 (int)HvLpConfig_getSystemPhysicalProcessors());
87
88 processors = (int)HvLpConfig_getPhysicalProcessors();
89 seq_printf(m, "partition_active_processors=%d\n", processors);
90
91 max_processors = (int)HvLpConfig_getMaxPhysicalProcessors();
92 seq_printf(m, "partition_potential_processors=%d\n", max_processors);
93
94 if (shared) {
95 entitled_capacity = HvLpConfig_getSharedProcUnits();
96 max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits();
97 } else {
98 entitled_capacity = processors * 100;
99 max_entitled_capacity = max_processors * 100;
100 }
101 seq_printf(m, "partition_entitled_capacity=%d\n", entitled_capacity);
102
103 seq_printf(m, "partition_max_entitled_capacity=%d\n",
104 max_entitled_capacity);
105
106 if (shared) {
107 pool_id = HvLpConfig_getSharedPoolIndex();
108 seq_printf(m, "pool=%d\n", (int)pool_id);
109 seq_printf(m, "pool_capacity=%d\n",
110 (int)(HvLpConfig_getNumProcsInSharedPool(pool_id) *
111 100));
112 seq_printf(m, "purr=%ld\n", purr);
113 }
114
115 seq_printf(m, "shared_processor_mode=%d\n", shared);
116
117 return 0;
118}
119
120#else
121
122static int iseries_lparcfg_data(struct seq_file *m, void *v)
123{
124 return 0;
125}
126
127#endif
128
129#ifdef CONFIG_PPC_PSERIES
130
131
132
133static void log_plpar_hcall_return(unsigned long rc, char *tag)
134{
135 switch(rc) {
136 case 0:
137 return;
138 case H_HARDWARE:
139 printk(KERN_INFO "plpar-hcall (%s) "
140 "Hardware fault\n", tag);
141 return;
142 case H_FUNCTION:
143 printk(KERN_INFO "plpar-hcall (%s) "
144 "Function not allowed\n", tag);
145 return;
146 case H_AUTHORITY:
147 printk(KERN_INFO "plpar-hcall (%s) "
148 "Not authorized to this function\n", tag);
149 return;
150 case H_PARAMETER:
151 printk(KERN_INFO "plpar-hcall (%s) "
152 "Bad parameter(s)\n",tag);
153 return;
154 default:
155 printk(KERN_INFO "plpar-hcall (%s) "
156 "Unexpected rc(0x%lx)\n", tag, rc);
157 }
158}
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180static unsigned int h_get_ppp(unsigned long *entitled,
181 unsigned long *unallocated,
182 unsigned long *aggregation,
183 unsigned long *resource)
184{
185 unsigned long rc;
186 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
187
188 rc = plpar_hcall(H_GET_PPP, retbuf);
189
190 *entitled = retbuf[0];
191 *unallocated = retbuf[1];
192 *aggregation = retbuf[2];
193 *resource = retbuf[3];
194
195 log_plpar_hcall_return(rc, "H_GET_PPP");
196
197 return rc;
198}
199
200static void h_pic(unsigned long *pool_idle_time, unsigned long *num_procs)
201{
202 unsigned long rc;
203 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
204
205 rc = plpar_hcall(H_PIC, retbuf);
206
207 *pool_idle_time = retbuf[0];
208 *num_procs = retbuf[1];
209
210 if (rc != H_AUTHORITY)
211 log_plpar_hcall_return(rc, "H_PIC");
212}
213
214#define SPLPAR_CHARACTERISTICS_TOKEN 20
215#define SPLPAR_MAXLENGTH 1026*(sizeof(char))
216
217
218
219
220
221
222
223static void parse_system_parameter_string(struct seq_file *m)
224{
225 int call_status;
226
227 unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
228 if (!local_buffer) {
229 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
230 __FILE__, __FUNCTION__, __LINE__);
231 return;
232 }
233
234 spin_lock(&rtas_data_buf_lock);
235 memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
236 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
237 NULL,
238 SPLPAR_CHARACTERISTICS_TOKEN,
239 __pa(rtas_data_buf),
240 RTAS_DATA_BUF_SIZE);
241 memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
242 spin_unlock(&rtas_data_buf_lock);
243
244 if (call_status != 0) {
245 printk(KERN_INFO
246 "%s %s Error calling get-system-parameter (0x%x)\n",
247 __FILE__, __FUNCTION__, call_status);
248 } else {
249 int splpar_strlen;
250 int idx, w_idx;
251 char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
252 if (!workbuffer) {
253 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
254 __FILE__, __FUNCTION__, __LINE__);
255 kfree(local_buffer);
256 return;
257 }
258#ifdef LPARCFG_DEBUG
259 printk(KERN_INFO "success calling get-system-parameter \n");
260#endif
261 splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
262 local_buffer += 2;
263
264 w_idx = 0;
265 idx = 0;
266 while ((*local_buffer) && (idx < splpar_strlen)) {
267 workbuffer[w_idx++] = local_buffer[idx++];
268 if ((local_buffer[idx] == ',')
269 || (local_buffer[idx] == '\0')) {
270 workbuffer[w_idx] = '\0';
271 if (w_idx) {
272
273 seq_printf(m, "%s\n", workbuffer);
274 }
275 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
276 idx++;
277 w_idx = 0;
278 } else if (local_buffer[idx] == '=') {
279
280
281 if (0 == strcmp(workbuffer, "MaxEntCap")) {
282 strcpy(workbuffer,
283 "partition_max_entitled_capacity");
284 w_idx = strlen(workbuffer);
285 }
286 if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
287 strcpy(workbuffer,
288 "system_potential_processors");
289 w_idx = strlen(workbuffer);
290 }
291 }
292 }
293 kfree(workbuffer);
294 local_buffer -= 2;
295 }
296 kfree(local_buffer);
297}
298
299
300
301
302
303static int lparcfg_count_active_processors(void)
304{
305 struct device_node *cpus_dn = NULL;
306 int count = 0;
307
308 while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
309#ifdef LPARCFG_DEBUG
310 printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
311#endif
312 count++;
313 }
314 return count;
315}
316
317static int pseries_lparcfg_data(struct seq_file *m, void *v)
318{
319 int partition_potential_processors;
320 int partition_active_processors;
321 struct device_node *rtas_node;
322 const int *lrdrp = NULL;
323
324 rtas_node = of_find_node_by_path("/rtas");
325 if (rtas_node)
326 lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
327
328 if (lrdrp == NULL) {
329 partition_potential_processors = vdso_data->processorCount;
330 } else {
331 partition_potential_processors = *(lrdrp + 4);
332 }
333 of_node_put(rtas_node);
334
335 partition_active_processors = lparcfg_count_active_processors();
336
337 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
338 unsigned long h_entitled, h_unallocated;
339 unsigned long h_aggregation, h_resource;
340 unsigned long pool_idle_time, pool_procs;
341 unsigned long purr;
342
343 h_get_ppp(&h_entitled, &h_unallocated, &h_aggregation,
344 &h_resource);
345
346 seq_printf(m, "R4=0x%lx\n", h_entitled);
347 seq_printf(m, "R5=0x%lx\n", h_unallocated);
348 seq_printf(m, "R6=0x%lx\n", h_aggregation);
349 seq_printf(m, "R7=0x%lx\n", h_resource);
350
351 purr = get_purr();
352
353
354 parse_system_parameter_string(m);
355
356 seq_printf(m, "partition_entitled_capacity=%ld\n", h_entitled);
357
358 seq_printf(m, "group=%ld\n", (h_aggregation >> 2 * 8) & 0xffff);
359
360 seq_printf(m, "system_active_processors=%ld\n",
361 (h_resource >> 0 * 8) & 0xffff);
362
363
364 if (lppaca[0].shared_proc) {
365
366 h_pic(&pool_idle_time, &pool_procs);
367
368 seq_printf(m, "pool=%ld\n",
369 (h_aggregation >> 0 * 8) & 0xffff);
370
371
372 seq_printf(m, "pool_capacity=%ld\n",
373 ((h_resource >> 2 * 8) & 0xffff) * 100);
374
375 seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
376
377 seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
378 }
379
380 seq_printf(m, "unallocated_capacity_weight=%ld\n",
381 (h_resource >> 4 * 8) & 0xFF);
382
383 seq_printf(m, "capacity_weight=%ld\n",
384 (h_resource >> 5 * 8) & 0xFF);
385
386 seq_printf(m, "capped=%ld\n", (h_resource >> 6 * 8) & 0x01);
387
388 seq_printf(m, "unallocated_capacity=%ld\n", h_unallocated);
389
390 seq_printf(m, "purr=%ld\n", purr);
391
392 } else {
393
394 seq_printf(m, "system_active_processors=%d\n",
395 partition_potential_processors);
396
397 seq_printf(m, "system_potential_processors=%d\n",
398 partition_potential_processors);
399
400 seq_printf(m, "partition_max_entitled_capacity=%d\n",
401 partition_potential_processors * 100);
402
403 seq_printf(m, "partition_entitled_capacity=%d\n",
404 partition_active_processors * 100);
405 }
406
407 seq_printf(m, "partition_active_processors=%d\n",
408 partition_active_processors);
409
410 seq_printf(m, "partition_potential_processors=%d\n",
411 partition_potential_processors);
412
413 seq_printf(m, "shared_processor_mode=%d\n", lppaca[0].shared_proc);
414
415 return 0;
416}
417
418
419
420
421
422
423
424
425
426
427
428static ssize_t lparcfg_write(struct file *file, const char __user * buf,
429 size_t count, loff_t * off)
430{
431 char *kbuf;
432 char *tmp;
433 u64 new_entitled, *new_entitled_ptr = &new_entitled;
434 u8 new_weight, *new_weight_ptr = &new_weight;
435
436 unsigned long current_entitled;
437 unsigned long dummy;
438 unsigned long resource;
439 u8 current_weight;
440
441 ssize_t retval = -ENOMEM;
442
443 if (!firmware_has_feature(FW_FEATURE_SPLPAR) ||
444 firmware_has_feature(FW_FEATURE_ISERIES))
445 return -EINVAL;
446
447 kbuf = kmalloc(count, GFP_KERNEL);
448 if (!kbuf)
449 goto out;
450
451 retval = -EFAULT;
452 if (copy_from_user(kbuf, buf, count))
453 goto out;
454
455 retval = -EINVAL;
456 kbuf[count - 1] = '\0';
457 tmp = strchr(kbuf, '=');
458 if (!tmp)
459 goto out;
460
461 *tmp++ = '\0';
462
463 if (!strcmp(kbuf, "partition_entitled_capacity")) {
464 char *endp;
465 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
466 if (endp == tmp)
467 goto out;
468 new_weight_ptr = ¤t_weight;
469 } else if (!strcmp(kbuf, "capacity_weight")) {
470 char *endp;
471 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
472 if (endp == tmp)
473 goto out;
474 new_entitled_ptr = ¤t_entitled;
475 } else
476 goto out;
477
478
479 retval = h_get_ppp(¤t_entitled, &dummy, &dummy, &resource);
480 if (retval) {
481 retval = -EIO;
482 goto out;
483 }
484
485 current_weight = (resource >> 5 * 8) & 0xFF;
486
487 pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
488 __FUNCTION__, current_entitled, current_weight);
489
490 pr_debug("%s: new_entitled = %lu, new_weight = %u\n",
491 __FUNCTION__, *new_entitled_ptr, *new_weight_ptr);
492
493 retval = plpar_hcall_norets(H_SET_PPP, *new_entitled_ptr,
494 *new_weight_ptr);
495
496 if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
497 retval = count;
498 } else if (retval == H_BUSY) {
499 retval = -EBUSY;
500 } else if (retval == H_HARDWARE) {
501 retval = -EIO;
502 } else if (retval == H_PARAMETER) {
503 retval = -EINVAL;
504 } else {
505 printk(KERN_WARNING "%s: received unknown hv return code %ld",
506 __FUNCTION__, retval);
507 retval = -EIO;
508 }
509
510out:
511 kfree(kbuf);
512 return retval;
513}
514
515#else
516
517static int pseries_lparcfg_data(struct seq_file *m, void *v)
518{
519 return 0;
520}
521
522static ssize_t lparcfg_write(struct file *file, const char __user * buf,
523 size_t count, loff_t * off)
524{
525 return -EINVAL;
526}
527
528#endif
529
530static int lparcfg_data(struct seq_file *m, void *v)
531{
532 struct device_node *rootdn;
533 const char *model = "";
534 const char *system_id = "";
535 const char *tmp;
536 const unsigned int *lp_index_ptr;
537 unsigned int lp_index = 0;
538
539 seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
540
541 rootdn = of_find_node_by_path("/");
542 if (rootdn) {
543 tmp = of_get_property(rootdn, "model", NULL);
544 if (tmp) {
545 model = tmp;
546
547 if (firmware_has_feature(FW_FEATURE_ISERIES))
548 model += 4;
549 }
550 tmp = of_get_property(rootdn, "system-id", NULL);
551 if (tmp) {
552 system_id = tmp;
553
554 if (firmware_has_feature(FW_FEATURE_ISERIES))
555 system_id += 4;
556 }
557 lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
558 NULL);
559 if (lp_index_ptr)
560 lp_index = *lp_index_ptr;
561 of_node_put(rootdn);
562 }
563 seq_printf(m, "serial_number=%s\n", system_id);
564 seq_printf(m, "system_type=%s\n", model);
565 seq_printf(m, "partition_id=%d\n", (int)lp_index);
566
567 if (firmware_has_feature(FW_FEATURE_ISERIES))
568 return iseries_lparcfg_data(m, v);
569 return pseries_lparcfg_data(m, v);
570}
571
572static int lparcfg_open(struct inode *inode, struct file *file)
573{
574 return single_open(file, lparcfg_data, NULL);
575}
576
577const struct file_operations lparcfg_fops = {
578 .owner = THIS_MODULE,
579 .read = seq_read,
580 .write = lparcfg_write,
581 .open = lparcfg_open,
582 .release = single_release,
583};
584
585int __init lparcfg_init(void)
586{
587 struct proc_dir_entry *ent;
588 mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
589
590
591 if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
592 !firmware_has_feature(FW_FEATURE_ISERIES))
593 mode |= S_IWUSR;
594
595 ent = create_proc_entry("ppc64/lparcfg", mode, NULL);
596 if (ent) {
597 ent->proc_fops = &lparcfg_fops;
598 ent->data = kmalloc(LPARCFG_BUFF_SIZE, GFP_KERNEL);
599 if (!ent->data) {
600 printk(KERN_ERR
601 "Failed to allocate buffer for lparcfg\n");
602 remove_proc_entry("lparcfg", ent->parent);
603 return -ENOMEM;
604 }
605 } else {
606 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
607 return -EIO;
608 }
609
610 proc_ppc64_lparcfg = ent;
611 return 0;
612}
613
614void __exit lparcfg_cleanup(void)
615{
616 if (proc_ppc64_lparcfg) {
617 kfree(proc_ppc64_lparcfg->data);
618 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
619 }
620}
621
622module_init(lparcfg_init);
623module_exit(lparcfg_cleanup);
624MODULE_DESCRIPTION("Interface for LPAR configuration data");
625MODULE_AUTHOR("Dave Engebretsen");
626MODULE_LICENSE("GPL");
627