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