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