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