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