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