Merge branch 'for-2.6.30' into for-2.6.31
[linux-2.6] / arch / s390 / kernel / sysinfo.c
1 /*
2  *  Copyright IBM Corp. 2001, 2009
3  *  Author(s): Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
4  *             Martin Schwidefsky <schwidefsky@de.ibm.com>,
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/mm.h>
9 #include <linux/proc_fs.h>
10 #include <linux/seq_file.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <asm/ebcdic.h>
15 #include <asm/sysinfo.h>
16 #include <asm/cpcmd.h>
17
18 /* Sigh, math-emu. Don't ask. */
19 #include <asm/sfp-util.h>
20 #include <math-emu/soft-fp.h>
21 #include <math-emu/single.h>
22
23 static inline int stsi_0(void)
24 {
25         int rc = stsi(NULL, 0, 0, 0);
26         return rc == -ENOSYS ? rc : (((unsigned int) rc) >> 28);
27 }
28
29 static int stsi_1_1_1(struct sysinfo_1_1_1 *info, char *page, int len)
30 {
31         if (stsi(info, 1, 1, 1) == -ENOSYS)
32                 return len;
33
34         EBCASC(info->manufacturer, sizeof(info->manufacturer));
35         EBCASC(info->type, sizeof(info->type));
36         EBCASC(info->model, sizeof(info->model));
37         EBCASC(info->sequence, sizeof(info->sequence));
38         EBCASC(info->plant, sizeof(info->plant));
39         EBCASC(info->model_capacity, sizeof(info->model_capacity));
40         EBCASC(info->model_perm_cap, sizeof(info->model_perm_cap));
41         EBCASC(info->model_temp_cap, sizeof(info->model_temp_cap));
42         len += sprintf(page + len, "Manufacturer:         %-16.16s\n",
43                        info->manufacturer);
44         len += sprintf(page + len, "Type:                 %-4.4s\n",
45                        info->type);
46         if (info->model[0] != '\0')
47                 /*
48                  * Sigh: the model field has been renamed with System z9
49                  * to model_capacity and a new model field has been added
50                  * after the plant field. To avoid confusing older programs
51                  * the "Model:" prints "model_capacity model" or just
52                  * "model_capacity" if the model string is empty .
53                  */
54                 len += sprintf(page + len,
55                                "Model:                %-16.16s %-16.16s\n",
56                                info->model_capacity, info->model);
57         else
58                 len += sprintf(page + len, "Model:                %-16.16s\n",
59                                info->model_capacity);
60         len += sprintf(page + len, "Sequence Code:        %-16.16s\n",
61                        info->sequence);
62         len += sprintf(page + len, "Plant:                %-4.4s\n",
63                        info->plant);
64         len += sprintf(page + len, "Model Capacity:       %-16.16s %08u\n",
65                        info->model_capacity, *(u32 *) info->model_cap_rating);
66         if (info->model_perm_cap[0] != '\0')
67                 len += sprintf(page + len,
68                                "Model Perm. Capacity: %-16.16s %08u\n",
69                                info->model_perm_cap,
70                                *(u32 *) info->model_perm_cap_rating);
71         if (info->model_temp_cap[0] != '\0')
72                 len += sprintf(page + len,
73                                "Model Temp. Capacity: %-16.16s %08u\n",
74                                info->model_temp_cap,
75                                *(u32 *) info->model_temp_cap_rating);
76         return len;
77 }
78
79 static int stsi_1_2_2(struct sysinfo_1_2_2 *info, char *page, int len)
80 {
81         struct sysinfo_1_2_2_extension *ext;
82         int i;
83
84         if (stsi(info, 1, 2, 2) == -ENOSYS)
85                 return len;
86         ext = (struct sysinfo_1_2_2_extension *)
87                 ((unsigned long) info + info->acc_offset);
88
89         len += sprintf(page + len, "\n");
90         len += sprintf(page + len, "CPUs Total:           %d\n",
91                        info->cpus_total);
92         len += sprintf(page + len, "CPUs Configured:      %d\n",
93                        info->cpus_configured);
94         len += sprintf(page + len, "CPUs Standby:         %d\n",
95                        info->cpus_standby);
96         len += sprintf(page + len, "CPUs Reserved:        %d\n",
97                        info->cpus_reserved);
98
99         if (info->format == 1) {
100                 /*
101                  * Sigh 2. According to the specification the alternate
102                  * capability field is a 32 bit floating point number
103                  * if the higher order 8 bits are not zero. Printing
104                  * a floating point number in the kernel is a no-no,
105                  * always print the number as 32 bit unsigned integer.
106                  * The user-space needs to know about the strange
107                  * encoding of the alternate cpu capability.
108                  */
109                 len += sprintf(page + len, "Capability:           %u %u\n",
110                                info->capability, ext->alt_capability);
111                 for (i = 2; i <= info->cpus_total; i++)
112                         len += sprintf(page + len,
113                                        "Adjustment %02d-way:    %u %u\n",
114                                        i, info->adjustment[i-2],
115                                        ext->alt_adjustment[i-2]);
116
117         } else {
118                 len += sprintf(page + len, "Capability:           %u\n",
119                                info->capability);
120                 for (i = 2; i <= info->cpus_total; i++)
121                         len += sprintf(page + len,
122                                        "Adjustment %02d-way:    %u\n",
123                                        i, info->adjustment[i-2]);
124         }
125
126         if (info->secondary_capability != 0)
127                 len += sprintf(page + len, "Secondary Capability: %d\n",
128                                info->secondary_capability);
129         return len;
130 }
131
132 static int stsi_2_2_2(struct sysinfo_2_2_2 *info, char *page, int len)
133 {
134         if (stsi(info, 2, 2, 2) == -ENOSYS)
135                 return len;
136
137         EBCASC(info->name, sizeof(info->name));
138
139         len += sprintf(page + len, "\n");
140         len += sprintf(page + len, "LPAR Number:          %d\n",
141                        info->lpar_number);
142
143         len += sprintf(page + len, "LPAR Characteristics: ");
144         if (info->characteristics & LPAR_CHAR_DEDICATED)
145                 len += sprintf(page + len, "Dedicated ");
146         if (info->characteristics & LPAR_CHAR_SHARED)
147                 len += sprintf(page + len, "Shared ");
148         if (info->characteristics & LPAR_CHAR_LIMITED)
149                 len += sprintf(page + len, "Limited ");
150         len += sprintf(page + len, "\n");
151
152         len += sprintf(page + len, "LPAR Name:            %-8.8s\n",
153                        info->name);
154
155         len += sprintf(page + len, "LPAR Adjustment:      %d\n",
156                        info->caf);
157
158         len += sprintf(page + len, "LPAR CPUs Total:      %d\n",
159                        info->cpus_total);
160         len += sprintf(page + len, "LPAR CPUs Configured: %d\n",
161                        info->cpus_configured);
162         len += sprintf(page + len, "LPAR CPUs Standby:    %d\n",
163                        info->cpus_standby);
164         len += sprintf(page + len, "LPAR CPUs Reserved:   %d\n",
165                        info->cpus_reserved);
166         len += sprintf(page + len, "LPAR CPUs Dedicated:  %d\n",
167                        info->cpus_dedicated);
168         len += sprintf(page + len, "LPAR CPUs Shared:     %d\n",
169                        info->cpus_shared);
170         return len;
171 }
172
173 static int stsi_3_2_2(struct sysinfo_3_2_2 *info, char *page, int len)
174 {
175         int i;
176
177         if (stsi(info, 3, 2, 2) == -ENOSYS)
178                 return len;
179         for (i = 0; i < info->count; i++) {
180                 EBCASC(info->vm[i].name, sizeof(info->vm[i].name));
181                 EBCASC(info->vm[i].cpi, sizeof(info->vm[i].cpi));
182                 len += sprintf(page + len, "\n");
183                 len += sprintf(page + len, "VM%02d Name:            %-8.8s\n",
184                                i, info->vm[i].name);
185                 len += sprintf(page + len, "VM%02d Control Program: %-16.16s\n",
186                                i, info->vm[i].cpi);
187
188                 len += sprintf(page + len, "VM%02d Adjustment:      %d\n",
189                                i, info->vm[i].caf);
190
191                 len += sprintf(page + len, "VM%02d CPUs Total:      %d\n",
192                                i, info->vm[i].cpus_total);
193                 len += sprintf(page + len, "VM%02d CPUs Configured: %d\n",
194                                i, info->vm[i].cpus_configured);
195                 len += sprintf(page + len, "VM%02d CPUs Standby:    %d\n",
196                                i, info->vm[i].cpus_standby);
197                 len += sprintf(page + len, "VM%02d CPUs Reserved:   %d\n",
198                                i, info->vm[i].cpus_reserved);
199         }
200         return len;
201 }
202
203 static int proc_read_sysinfo(char *page, char **start,
204                              off_t off, int count,
205                              int *eof, void *data)
206 {
207         unsigned long info = get_zeroed_page(GFP_KERNEL);
208         int level, len;
209
210         if (!info)
211                 return 0;
212
213         len = 0;
214         level = stsi_0();
215         if (level >= 1)
216                 len = stsi_1_1_1((struct sysinfo_1_1_1 *) info, page, len);
217
218         if (level >= 1)
219                 len = stsi_1_2_2((struct sysinfo_1_2_2 *) info, page, len);
220
221         if (level >= 2)
222                 len = stsi_2_2_2((struct sysinfo_2_2_2 *) info, page, len);
223
224         if (level >= 3)
225                 len = stsi_3_2_2((struct sysinfo_3_2_2 *) info, page, len);
226
227         free_page(info);
228         return len;
229 }
230
231 static __init int create_proc_sysinfo(void)
232 {
233         create_proc_read_entry("sysinfo", 0444, NULL,
234                                proc_read_sysinfo, NULL);
235         return 0;
236 }
237 device_initcall(create_proc_sysinfo);
238
239 /*
240  * Service levels interface.
241  */
242
243 static DECLARE_RWSEM(service_level_sem);
244 static LIST_HEAD(service_level_list);
245
246 int register_service_level(struct service_level *slr)
247 {
248         struct service_level *ptr;
249
250         down_write(&service_level_sem);
251         list_for_each_entry(ptr, &service_level_list, list)
252                 if (ptr == slr) {
253                         up_write(&service_level_sem);
254                         return -EEXIST;
255                 }
256         list_add_tail(&slr->list, &service_level_list);
257         up_write(&service_level_sem);
258         return 0;
259 }
260 EXPORT_SYMBOL(register_service_level);
261
262 int unregister_service_level(struct service_level *slr)
263 {
264         struct service_level *ptr, *next;
265         int rc = -ENOENT;
266
267         down_write(&service_level_sem);
268         list_for_each_entry_safe(ptr, next, &service_level_list, list) {
269                 if (ptr != slr)
270                         continue;
271                 list_del(&ptr->list);
272                 rc = 0;
273                 break;
274         }
275         up_write(&service_level_sem);
276         return rc;
277 }
278 EXPORT_SYMBOL(unregister_service_level);
279
280 static void *service_level_start(struct seq_file *m, loff_t *pos)
281 {
282         down_read(&service_level_sem);
283         return seq_list_start(&service_level_list, *pos);
284 }
285
286 static void *service_level_next(struct seq_file *m, void *p, loff_t *pos)
287 {
288         return seq_list_next(p, &service_level_list, pos);
289 }
290
291 static void service_level_stop(struct seq_file *m, void *p)
292 {
293         up_read(&service_level_sem);
294 }
295
296 static int service_level_show(struct seq_file *m, void *p)
297 {
298         struct service_level *slr;
299
300         slr = list_entry(p, struct service_level, list);
301         slr->seq_print(m, slr);
302         return 0;
303 }
304
305 static const struct seq_operations service_level_seq_ops = {
306         .start          = service_level_start,
307         .next           = service_level_next,
308         .stop           = service_level_stop,
309         .show           = service_level_show
310 };
311
312 static int service_level_open(struct inode *inode, struct file *file)
313 {
314         return seq_open(file, &service_level_seq_ops);
315 }
316
317 static const struct file_operations service_level_ops = {
318         .open           = service_level_open,
319         .read           = seq_read,
320         .llseek         = seq_lseek,
321         .release        = seq_release
322 };
323
324 static void service_level_vm_print(struct seq_file *m,
325                                    struct service_level *slr)
326 {
327         char *query_buffer, *str;
328
329         query_buffer = kmalloc(1024, GFP_KERNEL | GFP_DMA);
330         if (!query_buffer)
331                 return;
332         cpcmd("QUERY CPLEVEL", query_buffer, 1024, NULL);
333         str = strchr(query_buffer, '\n');
334         if (str)
335                 *str = 0;
336         seq_printf(m, "VM: %s\n", query_buffer);
337         kfree(query_buffer);
338 }
339
340 static struct service_level service_level_vm = {
341         .seq_print = service_level_vm_print
342 };
343
344 static __init int create_proc_service_level(void)
345 {
346         proc_create("service_levels", 0, NULL, &service_level_ops);
347         if (MACHINE_IS_VM)
348                 register_service_level(&service_level_vm);
349         return 0;
350 }
351 subsys_initcall(create_proc_service_level);
352
353 /*
354  * Bogomips calculation based on cpu capability.
355  */
356 int get_cpu_capability(unsigned int *capability)
357 {
358         struct sysinfo_1_2_2 *info;
359         int rc;
360
361         info = (void *) get_zeroed_page(GFP_KERNEL);
362         if (!info)
363                 return -ENOMEM;
364         rc = stsi(info, 1, 2, 2);
365         if (rc == -ENOSYS)
366                 goto out;
367         rc = 0;
368         *capability = info->capability;
369 out:
370         free_page((unsigned long) info);
371         return rc;
372 }
373
374 /*
375  * CPU capability might have changed. Therefore recalculate loops_per_jiffy.
376  */
377 void s390_adjust_jiffies(void)
378 {
379         struct sysinfo_1_2_2 *info;
380         const unsigned int fmil = 0x4b189680;   /* 1e7 as 32-bit float. */
381         FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);
382         FP_DECL_EX;
383         unsigned int capability;
384
385         info = (void *) get_zeroed_page(GFP_KERNEL);
386         if (!info)
387                 return;
388
389         if (stsi(info, 1, 2, 2) != -ENOSYS) {
390                 /*
391                  * Major sigh. The cpu capability encoding is "special".
392                  * If the first 9 bits of info->capability are 0 then it
393                  * is a 32 bit unsigned integer in the range 0 .. 2^23.
394                  * If the first 9 bits are != 0 then it is a 32 bit float.
395                  * In addition a lower value indicates a proportionally
396                  * higher cpu capacity. Bogomips are the other way round.
397                  * To get to a halfway suitable number we divide 1e7
398                  * by the cpu capability number. Yes, that means a floating
399                  * point division .. math-emu here we come :-)
400                  */
401                 FP_UNPACK_SP(SA, &fmil);
402                 if ((info->capability >> 23) == 0)
403                         FP_FROM_INT_S(SB, info->capability, 32, int);
404                 else
405                         FP_UNPACK_SP(SB, &info->capability);
406                 FP_DIV_S(SR, SA, SB);
407                 FP_TO_INT_S(capability, SR, 32, 0);
408         } else
409                 /*
410                  * Really old machine without stsi block for basic
411                  * cpu information. Report 42.0 bogomips.
412                  */
413                 capability = 42;
414         loops_per_jiffy = capability * (500000/HZ);
415         free_page((unsigned long) info);
416 }
417
418 /*
419  * calibrate the delay loop
420  */
421 void __cpuinit calibrate_delay(void)
422 {
423         s390_adjust_jiffies();
424         /* Print the good old Bogomips line .. */
425         printk(KERN_DEBUG "Calibrating delay loop (skipped)... "
426                "%lu.%02lu BogoMIPS preset\n", loops_per_jiffy/(500000/HZ),
427                (loops_per_jiffy/(5000/HZ)) % 100);
428 }