ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / lparcfg.c
1 /*
2  * PowerPC64 LPAR Configuration Information Driver
3  *
4  * Dave Engebretsen engebret@us.ibm.com
5  *    Copyright (c) 2003 Dave Engebretsen
6  * Will Schmidt willschm@us.ibm.com
7  *    SPLPAR updates, Copyright (c) 2003 Will Schmidt IBM Corporation.
8  * Nathan Lynch nathanl@austin.ibm.com
9  *    Added lparcfg_write, Copyright (C) 2004 Nathan Lynch IBM Corporation.
10  *
11  *      This program is free software; you can redistribute it and/or
12  *      modify it under the terms of the GNU General Public License
13  *      as published by the Free Software Foundation; either version
14  *      2 of the License, or (at your option) any later version.
15  *
16  * This driver creates a proc file at /proc/ppc64/lparcfg which contains
17  * keyword - value pairs that specify the configuration of the partition.
18  */
19
20 #include <linux/config.h>
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 <asm/uaccess.h>
27 #include <asm/iSeries/HvLpConfig.h>
28 #include <asm/iSeries/ItLpPaca.h>
29 #include <asm/hvcall.h>
30 #include <asm/cputable.h>
31
32 #define MODULE_VERS "1.0"
33 #define MODULE_NAME "lparcfg"
34
35 static struct proc_dir_entry *proc_ppc64_lparcfg;
36 #define LPARCFG_BUFF_SIZE 4096
37
38 #ifdef CONFIG_PPC_ISERIES
39
40 #define lparcfg_write NULL
41
42 static unsigned char e2a(unsigned char x)
43 {
44         switch (x) {
45         case 0xF0:
46                 return '0';
47         case 0xF1:
48                 return '1';
49         case 0xF2:
50                 return '2';
51         case 0xF3:
52                 return '3';
53         case 0xF4:
54                 return '4';
55         case 0xF5:
56                 return '5';
57         case 0xF6:
58                 return '6';
59         case 0xF7:
60                 return '7';
61         case 0xF8:
62                 return '8';
63         case 0xF9:
64                 return '9';
65         case 0xC1:
66                 return 'A';
67         case 0xC2:
68                 return 'B';
69         case 0xC3:
70                 return 'C';
71         case 0xC4:
72                 return 'D';
73         case 0xC5:
74                 return 'E';
75         case 0xC6:
76                 return 'F';
77         case 0xC7:
78                 return 'G';
79         case 0xC8:
80                 return 'H';
81         case 0xC9:
82                 return 'I';
83         case 0xD1:
84                 return 'J';
85         case 0xD2:
86                 return 'K';
87         case 0xD3:
88                 return 'L';
89         case 0xD4:
90                 return 'M';
91         case 0xD5:
92                 return 'N';
93         case 0xD6:
94                 return 'O';
95         case 0xD7:
96                 return 'P';
97         case 0xD8:
98                 return 'Q';
99         case 0xD9:
100                 return 'R';
101         case 0xE2:
102                 return 'S';
103         case 0xE3:
104                 return 'T';
105         case 0xE4:
106                 return 'U';
107         case 0xE5:
108                 return 'V';
109         case 0xE6:
110                 return 'W';
111         case 0xE7:
112                 return 'X';
113         case 0xE8:
114                 return 'Y';
115         case 0xE9:
116                 return 'Z';
117         }
118         return ' ';
119 }
120
121 /* 
122  * Methods used to fetch LPAR data when running on an iSeries platform.
123  */
124 static int lparcfg_data(unsigned char *buf, unsigned long size)
125 {
126         unsigned long n = 0, pool_id, lp_index; 
127         int shared, entitled_capacity, max_entitled_capacity;
128         int processors, max_processors;
129         struct paca_struct *lpaca = get_paca();
130
131         if((buf == NULL) || (size > LPARCFG_BUFF_SIZE)) {
132                 return -EFAULT;
133         }
134         memset(buf, 0, size); 
135
136         shared = (int)(lpaca->xLpPacaPtr->xSharedProc);
137         n += scnprintf(buf, LPARCFG_BUFF_SIZE - n,
138                       "serial_number=%c%c%c%c%c%c%c\n", 
139                       e2a(xItExtVpdPanel.mfgID[2]),
140                       e2a(xItExtVpdPanel.mfgID[3]),
141                       e2a(xItExtVpdPanel.systemSerial[1]),
142                       e2a(xItExtVpdPanel.systemSerial[2]),
143                       e2a(xItExtVpdPanel.systemSerial[3]),
144                       e2a(xItExtVpdPanel.systemSerial[4]),
145                       e2a(xItExtVpdPanel.systemSerial[5])); 
146
147         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
148                       "system_type=%c%c%c%c\n",
149                       e2a(xItExtVpdPanel.machineType[0]),
150                       e2a(xItExtVpdPanel.machineType[1]),
151                       e2a(xItExtVpdPanel.machineType[2]),
152                       e2a(xItExtVpdPanel.machineType[3])); 
153
154         lp_index = HvLpConfig_getLpIndex(); 
155         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
156                       "partition_id=%d\n", (int)lp_index); 
157
158         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
159                       "system_active_processors=%d\n", 
160                       (int)HvLpConfig_getSystemPhysicalProcessors()); 
161
162         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
163                       "system_potential_processors=%d\n", 
164                       (int)HvLpConfig_getSystemPhysicalProcessors()); 
165
166         processors = (int)HvLpConfig_getPhysicalProcessors(); 
167         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
168                       "partition_active_processors=%d\n", processors);  
169
170         max_processors = (int)HvLpConfig_getMaxPhysicalProcessors(); 
171         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
172                       "partition_potential_processors=%d\n", max_processors);  
173
174         if(shared) {
175                 entitled_capacity = HvLpConfig_getSharedProcUnits(); 
176                 max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits(); 
177         } else {
178                 entitled_capacity = processors * 100; 
179                 max_entitled_capacity = max_processors * 100; 
180         }
181         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
182                       "partition_entitled_capacity=%d\n", entitled_capacity);
183
184         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
185                       "partition_max_entitled_capacity=%d\n", 
186                       max_entitled_capacity);
187
188         if(shared) {
189                 pool_id = HvLpConfig_getSharedPoolIndex(); 
190                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n, "pool=%d\n",
191                               (int)pool_id); 
192                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
193                               "pool_capacity=%d\n", (int)(HvLpConfig_getNumProcsInSharedPool(pool_id)*100)); 
194         }
195
196         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
197                       "shared_processor_mode=%d\n", shared);
198
199         return 0;
200 }
201 #endif /* CONFIG_PPC_ISERIES */
202
203 #ifdef CONFIG_PPC_PSERIES
204 /* 
205  * Methods used to fetch LPAR data when running on a pSeries platform.
206  */
207
208 /*
209  * H_GET_PPP hcall returns info in 4 parms.
210  *  entitled_capacity,unallocated_capacity,
211  *  aggregation, resource_capability).
212  *
213  *  R4 = Entitled Processor Capacity Percentage. 
214  *  R5 = Unallocated Processor Capacity Percentage.
215  *  R6 (AABBCCDDEEFFGGHH).
216  *      XXXX - reserved (0)
217  *          XXXX - reserved (0)
218  *              XXXX - Group Number
219  *                  XXXX - Pool Number.
220  *  R7 (PPOONNMMLLKKJJII)
221  *      XX - reserved. (0)
222  *        XX - bit 0-6 reserved (0).   bit 7 is Capped indicator.
223  *          XX - variable processor Capacity Weight
224  *            XX - Unallocated Variable Processor Capacity Weight.
225  *              XXXX - Active processors in Physical Processor Pool.
226  *                  XXXX  - Processors active on platform. 
227  */
228 unsigned int h_get_ppp(unsigned long *entitled,unsigned long  *unallocated,unsigned long *aggregation,unsigned long *resource)
229 {
230         unsigned long rc;
231         rc = plpar_hcall_4out(H_GET_PPP,0,0,0,0,entitled,unallocated,aggregation,resource);
232         return 0;
233 }
234
235 /*
236  * get_splpar_potential_characteristics().
237  * Retrieve the potential_processors and max_entitled_capacity values
238  * through the get-system-parameter rtas call.
239  */
240 #define SPLPAR_CHARACTERISTICS_TOKEN 20
241 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
242 unsigned int get_splpar_potential_characteristics(void)
243 {
244         /* return 0 for now.  Underlying rtas functionality is not yet complete. 12/01/2003*/
245         return 0; 
246 #if 0 
247         long call_status;
248         unsigned long ret[2];
249
250         char * buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
251
252         printk("token for ibm,get-system-parameter (0x%x)\n",rtas_token("ibm,get-system-parameter"));
253
254         call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
255                                 NULL,
256                                 SPLPAR_CHARACTERISTICS_TOKEN,
257                                 &buffer,
258                                 SPLPAR_MAXLENGTH,
259                                 (void *)&ret);
260
261         if (call_status!=0) {
262                 printk("Error calling get-system-parameter (0x%lx)\n",call_status);
263                 kfree(buffer);
264                 return -1;
265         } else {
266                 printk("get-system-parameter (%s)\n",buffer);
267                 kfree(buffer);
268                 /* TODO: Add code here to parse out value for system_potential_processors and partition_max_entitled_capacity */
269                 return 1;
270         }
271 #endif
272 }
273
274 static int lparcfg_data(unsigned char *buf, unsigned long size)
275 {
276         unsigned long n = 0;
277         int shared, max_entitled_capacity;
278         int processors, system_active_processors, system_potential_processors;
279         struct device_node *root;
280         const char *model = "";
281         const char *system_id = "";
282         unsigned int *lp_index_ptr, lp_index = 0;
283         struct device_node *rtas_node;
284         int *ip;
285         unsigned long h_entitled,h_unallocated,h_aggregation,h_resource;
286
287         if((buf == NULL) || (size > LPARCFG_BUFF_SIZE)) {
288                 return -EFAULT;
289         }
290         memset(buf, 0, size); 
291
292         root = find_path_device("/");
293         if (root) {
294                 model = get_property(root, "model", NULL);
295                 system_id = get_property(root, "system-id", NULL);
296                 lp_index_ptr = (unsigned int *)get_property(root, "ibm,partition-no", NULL);
297                 if(lp_index_ptr) lp_index = *lp_index_ptr;
298         }
299
300         n  = scnprintf(buf, LPARCFG_BUFF_SIZE - n,
301                       "serial_number=%s\n", system_id); 
302
303         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
304                       "system_type=%s\n", model); 
305
306         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
307                       "partition_id=%d\n", (int)lp_index); 
308
309         rtas_node = find_path_device("/rtas");
310         ip = (int *)get_property(rtas_node, "ibm,lrdr-capacity", NULL);
311         if (ip == NULL) {
312                 system_active_processors = systemcfg->processorCount; 
313         } else {
314                 system_active_processors = *(ip + 4);
315         }
316
317         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
318                 h_get_ppp(&h_entitled,&h_unallocated,&h_aggregation,&h_resource);
319 #ifdef DEBUG
320                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
321                               "R4=0x%lx\n", h_entitled);
322                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
323                               "R5=0x%lx\n", h_unallocated);
324                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
325                               "R6=0x%lx\n", h_aggregation);
326                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
327                               "R7=0x%lx\n", h_resource);
328 #endif /* DEBUG */
329         }
330
331         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
332                 system_potential_processors =  get_splpar_potential_characteristics();
333                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
334                               "system_active_processors=%ld\n", 
335                               (h_resource >> 2*8) & 0xffff);
336                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
337                               "system_potential_processors=%d\n", 
338                               system_potential_processors);
339         } else {
340                 system_potential_processors = system_active_processors;
341                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
342                               "system_active_processors=%d\n", 
343                               system_active_processors);
344                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
345                               "system_potential_processors=%d\n", 
346                               system_potential_processors);
347         }
348
349         processors = systemcfg->processorCount;
350         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
351                       "partition_active_processors=%d\n", processors);  
352         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
353                       "partition_potential_processors=%d\n",
354                       system_active_processors);
355
356         /* max_entitled_capacity will come out of get_splpar_potential_characteristics() when that function is complete */
357         max_entitled_capacity = system_active_processors * 100; 
358         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
359                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
360                               "partition_entitled_capacity=%ld\n", h_entitled);
361         } else {
362                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
363                               "partition_entitled_capacity=%d\n", system_active_processors*100);
364         }
365
366         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
367                       "partition_max_entitled_capacity=%d\n", 
368                       max_entitled_capacity);
369
370         shared = 0;
371         n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
372                       "shared_processor_mode=%d\n", shared);
373
374         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
375                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
376                               "pool=%ld\n", (h_aggregation >> 0*8)&0xffff);
377
378                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
379                               "pool_capacity=%ld\n", (h_resource >> 3*8) &0xffff);
380
381                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
382                               "group=%ld\n", (h_aggregation >> 2*8)&0xffff);
383
384                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
385                               "capped=%ld\n", (h_resource >> 6*8)&0x40);
386
387                 n += scnprintf(buf+n, LPARCFG_BUFF_SIZE - n,
388                               "capacity_weight=%d\n", (int)(h_resource>>5*8)&0xFF);
389         }
390         return 0;
391 }
392
393 /*
394  * Interface for changing system parameters (variable capacity weight
395  * and entitled capacity).  Format of input is "param_name=value";
396  * anything after value is ignored.  Valid parameters at this time are
397  * "partition_entitled_capacity" and "capacity_weight".  We use
398  * H_SET_PPP to alter parameters.
399  *
400  * This function should be invoked only on systems with
401  * FW_FEATURE_SPLPAR.
402  */
403 static ssize_t lparcfg_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
404 {
405         char *kbuf;
406         char *tmp;
407         u64 new_entitled, *new_entitled_ptr = &new_entitled;
408         u8 new_weight, *new_weight_ptr = &new_weight;
409
410         unsigned long current_entitled;    /* parameters for h_get_ppp */
411         unsigned long dummy;
412         unsigned long resource;
413         u8 current_weight;
414
415         ssize_t retval = -ENOMEM;
416
417         kbuf = kmalloc(count, GFP_KERNEL);
418         if (!kbuf)
419                 goto out;
420
421         retval = -EFAULT;
422         if (copy_from_user(kbuf, buf, count))
423                 goto out;
424
425         retval = -EINVAL;
426         kbuf[count - 1] = '\0';
427         tmp = strchr(kbuf, '=');
428         if (!tmp)
429                 goto out;
430
431         *tmp++ = '\0';
432
433         if (!strcmp(kbuf, "partition_entitled_capacity")) {
434                 char *endp;
435                 *new_entitled_ptr = (u64)simple_strtoul(tmp, &endp, 10);
436                 if (endp == tmp)
437                         goto out;
438                 new_weight_ptr = &current_weight;
439         } else if (!strcmp(kbuf, "capacity_weight")) {
440                 char *endp;
441                 *new_weight_ptr = (u8)simple_strtoul(tmp, &endp, 10);
442                 if (endp == tmp)
443                         goto out;
444                 new_entitled_ptr = &current_entitled;
445         } else
446                 goto out;
447
448         /* Get our current parameters */
449         retval = h_get_ppp(&current_entitled, &dummy, &dummy, &resource);
450         if (retval) {
451                 retval = -EIO;
452                 goto out;
453         }
454
455         current_weight = (resource>>5*8)&0xFF;
456
457         pr_debug("%s: current_entitled = %lu, current_weight = %lu\n",
458                  __FUNCTION__, current_entitled, current_weight);
459
460         pr_debug("%s: new_entitled = %lu, new_weight = %lu\n",
461                  __FUNCTION__, *new_entitled_ptr, *new_weight_ptr);
462
463         retval = plpar_hcall_norets(H_SET_PPP, *new_entitled_ptr,
464                                     *new_weight_ptr);
465
466         if (retval == H_Success || retval == H_Constrained) {
467                 retval = count;
468         } else if (retval == H_Busy) {
469                 retval = -EBUSY;
470         } else if (retval == H_Hardware) {
471                 retval = -EIO;
472         } else if (retval == H_Parameter) {
473                 retval = -EINVAL;
474         } else {
475                 printk(KERN_WARNING "%s: received unknown hv return code %ld",
476                        __FUNCTION__, retval);
477                 retval = -EIO;
478         }
479
480 out:
481         kfree(kbuf);
482         return retval;
483 }
484
485 #endif /* CONFIG_PPC_PSERIES */
486
487
488 static ssize_t lparcfg_read(struct file *file, char *buf,
489                             size_t count, loff_t *ppos)
490 {
491         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
492         unsigned long *data = (unsigned long *)dp->data;
493         unsigned long p;
494         ssize_t read;
495         char * pnt;
496
497         if (!data) {
498                 printk(KERN_ERR "lparcfg: read failed no data\n");
499                 return -EIO;
500         }
501
502         if(ppos) {
503                 p = *ppos;
504         } else {
505                 return -EFAULT;
506         }
507
508         if (p >= LPARCFG_BUFF_SIZE) return 0;
509
510         lparcfg_data((unsigned char *)data, LPARCFG_BUFF_SIZE); 
511         if (count > (strlen((char *)data) - p))
512                 count = (strlen((char *)data)) - p;
513         read = 0;
514
515         pnt = (char *)(data) + p;
516         copy_to_user(buf, (void *)pnt, count);
517         read += count;
518         *ppos += read;
519         return read;
520 }
521
522 static int lparcfg_open(struct inode * inode, struct file * file)
523 {
524         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
525         unsigned int *data = (unsigned int *)dp->data;
526
527         if (!data) {
528                 printk(KERN_ERR "lparcfg: open failed no data\n");
529                 return -EIO;
530         }
531
532         return 0;
533 }
534
535 struct file_operations lparcfg_fops = {
536         owner:          THIS_MODULE,
537         read:           lparcfg_read,
538         open:           lparcfg_open,
539 };
540
541 int __init lparcfg_init(void)
542 {
543         struct proc_dir_entry *ent;
544         mode_t mode = S_IRUSR;
545
546         /* Allow writing if we have FW_FEATURE_SPLPAR */
547         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
548                 lparcfg_fops.write = lparcfg_write;
549                 mode |= S_IWUSR;
550         }
551
552         ent = create_proc_entry("ppc64/lparcfg", mode, NULL);
553         if (ent) {
554                 ent->proc_fops = &lparcfg_fops;
555                 ent->data = kmalloc(LPARCFG_BUFF_SIZE, GFP_KERNEL);
556                 if (!ent->data) {
557                         printk(KERN_ERR "Failed to allocate buffer for lparcfg\n");
558                         remove_proc_entry("lparcfg", ent->parent);
559                         return -ENOMEM;
560                 }
561         } else {
562                 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
563                 return -EIO;
564         }
565
566         proc_ppc64_lparcfg = ent;
567         return 0;
568 }
569
570 void __exit lparcfg_cleanup(void)
571 {
572         if (proc_ppc64_lparcfg) {
573                 if (proc_ppc64_lparcfg->data) {
574                     kfree(proc_ppc64_lparcfg->data);
575                 }
576                 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
577         }
578 }
579
580 module_init(lparcfg_init);
581 module_exit(lparcfg_cleanup);
582 MODULE_DESCRIPTION("Interface for LPAR configuration data");
583 MODULE_AUTHOR("Dave Engebretsen");
584 MODULE_LICENSE("GPL");