vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc64 / kernel / lparcfg.c
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/config.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/proc_fs.h>
26 #include <linux/init.h>
27 #include <linux/seq_file.h>
28 #include <asm/uaccess.h>
29 #include <asm/iSeries/HvLpConfig.h>
30 #include <asm/lppaca.h>
31 #include <asm/iSeries/LparData.h>
32 #include <asm/hvcall.h>
33 #include <asm/cputable.h>
34 #include <asm/rtas.h>
35 #include <asm/system.h>
36
37 #define MODULE_VERS "1.5"
38 #define MODULE_NAME "lparcfg"
39
40 /* #define LPARCFG_DEBUG */
41
42 /* find a better place for this function... */
43 void log_plpar_hcall_return(unsigned long rc, char *tag)
44 {
45         if (rc == 0)            /* success, return */
46                 return;
47 /* check for null tag ? */
48         if (rc == H_Hardware)
49                 printk(KERN_INFO
50                        "plpar-hcall (%s) failed with hardware fault\n", tag);
51         else if (rc == H_Function)
52                 printk(KERN_INFO
53                        "plpar-hcall (%s) failed; function not allowed\n", tag);
54         else if (rc == H_Authority)
55                 printk(KERN_INFO
56                        "plpar-hcall (%s) failed; not authorized to this function\n",
57                        tag);
58         else if (rc == H_Parameter)
59                 printk(KERN_INFO "plpar-hcall (%s) failed; Bad parameter(s)\n",
60                        tag);
61         else
62                 printk(KERN_INFO
63                        "plpar-hcall (%s) failed with unexpected rc(0x%lx)\n",
64                        tag, rc);
65
66 }
67
68 static struct proc_dir_entry *proc_ppc64_lparcfg;
69 #define LPARCFG_BUFF_SIZE 4096
70
71 #ifdef CONFIG_PPC_ISERIES
72
73 /*
74  * For iSeries legacy systems, the PPA purr function is available from the
75  * emulated_time_base field in the paca.
76  */
77 static unsigned long get_purr(void)
78 {
79         unsigned long sum_purr = 0;
80         int cpu;
81         struct paca_struct *lpaca;
82
83         for_each_cpu(cpu) {
84                 lpaca = paca + cpu;
85                 sum_purr += lpaca->lppaca.emulated_time_base;
86
87 #ifdef PURR_DEBUG
88                 printk(KERN_INFO "get_purr for cpu (%d) has value (%ld) \n",
89                         cpu, lpaca->lppaca.emulated_time_base);
90 #endif
91         }
92         return sum_purr;
93 }
94
95 #define lparcfg_write NULL
96
97 /* 
98  * Methods used to fetch LPAR data when running on an iSeries platform.
99  */
100 static int lparcfg_data(struct seq_file *m, void *v)
101 {
102         unsigned long pool_id, lp_index;
103         int shared, entitled_capacity, max_entitled_capacity;
104         int processors, max_processors;
105         struct paca_struct *lpaca = get_paca();
106         unsigned long purr = get_purr();
107
108         seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
109
110         shared = (int)(lpaca->lppaca_ptr->shared_proc);
111         seq_printf(m, "serial_number=%c%c%c%c%c%c%c\n",
112                    e2a(xItExtVpdPanel.mfgID[2]),
113                    e2a(xItExtVpdPanel.mfgID[3]),
114                    e2a(xItExtVpdPanel.systemSerial[1]),
115                    e2a(xItExtVpdPanel.systemSerial[2]),
116                    e2a(xItExtVpdPanel.systemSerial[3]),
117                    e2a(xItExtVpdPanel.systemSerial[4]),
118                    e2a(xItExtVpdPanel.systemSerial[5]));
119
120         seq_printf(m, "system_type=%c%c%c%c\n",
121                    e2a(xItExtVpdPanel.machineType[0]),
122                    e2a(xItExtVpdPanel.machineType[1]),
123                    e2a(xItExtVpdPanel.machineType[2]),
124                    e2a(xItExtVpdPanel.machineType[3]));
125
126         lp_index = HvLpConfig_getLpIndex();
127         seq_printf(m, "partition_id=%d\n", (int)lp_index);
128
129         seq_printf(m, "system_active_processors=%d\n",
130                    (int)HvLpConfig_getSystemPhysicalProcessors());
131
132         seq_printf(m, "system_potential_processors=%d\n",
133                    (int)HvLpConfig_getSystemPhysicalProcessors());
134
135         processors = (int)HvLpConfig_getPhysicalProcessors();
136         seq_printf(m, "partition_active_processors=%d\n", processors);
137
138         max_processors = (int)HvLpConfig_getMaxPhysicalProcessors();
139         seq_printf(m, "partition_potential_processors=%d\n", max_processors);
140
141         if (shared) {
142                 entitled_capacity = HvLpConfig_getSharedProcUnits();
143                 max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits();
144         } else {
145                 entitled_capacity = processors * 100;
146                 max_entitled_capacity = max_processors * 100;
147         }
148         seq_printf(m, "partition_entitled_capacity=%d\n", entitled_capacity);
149
150         seq_printf(m, "partition_max_entitled_capacity=%d\n",
151                    max_entitled_capacity);
152
153         if (shared) {
154                 pool_id = HvLpConfig_getSharedPoolIndex();
155                 seq_printf(m, "pool=%d\n", (int)pool_id);
156                 seq_printf(m, "pool_capacity=%d\n",
157                            (int)(HvLpConfig_getNumProcsInSharedPool(pool_id) *
158                                  100));
159                 seq_printf(m, "purr=%ld\n", purr);
160         }
161
162         seq_printf(m, "shared_processor_mode=%d\n", shared);
163
164         return 0;
165 }
166 #endif                          /* CONFIG_PPC_ISERIES */
167
168 #ifdef CONFIG_PPC_PSERIES
169 /* 
170  * Methods used to fetch LPAR data when running on a pSeries platform.
171  */
172
173 /*
174  * H_GET_PPP hcall returns info in 4 parms.
175  *  entitled_capacity,unallocated_capacity,
176  *  aggregation, resource_capability).
177  *
178  *  R4 = Entitled Processor Capacity Percentage. 
179  *  R5 = Unallocated Processor Capacity Percentage.
180  *  R6 (AABBCCDDEEFFGGHH).
181  *      XXXX - reserved (0)
182  *          XXXX - reserved (0)
183  *              XXXX - Group Number
184  *                  XXXX - Pool Number.
185  *  R7 (IIJJKKLLMMNNOOPP).
186  *      XX - reserved. (0)
187  *        XX - bit 0-6 reserved (0).   bit 7 is Capped indicator.
188  *          XX - variable processor Capacity Weight
189  *            XX - Unallocated Variable Processor Capacity Weight.
190  *              XXXX - Active processors in Physical Processor Pool.
191  *                  XXXX  - Processors active on platform. 
192  */
193 static unsigned int h_get_ppp(unsigned long *entitled,
194                               unsigned long *unallocated,
195                               unsigned long *aggregation,
196                               unsigned long *resource)
197 {
198         unsigned long rc;
199         rc = plpar_hcall_4out(H_GET_PPP, 0, 0, 0, 0, entitled, unallocated,
200                               aggregation, resource);
201
202         log_plpar_hcall_return(rc, "H_GET_PPP");
203
204         return rc;
205 }
206
207 static void h_pic(unsigned long *pool_idle_time, unsigned long *num_procs)
208 {
209         unsigned long rc;
210         unsigned long dummy;
211         rc = plpar_hcall(H_PIC, 0, 0, 0, 0, pool_idle_time, num_procs, &dummy);
212
213         log_plpar_hcall_return(rc, "H_PIC");
214 }
215
216 static unsigned long get_purr(void);
217 /* ToDo:  get sum of purr across all processors.  The purr collection code
218  * is coming, but at this time is still problematic, so for now this
219  * function will return 0.
220  */
221 static unsigned long get_purr(void)
222 {
223         unsigned long sum_purr = 0;
224         return sum_purr;
225 }
226
227 #define SPLPAR_CHARACTERISTICS_TOKEN 20
228 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
229
230 /*
231  * parse_system_parameter_string()
232  * Retrieve the potential_processors, max_entitled_capacity and friends
233  * through the get-system-parameter rtas call.  Replace keyword strings as
234  * necessary.
235  */
236 static void parse_system_parameter_string(struct seq_file *m)
237 {
238         int call_status;
239
240         char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
241         if (!local_buffer) {
242                 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
243                        __FILE__, __FUNCTION__, __LINE__);
244                 return;
245         }
246
247         spin_lock(&rtas_data_buf_lock);
248         memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
249         call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
250                                 NULL,
251                                 SPLPAR_CHARACTERISTICS_TOKEN,
252                                 __pa(rtas_data_buf));
253         memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
254         spin_unlock(&rtas_data_buf_lock);
255
256         if (call_status != 0) {
257                 printk(KERN_INFO
258                        "%s %s Error calling get-system-parameter (0x%x)\n",
259                        __FILE__, __FUNCTION__, call_status);
260         } else {
261                 int splpar_strlen;
262                 int idx, w_idx;
263                 char *workbuffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
264                 if (!workbuffer) {
265                         printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
266                                __FILE__, __FUNCTION__, __LINE__);
267                         return;
268                 }
269 #ifdef LPARCFG_DEBUG
270                 printk(KERN_INFO "success calling get-system-parameter \n");
271 #endif
272                 splpar_strlen = local_buffer[0] * 16 + local_buffer[1];
273                 local_buffer += 2;      /* step over strlen value */
274
275                 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
276                 w_idx = 0;
277                 idx = 0;
278                 while ((*local_buffer) && (idx < splpar_strlen)) {
279                         workbuffer[w_idx++] = local_buffer[idx++];
280                         if ((local_buffer[idx] == ',')
281                             || (local_buffer[idx] == '\0')) {
282                                 workbuffer[w_idx] = '\0';
283                                 if (w_idx) {
284                                         /* avoid the empty string */
285                                         seq_printf(m, "%s\n", workbuffer);
286                                 }
287                                 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
288                                 idx++;  /* skip the comma */
289                                 w_idx = 0;
290                         } else if (local_buffer[idx] == '=') {
291                                 /* code here to replace workbuffer contents
292                                    with different keyword strings */
293                                 if (0 == strcmp(workbuffer, "MaxEntCap")) {
294                                         strcpy(workbuffer,
295                                                "partition_max_entitled_capacity");
296                                         w_idx = strlen(workbuffer);
297                                 }
298                                 if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
299                                         strcpy(workbuffer,
300                                                "system_potential_processors");
301                                         w_idx = strlen(workbuffer);
302                                 }
303                         }
304                 }
305                 kfree(workbuffer);
306                 local_buffer -= 2;      /* back up over strlen value */
307         }
308         kfree(local_buffer);
309 }
310
311 static int lparcfg_count_active_processors(void);
312
313 /* Return the number of processors in the system.
314  * This function reads through the device tree and counts
315  * the virtual processors, this does not include threads.
316  */
317 static int lparcfg_count_active_processors(void)
318 {
319         struct device_node *cpus_dn = NULL;
320         int count = 0;
321
322         while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
323 #ifdef LPARCFG_DEBUG
324                 printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
325 #endif
326                 count++;
327         }
328         return count;
329 }
330
331 static int lparcfg_data(struct seq_file *m, void *v)
332 {
333         int partition_potential_processors;
334         int partition_active_processors;
335         struct device_node *rootdn;
336         const char *model = "";
337         const char *system_id = "";
338         unsigned int *lp_index_ptr, lp_index = 0;
339         struct device_node *rtas_node;
340         int *lrdrp;
341
342         rootdn = find_path_device("/");
343         if (rootdn) {
344                 model = get_property(rootdn, "model", NULL);
345                 system_id = get_property(rootdn, "system-id", NULL);
346                 lp_index_ptr = (unsigned int *)
347                     get_property(rootdn, "ibm,partition-no", NULL);
348                 if (lp_index_ptr)
349                         lp_index = *lp_index_ptr;
350         }
351
352         seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
353
354         seq_printf(m, "serial_number=%s\n", system_id);
355
356         seq_printf(m, "system_type=%s\n", model);
357
358         seq_printf(m, "partition_id=%d\n", (int)lp_index);
359
360         rtas_node = find_path_device("/rtas");
361         lrdrp = (int *)get_property(rtas_node, "ibm,lrdr-capacity", NULL);
362
363         if (lrdrp == NULL) {
364                 partition_potential_processors = systemcfg->processorCount;
365         } else {
366                 partition_potential_processors = *(lrdrp + 4);
367         }
368
369         partition_active_processors = lparcfg_count_active_processors();
370
371         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
372                 unsigned long h_entitled, h_unallocated;
373                 unsigned long h_aggregation, h_resource;
374                 unsigned long pool_idle_time, pool_procs;
375                 unsigned long purr;
376
377                 h_get_ppp(&h_entitled, &h_unallocated, &h_aggregation,
378                           &h_resource);
379
380                 seq_printf(m, "R4=0x%lx\n", h_entitled);
381                 seq_printf(m, "R5=0x%lx\n", h_unallocated);
382                 seq_printf(m, "R6=0x%lx\n", h_aggregation);
383                 seq_printf(m, "R7=0x%lx\n", h_resource);
384
385                 purr = get_purr();
386
387                 /* this call handles the ibm,get-system-parameter contents */
388                 parse_system_parameter_string(m);
389
390                 seq_printf(m, "partition_entitled_capacity=%ld\n", h_entitled);
391
392                 seq_printf(m, "group=%ld\n", (h_aggregation >> 2 * 8) & 0xffff);
393
394                 seq_printf(m, "system_active_processors=%ld\n",
395                            (h_resource >> 0 * 8) & 0xffff);
396
397                 /* pool related entries are apropriate for shared configs */
398                 if (paca[0].lppaca.shared_proc) {
399
400                         h_pic(&pool_idle_time, &pool_procs);
401
402                         seq_printf(m, "pool=%ld\n",
403                                    (h_aggregation >> 0 * 8) & 0xffff);
404
405                         /* report pool_capacity in percentage */
406                         seq_printf(m, "pool_capacity=%ld\n",
407                                    ((h_resource >> 2 * 8) & 0xffff) * 100);
408
409                         seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
410
411                         seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
412                 }
413
414                 seq_printf(m, "unallocated_capacity_weight=%ld\n",
415                            (h_resource >> 4 * 8) & 0xFF);
416
417                 seq_printf(m, "capacity_weight=%ld\n",
418                            (h_resource >> 5 * 8) & 0xFF);
419
420                 seq_printf(m, "capped=%ld\n", (h_resource >> 6 * 8) & 0x01);
421
422                 seq_printf(m, "unallocated_capacity=%ld\n", h_unallocated);
423
424                 seq_printf(m, "purr=%ld\n", purr);
425
426         } else {                /* non SPLPAR case */
427
428                 seq_printf(m, "system_active_processors=%d\n",
429                            partition_potential_processors);
430
431                 seq_printf(m, "system_potential_processors=%d\n",
432                            partition_potential_processors);
433
434                 seq_printf(m, "partition_max_entitled_capacity=%d\n",
435                            partition_potential_processors * 100);
436
437                 seq_printf(m, "partition_entitled_capacity=%d\n",
438                            partition_active_processors * 100);
439         }
440
441         seq_printf(m, "partition_active_processors=%d\n",
442                    partition_active_processors);
443
444         seq_printf(m, "partition_potential_processors=%d\n",
445                    partition_potential_processors);
446
447         seq_printf(m, "shared_processor_mode=%d\n", paca[0].lppaca.shared_proc);
448
449         return 0;
450 }
451
452 /*
453  * Interface for changing system parameters (variable capacity weight
454  * and entitled capacity).  Format of input is "param_name=value";
455  * anything after value is ignored.  Valid parameters at this time are
456  * "partition_entitled_capacity" and "capacity_weight".  We use
457  * H_SET_PPP to alter parameters.
458  *
459  * This function should be invoked only on systems with
460  * FW_FEATURE_SPLPAR.
461  */
462 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
463                              size_t count, loff_t * off)
464 {
465         char *kbuf;
466         char *tmp;
467         u64 new_entitled, *new_entitled_ptr = &new_entitled;
468         u8 new_weight, *new_weight_ptr = &new_weight;
469
470         unsigned long current_entitled; /* parameters for h_get_ppp */
471         unsigned long dummy;
472         unsigned long resource;
473         u8 current_weight;
474
475         ssize_t retval = -ENOMEM;
476
477         kbuf = kmalloc(count, GFP_KERNEL);
478         if (!kbuf)
479                 goto out;
480
481         retval = -EFAULT;
482         if (copy_from_user(kbuf, buf, count))
483                 goto out;
484
485         retval = -EINVAL;
486         kbuf[count - 1] = '\0';
487         tmp = strchr(kbuf, '=');
488         if (!tmp)
489                 goto out;
490
491         *tmp++ = '\0';
492
493         if (!strcmp(kbuf, "partition_entitled_capacity")) {
494                 char *endp;
495                 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
496                 if (endp == tmp)
497                         goto out;
498                 new_weight_ptr = &current_weight;
499         } else if (!strcmp(kbuf, "capacity_weight")) {
500                 char *endp;
501                 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
502                 if (endp == tmp)
503                         goto out;
504                 new_entitled_ptr = &current_entitled;
505         } else
506                 goto out;
507
508         /* Get our current parameters */
509         retval = h_get_ppp(&current_entitled, &dummy, &dummy, &resource);
510         if (retval) {
511                 retval = -EIO;
512                 goto out;
513         }
514
515         current_weight = (resource >> 5 * 8) & 0xFF;
516
517         pr_debug("%s: current_entitled = %lu, current_weight = %lu\n",
518                  __FUNCTION__, current_entitled, current_weight);
519
520         pr_debug("%s: new_entitled = %lu, new_weight = %lu\n",
521                  __FUNCTION__, *new_entitled_ptr, *new_weight_ptr);
522
523         retval = plpar_hcall_norets(H_SET_PPP, *new_entitled_ptr,
524                                     *new_weight_ptr);
525
526         if (retval == H_Success || retval == H_Constrained) {
527                 retval = count;
528         } else if (retval == H_Busy) {
529                 retval = -EBUSY;
530         } else if (retval == H_Hardware) {
531                 retval = -EIO;
532         } else if (retval == H_Parameter) {
533                 retval = -EINVAL;
534         } else {
535                 printk(KERN_WARNING "%s: received unknown hv return code %ld",
536                        __FUNCTION__, retval);
537                 retval = -EIO;
538         }
539
540       out:
541         kfree(kbuf);
542         return retval;
543 }
544
545 #endif                          /* CONFIG_PPC_PSERIES */
546
547 static int lparcfg_open(struct inode *inode, struct file *file)
548 {
549         return single_open(file, lparcfg_data, NULL);
550 }
551
552 struct file_operations lparcfg_fops = {
553       .owner    = THIS_MODULE,
554       .read     = seq_read,
555       .open     = lparcfg_open,
556       .release  = single_release,
557 };
558
559 int __init lparcfg_init(void)
560 {
561         struct proc_dir_entry *ent;
562         mode_t mode = S_IRUSR;
563
564         /* Allow writing if we have FW_FEATURE_SPLPAR */
565         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
566                 lparcfg_fops.write = lparcfg_write;
567                 mode |= S_IWUSR;
568         }
569
570         ent = create_proc_entry("ppc64/lparcfg", mode, NULL);
571         if (ent) {
572                 ent->proc_fops = &lparcfg_fops;
573                 ent->data = kmalloc(LPARCFG_BUFF_SIZE, GFP_KERNEL);
574                 if (!ent->data) {
575                         printk(KERN_ERR
576                                "Failed to allocate buffer for lparcfg\n");
577                         remove_proc_entry("lparcfg", ent->parent);
578                         return -ENOMEM;
579                 }
580         } else {
581                 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
582                 return -EIO;
583         }
584
585         proc_ppc64_lparcfg = ent;
586         return 0;
587 }
588
589 void __exit lparcfg_cleanup(void)
590 {
591         if (proc_ppc64_lparcfg) {
592                 if (proc_ppc64_lparcfg->data) {
593                         kfree(proc_ppc64_lparcfg->data);
594                 }
595                 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
596         }
597 }
598
599 module_init(lparcfg_init);
600 module_exit(lparcfg_cleanup);
601 MODULE_DESCRIPTION("Interface for LPAR configuration data");
602 MODULE_AUTHOR("Dave Engebretsen");
603 MODULE_LICENSE("GPL");