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