more changes on original files
[linux-2.4.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  *
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License
11  *      as published by the Free Software Foundation; either version
12  *      2 of the License, or (at your option) any later version.
13  *
14  * This driver creates a proc file at /proc/ppc64/lparcfg which contains
15  * keyword - value pairs that specify the configuration of the partition.
16  */
17
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/errno.h>
21 #include <linux/proc_fs.h>
22 #include <linux/init.h>
23 #include <asm/uaccess.h>
24 #include <asm/iSeries/HvLpConfig.h>
25 #include <asm/iSeries/ItLpPaca.h>
26 #include <asm/hvcall.h>
27 #include <asm/cputable.h>
28
29 #define MODULE_VERSION "1.0"
30 #define MODULE_NAME "lparcfg"
31
32 static struct proc_dir_entry *proc_ppc64_lparcfg;
33 #define LPARCFG_BUFF_SIZE 4096
34
35 #ifdef CONFIG_PPC_ISERIES
36 static unsigned char e2a(unsigned char x)
37 {
38         switch (x) {
39         case 0xF0:
40                 return '0';
41         case 0xF1:
42                 return '1';
43         case 0xF2:
44                 return '2';
45         case 0xF3:
46                 return '3';
47         case 0xF4:
48                 return '4';
49         case 0xF5:
50                 return '5';
51         case 0xF6:
52                 return '6';
53         case 0xF7:
54                 return '7';
55         case 0xF8:
56                 return '8';
57         case 0xF9:
58                 return '9';
59         case 0xC1:
60                 return 'A';
61         case 0xC2:
62                 return 'B';
63         case 0xC3:
64                 return 'C';
65         case 0xC4:
66                 return 'D';
67         case 0xC5:
68                 return 'E';
69         case 0xC6:
70                 return 'F';
71         case 0xC7:
72                 return 'G';
73         case 0xC8:
74                 return 'H';
75         case 0xC9:
76                 return 'I';
77         case 0xD1:
78                 return 'J';
79         case 0xD2:
80                 return 'K';
81         case 0xD3:
82                 return 'L';
83         case 0xD4:
84                 return 'M';
85         case 0xD5:
86                 return 'N';
87         case 0xD6:
88                 return 'O';
89         case 0xD7:
90                 return 'P';
91         case 0xD8:
92                 return 'Q';
93         case 0xD9:
94                 return 'R';
95         case 0xE2:
96                 return 'S';
97         case 0xE3:
98                 return 'T';
99         case 0xE4:
100                 return 'U';
101         case 0xE5:
102                 return 'V';
103         case 0xE6:
104                 return 'W';
105         case 0xE7:
106                 return 'X';
107         case 0xE8:
108                 return 'Y';
109         case 0xE9:
110                 return 'Z';
111         }
112         return ' ';
113 }
114
115 /*
116  * Methods used to fetch LPAR data when running on an iSeries platform.
117  */
118 static int lparcfg_data(unsigned char *buf, unsigned long size)
119 {
120         unsigned long n = 0, pool_id, lp_index;
121         int shared, entitled_capacity, max_entitled_capacity;
122         int processors, max_processors;
123         struct paca_struct *lpaca = get_paca();
124
125         if((buf == NULL) || (size > LPARCFG_BUFF_SIZE)) {
126                 return -EFAULT;
127         }
128         memset(buf, 0, size);
129
130         shared = (int)(lpaca->xLpPacaPtr->xSharedProc);
131         n += snprintf(buf, LPARCFG_BUFF_SIZE - n,
132                       "serial_number=%c%c%c%c%c%c%c\n",
133                       e2a(xItExtVpdPanel.mfgID[2]),
134                       e2a(xItExtVpdPanel.mfgID[3]),
135                       e2a(xItExtVpdPanel.systemSerial[1]),
136                       e2a(xItExtVpdPanel.systemSerial[2]),
137                       e2a(xItExtVpdPanel.systemSerial[3]),
138                       e2a(xItExtVpdPanel.systemSerial[4]),
139                       e2a(xItExtVpdPanel.systemSerial[5]));
140
141         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
142                       "system_type=%c%c%c%c\n",
143                       e2a(xItExtVpdPanel.machineType[0]),
144                       e2a(xItExtVpdPanel.machineType[1]),
145                       e2a(xItExtVpdPanel.machineType[2]),
146                       e2a(xItExtVpdPanel.machineType[3]));
147
148         lp_index = HvLpConfig_getLpIndex();
149         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
150                       "partition_id=%d\n", (int)lp_index);
151
152         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
153                       "system_active_processors=%d\n",
154                       (int)HvLpConfig_getSystemPhysicalProcessors());
155
156         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
157                       "system_potential_processors=%d\n",
158                       (int)HvLpConfig_getSystemPhysicalProcessors());
159
160         processors = (int)HvLpConfig_getPhysicalProcessors();
161         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
162                       "partition_active_processors=%d\n", processors);
163
164         max_processors = (int)HvLpConfig_getMaxPhysicalProcessors();
165         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
166                       "partition_potential_processors=%d\n", max_processors);
167
168         if(shared) {
169                 entitled_capacity = HvLpConfig_getSharedProcUnits();
170                 max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits();
171         } else {
172                 entitled_capacity = processors * 100;
173                 max_entitled_capacity = max_processors * 100;
174         }
175         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
176                       "partition_entitled_capacity=%d\n", entitled_capacity);
177
178         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
179                       "partition_max_entitled_capacity=%d\n",
180                       max_entitled_capacity);
181
182         if(shared) {
183                 pool_id = HvLpConfig_getSharedPoolIndex();
184                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n, "pool=%d\n",
185                               (int)pool_id);
186                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
187                               "pool_capacity=%d\n", (int)(HvLpConfig_getNumProcsInSharedPool(pool_id)*100));
188         }
189
190         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
191                       "shared_processor_mode=%d\n", shared);
192
193         return 0;
194 }
195 #endif /* CONFIG_PPC_ISERIES */
196
197 #ifdef CONFIG_PPC_PSERIES
198 /*
199  * Methods used to fetch LPAR data when running on a pSeries platform.
200  */
201
202 /*
203  * H_GET_PPP hcall returns info in 4 parms.
204  *  entitled_capacity,unallocated_capacity,
205  *  aggregation, resource_capability).
206  *
207  *  R4 = Entitled Processor Capacity Percentage.
208  *  R5 = Unallocated Processor Capacity Percentage.
209  *  R6 (AABBCCDDEEFFGGHH).
210  *      XXXX - reserved (0)
211  *          XXXX - reserved (0)
212  *              XXXX - Group Number
213  *                  XXXX - Pool Number.
214  *  R7 (PPOONNMMLLKKJJII)
215  *      XX - reserved. (0)
216  *        XX - bit 0-6 reserved (0).   bit 7 is Capped indicator.
217  *          XX - variable processor Capacity Weight
218  *            XX - Unallocated Variable Processor Capacity Weight.
219  *              XXXX - Active processors in Physical Processor Pool.
220  *                  XXXX  - Processors active on platform.
221  */
222 unsigned int h_get_ppp(unsigned long *entitled,unsigned long  *unallocated,unsigned long *aggregation,unsigned long *resource)
223 {
224         unsigned long rc;
225         rc = plpar_hcall_4out(H_GET_PPP,0,0,0,0,entitled,unallocated,aggregation,resource);
226         return 0;
227 }
228
229 /*
230  * get_splpar_potential_characteristics().
231  * Retrieve the potential_processors and max_entitled_capacity values
232  * through the get-system-parameter rtas call.
233  */
234 #define SPLPAR_CHARACTERISTICS_TOKEN 20
235 #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
236 unsigned int get_splpar_potential_characteristics()
237 {
238         /* return 0 for now.  Underlying rtas functionality is not yet complete. 12/01/2003*/
239         return 0;
240 #if 0
241         long call_status;
242         unsigned long ret[2];
243
244         char * buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
245
246         printk("token for ibm,get-system-parameter (0x%x)\n",rtas_token("ibm,get-system-parameter"));
247
248         call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
249                                 NULL,
250                                 SPLPAR_CHARACTERISTICS_TOKEN,
251                                 &buffer,
252                                 SPLPAR_MAXLENGTH,
253                                 (void *)&ret);
254
255         if (call_status!=0) {
256                 printk("Error calling get-system-parameter (0x%lx)\n",call_status);
257                 kfree(buffer);
258                 return -1;
259         } else {
260                 printk("get-system-parameter (%s)\n",buffer);
261                 kfree(buffer);
262                 /* TODO: Add code here to parse out value for system_potential_processors and partition_max_entitled_capacity */
263                 return 1;
264         }
265 #endif
266 }
267
268 static int lparcfg_data(unsigned char *buf, unsigned long size)
269 {
270         unsigned long n = 0;
271         int shared, max_entitled_capacity;
272         int processors, system_active_processors, system_potential_processors;
273         struct device_node *root;
274         const char *model = "";
275         const char *system_id = "";
276         unsigned int *lp_index_ptr, lp_index = 0;
277         struct device_node *rtas_node;
278         int *ip;
279         unsigned long h_entitled,h_unallocated,h_aggregation,h_resource;
280
281         if((buf == NULL) || (size > LPARCFG_BUFF_SIZE)) {
282                 return -EFAULT;
283         }
284         memset(buf, 0, size);
285
286         root = find_path_device("/");
287         if (root) {
288                 model = get_property(root, "model", NULL);
289                 system_id = get_property(root, "system-id", NULL);
290                 lp_index_ptr = (unsigned int *)get_property(root, "ibm,partition-no", NULL);
291                 if(lp_index_ptr) lp_index = *lp_index_ptr;
292         }
293
294         n  = snprintf(buf, LPARCFG_BUFF_SIZE - n,
295                       "serial_number=%s\n", system_id);
296
297         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
298                       "system_type=%s\n", model);
299
300         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
301                       "partition_id=%d\n", (int)lp_index);
302
303         rtas_node = find_path_device("/rtas");
304         ip = (int *)get_property(rtas_node, "ibm,lrdr-capacity", NULL);
305         if (ip == NULL) {
306                 system_active_processors = systemcfg->processorCount;
307         } else {
308                 system_active_processors = *(ip + 4);
309         }
310
311         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
312                 h_get_ppp(&h_entitled,&h_unallocated,&h_aggregation,&h_resource);
313 #ifdef DEBUG
314                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
315                               "R4=0x%lx\n", h_entitled);
316                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
317                               "R5=0x%lx\n", h_unallocated);
318                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
319                               "R6=0x%lx\n", h_aggregation);
320                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
321                               "R7=0x%lx\n", h_resource);
322 #endif /* DEBUG */
323         }
324
325         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
326                 system_potential_processors =  get_splpar_potential_characteristics();
327                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
328                               "system_active_processors=%d\n",
329                               (h_resource >> 2*8) & 0xffff);
330                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
331                               "system_potential_processors=%d\n",
332                               system_potential_processors);
333         } else {
334                 system_potential_processors = system_active_processors;
335                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
336                               "system_active_processors=%d\n",
337                               system_active_processors);
338                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
339                               "system_potential_processors=%d\n",
340                               system_potential_processors);
341         }
342
343         processors = systemcfg->processorCount;
344         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
345                       "partition_active_processors=%d\n", processors);
346         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
347                       "partition_potential_processors=%d\n",
348                       system_active_processors);
349
350         max_entitled_capacity = system_active_processors * 100;
351         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
352                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
353                               "partition_entitled_capacity=%ld\n", h_entitled);
354         } else {
355                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
356                               "partition_entitled_capacity=%d\n", system_active_processors*100);
357         }
358
359         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
360                       "partition_max_entitled_capacity=%d\n",
361                       max_entitled_capacity);
362
363         shared = 0;
364         n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
365                       "shared_processor_mode=%d\n", shared);
366
367         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
368                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
369                               "pool=%d\n", (h_aggregation >> 0*8)&0xffff);
370
371                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
372                               "pool_capacity=%d\n", (h_resource >> 3*8) &0xffff);
373
374                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
375                               "group=%d\n", (h_aggregation >> 2*8)&0xffff);
376
377                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
378                               "capped=%d\n", (h_resource >> 6*8)&0x40);
379
380                 n += snprintf(buf+n, LPARCFG_BUFF_SIZE - n,
381                               "capacity_weight=%d\n", (int)(h_resource>>5*8)&0xFF);
382         }
383         return 0;
384 }
385 #endif /* CONFIG_PPC_PSERIES */
386
387
388 static ssize_t lparcfg_read(struct file *file, char *buf,
389                             size_t count, loff_t *ppos)
390 {
391         struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
392         unsigned long *data = (unsigned long *)dp->data;
393         unsigned long p;
394         ssize_t read;
395         char * pnt;
396
397         if (!data) {
398                 printk(KERN_ERR "lparcfg: read failed no data\n");
399                 return -EIO;
400         }
401
402         if(ppos) {
403                 p = *ppos;
404         } else {
405                 return -EFAULT;
406         }
407
408         if (p >= LPARCFG_BUFF_SIZE) return 0;
409
410         lparcfg_data((unsigned char *)data, LPARCFG_BUFF_SIZE);
411         if (count > (strlen((char *)data) - p))
412                 count = (strlen((char *)data)) - p;
413         read = 0;
414
415         pnt = (char *)(data) + p;
416         copy_to_user(buf, (void *)pnt, count);
417         read += count;
418         *ppos = p + read;
419         return read;
420 }
421
422 static int lparcfg_open(struct inode * inode, struct file * file)
423 {
424         struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
425         unsigned int *data = (unsigned int *)dp->data;
426
427         if (!data) {
428                 printk(KERN_ERR "lparcfg: open failed no data\n");
429                 return -EIO;
430         }
431
432         return 0;
433 }
434
435 struct file_operations lparcfg_fops = {
436         owner:          THIS_MODULE,
437         read:           lparcfg_read,
438         open:           lparcfg_open,
439 };
440
441 int __init lparcfg_init(void)
442 {
443         struct proc_dir_entry *ent;
444
445         ent = create_proc_entry("ppc64/lparcfg", S_IRUSR, NULL);
446         if (ent) {
447                 ent->proc_fops = &lparcfg_fops;
448                 ent->data = kmalloc(LPARCFG_BUFF_SIZE, GFP_KERNEL);
449                 if (!ent->data) {
450                         printk(KERN_ERR "Failed to allocate buffer for lparcfg\n");
451                         remove_proc_entry("lparcfg", ent->parent);
452                         return -ENOMEM;
453                 }
454         } else {
455                 printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
456                 return -EIO;
457         }
458
459         proc_ppc64_lparcfg = ent;
460         return 0;
461 }
462
463 void __exit lparcfg_cleanup(void)
464 {
465         if (proc_ppc64_lparcfg) {
466                 if (proc_ppc64_lparcfg->data) {
467                     kfree(proc_ppc64_lparcfg->data);
468                 }
469                 remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
470         }
471 }
472
473 module_init(lparcfg_init);
474 module_exit(lparcfg_cleanup);
475 MODULE_DESCRIPTION("Interface for LPAR configuration data");
476 MODULE_AUTHOR("Dave Engebretsen");
477 MODULE_LICENSE("GPL");