powerpc: Display processor virtualization resource allocs in lparcfg
[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 #include <asm/vio.h>
38
39 #define MODULE_VERS "1.8"
40 #define MODULE_NAME "lparcfg"
41
42 /* #define LPARCFG_DEBUG */
43
44 static struct proc_dir_entry *proc_ppc64_lparcfg;
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)(local_paca->lppaca_ptr->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 /**
134  * h_get_mpp
135  * H_GET_MPP hcall returns info in 7 parms
136  */
137 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
138 {
139         int rc;
140         unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
141
142         rc = plpar_hcall9(H_GET_MPP, retbuf);
143
144         mpp_data->entitled_mem = retbuf[0];
145         mpp_data->mapped_mem = retbuf[1];
146
147         mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
148         mpp_data->pool_num = retbuf[2] & 0xffff;
149
150         mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
151         mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
152         mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffff;
153
154         mpp_data->pool_size = retbuf[4];
155         mpp_data->loan_request = retbuf[5];
156         mpp_data->backing_mem = retbuf[6];
157
158         return rc;
159 }
160 EXPORT_SYMBOL(h_get_mpp);
161
162 struct hvcall_ppp_data {
163         u64     entitlement;
164         u64     unallocated_entitlement;
165         u16     group_num;
166         u16     pool_num;
167         u8      capped;
168         u8      weight;
169         u8      unallocated_weight;
170         u16     active_procs_in_pool;
171         u16     active_system_procs;
172         u16     phys_platform_procs;
173         u32     max_proc_cap_avail;
174         u32     entitled_proc_cap_avail;
175 };
176
177 /*
178  * H_GET_PPP hcall returns info in 4 parms.
179  *  entitled_capacity,unallocated_capacity,
180  *  aggregation, resource_capability).
181  *
182  *  R4 = Entitled Processor Capacity Percentage.
183  *  R5 = Unallocated Processor Capacity Percentage.
184  *  R6 (AABBCCDDEEFFGGHH).
185  *      XXXX - reserved (0)
186  *          XXXX - reserved (0)
187  *              XXXX - Group Number
188  *                  XXXX - Pool Number.
189  *  R7 (IIJJKKLLMMNNOOPP).
190  *      XX - reserved. (0)
191  *        XX - bit 0-6 reserved (0).   bit 7 is Capped indicator.
192  *          XX - variable processor Capacity Weight
193  *            XX - Unallocated Variable Processor Capacity Weight.
194  *              XXXX - Active processors in Physical Processor Pool.
195  *                  XXXX  - Processors active on platform.
196  *  R8 (QQQQRRRRRRSSSSSS). if ibm,partition-performance-parameters-level >= 1
197  *      XXXX - Physical platform procs allocated to virtualization.
198  *          XXXXXX - Max procs capacity % available to the partitions pool.
199  *                XXXXXX - Entitled procs capacity % available to the
200  *                         partitions pool.
201  */
202 static unsigned int h_get_ppp(struct hvcall_ppp_data *ppp_data)
203 {
204         unsigned long rc;
205         unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
206
207         rc = plpar_hcall9(H_GET_PPP, retbuf);
208
209         ppp_data->entitlement = retbuf[0];
210         ppp_data->unallocated_entitlement = retbuf[1];
211
212         ppp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
213         ppp_data->pool_num = retbuf[2] & 0xffff;
214
215         ppp_data->capped = (retbuf[3] >> 6 * 8) & 0x01;
216         ppp_data->weight = (retbuf[3] >> 5 * 8) & 0xff;
217         ppp_data->unallocated_weight = (retbuf[3] >> 4 * 8) & 0xff;
218         ppp_data->active_procs_in_pool = (retbuf[3] >> 2 * 8) & 0xffff;
219         ppp_data->active_system_procs = retbuf[3] & 0xffff;
220
221         ppp_data->phys_platform_procs = retbuf[4] >> 6 * 8;
222         ppp_data->max_proc_cap_avail = (retbuf[4] >> 3 * 8) & 0xffffff;
223         ppp_data->entitled_proc_cap_avail = retbuf[4] & 0xffffff;
224
225         return rc;
226 }
227
228 static unsigned h_pic(unsigned long *pool_idle_time,
229                       unsigned long *num_procs)
230 {
231         unsigned long rc;
232         unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
233
234         rc = plpar_hcall(H_PIC, retbuf);
235
236         *pool_idle_time = retbuf[0];
237         *num_procs = retbuf[1];
238
239         return rc;
240 }
241
242 /*
243  * parse_ppp_data
244  * Parse out the data returned from h_get_ppp and h_pic
245  */
246 static void parse_ppp_data(struct seq_file *m)
247 {
248         struct hvcall_ppp_data ppp_data;
249         struct device_node *root;
250         const int *perf_level;
251         int rc;
252
253         rc = h_get_ppp(&ppp_data);
254         if (rc)
255                 return;
256
257         seq_printf(m, "partition_entitled_capacity=%lld\n",
258                    ppp_data.entitlement);
259         seq_printf(m, "group=%d\n", ppp_data.group_num);
260         seq_printf(m, "system_active_processors=%d\n",
261                    ppp_data.active_system_procs);
262
263         /* pool related entries are apropriate for shared configs */
264         if (lppaca[0].shared_proc) {
265                 unsigned long pool_idle_time, pool_procs;
266
267                 seq_printf(m, "pool=%d\n", ppp_data.pool_num);
268
269                 /* report pool_capacity in percentage */
270                 seq_printf(m, "pool_capacity=%d\n",
271                            ppp_data.active_procs_in_pool * 100);
272
273                 h_pic(&pool_idle_time, &pool_procs);
274                 seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
275                 seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
276         }
277
278         seq_printf(m, "unallocated_capacity_weight=%d\n",
279                    ppp_data.unallocated_weight);
280         seq_printf(m, "capacity_weight=%d\n", ppp_data.weight);
281         seq_printf(m, "capped=%d\n", ppp_data.capped);
282         seq_printf(m, "unallocated_capacity=%lld\n",
283                    ppp_data.unallocated_entitlement);
284
285         /* The last bits of information returned from h_get_ppp are only
286          * valid if the ibm,partition-performance-parameters-level
287          * property is >= 1.
288          */
289         root = of_find_node_by_path("/");
290         if (root) {
291                 perf_level = of_get_property(root,
292                                 "ibm,partition-performance-parameters-level",
293                                              NULL);
294                 if (perf_level && (*perf_level >= 1)) {
295                         seq_printf(m,
296                             "physical_procs_allocated_to_virtualization=%d\n",
297                                    ppp_data.phys_platform_procs);
298                         seq_printf(m, "max_proc_capacity_available=%d\n",
299                                    ppp_data.max_proc_cap_avail);
300                         seq_printf(m, "entitled_proc_capacity_available=%d\n",
301                                    ppp_data.entitled_proc_cap_avail);
302                 }
303
304                 of_node_put(root);
305         }
306 }
307
308 /**
309  * parse_mpp_data
310  * Parse out data returned from h_get_mpp
311  */
312 static void parse_mpp_data(struct seq_file *m)
313 {
314         struct hvcall_mpp_data mpp_data;
315         int rc;
316
317         rc = h_get_mpp(&mpp_data);
318         if (rc)
319                 return;
320
321         seq_printf(m, "entitled_memory=%ld\n", mpp_data.entitled_mem);
322
323         if (mpp_data.mapped_mem != -1)
324                 seq_printf(m, "mapped_entitled_memory=%ld\n",
325                            mpp_data.mapped_mem);
326
327         seq_printf(m, "entitled_memory_group_number=%d\n", mpp_data.group_num);
328         seq_printf(m, "entitled_memory_pool_number=%d\n", mpp_data.pool_num);
329
330         seq_printf(m, "entitled_memory_weight=%d\n", mpp_data.mem_weight);
331         seq_printf(m, "unallocated_entitled_memory_weight=%d\n",
332                    mpp_data.unallocated_mem_weight);
333         seq_printf(m, "unallocated_io_mapping_entitlement=%ld\n",
334                    mpp_data.unallocated_entitlement);
335
336         if (mpp_data.pool_size != -1)
337                 seq_printf(m, "entitled_memory_pool_size=%ld bytes\n",
338                            mpp_data.pool_size);
339
340         seq_printf(m, "entitled_memory_loan_request=%ld\n",
341                    mpp_data.loan_request);
342
343         seq_printf(m, "backing_memory=%ld bytes\n", mpp_data.backing_mem);
344 }
345
346 #define SPLPAR_CHARACTERISTICS_TOKEN 20
347 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
348
349 /*
350  * parse_system_parameter_string()
351  * Retrieve the potential_processors, max_entitled_capacity and friends
352  * through the get-system-parameter rtas call.  Replace keyword strings as
353  * necessary.
354  */
355 static void parse_system_parameter_string(struct seq_file *m)
356 {
357         int call_status;
358
359         unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
360         if (!local_buffer) {
361                 printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
362                        __FILE__, __func__, __LINE__);
363                 return;
364         }
365
366         spin_lock(&rtas_data_buf_lock);
367         memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
368         call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
369                                 NULL,
370                                 SPLPAR_CHARACTERISTICS_TOKEN,
371                                 __pa(rtas_data_buf),
372                                 RTAS_DATA_BUF_SIZE);
373         memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
374         spin_unlock(&rtas_data_buf_lock);
375
376         if (call_status != 0) {
377                 printk(KERN_INFO
378                        "%s %s Error calling get-system-parameter (0x%x)\n",
379                        __FILE__, __func__, call_status);
380         } else {
381                 int splpar_strlen;
382                 int idx, w_idx;
383                 char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
384                 if (!workbuffer) {
385                         printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
386                                __FILE__, __func__, __LINE__);
387                         kfree(local_buffer);
388                         return;
389                 }
390 #ifdef LPARCFG_DEBUG
391                 printk(KERN_INFO "success calling get-system-parameter \n");
392 #endif
393                 splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
394                 local_buffer += 2;      /* step over strlen value */
395
396                 w_idx = 0;
397                 idx = 0;
398                 while ((*local_buffer) && (idx < splpar_strlen)) {
399                         workbuffer[w_idx++] = local_buffer[idx++];
400                         if ((local_buffer[idx] == ',')
401                             || (local_buffer[idx] == '\0')) {
402                                 workbuffer[w_idx] = '\0';
403                                 if (w_idx) {
404                                         /* avoid the empty string */
405                                         seq_printf(m, "%s\n", workbuffer);
406                                 }
407                                 memset(workbuffer, 0, SPLPAR_MAXLENGTH);
408                                 idx++;  /* skip the comma */
409                                 w_idx = 0;
410                         } else if (local_buffer[idx] == '=') {
411                                 /* code here to replace workbuffer contents
412                                    with different keyword strings */
413                                 if (0 == strcmp(workbuffer, "MaxEntCap")) {
414                                         strcpy(workbuffer,
415                                                "partition_max_entitled_capacity");
416                                         w_idx = strlen(workbuffer);
417                                 }
418                                 if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
419                                         strcpy(workbuffer,
420                                                "system_potential_processors");
421                                         w_idx = strlen(workbuffer);
422                                 }
423                         }
424                 }
425                 kfree(workbuffer);
426                 local_buffer -= 2;      /* back up over strlen value */
427         }
428         kfree(local_buffer);
429 }
430
431 /* Return the number of processors in the system.
432  * This function reads through the device tree and counts
433  * the virtual processors, this does not include threads.
434  */
435 static int lparcfg_count_active_processors(void)
436 {
437         struct device_node *cpus_dn = NULL;
438         int count = 0;
439
440         while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
441 #ifdef LPARCFG_DEBUG
442                 printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
443 #endif
444                 count++;
445         }
446         return count;
447 }
448
449 static void pseries_cmo_data(struct seq_file *m)
450 {
451         int cpu;
452         unsigned long cmo_faults = 0;
453         unsigned long cmo_fault_time = 0;
454
455         seq_printf(m, "cmo_enabled=%d\n", firmware_has_feature(FW_FEATURE_CMO));
456
457         if (!firmware_has_feature(FW_FEATURE_CMO))
458                 return;
459
460         for_each_possible_cpu(cpu) {
461                 cmo_faults += lppaca[cpu].cmo_faults;
462                 cmo_fault_time += lppaca[cpu].cmo_fault_time;
463         }
464
465         seq_printf(m, "cmo_faults=%lu\n", cmo_faults);
466         seq_printf(m, "cmo_fault_time_usec=%lu\n",
467                    cmo_fault_time / tb_ticks_per_usec);
468         seq_printf(m, "cmo_primary_psp=%d\n", cmo_get_primary_psp());
469         seq_printf(m, "cmo_secondary_psp=%d\n", cmo_get_secondary_psp());
470         seq_printf(m, "cmo_page_size=%lu\n", cmo_get_page_size());
471 }
472
473 static void splpar_dispatch_data(struct seq_file *m)
474 {
475         int cpu;
476         unsigned long dispatches = 0;
477         unsigned long dispatch_dispersions = 0;
478
479         for_each_possible_cpu(cpu) {
480                 dispatches += lppaca[cpu].yield_count;
481                 dispatch_dispersions += lppaca[cpu].dispersion_count;
482         }
483
484         seq_printf(m, "dispatches=%lu\n", dispatches);
485         seq_printf(m, "dispatch_dispersions=%lu\n", dispatch_dispersions);
486 }
487
488 static int pseries_lparcfg_data(struct seq_file *m, void *v)
489 {
490         int partition_potential_processors;
491         int partition_active_processors;
492         struct device_node *rtas_node;
493         const int *lrdrp = NULL;
494
495         rtas_node = of_find_node_by_path("/rtas");
496         if (rtas_node)
497                 lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
498
499         if (lrdrp == NULL) {
500                 partition_potential_processors = vdso_data->processorCount;
501         } else {
502                 partition_potential_processors = *(lrdrp + 4);
503         }
504         of_node_put(rtas_node);
505
506         partition_active_processors = lparcfg_count_active_processors();
507
508         if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
509                 /* this call handles the ibm,get-system-parameter contents */
510                 parse_system_parameter_string(m);
511                 parse_ppp_data(m);
512                 parse_mpp_data(m);
513                 pseries_cmo_data(m);
514                 splpar_dispatch_data(m);
515
516                 seq_printf(m, "purr=%ld\n", get_purr());
517         } else {                /* non SPLPAR case */
518
519                 seq_printf(m, "system_active_processors=%d\n",
520                            partition_potential_processors);
521
522                 seq_printf(m, "system_potential_processors=%d\n",
523                            partition_potential_processors);
524
525                 seq_printf(m, "partition_max_entitled_capacity=%d\n",
526                            partition_potential_processors * 100);
527
528                 seq_printf(m, "partition_entitled_capacity=%d\n",
529                            partition_active_processors * 100);
530         }
531
532         seq_printf(m, "partition_active_processors=%d\n",
533                    partition_active_processors);
534
535         seq_printf(m, "partition_potential_processors=%d\n",
536                    partition_potential_processors);
537
538         seq_printf(m, "shared_processor_mode=%d\n", lppaca[0].shared_proc);
539
540         return 0;
541 }
542
543 static ssize_t update_ppp(u64 *entitlement, u8 *weight)
544 {
545         struct hvcall_ppp_data ppp_data;
546         u8 new_weight;
547         u64 new_entitled;
548         ssize_t retval;
549
550         /* Get our current parameters */
551         retval = h_get_ppp(&ppp_data);
552         if (retval)
553                 return retval;
554
555         if (entitlement) {
556                 new_weight = ppp_data.weight;
557                 new_entitled = *entitlement;
558         } else if (weight) {
559                 new_weight = *weight;
560                 new_entitled = ppp_data.entitlement;
561         } else
562                 return -EINVAL;
563
564         pr_debug("%s: current_entitled = %llu, current_weight = %u\n",
565                  __func__, ppp_data.entitlement, ppp_data.weight);
566
567         pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
568                  __func__, new_entitled, new_weight);
569
570         retval = plpar_hcall_norets(H_SET_PPP, new_entitled, new_weight);
571         return retval;
572 }
573
574 /**
575  * update_mpp
576  *
577  * Update the memory entitlement and weight for the partition.  Caller must
578  * specify either a new entitlement or weight, not both, to be updated
579  * since the h_set_mpp call takes both entitlement and weight as parameters.
580  */
581 static ssize_t update_mpp(u64 *entitlement, u8 *weight)
582 {
583         struct hvcall_mpp_data mpp_data;
584         u64 new_entitled;
585         u8 new_weight;
586         ssize_t rc;
587
588         if (entitlement) {
589                 /* Check with vio to ensure the new memory entitlement
590                  * can be handled.
591                  */
592                 rc = vio_cmo_entitlement_update(*entitlement);
593                 if (rc)
594                         return rc;
595         }
596
597         rc = h_get_mpp(&mpp_data);
598         if (rc)
599                 return rc;
600
601         if (entitlement) {
602                 new_weight = mpp_data.mem_weight;
603                 new_entitled = *entitlement;
604         } else if (weight) {
605                 new_weight = *weight;
606                 new_entitled = mpp_data.entitled_mem;
607         } else
608                 return -EINVAL;
609
610         pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
611                  __func__, mpp_data.entitled_mem, mpp_data.mem_weight);
612
613         pr_debug("%s: new_entitled = %llu, new_weight = %u\n",
614                  __func__, new_entitled, new_weight);
615
616         rc = plpar_hcall_norets(H_SET_MPP, new_entitled, new_weight);
617         return rc;
618 }
619
620 /*
621  * Interface for changing system parameters (variable capacity weight
622  * and entitled capacity).  Format of input is "param_name=value";
623  * anything after value is ignored.  Valid parameters at this time are
624  * "partition_entitled_capacity" and "capacity_weight".  We use
625  * H_SET_PPP to alter parameters.
626  *
627  * This function should be invoked only on systems with
628  * FW_FEATURE_SPLPAR.
629  */
630 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
631                              size_t count, loff_t * off)
632 {
633         int kbuf_sz = 64;
634         char kbuf[kbuf_sz];
635         char *tmp;
636         u64 new_entitled, *new_entitled_ptr = &new_entitled;
637         u8 new_weight, *new_weight_ptr = &new_weight;
638         ssize_t retval;
639
640         if (!firmware_has_feature(FW_FEATURE_SPLPAR) ||
641                         firmware_has_feature(FW_FEATURE_ISERIES))
642                 return -EINVAL;
643
644         if (count > kbuf_sz)
645                 return -EINVAL;
646
647         if (copy_from_user(kbuf, buf, count))
648                 return -EFAULT;
649
650         kbuf[count - 1] = '\0';
651         tmp = strchr(kbuf, '=');
652         if (!tmp)
653                 return -EINVAL;
654
655         *tmp++ = '\0';
656
657         if (!strcmp(kbuf, "partition_entitled_capacity")) {
658                 char *endp;
659                 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
660                 if (endp == tmp)
661                         return -EINVAL;
662
663                 retval = update_ppp(new_entitled_ptr, NULL);
664         } else if (!strcmp(kbuf, "capacity_weight")) {
665                 char *endp;
666                 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
667                 if (endp == tmp)
668                         return -EINVAL;
669
670                 retval = update_ppp(NULL, new_weight_ptr);
671         } else if (!strcmp(kbuf, "entitled_memory")) {
672                 char *endp;
673                 *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
674                 if (endp == tmp)
675                         return -EINVAL;
676
677                 retval = update_mpp(new_entitled_ptr, NULL);
678         } else if (!strcmp(kbuf, "entitled_memory_weight")) {
679                 char *endp;
680                 *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
681                 if (endp == tmp)
682                         return -EINVAL;
683
684                 retval = update_mpp(NULL, new_weight_ptr);
685         } else
686                 return -EINVAL;
687
688         if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
689                 retval = count;
690         } else if (retval == H_BUSY) {
691                 retval = -EBUSY;
692         } else if (retval == H_HARDWARE) {
693                 retval = -EIO;
694         } else if (retval == H_PARAMETER) {
695                 retval = -EINVAL;
696         }
697
698         return retval;
699 }
700
701 #else                           /* CONFIG_PPC_PSERIES */
702
703 static int pseries_lparcfg_data(struct seq_file *m, void *v)
704 {
705         return 0;
706 }
707
708 static ssize_t lparcfg_write(struct file *file, const char __user * buf,
709                              size_t count, loff_t * off)
710 {
711         return -EINVAL;
712 }
713
714 #endif                          /* CONFIG_PPC_PSERIES */
715
716 static int lparcfg_data(struct seq_file *m, void *v)
717 {
718         struct device_node *rootdn;
719         const char *model = "";
720         const char *system_id = "";
721         const char *tmp;
722         const unsigned int *lp_index_ptr;
723         unsigned int lp_index = 0;
724
725         seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
726
727         rootdn = of_find_node_by_path("/");
728         if (rootdn) {
729                 tmp = of_get_property(rootdn, "model", NULL);
730                 if (tmp) {
731                         model = tmp;
732                         /* Skip "IBM," - see platforms/iseries/dt.c */
733                         if (firmware_has_feature(FW_FEATURE_ISERIES))
734                                 model += 4;
735                 }
736                 tmp = of_get_property(rootdn, "system-id", NULL);
737                 if (tmp) {
738                         system_id = tmp;
739                         /* Skip "IBM," - see platforms/iseries/dt.c */
740                         if (firmware_has_feature(FW_FEATURE_ISERIES))
741                                 system_id += 4;
742                 }
743                 lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
744                                         NULL);
745                 if (lp_index_ptr)
746                         lp_index = *lp_index_ptr;
747                 of_node_put(rootdn);
748         }
749         seq_printf(m, "serial_number=%s\n", system_id);
750         seq_printf(m, "system_type=%s\n", model);
751         seq_printf(m, "partition_id=%d\n", (int)lp_index);
752
753         if (firmware_has_feature(FW_FEATURE_ISERIES))
754                 return iseries_lparcfg_data(m, v);
755         return pseries_lparcfg_data(m, v);
756 }
757
758 static int lparcfg_open(struct inode *inode, struct file *file)
759 {
760         return single_open(file, lparcfg_data, NULL);
761 }
762
763 static const struct file_operations lparcfg_fops = {
764         .owner          = THIS_MODULE,
765         .read           = seq_read,
766         .write          = lparcfg_write,
767         .open           = lparcfg_open,
768         .release        = single_release,
769 };
770
771 static int __init lparcfg_init(void)
772 {
773         struct proc_dir_entry *ent;
774         mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
775
776         /* Allow writing if we have FW_FEATURE_SPLPAR */
777         if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
778                         !firmware_has_feature(FW_FEATURE_ISERIES))
779                 mode |= S_IWUSR;
780
781         ent = proc_create("ppc64/lparcfg", mode, NULL, &lparcfg_fops);
782         if (!ent) {
783                 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
784                 return -EIO;
785         }
786
787         proc_ppc64_lparcfg = ent;
788         return 0;
789 }
790
791 static void __exit lparcfg_cleanup(void)
792 {
793         if (proc_ppc64_lparcfg)
794                 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
795 }
796
797 module_init(lparcfg_init);
798 module_exit(lparcfg_cleanup);
799 MODULE_DESCRIPTION("Interface for LPAR configuration data");
800 MODULE_AUTHOR("Dave Engebretsen");
801 MODULE_LICENSE("GPL");