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